1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #ifndef H_BLE_LL_
21 #define H_BLE_LL_
22
23 #include "stats/stats.h"
24 #include "os/os_cputime.h"
25 #include "nimble/nimble_opt.h"
26 #include "nimble/nimble_npl.h"
27 #include "controller/ble_phy.h"
28
29 #ifdef MYNEWT
30 #include "controller/ble_ll_ctrl.h"
31 #include "hal/hal_system.h"
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
39 #error 32.768kHz clock required
40 #endif
41
42 #if defined(MYNEWT) && MYNEWT_VAL(BLE_LL_VND_EVENT_ON_ASSERT)
43 #ifdef NDEBUG
44 #define BLE_LL_ASSERT(cond) (void(0))
45 #else
46 #define BLE_LL_ASSERT(cond) \
47 if (!(cond)) { \
48 if (hal_debugger_connected()) { \
49 assert(0);\
50 } else {\
51 ble_ll_hci_ev_send_vendor_err(__FILE__, __LINE__); \
52 while(1) {}\
53 }\
54 }
55 #endif
56 #else
57 #define BLE_LL_ASSERT(cond) assert(cond)
58 #endif
59 /*
60 * XXX:
61 * I guess this should not depend on the 32768 crystal to be honest. This
62 * should be done for TIMER0 as well since the rf clock chews up more current.
63 * Deal with this later.
64 *
65 * Another note: BLE_XTAL_SETTLE_TIME should be bsp related (I guess). There
66 * should be a note in there that the converted usecs to ticks value of this
67 * should not be 0. Thus: if you are using a 32.768 os cputime freq, the min
68 * value of settle time should be 31 usecs. I would suspect all settling times
69 * would exceed 31 usecs.
70 */
71
72 /* Determines if we need to turn on/off rf clock */
73 #undef BLE_XCVR_RFCLK
74
75 /* We will turn on/off rf clock */
76 #if MYNEWT_VAL(BLE_XTAL_SETTLE_TIME) != 0
77 #define BLE_XCVR_RFCLK
78
79 #endif
80
81 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
82 #define BLE_LL_BT5_PHY_SUPPORTED (1)
83 #else
84 #define BLE_LL_BT5_PHY_SUPPORTED (0)
85 #endif
86
87 /* Controller revision. */
88 #define BLE_LL_SUB_VERS_NR (0x0000)
89
90 /* Timing jitter as per spec is +/16 usecs */
91 #define BLE_LL_JITTER_USECS (16)
92
93 /* Packet queue header definition */
94 STAILQ_HEAD(ble_ll_pkt_q, os_mbuf_pkthdr);
95
96 /*
97 * Global Link Layer data object. There is only one Link Layer data object
98 * per controller although there may be many instances of the link layer state
99 * machine running.
100 */
101 struct ble_ll_obj
102 {
103 /* Supported features */
104 uint32_t ll_supp_features;
105
106 /* Current Link Layer state */
107 uint8_t ll_state;
108
109 /* Number of ACL data packets supported */
110 uint8_t ll_num_acl_pkts;
111
112 /* ACL data packet size */
113 uint16_t ll_acl_pkt_size;
114
115 /* Preferred PHY's */
116 uint8_t ll_pref_tx_phys;
117 uint8_t ll_pref_rx_phys;
118
119 #ifdef BLE_XCVR_RFCLK
120 uint8_t ll_rfclk_state;
121 uint16_t ll_xtal_ticks;
122 uint32_t ll_rfclk_start_time;
123 struct hal_timer ll_rfclk_timer;
124 #endif
125
126 /* Task event queue */
127 struct ble_npl_eventq ll_evq;
128
129 /* Wait for response timer */
130 struct hal_timer ll_wfr_timer;
131
132 /* Packet receive queue (and event). Holds received packets from PHY */
133 struct ble_npl_event ll_rx_pkt_ev;
134 struct ble_ll_pkt_q ll_rx_pkt_q;
135
136 /* Packet transmit queue */
137 struct ble_npl_event ll_tx_pkt_ev;
138 struct ble_ll_pkt_q ll_tx_pkt_q;
139
140 /* Data buffer overflow event */
141 struct ble_npl_event ll_dbuf_overflow_ev;
142
143 /* Number of completed packets event */
144 struct ble_npl_event ll_comp_pkt_ev;
145
146 /* HW error callout */
147 struct ble_npl_callout ll_hw_err_timer;
148 };
149 extern struct ble_ll_obj g_ble_ll_data;
150
151 /* Link layer statistics */
152 STATS_SECT_START(ble_ll_stats)
153 STATS_SECT_ENTRY(hci_cmds)
154 STATS_SECT_ENTRY(hci_cmd_errs)
155 STATS_SECT_ENTRY(hci_events_sent)
156 STATS_SECT_ENTRY(bad_ll_state)
157 STATS_SECT_ENTRY(bad_acl_hdr)
158 STATS_SECT_ENTRY(no_bufs)
159 STATS_SECT_ENTRY(rx_adv_pdu_crc_ok)
160 STATS_SECT_ENTRY(rx_adv_pdu_crc_err)
161 STATS_SECT_ENTRY(rx_adv_bytes_crc_ok)
162 STATS_SECT_ENTRY(rx_adv_bytes_crc_err)
163 STATS_SECT_ENTRY(rx_data_pdu_crc_ok)
164 STATS_SECT_ENTRY(rx_data_pdu_crc_err)
165 STATS_SECT_ENTRY(rx_data_bytes_crc_ok)
166 STATS_SECT_ENTRY(rx_data_bytes_crc_err)
167 STATS_SECT_ENTRY(rx_adv_malformed_pkts)
168 STATS_SECT_ENTRY(rx_adv_ind)
169 STATS_SECT_ENTRY(rx_adv_direct_ind)
170 STATS_SECT_ENTRY(rx_adv_nonconn_ind)
171 STATS_SECT_ENTRY(rx_adv_ext_ind)
172 STATS_SECT_ENTRY(rx_scan_reqs)
173 STATS_SECT_ENTRY(rx_scan_rsps)
174 STATS_SECT_ENTRY(rx_connect_reqs)
175 STATS_SECT_ENTRY(rx_scan_ind)
176 STATS_SECT_ENTRY(rx_aux_connect_rsp)
177 STATS_SECT_ENTRY(adv_txg)
178 STATS_SECT_ENTRY(adv_late_starts)
179 STATS_SECT_ENTRY(adv_resched_pdu_fail)
180 STATS_SECT_ENTRY(adv_drop_event)
181 STATS_SECT_ENTRY(sched_state_conn_errs)
182 STATS_SECT_ENTRY(sched_state_adv_errs)
183 STATS_SECT_ENTRY(scan_starts)
184 STATS_SECT_ENTRY(scan_stops)
185 STATS_SECT_ENTRY(scan_req_txf)
186 STATS_SECT_ENTRY(scan_req_txg)
187 STATS_SECT_ENTRY(scan_rsp_txg)
188 STATS_SECT_ENTRY(aux_missed_adv)
189 STATS_SECT_ENTRY(aux_scheduled)
190 STATS_SECT_ENTRY(aux_received)
191 STATS_SECT_ENTRY(aux_fired_for_read)
192 STATS_SECT_ENTRY(aux_allocated)
193 STATS_SECT_ENTRY(aux_freed)
194 STATS_SECT_ENTRY(aux_sched_cb)
195 STATS_SECT_ENTRY(aux_conn_req_tx)
196 STATS_SECT_ENTRY(aux_conn_rsp_tx)
197 STATS_SECT_ENTRY(aux_conn_rsp_err)
198 STATS_SECT_ENTRY(aux_scan_req_tx)
199 STATS_SECT_ENTRY(aux_scan_rsp_err)
200 STATS_SECT_ENTRY(aux_chain_cnt)
201 STATS_SECT_ENTRY(aux_chain_err)
202 STATS_SECT_ENTRY(adv_evt_dropped)
203 STATS_SECT_ENTRY(scan_timer_stopped)
204 STATS_SECT_ENTRY(scan_timer_restarted)
205 STATS_SECT_END
206 extern STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
207
208 /* States */
209 #define BLE_LL_STATE_STANDBY (0)
210 #define BLE_LL_STATE_ADV (1)
211 #define BLE_LL_STATE_SCANNING (2)
212 #define BLE_LL_STATE_INITIATING (3)
213 #define BLE_LL_STATE_CONNECTION (4)
214 #define BLE_LL_STATE_DTM (5)
215
216 /* LL Features */
217 #define BLE_LL_FEAT_LE_ENCRYPTION (0x00000001)
218 #define BLE_LL_FEAT_CONN_PARM_REQ (0x00000002)
219 #define BLE_LL_FEAT_EXTENDED_REJ (0x00000004)
220 #define BLE_LL_FEAT_SLAVE_INIT (0x00000008)
221 #define BLE_LL_FEAT_LE_PING (0x00000010)
222 #define BLE_LL_FEAT_DATA_LEN_EXT (0x00000020)
223 #define BLE_LL_FEAT_LL_PRIVACY (0x00000040)
224 #define BLE_LL_FEAT_EXT_SCAN_FILT (0x00000080)
225 #define BLE_LL_FEAT_LE_2M_PHY (0x00000100)
226 #define BLE_LL_FEAT_STABLE_MOD_ID_TX (0x00000200)
227 #define BLE_LL_FEAT_STABLE_MOD_ID_RX (0x00000400)
228 #define BLE_LL_FEAT_LE_CODED_PHY (0x00000800)
229 #define BLE_LL_FEAT_EXT_ADV (0x00001000)
230 #define BLE_LL_FEAT_PERIODIC_ADV (0x00002000)
231 #define BLE_LL_FEAT_CSA2 (0x00004000)
232 #define BLE_LL_FEAT_LE_POWER_CLASS_1 (0x00008000)
233 #define BLE_LL_FEAT_MIN_USED_CHAN (0x00010000)
234
235 /* This is initial mask, so if feature exchange will not happen,
236 * but host will want to use this procedure, we will try. If not
237 * succeed, feature bit will be cleared.
238 * Look at LL Features above to find out what is allowed
239 */
240 #define BLE_LL_CONN_INITIAL_FEATURES (0x00000002)
241
242 #define BLE_LL_CONN_CLEAR_FEATURE(connsm, feature) (connsm->conn_features &= ~(feature))
243
244 /* LL timing */
245 #define BLE_LL_IFS (150) /* usecs */
246 #define BLE_LL_MAFS (300) /* usecs */
247
248 /*
249 * BLE LL device address. Note that element 0 of the array is the LSB and
250 * is sent over the air first. Byte 5 is the MSB and is the last one sent over
251 * the air.
252 */
253 #define BLE_DEV_ADDR_LEN (6) /* bytes */
254
255 struct ble_dev_addr
256 {
257 uint8_t u8[BLE_DEV_ADDR_LEN];
258 };
259
260 #define BLE_IS_DEV_ADDR_STATIC(addr) ((addr->u8[5] & 0xc0) == 0xc0)
261 #define BLE_IS_DEV_ADDR_RESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x40)
262 #define BLE_IS_DEV_ADDR_UNRESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x00)
263
264 /*
265 * LL packet format
266 *
267 * -> Preamble (1/2 bytes)
268 * -> Access Address (4 bytes)
269 * -> PDU (2 to 257 octets)
270 * -> CRC (3 bytes)
271 */
272 #define BLE_LL_PREAMBLE_LEN (1)
273 #define BLE_LL_ACC_ADDR_LEN (4)
274 #define BLE_LL_CRC_LEN (3)
275 #define BLE_LL_PDU_HDR_LEN (2)
276 #define BLE_LL_MAX_PAYLOAD_LEN (255)
277 #define BLE_LL_MIN_PDU_LEN (BLE_LL_PDU_HDR_LEN)
278 #define BLE_LL_MAX_PDU_LEN ((BLE_LL_PDU_HDR_LEN) + (BLE_LL_MAX_PAYLOAD_LEN))
279 #define BLE_LL_CRCINIT_ADV (0x555555)
280
281 /* Access address for advertising channels */
282 #define BLE_ACCESS_ADDR_ADV (0x8E89BED6)
283
284 /*
285 * Advertising PDU format:
286 * -> 2 byte header
287 * -> LSB contains pdu type, txadd and rxadd bits.
288 * -> MSB contains length (6 bits). Length is length of payload. Does
289 * not include the header length itself.
290 * -> Payload (max 37 bytes)
291 */
292 #define BLE_ADV_PDU_HDR_TYPE_MASK (0x0F)
293 #define BLE_ADV_PDU_HDR_CHSEL_MASK (0x20)
294 #define BLE_ADV_PDU_HDR_TXADD_MASK (0x40)
295 #define BLE_ADV_PDU_HDR_RXADD_MASK (0x80)
296
297 /* Advertising channel PDU types */
298 #define BLE_ADV_PDU_TYPE_ADV_IND (0)
299 #define BLE_ADV_PDU_TYPE_ADV_DIRECT_IND (1)
300 #define BLE_ADV_PDU_TYPE_ADV_NONCONN_IND (2)
301 #define BLE_ADV_PDU_TYPE_SCAN_REQ (3)
302 #define BLE_ADV_PDU_TYPE_SCAN_RSP (4)
303 #define BLE_ADV_PDU_TYPE_CONNECT_REQ (5)
304 #define BLE_ADV_PDU_TYPE_ADV_SCAN_IND (6)
305 #define BLE_ADV_PDU_TYPE_ADV_EXT_IND (7)
306 #define BLE_ADV_PDU_TYPE_AUX_ADV_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
307 #define BLE_ADV_PDU_TYPE_AUX_SCAN_RSP BLE_ADV_PDU_TYPE_ADV_EXT_IND
308 #define BLE_ADV_PDU_TYPE_AUX_SYNC_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
309 #define BLE_ADV_PDU_TYPE_AUX_CHAIN_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
310 #define BLE_ADV_PDU_TYPE_AUX_CONNECT_REQ BLE_ADV_PDU_TYPE_CONNECT_REQ
311 #define BLE_ADV_PDU_TYPE_AUX_SCAN_REQ BLE_ADV_PDU_TYPE_SCAN_REQ
312 #define BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP (8)
313
314 /* Extended Header Length (6b) + AdvMode (2b) */
315 #define BLE_LL_EXT_ADV_HDR_LEN (1)
316
317 #define BLE_LL_EXT_ADV_ADVA_BIT (0)
318 #define BLE_LL_EXT_ADV_TARGETA_BIT (1)
319 #define BLE_LL_EXT_ADV_RFU_BIT (2)
320 #define BLE_LL_EXT_ADV_DATA_INFO_BIT (3)
321 #define BLE_LL_EXT_ADV_AUX_PTR_BIT (4)
322 #define BLE_LL_EXT_ADV_SYNC_INFO_BIT (5)
323 #define BLE_LL_EXT_ADV_TX_POWER_BIT (6)
324
325 #define BLE_LL_EXT_ADV_FLAGS_SIZE (1)
326 #define BLE_LL_EXT_ADV_ADVA_SIZE (6)
327 #define BLE_LL_EXT_ADV_TARGETA_SIZE (6)
328 #define BLE_LL_EXT_ADV_DATA_INFO_SIZE (2)
329 #define BLE_LL_EXT_ADV_AUX_PTR_SIZE (3)
330 #define BLE_LL_EXT_ADV_SYNC_INFO_SIZE (18)
331 #define BLE_LL_EXT_ADV_TX_POWER_SIZE (1)
332
333 #define BLE_LL_EXT_ADV_MODE_NON_CONN (0x00)
334 #define BLE_LL_EXT_ADV_MODE_CONN (0x01)
335 #define BLE_LL_EXT_ADV_MODE_SCAN (0x02)
336
337 /* If Channel Selection Algorithm #2 is supported */
338 #define BLE_ADV_PDU_HDR_CHSEL (0x20)
339
340 /*
341 * TxAdd and RxAdd bit definitions. A 0 is a public address; a 1 is a
342 * random address.
343 */
344 #define BLE_ADV_PDU_HDR_TXADD_RAND (0x40)
345 #define BLE_ADV_PDU_HDR_RXADD_RAND (0x80)
346
347 /*
348 * Data Channel format
349 *
350 * -> Header (2 bytes)
351 * -> LSB contains llid, nesn, sn and md
352 * -> MSB contains length (8 bits)
353 * -> Payload (0 to 251)
354 * -> MIC (0 or 4 bytes)
355 */
356 #define BLE_LL_DATA_HDR_LLID_MASK (0x03)
357 #define BLE_LL_DATA_HDR_NESN_MASK (0x04)
358 #define BLE_LL_DATA_HDR_SN_MASK (0x08)
359 #define BLE_LL_DATA_HDR_MD_MASK (0x10)
360 #define BLE_LL_DATA_HDR_RSRVD_MASK (0xE0)
361 #define BLE_LL_DATA_PDU_MAX_PYLD (251)
362 #define BLE_LL_DATA_MIC_LEN (4)
363
364 /* LLID definitions */
365 #define BLE_LL_LLID_RSRVD (0)
366 #define BLE_LL_LLID_DATA_FRAG (1)
367 #define BLE_LL_LLID_DATA_START (2)
368 #define BLE_LL_LLID_CTRL (3)
369
370 /*
371 * CONNECT_REQ
372 * -> InitA (6 bytes)
373 * -> AdvA (6 bytes)
374 * -> LLData (22 bytes)
375 * -> Access address (4 bytes)
376 * -> CRC init (3 bytes)
377 * -> WinSize (1 byte)
378 * -> WinOffset (2 bytes)
379 * -> Interval (2 bytes)
380 * -> Latency (2 bytes)
381 * -> Timeout (2 bytes)
382 * -> Channel Map (5 bytes)
383 * -> Hop Increment (5 bits)
384 * -> SCA (3 bits)
385 *
386 * InitA is the initiators public (TxAdd=0) or random (TxAdd=1) address.
387 * AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address.
388 * LLData contains connection request data.
389 * aa: Link Layer's access address
390 * crc_init: The CRC initialization value used for CRC calculation.
391 * winsize: The transmit window size = winsize * 1.25 msecs
392 * winoffset: The transmit window offset = winoffset * 1.25 msecs
393 * interval: The connection interval = interval * 1.25 msecs.
394 * latency: connection slave latency = latency
395 * timeout: Connection supervision timeout = timeout * 10 msecs.
396 * chanmap: contains channel mapping indicating used and unused data
397 * channels. Only bits that are 1 are usable. LSB is channel 0.
398 * hop_inc: Hop increment used for frequency hopping. Random value in
399 * range of 5 to 16.
400 */
401 #define BLE_CONNECT_REQ_LEN (34)
402 #define BLE_CONNECT_REQ_PDU_LEN (BLE_CONNECT_REQ_LEN + BLE_LL_PDU_HDR_LEN)
403
404 #define BLE_SCAN_REQ_LEN (12)
405 #define BLE_SCAN_RSP_MAX_LEN (37)
406 #define BLE_SCAN_RSP_MAX_EXT_LEN (251)
407
408 /*--- External API ---*/
409 /* Initialize the Link Layer */
410 void ble_ll_init(void);
411
412 /* Reset the Link Layer */
413 int ble_ll_reset(void);
414
415 /* 'Boolean' function returning true if address is a valid random address */
416 int ble_ll_is_valid_random_addr(uint8_t *addr);
417
418 /* Calculate the amount of time in microseconds a PDU with payload length of
419 * 'payload_len' will take to transmit on a PHY 'phy_mode'. */
420 uint32_t ble_ll_pdu_tx_time_get(uint16_t payload_len, int phy_mode);
421
422 /* Calculate maximum octets of PDU payload which can be transmitted during
423 * 'usecs' on a PHY 'phy_mode'. */
424 uint16_t ble_ll_pdu_max_tx_octets_get(uint32_t usecs, int phy_mode);
425
426 /* Is this address a resolvable private address? */
427 int ble_ll_is_rpa(uint8_t *addr, uint8_t addr_type);
428
429 /* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */
430 int ble_ll_is_our_devaddr(uint8_t *addr, int addr_type);
431
432 /* Get identity address 'addr_type' is public (0) or random (!=0) */
433 uint8_t *ble_ll_get_our_devaddr(uint8_t addr_type);
434
435 /**
436 * Called to put a packet on the Link Layer transmit packet queue.
437 *
438 * @param txpdu Pointer to transmit packet
439 */
440 void ble_ll_acl_data_in(struct os_mbuf *txpkt);
441
442 /**
443 * Allocate a pdu (chain) for reception.
444 *
445 * @param len Length of PDU. This includes the PDU header as well as payload.
446 * Does not include MIC if encrypted.
447 *
448 * @return struct os_mbuf* Pointer to mbuf chain to hold received packet
449 */
450 struct os_mbuf *ble_ll_rxpdu_alloc(uint16_t len);
451
452 /* Tell the Link Layer there has been a data buffer overflow */
453 void ble_ll_data_buffer_overflow(void);
454
455 /* Tell the link layer there has been a hardware error */
456 void ble_ll_hw_error(void);
457
458 /*--- PHY interfaces ---*/
459 struct ble_mbuf_hdr;
460
461 /* Called by the PHY when a packet has started */
462 int ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan, struct ble_mbuf_hdr *hdr);
463
464 /* Called by the PHY when a packet reception ends */
465 int ble_ll_rx_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr);
466
467 /* Helper callback to tx mbuf using ble_phy_tx() */
468 uint8_t ble_ll_tx_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte);
469 uint8_t ble_ll_tx_flat_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte);
470
471 /*--- Controller API ---*/
472 void ble_ll_mbuf_init(struct os_mbuf *m, uint8_t pdulen, uint8_t hdr);
473
474 /* Set the link layer state */
475 void ble_ll_state_set(uint8_t ll_state);
476
477 /* Get the link layer state */
478 uint8_t ble_ll_state_get(void);
479
480 /* Send an event to LL task */
481 void ble_ll_event_send(struct ble_npl_event *ev);
482
483 /* Hand received pdu's to LL task */
484 void ble_ll_rx_pdu_in(struct os_mbuf *rxpdu);
485
486 /* Set random address */
487 int ble_ll_set_random_addr(uint8_t *addr, bool hci_adv_ext);
488
489 /* Enable wait for response timer */
490 void ble_ll_wfr_enable(uint32_t cputime);
491
492 /* Disable wait for response timer */
493 void ble_ll_wfr_disable(void);
494
495 /* Wait for response timer expiration callback */
496 void ble_ll_wfr_timer_exp(void *arg);
497
498 /* Read set of features supported by the Link Layer */
499 uint32_t ble_ll_read_supp_features(void);
500
501 /* Read set of states supported by the Link Layer */
502 uint64_t ble_ll_read_supp_states(void);
503
504 /* Check if octets and time are valid. Returns 0 if not valid */
505 int ble_ll_chk_txrx_octets(uint16_t octets);
506 int ble_ll_chk_txrx_time(uint16_t time);
507
508 /* Random numbers */
509 int ble_ll_rand_init(void);
510 void ble_ll_rand_sample(uint8_t rnum);
511 int ble_ll_rand_data_get(uint8_t *buf, uint8_t len);
512 void ble_ll_rand_prand_get(uint8_t *prand);
513 int ble_ll_rand_start(void);
514
515 static inline int
ble_ll_get_addr_type(uint8_t txrxflag)516 ble_ll_get_addr_type(uint8_t txrxflag)
517 {
518 if (txrxflag) {
519 return BLE_HCI_ADV_OWN_ADDR_RANDOM;
520 }
521 return BLE_HCI_ADV_OWN_ADDR_PUBLIC;
522 }
523
524 /* Convert usecs to ticks and round up to nearest tick */
525 static inline uint32_t
ble_ll_usecs_to_ticks_round_up(uint32_t usecs)526 ble_ll_usecs_to_ticks_round_up(uint32_t usecs)
527 {
528 return os_cputime_usecs_to_ticks(usecs + 30);
529 }
530
531 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
532 /* LTK 0x4C68384139F574D836BCF34E9DFB01BF */
533 extern const uint8_t g_bletest_LTK[];
534 extern uint16_t g_bletest_EDIV;
535 extern uint64_t g_bletest_RAND;
536 extern uint64_t g_bletest_SKDm;
537 extern uint64_t g_bletest_SKDs;
538 extern uint32_t g_bletest_IVm;
539 extern uint32_t g_bletest_IVs;
540 #endif
541
542 #if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE)
543 void ble_ll_dtm_init();
544 #endif
545
546 #ifdef __cplusplus
547 }
548 #endif
549
550 #endif /* H_LL_ */
551