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_)
28     service_uuid_.reset(new UUID(*other.service_uuid_));
29 
30   if (other.service_uuid_mask_)
31     service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_));
32 }
33 
operator =(const ScanFilter & other)34 ScanFilter& ScanFilter::operator=(const ScanFilter& other) {
35   device_name_ = other.device_name_;
36   device_address_ = other.device_address_;
37 
38   if (other.service_uuid_)
39     service_uuid_.reset(new UUID(*other.service_uuid_));
40   else
41     service_uuid_ = nullptr;
42 
43   if (other.service_uuid_mask_)
44     service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_));
45   else
46     service_uuid_mask_ = nullptr;
47 
48   return *this;
49 }
50 
SetDeviceAddress(const std::string & device_address)51 bool ScanFilter::SetDeviceAddress(const std::string& device_address) {
52   if (!util::IsAddressValid(device_address))
53     return false;
54 
55   device_address_ = device_address;
56   return true;
57 }
58 
SetServiceUuid(const UUID & service_uuid)59 void ScanFilter::SetServiceUuid(const UUID& service_uuid) {
60   service_uuid_.reset(new UUID(service_uuid));
61   service_uuid_mask_.reset();
62 }
63 
SetServiceUuidWithMask(const UUID & service_uuid,const UUID & mask)64 void ScanFilter::SetServiceUuidWithMask(const UUID& service_uuid,
65                                         const UUID& mask) {
66   service_uuid_.reset(new UUID(service_uuid));
67   service_uuid_mask_.reset(new UUID(mask));
68 }
69 
operator ==(const ScanFilter & rhs) const70 bool ScanFilter::operator==(const ScanFilter& rhs) const {
71   if (device_name_ != rhs.device_name_)
72     return false;
73 
74   if (device_address_ != rhs.device_address_)
75     return false;
76 
77   // Both must be either NULL or non-NULL. If only one of them is NULL, then
78   // return false.
79   if (!!service_uuid_ != !!rhs.service_uuid_)
80     return false;
81 
82   if (service_uuid_ && rhs.service_uuid_ &&
83       *service_uuid_ != *rhs.service_uuid_)
84     return false;
85 
86   // Both must be either NULL or non-NULL. If only one of them is NULL, then
87   // return false.
88   if (!!service_uuid_mask_ != !!rhs.service_uuid_mask_)
89     return false;
90 
91   if (service_uuid_mask_ && rhs.service_uuid_mask_ &&
92       *service_uuid_mask_ != *rhs.service_uuid_mask_)
93     return false;
94 
95   return true;
96 }
97 
98 }  // namespace bluetooth
99