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) const34status_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::unique_ptr<UUID> uuid; 45 UUID tmp; 46 47 if (service_uuid_) { 48 tmp = *service_uuid_; 49 uuid.reset(&tmp); 50 } else { 51 uuid.reset(nullptr); 52 } 53 status = parcel->writeNullableParcelable(uuid); 54 uuid.release(); 55 if (status != OK) return status; 56 57 if (service_uuid_mask_) { 58 tmp = *service_uuid_mask_; 59 uuid.reset(&tmp); 60 } else { 61 uuid.reset(nullptr); 62 } 63 status = parcel->writeNullableParcelable(uuid); 64 uuid.release(); 65 66 return status; 67 } 68 readFromParcel(const Parcel * parcel)69status_t ScanFilter::readFromParcel(const Parcel* parcel) { 70 String16 name; 71 status_t status = parcel->readString16(&name); 72 if (status != OK) return status; 73 device_name_ = std::string(String8(name).string()); 74 75 String16 addr; 76 status = parcel->readString16(&addr); 77 if (status != OK) return status; 78 device_address_ = std::string(String8(addr).string()); 79 80 UUID uuid; 81 status = parcel->readParcelable(&uuid); 82 if (status != OK) return status; 83 service_uuid_.reset(new ::bluetooth::Uuid(uuid.uuid)); 84 85 status = parcel->readParcelable(&uuid); 86 if (status != OK) return status; 87 service_uuid_mask_.reset(new ::bluetooth::Uuid(uuid.uuid)); 88 89 return status; 90 } 91 92 } // namespace bluetooth 93 } // namespace android 94