1 /* hci_core.h - Bluetooth HCI core access */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 #ifndef __HCI_CORE_H
9 #define __HCI_CORE_H
10 /* LL connection parameters */
11 #define LE_CONN_LATENCY		0x0000
12 #define LE_CONN_TIMEOUT		0x002a
13 
14 #if defined(CONFIG_BT_BREDR)
15 #define LMP_FEAT_PAGES_COUNT	3
16 #else
17 #define LMP_FEAT_PAGES_COUNT	1
18 #endif
19 
20 /* SCO  settings */
21 #define BT_VOICE_CVSD_16BIT     0x0060
22 
23 /* k_poll event tags */
24 enum {
25 	BT_EVENT_CMD_TX,
26 	BT_EVENT_CONN_TX_QUEUE,
27 };
28 
29 /* bt_dev flags: the flags defined here represent BT controller state */
30 enum {
31 	BT_DEV_ENABLE,
32 	BT_DEV_READY,
33 	BT_DEV_PRESET_ID,
34 	BT_DEV_HAS_PUB_KEY,
35 	BT_DEV_PUB_KEY_BUSY,
36 
37 	BT_DEV_SCANNING,
38 	BT_DEV_EXPLICIT_SCAN,
39 	BT_DEV_ACTIVE_SCAN,
40 	BT_DEV_SCAN_FILTER_DUP,
41 	BT_DEV_SCAN_WL,
42 	BT_DEV_SCAN_LIMITED,
43 	BT_DEV_INITIATING,
44 
45 	BT_DEV_RPA_VALID,
46 	BT_DEV_RPA_TIMEOUT_SET,
47 
48 	BT_DEV_ID_PENDING,
49 	BT_DEV_STORE_ID,
50 
51 #if defined(CONFIG_BT_BREDR)
52 	BT_DEV_ISCAN,
53 	BT_DEV_PSCAN,
54 	BT_DEV_INQUIRY,
55 #endif /* CONFIG_BT_BREDR */
56 
57 	/* Total number of flags - must be at the end of the enum */
58 	BT_DEV_NUM_FLAGS,
59 };
60 
61 /* Flags which should not be cleared upon HCI_Reset */
62 #define BT_DEV_PERSISTENT_FLAGS (BIT(BT_DEV_ENABLE) | \
63 				 BIT(BT_DEV_PRESET_ID))
64 
65 enum {
66 	/* Advertising set has been created in the host. */
67 	BT_ADV_CREATED,
68 	/* Advertising parameters has been set in the controller.
69 	 * This implies that the advertising set has been created in the
70 	 * controller.
71 	 */
72 	BT_ADV_PARAMS_SET,
73 	/* Advertising data has been set in the controller. */
74 	BT_ADV_DATA_SET,
75 	/* Advertising random address pending to be set in the controller. */
76 	BT_ADV_RANDOM_ADDR_PENDING,
77 	/* The private random address of the advertiser is valid for this cycle
78 	 * of the RPA timeout.
79 	 */
80 	BT_ADV_RPA_VALID,
81 	/* The advertiser set is limited by a timeout, or number of advertising
82 	 * events, or both.
83 	 */
84 	BT_ADV_LIMITED,
85 	/* Advertiser set is currently advertising in the controller. */
86 	BT_ADV_ENABLED,
87 	/* Advertiser should include name in advertising data */
88 	BT_ADV_INCLUDE_NAME,
89 	/* Advertiser set is connectable */
90 	BT_ADV_CONNECTABLE,
91 	/* Advertiser set is scannable */
92 	BT_ADV_SCANNABLE,
93 	/* Advertiser set has disabled the use of private addresses and is using
94 	 * the identity address instead.
95 	 */
96 	BT_ADV_USE_IDENTITY,
97 	/* Advertiser has been configured to keep advertising after a connection
98 	 * has been established as long as there are connections available.
99 	 */
100 	BT_ADV_PERSIST,
101 	/* Advertiser has been temporarily disabled. */
102 	BT_ADV_PAUSED,
103 
104 	BT_ADV_NUM_FLAGS,
105 };
106 
107 struct bt_le_ext_adv {
108 	/* ID Address used for advertising */
109 	u8_t                    id;
110 
111 	/* Advertising handle */
112 	u16_t			handle;
113 
114 	/* Current local Random Address */
115 	bt_addr_le_t		random_addr;
116 
117 	/* Current target address */
118 	bt_addr_le_t            target_addr;
119 
120 	ATOMIC_DEFINE(flags, BT_ADV_NUM_FLAGS);
121 
122 #if defined(CONFIG_BT_EXT_ADV)
123 	const struct bt_le_ext_adv_cb *cb;
124 
125 	/* TX Power in use by the controller */
126 	s8_t                    tx_power;
127 #endif /* defined(CONFIG_BT_EXT_ADV) */
128 };
129 
130 struct bt_dev_le {
131 	/* LE features */
132 	u8_t			features[8];
133 	/* LE states */
134 	u64_t			states;
135 
136 #if defined(CONFIG_BT_CONN)
137 	/* Controller buffer information */
138 	u16_t			mtu_init;
139 	u16_t			mtu;
140 	struct k_sem		pkts;
141 #endif /* CONFIG_BT_CONN */
142 
143 #if defined(CONFIG_BT_SMP)
144 	/* Size of the the controller resolving list */
145 	u8_t                    rl_size;
146 	/* Number of entries in the resolving list. rl_entries > rl_size
147 	 * means that host-side resolving is used.
148 	 */
149 	u8_t                    rl_entries;
150 #endif /* CONFIG_BT_SMP */
151 };
152 
153 #if defined(CONFIG_BT_BREDR)
154 struct bt_dev_br {
155 	/* Max controller's acceptable ACL packet length */
156 	u16_t         mtu;
157 	struct k_sem  pkts;
158 	u16_t         esco_pkt_type;
159 };
160 #endif
161 
162 /* The theoretical max for these is 8 and 64, but there's no point
163  * in allocating the full memory if we only support a small subset.
164  * These values must be updated whenever the host implementation is
165  * extended beyond the current values.
166  */
167 #define BT_DEV_VS_FEAT_MAX  1
168 #define BT_DEV_VS_CMDS_MAX  2
169 
170 /* State tracking for the local Bluetooth controller */
171 struct bt_dev {
172 	/* Local Identity Address(es) */
173 	bt_addr_le_t            id_addr[CONFIG_BT_ID_MAX];
174 	u8_t                    id_count;
175 
176 	struct bt_conn_le_create_param create_param;
177 
178 #if !defined(CONFIG_BT_EXT_ADV)
179 	/* Legacy advertiser */
180 	struct bt_le_ext_adv    adv;
181 #else
182 	/* Pointer to reserved advertising set */
183 	struct bt_le_ext_adv    *adv;
184 #endif
185 	/* Current local Random Address */
186 	bt_addr_le_t            random_addr;
187 	u8_t                    adv_conn_id;
188 
189 	/* Controller version & manufacturer information */
190 	u8_t			hci_version;
191 	u8_t			lmp_version;
192 	u16_t			hci_revision;
193 	u16_t			lmp_subversion;
194 	u16_t			manufacturer;
195 
196 	/* LMP features (pages 0, 1, 2) */
197 	u8_t			features[LMP_FEAT_PAGES_COUNT][8];
198 
199 	/* Supported commands */
200 	u8_t			supported_commands[64];
201 
202 #if defined(CONFIG_BT_HCI_VS_EXT)
203 	/* Vendor HCI support */
204 	u8_t                    vs_features[BT_DEV_VS_FEAT_MAX];
205 	u8_t                    vs_commands[BT_DEV_VS_CMDS_MAX];
206 #endif
207 
208 	struct k_work           init;
209 
210 	ATOMIC_DEFINE(flags, BT_DEV_NUM_FLAGS);
211 
212 	/* LE controller specific features */
213 	struct bt_dev_le	le;
214 
215 #if defined(CONFIG_BT_BREDR)
216 	/* BR/EDR controller specific features */
217 	struct bt_dev_br	br;
218 #endif
219 
220 	/* Number of commands controller can accept */
221 	struct k_sem		ncmd_sem;
222 
223 	/* Last sent HCI command */
224 	struct net_buf		*sent_cmd;
225 
226 #if !defined(CONFIG_BT_RECV_IS_RX_THREAD)
227 	/* Queue for incoming HCI events & ACL data */
228 	struct kfifo		rx_queue;
229 #endif
230 
231 	/* Queue for outgoing HCI commands */
232 	struct kfifo		cmd_tx_queue;
233 
234 	/* Registered HCI driver */
235 	const struct bt_hci_driver *drv;
236 
237 #if defined(CONFIG_BT_PRIVACY)
238 	/* Local Identity Resolving Key */
239 	u8_t			irk[CONFIG_BT_ID_MAX][16];
240 
241 	/* Work used for RPA rotation */
242 	struct k_delayed_work rpa_update;
243 #endif
244 
245 	/* Local Name */
246 #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
247 	char			name[CONFIG_BT_DEVICE_NAME_MAX + 1];
248 #endif
249 };
250 
251 extern struct bt_dev bt_dev;
252 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
253 extern const struct bt_conn_auth_cb *bt_auth;
254 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
255 
256 int bt_hci_disconnect(u16_t handle, u8_t reason);
257 
258 bool bt_le_conn_params_valid(const struct bt_le_conn_param *param);
259 int bt_le_set_data_len(struct bt_conn *conn, u16_t tx_octets, u16_t tx_time);
260 int bt_le_set_phy(struct bt_conn *conn, u8_t tx_phy, u8_t rx_phy);
261 
262 int bt_le_scan_update(bool fast_scan);
263 
264 int bt_le_create_conn(const struct bt_conn *conn);
265 int bt_le_create_conn_cancel(void);
266 
267 bool bt_addr_le_is_bonded(u8_t id, const bt_addr_le_t *addr);
268 const bt_addr_le_t *bt_lookup_id_addr(u8_t id, const bt_addr_le_t *addr);
269 
270 int bt_send(struct net_buf *buf);
271 
272 /* Don't require everyone to include keys.h */
273 struct bt_keys;
274 void bt_id_add(struct bt_keys *keys);
275 void bt_id_del(struct bt_keys *keys);
276 
277 int bt_setup_random_id_addr(void);
278 void bt_setup_public_id_addr(void);
279 
280 void bt_finalize_init(void);
281 
282 int bt_le_adv_start_internal(const struct bt_le_adv_param *param,
283 			     const struct bt_data *ad, size_t ad_len,
284 			     const struct bt_data *sd, size_t sd_len,
285 			     const bt_addr_le_t *peer);
286 
287 void bt_le_adv_resume(void);
288 bool bt_le_scan_random_addr_check(void);
289 int hci_driver_init();
290 
291 int hci_h5_driver_init();
292 
293 #if defined(CONFIG_BT_USE_HCI_API)
294 
295 struct event_handler {
296 	u8_t event;
297 	u8_t min_len;
298 	void (*handler)(struct net_buf *buf);
299 };
300 
301 #define EVENT_HANDLER(_evt, _handler, _min_len) \
302 { \
303 	.event = _evt, \
304 	.handler = _handler, \
305 	.min_len = _min_len, \
306 }
307 #endif
308 
309 #endif // __HCI_CORE_H
310 
311