xref: /btstack/example/gatt_browser.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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     if (packet_type != HCI_EVENT_PACKET) return;
168     advertising_report_t report;
169 
170     uint8_t event = hci_event_packet_get_type(packet);
171     switch (event) {
172         case BTSTACK_EVENT_STATE:
173             // BTstack activated, get started
174             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
175             if (cmdline_addr_found){
176                 printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr));
177                 gap_connect(cmdline_addr, 0);
178                 break;
179             }
180             printf("BTstack activated, start scanning!\n");
181             gap_set_scan_parameters(0,0x0030, 0x0030);
182             gap_start_scan();
183             break;
184         case GAP_EVENT_ADVERTISING_REPORT:
185             fill_advertising_report_from_packet(&report, packet);
186             dump_advertising_report(&report);
187 
188             // stop scanning, and connect to the device
189             gap_stop_scan();
190             gap_connect(report.address,report.address_type);
191             break;
192         case HCI_EVENT_LE_META:
193             // wait for connection complete
194             if (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
195             connection_handler = hci_subevent_le_connection_complete_get_connection_handle(packet);
196             // query primary services
197             gatt_client_discover_primary_services(handle_gatt_client_event, connection_handler);
198             break;
199         case HCI_EVENT_DISCONNECTION_COMPLETE:
200             printf("\nGATT browser - DISCONNECTED\n");
201             exit(0);
202             break;
203         default:
204             break;
205     }
206 }
207 /* LISTING_END */
208 
209 /* @section GATT Client event handler
210  *
211  * @text Query results and further queries are handled by the GATT client packet
212  * handler, as shown in Listing GATTBrowserQueryHandler. Here, upon
213  * receiving the primary services, the
214  * gatt_client_discover_characteristics_for_service() query for the last
215  * received service is sent. After receiving the characteristics for the service,
216  * gap_disconnect is called to terminate the connection. Upon
217  * disconnect, the HCI packet handler receives the disconnect complete event.
218  */
219 
220 /* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */
221 static int search_services = 1;
222 
223 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
224     gatt_client_service_t service;
225     gatt_client_characteristic_t characteristic;
226     switch(hci_event_packet_get_type(packet)){
227         case GATT_EVENT_SERVICE_QUERY_RESULT:\
228             gatt_event_service_query_result_get_service(packet, &service);
229             dump_service(&service);
230             services[service_count++] = service;
231             break;
232         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
233             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
234             dump_characteristic(&characteristic);
235             break;
236         case GATT_EVENT_QUERY_COMPLETE:
237             if (search_services){
238                 // GATT_EVENT_QUERY_COMPLETE of search services
239                 service_index = 0;
240                 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s\n", uuid128_to_str(service.uuid128));
241                 search_services = 0;
242                 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &services[service_index]);
243             } else {
244                 // GATT_EVENT_QUERY_COMPLETE of search characteristics
245                 if (service_index < service_count) {
246                     service = services[service_index++];
247                     printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n",
248                         uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle);
249                     gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &service);
250                     break;
251                 }
252                 service_index = 0;
253                 gap_disconnect(connection_handler);
254             }
255             break;
256         default:
257             break;
258     }
259 }
260 /* LISTING_END */
261 
262 static void usage(const char *name){
263 	fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
264 	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");
265 }
266 
267 int btstack_main(int argc, const char * argv[]);
268 int btstack_main(int argc, const char * argv[]){
269 
270     int arg = 1;
271     cmdline_addr_found = 0;
272 
273     while (arg < argc) {
274 		if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
275 			arg++;
276 			cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
277             arg++;
278             continue;
279         }
280         usage(argv[0]);
281         return 0;
282 	}
283 
284     gatt_client_setup();
285 
286     // turn on!
287     hci_power_control(HCI_POWER_ON);
288 
289     return 0;
290 }
291 
292 /* EXAMPLE_END */
293 
294 
295