2009-10-29 20:25:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
/*
|
|
|
|
* daemon.c
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 7/1/09.
|
|
|
|
*
|
|
|
|
* BTstack background daemon
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-07-14 18:29:29 +00:00
|
|
|
#include "../config.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
|
|
|
|
|
|
|
#include "hci.h"
|
|
|
|
#include "hci_dump.h"
|
|
|
|
#include "l2cap.h"
|
2009-09-28 21:19:05 +00:00
|
|
|
#include <btstack/linked_list.h>
|
|
|
|
#include <btstack/run_loop.h>
|
2009-07-22 20:27:00 +00:00
|
|
|
#include "socket_connection.h"
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2009-07-11 10:37:48 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
#include "bt_control_iphone.h"
|
|
|
|
#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_H4
|
|
|
|
#include "hci_transport_h4.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
#include "hci_transport_usb.h"
|
|
|
|
#endif
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2009-10-25 21:43:04 +00:00
|
|
|
#define DAEMON_NO_CONNECTION_TIMEOUT 20000
|
2009-08-09 20:18:34 +00:00
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
static hci_transport_t * transport;
|
|
|
|
static hci_uart_config_t config;
|
|
|
|
|
2009-11-23 19:45:32 +00:00
|
|
|
static timer_source_t timeout;
|
2009-08-09 20:18:34 +00:00
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
static void dummy_bluetooth_status_handler(BLUETOOTH_STATE state){
|
|
|
|
printf("Bluetooth status: %u\n", state);
|
|
|
|
};
|
|
|
|
static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth_status_handler;
|
|
|
|
|
2009-08-09 20:18:34 +00:00
|
|
|
static void daemon_no_connections_timeout(){
|
2009-08-30 18:17:16 +00:00
|
|
|
#ifdef USE_LAUNCHD
|
2009-08-19 21:31:40 +00:00
|
|
|
printf("No connection for %u seconds -> POWER OFF and quit\n", DAEMON_NO_CONNECTION_TIMEOUT);
|
2009-08-09 20:18:34 +00:00
|
|
|
hci_power_control( HCI_POWER_OFF);
|
2009-08-19 21:31:40 +00:00
|
|
|
exit(0);
|
2009-08-30 18:17:16 +00:00
|
|
|
#else
|
|
|
|
printf("No connection for %u seconds -> POWER OFF\n", DAEMON_NO_CONNECTION_TIMEOUT);
|
|
|
|
hci_power_control( HCI_POWER_OFF);
|
|
|
|
#endif
|
2009-08-09 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
static int btstack_command_handler(connection_t *connection, uint8_t *packet, uint16_t size){
|
|
|
|
// BTstack Commands
|
|
|
|
hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
|
|
|
|
bd_addr_t addr;
|
|
|
|
uint16_t cid;
|
|
|
|
uint16_t psm;
|
|
|
|
uint8_t reason;
|
|
|
|
// 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:
|
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:
|
2009-07-31 21:41:15 +00:00
|
|
|
hci_power_control(packet[3]);
|
|
|
|
break;
|
2009-09-29 19:40:55 +00:00
|
|
|
case BTSTACK_SET_ACL_CAPTURE_MODE:
|
2009-08-24 21:56:12 +00:00
|
|
|
if (packet[3]) {
|
|
|
|
l2cap_set_capture_connection(connection);
|
|
|
|
} else {
|
|
|
|
l2cap_set_capture_connection(NULL);
|
|
|
|
}
|
|
|
|
break;
|
2010-01-09 11:01:23 +00:00
|
|
|
case BTSTACK_GET_VERSION:
|
|
|
|
hci_emit_btstack_version();
|
|
|
|
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);
|
|
|
|
l2cap_create_channel_internal( connection, addr, psm );
|
|
|
|
break;
|
|
|
|
case L2CAP_DISCONNECT:
|
|
|
|
cid = READ_BT_16(packet, 3);
|
|
|
|
reason = packet[5];
|
|
|
|
l2cap_disconnect_internal(cid, reason);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//@TODO: log into hci dump as vendor specific "event"
|
2009-09-22 20:03:48 +00:00
|
|
|
fprintf(stderr, "Error: command %u not implemented\n:", READ_CMD_OCF(packet));
|
2009-07-31 21:41:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int daemon_client_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length){
|
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:
|
|
|
|
hci_send_acl_packet(data, length);
|
|
|
|
break;
|
2009-08-02 13:32:55 +00:00
|
|
|
case L2CAP_DATA_PACKET:
|
2009-07-31 21:41:15 +00:00
|
|
|
// process l2cap packet...
|
|
|
|
l2cap_send_internal(channel, data, length);
|
2009-07-22 20:21:22 +00:00
|
|
|
break;
|
2009-08-09 17:17:00 +00:00
|
|
|
case DAEMON_EVENT_PACKET:
|
2009-08-09 20:18:34 +00:00
|
|
|
switch (data[0]) {
|
2009-09-29 20:10:24 +00:00
|
|
|
case DAEMON_EVENT_CONNECTION_CLOSED:
|
2009-08-09 20:18:34 +00:00
|
|
|
l2cap_close_channels_for_connection(connection);
|
|
|
|
break;
|
|
|
|
case DAEMON_NR_CONNECTIONS_CHANGED:
|
2009-09-22 20:03:48 +00:00
|
|
|
printf("Nr Connections changed, new %u\n", data[1]);
|
2009-08-09 20:18:34 +00:00
|
|
|
if (data[1]) {
|
|
|
|
run_loop_remove_timer(&timeout);
|
|
|
|
} else {
|
|
|
|
run_loop_set_timer(&timeout, DAEMON_NO_CONNECTION_TIMEOUT);
|
|
|
|
run_loop_add_timer(&timeout);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-08-09 17:17:00 +00:00
|
|
|
break;
|
2009-07-22 20:21:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
static void daemon_event_handler(uint8_t *packet, uint16_t size){
|
2009-09-30 20:37:28 +00:00
|
|
|
// local cache
|
|
|
|
static HCI_STATE hci_state = HCI_STATE_OFF;
|
|
|
|
static int num_connections = 0;
|
|
|
|
|
|
|
|
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];
|
2009-09-30 21:32:38 +00:00
|
|
|
printf("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];
|
2009-09-30 21:32:38 +00:00
|
|
|
printf("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) {
|
|
|
|
if (hci_state != HCI_STATE_WORKING) {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_OFF);
|
2009-08-15 21:31:23 +00:00
|
|
|
} else {
|
2009-09-30 20:37:28 +00:00
|
|
|
if (num_connections) {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_ACTIVE);
|
|
|
|
} else {
|
|
|
|
bluetooth_status_handler(BLUETOOTH_ON);
|
|
|
|
}
|
2009-08-15 21:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward event to clients
|
|
|
|
socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, packet, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void daemon_sigint_handler(int param){
|
2009-08-15 19:52:46 +00:00
|
|
|
printf(" <= SIGINT received, shutting down..\n");
|
|
|
|
hci_power_control( HCI_POWER_OFF);
|
|
|
|
printf("Good bye, see you.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-10-25 21:43:04 +00:00
|
|
|
static void daemon_sigpipe_handler(int param){
|
|
|
|
printf(" <= SIGPIPE received.. trying to ignore..\n");
|
|
|
|
}
|
|
|
|
|
2009-07-11 17:42:03 +00:00
|
|
|
int main (int argc, const char * argv[]){
|
2009-07-01 21:55:08 +00:00
|
|
|
|
|
|
|
bt_control_t * control = NULL;
|
|
|
|
|
2009-07-11 10:37:48 +00:00
|
|
|
#ifdef HAVE_TRANSPORT_H4
|
|
|
|
transport = hci_transport_h4_instance();
|
|
|
|
config.device_name = UART_DEVICE;
|
|
|
|
config.baudrate = UART_SPEED;
|
2009-07-01 21:55:08 +00:00
|
|
|
config.flowcontrol = 1;
|
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
|
|
|
|
|
|
|
#ifdef USE_SPRINGBOARD
|
2009-08-16 22:04:31 +00:00
|
|
|
bluetooth_status_handler = platform_iphone_status_handler;
|
2009-08-15 21:31:23 +00:00
|
|
|
#endif
|
|
|
|
|
2009-10-07 20:17:19 +00:00
|
|
|
run_loop_init(RUN_LOOP_POSIX);
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// @TODO: allow configuration per HCI CMD
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-07-16 20:12:42 +00:00
|
|
|
// use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
|
2009-10-11 23:09:31 +00:00
|
|
|
hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
|
2009-09-30 19:25:56 +00:00
|
|
|
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
|
2009-07-16 20:12:42 +00:00
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
// init HCI
|
|
|
|
hci_init(transport, &config, control);
|
|
|
|
|
|
|
|
// init L2CAP
|
|
|
|
l2cap_init();
|
2009-08-15 21:31:23 +00:00
|
|
|
l2cap_register_event_packet_handler(daemon_event_handler);
|
2009-08-09 20:18:34 +00:00
|
|
|
timeout.process = daemon_no_connections_timeout;
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// @TODO: make choice of socket server configurable (TCP and/or Unix Domain Socket)
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
// create server
|
2009-08-19 20:09:30 +00:00
|
|
|
// socket_connection_create_tcp(BTSTACK_PORT);
|
|
|
|
socket_connection_create_unix(BTSTACK_UNIX);
|
2009-08-20 22:54:29 +00:00
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
socket_connection_register_packet_callback(daemon_client_handler);
|
2009-08-15 19:52:46 +00:00
|
|
|
|
|
|
|
// handle CTRL-c
|
|
|
|
signal(SIGINT, daemon_sigint_handler);
|
2009-08-19 21:06:05 +00:00
|
|
|
// handle SIGTERM - suggested for launchd
|
|
|
|
signal(SIGTERM, daemon_sigint_handler);
|
2009-11-23 20:29:38 +00:00
|
|
|
// handle SIGPIPE
|
2009-10-25 21:43:04 +00:00
|
|
|
struct sigaction act;
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset (&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction (SIGPIPE, &act, NULL);
|
|
|
|
|
2009-08-19 21:06:05 +00:00
|
|
|
// make stderr unbuffered
|
|
|
|
setbuf(stderr, NULL);
|
2009-08-20 22:54:29 +00:00
|
|
|
setbuf(stdout, NULL);
|
|
|
|
printf("BTdaemon started - stdout\n");
|
|
|
|
fprintf(stderr,"BTdaemon started - stderr\n");
|
2009-07-01 21:55:08 +00:00
|
|
|
// go!
|
|
|
|
run_loop_execute();
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|