xref: /btstack/example/gatt_browser.c (revision 0e2df43f5cbae3fc71139523458b98f30307d21b)
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 /* EXAMPLE_START(gatt_browser): GATT Client - Discovering primary services and their characteristics
40bcf00d8fSMatthias Ringwald  *
41bcf00d8fSMatthias Ringwald  * @text This example shows how to use the GATT Client
42bcf00d8fSMatthias Ringwald  * API to discover primary services and their characteristics of the first found
43bcf00d8fSMatthias Ringwald  * device that is advertising its services.
44bcf00d8fSMatthias Ringwald  *
45bcf00d8fSMatthias Ringwald  * The logic is divided between the HCI and GATT client packet handlers.
46bcf00d8fSMatthias Ringwald  * The HCI packet handler is responsible for finding a remote device,
47bcf00d8fSMatthias Ringwald  * connecting to it, and for starting the first GATT client query.
48bcf00d8fSMatthias Ringwald  * Then, the GATT client packet handler receives all primary services and
49bcf00d8fSMatthias Ringwald  * requests the characteristics of the last one to keep the example short.
50bcf00d8fSMatthias Ringwald  *
51bcf00d8fSMatthias Ringwald  */
52bcf00d8fSMatthias Ringwald // *****************************************************************************
53bcf00d8fSMatthias Ringwald 
54bcf00d8fSMatthias Ringwald #include <stdint.h>
55bcf00d8fSMatthias Ringwald #include <stdio.h>
56bcf00d8fSMatthias Ringwald #include <stdlib.h>
57bcf00d8fSMatthias Ringwald #include <string.h>
58bcf00d8fSMatthias Ringwald 
59bcf00d8fSMatthias Ringwald #include "btstack.h"
60bcf00d8fSMatthias Ringwald 
61bcf00d8fSMatthias Ringwald typedef struct advertising_report {
62bcf00d8fSMatthias Ringwald     uint8_t   type;
63bcf00d8fSMatthias Ringwald     uint8_t   event_type;
64bcf00d8fSMatthias Ringwald     uint8_t   address_type;
65bcf00d8fSMatthias Ringwald     bd_addr_t address;
66bcf00d8fSMatthias Ringwald     uint8_t   rssi;
67bcf00d8fSMatthias Ringwald     uint8_t   length;
68bcf00d8fSMatthias Ringwald     uint8_t * data;
69bcf00d8fSMatthias Ringwald } advertising_report_t;
70bcf00d8fSMatthias Ringwald 
71bcf00d8fSMatthias Ringwald static bd_addr_t cmdline_addr = { };
72bcf00d8fSMatthias Ringwald static int cmdline_addr_found = 0;
73bcf00d8fSMatthias Ringwald 
74a59bfbf7SMatthias Ringwald static hci_con_handle_t connection_handler;
75bcf00d8fSMatthias Ringwald static gatt_client_service_t services[40];
76bcf00d8fSMatthias Ringwald static int service_count = 0;
77bcf00d8fSMatthias Ringwald static int service_index = 0;
78bcf00d8fSMatthias Ringwald 
79bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
80bcf00d8fSMatthias Ringwald 
81bcf00d8fSMatthias Ringwald /* @section GATT client setup
82bcf00d8fSMatthias Ringwald  *
83bcf00d8fSMatthias Ringwald  * @text In the setup phase, a GATT client must register the HCI and GATT client
84bcf00d8fSMatthias Ringwald  * packet handlers, as shown in Listing GATTClientSetup.
85bcf00d8fSMatthias Ringwald  * Additionally, the security manager can be setup, if signed writes, or
86bcf00d8fSMatthias Ringwald  * encrypted, or authenticated connection are required, to access the
87bcf00d8fSMatthias Ringwald  * characteristics, as explained in Section on [SMP](../protocols/#sec:smpProtocols).
88bcf00d8fSMatthias Ringwald  */
89bcf00d8fSMatthias Ringwald 
90bcf00d8fSMatthias Ringwald /* LISTING_START(GATTClientSetup): Setting up GATT client */
91bcf00d8fSMatthias Ringwald 
92bcf00d8fSMatthias Ringwald // Handles connect, disconnect, and advertising report events,
93bcf00d8fSMatthias Ringwald // starts the GATT client, and sends the first query.
94bcf00d8fSMatthias Ringwald static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
95bcf00d8fSMatthias Ringwald 
96bcf00d8fSMatthias Ringwald // Handles GATT client query results, sends queries and the
97bcf00d8fSMatthias Ringwald // GAP disconnect command when the querying is done.
98bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
99bcf00d8fSMatthias Ringwald 
100bcf00d8fSMatthias Ringwald static void gatt_client_setup(void){
101bcf00d8fSMatthias Ringwald 
102bcf00d8fSMatthias Ringwald     // register for HCI events
103bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &handle_hci_event;
104bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
105bcf00d8fSMatthias Ringwald 
106bcf00d8fSMatthias Ringwald     // Initialize L2CAP and register HCI event handler
107bcf00d8fSMatthias Ringwald     l2cap_init();
108bcf00d8fSMatthias Ringwald 
109bcf00d8fSMatthias Ringwald     // Initialize GATT client
110bcf00d8fSMatthias Ringwald     gatt_client_init();
111bcf00d8fSMatthias Ringwald 
112bcf00d8fSMatthias Ringwald     // Optinoally, Setup security manager
113bcf00d8fSMatthias Ringwald     sm_init();
114bcf00d8fSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
115bcf00d8fSMatthias Ringwald }
116bcf00d8fSMatthias Ringwald /* LISTING_END */
117bcf00d8fSMatthias Ringwald 
118bcf00d8fSMatthias Ringwald static void printUUID(uint8_t * uuid128, uint16_t uuid16){
119bcf00d8fSMatthias Ringwald     if (uuid16){
120bcf00d8fSMatthias Ringwald         printf("%04x",uuid16);
121bcf00d8fSMatthias Ringwald     } else {
122bcf00d8fSMatthias Ringwald         printf("%s", uuid128_to_str(uuid128));
123bcf00d8fSMatthias Ringwald     }
124bcf00d8fSMatthias Ringwald }
125bcf00d8fSMatthias Ringwald 
126bcf00d8fSMatthias Ringwald static void dump_advertising_report(advertising_report_t * e){
127bcf00d8fSMatthias Ringwald     printf("    * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type,
128bcf00d8fSMatthias Ringwald            e->address_type, bd_addr_to_str(e->address), e->rssi, e->length);
129bcf00d8fSMatthias Ringwald     printf_hexdump(e->data, e->length);
130bcf00d8fSMatthias Ringwald 
131bcf00d8fSMatthias Ringwald }
132bcf00d8fSMatthias Ringwald 
133bcf00d8fSMatthias Ringwald static void dump_characteristic(gatt_client_characteristic_t * characteristic){
134bcf00d8fSMatthias Ringwald     printf("    * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ",
135bcf00d8fSMatthias Ringwald                             characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties);
136bcf00d8fSMatthias Ringwald     printUUID(characteristic->uuid128, characteristic->uuid16);
137bcf00d8fSMatthias Ringwald     printf("\n");
138bcf00d8fSMatthias Ringwald }
139bcf00d8fSMatthias Ringwald 
140bcf00d8fSMatthias Ringwald static void dump_service(gatt_client_service_t * service){
141bcf00d8fSMatthias Ringwald     printf("    * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle);
142bcf00d8fSMatthias Ringwald     printUUID(service->uuid128, service->uuid16);
143bcf00d8fSMatthias Ringwald     printf("\n");
144bcf00d8fSMatthias Ringwald }
145bcf00d8fSMatthias Ringwald 
146bcf00d8fSMatthias Ringwald static void fill_advertising_report_from_packet(advertising_report_t * report, uint8_t *packet){
147bcf00d8fSMatthias Ringwald     int pos = 2;
148bcf00d8fSMatthias Ringwald     report->event_type = packet[pos++];
149bcf00d8fSMatthias Ringwald     report->address_type = packet[pos++];
150bcf00d8fSMatthias Ringwald     reverse_bd_addr(&packet[pos], report->address);
151bcf00d8fSMatthias Ringwald     pos += 6;
152bcf00d8fSMatthias Ringwald     report->rssi = packet[pos++];
153bcf00d8fSMatthias Ringwald     report->length = packet[pos++];
154bcf00d8fSMatthias Ringwald     report->data = &packet[pos];
155bcf00d8fSMatthias Ringwald     pos += report->length;
156bcf00d8fSMatthias Ringwald     dump_advertising_report(report);
157bcf00d8fSMatthias Ringwald }
158bcf00d8fSMatthias Ringwald 
159bcf00d8fSMatthias Ringwald 
160bcf00d8fSMatthias Ringwald /* @section HCI packet handler
161bcf00d8fSMatthias Ringwald  *
162bcf00d8fSMatthias Ringwald  * @text The HCI packet handler has to start the scanning,
163bcf00d8fSMatthias Ringwald  * to find the first advertising device, to stop scanning, to connect
164bcf00d8fSMatthias Ringwald  * to and later to disconnect from it, to start the GATT client upon
165bcf00d8fSMatthias Ringwald  * the connection is completed, and to send the first query - in this
166bcf00d8fSMatthias Ringwald  * case the gatt_client_discover_primary_services() is called, see
167bcf00d8fSMatthias Ringwald  * Listing GATTBrowserHCIPacketHandler.
168bcf00d8fSMatthias Ringwald  */
169bcf00d8fSMatthias Ringwald 
170bcf00d8fSMatthias Ringwald /* LISTING_START(GATTBrowserHCIPacketHandler): Connecting and disconnecting from the GATT client */
171bcf00d8fSMatthias Ringwald static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
172bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
173bcf00d8fSMatthias Ringwald     advertising_report_t report;
174bcf00d8fSMatthias Ringwald 
175*0e2df43fSMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
176bcf00d8fSMatthias Ringwald     switch (event) {
177bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
178bcf00d8fSMatthias Ringwald             // BTstack activated, get started
179bcf00d8fSMatthias Ringwald             if (packet[2] != HCI_STATE_WORKING) break;
180bcf00d8fSMatthias Ringwald             if (cmdline_addr_found){
181bcf00d8fSMatthias Ringwald                 printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr));
182bcf00d8fSMatthias Ringwald                 gap_connect(cmdline_addr, 0);
183bcf00d8fSMatthias Ringwald                 break;
184bcf00d8fSMatthias Ringwald             }
185bcf00d8fSMatthias Ringwald             printf("BTstack activated, start scanning!\n");
186bcf00d8fSMatthias Ringwald             gap_set_scan_parameters(0,0x0030, 0x0030);
187bcf00d8fSMatthias Ringwald             gap_start_scan();
188bcf00d8fSMatthias Ringwald             break;
189bcf00d8fSMatthias Ringwald         case GAP_LE_EVENT_ADVERTISING_REPORT:
190bcf00d8fSMatthias Ringwald             fill_advertising_report_from_packet(&report, packet);
191bcf00d8fSMatthias Ringwald             // stop scanning, and connect to the device
192bcf00d8fSMatthias Ringwald             gap_stop_scan();
193bcf00d8fSMatthias Ringwald             gap_connect(report.address,report.address_type);
194bcf00d8fSMatthias Ringwald             break;
195bcf00d8fSMatthias Ringwald         case HCI_EVENT_LE_META:
196bcf00d8fSMatthias Ringwald             // wait for connection complete
197bcf00d8fSMatthias Ringwald             if (packet[2] !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
198a59bfbf7SMatthias Ringwald             connection_handler = hci_subevent_le_connection_complete_get_connection_handle(packet);
199bcf00d8fSMatthias Ringwald             // query primary services
200a59bfbf7SMatthias Ringwald             gatt_client_discover_primary_services(handle_gatt_client_event, connection_handler);
201bcf00d8fSMatthias Ringwald             break;
202bcf00d8fSMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
203bcf00d8fSMatthias Ringwald             printf("\nGATT browser - DISCONNECTED\n");
204bcf00d8fSMatthias Ringwald             exit(0);
205bcf00d8fSMatthias Ringwald             break;
206bcf00d8fSMatthias Ringwald         default:
207bcf00d8fSMatthias Ringwald             break;
208bcf00d8fSMatthias Ringwald     }
209bcf00d8fSMatthias Ringwald }
210bcf00d8fSMatthias Ringwald /* LISTING_END */
211bcf00d8fSMatthias Ringwald 
212bcf00d8fSMatthias Ringwald /* @section GATT Client event handler
213bcf00d8fSMatthias Ringwald  *
214bcf00d8fSMatthias Ringwald  * @text Query results and further queries are handled by the GATT client packet
215bcf00d8fSMatthias Ringwald  * handler, as shown in Listing GATTBrowserQueryHandler. Here, upon
216bcf00d8fSMatthias Ringwald  * receiving the primary services, the
217bcf00d8fSMatthias Ringwald  * gatt_client_discover_characteristics_for_service() query for the last
218bcf00d8fSMatthias Ringwald  * received service is sent. After receiving the characteristics for the service,
219bcf00d8fSMatthias Ringwald  * gap_disconnect is called to terminate the connection. Upon
220bcf00d8fSMatthias Ringwald  * disconnect, the HCI packet handler receives the disconnect complete event.
221bcf00d8fSMatthias Ringwald  */
222bcf00d8fSMatthias Ringwald 
223bcf00d8fSMatthias Ringwald /* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */
224bcf00d8fSMatthias Ringwald static int search_services = 1;
225bcf00d8fSMatthias Ringwald 
226bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
227bcf00d8fSMatthias Ringwald     gatt_client_service_t service;
228bcf00d8fSMatthias Ringwald     gatt_client_characteristic_t characteristic;
229*0e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
230bcf00d8fSMatthias Ringwald         case GATT_EVENT_SERVICE_QUERY_RESULT:\
231bcf00d8fSMatthias Ringwald             gatt_event_service_query_result_get_service(packet, &service);
232bcf00d8fSMatthias Ringwald             dump_service(&service);
233bcf00d8fSMatthias Ringwald             services[service_count++] = service;
234bcf00d8fSMatthias Ringwald             break;
235bcf00d8fSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
236bcf00d8fSMatthias Ringwald             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
237bcf00d8fSMatthias Ringwald             dump_characteristic(&characteristic);
238bcf00d8fSMatthias Ringwald             break;
239bcf00d8fSMatthias Ringwald         case GATT_EVENT_QUERY_COMPLETE:
240bcf00d8fSMatthias Ringwald             if (search_services){
241bcf00d8fSMatthias Ringwald                 // GATT_EVENT_QUERY_COMPLETE of search services
242bcf00d8fSMatthias Ringwald                 service_index = 0;
243bcf00d8fSMatthias Ringwald                 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s\n", uuid128_to_str(service.uuid128));
244bcf00d8fSMatthias Ringwald                 search_services = 0;
245a59bfbf7SMatthias Ringwald                 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &services[service_index]);
246bcf00d8fSMatthias Ringwald             } else {
247bcf00d8fSMatthias Ringwald                 // GATT_EVENT_QUERY_COMPLETE of search characteristics
248bcf00d8fSMatthias Ringwald                 if (service_index < service_count) {
249bcf00d8fSMatthias Ringwald                     service = services[service_index++];
250bcf00d8fSMatthias Ringwald                     printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n",
251bcf00d8fSMatthias Ringwald                         uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle);
252a59bfbf7SMatthias Ringwald                     gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &service);
253bcf00d8fSMatthias Ringwald                     break;
254bcf00d8fSMatthias Ringwald                 }
255bcf00d8fSMatthias Ringwald                 service_index = 0;
256a59bfbf7SMatthias Ringwald                 gap_disconnect(connection_handler);
257bcf00d8fSMatthias Ringwald             }
258bcf00d8fSMatthias Ringwald             break;
259bcf00d8fSMatthias Ringwald         default:
260bcf00d8fSMatthias Ringwald             break;
261bcf00d8fSMatthias Ringwald     }
262bcf00d8fSMatthias Ringwald }
263bcf00d8fSMatthias Ringwald /* LISTING_END */
264bcf00d8fSMatthias Ringwald 
265bcf00d8fSMatthias Ringwald static void usage(const char *name){
266bcf00d8fSMatthias Ringwald 	fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
267bcf00d8fSMatthias 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");
268bcf00d8fSMatthias Ringwald }
269bcf00d8fSMatthias Ringwald 
270bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
271bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
272bcf00d8fSMatthias Ringwald 
273bcf00d8fSMatthias Ringwald     int arg = 1;
274bcf00d8fSMatthias Ringwald     cmdline_addr_found = 0;
275bcf00d8fSMatthias Ringwald 
276bcf00d8fSMatthias Ringwald     while (arg < argc) {
277bcf00d8fSMatthias Ringwald 		if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
278bcf00d8fSMatthias Ringwald 			arg++;
279bcf00d8fSMatthias Ringwald 			cmdline_addr_found = sscanf_bd_addr((uint8_t *)argv[arg], cmdline_addr);
280bcf00d8fSMatthias Ringwald             arg++;
281bcf00d8fSMatthias Ringwald             continue;
282bcf00d8fSMatthias Ringwald         }
283bcf00d8fSMatthias Ringwald         usage(argv[0]);
284bcf00d8fSMatthias Ringwald         return 0;
285bcf00d8fSMatthias Ringwald 	}
286bcf00d8fSMatthias Ringwald 
287bcf00d8fSMatthias Ringwald     gatt_client_setup();
288bcf00d8fSMatthias Ringwald 
289bcf00d8fSMatthias Ringwald     // turn on!
290bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
291bcf00d8fSMatthias Ringwald 
292bcf00d8fSMatthias Ringwald     return 0;
293bcf00d8fSMatthias Ringwald }
294bcf00d8fSMatthias Ringwald 
295bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
296bcf00d8fSMatthias Ringwald 
297bcf00d8fSMatthias Ringwald 
298