BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS
Dependencies: FatFileSystem mbed
Fork of BTstack by
BLE_demo.cpp@1:6078e430af82, 2013-02-07 (annotated)
- Committer:
- todotani
- Date:
- Thu Feb 07 14:01:01 2013 +0000
- Revision:
- 1:6078e430af82
- Child:
- 2:24b33d92e086
BLE demo for mbed; Ported SBDBT (RunningElectronics) firmware for BLE to mbed. It can communicate with iOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
todotani | 1:6078e430af82 | 1 | /* |
todotani | 1:6078e430af82 | 2 | * BLE_demo |
todotani | 1:6078e430af82 | 3 | */ |
todotani | 1:6078e430af82 | 4 | |
todotani | 1:6078e430af82 | 5 | // mbed specific |
todotani | 1:6078e430af82 | 6 | #include "mbed.h" |
todotani | 1:6078e430af82 | 7 | Serial pc(USBTX, USBRX); |
todotani | 1:6078e430af82 | 8 | DigitalOut led1(LED1), led2(LED2), led3(LED3); |
todotani | 1:6078e430af82 | 9 | |
todotani | 1:6078e430af82 | 10 | #define HEARTBEAT_PERIOD_MS 500 |
todotani | 1:6078e430af82 | 11 | |
todotani | 1:6078e430af82 | 12 | // from btstack ble_server.c |
todotani | 1:6078e430af82 | 13 | #include "global.h" |
todotani | 1:6078e430af82 | 14 | #include "debug.h" |
todotani | 1:6078e430af82 | 15 | #include "btstack/btstack.h" |
todotani | 1:6078e430af82 | 16 | #include "btstack/hci_cmds.h" |
todotani | 1:6078e430af82 | 17 | #include "btstack/run_loop.h" |
todotani | 1:6078e430af82 | 18 | |
todotani | 1:6078e430af82 | 19 | #include "hci.h" |
todotani | 1:6078e430af82 | 20 | #include "l2cap.h" |
todotani | 1:6078e430af82 | 21 | #include "btstack_memory.h" |
todotani | 1:6078e430af82 | 22 | #include "remote_device_db.h" |
todotani | 1:6078e430af82 | 23 | #include "config.h" |
todotani | 1:6078e430af82 | 24 | |
todotani | 1:6078e430af82 | 25 | #include "att.h" |
todotani | 1:6078e430af82 | 26 | |
todotani | 1:6078e430af82 | 27 | unsigned timer_counter=0; |
todotani | 1:6078e430af82 | 28 | uint8_t startup_state=0; |
todotani | 1:6078e430af82 | 29 | |
todotani | 1:6078e430af82 | 30 | |
todotani | 1:6078e430af82 | 31 | hci_transport_t * hci_transport_picusb_instance(); |
todotani | 1:6078e430af82 | 32 | |
todotani | 1:6078e430af82 | 33 | static att_connection_t att_connection; |
todotani | 1:6078e430af82 | 34 | static uint16_t att_response_handle = 0; |
todotani | 1:6078e430af82 | 35 | static uint16_t att_response_size = 0; |
todotani | 1:6078e430af82 | 36 | static uint8_t att_response_buffer[28]; |
todotani | 1:6078e430af82 | 37 | |
todotani | 1:6078e430af82 | 38 | uint8_t connection_status=0; |
todotani | 1:6078e430af82 | 39 | |
todotani | 1:6078e430af82 | 40 | void hexdump2(void *data, int size){ |
todotani | 1:6078e430af82 | 41 | int i; |
todotani | 1:6078e430af82 | 42 | for (i=0; i<size;i++){ |
todotani | 1:6078e430af82 | 43 | log_info("%02X ", ((uint8_t *)data)[i]); |
todotani | 1:6078e430af82 | 44 | } |
todotani | 1:6078e430af82 | 45 | log_info("\n"); |
todotani | 1:6078e430af82 | 46 | } |
todotani | 1:6078e430af82 | 47 | |
todotani | 1:6078e430af82 | 48 | static void att_try_respond(void){ |
todotani | 1:6078e430af82 | 49 | if (!att_response_size) return; |
todotani | 1:6078e430af82 | 50 | if (!att_response_handle) return; |
todotani | 1:6078e430af82 | 51 | if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) return; |
todotani | 1:6078e430af82 | 52 | |
todotani | 1:6078e430af82 | 53 | // update state before sending packet |
todotani | 1:6078e430af82 | 54 | uint16_t size = att_response_size; |
todotani | 1:6078e430af82 | 55 | att_response_size = 0; |
todotani | 1:6078e430af82 | 56 | l2cap_send_connectionless(att_response_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_buffer, size); |
todotani | 1:6078e430af82 | 57 | } |
todotani | 1:6078e430af82 | 58 | |
todotani | 1:6078e430af82 | 59 | |
todotani | 1:6078e430af82 | 60 | static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ |
todotani | 1:6078e430af82 | 61 | if (packet_type != ATT_DATA_PACKET) return; |
todotani | 1:6078e430af82 | 62 | |
todotani | 1:6078e430af82 | 63 | att_response_handle = handle; |
todotani | 1:6078e430af82 | 64 | att_response_size = att_handle_request(&att_connection, packet, size, att_response_buffer); |
todotani | 1:6078e430af82 | 65 | att_try_respond(); |
todotani | 1:6078e430af82 | 66 | } |
todotani | 1:6078e430af82 | 67 | |
todotani | 1:6078e430af82 | 68 | |
todotani | 1:6078e430af82 | 69 | // enable LE, setup ADV data |
todotani | 1:6078e430af82 | 70 | static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ |
todotani | 1:6078e430af82 | 71 | static bd_addr_t addr; |
todotani | 1:6078e430af82 | 72 | // uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff }; |
todotani | 1:6078e430af82 | 73 | const uint8_t adv_data[31]="\x02\x01\x05" "\x06\x09SBDBT"; |
todotani | 1:6078e430af82 | 74 | switch (packet_type) { |
todotani | 1:6078e430af82 | 75 | |
todotani | 1:6078e430af82 | 76 | case HCI_EVENT_PACKET: |
todotani | 1:6078e430af82 | 77 | switch (packet[0]) { |
todotani | 1:6078e430af82 | 78 | |
todotani | 1:6078e430af82 | 79 | case BTSTACK_EVENT_STATE: |
todotani | 1:6078e430af82 | 80 | // bt stack activated, get started - set local name |
todotani | 1:6078e430af82 | 81 | if (packet[2] == HCI_STATE_WORKING) { |
todotani | 1:6078e430af82 | 82 | log_info("Working!\n"); |
todotani | 1:6078e430af82 | 83 | hci_send_cmd(&hci_read_local_supported_features); |
todotani | 1:6078e430af82 | 84 | } |
todotani | 1:6078e430af82 | 85 | break; |
todotani | 1:6078e430af82 | 86 | |
todotani | 1:6078e430af82 | 87 | case DAEMON_EVENT_HCI_PACKET_SENT: |
todotani | 1:6078e430af82 | 88 | att_try_respond(); |
todotani | 1:6078e430af82 | 89 | break; |
todotani | 1:6078e430af82 | 90 | |
todotani | 1:6078e430af82 | 91 | case HCI_EVENT_LE_META: |
todotani | 1:6078e430af82 | 92 | switch (packet[2]) { |
todotani | 1:6078e430af82 | 93 | case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 94 | // reset connection MTU |
todotani | 1:6078e430af82 | 95 | att_connection.mtu = 23; |
todotani | 1:6078e430af82 | 96 | break; |
todotani | 1:6078e430af82 | 97 | default: |
todotani | 1:6078e430af82 | 98 | break; |
todotani | 1:6078e430af82 | 99 | } |
todotani | 1:6078e430af82 | 100 | break; |
todotani | 1:6078e430af82 | 101 | |
todotani | 1:6078e430af82 | 102 | case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: |
todotani | 1:6078e430af82 | 103 | if (packet[2]) { |
todotani | 1:6078e430af82 | 104 | connection_status=1; |
todotani | 1:6078e430af82 | 105 | log_info("CONNECTED\n"); |
todotani | 1:6078e430af82 | 106 | } else { |
todotani | 1:6078e430af82 | 107 | connection_status=0; |
todotani | 1:6078e430af82 | 108 | log_info("NOT CONNECTED\n"); |
todotani | 1:6078e430af82 | 109 | } |
todotani | 1:6078e430af82 | 110 | break; |
todotani | 1:6078e430af82 | 111 | |
todotani | 1:6078e430af82 | 112 | case HCI_EVENT_DISCONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 113 | att_response_handle =0; |
todotani | 1:6078e430af82 | 114 | att_response_size = 0; |
todotani | 1:6078e430af82 | 115 | |
todotani | 1:6078e430af82 | 116 | // restart advertising |
todotani | 1:6078e430af82 | 117 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 1:6078e430af82 | 118 | break; |
todotani | 1:6078e430af82 | 119 | |
todotani | 1:6078e430af82 | 120 | case HCI_EVENT_COMMAND_COMPLETE: |
todotani | 1:6078e430af82 | 121 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){ |
todotani | 1:6078e430af82 | 122 | bt_flip_addr(addr, &packet[6]); |
todotani | 1:6078e430af82 | 123 | log_info("BD ADDR: %s\n", bd_addr_to_str(addr)); |
todotani | 1:6078e430af82 | 124 | break; |
todotani | 1:6078e430af82 | 125 | } |
todotani | 1:6078e430af82 | 126 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ |
todotani | 1:6078e430af82 | 127 | log_info("Local supported features: %04lX%04lX\n", READ_BT_32(packet, 10), READ_BT_32(packet, 6)); |
todotani | 1:6078e430af82 | 128 | hci_send_cmd(&hci_set_event_mask, 0xffffffff, 0x20001fff); |
todotani | 1:6078e430af82 | 129 | break; |
todotani | 1:6078e430af82 | 130 | } |
todotani | 1:6078e430af82 | 131 | if (COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask)){ |
todotani | 1:6078e430af82 | 132 | hci_send_cmd(&hci_write_le_host_supported, 1, 1); |
todotani | 1:6078e430af82 | 133 | break; |
todotani | 1:6078e430af82 | 134 | } |
todotani | 1:6078e430af82 | 135 | if (COMMAND_COMPLETE_EVENT(packet, hci_write_le_host_supported)){ |
todotani | 1:6078e430af82 | 136 | hci_send_cmd(&hci_le_set_event_mask, 0xffffffff, 0xffffffff); |
todotani | 1:6078e430af82 | 137 | break; |
todotani | 1:6078e430af82 | 138 | } |
todotani | 1:6078e430af82 | 139 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_event_mask)){ |
todotani | 1:6078e430af82 | 140 | hci_send_cmd(&hci_le_read_buffer_size); |
todotani | 1:6078e430af82 | 141 | break; |
todotani | 1:6078e430af82 | 142 | } |
todotani | 1:6078e430af82 | 143 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ |
todotani | 1:6078e430af82 | 144 | log_info("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); |
todotani | 1:6078e430af82 | 145 | hci_send_cmd(&hci_le_read_supported_states); |
todotani | 1:6078e430af82 | 146 | break; |
todotani | 1:6078e430af82 | 147 | } |
todotani | 1:6078e430af82 | 148 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_supported_states)){ |
todotani | 1:6078e430af82 | 149 | hci_send_cmd(&hci_le_set_advertising_parameters, 0x0400, 0x0800, 0, 0, 0, &addr, 0x07, 0); |
todotani | 1:6078e430af82 | 150 | break; |
todotani | 1:6078e430af82 | 151 | } |
todotani | 1:6078e430af82 | 152 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_parameters)){ |
todotani | 1:6078e430af82 | 153 | hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data); |
todotani | 1:6078e430af82 | 154 | break; |
todotani | 1:6078e430af82 | 155 | } |
todotani | 1:6078e430af82 | 156 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)){ |
todotani | 1:6078e430af82 | 157 | hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data); |
todotani | 1:6078e430af82 | 158 | break; |
todotani | 1:6078e430af82 | 159 | } |
todotani | 1:6078e430af82 | 160 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)){ |
todotani | 1:6078e430af82 | 161 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 1:6078e430af82 | 162 | break; |
todotani | 1:6078e430af82 | 163 | } |
todotani | 1:6078e430af82 | 164 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertise_enable)){ |
todotani | 1:6078e430af82 | 165 | hci_discoverable_control(1); |
todotani | 1:6078e430af82 | 166 | log_info("startup_state=1\n"); |
todotani | 1:6078e430af82 | 167 | startup_state=1; |
todotani | 1:6078e430af82 | 168 | led1 = 0; |
todotani | 1:6078e430af82 | 169 | break; |
todotani | 1:6078e430af82 | 170 | } |
todotani | 1:6078e430af82 | 171 | |
todotani | 1:6078e430af82 | 172 | } |
todotani | 1:6078e430af82 | 173 | } |
todotani | 1:6078e430af82 | 174 | } |
todotani | 1:6078e430af82 | 175 | |
todotani | 1:6078e430af82 | 176 | // test profile |
todotani | 1:6078e430af82 | 177 | #include "profile.h" |
todotani | 1:6078e430af82 | 178 | |
todotani | 1:6078e430af82 | 179 | static uint8_t strbuf[80]; |
todotani | 1:6078e430af82 | 180 | static uint8_t ledvalue; |
todotani | 1:6078e430af82 | 181 | |
todotani | 1:6078e430af82 | 182 | // read requests |
todotani | 1:6078e430af82 | 183 | static uint16_t att_read_callback(uint16_t handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ |
todotani | 1:6078e430af82 | 184 | uint16_t ret=0,val; |
todotani | 1:6078e430af82 | 185 | |
todotani | 1:6078e430af82 | 186 | if(buffer){ |
todotani | 1:6078e430af82 | 187 | log_info("READ Callback, handle %04x\n", handle); |
todotani | 1:6078e430af82 | 188 | led1 = 1; |
todotani | 1:6078e430af82 | 189 | } |
todotani | 1:6078e430af82 | 190 | switch(handle){ |
todotani | 1:6078e430af82 | 191 | case 0x000b: |
todotani | 1:6078e430af82 | 192 | ret=strlen((char*)strbuf); |
todotani | 1:6078e430af82 | 193 | if(buffer && ret<=buffer_size){ |
todotani | 1:6078e430af82 | 194 | log_info("Read text: %s\n", strbuf); |
todotani | 1:6078e430af82 | 195 | memcpy(buffer,strbuf,ret); |
todotani | 1:6078e430af82 | 196 | } |
todotani | 1:6078e430af82 | 197 | break; |
todotani | 1:6078e430af82 | 198 | case 0x000d: |
todotani | 1:6078e430af82 | 199 | if(buffer && buffer_size){ |
todotani | 1:6078e430af82 | 200 | log_info("Read value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 201 | buffer[0]=ledvalue; |
todotani | 1:6078e430af82 | 202 | } |
todotani | 1:6078e430af82 | 203 | ret=1; |
todotani | 1:6078e430af82 | 204 | break; |
todotani | 1:6078e430af82 | 205 | case 0x000f: |
todotani | 1:6078e430af82 | 206 | if(buffer && buffer_size>=2){ |
todotani | 1:6078e430af82 | 207 | val=timer_counter; |
todotani | 1:6078e430af82 | 208 | log_info("Read value: %u\n", val); |
todotani | 1:6078e430af82 | 209 | buffer[0]=val&0xff; |
todotani | 1:6078e430af82 | 210 | buffer[1]=val>>8; |
todotani | 1:6078e430af82 | 211 | } |
todotani | 1:6078e430af82 | 212 | ret=2; |
todotani | 1:6078e430af82 | 213 | break; |
todotani | 1:6078e430af82 | 214 | } |
todotani | 1:6078e430af82 | 215 | return ret; |
todotani | 1:6078e430af82 | 216 | } |
todotani | 1:6078e430af82 | 217 | |
todotani | 1:6078e430af82 | 218 | // write requests |
todotani | 1:6078e430af82 | 219 | static void att_write_callback(uint16_t handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size, signature_t * signature){ |
todotani | 1:6078e430af82 | 220 | log_info("WRITE Callback, handle %04x\n", handle); |
todotani | 1:6078e430af82 | 221 | led1 = 1; |
todotani | 1:6078e430af82 | 222 | switch(handle){ |
todotani | 1:6078e430af82 | 223 | case 0x000b: |
todotani | 1:6078e430af82 | 224 | buffer[buffer_size]=0; |
todotani | 1:6078e430af82 | 225 | if(sizeof(strbuf)>buffer_size){ |
todotani | 1:6078e430af82 | 226 | strcpy((char*)strbuf,(char*)buffer); |
todotani | 1:6078e430af82 | 227 | } |
todotani | 1:6078e430af82 | 228 | log_info("New text: %s\n", buffer); |
todotani | 1:6078e430af82 | 229 | break; |
todotani | 1:6078e430af82 | 230 | case 0x000d: |
todotani | 1:6078e430af82 | 231 | log_info("New value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 232 | ledvalue=buffer[0]; |
todotani | 1:6078e430af82 | 233 | if (buffer[0]){ |
todotani | 1:6078e430af82 | 234 | led1 = 1; |
todotani | 1:6078e430af82 | 235 | } else { |
todotani | 1:6078e430af82 | 236 | led1 = 0; |
todotani | 1:6078e430af82 | 237 | } |
todotani | 1:6078e430af82 | 238 | break; |
todotani | 1:6078e430af82 | 239 | } |
todotani | 1:6078e430af82 | 240 | } |
todotani | 1:6078e430af82 | 241 | |
todotani | 1:6078e430af82 | 242 | |
todotani | 1:6078e430af82 | 243 | static void heartbeat_handler(struct timer *ts){ |
todotani | 1:6078e430af82 | 244 | run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); |
todotani | 1:6078e430af82 | 245 | run_loop_add_timer(ts); |
todotani | 1:6078e430af82 | 246 | led2 = !led2; |
todotani | 1:6078e430af82 | 247 | } |
todotani | 1:6078e430af82 | 248 | |
todotani | 1:6078e430af82 | 249 | |
todotani | 1:6078e430af82 | 250 | // main |
todotani | 1:6078e430af82 | 251 | int main(void) |
todotani | 1:6078e430af82 | 252 | { |
todotani | 1:6078e430af82 | 253 | pc.baud(115200); |
todotani | 1:6078e430af82 | 254 | log_info("%s\n", __FILE__); |
todotani | 1:6078e430af82 | 255 | |
todotani | 1:6078e430af82 | 256 | // init LEDs |
todotani | 1:6078e430af82 | 257 | led1 = led2 = led3 = 1; |
todotani | 1:6078e430af82 | 258 | |
todotani | 1:6078e430af82 | 259 | /// GET STARTED with BTstack /// |
todotani | 1:6078e430af82 | 260 | btstack_memory_init(); |
todotani | 1:6078e430af82 | 261 | run_loop_init(RUN_LOOP_EMBEDDED); |
todotani | 1:6078e430af82 | 262 | |
todotani | 1:6078e430af82 | 263 | // init HCI |
todotani | 1:6078e430af82 | 264 | // use BlueUSB |
todotani | 1:6078e430af82 | 265 | hci_transport_t* transport = hci_transport_usb_instance(); |
todotani | 1:6078e430af82 | 266 | bt_control_t * control = NULL; |
todotani | 1:6078e430af82 | 267 | hci_uart_config_t * config = NULL; |
todotani | 1:6078e430af82 | 268 | remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; |
todotani | 1:6078e430af82 | 269 | hci_init(transport, config, control, remote_db); |
todotani | 1:6078e430af82 | 270 | |
todotani | 1:6078e430af82 | 271 | // use eHCILL |
todotani | 1:6078e430af82 | 272 | // bt_control_cc256x_enable_ehcill(1); |
todotani | 1:6078e430af82 | 273 | |
todotani | 1:6078e430af82 | 274 | // set up l2cap_le |
todotani | 1:6078e430af82 | 275 | l2cap_init(); |
todotani | 1:6078e430af82 | 276 | l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); |
todotani | 1:6078e430af82 | 277 | l2cap_register_packet_handler(packet_handler); |
todotani | 1:6078e430af82 | 278 | |
todotani | 1:6078e430af82 | 279 | // set up ATT |
todotani | 1:6078e430af82 | 280 | att_set_db(profile_data); |
todotani | 1:6078e430af82 | 281 | att_set_write_callback(att_write_callback); |
todotani | 1:6078e430af82 | 282 | att_set_read_callback(att_read_callback); |
todotani | 1:6078e430af82 | 283 | att_dump_attributes(); |
todotani | 1:6078e430af82 | 284 | att_connection.mtu = 27; |
todotani | 1:6078e430af82 | 285 | |
todotani | 1:6078e430af82 | 286 | // set one-shot timer |
todotani | 1:6078e430af82 | 287 | timer_source_t heartbeat; |
todotani | 1:6078e430af82 | 288 | heartbeat.process = &heartbeat_handler; |
todotani | 1:6078e430af82 | 289 | run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); |
todotani | 1:6078e430af82 | 290 | run_loop_add_timer(&heartbeat); |
todotani | 1:6078e430af82 | 291 | |
todotani | 1:6078e430af82 | 292 | log_info("Run...\n\r"); |
todotani | 1:6078e430af82 | 293 | |
todotani | 1:6078e430af82 | 294 | // turn on! |
todotani | 1:6078e430af82 | 295 | hci_power_control(HCI_POWER_ON); |
todotani | 1:6078e430af82 | 296 | |
todotani | 1:6078e430af82 | 297 | // go! |
todotani | 1:6078e430af82 | 298 | run_loop_execute(); |
todotani | 1:6078e430af82 | 299 | |
todotani | 1:6078e430af82 | 300 | // happy compiler! |
todotani | 1:6078e430af82 | 301 | return 0; |
todotani | 1:6078e430af82 | 302 | } |