1bdc352b1SMatthias Ringwald /* 2bdc352b1SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bdc352b1SMatthias Ringwald * 4bdc352b1SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bdc352b1SMatthias Ringwald * modification, are permitted provided that the following conditions 6bdc352b1SMatthias Ringwald * are met: 7bdc352b1SMatthias Ringwald * 8bdc352b1SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bdc352b1SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bdc352b1SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bdc352b1SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bdc352b1SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bdc352b1SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bdc352b1SMatthias Ringwald * contributors may be used to endorse or promote products derived 15bdc352b1SMatthias Ringwald * from this software without specific prior written permission. 16bdc352b1SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bdc352b1SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bdc352b1SMatthias Ringwald * monetary gain. 19bdc352b1SMatthias Ringwald * 20bdc352b1SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bdc352b1SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bdc352b1SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bdc352b1SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bdc352b1SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bdc352b1SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bdc352b1SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bdc352b1SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bdc352b1SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bdc352b1SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bdc352b1SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bdc352b1SMatthias Ringwald * SUCH DAMAGE. 32bdc352b1SMatthias Ringwald * 33bdc352b1SMatthias Ringwald * Please inquire about commercial licensing options at 34bdc352b1SMatthias Ringwald * [email protected] 35bdc352b1SMatthias Ringwald * 36bdc352b1SMatthias Ringwald */ 37bdc352b1SMatthias Ringwald 38bdc352b1SMatthias Ringwald #define BTSTACK_FILE__ "gatt_counter.c" 39bdc352b1SMatthias Ringwald 40bdc352b1SMatthias Ringwald // ***************************************************************************** 41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gatt_counter): GATT Server - Heartbeat Counter over GATT 42bdc352b1SMatthias Ringwald * 43bdc352b1SMatthias Ringwald * @text All newer operating systems provide GATT Client functionality. 44bdc352b1SMatthias Ringwald * The LE Counter examples demonstrates how to specify a minimal GATT Database 45bdc352b1SMatthias Ringwald * with a custom GATT Service and a custom Characteristic that sends periodic 46bdc352b1SMatthias Ringwald * notifications. 47bdc352b1SMatthias Ringwald */ 48bdc352b1SMatthias Ringwald // ***************************************************************************** 49bdc352b1SMatthias Ringwald 50bdc352b1SMatthias Ringwald #include <stdint.h> 51bdc352b1SMatthias Ringwald #include <stdio.h> 52bdc352b1SMatthias Ringwald #include <stdlib.h> 53bdc352b1SMatthias Ringwald #include <string.h> 54bdc352b1SMatthias Ringwald 55bdc352b1SMatthias Ringwald #include "gatt_counter.h" 56bdc352b1SMatthias Ringwald #include "btstack.h" 57bdc352b1SMatthias Ringwald #include "ble/gatt-service/battery_service_server.h" 58bdc352b1SMatthias Ringwald 59bdc352b1SMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000 60bdc352b1SMatthias Ringwald 61bdc352b1SMatthias Ringwald /* @section Main Application Setup 62bdc352b1SMatthias Ringwald * 63bdc352b1SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 64bdc352b1SMatthias Ringwald * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled 65bdc352b1SMatthias Ringwald * ATT Database generated from $le_counter.gatt$. 66bdc352b1SMatthias Ringwald * Additionally, it enables the Battery Service Server with the current battery level. 67bdc352b1SMatthias Ringwald * Finally, it configures the advertisements 68bdc352b1SMatthias Ringwald * and the heartbeat handler and boots the Bluetooth stack. 69bdc352b1SMatthias Ringwald * In this example, the Advertisement contains the Flags attribute and the device name. 70bdc352b1SMatthias Ringwald * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported. 71bdc352b1SMatthias Ringwald */ 72bdc352b1SMatthias Ringwald 73bdc352b1SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */ 74bdc352b1SMatthias Ringwald static int le_notification_enabled; 75bdc352b1SMatthias Ringwald static btstack_timer_source_t heartbeat; 76bdc352b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 77bdc352b1SMatthias Ringwald static hci_con_handle_t con_handle; 78bdc352b1SMatthias Ringwald static uint8_t battery = 100; 79bdc352b1SMatthias Ringwald 80bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 81bdc352b1SMatthias Ringwald static uint8_t gatt_service_buffer[70]; 82bdc352b1SMatthias Ringwald #endif 83bdc352b1SMatthias Ringwald 84bdc352b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 85bdc352b1SMatthias Ringwald 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); 86bdc352b1SMatthias Ringwald 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); 87bdc352b1SMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts); 88bdc352b1SMatthias Ringwald static void beat(void); 89bdc352b1SMatthias Ringwald 90bdc352b1SMatthias Ringwald const uint8_t adv_data[] = { 91bdc352b1SMatthias Ringwald // Flags general discoverable, BR/EDR not supported 92bdc352b1SMatthias Ringwald 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 93bdc352b1SMatthias Ringwald // Name 94bdc352b1SMatthias Ringwald 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r', 95bdc352b1SMatthias Ringwald // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing! 96bdc352b1SMatthias Ringwald 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff, 97bdc352b1SMatthias Ringwald }; 98bdc352b1SMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data); 99bdc352b1SMatthias Ringwald 100bdc352b1SMatthias Ringwald static void le_counter_setup(void){ 101bdc352b1SMatthias Ringwald 102bdc352b1SMatthias Ringwald l2cap_init(); 103bdc352b1SMatthias Ringwald 104bdc352b1SMatthias Ringwald // setup le device db 105bdc352b1SMatthias Ringwald le_device_db_init(); 106bdc352b1SMatthias Ringwald 107bdc352b1SMatthias Ringwald // setup SM: Display only 108bdc352b1SMatthias Ringwald sm_init(); 109bdc352b1SMatthias Ringwald 110bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 111bdc352b1SMatthias Ringwald // init SDP, create record for GATT and register with SDP 112bdc352b1SMatthias Ringwald sdp_init(); 113bdc352b1SMatthias Ringwald memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer)); 114bdc352b1SMatthias Ringwald gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE); 115bdc352b1SMatthias Ringwald sdp_register_service(gatt_service_buffer); 116bdc352b1SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer)); 117bdc352b1SMatthias Ringwald 118bdc352b1SMatthias Ringwald // configure Classic GAP 119bdc352b1SMatthias Ringwald gap_set_local_name("GATT Counter BR/EDR 00:00:00:00:00:00"); 120bdc352b1SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 121bdc352b1SMatthias Ringwald gap_discoverable_control(1); 122bdc352b1SMatthias Ringwald #endif 123bdc352b1SMatthias Ringwald 124bdc352b1SMatthias Ringwald // setup ATT server 125bdc352b1SMatthias Ringwald att_server_init(profile_data, att_read_callback, att_write_callback); 126bdc352b1SMatthias Ringwald 127bdc352b1SMatthias Ringwald // setup battery service 128bdc352b1SMatthias Ringwald battery_service_server_init(battery); 129bdc352b1SMatthias Ringwald 130bdc352b1SMatthias Ringwald // setup advertisements 131bdc352b1SMatthias Ringwald uint16_t adv_int_min = 0x0030; 132bdc352b1SMatthias Ringwald uint16_t adv_int_max = 0x0030; 133bdc352b1SMatthias Ringwald uint8_t adv_type = 0; 134bdc352b1SMatthias Ringwald bd_addr_t null_addr; 135bdc352b1SMatthias Ringwald memset(null_addr, 0, 6); 136bdc352b1SMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 137bdc352b1SMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 138bdc352b1SMatthias Ringwald gap_advertisements_enable(1); 139bdc352b1SMatthias Ringwald 140bdc352b1SMatthias Ringwald // register for HCI events 141bdc352b1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 142bdc352b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 143bdc352b1SMatthias Ringwald 144bdc352b1SMatthias Ringwald // register for ATT event 145bdc352b1SMatthias Ringwald att_server_register_packet_handler(packet_handler); 146bdc352b1SMatthias Ringwald 147bdc352b1SMatthias Ringwald // set one-shot timer 148bdc352b1SMatthias Ringwald heartbeat.process = &heartbeat_handler; 149bdc352b1SMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 150bdc352b1SMatthias Ringwald btstack_run_loop_add_timer(&heartbeat); 151bdc352b1SMatthias Ringwald 152bdc352b1SMatthias Ringwald // beat once 153bdc352b1SMatthias Ringwald beat(); 154bdc352b1SMatthias Ringwald } 155bdc352b1SMatthias Ringwald /* LISTING_END */ 156bdc352b1SMatthias Ringwald 157bdc352b1SMatthias Ringwald /* 158bdc352b1SMatthias Ringwald * @section Heartbeat Handler 159bdc352b1SMatthias Ringwald * 160bdc352b1SMatthias Ringwald * @text The heartbeat handler updates the value of the single Characteristic provided in this example, 161bdc352b1SMatthias Ringwald * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat. 162bdc352b1SMatthias Ringwald */ 163bdc352b1SMatthias Ringwald 164bdc352b1SMatthias Ringwald /* LISTING_START(heartbeat): Hearbeat Handler */ 165bdc352b1SMatthias Ringwald static int counter = 0; 166bdc352b1SMatthias Ringwald static char counter_string[30]; 167bdc352b1SMatthias Ringwald static int counter_string_len; 168bdc352b1SMatthias Ringwald 169bdc352b1SMatthias Ringwald static void beat(void){ 170bdc352b1SMatthias Ringwald counter++; 171bdc352b1SMatthias Ringwald counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter); 172bdc352b1SMatthias Ringwald puts(counter_string); 173bdc352b1SMatthias Ringwald } 174bdc352b1SMatthias Ringwald 175bdc352b1SMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){ 176bdc352b1SMatthias Ringwald if (le_notification_enabled) { 177bdc352b1SMatthias Ringwald beat(); 178bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(con_handle); 179bdc352b1SMatthias Ringwald } 180bdc352b1SMatthias Ringwald 181bdc352b1SMatthias Ringwald // simulate battery drain 182bdc352b1SMatthias Ringwald battery--; 183bdc352b1SMatthias Ringwald if (battery < 50) { 184bdc352b1SMatthias Ringwald battery = 100; 185bdc352b1SMatthias Ringwald } 186bdc352b1SMatthias Ringwald battery_service_server_set_battery_value(battery); 187bdc352b1SMatthias Ringwald 188bdc352b1SMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 189bdc352b1SMatthias Ringwald btstack_run_loop_add_timer(ts); 190bdc352b1SMatthias Ringwald } 191bdc352b1SMatthias Ringwald /* LISTING_END */ 192bdc352b1SMatthias Ringwald 193bdc352b1SMatthias Ringwald /* 194bdc352b1SMatthias Ringwald * @section Packet Handler 195bdc352b1SMatthias Ringwald * 196bdc352b1SMatthias Ringwald * @text The packet handler is used to: 197bdc352b1SMatthias Ringwald * - stop the counter after a disconnect 198bdc352b1SMatthias Ringwald * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received 199bdc352b1SMatthias Ringwald */ 200bdc352b1SMatthias Ringwald 201bdc352b1SMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 202bdc352b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 203bdc352b1SMatthias Ringwald UNUSED(channel); 204bdc352b1SMatthias Ringwald UNUSED(size); 205bdc352b1SMatthias Ringwald 206*7bbeb3adSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return; 207*7bbeb3adSMilanka Ringwald 208bdc352b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 209bdc352b1SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 210bdc352b1SMatthias Ringwald le_notification_enabled = 0; 211bdc352b1SMatthias Ringwald break; 212bdc352b1SMatthias Ringwald case ATT_EVENT_CAN_SEND_NOW: 213bdc352b1SMatthias Ringwald att_server_notify(con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len); 214bdc352b1SMatthias Ringwald break; 215*7bbeb3adSMilanka Ringwald default: 216bdc352b1SMatthias Ringwald break; 217bdc352b1SMatthias Ringwald } 218bdc352b1SMatthias Ringwald } 219*7bbeb3adSMilanka Ringwald 220bdc352b1SMatthias Ringwald /* LISTING_END */ 221bdc352b1SMatthias Ringwald 222bdc352b1SMatthias Ringwald /* 223bdc352b1SMatthias Ringwald * @section ATT Read 224bdc352b1SMatthias Ringwald * 225bdc352b1SMatthias Ringwald * @text The ATT Server handles all reads to constant data. For dynamic data like the custom characteristic, the registered 226bdc352b1SMatthias Ringwald * att_read_callback is called. To handle long characteristics and long reads, the att_read_callback is first called 227bdc352b1SMatthias Ringwald * with buffer == NULL, to request the total value length. Then it will be called again requesting a chunk of the value. 228bdc352b1SMatthias Ringwald * See Listing attRead. 229bdc352b1SMatthias Ringwald */ 230bdc352b1SMatthias Ringwald 231bdc352b1SMatthias Ringwald /* LISTING_START(attRead): ATT Read */ 232bdc352b1SMatthias Ringwald 233bdc352b1SMatthias Ringwald // ATT Client Read Callback for Dynamic Data 234bdc352b1SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 235bdc352b1SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 236bdc352b1SMatthias Ringwald // @param offset defines start of attribute value 237bdc352b1SMatthias Ringwald static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 238bdc352b1SMatthias Ringwald UNUSED(connection_handle); 239bdc352b1SMatthias Ringwald 240bdc352b1SMatthias Ringwald if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){ 24134bd46a0SMatthias Ringwald return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size); 242bdc352b1SMatthias Ringwald } 243bdc352b1SMatthias Ringwald return 0; 244bdc352b1SMatthias Ringwald } 245bdc352b1SMatthias Ringwald /* LISTING_END */ 246bdc352b1SMatthias Ringwald 247bdc352b1SMatthias Ringwald 248bdc352b1SMatthias Ringwald /* 249bdc352b1SMatthias Ringwald * @section ATT Write 250bdc352b1SMatthias Ringwald * 251bdc352b1SMatthias Ringwald * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification 252bdc352b1SMatthias Ringwald * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored and used 253bdc352b1SMatthias Ringwald * in the heartbeat handler to decide if a new value should be sent. See Listing attWrite. 254bdc352b1SMatthias Ringwald */ 255bdc352b1SMatthias Ringwald 256bdc352b1SMatthias Ringwald /* LISTING_START(attWrite): ATT Write */ 257bdc352b1SMatthias Ringwald static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 258bdc352b1SMatthias Ringwald UNUSED(transaction_mode); 259bdc352b1SMatthias Ringwald UNUSED(offset); 260bdc352b1SMatthias Ringwald UNUSED(buffer_size); 261bdc352b1SMatthias Ringwald 262bdc352b1SMatthias Ringwald if (att_handle != ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE) return 0; 263bdc352b1SMatthias Ringwald le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; 264bdc352b1SMatthias Ringwald con_handle = connection_handle; 265bdc352b1SMatthias Ringwald return 0; 266bdc352b1SMatthias Ringwald } 267bdc352b1SMatthias Ringwald /* LISTING_END */ 268bdc352b1SMatthias Ringwald 269bdc352b1SMatthias Ringwald int btstack_main(void); 270bdc352b1SMatthias Ringwald int btstack_main(void) 271bdc352b1SMatthias Ringwald { 272bdc352b1SMatthias Ringwald le_counter_setup(); 273bdc352b1SMatthias Ringwald 274bdc352b1SMatthias Ringwald // turn on! 275bdc352b1SMatthias Ringwald hci_power_control(HCI_POWER_ON); 276bdc352b1SMatthias Ringwald 277bdc352b1SMatthias Ringwald return 0; 278bdc352b1SMatthias Ringwald } 279bdc352b1SMatthias Ringwald /* EXAMPLE_END */ 280