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 */ 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 */ 69 static int transport_open(void){ 70 return 0; 71 } 72 73 /** 74 * close transport connection 75 */ 76 static int transport_close(void){ 77 return 0; 78 } 79 80 /** 81 * register packet handler for HCI packets: ACL, SCO, and Events 82 */ 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 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 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 144 static const hci_transport_t * transport_get_instance(void){ 145 return &transport; 146 } 147 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 173 static uint32_t btstack_run_loop_zephyr_get_time_ms(void){ 174 return k_uptime_get_32(); 175 } 176 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 */ 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 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 */ 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 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 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 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_ZEPHYR_READ_STATIC_ADDRESS: 299 params = hci_event_command_complete_get_return_parameters(packet); 300 if(params[0] != 0) 301 break; 302 if(size < 13) 303 break; 304 reverse_48(¶ms[2], local_addr); 305 gap_random_address_set(local_addr); 306 break; 307 default: 308 break; 309 } 310 break; 311 default: 312 break; 313 } 314 } 315 316 int btstack_main(void); 317 318 #if defined(CONFIG_BT_CTLR_ASSERT_HANDLER) 319 void bt_ctlr_assert_handle(char *file, uint32_t line) 320 { 321 printf("CONFIG_BT_CTLR_ASSERT_HANDLER: file %s, line %u\n", file, line); 322 while (1) { 323 } 324 } 325 #endif /* CONFIG_BT_CTLR_ASSERT_HANDLER */ 326 327 int main(void) 328 { 329 #if defined(CONFIG_SOC_SERIES_NRF53X) 330 // switch the nrf5340_cpuapp to 128MHz 331 nrfx_clock_divider_set(NRF_CLOCK_DOMAIN_HFCLK, NRF_CLOCK_HFCLK_DIV_1); 332 #endif 333 334 printf("BTstack booting up..\n"); 335 336 // start with BTstack init - especially configure HCI Transport 337 btstack_memory_init(); 338 btstack_run_loop_init(btstack_run_loop_zephyr_get_instance()); 339 340 #ifdef ENABLE_HCI_DUMP 341 // enable full log output while porting 342 #ifdef ENABLE_SEGGER_RTT_BINARY 343 hci_dump_segger_rtt_binary_open( HCI_DUMP_PACKETLOGGER ); 344 hci_dump_init(hci_dump_segger_rtt_binary_get_instance()); 345 #else 346 hci_dump_init(hci_dump_embedded_stdout_get_instance()); 347 #endif 348 #endif 349 350 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_none_init_instance(); 351 // setup global tlv 352 btstack_tlv_set_instance(btstack_tlv_impl, NULL); 353 354 // setup LE Device DB using TLV 355 le_device_db_tlv_configure(btstack_tlv_impl, NULL); 356 357 // init HCI 358 hci_init(transport_get_instance(), NULL); 359 360 // inform about BTstack state 361 hci_event_callback_registration.callback = &packet_handler; 362 hci_add_event_handler(&hci_event_callback_registration); 363 364 // hand over to btstack embedded code 365 btstack_main(); 366 367 // go 368 btstack_run_loop_execute(); 369 370 while (1){}; 371 return 0; 372 } 373