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