xref: /btstack/platform/daemon/src/daemon.c (revision 092bd57e492d9c2d385e596bac3c5e50f1bb6d7b)
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 
38ab2c6ae4SMatthias 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 #include <getopt.h>
582531c97eSMatthias Ringwald 
59*092bd57eSMatthias Ringwald #ifdef _WIN32
60*092bd57eSMatthias Ringwald #include "Winsock2.h"
61*092bd57eSMatthias Ringwald #endif
62*092bd57eSMatthias Ringwald 
632531c97eSMatthias Ringwald #include "btstack.h"
642531c97eSMatthias Ringwald #include "btstack_client.h"
652531c97eSMatthias Ringwald #include "btstack_debug.h"
662531c97eSMatthias Ringwald #include "btstack_device_name_db.h"
672531c97eSMatthias Ringwald #include "btstack_event.h"
682531c97eSMatthias Ringwald #include "btstack_linked_list.h"
692531c97eSMatthias Ringwald #include "btstack_run_loop.h"
702531c97eSMatthias Ringwald #include "btstack_run_loop_posix.h"
712531c97eSMatthias Ringwald #include "btstack_version.h"
722531c97eSMatthias Ringwald #include "classic/btstack_link_key_db.h"
732531c97eSMatthias Ringwald #include "classic/rfcomm.h"
742531c97eSMatthias Ringwald #include "classic/sdp_server.h"
752531c97eSMatthias Ringwald #include "classic/sdp_client.h"
76efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
772531c97eSMatthias Ringwald #include "hci.h"
782531c97eSMatthias Ringwald #include "hci_cmd.h"
792531c97eSMatthias Ringwald #include "hci_dump.h"
802531c97eSMatthias Ringwald #include "hci_transport.h"
812531c97eSMatthias Ringwald #include "l2cap.h"
822531c97eSMatthias Ringwald #include "rfcomm_service_db.h"
832531c97eSMatthias Ringwald #include "socket_connection.h"
842531c97eSMatthias Ringwald 
852531c97eSMatthias Ringwald #ifdef ENABLE_BLE
862531c97eSMatthias Ringwald #include "ble/gatt_client.h"
872531c97eSMatthias Ringwald #include "ble/att_server.h"
882531c97eSMatthias Ringwald #include "ble/att_db.h"
892531c97eSMatthias Ringwald #include "ble/le_device_db.h"
902531c97eSMatthias Ringwald #include "ble/sm.h"
912531c97eSMatthias Ringwald #endif
922531c97eSMatthias Ringwald 
932531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
942531c97eSMatthias Ringwald #include <CoreFoundation/CoreFoundation.h>
952531c97eSMatthias Ringwald #include <notify.h>
962531c97eSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h"
972531c97eSMatthias Ringwald #include "../port/ios/src/platform_iphone.h"
982531c97eSMatthias Ringwald // support for "enforece wake device" in h4 - used by iOS power management
992531c97eSMatthias Ringwald extern void hci_transport_h4_iphone_set_enforce_wake_device(char *path);
1002531c97eSMatthias Ringwald #endif
1012531c97eSMatthias Ringwald 
1022531c97eSMatthias Ringwald // copy of prototypes
103f8d88472SMatthias Ringwald const btstack_device_name_db_t * btstack_device_name_db_corefoundation_instance(void);
1042531c97eSMatthias Ringwald const btstack_device_name_db_t * btstack_device_name_db_fs_instance(void);
105f8d88472SMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db_corefoundation_instance(void);
1062531c97eSMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void);
1072531c97eSMatthias Ringwald 
1082531c97eSMatthias Ringwald #ifndef BTSTACK_LOG_FILE
1092531c97eSMatthias Ringwald #define BTSTACK_LOG_FILE "/tmp/hci_dump.pklg"
1102531c97eSMatthias Ringwald #endif
1112531c97eSMatthias Ringwald 
1122531c97eSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
1132531c97eSMatthias Ringwald #ifndef BTSTACK_LOG_TYPE
1142531c97eSMatthias Ringwald #define BTSTACK_LOG_TYPE HCI_DUMP_PACKETLOGGER
1152531c97eSMatthias Ringwald #endif
1162531c97eSMatthias Ringwald 
1172531c97eSMatthias Ringwald #define DAEMON_NO_ACTIVE_CLIENT_TIMEOUT 10000
1182531c97eSMatthias Ringwald 
1192531c97eSMatthias Ringwald #define ATT_MAX_LONG_ATTRIBUTE_SIZE 512
1202531c97eSMatthias Ringwald 
1212531c97eSMatthias Ringwald 
1222531c97eSMatthias Ringwald #define SERVICE_LENGTH                      20
1232531c97eSMatthias Ringwald #define CHARACTERISTIC_LENGTH               24
1242531c97eSMatthias Ringwald #define CHARACTERISTIC_DESCRIPTOR_LENGTH    18
1252531c97eSMatthias Ringwald 
1262531c97eSMatthias Ringwald // ATT_MTU - 1
1272531c97eSMatthias Ringwald #define ATT_MAX_ATTRIBUTE_SIZE 22
1282531c97eSMatthias Ringwald 
1292531c97eSMatthias Ringwald // HCI CMD OGF/OCF
1302531c97eSMatthias Ringwald #define READ_CMD_OGF(buffer) (buffer[1] >> 2)
1312531c97eSMatthias Ringwald #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
1322531c97eSMatthias Ringwald 
1332531c97eSMatthias Ringwald typedef struct {
1342531c97eSMatthias Ringwald     // linked list - assert: first field
1352531c97eSMatthias Ringwald     btstack_linked_item_t    item;
1362531c97eSMatthias Ringwald 
1372531c97eSMatthias Ringwald     // connection
1382531c97eSMatthias Ringwald     connection_t * connection;
1392531c97eSMatthias Ringwald 
1402531c97eSMatthias Ringwald     btstack_linked_list_t rfcomm_cids;
1412531c97eSMatthias Ringwald     btstack_linked_list_t rfcomm_services;
1422531c97eSMatthias Ringwald     btstack_linked_list_t l2cap_cids;
1432531c97eSMatthias Ringwald     btstack_linked_list_t l2cap_psms;
1442531c97eSMatthias Ringwald     btstack_linked_list_t sdp_record_handles;
1452531c97eSMatthias Ringwald     btstack_linked_list_t gatt_con_handles;
1462531c97eSMatthias Ringwald     // power mode
1472531c97eSMatthias Ringwald     HCI_POWER_MODE power_mode;
1482531c97eSMatthias Ringwald 
1492531c97eSMatthias Ringwald     // discoverable
1502531c97eSMatthias Ringwald     uint8_t        discoverable;
1512531c97eSMatthias Ringwald 
1522531c97eSMatthias Ringwald } client_state_t;
1532531c97eSMatthias Ringwald 
1542531c97eSMatthias Ringwald typedef struct btstack_linked_list_uint32 {
1552531c97eSMatthias Ringwald     btstack_linked_item_t   item;
1562531c97eSMatthias Ringwald     uint32_t        value;
1572531c97eSMatthias Ringwald } btstack_linked_list_uint32_t;
1582531c97eSMatthias Ringwald 
1592531c97eSMatthias Ringwald typedef struct btstack_linked_list_connection {
1602531c97eSMatthias Ringwald     btstack_linked_item_t   item;
1612531c97eSMatthias Ringwald     connection_t  * connection;
1622531c97eSMatthias Ringwald } btstack_linked_list_connection_t;
1632531c97eSMatthias Ringwald 
1642531c97eSMatthias Ringwald typedef struct btstack_linked_list_gatt_client_helper{
1652531c97eSMatthias Ringwald     btstack_linked_item_t item;
166711e6c80SMatthias Ringwald     hci_con_handle_t con_handle;
1672531c97eSMatthias Ringwald     connection_t * active_connection;   // the one that started the current query
1682531c97eSMatthias Ringwald     btstack_linked_list_t  all_connections;     // list of all connections that ever used this helper
1692531c97eSMatthias Ringwald     uint16_t characteristic_length;
1702531c97eSMatthias Ringwald     uint16_t characteristic_handle;
1712531c97eSMatthias Ringwald     uint8_t  characteristic_buffer[10 + ATT_MAX_LONG_ATTRIBUTE_SIZE];   // header for sending event right away
1722531c97eSMatthias Ringwald     uint8_t  long_query_type;
1732531c97eSMatthias Ringwald } btstack_linked_list_gatt_client_helper_t;
1742531c97eSMatthias Ringwald 
1752531c97eSMatthias Ringwald // MARK: prototypes
1762531c97eSMatthias Ringwald static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1772531c97eSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1782531c97eSMatthias Ringwald #ifdef ENABLE_BLE
1792531c97eSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size);
1802531c97eSMatthias Ringwald #endif
1812531c97eSMatthias Ringwald static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state);
1822531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection);
1832531c97eSMatthias Ringwald static int              clients_require_power_on(void);
1842531c97eSMatthias Ringwald static int              clients_require_discoverable(void);
1852531c97eSMatthias Ringwald static void              clients_clear_power_request(void);
1862531c97eSMatthias Ringwald static void start_power_off_timer(void);
1872531c97eSMatthias Ringwald static void stop_power_off_timer(void);
1882531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection);
1892531c97eSMatthias Ringwald static void hci_emit_system_bluetooth_enabled(uint8_t enabled);
1902531c97eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size);
1912531c97eSMatthias Ringwald 
1922531c97eSMatthias Ringwald 
1932531c97eSMatthias Ringwald // MARK: globals
1948c9f78e5SMatthias Ringwald 
1958c9f78e5SMatthias Ringwald #ifdef HAVE_TRANSPORT_H4
1962531c97eSMatthias Ringwald static hci_transport_config_uart_t hci_transport_config_uart;
1978c9f78e5SMatthias Ringwald #endif
1988c9f78e5SMatthias Ringwald 
1998c9f78e5SMatthias Ringwald static const hci_transport_t * transport;
2002531c97eSMatthias Ringwald static btstack_timer_source_t timeout;
2012531c97eSMatthias Ringwald static uint8_t timeout_active = 0;
2022531c97eSMatthias Ringwald static int power_management_sleep = 0;
2032531c97eSMatthias Ringwald static btstack_linked_list_t clients = NULL;        // list of connected clients `
2042531c97eSMatthias Ringwald #ifdef ENABLE_BLE
2052531c97eSMatthias Ringwald static btstack_linked_list_t gatt_client_helpers = NULL;   // list of used gatt client (helpers)
2062531c97eSMatthias Ringwald #endif
2072531c97eSMatthias Ringwald 
2082531c97eSMatthias Ringwald static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth_status_handler;
2092531c97eSMatthias Ringwald 
2102531c97eSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
2112531c97eSMatthias Ringwald 
2122531c97eSMatthias Ringwald static int global_enable = 0;
2132531c97eSMatthias Ringwald 
2142531c97eSMatthias Ringwald static btstack_link_key_db_t    const * btstack_link_key_db = NULL;
2152531c97eSMatthias Ringwald static btstack_device_name_db_t const * btstack_device_name_db = NULL;
2162531c97eSMatthias Ringwald // static int rfcomm_channel_generator = 1;
2172531c97eSMatthias Ringwald 
2182531c97eSMatthias Ringwald static uint8_t   attribute_value[1000];
2192531c97eSMatthias Ringwald static const int attribute_value_buffer_size = sizeof(attribute_value);
2202531c97eSMatthias Ringwald static uint8_t serviceSearchPattern[200];
2212531c97eSMatthias Ringwald static uint8_t attributeIDList[50];
2222531c97eSMatthias Ringwald static void * sdp_client_query_connection;
2232531c97eSMatthias Ringwald 
2242531c97eSMatthias Ringwald static int loggingEnabled;
2252531c97eSMatthias Ringwald 
2262531c97eSMatthias Ringwald // stashed code from l2cap.c and rfcomm.c -- needed for new implementation
2272531c97eSMatthias Ringwald #if 0
2282531c97eSMatthias Ringwald static void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
2292531c97eSMatthias Ringwald 
23062c64df1SMatthias Ringwald     log_info("DAEMON_EVENT_L2CAP_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits);
2312531c97eSMatthias Ringwald 
2322531c97eSMatthias Ringwald     uint8_t event[5];
23362c64df1SMatthias Ringwald     event[0] = DAEMON_EVENT_L2CAP_CREDITS;
2342531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
2352531c97eSMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
2362531c97eSMatthias Ringwald     event[4] = credits;
2372531c97eSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
2382531c97eSMatthias Ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2392531c97eSMatthias Ringwald }
2402531c97eSMatthias Ringwald 
2412531c97eSMatthias Ringwald static void l2cap_hand_out_credits(void){
2422531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
2432531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
2442531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2452531c97eSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2462531c97eSMatthias Ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2472531c97eSMatthias Ringwald         if (!hci_number_free_acl_slots_for_handle(channel->handle)) return;
2482531c97eSMatthias Ringwald         l2cap_emit_credits(channel, 1);
2492531c97eSMatthias Ringwald     }
2502531c97eSMatthias Ringwald }
2512531c97eSMatthias Ringwald static void rfcomm_emit_credits(rfcomm_channel_t * channel, uint8_t credits) {
25262c64df1SMatthias Ringwald     log_info("DAEMON_EVENT_RFCOMM_CREDITS cid 0x%02x credits %u", channel->rfcomm_cid, credits);
2532531c97eSMatthias Ringwald     uint8_t event[5];
25462c64df1SMatthias Ringwald     event[0] = DAEMON_EVENT_RFCOMM_CREDITS;
2552531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
2562531c97eSMatthias Ringwald     little_endian_store_16(event, 2, channel->rfcomm_cid);
2572531c97eSMatthias Ringwald     event[4] = credits;
2582531c97eSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
2592531c97eSMatthias Ringwald     (*app_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
2602531c97eSMatthias Ringwald }
2612531c97eSMatthias Ringwald static void rfcomm_hand_out_credits(void){
2622531c97eSMatthias Ringwald     btstack_linked_item_t * it;
2632531c97eSMatthias Ringwald     for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){
2642531c97eSMatthias Ringwald         rfcomm_channel_t * channel = (rfcomm_channel_t *) it;
2652531c97eSMatthias Ringwald         if (channel->state != RFCOMM_CHANNEL_OPEN) {
26662c64df1SMatthias Ringwald             // log_info("DAEMON_EVENT_RFCOMM_CREDITS: multiplexer not open");
2672531c97eSMatthias Ringwald             continue;
2682531c97eSMatthias Ringwald         }
2692531c97eSMatthias Ringwald         if (!channel->credits_outgoing) {
27062c64df1SMatthias Ringwald             // log_info("DAEMON_EVENT_RFCOMM_CREDITS: no outgoing credits");
2712531c97eSMatthias Ringwald             continue;
2722531c97eSMatthias Ringwald         }
2732531c97eSMatthias Ringwald         // channel open, multiplexer has l2cap credits and we didn't hand out credit before -> go!
27462c64df1SMatthias Ringwald         // log_info("DAEMON_EVENT_RFCOMM_CREDITS: 1");
2752531c97eSMatthias Ringwald         rfcomm_emit_credits(channel, 1);
2762531c97eSMatthias Ringwald     }
2772531c97eSMatthias Ringwald }
2782531c97eSMatthias Ringwald 
2792531c97eSMatthias Ringwald #endif
2802531c97eSMatthias Ringwald 
2812531c97eSMatthias Ringwald static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state){
2822531c97eSMatthias Ringwald     log_info("Bluetooth status: %u\n", state);
2832531c97eSMatthias Ringwald };
2842531c97eSMatthias Ringwald 
2852531c97eSMatthias Ringwald static void daemon_no_connections_timeout(struct btstack_timer_source *ts){
2862531c97eSMatthias Ringwald     if (clients_require_power_on()) return;    // false alarm :)
2872531c97eSMatthias Ringwald     log_info("No active client connection for %u seconds -> POWER OFF\n", DAEMON_NO_ACTIVE_CLIENT_TIMEOUT/1000);
2882531c97eSMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
2892531c97eSMatthias Ringwald }
2902531c97eSMatthias Ringwald 
2912531c97eSMatthias Ringwald 
2922531c97eSMatthias Ringwald static void add_uint32_to_list(btstack_linked_list_t *list, uint32_t value){
2932531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
2942531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, list);
2952531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2962531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
2972531c97eSMatthias Ringwald         if ( item->value == value) return; // already in list
2982531c97eSMatthias Ringwald     }
2992531c97eSMatthias Ringwald 
3002531c97eSMatthias Ringwald     btstack_linked_list_uint32_t * item = malloc(sizeof(btstack_linked_list_uint32_t));
3012531c97eSMatthias Ringwald     if (!item) return;
3022531c97eSMatthias Ringwald     item->value = value;
3032531c97eSMatthias Ringwald     btstack_linked_list_add(list, (btstack_linked_item_t *) item);
3042531c97eSMatthias Ringwald }
3052531c97eSMatthias Ringwald 
3062531c97eSMatthias Ringwald static void remove_and_free_uint32_from_list(btstack_linked_list_t *list, uint32_t value){
3072531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
3082531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, list);
3092531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3102531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
3112531c97eSMatthias Ringwald         if ( item->value != value) continue;
3122531c97eSMatthias Ringwald         btstack_linked_list_remove(list, (btstack_linked_item_t *) item);
3132531c97eSMatthias Ringwald         free(item);
3142531c97eSMatthias Ringwald     }
3152531c97eSMatthias Ringwald }
3162531c97eSMatthias Ringwald 
3172531c97eSMatthias Ringwald static void daemon_add_client_rfcomm_service(connection_t * connection, uint16_t service_channel){
3182531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3192531c97eSMatthias Ringwald     if (!client_state) return;
3202531c97eSMatthias Ringwald     add_uint32_to_list(&client_state->rfcomm_services, service_channel);
3212531c97eSMatthias Ringwald }
3222531c97eSMatthias Ringwald 
3232531c97eSMatthias Ringwald static void daemon_remove_client_rfcomm_service(connection_t * connection, uint16_t service_channel){
3242531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3252531c97eSMatthias Ringwald     if (!client_state) return;
3262531c97eSMatthias Ringwald     remove_and_free_uint32_from_list(&client_state->rfcomm_services, service_channel);
3272531c97eSMatthias Ringwald }
3282531c97eSMatthias Ringwald 
3292531c97eSMatthias Ringwald static void daemon_add_client_rfcomm_channel(connection_t * connection, uint16_t cid){
3302531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3312531c97eSMatthias Ringwald     if (!client_state) return;
3322531c97eSMatthias Ringwald     add_uint32_to_list(&client_state->rfcomm_cids, cid);
3332531c97eSMatthias Ringwald }
3342531c97eSMatthias Ringwald 
3352531c97eSMatthias Ringwald static void daemon_remove_client_rfcomm_channel(connection_t * connection, uint16_t cid){
3362531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3372531c97eSMatthias Ringwald     if (!client_state) return;
3382531c97eSMatthias Ringwald     remove_and_free_uint32_from_list(&client_state->rfcomm_cids, cid);
3392531c97eSMatthias Ringwald }
3402531c97eSMatthias Ringwald 
3412531c97eSMatthias Ringwald static void daemon_add_client_l2cap_service(connection_t * connection, uint16_t psm){
3422531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3432531c97eSMatthias Ringwald     if (!client_state) return;
3442531c97eSMatthias Ringwald     add_uint32_to_list(&client_state->l2cap_psms, psm);
3452531c97eSMatthias Ringwald }
3462531c97eSMatthias Ringwald 
3472531c97eSMatthias Ringwald static void daemon_remove_client_l2cap_service(connection_t * connection, uint16_t psm){
3482531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3492531c97eSMatthias Ringwald     if (!client_state) return;
3502531c97eSMatthias Ringwald     remove_and_free_uint32_from_list(&client_state->l2cap_psms, psm);
3512531c97eSMatthias Ringwald }
3522531c97eSMatthias Ringwald 
3532531c97eSMatthias Ringwald static void daemon_add_client_l2cap_channel(connection_t * connection, uint16_t cid){
3542531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3552531c97eSMatthias Ringwald     if (!client_state) return;
3562531c97eSMatthias Ringwald     add_uint32_to_list(&client_state->l2cap_cids, cid);
3572531c97eSMatthias Ringwald }
3582531c97eSMatthias Ringwald 
3592531c97eSMatthias Ringwald static void daemon_remove_client_l2cap_channel(connection_t * connection, uint16_t cid){
3602531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3612531c97eSMatthias Ringwald     if (!client_state) return;
3622531c97eSMatthias Ringwald     remove_and_free_uint32_from_list(&client_state->l2cap_cids, cid);
3632531c97eSMatthias Ringwald }
3642531c97eSMatthias Ringwald 
3652531c97eSMatthias Ringwald static void daemon_add_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){
3662531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3672531c97eSMatthias Ringwald     if (!client_state) return;
3682531c97eSMatthias Ringwald     add_uint32_to_list(&client_state->sdp_record_handles, handle);
3692531c97eSMatthias Ringwald }
3702531c97eSMatthias Ringwald 
3712531c97eSMatthias Ringwald static void daemon_remove_client_sdp_service_record_handle(connection_t * connection, uint32_t handle){
3722531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3732531c97eSMatthias Ringwald     if (!client_state) return;
3742531c97eSMatthias Ringwald     remove_and_free_uint32_from_list(&client_state->sdp_record_handles, handle);
3752531c97eSMatthias Ringwald }
3762531c97eSMatthias Ringwald 
3772531c97eSMatthias Ringwald #ifdef ENABLE_BLE
3782531c97eSMatthias Ringwald static void daemon_add_gatt_client_handle(connection_t * connection, uint32_t handle){
3792531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
3802531c97eSMatthias Ringwald     if (!client_state) return;
3812531c97eSMatthias Ringwald 
3822531c97eSMatthias Ringwald     // check if handle already exists in the gatt_con_handles list
3832531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
3842531c97eSMatthias Ringwald     int handle_found = 0;
3852531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
3862531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3872531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
3882531c97eSMatthias Ringwald         if (item->value == handle){
3892531c97eSMatthias Ringwald             handle_found = 1;
3902531c97eSMatthias Ringwald             break;
3912531c97eSMatthias Ringwald         }
3922531c97eSMatthias Ringwald     }
3932531c97eSMatthias Ringwald     // if handle doesn't exist add it to gatt_con_handles
3942531c97eSMatthias Ringwald     if (!handle_found){
3952531c97eSMatthias Ringwald         add_uint32_to_list(&client_state->gatt_con_handles, handle);
3962531c97eSMatthias Ringwald     }
3972531c97eSMatthias Ringwald 
3982531c97eSMatthias Ringwald     // check if there is a helper with given handle
3992531c97eSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * gatt_helper = NULL;
4002531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
4012531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4022531c97eSMatthias Ringwald         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
4032531c97eSMatthias Ringwald         if (item->con_handle == handle){
4042531c97eSMatthias Ringwald             gatt_helper = item;
4052531c97eSMatthias Ringwald             break;
4062531c97eSMatthias Ringwald         }
4072531c97eSMatthias Ringwald     }
4082531c97eSMatthias Ringwald 
4092531c97eSMatthias Ringwald     // if gatt_helper doesn't exist, create it and add it to gatt_client_helpers list
4102531c97eSMatthias Ringwald     if (!gatt_helper){
411af0d0df5SMatthias Ringwald         gatt_helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1);
4122531c97eSMatthias Ringwald         if (!gatt_helper) return;
4132531c97eSMatthias Ringwald         gatt_helper->con_handle = handle;
4142531c97eSMatthias Ringwald         btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) gatt_helper);
4152531c97eSMatthias Ringwald     }
4162531c97eSMatthias Ringwald 
4172531c97eSMatthias Ringwald     // check if connection exists
4182531c97eSMatthias Ringwald     int connection_found = 0;
4192531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_helper->all_connections);
4202531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4212531c97eSMatthias Ringwald         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
4222531c97eSMatthias Ringwald         if (item->connection == connection){
4232531c97eSMatthias Ringwald             connection_found = 1;
4242531c97eSMatthias Ringwald             break;
4252531c97eSMatthias Ringwald         }
4262531c97eSMatthias Ringwald     }
4272531c97eSMatthias Ringwald 
4282531c97eSMatthias Ringwald     // if connection is not found, add it to the all_connections, and set it as active connection
4292531c97eSMatthias Ringwald     if (!connection_found){
430af0d0df5SMatthias Ringwald         btstack_linked_list_connection_t * con = calloc(sizeof(btstack_linked_list_connection_t), 1);
4312531c97eSMatthias Ringwald         if (!con) return;
4322531c97eSMatthias Ringwald         con->connection = connection;
4332531c97eSMatthias Ringwald         btstack_linked_list_add(&gatt_helper->all_connections, (btstack_linked_item_t *)con);
4342531c97eSMatthias Ringwald     }
4352531c97eSMatthias Ringwald }
4362531c97eSMatthias Ringwald 
4372531c97eSMatthias Ringwald 
4382531c97eSMatthias Ringwald static void daemon_remove_gatt_client_handle(connection_t * connection, uint32_t handle){
4392531c97eSMatthias Ringwald     // PART 1 - uses connection & handle
4402531c97eSMatthias Ringwald     // might be extracted or vanish totally
4412531c97eSMatthias Ringwald     client_state_t * client_state = client_for_connection(connection);
4422531c97eSMatthias Ringwald     if (!client_state) return;
4432531c97eSMatthias Ringwald 
4442531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
4452531c97eSMatthias Ringwald     // remove handle from gatt_con_handles list
4462531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
4472531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4482531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
4492531c97eSMatthias Ringwald         if (item->value == handle){
4502531c97eSMatthias Ringwald             btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item);
4512531c97eSMatthias Ringwald             free(item);
4522531c97eSMatthias Ringwald         }
4532531c97eSMatthias Ringwald     }
4542531c97eSMatthias Ringwald 
4552531c97eSMatthias Ringwald     // PART 2 - only uses handle
4562531c97eSMatthias Ringwald 
4572531c97eSMatthias Ringwald     // find helper with given handle
4582531c97eSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * helper = NULL;
4592531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
4602531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4612531c97eSMatthias Ringwald         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
4622531c97eSMatthias Ringwald         if (item->con_handle == handle){
4632531c97eSMatthias Ringwald             helper = item;
4642531c97eSMatthias Ringwald             break;
4652531c97eSMatthias Ringwald         }
4662531c97eSMatthias Ringwald     }
4672531c97eSMatthias Ringwald 
4682531c97eSMatthias Ringwald     if (!helper) return;
4692531c97eSMatthias Ringwald     // remove connection from helper
4702531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &helper->all_connections);
4712531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4722531c97eSMatthias Ringwald         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
4732531c97eSMatthias Ringwald         if (item->connection == connection){
4742531c97eSMatthias Ringwald             btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item);
4752531c97eSMatthias Ringwald             free(item);
4762531c97eSMatthias Ringwald             break;
4772531c97eSMatthias Ringwald         }
4782531c97eSMatthias Ringwald     }
4792531c97eSMatthias Ringwald 
4802531c97eSMatthias Ringwald     if (helper->active_connection == connection){
4812531c97eSMatthias Ringwald         helper->active_connection = NULL;
4822531c97eSMatthias Ringwald     }
4832531c97eSMatthias Ringwald     // if helper has no more connections, call disconnect
4842531c97eSMatthias Ringwald     if (helper->all_connections == NULL){
4852531c97eSMatthias Ringwald         gap_disconnect((hci_con_handle_t) helper->con_handle);
4862531c97eSMatthias Ringwald     }
4872531c97eSMatthias Ringwald }
4882531c97eSMatthias Ringwald 
4892531c97eSMatthias Ringwald 
4902531c97eSMatthias Ringwald static void daemon_remove_gatt_client_helper(uint32_t con_handle){
4912531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it, cl;
4922531c97eSMatthias Ringwald     // find helper with given handle
4932531c97eSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * helper = NULL;
4942531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
4952531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
4962531c97eSMatthias Ringwald         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
4972531c97eSMatthias Ringwald         if (item->con_handle == con_handle){
4982531c97eSMatthias Ringwald             helper = item;
4992531c97eSMatthias Ringwald             break;
5002531c97eSMatthias Ringwald         }
5012531c97eSMatthias Ringwald     }
5022531c97eSMatthias Ringwald 
5032531c97eSMatthias Ringwald     if (!helper) return;
5042531c97eSMatthias Ringwald 
5052531c97eSMatthias Ringwald     // remove all connection from helper
5062531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &helper->all_connections);
5072531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5082531c97eSMatthias Ringwald         btstack_linked_list_connection_t * item = (btstack_linked_list_connection_t*) btstack_linked_list_iterator_next(&it);
5092531c97eSMatthias Ringwald         btstack_linked_list_remove(&helper->all_connections, (btstack_linked_item_t *) item);
5102531c97eSMatthias Ringwald         free(item);
5112531c97eSMatthias Ringwald     }
5122531c97eSMatthias Ringwald 
5132531c97eSMatthias Ringwald     btstack_linked_list_remove(&gatt_client_helpers, (btstack_linked_item_t *) helper);
5142531c97eSMatthias Ringwald     free(helper);
5152531c97eSMatthias Ringwald 
5162531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&cl, &clients);
5172531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&cl)){
5182531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
5192531c97eSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &client_state->gatt_con_handles);
5202531c97eSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
5212531c97eSMatthias Ringwald             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5222531c97eSMatthias Ringwald             if (item->value == con_handle){
5232531c97eSMatthias Ringwald                 btstack_linked_list_remove(&client_state->gatt_con_handles, (btstack_linked_item_t *) item);
5242531c97eSMatthias Ringwald                 free(item);
5252531c97eSMatthias Ringwald             }
5262531c97eSMatthias Ringwald         }
5272531c97eSMatthias Ringwald     }
5282531c97eSMatthias Ringwald }
5292531c97eSMatthias Ringwald #endif
5302531c97eSMatthias Ringwald 
5312531c97eSMatthias Ringwald static void daemon_rfcomm_close_connection(client_state_t * daemon_client){
5322531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
5332531c97eSMatthias Ringwald     btstack_linked_list_t *rfcomm_services = &daemon_client->rfcomm_services;
5342531c97eSMatthias Ringwald     btstack_linked_list_t *rfcomm_cids = &daemon_client->rfcomm_cids;
5352531c97eSMatthias Ringwald 
5362531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, rfcomm_services);
5372531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5382531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5392531c97eSMatthias Ringwald         rfcomm_unregister_service(item->value);
5402531c97eSMatthias Ringwald         btstack_linked_list_remove(rfcomm_services, (btstack_linked_item_t *) item);
5412531c97eSMatthias Ringwald         free(item);
5422531c97eSMatthias Ringwald     }
5432531c97eSMatthias Ringwald 
5442531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, rfcomm_cids);
5452531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5462531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5472531c97eSMatthias Ringwald         rfcomm_disconnect(item->value);
5482531c97eSMatthias Ringwald         btstack_linked_list_remove(rfcomm_cids, (btstack_linked_item_t *) item);
5492531c97eSMatthias Ringwald         free(item);
5502531c97eSMatthias Ringwald     }
5512531c97eSMatthias Ringwald }
5522531c97eSMatthias Ringwald 
5532531c97eSMatthias Ringwald 
5542531c97eSMatthias Ringwald static void daemon_l2cap_close_connection(client_state_t * daemon_client){
5552531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
5562531c97eSMatthias Ringwald     btstack_linked_list_t *l2cap_psms = &daemon_client->l2cap_psms;
5572531c97eSMatthias Ringwald     btstack_linked_list_t *l2cap_cids = &daemon_client->l2cap_cids;
5582531c97eSMatthias Ringwald 
5592531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, l2cap_psms);
5602531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5612531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5622531c97eSMatthias Ringwald         l2cap_unregister_service(item->value);
5632531c97eSMatthias Ringwald         btstack_linked_list_remove(l2cap_psms, (btstack_linked_item_t *) item);
5642531c97eSMatthias Ringwald         free(item);
5652531c97eSMatthias Ringwald     }
5662531c97eSMatthias Ringwald 
5672531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, l2cap_cids);
5682531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5692531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5702531c97eSMatthias Ringwald         l2cap_disconnect(item->value, 0); // note: reason isn't used
5712531c97eSMatthias Ringwald         btstack_linked_list_remove(l2cap_cids, (btstack_linked_item_t *) item);
5722531c97eSMatthias Ringwald         free(item);
5732531c97eSMatthias Ringwald     }
5742531c97eSMatthias Ringwald }
5752531c97eSMatthias Ringwald 
5762531c97eSMatthias Ringwald static void daemon_sdp_close_connection(client_state_t * daemon_client){
5772531c97eSMatthias Ringwald     btstack_linked_list_t * list = &daemon_client->sdp_record_handles;
5782531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
5792531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, list);
5802531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5812531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5822531c97eSMatthias Ringwald         sdp_unregister_service(item->value);
5832531c97eSMatthias Ringwald         btstack_linked_list_remove(list, (btstack_linked_item_t *) item);
5842531c97eSMatthias Ringwald         free(item);
5852531c97eSMatthias Ringwald     }
5862531c97eSMatthias Ringwald }
5872531c97eSMatthias Ringwald 
5882531c97eSMatthias Ringwald static connection_t * connection_for_l2cap_cid(uint16_t cid){
5892531c97eSMatthias Ringwald     btstack_linked_list_iterator_t cl;
5902531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&cl, &clients);
5912531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&cl)){
5922531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
5932531c97eSMatthias Ringwald         btstack_linked_list_iterator_t it;
5942531c97eSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &client_state->l2cap_cids);
5952531c97eSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
5962531c97eSMatthias Ringwald             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
5972531c97eSMatthias Ringwald             if (item->value == cid){
5982531c97eSMatthias Ringwald                 return client_state->connection;
5992531c97eSMatthias Ringwald             }
6002531c97eSMatthias Ringwald         }
6012531c97eSMatthias Ringwald     }
6022531c97eSMatthias Ringwald     return NULL;
6032531c97eSMatthias Ringwald }
6042531c97eSMatthias Ringwald 
6052531c97eSMatthias Ringwald static const uint8_t removeServiceRecordHandleAttributeIDList[] = { 0x36, 0x00, 0x05, 0x0A, 0x00, 0x01, 0xFF, 0xFF };
6062531c97eSMatthias Ringwald 
6072531c97eSMatthias Ringwald // register a service record
6082531c97eSMatthias Ringwald // pre: AttributeIDs are in ascending order
6092531c97eSMatthias Ringwald // pre: ServiceRecordHandle is first attribute and is not already registered in database
6102531c97eSMatthias Ringwald // @returns status
6112531c97eSMatthias Ringwald static uint32_t daemon_sdp_create_and_register_service(uint8_t * record){
6122531c97eSMatthias Ringwald 
6132531c97eSMatthias Ringwald     // create new handle
6142531c97eSMatthias Ringwald     uint32_t record_handle = sdp_create_service_record_handle();
6152531c97eSMatthias Ringwald 
6162531c97eSMatthias Ringwald     // calculate size of new service record: DES (2 byte len)
6172531c97eSMatthias Ringwald     // + ServiceRecordHandle attribute (UINT16 UINT32) + size of existing attributes
6182531c97eSMatthias Ringwald     uint16_t recordSize =  3 + (3 + 5) + de_get_data_size(record);
6192531c97eSMatthias Ringwald 
6202531c97eSMatthias Ringwald     // alloc memory for new service record
6212531c97eSMatthias Ringwald     uint8_t * newRecord = malloc(recordSize);
6222531c97eSMatthias Ringwald     if (!newRecord) return 0;
6232531c97eSMatthias Ringwald 
6242531c97eSMatthias Ringwald     // create DES for new record
6252531c97eSMatthias Ringwald     de_create_sequence(newRecord);
6262531c97eSMatthias Ringwald 
6272531c97eSMatthias Ringwald     // set service record handle
6282531c97eSMatthias Ringwald     de_add_number(newRecord, DE_UINT, DE_SIZE_16, 0);
6292531c97eSMatthias Ringwald     de_add_number(newRecord, DE_UINT, DE_SIZE_32, record_handle);
6302531c97eSMatthias Ringwald 
6312531c97eSMatthias Ringwald     // add other attributes
6322531c97eSMatthias Ringwald     sdp_append_attributes_in_attributeIDList(record, (uint8_t *) removeServiceRecordHandleAttributeIDList, 0, recordSize, newRecord);
6332531c97eSMatthias Ringwald 
6342531c97eSMatthias Ringwald     uint8_t status = sdp_register_service(newRecord);
6352531c97eSMatthias Ringwald 
6362531c97eSMatthias Ringwald     if (status) {
6372531c97eSMatthias Ringwald         free(newRecord);
6382531c97eSMatthias Ringwald         return 0;
6392531c97eSMatthias Ringwald     }
6402531c97eSMatthias Ringwald 
6412531c97eSMatthias Ringwald     return record_handle;
6422531c97eSMatthias Ringwald }
6432531c97eSMatthias Ringwald 
6442531c97eSMatthias Ringwald static connection_t * connection_for_rfcomm_cid(uint16_t cid){
6452531c97eSMatthias Ringwald     btstack_linked_list_iterator_t cl;
6462531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&cl, &clients);
6472531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&cl)){
6482531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) btstack_linked_list_iterator_next(&cl);
6492531c97eSMatthias Ringwald         btstack_linked_list_iterator_t it;
6502531c97eSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &client_state->rfcomm_cids);
6512531c97eSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
6522531c97eSMatthias Ringwald             btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
6532531c97eSMatthias Ringwald             if (item->value == cid){
6542531c97eSMatthias Ringwald                 return client_state->connection;
6552531c97eSMatthias Ringwald             }
6562531c97eSMatthias Ringwald         }
6572531c97eSMatthias Ringwald     }
6582531c97eSMatthias Ringwald     return NULL;
6592531c97eSMatthias Ringwald }
6602531c97eSMatthias Ringwald 
6612531c97eSMatthias Ringwald #ifdef ENABLE_BLE
6622531c97eSMatthias Ringwald static void daemon_gatt_client_close_connection(connection_t * connection){
6632531c97eSMatthias Ringwald     client_state_t * client = client_for_connection(connection);
6642531c97eSMatthias Ringwald     if (!client) return;
6652531c97eSMatthias Ringwald 
6662531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
6672531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &client->gatt_con_handles);
6682531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6692531c97eSMatthias Ringwald         btstack_linked_list_uint32_t * item = (btstack_linked_list_uint32_t*) btstack_linked_list_iterator_next(&it);
6702531c97eSMatthias Ringwald         daemon_remove_gatt_client_handle(connection, item->value);
6712531c97eSMatthias Ringwald     }
6722531c97eSMatthias Ringwald }
6732531c97eSMatthias Ringwald #endif
6742531c97eSMatthias Ringwald 
6752531c97eSMatthias Ringwald static void daemon_disconnect_client(connection_t * connection){
6762531c97eSMatthias Ringwald     log_info("Daemon disconnect client %p\n",connection);
6772531c97eSMatthias Ringwald 
6782531c97eSMatthias Ringwald     client_state_t * client = client_for_connection(connection);
6792531c97eSMatthias Ringwald     if (!client) return;
6802531c97eSMatthias Ringwald 
6812531c97eSMatthias Ringwald     daemon_sdp_close_connection(client);
6822531c97eSMatthias Ringwald     daemon_rfcomm_close_connection(client);
6832531c97eSMatthias Ringwald     daemon_l2cap_close_connection(client);
6842531c97eSMatthias Ringwald #ifdef ENABLE_BLE
6852531c97eSMatthias Ringwald     // NOTE: experimental - disconnect all LE connections where GATT Client was used
6862531c97eSMatthias Ringwald     // gatt_client_disconnect_connection(connection);
6872531c97eSMatthias Ringwald     daemon_gatt_client_close_connection(connection);
6882531c97eSMatthias Ringwald #endif
6892531c97eSMatthias Ringwald 
6902531c97eSMatthias Ringwald     btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
6912531c97eSMatthias Ringwald     free(client);
6922531c97eSMatthias Ringwald }
6932531c97eSMatthias Ringwald 
6942531c97eSMatthias Ringwald static void hci_emit_btstack_version(void){
6952531c97eSMatthias Ringwald     log_info("DAEMON_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
6962531c97eSMatthias Ringwald     uint8_t event[6];
6972531c97eSMatthias Ringwald     event[0] = DAEMON_EVENT_VERSION;
6982531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
6992531c97eSMatthias Ringwald     event[2] = BTSTACK_MAJOR;
7002531c97eSMatthias Ringwald     event[3] = BTSTACK_MINOR;
7012531c97eSMatthias Ringwald     little_endian_store_16(event, 4, 3257);    // last SVN commit on Google Code + 1
7022531c97eSMatthias Ringwald     socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event));
7032531c97eSMatthias Ringwald }
7042531c97eSMatthias Ringwald 
7052531c97eSMatthias Ringwald static void hci_emit_system_bluetooth_enabled(uint8_t enabled){
7062531c97eSMatthias Ringwald     log_info("DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
7072531c97eSMatthias Ringwald     uint8_t event[3];
7082531c97eSMatthias Ringwald     event[0] = DAEMON_EVENT_SYSTEM_BLUETOOTH_ENABLED;
7092531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
7102531c97eSMatthias Ringwald     event[2] = enabled;
7112531c97eSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
7122531c97eSMatthias Ringwald     socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, event, sizeof(event));
7132531c97eSMatthias Ringwald }
7142531c97eSMatthias Ringwald 
7152531c97eSMatthias Ringwald static void send_l2cap_connection_open_failed(connection_t * connection, bd_addr_t address, uint16_t psm, uint8_t status){
7162531c97eSMatthias Ringwald     // emit error - see l2cap.c:l2cap_emit_channel_opened(..)
7172531c97eSMatthias Ringwald     uint8_t event[23];
7182531c97eSMatthias Ringwald     memset(event, 0, sizeof(event));
7192531c97eSMatthias Ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
7202531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
7212531c97eSMatthias Ringwald     event[2] = status;
7222531c97eSMatthias Ringwald     reverse_bd_addr(address, &event[3]);
7232531c97eSMatthias Ringwald     // little_endian_store_16(event,  9, channel->handle);
7242531c97eSMatthias Ringwald     little_endian_store_16(event, 11, psm);
7252531c97eSMatthias Ringwald     // little_endian_store_16(event, 13, channel->local_cid);
7262531c97eSMatthias Ringwald     // little_endian_store_16(event, 15, channel->remote_cid);
7272531c97eSMatthias Ringwald     // little_endian_store_16(event, 17, channel->local_mtu);
7282531c97eSMatthias Ringwald     // little_endian_store_16(event, 19, channel->remote_mtu);
7292531c97eSMatthias Ringwald     // little_endian_store_16(event, 21, channel->flush_timeout);
7302531c97eSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
7312531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
7322531c97eSMatthias Ringwald }
7332531c97eSMatthias Ringwald 
7342531c97eSMatthias Ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
7352531c97eSMatthias Ringwald     uint8_t event[5];
73662c64df1SMatthias Ringwald     event[0] = DAEMON_EVENT_L2CAP_SERVICE_REGISTERED;
7372531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
7382531c97eSMatthias Ringwald     event[2] = status;
7392531c97eSMatthias Ringwald     little_endian_store_16(event, 3, psm);
7402531c97eSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
7412531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
7422531c97eSMatthias Ringwald }
7432531c97eSMatthias Ringwald 
7442531c97eSMatthias Ringwald static void rfcomm_emit_service_registered(void *connection, uint8_t status, uint8_t channel){
7452531c97eSMatthias Ringwald     uint8_t event[4];
74662c64df1SMatthias Ringwald     event[0] = DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED;
7472531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
7482531c97eSMatthias Ringwald     event[2] = status;
7492531c97eSMatthias Ringwald     event[3] = channel;
7502531c97eSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
7512531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
7522531c97eSMatthias Ringwald }
7532531c97eSMatthias Ringwald 
7542531c97eSMatthias Ringwald static void send_rfcomm_create_channel_failed(void * connection, bd_addr_t addr, uint8_t server_channel, uint8_t status){
7552531c97eSMatthias Ringwald     // emit error - see rfcom.c:rfcomm_emit_channel_open_failed_outgoing_memory(..)
7562531c97eSMatthias Ringwald     uint8_t event[16];
7572531c97eSMatthias Ringwald     memset(event, 0, sizeof(event));
7582531c97eSMatthias Ringwald     uint8_t pos = 0;
759f8f6a918SMatthias Ringwald     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
7602531c97eSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
7612531c97eSMatthias Ringwald     event[pos++] = status;
7622531c97eSMatthias Ringwald     reverse_bd_addr(addr, &event[pos]); pos += 6;
7632531c97eSMatthias Ringwald     little_endian_store_16(event,  pos, 0);   pos += 2;
7642531c97eSMatthias Ringwald     event[pos++] = server_channel;
7652531c97eSMatthias Ringwald     little_endian_store_16(event, pos, 0); pos += 2;   // channel ID
7662531c97eSMatthias Ringwald     little_endian_store_16(event, pos, 0); pos += 2;   // max frame size
7672531c97eSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
7682531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
7692531c97eSMatthias Ringwald }
7702531c97eSMatthias Ringwald 
7712531c97eSMatthias Ringwald // data: event(8), len(8), status(8), service_record_handle(32)
7722531c97eSMatthias Ringwald static void sdp_emit_service_registered(void *connection, uint32_t handle, uint8_t status) {
7732531c97eSMatthias Ringwald     uint8_t event[7];
77462c64df1SMatthias Ringwald     event[0] = DAEMON_EVENT_SDP_SERVICE_REGISTERED;
7752531c97eSMatthias Ringwald     event[1] = sizeof(event) - 2;
7762531c97eSMatthias Ringwald     event[2] = status;
7772531c97eSMatthias Ringwald     little_endian_store_32(event, 3, handle);
7782531c97eSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
7792531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
7802531c97eSMatthias Ringwald }
7812531c97eSMatthias Ringwald 
7822531c97eSMatthias Ringwald #ifdef ENABLE_BLE
7832531c97eSMatthias Ringwald 
784fc64f94aSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(hci_con_handle_t con_handle) {
7852531c97eSMatthias Ringwald     btstack_linked_list_iterator_t it;
7862531c97eSMatthias Ringwald     if (!gatt_client_helpers) return NULL;
787fc64f94aSMatthias Ringwald     log_info("daemon_get_gatt_client_helper for handle 0x%02x", con_handle);
7882531c97eSMatthias Ringwald 
7892531c97eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
7902531c97eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7912531c97eSMatthias Ringwald         btstack_linked_list_gatt_client_helper_t * item = (btstack_linked_list_gatt_client_helper_t*) btstack_linked_list_iterator_next(&it);
7922531c97eSMatthias Ringwald         if (!item ) {
7932531c97eSMatthias Ringwald             log_info("daemon_get_gatt_client_helper gatt_client_helpers null item");
7942531c97eSMatthias Ringwald             break;
7952531c97eSMatthias Ringwald         }
796fc64f94aSMatthias Ringwald         if (item->con_handle == con_handle){
7972531c97eSMatthias Ringwald             return item;
7982531c97eSMatthias Ringwald         }
7992531c97eSMatthias Ringwald     }
800fc64f94aSMatthias Ringwald     log_info("daemon_get_gatt_client_helper for handle 0x%02x is NULL.", con_handle);
8012531c97eSMatthias Ringwald     return NULL;
8022531c97eSMatthias Ringwald }
8032531c97eSMatthias Ringwald 
804fc64f94aSMatthias Ringwald static void send_gatt_query_complete(connection_t * connection, hci_con_handle_t con_handle, uint8_t status){
8052531c97eSMatthias Ringwald     // @format H1
8062531c97eSMatthias Ringwald     uint8_t event[5];
8072531c97eSMatthias Ringwald     event[0] = GATT_EVENT_QUERY_COMPLETE;
8082531c97eSMatthias Ringwald     event[1] = 3;
809fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
8102531c97eSMatthias Ringwald     event[4] = status;
8112531c97eSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
8122531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
8132531c97eSMatthias Ringwald }
8142531c97eSMatthias Ringwald 
815fc64f94aSMatthias Ringwald static void send_gatt_mtu_event(connection_t * connection, hci_con_handle_t con_handle, uint16_t mtu){
8162531c97eSMatthias Ringwald     uint8_t event[6];
8172531c97eSMatthias Ringwald     int pos = 0;
8182531c97eSMatthias Ringwald     event[pos++] = GATT_EVENT_MTU;
8192531c97eSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
820fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
8212531c97eSMatthias Ringwald     pos += 2;
8222531c97eSMatthias Ringwald     little_endian_store_16(event, pos, mtu);
8232531c97eSMatthias Ringwald     pos += 2;
8242531c97eSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
8252531c97eSMatthias Ringwald     socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
8262531c97eSMatthias Ringwald }
8272531c97eSMatthias Ringwald 
8282531c97eSMatthias Ringwald btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(connection_t *connection, uint8_t *packet, int track_active_connection) {
829fc64f94aSMatthias Ringwald     hci_con_handle_t con_handle = little_endian_read_16(packet, 3);
830fc64f94aSMatthias Ringwald     log_info("daemon_setup_gatt_client_request for handle 0x%02x", con_handle);
831fc64f94aSMatthias Ringwald     hci_connection_t * hci_con = hci_connection_for_handle(con_handle);
8322531c97eSMatthias Ringwald     if ((hci_con == NULL) || (hci_con->state != OPEN)){
833fc64f94aSMatthias Ringwald         send_gatt_query_complete(connection, con_handle, GATT_CLIENT_NOT_CONNECTED);
8342531c97eSMatthias Ringwald         return NULL;
8352531c97eSMatthias Ringwald     }
8362531c97eSMatthias Ringwald 
837fc64f94aSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * helper = daemon_get_gatt_client_helper(con_handle);
8382531c97eSMatthias Ringwald 
8392531c97eSMatthias Ringwald     if (!helper){
8402531c97eSMatthias Ringwald         log_info("helper does not exist");
841af0d0df5SMatthias Ringwald         helper = calloc(sizeof(btstack_linked_list_gatt_client_helper_t), 1);
8422531c97eSMatthias Ringwald         if (!helper) return NULL;
843fc64f94aSMatthias Ringwald         helper->con_handle = con_handle;
8442531c97eSMatthias Ringwald         btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) helper);
8452531c97eSMatthias Ringwald     }
8462531c97eSMatthias Ringwald 
8472531c97eSMatthias Ringwald     if (track_active_connection && helper->active_connection){
848fc64f94aSMatthias Ringwald         send_gatt_query_complete(connection, con_handle, GATT_CLIENT_BUSY);
8492531c97eSMatthias Ringwald         return NULL;
8502531c97eSMatthias Ringwald     }
8512531c97eSMatthias Ringwald 
852fc64f94aSMatthias Ringwald     daemon_add_gatt_client_handle(connection, con_handle);
8532531c97eSMatthias Ringwald 
8542531c97eSMatthias Ringwald     if (track_active_connection){
8552531c97eSMatthias Ringwald         // remember connection responsible for this request
8562531c97eSMatthias Ringwald         helper->active_connection = connection;
8572531c97eSMatthias Ringwald     }
8582531c97eSMatthias Ringwald 
8592531c97eSMatthias Ringwald     return helper;
8602531c97eSMatthias Ringwald }
8612531c97eSMatthias Ringwald 
8622531c97eSMatthias Ringwald // (de)serialize structs from/to HCI commands/events
8632531c97eSMatthias Ringwald 
8642531c97eSMatthias Ringwald void daemon_gatt_serialize_service(gatt_client_service_t * service, uint8_t * event, int offset){
8652531c97eSMatthias Ringwald     little_endian_store_16(event, offset, service->start_group_handle);
8662531c97eSMatthias Ringwald     little_endian_store_16(event, offset+2, service->end_group_handle);
8672531c97eSMatthias Ringwald     reverse_128(service->uuid128, &event[offset + 4]);
8682531c97eSMatthias Ringwald }
8692531c97eSMatthias Ringwald 
8702531c97eSMatthias Ringwald void daemon_gatt_serialize_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * event, int offset){
8712531c97eSMatthias Ringwald     little_endian_store_16(event, offset, characteristic->start_handle);
8722531c97eSMatthias Ringwald     little_endian_store_16(event, offset+2, characteristic->value_handle);
8732531c97eSMatthias Ringwald     little_endian_store_16(event, offset+4, characteristic->end_handle);
8742531c97eSMatthias Ringwald     little_endian_store_16(event, offset+6, characteristic->properties);
8752531c97eSMatthias Ringwald     reverse_128(characteristic->uuid128, &event[offset+8]);
8762531c97eSMatthias Ringwald }
8772531c97eSMatthias Ringwald 
8782531c97eSMatthias Ringwald void daemon_gatt_serialize_characteristic_descriptor(gatt_client_characteristic_descriptor_t * characteristic_descriptor, uint8_t * event, int offset){
8792531c97eSMatthias Ringwald     little_endian_store_16(event, offset, characteristic_descriptor->handle);
8802531c97eSMatthias Ringwald     reverse_128(characteristic_descriptor->uuid128, &event[offset+2]);
8812531c97eSMatthias Ringwald }
8822531c97eSMatthias Ringwald 
8832531c97eSMatthias Ringwald #endif
8842531c97eSMatthias Ringwald 
8852531c97eSMatthias Ringwald static int btstack_command_handler(connection_t *connection, uint8_t *packet, uint16_t size){
8862531c97eSMatthias Ringwald 
8872531c97eSMatthias Ringwald     bd_addr_t addr;
888a046efa6SMatthias Ringwald #ifdef ENABLE_BLE
8892531c97eSMatthias Ringwald     bd_addr_type_t addr_type;
8902531c97eSMatthias Ringwald     hci_con_handle_t handle;
891a046efa6SMatthias Ringwald #endif
8922531c97eSMatthias Ringwald     uint16_t cid;
8932531c97eSMatthias Ringwald     uint16_t psm;
8942531c97eSMatthias Ringwald     uint16_t service_channel;
8952531c97eSMatthias Ringwald     uint16_t mtu;
8962531c97eSMatthias Ringwald     uint8_t  reason;
8972531c97eSMatthias Ringwald     uint8_t  rfcomm_channel;
8982531c97eSMatthias Ringwald     uint8_t  rfcomm_credits;
8992531c97eSMatthias Ringwald     uint32_t service_record_handle;
9002531c97eSMatthias Ringwald     client_state_t *client;
9012531c97eSMatthias Ringwald     uint8_t status;
9022531c97eSMatthias Ringwald     uint8_t  * data;
9032531c97eSMatthias Ringwald #if defined(HAVE_MALLOC) && defined(ENABLE_BLE)
9042531c97eSMatthias Ringwald     uint8_t uuid128[16];
9052531c97eSMatthias Ringwald     gatt_client_service_t service;
9062531c97eSMatthias Ringwald     gatt_client_characteristic_t characteristic;
9072531c97eSMatthias Ringwald     gatt_client_characteristic_descriptor_t descriptor;
9082531c97eSMatthias Ringwald     uint16_t data_length;
9092531c97eSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * gatt_helper;
9102531c97eSMatthias Ringwald #endif
9112531c97eSMatthias Ringwald 
9122531c97eSMatthias Ringwald     uint16_t serviceSearchPatternLen;
9132531c97eSMatthias Ringwald     uint16_t attributeIDListLen;
9142531c97eSMatthias Ringwald 
9152531c97eSMatthias Ringwald     // verbose log info before other info to allow for better tracking
9162531c97eSMatthias Ringwald     hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
9172531c97eSMatthias Ringwald 
9182531c97eSMatthias Ringwald     // BTstack internal commands - 16 Bit OpCode, 8 Bit ParamLen, Params...
9192531c97eSMatthias Ringwald     switch (READ_CMD_OCF(packet)){
9202531c97eSMatthias Ringwald         case BTSTACK_GET_STATE:
9212531c97eSMatthias Ringwald             log_info("BTSTACK_GET_STATE");
9222531c97eSMatthias Ringwald             hci_emit_state();
9232531c97eSMatthias Ringwald             break;
9242531c97eSMatthias Ringwald         case BTSTACK_SET_POWER_MODE:
9252531c97eSMatthias Ringwald             log_info("BTSTACK_SET_POWER_MODE %u", packet[3]);
9262531c97eSMatthias Ringwald             // track client power requests
9272531c97eSMatthias Ringwald             client = client_for_connection(connection);
9282531c97eSMatthias Ringwald             if (!client) break;
9292531c97eSMatthias Ringwald             client->power_mode = packet[3];
9302531c97eSMatthias Ringwald             // handle merged state
9312531c97eSMatthias Ringwald             if (!clients_require_power_on()){
9322531c97eSMatthias Ringwald                 start_power_off_timer();
9332531c97eSMatthias Ringwald             } else if (!power_management_sleep) {
9342531c97eSMatthias Ringwald                 stop_power_off_timer();
9352531c97eSMatthias Ringwald                 hci_power_control(HCI_POWER_ON);
9362531c97eSMatthias Ringwald             }
9372531c97eSMatthias Ringwald             break;
9382531c97eSMatthias Ringwald         case BTSTACK_GET_VERSION:
9392531c97eSMatthias Ringwald             log_info("BTSTACK_GET_VERSION");
9402531c97eSMatthias Ringwald             hci_emit_btstack_version();
9412531c97eSMatthias Ringwald             break;
9422531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
9432531c97eSMatthias Ringwald         case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
9442531c97eSMatthias Ringwald             log_info("BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED %u", packet[3]);
9452531c97eSMatthias Ringwald             btstack_control_iphone_bt_set_enabled(packet[3]);
9462531c97eSMatthias Ringwald             hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled());
9472531c97eSMatthias Ringwald             break;
9482531c97eSMatthias Ringwald 
9492531c97eSMatthias Ringwald         case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
9502531c97eSMatthias Ringwald             log_info("BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED");
9512531c97eSMatthias Ringwald             hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled());
9522531c97eSMatthias Ringwald             break;
9532531c97eSMatthias Ringwald #else
9542531c97eSMatthias Ringwald         case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
9552531c97eSMatthias Ringwald         case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
9562531c97eSMatthias Ringwald             hci_emit_system_bluetooth_enabled(0);
9572531c97eSMatthias Ringwald             break;
9582531c97eSMatthias Ringwald #endif
9592531c97eSMatthias Ringwald         case BTSTACK_SET_DISCOVERABLE:
9602531c97eSMatthias Ringwald             log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]);
9612531c97eSMatthias Ringwald             // track client discoverable requests
9622531c97eSMatthias Ringwald             client = client_for_connection(connection);
9632531c97eSMatthias Ringwald             if (!client) break;
9642531c97eSMatthias Ringwald             client->discoverable = packet[3];
9652531c97eSMatthias Ringwald             // merge state
9662531c97eSMatthias Ringwald             gap_discoverable_control(clients_require_discoverable());
9672531c97eSMatthias Ringwald             break;
9682531c97eSMatthias Ringwald         case BTSTACK_SET_BLUETOOTH_ENABLED:
9692531c97eSMatthias Ringwald             log_info("BTSTACK_SET_BLUETOOTH_ENABLED: %u\n", packet[3]);
9702531c97eSMatthias Ringwald             if (packet[3]) {
9712531c97eSMatthias Ringwald                 // global enable
9722531c97eSMatthias Ringwald                 global_enable = 1;
9732531c97eSMatthias Ringwald                 hci_power_control(HCI_POWER_ON);
9742531c97eSMatthias Ringwald             } else {
9752531c97eSMatthias Ringwald                 global_enable = 0;
9762531c97eSMatthias Ringwald                 clients_clear_power_request();
9772531c97eSMatthias Ringwald                 hci_power_control(HCI_POWER_OFF);
9782531c97eSMatthias Ringwald             }
9792531c97eSMatthias Ringwald             break;
9802531c97eSMatthias Ringwald         case L2CAP_CREATE_CHANNEL_MTU:
9812531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
9822531c97eSMatthias Ringwald             psm = little_endian_read_16(packet, 9);
9832531c97eSMatthias Ringwald             mtu = little_endian_read_16(packet, 11);
9842531c97eSMatthias Ringwald             status = l2cap_create_channel(NULL, addr, psm, mtu, &cid);
9852531c97eSMatthias Ringwald             if (status){
9862531c97eSMatthias Ringwald                 send_l2cap_connection_open_failed(connection, addr, psm, status);
9872531c97eSMatthias Ringwald             } else {
9882531c97eSMatthias Ringwald                 daemon_add_client_l2cap_channel(connection, cid);
9892531c97eSMatthias Ringwald             }
9902531c97eSMatthias Ringwald             break;
9912531c97eSMatthias Ringwald         case L2CAP_CREATE_CHANNEL:
9922531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
9932531c97eSMatthias Ringwald             psm = little_endian_read_16(packet, 9);
9942531c97eSMatthias Ringwald             mtu = 150; // until r865
9952531c97eSMatthias Ringwald             status = l2cap_create_channel(NULL, addr, psm, mtu, &cid);
9962531c97eSMatthias Ringwald             if (status){
9972531c97eSMatthias Ringwald                 send_l2cap_connection_open_failed(connection, addr, psm, status);
9982531c97eSMatthias Ringwald             } else {
9992531c97eSMatthias Ringwald                 daemon_add_client_l2cap_channel(connection, cid);
10002531c97eSMatthias Ringwald             }
10012531c97eSMatthias Ringwald             break;
10022531c97eSMatthias Ringwald         case L2CAP_DISCONNECT:
10032531c97eSMatthias Ringwald             cid = little_endian_read_16(packet, 3);
10042531c97eSMatthias Ringwald             reason = packet[5];
10052531c97eSMatthias Ringwald             l2cap_disconnect(cid, reason);
10062531c97eSMatthias Ringwald             break;
10072531c97eSMatthias Ringwald         case L2CAP_REGISTER_SERVICE:
10082531c97eSMatthias Ringwald             psm = little_endian_read_16(packet, 3);
10092531c97eSMatthias Ringwald             mtu = little_endian_read_16(packet, 5);
10102531c97eSMatthias Ringwald             status = l2cap_register_service(NULL, psm, mtu, LEVEL_0);
10112531c97eSMatthias Ringwald             daemon_add_client_l2cap_service(connection, little_endian_read_16(packet, 3));
10122531c97eSMatthias Ringwald             l2cap_emit_service_registered(connection, status, psm);
10132531c97eSMatthias Ringwald             break;
10142531c97eSMatthias Ringwald         case L2CAP_UNREGISTER_SERVICE:
10152531c97eSMatthias Ringwald             psm = little_endian_read_16(packet, 3);
10162531c97eSMatthias Ringwald             daemon_remove_client_l2cap_service(connection, psm);
10172531c97eSMatthias Ringwald             l2cap_unregister_service(psm);
10182531c97eSMatthias Ringwald             break;
10192531c97eSMatthias Ringwald         case L2CAP_ACCEPT_CONNECTION:
10202531c97eSMatthias Ringwald             cid    = little_endian_read_16(packet, 3);
10212531c97eSMatthias Ringwald             l2cap_accept_connection(cid);
10222531c97eSMatthias Ringwald             break;
10232531c97eSMatthias Ringwald         case L2CAP_DECLINE_CONNECTION:
10242531c97eSMatthias Ringwald             cid    = little_endian_read_16(packet, 3);
10252531c97eSMatthias Ringwald             reason = packet[7];
10267ef6a7bbSMatthias Ringwald             l2cap_decline_connection(cid);
10272531c97eSMatthias Ringwald             break;
10282531c97eSMatthias Ringwald         case RFCOMM_CREATE_CHANNEL:
10292531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
10302531c97eSMatthias Ringwald             rfcomm_channel = packet[9];
10312531c97eSMatthias Ringwald             status = rfcomm_create_channel(&rfcomm_packet_handler, addr, rfcomm_channel, &cid);
10322531c97eSMatthias Ringwald             if (status){
10332531c97eSMatthias Ringwald                 send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status);
10342531c97eSMatthias Ringwald             } else {
10352531c97eSMatthias Ringwald                 daemon_add_client_rfcomm_channel(connection, cid);
10362531c97eSMatthias Ringwald             }
10372531c97eSMatthias Ringwald             break;
10382531c97eSMatthias Ringwald         case RFCOMM_CREATE_CHANNEL_WITH_CREDITS:
10392531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
10402531c97eSMatthias Ringwald             rfcomm_channel = packet[9];
10412531c97eSMatthias Ringwald             rfcomm_credits = packet[10];
10422531c97eSMatthias Ringwald             status = rfcomm_create_channel_with_initial_credits(&rfcomm_packet_handler, addr, rfcomm_channel, rfcomm_credits, &cid );
10432531c97eSMatthias Ringwald             if (status){
10442531c97eSMatthias Ringwald                 send_rfcomm_create_channel_failed(connection, addr, rfcomm_channel, status);
10452531c97eSMatthias Ringwald             } else {
10462531c97eSMatthias Ringwald                 daemon_add_client_rfcomm_channel(connection, cid);
10472531c97eSMatthias Ringwald             }
10482531c97eSMatthias Ringwald             break;
10492531c97eSMatthias Ringwald         case RFCOMM_DISCONNECT:
10502531c97eSMatthias Ringwald             cid = little_endian_read_16(packet, 3);
10512531c97eSMatthias Ringwald             reason = packet[5];
10522531c97eSMatthias Ringwald             rfcomm_disconnect(cid);
10532531c97eSMatthias Ringwald             break;
10542531c97eSMatthias Ringwald         case RFCOMM_REGISTER_SERVICE:
10552531c97eSMatthias Ringwald             rfcomm_channel = packet[3];
10562531c97eSMatthias Ringwald             mtu = little_endian_read_16(packet, 4);
10572531c97eSMatthias Ringwald             status = rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel, mtu);
10582531c97eSMatthias Ringwald             rfcomm_emit_service_registered(connection, status, rfcomm_channel);
10592531c97eSMatthias Ringwald             break;
10602531c97eSMatthias Ringwald         case RFCOMM_REGISTER_SERVICE_WITH_CREDITS:
10612531c97eSMatthias Ringwald             rfcomm_channel = packet[3];
10622531c97eSMatthias Ringwald             mtu = little_endian_read_16(packet, 4);
10632531c97eSMatthias Ringwald             rfcomm_credits = packet[6];
10642531c97eSMatthias Ringwald             status = rfcomm_register_service_with_initial_credits(&rfcomm_packet_handler, rfcomm_channel, mtu, rfcomm_credits);
10652531c97eSMatthias Ringwald             rfcomm_emit_service_registered(connection, status, rfcomm_channel);
10662531c97eSMatthias Ringwald             break;
10672531c97eSMatthias Ringwald         case RFCOMM_UNREGISTER_SERVICE:
10682531c97eSMatthias Ringwald             service_channel = little_endian_read_16(packet, 3);
10692531c97eSMatthias Ringwald             daemon_remove_client_rfcomm_service(connection, service_channel);
10702531c97eSMatthias Ringwald             rfcomm_unregister_service(service_channel);
10712531c97eSMatthias Ringwald             break;
10722531c97eSMatthias Ringwald         case RFCOMM_ACCEPT_CONNECTION:
10732531c97eSMatthias Ringwald             cid    = little_endian_read_16(packet, 3);
10742531c97eSMatthias Ringwald             rfcomm_accept_connection(cid);
10752531c97eSMatthias Ringwald             break;
10762531c97eSMatthias Ringwald         case RFCOMM_DECLINE_CONNECTION:
10772531c97eSMatthias Ringwald             cid    = little_endian_read_16(packet, 3);
10782531c97eSMatthias Ringwald             reason = packet[7];
10792531c97eSMatthias Ringwald             rfcomm_decline_connection(cid);
10802531c97eSMatthias Ringwald             break;
10812531c97eSMatthias Ringwald         case RFCOMM_GRANT_CREDITS:
10822531c97eSMatthias Ringwald             cid    = little_endian_read_16(packet, 3);
10832531c97eSMatthias Ringwald             rfcomm_credits = packet[5];
10842531c97eSMatthias Ringwald             rfcomm_grant_credits(cid, rfcomm_credits);
10852531c97eSMatthias Ringwald             break;
10862531c97eSMatthias Ringwald         case RFCOMM_PERSISTENT_CHANNEL: {
10872531c97eSMatthias Ringwald             // enforce \0
10882531c97eSMatthias Ringwald             packet[3+248] = 0;
10892531c97eSMatthias Ringwald             rfcomm_channel = rfcomm_service_db_channel_for_service((char*)&packet[3]);
10901bb145c9SMatthias Ringwald             log_info("DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL %u", rfcomm_channel);
10912531c97eSMatthias Ringwald             uint8_t event[4];
10921bb145c9SMatthias Ringwald             event[0] = DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL;
10932531c97eSMatthias Ringwald             event[1] = sizeof(event) - 2;
10942531c97eSMatthias Ringwald             event[2] = 0;
10952531c97eSMatthias Ringwald             event[3] = rfcomm_channel;
10962531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
10972531c97eSMatthias Ringwald             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
10982531c97eSMatthias Ringwald             break;
10992531c97eSMatthias Ringwald         }
11002531c97eSMatthias Ringwald         case SDP_REGISTER_SERVICE_RECORD:
11012531c97eSMatthias Ringwald             log_info("SDP_REGISTER_SERVICE_RECORD size %u\n", size);
11022531c97eSMatthias Ringwald             service_record_handle = daemon_sdp_create_and_register_service(&packet[3]);
11032531c97eSMatthias Ringwald             if (service_record_handle){
11042531c97eSMatthias Ringwald                 daemon_add_client_sdp_service_record_handle(connection, service_record_handle);
11052531c97eSMatthias Ringwald                 sdp_emit_service_registered(connection, service_record_handle, 0);
11062531c97eSMatthias Ringwald             } else {
11072531c97eSMatthias Ringwald                sdp_emit_service_registered(connection, 0, BTSTACK_MEMORY_ALLOC_FAILED);
11082531c97eSMatthias Ringwald             }
11092531c97eSMatthias Ringwald             break;
11102531c97eSMatthias Ringwald         case SDP_UNREGISTER_SERVICE_RECORD:
11112531c97eSMatthias Ringwald             service_record_handle = little_endian_read_32(packet, 3);
11122531c97eSMatthias Ringwald             log_info("SDP_UNREGISTER_SERVICE_RECORD handle 0x%x ", service_record_handle);
11132531c97eSMatthias Ringwald             data = sdp_get_record_for_handle(service_record_handle);
11142531c97eSMatthias Ringwald             sdp_unregister_service(service_record_handle);
11152531c97eSMatthias Ringwald             daemon_remove_client_sdp_service_record_handle(connection, service_record_handle);
11162531c97eSMatthias Ringwald             if (data){
11172531c97eSMatthias Ringwald                 free(data);
11182531c97eSMatthias Ringwald             }
11192531c97eSMatthias Ringwald             break;
11202531c97eSMatthias Ringwald         case SDP_CLIENT_QUERY_RFCOMM_SERVICES:
11212531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
11222531c97eSMatthias Ringwald 
11232531c97eSMatthias Ringwald             serviceSearchPatternLen = de_get_len(&packet[9]);
11242531c97eSMatthias Ringwald             memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
11252531c97eSMatthias Ringwald 
11262531c97eSMatthias Ringwald             sdp_client_query_connection = connection;
11277fbb5f59SMatthias Ringwald             sdp_client_query_rfcomm_channel_and_name_for_search_pattern(&handle_sdp_rfcomm_service_result, addr, serviceSearchPattern);
11282531c97eSMatthias Ringwald 
11292531c97eSMatthias Ringwald             break;
11302531c97eSMatthias Ringwald         case SDP_CLIENT_QUERY_SERVICES:
11312531c97eSMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
11322531c97eSMatthias Ringwald             sdp_client_query_connection = connection;
11332531c97eSMatthias Ringwald 
11342531c97eSMatthias Ringwald             serviceSearchPatternLen = de_get_len(&packet[9]);
11352531c97eSMatthias Ringwald             memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
11362531c97eSMatthias Ringwald 
11372531c97eSMatthias Ringwald             attributeIDListLen = de_get_len(&packet[9+serviceSearchPatternLen]);
11382531c97eSMatthias Ringwald             memcpy(attributeIDList, &packet[9+serviceSearchPatternLen], attributeIDListLen);
11392531c97eSMatthias Ringwald 
11402531c97eSMatthias Ringwald             sdp_client_query(&handle_sdp_client_query_result, addr, (uint8_t*)&serviceSearchPattern[0], (uint8_t*)&attributeIDList[0]);
11412531c97eSMatthias Ringwald             break;
1142a046efa6SMatthias Ringwald #ifdef ENABLE_BLE
11432531c97eSMatthias Ringwald         case GAP_LE_SCAN_START:
11442531c97eSMatthias Ringwald             gap_start_scan();
11452531c97eSMatthias Ringwald             break;
11462531c97eSMatthias Ringwald         case GAP_LE_SCAN_STOP:
11472531c97eSMatthias Ringwald             gap_stop_scan();
11482531c97eSMatthias Ringwald             break;
11492531c97eSMatthias Ringwald         case GAP_LE_SET_SCAN_PARAMETERS:
11502531c97eSMatthias Ringwald             gap_set_scan_parameters(packet[3], little_endian_read_16(packet, 4), little_endian_read_16(packet, 6));
11512531c97eSMatthias Ringwald             break;
11522531c97eSMatthias Ringwald         case GAP_LE_CONNECT:
11532531c97eSMatthias Ringwald             reverse_bd_addr(&packet[4], addr);
11542531c97eSMatthias Ringwald             addr_type = packet[3];
11552531c97eSMatthias Ringwald             gap_connect(addr, addr_type);
11562531c97eSMatthias Ringwald             break;
11572531c97eSMatthias Ringwald         case GAP_LE_CONNECT_CANCEL:
11582531c97eSMatthias Ringwald             gap_connect_cancel();
11592531c97eSMatthias Ringwald             break;
11602531c97eSMatthias Ringwald         case GAP_DISCONNECT:
11612531c97eSMatthias Ringwald             handle = little_endian_read_16(packet, 3);
11622531c97eSMatthias Ringwald             gap_disconnect(handle);
11632531c97eSMatthias Ringwald             break;
1164a046efa6SMatthias Ringwald #endif
11652531c97eSMatthias Ringwald #if defined(HAVE_MALLOC) && defined(ENABLE_BLE)
11662531c97eSMatthias Ringwald         case GATT_DISCOVER_ALL_PRIMARY_SERVICES:
11672531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11682531c97eSMatthias Ringwald             if (!gatt_helper) break;
11692531c97eSMatthias Ringwald             gatt_client_discover_primary_services(&handle_gatt_client_event, gatt_helper->con_handle);
11702531c97eSMatthias Ringwald             break;
11712531c97eSMatthias Ringwald         case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16:
11722531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11732531c97eSMatthias Ringwald             if (!gatt_helper) break;
11742531c97eSMatthias Ringwald             gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, gatt_helper->con_handle, little_endian_read_16(packet, 5));
11752531c97eSMatthias Ringwald             break;
11762531c97eSMatthias Ringwald         case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128:
11772531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11782531c97eSMatthias Ringwald             if (!gatt_helper) break;
11792531c97eSMatthias Ringwald             reverse_128(&packet[5], uuid128);
11802531c97eSMatthias Ringwald             gatt_client_discover_primary_services_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, uuid128);
11812531c97eSMatthias Ringwald             break;
11822531c97eSMatthias Ringwald         case GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE:
11832531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11842531c97eSMatthias Ringwald             if (!gatt_helper) break;
11852531c97eSMatthias Ringwald             gatt_client_deserialize_service(packet, 5, &service);
11862531c97eSMatthias Ringwald             gatt_client_find_included_services_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
11872531c97eSMatthias Ringwald             break;
11882531c97eSMatthias Ringwald 
11892531c97eSMatthias Ringwald         case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE:
11902531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11912531c97eSMatthias Ringwald             if (!gatt_helper) break;
11922531c97eSMatthias Ringwald             gatt_client_deserialize_service(packet, 5, &service);
11932531c97eSMatthias Ringwald             gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
11942531c97eSMatthias Ringwald             break;
11952531c97eSMatthias Ringwald         case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128:
11962531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
11972531c97eSMatthias Ringwald             if (!gatt_helper) break;
11982531c97eSMatthias Ringwald             gatt_client_deserialize_service(packet, 5, &service);
11992531c97eSMatthias Ringwald             reverse_128(&packet[5 + SERVICE_LENGTH], uuid128);
12002531c97eSMatthias Ringwald             gatt_client_discover_characteristics_for_service_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, &service, uuid128);
12012531c97eSMatthias Ringwald             break;
12022531c97eSMatthias Ringwald         case GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
12032531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12042531c97eSMatthias Ringwald             if (!gatt_helper) break;
12052531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12062531c97eSMatthias Ringwald             gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
12072531c97eSMatthias Ringwald             break;
12082531c97eSMatthias Ringwald 
12092531c97eSMatthias Ringwald         case GATT_READ_VALUE_OF_CHARACTERISTIC:
12102531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12112531c97eSMatthias Ringwald             if (!gatt_helper) break;
12122531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12132531c97eSMatthias Ringwald             gatt_client_read_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
12142531c97eSMatthias Ringwald             break;
12152531c97eSMatthias Ringwald         case GATT_READ_LONG_VALUE_OF_CHARACTERISTIC:
12162531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12172531c97eSMatthias Ringwald             if (!gatt_helper) break;
12182531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12192531c97eSMatthias Ringwald             gatt_client_read_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
12202531c97eSMatthias Ringwald             break;
12212531c97eSMatthias Ringwald 
12222531c97eSMatthias Ringwald         case GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
12232531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 0);  // note: don't track active connection
12242531c97eSMatthias Ringwald             if (!gatt_helper) break;
12252531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12262531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
12272531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12282531c97eSMatthias Ringwald             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
12290ce46700SMatthias Ringwald             gatt_client_write_value_of_characteristic_without_response(gatt_helper->con_handle, characteristic.value_handle, data_length, data);
12302531c97eSMatthias Ringwald             break;
12312531c97eSMatthias Ringwald         case GATT_WRITE_VALUE_OF_CHARACTERISTIC:
12322531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12332531c97eSMatthias Ringwald             if (!gatt_helper) break;
12342531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12352531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
12362531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12372531c97eSMatthias Ringwald             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
12382531c97eSMatthias Ringwald             gatt_client_write_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
12392531c97eSMatthias Ringwald             break;
12402531c97eSMatthias Ringwald         case GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
12412531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12422531c97eSMatthias Ringwald             if (!gatt_helper) break;
12432531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12442531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
12452531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12462531c97eSMatthias Ringwald             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
12472531c97eSMatthias Ringwald             gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
12482531c97eSMatthias Ringwald             break;
12492531c97eSMatthias Ringwald         case GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
12502531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12512531c97eSMatthias Ringwald             if (!gatt_helper) break;
12522531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12532531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
12542531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12552531c97eSMatthias Ringwald             memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
12562531c97eSMatthias Ringwald             gatt_client_write_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, characteristic.value_handle, data_length, data);
12572531c97eSMatthias Ringwald             break;
12582531c97eSMatthias Ringwald         case GATT_READ_CHARACTERISTIC_DESCRIPTOR:
12592531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12602531c97eSMatthias Ringwald             if (!gatt_helper) break;
12612531c97eSMatthias Ringwald             handle = little_endian_read_16(packet, 3);
12622531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
12632531c97eSMatthias Ringwald             gatt_client_read_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
12642531c97eSMatthias Ringwald             break;
12652531c97eSMatthias Ringwald         case GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR:
12662531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12672531c97eSMatthias Ringwald             if (!gatt_helper) break;
12682531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
12692531c97eSMatthias Ringwald             gatt_client_read_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
12702531c97eSMatthias Ringwald             break;
12712531c97eSMatthias Ringwald 
12722531c97eSMatthias Ringwald         case GATT_WRITE_CHARACTERISTIC_DESCRIPTOR:
12732531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12742531c97eSMatthias Ringwald             if (!gatt_helper) break;
12752531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
12762531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12772531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
12782531c97eSMatthias Ringwald             gatt_client_write_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
12792531c97eSMatthias Ringwald             break;
12802531c97eSMatthias Ringwald         case GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR:
12812531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12822531c97eSMatthias Ringwald             if (!gatt_helper) break;
12832531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
12842531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12852531c97eSMatthias Ringwald             data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
12862531c97eSMatthias Ringwald             gatt_client_write_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
12872531c97eSMatthias Ringwald             break;
12882531c97eSMatthias Ringwald         case GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:{
12892531c97eSMatthias Ringwald             uint16_t configuration = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
12902531c97eSMatthias Ringwald             gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
12912531c97eSMatthias Ringwald             if (!gatt_helper) break;
12922531c97eSMatthias Ringwald             data = gatt_helper->characteristic_buffer;
12932531c97eSMatthias Ringwald             gatt_client_deserialize_characteristic(packet, 5, &characteristic);
12942531c97eSMatthias Ringwald             gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic, configuration);
12952531c97eSMatthias Ringwald             break;
12962531c97eSMatthias Ringwald         case GATT_GET_MTU:
12972531c97eSMatthias Ringwald             handle = little_endian_read_16(packet, 3);
12982531c97eSMatthias Ringwald             gatt_client_get_mtu(handle, &mtu);
12992531c97eSMatthias Ringwald             send_gatt_mtu_event(connection, handle, mtu);
13002531c97eSMatthias Ringwald             break;
13012531c97eSMatthias Ringwald         }
13022531c97eSMatthias Ringwald #endif
13032531c97eSMatthias Ringwald     default:
13042531c97eSMatthias Ringwald             log_error("Error: command %u not implemented:", READ_CMD_OCF(packet));
13052531c97eSMatthias Ringwald             break;
13062531c97eSMatthias Ringwald     }
13072531c97eSMatthias Ringwald 
13082531c97eSMatthias Ringwald     return 0;
13092531c97eSMatthias Ringwald }
13102531c97eSMatthias Ringwald 
13112531c97eSMatthias Ringwald static int daemon_client_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length){
13122531c97eSMatthias Ringwald 
13132531c97eSMatthias Ringwald     int err = 0;
13142531c97eSMatthias Ringwald     client_state_t * client;
13152531c97eSMatthias Ringwald 
13162531c97eSMatthias Ringwald     switch (packet_type){
13172531c97eSMatthias Ringwald         case HCI_COMMAND_DATA_PACKET:
13182531c97eSMatthias Ringwald             if (READ_CMD_OGF(data) != OGF_BTSTACK) {
13192531c97eSMatthias Ringwald                 // HCI Command
13202531c97eSMatthias Ringwald                 hci_send_cmd_packet(data, length);
13212531c97eSMatthias Ringwald             } else {
13222531c97eSMatthias Ringwald                 // BTstack command
13232531c97eSMatthias Ringwald                 btstack_command_handler(connection, data, length);
13242531c97eSMatthias Ringwald             }
13252531c97eSMatthias Ringwald             break;
13262531c97eSMatthias Ringwald         case L2CAP_DATA_PACKET:
13272531c97eSMatthias Ringwald             // process l2cap packet...
13282531c97eSMatthias Ringwald             err = l2cap_send(channel, data, length);
13292531c97eSMatthias Ringwald             break;
13302531c97eSMatthias Ringwald         case RFCOMM_DATA_PACKET:
13312531c97eSMatthias Ringwald             // process l2cap packet...
13322531c97eSMatthias Ringwald             err = rfcomm_send(channel, data, length);
13332531c97eSMatthias Ringwald             break;
13342531c97eSMatthias Ringwald         case DAEMON_EVENT_PACKET:
13352531c97eSMatthias Ringwald             switch (data[0]) {
13362531c97eSMatthias Ringwald                 case DAEMON_EVENT_CONNECTION_OPENED:
13372531c97eSMatthias Ringwald                     log_info("DAEMON_EVENT_CONNECTION_OPENED %p\n",connection);
13382531c97eSMatthias Ringwald 
1339af0d0df5SMatthias Ringwald                     client = calloc(sizeof(client_state_t), 1);
13402531c97eSMatthias Ringwald                     if (!client) break; // fail
13412531c97eSMatthias Ringwald                     client->connection   = connection;
13422531c97eSMatthias Ringwald                     client->power_mode   = HCI_POWER_OFF;
13432531c97eSMatthias Ringwald                     client->discoverable = 0;
13442531c97eSMatthias Ringwald                     btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
13452531c97eSMatthias Ringwald                     break;
13462531c97eSMatthias Ringwald                 case DAEMON_EVENT_CONNECTION_CLOSED:
13472531c97eSMatthias Ringwald                     log_info("DAEMON_EVENT_CONNECTION_CLOSED %p\n",connection);
13482531c97eSMatthias Ringwald                     daemon_disconnect_client(connection);
13492531c97eSMatthias Ringwald                     // no clients -> no HCI connections
13502531c97eSMatthias Ringwald                     if (!clients){
13512531c97eSMatthias Ringwald                         hci_disconnect_all();
13522531c97eSMatthias Ringwald                     }
13532531c97eSMatthias Ringwald 
13542531c97eSMatthias Ringwald                     // update discoverable mode
13552531c97eSMatthias Ringwald                     gap_discoverable_control(clients_require_discoverable());
13562531c97eSMatthias Ringwald                     // start power off, if last active client
13572531c97eSMatthias Ringwald                     if (!clients_require_power_on()){
13582531c97eSMatthias Ringwald                         start_power_off_timer();
13592531c97eSMatthias Ringwald                     }
13602531c97eSMatthias Ringwald                     break;
13612531c97eSMatthias Ringwald                 default:
13622531c97eSMatthias Ringwald                     break;
13632531c97eSMatthias Ringwald             }
13642531c97eSMatthias Ringwald             break;
13652531c97eSMatthias Ringwald     }
13662531c97eSMatthias Ringwald     if (err) {
13672531c97eSMatthias Ringwald         log_info("Daemon Handler: err %d\n", err);
13682531c97eSMatthias Ringwald     }
13692531c97eSMatthias Ringwald     return err;
13702531c97eSMatthias Ringwald }
13712531c97eSMatthias Ringwald 
13722531c97eSMatthias Ringwald 
13732531c97eSMatthias Ringwald static void daemon_set_logging_enabled(int enabled){
13742531c97eSMatthias Ringwald     if (enabled && !loggingEnabled){
13752531c97eSMatthias Ringwald         hci_dump_open(BTSTACK_LOG_FILE, BTSTACK_LOG_TYPE);
13762531c97eSMatthias Ringwald     }
13772531c97eSMatthias Ringwald     if (!enabled && loggingEnabled){
13782531c97eSMatthias Ringwald         hci_dump_close();
13792531c97eSMatthias Ringwald     }
13802531c97eSMatthias Ringwald     loggingEnabled = enabled;
13812531c97eSMatthias Ringwald }
13822531c97eSMatthias Ringwald 
13832531c97eSMatthias Ringwald // local cache used to manage UI status
13842531c97eSMatthias Ringwald static HCI_STATE hci_state = HCI_STATE_OFF;
13852531c97eSMatthias Ringwald static int num_connections = 0;
13862531c97eSMatthias Ringwald static void update_ui_status(void){
13872531c97eSMatthias Ringwald     if (hci_state != HCI_STATE_WORKING) {
13882531c97eSMatthias Ringwald         bluetooth_status_handler(BLUETOOTH_OFF);
13892531c97eSMatthias Ringwald     } else {
13902531c97eSMatthias Ringwald         if (num_connections) {
13912531c97eSMatthias Ringwald             bluetooth_status_handler(BLUETOOTH_ACTIVE);
13922531c97eSMatthias Ringwald         } else {
13932531c97eSMatthias Ringwald             bluetooth_status_handler(BLUETOOTH_ON);
13942531c97eSMatthias Ringwald         }
13952531c97eSMatthias Ringwald     }
13962531c97eSMatthias Ringwald }
13972531c97eSMatthias Ringwald 
13982531c97eSMatthias Ringwald #ifdef USE_SPRINGBOARD
13992531c97eSMatthias Ringwald static void preferences_changed_callback(void){
14002531c97eSMatthias Ringwald     int logging = platform_iphone_logging_enabled();
14012531c97eSMatthias Ringwald     log_info("Logging enabled: %u\n", logging);
14022531c97eSMatthias Ringwald     daemon_set_logging_enabled(logging);
14032531c97eSMatthias Ringwald }
14042531c97eSMatthias Ringwald #endif
14052531c97eSMatthias Ringwald 
14062531c97eSMatthias Ringwald static void deamon_status_event_handler(uint8_t *packet, uint16_t size){
14072531c97eSMatthias Ringwald 
14082531c97eSMatthias Ringwald     uint8_t update_status = 0;
14092531c97eSMatthias Ringwald 
14102531c97eSMatthias Ringwald     // handle state event
14110e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
14122531c97eSMatthias Ringwald         case BTSTACK_EVENT_STATE:
14132531c97eSMatthias Ringwald             hci_state = packet[2];
14142531c97eSMatthias Ringwald             log_info("New state: %u\n", hci_state);
14152531c97eSMatthias Ringwald             update_status = 1;
14162531c97eSMatthias Ringwald             break;
14172531c97eSMatthias Ringwald         case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
14182531c97eSMatthias Ringwald             num_connections = packet[2];
14192531c97eSMatthias Ringwald             log_info("New nr connections: %u\n", num_connections);
14202531c97eSMatthias Ringwald             update_status = 1;
14212531c97eSMatthias Ringwald             break;
14222531c97eSMatthias Ringwald         default:
14232531c97eSMatthias Ringwald             break;
14242531c97eSMatthias Ringwald     }
14252531c97eSMatthias Ringwald 
14262531c97eSMatthias Ringwald     // choose full bluetooth state
14272531c97eSMatthias Ringwald     if (update_status) {
14282531c97eSMatthias Ringwald         update_ui_status();
14292531c97eSMatthias Ringwald     }
14302531c97eSMatthias Ringwald }
14312531c97eSMatthias Ringwald 
14322531c97eSMatthias Ringwald static void daemon_retry_parked(void){
14332531c97eSMatthias Ringwald 
14342531c97eSMatthias Ringwald     // socket_connection_retry_parked is not reentrant
14352531c97eSMatthias Ringwald     static int retry_mutex = 0;
14362531c97eSMatthias Ringwald 
14372531c97eSMatthias Ringwald     // lock mutex
14382531c97eSMatthias Ringwald     if (retry_mutex) return;
14392531c97eSMatthias Ringwald     retry_mutex = 1;
14402531c97eSMatthias Ringwald 
14412531c97eSMatthias Ringwald     // ... try sending again
14422531c97eSMatthias Ringwald     socket_connection_retry_parked();
14432531c97eSMatthias Ringwald 
14442531c97eSMatthias Ringwald     // unlock mutex
14452531c97eSMatthias Ringwald     retry_mutex = 0;
14462531c97eSMatthias Ringwald }
14472531c97eSMatthias Ringwald 
14482531c97eSMatthias Ringwald #if 0
14492531c97eSMatthias Ringwald 
14502531c97eSMatthias Ringwald Minimal Code for LE Peripheral
14512531c97eSMatthias Ringwald 
14522531c97eSMatthias Ringwald enum {
14532531c97eSMatthias Ringwald     SET_ADVERTISEMENT_PARAMS = 1 << 0,
14542531c97eSMatthias Ringwald     SET_ADVERTISEMENT_DATA   = 1 << 1,
14552531c97eSMatthias Ringwald     ENABLE_ADVERTISEMENTS    = 1 << 2,
14562531c97eSMatthias Ringwald };
14572531c97eSMatthias Ringwald 
14582531c97eSMatthias Ringwald const uint8_t adv_data[] = {
14592531c97eSMatthias Ringwald     // Flags general discoverable
14602531c97eSMatthias Ringwald     0x02, 0x01, 0x02,
14612531c97eSMatthias Ringwald     // Name
14622531c97eSMatthias Ringwald     0x08, 0x09, 'B', 'T', 's', 't', 'a', 'c', 'k'
14632531c97eSMatthias Ringwald };
14642531c97eSMatthias Ringwald uint8_t adv_data_len = sizeof(adv_data);
14652531c97eSMatthias Ringwald static uint16_t todos = 0;
14662531c97eSMatthias Ringwald 
14672531c97eSMatthias Ringwald static void app_run(void){
14682531c97eSMatthias Ringwald 
14692531c97eSMatthias Ringwald     if (!hci_can_send_command_packet_now()) return;
14702531c97eSMatthias Ringwald 
14712531c97eSMatthias Ringwald     if (todos & SET_ADVERTISEMENT_DATA){
14722531c97eSMatthias Ringwald         log_info("app_run: set advertisement data\n");
14732531c97eSMatthias Ringwald         todos &= ~SET_ADVERTISEMENT_DATA;
14742531c97eSMatthias Ringwald         hci_send_cmd(&hci_le_set_advertising_data, adv_data_len, adv_data);
14752531c97eSMatthias Ringwald         return;
14762531c97eSMatthias Ringwald     }
14772531c97eSMatthias Ringwald 
14782531c97eSMatthias Ringwald     if (todos & SET_ADVERTISEMENT_PARAMS){
14792531c97eSMatthias Ringwald         todos &= ~SET_ADVERTISEMENT_PARAMS;
14802531c97eSMatthias Ringwald         uint8_t adv_type = 0;   // default
14812531c97eSMatthias Ringwald         bd_addr_t null_addr;
14822531c97eSMatthias Ringwald         memset(null_addr, 0, 6);
14832531c97eSMatthias Ringwald         uint16_t adv_int_min = 0x0030;
14842531c97eSMatthias Ringwald         uint16_t adv_int_max = 0x0030;
14852531c97eSMatthias Ringwald         hci_send_cmd(&hci_le_set_advertising_parameters, adv_int_min, adv_int_max, adv_type, 0, 0, &null_addr, 0x07, 0x00);
14862531c97eSMatthias Ringwald         return;
14872531c97eSMatthias Ringwald     }
14882531c97eSMatthias Ringwald 
14892531c97eSMatthias Ringwald     if (todos & ENABLE_ADVERTISEMENTS){
14902531c97eSMatthias Ringwald         log_info("app_run: enable advertisements\n");
14912531c97eSMatthias Ringwald         todos &= ~ENABLE_ADVERTISEMENTS;
14922531c97eSMatthias Ringwald         hci_send_cmd(&hci_le_set_advertise_enable, 1);
14932531c97eSMatthias Ringwald         return;
14942531c97eSMatthias Ringwald     }
14952531c97eSMatthias Ringwald }
14962531c97eSMatthias Ringwald #endif
14972531c97eSMatthias Ringwald 
14982531c97eSMatthias Ringwald static void daemon_emit_packet(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
14992531c97eSMatthias Ringwald     if (connection) {
15002531c97eSMatthias Ringwald         socket_connection_send_packet(connection, packet_type, channel, packet, size);
15012531c97eSMatthias Ringwald     } else {
15022531c97eSMatthias Ringwald         socket_connection_send_packet_all(packet_type, channel, packet, size);
15032531c97eSMatthias Ringwald     }
15042531c97eSMatthias Ringwald }
15052531c97eSMatthias Ringwald 
15062531c97eSMatthias Ringwald static uint8_t remote_name_event[2+1+6+DEVICE_NAME_LEN+1]; // +1 for \0 in log_info
15072531c97eSMatthias Ringwald static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
15082531c97eSMatthias Ringwald     uint16_t cid;
15092531c97eSMatthias Ringwald     int i;
15102531c97eSMatthias Ringwald     bd_addr_t addr;
15112531c97eSMatthias Ringwald     switch (packet_type) {
15122531c97eSMatthias Ringwald         case HCI_EVENT_PACKET:
15132531c97eSMatthias Ringwald             deamon_status_event_handler(packet, size);
15140e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)){
15152531c97eSMatthias Ringwald 
15162531c97eSMatthias Ringwald                 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
15172531c97eSMatthias Ringwald                     // ACL buffer freed...
15182531c97eSMatthias Ringwald                     daemon_retry_parked();
15192531c97eSMatthias Ringwald                     // no need to tell clients
15202531c97eSMatthias Ringwald                     return;
15212531c97eSMatthias Ringwald 
15222531c97eSMatthias Ringwald                 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
15232531c97eSMatthias Ringwald                     if (!btstack_device_name_db) break;
15242531c97eSMatthias Ringwald                     if (packet[2]) break; // status not ok
15252531c97eSMatthias Ringwald 
15262531c97eSMatthias Ringwald                     reverse_bd_addr(&packet[3], addr);
15272531c97eSMatthias Ringwald                     // fix for invalid remote names - terminate on 0xff
15282531c97eSMatthias Ringwald                     for (i=0; i<248;i++){
15292531c97eSMatthias Ringwald                         if (packet[9+i] == 0xff){
15302531c97eSMatthias Ringwald                             packet[9+i] = 0;
15312531c97eSMatthias Ringwald                             break;
15322531c97eSMatthias Ringwald                         }
15332531c97eSMatthias Ringwald                     }
15342531c97eSMatthias Ringwald                     packet[9+248] = 0;
15352531c97eSMatthias Ringwald                     btstack_device_name_db->put_name(addr, (device_name_t *)&packet[9]);
15362531c97eSMatthias Ringwald                     break;
15372531c97eSMatthias Ringwald 
15382531c97eSMatthias Ringwald                 case HCI_EVENT_INQUIRY_RESULT:
15392531c97eSMatthias Ringwald                 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
15402531c97eSMatthias Ringwald                     if (!btstack_device_name_db) break;
15412531c97eSMatthias Ringwald 
15422531c97eSMatthias Ringwald                     // first send inq result packet
15432531c97eSMatthias Ringwald                     daemon_emit_packet(connection, packet_type, channel, packet, size);
15442531c97eSMatthias Ringwald 
15452531c97eSMatthias Ringwald                     // then send cached remote names
15462531c97eSMatthias Ringwald                     int offset = 3;
15472531c97eSMatthias Ringwald                     for (i=0; i<packet[2];i++){
15482531c97eSMatthias Ringwald                         reverse_bd_addr(&packet[offset], addr);
15492531c97eSMatthias Ringwald                         if (btstack_device_name_db->get_name(addr, (device_name_t *) &remote_name_event[9])){
15502531c97eSMatthias Ringwald                             remote_name_event[0] = DAEMON_EVENT_REMOTE_NAME_CACHED;
15512531c97eSMatthias Ringwald                             remote_name_event[1] = sizeof(remote_name_event) - 2 - 1;
15522531c97eSMatthias Ringwald                             remote_name_event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
15532531c97eSMatthias Ringwald                             reverse_bd_addr(addr, &remote_name_event[3]);
15542531c97eSMatthias Ringwald 
15552531c97eSMatthias Ringwald                             remote_name_event[9+248] = 0;   // assert \0 for log_info
15562531c97eSMatthias Ringwald                             log_info("DAEMON_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(addr), &remote_name_event[9]);
15572531c97eSMatthias Ringwald                             hci_dump_packet(HCI_EVENT_PACKET, 0, remote_name_event, sizeof(remote_name_event)-1);
15582531c97eSMatthias Ringwald                             daemon_emit_packet(connection, HCI_EVENT_PACKET, channel, remote_name_event, sizeof(remote_name_event) -1);
15592531c97eSMatthias Ringwald                         }
15602531c97eSMatthias Ringwald                         offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
15612531c97eSMatthias Ringwald                     }
15622531c97eSMatthias Ringwald                     return;
15632531c97eSMatthias Ringwald                 }
15642531c97eSMatthias Ringwald 
156562c64df1SMatthias Ringwald                 case DAEMON_EVENT_RFCOMM_CREDITS:
15662531c97eSMatthias Ringwald                     // RFCOMM CREDITS received...
15672531c97eSMatthias Ringwald                     daemon_retry_parked();
15682531c97eSMatthias Ringwald                     break;
15692531c97eSMatthias Ringwald 
1570f8f6a918SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_OPENED:
15712531c97eSMatthias Ringwald                     cid = little_endian_read_16(packet, 13);
15722531c97eSMatthias Ringwald                     connection = connection_for_rfcomm_cid(cid);
15732531c97eSMatthias Ringwald                     if (!connection) break;
15742531c97eSMatthias Ringwald                     if (packet[2]) {
15752531c97eSMatthias Ringwald                         daemon_remove_client_rfcomm_channel(connection, cid);
15762531c97eSMatthias Ringwald                     } else {
15772531c97eSMatthias Ringwald                         daemon_add_client_rfcomm_channel(connection, cid);
15782531c97eSMatthias Ringwald                     }
15792531c97eSMatthias Ringwald                     break;
15802531c97eSMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
15812531c97eSMatthias Ringwald                     cid = little_endian_read_16(packet, 2);
15822531c97eSMatthias Ringwald                     connection = connection_for_rfcomm_cid(cid);
15832531c97eSMatthias Ringwald                     if (!connection) break;
15842531c97eSMatthias Ringwald                     daemon_remove_client_rfcomm_channel(connection, cid);
15852531c97eSMatthias Ringwald                     break;
158662c64df1SMatthias Ringwald                 case DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED:
15872531c97eSMatthias Ringwald                     if (packet[2]) break;
15882531c97eSMatthias Ringwald                     daemon_add_client_rfcomm_service(connection, packet[3]);
15892531c97eSMatthias Ringwald                     break;
15902531c97eSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
15912531c97eSMatthias Ringwald                     cid = little_endian_read_16(packet, 13);
15922531c97eSMatthias Ringwald                     connection = connection_for_l2cap_cid(cid);
15932531c97eSMatthias Ringwald                     if (!connection) break;
15942531c97eSMatthias Ringwald                     if (packet[2]) {
15952531c97eSMatthias Ringwald                         daemon_remove_client_l2cap_channel(connection, cid);
15962531c97eSMatthias Ringwald                     } else {
15972531c97eSMatthias Ringwald                         daemon_add_client_l2cap_channel(connection, cid);
15982531c97eSMatthias Ringwald                     }
15992531c97eSMatthias Ringwald                     break;
16002531c97eSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
16012531c97eSMatthias Ringwald                     cid = little_endian_read_16(packet, 2);
16022531c97eSMatthias Ringwald                     connection = connection_for_l2cap_cid(cid);
16032531c97eSMatthias Ringwald                     if (!connection) break;
16042531c97eSMatthias Ringwald                     daemon_remove_client_l2cap_channel(connection, cid);
16052531c97eSMatthias Ringwald                     break;
16062531c97eSMatthias Ringwald #if defined(ENABLE_BLE) && defined(HAVE_MALLOC)
16072531c97eSMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
16082531c97eSMatthias Ringwald                     log_info("daemon : ignore HCI_EVENT_DISCONNECTION_COMPLETE ingnoring.");
16092531c97eSMatthias Ringwald                     // note: moved to gatt_client_handler because it's received here prematurely
16102531c97eSMatthias Ringwald                     // daemon_remove_gatt_client_helper(little_endian_read_16(packet, 3));
16112531c97eSMatthias Ringwald                     break;
16122531c97eSMatthias Ringwald #endif
16132531c97eSMatthias Ringwald                 default:
16142531c97eSMatthias Ringwald                     break;
16152531c97eSMatthias Ringwald             }
16162531c97eSMatthias Ringwald             break;
16172531c97eSMatthias Ringwald         case L2CAP_DATA_PACKET:
16182531c97eSMatthias Ringwald             connection = connection_for_l2cap_cid(channel);
16192531c97eSMatthias Ringwald             if (!connection) return;
16202531c97eSMatthias Ringwald             break;
16212531c97eSMatthias Ringwald         case RFCOMM_DATA_PACKET:
16222531c97eSMatthias Ringwald             connection = connection_for_l2cap_cid(channel);
16232531c97eSMatthias Ringwald             if (!connection) return;
16242531c97eSMatthias Ringwald             break;
16252531c97eSMatthias Ringwald         default:
16262531c97eSMatthias Ringwald             break;
16272531c97eSMatthias Ringwald     }
16282531c97eSMatthias Ringwald 
16292531c97eSMatthias Ringwald     daemon_emit_packet(connection, packet_type, channel, packet, size);
16302531c97eSMatthias Ringwald }
16312531c97eSMatthias Ringwald 
16322531c97eSMatthias Ringwald static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
16332531c97eSMatthias Ringwald     daemon_packet_handler(NULL, packet_type, channel, packet, size);
16342531c97eSMatthias Ringwald }
16352531c97eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
16362531c97eSMatthias Ringwald     daemon_packet_handler(NULL, packet_type, channel, packet, size);
16372531c97eSMatthias Ringwald }
16382531c97eSMatthias Ringwald 
16392531c97eSMatthias Ringwald static void handle_sdp_rfcomm_service_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
16400e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
16412531c97eSMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
16422531c97eSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
16432531c97eSMatthias Ringwald             // already HCI Events, just forward them
16442531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
16452531c97eSMatthias Ringwald             socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, size);
16462531c97eSMatthias Ringwald             break;
16472531c97eSMatthias Ringwald         default:
16482531c97eSMatthias Ringwald             break;
16492531c97eSMatthias Ringwald     }
16502531c97eSMatthias Ringwald }
16512531c97eSMatthias Ringwald 
16522531c97eSMatthias Ringwald static void sdp_client_assert_buffer(int size){
16532531c97eSMatthias Ringwald     if (size > attribute_value_buffer_size){
16542531c97eSMatthias Ringwald         log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
16552531c97eSMatthias Ringwald     }
16562531c97eSMatthias Ringwald }
16572531c97eSMatthias Ringwald 
16582531c97eSMatthias Ringwald // define new packet type SDP_CLIENT_PACKET
16592531c97eSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
16602531c97eSMatthias Ringwald     int event_len;
16612531c97eSMatthias Ringwald 
16620e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
16632531c97eSMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_BYTE:
16642531c97eSMatthias Ringwald             sdp_client_assert_buffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
16652531c97eSMatthias Ringwald             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
16662531c97eSMatthias Ringwald             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
16672531c97eSMatthias Ringwald                 log_info_hexdump(attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet));
16682531c97eSMatthias Ringwald 
16692531c97eSMatthias Ringwald                 int event_len = 1 + 3 * 2 + sdp_event_query_attribute_byte_get_attribute_length(packet);
16702531c97eSMatthias Ringwald                 uint8_t event[event_len];
16712531c97eSMatthias Ringwald                 event[0] = SDP_EVENT_QUERY_ATTRIBUTE_VALUE;
16722531c97eSMatthias Ringwald                 little_endian_store_16(event, 1, sdp_event_query_attribute_byte_get_record_id(packet));
16732531c97eSMatthias Ringwald                 little_endian_store_16(event, 3, sdp_event_query_attribute_byte_get_attribute_id(packet));
16742531c97eSMatthias Ringwald                 little_endian_store_16(event, 5, (uint16_t)sdp_event_query_attribute_byte_get_attribute_length(packet));
16752531c97eSMatthias Ringwald                 memcpy(&event[7], attribute_value, sdp_event_query_attribute_byte_get_attribute_length(packet));
16762531c97eSMatthias Ringwald                 hci_dump_packet(SDP_CLIENT_PACKET, 0, event, event_len);
16772531c97eSMatthias Ringwald                 socket_connection_send_packet(sdp_client_query_connection, SDP_CLIENT_PACKET, 0, event, event_len);
16782531c97eSMatthias Ringwald             }
16792531c97eSMatthias Ringwald             break;
16802531c97eSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
16812531c97eSMatthias Ringwald             event_len = packet[1] + 2;
16822531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, event_len);
16832531c97eSMatthias Ringwald             socket_connection_send_packet(sdp_client_query_connection, HCI_EVENT_PACKET, 0, packet, event_len);
16842531c97eSMatthias Ringwald             break;
16852531c97eSMatthias Ringwald     }
16862531c97eSMatthias Ringwald }
16872531c97eSMatthias Ringwald 
16882531c97eSMatthias Ringwald static void power_notification_callback(POWER_NOTIFICATION_t notification){
16892531c97eSMatthias Ringwald     switch (notification) {
16902531c97eSMatthias Ringwald         case POWER_WILL_SLEEP:
16912531c97eSMatthias Ringwald             // let's sleep
16922531c97eSMatthias Ringwald             power_management_sleep = 1;
16932531c97eSMatthias Ringwald             hci_power_control(HCI_POWER_SLEEP);
16942531c97eSMatthias Ringwald             break;
16952531c97eSMatthias Ringwald         case POWER_WILL_WAKE_UP:
16962531c97eSMatthias Ringwald             // assume that all clients use Bluetooth -> if connection, start Bluetooth
16972531c97eSMatthias Ringwald             power_management_sleep = 0;
16982531c97eSMatthias Ringwald             if (clients_require_power_on()) {
16992531c97eSMatthias Ringwald                 hci_power_control(HCI_POWER_ON);
17002531c97eSMatthias Ringwald             }
17012531c97eSMatthias Ringwald             break;
17022531c97eSMatthias Ringwald         default:
17032531c97eSMatthias Ringwald             break;
17042531c97eSMatthias Ringwald     }
17052531c97eSMatthias Ringwald }
17062531c97eSMatthias Ringwald 
17072531c97eSMatthias Ringwald static void daemon_sigint_handler(int param){
17082531c97eSMatthias Ringwald 
17092531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
17102531c97eSMatthias Ringwald     // notify daemons
17112531c97eSMatthias Ringwald     notify_post("ch.ringwald.btstack.stopped");
17122531c97eSMatthias Ringwald #endif
17132531c97eSMatthias Ringwald 
17142531c97eSMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
17152531c97eSMatthias Ringwald 
17162531c97eSMatthias Ringwald     hci_power_control( HCI_POWER_OFF);
17172531c97eSMatthias Ringwald     hci_close();
17182531c97eSMatthias Ringwald 
17192531c97eSMatthias Ringwald     log_info("Good bye, see you.\n");
17202531c97eSMatthias Ringwald 
17212531c97eSMatthias Ringwald     exit(0);
17222531c97eSMatthias Ringwald }
17232531c97eSMatthias Ringwald 
17242531c97eSMatthias Ringwald // MARK: manage power off timer
17252531c97eSMatthias Ringwald 
17262531c97eSMatthias Ringwald #define USE_POWER_OFF_TIMER
17272531c97eSMatthias Ringwald 
17282531c97eSMatthias Ringwald static void stop_power_off_timer(void){
17292531c97eSMatthias Ringwald #ifdef USE_POWER_OFF_TIMER
17302531c97eSMatthias Ringwald     if (timeout_active) {
17312531c97eSMatthias Ringwald         btstack_run_loop_remove_timer(&timeout);
17322531c97eSMatthias Ringwald         timeout_active = 0;
17332531c97eSMatthias Ringwald     }
17342531c97eSMatthias Ringwald #endif
17352531c97eSMatthias Ringwald }
17362531c97eSMatthias Ringwald 
17372531c97eSMatthias Ringwald static void start_power_off_timer(void){
17382531c97eSMatthias Ringwald #ifdef USE_POWER_OFF_TIMER
17392531c97eSMatthias Ringwald     stop_power_off_timer();
17402531c97eSMatthias Ringwald     btstack_run_loop_set_timer(&timeout, DAEMON_NO_ACTIVE_CLIENT_TIMEOUT);
17412531c97eSMatthias Ringwald     btstack_run_loop_add_timer(&timeout);
17422531c97eSMatthias Ringwald     timeout_active = 1;
17432531c97eSMatthias Ringwald #else
17442531c97eSMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
17452531c97eSMatthias Ringwald #endif
17462531c97eSMatthias Ringwald }
17472531c97eSMatthias Ringwald 
17482531c97eSMatthias Ringwald // MARK: manage list of clients
17492531c97eSMatthias Ringwald 
17502531c97eSMatthias Ringwald 
17512531c97eSMatthias Ringwald static client_state_t * client_for_connection(connection_t *connection) {
17522531c97eSMatthias Ringwald     btstack_linked_item_t *it;
17532531c97eSMatthias Ringwald     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
17542531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) it;
17552531c97eSMatthias Ringwald         if (client_state->connection == connection) {
17562531c97eSMatthias Ringwald             return client_state;
17572531c97eSMatthias Ringwald         }
17582531c97eSMatthias Ringwald     }
17592531c97eSMatthias Ringwald     return NULL;
17602531c97eSMatthias Ringwald }
17612531c97eSMatthias Ringwald 
17622531c97eSMatthias Ringwald static void clients_clear_power_request(void){
17632531c97eSMatthias Ringwald     btstack_linked_item_t *it;
17642531c97eSMatthias Ringwald     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
17652531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) it;
17662531c97eSMatthias Ringwald         client_state->power_mode = HCI_POWER_OFF;
17672531c97eSMatthias Ringwald     }
17682531c97eSMatthias Ringwald }
17692531c97eSMatthias Ringwald 
17702531c97eSMatthias Ringwald static int clients_require_power_on(void){
17712531c97eSMatthias Ringwald 
17722531c97eSMatthias Ringwald     if (global_enable) return 1;
17732531c97eSMatthias Ringwald 
17742531c97eSMatthias Ringwald     btstack_linked_item_t *it;
17752531c97eSMatthias Ringwald     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
17762531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) it;
17772531c97eSMatthias Ringwald         if (client_state->power_mode == HCI_POWER_ON) {
17782531c97eSMatthias Ringwald             return 1;
17792531c97eSMatthias Ringwald         }
17802531c97eSMatthias Ringwald     }
17812531c97eSMatthias Ringwald     return 0;
17822531c97eSMatthias Ringwald }
17832531c97eSMatthias Ringwald 
17842531c97eSMatthias Ringwald static int clients_require_discoverable(void){
17852531c97eSMatthias Ringwald     btstack_linked_item_t *it;
17862531c97eSMatthias Ringwald     for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
17872531c97eSMatthias Ringwald         client_state_t * client_state = (client_state_t *) it;
17882531c97eSMatthias Ringwald         if (client_state->discoverable) {
17892531c97eSMatthias Ringwald             return 1;
17902531c97eSMatthias Ringwald         }
17912531c97eSMatthias Ringwald     }
17922531c97eSMatthias Ringwald     return 0;
17932531c97eSMatthias Ringwald }
17942531c97eSMatthias Ringwald 
17952531c97eSMatthias Ringwald static void usage(const char * name) {
17962531c97eSMatthias Ringwald     printf("%s, BTstack background daemon\n", name);
17972531c97eSMatthias Ringwald     printf("usage: %s [--help] [--tcp port]\n", name);
17982531c97eSMatthias Ringwald     printf("    --help   display this usage\n");
17992531c97eSMatthias Ringwald     printf("    --tcp    use TCP server on port %u\n", BTSTACK_PORT);
18002531c97eSMatthias Ringwald     printf("Without the --tcp option, BTstack daemon is listening on unix domain socket %s\n\n", BTSTACK_UNIX);
18012531c97eSMatthias Ringwald }
18022531c97eSMatthias Ringwald 
18032531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
18042531c97eSMatthias Ringwald static void * btstack_run_loop_thread(void *context){
18052531c97eSMatthias Ringwald     btstack_run_loop_execute();
18062531c97eSMatthias Ringwald     return NULL;
18072531c97eSMatthias Ringwald }
18082531c97eSMatthias Ringwald #endif
18092531c97eSMatthias Ringwald 
18102531c97eSMatthias Ringwald #ifdef ENABLE_BLE
18112531c97eSMatthias Ringwald 
18122531c97eSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
18132531c97eSMatthias Ringwald 
18142531c97eSMatthias Ringwald     // hack: handle disconnection_complete_here instead of main hci event packet handler
18152531c97eSMatthias Ringwald     // we receive a HCI event packet in disguise
18160e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){
18172531c97eSMatthias Ringwald         log_info("daemon hack: handle disconnection_complete in handle_gatt_client_event instead of main hci event packet handler");
1818fc64f94aSMatthias Ringwald         hci_con_handle_t con_handle = little_endian_read_16(packet, 3);
1819fc64f94aSMatthias Ringwald         daemon_remove_gatt_client_helper(con_handle);
18202531c97eSMatthias Ringwald         return;
18212531c97eSMatthias Ringwald     }
18222531c97eSMatthias Ringwald 
18232531c97eSMatthias Ringwald     // only handle GATT Events
18240e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
18252531c97eSMatthias Ringwald         case GATT_EVENT_SERVICE_QUERY_RESULT:
18262531c97eSMatthias Ringwald         case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:
18272531c97eSMatthias Ringwald         case GATT_EVENT_NOTIFICATION:
18282531c97eSMatthias Ringwald         case GATT_EVENT_INDICATION:
18292531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
18302531c97eSMatthias Ringwald         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
18312531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
18322531c97eSMatthias Ringwald         case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
18332531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
18342531c97eSMatthias Ringwald         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
18352531c97eSMatthias Ringwald         case GATT_EVENT_QUERY_COMPLETE:
18362531c97eSMatthias Ringwald            break;
18372531c97eSMatthias Ringwald         default:
18382531c97eSMatthias Ringwald             return;
18392531c97eSMatthias Ringwald     }
18402531c97eSMatthias Ringwald 
1841711e6c80SMatthias Ringwald     hci_con_handle_t con_handle = little_endian_read_16(packet, 2);
18422531c97eSMatthias Ringwald     btstack_linked_list_gatt_client_helper_t * gatt_client_helper = daemon_get_gatt_client_helper(con_handle);
18432531c97eSMatthias Ringwald     if (!gatt_client_helper){
18442531c97eSMatthias Ringwald         log_info("daemon handle_gatt_client_event: gc helper for handle 0x%2x is NULL.", con_handle);
18452531c97eSMatthias Ringwald         return;
18462531c97eSMatthias Ringwald     }
18472531c97eSMatthias Ringwald 
18482531c97eSMatthias Ringwald     connection_t *connection = NULL;
18492531c97eSMatthias Ringwald 
18502531c97eSMatthias Ringwald     // daemon doesn't track which connection subscribed to this particular handle, so we just notify all connections
18510e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
18522531c97eSMatthias Ringwald         case GATT_EVENT_NOTIFICATION:
18532531c97eSMatthias Ringwald         case GATT_EVENT_INDICATION:{
18542531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
18552531c97eSMatthias Ringwald 
18562531c97eSMatthias Ringwald             btstack_linked_item_t *it;
18572531c97eSMatthias Ringwald             for (it = (btstack_linked_item_t *) clients; it ; it = it->next){
18582531c97eSMatthias Ringwald                 client_state_t * client_state = (client_state_t *) it;
18592531c97eSMatthias Ringwald                 socket_connection_send_packet(client_state->connection, HCI_EVENT_PACKET, 0, packet, size);
18602531c97eSMatthias Ringwald             }
18612531c97eSMatthias Ringwald             return;
18622531c97eSMatthias Ringwald         }
18632531c97eSMatthias Ringwald         default:
18642531c97eSMatthias Ringwald             break;
18652531c97eSMatthias Ringwald     }
18662531c97eSMatthias Ringwald 
18672531c97eSMatthias Ringwald     // otherwise, we have to have an active connection
18682531c97eSMatthias Ringwald     connection = gatt_client_helper->active_connection;
18692531c97eSMatthias Ringwald     uint16_t offset;
18702531c97eSMatthias Ringwald     uint16_t length;
18712531c97eSMatthias Ringwald 
18722531c97eSMatthias Ringwald     if (!connection) return;
18732531c97eSMatthias Ringwald 
18740e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
18752531c97eSMatthias Ringwald 
18762531c97eSMatthias Ringwald         case GATT_EVENT_SERVICE_QUERY_RESULT:
18772531c97eSMatthias Ringwald         case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:
18782531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
18792531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
18802531c97eSMatthias Ringwald         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
18812531c97eSMatthias Ringwald         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
18822531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
18832531c97eSMatthias Ringwald             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size);
18842531c97eSMatthias Ringwald             break;
18852531c97eSMatthias Ringwald 
18862531c97eSMatthias Ringwald         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
18872531c97eSMatthias Ringwald         case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
18882531c97eSMatthias Ringwald             offset = little_endian_read_16(packet, 6);
18892531c97eSMatthias Ringwald             length = little_endian_read_16(packet, 8);
18900e2df43fSMatthias Ringwald             gatt_client_helper->characteristic_buffer[0] = hci_event_packet_get_type(packet);  // store type (characteristic/descriptor)
18912531c97eSMatthias Ringwald             gatt_client_helper->characteristic_handle    = little_endian_read_16(packet, 4);   // store attribute handle
18922531c97eSMatthias Ringwald             gatt_client_helper->characteristic_length = offset + length;            // update length
18932531c97eSMatthias Ringwald             memcpy(&gatt_client_helper->characteristic_buffer[10 + offset], &packet[10], length);
18942531c97eSMatthias Ringwald             break;
18952531c97eSMatthias Ringwald 
18962531c97eSMatthias Ringwald         case GATT_EVENT_QUERY_COMPLETE:{
18972531c97eSMatthias Ringwald             gatt_client_helper->active_connection = NULL;
18982531c97eSMatthias Ringwald             if (gatt_client_helper->characteristic_length){
18992531c97eSMatthias Ringwald                 // send re-combined long characteristic value or long characteristic descriptor value
19002531c97eSMatthias Ringwald                 uint8_t * event = gatt_client_helper->characteristic_buffer;
19012531c97eSMatthias Ringwald                 uint16_t event_size = 10 + gatt_client_helper->characteristic_length;
19022531c97eSMatthias Ringwald                 // event[0] == already set by previsous case
19032531c97eSMatthias Ringwald                 event[1] = 8 + gatt_client_helper->characteristic_length;
19042531c97eSMatthias Ringwald                 little_endian_store_16(event, 2, little_endian_read_16(packet, 2));
19052531c97eSMatthias Ringwald                 little_endian_store_16(event, 4, gatt_client_helper->characteristic_handle);
19062531c97eSMatthias Ringwald                 little_endian_store_16(event, 6, 0);   // offset
19072531c97eSMatthias Ringwald                 little_endian_store_16(event, 8, gatt_client_helper->characteristic_length);
19082531c97eSMatthias Ringwald                 hci_dump_packet(HCI_EVENT_PACKET, 0, event, event_size);
19092531c97eSMatthias Ringwald                 socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, event_size);
19102531c97eSMatthias Ringwald                 gatt_client_helper->characteristic_length = 0;
19112531c97eSMatthias Ringwald             }
19122531c97eSMatthias Ringwald             hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
19132531c97eSMatthias Ringwald             socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, packet, size);
19142531c97eSMatthias Ringwald             break;
19152531c97eSMatthias Ringwald         }
19162531c97eSMatthias Ringwald         default:
19172531c97eSMatthias Ringwald             break;
19182531c97eSMatthias Ringwald     }
19192531c97eSMatthias Ringwald }
19202531c97eSMatthias Ringwald #endif
19212531c97eSMatthias Ringwald 
19222531c97eSMatthias Ringwald static char hostname[30];
19232531c97eSMatthias Ringwald 
19242531c97eSMatthias Ringwald int main (int argc,  char * const * argv){
19252531c97eSMatthias Ringwald 
19262531c97eSMatthias Ringwald     static int tcp_flag = 0;
19272531c97eSMatthias Ringwald 
19282531c97eSMatthias Ringwald     while (1) {
19292531c97eSMatthias Ringwald         static struct option long_options[] = {
19302531c97eSMatthias Ringwald             { "tcp", no_argument, &tcp_flag, 1 },
19312531c97eSMatthias Ringwald             { "help", no_argument, 0, 0 },
19322531c97eSMatthias Ringwald             { 0,0,0,0 } // This is a filler for -1
19332531c97eSMatthias Ringwald         };
19342531c97eSMatthias Ringwald 
19352531c97eSMatthias Ringwald         int c;
19362531c97eSMatthias Ringwald         int option_index = -1;
19372531c97eSMatthias Ringwald 
19382531c97eSMatthias Ringwald         c = getopt_long(argc, argv, "h", long_options, &option_index);
19392531c97eSMatthias Ringwald 
19402531c97eSMatthias Ringwald         if (c == -1) break; // no more option
19412531c97eSMatthias Ringwald 
19422531c97eSMatthias Ringwald         // treat long parameter first
19432531c97eSMatthias Ringwald         if (option_index == -1) {
19442531c97eSMatthias Ringwald             switch (c) {
19452531c97eSMatthias Ringwald                 case '?':
19462531c97eSMatthias Ringwald                 case 'h':
19472531c97eSMatthias Ringwald                     usage(argv[0]);
19482531c97eSMatthias Ringwald                     return 0;
19492531c97eSMatthias Ringwald                     break;
19502531c97eSMatthias Ringwald             }
19512531c97eSMatthias Ringwald         } else {
19522531c97eSMatthias Ringwald             switch (option_index) {
19532531c97eSMatthias Ringwald                 case 1:
19542531c97eSMatthias Ringwald                     usage(argv[0]);
19552531c97eSMatthias Ringwald                     return 0;
19562531c97eSMatthias Ringwald                     break;
19572531c97eSMatthias Ringwald             }
19582531c97eSMatthias Ringwald         }
19592531c97eSMatthias Ringwald     }
19602531c97eSMatthias Ringwald 
196142963eceSMatthias Ringwald #ifndef HAVE_UNIX_SOCKETS
196242963eceSMatthias Ringwald     // TCP is default if there are no unix sockets
196342963eceSMatthias Ringwald     tcp_flag = 1;
196442963eceSMatthias Ringwald #endif
196542963eceSMatthias Ringwald 
19662531c97eSMatthias Ringwald     if (tcp_flag){
19672531c97eSMatthias Ringwald         printf("BTstack Daemon started on port %u\n", BTSTACK_PORT);
19682531c97eSMatthias Ringwald     } else {
19692531c97eSMatthias Ringwald         printf("BTstack Daemon started on socket %s\n", BTSTACK_UNIX);
19702531c97eSMatthias Ringwald     }
19712531c97eSMatthias Ringwald 
19722531c97eSMatthias Ringwald     // make stdout unbuffered
19732531c97eSMatthias Ringwald     setbuf(stdout, NULL);
19742531c97eSMatthias Ringwald 
19752531c97eSMatthias Ringwald     // handle CTRL-c
19762531c97eSMatthias Ringwald     signal(SIGINT, daemon_sigint_handler);
19772531c97eSMatthias Ringwald     // handle SIGTERM - suggested for launchd
19782531c97eSMatthias Ringwald     signal(SIGTERM, daemon_sigint_handler);
19792531c97eSMatthias Ringwald 
19809490bce5SMatthias Ringwald     socket_connection_init();
19812531c97eSMatthias Ringwald 
19822531c97eSMatthias Ringwald     btstack_control_t * control = NULL;
19832531c97eSMatthias Ringwald     void * config;
1984a046efa6SMatthias Ringwald     const btstack_uart_block_t * uart_block_implementation = NULL;
1985a046efa6SMatthias Ringwald     (void) uart_block_implementation;
19862531c97eSMatthias Ringwald 
19872531c97eSMatthias Ringwald #ifdef HAVE_TRANSPORT_H4
19882531c97eSMatthias Ringwald     hci_transport_config_uart.type = HCI_TRANSPORT_CONFIG_UART;
19892531c97eSMatthias Ringwald     hci_transport_config_uart.baudrate_init = UART_SPEED;
19902531c97eSMatthias Ringwald     hci_transport_config_uart.baudrate_main = 0;
19912531c97eSMatthias Ringwald     hci_transport_config_uart.flowcontrol = 1;
19922531c97eSMatthias Ringwald     hci_transport_config_uart.device_name   = UART_DEVICE;
1993a046efa6SMatthias Ringwald 
1994a046efa6SMatthias Ringwald #ifndef HAVE_PLATFORM_IPHONE_OS
1995a046efa6SMatthias Ringwald     uart_block_implementation = btstack_uart_block_posix_instance();
1996a046efa6SMatthias Ringwald #endif
19972531c97eSMatthias Ringwald 
19982531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
19992531c97eSMatthias Ringwald     // use default (max) UART baudrate over netgraph interface
20002531c97eSMatthias Ringwald     hci_transport_config_uart.baudrate_init = 0;
20012531c97eSMatthias Ringwald #endif
20022531c97eSMatthias Ringwald 
20032531c97eSMatthias Ringwald     config = &hci_transport_config_uart;
2004a046efa6SMatthias Ringwald     transport = hci_transport_h4_instance(uart_block_implementation);
20052531c97eSMatthias Ringwald #endif
20062531c97eSMatthias Ringwald 
20072531c97eSMatthias Ringwald #ifdef HAVE_TRANSPORT_USB
20082531c97eSMatthias Ringwald     transport = hci_transport_usb_instance();
20092531c97eSMatthias Ringwald #endif
20102531c97eSMatthias Ringwald 
20112531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
20122531c97eSMatthias Ringwald     control = &btstack_control_iphone;
20132531c97eSMatthias Ringwald     if (btstack_control_iphone_power_management_supported()){
20142531c97eSMatthias Ringwald         hci_transport_h4_iphone_set_enforce_wake_device("/dev/btwake");
20152531c97eSMatthias Ringwald     }
20162531c97eSMatthias Ringwald     bluetooth_status_handler = platform_iphone_status_handler;
20172531c97eSMatthias Ringwald     platform_iphone_register_window_manager_restart(update_ui_status);
20182531c97eSMatthias Ringwald     platform_iphone_register_preferences_changed(preferences_changed_callback);
20192531c97eSMatthias Ringwald #endif
20202531c97eSMatthias Ringwald 
20212531c97eSMatthias Ringwald #ifdef BTSTACK_LINK_KEY_DB_INSTANCE
20222531c97eSMatthias Ringwald     btstack_link_key_db = BTSTACK_LINK_KEY_DB_INSTANCE();
20232531c97eSMatthias Ringwald #endif
20242531c97eSMatthias Ringwald 
20252531c97eSMatthias Ringwald #ifdef BTSTACK_DEVICE_NAME_DB_INSTANCE
20262531c97eSMatthias Ringwald     btstack_device_name_db = BTSTACK_DEVICE_NAME_DB_INSTANCE();
20272531c97eSMatthias Ringwald #endif
20282531c97eSMatthias Ringwald 
20292531c97eSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
20302531c97eSMatthias Ringwald 
20312531c97eSMatthias Ringwald     // init power management notifications
20322531c97eSMatthias Ringwald     if (control && control->register_for_power_notifications){
20332531c97eSMatthias Ringwald         control->register_for_power_notifications(power_notification_callback);
20342531c97eSMatthias Ringwald     }
20352531c97eSMatthias Ringwald 
20362531c97eSMatthias Ringwald     // logging
20372531c97eSMatthias Ringwald     loggingEnabled = 0;
20382531c97eSMatthias Ringwald     int newLoggingEnabled = 1;
20392531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
20402531c97eSMatthias Ringwald     // iPhone has toggle in Preferences.app
20412531c97eSMatthias Ringwald     newLoggingEnabled = platform_iphone_logging_enabled();
20422531c97eSMatthias Ringwald #endif
20432531c97eSMatthias Ringwald     daemon_set_logging_enabled(newLoggingEnabled);
20442531c97eSMatthias Ringwald 
20452531c97eSMatthias Ringwald     // dump version
20462531c97eSMatthias Ringwald     log_info("BTdaemon started\n");
20472531c97eSMatthias Ringwald     log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE);
20482531c97eSMatthias Ringwald 
20492531c97eSMatthias Ringwald     // init HCI
20502531c97eSMatthias Ringwald     hci_init(transport, config);
20512531c97eSMatthias Ringwald     if (btstack_link_key_db){
20522531c97eSMatthias Ringwald         hci_set_link_key_db(btstack_link_key_db);
20532531c97eSMatthias Ringwald     }
20542531c97eSMatthias Ringwald     if (control){
20552531c97eSMatthias Ringwald         hci_set_control(control);
20562531c97eSMatthias Ringwald     }
20572531c97eSMatthias Ringwald 
20582531c97eSMatthias Ringwald     // hostname for POSIX systems
20592531c97eSMatthias Ringwald     gethostname(hostname, 30);
20602531c97eSMatthias Ringwald     hostname[29] = '\0';
20612531c97eSMatthias Ringwald     gap_set_local_name(hostname);
20622531c97eSMatthias Ringwald 
20632531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
20642531c97eSMatthias Ringwald     // iPhone doesn't use SSP yet as there's no UI for it yet and auto accept is not an option
20652531c97eSMatthias Ringwald     gap_ssp_set_enable(0);
20662531c97eSMatthias Ringwald #endif
20672531c97eSMatthias Ringwald 
20682531c97eSMatthias Ringwald     // register for HCI events
20692531c97eSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_packet_handler;
20702531c97eSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
20712531c97eSMatthias Ringwald 
20722531c97eSMatthias Ringwald     // init L2CAP
20732531c97eSMatthias Ringwald     l2cap_init();
20742531c97eSMatthias Ringwald     l2cap_register_packet_handler(&l2cap_packet_handler);
20752531c97eSMatthias Ringwald     timeout.process = daemon_no_connections_timeout;
20762531c97eSMatthias Ringwald 
20772531c97eSMatthias Ringwald #ifdef ENABLE_RFCOMM
20782531c97eSMatthias Ringwald     log_info("config.h: ENABLE_RFCOMM\n");
20792531c97eSMatthias Ringwald     rfcomm_init();
20802531c97eSMatthias Ringwald #endif
20812531c97eSMatthias Ringwald 
20822531c97eSMatthias Ringwald #ifdef ENABLE_SDP
20832531c97eSMatthias Ringwald     sdp_init();
20842531c97eSMatthias Ringwald #endif
20852531c97eSMatthias Ringwald 
20862531c97eSMatthias Ringwald #ifdef ENABLE_BLE
20872531c97eSMatthias Ringwald     // GATT Client
20882531c97eSMatthias Ringwald     gatt_client_init();
20892531c97eSMatthias Ringwald 
20902531c97eSMatthias Ringwald     // sm_init();
20912531c97eSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
20922531c97eSMatthias Ringwald     // sm_set_authentication_requirements( SM_AUTHREQ_BONDING | SM_AUTHREQ_MITM_PROTECTION);
20932531c97eSMatthias Ringwald 
20942531c97eSMatthias Ringwald     // GATT Server - empty attribute database
20952531c97eSMatthias Ringwald     le_device_db_init();
20962531c97eSMatthias Ringwald     att_server_init(NULL, NULL, NULL);
20972531c97eSMatthias Ringwald 
20982531c97eSMatthias Ringwald #endif
20992531c97eSMatthias Ringwald 
21002531c97eSMatthias Ringwald #ifdef USE_LAUNCHD
21012531c97eSMatthias Ringwald     socket_connection_create_launchd();
21022531c97eSMatthias Ringwald #else
21032531c97eSMatthias Ringwald     // create server
21042531c97eSMatthias Ringwald     if (tcp_flag) {
21052531c97eSMatthias Ringwald         socket_connection_create_tcp(BTSTACK_PORT);
21062531c97eSMatthias Ringwald     } else {
210742963eceSMatthias Ringwald #ifdef HAVE_UNIX_SOCKETS
21082531c97eSMatthias Ringwald         socket_connection_create_unix(BTSTACK_UNIX);
210942963eceSMatthias Ringwald #endif
21102531c97eSMatthias Ringwald     }
21112531c97eSMatthias Ringwald #endif
21122531c97eSMatthias Ringwald     socket_connection_register_packet_callback(&daemon_client_handler);
21132531c97eSMatthias Ringwald 
21142531c97eSMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
21152531c97eSMatthias Ringwald     // notify daemons
21162531c97eSMatthias Ringwald     notify_post("ch.ringwald.btstack.started");
21172531c97eSMatthias Ringwald 
21182531c97eSMatthias Ringwald     // spawn thread to have BTstack run loop on new thread, while main thread is used to keep CFRunLoop
21192531c97eSMatthias Ringwald     pthread_t run_loop;
21202531c97eSMatthias Ringwald     pthread_create(&run_loop, NULL, &btstack_run_loop_thread, NULL);
21212531c97eSMatthias Ringwald 
21222531c97eSMatthias Ringwald     // needed to receive notifications
21232531c97eSMatthias Ringwald     CFRunLoopRun();
21242531c97eSMatthias Ringwald #endif
21252531c97eSMatthias Ringwald         // go!
21262531c97eSMatthias Ringwald     btstack_run_loop_execute();
21272531c97eSMatthias Ringwald     return 0;
21282531c97eSMatthias Ringwald }
2129