1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "spp_and_gatt_counter.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(spp_and_le_counter): Dual mode - SPP and LE Counter 42 * 43 * @text The SPP and LE Counter example combines the Bluetooth Classic SPP Counter 44 * and the Bluetooth LE Counter into a single application. 45 * 46 * @text In this Section, we only point out the differences to the individual examples 47 * and how the stack is configured. 48 * 49 * @text Note: To test, please run the example, and then: 50 * - for SPP pair from a remote device, and open the Virtual Serial Port, 51 * - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications. 52 */ 53 // ***************************************************************************** 54 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <inttypes.h> 60 61 #include "btstack.h" 62 #include "spp_and_gatt_counter.h" 63 64 #define RFCOMM_SERVER_CHANNEL 1 65 #define HEARTBEAT_PERIOD_MS 1000 66 67 static uint16_t rfcomm_channel_id; 68 static uint8_t spp_service_buffer[150]; 69 static int le_notification_enabled; 70 static hci_con_handle_t att_con_handle; 71 72 // THE Couner 73 static btstack_timer_source_t heartbeat; 74 static int counter = 0; 75 static char counter_string[30]; 76 static int counter_string_len; 77 78 static btstack_packet_callback_registration_t hci_event_callback_registration; 79 80 #ifdef ENABLE_GATT_OVER_CLASSIC 81 static uint8_t gatt_service_buffer[70]; 82 #endif 83 84 /* 85 * @section Advertisements 86 * 87 * @text The Flags attribute in the Advertisement Data indicates if a device is in dual-mode or not. 88 * Flag 0x06 indicates LE General Discoverable, BR/EDR not supported although we're actually using BR/EDR. 89 * In the past, there have been problems with Anrdoid devices when the flag was not set. 90 * Setting it should prevent the remote implementation to try to use GATT over LE/EDR, which is not 91 * implemented by BTstack. So, setting the flag seems like the safer choice (while it's technically incorrect). 92 */ 93 /* LISTING_START(advertisements): Advertisement data: Flag 0x06 indicates LE-only device */ 94 const uint8_t adv_data[] = { 95 // Flags general discoverable, BR/EDR not supported 96 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 97 // Name 98 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r', 99 // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing! 100 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff, 101 }; 102 /* LISTING_END */ 103 uint8_t adv_data_len = sizeof(adv_data); 104 105 106 /* 107 * @section Packet Handler 108 * 109 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 110 */ 111 112 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 113 UNUSED(channel); 114 115 bd_addr_t event_addr; 116 uint8_t rfcomm_channel_nr; 117 uint16_t mtu; 118 int i; 119 120 switch (packet_type) { 121 case HCI_EVENT_PACKET: 122 switch (hci_event_packet_get_type(packet)) { 123 case HCI_EVENT_PIN_CODE_REQUEST: 124 // inform about pin code request 125 printf("Pin code request - using '0000'\n"); 126 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 127 gap_pin_code_response(event_addr, "0000"); 128 break; 129 130 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 131 // inform about user confirmation request 132 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 133 printf("SSP User Confirmation Auto accept\n"); 134 break; 135 136 case HCI_EVENT_DISCONNECTION_COMPLETE: 137 le_notification_enabled = 0; 138 break; 139 140 case ATT_EVENT_CAN_SEND_NOW: 141 att_server_notify(att_con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len); 142 break; 143 144 case RFCOMM_EVENT_INCOMING_CONNECTION: 145 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 146 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 147 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 148 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 149 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 150 rfcomm_accept_connection(rfcomm_channel_id); 151 break; 152 153 case RFCOMM_EVENT_CHANNEL_OPENED: 154 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 155 if (rfcomm_event_channel_opened_get_status(packet)) { 156 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 157 } else { 158 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 159 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 160 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 161 } 162 break; 163 164 case RFCOMM_EVENT_CAN_SEND_NOW: 165 rfcomm_send(rfcomm_channel_id, (uint8_t*) counter_string, counter_string_len); 166 break; 167 168 case RFCOMM_EVENT_CHANNEL_CLOSED: 169 printf("RFCOMM channel closed\n"); 170 rfcomm_channel_id = 0; 171 break; 172 173 default: 174 break; 175 } 176 break; 177 178 case RFCOMM_DATA_PACKET: 179 printf("RCV: '"); 180 for (i=0;i<size;i++){ 181 putchar(packet[i]); 182 } 183 printf("'\n"); 184 break; 185 186 default: 187 break; 188 } 189 } 190 191 // ATT Client Read Callback for Dynamic Data 192 // - if buffer == NULL, don't copy data, just return size of value 193 // - if buffer != NULL, copy data and return number bytes copied 194 // @param offset defines start of attribute value 195 static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 196 UNUSED(con_handle); 197 198 if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){ 199 return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size); 200 } 201 return 0; 202 } 203 204 // write requests 205 static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 206 // ignore cancel sent for new connections 207 if (transaction_mode == ATT_TRANSACTION_MODE_CANCEL) return 0; 208 // find characteristic for handle 209 switch (att_handle){ 210 case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 211 le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; 212 att_con_handle = con_handle; 213 return 0; 214 case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE: 215 printf("Write on test characteristic: "); 216 printf_hexdump(buffer, buffer_size); 217 return 0; 218 default: 219 printf("WRITE Callback, handle %04x, mode %u, offset %u, data: ", con_handle, transaction_mode, offset); 220 printf_hexdump(buffer, buffer_size); 221 return 0; 222 } 223 } 224 225 static void beat(void){ 226 counter++; 227 counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter); 228 puts(counter_string); 229 } 230 231 /* 232 * @section Heartbeat Handler 233 * 234 * @text Similar to the packet handler, the heartbeat handler is the combination of the individual ones. 235 * After updating the counter, it requests an ATT_EVENT_CAN_SEND_NOW and/or RFCOMM_EVENT_CAN_SEND_NOW 236 */ 237 238 /* LISTING_START(heartbeat): Combined Heartbeat handler */ 239 static void heartbeat_handler(struct btstack_timer_source *ts){ 240 241 if (rfcomm_channel_id || le_notification_enabled) { 242 beat(); 243 } 244 245 if (rfcomm_channel_id){ 246 rfcomm_request_can_send_now_event(rfcomm_channel_id); 247 } 248 249 if (le_notification_enabled) { 250 att_server_request_can_send_now_event(att_con_handle); 251 } 252 253 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 254 btstack_run_loop_add_timer(ts); 255 } 256 /* LISTING_END */ 257 258 /* 259 * @section Main Application Setup 260 * 261 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 262 */ 263 264 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 265 int btstack_main(void); 266 int btstack_main(void) 267 { 268 l2cap_init(); 269 270 rfcomm_init(); 271 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 272 273 // init SDP, create record for SPP and register with SDP 274 sdp_init(); 275 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 276 spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter"); 277 sdp_register_service(spp_service_buffer); 278 printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 279 280 #ifdef ENABLE_GATT_OVER_CLASSIC 281 // init SDP, create record for GATT and register with SDP 282 memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer)); 283 gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE); 284 sdp_register_service(gatt_service_buffer); 285 printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer)); 286 #endif 287 288 gap_set_local_name("SPP and LE Counter 00:00:00:00:00:00"); 289 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 290 gap_discoverable_control(1); 291 292 // setup le device db 293 le_device_db_init(); 294 295 // setup SM: Display only 296 sm_init(); 297 298 // setup ATT server 299 att_server_init(profile_data, att_read_callback, att_write_callback); 300 301 // register for HCI events 302 hci_event_callback_registration.callback = &packet_handler; 303 hci_add_event_handler(&hci_event_callback_registration); 304 305 // register for ATT events 306 att_server_register_packet_handler(packet_handler); 307 308 // setup advertisements 309 uint16_t adv_int_min = 0x0030; 310 uint16_t adv_int_max = 0x0030; 311 uint8_t adv_type = 0; 312 bd_addr_t null_addr; 313 memset(null_addr, 0, 6); 314 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 315 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 316 gap_advertisements_enable(1); 317 318 // set one-shot timer 319 heartbeat.process = &heartbeat_handler; 320 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 321 btstack_run_loop_add_timer(&heartbeat); 322 323 // beat once 324 beat(); 325 326 // turn on! 327 hci_power_control(HCI_POWER_ON); 328 329 return 0; 330 } 331 /* LISTING_END */ 332 /* EXAMPLE_END */ 333