xref: /aosp_15_r20/system/chre/apps/nearby/location/lbs/contexthub/nanoapps/nearby/mock_ble.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2023 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 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_MOCK_BLE_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_MOCK_BLE_H_
19 
20 #include <chre.h>
21 
22 namespace nearby {
23 
24 struct MockBle {
25 #if defined(MOCK_FAST_PAIR)
26   // A BLEScanRecord consists of one advertisement of Fast Pair initial pair.
27   static constexpr uint8_t kBleScanRecordData[] = {
28       // Advertisement.
29       6,     // byte length of ad below.
30       0x16,  // type of ad data (service data).
31       0x2C,  // 2 bytes uuid in little-endian (Fast Pair).
32       0xFE,
33       0x1F,  // 3 bytes Fast Pair initial pair ervice data (model ID).
34       0xD7,  // second byte of model ID.
35       0xD0,  // third byte of model ID.
36   };
37 #elif defined(MOCK_SUBSEQUENT_PAIR)
38   // A BLEScanRecord consists of one advertisement of Fast Pair subsequent pair.
39   static constexpr uint8_t kBleScanRecordData[] = {
40       // Advertisement.
41       12,    // byte length of ad below.
42       0x16,  // type of ad data (service data).
43       0x2C,  // 2 bytes uuid in little-endian (Fast Pair).
44       0xFE,
45       0x00,  // Version 0 with Flag 0
46       0x40,  // 4 bytes Bloom Filter
47       0x02, 0x0C, 0x80, 0x2A,
48       0x21,  // 2 bytes salt.
49       0xC7, 0xC8,
50   };
51 #elif defined(MOCK_PRESENCE_V0)
52   // A BLEScanRecord consists of one advertisement of Presence V0.
53   static constexpr uint8_t kBleScanRecordData[] = {
54       // Advertisement.
55       0x0B,  // byte length of ad below.
56       0x16,  // type of ad data (service data).
57       0xF1,  // uuid in little-endian (Nearby Presence)
58       0xFC,
59       // Presence service data below.
60       0b00100100,  // service data header (format 0bVVVLLLLR) with 2 fields.
61       // Intent field below, 1 byte header plus 2 byte value.
62       0b00100101,  // field header with 0b0101 type
63       1,           // first intent as 1
64       5,           // second intent as 5
65       // Model ID, 3 bytes length with 0b0111 type.
66       0b00110111,
67       0b00000001,
68       0b00000010,
69       0b00000100,
70   };
71 #else
72   // A BLEScanRecord consists of one advertisement of Presence V1.
73 #define IDENTITY_VALUE 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
74 #define DE_SIGNATURE 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
75   static constexpr uint8_t kBleScanRecordData[] = {
76       // Advertisement.
77       51,    // byte length of ad below.
78       0x16,  // type of ad data (service data).
79       0xF1,  // uuid in little-endian (Nearby Presence)
80       0xFC,
81       // Presence service data below.
82       0b00100000,  // Header with version v1.
83       0b00100000,  // Salt header: length 2, type 0
84       2,
85       3,           // Salt value.
86       0b10010000,  // Identity header: length 16, type 4
87       0b00000100,
88       IDENTITY_VALUE,  // Identity value: 16 bytes.
89       0b00010110,      // Action header: length 1, type 6
90       1,
91       0b00010110,  // Action header: length 1, type 6
92       124,
93       0b00010101,  // TX power header: length 1, type 5
94       20,
95       0b00110111,  // Model ID header: length 3, type 7
96       0, 1, 2,
97       DE_SIGNATURE,  // Data Element signature: 16 bytes
98   };
99 #endif
100 
101   static constexpr chreBleAdvertisingReport kReport = {
102       .address = {1, 2, 3, 4, 5, 6},
103       .txPower = 20,
104       .rssi = 10,
105       .directAddress = {1, 2, 3, 4, 5, 6},
106       .dataLength = sizeof(kBleScanRecordData),
107       .data = kBleScanRecordData,
108   };
109   static constexpr chreBleAdvertisementEvent kBleEvent = {
110       .numReports = 1,
111       .reports = &kReport,
112   };
113   static constexpr chreAsyncResult kBleFlushCompleteEvent = {
114       .requestType = CHRE_BLE_REQUEST_TYPE_FLUSH,
115       .success = true,
116       .errorCode = CHRE_ERROR_NONE,
117       .reserved = 0,
118       .cookie = nullptr};
119   static constexpr chreBatchCompleteEvent kBleBatchCompleteEvent = {
120       .eventType = CHRE_EVENT_BLE_ADVERTISEMENT};
121 #ifdef MOCK_BLE_BATCH_SCAN
122   static constexpr bool kBleBatchScanSupported = true;
123 #else
124   static constexpr bool kBleBatchScanSupported = false;
125 #endif
126   static constexpr uint32_t kBleFlushCompleteTimeoutMs = 50;
127   static constexpr uint32_t kBleFlushScanResultIntervalMs = 10;
128   static constexpr uint32_t kBleReportDelayMinMs = 10;
129 };
130 
131 }  // namespace nearby
132 
133 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_MOCK_BLE_H_
134