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/scan_filter.h"
18 #include "android/bluetooth/uuid.h"
19 
20 #include <binder/Parcel.h>
21 #include <utils/String16.h>
22 #include <utils/String8.h>
23 
24 using android::Parcelable;
25 using android::Parcel;
26 using android::String8;
27 using android::String16;
28 using android::status_t;
29 using android::OK;
30 
31 namespace android {
32 namespace bluetooth {
33 
writeToParcel(Parcel * parcel) const34 status_t ScanFilter::writeToParcel(Parcel* parcel) const {
35   status_t status =
36       parcel->writeString16(String16(String8(device_name_.c_str())));
37   if (status != OK) return status;
38 
39   status = parcel->writeString16(String16(String8(device_address_.c_str())));
40   if (status != OK) return status;
41 
42   // TODO(jpawlowski) make type casting nicer
43   // uuid won't really keep ownership, it's just for type casting
44   std::optional<UUID> uuid;
45 
46   if (service_uuid_) {
47     uuid = *service_uuid_;
48   }
49   status = parcel->writeNullableParcelable(uuid);
50   if (status != OK) return status;
51 
52   uuid.reset();
53   if (service_uuid_mask_) {
54     uuid = *service_uuid_mask_;
55   }
56   status = parcel->writeNullableParcelable(uuid);
57   return status;
58 }
59 
readFromParcel(const Parcel * parcel)60 status_t ScanFilter::readFromParcel(const Parcel* parcel) {
61   String16 name;
62   status_t status = parcel->readString16(&name);
63   if (status != OK) return status;
64   device_name_ = std::string(String8(name).string());
65 
66   String16 addr;
67   status = parcel->readString16(&addr);
68   if (status != OK) return status;
69   device_address_ = std::string(String8(addr).string());
70 
71   UUID uuid;
72   status = parcel->readParcelable(&uuid);
73   if (status != OK) return status;
74   service_uuid_.reset(new ::bluetooth::Uuid(uuid.uuid));
75 
76   status = parcel->readParcelable(&uuid);
77   if (status != OK) return status;
78   service_uuid_mask_.reset(new ::bluetooth::Uuid(uuid.uuid));
79 
80   return status;
81 }
82 
83 }  // namespace bluetooth
84 }  // namespace android
85