xref: /btstack/platform/daemon/src/daemon.c (revision 10a9d105186b5155bb21f19651123635cb490dca)
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__ "daemon.c"
39 
40 /*
41  *  daemon.c
42  *
43  *  Created by Matthias Ringwald on 7/1/09.
44  *
45  *  BTstack background daemon
46  *
47  */
48 
49 #include "btstack_config.h"
50 
51 #include <pthread.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <strings.h>
56 #include <unistd.h>
57 
58 #ifdef _WIN32
59 #include "Winsock2.h"
60 #endif
61 
62 #include <getopt.h>
63 
64 #include "btstack.h"
65 #include "btstack_client.h"
66 #include "btstack_debug.h"
67 #include "btstack_device_name_db.h"
68 #include "btstack_event.h"
69 #include "btstack_linked_list.h"
70 #include "btstack_run_loop.h"
71 #include "btstack_tlv_posix.h"
72 #include "btstack_util.h"
73 
74 #include "btstack_server.h"
75 
76 #ifdef _WIN32
77 #include "btstack_run_loop_windows.h"
78 #else
79 #include "btstack_run_loop_posix.h"
80 #endif
81 
82 #include "btstack_version.h"
83 #include "classic/btstack_link_key_db.h"
84 #include "classic/btstack_link_key_db_tlv.h"
85 #include "classic/rfcomm.h"
86 #include "classic/sdp_server.h"
87 #include "classic/sdp_client.h"
88 #include "classic/sdp_client_rfcomm.h"
89 #include "hci.h"
90 #include "hci_cmd.h"
91 #include "hci_dump.h"
92 #include "hci_dump_posix_fs.h"
93 #include "hci_dump_posix_stdout.h"
94 #include "hci_transport.h"
95 #include "hci_transport_h4.h"
96 #include "hci_transport_usb.h"
97 #include "l2cap.h"
98 #include "rfcomm_service_db.h"
99 #include "socket_connection.h"
100 
101 #ifdef HAVE_INTEL_USB
102 #include "btstack_chipset_intel_firmware.h"
103 #endif
104 
105 #ifdef ENABLE_BLE
106 #include "ble/gatt_client.h"
107 #include "ble/att_server.h"
108 #include "ble/att_db.h"
109 #include "ble/le_device_db.h"
110 #include "ble/le_device_db_tlv.h"
111 #include "ble/sm.h"
112 #endif
113 
114 // copy of prototypes
115 const btstack_device_name_db_t * btstack_device_name_db_corefoundation_instance(void);
116 const btstack_device_name_db_t * btstack_device_name_db_fs_instance(void);
117 const btstack_link_key_db_t * btstack_link_key_db_corefoundation_instance(void);
118 const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void);
119 
120 // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
121 #ifndef BTSTACK_LOG_TYPE
122 #define BTSTACK_LOG_TYPE HCI_DUMP_PACKETLOGGER
123 #endif
124 
125 #define DAEMON_NO_ACTIVE_CLIENT_TIMEOUT 10000
126 
127 #define ATT_MAX_LONG_ATTRIBUTE_SIZE 512
128 
129 
130 #define SERVICE_LENGTH                      20
131 #define CHARACTERISTIC_LENGTH               24
132 #define CHARACTERISTIC_DESCRIPTOR_LENGTH    18
133 
134 // ATT_MTU - 1
135 #define ATT_MAX_ATTRIBUTE_SIZE 22
136 
137 // HCI CMD OGF/OCF
138 #define READ_CMD_OGF(buffer) (buffer[1] >> 2)
139 #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
140 
141 typedef struct {
142     // linked list - assert: first field
143     btstack_linked_item_t    item;
144 
145     // connection
146     connection_t * connection;
147 
148     btstack_linked_list_t rfcomm_cids;
149     btstack_linked_list_t rfcomm_services;
150     btstack_linked_list_t l2cap_cids;
151     btstack_linked_list_t l2cap_psms;
152     btstack_linked_list_t sdp_record_handles;
153     btstack_linked_list_t gatt_con_handles;
154 
155     // power mode
156     HCI_POWER_MODE power_mode;
157 
158     // discoverable
159     uint8_t        discoverable;
160 
161 } client_state_t;
162 
163 typedef struct btstack_linked_list_uint32 {
164     btstack_linked_item_t   item;
165     uint32_t        value;
166 } btstack_linked_list_uint32_t;
167 
168 typedef struct btstack_linked_list_connection {
169     btstack_linked_item_t   item;
170     connection_t  * connection;
171 } btstack_linked_list_connection_t;
172 
173 typedef struct btstack_linked_list_gatt_client_helper{
174     btstack_linked_item_t item;
175     hci_con_handle_t con_handle;
176     connection_t * active_connection;   // the one that started the current query
177     btstack_linked_list_t  all_connections;     // list of all connections that ever used this helper
178     uint16_t characteristic_length;
179     uint16_t characteristic_handle;
180     uint8_t  characteristic_buffer[10 + ATT_MAX_LONG_ATTRIBUTE_SIZE];   // header for sending event right away
181     uint8_t  long_query_type;
182 } btstack_linked_list_gatt_client_helper_t;
183 
184 // MARK: prototypes
185 static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
186 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
187 #ifdef ENABLE_BLE
188 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size);
189 #endif
190 static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state);
191 static client_state_t * client_for_connection(connection_t *connection);
192 static int              clients_require_power_on(void);
193 static int              clients_require_discoverable(void);
194 static void              clients_clear_power_request(void);
195 static void start_power_off_timer(void);
196 static void stop_power_off_timer(void);
197 static client_state_t * client_for_connection(connection_t *connection);
198 static void hci_emit_system_bluetooth_enabled(uint8_t enabled);
199 static void stack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size);
200 static void btstack_server_configure_stack(void);
201 
202 // MARK: globals
203 
204 #ifdef HAVE_TRANSPORT_H4
205 static hci_transport_config_uart_t hci_transport_config_uart;
206 #endif
207 
208 // used for stack configuration
209 static const hci_transport_t * transport;
210 static void * config = NULL;
211 static btstack_control_t * control;
212 
213 #ifdef HAVE_INTEL_USB
214 static int intel_firmware_loaded;
215 #endif
216 
217 static btstack_timer_source_t timeout;
218 static uint8_t timeout_active = 0;
219 static int power_management_sleep = 0;
220 static btstack_linked_list_t clients = NULL;        // list of connected clients `
221 #ifdef ENABLE_BLE
222 static gatt_client_notification_t daemon_gatt_client_notifications;
223 static btstack_linked_list_t gatt_client_helpers = NULL;   // list of used gatt client (helpers)
224 #endif
225 
226 static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth_status_handler;
227 
228 static btstack_packet_callback_registration_t hci_event_callback_registration;
229 static btstack_packet_callback_registration_t l2cap_event_callback_registration;
230 static btstack_packet_callback_registration_t sm_event_callback_registration;
231 
232 static int global_enable = 0;
233 
234 static btstack_link_key_db_t    const * btstack_link_key_db = NULL;
235 static btstack_device_name_db_t const * btstack_device_name_db = NULL;
236 // static int rfcomm_channel_generator = 1;
237 
238 static uint8_t   attribute_value[1000];
239 static const int attribute_value_buffer_size = sizeof(attribute_value);
240 static uint8_t serviceSearchPattern[200];
241 static uint8_t attributeIDList[50];
242 static void * sdp_client_query_connection;
243 
244 static char string_buffer[1000];
245 
246 static int loggingEnabled;
247 
248 static const char * btstack_server_storage_path;
249 
250 // GAP command buffer
251 #ifdef ENABLE_CLASSIC
252 static uint8_t daemon_gap_pin_code[16];
253 #endif
254 
255 // TLV
256 static int                   tlv_setup_done;
257 static const btstack_tlv_t * tlv_impl;
258 static btstack_tlv_posix_t   tlv_context;
259 
260 static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state){
261     log_info("Bluetooth status: %u\n", state);
262 };
263 
264 static void daemon_no_connections_timeout(struct btstack_timer_source *ts){
265     if (clients_require_power_on()) return;    // false alarm :)
266     log_info("No active client connection for %u seconds -> POWER OFF\n", DAEMON_NO_ACTIVE_CLIENT_TIMEOUT/1000);
267     hci_power_control(HCI_POWER_OFF);
268 }
269 
270 
271 static void add_uint32_to_list(btstack_linked_list_t *list, uint32_t value){
272     btstack_linked_list_iterator_t it;
273     btstack_linked_list_iterator_init(&it, list);
274     while (btstack_linked_list_iterator_has_next(&it)){
275         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
276         if ( item->value == value) return; // already in list
277     }
278 
279     btstack_linked_list_uint32_t * item = malloc(sizeof(btstack_linked_list_uint32_t));
280     if (!item) return;
281     memset(item, 0, sizeof(btstack_linked_list_uint32_t));
282     item->value = value;
283     btstack_linked_list_add(list, (btstack_linked_item_t *) item);
284 }
285 
286 static void remove_and_free_uint32_from_list(btstack_linked_list_t *list, uint32_t value){
287     btstack_linked_list_iterator_t it;
288     btstack_linked_list_iterator_init(&it, list);
289     while (btstack_linked_list_iterator_has_next(&it)){
290         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
291         if ( item->value != value) continue;
292         btstack_linked_list_remove(list, (btstack_linked_item_t *) item);
293         free(item);
294     }
295 }
296 
297 static void daemon_add_client_rfcomm_service(connection_t * connection, uint16_t service_channel){
298     client_state_t * client_state = client_for_connection(connection);
299     if (!client_state) return;
300     add_uint32_to_list(&client_state->rfcomm_services, service_channel);
301 }
302 
303 static void daemon_remove_client_rfcomm_service(connection_t * connection, uint16_t service_channel){
304     client_state_t * client_state = client_for_connection(connection);
305     if (!client_state) return;
306     remove_and_free_uint32_from_list(&client_state->rfcomm_services, service_channel);
307 }
308 
309 static void daemon_add_client_rfcomm_channel(connection_t * connection, uint16_t cid){
310     client_state_t * client_state = client_for_connection(connection);
311     if (!client_state) return;
312     add_uint32_to_list(&client_state->rfcomm_cids, cid);
313 }
314 
315 static void daemon_remove_client_rfcomm_channel(connection_t * connection, uint16_t cid){
316     client_state_t * client_state = client_for_connection(connection);
317     if (!client_state) return;
318     remove_and_free_uint32_from_list(&client_state->rfcomm_cids, cid);
319 }
320 
321 static void daemon_add_client_l2cap_service(connection_t * connection, uint16_t psm){
322     client_state_t * client_state = client_for_connection(connection);
323     if (!client_state) return;
324     add_uint32_to_list(&client_state->l2cap_psms, psm);
325 }
326 
327 static void daemon_remove_client_l2cap_service(connection_t * connection, uint16_t psm){
328     client_state_t * client_state = client_for_connection(connection);
329     if (!client_state) return;
330     remove_and_free_uint32_from_list(&client_state->l2cap_psms, psm);
331 }
332 
333 static void daemon_add_client_l2cap_channel(connection_t * connection, uint16_t cid){
334     client_state_t * client_state = client_for_connection(connection);
335     if (!client_state) return;
336     add_uint32_to_list(&client_state->l2cap_cids, cid);
337 }
338 
339 static void daemon_remove_client_l2cap_channel(connection_t * connection, uint16_t cid){
340     client_state_t * client_state = client_for_connection(connection);
341     if (!client_state) return;
342     remove_and_free_uint32_from_list(&client_state->l2cap_cids, cid);
343 }
344 
345 static void daemon_add_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){
346     client_state_t * client_state = client_for_connection(connection);
347     if (!client_state) return;
348     add_uint32_to_list(&client_state->sdp_record_handles, handle);
349 }
350 
351 static void daemon_remove_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){
352     client_state_t * client_state = client_for_connection(connection);
353     if (!client_state) return;
354     remove_and_free_uint32_from_list(&client_state->sdp_record_handles, handle);
355 }
356 
357 #ifdef ENABLE_BLE
358 static void daemon_add_gatt_client_handle(connection_t * connection, uint32_t handle){
359     client_state_t * client_state = client_for_connection(connection);
360     if (!client_state) return;
361 
362     // check if handle already exists in the gatt_con_handles list
363     btstack_linked_list_iterator_t it;
364     int handle_found = 0;
365     btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
366     while (btstack_linked_list_iterator_has_next(&it)){
367         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
368         if (item->value == handle){
369             handle_found = 1;
370             break;
371         }
372     }
373     // if handle doesn't exist add it to gatt_con_handles
374     if (!handle_found){
375         add_uint32_to_list(&client_state->gatt_con_handles, handle);
376     }
377 
378     // check if there is a helper with given handle
379     btstack_linked_list_gatt_client_helper_t * gatt_helper = NULL;
380     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
381     while (btstack_linked_list_iterator_has_next(&it)){
382         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
383         if (item->con_handle == handle){
384             gatt_helper = item;
385             break;
386         }
387     }
388 
389     // if gatt_helper doesn't exist, create it and add it to gatt_client_helpers list
390     if (!gatt_helper){
391         gatt_helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1);
392         if (!gatt_helper) return;
393         gatt_helper->con_handle = handle;
394         btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) gatt_helper);
395     }
396 
397     // check if connection exists
398     int connection_found = 0;
399     btstack_linked_list_iterator_init(&it, &gatt_helper->all_connections);
400     while (btstack_linked_list_iterator_has_next(&it)){
401         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
402         if (item->connection == connection){
403             connection_found = 1;
404             break;
405         }
406     }
407 
408     // if connection is not found, add it to the all_connections, and set it as active connection
409     if (!connection_found){
410         btstack_linked_list_connection_t * con = calloc(sizeof(btstack_linked_list_connection_t), 1);
411         if (!con) return;
412         con->connection = connection;
413         btstack_linked_list_add(&gatt_helper->all_connections, (btstack_linked_item_t *)con);
414     }
415 }
416 
417 
418 static void daemon_remove_gatt_client_handle(connection_t * connection, uint32_t handle){
419     // PART 1 - uses connection & handle
420     // might be extracted or vanish totally
421     client_state_t * client_state = client_for_connection(connection);
422     if (!client_state) return;
423 
424     btstack_linked_list_iterator_t it;
425     // remove handle from gatt_con_handles list
426     btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
427     while (btstack_linked_list_iterator_has_next(&it)){
428         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
429         if (item->value == handle){
430             btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item);
431             free(item);
432         }
433     }
434 
435     // PART 2 - only uses handle
436 
437     // find helper with given handle
438     btstack_linked_list_gatt_client_helper_t * helper = NULL;
439     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
440     while (btstack_linked_list_iterator_has_next(&it)){
441         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
442         if (item->con_handle == handle){
443             helper = item;
444             break;
445         }
446     }
447 
448     if (!helper) return;
449     // remove connection from helper
450     btstack_linked_list_iterator_init(&it, &helper->all_connections);
451     while (btstack_linked_list_iterator_has_next(&it)){
452         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
453         if (item->connection == connection){
454             btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item);
455             free(item);
456             break;
457         }
458     }
459 
460     if (helper->active_connection == connection){
461         helper->active_connection = NULL;
462     }
463     // if helper has no more connections, call disconnect
464     if (helper->all_connections == NULL){
465         gap_disconnect((hci_con_handle_t) helper->con_handle);
466     }
467 }
468 
469 
470 static void daemon_remove_gatt_client_helper(uint32_t con_handle){
471     log_info("daemon_remove_gatt_client_helper for con_handle 0x%04x", con_handle);
472 
473     btstack_linked_list_iterator_t it, cl;
474     // find helper with given handle
475     btstack_linked_list_gatt_client_helper_t * helper = NULL;
476     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
477     while (btstack_linked_list_iterator_has_next(&it)){
478         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
479         if (item->con_handle == con_handle){
480             helper = item;
481             break;
482         }
483     }
484 
485     if (!helper) return;
486 
487     // remove all connection from helper
488     btstack_linked_list_iterator_init(&it, &helper->all_connections);
489     while (btstack_linked_list_iterator_has_next(&it)){
490         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
491         btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item);
492         free(item);
493     }
494 
495     btstack_linked_list_remove(&gatt_client_helpers, (btstack_linked_item_t *) helper);
496     free(helper);
497 
498     btstack_linked_list_iterator_init(&cl, &clients);
499     while (btstack_linked_list_iterator_has_next(&cl)){
500         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
501         btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
502         while (btstack_linked_list_iterator_has_next(&it)){
503             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
504             if (item->value == con_handle){
505                 btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item);
506                 free(item);
507             }
508         }
509     }
510 }
511 #endif
512 
513 static void daemon_rfcomm_close_connection(client_state_t * daemon_client){
514     btstack_linked_list_iterator_t it;
515     btstack_linked_list_t *rfcomm_services = &daemon_client->rfcomm_services;
516     btstack_linked_list_t *rfcomm_cids = &daemon_client->rfcomm_cids;
517 
518     btstack_linked_list_iterator_init(&it, rfcomm_services);
519     while (btstack_linked_list_iterator_has_next(&it)){
520         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
521         rfcomm_unregister_service(item->value);
522         btstack_linked_list_remove(rfcomm_services, (btstack_linked_item_t *) item);
523         free(item);
524     }
525 
526     btstack_linked_list_iterator_init(&it, rfcomm_cids);
527     while (btstack_linked_list_iterator_has_next(&it)){
528         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
529         rfcomm_disconnect(item->value);
530         btstack_linked_list_remove(rfcomm_cids, (btstack_linked_item_t *) item);
531         free(item);
532     }
533 }
534 
535 
536 static void daemon_l2cap_close_connection(client_state_t * daemon_client){
537     btstack_linked_list_iterator_t it;
538     btstack_linked_list_t *l2cap_psms = &daemon_client->l2cap_psms;
539     btstack_linked_list_t *l2cap_cids = &daemon_client->l2cap_cids;
540 
541     btstack_linked_list_iterator_init(&it, l2cap_psms);
542     while (btstack_linked_list_iterator_has_next(&it)){
543         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
544         l2cap_unregister_service(item->value);
545         btstack_linked_list_remove(l2cap_psms, (btstack_linked_item_t *) item);
546         free(item);
547     }
548 
549     btstack_linked_list_iterator_init(&it, l2cap_cids);
550     while (btstack_linked_list_iterator_has_next(&it)){
551         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
552         l2cap_disconnect(item->value);
553         btstack_linked_list_remove(l2cap_cids, (btstack_linked_item_t *) item);
554         free(item);
555     }
556 }
557 
558 static void daemon_sdp_close_connection(client_state_t * daemon_client){
559     btstack_linked_list_t * list = &daemon_client->sdp_record_handles;
560     btstack_linked_list_iterator_t it;
561     btstack_linked_list_iterator_init(&it, list);
562     while (btstack_linked_list_iterator_has_next(&it)){
563         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
564         sdp_unregister_service(item->value);
565         btstack_linked_list_remove(list, (btstack_linked_item_t *) item);
566         free(item);
567     }
568 }
569 
570 static connection_t * connection_for_l2cap_cid(uint16_t cid){
571     btstack_linked_list_iterator_t cl;
572     btstack_linked_list_iterator_init(&cl, &clients);
573     while (btstack_linked_list_iterator_has_next(&cl)){
574         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
575         btstack_linked_list_iterator_t it;
576         btstack_linked_list_iterator_init(&it, &client_state->l2cap_cids);
577         while (btstack_linked_list_iterator_has_next(&it)){
578             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
579             if (item->value == cid){
580                 return client_state->connection;
581             }
582         }
583     }
584     return NULL;
585 }
586 
587 static const uint8_t removeServiceRecordHandleAttributeIDList[] = { 0x36, 0x00, 0x05, 0x0A, 0x00, 0x01, 0xFF, 0xFF };
588 
589 // register a service record
590 // pre: AttributeIDs are in ascending order
591 // pre: ServiceRecordHandle is first attribute and is not already registered in database
592 // @returns status
593 static uint32_t daemon_sdp_create_and_register_service(uint8_t * record){
594 
595     // create new handle
596     uint32_t record_handle = sdp_create_service_record_handle();
597 
598     // calculate size of new service record: DES (2 byte len)
599     // + ServiceRecordHandle attribute (UINT16 UINT32) + size of existing attributes
600     uint16_t recordSize =  3 + (3 + 5) + de_get_data_size(record);
601 
602     // alloc memory for new service record
603     uint8_t * newRecord = malloc(recordSize);
604     if (!newRecord) return 0;
605 
606     // create DES for new record
607     de_create_sequence(newRecord);
608 
609     // set service record handle
610     de_add_number(newRecord, DE_UINT, DE_SIZE_16, 0);
611     de_add_number(newRecord, DE_UINT, DE_SIZE_32, record_handle);
612 
613     // add other attributes
614     sdp_append_attributes_in_attributeIDList(record, (uint8_t *) removeServiceRecordHandleAttributeIDList, 0, recordSize, newRecord);
615 
616     uint8_t status = sdp_register_service(newRecord);
617 
618     if (status) {
619         free(newRecord);
620         return 0;
621     }
622 
623     return record_handle;
624 }
625 
626 static connection_t * connection_for_rfcomm_cid(uint16_t cid){
627     btstack_linked_list_iterator_t cl;
628     btstack_linked_list_iterator_init(&cl, &clients);
629     while (btstack_linked_list_iterator_has_next(&cl)){
630         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
631         btstack_linked_list_iterator_t it;
632         btstack_linked_list_iterator_init(&it, &client_state->rfcomm_cids);
633         while (btstack_linked_list_iterator_has_next(&it)){
634             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
635             if (item->value == cid){
636                 return client_state->connection;
637             }
638         }
639     }
640     return NULL;
641 }
642 
643 #ifdef ENABLE_BLE
644 static void daemon_gatt_client_close_connection(connection_t * connection){
645     client_state_t * client = client_for_connection(connection);
646     if (!client) return;
647 
648     btstack_linked_list_iterator_t it;
649 
650     btstack_linked_list_iterator_init(&it, &client->gatt_con_handles);
651     while (btstack_linked_list_iterator_has_next(&it)){
652         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
653         daemon_remove_gatt_client_handle(connection, item->value);
654     }
655 }
656 #endif
657 
658 static void daemon_disconnect_client(connection_t * connection){
659     log_info("Daemon disconnect client %p\n",connection);
660 
661     client_state_t * client = client_for_connection(connection);
662     if (!client) return;
663 
664     daemon_sdp_close_connection(client);
665     daemon_rfcomm_close_connection(client);
666     daemon_l2cap_close_connection(client);
667 #ifdef ENABLE_BLE
668     // NOTE: experimental - disconnect all LE connections where GATT Client was used
669     // gatt_client_disconnect_connection(connection);
670     daemon_gatt_client_close_connection(connection);
671 #endif
672 
673     btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
674     free(client);
675 }
676 
677 static void hci_emit_btstack_version(void){
678     log_info("DAEMON_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
679     uint8_t event[6];
680     event[0] = DAEMON_EVENT_VERSION;
681     event[1] = sizeof(event) - 2;
682     event[2] = BTSTACK_MAJOR;
683     event[3] = BTSTACK_MINOR;
684     little_endian_store_16(event, 4, 3257);    // last SVN commit on Google Code + 1
685     socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event));
686 }
687 
688 static void hci_emit_system_bluetooth_enabled(uint8_t enabled){
689     log_info("DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
690     uint8_t event[3];
691     event[0] = DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED;
692     event[1] = sizeof(event) - 2;
693     event[2] = enabled;
694     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
695     socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event));
696 }
697 
698 static void send_l2cap_connection_open_failed(connection_t * connection, bd_addr_t address, uint16_t psm, uint8_t status){
699     // emit error - see l2cap.c:l2cap_emit_channel_opened(..)
700     uint8_t event[23];
701     memset(event, 0, sizeof(event));
702     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
703     event[1] = sizeof(event) - 2;
704     event[2] = status;
705     reverse_bd_addr(address, &event[3]);
706     // little_endian_store_16(event,  9, channel->handle);
707     little_endian_store_16(event, 11, psm);
708     // little_endian_store_16(event, 13, channel->local_cid);
709     // little_endian_store_16(event, 15, channel->remote_cid);
710     // little_endian_store_16(event, 17, channel->local_mtu);
711     // little_endian_store_16(event, 19, channel->remote_mtu);
712     // little_endian_store_16(event, 21, channel->flush_timeout);
713     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
714     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
715 }
716 
717 static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
718     uint8_t event[5];
719     event[0] = DAEMON_EVENT_L2CAP_SERVICE_REGISTERED;
720     event[1] = sizeof(event) - 2;
721     event[2] = status;
722     little_endian_store_16(event, 3, psm);
723     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
724     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
725 }
726 
727 static void rfcomm_emit_service_registered(void *connection, uint8_t status, uint8_t channel){
728     uint8_t event[4];
729     event[0] = DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED;
730     event[1] = sizeof(event) - 2;
731     event[2] = status;
732     event[3] = channel;
733     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
734     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
735 }
736 
737 static void send_rfcomm_create_channel_failed(void * connection, bd_addr_t addr, uint8_t server_channel, uint8_t status){
738     // emit error - see rfcom.c:rfcomm_emit_channel_open_failed_outgoing_memory(..)
739     uint8_t event[16];
740     memset(event, 0, sizeof(event));
741     uint8_t pos = 0;
742     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
743     event[pos++] = sizeof(event) - 2;
744     event[pos++] = status;
745     reverse_bd_addr(addr, &event[pos]); pos += 6;
746     little_endian_store_16(event,  pos, 0);   pos += 2;
747     event[pos++] = server_channel;
748     little_endian_store_16(event, pos, 0); pos += 2;   // channel ID
749     little_endian_store_16(event, pos, 0); pos += 2;   // max frame size
750     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
751     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
752 }
753 
754 // data: event(8), len(8), status(8), service_record_handle(32)
755 static void sdp_emit_service_registered(void *connection, uint32_t handle, uint8_t status) {
756     uint8_t event[7];
757     event[0] = DAEMON_EVENT_SDP_SERVICE_REGISTERED;
758     event[1] = sizeof(event) - 2;
759     event[2] = status;
760     little_endian_store_32(event, 3, handle);
761     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
762     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
763 }
764 
765 #ifdef ENABLE_BLE
766 
767 btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(hci_con_handle_t con_handle) {
768     btstack_linked_list_iterator_t it;
769     if (!gatt_client_helpers) return NULL;
770     log_debug("daemon_get_gatt_client_helper for handle 0x%02x", con_handle);
771 
772     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
773     while (btstack_linked_list_iterator_has_next(&it)){
774         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
775         if (item->con_handle == con_handle){
776             return item;
777         }
778     }
779     log_info("no gatt_client_helper for handle 0x%02x yet", con_handle);
780     return NULL;
781 }
782 
783 static void send_gatt_query_complete(connection_t * connection, hci_con_handle_t con_handle, uint8_t status){
784     // @format H1
785     uint8_t event[5];
786     event[0] = GATT_EVENT_QUERY_COMPLETE;
787     event[1] = 3;
788     little_endian_store_16(event, 2, con_handle);
789     event[4] = status;
790     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
791     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
792 }
793 
794 static void send_gatt_mtu_event(connection_t * connection, hci_con_handle_t con_handle, uint16_t mtu){
795     uint8_t event[6];
796     int pos = 0;
797     event[pos++] = GATT_EVENT_MTU;
798     event[pos++] = sizeof(event) - 2;
799     little_endian_store_16(event, pos, con_handle);
800     pos += 2;
801     little_endian_store_16(event, pos, mtu);
802     pos += 2;
803     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
804     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
805 }
806 
807 btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(connection_t *connection, uint8_t *packet, int track_active_connection) {
808     hci_con_handle_t con_handle = little_endian_read_16(packet, 3);
809     log_info("daemon_setup_gatt_client_request for handle 0x%02x", con_handle);
810     hci_connection_t * hci_con = hci_connection_for_handle(con_handle);
811     if ((hci_con == NULL) || (hci_con->state != OPEN)){
812         send_gatt_query_complete(connection, con_handle, GATT_CLIENT_NOT_CONNECTED);
813         return NULL;
814     }
815 
816     btstack_linked_list_gatt_client_helper_t * helper = daemon_get_gatt_client_helper(con_handle);
817 
818     if (!helper){
819         log_info("helper does not exist");
820         helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1);
821         if (!helper) return NULL;
822         helper->con_handle = con_handle;
823         btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) helper);
824     }
825 
826     if (track_active_connection && helper->active_connection){
827         send_gatt_query_complete(connection, con_handle, GATT_CLIENT_BUSY);
828         return NULL;
829     }
830 
831     daemon_add_gatt_client_handle(connection, con_handle);
832 
833     if (track_active_connection){
834         // remember connection responsible for this request
835         helper->active_connection = connection;
836     }
837 
838     return helper;
839 }
840 
841 // (de)serialize structs from/to HCI commands/events
842 
843 void daemon_gatt_serialize_service(gatt_client_service_t * service, uint8_t * event, int offset){
844     little_endian_store_16(event, offset, service->start_group_handle);
845     little_endian_store_16(event, offset+2, service->end_group_handle);
846     reverse_128(service->uuid128, &event[offset + 4]);
847 }
848 
849 void daemon_gatt_serialize_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * event, int offset){
850     little_endian_store_16(event, offset, characteristic->start_handle);
851     little_endian_store_16(event, offset+2, characteristic->value_handle);
852     little_endian_store_16(event, offset+4, characteristic->end_handle);
853     little_endian_store_16(event, offset+6, characteristic->properties);
854     reverse_128(characteristic->uuid128, &event[offset+8]);
855 }
856 
857 void daemon_gatt_serialize_characteristic_descriptor(gatt_client_characteristic_descriptor_t * characteristic_descriptor, uint8_t * event, int offset){
858     little_endian_store_16(event, offset, characteristic_descriptor->handle);
859     reverse_128(characteristic_descriptor->uuid128, &event[offset+2]);
860 }
861 
862 #endif
863 
864 #ifdef HAVE_INTEL_USB
865 static void btstack_server_intel_firmware_done(int result){
866     intel_firmware_loaded = 1;
867     // setup stack
868     btstack_server_configure_stack();
869     // start power up
870     hci_power_control(HCI_POWER_ON);
871 }
872 #endif
873 
874 static int btstack_command_handler(connection_t *connection, uint8_t *packet, uint16_t size){
875 
876     bd_addr_t addr;
877 #ifdef ENABLE_BLE
878     bd_addr_type_t addr_type;
879     hci_con_handle_t handle;
880 #endif
881 #ifdef ENABLE_CLASSIC
882     uint8_t  rfcomm_channel;
883     uint8_t  rfcomm_credits;
884     uint16_t mtu;
885     uint32_t service_record_handle;
886     uint16_t cid;
887     uint16_t service_channel;
888     uint16_t serviceSearchPatternLen;
889     uint16_t attributeIDListLen;
890     uint16_t psm;
891 #endif
892     client_state_t *client;
893     uint8_t status;
894     uint8_t  * data;
895 #if defined(HAVE_MALLOC) && defined(ENABLE_BLE)
896     uint8_t uuid128[16];
897     gatt_client_service_t service;
898     gatt_client_characteristic_t characteristic;
899     gatt_client_characteristic_descriptor_t descriptor;
900     uint16_t data_length;
901     btstack_linked_list_gatt_client_helper_t * gatt_helper;
902 #endif
903 
904 
905     // verbose log info before other info to allow for better tracking
906     hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
907 
908     // BTstack internal commands - 16 Bit OpCode, 8 Bit ParamLen, Params...
909     switch (READ_CMD_OCF(packet)){
910         case BTSTACK_GET_STATE:
911             log_info("BTSTACK_GET_STATE");
912             hci_emit_state();
913             break;
914         case BTSTACK_SET_POWER_MODE:
915             log_info("BTSTACK_SET_POWER_MODE %u", packet[3]);
916             // track client power requests
917             client = client_for_connection(connection);
918             if (!client) break;
919             client->power_mode = packet[3];
920             // handle merged state
921             if (!clients_require_power_on()){
922                 start_power_off_timer();
923             } else if (!power_management_sleep) {
924                 stop_power_off_timer();
925 #ifdef HAVE_INTEL_USB
926                 if (!intel_firmware_loaded){
927                     // before staring up the stack, load intel firmware
928                     btstack_chipset_intel_download_firmware(transport, &btstack_server_intel_firmware_done);
929                     break;
930                 }
931 #endif
932                 hci_power_control(HCI_POWER_ON);
933             }
934             break;
935         case BTSTACK_GET_VERSION:
936             log_info("BTSTACK_GET_VERSION");
937             hci_emit_btstack_version();
938             break;
939         case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
940         case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
941             hci_emit_system_bluetooth_enabled(0);
942             break;
943         case BTSTACK_SET_DISCOVERABLE:
944             log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]);
945             // track client discoverable requests
946             client = client_for_connection(connection);
947             if (!client) break;
948             client->discoverable = packet[3];
949             // merge state
950             gap_discoverable_control(clients_require_discoverable());
951             break;
952         case BTSTACK_SET_BLUETOOTH_ENABLED:
953             log_info("BTSTACK_SET_BLUETOOTH_ENABLED: %u\n", packet[3]);
954             if (packet[3]) {
955                 // global enable
956                 global_enable = 1;
957                 hci_power_control(HCI_POWER_ON);
958             } else {
959                 global_enable = 0;
960                 clients_clear_power_request();
961                 hci_power_control(HCI_POWER_OFF);
962             }
963             break;
964 #ifdef ENABLE_CLASSIC
965         case L2CAP_CREATE_CHANNEL_MTU:
966             reverse_bd_addr(&packet[3], addr);
967             psm = little_endian_read_16(packet, 9);
968             mtu = little_endian_read_16(packet, 11);
969             status = l2cap_create_channel(NULL, addr, psm, mtu, &cid);
970             if (status){
971                 send_l2cap_connection_open_failed(connection, addr, psm, status);
972             } else {
973                 daemon_add_client_l2cap_channel(connection, cid);
974             }
975             break;
976         case L2CAP_CREATE_CHANNEL:
977             reverse_bd_addr(&packet[3], addr);
978             psm = little_endian_read_16(packet, 9);
979             mtu = 150; // until r865
980             status = l2cap_create_channel(NULL, addr, psm, mtu, &cid);
981             if (status){
982                 send_l2cap_connection_open_failed(connection, addr, psm, status);
983             } else {
984                 daemon_add_client_l2cap_channel(connection, cid);
985             }
986             break;
987         case L2CAP_DISCONNECT:
988             cid = little_endian_read_16(packet, 3);
989             l2cap_disconnect(cid);
990             break;
991         case L2CAP_REGISTER_SERVICE:
992             psm = little_endian_read_16(packet, 3);
993             mtu = little_endian_read_16(packet, 5);
994             status = l2cap_register_service(NULL, psm, mtu, LEVEL_0);
995             daemon_add_client_l2cap_service(connection, little_endian_read_16(packet, 3));
996             l2cap_emit_service_registered(connection, status, psm);
997             break;
998         case L2CAP_UNREGISTER_SERVICE:
999             psm = little_endian_read_16(packet, 3);
1000             daemon_remove_client_l2cap_service(connection, psm);
1001             l2cap_unregister_service(psm);
1002             break;
1003         case L2CAP_ACCEPT_CONNECTION:
1004             cid    = little_endian_read_16(packet, 3);
1005             l2cap_accept_connection(cid);
1006             break;
1007         case L2CAP_DECLINE_CONNECTION:
1008             cid    = little_endian_read_16(packet, 3);
1009             l2cap_decline_connection(cid);
1010             break;
1011         case RFCOMM_CREATE_CHANNEL:
1012             reverse_bd_addr(&packet[3], addr);
1013             rfcomm_channel = packet[9];
1014             status = rfcomm_create_channel(&stack_packet_handler, addr, rfcomm_channel, &cid);
1015             if (status){
1016                 send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status);
1017             } else {
1018                 daemon_add_client_rfcomm_channel(connection, cid);
1019             }
1020             break;
1021         case RFCOMM_CREATE_CHANNEL_WITH_CREDITS:
1022             reverse_bd_addr(&packet[3], addr);
1023             rfcomm_channel = packet[9];
1024             rfcomm_credits = packet[10];
1025             status = rfcomm_create_channel_with_initial_credits(&stack_packet_handler, addr, rfcomm_channel, rfcomm_credits, &cid );
1026             if (status){
1027                 send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status);
1028             } else {
1029                 daemon_add_client_rfcomm_channel(connection, cid);
1030             }
1031             break;
1032         case RFCOMM_DISCONNECT:
1033             cid = little_endian_read_16(packet, 3);
1034             rfcomm_disconnect(cid);
1035             break;
1036         case RFCOMM_REGISTER_SERVICE:
1037             rfcomm_channel = packet[3];
1038             mtu = little_endian_read_16(packet, 4);
1039             status = rfcomm_register_service(&stack_packet_handler, rfcomm_channel, mtu);
1040             rfcomm_emit_service_registered(connection, status, rfcomm_channel);
1041             break;
1042         case RFCOMM_REGISTER_SERVICE_WITH_CREDITS:
1043             rfcomm_channel = packet[3];
1044             mtu = little_endian_read_16(packet, 4);
1045             rfcomm_credits = packet[6];
1046             status = rfcomm_register_service_with_initial_credits(&stack_packet_handler, rfcomm_channel, mtu, rfcomm_credits);
1047             rfcomm_emit_service_registered(connection, status, rfcomm_channel);
1048             break;
1049         case RFCOMM_UNREGISTER_SERVICE:
1050             service_channel = little_endian_read_16(packet, 3);
1051             daemon_remove_client_rfcomm_service(connection, service_channel);
1052             rfcomm_unregister_service(service_channel);
1053             break;
1054         case RFCOMM_ACCEPT_CONNECTION:
1055             cid    = little_endian_read_16(packet, 3);
1056             rfcomm_accept_connection(cid);
1057             break;
1058         case RFCOMM_DECLINE_CONNECTION:
1059             cid    = little_endian_read_16(packet, 3);
1060             rfcomm_decline_connection(cid);
1061             break;
1062         case RFCOMM_GRANT_CREDITS:
1063             cid    = little_endian_read_16(packet, 3);
1064             rfcomm_credits = packet[5];
1065             rfcomm_grant_credits(cid, rfcomm_credits);
1066             break;
1067         case RFCOMM_PERSISTENT_CHANNEL: {
1068             // enforce \0
1069             packet[3+248] = 0;
1070             rfcomm_channel = rfcomm_service_db_channel_for_service((char*)&packet[3]);
1071             log_info("DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL %u", rfcomm_channel);
1072             uint8_t event[4];
1073             event[0] = DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL;
1074             event[1] = sizeof(event) - 2;
1075             event[2] = 0;
1076             event[3] = rfcomm_channel;
1077             hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1078             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
1079             break;
1080         }
1081         case SDP_REGISTER_SERVICE_RECORD:
1082             log_info("SDP_REGISTER_SERVICE_RECORD size %u\n", size);
1083             service_record_handle = daemon_sdp_create_and_register_service(&packet[3]);
1084             if (service_record_handle){
1085                 daemon_add_client_sdp_service_record_handle(connection, service_record_handle);
1086                 sdp_emit_service_registered(connection, service_record_handle, 0);
1087             } else {
1088                sdp_emit_service_registered(connection, 0, BTSTACK_MEMORY_ALLOC_FAILED);
1089             }
1090             break;
1091         case SDP_UNREGISTER_SERVICE_RECORD:
1092             service_record_handle = little_endian_read_32(packet, 3);
1093             log_info("SDP_UNREGISTER_SERVICE_RECORD handle 0x%x ", service_record_handle);
1094             data = sdp_get_record_for_handle(service_record_handle);
1095             sdp_unregister_service(service_record_handle);
1096             daemon_remove_client_sdp_service_record_handle(connection, service_record_handle);
1097             if (data){
1098                 free(data);
1099             }
1100             break;
1101         case SDP_CLIENT_QUERY_RFCOMM_SERVICES:
1102             reverse_bd_addr(&packet[3], addr);
1103 
1104             serviceSearchPatternLen = de_get_len(&packet[9]);
1105             memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
1106 
1107             sdp_client_query_connection = connection;
1108             sdp_client_query_rfcomm_channel_and_name_for_search_pattern(&handle_sdp_rfcomm_service_result, addr, serviceSearchPattern);
1109 
1110             break;
1111         case SDP_CLIENT_QUERY_SERVICES:
1112             reverse_bd_addr(&packet[3], addr);
1113             sdp_client_query_connection = connection;
1114 
1115             serviceSearchPatternLen = de_get_len(&packet[9]);
1116             memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
1117 
1118             attributeIDListLen = de_get_len(&packet[9+serviceSearchPatternLen]);
1119             memcpy(attributeIDList, &packet[9+serviceSearchPatternLen], attributeIDListLen);
1120 
1121             sdp_client_query(&handle_sdp_client_query_result, addr, (uint8_t*)&serviceSearchPattern[0], (uint8_t*)&attributeIDList[0]);
1122             break;
1123 #endif
1124         case GAP_DISCONNECT:
1125             handle = little_endian_read_16(packet, 3);
1126             gap_disconnect(handle);
1127             break;
1128 #ifdef ENABLE_CLASSIC
1129         case GAP_INQUIRY_START:
1130             gap_inquiry_start(packet[3]);
1131             break;
1132         case GAP_INQUIRY_STOP:
1133             gap_inquiry_stop();
1134             break;
1135         case GAP_REMOTE_NAME_REQUEST:
1136             reverse_bd_addr(&packet[3], addr);
1137             gap_remote_name_request(addr, packet[9], little_endian_read_16(packet, 10));
1138             break;
1139         case GAP_DROP_LINK_KEY_FOR_BD_ADDR:
1140             reverse_bd_addr(&packet[3], addr);
1141             gap_drop_link_key_for_bd_addr(addr);
1142             break;
1143         case GAP_DELETE_ALL_LINK_KEYS:
1144             gap_delete_all_link_keys();
1145             break;
1146         case GAP_PIN_CODE_RESPONSE:
1147             reverse_bd_addr(&packet[3], addr);
1148             memcpy(daemon_gap_pin_code, &packet[10], 16);
1149             gap_pin_code_response_binary(addr, daemon_gap_pin_code, packet[9]);
1150             break;
1151         case GAP_PIN_CODE_NEGATIVE:
1152             reverse_bd_addr(&packet[3], addr);
1153             gap_pin_code_negative(addr);
1154             break;
1155 #endif
1156 #ifdef ENABLE_BLE
1157         case GAP_LE_SCAN_START:
1158             gap_start_scan();
1159             break;
1160         case GAP_LE_SCAN_STOP:
1161             gap_stop_scan();
1162             break;
1163         case GAP_LE_SET_SCAN_PARAMETERS:
1164             gap_set_scan_parameters(packet[3], little_endian_read_16(packet, 4), little_endian_read_16(packet, 6));
1165             break;
1166         case GAP_LE_CONNECT:
1167             reverse_bd_addr(&packet[4], addr);
1168             addr_type = packet[3];
1169             gap_connect(addr, addr_type);
1170             break;
1171         case GAP_LE_CONNECT_CANCEL:
1172             gap_connect_cancel();
1173             break;
1174 #endif
1175 #if defined(HAVE_MALLOC) && defined(ENABLE_BLE)
1176         case GATT_DISCOVER_ALL_PRIMARY_SERVICES:
1177             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1178             if (!gatt_helper) break;
1179             gatt_client_discover_primary_services(&handle_gatt_client_event, gatt_helper->con_handle);
1180             break;
1181         case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16:
1182             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1183             if (!gatt_helper) break;
1184             gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, gatt_helper->con_handle, little_endian_read_16(packet, 5));
1185             break;
1186         case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128:
1187             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1188             if (!gatt_helper) break;
1189             reverse_128(&packet[5], uuid128);
1190             gatt_client_discover_primary_services_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, uuid128);
1191             break;
1192         case GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE:
1193             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1194             if (!gatt_helper) break;
1195             gatt_client_deserialize_service(packet, 5, &service);
1196             gatt_client_find_included_services_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
1197             break;
1198 
1199         case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE:
1200             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1201             if (!gatt_helper) break;
1202             gatt_client_deserialize_service(packet, 5, &service);
1203             gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
1204             break;
1205         case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128:
1206             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1207             if (!gatt_helper) break;
1208             gatt_client_deserialize_service(packet, 5, &service);
1209             reverse_128(&packet[5 + SERVICE_LENGTH], uuid128);
1210             gatt_client_discover_characteristics_for_service_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, &service, uuid128);
1211             break;
1212         case GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
1213             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1214             if (!gatt_helper) break;
1215             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1216             gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
1217             break;
1218 
1219         case GATT_READ_VALUE_OF_CHARACTERISTIC:
1220             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1221             if (!gatt_helper) break;
1222             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1223             gatt_client_read_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
1224             break;
1225         case GATT_READ_LONG_VALUE_OF_CHARACTERISTIC:
1226             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1227             if (!gatt_helper) break;
1228             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1229             gatt_client_read_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
1230             break;
1231 
1232         case GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
1233             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 0);  // note: don't track active connection
1234             if (!gatt_helper) break;
1235             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1236             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
1237             data = gatt_helper->characteristic_buffer;
1238             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
1239             gatt_client_write_value_of_characteristic_without_response(gatt_helper->con_handle, characteristic.value_handle, data_length, data);
1240             break;
1241         case GATT_WRITE_VALUE_OF_CHARACTERISTIC:
1242             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1243             if (!gatt_helper) break;
1244             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1245             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
1246             data = gatt_helper->characteristic_buffer;
1247             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
1248             gatt_client_write_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
1249             break;
1250         case GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
1251             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1252             if (!gatt_helper) break;
1253             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1254             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
1255             data = gatt_helper->characteristic_buffer;
1256             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
1257             gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
1258             break;
1259         case GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
1260             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1261             if (!gatt_helper) break;
1262             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1263             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
1264             data = gatt_helper->characteristic_buffer;
1265             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
1266             gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
1267             break;
1268         case GATT_READ_CHARACTERISTIC_DESCRIPTOR:
1269             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1270             if (!gatt_helper) break;
1271             handle = little_endian_read_16(packet, 3);
1272             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
1273             gatt_client_read_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
1274             break;
1275         case GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR:
1276             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1277             if (!gatt_helper) break;
1278             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
1279             gatt_client_read_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
1280             break;
1281         case GATT_WRITE_CHARACTERISTIC_DESCRIPTOR:
1282             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1283             if (!gatt_helper) break;
1284             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
1285             data = gatt_helper->characteristic_buffer;
1286             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
1287             gatt_client_write_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
1288             break;
1289         case GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR:
1290             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1291             if (!gatt_helper) break;
1292             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
1293             data = gatt_helper->characteristic_buffer;
1294             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
1295             gatt_client_write_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
1296             break;
1297         case GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:{
1298             uint16_t configuration = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
1299             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
1300             if (!gatt_helper) break;
1301             data = gatt_helper->characteristic_buffer;
1302             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
1303             status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic, configuration);
1304             if (status){
1305                 send_gatt_query_complete(connection, gatt_helper->con_handle, status);
1306             }
1307             break;
1308         }
1309         case GATT_GET_MTU:
1310             handle = little_endian_read_16(packet, 3);
1311             gatt_client_get_mtu(handle, &mtu);
1312             send_gatt_mtu_event(connection, handle, mtu);
1313             break;
1314 #endif
1315 #ifdef ENABLE_BLE
1316         case SM_SET_AUTHENTICATION_REQUIREMENTS:
1317             log_info("set auth %x", packet[3]);
1318             sm_set_authentication_requirements(packet[3]);
1319             break;
1320         case SM_SET_IO_CAPABILITIES:
1321             log_info("set io %x", packet[3]);
1322             sm_set_io_capabilities(packet[3]);
1323             break;
1324         case SM_BONDING_DECLINE:
1325             sm_bonding_decline(little_endian_read_16(packet, 3));
1326             break;
1327         case SM_JUST_WORKS_CONFIRM:
1328             sm_just_works_confirm(little_endian_read_16(packet, 3));
1329             break;
1330         case SM_NUMERIC_COMPARISON_CONFIRM:
1331             sm_numeric_comparison_confirm(little_endian_read_16(packet, 3));
1332             break;
1333         case SM_PASSKEY_INPUT:
1334             sm_passkey_input(little_endian_read_16(packet, 3), little_endian_read_32(packet, 5));
1335             break;
1336 #endif
1337     default:
1338             log_error("Error: command %u not implemented:", READ_CMD_OCF(packet));
1339             break;
1340     }
1341 
1342     return 0;
1343 }
1344 
1345 static int daemon_client_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length){
1346 
1347     int err = 0;
1348     client_state_t * client;
1349 
1350     switch (packet_type){
1351         case HCI_COMMAND_DATA_PACKET:
1352             if (READ_CMD_OGF(data) != OGF_BTSTACK) {
1353                 // HCI Command
1354                 hci_send_cmd_packet(data, length);
1355             } else {
1356                 // BTstack command
1357                 btstack_command_handler(connection, data, length);
1358             }
1359             break;
1360         case L2CAP_DATA_PACKET:
1361             // process l2cap packet...
1362             err = l2cap_send(channel, data, length);
1363             switch (err){
1364                 case BTSTACK_ACL_BUFFERS_FULL:
1365                     // temp flow issue
1366                     l2cap_request_can_send_now_event(channel);
1367                     break;
1368                 default:
1369                     // sending failed
1370                     err = ERROR_CODE_SUCCESS;
1371                     log_error("Dropping L2CAP packet");
1372                     break;
1373             }
1374             break;
1375         case RFCOMM_DATA_PACKET:
1376             // process rfcomm packet...
1377             err = rfcomm_send(channel, data, length);
1378             switch (err){
1379                 case BTSTACK_ACL_BUFFERS_FULL:
1380                 case RFCOMM_NO_OUTGOING_CREDITS:
1381                 case RFCOMM_AGGREGATE_FLOW_OFF:
1382                     // flow issue
1383                     rfcomm_request_can_send_now_event(channel);
1384                     break;
1385                 default:
1386                     // sending failed
1387                     err = ERROR_CODE_SUCCESS;
1388                     log_error("Dropping RFCOMM packet");
1389                     break;
1390             }
1391             break;
1392         case DAEMON_EVENT_PACKET:
1393             switch (data[0]) {
1394                 case DAEMON_EVENT_CONNECTION_OPENED:
1395                     log_info("DAEMON_EVENT_CONNECTION_OPENED %p\n",connection);
1396 
1397                     client = calloc(sizeof(client_state_t), 1);
1398                     if (!client) break; // fail
1399                     client->connection   = connection;
1400                     client->power_mode   = HCI_POWER_OFF;
1401                     client->discoverable = 0;
1402                     btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
1403                     break;
1404                 case DAEMON_EVENT_CONNECTION_CLOSED:
1405                     log_info("DAEMON_EVENT_CONNECTION_CLOSED %p\n",connection);
1406                     daemon_disconnect_client(connection);
1407                     // no clients -> no HCI connections
1408                     if (!clients){
1409                         hci_disconnect_all();
1410                     }
1411 
1412                     // update discoverable mode
1413                     gap_discoverable_control(clients_require_discoverable());
1414                     // start power off, if last active client
1415                     if (!clients_require_power_on()){
1416                         start_power_off_timer();
1417                     }
1418                     break;
1419                 default:
1420                     break;
1421             }
1422             break;
1423     }
1424     if (err) {
1425         log_info("Daemon Handler: err %d\n", err);
1426     }
1427     return err;
1428 }
1429 
1430 
1431 static void daemon_set_logging_enabled(int enabled){
1432     if (enabled && !loggingEnabled){
1433         // construct path to log file
1434         const hci_dump_t * hci_dump_impl;
1435         switch (BTSTACK_LOG_TYPE){
1436             case HCI_DUMP_PACKETLOGGER:
1437                 hci_dump_impl = hci_dump_posix_fs_get_instance();
1438                 snprintf(string_buffer, sizeof(string_buffer), "%s/hci_dump.pklg", btstack_server_storage_path);
1439                 hci_dump_posix_fs_open(string_buffer, HCI_DUMP_PACKETLOGGER);
1440                 break;
1441             case HCI_DUMP_BLUEZ:
1442                 hci_dump_impl = hci_dump_posix_fs_get_instance();
1443                 snprintf(string_buffer, sizeof(string_buffer), "%s/hci_dump.snoop", btstack_server_storage_path);
1444                 hci_dump_posix_fs_open(string_buffer, HCI_DUMP_BLUEZ);
1445                 break;
1446             default:
1447                 break;
1448         }
1449         hci_dump_init(hci_dump_impl);
1450         printf("Logging to %s\n", string_buffer);
1451     }
1452     if (!enabled && loggingEnabled){
1453         hci_dump_posix_fs_close();
1454         hci_dump_init(NULL);
1455     }
1456     loggingEnabled = enabled;
1457 }
1458 
1459 // local cache used to manage UI status
1460 static HCI_STATE hci_state = HCI_STATE_OFF;
1461 static int num_connections = 0;
1462 static void update_ui_status(void){
1463     if (hci_state != HCI_STATE_WORKING) {
1464         bluetooth_status_handler(BLUETOOTH_OFF);
1465     } else {
1466         if (num_connections) {
1467             bluetooth_status_handler(BLUETOOTH_ACTIVE);
1468         } else {
1469             bluetooth_status_handler(BLUETOOTH_ON);
1470         }
1471     }
1472 }
1473 
1474 #ifdef USE_SPRINGBOARD
1475 static void preferences_changed_callback(void){
1476     int logging = platform_iphone_logging_enabled();
1477     log_info("Logging enabled: %u\n", logging);
1478     daemon_set_logging_enabled(logging);
1479 }
1480 #endif
1481 
1482 static void deamon_status_event_handler(uint8_t *packet, uint16_t size){
1483 
1484     uint8_t update_status = 0;
1485 
1486     // handle state event
1487     switch (hci_event_packet_get_type(packet)) {
1488         case BTSTACK_EVENT_STATE:
1489             hci_state = packet[2];
1490             log_info("New state: %u\n", hci_state);
1491             update_status = 1;
1492             break;
1493         case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
1494             num_connections = packet[2];
1495             log_info("New nr connections: %u\n", num_connections);
1496             update_status = 1;
1497             break;
1498         default:
1499             break;
1500     }
1501 
1502     // choose full bluetooth state
1503     if (update_status) {
1504         update_ui_status();
1505     }
1506 }
1507 
1508 static void daemon_retry_parked(void){
1509 
1510     // socket_connection_retry_parked is not reentrant
1511     static int retry_mutex = 0;
1512 
1513     // lock mutex
1514     if (retry_mutex) return;
1515     retry_mutex = 1;
1516 
1517     // ... try sending again
1518     socket_connection_retry_parked();
1519 
1520     // unlock mutex
1521     retry_mutex = 0;
1522 }
1523 
1524 static void daemon_emit_packet(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1525     if (connection) {
1526         socket_connection_send_packet(connection, packet_type, channel, packet, size);
1527     } else {
1528         socket_connection_send_packet_all(packet_type, channel, packet, size);
1529     }
1530 }
1531 
1532 char * bd_addr_to_str_dashed(const bd_addr_t addr){
1533     return bd_addr_to_str_with_delimiter(addr, '-');
1534 }
1535 
1536 static uint8_t remote_name_event[2+1+6+DEVICE_NAME_LEN+1]; // +1 for \0 in log_info
1537 static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1538     uint16_t cid;
1539     int i;
1540     bd_addr_t addr;
1541     switch (packet_type) {
1542         case HCI_EVENT_PACKET:
1543             deamon_status_event_handler(packet, size);
1544             switch (hci_event_packet_get_type(packet)){
1545 
1546                 case BTSTACK_EVENT_STATE:
1547                     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
1548                     if (tlv_setup_done) break;
1549 
1550                     // setup TLV using local address as part of the name
1551                     gap_local_bd_addr(addr);
1552                     log_info("BTstack up and running at %s",  bd_addr_to_str(addr));
1553                     snprintf(string_buffer, sizeof(string_buffer), "%s/btstack_%s.tlv", btstack_server_storage_path, bd_addr_to_str_dashed(addr));
1554                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, string_buffer);
1555                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
1556 
1557                     // setup link key db
1558                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
1559 
1560                     // init le device db to use TLV
1561                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
1562                     le_device_db_init();
1563                     le_device_db_set_local_bd_addr(addr);
1564 
1565                     tlv_setup_done = 1;
1566                     break;
1567 
1568                 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
1569                     // ACL buffer freed...
1570                     daemon_retry_parked();
1571                     // no need to tell clients
1572                     return;
1573 
1574                 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
1575                     if (!btstack_device_name_db) break;
1576                     if (packet[2]) break; // status not ok
1577 
1578                     reverse_bd_addr(&packet[3], addr);
1579                     // fix for invalid remote names - terminate on 0xff
1580                     for (i=0; i<248;i++){
1581                         if (packet[9+i] == 0xff){
1582                             packet[9+i] = 0;
1583                             break;
1584                         }
1585                     }
1586                     packet[9+248] = 0;
1587                     btstack_device_name_db->put_name(addr, (device_name_t *)&packet[9]);
1588                     break;
1589 
1590                 case HCI_EVENT_INQUIRY_RESULT:
1591                 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
1592                     if (!btstack_device_name_db) break;
1593 
1594                     // first send inq result packet
1595                     daemon_emit_packet(connection, packet_type, channel, packet, size);
1596 
1597                     // then send cached remote names
1598                     int offset = 3;
1599                     for (i=0; i<packet[2];i++){
1600                         reverse_bd_addr(&packet[offset], addr);
1601                         if (btstack_device_name_db->get_name(addr, (device_name_t *) &remote_name_event[9])){
1602                             remote_name_event[0] = DAEMON_EVENT_REMOTE_NAME_CACHED;
1603                             remote_name_event[1] = sizeof(remote_name_event) - 2 - 1;
1604                             remote_name_event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
1605                             reverse_bd_addr(addr, &remote_name_event[3]);
1606 
1607                             remote_name_event[9+248] = 0;   // assert \0 for log_info
1608                             log_info("DAEMON_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(addr), &remote_name_event[9]);
1609                             hci_dump_packet(HCI_EVENT_PACKET, 0, remote_name_event, sizeof(remote_name_event)-1);
1610                             daemon_emit_packet(connection, HCI_EVENT_PACKET, channel, remote_name_event, sizeof(remote_name_event) -1);
1611                         }
1612                         offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
1613                     }
1614                     return;
1615                 }
1616 
1617                 case RFCOMM_EVENT_CHANNEL_OPENED:
1618                     cid = little_endian_read_16(packet, 13);
1619                     connection = connection_for_rfcomm_cid(cid);
1620                     if (!connection) break;
1621                     if (packet[2]) {
1622                         daemon_remove_client_rfcomm_channel(connection, cid);
1623                     } else {
1624                         daemon_add_client_rfcomm_channel(connection, cid);
1625                     }
1626                     break;
1627                 case RFCOMM_EVENT_CAN_SEND_NOW:
1628                     daemon_retry_parked();
1629                     break;
1630                 case RFCOMM_EVENT_CHANNEL_CLOSED:
1631                     cid = little_endian_read_16(packet, 2);
1632                     connection = connection_for_rfcomm_cid(cid);
1633                     if (!connection) break;
1634                     daemon_remove_client_rfcomm_channel(connection, cid);
1635                     break;
1636                 case DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED:
1637                     if (packet[2]) break;
1638                     daemon_add_client_rfcomm_service(connection, packet[3]);
1639                     break;
1640                 case L2CAP_EVENT_CHANNEL_OPENED:
1641                     cid = little_endian_read_16(packet, 13);
1642                     connection = connection_for_l2cap_cid(cid);
1643                     if (!connection) break;
1644                     if (packet[2]) {
1645                         daemon_remove_client_l2cap_channel(connection, cid);
1646                     } else {
1647                         daemon_add_client_l2cap_channel(connection, cid);
1648                     }
1649                     break;
1650                 case L2CAP_EVENT_CAN_SEND_NOW:
1651                     daemon_retry_parked();
1652                     break;
1653                 case L2CAP_EVENT_CHANNEL_CLOSED:
1654                     cid = little_endian_read_16(packet, 2);
1655                     connection = connection_for_l2cap_cid(cid);
1656                     if (!connection) break;
1657                     daemon_remove_client_l2cap_channel(connection, cid);
1658                     break;
1659 #if defined(ENABLE_BLE) && defined(HAVE_MALLOC)
1660                 case HCI_EVENT_DISCONNECTION_COMPLETE:
1661                     daemon_remove_gatt_client_helper(little_endian_read_16(packet, 3));
1662                     break;
1663 #endif
1664                 default:
1665                     break;
1666             }
1667             break;
1668         case L2CAP_DATA_PACKET:
1669             connection = connection_for_l2cap_cid(channel);
1670             if (!connection) return;
1671             break;
1672         case RFCOMM_DATA_PACKET:
1673             connection = connection_for_rfcomm_cid(channel);
1674             if (!connection) return;
1675             break;
1676         default:
1677             break;
1678     }
1679 
1680     daemon_emit_packet(connection, packet_type, channel, packet, size);
1681 }
1682 
1683 static void stack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
1684     daemon_packet_handler(NULL, packet_type, channel, packet, size);
1685 }
1686 
1687 static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1688     switch (hci_event_packet_get_type(packet)){
1689         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
1690         case SDP_EVENT_QUERY_COMPLETE:
1691             // already HCI Events, just forward them
1692             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
1693             socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, size);
1694             break;
1695         default:
1696             break;
1697     }
1698 }
1699 
1700 static void sdp_client_assert_buffer(int size){
1701     if (size > attribute_value_buffer_size){
1702         log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
1703     }
1704 }
1705 
1706 // define new packet type SDP_CLIENT_PACKET
1707 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1708     int event_len;
1709 
1710     switch (hci_event_packet_get_type(packet)){
1711         case SDP_EVENT_QUERY_ATTRIBUTE_BYTE:
1712             sdp_client_assert_buffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
1713             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
1714             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
1715                 log_info_hexdump(attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet));
1716 
1717                 int event_len = 1 + 3 * 2 + sdp_event_query_attribute_byte_get_attribute_length(packet);
1718                 uint8_t event[event_len];
1719                 event[0] = SDP_EVENT_QUERY_ATTRIBUTE_VALUE;
1720                 little_endian_store_16(event, 1, sdp_event_query_attribute_byte_get_record_id(packet));
1721                 little_endian_store_16(event, 3, sdp_event_query_attribute_byte_get_attribute_id(packet));
1722                 little_endian_store_16(event, 5, (uint16_t)sdp_event_query_attribute_byte_get_attribute_length(packet));
1723                 memcpy(&event[7], attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet));
1724                 hci_dump_packet(SDP_CLIENT_PACKET, 0, event, event_len);
1725                 socket_connection_send_packet(sdp_client_query_connection, SDP_CLIENT_PACKET, 0, event, event_len);
1726             }
1727             break;
1728         case SDP_EVENT_QUERY_COMPLETE:
1729             event_len = packet[1] + 2;
1730             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, event_len);
1731             socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, event_len);
1732             break;
1733     }
1734 }
1735 
1736 static void power_notification_callback(POWER_NOTIFICATION_t notification){
1737     switch (notification) {
1738         case POWER_WILL_SLEEP:
1739             // let's sleep
1740             power_management_sleep = 1;
1741             hci_power_control(HCI_POWER_SLEEP);
1742             break;
1743         case POWER_WILL_WAKE_UP:
1744             // assume that all clients use Bluetooth -> if connection, start Bluetooth
1745             power_management_sleep = 0;
1746             if (clients_require_power_on()) {
1747                 hci_power_control(HCI_POWER_ON);
1748             }
1749             break;
1750         default:
1751             break;
1752     }
1753 }
1754 
1755 static void daemon_sigint_handler(int param){
1756 
1757     log_info(" <= SIGINT received, shutting down..\n");
1758 
1759     int send_power_off = 1;
1760 #ifdef HAVE_INTEL_USB
1761     // power off and close only if hci was initialized before
1762     send_power_off = intel_firmware_loaded;
1763 #endif
1764 
1765     if (send_power_off){
1766         hci_power_control( HCI_POWER_OFF);
1767         hci_close();
1768     }
1769 
1770     log_info("Good bye, see you.\n");
1771 
1772     exit(0);
1773 }
1774 
1775 // MARK: manage power off timer
1776 
1777 #define USE_POWER_OFF_TIMER
1778 
1779 static void stop_power_off_timer(void){
1780 #ifdef USE_POWER_OFF_TIMER
1781     if (timeout_active) {
1782         btstack_run_loop_remove_timer(&timeout);
1783         timeout_active = 0;
1784     }
1785 #endif
1786 }
1787 
1788 static void start_power_off_timer(void){
1789 #ifdef USE_POWER_OFF_TIMER
1790     stop_power_off_timer();
1791     btstack_run_loop_set_timer(&timeout, DAEMON_NO_ACTIVE_CLIENT_TIMEOUT);
1792     btstack_run_loop_add_timer(&timeout);
1793     timeout_active = 1;
1794 #else
1795     hci_power_control(HCI_POWER_OFF);
1796 #endif
1797 }
1798 
1799 // MARK: manage list of clients
1800 
1801 
1802 static client_state_t * client_for_connection(connection_t *connection) {
1803     btstack_linked_item_t *it;
1804     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
1805         client_state_t * client_state = (client_state_t *) it;
1806         if (client_state->connection == connection) {
1807             return client_state;
1808         }
1809     }
1810     return NULL;
1811 }
1812 
1813 static void clients_clear_power_request(void){
1814     btstack_linked_item_t *it;
1815     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
1816         client_state_t * client_state = (client_state_t *) it;
1817         client_state->power_mode = HCI_POWER_OFF;
1818     }
1819 }
1820 
1821 static int clients_require_power_on(void){
1822 
1823     if (global_enable) return 1;
1824 
1825     btstack_linked_item_t *it;
1826     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
1827         client_state_t * client_state = (client_state_t *) it;
1828         if (client_state->power_mode == HCI_POWER_ON) {
1829             return 1;
1830         }
1831     }
1832     return 0;
1833 }
1834 
1835 static int clients_require_discoverable(void){
1836     btstack_linked_item_t *it;
1837     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
1838         client_state_t * client_state = (client_state_t *) it;
1839         if (client_state->discoverable) {
1840             return 1;
1841         }
1842     }
1843     return 0;
1844 }
1845 
1846 static void usage(const char * name) {
1847     printf("%s, BTstack background daemon\n", name);
1848     printf("usage: %s [--help] [--tcp]\n", name);
1849     printf("    --help   display this usage\n");
1850     printf("    --tcp    use TCP server on port %u\n", BTSTACK_PORT);
1851     printf("Without the --tcp option, BTstack Server is listening on unix domain socket %s\n\n", BTSTACK_UNIX);
1852 }
1853 
1854 #ifdef ENABLE_BLE
1855 
1856 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
1857 
1858     // only handle GATT Events
1859     switch(hci_event_packet_get_type(packet)){
1860         case GATT_EVENT_SERVICE_QUERY_RESULT:
1861         case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:
1862         case GATT_EVENT_NOTIFICATION:
1863         case GATT_EVENT_INDICATION:
1864         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
1865         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1866         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
1867         case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
1868         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
1869         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
1870         case GATT_EVENT_QUERY_COMPLETE:
1871            break;
1872         default:
1873             return;
1874     }
1875 
1876     hci_con_handle_t con_handle = little_endian_read_16(packet, 2);
1877     btstack_linked_list_gatt_client_helper_t * gatt_client_helper = daemon_get_gatt_client_helper(con_handle);
1878     if (!gatt_client_helper){
1879         log_info("daemon handle_gatt_client_event: gc helper for handle 0x%2x is NULL.", con_handle);
1880         return;
1881     }
1882 
1883     connection_t *connection = NULL;
1884 
1885     // daemon doesn't track which connection subscribed to this particular handle, so we just notify all connections
1886     switch(hci_event_packet_get_type(packet)){
1887         case GATT_EVENT_NOTIFICATION:
1888         case GATT_EVENT_INDICATION:{
1889             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
1890 
1891             btstack_linked_item_t *it;
1892             for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
1893                 client_state_t * client_state = (client_state_t *) it;
1894                 socket_connection_send_packet(client_state->connection, HCI_EVENT_PACKET, 0, packet, size);
1895             }
1896             return;
1897         }
1898         default:
1899             break;
1900     }
1901 
1902     // otherwise, we have to have an active connection
1903     connection = gatt_client_helper->active_connection;
1904     uint16_t offset;
1905     uint16_t length;
1906 
1907     if (!connection) return;
1908 
1909     switch(hci_event_packet_get_type(packet)){
1910 
1911         case GATT_EVENT_SERVICE_QUERY_RESULT:
1912         case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:
1913         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
1914         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
1915         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
1916         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1917             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
1918             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size);
1919             break;
1920 
1921         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
1922         case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
1923             offset = little_endian_read_16(packet, 6);
1924             length = little_endian_read_16(packet, 8);
1925             gatt_client_helper->characteristic_buffer[0] = hci_event_packet_get_type(packet);  // store type (characteristic/descriptor)
1926             gatt_client_helper->characteristic_handle    = little_endian_read_16(packet, 4);   // store attribute handle
1927             gatt_client_helper->characteristic_length = offset + length;            // update length
1928             memcpy(&gatt_client_helper->characteristic_buffer[10 + offset], &packet[10], length);
1929             break;
1930 
1931         case GATT_EVENT_QUERY_COMPLETE:{
1932             gatt_client_helper->active_connection = NULL;
1933             if (gatt_client_helper->characteristic_length){
1934                 // send re-combined long characteristic value or long characteristic descriptor value
1935                 uint8_t * event = gatt_client_helper->characteristic_buffer;
1936                 uint16_t event_size = 10 + gatt_client_helper->characteristic_length;
1937                 // event[0] == already set by previsous case
1938                 event[1] = 8 + gatt_client_helper->characteristic_length;
1939                 little_endian_store_16(event, 2, little_endian_read_16(packet, 2));
1940                 little_endian_store_16(event, 4, gatt_client_helper->characteristic_handle);
1941                 little_endian_store_16(event, 6, 0);   // offset
1942                 little_endian_store_16(event, 8, gatt_client_helper->characteristic_length);
1943                 hci_dump_packet(HCI_EVENT_PACKET, 0, event, event_size);
1944                 socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, event_size);
1945                 gatt_client_helper->characteristic_length = 0;
1946             }
1947             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
1948             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size);
1949             break;
1950         }
1951         default:
1952             break;
1953     }
1954 }
1955 #endif
1956 
1957 static char hostname[30];
1958 
1959 static void btstack_server_configure_stack(void){
1960     // init HCI
1961     hci_init(transport, config);
1962     if (btstack_link_key_db){
1963         hci_set_link_key_db(btstack_link_key_db);
1964     }
1965     if (control){
1966         hci_set_control(control);
1967     }
1968 
1969     // hostname for POSIX systems
1970     gethostname(hostname, 30);
1971     hostname[29] = '\0';
1972     gap_set_local_name(hostname);
1973 
1974     // enabled EIR
1975     hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
1976 
1977     // register for HCI events
1978     hci_event_callback_registration.callback = &stack_packet_handler;
1979     hci_add_event_handler(&hci_event_callback_registration);
1980 
1981     // init L2CAP
1982     l2cap_init();
1983     l2cap_event_callback_registration.callback = &stack_packet_handler;
1984     l2cap_add_event_handler(&l2cap_event_callback_registration);
1985     timeout.process = daemon_no_connections_timeout;
1986 
1987 #ifdef ENABLE_RFCOMM
1988     log_info("config.h: ENABLE_RFCOMM\n");
1989     rfcomm_init();
1990 #endif
1991 
1992 #ifdef ENABLE_SDP
1993     sdp_init();
1994 #endif
1995 
1996 #ifdef ENABLE_BLE
1997     sm_init();
1998     sm_event_callback_registration.callback = &stack_packet_handler;
1999     sm_add_event_handler(&sm_event_callback_registration);
2000     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
2001     // sm_set_authentication_requirements( SM_AUTHREQ_BONDING | SM_AUTHREQ_MITM_PROTECTION);
2002 
2003     // GATT Client
2004     gatt_client_init();
2005     gatt_client_listen_for_characteristic_value_updates(&daemon_gatt_client_notifications, &handle_gatt_client_event, GATT_CLIENT_ANY_CONNECTION, GATT_CLIENT_ANY_VALUE_HANDLE);
2006 
2007     // GATT Server - empty attribute database
2008     att_server_init(NULL, NULL, NULL);
2009 
2010 #endif
2011 }
2012 
2013 int btstack_server_run(int tcp_flag){
2014 
2015     if (tcp_flag){
2016         printf("BTstack Server started on port %u\n", BTSTACK_PORT);
2017     } else {
2018         printf("BTstack Server started on socket %s\n", BTSTACK_UNIX);
2019     }
2020 
2021     // handle default init
2022     if (!btstack_server_storage_path){
2023 #ifdef _WIN32
2024         btstack_server_storage_path = strdup(".");
2025 #else
2026         btstack_server_storage_path = strdup("/tmp");
2027 #endif
2028     }
2029 
2030     // make stdout unbuffered
2031     setbuf(stdout, NULL);
2032 
2033     // handle CTRL-c
2034     signal(SIGINT, daemon_sigint_handler);
2035     // handle SIGTERM - suggested for launchd
2036     signal(SIGTERM, daemon_sigint_handler);
2037 
2038     socket_connection_init();
2039 
2040     btstack_control_t * control = NULL;
2041     const btstack_uart_t *       uart_implementation = NULL;
2042     (void) uart_implementation;
2043 
2044 #ifdef HAVE_TRANSPORT_H4
2045     hci_transport_config_uart.type = HCI_TRANSPORT_CONFIG_UART;
2046     hci_transport_config_uart.baudrate_init = UART_SPEED;
2047     hci_transport_config_uart.baudrate_main = 0;
2048     hci_transport_config_uart.flowcontrol = 1;
2049     hci_transport_config_uart.device_name   = UART_DEVICE;
2050 
2051 #ifdef _WIN32
2052     uart_implementation = (const btstack_uart_t *) btstack_uart_block_windows_instance();
2053 #else
2054     uart_implementation = btstack_uart_posix_instance();
2055 #endif
2056 
2057     config = &hci_transport_config_uart;
2058     transport = hci_transport_h4_instance_for_uart(uart_implementation);
2059 #endif
2060 
2061 #ifdef HAVE_TRANSPORT_USB
2062     transport = hci_transport_usb_instance();
2063 #endif
2064 
2065 #ifdef BTSTACK_DEVICE_NAME_DB_INSTANCE
2066     btstack_device_name_db = BTSTACK_DEVICE_NAME_DB_INSTANCE();
2067 #endif
2068 
2069 #ifdef _WIN32
2070     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
2071 #else
2072     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2073 #endif
2074 
2075     // init power management notifications
2076     if (control && control->register_for_power_notifications){
2077         control->register_for_power_notifications(power_notification_callback);
2078     }
2079 
2080     // logging
2081     loggingEnabled = 0;
2082     int newLoggingEnabled = 1;
2083     daemon_set_logging_enabled(newLoggingEnabled);
2084 
2085     // dump version
2086     log_info("BTStack Server started\n");
2087     log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE);
2088 
2089 #ifndef HAVE_INTEL_USB
2090     btstack_server_configure_stack();
2091 #endif
2092 
2093 #ifdef USE_LAUNCHD
2094     socket_connection_create_launchd();
2095 #else
2096     // create server
2097     if (tcp_flag) {
2098         socket_connection_create_tcp(BTSTACK_PORT);
2099     } else {
2100 #ifdef HAVE_UNIX_SOCKETS
2101         socket_connection_create_unix(BTSTACK_UNIX);
2102 #endif
2103     }
2104 #endif
2105     socket_connection_register_packet_callback(&daemon_client_handler);
2106 
2107     // go!
2108     btstack_run_loop_execute();
2109     return 0;
2110 }
2111 
2112 int btstack_server_run_tcp(void){
2113      return btstack_server_run(1);
2114 }
2115 
2116 int main (int argc,  char * const * argv){
2117 
2118     int tcp_flag = 0;
2119     struct option long_options[] = {
2120         { "tcp", no_argument, &tcp_flag, 1 },
2121         { "help", no_argument, 0, 0 },
2122         { 0,0,0,0 } // This is a filler for -1
2123     };
2124 
2125     while (true) {
2126         int c;
2127         int option_index = -1;
2128         c = getopt_long(argc, argv, "h", long_options, &option_index);
2129         if (c == -1) break; // no more option
2130 
2131         // treat long parameter first
2132         if (option_index == -1) {
2133             switch (c) {
2134                 case '?':
2135                 case 'h':
2136                     usage(argv[0]);
2137                     return 0;
2138                     break;
2139             }
2140         } else {
2141             switch (option_index) {
2142                 case 1:
2143                     usage(argv[0]);
2144                     return 0;
2145                     break;
2146             }
2147         }
2148     }
2149 
2150 #ifndef HAVE_UNIX_SOCKETS
2151     // TCP is default if there are no unix sockets
2152     tcp_flag = 1;
2153 #endif
2154 
2155     btstack_server_run(tcp_flag);
2156 
2157     return 0;
2158 }
2159 
2160 void btstack_server_set_storage_path(const char * path){
2161     if (btstack_server_storage_path){
2162         free((void*)btstack_server_storage_path);
2163         btstack_server_storage_path = NULL;
2164     }
2165     btstack_server_storage_path = strdup(path);
2166     log_info("Storage path %s", btstack_server_storage_path);
2167 }
2168