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   // Pass original bda up to GattService#onScanResult
120   RawAddress original_bda;
121 } tBTM_INQ_RESULTS;
122 
123 /****************************************
124  *  Device Discovery Callback Functions
125  ****************************************/
126 /* Callback function for notifications when the BTM gets inquiry response.
127  * First param is inquiry results database, second is pointer of EIR.
128  */
129 typedef void(tBTM_INQ_RESULTS_CB)(tBTM_INQ_RESULTS* p_inq_results,
130                                   uint8_t* p_eir, uint16_t eir_len);
131 
132 typedef struct {
133   uint32_t inq_count; /* Used for determining if a response has already been */
134   /* received for the current inquiry operation. (We do not   */
135   /* want to flood the caller with multiple responses from    */
136   /* the same device.                                         */
137   RawAddress bd_addr;
138 } tINQ_BDADDR;
139 
140 /* This is the inquiry response information held in its database by BTM, and
141  * available to applications via BTM_InqDbRead, BTM_InqDbFirst, and
142  * BTM_InqDbNext.
143  */
144 typedef struct {
145   tBTM_INQ_RESULTS results;
146 
147   bool appl_knows_rem_name; /* set by application if it knows the remote name of
148                                the peer device.
149                                This is later used by application to determine if
150                                remote name request is
151                                required to be done. Having the flag here avoid
152                                duplicate store of inquiry results */
153   uint16_t remote_name_len;
154   tBTM_BD_NAME remote_name;
155   uint8_t remote_name_state;
156   uint8_t remote_name_type;
157 
158 } tBTM_INQ_INFO;
159 
160 typedef struct {
161   uint64_t time_of_resp;
162   uint32_t
163       inq_count; /* "timestamps" the entry with a particular inquiry count   */
164                  /* Used for determining if a response has already been      */
165                  /* received for the current inquiry operation. (We do not   */
166                  /* want to flood the caller with multiple responses from    */
167                  /* the same device.                                         */
168   tBTM_INQ_INFO inq_info;
169   bool in_use;
170   bool scan_rsp;
171 } tINQ_DB_ENT;
172 
173 typedef struct /* contains the parameters passed to the inquiry functions */
174 {
175   uint8_t mode;     /* general or limited */
176   uint8_t duration; /* duration of the inquiry (1.28 sec increments) */
177 } tBTM_INQ_PARMS;
178 
179 /* Structure returned with inquiry complete callback */
180 typedef struct {
181   tBTM_STATUS status;
182   uint8_t num_resp; /* Number of results from the current inquiry */
183 } tBTM_INQUIRY_CMPL;
184 
185 typedef struct {
186   tBTM_CMPL_CB* p_remname_cmpl_cb;
187 
188 #define BTM_EXT_RMT_NAME_TIMEOUT_MS (40 * 1000) /* 40 seconds */
189 
190   alarm_t* remote_name_timer;
191 
192   uint16_t discoverable_mode;
193   uint16_t connectable_mode;
194   uint16_t page_scan_window;
195   uint16_t page_scan_period;
196   uint16_t inq_scan_window;
197   uint16_t inq_scan_period;
198   uint16_t inq_scan_type;
199   uint16_t page_scan_type; /* current page scan type */
200 
201   RawAddress remname_bda; /* Name of bd addr for active remote name request */
202 #define BTM_RMT_NAME_EXT 0x1 /* Initiated through API */
203   bool remname_active; /* State of a remote name request by external API */
204 
205   tBTM_CMPL_CB* p_inq_cmpl_cb;
206   tBTM_INQ_RESULTS_CB* p_inq_results_cb;
207   uint32_t inq_counter; /* Counter incremented each time an inquiry completes */
208   /* Used for determining whether or not duplicate devices */
209   /* have responded to the same inquiry */
210   tINQ_BDADDR* p_bd_db;    /* Pointer to memory that holds bdaddrs */
211   uint16_t num_bd_entries; /* Number of entries in database */
212   uint16_t max_bd_entries; /* Maximum number of entries that can be stored */
213   tINQ_DB_ENT inq_db[BTM_INQ_DB_SIZE];
214   tBTM_INQ_PARMS inqparms; /* Contains the parameters for the current inquiry */
215   tBTM_INQUIRY_CMPL
216       inq_cmpl_info; /* Status and number of responses from the last inquiry */
217 
218   uint16_t per_min_delay; /* Current periodic minimum delay */
219   uint16_t per_max_delay; /* Current periodic maximum delay */
220   /* inquiry that has been cancelled*/
221   uint8_t inqfilt_type; /* Contains the inquiry filter type (BD ADDR, COD, or
222                            Clear) */
223 
224 #define BTM_INQ_INACTIVE_STATE 0
225 #define BTM_INQ_ACTIVE_STATE \
226   3 /* Actual inquiry or periodic inquiry is in progress */
227 
228   uint8_t state;      /* Current state that the inquiry process is in */
229   uint8_t inq_active; /* Bit Mask indicating type of inquiry is active */
230   bool no_inc_ssp;    /* true, to stop inquiry on incoming SSP */
231 
Init__anon4b300a4f0c08232   void Init() {
233     alarm_free(remote_name_timer);
234     remote_name_timer = alarm_new("btm_inq.remote_name_timer");
235     no_inc_ssp = BTM_NO_SSP_ON_INQUIRY;
236   }
Free__anon4b300a4f0c08237   void Free() { alarm_free(remote_name_timer); }
238 
239 } tBTM_INQUIRY_VAR_ST;
240 
241 /* Structure returned with remote name  request */
242 typedef struct {
243   uint16_t status;
244   RawAddress bd_addr;
245   uint16_t length;
246   BD_NAME remote_bd_name;
247 } tBTM_REMOTE_DEV_NAME;
248 
249 typedef union /* contains the inquiry filter condition */
250 {
251   RawAddress bdaddr_cond;
252   tBTM_COD_COND cod_cond;
253 } tBTM_INQ_FILT_COND;
254 
255 #define BTM_INQ_RESULT_BR 0x01
256 #define BTM_INQ_RESULT_BLE 0x02
257 
258 extern bool btm_inq_find_bdaddr(const RawAddress& p_bda);
259 extern tINQ_DB_ENT* btm_inq_db_find(const RawAddress& p_bda);
260