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