1 /*
2  * Copyright 2020 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 #pragma once
18 
19 #include <cstdint>
20 
21 #include "osi/include/alarm.h"
22 #include "stack/include/btm_api_types.h"
23 #include "types/ble_address_with_type.h"
24 #include "types/raw_address.h"
25 
26 /* Discoverable modes */
27 enum : uint16_t {
28   BTM_NON_DISCOVERABLE = 0,
29   BTM_LIMITED_DISCOVERABLE = (1 << 0),
30   BTM_GENERAL_DISCOVERABLE = (1 << 1),
31   BTM_MAX_DISCOVERABLE = BTM_GENERAL_DISCOVERABLE,
32   BTM_DISCOVERABLE_MASK = (BTM_LIMITED_DISCOVERABLE | BTM_GENERAL_DISCOVERABLE),
33   /* high byte for BLE Discoverable modes */
34   BTM_BLE_NON_DISCOVERABLE = 0x0000,
35   BTM_BLE_LIMITED_DISCOVERABLE = 0x0100,
36   BTM_BLE_GENERAL_DISCOVERABLE = 0x0200,
37   BTM_BLE_MAX_DISCOVERABLE = BTM_BLE_GENERAL_DISCOVERABLE,
38   BTM_BLE_DISCOVERABLE_MASK =
39       (BTM_BLE_LIMITED_DISCOVERABLE | BTM_BLE_GENERAL_DISCOVERABLE),
40 };
41 
42 /* Connectable modes */
43 enum : uint16_t {
44   BTM_NON_CONNECTABLE = 0,
45   BTM_CONNECTABLE = (1 << 0),
46   BTM_CONNECTABLE_MASK = (BTM_NON_CONNECTABLE | BTM_CONNECTABLE),
47   /* high byte for BLE Connectable modes */
48   BTM_BLE_NON_CONNECTABLE = BTM_NON_CONNECTABLE,
49   BTM_BLE_CONNECTABLE = 0x0100,
50   BTM_BLE_MAX_CONNECTABLE = BTM_BLE_CONNECTABLE,
51   BTM_BLE_CONNECTABLE_MASK = (BTM_BLE_NON_CONNECTABLE | BTM_BLE_CONNECTABLE),
52 };
53 
54 /* Inquiry modes
55  * Note: These modes are associated with the inquiry active values (BTM_*ACTIVE)
56  */
57 enum : uint8_t {
58   BTM_INQUIRY_NONE = 0,
59   BTM_INQUIRY_INACTIVE = 0x0,
60   BTM_GENERAL_INQUIRY = 0x01,
61   /* SSP is active, so inquiry is disallowed (work around for FW bug) */
62   BTM_SSP_INQUIRY_ACTIVE = 0x4,
63   /* high nibble of inquiry mode for BLE inquiry mode */
64   BTM_BLE_GENERAL_INQUIRY = 0x10,
65   BTM_BR_INQUIRY_MASK = (BTM_GENERAL_INQUIRY),
66   BTM_BLE_INQUIRY_MASK = (BTM_BLE_GENERAL_INQUIRY),
67   BTM_BLE_INQUIRY_NONE = BTM_INQUIRY_NONE,
68   BTM_GENERAL_INQUIRY_ACTIVE = BTM_GENERAL_INQUIRY,
69   /* a general inquiry is in progress */
70   BTM_LE_GENERAL_INQUIRY_ACTIVE = BTM_BLE_GENERAL_INQUIRY,
71   /* BR/EDR inquiry activity mask */
72   BTM_BR_INQ_ACTIVE_MASK = (BTM_GENERAL_INQUIRY_ACTIVE),
73   /* LE scan activity mask */
74   BTM_BLE_SCAN_ACTIVE_MASK = 0xF0,
75   /* LE inquiry activity mask*/
76   BTM_BLE_INQ_ACTIVE_MASK = (BTM_LE_GENERAL_INQUIRY_ACTIVE),
77   /* inquiry activity mask */
78   BTM_INQUIRY_ACTIVE_MASK = (BTM_BR_INQ_ACTIVE_MASK | BTM_BLE_INQ_ACTIVE_MASK),
79 };
80 
81 /* Define scan types */
82 enum : uint16_t {
83   BTM_SCAN_TYPE_STANDARD = 0,
84   BTM_SCAN_TYPE_INTERLACED = 1, /* 1.2 devices only */
85 };
86 
87 /* Define inquiry results mode */
88 enum : uint8_t {
89   BTM_INQ_RESULT_STANDARD = 0,
90   BTM_INQ_RESULT_WITH_RSSI = 1,
91   BTM_INQ_RESULT_EXTENDED = 2,
92   /* RSSI value not supplied (ignore it) */
93   BTM_INQ_RES_IGNORE_RSSI = 0x7f,
94 };
95 
96 /* These are the fields returned in each device's response to the inquiry.  It
97  * is returned in the results callback if registered.
98  */
99 typedef struct {
100   uint16_t clock_offset;
101   RawAddress remote_bd_addr;
102   DEV_CLASS dev_class;
103   uint8_t page_scan_rep_mode;
104   uint8_t page_scan_per_mode;
105   uint8_t page_scan_mode;
106   int8_t rssi; /* Set to BTM_INQ_RES_IGNORE_RSSI if  not valid */
107   uint32_t eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE];
108   bool eir_complete_list;
109   tBT_DEVICE_TYPE device_type;
110   uint8_t inq_result_type;
111   tBLE_ADDR_TYPE ble_addr_type;
112   uint16_t ble_evt_type;
113   uint8_t ble_primary_phy;
114   uint8_t ble_secondary_phy;
115   uint8_t ble_advertising_sid;
116   int8_t ble_tx_power;
117   uint16_t ble_periodic_adv_int;
118   uint8_t flag;
119 } tBTM_INQ_RESULTS;
120 
121 /****************************************
122  *  Device Discovery Callback Functions
123  ****************************************/
124 /* Callback function for notifications when the BTM gets inquiry response.
125  * First param is inquiry results database, second is pointer of EIR.
126  */
127 typedef void(tBTM_INQ_RESULTS_CB)(tBTM_INQ_RESULTS* p_inq_results,
128                                   uint8_t* p_eir, uint16_t eir_len);
129 
130 typedef struct {
131   uint32_t inq_count; /* Used for determining if a response has already been */
132   /* received for the current inquiry operation. (We do not   */
133   /* want to flood the caller with multiple responses from    */
134   /* the same device.                                         */
135   RawAddress bd_addr;
136 } tINQ_BDADDR;
137 
138 /* This is the inquiry response information held in its database by BTM, and
139  * available to applications via BTM_InqDbRead, BTM_InqDbFirst, and
140  * BTM_InqDbNext.
141  */
142 typedef struct {
143   tBTM_INQ_RESULTS results;
144 
145   bool appl_knows_rem_name; /* set by application if it knows the remote name of
146                                the peer device.
147                                This is later used by application to determine if
148                                remote name request is
149                                required to be done. Having the flag here avoid
150                                duplicate store of inquiry results */
151   uint16_t remote_name_len;
152   tBTM_BD_NAME remote_name;
153   uint8_t remote_name_state;
154   uint8_t remote_name_type;
155 
156 } tBTM_INQ_INFO;
157 
158 typedef struct {
159   uint64_t time_of_resp;
160   uint32_t
161       inq_count; /* "timestamps" the entry with a particular inquiry count   */
162                  /* Used for determining if a response has already been      */
163                  /* received for the current inquiry operation. (We do not   */
164                  /* want to flood the caller with multiple responses from    */
165                  /* the same device.                                         */
166   tBTM_INQ_INFO inq_info;
167   bool in_use;
168   bool scan_rsp;
169 } tINQ_DB_ENT;
170 
171 typedef struct /* contains the parameters passed to the inquiry functions */
172 {
173   uint8_t mode;     /* general or limited */
174   uint8_t duration; /* duration of the inquiry (1.28 sec increments) */
175 } tBTM_INQ_PARMS;
176 
177 /* Structure returned with inquiry complete callback */
178 typedef struct {
179   tBTM_STATUS status;
180   uint8_t num_resp; /* Number of results from the current inquiry */
181 } tBTM_INQUIRY_CMPL;
182 
183 typedef struct {
184   tBTM_CMPL_CB* p_remname_cmpl_cb;
185 
186 #define BTM_EXT_RMT_NAME_TIMEOUT_MS (40 * 1000) /* 40 seconds */
187 
188   alarm_t* remote_name_timer;
189 
190   uint16_t discoverable_mode;
191   uint16_t connectable_mode;
192   uint16_t page_scan_window;
193   uint16_t page_scan_period;
194   uint16_t inq_scan_window;
195   uint16_t inq_scan_period;
196   uint16_t inq_scan_type;
197   uint16_t page_scan_type; /* current page scan type */
198 
199   RawAddress remname_bda; /* Name of bd addr for active remote name request */
200 #define BTM_RMT_NAME_EXT 0x1 /* Initiated through API */
201   bool remname_active; /* State of a remote name request by external API */
202 
203   tBTM_CMPL_CB* p_inq_cmpl_cb;
204   tBTM_INQ_RESULTS_CB* p_inq_results_cb;
205   uint32_t inq_counter; /* Counter incremented each time an inquiry completes */
206   /* Used for determining whether or not duplicate devices */
207   /* have responded to the same inquiry */
208   tINQ_BDADDR* p_bd_db;    /* Pointer to memory that holds bdaddrs */
209   uint16_t num_bd_entries; /* Number of entries in database */
210   uint16_t max_bd_entries; /* Maximum number of entries that can be stored */
211   tINQ_DB_ENT inq_db[BTM_INQ_DB_SIZE];
212   tBTM_INQ_PARMS inqparms; /* Contains the parameters for the current inquiry */
213   tBTM_INQUIRY_CMPL
214       inq_cmpl_info; /* Status and number of responses from the last inquiry */
215 
216   uint16_t per_min_delay; /* Current periodic minimum delay */
217   uint16_t per_max_delay; /* Current periodic maximum delay */
218   /* inquiry that has been cancelled*/
219   uint8_t inqfilt_type; /* Contains the inquiry filter type (BD ADDR, COD, or
220                            Clear) */
221 
222 #define BTM_INQ_INACTIVE_STATE 0
223 #define BTM_INQ_ACTIVE_STATE \
224   3 /* Actual inquiry or periodic inquiry is in progress */
225 
226   uint8_t state;      /* Current state that the inquiry process is in */
227   uint8_t inq_active; /* Bit Mask indicating type of inquiry is active */
228   bool no_inc_ssp;    /* true, to stop inquiry on incoming SSP */
229 
Init__anonfe66f7630c08230   void Init() {
231     alarm_free(remote_name_timer);
232     remote_name_timer = alarm_new("btm_inq.remote_name_timer");
233     no_inc_ssp = BTM_NO_SSP_ON_INQUIRY;
234   }
Free__anonfe66f7630c08235   void Free() { alarm_free(remote_name_timer); }
236 
237 } tBTM_INQUIRY_VAR_ST;
238 
239 /* Structure returned with remote name  request */
240 typedef struct {
241   uint16_t status;
242   RawAddress bd_addr;
243   uint16_t length;
244   BD_NAME remote_bd_name;
245 } tBTM_REMOTE_DEV_NAME;
246 
247 typedef union /* contains the inquiry filter condition */
248 {
249   RawAddress bdaddr_cond;
250   tBTM_COD_COND cod_cond;
251 } tBTM_INQ_FILT_COND;
252 
253 #define BTM_INQ_RESULT_BR 0x01
254 #define BTM_INQ_RESULT_BLE 0x02
255 
256 extern bool btm_inq_find_bdaddr(const RawAddress& p_bda);
257 extern tINQ_DB_ENT* btm_inq_db_find(const RawAddress& p_bda);
258