• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "location/lbs/contexthub/nanoapps/nearby/hw_filter.h"
2 
3 #include <cstdint>
4 
5 #include "chre_api/chre.h"
6 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h"
7 
8 #define LOG_TAG "[NEARBY][HW_FILTER]"
9 
10 namespace nearby {
11 
Match(const chre::DynamicVector<chreBleGenericFilter> & hardware_filters,const chreBleAdvertisingReport & report)12 bool HwFilter::Match(
13     const chre::DynamicVector<chreBleGenericFilter> &hardware_filters,
14     const chreBleAdvertisingReport &report) {
15   for (const auto filter : hardware_filters) {
16     // Scans through data and parses each advertisement structure.
17     for (int i = 0; i < report.dataLength;) {
18       // First byte has the advertisement data length including type and data.
19       uint8_t ad_type_data_length = report.data[i];
20       // Early termination with zero length advertisement.
21       if (ad_type_data_length == 0) break;
22       // Terminates when advertisement length passes over the end of data
23       // buffer.
24       if (ad_type_data_length >= report.dataLength - i) break;
25       // Second byte has advertisement data type.
26       ++i;
27       // Moves to the next data structure if advertisement data length is less
28       // than filter length regardless of data mask or advertisement data
29       // type is different from filter type.
30       if (ad_type_data_length - 1 >= filter.len &&
31           report.data[i] == filter.type) {
32         // Assumes advertisement data structure is matched.
33         bool matched = true;
34         // Data should match through data filter mask within filter length.
35         for (int j = 0; j < filter.len; ++j) {
36           if ((report.data[i + 1 + j] & filter.dataMask[j]) !=
37               (filter.data[j] & filter.dataMask[j])) {
38             matched = false;
39             break;
40           }
41         }
42         if (matched) {
43           return true;
44         }
45       }
46       // Moves to next advertisement structure.
47       i += ad_type_data_length;
48     }
49   }
50   return false;
51 }
52 
CheckRssi(int8_t rssi_threshold,const chreBleAdvertisingReport & report)53 bool HwFilter::CheckRssi(int8_t rssi_threshold,
54                          const chreBleAdvertisingReport &report) {
55   return (rssi_threshold == CHRE_BLE_RSSI_NONE ||
56           (report.rssi != CHRE_BLE_RSSI_NONE && report.rssi >= rssi_threshold));
57 }
58 
59 }  // namespace nearby
60