xref: /btstack/example/gatt_browser.c (revision bcf00d8fa453bd3a5c441c3b99e6e4b685f0d8f0)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 /* EXAMPLE_START(gatt_browser): GATT Client - Discovering primary services and their characteristics
40  *
41  * @text This example shows how to use the GATT Client
42  * API to discover primary services and their characteristics of the first found
43  * device that is advertising its services.
44  *
45  * The logic is divided between the HCI and GATT client packet handlers.
46  * The HCI packet handler is responsible for finding a remote device,
47  * connecting to it, and for starting the first GATT client query.
48  * Then, the GATT client packet handler receives all primary services and
49  * requests the characteristics of the last one to keep the example short.
50  *
51  */
52 // *****************************************************************************
53 
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 
59 #include "btstack.h"
60 
61 typedef struct advertising_report {
62     uint8_t   type;
63     uint8_t   event_type;
64     uint8_t   address_type;
65     bd_addr_t address;
66     uint8_t   rssi;
67     uint8_t   length;
68     uint8_t * data;
69 } advertising_report_t;
70 
71 static bd_addr_t cmdline_addr = { };
72 static int cmdline_addr_found = 0;
73 
74 static uint16_t gc_handle;
75 static gatt_client_service_t services[40];
76 static int service_count = 0;
77 static int service_index = 0;
78 
79 static btstack_packet_callback_registration_t hci_event_callback_registration;
80 
81 /* @section GATT client setup
82  *
83  * @text In the setup phase, a GATT client must register the HCI and GATT client
84  * packet handlers, as shown in Listing GATTClientSetup.
85  * Additionally, the security manager can be setup, if signed writes, or
86  * encrypted, or authenticated connection are required, to access the
87  * characteristics, as explained in Section on [SMP](../protocols/#sec:smpProtocols).
88  */
89 
90 /* LISTING_START(GATTClientSetup): Setting up GATT client */
91 
92 // Handles connect, disconnect, and advertising report events,
93 // starts the GATT client, and sends the first query.
94 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
95 
96 // Handles GATT client query results, sends queries and the
97 // GAP disconnect command when the querying is done.
98 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
99 
100 static void gatt_client_setup(void){
101 
102     // register for HCI events
103     hci_event_callback_registration.callback = &handle_hci_event;
104     hci_add_event_handler(&hci_event_callback_registration);
105 
106     // Initialize L2CAP and register HCI event handler
107     l2cap_init();
108 
109     // Initialize GATT client
110     gatt_client_init();
111 
112     // Optinoally, Setup security manager
113     sm_init();
114     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
115 }
116 /* LISTING_END */
117 
118 static void printUUID(uint8_t * uuid128, uint16_t uuid16){
119     if (uuid16){
120         printf("%04x",uuid16);
121     } else {
122         printf("%s", uuid128_to_str(uuid128));
123     }
124 }
125 
126 static void dump_advertising_report(advertising_report_t * e){
127     printf("    * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type,
128            e->address_type, bd_addr_to_str(e->address), e->rssi, e->length);
129     printf_hexdump(e->data, e->length);
130 
131 }
132 
133 static void dump_characteristic(gatt_client_characteristic_t * characteristic){
134     printf("    * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ",
135                             characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties);
136     printUUID(characteristic->uuid128, characteristic->uuid16);
137     printf("\n");
138 }
139 
140 static void dump_service(gatt_client_service_t * service){
141     printf("    * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle);
142     printUUID(service->uuid128, service->uuid16);
143     printf("\n");
144 }
145 
146 static void fill_advertising_report_from_packet(advertising_report_t * report, uint8_t *packet){
147     int pos = 2;
148     report->event_type = packet[pos++];
149     report->address_type = packet[pos++];
150     reverse_bd_addr(&packet[pos], report->address);
151     pos += 6;
152     report->rssi = packet[pos++];
153     report->length = packet[pos++];
154     report->data = &packet[pos];
155     pos += report->length;
156     dump_advertising_report(report);
157 }
158 
159 
160 /* @section HCI packet handler
161  *
162  * @text The HCI packet handler has to start the scanning,
163  * to find the first advertising device, to stop scanning, to connect
164  * to and later to disconnect from it, to start the GATT client upon
165  * the connection is completed, and to send the first query - in this
166  * case the gatt_client_discover_primary_services() is called, see
167  * Listing GATTBrowserHCIPacketHandler.
168  */
169 
170 /* LISTING_START(GATTBrowserHCIPacketHandler): Connecting and disconnecting from the GATT client */
171 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
172     if (packet_type != HCI_EVENT_PACKET) return;
173     advertising_report_t report;
174 
175     uint8_t event = packet[0];
176     switch (event) {
177         case BTSTACK_EVENT_STATE:
178             // BTstack activated, get started
179             if (packet[2] != HCI_STATE_WORKING) break;
180             if (cmdline_addr_found){
181                 printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr));
182                 gap_connect(cmdline_addr, 0);
183                 break;
184             }
185             printf("BTstack activated, start scanning!\n");
186             gap_set_scan_parameters(0,0x0030, 0x0030);
187             gap_start_scan();
188             break;
189         case GAP_LE_EVENT_ADVERTISING_REPORT:
190             fill_advertising_report_from_packet(&report, packet);
191             // stop scanning, and connect to the device
192             gap_stop_scan();
193             gap_connect(report.address,report.address_type);
194             break;
195         case HCI_EVENT_LE_META:
196             // wait for connection complete
197             if (packet[2] !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
198             gc_handle = little_endian_read_16(packet, 4);
199             // query primary services
200             gatt_client_discover_primary_services(handle_gatt_client_event, gc_handle);
201             break;
202         case HCI_EVENT_DISCONNECTION_COMPLETE:
203             printf("\nGATT browser - DISCONNECTED\n");
204             exit(0);
205             break;
206         default:
207             break;
208     }
209 }
210 /* LISTING_END */
211 
212 /* @section GATT Client event handler
213  *
214  * @text Query results and further queries are handled by the GATT client packet
215  * handler, as shown in Listing GATTBrowserQueryHandler. Here, upon
216  * receiving the primary services, the
217  * gatt_client_discover_characteristics_for_service() query for the last
218  * received service is sent. After receiving the characteristics for the service,
219  * gap_disconnect is called to terminate the connection. Upon
220  * disconnect, the HCI packet handler receives the disconnect complete event.
221  */
222 
223 /* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */
224 static int search_services = 1;
225 
226 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
227     gatt_client_service_t service;
228     gatt_client_characteristic_t characteristic;
229     switch(packet[0]){
230         case GATT_EVENT_SERVICE_QUERY_RESULT:\
231             gatt_event_service_query_result_get_service(packet, &service);
232             dump_service(&service);
233             services[service_count++] = service;
234             break;
235         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
236             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
237             dump_characteristic(&characteristic);
238             break;
239         case GATT_EVENT_QUERY_COMPLETE:
240             if (search_services){
241                 // GATT_EVENT_QUERY_COMPLETE of search services
242                 service_index = 0;
243                 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s\n", uuid128_to_str(service.uuid128));
244                 search_services = 0;
245                 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, gc_handle, &services[service_index]);
246             } else {
247                 // GATT_EVENT_QUERY_COMPLETE of search characteristics
248                 if (service_index < service_count) {
249                     service = services[service_index++];
250                     printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n",
251                         uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle);
252                     gatt_client_discover_characteristics_for_service(handle_gatt_client_event, gc_handle, &service);
253                     break;
254                 }
255                 service_index = 0;
256                 gap_disconnect(gc_handle);
257             }
258             break;
259         default:
260             break;
261     }
262 }
263 /* LISTING_END */
264 
265 static void usage(const char *name){
266 	fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
267 	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");
268 }
269 
270 int btstack_main(int argc, const char * argv[]);
271 int btstack_main(int argc, const char * argv[]){
272 
273     int arg = 1;
274     cmdline_addr_found = 0;
275 
276     while (arg < argc) {
277 		if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
278 			arg++;
279 			cmdline_addr_found = sscanf_bd_addr((uint8_t *)argv[arg], cmdline_addr);
280             arg++;
281             continue;
282         }
283         usage(argv[0]);
284         return 0;
285 	}
286 
287     gatt_client_setup();
288 
289     // turn on!
290     hci_power_control(HCI_POWER_ON);
291 
292     return 0;
293 }
294 
295 /* EXAMPLE_END */
296 
297 
298