Home | History | Annotate | Download | only in bluetooth
      1 //
      2 //  Copyright 2016 Google, Inc.
      3 //
      4 //  Licensed under the Apache License, Version 2.0 (the "License");
      5 //  you may not use this file except in compliance with the License.
      6 //  You may obtain a copy of the License at:
      7 //
      8 //  http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 //  Unless required by applicable law or agreed to in writing, software
     11 //  distributed under the License is distributed on an "AS IS" BASIS,
     12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 //  See the License for the specific language governing permissions and
     14 //  limitations under the License.
     15 //
     16 
     17 #include "android/bluetooth/uuid.h"
     18 
     19 #include <binder/Parcel.h>
     20 
     21 using android::Parcelable;
     22 using android::Parcel;
     23 using android::String16;
     24 using android::status_t;
     25 using android::OK;
     26 
     27 namespace android {
     28 namespace bluetooth {
     29 
     30 status_t UUID::writeToParcel(Parcel* parcel) const {
     31   // The scheme used by android.os.ParcelUuid is to wrote the most significant
     32   // bits first as one 64-bit integer, followed by the least significant bits in
     33   // a second 64-bit integer. This is the same as writing the raw-bytes in
     34   // sequence, but we don't want to assume any host-endianness here. So follow
     35   // the same scheme and use the same Parcel APIs.
     36   ::bluetooth::Uuid::UUID128Bit bytes = uuid.To128BitBE();
     37 
     38   uint64_t most_sig_bits =
     39       ((((uint64_t)bytes[0]) << 56) | (((uint64_t)bytes[1]) << 48) |
     40        (((uint64_t)bytes[2]) << 40) | (((uint64_t)bytes[3]) << 32) |
     41        (((uint64_t)bytes[4]) << 24) | (((uint64_t)bytes[5]) << 16) |
     42        (((uint64_t)bytes[6]) << 8) | bytes[7]);
     43 
     44   uint64_t least_sig_bits =
     45       ((((uint64_t)bytes[8]) << 56) | (((uint64_t)bytes[9]) << 48) |
     46        (((uint64_t)bytes[10]) << 40) | (((uint64_t)bytes[11]) << 32) |
     47        (((uint64_t)bytes[12]) << 24) | (((uint64_t)bytes[13]) << 16) |
     48        (((uint64_t)bytes[14]) << 8) | bytes[15]);
     49 
     50   status_t status = parcel->writeUint64(most_sig_bits);
     51   if (status != OK) return status;
     52 
     53   status = parcel->writeUint64(least_sig_bits);
     54   return status;
     55 }
     56 
     57 status_t UUID::readFromParcel(const Parcel* parcel) {
     58   ::bluetooth::Uuid::UUID128Bit bytes;
     59 
     60   uint64_t most_sig_bits, least_sig_bits;
     61   status_t status = parcel->readUint64(&most_sig_bits);
     62   if (status != OK) return status;
     63 
     64   status = parcel->readUint64(&least_sig_bits);
     65   if (status != OK) return status;
     66 
     67   bytes[0] = (most_sig_bits >> 56) & 0xFF;
     68   bytes[1] = (most_sig_bits >> 48) & 0xFF;
     69   bytes[2] = (most_sig_bits >> 40) & 0xFF;
     70   bytes[3] = (most_sig_bits >> 32) & 0xFF;
     71   bytes[4] = (most_sig_bits >> 24) & 0xFF;
     72   bytes[5] = (most_sig_bits >> 16) & 0xFF;
     73   bytes[6] = (most_sig_bits >> 8) & 0xFF;
     74   bytes[7] = most_sig_bits & 0xFF;
     75 
     76   bytes[8] = (least_sig_bits >> 56) & 0xFF;
     77   bytes[9] = (least_sig_bits >> 48) & 0xFF;
     78   bytes[10] = (least_sig_bits >> 40) & 0xFF;
     79   bytes[11] = (least_sig_bits >> 32) & 0xFF;
     80   bytes[12] = (least_sig_bits >> 24) & 0xFF;
     81   bytes[13] = (least_sig_bits >> 16) & 0xFF;
     82   bytes[14] = (least_sig_bits >> 8) & 0xFF;
     83   bytes[15] = least_sig_bits & 0xFF;
     84 
     85   uuid = ::bluetooth::Uuid::From128BitBE(bytes);
     86   return status;
     87 }
     88 
     89 }  // namespace bluetooth
     90 }  // namespace android
     91