xref: /btstack/example/gatt_browser.c (revision 9ec2630ce4bfd36adcb438d2e8ba6fc26890f857)
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     const 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 hci_con_handle_t connection_handler;
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     gap_event_advertising_report_get_address(packet, report->address);
148     report->event_type = gap_event_advertising_report_get_advertising_event_type(packet);
149     report->address_type = gap_event_advertising_report_get_address_type(packet);
150     report->rssi = gap_event_advertising_report_get_rssi(packet);
151     report->length = gap_event_advertising_report_get_data_length(packet);
152     report->data = gap_event_advertising_report_get_data(packet);
153 }
154 
155 /* @section HCI packet handler
156  *
157  * @text The HCI packet handler has to start the scanning,
158  * to find the first advertising device, to stop scanning, to connect
159  * to and later to disconnect from it, to start the GATT client upon
160  * the connection is completed, and to send the first query - in this
161  * case the gatt_client_discover_primary_services() is called, see
162  * Listing GATTBrowserHCIPacketHandler.
163  */
164 
165 /* LISTING_START(GATTBrowserHCIPacketHandler): Connecting and disconnecting from the GATT client */
166 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
167     UNUSED(channel);
168     UNUSED(size);
169 
170     if (packet_type != HCI_EVENT_PACKET) return;
171     advertising_report_t report;
172 
173     uint8_t event = hci_event_packet_get_type(packet);
174     switch (event) {
175         case BTSTACK_EVENT_STATE:
176             // BTstack activated, get started
177             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
178             if (cmdline_addr_found){
179                 printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr));
180                 gap_connect(cmdline_addr, 0);
181                 break;
182             }
183             printf("BTstack activated, start scanning!\n");
184             gap_set_scan_parameters(0,0x0030, 0x0030);
185             gap_start_scan();
186             break;
187         case GAP_EVENT_ADVERTISING_REPORT:
188             fill_advertising_report_from_packet(&report, packet);
189             dump_advertising_report(&report);
190 
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 (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
198             connection_handler = hci_subevent_le_connection_complete_get_connection_handle(packet);
199             // query primary services
200             gatt_client_discover_primary_services(handle_gatt_client_event, connection_handler);
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     UNUSED(packet_type);
228     UNUSED(channel);
229     UNUSED(size);
230 
231     gatt_client_service_t service;
232     gatt_client_characteristic_t characteristic;
233     switch(hci_event_packet_get_type(packet)){
234         case GATT_EVENT_SERVICE_QUERY_RESULT:\
235             gatt_event_service_query_result_get_service(packet, &service);
236             dump_service(&service);
237             services[service_count++] = service;
238             break;
239         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
240             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
241             dump_characteristic(&characteristic);
242             break;
243         case GATT_EVENT_QUERY_COMPLETE:
244             if (search_services){
245                 // GATT_EVENT_QUERY_COMPLETE of search services
246                 service_index = 0;
247                 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s\n", uuid128_to_str(service.uuid128));
248                 search_services = 0;
249                 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &services[service_index]);
250             } else {
251                 // GATT_EVENT_QUERY_COMPLETE of search characteristics
252                 if (service_index < service_count) {
253                     service = services[service_index++];
254                     printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n",
255                         uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle);
256                     gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &service);
257                     break;
258                 }
259                 service_index = 0;
260                 gap_disconnect(connection_handler);
261             }
262             break;
263         default:
264             break;
265     }
266 }
267 /* LISTING_END */
268 
269 static void usage(const char *name){
270 	fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
271 	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");
272 }
273 
274 int btstack_main(int argc, const char * argv[]);
275 int btstack_main(int argc, const char * argv[]){
276 
277     int arg = 1;
278     cmdline_addr_found = 0;
279 
280     while (arg < argc) {
281 		if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
282 			arg++;
283 			cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
284             arg++;
285             continue;
286         }
287         usage(argv[0]);
288         return 0;
289 	}
290 
291     gatt_client_setup();
292 
293     // turn on!
294     hci_power_control(HCI_POWER_ON);
295 
296     return 0;
297 }
298 
299 /* EXAMPLE_END */
300 
301 
302