• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "chre/core/ble_request.h"
18 
19 #include <inttypes.h>
20 
21 #include "chre/platform/fatal_error.h"
22 #include "chre/util/memory.h"
23 
24 namespace chre {
25 
26 namespace {
27 
filtersMatch(const chreBleGenericFilter & filter,const chreBleGenericFilter & otherFilter)28 bool filtersMatch(const chreBleGenericFilter &filter,
29                   const chreBleGenericFilter &otherFilter) {
30   return filter.len == otherFilter.len && filter.type == otherFilter.type &&
31          (memcmp(filter.data, otherFilter.data, filter.len) == 0) &&
32          (memcmp(filter.dataMask, otherFilter.dataMask, filter.len) == 0);
33 }
34 
35 }  // namespace
36 
BleRequest()37 BleRequest::BleRequest() : BleRequest(0, false) {}
38 
BleRequest(uint16_t instanceId,bool enable)39 BleRequest::BleRequest(uint16_t instanceId, bool enable)
40     : BleRequest(instanceId, enable, CHRE_BLE_SCAN_MODE_BACKGROUND,
41                  0 /* reportDelayMs */, nullptr /* filter */) {}
42 
BleRequest(uint16_t instanceId,bool enable,chreBleScanMode mode,uint32_t reportDelayMs,const chreBleScanFilter * filter)43 BleRequest::BleRequest(uint16_t instanceId, bool enable, chreBleScanMode mode,
44                        uint32_t reportDelayMs, const chreBleScanFilter *filter)
45     : mReportDelayMs(reportDelayMs),
46       mInstanceId(instanceId),
47       mMode(mode),
48       mEnabled(enable),
49       mRssiThreshold(CHRE_BLE_RSSI_THRESHOLD_NONE),
50       mStatus(RequestStatus::PENDING_REQ) {
51   if (filter != nullptr) {
52     mRssiThreshold = filter->rssiThreshold;
53     if (filter->scanFilterCount > 0) {
54       if (!mFilters.resize(filter->scanFilterCount)) {
55         FATAL_ERROR("Unable to reserve filter count");
56       }
57       for (size_t i = 0; i < filter->scanFilterCount; i++) {
58         mFilters[i] = filter->scanFilters[i];
59       }
60     }
61   }
62 }
63 
BleRequest(BleRequest && other)64 BleRequest::BleRequest(BleRequest &&other) {
65   *this = std::move(other);
66 }
67 
operator =(BleRequest && other)68 BleRequest &BleRequest::operator=(BleRequest &&other) {
69   mInstanceId = other.mInstanceId;
70   mMode = other.mMode;
71   mReportDelayMs = other.mReportDelayMs;
72   mRssiThreshold = other.mRssiThreshold;
73   mFilters = std::move(other.mFilters);
74   mEnabled = other.mEnabled;
75   mStatus = other.mStatus;
76   return *this;
77 }
78 
mergeWith(const BleRequest & request)79 bool BleRequest::mergeWith(const BleRequest &request) {
80   // Only merge parameters of enabled requests.
81   if (!request.mEnabled) {
82     return false;
83   }
84   bool attributesChanged = false;
85   // Replace disabled request parameters.
86   if (!mEnabled) {
87     mEnabled = true;
88     mMode = request.mMode;
89     mReportDelayMs = request.mReportDelayMs;
90     mRssiThreshold = request.mRssiThreshold;
91     attributesChanged = true;
92   } else {
93     if (mMode < request.mMode) {
94       mMode = request.mMode;
95       attributesChanged = true;
96     }
97     if (mReportDelayMs > request.mReportDelayMs) {
98       mReportDelayMs = request.mReportDelayMs;
99       attributesChanged = true;
100     }
101     if (mRssiThreshold > request.mRssiThreshold) {
102       mRssiThreshold = request.mRssiThreshold;
103       attributesChanged = true;
104     }
105   }
106   const DynamicVector<chreBleGenericFilter> &otherFilters = request.mFilters;
107   for (const chreBleGenericFilter &otherFilter : otherFilters) {
108     bool addFilter = true;
109     for (const chreBleGenericFilter &filter : mFilters) {
110       if (filtersMatch(filter, otherFilter)) {
111         addFilter = false;
112         break;
113       }
114     }
115     if (addFilter) {
116       attributesChanged = true;
117       if (!mFilters.push_back(otherFilter)) {
118         FATAL_ERROR("Unable to merge filters");
119       }
120     }
121   }
122   return attributesChanged;
123 }
124 
isEquivalentTo(const BleRequest & request)125 bool BleRequest::isEquivalentTo(const BleRequest &request) {
126   const DynamicVector<chreBleGenericFilter> &otherFilters = request.mFilters;
127   bool isEquivalent = (mEnabled && request.mEnabled && mMode == request.mMode &&
128                        mReportDelayMs == request.mReportDelayMs &&
129                        mRssiThreshold == request.mRssiThreshold &&
130                        mFilters.size() == otherFilters.size());
131   if (isEquivalent) {
132     for (size_t i = 0; i < otherFilters.size(); i++) {
133       if (!filtersMatch(mFilters[i], otherFilters[i])) {
134         isEquivalent = false;
135         break;
136       }
137     }
138   }
139   return isEquivalent;
140 }
141 
getInstanceId() const142 uint16_t BleRequest::getInstanceId() const {
143   return mInstanceId;
144 }
145 
getMode() const146 chreBleScanMode BleRequest::getMode() const {
147   return mMode;
148 }
149 
getReportDelayMs() const150 uint32_t BleRequest::getReportDelayMs() const {
151   return mReportDelayMs;
152 }
153 
getRssiThreshold() const154 int8_t BleRequest::getRssiThreshold() const {
155   return mRssiThreshold;
156 }
157 
getRequestStatus() const158 RequestStatus BleRequest::getRequestStatus() const {
159   return mStatus;
160 }
161 
setRequestStatus(RequestStatus status)162 void BleRequest::setRequestStatus(RequestStatus status) {
163   mStatus = status;
164 }
165 
getGenericFilters() const166 const DynamicVector<chreBleGenericFilter> &BleRequest::getGenericFilters()
167     const {
168   return mFilters;
169 }
170 
getScanFilter() const171 chreBleScanFilter BleRequest::getScanFilter() const {
172   return chreBleScanFilter{
173       mRssiThreshold, static_cast<uint8_t>(mFilters.size()), mFilters.data()};
174 }
175 
isEnabled() const176 bool BleRequest::isEnabled() const {
177   return mEnabled;
178 }
179 
logStateToBuffer(DebugDumpWrapper & debugDump,bool isPlatformRequest) const180 void BleRequest::logStateToBuffer(DebugDumpWrapper &debugDump,
181                                   bool isPlatformRequest) const {
182   if (!isPlatformRequest) {
183     debugDump.print("  instanceId=%" PRIu16 " status=%" PRIu8, mInstanceId,
184                     static_cast<uint8_t>(mStatus));
185   }
186   debugDump.print(" %s", mEnabled ? " enable" : " disable\n");
187   if (mEnabled) {
188     debugDump.print(
189         " mode=%" PRIu8 " reportDelayMs=%" PRIu32 " rssiThreshold=%" PRId8,
190         static_cast<uint8_t>(mMode), mReportDelayMs, mRssiThreshold);
191     if (isPlatformRequest) {
192       debugDump.print(" filters=[");
193       for (const chreBleGenericFilter &filter : mFilters) {
194         debugDump.print("(type=%" PRIx8, filter.type);
195         if (filter.len > 0) {
196           debugDump.print(" data=%s dataMask=%s len=%" PRIu8 "), ",
197                           &filter.data[0], &filter.dataMask[0], filter.len);
198         } else {
199           debugDump.print("), ");
200         }
201       }
202       debugDump.print("]\n");
203     } else {
204       debugDump.print(" filterCount=%" PRIu8 "\n",
205                       static_cast<uint8_t>(mFilters.size()));
206     }
207   }
208 }
209 
210 }  // namespace chre