ban4jp -
/
BTstack_test
https://mbed.org/forum/ja/topic/4890/
Fork of BTstack by
spp_demo.cpp@3:316daabf0d24, 2014-05-09 (annotated)
- Committer:
- ban4jp
- Date:
- Fri May 09 15:54:35 2014 +0000
- Revision:
- 3:316daabf0d24
- Parent:
- 1:b657594559be
??????????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ban4jp | 3:316daabf0d24 | 1 | #if 1 |
va009039 | 0:1ed23ab1345f | 2 | /* |
va009039 | 0:1ed23ab1345f | 3 | * spp_demo |
va009039 | 0:1ed23ab1345f | 4 | */ |
va009039 | 0:1ed23ab1345f | 5 | #include "mbed.h" |
va009039 | 0:1ed23ab1345f | 6 | #include <btstack/hci_cmds.h> |
va009039 | 0:1ed23ab1345f | 7 | #include <btstack/run_loop.h> |
va009039 | 0:1ed23ab1345f | 8 | #include <btstack/sdp_util.h> |
va009039 | 0:1ed23ab1345f | 9 | #include "hci.h" |
va009039 | 0:1ed23ab1345f | 10 | #include "l2cap.h" |
va009039 | 0:1ed23ab1345f | 11 | #include "btstack_memory.h" |
va009039 | 0:1ed23ab1345f | 12 | #include "remote_device_db.h" |
va009039 | 0:1ed23ab1345f | 13 | #include "rfcomm.h" |
va009039 | 1:b657594559be | 14 | extern "C" { |
va009039 | 0:1ed23ab1345f | 15 | #include "sdp.h" |
va009039 | 1:b657594559be | 16 | } |
va009039 | 0:1ed23ab1345f | 17 | #include "config.h" |
va009039 | 0:1ed23ab1345f | 18 | #include "debug.h" |
va009039 | 0:1ed23ab1345f | 19 | #include "bd_addr.h" // class bd_addr |
va009039 | 0:1ed23ab1345f | 20 | |
va009039 | 0:1ed23ab1345f | 21 | Serial pc(USBTX, USBRX); |
ban4jp | 3:316daabf0d24 | 22 | DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); |
va009039 | 0:1ed23ab1345f | 23 | |
va009039 | 0:1ed23ab1345f | 24 | #define HEARTBEAT_PERIOD_MS 500 |
va009039 | 0:1ed23ab1345f | 25 | |
va009039 | 0:1ed23ab1345f | 26 | static uint8_t rfcomm_channel_nr = 1; |
va009039 | 0:1ed23ab1345f | 27 | static uint16_t rfcomm_channel_id = 0; |
va009039 | 0:1ed23ab1345f | 28 | static uint8_t spp_service_buffer[128]; |
va009039 | 0:1ed23ab1345f | 29 | |
ban4jp | 3:316daabf0d24 | 30 | static void data_recv(uint8_t *packet, uint16_t size); |
ban4jp | 3:316daabf0d24 | 31 | |
va009039 | 0:1ed23ab1345f | 32 | // Bluetooth logic |
va009039 | 0:1ed23ab1345f | 33 | static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ |
va009039 | 0:1ed23ab1345f | 34 | bd_addr_t event_addr; |
va009039 | 0:1ed23ab1345f | 35 | uint8_t rfcomm_channel_nr; |
va009039 | 0:1ed23ab1345f | 36 | uint16_t mtu; |
va009039 | 0:1ed23ab1345f | 37 | int err; |
va009039 | 0:1ed23ab1345f | 38 | switch (packet_type) { |
va009039 | 0:1ed23ab1345f | 39 | case HCI_EVENT_PACKET: |
va009039 | 0:1ed23ab1345f | 40 | switch (packet[0]) { |
va009039 | 0:1ed23ab1345f | 41 | |
va009039 | 0:1ed23ab1345f | 42 | case BTSTACK_EVENT_STATE: |
va009039 | 0:1ed23ab1345f | 43 | // bt stack activated, get started - set local name |
va009039 | 0:1ed23ab1345f | 44 | if (packet[2] == HCI_STATE_WORKING) { |
va009039 | 0:1ed23ab1345f | 45 | hci_send_cmd(&hci_write_local_name, "mbed"); |
va009039 | 0:1ed23ab1345f | 46 | } |
va009039 | 0:1ed23ab1345f | 47 | break; |
va009039 | 0:1ed23ab1345f | 48 | |
va009039 | 0:1ed23ab1345f | 49 | case HCI_EVENT_COMMAND_COMPLETE: |
va009039 | 0:1ed23ab1345f | 50 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){ |
va009039 | 0:1ed23ab1345f | 51 | bt_flip_addr(event_addr, &packet[6]); |
va009039 | 0:1ed23ab1345f | 52 | log_info("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr)); |
va009039 | 0:1ed23ab1345f | 53 | break; |
va009039 | 0:1ed23ab1345f | 54 | } |
va009039 | 0:1ed23ab1345f | 55 | if (COMMAND_COMPLETE_EVENT(packet, hci_write_local_name)){ |
va009039 | 0:1ed23ab1345f | 56 | hci_discoverable_control(1); |
va009039 | 0:1ed23ab1345f | 57 | break; |
va009039 | 0:1ed23ab1345f | 58 | } |
va009039 | 0:1ed23ab1345f | 59 | break; |
va009039 | 0:1ed23ab1345f | 60 | |
va009039 | 0:1ed23ab1345f | 61 | case HCI_EVENT_LINK_KEY_REQUEST: |
va009039 | 0:1ed23ab1345f | 62 | // deny link key request |
va009039 | 0:1ed23ab1345f | 63 | log_info("Link key request\n\r"); |
va009039 | 0:1ed23ab1345f | 64 | bt_flip_addr(event_addr, &packet[2]); |
va009039 | 0:1ed23ab1345f | 65 | hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr); |
va009039 | 0:1ed23ab1345f | 66 | break; |
va009039 | 0:1ed23ab1345f | 67 | |
va009039 | 0:1ed23ab1345f | 68 | case HCI_EVENT_PIN_CODE_REQUEST: |
va009039 | 0:1ed23ab1345f | 69 | // inform about pin code request |
va009039 | 0:1ed23ab1345f | 70 | log_info("Pin code request - using '0000'\n\r"); |
va009039 | 0:1ed23ab1345f | 71 | bt_flip_addr(event_addr, &packet[2]); |
va009039 | 0:1ed23ab1345f | 72 | hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); |
va009039 | 0:1ed23ab1345f | 73 | break; |
va009039 | 0:1ed23ab1345f | 74 | |
va009039 | 0:1ed23ab1345f | 75 | case RFCOMM_EVENT_INCOMING_CONNECTION: |
va009039 | 0:1ed23ab1345f | 76 | // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) |
va009039 | 0:1ed23ab1345f | 77 | bt_flip_addr(event_addr, &packet[2]); |
va009039 | 0:1ed23ab1345f | 78 | rfcomm_channel_nr = packet[8]; |
va009039 | 0:1ed23ab1345f | 79 | rfcomm_channel_id = READ_BT_16(packet, 9); |
va009039 | 0:1ed23ab1345f | 80 | log_info("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr)); |
va009039 | 0:1ed23ab1345f | 81 | rfcomm_accept_connection_internal(rfcomm_channel_id); |
va009039 | 0:1ed23ab1345f | 82 | break; |
va009039 | 0:1ed23ab1345f | 83 | |
va009039 | 0:1ed23ab1345f | 84 | case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: |
va009039 | 0:1ed23ab1345f | 85 | // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) |
va009039 | 0:1ed23ab1345f | 86 | if (packet[2]) { |
va009039 | 0:1ed23ab1345f | 87 | log_info("RFCOMM channel open failed, status %u\n\r", packet[2]); |
va009039 | 0:1ed23ab1345f | 88 | } else { |
va009039 | 0:1ed23ab1345f | 89 | rfcomm_channel_id = READ_BT_16(packet, 12); |
va009039 | 0:1ed23ab1345f | 90 | mtu = READ_BT_16(packet, 14); |
va009039 | 0:1ed23ab1345f | 91 | log_info("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu); |
va009039 | 0:1ed23ab1345f | 92 | } |
va009039 | 0:1ed23ab1345f | 93 | break; |
va009039 | 0:1ed23ab1345f | 94 | |
va009039 | 0:1ed23ab1345f | 95 | case RFCOMM_EVENT_CHANNEL_CLOSED: |
va009039 | 0:1ed23ab1345f | 96 | rfcomm_channel_id = 0; |
va009039 | 0:1ed23ab1345f | 97 | break; |
va009039 | 0:1ed23ab1345f | 98 | |
va009039 | 0:1ed23ab1345f | 99 | default: |
va009039 | 0:1ed23ab1345f | 100 | break; |
va009039 | 0:1ed23ab1345f | 101 | } |
va009039 | 0:1ed23ab1345f | 102 | break; |
va009039 | 0:1ed23ab1345f | 103 | |
va009039 | 0:1ed23ab1345f | 104 | case RFCOMM_DATA_PACKET: |
va009039 | 0:1ed23ab1345f | 105 | // loopback |
va009039 | 0:1ed23ab1345f | 106 | if (rfcomm_channel_id) { |
va009039 | 0:1ed23ab1345f | 107 | err = rfcomm_send_internal(rfcomm_channel_id, packet, size); |
va009039 | 0:1ed23ab1345f | 108 | if (err) { |
va009039 | 0:1ed23ab1345f | 109 | log_info("rfcomm_send_internal -> error %d", err); |
va009039 | 0:1ed23ab1345f | 110 | } |
va009039 | 0:1ed23ab1345f | 111 | } |
ban4jp | 3:316daabf0d24 | 112 | |
ban4jp | 3:316daabf0d24 | 113 | data_recv(packet, size); |
ban4jp | 3:316daabf0d24 | 114 | |
ban4jp | 3:316daabf0d24 | 115 | // led3 = !led3; |
va009039 | 0:1ed23ab1345f | 116 | break; |
va009039 | 0:1ed23ab1345f | 117 | default: |
va009039 | 0:1ed23ab1345f | 118 | break; |
va009039 | 0:1ed23ab1345f | 119 | } |
va009039 | 0:1ed23ab1345f | 120 | } |
va009039 | 0:1ed23ab1345f | 121 | |
ban4jp | 3:316daabf0d24 | 122 | static void data_recv(uint8_t *packet, uint16_t size){ |
ban4jp | 3:316daabf0d24 | 123 | int i; |
ban4jp | 3:316daabf0d24 | 124 | log_info("packet(size:%d) = ", size); |
ban4jp | 3:316daabf0d24 | 125 | for (i = 0; i < size; i++){ |
ban4jp | 3:316daabf0d24 | 126 | log_info("%c", packet[i]); |
ban4jp | 3:316daabf0d24 | 127 | switch (packet[i]) { |
ban4jp | 3:316daabf0d24 | 128 | case 'a': |
ban4jp | 3:316daabf0d24 | 129 | led1 = !led1; |
ban4jp | 3:316daabf0d24 | 130 | break; |
ban4jp | 3:316daabf0d24 | 131 | case 'b': |
ban4jp | 3:316daabf0d24 | 132 | led2 = !led2; |
ban4jp | 3:316daabf0d24 | 133 | break; |
ban4jp | 3:316daabf0d24 | 134 | case 'c': |
ban4jp | 3:316daabf0d24 | 135 | led3 = !led3; |
ban4jp | 3:316daabf0d24 | 136 | break; |
ban4jp | 3:316daabf0d24 | 137 | case 'd': |
ban4jp | 3:316daabf0d24 | 138 | led4 = !led4; |
ban4jp | 3:316daabf0d24 | 139 | break; |
ban4jp | 3:316daabf0d24 | 140 | } |
ban4jp | 3:316daabf0d24 | 141 | } |
ban4jp | 3:316daabf0d24 | 142 | log_info("\n\r"); |
ban4jp | 3:316daabf0d24 | 143 | } |
ban4jp | 3:316daabf0d24 | 144 | |
va009039 | 0:1ed23ab1345f | 145 | static void heartbeat_handler(struct timer *ts){ |
va009039 | 0:1ed23ab1345f | 146 | run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); |
va009039 | 0:1ed23ab1345f | 147 | run_loop_add_timer(ts); |
ban4jp | 3:316daabf0d24 | 148 | // led2 = !led2; |
va009039 | 0:1ed23ab1345f | 149 | } |
va009039 | 0:1ed23ab1345f | 150 | |
va009039 | 0:1ed23ab1345f | 151 | // main |
va009039 | 0:1ed23ab1345f | 152 | int main(void) |
va009039 | 0:1ed23ab1345f | 153 | { |
va009039 | 0:1ed23ab1345f | 154 | pc.baud(921600); |
va009039 | 0:1ed23ab1345f | 155 | log_info("%s\n", __FILE__); |
va009039 | 0:1ed23ab1345f | 156 | |
va009039 | 0:1ed23ab1345f | 157 | // init LEDs |
ban4jp | 3:316daabf0d24 | 158 | led1 = led2 = led3 = led4 = 0; |
va009039 | 0:1ed23ab1345f | 159 | |
va009039 | 0:1ed23ab1345f | 160 | /// GET STARTED with BTstack /// |
va009039 | 0:1ed23ab1345f | 161 | btstack_memory_init(); |
va009039 | 0:1ed23ab1345f | 162 | run_loop_init(RUN_LOOP_EMBEDDED); |
va009039 | 0:1ed23ab1345f | 163 | |
va009039 | 0:1ed23ab1345f | 164 | // init HCI |
va009039 | 0:1ed23ab1345f | 165 | hci_transport_t* transport = hci_transport_usb_instance(); |
va009039 | 0:1ed23ab1345f | 166 | remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; |
va009039 | 0:1ed23ab1345f | 167 | hci_init(transport, NULL, NULL, remote_db); |
va009039 | 0:1ed23ab1345f | 168 | |
va009039 | 0:1ed23ab1345f | 169 | // init L2CAP |
va009039 | 0:1ed23ab1345f | 170 | l2cap_init(); |
va009039 | 0:1ed23ab1345f | 171 | l2cap_register_packet_handler(packet_handler); |
va009039 | 0:1ed23ab1345f | 172 | |
va009039 | 0:1ed23ab1345f | 173 | // init RFCOMM |
va009039 | 0:1ed23ab1345f | 174 | rfcomm_init(); |
va009039 | 0:1ed23ab1345f | 175 | rfcomm_register_packet_handler(packet_handler); |
va009039 | 0:1ed23ab1345f | 176 | rfcomm_register_service_internal(NULL, rfcomm_channel_nr, 100); // reserved channel, mtu=100 |
va009039 | 0:1ed23ab1345f | 177 | |
va009039 | 0:1ed23ab1345f | 178 | // init SDP, create record for SPP and register with SDP |
va009039 | 0:1ed23ab1345f | 179 | sdp_init(); |
va009039 | 0:1ed23ab1345f | 180 | memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); |
va009039 | 0:1ed23ab1345f | 181 | service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; |
va009039 | 0:1ed23ab1345f | 182 | sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "loopback"); |
va009039 | 0:1ed23ab1345f | 183 | log_info("SDP service buffer size: %u\n\r", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); |
va009039 | 0:1ed23ab1345f | 184 | sdp_register_service_internal(NULL, service_record_item); |
va009039 | 0:1ed23ab1345f | 185 | |
va009039 | 0:1ed23ab1345f | 186 | // set one-shot timer |
va009039 | 0:1ed23ab1345f | 187 | timer_source_t heartbeat; |
va009039 | 0:1ed23ab1345f | 188 | heartbeat.process = &heartbeat_handler; |
va009039 | 0:1ed23ab1345f | 189 | run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); |
va009039 | 0:1ed23ab1345f | 190 | run_loop_add_timer(&heartbeat); |
va009039 | 0:1ed23ab1345f | 191 | |
va009039 | 0:1ed23ab1345f | 192 | log_info("SPP loopback demo...\n\r"); |
va009039 | 0:1ed23ab1345f | 193 | |
va009039 | 0:1ed23ab1345f | 194 | // turn on! |
va009039 | 0:1ed23ab1345f | 195 | hci_power_control(HCI_POWER_ON); |
va009039 | 0:1ed23ab1345f | 196 | |
va009039 | 0:1ed23ab1345f | 197 | // go! |
va009039 | 0:1ed23ab1345f | 198 | run_loop_execute(); |
va009039 | 0:1ed23ab1345f | 199 | |
va009039 | 0:1ed23ab1345f | 200 | return 0; |
va009039 | 0:1ed23ab1345f | 201 | } |
va009039 | 0:1ed23ab1345f | 202 | #endif |