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