12531c97eSMatthias Ringwald /* 22531c97eSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 32531c97eSMatthias Ringwald * 42531c97eSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 52531c97eSMatthias Ringwald * modification, are permitted provided that the following conditions 62531c97eSMatthias Ringwald * are met: 72531c97eSMatthias Ringwald * 82531c97eSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 92531c97eSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 102531c97eSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 112531c97eSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 122531c97eSMatthias Ringwald * documentation and/or other materials provided with the distribution. 132531c97eSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 142531c97eSMatthias Ringwald * contributors may be used to endorse or promote products derived 152531c97eSMatthias Ringwald * from this software without specific prior written permission. 162531c97eSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 172531c97eSMatthias Ringwald * personal benefit and not for any commercial purpose or for 182531c97eSMatthias Ringwald * monetary gain. 192531c97eSMatthias Ringwald * 202531c97eSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 212531c97eSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 222531c97eSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232531c97eSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 242531c97eSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 252531c97eSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 262531c97eSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 272531c97eSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 282531c97eSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 292531c97eSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 302531c97eSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312531c97eSMatthias Ringwald * SUCH DAMAGE. 322531c97eSMatthias Ringwald * 332531c97eSMatthias Ringwald * Please inquire about commercial licensing options at 342531c97eSMatthias Ringwald * [email protected] 352531c97eSMatthias Ringwald * 362531c97eSMatthias Ringwald */ 372531c97eSMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "daemon.c" 39ab2c6ae4SMatthias Ringwald 402531c97eSMatthias Ringwald /* 412531c97eSMatthias Ringwald * daemon.c 422531c97eSMatthias Ringwald * 432531c97eSMatthias Ringwald * Created by Matthias Ringwald on 7/1/09. 442531c97eSMatthias Ringwald * 452531c97eSMatthias Ringwald * BTstack background daemon 462531c97eSMatthias Ringwald * 472531c97eSMatthias Ringwald */ 482531c97eSMatthias Ringwald 492531c97eSMatthias Ringwald #include "btstack_config.h" 502531c97eSMatthias Ringwald 512531c97eSMatthias Ringwald #include <pthread.h> 522531c97eSMatthias Ringwald #include <signal.h> 532531c97eSMatthias Ringwald #include <stdio.h> 542531c97eSMatthias Ringwald #include <stdlib.h> 552531c97eSMatthias Ringwald #include <strings.h> 562531c97eSMatthias Ringwald #include <unistd.h> 572531c97eSMatthias Ringwald 58092bd57eSMatthias Ringwald #ifdef _WIN32 59092bd57eSMatthias Ringwald #include "Winsock2.h" 60092bd57eSMatthias Ringwald #endif 61092bd57eSMatthias Ringwald 62b9dcd1ccSMatthias Ringwald #include <getopt.h> 63b9dcd1ccSMatthias Ringwald 642531c97eSMatthias Ringwald #include "btstack.h" 652531c97eSMatthias Ringwald #include "btstack_client.h" 662531c97eSMatthias Ringwald #include "btstack_debug.h" 672531c97eSMatthias Ringwald #include "btstack_device_name_db.h" 682531c97eSMatthias Ringwald #include "btstack_event.h" 692531c97eSMatthias Ringwald #include "btstack_linked_list.h" 702531c97eSMatthias Ringwald #include "btstack_run_loop.h" 71a05b2457SMatthias Ringwald #include "btstack_tlv_posix.h" 722370d9c1SMatthias Ringwald #include "btstack_util.h" 73a05b2457SMatthias Ringwald 74ee252d54SMatthias Ringwald #include "btstack_server.h" 75b9dcd1ccSMatthias Ringwald 76b9dcd1ccSMatthias Ringwald #ifdef _WIN32 77b9dcd1ccSMatthias Ringwald #include "btstack_run_loop_windows.h" 78b9dcd1ccSMatthias Ringwald #else 792531c97eSMatthias Ringwald #include "btstack_run_loop_posix.h" 80b9dcd1ccSMatthias Ringwald #endif 81e9b53984SMatthias Ringwald 822531c97eSMatthias Ringwald #include "btstack_version.h" 832531c97eSMatthias Ringwald #include "classic/btstack_link_key_db.h" 84a05b2457SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 852531c97eSMatthias Ringwald #include "classic/rfcomm.h" 862531c97eSMatthias Ringwald #include "classic/sdp_server.h" 872531c97eSMatthias Ringwald #include "classic/sdp_client.h" 88efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 892531c97eSMatthias Ringwald #include "hci.h" 902531c97eSMatthias Ringwald #include "hci_cmd.h" 912531c97eSMatthias Ringwald #include "hci_dump.h" 922531c97eSMatthias Ringwald #include "hci_transport.h" 932531c97eSMatthias Ringwald #include "l2cap.h" 942531c97eSMatthias Ringwald #include "rfcomm_service_db.h" 952531c97eSMatthias Ringwald #include "socket_connection.h" 962531c97eSMatthias Ringwald 978129e542SMatthias Ringwald #ifdef HAVE_INTEL_USB 988129e542SMatthias Ringwald #include "btstack_chipset_intel_firmware.h" 998129e542SMatthias Ringwald #endif 1008129e542SMatthias Ringwald 1012531c97eSMatthias Ringwald #ifdef ENABLE_BLE 1022531c97eSMatthias Ringwald #include "ble/gatt_client.h" 1032531c97eSMatthias Ringwald #include "ble/att_server.h" 1042531c97eSMatthias Ringwald #include "ble/att_db.h" 1052531c97eSMatthias Ringwald #include "ble/le_device_db.h" 106a05b2457SMatthias Ringwald #include "ble/le_device_db_tlv.h" 1072531c97eSMatthias Ringwald #include "ble/sm.h" 1082531c97eSMatthias Ringwald #endif 1092531c97eSMatthias Ringwald 1102531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 1112531c97eSMatthias Ringwald #include <CoreFoundation/CoreFoundation.h> 1122531c97eSMatthias Ringwald #include <notify.h> 1132531c97eSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 1142531c97eSMatthias Ringwald #include "../port/ios/src/platform_iphone.h" 1152531c97eSMatthias Ringwald // support for "enforece wake device" in h4 - used by iOS power management 1162531c97eSMatthias Ringwald extern void hci_transport_h4_iphone_set_enforce_wake_device(char *path); 1172531c97eSMatthias Ringwald #endif 1182531c97eSMatthias Ringwald 1192531c97eSMatthias Ringwald // copy of prototypes 120f8d88472SMatthias Ringwald const btstack_device_name_db_t * btstack_device_name_db_corefoundation_instance(void); 1212531c97eSMatthias Ringwald const btstack_device_name_db_t * btstack_device_name_db_fs_instance(void); 122f8d88472SMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db_corefoundation_instance(void); 1232531c97eSMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void); 1242531c97eSMatthias Ringwald 1252531c97eSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 1262531c97eSMatthias Ringwald #ifndef BTSTACK_LOG_TYPE 1272531c97eSMatthias Ringwald #define BTSTACK_LOG_TYPE HCI_DUMP_PACKETLOGGER 1282531c97eSMatthias Ringwald #endif 1292531c97eSMatthias Ringwald 1302531c97eSMatthias Ringwald #define DAEMON_NO_ACTIVE_CLIENT_TIMEOUT 10000 1312531c97eSMatthias Ringwald 1322531c97eSMatthias Ringwald #define ATT_MAX_LONG_ATTRIBUTE_SIZE 512 1332531c97eSMatthias Ringwald 1342531c97eSMatthias Ringwald 1352531c97eSMatthias Ringwald #define SERVICE_LENGTH 20 1362531c97eSMatthias Ringwald #define CHARACTERISTIC_LENGTH 24 1372531c97eSMatthias Ringwald #define CHARACTERISTIC_DESCRIPTOR_LENGTH 18 1382531c97eSMatthias Ringwald 1392531c97eSMatthias Ringwald // ATT_MTU - 1 1402531c97eSMatthias Ringwald #define ATT_MAX_ATTRIBUTE_SIZE 22 1412531c97eSMatthias Ringwald 1422531c97eSMatthias Ringwald // HCI CMD OGF/OCF 1432531c97eSMatthias Ringwald #define READ_CMD_OGF(buffer) (buffer[1] >> 2) 1442531c97eSMatthias Ringwald #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0]) 1452531c97eSMatthias Ringwald 1462531c97eSMatthias Ringwald typedef struct { 1472531c97eSMatthias Ringwald // linked list - assert: first field 1482531c97eSMatthias Ringwald btstack_linked_item_t item; 1492531c97eSMatthias Ringwald 1502531c97eSMatthias Ringwald // connection 1512531c97eSMatthias Ringwald connection_t * connection; 1522531c97eSMatthias Ringwald 1532531c97eSMatthias Ringwald btstack_linked_list_t rfcomm_cids; 1542531c97eSMatthias Ringwald btstack_linked_list_t rfcomm_services; 1552531c97eSMatthias Ringwald btstack_linked_list_t l2cap_cids; 1562531c97eSMatthias Ringwald btstack_linked_list_t l2cap_psms; 1572531c97eSMatthias Ringwald btstack_linked_list_t sdp_record_handles; 1582531c97eSMatthias Ringwald btstack_linked_list_t gatt_con_handles; 15924047dd6SMatthias Ringwald 1602531c97eSMatthias Ringwald // power mode 1612531c97eSMatthias Ringwald HCI_POWER_MODE power_mode; 1622531c97eSMatthias Ringwald 1632531c97eSMatthias Ringwald // discoverable 1642531c97eSMatthias Ringwald uint8_t discoverable; 1652531c97eSMatthias Ringwald 1662531c97eSMatthias Ringwald } client_state_t; 1672531c97eSMatthias Ringwald 1682531c97eSMatthias Ringwald typedef struct btstack_linked_list_uint32 { 1692531c97eSMatthias Ringwald btstack_linked_item_t item; 1702531c97eSMatthias Ringwald uint32_t value; 1712531c97eSMatthias Ringwald } btstack_linked_list_uint32_t; 1722531c97eSMatthias Ringwald 1732531c97eSMatthias Ringwald typedef struct btstack_linked_list_connection { 1742531c97eSMatthias Ringwald btstack_linked_item_t item; 1752531c97eSMatthias Ringwald connection_t * connection; 1762531c97eSMatthias Ringwald } btstack_linked_list_connection_t; 1772531c97eSMatthias Ringwald 1782531c97eSMatthias Ringwald typedef struct btstack_linked_list_gatt_client_helper{ 1792531c97eSMatthias Ringwald btstack_linked_item_t item; 180711e6c80SMatthias Ringwald hci_con_handle_t con_handle; 1812531c97eSMatthias Ringwald connection_t * active_connection; // the one that started the current query 1822531c97eSMatthias Ringwald btstack_linked_list_t all_connections; // list of all connections that ever used this helper 18324047dd6SMatthias Ringwald btstack_linked_list_t gatt_client_notifications; // could be in client_state_t as well 1842531c97eSMatthias Ringwald uint16_t characteristic_length; 1852531c97eSMatthias Ringwald uint16_t characteristic_handle; 1862531c97eSMatthias Ringwald uint8_t characteristic_buffer[10 + ATT_MAX_LONG_ATTRIBUTE_SIZE]; // header for sending event right away 1872531c97eSMatthias Ringwald uint8_t long_query_type; 1882531c97eSMatthias Ringwald } btstack_linked_list_gatt_client_helper_t; 1892531c97eSMatthias Ringwald 19024047dd6SMatthias Ringwald typedef struct { 19124047dd6SMatthias Ringwald btstack_linked_item_t item; 19224047dd6SMatthias Ringwald gatt_client_notification_t notification_listener; 19324047dd6SMatthias Ringwald } btstack_linked_list_gatt_client_notification_t; 19424047dd6SMatthias Ringwald 1952531c97eSMatthias Ringwald // MARK: prototypes 1962531c97eSMatthias Ringwald static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1972531c97eSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1982531c97eSMatthias Ringwald #ifdef ENABLE_BLE 1992531c97eSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size); 2002531c97eSMatthias Ringwald #endif 2012531c97eSMatthias Ringwald static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state); 2022531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection); 2032531c97eSMatthias Ringwald static int clients_require_power_on(void); 2042531c97eSMatthias Ringwald static int clients_require_discoverable(void); 2052531c97eSMatthias Ringwald static void clients_clear_power_request(void); 2062531c97eSMatthias Ringwald static void start_power_off_timer(void); 2072531c97eSMatthias Ringwald static void stop_power_off_timer(void); 2082531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection); 2092531c97eSMatthias Ringwald static void hci_emit_system_bluetooth_enabled(uint8_t enabled); 2101edc4fc7SMatthias Ringwald static void stack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size); 2118129e542SMatthias Ringwald static void btstack_server_configure_stack(void); 2122531c97eSMatthias Ringwald 2132531c97eSMatthias Ringwald // MARK: globals 2148c9f78e5SMatthias Ringwald 2158c9f78e5SMatthias Ringwald #ifdef HAVE_TRANSPORT_H4 2162531c97eSMatthias Ringwald static hci_transport_config_uart_t hci_transport_config_uart; 2178c9f78e5SMatthias Ringwald #endif 2188c9f78e5SMatthias Ringwald 2198129e542SMatthias Ringwald // used for stack configuration 2208c9f78e5SMatthias Ringwald static const hci_transport_t * transport; 2218129e542SMatthias Ringwald static void * config = NULL; 2228129e542SMatthias Ringwald static btstack_control_t * control; 2238129e542SMatthias Ringwald 2248129e542SMatthias Ringwald #ifdef HAVE_INTEL_USB 2258129e542SMatthias Ringwald static int intel_firmware_loaded; 2268129e542SMatthias Ringwald #endif 2278129e542SMatthias Ringwald 2282531c97eSMatthias Ringwald static btstack_timer_source_t timeout; 2292531c97eSMatthias Ringwald static uint8_t timeout_active = 0; 2302531c97eSMatthias Ringwald static int power_management_sleep = 0; 2312531c97eSMatthias Ringwald static btstack_linked_list_t clients = NULL; // list of connected clients ` 2322531c97eSMatthias Ringwald #ifdef ENABLE_BLE 2332531c97eSMatthias Ringwald static btstack_linked_list_t gatt_client_helpers = NULL; // list of used gatt client (helpers) 2342531c97eSMatthias Ringwald #endif 2352531c97eSMatthias Ringwald 2362531c97eSMatthias Ringwald static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth_status_handler; 2372531c97eSMatthias Ringwald 2382531c97eSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 2391edc4fc7SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 2402531c97eSMatthias Ringwald 2412531c97eSMatthias Ringwald static int global_enable = 0; 2422531c97eSMatthias Ringwald 2432531c97eSMatthias Ringwald static btstack_link_key_db_t const * btstack_link_key_db = NULL; 2442531c97eSMatthias Ringwald static btstack_device_name_db_t const * btstack_device_name_db = NULL; 2452531c97eSMatthias Ringwald // static int rfcomm_channel_generator = 1; 2462531c97eSMatthias Ringwald 2472531c97eSMatthias Ringwald static uint8_t attribute_value[1000]; 2482531c97eSMatthias Ringwald static const int attribute_value_buffer_size = sizeof(attribute_value); 2492531c97eSMatthias Ringwald static uint8_t serviceSearchPattern[200]; 2502531c97eSMatthias Ringwald static uint8_t attributeIDList[50]; 2512531c97eSMatthias Ringwald static void * sdp_client_query_connection; 2522531c97eSMatthias Ringwald 2536cf7652eSMatthias Ringwald static char string_buffer[1000]; 2546cf7652eSMatthias Ringwald 2552531c97eSMatthias Ringwald static int loggingEnabled; 2562531c97eSMatthias Ringwald 2576cf7652eSMatthias Ringwald static const char * btstack_server_storage_path; 2582531c97eSMatthias Ringwald 259a05b2457SMatthias Ringwald // TLV 260a05b2457SMatthias Ringwald static int tlv_setup_done; 261a05b2457SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 262a05b2457SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 263a05b2457SMatthias Ringwald 2642531c97eSMatthias Ringwald static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state){ 2652531c97eSMatthias Ringwald log_info("Bluetooth status: %u\n", state); 2662531c97eSMatthias Ringwald }; 2672531c97eSMatthias Ringwald 2682531c97eSMatthias Ringwald static void daemon_no_connections_timeout(struct btstack_timer_source *ts){ 2692531c97eSMatthias Ringwald if (clients_require_power_on()) return; // false alarm :) 2702531c97eSMatthias Ringwald log_info("No active client connection for %u seconds -> POWER OFF\n", DAEMON_NO_ACTIVE_CLIENT_TIMEOUT/1000); 2712531c97eSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 2722531c97eSMatthias Ringwald } 2732531c97eSMatthias Ringwald 2742531c97eSMatthias Ringwald 2752531c97eSMatthias Ringwald static void add_uint32_to_list(btstack_linked_list_t *list, uint32_t value){ 2762531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 2772531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, list); 2782531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2792531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 2802531c97eSMatthias Ringwald if ( item->value == value) return; // already in list 2812531c97eSMatthias Ringwald } 2822531c97eSMatthias Ringwald 2832531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = malloc(sizeof(btstack_linked_list_uint32_t)); 2842531c97eSMatthias Ringwald if (!item) return; 285c77e7c45SMatthias Ringwald memset(item, 0, sizeof(btstack_linked_list_uint32_t)); 2862531c97eSMatthias Ringwald item->value = value; 2872531c97eSMatthias Ringwald btstack_linked_list_add(list, (btstack_linked_item_t *) item); 2882531c97eSMatthias Ringwald } 2892531c97eSMatthias Ringwald 2902531c97eSMatthias Ringwald static void remove_and_free_uint32_from_list(btstack_linked_list_t *list, uint32_t value){ 2912531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 2922531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, list); 2932531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2942531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 2952531c97eSMatthias Ringwald if ( item->value != value) continue; 2962531c97eSMatthias Ringwald btstack_linked_list_remove(list, (btstack_linked_item_t *) item); 2972531c97eSMatthias Ringwald free(item); 2982531c97eSMatthias Ringwald } 2992531c97eSMatthias Ringwald } 3002531c97eSMatthias Ringwald 3012531c97eSMatthias Ringwald static void daemon_add_client_rfcomm_service(connection_t * connection, uint16_t service_channel){ 3022531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3032531c97eSMatthias Ringwald if (!client_state) return; 3042531c97eSMatthias Ringwald add_uint32_to_list(&client_state->rfcomm_services, service_channel); 3052531c97eSMatthias Ringwald } 3062531c97eSMatthias Ringwald 3072531c97eSMatthias Ringwald static void daemon_remove_client_rfcomm_service(connection_t * connection, uint16_t service_channel){ 3082531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3092531c97eSMatthias Ringwald if (!client_state) return; 3102531c97eSMatthias Ringwald remove_and_free_uint32_from_list(&client_state->rfcomm_services, service_channel); 3112531c97eSMatthias Ringwald } 3122531c97eSMatthias Ringwald 3132531c97eSMatthias Ringwald static void daemon_add_client_rfcomm_channel(connection_t * connection, uint16_t cid){ 3142531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3152531c97eSMatthias Ringwald if (!client_state) return; 3162531c97eSMatthias Ringwald add_uint32_to_list(&client_state->rfcomm_cids, cid); 3172531c97eSMatthias Ringwald } 3182531c97eSMatthias Ringwald 3192531c97eSMatthias Ringwald static void daemon_remove_client_rfcomm_channel(connection_t * connection, uint16_t cid){ 3202531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3212531c97eSMatthias Ringwald if (!client_state) return; 3222531c97eSMatthias Ringwald remove_and_free_uint32_from_list(&client_state->rfcomm_cids, cid); 3232531c97eSMatthias Ringwald } 3242531c97eSMatthias Ringwald 3252531c97eSMatthias Ringwald static void daemon_add_client_l2cap_service(connection_t * connection, uint16_t psm){ 3262531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3272531c97eSMatthias Ringwald if (!client_state) return; 3282531c97eSMatthias Ringwald add_uint32_to_list(&client_state->l2cap_psms, psm); 3292531c97eSMatthias Ringwald } 3302531c97eSMatthias Ringwald 3312531c97eSMatthias Ringwald static void daemon_remove_client_l2cap_service(connection_t * connection, uint16_t psm){ 3322531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3332531c97eSMatthias Ringwald if (!client_state) return; 3342531c97eSMatthias Ringwald remove_and_free_uint32_from_list(&client_state->l2cap_psms, psm); 3352531c97eSMatthias Ringwald } 3362531c97eSMatthias Ringwald 3372531c97eSMatthias Ringwald static void daemon_add_client_l2cap_channel(connection_t * connection, uint16_t cid){ 3382531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3392531c97eSMatthias Ringwald if (!client_state) return; 3402531c97eSMatthias Ringwald add_uint32_to_list(&client_state->l2cap_cids, cid); 3412531c97eSMatthias Ringwald } 3422531c97eSMatthias Ringwald 3432531c97eSMatthias Ringwald static void daemon_remove_client_l2cap_channel(connection_t * connection, uint16_t cid){ 3442531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3452531c97eSMatthias Ringwald if (!client_state) return; 3462531c97eSMatthias Ringwald remove_and_free_uint32_from_list(&client_state->l2cap_cids, cid); 3472531c97eSMatthias Ringwald } 3482531c97eSMatthias Ringwald 3492531c97eSMatthias Ringwald static void daemon_add_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){ 3502531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3512531c97eSMatthias Ringwald if (!client_state) return; 3522531c97eSMatthias Ringwald add_uint32_to_list(&client_state->sdp_record_handles, handle); 3532531c97eSMatthias Ringwald } 3542531c97eSMatthias Ringwald 3552531c97eSMatthias Ringwald static void daemon_remove_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){ 3562531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3572531c97eSMatthias Ringwald if (!client_state) return; 3582531c97eSMatthias Ringwald remove_and_free_uint32_from_list(&client_state->sdp_record_handles, handle); 3592531c97eSMatthias Ringwald } 3602531c97eSMatthias Ringwald 3612531c97eSMatthias Ringwald #ifdef ENABLE_BLE 3622531c97eSMatthias Ringwald static void daemon_add_gatt_client_handle(connection_t * connection, uint32_t handle){ 3632531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 3642531c97eSMatthias Ringwald if (!client_state) return; 3652531c97eSMatthias Ringwald 3662531c97eSMatthias Ringwald // check if handle already exists in the gatt_con_handles list 3672531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 3682531c97eSMatthias Ringwald int handle_found = 0; 3692531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles); 3702531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3712531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 3722531c97eSMatthias Ringwald if (item->value == handle){ 3732531c97eSMatthias Ringwald handle_found = 1; 3742531c97eSMatthias Ringwald break; 3752531c97eSMatthias Ringwald } 3762531c97eSMatthias Ringwald } 3772531c97eSMatthias Ringwald // if handle doesn't exist add it to gatt_con_handles 3782531c97eSMatthias Ringwald if (!handle_found){ 3792531c97eSMatthias Ringwald add_uint32_to_list(&client_state->gatt_con_handles, handle); 3802531c97eSMatthias Ringwald } 3812531c97eSMatthias Ringwald 3822531c97eSMatthias Ringwald // check if there is a helper with given handle 3832531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * gatt_helper = NULL; 3842531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_helpers); 3852531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3862531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it); 3872531c97eSMatthias Ringwald if (item->con_handle == handle){ 3882531c97eSMatthias Ringwald gatt_helper = item; 3892531c97eSMatthias Ringwald break; 3902531c97eSMatthias Ringwald } 3912531c97eSMatthias Ringwald } 3922531c97eSMatthias Ringwald 3932531c97eSMatthias Ringwald // if gatt_helper doesn't exist, create it and add it to gatt_client_helpers list 3942531c97eSMatthias Ringwald if (!gatt_helper){ 395af0d0df5SMatthias Ringwald gatt_helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1); 3962531c97eSMatthias Ringwald if (!gatt_helper) return; 3972531c97eSMatthias Ringwald gatt_helper->con_handle = handle; 3982531c97eSMatthias Ringwald btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) gatt_helper); 3992531c97eSMatthias Ringwald } 4002531c97eSMatthias Ringwald 4012531c97eSMatthias Ringwald // check if connection exists 4022531c97eSMatthias Ringwald int connection_found = 0; 4032531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_helper->all_connections); 4042531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4052531c97eSMatthias Ringwald btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it); 4062531c97eSMatthias Ringwald if (item->connection == connection){ 4072531c97eSMatthias Ringwald connection_found = 1; 4082531c97eSMatthias Ringwald break; 4092531c97eSMatthias Ringwald } 4102531c97eSMatthias Ringwald } 4112531c97eSMatthias Ringwald 4122531c97eSMatthias Ringwald // if connection is not found, add it to the all_connections, and set it as active connection 4132531c97eSMatthias Ringwald if (!connection_found){ 414af0d0df5SMatthias Ringwald btstack_linked_list_connection_t * con = calloc(sizeof(btstack_linked_list_connection_t), 1); 4152531c97eSMatthias Ringwald if (!con) return; 4162531c97eSMatthias Ringwald con->connection = connection; 4172531c97eSMatthias Ringwald btstack_linked_list_add(&gatt_helper->all_connections, (btstack_linked_item_t *)con); 4182531c97eSMatthias Ringwald } 4192531c97eSMatthias Ringwald } 4202531c97eSMatthias Ringwald 4212531c97eSMatthias Ringwald 4222531c97eSMatthias Ringwald static void daemon_remove_gatt_client_handle(connection_t * connection, uint32_t handle){ 4232531c97eSMatthias Ringwald // PART 1 - uses connection & handle 4242531c97eSMatthias Ringwald // might be extracted or vanish totally 4252531c97eSMatthias Ringwald client_state_t * client_state = client_for_connection(connection); 4262531c97eSMatthias Ringwald if (!client_state) return; 4272531c97eSMatthias Ringwald 4282531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 4292531c97eSMatthias Ringwald // remove handle from gatt_con_handles list 4302531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles); 4312531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4322531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 4332531c97eSMatthias Ringwald if (item->value == handle){ 4342531c97eSMatthias Ringwald btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item); 4352531c97eSMatthias Ringwald free(item); 4362531c97eSMatthias Ringwald } 4372531c97eSMatthias Ringwald } 4382531c97eSMatthias Ringwald 4392531c97eSMatthias Ringwald // PART 2 - only uses handle 4402531c97eSMatthias Ringwald 4412531c97eSMatthias Ringwald // find helper with given handle 4422531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * helper = NULL; 4432531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_helpers); 4442531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4452531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it); 4462531c97eSMatthias Ringwald if (item->con_handle == handle){ 4472531c97eSMatthias Ringwald helper = item; 4482531c97eSMatthias Ringwald break; 4492531c97eSMatthias Ringwald } 4502531c97eSMatthias Ringwald } 4512531c97eSMatthias Ringwald 4522531c97eSMatthias Ringwald if (!helper) return; 4532531c97eSMatthias Ringwald // remove connection from helper 4542531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &helper->all_connections); 4552531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4562531c97eSMatthias Ringwald btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it); 4572531c97eSMatthias Ringwald if (item->connection == connection){ 4582531c97eSMatthias Ringwald btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item); 4592531c97eSMatthias Ringwald free(item); 4602531c97eSMatthias Ringwald break; 4612531c97eSMatthias Ringwald } 4622531c97eSMatthias Ringwald } 4632531c97eSMatthias Ringwald 4642531c97eSMatthias Ringwald if (helper->active_connection == connection){ 4652531c97eSMatthias Ringwald helper->active_connection = NULL; 4662531c97eSMatthias Ringwald } 4672531c97eSMatthias Ringwald // if helper has no more connections, call disconnect 4682531c97eSMatthias Ringwald if (helper->all_connections == NULL){ 4692531c97eSMatthias Ringwald gap_disconnect((hci_con_handle_t) helper->con_handle); 4702531c97eSMatthias Ringwald } 4712531c97eSMatthias Ringwald } 4722531c97eSMatthias Ringwald 4732531c97eSMatthias Ringwald 4742531c97eSMatthias Ringwald static void daemon_remove_gatt_client_helper(uint32_t con_handle){ 475712999cdSMatthias Ringwald log_info("daemon_remove_gatt_client_helper for con_handle 0x%04x", con_handle); 476712999cdSMatthias Ringwald 4772531c97eSMatthias Ringwald btstack_linked_list_iterator_t it, cl; 4782531c97eSMatthias Ringwald // find helper with given handle 4792531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * helper = NULL; 4802531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_helpers); 4812531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4822531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it); 4832531c97eSMatthias Ringwald if (item->con_handle == con_handle){ 4842531c97eSMatthias Ringwald helper = item; 4852531c97eSMatthias Ringwald break; 4862531c97eSMatthias Ringwald } 4872531c97eSMatthias Ringwald } 4882531c97eSMatthias Ringwald 4892531c97eSMatthias Ringwald if (!helper) return; 4902531c97eSMatthias Ringwald 49124047dd6SMatthias Ringwald // stop listening 49224047dd6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &helper->gatt_client_notifications); 49324047dd6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 49424047dd6SMatthias Ringwald btstack_linked_list_gatt_client_notification_t * item = (btstack_linked_list_gatt_client_notification_t*) btstack_linked_list_iterator_next(&it); 49524047dd6SMatthias Ringwald log_info("Stop gatt notification listener %p", item); 49624047dd6SMatthias Ringwald gatt_client_stop_listening_for_characteristic_value_updates(&item->notification_listener); 49724047dd6SMatthias Ringwald btstack_linked_list_remove(&helper->gatt_client_notifications, (btstack_linked_item_t *) item); 49824047dd6SMatthias Ringwald free(item); 49924047dd6SMatthias Ringwald } 50024047dd6SMatthias Ringwald 5012531c97eSMatthias Ringwald // remove all connection from helper 5022531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &helper->all_connections); 5032531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5042531c97eSMatthias Ringwald btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it); 5052531c97eSMatthias Ringwald btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item); 5062531c97eSMatthias Ringwald free(item); 5072531c97eSMatthias Ringwald } 5082531c97eSMatthias Ringwald 5092531c97eSMatthias Ringwald btstack_linked_list_remove(&gatt_client_helpers, (btstack_linked_item_t *) helper); 5102531c97eSMatthias Ringwald free(helper); 5112531c97eSMatthias Ringwald 5122531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&cl, &clients); 5132531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&cl)){ 5142531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl); 5152531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles); 5162531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5172531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5182531c97eSMatthias Ringwald if (item->value == con_handle){ 5192531c97eSMatthias Ringwald btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item); 5202531c97eSMatthias Ringwald free(item); 5212531c97eSMatthias Ringwald } 5222531c97eSMatthias Ringwald } 5232531c97eSMatthias Ringwald } 5242531c97eSMatthias Ringwald } 5252531c97eSMatthias Ringwald #endif 5262531c97eSMatthias Ringwald 5272531c97eSMatthias Ringwald static void daemon_rfcomm_close_connection(client_state_t * daemon_client){ 5282531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 5292531c97eSMatthias Ringwald btstack_linked_list_t *rfcomm_services = &daemon_client->rfcomm_services; 5302531c97eSMatthias Ringwald btstack_linked_list_t *rfcomm_cids = &daemon_client->rfcomm_cids; 5312531c97eSMatthias Ringwald 5322531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, rfcomm_services); 5332531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5342531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5352531c97eSMatthias Ringwald rfcomm_unregister_service(item->value); 5362531c97eSMatthias Ringwald btstack_linked_list_remove(rfcomm_services, (btstack_linked_item_t *) item); 5372531c97eSMatthias Ringwald free(item); 5382531c97eSMatthias Ringwald } 5392531c97eSMatthias Ringwald 5402531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, rfcomm_cids); 5412531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5422531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5432531c97eSMatthias Ringwald rfcomm_disconnect(item->value); 5442531c97eSMatthias Ringwald btstack_linked_list_remove(rfcomm_cids, (btstack_linked_item_t *) item); 5452531c97eSMatthias Ringwald free(item); 5462531c97eSMatthias Ringwald } 5472531c97eSMatthias Ringwald } 5482531c97eSMatthias Ringwald 5492531c97eSMatthias Ringwald 5502531c97eSMatthias Ringwald static void daemon_l2cap_close_connection(client_state_t * daemon_client){ 5512531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 5522531c97eSMatthias Ringwald btstack_linked_list_t *l2cap_psms = &daemon_client->l2cap_psms; 5532531c97eSMatthias Ringwald btstack_linked_list_t *l2cap_cids = &daemon_client->l2cap_cids; 5542531c97eSMatthias Ringwald 5552531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, l2cap_psms); 5562531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5572531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5582531c97eSMatthias Ringwald l2cap_unregister_service(item->value); 5592531c97eSMatthias Ringwald btstack_linked_list_remove(l2cap_psms, (btstack_linked_item_t *) item); 5602531c97eSMatthias Ringwald free(item); 5612531c97eSMatthias Ringwald } 5622531c97eSMatthias Ringwald 5632531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, l2cap_cids); 5642531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5652531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5662531c97eSMatthias Ringwald l2cap_disconnect(item->value, 0); // note: reason isn't used 5672531c97eSMatthias Ringwald btstack_linked_list_remove(l2cap_cids, (btstack_linked_item_t *) item); 5682531c97eSMatthias Ringwald free(item); 5692531c97eSMatthias Ringwald } 5702531c97eSMatthias Ringwald } 5712531c97eSMatthias Ringwald 5722531c97eSMatthias Ringwald static void daemon_sdp_close_connection(client_state_t * daemon_client){ 5732531c97eSMatthias Ringwald btstack_linked_list_t * list = &daemon_client->sdp_record_handles; 5742531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 5752531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, list); 5762531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5772531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5782531c97eSMatthias Ringwald sdp_unregister_service(item->value); 5792531c97eSMatthias Ringwald btstack_linked_list_remove(list, (btstack_linked_item_t *) item); 5802531c97eSMatthias Ringwald free(item); 5812531c97eSMatthias Ringwald } 5822531c97eSMatthias Ringwald } 5832531c97eSMatthias Ringwald 5842531c97eSMatthias Ringwald static connection_t * connection_for_l2cap_cid(uint16_t cid){ 5852531c97eSMatthias Ringwald btstack_linked_list_iterator_t cl; 5862531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&cl, &clients); 5872531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&cl)){ 5882531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl); 5892531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 5902531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client_state->l2cap_cids); 5912531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5922531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 5932531c97eSMatthias Ringwald if (item->value == cid){ 5942531c97eSMatthias Ringwald return client_state->connection; 5952531c97eSMatthias Ringwald } 5962531c97eSMatthias Ringwald } 5972531c97eSMatthias Ringwald } 5982531c97eSMatthias Ringwald return NULL; 5992531c97eSMatthias Ringwald } 6002531c97eSMatthias Ringwald 6012531c97eSMatthias Ringwald static const uint8_t removeServiceRecordHandleAttributeIDList[] = { 0x36, 0x00, 0x05, 0x0A, 0x00, 0x01, 0xFF, 0xFF }; 6022531c97eSMatthias Ringwald 6032531c97eSMatthias Ringwald // register a service record 6042531c97eSMatthias Ringwald // pre: AttributeIDs are in ascending order 6052531c97eSMatthias Ringwald // pre: ServiceRecordHandle is first attribute and is not already registered in database 6062531c97eSMatthias Ringwald // @returns status 6072531c97eSMatthias Ringwald static uint32_t daemon_sdp_create_and_register_service(uint8_t * record){ 6082531c97eSMatthias Ringwald 6092531c97eSMatthias Ringwald // create new handle 6102531c97eSMatthias Ringwald uint32_t record_handle = sdp_create_service_record_handle(); 6112531c97eSMatthias Ringwald 6122531c97eSMatthias Ringwald // calculate size of new service record: DES (2 byte len) 6132531c97eSMatthias Ringwald // + ServiceRecordHandle attribute (UINT16 UINT32) + size of existing attributes 6142531c97eSMatthias Ringwald uint16_t recordSize = 3 + (3 + 5) + de_get_data_size(record); 6152531c97eSMatthias Ringwald 6162531c97eSMatthias Ringwald // alloc memory for new service record 6172531c97eSMatthias Ringwald uint8_t * newRecord = malloc(recordSize); 6182531c97eSMatthias Ringwald if (!newRecord) return 0; 6192531c97eSMatthias Ringwald 6202531c97eSMatthias Ringwald // create DES for new record 6212531c97eSMatthias Ringwald de_create_sequence(newRecord); 6222531c97eSMatthias Ringwald 6232531c97eSMatthias Ringwald // set service record handle 6242531c97eSMatthias Ringwald de_add_number(newRecord, DE_UINT, DE_SIZE_16, 0); 6252531c97eSMatthias Ringwald de_add_number(newRecord, DE_UINT, DE_SIZE_32, record_handle); 6262531c97eSMatthias Ringwald 6272531c97eSMatthias Ringwald // add other attributes 6282531c97eSMatthias Ringwald sdp_append_attributes_in_attributeIDList(record, (uint8_t *) removeServiceRecordHandleAttributeIDList, 0, recordSize, newRecord); 6292531c97eSMatthias Ringwald 6302531c97eSMatthias Ringwald uint8_t status = sdp_register_service(newRecord); 6312531c97eSMatthias Ringwald 6322531c97eSMatthias Ringwald if (status) { 6332531c97eSMatthias Ringwald free(newRecord); 6342531c97eSMatthias Ringwald return 0; 6352531c97eSMatthias Ringwald } 6362531c97eSMatthias Ringwald 6372531c97eSMatthias Ringwald return record_handle; 6382531c97eSMatthias Ringwald } 6392531c97eSMatthias Ringwald 6402531c97eSMatthias Ringwald static connection_t * connection_for_rfcomm_cid(uint16_t cid){ 6412531c97eSMatthias Ringwald btstack_linked_list_iterator_t cl; 6422531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&cl, &clients); 6432531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&cl)){ 6442531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl); 6452531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 6462531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client_state->rfcomm_cids); 6472531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6482531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 6492531c97eSMatthias Ringwald if (item->value == cid){ 6502531c97eSMatthias Ringwald return client_state->connection; 6512531c97eSMatthias Ringwald } 6522531c97eSMatthias Ringwald } 6532531c97eSMatthias Ringwald } 6542531c97eSMatthias Ringwald return NULL; 6552531c97eSMatthias Ringwald } 6562531c97eSMatthias Ringwald 6572531c97eSMatthias Ringwald #ifdef ENABLE_BLE 6582531c97eSMatthias Ringwald static void daemon_gatt_client_close_connection(connection_t * connection){ 6592531c97eSMatthias Ringwald client_state_t * client = client_for_connection(connection); 6602531c97eSMatthias Ringwald if (!client) return; 6612531c97eSMatthias Ringwald 6622531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 66324047dd6SMatthias Ringwald 6642531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &client->gatt_con_handles); 6652531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6662531c97eSMatthias Ringwald btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it); 6672531c97eSMatthias Ringwald daemon_remove_gatt_client_handle(connection, item->value); 6682531c97eSMatthias Ringwald } 6692531c97eSMatthias Ringwald } 6702531c97eSMatthias Ringwald #endif 6712531c97eSMatthias Ringwald 6722531c97eSMatthias Ringwald static void daemon_disconnect_client(connection_t * connection){ 6732531c97eSMatthias Ringwald log_info("Daemon disconnect client %p\n",connection); 6742531c97eSMatthias Ringwald 6752531c97eSMatthias Ringwald client_state_t * client = client_for_connection(connection); 6762531c97eSMatthias Ringwald if (!client) return; 6772531c97eSMatthias Ringwald 6782531c97eSMatthias Ringwald daemon_sdp_close_connection(client); 6792531c97eSMatthias Ringwald daemon_rfcomm_close_connection(client); 6802531c97eSMatthias Ringwald daemon_l2cap_close_connection(client); 6812531c97eSMatthias Ringwald #ifdef ENABLE_BLE 6822531c97eSMatthias Ringwald // NOTE: experimental - disconnect all LE connections where GATT Client was used 6832531c97eSMatthias Ringwald // gatt_client_disconnect_connection(connection); 6842531c97eSMatthias Ringwald daemon_gatt_client_close_connection(connection); 6852531c97eSMatthias Ringwald #endif 6862531c97eSMatthias Ringwald 6872531c97eSMatthias Ringwald btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client); 6882531c97eSMatthias Ringwald free(client); 6892531c97eSMatthias Ringwald } 6902531c97eSMatthias Ringwald 6912531c97eSMatthias Ringwald static void hci_emit_btstack_version(void){ 6922531c97eSMatthias Ringwald log_info("DAEMON_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 6932531c97eSMatthias Ringwald uint8_t event[6]; 6942531c97eSMatthias Ringwald event[0] = DAEMON_EVENT_VERSION; 6952531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 6962531c97eSMatthias Ringwald event[2] = BTSTACK_MAJOR; 6972531c97eSMatthias Ringwald event[3] = BTSTACK_MINOR; 6982531c97eSMatthias Ringwald little_endian_store_16(event, 4, 3257); // last SVN commit on Google Code + 1 6992531c97eSMatthias Ringwald socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event)); 7002531c97eSMatthias Ringwald } 7012531c97eSMatthias Ringwald 7022531c97eSMatthias Ringwald static void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 7032531c97eSMatthias Ringwald log_info("DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 7042531c97eSMatthias Ringwald uint8_t event[3]; 7052531c97eSMatthias Ringwald event[0] = DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED; 7062531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 7072531c97eSMatthias Ringwald event[2] = enabled; 7082531c97eSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 7092531c97eSMatthias Ringwald socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event)); 7102531c97eSMatthias Ringwald } 7112531c97eSMatthias Ringwald 7122531c97eSMatthias Ringwald static void send_l2cap_connection_open_failed(connection_t * connection, bd_addr_t address, uint16_t psm, uint8_t status){ 7132531c97eSMatthias Ringwald // emit error - see l2cap.c:l2cap_emit_channel_opened(..) 7142531c97eSMatthias Ringwald uint8_t event[23]; 7152531c97eSMatthias Ringwald memset(event, 0, sizeof(event)); 7162531c97eSMatthias Ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 7172531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 7182531c97eSMatthias Ringwald event[2] = status; 7192531c97eSMatthias Ringwald reverse_bd_addr(address, &event[3]); 7202531c97eSMatthias Ringwald // little_endian_store_16(event, 9, channel->handle); 7212531c97eSMatthias Ringwald little_endian_store_16(event, 11, psm); 7222531c97eSMatthias Ringwald // little_endian_store_16(event, 13, channel->local_cid); 7232531c97eSMatthias Ringwald // little_endian_store_16(event, 15, channel->remote_cid); 7242531c97eSMatthias Ringwald // little_endian_store_16(event, 17, channel->local_mtu); 7252531c97eSMatthias Ringwald // little_endian_store_16(event, 19, channel->remote_mtu); 7262531c97eSMatthias Ringwald // little_endian_store_16(event, 21, channel->flush_timeout); 7272531c97eSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 7282531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 7292531c97eSMatthias Ringwald } 7302531c97eSMatthias Ringwald 7312531c97eSMatthias Ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 7322531c97eSMatthias Ringwald uint8_t event[5]; 73362c64df1SMatthias Ringwald event[0] = DAEMON_EVENT_L2CAP_SERVICE_REGISTERED; 7342531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 7352531c97eSMatthias Ringwald event[2] = status; 7362531c97eSMatthias Ringwald little_endian_store_16(event, 3, psm); 7372531c97eSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 7382531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 7392531c97eSMatthias Ringwald } 7402531c97eSMatthias Ringwald 7412531c97eSMatthias Ringwald static void rfcomm_emit_service_registered(void *connection, uint8_t status, uint8_t channel){ 7422531c97eSMatthias Ringwald uint8_t event[4]; 74362c64df1SMatthias Ringwald event[0] = DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED; 7442531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 7452531c97eSMatthias Ringwald event[2] = status; 7462531c97eSMatthias Ringwald event[3] = channel; 7472531c97eSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 7482531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 7492531c97eSMatthias Ringwald } 7502531c97eSMatthias Ringwald 7512531c97eSMatthias Ringwald static void send_rfcomm_create_channel_failed(void * connection, bd_addr_t addr, uint8_t server_channel, uint8_t status){ 7522531c97eSMatthias Ringwald // emit error - see rfcom.c:rfcomm_emit_channel_open_failed_outgoing_memory(..) 7532531c97eSMatthias Ringwald uint8_t event[16]; 7542531c97eSMatthias Ringwald memset(event, 0, sizeof(event)); 7552531c97eSMatthias Ringwald uint8_t pos = 0; 756f8f6a918SMatthias Ringwald event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED; 7572531c97eSMatthias Ringwald event[pos++] = sizeof(event) - 2; 7582531c97eSMatthias Ringwald event[pos++] = status; 7592531c97eSMatthias Ringwald reverse_bd_addr(addr, &event[pos]); pos += 6; 7602531c97eSMatthias Ringwald little_endian_store_16(event, pos, 0); pos += 2; 7612531c97eSMatthias Ringwald event[pos++] = server_channel; 7622531c97eSMatthias Ringwald little_endian_store_16(event, pos, 0); pos += 2; // channel ID 7632531c97eSMatthias Ringwald little_endian_store_16(event, pos, 0); pos += 2; // max frame size 7642531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 7652531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 7662531c97eSMatthias Ringwald } 7672531c97eSMatthias Ringwald 7682531c97eSMatthias Ringwald // data: event(8), len(8), status(8), service_record_handle(32) 7692531c97eSMatthias Ringwald static void sdp_emit_service_registered(void *connection, uint32_t handle, uint8_t status) { 7702531c97eSMatthias Ringwald uint8_t event[7]; 77162c64df1SMatthias Ringwald event[0] = DAEMON_EVENT_SDP_SERVICE_REGISTERED; 7722531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 7732531c97eSMatthias Ringwald event[2] = status; 7742531c97eSMatthias Ringwald little_endian_store_32(event, 3, handle); 7752531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 7762531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 7772531c97eSMatthias Ringwald } 7782531c97eSMatthias Ringwald 7792531c97eSMatthias Ringwald #ifdef ENABLE_BLE 7802531c97eSMatthias Ringwald 781fc64f94aSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(hci_con_handle_t con_handle) { 7822531c97eSMatthias Ringwald btstack_linked_list_iterator_t it; 7832531c97eSMatthias Ringwald if (!gatt_client_helpers) return NULL; 784712999cdSMatthias Ringwald log_debug("daemon_get_gatt_client_helper for handle 0x%02x", con_handle); 7852531c97eSMatthias Ringwald 7862531c97eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_helpers); 7872531c97eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7882531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it); 789fc64f94aSMatthias Ringwald if (item->con_handle == con_handle){ 7902531c97eSMatthias Ringwald return item; 7912531c97eSMatthias Ringwald } 7922531c97eSMatthias Ringwald } 793712999cdSMatthias Ringwald log_info("no gatt_client_helper for handle 0x%02x yet", con_handle); 7942531c97eSMatthias Ringwald return NULL; 7952531c97eSMatthias Ringwald } 7962531c97eSMatthias Ringwald 797fc64f94aSMatthias Ringwald static void send_gatt_query_complete(connection_t * connection, hci_con_handle_t con_handle, uint8_t status){ 7982531c97eSMatthias Ringwald // @format H1 7992531c97eSMatthias Ringwald uint8_t event[5]; 8002531c97eSMatthias Ringwald event[0] = GATT_EVENT_QUERY_COMPLETE; 8012531c97eSMatthias Ringwald event[1] = 3; 802fc64f94aSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 8032531c97eSMatthias Ringwald event[4] = status; 8042531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 8052531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 8062531c97eSMatthias Ringwald } 8072531c97eSMatthias Ringwald 808fc64f94aSMatthias Ringwald static void send_gatt_mtu_event(connection_t * connection, hci_con_handle_t con_handle, uint16_t mtu){ 8092531c97eSMatthias Ringwald uint8_t event[6]; 8102531c97eSMatthias Ringwald int pos = 0; 8112531c97eSMatthias Ringwald event[pos++] = GATT_EVENT_MTU; 8122531c97eSMatthias Ringwald event[pos++] = sizeof(event) - 2; 813fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 8142531c97eSMatthias Ringwald pos += 2; 8152531c97eSMatthias Ringwald little_endian_store_16(event, pos, mtu); 8162531c97eSMatthias Ringwald pos += 2; 8172531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 8182531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 8192531c97eSMatthias Ringwald } 8202531c97eSMatthias Ringwald 8212531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(connection_t *connection, uint8_t *packet, int track_active_connection) { 822fc64f94aSMatthias Ringwald hci_con_handle_t con_handle = little_endian_read_16(packet, 3); 823fc64f94aSMatthias Ringwald log_info("daemon_setup_gatt_client_request for handle 0x%02x", con_handle); 824fc64f94aSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 8252531c97eSMatthias Ringwald if ((hci_con == NULL) || (hci_con->state != OPEN)){ 826fc64f94aSMatthias Ringwald send_gatt_query_complete(connection, con_handle, GATT_CLIENT_NOT_CONNECTED); 8272531c97eSMatthias Ringwald return NULL; 8282531c97eSMatthias Ringwald } 8292531c97eSMatthias Ringwald 830fc64f94aSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * helper = daemon_get_gatt_client_helper(con_handle); 8312531c97eSMatthias Ringwald 8322531c97eSMatthias Ringwald if (!helper){ 8332531c97eSMatthias Ringwald log_info("helper does not exist"); 834af0d0df5SMatthias Ringwald helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1); 8352531c97eSMatthias Ringwald if (!helper) return NULL; 836fc64f94aSMatthias Ringwald helper->con_handle = con_handle; 8372531c97eSMatthias Ringwald btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) helper); 8382531c97eSMatthias Ringwald } 8392531c97eSMatthias Ringwald 8402531c97eSMatthias Ringwald if (track_active_connection && helper->active_connection){ 841fc64f94aSMatthias Ringwald send_gatt_query_complete(connection, con_handle, GATT_CLIENT_BUSY); 8422531c97eSMatthias Ringwald return NULL; 8432531c97eSMatthias Ringwald } 8442531c97eSMatthias Ringwald 845fc64f94aSMatthias Ringwald daemon_add_gatt_client_handle(connection, con_handle); 8462531c97eSMatthias Ringwald 8472531c97eSMatthias Ringwald if (track_active_connection){ 8482531c97eSMatthias Ringwald // remember connection responsible for this request 8492531c97eSMatthias Ringwald helper->active_connection = connection; 8502531c97eSMatthias Ringwald } 8512531c97eSMatthias Ringwald 8522531c97eSMatthias Ringwald return helper; 8532531c97eSMatthias Ringwald } 8542531c97eSMatthias Ringwald 8552531c97eSMatthias Ringwald // (de)serialize structs from/to HCI commands/events 8562531c97eSMatthias Ringwald 8572531c97eSMatthias Ringwald void daemon_gatt_serialize_service(gatt_client_service_t * service, uint8_t * event, int offset){ 8582531c97eSMatthias Ringwald little_endian_store_16(event, offset, service->start_group_handle); 8592531c97eSMatthias Ringwald little_endian_store_16(event, offset+2, service->end_group_handle); 8602531c97eSMatthias Ringwald reverse_128(service->uuid128, &event[offset + 4]); 8612531c97eSMatthias Ringwald } 8622531c97eSMatthias Ringwald 8632531c97eSMatthias Ringwald void daemon_gatt_serialize_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * event, int offset){ 8642531c97eSMatthias Ringwald little_endian_store_16(event, offset, characteristic->start_handle); 8652531c97eSMatthias Ringwald little_endian_store_16(event, offset+2, characteristic->value_handle); 8662531c97eSMatthias Ringwald little_endian_store_16(event, offset+4, characteristic->end_handle); 8672531c97eSMatthias Ringwald little_endian_store_16(event, offset+6, characteristic->properties); 8682531c97eSMatthias Ringwald reverse_128(characteristic->uuid128, &event[offset+8]); 8692531c97eSMatthias Ringwald } 8702531c97eSMatthias Ringwald 8712531c97eSMatthias Ringwald void daemon_gatt_serialize_characteristic_descriptor(gatt_client_characteristic_descriptor_t * characteristic_descriptor, uint8_t * event, int offset){ 8722531c97eSMatthias Ringwald little_endian_store_16(event, offset, characteristic_descriptor->handle); 8732531c97eSMatthias Ringwald reverse_128(characteristic_descriptor->uuid128, &event[offset+2]); 8742531c97eSMatthias Ringwald } 8752531c97eSMatthias Ringwald 8762531c97eSMatthias Ringwald #endif 8772531c97eSMatthias Ringwald 8788129e542SMatthias Ringwald #ifdef HAVE_INTEL_USB 8798129e542SMatthias Ringwald static void btstack_server_intel_firmware_done(int result){ 8808129e542SMatthias Ringwald intel_firmware_loaded = 1; 8818129e542SMatthias Ringwald // setup stack 8828129e542SMatthias Ringwald btstack_server_configure_stack(); 8838129e542SMatthias Ringwald // start power up 8848129e542SMatthias Ringwald hci_power_control(HCI_POWER_ON); 8858129e542SMatthias Ringwald } 8868129e542SMatthias Ringwald #endif 8878129e542SMatthias Ringwald 8882531c97eSMatthias Ringwald static int btstack_command_handler(connection_t *connection, uint8_t *packet, uint16_t size){ 8892531c97eSMatthias Ringwald 8902531c97eSMatthias Ringwald bd_addr_t addr; 891a046efa6SMatthias Ringwald #ifdef ENABLE_BLE 8922531c97eSMatthias Ringwald bd_addr_type_t addr_type; 8932531c97eSMatthias Ringwald hci_con_handle_t handle; 894a046efa6SMatthias Ringwald #endif 8952531c97eSMatthias Ringwald uint16_t cid; 8962531c97eSMatthias Ringwald uint16_t psm; 8972531c97eSMatthias Ringwald uint16_t service_channel; 8982531c97eSMatthias Ringwald uint16_t mtu; 8992531c97eSMatthias Ringwald uint8_t reason; 9002531c97eSMatthias Ringwald uint8_t rfcomm_channel; 9012531c97eSMatthias Ringwald uint8_t rfcomm_credits; 9022531c97eSMatthias Ringwald uint32_t service_record_handle; 9032531c97eSMatthias Ringwald client_state_t *client; 9042531c97eSMatthias Ringwald uint8_t status; 9052531c97eSMatthias Ringwald uint8_t * data; 9062531c97eSMatthias Ringwald #if defined(HAVE_MALLOC) && defined(ENABLE_BLE) 9072531c97eSMatthias Ringwald uint8_t uuid128[16]; 9082531c97eSMatthias Ringwald gatt_client_service_t service; 9092531c97eSMatthias Ringwald gatt_client_characteristic_t characteristic; 9102531c97eSMatthias Ringwald gatt_client_characteristic_descriptor_t descriptor; 9112531c97eSMatthias Ringwald uint16_t data_length; 9122531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * gatt_helper; 9132531c97eSMatthias Ringwald #endif 9142531c97eSMatthias Ringwald 9152531c97eSMatthias Ringwald uint16_t serviceSearchPatternLen; 9162531c97eSMatthias Ringwald uint16_t attributeIDListLen; 9172531c97eSMatthias Ringwald 9182531c97eSMatthias Ringwald // verbose log info before other info to allow for better tracking 9192531c97eSMatthias Ringwald hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size); 9202531c97eSMatthias Ringwald 9212531c97eSMatthias Ringwald // BTstack internal commands - 16 Bit OpCode, 8 Bit ParamLen, Params... 9222531c97eSMatthias Ringwald switch (READ_CMD_OCF(packet)){ 9232531c97eSMatthias Ringwald case BTSTACK_GET_STATE: 9242531c97eSMatthias Ringwald log_info("BTSTACK_GET_STATE"); 9252531c97eSMatthias Ringwald hci_emit_state(); 9262531c97eSMatthias Ringwald break; 9272531c97eSMatthias Ringwald case BTSTACK_SET_POWER_MODE: 9282531c97eSMatthias Ringwald log_info("BTSTACK_SET_POWER_MODE %u", packet[3]); 9292531c97eSMatthias Ringwald // track client power requests 9302531c97eSMatthias Ringwald client = client_for_connection(connection); 9312531c97eSMatthias Ringwald if (!client) break; 9322531c97eSMatthias Ringwald client->power_mode = packet[3]; 9332531c97eSMatthias Ringwald // handle merged state 9342531c97eSMatthias Ringwald if (!clients_require_power_on()){ 9352531c97eSMatthias Ringwald start_power_off_timer(); 9362531c97eSMatthias Ringwald } else if (!power_management_sleep) { 9372531c97eSMatthias Ringwald stop_power_off_timer(); 9388129e542SMatthias Ringwald #ifdef HAVE_INTEL_USB 9390c208dfbSMatthias Ringwald if (!intel_firmware_loaded){ 9408129e542SMatthias Ringwald // before staring up the stack, load intel firmware 9418129e542SMatthias Ringwald btstack_chipset_intel_download_firmware(transport, &btstack_server_intel_firmware_done); 9420c208dfbSMatthias Ringwald break; 9430c208dfbSMatthias Ringwald } 9448129e542SMatthias Ringwald #endif 945*59064b80SMatthias Ringwald hci_power_control(HCI_POWER_ON); 9462531c97eSMatthias Ringwald } 9472531c97eSMatthias Ringwald break; 9482531c97eSMatthias Ringwald case BTSTACK_GET_VERSION: 9492531c97eSMatthias Ringwald log_info("BTSTACK_GET_VERSION"); 9502531c97eSMatthias Ringwald hci_emit_btstack_version(); 9512531c97eSMatthias Ringwald break; 9522531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 9532531c97eSMatthias Ringwald case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED: 9542531c97eSMatthias Ringwald log_info("BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED %u", packet[3]); 9552531c97eSMatthias Ringwald btstack_control_iphone_bt_set_enabled(packet[3]); 9562531c97eSMatthias Ringwald hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled()); 9572531c97eSMatthias Ringwald break; 9582531c97eSMatthias Ringwald 9592531c97eSMatthias Ringwald case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED: 9602531c97eSMatthias Ringwald log_info("BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED"); 9612531c97eSMatthias Ringwald hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled()); 9622531c97eSMatthias Ringwald break; 9632531c97eSMatthias Ringwald #else 9642531c97eSMatthias Ringwald case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED: 9652531c97eSMatthias Ringwald case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED: 9662531c97eSMatthias Ringwald hci_emit_system_bluetooth_enabled(0); 9672531c97eSMatthias Ringwald break; 9682531c97eSMatthias Ringwald #endif 9692531c97eSMatthias Ringwald case BTSTACK_SET_DISCOVERABLE: 9702531c97eSMatthias Ringwald log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]); 9712531c97eSMatthias Ringwald // track client discoverable requests 9722531c97eSMatthias Ringwald client = client_for_connection(connection); 9732531c97eSMatthias Ringwald if (!client) break; 9742531c97eSMatthias Ringwald client->discoverable = packet[3]; 9752531c97eSMatthias Ringwald // merge state 9762531c97eSMatthias Ringwald gap_discoverable_control(clients_require_discoverable()); 9772531c97eSMatthias Ringwald break; 9782531c97eSMatthias Ringwald case BTSTACK_SET_BLUETOOTH_ENABLED: 9792531c97eSMatthias Ringwald log_info("BTSTACK_SET_BLUETOOTH_ENABLED: %u\n", packet[3]); 9802531c97eSMatthias Ringwald if (packet[3]) { 9812531c97eSMatthias Ringwald // global enable 9822531c97eSMatthias Ringwald global_enable = 1; 9832531c97eSMatthias Ringwald hci_power_control(HCI_POWER_ON); 9842531c97eSMatthias Ringwald } else { 9852531c97eSMatthias Ringwald global_enable = 0; 9862531c97eSMatthias Ringwald clients_clear_power_request(); 9872531c97eSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 9882531c97eSMatthias Ringwald } 9892531c97eSMatthias Ringwald break; 9902531c97eSMatthias Ringwald case L2CAP_CREATE_CHANNEL_MTU: 9912531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 9922531c97eSMatthias Ringwald psm = little_endian_read_16(packet, 9); 9932531c97eSMatthias Ringwald mtu = little_endian_read_16(packet, 11); 9942531c97eSMatthias Ringwald status = l2cap_create_channel(NULL, addr, psm, mtu, &cid); 9952531c97eSMatthias Ringwald if (status){ 9962531c97eSMatthias Ringwald send_l2cap_connection_open_failed(connection, addr, psm, status); 9972531c97eSMatthias Ringwald } else { 9982531c97eSMatthias Ringwald daemon_add_client_l2cap_channel(connection, cid); 9992531c97eSMatthias Ringwald } 10002531c97eSMatthias Ringwald break; 10012531c97eSMatthias Ringwald case L2CAP_CREATE_CHANNEL: 10022531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 10032531c97eSMatthias Ringwald psm = little_endian_read_16(packet, 9); 10042531c97eSMatthias Ringwald mtu = 150; // until r865 10052531c97eSMatthias Ringwald status = l2cap_create_channel(NULL, addr, psm, mtu, &cid); 10062531c97eSMatthias Ringwald if (status){ 10072531c97eSMatthias Ringwald send_l2cap_connection_open_failed(connection, addr, psm, status); 10082531c97eSMatthias Ringwald } else { 10092531c97eSMatthias Ringwald daemon_add_client_l2cap_channel(connection, cid); 10102531c97eSMatthias Ringwald } 10112531c97eSMatthias Ringwald break; 10122531c97eSMatthias Ringwald case L2CAP_DISCONNECT: 10132531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10142531c97eSMatthias Ringwald reason = packet[5]; 10152531c97eSMatthias Ringwald l2cap_disconnect(cid, reason); 10162531c97eSMatthias Ringwald break; 10172531c97eSMatthias Ringwald case L2CAP_REGISTER_SERVICE: 10182531c97eSMatthias Ringwald psm = little_endian_read_16(packet, 3); 10192531c97eSMatthias Ringwald mtu = little_endian_read_16(packet, 5); 10202531c97eSMatthias Ringwald status = l2cap_register_service(NULL, psm, mtu, LEVEL_0); 10212531c97eSMatthias Ringwald daemon_add_client_l2cap_service(connection, little_endian_read_16(packet, 3)); 10222531c97eSMatthias Ringwald l2cap_emit_service_registered(connection, status, psm); 10232531c97eSMatthias Ringwald break; 10242531c97eSMatthias Ringwald case L2CAP_UNREGISTER_SERVICE: 10252531c97eSMatthias Ringwald psm = little_endian_read_16(packet, 3); 10262531c97eSMatthias Ringwald daemon_remove_client_l2cap_service(connection, psm); 10272531c97eSMatthias Ringwald l2cap_unregister_service(psm); 10282531c97eSMatthias Ringwald break; 10292531c97eSMatthias Ringwald case L2CAP_ACCEPT_CONNECTION: 10302531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10312531c97eSMatthias Ringwald l2cap_accept_connection(cid); 10322531c97eSMatthias Ringwald break; 10332531c97eSMatthias Ringwald case L2CAP_DECLINE_CONNECTION: 10342531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10352531c97eSMatthias Ringwald reason = packet[7]; 10367ef6a7bbSMatthias Ringwald l2cap_decline_connection(cid); 10372531c97eSMatthias Ringwald break; 10382531c97eSMatthias Ringwald case RFCOMM_CREATE_CHANNEL: 10392531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 10402531c97eSMatthias Ringwald rfcomm_channel = packet[9]; 10411edc4fc7SMatthias Ringwald status = rfcomm_create_channel(&stack_packet_handler, addr, rfcomm_channel, &cid); 10422531c97eSMatthias Ringwald if (status){ 10432531c97eSMatthias Ringwald send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status); 10442531c97eSMatthias Ringwald } else { 10452531c97eSMatthias Ringwald daemon_add_client_rfcomm_channel(connection, cid); 10462531c97eSMatthias Ringwald } 10472531c97eSMatthias Ringwald break; 10482531c97eSMatthias Ringwald case RFCOMM_CREATE_CHANNEL_WITH_CREDITS: 10492531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 10502531c97eSMatthias Ringwald rfcomm_channel = packet[9]; 10512531c97eSMatthias Ringwald rfcomm_credits = packet[10]; 10521edc4fc7SMatthias Ringwald status = rfcomm_create_channel_with_initial_credits(&stack_packet_handler, addr, rfcomm_channel, rfcomm_credits, &cid ); 10532531c97eSMatthias Ringwald if (status){ 10542531c97eSMatthias Ringwald send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status); 10552531c97eSMatthias Ringwald } else { 10562531c97eSMatthias Ringwald daemon_add_client_rfcomm_channel(connection, cid); 10572531c97eSMatthias Ringwald } 10582531c97eSMatthias Ringwald break; 10592531c97eSMatthias Ringwald case RFCOMM_DISCONNECT: 10602531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10612531c97eSMatthias Ringwald reason = packet[5]; 10622531c97eSMatthias Ringwald rfcomm_disconnect(cid); 10632531c97eSMatthias Ringwald break; 10642531c97eSMatthias Ringwald case RFCOMM_REGISTER_SERVICE: 10652531c97eSMatthias Ringwald rfcomm_channel = packet[3]; 10662531c97eSMatthias Ringwald mtu = little_endian_read_16(packet, 4); 10671edc4fc7SMatthias Ringwald status = rfcomm_register_service(&stack_packet_handler, rfcomm_channel, mtu); 10682531c97eSMatthias Ringwald rfcomm_emit_service_registered(connection, status, rfcomm_channel); 10692531c97eSMatthias Ringwald break; 10702531c97eSMatthias Ringwald case RFCOMM_REGISTER_SERVICE_WITH_CREDITS: 10712531c97eSMatthias Ringwald rfcomm_channel = packet[3]; 10722531c97eSMatthias Ringwald mtu = little_endian_read_16(packet, 4); 10732531c97eSMatthias Ringwald rfcomm_credits = packet[6]; 10741edc4fc7SMatthias Ringwald status = rfcomm_register_service_with_initial_credits(&stack_packet_handler, rfcomm_channel, mtu, rfcomm_credits); 10752531c97eSMatthias Ringwald rfcomm_emit_service_registered(connection, status, rfcomm_channel); 10762531c97eSMatthias Ringwald break; 10772531c97eSMatthias Ringwald case RFCOMM_UNREGISTER_SERVICE: 10782531c97eSMatthias Ringwald service_channel = little_endian_read_16(packet, 3); 10792531c97eSMatthias Ringwald daemon_remove_client_rfcomm_service(connection, service_channel); 10802531c97eSMatthias Ringwald rfcomm_unregister_service(service_channel); 10812531c97eSMatthias Ringwald break; 10822531c97eSMatthias Ringwald case RFCOMM_ACCEPT_CONNECTION: 10832531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10842531c97eSMatthias Ringwald rfcomm_accept_connection(cid); 10852531c97eSMatthias Ringwald break; 10862531c97eSMatthias Ringwald case RFCOMM_DECLINE_CONNECTION: 10872531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10882531c97eSMatthias Ringwald reason = packet[7]; 10892531c97eSMatthias Ringwald rfcomm_decline_connection(cid); 10902531c97eSMatthias Ringwald break; 10912531c97eSMatthias Ringwald case RFCOMM_GRANT_CREDITS: 10922531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 3); 10932531c97eSMatthias Ringwald rfcomm_credits = packet[5]; 10942531c97eSMatthias Ringwald rfcomm_grant_credits(cid, rfcomm_credits); 10952531c97eSMatthias Ringwald break; 10962531c97eSMatthias Ringwald case RFCOMM_PERSISTENT_CHANNEL: { 10972531c97eSMatthias Ringwald // enforce \0 10982531c97eSMatthias Ringwald packet[3+248] = 0; 10992531c97eSMatthias Ringwald rfcomm_channel = rfcomm_service_db_channel_for_service((char*)&packet[3]); 11001bb145c9SMatthias Ringwald log_info("DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL %u", rfcomm_channel); 11012531c97eSMatthias Ringwald uint8_t event[4]; 11021bb145c9SMatthias Ringwald event[0] = DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL; 11032531c97eSMatthias Ringwald event[1] = sizeof(event) - 2; 11042531c97eSMatthias Ringwald event[2] = 0; 11052531c97eSMatthias Ringwald event[3] = rfcomm_channel; 11062531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 11072531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); 11082531c97eSMatthias Ringwald break; 11092531c97eSMatthias Ringwald } 11102531c97eSMatthias Ringwald case SDP_REGISTER_SERVICE_RECORD: 11112531c97eSMatthias Ringwald log_info("SDP_REGISTER_SERVICE_RECORD size %u\n", size); 11122531c97eSMatthias Ringwald service_record_handle = daemon_sdp_create_and_register_service(&packet[3]); 11132531c97eSMatthias Ringwald if (service_record_handle){ 11142531c97eSMatthias Ringwald daemon_add_client_sdp_service_record_handle(connection, service_record_handle); 11152531c97eSMatthias Ringwald sdp_emit_service_registered(connection, service_record_handle, 0); 11162531c97eSMatthias Ringwald } else { 11172531c97eSMatthias Ringwald sdp_emit_service_registered(connection, 0, BTSTACK_MEMORY_ALLOC_FAILED); 11182531c97eSMatthias Ringwald } 11192531c97eSMatthias Ringwald break; 11202531c97eSMatthias Ringwald case SDP_UNREGISTER_SERVICE_RECORD: 11212531c97eSMatthias Ringwald service_record_handle = little_endian_read_32(packet, 3); 11222531c97eSMatthias Ringwald log_info("SDP_UNREGISTER_SERVICE_RECORD handle 0x%x ", service_record_handle); 11232531c97eSMatthias Ringwald data = sdp_get_record_for_handle(service_record_handle); 11242531c97eSMatthias Ringwald sdp_unregister_service(service_record_handle); 11252531c97eSMatthias Ringwald daemon_remove_client_sdp_service_record_handle(connection, service_record_handle); 11262531c97eSMatthias Ringwald if (data){ 11272531c97eSMatthias Ringwald free(data); 11282531c97eSMatthias Ringwald } 11292531c97eSMatthias Ringwald break; 11302531c97eSMatthias Ringwald case SDP_CLIENT_QUERY_RFCOMM_SERVICES: 11312531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 11322531c97eSMatthias Ringwald 11332531c97eSMatthias Ringwald serviceSearchPatternLen = de_get_len(&packet[9]); 11342531c97eSMatthias Ringwald memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen); 11352531c97eSMatthias Ringwald 11362531c97eSMatthias Ringwald sdp_client_query_connection = connection; 11377fbb5f59SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_search_pattern(&handle_sdp_rfcomm_service_result, addr, serviceSearchPattern); 11382531c97eSMatthias Ringwald 11392531c97eSMatthias Ringwald break; 11402531c97eSMatthias Ringwald case SDP_CLIENT_QUERY_SERVICES: 11412531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 11422531c97eSMatthias Ringwald sdp_client_query_connection = connection; 11432531c97eSMatthias Ringwald 11442531c97eSMatthias Ringwald serviceSearchPatternLen = de_get_len(&packet[9]); 11452531c97eSMatthias Ringwald memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen); 11462531c97eSMatthias Ringwald 11472531c97eSMatthias Ringwald attributeIDListLen = de_get_len(&packet[9+serviceSearchPatternLen]); 11482531c97eSMatthias Ringwald memcpy(attributeIDList, &packet[9+serviceSearchPatternLen], attributeIDListLen); 11492531c97eSMatthias Ringwald 11502531c97eSMatthias Ringwald sdp_client_query(&handle_sdp_client_query_result, addr, (uint8_t*)&serviceSearchPattern[0], (uint8_t*)&attributeIDList[0]); 11512531c97eSMatthias Ringwald break; 1152a046efa6SMatthias Ringwald #ifdef ENABLE_BLE 11532531c97eSMatthias Ringwald case GAP_LE_SCAN_START: 11542531c97eSMatthias Ringwald gap_start_scan(); 11552531c97eSMatthias Ringwald break; 11562531c97eSMatthias Ringwald case GAP_LE_SCAN_STOP: 11572531c97eSMatthias Ringwald gap_stop_scan(); 11582531c97eSMatthias Ringwald break; 11592531c97eSMatthias Ringwald case GAP_LE_SET_SCAN_PARAMETERS: 11602531c97eSMatthias Ringwald gap_set_scan_parameters(packet[3], little_endian_read_16(packet, 4), little_endian_read_16(packet, 6)); 11612531c97eSMatthias Ringwald break; 11622531c97eSMatthias Ringwald case GAP_LE_CONNECT: 11632531c97eSMatthias Ringwald reverse_bd_addr(&packet[4], addr); 11642531c97eSMatthias Ringwald addr_type = packet[3]; 11652531c97eSMatthias Ringwald gap_connect(addr, addr_type); 11662531c97eSMatthias Ringwald break; 11672531c97eSMatthias Ringwald case GAP_LE_CONNECT_CANCEL: 11682531c97eSMatthias Ringwald gap_connect_cancel(); 11692531c97eSMatthias Ringwald break; 11702531c97eSMatthias Ringwald case GAP_DISCONNECT: 11712531c97eSMatthias Ringwald handle = little_endian_read_16(packet, 3); 11722531c97eSMatthias Ringwald gap_disconnect(handle); 11732531c97eSMatthias Ringwald break; 1174a046efa6SMatthias Ringwald #endif 11752531c97eSMatthias Ringwald #if defined(HAVE_MALLOC) && defined(ENABLE_BLE) 11762531c97eSMatthias Ringwald case GATT_DISCOVER_ALL_PRIMARY_SERVICES: 11772531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 11782531c97eSMatthias Ringwald if (!gatt_helper) break; 11792531c97eSMatthias Ringwald gatt_client_discover_primary_services(&handle_gatt_client_event, gatt_helper->con_handle); 11802531c97eSMatthias Ringwald break; 11812531c97eSMatthias Ringwald case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16: 11822531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 11832531c97eSMatthias Ringwald if (!gatt_helper) break; 11842531c97eSMatthias Ringwald gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, gatt_helper->con_handle, little_endian_read_16(packet, 5)); 11852531c97eSMatthias Ringwald break; 11862531c97eSMatthias Ringwald case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128: 11872531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 11882531c97eSMatthias Ringwald if (!gatt_helper) break; 11892531c97eSMatthias Ringwald reverse_128(&packet[5], uuid128); 11902531c97eSMatthias Ringwald gatt_client_discover_primary_services_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, uuid128); 11912531c97eSMatthias Ringwald break; 11922531c97eSMatthias Ringwald case GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE: 11932531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 11942531c97eSMatthias Ringwald if (!gatt_helper) break; 11952531c97eSMatthias Ringwald gatt_client_deserialize_service(packet, 5, &service); 11962531c97eSMatthias Ringwald gatt_client_find_included_services_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service); 11972531c97eSMatthias Ringwald break; 11982531c97eSMatthias Ringwald 11992531c97eSMatthias Ringwald case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE: 12002531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12012531c97eSMatthias Ringwald if (!gatt_helper) break; 12022531c97eSMatthias Ringwald gatt_client_deserialize_service(packet, 5, &service); 12032531c97eSMatthias Ringwald gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service); 12042531c97eSMatthias Ringwald break; 12052531c97eSMatthias Ringwald case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128: 12062531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12072531c97eSMatthias Ringwald if (!gatt_helper) break; 12082531c97eSMatthias Ringwald gatt_client_deserialize_service(packet, 5, &service); 12092531c97eSMatthias Ringwald reverse_128(&packet[5 + SERVICE_LENGTH], uuid128); 12102531c97eSMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, &service, uuid128); 12112531c97eSMatthias Ringwald break; 12122531c97eSMatthias Ringwald case GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS: 12132531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12142531c97eSMatthias Ringwald if (!gatt_helper) break; 12152531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12162531c97eSMatthias Ringwald gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic); 12172531c97eSMatthias Ringwald break; 12182531c97eSMatthias Ringwald 12192531c97eSMatthias Ringwald case GATT_READ_VALUE_OF_CHARACTERISTIC: 12202531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12212531c97eSMatthias Ringwald if (!gatt_helper) break; 12222531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12232531c97eSMatthias Ringwald gatt_client_read_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic); 12242531c97eSMatthias Ringwald break; 12252531c97eSMatthias Ringwald case GATT_READ_LONG_VALUE_OF_CHARACTERISTIC: 12262531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12272531c97eSMatthias Ringwald if (!gatt_helper) break; 12282531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12292531c97eSMatthias Ringwald gatt_client_read_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic); 12302531c97eSMatthias Ringwald break; 12312531c97eSMatthias Ringwald 12322531c97eSMatthias Ringwald case GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE: 12332531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 0); // note: don't track active connection 12342531c97eSMatthias Ringwald if (!gatt_helper) break; 12352531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12362531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH); 12372531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12382531c97eSMatthias Ringwald memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length); 12390ce46700SMatthias Ringwald gatt_client_write_value_of_characteristic_without_response(gatt_helper->con_handle, characteristic.value_handle, data_length, data); 12402531c97eSMatthias Ringwald break; 12412531c97eSMatthias Ringwald case GATT_WRITE_VALUE_OF_CHARACTERISTIC: 12422531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12432531c97eSMatthias Ringwald if (!gatt_helper) break; 12442531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12452531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH); 12462531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12472531c97eSMatthias Ringwald memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length); 12482531c97eSMatthias Ringwald gatt_client_write_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data); 12492531c97eSMatthias Ringwald break; 12502531c97eSMatthias Ringwald case GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC: 12512531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12522531c97eSMatthias Ringwald if (!gatt_helper) break; 12532531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12542531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH); 12552531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12562531c97eSMatthias Ringwald memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length); 12572531c97eSMatthias Ringwald gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data); 12582531c97eSMatthias Ringwald break; 12592531c97eSMatthias Ringwald case GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC: 12602531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12612531c97eSMatthias Ringwald if (!gatt_helper) break; 12622531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 12632531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH); 12642531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12652531c97eSMatthias Ringwald memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length); 12662531c97eSMatthias Ringwald gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data); 12672531c97eSMatthias Ringwald break; 12682531c97eSMatthias Ringwald case GATT_READ_CHARACTERISTIC_DESCRIPTOR: 12692531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12702531c97eSMatthias Ringwald if (!gatt_helper) break; 12712531c97eSMatthias Ringwald handle = little_endian_read_16(packet, 3); 12722531c97eSMatthias Ringwald gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor); 12732531c97eSMatthias Ringwald gatt_client_read_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor); 12742531c97eSMatthias Ringwald break; 12752531c97eSMatthias Ringwald case GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR: 12762531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12772531c97eSMatthias Ringwald if (!gatt_helper) break; 12782531c97eSMatthias Ringwald gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor); 12792531c97eSMatthias Ringwald gatt_client_read_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor); 12802531c97eSMatthias Ringwald break; 12812531c97eSMatthias Ringwald 12822531c97eSMatthias Ringwald case GATT_WRITE_CHARACTERISTIC_DESCRIPTOR: 12832531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12842531c97eSMatthias Ringwald if (!gatt_helper) break; 12852531c97eSMatthias Ringwald gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor); 12862531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12872531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH); 12882531c97eSMatthias Ringwald gatt_client_write_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data); 12892531c97eSMatthias Ringwald break; 12902531c97eSMatthias Ringwald case GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR: 12912531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 12922531c97eSMatthias Ringwald if (!gatt_helper) break; 12932531c97eSMatthias Ringwald gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor); 12942531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 12952531c97eSMatthias Ringwald data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH); 12962531c97eSMatthias Ringwald gatt_client_write_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data); 12972531c97eSMatthias Ringwald break; 12982531c97eSMatthias Ringwald case GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:{ 12992531c97eSMatthias Ringwald uint16_t configuration = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH); 13002531c97eSMatthias Ringwald gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1); 13012531c97eSMatthias Ringwald if (!gatt_helper) break; 13022531c97eSMatthias Ringwald data = gatt_helper->characteristic_buffer; 13032531c97eSMatthias Ringwald gatt_client_deserialize_characteristic(packet, 5, &characteristic); 13048d2f5935SMatthias Ringwald status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic, configuration); 13058d2f5935SMatthias Ringwald if (status){ 13068d2f5935SMatthias Ringwald send_gatt_query_complete(connection, gatt_helper->con_handle, status); 130724047dd6SMatthias Ringwald break; 13088d2f5935SMatthias Ringwald } 130924047dd6SMatthias Ringwald // ignore notification off 131024047dd6SMatthias Ringwald if (configuration == 0) break; 131124047dd6SMatthias Ringwald 131224047dd6SMatthias Ringwald // TODO: we assume it works 131324047dd6SMatthias Ringwald 131424047dd6SMatthias Ringwald // start listening 131524047dd6SMatthias Ringwald btstack_linked_list_gatt_client_notification_t * linked_notification = malloc(sizeof(btstack_linked_list_gatt_client_notification_t)); 131624047dd6SMatthias Ringwald if (!linked_notification) break; 1317c77e7c45SMatthias Ringwald memset(linked_notification, 0, sizeof(btstack_linked_list_gatt_client_notification_t)); 131824047dd6SMatthias Ringwald log_info("Start gatt notification listener %p", linked_notification); 131924047dd6SMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(&linked_notification->notification_listener, &handle_gatt_client_event, gatt_helper->con_handle, &characteristic); 132024047dd6SMatthias Ringwald btstack_linked_list_add(&gatt_helper->gatt_client_notifications, (btstack_linked_item_t *) linked_notification); 13212531c97eSMatthias Ringwald break; 13228d2f5935SMatthias Ringwald } 13232531c97eSMatthias Ringwald case GATT_GET_MTU: 13242531c97eSMatthias Ringwald handle = little_endian_read_16(packet, 3); 13252531c97eSMatthias Ringwald gatt_client_get_mtu(handle, &mtu); 13262531c97eSMatthias Ringwald send_gatt_mtu_event(connection, handle, mtu); 13272531c97eSMatthias Ringwald break; 13282531c97eSMatthias Ringwald #endif 13291edc4fc7SMatthias Ringwald #ifdef ENABLE_BLE 13301edc4fc7SMatthias Ringwald case SM_SET_AUTHENTICATION_REQUIREMENTS: 1331fb4ddcf3SMatthias Ringwald log_info("set auth %x", packet[3]); 13321edc4fc7SMatthias Ringwald sm_set_authentication_requirements(packet[3]); 13331edc4fc7SMatthias Ringwald break; 13341edc4fc7SMatthias Ringwald case SM_SET_IO_CAPABILITIES: 1335fb4ddcf3SMatthias Ringwald log_info("set io %x", packet[3]); 13361edc4fc7SMatthias Ringwald sm_set_io_capabilities(packet[3]); 13371edc4fc7SMatthias Ringwald break; 13381edc4fc7SMatthias Ringwald case SM_BONDING_DECLINE: 13391edc4fc7SMatthias Ringwald sm_bonding_decline(little_endian_read_16(packet, 3)); 13401edc4fc7SMatthias Ringwald break; 13411edc4fc7SMatthias Ringwald case SM_JUST_WORKS_CONFIRM: 13421edc4fc7SMatthias Ringwald sm_just_works_confirm(little_endian_read_16(packet, 3)); 13431edc4fc7SMatthias Ringwald break; 13441edc4fc7SMatthias Ringwald case SM_NUMERIC_COMPARISON_CONFIRM: 13451edc4fc7SMatthias Ringwald sm_numeric_comparison_confirm(little_endian_read_16(packet, 3)); 13461edc4fc7SMatthias Ringwald break; 13471edc4fc7SMatthias Ringwald case SM_PASSKEY_INPUT: 13481edc4fc7SMatthias Ringwald sm_passkey_input(little_endian_read_16(packet, 3), little_endian_read_32(packet, 5)); 13491edc4fc7SMatthias Ringwald break; 13501edc4fc7SMatthias Ringwald #endif 13512531c97eSMatthias Ringwald default: 13522531c97eSMatthias Ringwald log_error("Error: command %u not implemented:", READ_CMD_OCF(packet)); 13532531c97eSMatthias Ringwald break; 13542531c97eSMatthias Ringwald } 13552531c97eSMatthias Ringwald 13562531c97eSMatthias Ringwald return 0; 13572531c97eSMatthias Ringwald } 13582531c97eSMatthias Ringwald 13592531c97eSMatthias Ringwald static int daemon_client_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length){ 13602531c97eSMatthias Ringwald 13612531c97eSMatthias Ringwald int err = 0; 13622531c97eSMatthias Ringwald client_state_t * client; 13632531c97eSMatthias Ringwald 13642531c97eSMatthias Ringwald switch (packet_type){ 13652531c97eSMatthias Ringwald case HCI_COMMAND_DATA_PACKET: 13662531c97eSMatthias Ringwald if (READ_CMD_OGF(data) != OGF_BTSTACK) { 13672531c97eSMatthias Ringwald // HCI Command 13682531c97eSMatthias Ringwald hci_send_cmd_packet(data, length); 13692531c97eSMatthias Ringwald } else { 13702531c97eSMatthias Ringwald // BTstack command 13712531c97eSMatthias Ringwald btstack_command_handler(connection, data, length); 13722531c97eSMatthias Ringwald } 13732531c97eSMatthias Ringwald break; 13742531c97eSMatthias Ringwald case L2CAP_DATA_PACKET: 13752531c97eSMatthias Ringwald // process l2cap packet... 13762531c97eSMatthias Ringwald err = l2cap_send(channel, data, length); 13772531c97eSMatthias Ringwald break; 13782531c97eSMatthias Ringwald case RFCOMM_DATA_PACKET: 13792531c97eSMatthias Ringwald // process l2cap packet... 13802531c97eSMatthias Ringwald err = rfcomm_send(channel, data, length); 13812531c97eSMatthias Ringwald break; 13822531c97eSMatthias Ringwald case DAEMON_EVENT_PACKET: 13832531c97eSMatthias Ringwald switch (data[0]) { 13842531c97eSMatthias Ringwald case DAEMON_EVENT_CONNECTION_OPENED: 13852531c97eSMatthias Ringwald log_info("DAEMON_EVENT_CONNECTION_OPENED %p\n",connection); 13862531c97eSMatthias Ringwald 1387af0d0df5SMatthias Ringwald client = calloc(sizeof(client_state_t), 1); 13882531c97eSMatthias Ringwald if (!client) break; // fail 13892531c97eSMatthias Ringwald client->connection = connection; 13902531c97eSMatthias Ringwald client->power_mode = HCI_POWER_OFF; 13912531c97eSMatthias Ringwald client->discoverable = 0; 13922531c97eSMatthias Ringwald btstack_linked_list_add(&clients, (btstack_linked_item_t *) client); 13932531c97eSMatthias Ringwald break; 13942531c97eSMatthias Ringwald case DAEMON_EVENT_CONNECTION_CLOSED: 13952531c97eSMatthias Ringwald log_info("DAEMON_EVENT_CONNECTION_CLOSED %p\n",connection); 13962531c97eSMatthias Ringwald daemon_disconnect_client(connection); 13972531c97eSMatthias Ringwald // no clients -> no HCI connections 13982531c97eSMatthias Ringwald if (!clients){ 13992531c97eSMatthias Ringwald hci_disconnect_all(); 14002531c97eSMatthias Ringwald } 14012531c97eSMatthias Ringwald 14022531c97eSMatthias Ringwald // update discoverable mode 14032531c97eSMatthias Ringwald gap_discoverable_control(clients_require_discoverable()); 14042531c97eSMatthias Ringwald // start power off, if last active client 14052531c97eSMatthias Ringwald if (!clients_require_power_on()){ 14062531c97eSMatthias Ringwald start_power_off_timer(); 14072531c97eSMatthias Ringwald } 14082531c97eSMatthias Ringwald break; 14092531c97eSMatthias Ringwald default: 14102531c97eSMatthias Ringwald break; 14112531c97eSMatthias Ringwald } 14122531c97eSMatthias Ringwald break; 14132531c97eSMatthias Ringwald } 14142531c97eSMatthias Ringwald if (err) { 14152531c97eSMatthias Ringwald log_info("Daemon Handler: err %d\n", err); 14162531c97eSMatthias Ringwald } 14172531c97eSMatthias Ringwald return err; 14182531c97eSMatthias Ringwald } 14192531c97eSMatthias Ringwald 14202531c97eSMatthias Ringwald 14212531c97eSMatthias Ringwald static void daemon_set_logging_enabled(int enabled){ 14222531c97eSMatthias Ringwald if (enabled && !loggingEnabled){ 14236cf7652eSMatthias Ringwald // construct path to log file 14246cf7652eSMatthias Ringwald switch (BTSTACK_LOG_TYPE){ 14256cf7652eSMatthias Ringwald case HCI_DUMP_STDOUT: 14266cf7652eSMatthias Ringwald snprintf(string_buffer, sizeof(string_buffer), "stdout"); 14276cf7652eSMatthias Ringwald break; 14286cf7652eSMatthias Ringwald case HCI_DUMP_PACKETLOGGER: 14296cf7652eSMatthias Ringwald snprintf(string_buffer, sizeof(string_buffer), "%s/hci_dump.pklg", btstack_server_storage_path); 14306cf7652eSMatthias Ringwald break; 14316cf7652eSMatthias Ringwald case HCI_DUMP_BLUEZ: 14326cf7652eSMatthias Ringwald snprintf(string_buffer, sizeof(string_buffer), "%s/hci_dump.snoop", btstack_server_storage_path); 14336cf7652eSMatthias Ringwald break; 14346cf7652eSMatthias Ringwald default: 14356cf7652eSMatthias Ringwald break; 14366cf7652eSMatthias Ringwald } 14376cf7652eSMatthias Ringwald hci_dump_open(string_buffer, BTSTACK_LOG_TYPE); 14386cf7652eSMatthias Ringwald printf("Logging to %s\n", string_buffer); 14392531c97eSMatthias Ringwald } 14402531c97eSMatthias Ringwald if (!enabled && loggingEnabled){ 14412531c97eSMatthias Ringwald hci_dump_close(); 14422531c97eSMatthias Ringwald } 14432531c97eSMatthias Ringwald loggingEnabled = enabled; 14442531c97eSMatthias Ringwald } 14452531c97eSMatthias Ringwald 14462531c97eSMatthias Ringwald // local cache used to manage UI status 14472531c97eSMatthias Ringwald static HCI_STATE hci_state = HCI_STATE_OFF; 14482531c97eSMatthias Ringwald static int num_connections = 0; 14492531c97eSMatthias Ringwald static void update_ui_status(void){ 14502531c97eSMatthias Ringwald if (hci_state != HCI_STATE_WORKING) { 14512531c97eSMatthias Ringwald bluetooth_status_handler(BLUETOOTH_OFF); 14522531c97eSMatthias Ringwald } else { 14532531c97eSMatthias Ringwald if (num_connections) { 14542531c97eSMatthias Ringwald bluetooth_status_handler(BLUETOOTH_ACTIVE); 14552531c97eSMatthias Ringwald } else { 14562531c97eSMatthias Ringwald bluetooth_status_handler(BLUETOOTH_ON); 14572531c97eSMatthias Ringwald } 14582531c97eSMatthias Ringwald } 14592531c97eSMatthias Ringwald } 14602531c97eSMatthias Ringwald 14612531c97eSMatthias Ringwald #ifdef USE_SPRINGBOARD 14622531c97eSMatthias Ringwald static void preferences_changed_callback(void){ 14632531c97eSMatthias Ringwald int logging = platform_iphone_logging_enabled(); 14642531c97eSMatthias Ringwald log_info("Logging enabled: %u\n", logging); 14652531c97eSMatthias Ringwald daemon_set_logging_enabled(logging); 14662531c97eSMatthias Ringwald } 14672531c97eSMatthias Ringwald #endif 14682531c97eSMatthias Ringwald 14692531c97eSMatthias Ringwald static void deamon_status_event_handler(uint8_t *packet, uint16_t size){ 14702531c97eSMatthias Ringwald 14712531c97eSMatthias Ringwald uint8_t update_status = 0; 14722531c97eSMatthias Ringwald 14732531c97eSMatthias Ringwald // handle state event 14740e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 14752531c97eSMatthias Ringwald case BTSTACK_EVENT_STATE: 14762531c97eSMatthias Ringwald hci_state = packet[2]; 14772531c97eSMatthias Ringwald log_info("New state: %u\n", hci_state); 14782531c97eSMatthias Ringwald update_status = 1; 14792531c97eSMatthias Ringwald break; 14802531c97eSMatthias Ringwald case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: 14812531c97eSMatthias Ringwald num_connections = packet[2]; 14822531c97eSMatthias Ringwald log_info("New nr connections: %u\n", num_connections); 14832531c97eSMatthias Ringwald update_status = 1; 14842531c97eSMatthias Ringwald break; 14852531c97eSMatthias Ringwald default: 14862531c97eSMatthias Ringwald break; 14872531c97eSMatthias Ringwald } 14882531c97eSMatthias Ringwald 14892531c97eSMatthias Ringwald // choose full bluetooth state 14902531c97eSMatthias Ringwald if (update_status) { 14912531c97eSMatthias Ringwald update_ui_status(); 14922531c97eSMatthias Ringwald } 14932531c97eSMatthias Ringwald } 14942531c97eSMatthias Ringwald 14952531c97eSMatthias Ringwald static void daemon_retry_parked(void){ 14962531c97eSMatthias Ringwald 14972531c97eSMatthias Ringwald // socket_connection_retry_parked is not reentrant 14982531c97eSMatthias Ringwald static int retry_mutex = 0; 14992531c97eSMatthias Ringwald 15002531c97eSMatthias Ringwald // lock mutex 15012531c97eSMatthias Ringwald if (retry_mutex) return; 15022531c97eSMatthias Ringwald retry_mutex = 1; 15032531c97eSMatthias Ringwald 15042531c97eSMatthias Ringwald // ... try sending again 15052531c97eSMatthias Ringwald socket_connection_retry_parked(); 15062531c97eSMatthias Ringwald 15072531c97eSMatthias Ringwald // unlock mutex 15082531c97eSMatthias Ringwald retry_mutex = 0; 15092531c97eSMatthias Ringwald } 15102531c97eSMatthias Ringwald 15112531c97eSMatthias Ringwald static void daemon_emit_packet(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 15122531c97eSMatthias Ringwald if (connection) { 15132531c97eSMatthias Ringwald socket_connection_send_packet(connection, packet_type, channel, packet, size); 15142531c97eSMatthias Ringwald } else { 15152531c97eSMatthias Ringwald socket_connection_send_packet_all(packet_type, channel, packet, size); 15162531c97eSMatthias Ringwald } 15172531c97eSMatthias Ringwald } 15182531c97eSMatthias Ringwald 15192370d9c1SMatthias Ringwald // copy from btstack_util, just using a '-' 15202370d9c1SMatthias Ringwald static char bd_addr_to_str_buffer[6*3]; // 12:45:78:01:34:67\0 15212370d9c1SMatthias Ringwald char * bd_addr_to_str_dashed(const bd_addr_t addr){ 15222370d9c1SMatthias Ringwald // orig code 15232370d9c1SMatthias Ringwald // sprintf(bd_addr_to_str_buffer, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 15242370d9c1SMatthias Ringwald // sprintf-free code 15252370d9c1SMatthias Ringwald char * p = bd_addr_to_str_buffer; 15262370d9c1SMatthias Ringwald int i; 15272370d9c1SMatthias Ringwald for (i = 0; i < 6 ; i++) { 15282370d9c1SMatthias Ringwald uint8_t byte = addr[i]; 15292370d9c1SMatthias Ringwald *p++ = char_for_nibble(byte >> 4); 15302370d9c1SMatthias Ringwald *p++ = char_for_nibble(byte & 0x0f); 15312370d9c1SMatthias Ringwald *p++ = '-'; 15322370d9c1SMatthias Ringwald } 15332370d9c1SMatthias Ringwald *--p = 0; 15342370d9c1SMatthias Ringwald return (char *) bd_addr_to_str_buffer; 15352370d9c1SMatthias Ringwald } 15362370d9c1SMatthias Ringwald 15372531c97eSMatthias Ringwald static uint8_t remote_name_event[2+1+6+DEVICE_NAME_LEN+1]; // +1 for \0 in log_info 15382531c97eSMatthias Ringwald static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 15392531c97eSMatthias Ringwald uint16_t cid; 15402531c97eSMatthias Ringwald int i; 15412531c97eSMatthias Ringwald bd_addr_t addr; 15422531c97eSMatthias Ringwald switch (packet_type) { 15432531c97eSMatthias Ringwald case HCI_EVENT_PACKET: 15442531c97eSMatthias Ringwald deamon_status_event_handler(packet, size); 15450e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 15462531c97eSMatthias Ringwald 1547a05b2457SMatthias Ringwald case BTSTACK_EVENT_STATE: 1548a05b2457SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 1549a05b2457SMatthias Ringwald if (tlv_setup_done) break; 1550e9b53984SMatthias Ringwald 1551a05b2457SMatthias Ringwald // setup TLV using local address as part of the name 1552a05b2457SMatthias Ringwald gap_local_bd_addr(addr); 1553a05b2457SMatthias Ringwald log_info("BTstack up and running at %s", bd_addr_to_str(addr)); 15542370d9c1SMatthias Ringwald snprintf(string_buffer, sizeof(string_buffer), "%s/btstack_%s.tlv", btstack_server_storage_path, bd_addr_to_str_dashed(addr)); 1555a05b2457SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, string_buffer); 1556a05b2457SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 1557a05b2457SMatthias Ringwald 1558e9b53984SMatthias Ringwald // setup link key db 1559a05b2457SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 1560a05b2457SMatthias Ringwald 1561a05b2457SMatthias Ringwald // init le device db to use TLV 1562a05b2457SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 1563a05b2457SMatthias Ringwald le_device_db_init(); 1564a05b2457SMatthias Ringwald le_device_db_set_local_bd_addr(addr); 1565a05b2457SMatthias Ringwald 1566a05b2457SMatthias Ringwald tlv_setup_done = 1; 1567a05b2457SMatthias Ringwald break; 1568a05b2457SMatthias Ringwald 15692531c97eSMatthias Ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 15702531c97eSMatthias Ringwald // ACL buffer freed... 15712531c97eSMatthias Ringwald daemon_retry_parked(); 15722531c97eSMatthias Ringwald // no need to tell clients 15732531c97eSMatthias Ringwald return; 15742531c97eSMatthias Ringwald 15752531c97eSMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 15762531c97eSMatthias Ringwald if (!btstack_device_name_db) break; 15772531c97eSMatthias Ringwald if (packet[2]) break; // status not ok 15782531c97eSMatthias Ringwald 15792531c97eSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 15802531c97eSMatthias Ringwald // fix for invalid remote names - terminate on 0xff 15812531c97eSMatthias Ringwald for (i=0; i<248;i++){ 15822531c97eSMatthias Ringwald if (packet[9+i] == 0xff){ 15832531c97eSMatthias Ringwald packet[9+i] = 0; 15842531c97eSMatthias Ringwald break; 15852531c97eSMatthias Ringwald } 15862531c97eSMatthias Ringwald } 15872531c97eSMatthias Ringwald packet[9+248] = 0; 15882531c97eSMatthias Ringwald btstack_device_name_db->put_name(addr, (device_name_t *)&packet[9]); 15892531c97eSMatthias Ringwald break; 15902531c97eSMatthias Ringwald 15912531c97eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 15922531c97eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 15932531c97eSMatthias Ringwald if (!btstack_device_name_db) break; 15942531c97eSMatthias Ringwald 15952531c97eSMatthias Ringwald // first send inq result packet 15962531c97eSMatthias Ringwald daemon_emit_packet(connection, packet_type, channel, packet, size); 15972531c97eSMatthias Ringwald 15982531c97eSMatthias Ringwald // then send cached remote names 15992531c97eSMatthias Ringwald int offset = 3; 16002531c97eSMatthias Ringwald for (i=0; i<packet[2];i++){ 16012531c97eSMatthias Ringwald reverse_bd_addr(&packet[offset], addr); 16022531c97eSMatthias Ringwald if (btstack_device_name_db->get_name(addr, (device_name_t *) &remote_name_event[9])){ 16032531c97eSMatthias Ringwald remote_name_event[0] = DAEMON_EVENT_REMOTE_NAME_CACHED; 16042531c97eSMatthias Ringwald remote_name_event[1] = sizeof(remote_name_event) - 2 - 1; 16052531c97eSMatthias Ringwald remote_name_event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 16062531c97eSMatthias Ringwald reverse_bd_addr(addr, &remote_name_event[3]); 16072531c97eSMatthias Ringwald 16082531c97eSMatthias Ringwald remote_name_event[9+248] = 0; // assert \0 for log_info 16092531c97eSMatthias Ringwald log_info("DAEMON_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(addr), &remote_name_event[9]); 16102531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, remote_name_event, sizeof(remote_name_event)-1); 16112531c97eSMatthias Ringwald daemon_emit_packet(connection, HCI_EVENT_PACKET, channel, remote_name_event, sizeof(remote_name_event) -1); 16122531c97eSMatthias Ringwald } 16132531c97eSMatthias Ringwald offset += 14; // 6 + 1 + 1 + 1 + 3 + 2; 16142531c97eSMatthias Ringwald } 16152531c97eSMatthias Ringwald return; 16162531c97eSMatthias Ringwald } 16172531c97eSMatthias Ringwald 161862c64df1SMatthias Ringwald case DAEMON_EVENT_RFCOMM_CREDITS: 16192531c97eSMatthias Ringwald // RFCOMM CREDITS received... 16202531c97eSMatthias Ringwald daemon_retry_parked(); 16212531c97eSMatthias Ringwald break; 16222531c97eSMatthias Ringwald 1623f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 16242531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 13); 16252531c97eSMatthias Ringwald connection = connection_for_rfcomm_cid(cid); 16262531c97eSMatthias Ringwald if (!connection) break; 16272531c97eSMatthias Ringwald if (packet[2]) { 16282531c97eSMatthias Ringwald daemon_remove_client_rfcomm_channel(connection, cid); 16292531c97eSMatthias Ringwald } else { 16302531c97eSMatthias Ringwald daemon_add_client_rfcomm_channel(connection, cid); 16312531c97eSMatthias Ringwald } 16322531c97eSMatthias Ringwald break; 16332531c97eSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 16342531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 2); 16352531c97eSMatthias Ringwald connection = connection_for_rfcomm_cid(cid); 16362531c97eSMatthias Ringwald if (!connection) break; 16372531c97eSMatthias Ringwald daemon_remove_client_rfcomm_channel(connection, cid); 16382531c97eSMatthias Ringwald break; 163962c64df1SMatthias Ringwald case DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED: 16402531c97eSMatthias Ringwald if (packet[2]) break; 16412531c97eSMatthias Ringwald daemon_add_client_rfcomm_service(connection, packet[3]); 16422531c97eSMatthias Ringwald break; 16432531c97eSMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 16442531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 13); 16452531c97eSMatthias Ringwald connection = connection_for_l2cap_cid(cid); 16462531c97eSMatthias Ringwald if (!connection) break; 16472531c97eSMatthias Ringwald if (packet[2]) { 16482531c97eSMatthias Ringwald daemon_remove_client_l2cap_channel(connection, cid); 16492531c97eSMatthias Ringwald } else { 16502531c97eSMatthias Ringwald daemon_add_client_l2cap_channel(connection, cid); 16512531c97eSMatthias Ringwald } 16522531c97eSMatthias Ringwald break; 16532531c97eSMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 16542531c97eSMatthias Ringwald cid = little_endian_read_16(packet, 2); 16552531c97eSMatthias Ringwald connection = connection_for_l2cap_cid(cid); 16562531c97eSMatthias Ringwald if (!connection) break; 16572531c97eSMatthias Ringwald daemon_remove_client_l2cap_channel(connection, cid); 16582531c97eSMatthias Ringwald break; 16592531c97eSMatthias Ringwald #if defined(ENABLE_BLE) && defined(HAVE_MALLOC) 16602531c97eSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1661712999cdSMatthias Ringwald daemon_remove_gatt_client_helper(little_endian_read_16(packet, 3)); 16622531c97eSMatthias Ringwald break; 16632531c97eSMatthias Ringwald #endif 16642531c97eSMatthias Ringwald default: 16652531c97eSMatthias Ringwald break; 16662531c97eSMatthias Ringwald } 16672531c97eSMatthias Ringwald break; 16682531c97eSMatthias Ringwald case L2CAP_DATA_PACKET: 16692531c97eSMatthias Ringwald connection = connection_for_l2cap_cid(channel); 16702531c97eSMatthias Ringwald if (!connection) return; 16712531c97eSMatthias Ringwald break; 16722531c97eSMatthias Ringwald case RFCOMM_DATA_PACKET: 16732531c97eSMatthias Ringwald connection = connection_for_l2cap_cid(channel); 16742531c97eSMatthias Ringwald if (!connection) return; 16752531c97eSMatthias Ringwald break; 16762531c97eSMatthias Ringwald default: 16772531c97eSMatthias Ringwald break; 16782531c97eSMatthias Ringwald } 16792531c97eSMatthias Ringwald 16802531c97eSMatthias Ringwald daemon_emit_packet(connection, packet_type, channel, packet, size); 16812531c97eSMatthias Ringwald } 16822531c97eSMatthias Ringwald 16831edc4fc7SMatthias Ringwald static void stack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ 16842531c97eSMatthias Ringwald daemon_packet_handler(NULL, packet_type, channel, packet, size); 16852531c97eSMatthias Ringwald } 16862531c97eSMatthias Ringwald 16872531c97eSMatthias Ringwald static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 16880e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 16892531c97eSMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 16902531c97eSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 16912531c97eSMatthias Ringwald // already HCI Events, just forward them 16922531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 16932531c97eSMatthias Ringwald socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, size); 16942531c97eSMatthias Ringwald break; 16952531c97eSMatthias Ringwald default: 16962531c97eSMatthias Ringwald break; 16972531c97eSMatthias Ringwald } 16982531c97eSMatthias Ringwald } 16992531c97eSMatthias Ringwald 17002531c97eSMatthias Ringwald static void sdp_client_assert_buffer(int size){ 17012531c97eSMatthias Ringwald if (size > attribute_value_buffer_size){ 17022531c97eSMatthias Ringwald log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size); 17032531c97eSMatthias Ringwald } 17042531c97eSMatthias Ringwald } 17052531c97eSMatthias Ringwald 17062531c97eSMatthias Ringwald // define new packet type SDP_CLIENT_PACKET 17072531c97eSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 17082531c97eSMatthias Ringwald int event_len; 17092531c97eSMatthias Ringwald 17100e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 17112531c97eSMatthias Ringwald case SDP_EVENT_QUERY_ATTRIBUTE_BYTE: 17122531c97eSMatthias Ringwald sdp_client_assert_buffer(sdp_event_query_attribute_byte_get_attribute_length(packet)); 17132531c97eSMatthias Ringwald attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 17142531c97eSMatthias Ringwald if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){ 17152531c97eSMatthias Ringwald log_info_hexdump(attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet)); 17162531c97eSMatthias Ringwald 17172531c97eSMatthias Ringwald int event_len = 1 + 3 * 2 + sdp_event_query_attribute_byte_get_attribute_length(packet); 17182531c97eSMatthias Ringwald uint8_t event[event_len]; 17192531c97eSMatthias Ringwald event[0] = SDP_EVENT_QUERY_ATTRIBUTE_VALUE; 17202531c97eSMatthias Ringwald little_endian_store_16(event, 1, sdp_event_query_attribute_byte_get_record_id(packet)); 17212531c97eSMatthias Ringwald little_endian_store_16(event, 3, sdp_event_query_attribute_byte_get_attribute_id(packet)); 17222531c97eSMatthias Ringwald little_endian_store_16(event, 5, (uint16_t)sdp_event_query_attribute_byte_get_attribute_length(packet)); 17232531c97eSMatthias Ringwald memcpy(&event[7], attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet)); 17242531c97eSMatthias Ringwald hci_dump_packet(SDP_CLIENT_PACKET, 0, event, event_len); 17252531c97eSMatthias Ringwald socket_connection_send_packet(sdp_client_query_connection, SDP_CLIENT_PACKET, 0, event, event_len); 17262531c97eSMatthias Ringwald } 17272531c97eSMatthias Ringwald break; 17282531c97eSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 17292531c97eSMatthias Ringwald event_len = packet[1] + 2; 17302531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, event_len); 17312531c97eSMatthias Ringwald socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, event_len); 17322531c97eSMatthias Ringwald break; 17332531c97eSMatthias Ringwald } 17342531c97eSMatthias Ringwald } 17352531c97eSMatthias Ringwald 17362531c97eSMatthias Ringwald static void power_notification_callback(POWER_NOTIFICATION_t notification){ 17372531c97eSMatthias Ringwald switch (notification) { 17382531c97eSMatthias Ringwald case POWER_WILL_SLEEP: 17392531c97eSMatthias Ringwald // let's sleep 17402531c97eSMatthias Ringwald power_management_sleep = 1; 17412531c97eSMatthias Ringwald hci_power_control(HCI_POWER_SLEEP); 17422531c97eSMatthias Ringwald break; 17432531c97eSMatthias Ringwald case POWER_WILL_WAKE_UP: 17442531c97eSMatthias Ringwald // assume that all clients use Bluetooth -> if connection, start Bluetooth 17452531c97eSMatthias Ringwald power_management_sleep = 0; 17462531c97eSMatthias Ringwald if (clients_require_power_on()) { 17472531c97eSMatthias Ringwald hci_power_control(HCI_POWER_ON); 17482531c97eSMatthias Ringwald } 17492531c97eSMatthias Ringwald break; 17502531c97eSMatthias Ringwald default: 17512531c97eSMatthias Ringwald break; 17522531c97eSMatthias Ringwald } 17532531c97eSMatthias Ringwald } 17542531c97eSMatthias Ringwald 17552531c97eSMatthias Ringwald static void daemon_sigint_handler(int param){ 17562531c97eSMatthias Ringwald 17572531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 17582531c97eSMatthias Ringwald // notify daemons 17592531c97eSMatthias Ringwald notify_post("ch.ringwald.btstack.stopped"); 17602531c97eSMatthias Ringwald #endif 17612531c97eSMatthias Ringwald 17622531c97eSMatthias Ringwald log_info(" <= SIGINT received, shutting down..\n"); 17632531c97eSMatthias Ringwald 1764948e8bfeSMatthias Ringwald int send_power_off = 1; 1765948e8bfeSMatthias Ringwald #ifdef HAVE_INTEL_USB 1766948e8bfeSMatthias Ringwald // power off and close only if hci was initialized before 1767948e8bfeSMatthias Ringwald send_power_off = intel_firmware_loaded; 1768948e8bfeSMatthias Ringwald #endif 1769948e8bfeSMatthias Ringwald 1770948e8bfeSMatthias Ringwald if (send_power_off){ 17712531c97eSMatthias Ringwald hci_power_control( HCI_POWER_OFF); 17722531c97eSMatthias Ringwald hci_close(); 1773948e8bfeSMatthias Ringwald } 17742531c97eSMatthias Ringwald 17752531c97eSMatthias Ringwald log_info("Good bye, see you.\n"); 17762531c97eSMatthias Ringwald 17772531c97eSMatthias Ringwald exit(0); 17782531c97eSMatthias Ringwald } 17792531c97eSMatthias Ringwald 17802531c97eSMatthias Ringwald // MARK: manage power off timer 17812531c97eSMatthias Ringwald 17822531c97eSMatthias Ringwald #define USE_POWER_OFF_TIMER 17832531c97eSMatthias Ringwald 17842531c97eSMatthias Ringwald static void stop_power_off_timer(void){ 17852531c97eSMatthias Ringwald #ifdef USE_POWER_OFF_TIMER 17862531c97eSMatthias Ringwald if (timeout_active) { 17872531c97eSMatthias Ringwald btstack_run_loop_remove_timer(&timeout); 17882531c97eSMatthias Ringwald timeout_active = 0; 17892531c97eSMatthias Ringwald } 17902531c97eSMatthias Ringwald #endif 17912531c97eSMatthias Ringwald } 17922531c97eSMatthias Ringwald 17932531c97eSMatthias Ringwald static void start_power_off_timer(void){ 17942531c97eSMatthias Ringwald #ifdef USE_POWER_OFF_TIMER 17952531c97eSMatthias Ringwald stop_power_off_timer(); 17962531c97eSMatthias Ringwald btstack_run_loop_set_timer(&timeout, DAEMON_NO_ACTIVE_CLIENT_TIMEOUT); 17972531c97eSMatthias Ringwald btstack_run_loop_add_timer(&timeout); 17982531c97eSMatthias Ringwald timeout_active = 1; 17992531c97eSMatthias Ringwald #else 18002531c97eSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 18012531c97eSMatthias Ringwald #endif 18022531c97eSMatthias Ringwald } 18032531c97eSMatthias Ringwald 18042531c97eSMatthias Ringwald // MARK: manage list of clients 18052531c97eSMatthias Ringwald 18062531c97eSMatthias Ringwald 18072531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection) { 18082531c97eSMatthias Ringwald btstack_linked_item_t *it; 18092531c97eSMatthias Ringwald for (it = (btstack_linked_item_t *) clients; it ; it = it->next){ 18102531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) it; 18112531c97eSMatthias Ringwald if (client_state->connection == connection) { 18122531c97eSMatthias Ringwald return client_state; 18132531c97eSMatthias Ringwald } 18142531c97eSMatthias Ringwald } 18152531c97eSMatthias Ringwald return NULL; 18162531c97eSMatthias Ringwald } 18172531c97eSMatthias Ringwald 18182531c97eSMatthias Ringwald static void clients_clear_power_request(void){ 18192531c97eSMatthias Ringwald btstack_linked_item_t *it; 18202531c97eSMatthias Ringwald for (it = (btstack_linked_item_t *) clients; it ; it = it->next){ 18212531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) it; 18222531c97eSMatthias Ringwald client_state->power_mode = HCI_POWER_OFF; 18232531c97eSMatthias Ringwald } 18242531c97eSMatthias Ringwald } 18252531c97eSMatthias Ringwald 18262531c97eSMatthias Ringwald static int clients_require_power_on(void){ 18272531c97eSMatthias Ringwald 18282531c97eSMatthias Ringwald if (global_enable) return 1; 18292531c97eSMatthias Ringwald 18302531c97eSMatthias Ringwald btstack_linked_item_t *it; 18312531c97eSMatthias Ringwald for (it = (btstack_linked_item_t *) clients; it ; it = it->next){ 18322531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) it; 18332531c97eSMatthias Ringwald if (client_state->power_mode == HCI_POWER_ON) { 18342531c97eSMatthias Ringwald return 1; 18352531c97eSMatthias Ringwald } 18362531c97eSMatthias Ringwald } 18372531c97eSMatthias Ringwald return 0; 18382531c97eSMatthias Ringwald } 18392531c97eSMatthias Ringwald 18402531c97eSMatthias Ringwald static int clients_require_discoverable(void){ 18412531c97eSMatthias Ringwald btstack_linked_item_t *it; 18422531c97eSMatthias Ringwald for (it = (btstack_linked_item_t *) clients; it ; it = it->next){ 18432531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) it; 18442531c97eSMatthias Ringwald if (client_state->discoverable) { 18452531c97eSMatthias Ringwald return 1; 18462531c97eSMatthias Ringwald } 18472531c97eSMatthias Ringwald } 18482531c97eSMatthias Ringwald return 0; 18492531c97eSMatthias Ringwald } 18502531c97eSMatthias Ringwald 18512531c97eSMatthias Ringwald static void usage(const char * name) { 18522531c97eSMatthias Ringwald printf("%s, BTstack background daemon\n", name); 185385fb0500SMatthias Ringwald printf("usage: %s [--help] [--tcp]\n", name); 18542531c97eSMatthias Ringwald printf(" --help display this usage\n"); 18552531c97eSMatthias Ringwald printf(" --tcp use TCP server on port %u\n", BTSTACK_PORT); 18566cf7652eSMatthias Ringwald printf("Without the --tcp option, BTstack Server is listening on unix domain socket %s\n\n", BTSTACK_UNIX); 18572531c97eSMatthias Ringwald } 18582531c97eSMatthias Ringwald 18592531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 18602531c97eSMatthias Ringwald static void * btstack_run_loop_thread(void *context){ 18612531c97eSMatthias Ringwald btstack_run_loop_execute(); 18622531c97eSMatthias Ringwald return NULL; 18632531c97eSMatthias Ringwald } 18642531c97eSMatthias Ringwald #endif 18652531c97eSMatthias Ringwald 18662531c97eSMatthias Ringwald #ifdef ENABLE_BLE 18672531c97eSMatthias Ringwald 18682531c97eSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ 18692531c97eSMatthias Ringwald 18702531c97eSMatthias Ringwald // only handle GATT Events 18710e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 18722531c97eSMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 18732531c97eSMatthias Ringwald case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT: 18742531c97eSMatthias Ringwald case GATT_EVENT_NOTIFICATION: 18752531c97eSMatthias Ringwald case GATT_EVENT_INDICATION: 18762531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 18772531c97eSMatthias Ringwald case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 18782531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 18792531c97eSMatthias Ringwald case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 18802531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 18812531c97eSMatthias Ringwald case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT: 18822531c97eSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 18832531c97eSMatthias Ringwald break; 18842531c97eSMatthias Ringwald default: 18852531c97eSMatthias Ringwald return; 18862531c97eSMatthias Ringwald } 18872531c97eSMatthias Ringwald 1888711e6c80SMatthias Ringwald hci_con_handle_t con_handle = little_endian_read_16(packet, 2); 18892531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * gatt_client_helper = daemon_get_gatt_client_helper(con_handle); 18902531c97eSMatthias Ringwald if (!gatt_client_helper){ 18912531c97eSMatthias Ringwald log_info("daemon handle_gatt_client_event: gc helper for handle 0x%2x is NULL.", con_handle); 18922531c97eSMatthias Ringwald return; 18932531c97eSMatthias Ringwald } 18942531c97eSMatthias Ringwald 18952531c97eSMatthias Ringwald connection_t *connection = NULL; 18962531c97eSMatthias Ringwald 18972531c97eSMatthias Ringwald // daemon doesn't track which connection subscribed to this particular handle, so we just notify all connections 18980e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 18992531c97eSMatthias Ringwald case GATT_EVENT_NOTIFICATION: 19002531c97eSMatthias Ringwald case GATT_EVENT_INDICATION:{ 19012531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 19022531c97eSMatthias Ringwald 19032531c97eSMatthias Ringwald btstack_linked_item_t *it; 19042531c97eSMatthias Ringwald for (it = (btstack_linked_item_t *) clients; it ; it = it->next){ 19052531c97eSMatthias Ringwald client_state_t * client_state = (client_state_t *) it; 19062531c97eSMatthias Ringwald socket_connection_send_packet(client_state->connection, HCI_EVENT_PACKET, 0, packet, size); 19072531c97eSMatthias Ringwald } 19082531c97eSMatthias Ringwald return; 19092531c97eSMatthias Ringwald } 19102531c97eSMatthias Ringwald default: 19112531c97eSMatthias Ringwald break; 19122531c97eSMatthias Ringwald } 19132531c97eSMatthias Ringwald 19142531c97eSMatthias Ringwald // otherwise, we have to have an active connection 19152531c97eSMatthias Ringwald connection = gatt_client_helper->active_connection; 19162531c97eSMatthias Ringwald uint16_t offset; 19172531c97eSMatthias Ringwald uint16_t length; 19182531c97eSMatthias Ringwald 19192531c97eSMatthias Ringwald if (!connection) return; 19202531c97eSMatthias Ringwald 19210e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 19222531c97eSMatthias Ringwald 19232531c97eSMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 19242531c97eSMatthias Ringwald case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT: 19252531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 19262531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 19272531c97eSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 19282531c97eSMatthias Ringwald case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 19292531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 19302531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size); 19312531c97eSMatthias Ringwald break; 19322531c97eSMatthias Ringwald 19332531c97eSMatthias Ringwald case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT: 19342531c97eSMatthias Ringwald case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 19352531c97eSMatthias Ringwald offset = little_endian_read_16(packet, 6); 19362531c97eSMatthias Ringwald length = little_endian_read_16(packet, 8); 19370e2df43fSMatthias Ringwald gatt_client_helper->characteristic_buffer[0] = hci_event_packet_get_type(packet); // store type (characteristic/descriptor) 19382531c97eSMatthias Ringwald gatt_client_helper->characteristic_handle = little_endian_read_16(packet, 4); // store attribute handle 19392531c97eSMatthias Ringwald gatt_client_helper->characteristic_length = offset + length; // update length 19402531c97eSMatthias Ringwald memcpy(&gatt_client_helper->characteristic_buffer[10 + offset], &packet[10], length); 19412531c97eSMatthias Ringwald break; 19422531c97eSMatthias Ringwald 19432531c97eSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE:{ 19442531c97eSMatthias Ringwald gatt_client_helper->active_connection = NULL; 19452531c97eSMatthias Ringwald if (gatt_client_helper->characteristic_length){ 19462531c97eSMatthias Ringwald // send re-combined long characteristic value or long characteristic descriptor value 19472531c97eSMatthias Ringwald uint8_t * event = gatt_client_helper->characteristic_buffer; 19482531c97eSMatthias Ringwald uint16_t event_size = 10 + gatt_client_helper->characteristic_length; 19492531c97eSMatthias Ringwald // event[0] == already set by previsous case 19502531c97eSMatthias Ringwald event[1] = 8 + gatt_client_helper->characteristic_length; 19512531c97eSMatthias Ringwald little_endian_store_16(event, 2, little_endian_read_16(packet, 2)); 19522531c97eSMatthias Ringwald little_endian_store_16(event, 4, gatt_client_helper->characteristic_handle); 19532531c97eSMatthias Ringwald little_endian_store_16(event, 6, 0); // offset 19542531c97eSMatthias Ringwald little_endian_store_16(event, 8, gatt_client_helper->characteristic_length); 19552531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, event_size); 19562531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, event_size); 19572531c97eSMatthias Ringwald gatt_client_helper->characteristic_length = 0; 19582531c97eSMatthias Ringwald } 19592531c97eSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 19602531c97eSMatthias Ringwald socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size); 19612531c97eSMatthias Ringwald break; 19622531c97eSMatthias Ringwald } 19632531c97eSMatthias Ringwald default: 19642531c97eSMatthias Ringwald break; 19652531c97eSMatthias Ringwald } 19662531c97eSMatthias Ringwald } 19672531c97eSMatthias Ringwald #endif 19682531c97eSMatthias Ringwald 19692531c97eSMatthias Ringwald static char hostname[30]; 19702531c97eSMatthias Ringwald 19718129e542SMatthias Ringwald static void btstack_server_configure_stack(void){ 19724252c4d7SMatthias Ringwald // init HCI 19734252c4d7SMatthias Ringwald hci_init(transport, config); 19744252c4d7SMatthias Ringwald if (btstack_link_key_db){ 19754252c4d7SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 19764252c4d7SMatthias Ringwald } 19774252c4d7SMatthias Ringwald if (control){ 19784252c4d7SMatthias Ringwald hci_set_control(control); 19794252c4d7SMatthias Ringwald } 19804252c4d7SMatthias Ringwald 19814252c4d7SMatthias Ringwald // hostname for POSIX systems 19824252c4d7SMatthias Ringwald gethostname(hostname, 30); 19834252c4d7SMatthias Ringwald hostname[29] = '\0'; 19844252c4d7SMatthias Ringwald gap_set_local_name(hostname); 19854252c4d7SMatthias Ringwald 19864252c4d7SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 19874252c4d7SMatthias Ringwald // iPhone doesn't use SSP yet as there's no UI for it yet and auto accept is not an option 19884252c4d7SMatthias Ringwald gap_ssp_set_enable(0); 19894252c4d7SMatthias Ringwald #endif 19904252c4d7SMatthias Ringwald 19914252c4d7SMatthias Ringwald // register for HCI events 19924252c4d7SMatthias Ringwald hci_event_callback_registration.callback = &stack_packet_handler; 19934252c4d7SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 19944252c4d7SMatthias Ringwald 19954252c4d7SMatthias Ringwald // init L2CAP 19964252c4d7SMatthias Ringwald l2cap_init(); 19974252c4d7SMatthias Ringwald l2cap_register_packet_handler(&stack_packet_handler); 19984252c4d7SMatthias Ringwald timeout.process = daemon_no_connections_timeout; 19994252c4d7SMatthias Ringwald 20004252c4d7SMatthias Ringwald #ifdef ENABLE_RFCOMM 20014252c4d7SMatthias Ringwald log_info("config.h: ENABLE_RFCOMM\n"); 20024252c4d7SMatthias Ringwald rfcomm_init(); 20034252c4d7SMatthias Ringwald #endif 20044252c4d7SMatthias Ringwald 20054252c4d7SMatthias Ringwald #ifdef ENABLE_SDP 20064252c4d7SMatthias Ringwald sdp_init(); 20074252c4d7SMatthias Ringwald #endif 20084252c4d7SMatthias Ringwald 20094252c4d7SMatthias Ringwald #ifdef ENABLE_BLE 20104252c4d7SMatthias Ringwald sm_init(); 20114252c4d7SMatthias Ringwald sm_event_callback_registration.callback = &stack_packet_handler; 20124252c4d7SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 20134252c4d7SMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 20144252c4d7SMatthias Ringwald // sm_set_authentication_requirements( SM_AUTHREQ_BONDING | SM_AUTHREQ_MITM_PROTECTION); 20154252c4d7SMatthias Ringwald 20164252c4d7SMatthias Ringwald // GATT Client 20174252c4d7SMatthias Ringwald gatt_client_init(); 20184252c4d7SMatthias Ringwald 20194252c4d7SMatthias Ringwald // GATT Server - empty attribute database 20204252c4d7SMatthias Ringwald att_server_init(NULL, NULL, NULL); 20214252c4d7SMatthias Ringwald 20224252c4d7SMatthias Ringwald #endif 20234252c4d7SMatthias Ringwald } 20244252c4d7SMatthias Ringwald 2025ee252d54SMatthias Ringwald int btstack_server_run(int tcp_flag){ 202642963eceSMatthias Ringwald 20272531c97eSMatthias Ringwald if (tcp_flag){ 20286cf7652eSMatthias Ringwald printf("BTstack Server started on port %u\n", BTSTACK_PORT); 20292531c97eSMatthias Ringwald } else { 20306cf7652eSMatthias Ringwald printf("BTstack Server started on socket %s\n", BTSTACK_UNIX); 20316cf7652eSMatthias Ringwald } 20326cf7652eSMatthias Ringwald 20336cf7652eSMatthias Ringwald // handle default init 20346cf7652eSMatthias Ringwald if (!btstack_server_storage_path){ 2035fb4ddcf3SMatthias Ringwald #ifdef _WIN32 2036b6a1d731SMatthias Ringwald btstack_server_storage_path = strdup("."); 2037fb4ddcf3SMatthias Ringwald #else 20386cf7652eSMatthias Ringwald btstack_server_storage_path = strdup("/tmp"); 2039fb4ddcf3SMatthias Ringwald #endif 20402531c97eSMatthias Ringwald } 20412531c97eSMatthias Ringwald 20422531c97eSMatthias Ringwald // make stdout unbuffered 20432531c97eSMatthias Ringwald setbuf(stdout, NULL); 20442531c97eSMatthias Ringwald 20452531c97eSMatthias Ringwald // handle CTRL-c 20462531c97eSMatthias Ringwald signal(SIGINT, daemon_sigint_handler); 20472531c97eSMatthias Ringwald // handle SIGTERM - suggested for launchd 20482531c97eSMatthias Ringwald signal(SIGTERM, daemon_sigint_handler); 20492531c97eSMatthias Ringwald 20509490bce5SMatthias Ringwald socket_connection_init(); 20512531c97eSMatthias Ringwald 20522531c97eSMatthias Ringwald btstack_control_t * control = NULL; 2053a046efa6SMatthias Ringwald const btstack_uart_block_t * uart_block_implementation = NULL; 2054a046efa6SMatthias Ringwald (void) uart_block_implementation; 20552531c97eSMatthias Ringwald 20562531c97eSMatthias Ringwald #ifdef HAVE_TRANSPORT_H4 20572531c97eSMatthias Ringwald hci_transport_config_uart.type = HCI_TRANSPORT_CONFIG_UART; 20582531c97eSMatthias Ringwald hci_transport_config_uart.baudrate_init = UART_SPEED; 20592531c97eSMatthias Ringwald hci_transport_config_uart.baudrate_main = 0; 20602531c97eSMatthias Ringwald hci_transport_config_uart.flowcontrol = 1; 20612531c97eSMatthias Ringwald hci_transport_config_uart.device_name = UART_DEVICE; 2062a046efa6SMatthias Ringwald 2063a046efa6SMatthias Ringwald #ifndef HAVE_PLATFORM_IPHONE_OS 2064b9dcd1ccSMatthias Ringwald #ifdef _WIN32 2065b9dcd1ccSMatthias Ringwald uart_block_implementation = btstack_uart_block_windows_instance(); 2066b9dcd1ccSMatthias Ringwald #else 2067a046efa6SMatthias Ringwald uart_block_implementation = btstack_uart_block_posix_instance(); 2068a046efa6SMatthias Ringwald #endif 2069b9dcd1ccSMatthias Ringwald #endif 20702531c97eSMatthias Ringwald 20712531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 20722531c97eSMatthias Ringwald // use default (max) UART baudrate over netgraph interface 20732531c97eSMatthias Ringwald hci_transport_config_uart.baudrate_init = 0; 20742531c97eSMatthias Ringwald #endif 20752531c97eSMatthias Ringwald 20762531c97eSMatthias Ringwald config = &hci_transport_config_uart; 2077a046efa6SMatthias Ringwald transport = hci_transport_h4_instance(uart_block_implementation); 20782531c97eSMatthias Ringwald #endif 20792531c97eSMatthias Ringwald 20802531c97eSMatthias Ringwald #ifdef HAVE_TRANSPORT_USB 20812531c97eSMatthias Ringwald transport = hci_transport_usb_instance(); 20822531c97eSMatthias Ringwald #endif 20832531c97eSMatthias Ringwald 20842531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 20852531c97eSMatthias Ringwald control = &btstack_control_iphone; 20862531c97eSMatthias Ringwald if (btstack_control_iphone_power_management_supported()){ 20872531c97eSMatthias Ringwald hci_transport_h4_iphone_set_enforce_wake_device("/dev/btwake"); 20882531c97eSMatthias Ringwald } 20892531c97eSMatthias Ringwald bluetooth_status_handler = platform_iphone_status_handler; 20902531c97eSMatthias Ringwald platform_iphone_register_window_manager_restart(update_ui_status); 20912531c97eSMatthias Ringwald platform_iphone_register_preferences_changed(preferences_changed_callback); 20922531c97eSMatthias Ringwald #endif 20932531c97eSMatthias Ringwald 20942531c97eSMatthias Ringwald #ifdef BTSTACK_DEVICE_NAME_DB_INSTANCE 20952531c97eSMatthias Ringwald btstack_device_name_db = BTSTACK_DEVICE_NAME_DB_INSTANCE(); 20962531c97eSMatthias Ringwald #endif 20972531c97eSMatthias Ringwald 2098b9dcd1ccSMatthias Ringwald #ifdef _WIN32 2099b9dcd1ccSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 2100b9dcd1ccSMatthias Ringwald #else 21012531c97eSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 2102b9dcd1ccSMatthias Ringwald #endif 21032531c97eSMatthias Ringwald 21042531c97eSMatthias Ringwald // init power management notifications 21052531c97eSMatthias Ringwald if (control && control->register_for_power_notifications){ 21062531c97eSMatthias Ringwald control->register_for_power_notifications(power_notification_callback); 21072531c97eSMatthias Ringwald } 21082531c97eSMatthias Ringwald 21092531c97eSMatthias Ringwald // logging 21102531c97eSMatthias Ringwald loggingEnabled = 0; 21112531c97eSMatthias Ringwald int newLoggingEnabled = 1; 21122531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 21132531c97eSMatthias Ringwald // iPhone has toggle in Preferences.app 21142531c97eSMatthias Ringwald newLoggingEnabled = platform_iphone_logging_enabled(); 21152531c97eSMatthias Ringwald #endif 21162531c97eSMatthias Ringwald daemon_set_logging_enabled(newLoggingEnabled); 21172531c97eSMatthias Ringwald 21182531c97eSMatthias Ringwald // dump version 2119a05b2457SMatthias Ringwald log_info("BTStack Server started\n"); 21202531c97eSMatthias Ringwald log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE); 21212531c97eSMatthias Ringwald 21228129e542SMatthias Ringwald #ifndef HAVE_INTEL_USB 21238129e542SMatthias Ringwald btstack_server_configure_stack(); 21248129e542SMatthias Ringwald #endif 21252531c97eSMatthias Ringwald 21262531c97eSMatthias Ringwald #ifdef USE_LAUNCHD 21272531c97eSMatthias Ringwald socket_connection_create_launchd(); 21282531c97eSMatthias Ringwald #else 21292531c97eSMatthias Ringwald // create server 21302531c97eSMatthias Ringwald if (tcp_flag) { 21312531c97eSMatthias Ringwald socket_connection_create_tcp(BTSTACK_PORT); 21322531c97eSMatthias Ringwald } else { 213342963eceSMatthias Ringwald #ifdef HAVE_UNIX_SOCKETS 21342531c97eSMatthias Ringwald socket_connection_create_unix(BTSTACK_UNIX); 213542963eceSMatthias Ringwald #endif 21362531c97eSMatthias Ringwald } 21372531c97eSMatthias Ringwald #endif 21382531c97eSMatthias Ringwald socket_connection_register_packet_callback(&daemon_client_handler); 21392531c97eSMatthias Ringwald 21402531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 21412531c97eSMatthias Ringwald // notify daemons 21422531c97eSMatthias Ringwald notify_post("ch.ringwald.btstack.started"); 21432531c97eSMatthias Ringwald 21442531c97eSMatthias Ringwald // spawn thread to have BTstack run loop on new thread, while main thread is used to keep CFRunLoop 21452531c97eSMatthias Ringwald pthread_t run_loop; 21462531c97eSMatthias Ringwald pthread_create(&run_loop, NULL, &btstack_run_loop_thread, NULL); 21472531c97eSMatthias Ringwald 21482531c97eSMatthias Ringwald // needed to receive notifications 21492531c97eSMatthias Ringwald CFRunLoopRun(); 21502531c97eSMatthias Ringwald #endif 21512531c97eSMatthias Ringwald // go! 21522531c97eSMatthias Ringwald btstack_run_loop_execute(); 21532531c97eSMatthias Ringwald return 0; 21542531c97eSMatthias Ringwald } 215585fb0500SMatthias Ringwald 21564c293e47SMatthias Ringwald int btstack_server_run_tcp(void){ 21574c293e47SMatthias Ringwald return btstack_server_run(1); 21584c293e47SMatthias Ringwald } 2159ee252d54SMatthias Ringwald 216085fb0500SMatthias Ringwald int main (int argc, char * const * argv){ 216185fb0500SMatthias Ringwald 216285fb0500SMatthias Ringwald int tcp_flag = 0; 216385fb0500SMatthias Ringwald struct option long_options[] = { 216485fb0500SMatthias Ringwald { "tcp", no_argument, &tcp_flag, 1 }, 216585fb0500SMatthias Ringwald { "help", no_argument, 0, 0 }, 216685fb0500SMatthias Ringwald { 0,0,0,0 } // This is a filler for -1 216785fb0500SMatthias Ringwald }; 216885fb0500SMatthias Ringwald 216985fb0500SMatthias Ringwald while (1) { 217085fb0500SMatthias Ringwald int c; 217185fb0500SMatthias Ringwald int option_index = -1; 217285fb0500SMatthias Ringwald c = getopt_long(argc, argv, "h", long_options, &option_index); 217385fb0500SMatthias Ringwald if (c == -1) break; // no more option 217485fb0500SMatthias Ringwald 217585fb0500SMatthias Ringwald // treat long parameter first 217685fb0500SMatthias Ringwald if (option_index == -1) { 217785fb0500SMatthias Ringwald switch (c) { 217885fb0500SMatthias Ringwald case '?': 217985fb0500SMatthias Ringwald case 'h': 218085fb0500SMatthias Ringwald usage(argv[0]); 218185fb0500SMatthias Ringwald return 0; 218285fb0500SMatthias Ringwald break; 218385fb0500SMatthias Ringwald } 218485fb0500SMatthias Ringwald } else { 218585fb0500SMatthias Ringwald switch (option_index) { 218685fb0500SMatthias Ringwald case 1: 218785fb0500SMatthias Ringwald usage(argv[0]); 218885fb0500SMatthias Ringwald return 0; 218985fb0500SMatthias Ringwald break; 219085fb0500SMatthias Ringwald } 219185fb0500SMatthias Ringwald } 219285fb0500SMatthias Ringwald } 219385fb0500SMatthias Ringwald 219485fb0500SMatthias Ringwald #ifndef HAVE_UNIX_SOCKETS 219585fb0500SMatthias Ringwald // TCP is default if there are no unix sockets 219685fb0500SMatthias Ringwald tcp_flag = 1; 219785fb0500SMatthias Ringwald #endif 219885fb0500SMatthias Ringwald 219985fb0500SMatthias Ringwald btstack_server_run(tcp_flag); 220085fb0500SMatthias Ringwald } 22016cf7652eSMatthias Ringwald 22026cf7652eSMatthias Ringwald void btstack_server_set_storage_path(const char * path){ 22036cf7652eSMatthias Ringwald if (btstack_server_storage_path){ 22046cf7652eSMatthias Ringwald free((void*)btstack_server_storage_path); 22056cf7652eSMatthias Ringwald btstack_server_storage_path = NULL; 22066cf7652eSMatthias Ringwald } 22076cf7652eSMatthias Ringwald btstack_server_storage_path = strdup(path); 22086cf7652eSMatthias Ringwald log_info("Storage path %s", btstack_server_storage_path); 22096cf7652eSMatthias Ringwald } 2210