1 /*
2 * Copyright (c) 2016 Nordic Semiconductor ASA
3 * Copyright (c) 2015-2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <errno.h>
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <zephyr/kernel.h>
14 #include <zephyr/bluetooth/bluetooth.h>
15 #include <zephyr/bluetooth/l2cap.h>
16 #include <zephyr/bluetooth/hci.h>
17 #include <zephyr/bluetooth/buf.h>
18 #include <zephyr/bluetooth/hci_raw.h>
19
20 // Nordic NDK
21 #if defined(CONFIG_HAS_NORDIC_DRIVERS)
22 #include "nrf.h"
23 #include <nrfx_clock.h>
24 #endif
25
26 // BTstack
27 #include "btstack_debug.h"
28 #include "btstack_event.h"
29 #include "btstack_memory.h"
30 #include "hci.h"
31 #include "hci_dump.h"
32 #ifdef ENABLE_HCI_DUMP
33 #ifdef ENABLE_SEGGER_RTT_BINARY
34 #include "hci_dump_segger_rtt_binary.h"
35 #else
36 #include "hci_dump_embedded_stdout.h"
37 #endif
38 #endif
39 #include "hci_transport.h"
40 #include "bluetooth_company_id.h"
41 #include "btstack_chipset_zephyr.h"
42
43 #include "btstack_tlv.h"
44 #include "btstack_tlv_none.h"
45 #include "ble/le_device_db_tlv.h"
46
47 static K_FIFO_DEFINE(tx_queue);
48 static K_FIFO_DEFINE(rx_queue);
49
50 //
51 // hci_transport_zephyr.c
52 //
53
54 static void (*transport_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
55
56 /**
57 * init transport
58 * @param transport_config
59 */
transport_init(const void * transport_config)60 static void transport_init(const void *transport_config){
61 /* startup Controller */
62 bt_enable_raw(&rx_queue);
63 bt_hci_raw_set_mode( BT_HCI_RAW_MODE_PASSTHROUGH );
64 }
65
66 /**
67 * open transport connection
68 */
transport_open(void)69 static int transport_open(void){
70 return 0;
71 }
72
73 /**
74 * close transport connection
75 */
transport_close(void)76 static int transport_close(void){
77 return 0;
78 }
79
80 /**
81 * register packet handler for HCI packets: ACL, SCO, and Events
82 */
transport_register_packet_handler(void (* handler)(uint8_t packet_type,uint8_t * packet,uint16_t size))83 static void transport_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
84 transport_packet_handler = handler;
85 }
86
send_hardware_error(uint8_t error_code)87 static void send_hardware_error(uint8_t error_code){
88 // hci_outgoing_event[0] = HCI_EVENT_HARDWARE_ERROR;
89 // hci_outgoing_event[1] = 1;
90 // hci_outgoing_event[2] = error_code;
91 // hci_outgoing_event_ready = 1;
92 }
93
transport_send_packet(uint8_t packet_type,uint8_t * packet,int size)94 static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
95 struct net_buf *buf;
96 switch (packet_type){
97 case HCI_COMMAND_DATA_PACKET:
98 buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT, packet, size);
99 if (!buf) {
100 log_error("No available command buffers!\n");
101 break;
102 }
103
104 bt_send(buf);
105 break;
106 case HCI_ACL_DATA_PACKET:
107 buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_NO_WAIT, packet, size);
108 if (!buf) {
109 log_error("No available ACL buffers!\n");
110 break;
111 }
112
113 bt_send(buf);
114 break;
115 case HCI_ISO_DATA_PACKET:
116 buf = bt_buf_get_tx(BT_BUF_ISO_OUT, K_NO_WAIT, packet, size);
117 if (!buf) {
118 log_error("No available ISO buffers!\n");
119 break;
120 }
121
122 bt_send(buf);
123 break;
124 default:
125 send_hardware_error(0x01); // invalid HCI packet
126 break;
127 }
128
129 return 0;
130 }
131
132 static const hci_transport_t transport = {
133 /* const char * name; */ "zephyr",
134 /* void (*init) (const void *transport_config); */ &transport_init,
135 /* int (*open)(void); */ &transport_open,
136 /* int (*close)(void); */ &transport_close,
137 /* void (*register_packet_handler)(void (*handler)(...); */ &transport_register_packet_handler,
138 /* int (*can_send_packet_now)(uint8_t packet_type); */ NULL,
139 /* int (*send_packet)(...); */ &transport_send_packet,
140 /* int (*set_baudrate)(uint32_t baudrate); */ NULL,
141 /* void (*reset_link)(void); */ NULL,
142 };
143
transport_get_instance(void)144 static const hci_transport_t * transport_get_instance(void){
145 return &transport;
146 }
147
transport_deliver_controller_packet(struct net_buf * buf)148 static void transport_deliver_controller_packet(struct net_buf * buf){
149 uint16_t size = buf->len;
150 uint8_t * packet = buf->data;
151 switch (bt_buf_get_type(buf)) {
152 case BT_BUF_ISO_IN:
153 transport_packet_handler(HCI_ISO_DATA_PACKET, packet, size);
154 break;
155 case BT_BUF_ACL_IN:
156 transport_packet_handler(HCI_ACL_DATA_PACKET, packet, size);
157 break;
158 case BT_BUF_EVT:
159 transport_packet_handler(HCI_EVENT_PACKET, packet, size);
160 break;
161 default:
162 log_error("Unknown type %u\n", bt_buf_get_type(buf));
163 break;
164 }
165 net_buf_unref(buf);
166 }
167
168 // btstack_run_loop_zephry.c
169
170 // the run loop
171
172 // TODO: handle 32 bit ms time overrun
btstack_run_loop_zephyr_get_time_ms(void)173 static uint32_t btstack_run_loop_zephyr_get_time_ms(void){
174 return k_uptime_get_32();
175 }
176
btstack_run_loop_zephyr_set_timer(btstack_timer_source_t * ts,uint32_t timeout_in_ms)177 static void btstack_run_loop_zephyr_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
178 ts->timeout = k_uptime_get_32() + 1 + timeout_in_ms;
179 }
180
181 /**
182 * Execute run_loop
183 */
btstack_run_loop_zephyr_execute(void)184 static void btstack_run_loop_zephyr_execute(void) {
185 while (1) {
186 // process timers
187 uint32_t now = k_uptime_get_32();
188 btstack_run_loop_base_process_timers(now);
189
190 // get time until next timer expires
191 k_timeout_t timeout;
192 timeout.ticks = btstack_run_loop_base_get_time_until_timeout(now);
193 if (timeout.ticks < 0){
194 timeout.ticks = K_TICKS_FOREVER;
195 }
196
197 // process RX fifo only
198 struct net_buf *buf = net_buf_get(&rx_queue, timeout);
199 if (buf){
200 transport_deliver_controller_packet(buf);
201 }
202 }
203 }
204
btstack_run_loop_zephyr_btstack_run_loop_init(void)205 static void btstack_run_loop_zephyr_btstack_run_loop_init(void){
206 btstack_run_loop_base_init();
207 }
208
209 static const btstack_run_loop_t btstack_run_loop_zephyr = {
210 &btstack_run_loop_zephyr_btstack_run_loop_init,
211 NULL,
212 NULL,
213 NULL,
214 NULL,
215 &btstack_run_loop_zephyr_set_timer,
216 &btstack_run_loop_base_add_timer,
217 &btstack_run_loop_base_remove_timer,
218 &btstack_run_loop_zephyr_execute,
219 &btstack_run_loop_base_dump_timer,
220 &btstack_run_loop_zephyr_get_time_ms,
221 };
222 /**
223 * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
224 */
btstack_run_loop_zephyr_get_instance(void)225 static const btstack_run_loop_t * btstack_run_loop_zephyr_get_instance(void){
226 return &btstack_run_loop_zephyr;
227 }
228
229 static btstack_packet_callback_registration_t hci_event_callback_registration;
230
231 static bd_addr_t local_addr = { 0 };
232
nrf_get_static_random_addr(bd_addr_t addr)233 void nrf_get_static_random_addr( bd_addr_t addr ) {
234 // nRF5 chipsets don't have an official public address
235 // Instead, a Static Random Address is assigned during manufacturing
236 // let's use it as well
237 #if defined(CONFIG_SOC_SERIES_NRF51X) || defined(CONFIG_SOC_SERIES_NRF52X)
238 big_endian_store_16(addr, 0, NRF_FICR->DEVICEADDR[1] | 0xc000);
239 big_endian_store_32(addr, 2, NRF_FICR->DEVICEADDR[0]);
240 #elif defined(CONFIG_SOC_SERIES_NRF53X)
241 big_endian_store_16(addr, 0, NRF_FICR->INFO.DEVICEID[1] | 0xc000 );
242 big_endian_store_32(addr, 2, NRF_FICR->INFO.DEVICEID[0]);
243 #endif
244 }
245
local_version_information_handler(uint8_t * packet)246 static void local_version_information_handler(uint8_t * packet){
247 printf("Local version information:\n");
248 uint16_t hci_version = packet[6];
249 uint16_t hci_revision = little_endian_read_16(packet, 7);
250 uint16_t lmp_version = packet[9];
251 uint16_t manufacturer = little_endian_read_16(packet, 10);
252 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
253 printf("- HCI Version %#04x\n", hci_version);
254 printf("- HCI Revision %#04x\n", hci_revision);
255 printf("- LMP Version %#04x\n", lmp_version);
256 printf("- LMP Subversion %#04x\n", lmp_subversion);
257 printf("- Manufacturer %#04x\n", manufacturer);
258 switch (manufacturer){
259 case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
260 case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
261 printf("Nordic Semiconductor nRF5 chipset.\n");
262 hci_set_chipset(btstack_chipset_zephyr_instance());
263 break;
264 case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
265 printf("PacketCraft HCI Controller\n");
266 nrf_get_static_random_addr( local_addr );
267 gap_random_address_set( local_addr );
268 break;
269 default:
270 printf("Unknown manufacturer.\n");
271 nrf_get_static_random_addr( local_addr );
272 gap_random_address_set( local_addr );
273 break;
274 }
275 }
276
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)277 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
278 const uint8_t *params;
279 if (packet_type != HCI_EVENT_PACKET) return;
280 switch (hci_event_packet_get_type(packet)){
281 case BTSTACK_EVENT_STATE:
282 switch(btstack_event_state_get_state(packet)){
283 case HCI_STATE_WORKING:
284 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
285 break;
286 case HCI_STATE_OFF:
287 log_info("Good bye, see you.\n");
288 break;
289 default:
290 break;
291 }
292 break;
293 case HCI_EVENT_COMMAND_COMPLETE:
294 switch (hci_event_command_complete_get_command_opcode(packet)){
295 case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
296 local_version_information_handler(packet);
297 break;
298 case HCI_OPCODE_HCI_READ_BD_ADDR:
299 params = hci_event_command_complete_get_return_parameters(packet);
300 if(params[0] != 0)
301 break;
302 if(size < 12)
303 break;
304 reverse_48(¶ms[1], local_addr);
305 break;
306 case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
307 params = hci_event_command_complete_get_return_parameters(packet);
308 if(params[0] != 0)
309 break;
310 if(size < 13)
311 break;
312 reverse_48(¶ms[2], local_addr);
313 gap_random_address_set(local_addr);
314 break;
315 default:
316 break;
317 }
318 break;
319 default:
320 break;
321 }
322 }
323
324 int btstack_main(void);
325
326 #if defined(CONFIG_BT_CTLR_ASSERT_HANDLER)
bt_ctlr_assert_handle(char * file,uint32_t line)327 void bt_ctlr_assert_handle(char *file, uint32_t line)
328 {
329 printf("CONFIG_BT_CTLR_ASSERT_HANDLER: file %s, line %u\n", file, line);
330 while (1) {
331 }
332 }
333 #endif /* CONFIG_BT_CTLR_ASSERT_HANDLER */
334
main(void)335 int main(void)
336 {
337 #if defined(CONFIG_SOC_SERIES_NRF53X)
338 // switch the nrf5340_cpuapp to 128MHz
339 nrfx_clock_divider_set(NRF_CLOCK_DOMAIN_HFCLK, NRF_CLOCK_HFCLK_DIV_1);
340 #endif
341
342 printf("BTstack booting up..\n");
343
344 // start with BTstack init - especially configure HCI Transport
345 btstack_memory_init();
346 btstack_run_loop_init(btstack_run_loop_zephyr_get_instance());
347
348 #ifdef ENABLE_HCI_DUMP
349 // enable full log output while porting
350 #ifdef ENABLE_SEGGER_RTT_BINARY
351 hci_dump_segger_rtt_binary_open( HCI_DUMP_PACKETLOGGER );
352 hci_dump_init(hci_dump_segger_rtt_binary_get_instance());
353 #else
354 hci_dump_init(hci_dump_embedded_stdout_get_instance());
355 #endif
356 #endif
357
358 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_none_init_instance();
359 // setup global tlv
360 btstack_tlv_set_instance(btstack_tlv_impl, NULL);
361
362 // setup LE Device DB using TLV
363 le_device_db_tlv_configure(btstack_tlv_impl, NULL);
364
365 // init HCI
366 hci_init(transport_get_instance(), NULL);
367
368 // inform about BTstack state
369 hci_event_callback_registration.callback = &packet_handler;
370 hci_add_event_handler(&hci_event_callback_registration);
371
372 // hand over to btstack embedded code
373 btstack_main();
374
375 // go
376 btstack_run_loop_execute();
377
378 while (1){};
379 return 0;
380 }
381