2009-10-29 20:25:42 +00:00
|
|
|
/*
|
2012-05-07 21:54:09 +00:00
|
|
|
* Copyright (C) 2009-2012 by Matthias Ringwald
|
2009-10-29 20:25:42 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2012-05-07 21:54:09 +00:00
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
2009-10-29 20:25:42 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2012-05-07 21:54:09 +00:00
|
|
|
* Please inquire about commercial licensing options at btstack@ringwald.ch
|
|
|
|
*
|
2009-10-29 20:25:42 +00:00
|
|
|
*/
|
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
/*
|
|
|
|
* daemon.c
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 7/1/09.
|
|
|
|
*
|
|
|
|
* BTstack background daemon
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-01-19 16:45:57 +00:00
|
|
|
#include "btstack-config.h"
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2011-06-03 21:34:00 +00:00
|
|
|
#include <pthread.h>
|
2009-08-15 19:52:46 +00:00
|
|
|
#include <signal.h>
|
2009-07-01 21:55:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
2009-08-15 19:52:46 +00:00
|
|
|
#include <unistd.h>
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2010-02-28 22:02:28 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2010-02-28 21:37:59 +00:00
|
|
|
#include <btstack/btstack.h>
|
2009-09-28 21:19:05 +00:00
|
|
|
#include <btstack/linked_list.h>
|
|
|
|
#include <btstack/run_loop.h>
|
2013-10-18 10:19:57 +00:00
|
|
|
#include <btstack/hci_cmds.h>
|
2010-06-13 08:26:47 +00:00
|
|
|
|
2011-05-05 18:45:28 +00:00
|
|
|
#include "debug.h"
|
2010-06-13 08:26:47 +00:00
|
|
|
#include "hci.h"
|
|
|
|
#include "hci_dump.h"
|
2010-09-24 21:01:07 +00:00
|
|
|
#include "hci_transport.h"
|
2010-06-13 08:26:47 +00:00
|
|
|
#include "l2cap.h"
|
2011-05-01 11:36:12 +00:00
|
|
|
#include "rfcomm.h"
|
2010-06-13 08:26:47 +00:00
|
|
|
#include "sdp.h"
|
2013-10-17 14:28:55 +00:00
|
|
|
#include "sdp_parser.h"
|
2013-04-26 20:19:34 +00:00
|
|
|
#include "sdp_client.h"
|
2013-10-17 14:28:55 +00:00
|
|
|
#include "sdp_query_util.h"
|
2013-04-26 20:19:34 +00:00
|
|
|
#include "sdp_query_rfcomm.h"
|
2009-07-22 20:27:00 +00:00
|
|
|
#include "socket_connection.h"
|
2014-05-06 21:22:02 +00:00
|
|
|
#include "gatt_client.h"
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2009-07-11 10:37:48 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
2011-06-03 21:34:00 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2009-07-11 10:37:48 +00:00
|
|
|
#include "bt_control_iphone.h"
|
2011-04-30 20:11:20 +00:00
|
|
|
#include <notify.h>
|
2009-07-11 10:37:48 +00:00
|
|
|
#endif
|
|
|
|
|
2009-08-16 22:00:58 +00:00
|
|
|
#ifdef USE_SPRINGBOARD
|
|
|
|
#include "platform_iphone.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-11 10:37:48 +00:00
|
|
|
#ifdef HAVE_TRANSPORT_USB
|
2009-07-09 18:49:43 +00:00
|
|
|
#include <libusb-1.0/libusb.h>
|
2009-07-11 10:37:48 +00:00
|
|
|
#endif
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2011-01-08 22:01:06 +00:00
|
|
|
#define DAEMON_NO_ACTIVE_CLIENT_TIMEOUT 10000
|
2009-08-09 20:18:34 +00:00
|
|
|
|
2011-01-07 21:34:50 +00:00
|
|
|
|
2011-01-09 14:48:03 +00:00
|
|
|
typedef struct {
|
|
|
|
// linked list - assert: first field
|
|
|
|
linked_item_t item;
|
|
|
|
|
|
|
|
// connection
|
|
|
|
connection_t * connection;
|
|
|
|
|
|
|
|
// power mode
|
|
|
|
HCI_POWER_MODE power_mode;
|
|
|
|
|
|
|
|
// discoverable
|
|
|
|
uint8_t discoverable;
|
|
|
|
|
|
|
|
} client_state_t;
|
2011-01-07 21:34:50 +00:00
|
|
|
|
2011-06-23 18:28:19 +00:00
|
|
|
// MARK: prototypes
|
2013-04-28 13:45:53 +00:00
|
|
|
static void handle_sdp_rfcomm_service_result(sdp_query_event_t * event, void * context);
|
2013-10-18 14:13:50 +00:00
|
|
|
static void handle_sdp_client_query_result(sdp_query_event_t * event);
|
2011-01-10 21:11:58 +00:00
|
|
|
static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state);
|
|
|
|
static client_state_t * client_for_connection(connection_t *connection);
|
2011-06-06 14:12:38 +00:00
|
|
|
static int clients_require_power_on(void);
|
|
|
|
static int clients_require_discoverable(void);
|
|
|
|
static void clients_clear_power_request(void);
|
|
|
|
static void start_power_off_timer(void);
|
|
|
|
static void stop_power_off_timer(void);
|
2011-01-10 21:11:58 +00:00
|
|
|
|
2011-06-23 18:28:19 +00:00
|
|
|
// MARK: globals
|
2011-01-07 21:34:50 +00:00
|
|
|
static hci_transport_t * transport;
|
|
|
|
static hci_uart_config_t config;
|
|
|
|
static timer_source_t timeout;
|
|
|
|
static uint8_t timeout_active = 0;
|
2011-01-08 22:01:06 +00:00
|
|
|
static int power_management_sleep = 0;
|
2011-01-09 14:48:03 +00:00
|
|
|
static linked_list_t clients = NULL; // list of connected clients
|
2011-01-10 21:11:58 +00:00
|
|
|
static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth_status_handler;
|
2011-01-09 14:48:03 +00:00
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
static int global_enable = 0;
|
2010-12-24 00:22:04 +00:00
|
|
|
|
2011-11-09 15:43:28 +00:00
|
|
|
static remote_device_db_t const * remote_device_db = NULL;
|
2011-05-27 20:47:31 +00:00
|
|
|
static int rfcomm_channel_generator = 1;
|
2011-05-03 21:14:41 +00:00
|
|
|
|
2013-10-17 14:28:55 +00:00
|
|
|
static uint8_t attribute_value[1000];
|
|
|
|
static const int attribute_value_buffer_size = sizeof(attribute_value);
|
2013-10-18 14:48:08 +00:00
|
|
|
static uint8_t serviceSearchPattern[200];
|
|
|
|
static uint8_t attributeIDList[50];
|
|
|
|
|
2013-10-17 14:28:55 +00:00
|
|
|
|
2012-02-07 22:22:00 +00:00
|
|
|
static int loggingEnabled;
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Bluetooth status: %u\n", state);
|
2009-08-15 21:31:23 +00:00
|
|
|
};
|
|
|
|
|
2011-06-06 14:34:29 +00:00
|
|
|
static void daemon_no_connections_timeout(struct timer *ts){
|
2011-01-10 21:11:58 +00:00
|
|
|
if (clients_require_power_on()) return; // false alarm :)
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("No active client connection for %u seconds -> POWER OFF\n", DAEMON_NO_ACTIVE_CLIENT_TIMEOUT/1000);
|
2011-01-07 22:21:25 +00:00
|
|
|
hci_power_control(HCI_POWER_OFF);
|
2009-08-09 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 13:31:06 +00:00
|
|
|
#define ATT_MAX_LONG_ATTRIBUTE_SIZE 512
|
|
|
|
|
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
#ifdef HAVE_BLE
|
2014-05-08 15:08:47 +00:00
|
|
|
#define SERVICE_LENGTH 20
|
2014-05-09 14:28:05 +00:00
|
|
|
#define CHARACTERISTIC_LENGTH 24
|
2014-05-08 15:08:47 +00:00
|
|
|
#define CHARACTERISTIC_DESCRIPTOR_LENGTH 18
|
|
|
|
|
2014-05-11 20:34:13 +00:00
|
|
|
// ATT_MTU - 1
|
|
|
|
#define ATT_MAX_ATTRIBUTE_SIZE 22
|
|
|
|
|
|
|
|
typedef struct gatt_client_helper {
|
|
|
|
uint16_t characteristic_length;
|
|
|
|
} gatt_client_helper_t;
|
2014-05-08 13:31:06 +00:00
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
static gatt_client_t * daemon_provide_gatt_client_context_for_handle(uint16_t handle){
|
|
|
|
gatt_client_t *context;
|
|
|
|
context = get_gatt_client_context_for_handle(handle);
|
2014-05-11 20:52:34 +00:00
|
|
|
if (context) return context;
|
|
|
|
|
|
|
|
context = (gatt_client_t*)malloc(sizeof(gatt_client_t) + sizeof(gatt_client_helper_t) + ATT_MAX_LONG_ATTRIBUTE_SIZE);
|
2014-05-08 13:39:53 +00:00
|
|
|
if (!context) return NULL;
|
2014-05-11 20:52:34 +00:00
|
|
|
|
2014-05-08 13:39:53 +00:00
|
|
|
gatt_client_start(context, handle);
|
2014-05-07 20:56:15 +00:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2014-05-11 20:34:13 +00:00
|
|
|
gatt_client_helper_t * daemon_get_gatt_helper(gatt_client_t * context){
|
|
|
|
return (gatt_client_helper_t*) (((uint8_t *) context) + sizeof(gatt_client_t));
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:31:06 +00:00
|
|
|
uint8_t * daemon_get_data_buffer(gatt_client_t *context) {
|
2014-05-11 20:34:13 +00:00
|
|
|
return ((uint8_t *) context) + sizeof(gatt_client_t) + sizeof(gatt_client_helper_t);
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void send_gatt_query_complete(connection_t * connection, uint8_t event_type, uint16_t handle, uint8_t status){
|
2014-05-07 20:56:15 +00:00
|
|
|
// @format H1
|
|
|
|
uint8_t event[5];
|
2014-05-08 13:31:06 +00:00
|
|
|
event[0] = event_type;
|
2014-05-08 15:08:47 +00:00
|
|
|
event[1] = 3;
|
2014-05-07 20:56:15 +00:00
|
|
|
bt_store_16(event, 2, handle);
|
|
|
|
event[4] = status;
|
|
|
|
socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
}
|
2014-05-08 13:31:06 +00:00
|
|
|
|
|
|
|
gatt_client_t * daemon_prepare_gatt_client_context(connection_t *connection, uint8_t *packet, uint8_t event_type) {
|
|
|
|
hci_con_handle_t handle = READ_BT_16(packet, 3);
|
|
|
|
|
|
|
|
gatt_client_t *context = daemon_provide_gatt_client_context_for_handle(handle);
|
|
|
|
if (!context) {
|
|
|
|
send_gatt_query_complete(connection, event_type, handle, BTSTACK_MEMORY_ALLOC_FAILED);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// check state
|
|
|
|
if (!gatt_client_is_ready(context)){
|
|
|
|
send_gatt_query_complete(connection, event_type, handle, GATT_CLIENT_BUSY);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
context->context = connection;
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
|
|
|
|
// (de)serialize structs from/to HCI commands/events
|
|
|
|
|
|
|
|
void daemon_gatt_deserialize_service(uint8_t *packet, int offset, le_service_t *service){
|
|
|
|
service->start_group_handle = READ_BT_16(packet, offset);
|
|
|
|
service->end_group_handle = READ_BT_16(packet, offset + 2);
|
|
|
|
swap128(&packet[offset + 4], service->uuid128);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemon_gatt_serialize_service(le_service_t * service, uint8_t * event, int offset){
|
|
|
|
bt_store_16(event, offset, service->start_group_handle);
|
|
|
|
bt_store_16(event, offset+2, service->end_group_handle);
|
|
|
|
swap128(service->uuid128, &event[offset + 4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemon_gatt_deserialize_characteristic(uint8_t * packet, int offset, le_characteristic_t * characteristic){
|
|
|
|
characteristic->start_handle = READ_BT_16(packet, offset);
|
|
|
|
characteristic->value_handle = READ_BT_16(packet, offset + 2);
|
|
|
|
characteristic->end_handle = READ_BT_16(packet, offset + 4);
|
|
|
|
characteristic->properties = READ_BT_16(packet, offset + 6);
|
|
|
|
characteristic->uuid16 = READ_BT_16(packet, offset + 8);
|
|
|
|
swap128(&packet[offset+10], characteristic->uuid128);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemon_gatt_serialize_characteristic(le_characteristic_t * characteristic, uint8_t * event, int offset){
|
|
|
|
bt_store_16(event, offset, characteristic->start_handle);
|
2014-05-09 14:28:05 +00:00
|
|
|
bt_store_16(event, offset+2, characteristic->value_handle);
|
|
|
|
bt_store_16(event, offset+4, characteristic->end_handle);
|
|
|
|
bt_store_16(event, offset+6, characteristic->properties);
|
|
|
|
swap128(characteristic->uuid128, &event[offset+8]);
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
void daemon_gatt_deserialize_characteristic_descriptor(uint8_t * packet, int offset, le_characteristic_descriptor_t * descriptor){
|
|
|
|
descriptor->handle = READ_BT_16(packet, offset);
|
|
|
|
swap128(&packet[offset+2], descriptor->uuid128);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemon_gatt_serialize_characteristic_descriptor(le_characteristic_descriptor_t * characteristic_descriptor, uint8_t * event, int offset){
|
|
|
|
bt_store_16(event, offset, characteristic_descriptor->handle);
|
|
|
|
swap128(characteristic_descriptor->uuid128, &event[offset+2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup events
|
|
|
|
void daemon_setup_service_event(le_event_t *le_event, uint8_t* event) {
|
2014-05-08 13:31:06 +00:00
|
|
|
le_service_event_t * service_event = (le_service_event_t *) le_event;
|
|
|
|
event[0] = le_event->type;
|
|
|
|
event[1] = SERVICE_LENGTH;
|
|
|
|
bt_store_16(event, 2, service_event->client->handle);
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_serialize_service(&service_event->service, event, 4);
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
void daemon_gatt_setup_characteristic_event(le_event_t *le_event, uint8_t* event) {
|
2014-05-08 13:31:06 +00:00
|
|
|
le_characteristic_event_t * characteristic_event = (le_characteristic_event_t *) le_event;
|
|
|
|
event[0] = le_event->type;
|
|
|
|
event[1] = CHARACTERISTIC_LENGTH;
|
|
|
|
bt_store_16(event, 2, characteristic_event->client->handle);
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_serialize_characteristic(&characteristic_event->characteristic, event, 4);
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
void daemon_setup_characteristic_descriptor_event(le_event_t *le_event, uint8_t* event) {
|
|
|
|
le_characteristic_descriptor_event_t * descriptor_event = (le_characteristic_descriptor_event_t *) le_event;
|
|
|
|
event[0] = le_event->type;
|
|
|
|
event[1] = CHARACTERISTIC_DESCRIPTOR_LENGTH;
|
|
|
|
bt_store_16(event, 2, descriptor_event->client->handle);
|
|
|
|
daemon_gatt_serialize_characteristic_descriptor(&descriptor_event->characteristic_descriptor, event, 4);
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
void daemon_setup_characteristic_value_event(le_event_t *le_event, uint8_t* event) {
|
2014-05-08 13:31:06 +00:00
|
|
|
le_characteristic_value_event_t * cvalue_event = (le_characteristic_value_event_t *) le_event;
|
|
|
|
event[0] = le_event->type;
|
2014-05-11 20:34:13 +00:00
|
|
|
event[1] = 2 + (2 + 2 + cvalue_event->blob_length);
|
2014-05-08 13:31:06 +00:00
|
|
|
bt_store_16(event, 2, cvalue_event->client->handle);
|
2014-05-11 20:34:13 +00:00
|
|
|
bt_store_16(event, 4, cvalue_event->value_handle);
|
|
|
|
// TODO bt_store_16(event, 6, cvalue_event->blob_length);
|
|
|
|
event[6] = cvalue_event->blob_length;
|
|
|
|
memcpy(&event[7], cvalue_event->blob, cvalue_event->blob_length); // &event[8] with 2 byte length
|
2014-05-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
#endif
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
static int btstack_command_handler(connection_t *connection, uint8_t *packet, uint16_t size){
|
2011-01-09 14:48:03 +00:00
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
bd_addr_t addr;
|
2014-05-03 22:31:08 +00:00
|
|
|
bd_addr_type_t addr_type;
|
|
|
|
hci_con_handle_t handle;
|
2009-07-31 21:41:15 +00:00
|
|
|
uint16_t cid;
|
|
|
|
uint16_t psm;
|
2011-04-30 20:30:05 +00:00
|
|
|
uint16_t service_channel;
|
2010-01-25 20:25:20 +00:00
|
|
|
uint16_t mtu;
|
|
|
|
uint8_t reason;
|
2011-04-30 20:30:05 +00:00
|
|
|
uint8_t rfcomm_channel;
|
2011-09-11 21:01:09 +00:00
|
|
|
uint8_t rfcomm_credits;
|
2010-06-12 21:00:22 +00:00
|
|
|
uint32_t service_record_handle;
|
2011-01-09 14:48:03 +00:00
|
|
|
client_state_t *client;
|
2014-05-08 13:39:53 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_MALLOC) && defined(HAVE_BLE)
|
|
|
|
gatt_client_t * context;
|
|
|
|
uint8_t uuid128[16];
|
|
|
|
le_service_t service;
|
|
|
|
le_characteristic_t characteristic;
|
|
|
|
le_characteristic_descriptor_t descriptor;
|
|
|
|
uint16_t data_length;
|
|
|
|
uint8_t * data;
|
|
|
|
#endif
|
|
|
|
|
2013-10-18 14:48:08 +00:00
|
|
|
uint16_t serviceSearchPatternLen;
|
|
|
|
uint16_t attributeIDListLen;
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// BTstack internal commands - 16 Bit OpCode, 8 Bit ParamLen, Params...
|
|
|
|
switch (READ_CMD_OCF(packet)){
|
2009-09-29 19:40:55 +00:00
|
|
|
case BTSTACK_GET_STATE:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_GET_STATE");
|
2009-07-31 21:41:15 +00:00
|
|
|
hci_emit_state();
|
|
|
|
break;
|
2009-09-29 19:40:55 +00:00
|
|
|
case BTSTACK_SET_POWER_MODE:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_SET_POWER_MODE %u", packet[3]);
|
2011-01-08 22:01:06 +00:00
|
|
|
// track client power requests
|
2011-01-09 14:48:03 +00:00
|
|
|
client = client_for_connection(connection);
|
|
|
|
if (!client) break;
|
|
|
|
client->power_mode = packet[3];
|
2011-01-08 22:01:06 +00:00
|
|
|
// handle merged state
|
2011-01-10 21:11:58 +00:00
|
|
|
if (!clients_require_power_on()){
|
2011-01-09 14:48:03 +00:00
|
|
|
start_power_off_timer();
|
2011-01-08 22:01:06 +00:00
|
|
|
} else if (!power_management_sleep) {
|
2011-01-09 14:48:03 +00:00
|
|
|
stop_power_off_timer();
|
2011-01-08 22:01:06 +00:00
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
}
|
2009-07-31 21:41:15 +00:00
|
|
|
break;
|
2010-01-09 11:01:23 +00:00
|
|
|
case BTSTACK_GET_VERSION:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_GET_VERSION");
|
2010-01-09 11:01:23 +00:00
|
|
|
hci_emit_btstack_version();
|
|
|
|
break;
|
2010-01-09 18:25:30 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED %u", packet[3]);
|
2010-01-09 18:25:30 +00:00
|
|
|
iphone_system_bt_set_enabled(packet[3]);
|
2011-01-10 21:11:58 +00:00
|
|
|
hci_emit_system_bluetooth_enabled(iphone_system_bt_enabled());
|
|
|
|
break;
|
|
|
|
|
2010-01-09 18:25:30 +00:00
|
|
|
case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED");
|
2010-01-09 18:25:30 +00:00
|
|
|
hci_emit_system_bluetooth_enabled(iphone_system_bt_enabled());
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED:
|
|
|
|
case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED:
|
|
|
|
hci_emit_system_bluetooth_enabled(0);
|
|
|
|
break;
|
|
|
|
#endif
|
2011-01-10 21:11:58 +00:00
|
|
|
case BTSTACK_SET_DISCOVERABLE:
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("BTSTACK_SET_DISCOVERABLE discoverable %u)", packet[3]);
|
2011-01-10 21:11:58 +00:00
|
|
|
// track client discoverable requests
|
|
|
|
client = client_for_connection(connection);
|
|
|
|
if (!client) break;
|
|
|
|
client->discoverable = packet[3];
|
|
|
|
// merge state
|
|
|
|
hci_discoverable_control(clients_require_discoverable());
|
|
|
|
break;
|
2011-04-30 20:11:20 +00:00
|
|
|
case BTSTACK_SET_BLUETOOTH_ENABLED:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("BTSTACK_SET_BLUETOOTH_ENABLED: %u\n", packet[3]);
|
2011-04-30 20:11:20 +00:00
|
|
|
if (packet[3]) {
|
|
|
|
// global enable
|
|
|
|
global_enable = 1;
|
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
} else {
|
|
|
|
global_enable = 0;
|
|
|
|
clients_clear_power_request();
|
|
|
|
hci_power_control(HCI_POWER_OFF);
|
|
|
|
}
|
|
|
|
break;
|
2010-08-10 20:43:55 +00:00
|
|
|
case L2CAP_CREATE_CHANNEL_MTU:
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
psm = READ_BT_16(packet, 9);
|
|
|
|
mtu = READ_BT_16(packet, 11);
|
|
|
|
l2cap_create_channel_internal( connection, NULL, addr, psm, mtu);
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
case L2CAP_CREATE_CHANNEL:
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
psm = READ_BT_16(packet, 9);
|
2010-08-10 20:43:55 +00:00
|
|
|
l2cap_create_channel_internal( connection, NULL, addr, psm, 150); // until r865
|
2009-07-31 21:41:15 +00:00
|
|
|
break;
|
|
|
|
case L2CAP_DISCONNECT:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
reason = packet[5];
|
|
|
|
l2cap_disconnect_internal(cid, reason);
|
|
|
|
break;
|
2010-01-25 20:25:20 +00:00
|
|
|
case L2CAP_REGISTER_SERVICE:
|
|
|
|
psm = READ_BT_16(packet, 3);
|
|
|
|
mtu = READ_BT_16(packet, 5);
|
2014-01-17 13:58:37 +00:00
|
|
|
l2cap_register_service_internal(connection, NULL, psm, mtu, LEVEL_0);
|
2010-01-25 20:25:20 +00:00
|
|
|
break;
|
|
|
|
case L2CAP_UNREGISTER_SERVICE:
|
|
|
|
psm = READ_BT_16(packet, 3);
|
|
|
|
l2cap_unregister_service_internal(connection, psm);
|
|
|
|
break;
|
|
|
|
case L2CAP_ACCEPT_CONNECTION:
|
2010-01-26 20:49:02 +00:00
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
l2cap_accept_connection_internal(cid);
|
2010-01-25 20:25:20 +00:00
|
|
|
break;
|
|
|
|
case L2CAP_DECLINE_CONNECTION:
|
2010-01-26 20:49:02 +00:00
|
|
|
cid = READ_BT_16(packet, 3);
|
2010-01-25 20:25:20 +00:00
|
|
|
reason = packet[7];
|
2010-01-26 20:49:02 +00:00
|
|
|
l2cap_decline_connection_internal(cid, reason);
|
2010-01-25 20:25:20 +00:00
|
|
|
break;
|
2011-04-30 20:30:05 +00:00
|
|
|
case RFCOMM_CREATE_CHANNEL:
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
rfcomm_channel = packet[9];
|
|
|
|
rfcomm_create_channel_internal( connection, &addr, rfcomm_channel );
|
|
|
|
break;
|
2011-09-11 21:01:09 +00:00
|
|
|
case RFCOMM_CREATE_CHANNEL_WITH_CREDITS:
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
rfcomm_channel = packet[9];
|
|
|
|
rfcomm_credits = packet[10];
|
|
|
|
rfcomm_create_channel_with_initial_credits_internal( connection, &addr, rfcomm_channel, rfcomm_credits );
|
|
|
|
break;
|
2011-04-30 20:30:05 +00:00
|
|
|
case RFCOMM_DISCONNECT:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
reason = packet[5];
|
|
|
|
rfcomm_disconnect_internal(cid);
|
|
|
|
break;
|
|
|
|
case RFCOMM_REGISTER_SERVICE:
|
2011-05-03 21:14:41 +00:00
|
|
|
rfcomm_channel = packet[3];
|
|
|
|
mtu = READ_BT_16(packet, 4);
|
|
|
|
rfcomm_register_service_internal(connection, rfcomm_channel, mtu);
|
2011-04-30 20:30:05 +00:00
|
|
|
break;
|
2011-09-11 21:01:09 +00:00
|
|
|
case RFCOMM_REGISTER_SERVICE_WITH_CREDITS:
|
|
|
|
rfcomm_channel = packet[3];
|
|
|
|
mtu = READ_BT_16(packet, 4);
|
|
|
|
rfcomm_credits = packet[6];
|
|
|
|
rfcomm_register_service_with_initial_credits_internal(connection, rfcomm_channel, mtu, rfcomm_credits);
|
|
|
|
break;
|
2011-04-30 20:30:05 +00:00
|
|
|
case RFCOMM_UNREGISTER_SERVICE:
|
|
|
|
service_channel = READ_BT_16(packet, 3);
|
|
|
|
rfcomm_unregister_service_internal(service_channel);
|
|
|
|
break;
|
|
|
|
case RFCOMM_ACCEPT_CONNECTION:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
rfcomm_accept_connection_internal(cid);
|
|
|
|
break;
|
|
|
|
case RFCOMM_DECLINE_CONNECTION:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
reason = packet[7];
|
|
|
|
rfcomm_decline_connection_internal(cid);
|
|
|
|
break;
|
2011-09-11 21:01:09 +00:00
|
|
|
case RFCOMM_GRANT_CREDITS:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
rfcomm_credits = packet[5];
|
|
|
|
rfcomm_grant_credits(cid, rfcomm_credits);
|
|
|
|
break;
|
2011-05-03 21:14:41 +00:00
|
|
|
case RFCOMM_PERSISTENT_CHANNEL: {
|
|
|
|
if (remote_device_db) {
|
|
|
|
// enforce \0
|
|
|
|
packet[3+248] = 0;
|
2011-05-27 20:47:31 +00:00
|
|
|
rfcomm_channel = remote_device_db->persistent_rfcomm_channel((char*)&packet[3]);
|
2011-05-03 21:14:41 +00:00
|
|
|
} else {
|
|
|
|
// NOTE: hack for non-iOS platforms
|
|
|
|
rfcomm_channel = rfcomm_channel_generator++;
|
|
|
|
}
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("RFCOMM_EVENT_PERSISTENT_CHANNEL %u", rfcomm_channel);
|
2011-05-03 21:14:41 +00:00
|
|
|
uint8_t event[4];
|
|
|
|
event[0] = RFCOMM_EVENT_PERSISTENT_CHANNEL;
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
event[2] = 0;
|
|
|
|
event[3] = rfcomm_channel;
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
|
|
|
|
break;
|
|
|
|
}
|
2011-04-30 20:30:05 +00:00
|
|
|
|
2010-06-12 21:00:22 +00:00
|
|
|
case SDP_REGISTER_SERVICE_RECORD:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("SDP_REGISTER_SERVICE_RECORD size %u\n", size);
|
2010-06-13 08:26:47 +00:00
|
|
|
sdp_register_service_internal(connection, &packet[3]);
|
2010-06-12 21:00:22 +00:00
|
|
|
break;
|
|
|
|
case SDP_UNREGISTER_SERVICE_RECORD:
|
|
|
|
service_record_handle = READ_BT_32(packet, 3);
|
2012-07-30 18:58:08 +00:00
|
|
|
log_info("SDP_UNREGISTER_SERVICE_RECORD handle 0x%x ", service_record_handle);
|
2010-06-13 08:26:47 +00:00
|
|
|
sdp_unregister_service_internal(connection, service_record_handle);
|
2010-06-12 21:00:22 +00:00
|
|
|
break;
|
2013-10-18 14:48:08 +00:00
|
|
|
case SDP_CLIENT_QUERY_RFCOMM_SERVICES:
|
2013-04-26 20:19:34 +00:00
|
|
|
bt_flip_addr(addr, &packet[3]);
|
2013-10-18 14:48:08 +00:00
|
|
|
|
|
|
|
serviceSearchPatternLen = de_get_len(&packet[9]);
|
|
|
|
memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
|
|
|
|
|
2013-04-26 20:19:34 +00:00
|
|
|
sdp_query_rfcomm_register_callback(handle_sdp_rfcomm_service_result, connection);
|
2013-10-18 14:48:08 +00:00
|
|
|
sdp_query_rfcomm_channel_and_name_for_search_pattern(addr, serviceSearchPattern);
|
|
|
|
|
2013-04-26 20:19:34 +00:00
|
|
|
break;
|
2013-10-17 14:28:55 +00:00
|
|
|
case SDP_CLIENT_QUERY_SERVICES:
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
sdp_parser_init();
|
|
|
|
sdp_parser_register_callback(handle_sdp_client_query_result);
|
2013-10-18 14:48:08 +00:00
|
|
|
|
|
|
|
serviceSearchPatternLen = de_get_len(&packet[9]);
|
|
|
|
memcpy(serviceSearchPattern, &packet[9], serviceSearchPatternLen);
|
|
|
|
|
|
|
|
attributeIDListLen = de_get_len(&packet[9+serviceSearchPatternLen]);
|
|
|
|
memcpy(attributeIDList, &packet[9+serviceSearchPatternLen], attributeIDListLen);
|
|
|
|
|
|
|
|
sdp_client_query(addr, (uint8_t*)&serviceSearchPattern[0], (uint8_t*)&attributeIDList[0]);
|
|
|
|
|
|
|
|
// sdp_general_query_for_uuid(addr, 0x1002);
|
2013-10-17 14:28:55 +00:00
|
|
|
break;
|
2014-05-03 22:31:08 +00:00
|
|
|
case GAP_LE_SCAN_START:
|
|
|
|
le_central_start_scan();
|
|
|
|
break;
|
|
|
|
case GAP_LE_SCAN_STOP:
|
|
|
|
le_central_stop_scan();
|
|
|
|
break;
|
|
|
|
case GAP_LE_CONNECT:
|
|
|
|
bt_flip_addr(addr, &packet[4]);
|
|
|
|
addr_type = packet[3];
|
|
|
|
le_central_connect(&addr, addr_type);
|
|
|
|
break;
|
|
|
|
case GAP_DISCONNECT:
|
|
|
|
handle = READ_BT_16(packet, 3);
|
|
|
|
gap_disconnect(handle);
|
|
|
|
break;
|
2014-05-06 21:22:02 +00:00
|
|
|
#if defined(HAVE_MALLOC) && defined(HAVE_BLE)
|
2014-05-08 13:31:06 +00:00
|
|
|
case GATT_DISCOVER_ALL_PRIMARY_SERVICES:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_SERVICE_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-06 21:22:02 +00:00
|
|
|
gatt_client_discover_primary_services(context);
|
|
|
|
break;
|
2014-05-08 13:31:06 +00:00
|
|
|
case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_SERVICE_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
|
|
|
gatt_client_discover_primary_services_by_uuid16(context, READ_BT_16(packet, 5));
|
|
|
|
break;
|
|
|
|
case GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_SERVICE_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
|
|
|
swap128(&packet[25], uuid128);
|
|
|
|
gatt_client_discover_primary_services_by_uuid128(context, uuid128);
|
|
|
|
break;
|
|
|
|
case GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_INCLUDED_SERVICE_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_service(packet, 5, &service);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_find_included_services_for_service(context, &service);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_service(packet, 5, &service);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_discover_characteristics_for_service(context, &service);
|
|
|
|
break;
|
|
|
|
case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_service(packet, 5, &service);
|
2014-05-08 13:31:06 +00:00
|
|
|
swap128(&packet[5 + SERVICE_LENGTH], uuid128);
|
|
|
|
gatt_client_discover_characteristics_for_service_by_uuid128(context, &service, uuid128);
|
|
|
|
break;
|
|
|
|
case GATT_DISCOVER_CHARACTERISTIC_DESCRIPTOR:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_discover_characteristic_descriptors(context, &characteristic);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_READ_VALUE_OF_CHARACTERISTIC:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_VALUE_QUERY_RESULT);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_read_value_of_characteristic(context, &characteristic);
|
|
|
|
break;
|
|
|
|
case GATT_READ_LONG_VALUE_OF_CHARACTERISTIC:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_LONG_CHARACTERISTIC_VALUE_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_read_long_value_of_characteristic(context, &characteristic);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_VALUE_WRITE_RESPONSE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
|
|
|
gatt_client_write_value_of_characteristic_without_response(context, characteristic.value_handle, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_WRITE_VALUE_OF_CHARACTERISTIC:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_VALUE_WRITE_RESPONSE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
|
|
|
gatt_client_write_value_of_characteristic(context, characteristic.value_handle, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_LONG_CHARACTERISTIC_VALUE_WRITE_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
|
|
|
gatt_client_write_long_value_of_characteristic(context, characteristic.value_handle, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_LONG_CHARACTERISTIC_VALUE_WRITE_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
|
|
|
gatt_client_write_long_value_of_characteristic(context, characteristic.value_handle, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_READ_CHARACTERISTIC_DESCRIPTOR:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_read_characteristic_descriptor(context, &descriptor);
|
|
|
|
break;
|
|
|
|
case GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_read_long_characteristic_descriptor(context, &descriptor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_WRITE_CHARACTERISTIC_DESCRIPTOR:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_CHARACTERISTIC_DESCRIPTOR_WRITE_RESPONSE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_write_characteristic_descriptor(context, &descriptor, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR:
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, GATT_LONG_CHARACTERISTIC_DESCRIPTOR_WRITE_COMPLETE);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
|
|
|
data = daemon_get_data_buffer(context);
|
|
|
|
data_length = READ_BT_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_write_long_characteristic_descriptor(context, &descriptor, data_length, data);
|
|
|
|
break;
|
|
|
|
case GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:{
|
|
|
|
uint16_t configuration = READ_BT_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
|
|
|
context = daemon_prepare_gatt_client_context(connection, packet, configuration);
|
|
|
|
if (!context) break;
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
2014-05-08 13:31:06 +00:00
|
|
|
gatt_client_write_client_characteristic_configuration(context, &characteristic, configuration);
|
|
|
|
break;
|
2014-05-06 21:22:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-05-03 22:31:08 +00:00
|
|
|
default:
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("Error: command %u not implemented\n:", READ_CMD_OCF(packet));
|
2009-07-31 21:41:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-07-30 18:58:08 +00:00
|
|
|
|
|
|
|
// verbose log info on command before dumped command unknown to PacketLogger or Wireshark
|
|
|
|
hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int daemon_client_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length){
|
2010-07-29 19:35:34 +00:00
|
|
|
|
|
|
|
int err = 0;
|
2011-01-09 14:48:03 +00:00
|
|
|
client_state_t * client;
|
2010-07-29 19:35:34 +00:00
|
|
|
|
2009-07-22 20:21:22 +00:00
|
|
|
switch (packet_type){
|
|
|
|
case HCI_COMMAND_DATA_PACKET:
|
2009-07-31 21:41:15 +00:00
|
|
|
if (READ_CMD_OGF(data) != OGF_BTSTACK) {
|
|
|
|
// HCI Command
|
|
|
|
hci_send_cmd_packet(data, length);
|
|
|
|
} else {
|
|
|
|
// BTstack command
|
|
|
|
btstack_command_handler(connection, data, length);
|
|
|
|
}
|
2009-07-22 20:21:22 +00:00
|
|
|
break;
|
2009-08-24 21:56:12 +00:00
|
|
|
case HCI_ACL_DATA_PACKET:
|
2010-07-29 19:35:34 +00:00
|
|
|
err = hci_send_acl_packet(data, length);
|
2009-08-24 21:56:12 +00:00
|
|
|
break;
|
2009-08-02 13:32:55 +00:00
|
|
|
case L2CAP_DATA_PACKET:
|
2009-07-31 21:41:15 +00:00
|
|
|
// process l2cap packet...
|
2010-07-29 19:35:34 +00:00
|
|
|
err = l2cap_send_internal(channel, data, length);
|
2011-06-18 11:07:53 +00:00
|
|
|
if (err == BTSTACK_ACL_BUFFERS_FULL) {
|
|
|
|
l2cap_block_new_credits(1);
|
|
|
|
}
|
2009-07-22 20:21:22 +00:00
|
|
|
break;
|
2011-04-30 20:30:05 +00:00
|
|
|
case RFCOMM_DATA_PACKET:
|
|
|
|
// process l2cap packet...
|
|
|
|
err = rfcomm_send_internal(channel, data, length);
|
|
|
|
break;
|
2009-08-09 17:17:00 +00:00
|
|
|
case DAEMON_EVENT_PACKET:
|
2009-08-09 20:18:34 +00:00
|
|
|
switch (data[0]) {
|
2011-01-09 14:48:03 +00:00
|
|
|
case DAEMON_EVENT_CONNECTION_OPENED:
|
2012-04-04 18:22:44 +00:00
|
|
|
log_info("DAEMON_EVENT_CONNECTION_OPENED %p\n",connection);
|
|
|
|
|
2011-01-09 14:48:03 +00:00
|
|
|
client = malloc(sizeof(client_state_t));
|
|
|
|
if (!client) break; // fail
|
|
|
|
client->connection = connection;
|
|
|
|
client->power_mode = HCI_POWER_OFF;
|
|
|
|
client->discoverable = 0;
|
|
|
|
linked_list_add(&clients, (linked_item_t *) client);
|
|
|
|
break;
|
2009-09-29 20:10:24 +00:00
|
|
|
case DAEMON_EVENT_CONNECTION_CLOSED:
|
2012-04-04 18:22:44 +00:00
|
|
|
log_info("DAEMON_EVENT_CONNECTION_CLOSED %p\n",connection);
|
2010-06-13 08:26:47 +00:00
|
|
|
sdp_unregister_services_for_connection(connection);
|
2011-05-05 22:18:39 +00:00
|
|
|
rfcomm_close_connection(connection);
|
2010-01-25 18:29:06 +00:00
|
|
|
l2cap_close_connection(connection);
|
2011-01-09 14:48:03 +00:00
|
|
|
client = client_for_connection(connection);
|
|
|
|
if (!client) break;
|
|
|
|
linked_list_remove(&clients, (linked_item_t *) client);
|
|
|
|
free(client);
|
2011-01-10 21:11:58 +00:00
|
|
|
// update discoverable mode
|
|
|
|
hci_discoverable_control(clients_require_discoverable());
|
|
|
|
// start power off, if last active client
|
|
|
|
if (!clients_require_power_on()){
|
2011-01-09 14:48:03 +00:00
|
|
|
start_power_off_timer();
|
2011-01-08 22:01:06 +00:00
|
|
|
}
|
2009-08-09 20:18:34 +00:00
|
|
|
break;
|
|
|
|
case DAEMON_NR_CONNECTIONS_CHANGED:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Nr Connections changed, new %u\n",data[1]);
|
2011-01-08 22:01:06 +00:00
|
|
|
break;
|
2009-08-09 20:18:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-08-09 17:17:00 +00:00
|
|
|
break;
|
2009-07-22 20:21:22 +00:00
|
|
|
}
|
2010-07-31 09:07:48 +00:00
|
|
|
if (err) {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Daemon Handler: err %d\n", err);
|
2010-07-31 09:07:48 +00:00
|
|
|
}
|
2010-07-29 19:35:34 +00:00
|
|
|
return err;
|
2009-07-22 20:21:22 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 22:22:00 +00:00
|
|
|
|
|
|
|
static void daemon_set_logging_enabled(int enabled){
|
|
|
|
if (enabled && !loggingEnabled){
|
|
|
|
// use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
|
2014-05-08 13:39:53 +00:00
|
|
|
hci_dump_open("/data/btstack/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
|
2012-02-07 22:22:00 +00:00
|
|
|
}
|
|
|
|
if (!enabled && loggingEnabled){
|
|
|
|
hci_dump_close();
|
|
|
|
}
|
|
|
|
loggingEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2011-02-02 21:37:40 +00:00
|
|
|
// local cache used to manage UI status
|
|
|
|
static HCI_STATE hci_state = HCI_STATE_OFF;
|
|
|
|
static int num_connections = 0;
|
2011-06-06 14:12:38 +00:00
|
|
|
static void update_ui_status(void){
|
2011-02-02 21:37:40 +00:00
|
|
|
if (hci_state != HCI_STATE_WORKING) {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_OFF);
|
|
|
|
} else {
|
|
|
|
if (num_connections) {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_ACTIVE);
|
|
|
|
} else {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 21:11:27 +00:00
|
|
|
|
2012-02-14 19:20:42 +00:00
|
|
|
#ifdef USE_SPRINGBOARD
|
2012-02-07 21:11:27 +00:00
|
|
|
static void preferences_changed_callback(void){
|
|
|
|
int logging = platform_iphone_logging_enabled();
|
|
|
|
log_info("Logging enabled: %u\n", logging);
|
2012-02-07 22:22:00 +00:00
|
|
|
daemon_set_logging_enabled(logging);
|
2012-02-07 21:11:27 +00:00
|
|
|
}
|
2012-02-14 19:20:42 +00:00
|
|
|
#endif
|
2012-02-07 21:11:27 +00:00
|
|
|
|
2010-07-18 17:14:11 +00:00
|
|
|
static void deamon_status_event_handler(uint8_t *packet, uint16_t size){
|
|
|
|
|
2009-09-30 20:37:28 +00:00
|
|
|
uint8_t update_status = 0;
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
// handle state event
|
2009-09-30 20:37:28 +00:00
|
|
|
switch (packet[0]) {
|
|
|
|
case BTSTACK_EVENT_STATE:
|
|
|
|
hci_state = packet[2];
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("New state: %u\n", hci_state);
|
2009-09-30 20:37:28 +00:00
|
|
|
update_status = 1;
|
|
|
|
break;
|
|
|
|
case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
|
|
|
|
num_connections = packet[2];
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("New nr connections: %u\n", num_connections);
|
2009-09-30 20:37:28 +00:00
|
|
|
update_status = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-08-15 21:31:23 +00:00
|
|
|
}
|
2009-09-30 20:37:28 +00:00
|
|
|
|
|
|
|
// choose full bluetooth state
|
|
|
|
if (update_status) {
|
2011-02-02 21:37:40 +00:00
|
|
|
update_ui_status();
|
2009-08-15 21:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:20 +00:00
|
|
|
static void daemon_retry_parked(void){
|
2012-02-05 15:46:06 +00:00
|
|
|
|
|
|
|
// socket_connection_retry_parked is not reentrant
|
|
|
|
static int retry_mutex = 0;
|
|
|
|
|
|
|
|
// lock mutex
|
|
|
|
if (retry_mutex) return;
|
|
|
|
retry_mutex = 1;
|
|
|
|
|
2011-06-18 21:17:20 +00:00
|
|
|
// ... try sending again
|
|
|
|
socket_connection_retry_parked();
|
2012-02-05 15:46:06 +00:00
|
|
|
|
2011-06-18 21:17:20 +00:00
|
|
|
if (!socket_connection_has_parked_connections()){
|
|
|
|
l2cap_block_new_credits(0);
|
|
|
|
}
|
2012-02-05 15:46:06 +00:00
|
|
|
|
|
|
|
// unlock mutex
|
|
|
|
retry_mutex = 0;
|
2011-06-18 21:17:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 17:50:59 +00:00
|
|
|
static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2011-06-18 21:17:20 +00:00
|
|
|
switch (packet_type) {
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
deamon_status_event_handler(packet, size);
|
|
|
|
switch (packet[0]){
|
|
|
|
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
|
|
|
|
// ACL buffer freed...
|
|
|
|
daemon_retry_parked();
|
2012-04-04 19:22:19 +00:00
|
|
|
// no need to tell clients
|
|
|
|
return;
|
2011-06-18 21:17:20 +00:00
|
|
|
case RFCOMM_EVENT_CREDITS:
|
|
|
|
// RFCOMM CREDITS received...
|
|
|
|
daemon_retry_parked();
|
|
|
|
break;
|
2014-05-07 20:56:15 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_BLE) && defined(HAVE_MALLOC)
|
|
|
|
case HCI_EVENT_DISCONNECTION_COMPLETE:{
|
|
|
|
uint16_t handle = READ_BT_16(packet, 3);
|
|
|
|
gatt_client_t *context = get_gatt_client_context_for_handle(handle);
|
|
|
|
if (!context) break;
|
|
|
|
// @TODO return some complete event if request is active
|
|
|
|
gatt_client_stop(context);
|
|
|
|
free(context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-06-18 21:17:20 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DAEMON_EVENT_PACKET:
|
|
|
|
switch (packet[0]){
|
|
|
|
case DAEMON_EVENT_NEW_RFCOMM_CREDITS:
|
|
|
|
daemon_retry_parked();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2010-07-31 09:07:48 +00:00
|
|
|
}
|
|
|
|
if (connection) {
|
|
|
|
socket_connection_send_packet(connection, packet_type, channel, packet, size);
|
|
|
|
} else {
|
|
|
|
socket_connection_send_packet_all(packet_type, channel, packet, size);
|
2010-07-18 17:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-28 13:45:53 +00:00
|
|
|
static void handle_sdp_rfcomm_service_result(sdp_query_event_t * rfcomm_event, void * context){
|
2013-04-26 20:19:34 +00:00
|
|
|
switch (rfcomm_event->type){
|
|
|
|
case SDP_QUERY_RFCOMM_SERVICE: {
|
|
|
|
sdp_query_rfcomm_service_event_t * service_event = (sdp_query_rfcomm_service_event_t*) rfcomm_event;
|
2014-05-03 22:31:08 +00:00
|
|
|
int name_len = (int)strlen((const char*)service_event);
|
2013-04-26 20:19:34 +00:00
|
|
|
int event_len = 3 + name_len;
|
|
|
|
uint8_t event[event_len];
|
|
|
|
event[0] = rfcomm_event->type;
|
|
|
|
event[1] = 1 + name_len;
|
2013-04-26 20:51:18 +00:00
|
|
|
event[2] = service_event->channel_nr;
|
|
|
|
memcpy(&event[3], service_event->service_name, name_len);
|
2013-04-26 21:05:42 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, event_len);
|
|
|
|
socket_connection_send_packet(context, HCI_EVENT_PACKET, 0, event, event_len);
|
2013-04-26 20:19:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDP_QUERY_COMPLETE: {
|
2013-04-28 13:45:53 +00:00
|
|
|
sdp_query_complete_event_t * complete_event = (sdp_query_complete_event_t*) rfcomm_event;
|
2013-04-26 20:19:34 +00:00
|
|
|
uint8_t event[] = { rfcomm_event->type, 1, complete_event->status};
|
2013-04-26 21:05:42 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2013-04-26 20:19:34 +00:00
|
|
|
socket_connection_send_packet(context, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 21:37:40 +00:00
|
|
|
|
2013-10-17 14:28:55 +00:00
|
|
|
static void sdp_client_assert_buffer(int size){
|
|
|
|
if (size > attribute_value_buffer_size){
|
|
|
|
log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// define new packet type SDP_CLIENT_PACKET
|
2013-10-18 14:13:50 +00:00
|
|
|
static void handle_sdp_client_query_result(sdp_query_event_t * event){
|
|
|
|
sdp_query_attribute_value_event_t * ve;
|
2013-10-18 10:19:57 +00:00
|
|
|
sdp_query_complete_event_t * complete_event;
|
2013-10-17 14:28:55 +00:00
|
|
|
|
|
|
|
switch (event->type){
|
2013-10-18 10:19:57 +00:00
|
|
|
case SDP_QUERY_ATTRIBUTE_VALUE:
|
2013-10-18 14:13:50 +00:00
|
|
|
ve = (sdp_query_attribute_value_event_t*) event;
|
2013-10-17 14:28:55 +00:00
|
|
|
|
|
|
|
sdp_client_assert_buffer(ve->attribute_length);
|
|
|
|
|
|
|
|
attribute_value[ve->data_offset] = ve->data;
|
|
|
|
|
|
|
|
if ((uint16_t)(ve->data_offset+1) == ve->attribute_length){
|
|
|
|
hexdump(attribute_value, ve->attribute_length);
|
|
|
|
|
|
|
|
int event_len = 1 + 3 * 2 + ve->attribute_length;
|
|
|
|
uint8_t event[event_len];
|
|
|
|
event[0] = SDP_QUERY_ATTRIBUTE_VALUE;
|
|
|
|
event[1] = ve->record_id;
|
|
|
|
event[3] = ve->attribute_id;
|
|
|
|
event[5] = ve->attribute_length;
|
|
|
|
|
|
|
|
memcpy(&event[7], attribute_value, ve->attribute_length);
|
|
|
|
hci_dump_packet(SDP_CLIENT_PACKET, 0, event, event_len);
|
|
|
|
socket_connection_send_packet(NULL, SDP_CLIENT_PACKET, 0, event, event_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-10-18 10:19:57 +00:00
|
|
|
case SDP_QUERY_COMPLETE:
|
|
|
|
complete_event = (sdp_query_complete_event_t*) event;
|
2013-10-17 14:28:55 +00:00
|
|
|
uint8_t event[] = { SDP_QUERY_COMPLETE, 1, complete_event->status};
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
socket_connection_send_packet(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-08 22:45:27 +00:00
|
|
|
static void power_notification_callback(POWER_NOTIFICATION_t notification){
|
|
|
|
switch (notification) {
|
|
|
|
case POWER_WILL_SLEEP:
|
|
|
|
// let's sleep
|
|
|
|
power_management_sleep = 1;
|
|
|
|
hci_power_control(HCI_POWER_SLEEP);
|
|
|
|
break;
|
|
|
|
case POWER_WILL_WAKE_UP:
|
|
|
|
// assume that all clients use Bluetooth -> if connection, start Bluetooth
|
|
|
|
power_management_sleep = 0;
|
2011-01-10 21:11:58 +00:00
|
|
|
if (clients_require_power_on()) {
|
2011-01-08 22:45:27 +00:00
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
static void daemon_sigint_handler(int param){
|
2011-06-03 21:34:00 +00:00
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
2011-06-03 21:34:00 +00:00
|
|
|
// notify daemons
|
2011-04-30 20:11:20 +00:00
|
|
|
notify_post("ch.ringwald.btstack.stopped");
|
|
|
|
#endif
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info(" <= SIGINT received, shutting down..\n");
|
2011-06-03 21:34:00 +00:00
|
|
|
|
2009-08-15 19:52:46 +00:00
|
|
|
hci_power_control( HCI_POWER_OFF);
|
2010-08-30 20:10:51 +00:00
|
|
|
hci_close();
|
2011-06-03 21:34:00 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Good bye, see you.\n");
|
2011-06-03 21:34:00 +00:00
|
|
|
|
2009-08-15 19:52:46 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-07-22 18:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: manage power off timer
|
|
|
|
|
|
|
|
#define USE_POWER_OFF_TIMER
|
|
|
|
|
|
|
|
static void stop_power_off_timer(void){
|
|
|
|
#ifdef USE_POWER_OFF_TIMER
|
|
|
|
if (timeout_active) {
|
|
|
|
run_loop_remove_timer(&timeout);
|
|
|
|
timeout_active = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start_power_off_timer(void){
|
|
|
|
#ifdef USE_POWER_OFF_TIMER
|
|
|
|
stop_power_off_timer();
|
|
|
|
run_loop_set_timer(&timeout, DAEMON_NO_ACTIVE_CLIENT_TIMEOUT);
|
|
|
|
run_loop_add_timer(&timeout);
|
|
|
|
timeout_active = 1;
|
|
|
|
#else
|
|
|
|
hci_power_control(HCI_POWER_OFF);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: manage list of clients
|
|
|
|
|
|
|
|
|
|
|
|
static client_state_t * client_for_connection(connection_t *connection) {
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) clients; it ; it = it->next){
|
|
|
|
client_state_t * client_state = (client_state_t *) it;
|
|
|
|
if (client_state->connection == connection) {
|
|
|
|
return client_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clients_clear_power_request(void){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) clients; it ; it = it->next){
|
|
|
|
client_state_t * client_state = (client_state_t *) it;
|
|
|
|
client_state->power_mode = HCI_POWER_OFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clients_require_power_on(void){
|
|
|
|
|
|
|
|
if (global_enable) return 1;
|
|
|
|
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) clients; it ; it = it->next){
|
|
|
|
client_state_t * client_state = (client_state_t *) it;
|
|
|
|
if (client_state->power_mode == HCI_POWER_ON) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clients_require_discoverable(void){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) clients; it ; it = it->next){
|
|
|
|
client_state_t * client_state = (client_state_t *) it;
|
|
|
|
if (client_state->discoverable) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-28 22:02:28 +00:00
|
|
|
static void usage(const char * name) {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("%s, BTstack background daemon\n", name);
|
|
|
|
log_info("usage: %s [-h|--help] [--tcp]\n", name);
|
|
|
|
log_info(" -h|--help display this usage\n");
|
|
|
|
log_info(" --tcp use TCP server socket instead of local unix socket\n");
|
2010-02-28 22:02:28 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 14:34:29 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
2011-06-03 21:34:00 +00:00
|
|
|
static void * run_loop_thread(void *context){
|
|
|
|
run_loop_execute();
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-06 14:34:29 +00:00
|
|
|
#endif
|
2011-06-03 21:34:00 +00:00
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
#ifdef HAVE_BLE
|
2014-05-08 13:31:06 +00:00
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
static void handle_gatt_client_event(le_event_t * le_event){
|
2014-05-09 13:36:42 +00:00
|
|
|
|
2014-05-07 20:56:15 +00:00
|
|
|
switch(le_event->type){
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
case GATT_SERVICE_QUERY_RESULT:
|
|
|
|
case GATT_INCLUDED_SERVICE_QUERY_RESULT:{
|
|
|
|
uint8_t event[4 + SERVICE_LENGTH];
|
|
|
|
daemon_setup_service_event(le_event, event);
|
2014-05-09 14:28:05 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 13:31:06 +00:00
|
|
|
socket_connection_send_packet((connection_t *)le_event->client->context, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-07 20:56:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-05-08 15:08:47 +00:00
|
|
|
case GATT_CHARACTERISTIC_QUERY_RESULT:{
|
|
|
|
uint8_t event[4 + CHARACTERISTIC_LENGTH];
|
|
|
|
daemon_gatt_setup_characteristic_event(le_event, event);
|
2014-05-09 14:28:05 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 15:08:47 +00:00
|
|
|
socket_connection_send_packet((connection_t *)le_event->client->context, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
2014-05-08 15:08:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 13:31:06 +00:00
|
|
|
case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
|
2014-05-08 15:08:47 +00:00
|
|
|
case GATT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
|
|
|
|
case GATT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:{
|
|
|
|
uint8_t event[4 + CHARACTERISTIC_DESCRIPTOR_LENGTH];
|
|
|
|
daemon_setup_characteristic_descriptor_event(le_event, event);
|
2014-05-09 14:28:05 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 15:08:47 +00:00
|
|
|
socket_connection_send_packet((connection_t *)le_event->client->context, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
2014-05-08 15:08:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 20:34:13 +00:00
|
|
|
case GATT_CHARACTERISTIC_VALUE_QUERY_RESULT: {
|
|
|
|
uint8_t event[4 + 2 + 1 + ATT_MAX_ATTRIBUTE_SIZE]; // (type, len, handle), handle, len, data
|
2014-05-08 15:08:47 +00:00
|
|
|
daemon_setup_characteristic_value_event(le_event, event);
|
2014-05-09 14:28:05 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 15:08:47 +00:00
|
|
|
socket_connection_send_packet((connection_t *)le_event->client->context, HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
2014-05-08 15:08:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 20:34:13 +00:00
|
|
|
case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
|
|
|
|
// TODO
|
|
|
|
break;
|
2014-05-08 13:31:06 +00:00
|
|
|
case GATT_NOTIFICATION:
|
2014-05-11 20:34:13 +00:00
|
|
|
// TODO
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
|
|
|
case GATT_INDICATION:
|
2014-05-11 20:34:13 +00:00
|
|
|
// TODO
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
2014-05-07 20:56:15 +00:00
|
|
|
|
2014-05-08 13:31:06 +00:00
|
|
|
case GATT_SERVICE_QUERY_COMPLETE:
|
|
|
|
case GATT_CHARACTERISTIC_QUERY_COMPLETE:
|
|
|
|
case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_COMPLETE:
|
|
|
|
case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_COMPLETE:
|
|
|
|
case GATT_LONG_CHARACTERISTIC_VALUE_WRITE_COMPLETE:
|
|
|
|
case GATT_CLIENT_CHARACTERISTIC_CONFIGURATION_COMPLETE:
|
|
|
|
case GATT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_COMPLETE:
|
|
|
|
case GATT_LONG_CHARACTERISTIC_DESCRIPTOR_WRITE_COMPLETE:
|
2014-05-08 15:08:47 +00:00
|
|
|
case GATT_CHARACTERISTIC_VALUE_WRITE_RESPONSE:
|
|
|
|
case GATT_CHARACTERISTIC_DESCRIPTOR_WRITE_RESPONSE:
|
|
|
|
send_gatt_query_complete((connection_t *) le_event->client->context, le_event->type, le_event->client->handle, 0);
|
2014-05-08 13:31:06 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-08 15:08:47 +00:00
|
|
|
case GATT_LONG_CHARACTERISTIC_VALUE_WRITE_CANCELED:
|
|
|
|
send_gatt_query_complete((connection_t *) le_event->client->context, le_event->type, le_event->client->handle, 1);
|
|
|
|
break;
|
2014-05-07 20:56:15 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-28 22:02:28 +00:00
|
|
|
int main (int argc, char * const * argv){
|
|
|
|
|
|
|
|
static int tcp_flag = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{ "tcp", no_argument, &tcp_flag, 1 },
|
|
|
|
{ "help", no_argument, 0, 0 },
|
|
|
|
{ 0,0,0,0 } // This is a filler for -1
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
int option_index = -1;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "h", long_options, &option_index);
|
|
|
|
|
|
|
|
if (c == -1) break; // no more option
|
|
|
|
|
|
|
|
// treat long parameter first
|
|
|
|
if (option_index == -1) {
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (option_index) {
|
|
|
|
case 1:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2011-07-22 18:28:58 +00:00
|
|
|
// make stdout unbuffered
|
2010-12-23 20:21:21 +00:00
|
|
|
setbuf(stdout, NULL);
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("BTdaemon started\n");
|
2010-12-23 20:21:21 +00:00
|
|
|
|
|
|
|
// handle CTRL-c
|
|
|
|
signal(SIGINT, daemon_sigint_handler);
|
|
|
|
// handle SIGTERM - suggested for launchd
|
|
|
|
signal(SIGTERM, daemon_sigint_handler);
|
|
|
|
// handle SIGPIPE
|
|
|
|
struct sigaction act;
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset (&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction (SIGPIPE, &act, NULL);
|
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
bt_control_t * control = NULL;
|
2010-08-30 20:10:51 +00:00
|
|
|
|
2009-07-11 10:37:48 +00:00
|
|
|
#ifdef HAVE_TRANSPORT_H4
|
2011-06-05 09:55:32 +00:00
|
|
|
config.device_name = UART_DEVICE;
|
|
|
|
config.baudrate_init = UART_SPEED;
|
|
|
|
config.baudrate_main = 0;
|
2009-07-01 21:55:08 +00:00
|
|
|
config.flowcontrol = 1;
|
2012-02-13 21:51:09 +00:00
|
|
|
#if defined(USE_BLUETOOL) && defined(USE_POWERMANAGEMENT)
|
|
|
|
if (bt_control_iphone_power_management_supported()){
|
|
|
|
// use default (max) UART baudrate over netraph interface
|
|
|
|
config.baudrate_init = 0;
|
|
|
|
transport = hci_transport_h4_iphone_instance();
|
|
|
|
} else {
|
|
|
|
transport = hci_transport_h4_instance();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
transport = hci_transport_h4_instance();
|
|
|
|
#endif
|
2009-07-11 10:37:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TRANSPORT_USB
|
|
|
|
transport = hci_transport_usb_instance();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_BLUETOOL
|
2009-07-01 21:55:08 +00:00
|
|
|
control = &bt_control_iphone;
|
|
|
|
#endif
|
2009-08-15 21:31:23 +00:00
|
|
|
|
2011-08-28 19:10:54 +00:00
|
|
|
#if defined(USE_BLUETOOL) && defined(USE_POWERMANAGEMENT)
|
|
|
|
if (bt_control_iphone_power_management_supported()){
|
2012-02-13 21:51:09 +00:00
|
|
|
hci_transport_h4_iphone_set_enforce_wake_device("/dev/btwake");
|
2011-08-28 19:10:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
#ifdef USE_SPRINGBOARD
|
2009-08-16 22:04:31 +00:00
|
|
|
bluetooth_status_handler = platform_iphone_status_handler;
|
2011-02-02 21:37:40 +00:00
|
|
|
platform_iphone_register_window_manager_restart(update_ui_status);
|
2012-02-07 21:11:27 +00:00
|
|
|
platform_iphone_register_preferences_changed(preferences_changed_callback);
|
2009-08-15 21:31:23 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-30 20:10:51 +00:00
|
|
|
#ifdef REMOTE_DEVICE_DB
|
|
|
|
remote_device_db = &REMOTE_DEVICE_DB;
|
|
|
|
#endif
|
2011-08-28 19:10:54 +00:00
|
|
|
|
2011-01-07 22:21:25 +00:00
|
|
|
run_loop_init(RUN_LOOP_POSIX);
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2011-01-08 22:45:27 +00:00
|
|
|
// init power management notifications
|
2011-01-12 19:36:27 +00:00
|
|
|
if (control && control->register_for_power_notifications){
|
2011-01-08 22:45:27 +00:00
|
|
|
control->register_for_power_notifications(power_notification_callback);
|
|
|
|
}
|
|
|
|
|
2012-02-07 22:22:00 +00:00
|
|
|
// logging
|
|
|
|
loggingEnabled = 0;
|
|
|
|
int newLoggingEnabled = 1;
|
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
// iPhone has toggle in Preferences.app
|
|
|
|
newLoggingEnabled = platform_iphone_logging_enabled();
|
|
|
|
#endif
|
|
|
|
daemon_set_logging_enabled(newLoggingEnabled);
|
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
// init HCI
|
2010-08-30 20:10:51 +00:00
|
|
|
hci_init(transport, &config, control, remote_device_db);
|
2013-10-31 21:16:00 +00:00
|
|
|
|
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
// iPhone doesn't use SSP yet as there's no UI for it yet and auto accept is not an option
|
|
|
|
hci_ssp_set_enable(0);
|
|
|
|
#endif
|
2009-07-01 21:55:08 +00:00
|
|
|
// init L2CAP
|
|
|
|
l2cap_init();
|
2014-05-07 20:56:15 +00:00
|
|
|
l2cap_register_packet_handler(&daemon_packet_handler);
|
2009-08-09 20:18:34 +00:00
|
|
|
timeout.process = daemon_no_connections_timeout;
|
2010-03-02 20:58:27 +00:00
|
|
|
|
2011-04-30 20:30:05 +00:00
|
|
|
#ifdef HAVE_RFCOMM
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("config.h: HAVE_RFCOMM\n");
|
2011-04-30 20:30:05 +00:00
|
|
|
rfcomm_init();
|
2014-05-07 20:56:15 +00:00
|
|
|
rfcomm_register_packet_handler(&daemon_packet_handler);
|
2011-04-30 20:30:05 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-02 15:40:31 +00:00
|
|
|
#ifdef HAVE_SDP
|
2010-06-09 17:34:52 +00:00
|
|
|
sdp_init();
|
2014-05-07 20:56:15 +00:00
|
|
|
sdp_register_packet_handler(&daemon_packet_handler);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_BLE
|
2014-05-09 13:36:42 +00:00
|
|
|
gatt_client_init();
|
2014-05-07 20:56:15 +00:00
|
|
|
gatt_client_register_handler(&handle_gatt_client_event);
|
2010-10-02 15:40:31 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-02 20:58:27 +00:00
|
|
|
#ifdef USE_LAUNCHD
|
|
|
|
socket_connection_create_launchd();
|
|
|
|
#else
|
2009-07-01 21:55:08 +00:00
|
|
|
// create server
|
2010-02-28 22:02:28 +00:00
|
|
|
if (tcp_flag) {
|
|
|
|
socket_connection_create_tcp(BTSTACK_PORT);
|
|
|
|
} else {
|
|
|
|
socket_connection_create_unix(BTSTACK_UNIX);
|
|
|
|
}
|
2010-03-02 20:58:27 +00:00
|
|
|
#endif
|
2014-05-07 20:56:15 +00:00
|
|
|
socket_connection_register_packet_callback(&daemon_client_handler);
|
2011-01-08 22:45:27 +00:00
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
2011-06-03 21:34:00 +00:00
|
|
|
// notify daemons
|
2011-04-30 20:11:20 +00:00
|
|
|
notify_post("ch.ringwald.btstack.started");
|
2011-06-03 21:34:00 +00:00
|
|
|
|
|
|
|
// spawn thread to have BTstack run loop on new thread, while main thread is used to keep CFRunLoop
|
|
|
|
pthread_t run_loop;
|
|
|
|
pthread_create(&run_loop, NULL, &run_loop_thread, NULL);
|
|
|
|
|
|
|
|
// needed to receive notifications
|
|
|
|
CFRunLoopRun();
|
2011-04-30 20:11:20 +00:00
|
|
|
#endif
|
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
// go!
|
|
|
|
run_loop_execute();
|
|
|
|
return 0;
|
2011-01-08 22:01:06 +00:00
|
|
|
}
|