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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH 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
90e06798b6SMatthias Ringwald // Flags general discoverable, BR/EDR supported (== not supported flag not set) when ENABLE_GATT_OVER_CLASSIC is enabled
91e06798b6SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
92edf70fbfSMatthias Ringwald #define APP_AD_FLAGS 0x02
93e06798b6SMatthias Ringwald #else
94edf70fbfSMatthias Ringwald #define APP_AD_FLAGS 0x06
95e06798b6SMatthias Ringwald #endif
96e06798b6SMatthias Ringwald
97bdc352b1SMatthias Ringwald const uint8_t adv_data[] = {
98e06798b6SMatthias Ringwald // Flags general discoverable
99edf70fbfSMatthias Ringwald 0x02, BLUETOOTH_DATA_TYPE_FLAGS, APP_AD_FLAGS,
100bdc352b1SMatthias Ringwald // Name
101bdc352b1SMatthias Ringwald 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r',
102bdc352b1SMatthias Ringwald // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
103bdc352b1SMatthias Ringwald 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
104bdc352b1SMatthias Ringwald };
105bdc352b1SMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
106bdc352b1SMatthias Ringwald
le_counter_setup(void)107bdc352b1SMatthias Ringwald static void le_counter_setup(void){
108bdc352b1SMatthias Ringwald
109bdc352b1SMatthias Ringwald l2cap_init();
110bdc352b1SMatthias Ringwald
111bdc352b1SMatthias Ringwald // setup SM: Display only
112bdc352b1SMatthias Ringwald sm_init();
113bdc352b1SMatthias Ringwald
114bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
115bdc352b1SMatthias Ringwald // init SDP, create record for GATT and register with SDP
116bdc352b1SMatthias Ringwald sdp_init();
117bdc352b1SMatthias Ringwald memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
118762141afSMatthias Ringwald gatt_create_sdp_record(gatt_service_buffer, sdp_create_service_record_handle(), ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
119762141afSMatthias Ringwald btstack_assert(de_get_len( gatt_service_buffer) <= sizeof(gatt_service_buffer));
120bdc352b1SMatthias Ringwald sdp_register_service(gatt_service_buffer);
121bdc352b1SMatthias Ringwald
122bdc352b1SMatthias Ringwald // configure Classic GAP
123bdc352b1SMatthias Ringwald gap_set_local_name("GATT Counter BR/EDR 00:00:00:00:00:00");
124bdc352b1SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
125bdc352b1SMatthias Ringwald gap_discoverable_control(1);
126bdc352b1SMatthias Ringwald #endif
127bdc352b1SMatthias Ringwald
128bdc352b1SMatthias Ringwald // setup ATT server
129bdc352b1SMatthias Ringwald att_server_init(profile_data, att_read_callback, att_write_callback);
130bdc352b1SMatthias Ringwald
131bdc352b1SMatthias Ringwald // setup battery service
132bdc352b1SMatthias Ringwald battery_service_server_init(battery);
133bdc352b1SMatthias Ringwald
134bdc352b1SMatthias Ringwald // setup advertisements
135bdc352b1SMatthias Ringwald uint16_t adv_int_min = 0x0030;
136bdc352b1SMatthias Ringwald uint16_t adv_int_max = 0x0030;
137bdc352b1SMatthias Ringwald uint8_t adv_type = 0;
138bdc352b1SMatthias Ringwald bd_addr_t null_addr;
139bdc352b1SMatthias Ringwald memset(null_addr, 0, 6);
140bdc352b1SMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
141bdc352b1SMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
142bdc352b1SMatthias Ringwald gap_advertisements_enable(1);
143bdc352b1SMatthias Ringwald
144bdc352b1SMatthias Ringwald // register for HCI events
145bdc352b1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
146bdc352b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
147bdc352b1SMatthias Ringwald
148bdc352b1SMatthias Ringwald // register for ATT event
149bdc352b1SMatthias Ringwald att_server_register_packet_handler(packet_handler);
150bdc352b1SMatthias Ringwald
151bdc352b1SMatthias Ringwald // set one-shot timer
152bdc352b1SMatthias Ringwald heartbeat.process = &heartbeat_handler;
153bdc352b1SMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
154bdc352b1SMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
155bdc352b1SMatthias Ringwald
156bdc352b1SMatthias Ringwald // beat once
157bdc352b1SMatthias Ringwald beat();
158bdc352b1SMatthias Ringwald }
159bdc352b1SMatthias Ringwald /* LISTING_END */
160bdc352b1SMatthias Ringwald
161bdc352b1SMatthias Ringwald /*
162bdc352b1SMatthias Ringwald * @section Heartbeat Handler
163bdc352b1SMatthias Ringwald *
164bdc352b1SMatthias Ringwald * @text The heartbeat handler updates the value of the single Characteristic provided in this example,
165bdc352b1SMatthias Ringwald * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat.
166bdc352b1SMatthias Ringwald */
167bdc352b1SMatthias Ringwald
168bdc352b1SMatthias Ringwald /* LISTING_START(heartbeat): Hearbeat Handler */
169bdc352b1SMatthias Ringwald static int counter = 0;
170bdc352b1SMatthias Ringwald static char counter_string[30];
171bdc352b1SMatthias Ringwald static int counter_string_len;
172bdc352b1SMatthias Ringwald
beat(void)173bdc352b1SMatthias Ringwald static void beat(void){
174bdc352b1SMatthias Ringwald counter++;
1750801ef92SMatthias Ringwald counter_string_len = snprintf(counter_string, sizeof(counter_string), "BTstack counter %04u", counter);
176bdc352b1SMatthias Ringwald puts(counter_string);
177bdc352b1SMatthias Ringwald }
178bdc352b1SMatthias Ringwald
heartbeat_handler(struct btstack_timer_source * ts)179bdc352b1SMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){
180bdc352b1SMatthias Ringwald if (le_notification_enabled) {
181bdc352b1SMatthias Ringwald beat();
182bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(con_handle);
183bdc352b1SMatthias Ringwald }
184bdc352b1SMatthias Ringwald
185bdc352b1SMatthias Ringwald // simulate battery drain
186bdc352b1SMatthias Ringwald battery--;
187bdc352b1SMatthias Ringwald if (battery < 50) {
188bdc352b1SMatthias Ringwald battery = 100;
189bdc352b1SMatthias Ringwald }
190bdc352b1SMatthias Ringwald battery_service_server_set_battery_value(battery);
191bdc352b1SMatthias Ringwald
192bdc352b1SMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
193bdc352b1SMatthias Ringwald btstack_run_loop_add_timer(ts);
194bdc352b1SMatthias Ringwald }
195bdc352b1SMatthias Ringwald /* LISTING_END */
196bdc352b1SMatthias Ringwald
197bdc352b1SMatthias Ringwald /*
198bdc352b1SMatthias Ringwald * @section Packet Handler
199bdc352b1SMatthias Ringwald *
200bdc352b1SMatthias Ringwald * @text The packet handler is used to:
201bdc352b1SMatthias Ringwald * - stop the counter after a disconnect
202bdc352b1SMatthias Ringwald * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received
203bdc352b1SMatthias Ringwald */
204bdc352b1SMatthias Ringwald
205bdc352b1SMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)206bdc352b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
207bdc352b1SMatthias Ringwald UNUSED(channel);
208bdc352b1SMatthias Ringwald UNUSED(size);
209bdc352b1SMatthias Ringwald
2107bbeb3adSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return;
2117bbeb3adSMilanka Ringwald
212bdc352b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
213bdc352b1SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE:
214bdc352b1SMatthias Ringwald le_notification_enabled = 0;
215bdc352b1SMatthias Ringwald break;
216bdc352b1SMatthias Ringwald case ATT_EVENT_CAN_SEND_NOW:
217bdc352b1SMatthias Ringwald att_server_notify(con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len);
218bdc352b1SMatthias Ringwald break;
2197bbeb3adSMilanka Ringwald default:
220bdc352b1SMatthias Ringwald break;
221bdc352b1SMatthias Ringwald }
222bdc352b1SMatthias Ringwald }
2237bbeb3adSMilanka Ringwald
224bdc352b1SMatthias Ringwald /* LISTING_END */
225bdc352b1SMatthias Ringwald
226bdc352b1SMatthias Ringwald /*
227bdc352b1SMatthias Ringwald * @section ATT Read
228bdc352b1SMatthias Ringwald *
229bdc352b1SMatthias Ringwald * @text The ATT Server handles all reads to constant data. For dynamic data like the custom characteristic, the registered
230bdc352b1SMatthias Ringwald * att_read_callback is called. To handle long characteristics and long reads, the att_read_callback is first called
231bdc352b1SMatthias Ringwald * with buffer == NULL, to request the total value length. Then it will be called again requesting a chunk of the value.
232bdc352b1SMatthias Ringwald * See Listing attRead.
233bdc352b1SMatthias Ringwald */
234bdc352b1SMatthias Ringwald
235bdc352b1SMatthias Ringwald /* LISTING_START(attRead): ATT Read */
236bdc352b1SMatthias Ringwald
237bdc352b1SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
238bdc352b1SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
239bdc352b1SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
240bdc352b1SMatthias Ringwald // @param offset defines start of attribute value
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)241bdc352b1SMatthias 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){
242bdc352b1SMatthias Ringwald UNUSED(connection_handle);
243bdc352b1SMatthias Ringwald
244bdc352b1SMatthias Ringwald if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
24534bd46a0SMatthias Ringwald return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size);
246bdc352b1SMatthias Ringwald }
247bdc352b1SMatthias Ringwald return 0;
248bdc352b1SMatthias Ringwald }
249bdc352b1SMatthias Ringwald /* LISTING_END */
250bdc352b1SMatthias Ringwald
251bdc352b1SMatthias Ringwald
252bdc352b1SMatthias Ringwald /*
253bdc352b1SMatthias Ringwald * @section ATT Write
254bdc352b1SMatthias Ringwald *
255*c0e4ccdaSMatthias Ringwald * @text The only valid ATT writes in this example are to the Client Characteristic Configuration, which configures notification
256*c0e4ccdaSMatthias Ringwald * and indication and to the the Characteristic Value.
257*c0e4ccdaSMatthias Ringwald * If the ATT handle matches the client configuration handle, the new configuration value is stored and used
258*c0e4ccdaSMatthias Ringwald * in the heartbeat handler to decide if a new value should be sent.
259*c0e4ccdaSMatthias Ringwald * If the ATT handle matches the characteristic value handle, we print the write as hexdump
260*c0e4ccdaSMatthias Ringwald * See Listing attWrite.
261bdc352b1SMatthias Ringwald */
262bdc352b1SMatthias Ringwald
263bdc352b1SMatthias Ringwald /* LISTING_START(attWrite): ATT Write */
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)264bdc352b1SMatthias 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){
265*c0e4ccdaSMatthias Ringwald switch (att_handle){
266*c0e4ccdaSMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
267bdc352b1SMatthias Ringwald le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
268bdc352b1SMatthias Ringwald con_handle = connection_handle;
269*c0e4ccdaSMatthias Ringwald break;
270*c0e4ccdaSMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
271*c0e4ccdaSMatthias Ringwald printf("Write: transaction mode %u, offset %u, data (%u bytes): ", transaction_mode, offset, buffer_size);
272*c0e4ccdaSMatthias Ringwald printf_hexdump(buffer, buffer_size);
273*c0e4ccdaSMatthias Ringwald break;
274*c0e4ccdaSMatthias Ringwald default:
275*c0e4ccdaSMatthias Ringwald break;
276*c0e4ccdaSMatthias Ringwald }
277bdc352b1SMatthias Ringwald return 0;
278bdc352b1SMatthias Ringwald }
279bdc352b1SMatthias Ringwald /* LISTING_END */
280bdc352b1SMatthias Ringwald
281bdc352b1SMatthias Ringwald int btstack_main(void);
btstack_main(void)282bdc352b1SMatthias Ringwald int btstack_main(void)
283bdc352b1SMatthias Ringwald {
284bdc352b1SMatthias Ringwald le_counter_setup();
285bdc352b1SMatthias Ringwald
286bdc352b1SMatthias Ringwald // turn on!
287bdc352b1SMatthias Ringwald hci_power_control(HCI_POWER_ON);
288bdc352b1SMatthias Ringwald
289bdc352b1SMatthias Ringwald return 0;
290bdc352b1SMatthias Ringwald }
291bdc352b1SMatthias Ringwald /* EXAMPLE_END */
292