Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FatFileSystem TB6612FNG2 mbed
BLE_demo.cpp@0:de03cbbcd0ff, 2015-11-30 (annotated)
- Committer:
- mbed_Cookbook_SE
- Date:
- Mon Nov 30 09:32:15 2015 +0000
- Revision:
- 0:de03cbbcd0ff
??
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 1 | /* |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 2 | * Copyright (C) 2011-2012 by Matthias Ringwald |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 3 | * |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 4 | * Redistribution and use in source and binary forms, with or without |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 5 | * modification, are permitted provided that the following conditions |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 6 | * are met: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 7 | * |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 8 | * 1. Redistributions of source code must retain the above copyright |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 9 | * notice, this list of conditions and the following disclaimer. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 11 | * notice, this list of conditions and the following disclaimer in the |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 12 | * documentation and/or other materials provided with the distribution. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 13 | * 3. Neither the name of the copyright holders nor the names of |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 14 | * contributors may be used to endorse or promote products derived |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 15 | * from this software without specific prior written permission. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 16 | * 4. This software may not be used in a commercial product |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 17 | * without an explicit license granted by the copyright holder. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 18 | * |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 19 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 22 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 23 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 25 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 26 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 29 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 30 | * SUCH DAMAGE. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 31 | * |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 32 | */ |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 33 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 34 | //***************************************************************************** |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 35 | // |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 36 | // att device demo |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 37 | // |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 38 | //***************************************************************************** |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 39 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 40 | // TODO: seperate BR/EDR from LE ACL buffers |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 41 | // TODO: move LE init into HCI |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 42 | // .. |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 43 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 44 | // NOTE: Supports only a single connection |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 45 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 46 | // mbed specific |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 47 | #include "mbed.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 48 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 49 | // from btstack ble_server.c |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 50 | #include "global.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 51 | #include "debug.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 52 | #include "btstack/btstack.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 53 | #include "btstack/hci_cmds.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 54 | #include "btstack/run_loop.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 55 | #include "btstack/hal_tick.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 56 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 57 | #include "hci.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 58 | #include "l2cap.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 59 | #include "btstack_memory.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 60 | #include "remote_device_db.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 61 | #include "config.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 62 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 63 | #include "att.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 64 | #include "TB6612.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 65 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 66 | Serial pc(USBTX, USBRX); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 67 | TB6612 MOTOR_A(p21,p19,p20); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 68 | TB6612 MOTOR_B(p22,p29,p30); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 69 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 70 | hci_transport_t * hci_transport_picusb_instance(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 71 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 72 | static att_connection_t att_connection; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 73 | static uint16_t att_response_handle = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 74 | static uint16_t att_response_size = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 75 | static uint8_t att_response_buffer[28]; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 76 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 77 | static void att_try_respond(void) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 78 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 79 | if (!att_response_size) return; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 80 | if (!att_response_handle) return; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 81 | if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) return; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 82 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 83 | // update state before sending packet |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 84 | uint16_t size = att_response_size; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 85 | att_response_size = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 86 | l2cap_send_connectionless(att_response_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_buffer, size); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 87 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 88 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 89 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 90 | static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 91 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 92 | if (packet_type != ATT_DATA_PACKET) return; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 93 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 94 | att_response_handle = handle; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 95 | att_response_size = att_handle_request(&att_connection, packet, size, att_response_buffer); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 96 | att_try_respond(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 97 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 98 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 99 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 100 | // enable LE, setup ADV data |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 101 | static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 102 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 103 | static bd_addr_t addr; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 104 | // uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff }; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 105 | const uint8_t adv_data[31]="\x02\x01\x05" "\x06\x09mbed"; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 106 | switch (packet_type) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 107 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 108 | case HCI_EVENT_PACKET: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 109 | switch (packet[0]) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 110 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 111 | case BTSTACK_EVENT_STATE: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 112 | // bt stack activated, get started - set local name |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 113 | if (packet[2] == HCI_STATE_WORKING) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 114 | log_info("Working!\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 115 | hci_send_cmd(&hci_read_local_supported_features); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 116 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 117 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 118 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 119 | case DAEMON_EVENT_HCI_PACKET_SENT: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 120 | att_try_respond(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 121 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 122 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 123 | case HCI_EVENT_LE_META: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 124 | switch (packet[2]) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 125 | case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 126 | // reset connection MTU |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 127 | att_connection.mtu = 23; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 128 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 129 | default: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 130 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 131 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 132 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 133 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 134 | case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 135 | if (packet[2]) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 136 | connection_status=1; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 137 | log_info("CONNECTED\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 138 | } else { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 139 | connection_status=0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 140 | MOTOR_A = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 141 | MOTOR_B = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 142 | log_info("NOT CONNECTED\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 143 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 144 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 145 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 146 | case HCI_EVENT_DISCONNECTION_COMPLETE: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 147 | att_response_handle =0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 148 | att_response_size = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 149 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 150 | // restart advertising |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 151 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 152 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 153 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 154 | case HCI_EVENT_COMMAND_COMPLETE: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 155 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 156 | bt_flip_addr(addr, &packet[6]); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 157 | log_info("BD ADDR: %s\n", bd_addr_to_str(addr)); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 158 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 159 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 160 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 161 | log_info("Local supported features: %04lX%04lX\n", READ_BT_32(packet, 10), READ_BT_32(packet, 6)); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 162 | hci_send_cmd(&hci_set_event_mask, 0xffffffff, 0x20001fff); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 163 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 164 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 165 | if (COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 166 | hci_send_cmd(&hci_write_le_host_supported, 1, 1); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 167 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 168 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 169 | if (COMMAND_COMPLETE_EVENT(packet, hci_write_le_host_supported)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 170 | hci_send_cmd(&hci_le_set_event_mask, 0xffffffff, 0xffffffff); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 171 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 172 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 173 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_event_mask)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 174 | hci_send_cmd(&hci_le_read_buffer_size); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 175 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 176 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 177 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 178 | log_info("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 179 | hci_send_cmd(&hci_le_read_supported_states); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 180 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 181 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 182 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_supported_states)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 183 | hci_send_cmd(&hci_le_set_advertising_parameters, 0x0400, 0x0800, 0, 0, 0, &addr, 0x07, 0); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 184 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 185 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 186 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_parameters)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 187 | hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 188 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 189 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 190 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 191 | hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 192 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 193 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 194 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 195 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 196 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 197 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 198 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertise_enable)) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 199 | hci_discoverable_control(1); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 200 | log_info("startup_state=1\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 201 | startup_state=1; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 202 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 203 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 204 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 205 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 206 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 207 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 208 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 209 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 210 | // test profile |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 211 | #include "profile.h" |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 212 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 213 | static uint8_t strbuf[80]; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 214 | static uint8_t ledvalue; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 215 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 216 | // read requests |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 217 | static uint16_t att_read_callback(uint16_t handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 218 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 219 | uint16_t ret=0,val; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 220 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 221 | if(buffer) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 222 | log_info("READ Callback, handle %04x\n", handle); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 223 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 224 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 225 | switch(handle) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 226 | // Correspond to Characteristics 0xFFF1 |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 227 | case 0x000b: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 228 | #if 0 |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 229 | if(buffer && ret<=buffer_size) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 230 | buffer[0] = sw1.read(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 231 | log_info("Read value: %u\n", buffer[0]); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 232 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 233 | ret = 1; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 234 | #else |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 235 | ret = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 236 | log_info("No action\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 237 | #endif |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 238 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 239 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 240 | // Correspond to Characteristics 0xFFF2 |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 241 | case 0x000d: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 242 | if(buffer && buffer_size) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 243 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 244 | buffer[0] = ledvalue; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 245 | log_info("Read value: %u\n", buffer[0]); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 246 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 247 | ret=1; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 248 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 249 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 250 | // Correspond to Characteristics 0x00001234-0000-1000-8000-00805F9B34FB |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 251 | case 0x000f: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 252 | if(buffer && buffer_size>=2) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 253 | val=timer_counter; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 254 | log_info("Read value: %u\n", val); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 255 | buffer[0]=val&0xff; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 256 | buffer[1]=val>>8; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 257 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 258 | ret=2; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 259 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 260 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 261 | return ret; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 262 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 263 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 264 | // write requests |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 265 | 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) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 266 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 267 | log_info("WRITE Callback, handle %04x\n", handle); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 268 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 269 | switch(handle) { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 270 | // Correspond to Characteristics 0xFFF1 |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 271 | case 0x000b: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 272 | switch(buffer[0]) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 273 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 274 | case 0x01: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 275 | MOTOR_A = 100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 276 | MOTOR_B = 100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 277 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 278 | case 0x02: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 279 | MOTOR_A = -100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 280 | MOTOR_B = -100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 281 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 282 | case 0x04: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 283 | MOTOR_A = 100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 284 | MOTOR_B = -100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 285 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 286 | case 0x08: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 287 | MOTOR_A = -100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 288 | MOTOR_B = 100; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 289 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 290 | default: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 291 | MOTOR_A = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 292 | MOTOR_B = 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 293 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 294 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 295 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 296 | // Correspond to Characteristics 0xFFF2 |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 297 | case 0x000d: |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 298 | log_info("New value: %u\n", buffer[0]); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 299 | ledvalue = buffer[0]; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 300 | break; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 301 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 302 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 303 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 304 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 305 | void hal_tick_event(void) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 306 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 307 | timer_counter++; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 308 | } |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 309 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 310 | // main |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 311 | int main(void) |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 312 | { |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 313 | pc.baud(921600); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 314 | //pc.baud(230400); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 315 | log_info("%s\n", __FILE__); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 316 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 317 | /// GET STARTED with BTstack /// |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 318 | btstack_memory_init(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 319 | run_loop_init(RUN_LOOP_EMBEDDED); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 320 | hal_tick_set_handler(&hal_tick_event); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 321 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 322 | // init HCI |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 323 | // use BlueUSB |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 324 | hci_transport_t* transport = hci_transport_usb_instance(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 325 | bt_control_t * control = NULL; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 326 | hci_uart_config_t * config = NULL; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 327 | remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 328 | hci_init(transport, config, control, remote_db); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 329 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 330 | // use eHCILL |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 331 | // bt_control_cc256x_enable_ehcill(1); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 332 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 333 | // set up l2cap_le |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 334 | l2cap_init(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 335 | l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 336 | l2cap_register_packet_handler(packet_handler); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 337 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 338 | // set up ATT |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 339 | att_set_db(profile_data); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 340 | att_set_write_callback(att_write_callback); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 341 | att_set_read_callback(att_read_callback); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 342 | att_dump_attributes(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 343 | att_connection.mtu = 27; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 344 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 345 | log_info("Run...\n\n"); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 346 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 347 | // turn on! |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 348 | hci_power_control(HCI_POWER_ON); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 349 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 350 | // go! |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 351 | run_loop_execute(); |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 352 | |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 353 | // happy compiler! |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 354 | return 0; |
| mbed_Cookbook_SE | 0:de03cbbcd0ff | 355 | } |