1 //
2 //  Copyright 2017, The Android Open Source Project
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/os/parcel_uuid.h"
18 
19 using android::OK;
20 using android::Parcel;
21 using android::status_t;
22 using ::bluetooth::Uuid;
23 
24 namespace android {
25 namespace os {
26 
27 namespace {
uuid_lsb(const Uuid & uuid)28 static uint64_t uuid_lsb(const Uuid& uuid) {
29   uint64_t lsb = 0;
30 
31   auto uu = uuid.To128BitBE();
32   for (int i = 8; i <= 15; i++) {
33     lsb <<= 8;
34     lsb |= uu[i];
35   }
36 
37   return lsb;
38 }
39 
uuid_msb(const Uuid & uuid)40 static uint64_t uuid_msb(const Uuid& uuid) {
41   uint64_t msb = 0;
42 
43   auto uu = uuid.To128BitBE();
44   for (int i = 0; i <= 7; i++) {
45     msb <<= 8;
46     msb |= uu[i];
47   }
48 
49   return msb;
50 }
51 }  // namespace
52 
writeToParcel(Parcel * parcel) const53 status_t ParcelUuid::writeToParcel(Parcel* parcel) const {
54   status_t status = parcel->writeInt64(uuid_msb(uuid));
55   if (status != OK) return status;
56 
57   status = parcel->writeInt64(uuid_lsb(uuid));
58   return status;
59 }
60 
readFromParcel(const Parcel * parcel)61 status_t ParcelUuid::readFromParcel(const Parcel* parcel) {
62   int64_t uuid_msb, uuid_lsb;
63 
64   status_t status = parcel->readInt64(&uuid_msb);
65   if (status != OK) return status;
66 
67   status = parcel->readInt64(&uuid_lsb);
68   if (status != OK) return status;
69 
70   std::array<uint8_t, Uuid::kNumBytes128> uu;
71   for (int i = 0; i < 8; i++) {
72     uu[7 - i] = (uuid_msb >> (8 * i)) & 0xFF;
73     uu[15 - i] = (uuid_lsb >> (8 * i)) & 0xFF;
74   }
75 
76   uuid = Uuid::From128BitBE(uu);
77   return OK;
78 }
79 
80 }  // namespace os
81 }  // namespace android