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