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-07-01 21:55:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
#include "hci.h"
|
|
|
|
#include "hci_dump.h"
|
|
|
|
#include "l2cap.h"
|
2009-07-13 21:51:42 +00:00
|
|
|
#include "linked_list.h"
|
2009-07-01 21:55:08 +00:00
|
|
|
#include "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
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
static hci_transport_t * transport;
|
|
|
|
static hci_uart_config_t config;
|
|
|
|
|
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)){
|
|
|
|
case HCI_BTSTACK_GET_STATE:
|
|
|
|
hci_emit_state();
|
|
|
|
break;
|
|
|
|
case HCI_BTSTACK_SET_POWER_MODE:
|
|
|
|
hci_power_control(packet[3]);
|
|
|
|
break;
|
|
|
|
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"
|
|
|
|
printf("Error: command %u not implemented\n:", READ_CMD_OCF(packet));
|
|
|
|
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-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:
|
|
|
|
// only one event so far: client connection died
|
|
|
|
l2cap_close_channels_for_connection(connection);
|
|
|
|
break;
|
2009-07-22 20:21:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-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-08-08 21:29:38 +00:00
|
|
|
// hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
|
|
|
|
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-02 10:51:41 +00:00
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// @TODO: make choice of socket server configurable (TCP and/or Unix Domain Socket)
|
|
|
|
// @TODO: make port and/or socket configurable per config.h
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-07-01 21:55:08 +00:00
|
|
|
// create server
|
2009-07-22 21:31:35 +00:00
|
|
|
socket_connection_create_tcp(BTSTACK_PORT);
|
2009-07-31 21:41:15 +00:00
|
|
|
socket_connection_register_packet_callback(daemon_client_handler);
|
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;
|
|
|
|
}
|