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