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 #include "nrf.h" 22 23 // BTstack 24 #include "btstack_debug.h" 25 #include "btstack_event.h" 26 #include "btstack_memory.h" 27 #include "hci.h" 28 #include "hci_dump.h" 29 #include "hci_dump_embedded_stdout.h" 30 #include "hci_transport.h" 31 32 #include "btstack_tlv.h" 33 #include "btstack_tlv_none.h" 34 #include "ble/le_device_db_tlv.h" 35 36 static K_FIFO_DEFINE(tx_queue); 37 static K_FIFO_DEFINE(rx_queue); 38 39 // 40 // hci_transport_zephyr.c 41 // 42 43 static void (*transport_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 44 45 /** 46 * init transport 47 * @param transport_config 48 */ 49 static void transport_init(const void *transport_config){ 50 /* startup Controller */ 51 bt_enable_raw(&rx_queue); 52 } 53 54 /** 55 * open transport connection 56 */ 57 static int transport_open(void){ 58 return 0; 59 } 60 61 /** 62 * close transport connection 63 */ 64 static int transport_close(void){ 65 return 0; 66 } 67 68 /** 69 * register packet handler for HCI packets: ACL, SCO, and Events 70 */ 71 static void transport_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 72 transport_packet_handler = handler; 73 } 74 75 static void send_hardware_error(uint8_t error_code){ 76 // hci_outgoing_event[0] = HCI_EVENT_HARDWARE_ERROR; 77 // hci_outgoing_event[1] = 1; 78 // hci_outgoing_event[2] = error_code; 79 // hci_outgoing_event_ready = 1; 80 } 81 82 static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){ 83 struct net_buf *buf; 84 switch (packet_type){ 85 case HCI_COMMAND_DATA_PACKET: 86 buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT, packet, size); 87 if (!buf) { 88 log_error("No available command buffers!\n"); 89 break; 90 } 91 92 memcpy(net_buf_add(buf, size), packet, size); 93 bt_send(buf); 94 break; 95 case HCI_ACL_DATA_PACKET: 96 buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_NO_WAIT, packet, size); 97 if (!buf) { 98 log_error("No available ACL buffers!\n"); 99 break; 100 } 101 102 memcpy(net_buf_add(buf, size), packet, size); 103 bt_send(buf); 104 break; 105 default: 106 send_hardware_error(0x01); // invalid HCI packet 107 break; 108 } 109 110 return 0; 111 } 112 113 static const hci_transport_t transport = { 114 /* const char * name; */ "zephyr", 115 /* void (*init) (const void *transport_config); */ &transport_init, 116 /* int (*open)(void); */ &transport_open, 117 /* int (*close)(void); */ &transport_close, 118 /* void (*register_packet_handler)(void (*handler)(...); */ &transport_register_packet_handler, 119 /* int (*can_send_packet_now)(uint8_t packet_type); */ NULL, 120 /* int (*send_packet)(...); */ &transport_send_packet, 121 /* int (*set_baudrate)(uint32_t baudrate); */ NULL, 122 /* void (*reset_link)(void); */ NULL, 123 }; 124 125 static const hci_transport_t * transport_get_instance(void){ 126 return &transport; 127 } 128 129 static void transport_deliver_controller_packet(struct net_buf * buf){ 130 uint16_t size = buf->len; 131 uint8_t * packet = buf->data; 132 switch (bt_buf_get_type(buf)) { 133 case BT_BUF_ACL_IN: 134 transport_packet_handler(HCI_ACL_DATA_PACKET, packet, size); 135 break; 136 case BT_BUF_EVT: 137 transport_packet_handler(HCI_EVENT_PACKET, packet, size); 138 break; 139 default: 140 log_error("Unknown type %u\n", bt_buf_get_type(buf)); 141 break; 142 } 143 net_buf_unref(buf); 144 } 145 146 // btstack_run_loop_zephry.c 147 148 // the run loop 149 //static btstack_linked_list_t timers; 150 151 // TODO: handle 32 bit ms time overrun 152 static uint32_t btstack_run_loop_zephyr_get_time_ms(void){ 153 return k_uptime_get_32(); 154 } 155 156 static void btstack_run_loop_zephyr_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 157 ts->timeout = k_uptime_get_32() + 1 + timeout_in_ms; 158 } 159 160 /** 161 * Execute run_loop 162 */ 163 static void btstack_run_loop_zephyr_execute(void) { 164 while (1) { 165 // process timers 166 uint32_t now = k_uptime_get_32(); 167 btstack_run_loop_base_process_timers(now); 168 169 // get time until next timer expires 170 k_timeout_t timeout; 171 timeout.ticks = btstack_run_loop_base_get_time_until_timeout(now); 172 if (timeout.ticks < 0){ 173 timeout.ticks = K_TICKS_FOREVER; 174 } 175 176 // process RX fifo only 177 struct net_buf *buf = net_buf_get(&rx_queue, timeout); 178 if (buf){ 179 transport_deliver_controller_packet(buf); 180 } 181 } 182 } 183 184 static void btstack_run_loop_zephyr_btstack_run_loop_init(void){ 185 btstack_run_loop_base_init(); 186 } 187 188 static const btstack_run_loop_t btstack_run_loop_zephyr = { 189 &btstack_run_loop_zephyr_btstack_run_loop_init, 190 NULL, 191 NULL, 192 NULL, 193 NULL, 194 &btstack_run_loop_zephyr_set_timer, 195 &btstack_run_loop_base_add_timer, 196 &btstack_run_loop_base_remove_timer, 197 &btstack_run_loop_zephyr_execute, 198 &btstack_run_loop_base_dump_timer, 199 &btstack_run_loop_zephyr_get_time_ms, 200 }; 201 /** 202 * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 203 */ 204 const btstack_run_loop_t * btstack_run_loop_zephyr_get_instance(void){ 205 return &btstack_run_loop_zephyr; 206 } 207 208 static btstack_packet_callback_registration_t hci_event_callback_registration; 209 210 static bd_addr_t static_address; 211 212 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 213 if (packet_type != HCI_EVENT_PACKET) return; 214 if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; 215 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 216 printf("BTstack up and running as %s.\n", bd_addr_to_str(static_address)); 217 } 218 219 int btstack_main(void); 220 221 #if defined(CONFIG_BT_CTLR_ASSERT_HANDLER) 222 void bt_ctlr_assert_handle(char *file, uint32_t line) 223 { 224 printf("CONFIG_BT_CTLR_ASSERT_HANDLER: file %s, line %u\n", file, line); 225 while (1) { 226 } 227 } 228 #endif /* CONFIG_BT_CTLR_ASSERT_HANDLER */ 229 230 int main(void) 231 { 232 // configure console UART by replacing CONFIG_UART_NRF5_BAUD_RATE with 115200 in uart_console.c 233 234 printf("BTstack booting up..\n"); 235 236 // start with BTstack init - especially configure HCI Transport 237 btstack_memory_init(); 238 btstack_run_loop_init(btstack_run_loop_zephyr_get_instance()); 239 240 // enable full log output while porting 241 hci_dump_init(hci_dump_embedded_stdout_get_instance()); 242 243 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_none_init_instance(); 244 // setup global tlv 245 btstack_tlv_set_instance(btstack_tlv_impl, NULL); 246 247 // setup LE Device DB using TLV 248 le_device_db_tlv_configure(btstack_tlv_impl, NULL); 249 250 // init HCI 251 hci_init(transport_get_instance(), NULL); 252 253 // nRF5 chipsets don't have an official public address 254 // Instead, a Static Random Address is assigned during manufacturing 255 // let's use it as well 256 big_endian_store_16(static_address, 0, NRF_FICR->DEVICEADDR[1] | 0xc000); 257 big_endian_store_32(static_address, 2, NRF_FICR->DEVICEADDR[0]); 258 gap_random_address_set(static_address); 259 260 // inform about BTstack state 261 hci_event_callback_registration.callback = &packet_handler; 262 hci_add_event_handler(&hci_event_callback_registration); 263 264 // hand over to btstack embedded code 265 btstack_main(); 266 267 // go 268 btstack_run_loop_execute(); 269 270 while (1){}; 271 return 0; 272 } 273