daemon.c (bb2a499a758e2a4b4fd9e758a91760071f77c055) daemon.c (6fad2c376995e272e1924e97046a38cb37cd48b1)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 97 unchanged lines hidden (view full) ---

106#include "ble/gatt_client.h"
107#include "ble/att_server.h"
108#include "ble/att_db.h"
109#include "ble/le_device_db.h"
110#include "ble/le_device_db_tlv.h"
111#include "ble/sm.h"
112#endif
113
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 97 unchanged lines hidden (view full) ---

106#include "ble/gatt_client.h"
107#include "ble/att_server.h"
108#include "ble/att_db.h"
109#include "ble/le_device_db.h"
110#include "ble/le_device_db_tlv.h"
111#include "ble/sm.h"
112#endif
113
114#ifdef HAVE_PLATFORM_IPHONE_OS
115#include <CoreFoundation/CoreFoundation.h>
116#include <notify.h>
117#include "../port/ios/src/btstack_control_iphone.h"
118#include "../port/ios/src/platform_iphone.h"
119// support for "enforece wake device" in h4 - used by iOS power management
120extern void hci_transport_h4_iphone_set_enforce_wake_device(char *path);
121#endif
122
123// copy of prototypes
124const btstack_device_name_db_t * btstack_device_name_db_corefoundation_instance(void);
125const btstack_device_name_db_t * btstack_device_name_db_fs_instance(void);
126const btstack_link_key_db_t * btstack_link_key_db_corefoundation_instance(void);
127const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void);
128
129// use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
130#ifndef BTSTACK_LOG_TYPE

--- 807 unchanged lines hidden (view full) ---

938#endif
939 hci_power_control(HCI_POWER_ON);
940 }
941 break;
942 case BTSTACK_GET_VERSION:
943 log_info("BTSTACK_GET_VERSION");
944 hci_emit_btstack_version();
945 break;
114// copy of prototypes
115const btstack_device_name_db_t * btstack_device_name_db_corefoundation_instance(void);
116const btstack_device_name_db_t * btstack_device_name_db_fs_instance(void);
117const btstack_link_key_db_t * btstack_link_key_db_corefoundation_instance(void);
118const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void);
119
120// use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
121#ifndef BTSTACK_LOG_TYPE

--- 807 unchanged lines hidden (view full) ---

929#endif
930 hci_power_control(HCI_POWER_ON);
931 }
932 break;
933 case BTSTACK_GET_VERSION:
934 log_info("BTSTACK_GET_VERSION");
935 hci_emit_btstack_version();
936 break;
946#ifdef HAVE_PLATFORM_IPHONE_OS
947 case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
937 case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
948 log_info("BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED %u", packet[3]);
949 btstack_control_iphone_bt_set_enabled(packet[3]);
950 hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled());
951 break;
952
953 case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
938 case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
954 log_info("BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED");
955 hci_emit_system_bluetooth_enabled(btstack_control_iphone_bt_enabled());
956 break;
957#else
958 case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
959 case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
960 hci_emit_system_bluetooth_enabled(0);
961 break;
939 hci_emit_system_bluetooth_enabled(0);
940 break;
962#endif
963 case BTSTACK_SET_DISCOVERABLE:
964 log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]);
965 // track client discoverable requests
966 client = client_for_connection(connection);
967 if (!client) break;
968 client->discoverable = packet[3];
969 // merge state
970 gap_discoverable_control(clients_require_discoverable());

--- 791 unchanged lines hidden (view full) ---

1762 break;
1763 default:
1764 break;
1765 }
1766}
1767
1768static void daemon_sigint_handler(int param){
1769
941 case BTSTACK_SET_DISCOVERABLE:
942 log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]);
943 // track client discoverable requests
944 client = client_for_connection(connection);
945 if (!client) break;
946 client->discoverable = packet[3];
947 // merge state
948 gap_discoverable_control(clients_require_discoverable());

--- 791 unchanged lines hidden (view full) ---

1740 break;
1741 default:
1742 break;
1743 }
1744}
1745
1746static void daemon_sigint_handler(int param){
1747
1770#ifdef HAVE_PLATFORM_IPHONE_OS
1771 // notify daemons
1772 notify_post("ch.ringwald.btstack.stopped");
1773#endif
1774
1775 log_info(" <= SIGINT received, shutting down..\n");
1748 log_info(" <= SIGINT received, shutting down..\n");
1776
1777 int send_power_off = 1;
1778#ifdef HAVE_INTEL_USB
1779 // power off and close only if hci was initialized before
1780 send_power_off = intel_firmware_loaded;
1781#endif
1782
1783 if (send_power_off){

--- 80 unchanged lines hidden (view full) ---

1864static void usage(const char * name) {
1865 printf("%s, BTstack background daemon\n", name);
1866 printf("usage: %s [--help] [--tcp]\n", name);
1867 printf(" --help display this usage\n");
1868 printf(" --tcp use TCP server on port %u\n", BTSTACK_PORT);
1869 printf("Without the --tcp option, BTstack Server is listening on unix domain socket %s\n\n", BTSTACK_UNIX);
1870}
1871
1749
1750 int send_power_off = 1;
1751#ifdef HAVE_INTEL_USB
1752 // power off and close only if hci was initialized before
1753 send_power_off = intel_firmware_loaded;
1754#endif
1755
1756 if (send_power_off){

--- 80 unchanged lines hidden (view full) ---

1837static void usage(const char * name) {
1838 printf("%s, BTstack background daemon\n", name);
1839 printf("usage: %s [--help] [--tcp]\n", name);
1840 printf(" --help display this usage\n");
1841 printf(" --tcp use TCP server on port %u\n", BTSTACK_PORT);
1842 printf("Without the --tcp option, BTstack Server is listening on unix domain socket %s\n\n", BTSTACK_UNIX);
1843}
1844
1872#ifdef HAVE_PLATFORM_IPHONE_OS
1873static void * btstack_run_loop_thread(void *context){
1874 btstack_run_loop_execute();
1875 return NULL;
1876}
1877#endif
1878
1879#ifdef ENABLE_BLE
1880
1881static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
1882
1883 // only handle GATT Events
1884 switch(hci_event_packet_get_type(packet)){
1885 case GATT_EVENT_SERVICE_QUERY_RESULT:
1886 case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:

--- 104 unchanged lines hidden (view full) ---

1991 hci_set_control(control);
1992 }
1993
1994 // hostname for POSIX systems
1995 gethostname(hostname, 30);
1996 hostname[29] = '\0';
1997 gap_set_local_name(hostname);
1998
1845#ifdef ENABLE_BLE
1846
1847static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
1848
1849 // only handle GATT Events
1850 switch(hci_event_packet_get_type(packet)){
1851 case GATT_EVENT_SERVICE_QUERY_RESULT:
1852 case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:

--- 104 unchanged lines hidden (view full) ---

1957 hci_set_control(control);
1958 }
1959
1960 // hostname for POSIX systems
1961 gethostname(hostname, 30);
1962 hostname[29] = '\0';
1963 gap_set_local_name(hostname);
1964
1999#ifdef HAVE_PLATFORM_IPHONE_OS
2000 // iPhone doesn't use SSP yet as there's no UI for it yet and auto accept is not an option
2001 gap_ssp_set_enable(0);
2002#endif
2003
2004 // enabled EIR
2005 hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
2006
2007 // register for HCI events
2008 hci_event_callback_registration.callback = &stack_packet_handler;
2009 hci_add_event_handler(&hci_event_callback_registration);
2010
2011 // init L2CAP

--- 60 unchanged lines hidden (view full) ---

2072
2073#ifdef HAVE_TRANSPORT_H4
2074 hci_transport_config_uart.type = HCI_TRANSPORT_CONFIG_UART;
2075 hci_transport_config_uart.baudrate_init = UART_SPEED;
2076 hci_transport_config_uart.baudrate_main = 0;
2077 hci_transport_config_uart.flowcontrol = 1;
2078 hci_transport_config_uart.device_name = UART_DEVICE;
2079
1965 // enabled EIR
1966 hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
1967
1968 // register for HCI events
1969 hci_event_callback_registration.callback = &stack_packet_handler;
1970 hci_add_event_handler(&hci_event_callback_registration);
1971
1972 // init L2CAP

--- 60 unchanged lines hidden (view full) ---

2033
2034#ifdef HAVE_TRANSPORT_H4
2035 hci_transport_config_uart.type = HCI_TRANSPORT_CONFIG_UART;
2036 hci_transport_config_uart.baudrate_init = UART_SPEED;
2037 hci_transport_config_uart.baudrate_main = 0;
2038 hci_transport_config_uart.flowcontrol = 1;
2039 hci_transport_config_uart.device_name = UART_DEVICE;
2040
2080#ifndef HAVE_PLATFORM_IPHONE_OS
2081#ifdef _WIN32
2082 uart_implementation = (const btstack_uart_t *) btstack_uart_block_windows_instance();
2083#else
2084 uart_implementation = btstack_uart_posix_instance();
2085#endif
2041#ifdef _WIN32
2042 uart_implementation = (const btstack_uart_t *) btstack_uart_block_windows_instance();
2043#else
2044 uart_implementation = btstack_uart_posix_instance();
2045#endif
2086#endif
2087
2088#ifdef HAVE_PLATFORM_IPHONE_OS
2089 // use default (max) UART baudrate over netgraph interface
2090 hci_transport_config_uart.baudrate_init = 0;
2091#endif
2092
2093 config = &hci_transport_config_uart;
2094 transport = hci_transport_h4_instance_for_uart(uart_implementation);
2095#endif
2096
2097#ifdef HAVE_TRANSPORT_USB
2098 transport = hci_transport_usb_instance();
2099#endif
2100
2046
2047 config = &hci_transport_config_uart;
2048 transport = hci_transport_h4_instance_for_uart(uart_implementation);
2049#endif
2050
2051#ifdef HAVE_TRANSPORT_USB
2052 transport = hci_transport_usb_instance();
2053#endif
2054
2101#ifdef HAVE_PLATFORM_IPHONE_OS
2102 control = &btstack_control_iphone;
2103 if (btstack_control_iphone_power_management_supported()){
2104 hci_transport_h4_iphone_set_enforce_wake_device("/dev/btwake");
2105 }
2106 bluetooth_status_handler = platform_iphone_status_handler;
2107 platform_iphone_register_window_manager_restart(update_ui_status);
2108 platform_iphone_register_preferences_changed(preferences_changed_callback);
2109#endif
2110
2111#ifdef BTSTACK_DEVICE_NAME_DB_INSTANCE
2112 btstack_device_name_db = BTSTACK_DEVICE_NAME_DB_INSTANCE();
2113#endif
2114
2115#ifdef _WIN32
2116 btstack_run_loop_init(btstack_run_loop_windows_get_instance());
2117#else
2118 btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2119#endif
2120
2121 // init power management notifications
2122 if (control && control->register_for_power_notifications){
2123 control->register_for_power_notifications(power_notification_callback);
2124 }
2125
2126 // logging
2127 loggingEnabled = 0;
2128 int newLoggingEnabled = 1;
2055#ifdef BTSTACK_DEVICE_NAME_DB_INSTANCE
2056 btstack_device_name_db = BTSTACK_DEVICE_NAME_DB_INSTANCE();
2057#endif
2058
2059#ifdef _WIN32
2060 btstack_run_loop_init(btstack_run_loop_windows_get_instance());
2061#else
2062 btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2063#endif
2064
2065 // init power management notifications
2066 if (control && control->register_for_power_notifications){
2067 control->register_for_power_notifications(power_notification_callback);
2068 }
2069
2070 // logging
2071 loggingEnabled = 0;
2072 int newLoggingEnabled = 1;
2129#ifdef HAVE_PLATFORM_IPHONE_OS
2130 // iPhone has toggle in Preferences.app
2131 newLoggingEnabled = platform_iphone_logging_enabled();
2132#endif
2133 daemon_set_logging_enabled(newLoggingEnabled);
2134
2135 // dump version
2136 log_info("BTStack Server started\n");
2137 log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE);
2138
2139#ifndef HAVE_INTEL_USB
2140 btstack_server_configure_stack();

--- 7 unchanged lines hidden (view full) ---

2148 socket_connection_create_tcp(BTSTACK_PORT);
2149 } else {
2150#ifdef HAVE_UNIX_SOCKETS
2151 socket_connection_create_unix(BTSTACK_UNIX);
2152#endif
2153 }
2154#endif
2155 socket_connection_register_packet_callback(&daemon_client_handler);
2073 daemon_set_logging_enabled(newLoggingEnabled);
2074
2075 // dump version
2076 log_info("BTStack Server started\n");
2077 log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE);
2078
2079#ifndef HAVE_INTEL_USB
2080 btstack_server_configure_stack();

--- 7 unchanged lines hidden (view full) ---

2088 socket_connection_create_tcp(BTSTACK_PORT);
2089 } else {
2090#ifdef HAVE_UNIX_SOCKETS
2091 socket_connection_create_unix(BTSTACK_UNIX);
2092#endif
2093 }
2094#endif
2095 socket_connection_register_packet_callback(&daemon_client_handler);
2156
2157#ifdef HAVE_PLATFORM_IPHONE_OS
2158 // notify daemons
2159 notify_post("ch.ringwald.btstack.started");
2160
2096
2161 // spawn thread to have BTstack run loop on new thread, while main thread is used to keep CFRunLoop
2162 pthread_t run_loop;
2163 pthread_create(&run_loop, NULL, &btstack_run_loop_thread, NULL);
2164
2165 // needed to receive notifications
2166 CFRunLoopRun();
2167#endif
2168 // go!
2097 // go!
2169 btstack_run_loop_execute();
2170 return 0;
2171}
2172
2173int btstack_server_run_tcp(void){
2174 return btstack_server_run(1);
2175}
2176

--- 52 unchanged lines hidden ---
2098 btstack_run_loop_execute();
2099 return 0;
2100}
2101
2102int btstack_server_run_tcp(void){
2103 return btstack_server_run(1);
2104}
2105

--- 52 unchanged lines hidden ---