xref: /btstack/example/gatt_battery_query.c (revision 3ee82ab15a812c0c64b33e6fc1b1a5320dc81d68)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38bcf00d8fSMatthias Ringwald // *****************************************************************************
39bcf00d8fSMatthias Ringwald //
40bcf00d8fSMatthias Ringwald // BLE Client
41bcf00d8fSMatthias Ringwald //
42bcf00d8fSMatthias Ringwald // *****************************************************************************
43bcf00d8fSMatthias Ringwald 
44bcf00d8fSMatthias Ringwald #include <stdint.h>
45bcf00d8fSMatthias Ringwald #include <stdio.h>
46bcf00d8fSMatthias Ringwald #include <stdlib.h>
47bcf00d8fSMatthias Ringwald #include <string.h>
48bcf00d8fSMatthias Ringwald 
49bcf00d8fSMatthias Ringwald #include "btstack.h"
50bcf00d8fSMatthias Ringwald 
51bcf00d8fSMatthias Ringwald typedef struct advertising_report {
52bcf00d8fSMatthias Ringwald     uint8_t   type;
53bcf00d8fSMatthias Ringwald     uint8_t   event_type;
54bcf00d8fSMatthias Ringwald     uint8_t   address_type;
55bcf00d8fSMatthias Ringwald     bd_addr_t address;
56bcf00d8fSMatthias Ringwald     uint8_t   rssi;
57bcf00d8fSMatthias Ringwald     uint8_t   length;
58*3ee82ab1SMilanka Ringwald     const uint8_t * data;
59bcf00d8fSMatthias Ringwald } advertising_report_t;
60bcf00d8fSMatthias Ringwald 
61bcf00d8fSMatthias Ringwald 
62bcf00d8fSMatthias Ringwald typedef enum {
63bcf00d8fSMatthias Ringwald     TC_IDLE,
64bcf00d8fSMatthias Ringwald     TC_W4_SCAN_RESULT,
65bcf00d8fSMatthias Ringwald     TC_W4_CONNECT,
66bcf00d8fSMatthias Ringwald     TC_W4_SERVICE_RESULT,
67bcf00d8fSMatthias Ringwald     TC_W4_CHARACTERISTIC_RESULT,
68bcf00d8fSMatthias Ringwald     TC_W4_BATTERY_DATA
69bcf00d8fSMatthias Ringwald } gc_state_t;
70bcf00d8fSMatthias Ringwald 
71bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
72bcf00d8fSMatthias Ringwald 
73bcf00d8fSMatthias Ringwald static bd_addr_t cmdline_addr = { };
74bcf00d8fSMatthias Ringwald static int cmdline_addr_found = 0;
75bcf00d8fSMatthias Ringwald 
76a59bfbf7SMatthias Ringwald static hci_con_handle_t connection_handle;
77bcf00d8fSMatthias Ringwald static uint16_t battery_service_uuid = 0x180F;
78bcf00d8fSMatthias Ringwald static uint16_t battery_level_characteristic_uuid = 0x2a19;
79bcf00d8fSMatthias Ringwald static gatt_client_service_t battery_service;
80bcf00d8fSMatthias Ringwald static gatt_client_characteristic_t config_characteristic;
81bcf00d8fSMatthias Ringwald 
82bcf00d8fSMatthias Ringwald static gc_state_t state = TC_IDLE;
83bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
84bcf00d8fSMatthias Ringwald 
85bcf00d8fSMatthias Ringwald static void printUUID(uint8_t * uuid128, uint16_t uuid16){
86bcf00d8fSMatthias Ringwald     if (uuid16){
87bcf00d8fSMatthias Ringwald         printf("%04x",uuid16);
88bcf00d8fSMatthias Ringwald     } else {
89bcf00d8fSMatthias Ringwald         printf("%s", uuid128_to_str(uuid128));
90bcf00d8fSMatthias Ringwald     }
91bcf00d8fSMatthias Ringwald }
92bcf00d8fSMatthias Ringwald 
93bcf00d8fSMatthias Ringwald static void dump_advertising_report(advertising_report_t * e){
94bcf00d8fSMatthias Ringwald     printf("    * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type,
95bcf00d8fSMatthias Ringwald            e->address_type, bd_addr_to_str(e->address), e->rssi, e->length);
96bcf00d8fSMatthias Ringwald     printf_hexdump(e->data, e->length);
97bcf00d8fSMatthias Ringwald 
98bcf00d8fSMatthias Ringwald }
99bcf00d8fSMatthias Ringwald 
100bcf00d8fSMatthias Ringwald static void dump_characteristic_value(uint8_t * blob, uint16_t blob_length){
101bcf00d8fSMatthias Ringwald     printf("    * characteristic value of length %d *** ", blob_length );
102bcf00d8fSMatthias Ringwald     printf_hexdump(blob , blob_length);
103bcf00d8fSMatthias Ringwald     printf("\n");
104bcf00d8fSMatthias Ringwald }
105bcf00d8fSMatthias Ringwald 
106bcf00d8fSMatthias Ringwald static void dump_characteristic(gatt_client_characteristic_t * characteristic){
107bcf00d8fSMatthias Ringwald     printf("    * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ",
108bcf00d8fSMatthias Ringwald                             characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties);
109bcf00d8fSMatthias Ringwald     printUUID(characteristic->uuid128, characteristic->uuid16);
110bcf00d8fSMatthias Ringwald     printf("\n");
111bcf00d8fSMatthias Ringwald }
112bcf00d8fSMatthias Ringwald 
113bcf00d8fSMatthias Ringwald static void dump_service(gatt_client_service_t * service){
114bcf00d8fSMatthias Ringwald     printf("    * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle);
115bcf00d8fSMatthias Ringwald     printUUID(service->uuid128, service->uuid16);
116bcf00d8fSMatthias Ringwald     printf("\n");
117bcf00d8fSMatthias Ringwald }
118bcf00d8fSMatthias Ringwald 
119bcf00d8fSMatthias Ringwald static void error_code(int status){
120bcf00d8fSMatthias Ringwald     switch(status){
121bcf00d8fSMatthias Ringwald         case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
122bcf00d8fSMatthias Ringwald             printf("ATT_ERROR_INSUFFICIENT_AUTHENTICATION. TODO: security manager is not complete yet.\n");
123bcf00d8fSMatthias Ringwald             break;
124bcf00d8fSMatthias Ringwald         default:
125bcf00d8fSMatthias Ringwald             printf(" status 0x%02x\n", status);
126bcf00d8fSMatthias Ringwald             break;
127bcf00d8fSMatthias Ringwald     }
128bcf00d8fSMatthias Ringwald }
129bcf00d8fSMatthias Ringwald 
130bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
131bcf00d8fSMatthias Ringwald 
132bcf00d8fSMatthias Ringwald     switch(state){
133bcf00d8fSMatthias Ringwald         case TC_W4_SERVICE_RESULT:
1340e2df43fSMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
135bcf00d8fSMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
136bcf00d8fSMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &battery_service);
137bcf00d8fSMatthias Ringwald                     printf("Battery service found:\n");
138bcf00d8fSMatthias Ringwald                     dump_service(&battery_service);
139bcf00d8fSMatthias Ringwald                     break;
140bcf00d8fSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
141bcf00d8fSMatthias Ringwald                     if (!packet[4]){
142bcf00d8fSMatthias Ringwald                         printf("Battery service not found. Restart scan.\n");
143bcf00d8fSMatthias Ringwald                         state = TC_W4_SCAN_RESULT;
144bcf00d8fSMatthias Ringwald                         gap_start_scan();
145bcf00d8fSMatthias Ringwald                         break;
146bcf00d8fSMatthias Ringwald                     }
147bcf00d8fSMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_RESULT;
148bcf00d8fSMatthias Ringwald                     printf("\nSearch for battery level characteristic in battery service. ");
149a59bfbf7SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &battery_service, battery_level_characteristic_uuid);
150bcf00d8fSMatthias Ringwald                     break;
151bcf00d8fSMatthias Ringwald                 default:
152bcf00d8fSMatthias Ringwald                     break;
153bcf00d8fSMatthias Ringwald             }
154bcf00d8fSMatthias Ringwald             break;
155bcf00d8fSMatthias Ringwald 
156bcf00d8fSMatthias Ringwald         case TC_W4_CHARACTERISTIC_RESULT:
1570e2df43fSMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
158bcf00d8fSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
159bcf00d8fSMatthias Ringwald                     printf("Battery level characteristic found:\n");
160bcf00d8fSMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &config_characteristic);
161bcf00d8fSMatthias Ringwald                     dump_characteristic(&config_characteristic);
162bcf00d8fSMatthias Ringwald                     break;
163bcf00d8fSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
164bcf00d8fSMatthias Ringwald                     state = TC_W4_BATTERY_DATA;
165bcf00d8fSMatthias Ringwald                     printf("\nConfigure battery level characteristic for notify.");
166a59bfbf7SMatthias Ringwald                     gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &config_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
167bcf00d8fSMatthias Ringwald                     break;
168bcf00d8fSMatthias Ringwald                 default:
169bcf00d8fSMatthias Ringwald                     break;
170bcf00d8fSMatthias Ringwald             }
171bcf00d8fSMatthias Ringwald             break;
172bcf00d8fSMatthias Ringwald         case TC_W4_BATTERY_DATA:
1730e2df43fSMatthias Ringwald             if (hci_event_packet_get_type(packet) == GATT_EVENT_QUERY_COMPLETE){
174bcf00d8fSMatthias Ringwald                 if (packet[4] != 0){
175bcf00d8fSMatthias Ringwald                     printf("\nNotification is not possible: ");
176bcf00d8fSMatthias Ringwald                     error_code(packet[4]);
177bcf00d8fSMatthias Ringwald                 }
178bcf00d8fSMatthias Ringwald                 break;
179bcf00d8fSMatthias Ringwald             }
1800e2df43fSMatthias Ringwald             if (hci_event_packet_get_type(packet) != GATT_EVENT_NOTIFICATION) break;
181bcf00d8fSMatthias Ringwald             printf("\nBattery Data:\n");
182bcf00d8fSMatthias Ringwald             dump_characteristic_value(&packet[8], little_endian_read_16(packet, 6));
183bcf00d8fSMatthias Ringwald             break;
184bcf00d8fSMatthias Ringwald 
185bcf00d8fSMatthias Ringwald         default:
186bcf00d8fSMatthias Ringwald             printf("error\n");
187bcf00d8fSMatthias Ringwald             break;
188bcf00d8fSMatthias Ringwald     }
189bcf00d8fSMatthias Ringwald 
190bcf00d8fSMatthias Ringwald }
191bcf00d8fSMatthias Ringwald 
192bcf00d8fSMatthias Ringwald static void fill_advertising_report_from_packet(advertising_report_t * report, uint8_t *packet){
193*3ee82ab1SMilanka Ringwald     gap_event_advertising_report_get_address(packet, report->address);
194*3ee82ab1SMilanka Ringwald     report->event_type = gap_event_advertising_report_get_advertising_event_type(packet);
195*3ee82ab1SMilanka Ringwald     report->address_type = gap_event_advertising_report_get_address_type(packet);
196*3ee82ab1SMilanka Ringwald     report->rssi = gap_event_advertising_report_get_rssi(packet);
197*3ee82ab1SMilanka Ringwald     report->length = gap_event_advertising_report_get_data_length(packet);
198*3ee82ab1SMilanka Ringwald     report->data = gap_event_advertising_report_get_data(packet);
199bcf00d8fSMatthias Ringwald }
200bcf00d8fSMatthias Ringwald 
201bcf00d8fSMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
202bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
203bcf00d8fSMatthias Ringwald     advertising_report_t report;
2040e2df43fSMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
205bcf00d8fSMatthias Ringwald     switch (event) {
206bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
207bcf00d8fSMatthias Ringwald             // BTstack activated, get started
208bcf00d8fSMatthias Ringwald             if (packet[2] != HCI_STATE_WORKING) break;
209bcf00d8fSMatthias Ringwald             if (cmdline_addr_found){
210bcf00d8fSMatthias Ringwald                 printf("Start connect to %s\n", bd_addr_to_str(cmdline_addr));
211bcf00d8fSMatthias Ringwald                 state = TC_W4_CONNECT;
212bcf00d8fSMatthias Ringwald                 gap_connect(cmdline_addr, 0);
213bcf00d8fSMatthias Ringwald                 break;
214bcf00d8fSMatthias Ringwald             }
215bcf00d8fSMatthias Ringwald             printf("BTstack activated, start scanning!\n");
216bcf00d8fSMatthias Ringwald             state = TC_W4_SCAN_RESULT;
217bcf00d8fSMatthias Ringwald             gap_set_scan_parameters(0,0x0030, 0x0030);
218bcf00d8fSMatthias Ringwald             gap_start_scan();
219bcf00d8fSMatthias Ringwald             break;
220045013feSMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:
221bcf00d8fSMatthias Ringwald             if (state != TC_W4_SCAN_RESULT) return;
222bcf00d8fSMatthias Ringwald             fill_advertising_report_from_packet(&report, packet);
223*3ee82ab1SMilanka Ringwald             dump_advertising_report(&report);
224*3ee82ab1SMilanka Ringwald 
225bcf00d8fSMatthias Ringwald             // stop scanning, and connect to the device
226bcf00d8fSMatthias Ringwald             state = TC_W4_CONNECT;
227bcf00d8fSMatthias Ringwald             gap_stop_scan();
228bcf00d8fSMatthias Ringwald             printf("Stop scan. Start connect to device with addr %s.\n", bd_addr_to_str(report.address));
229bcf00d8fSMatthias Ringwald             gap_connect(report.address,report.address_type);
230bcf00d8fSMatthias Ringwald             break;
231bcf00d8fSMatthias Ringwald         case HCI_EVENT_LE_META:
232bcf00d8fSMatthias Ringwald             // wait for connection complete
233bcf00d8fSMatthias Ringwald             if (packet[2] !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
234bcf00d8fSMatthias Ringwald             if (state != TC_W4_CONNECT) return;
235a59bfbf7SMatthias Ringwald             connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
236bcf00d8fSMatthias Ringwald             // initialize gatt client context with handle, and add it to the list of active clients
237bcf00d8fSMatthias Ringwald             // query primary services
2388e14b3b1SMatthias Ringwald             printf("\nSearch for battery service.\n");
239bcf00d8fSMatthias Ringwald             state = TC_W4_SERVICE_RESULT;
240a59bfbf7SMatthias Ringwald             gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid);
241bcf00d8fSMatthias Ringwald             break;
242bcf00d8fSMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
243bcf00d8fSMatthias Ringwald             printf("\nDISCONNECTED\n");
244bcf00d8fSMatthias Ringwald             exit(0);
245bcf00d8fSMatthias Ringwald             break;
246bcf00d8fSMatthias Ringwald         default:
247bcf00d8fSMatthias Ringwald             break;
248bcf00d8fSMatthias Ringwald     }
249bcf00d8fSMatthias Ringwald }
250bcf00d8fSMatthias Ringwald 
251bcf00d8fSMatthias Ringwald static void usage(const char *name){
252bcf00d8fSMatthias Ringwald 	fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
253bcf00d8fSMatthias Ringwald 	fprintf(stderr, "If no argument is provided, GATT browser will start scanning and connect to the first found device.\nTo connect to a specific device use argument [-a].\n\n");
254bcf00d8fSMatthias Ringwald }
255bcf00d8fSMatthias Ringwald 
256bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
257bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
258bcf00d8fSMatthias Ringwald 
259bcf00d8fSMatthias Ringwald     int arg = 1;
260bcf00d8fSMatthias Ringwald     cmdline_addr_found = 0;
261bcf00d8fSMatthias Ringwald 
262bcf00d8fSMatthias Ringwald     while (arg < argc) {
263bcf00d8fSMatthias Ringwald 		if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
264bcf00d8fSMatthias Ringwald 			arg++;
265a6efb919SMatthias Ringwald 			cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
266bcf00d8fSMatthias Ringwald             arg++;
267bcf00d8fSMatthias Ringwald             continue;
268bcf00d8fSMatthias Ringwald         }
269bcf00d8fSMatthias Ringwald         usage(argv[0]);
270bcf00d8fSMatthias Ringwald         return 0;
271bcf00d8fSMatthias Ringwald 	}
272bcf00d8fSMatthias Ringwald 
273bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_handler;
274bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
275bcf00d8fSMatthias Ringwald 
276bcf00d8fSMatthias Ringwald     l2cap_init();
277bcf00d8fSMatthias Ringwald 
278bcf00d8fSMatthias Ringwald     gatt_client_init();
279bcf00d8fSMatthias Ringwald 
280bcf00d8fSMatthias Ringwald     sm_init();
281bcf00d8fSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
282bcf00d8fSMatthias Ringwald 
283bcf00d8fSMatthias Ringwald     // turn on!
284bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
285bcf00d8fSMatthias Ringwald 
286bcf00d8fSMatthias Ringwald     return 0;
287bcf00d8fSMatthias Ringwald }
288bcf00d8fSMatthias Ringwald 
289bcf00d8fSMatthias Ringwald 
290bcf00d8fSMatthias Ringwald 
291bcf00d8fSMatthias Ringwald 
292