1 //
2 //  Copyright (C) 2015 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 "service/common/bluetooth/scan_filter.h"
18 
19 #include "service/common/bluetooth/util/address_helper.h"
20 
21 namespace bluetooth {
22 
ScanFilter(const ScanFilter & other)23 ScanFilter::ScanFilter(const ScanFilter& other) {
24   device_name_ = other.device_name_;
25   device_address_ = other.device_address_;
26 
27   if (other.service_uuid_) service_uuid_.reset(new UUID(*other.service_uuid_));
28 
29   if (other.service_uuid_mask_)
30     service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_));
31 }
32 
operator =(const ScanFilter & other)33 ScanFilter& ScanFilter::operator=(const ScanFilter& other) {
34   device_name_ = other.device_name_;
35   device_address_ = other.device_address_;
36 
37   if (other.service_uuid_)
38     service_uuid_.reset(new UUID(*other.service_uuid_));
39   else
40     service_uuid_ = nullptr;
41 
42   if (other.service_uuid_mask_)
43     service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_));
44   else
45     service_uuid_mask_ = nullptr;
46 
47   return *this;
48 }
49 
SetDeviceAddress(const std::string & device_address)50 bool ScanFilter::SetDeviceAddress(const std::string& device_address) {
51   if (!util::IsAddressValid(device_address)) return false;
52 
53   device_address_ = device_address;
54   return true;
55 }
56 
SetServiceUuid(const UUID & service_uuid)57 void ScanFilter::SetServiceUuid(const UUID& service_uuid) {
58   service_uuid_.reset(new UUID(service_uuid));
59   service_uuid_mask_.reset();
60 }
61 
SetServiceUuidWithMask(const UUID & service_uuid,const UUID & mask)62 void ScanFilter::SetServiceUuidWithMask(const UUID& service_uuid,
63                                         const UUID& mask) {
64   service_uuid_.reset(new UUID(service_uuid));
65   service_uuid_mask_.reset(new UUID(mask));
66 }
67 
operator ==(const ScanFilter & rhs) const68 bool ScanFilter::operator==(const ScanFilter& rhs) const {
69   if (device_name_ != rhs.device_name_) return false;
70 
71   if (device_address_ != rhs.device_address_) return false;
72 
73   // Both must be either NULL or non-NULL. If only one of them is NULL, then
74   // return false.
75   if (!!service_uuid_ != !!rhs.service_uuid_) return false;
76 
77   if (service_uuid_ && rhs.service_uuid_ &&
78       *service_uuid_ != *rhs.service_uuid_)
79     return false;
80 
81   // Both must be either NULL or non-NULL. If only one of them is NULL, then
82   // return false.
83   if (!!service_uuid_mask_ != !!rhs.service_uuid_mask_) return false;
84 
85   if (service_uuid_mask_ && rhs.service_uuid_mask_ &&
86       *service_uuid_mask_ != *rhs.service_uuid_mask_)
87     return false;
88 
89   return true;
90 }
91 
92 }  // namespace bluetooth
93