From 37ac922291ca536d9890d405c3d0c6003ecd897a Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Tue, 14 Jul 2009 18:29:29 +0000 Subject: [PATCH] remove test code from daemon and put into example/test.c - not working yet --- project.xcodeproj/project.pbxproj | 16 ++- src/btstack.c | 16 +++ src/btstack.h | 16 +++ src/daemon.c | 209 ++---------------------------- 4 files changed, 57 insertions(+), 200 deletions(-) diff --git a/project.xcodeproj/project.pbxproj b/project.xcodeproj/project.pbxproj index e6395d241..0cfd264e7 100644 --- a/project.xcodeproj/project.pbxproj +++ b/project.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 9C46FC390FA906F700ABEF05 /* hci.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C46FC340FA906F700ABEF05 /* hci.c */; }; 9C46FC3A0FA906F700ABEF05 /* hci_transport_h4.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C46FC360FA906F700ABEF05 /* hci_transport_h4.c */; }; 9C7B5AC0100BD3340065D87E /* linked_list.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7B5ABF100BD3340065D87E /* linked_list.c */; }; + 9C7B5B7F100D04450065D87E /* test.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7B5B7E100D04450065D87E /* test.c */; }; 9C7ECB840FCC85650085DAC5 /* bt_control_iphone.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECB820FCC85650085DAC5 /* bt_control_iphone.c */; }; 9C7ECBB50FCC95DD0085DAC5 /* hci_dump.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECBB40FCC95DD0085DAC5 /* hci_dump.c */; }; 9C88500E0FBF6702004980E4 /* l2cap.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C88500C0FBF6702004980E4 /* l2cap.c */; }; @@ -48,6 +49,7 @@ 9C46FC380FA906F700ABEF05 /* hci_transport.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_transport.h; path = src/hci_transport.h; sourceTree = ""; }; 9C7B5ABE100BD3340065D87E /* linked_list.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = linked_list.h; path = src/linked_list.h; sourceTree = ""; }; 9C7B5ABF100BD3340065D87E /* linked_list.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = linked_list.c; path = src/linked_list.c; sourceTree = ""; }; + 9C7B5B7E100D04450065D87E /* test.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = test.c; path = example/test.c; sourceTree = ""; }; 9C7ECB810FCC85650085DAC5 /* bt_control.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = bt_control.h; path = src/bt_control.h; sourceTree = ""; }; 9C7ECB820FCC85650085DAC5 /* bt_control_iphone.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; name = bt_control_iphone.c; path = src/bt_control_iphone.c; sourceTree = ""; }; 9C7ECB830FCC85650085DAC5 /* bt_control_iphone.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = bt_control_iphone.h; path = src/bt_control_iphone.h; sourceTree = ""; }; @@ -78,12 +80,13 @@ 08FB7794FE84155DC02AAC07 /* project */ = { isa = PBXGroup; children = ( - 9CC152C61009052100223347 /* config.h */, - 9C20720E10025E0500A07EA4 /* libusb-1.0.dylib */, + 9C7B5B81100D04520065D87E /* Example */, 08FB7795FE84155DC02AAC07 /* Source */, C6A0FF2B0290797F04C91782 /* Documentation */, 1AB674ADFE9D54B511CA2CBB /* Products */, + 9CC152C61009052100223347 /* config.h */, 9CA3C0900FB8B3C4005F48DE /* TODO.txt */, + 9C20720E10025E0500A07EA4 /* libusb-1.0.dylib */, ); name = project; sourceTree = ""; @@ -127,6 +130,14 @@ name = Products; sourceTree = ""; }; + 9C7B5B81100D04520065D87E /* Example */ = { + isa = PBXGroup; + children = ( + 9C7B5B7E100D04450065D87E /* test.c */, + ); + name = Example; + sourceTree = ""; + }; C6A0FF2B0290797F04C91782 /* Documentation */ = { isa = PBXGroup; children = ( @@ -188,6 +199,7 @@ 9CC813A50FFC0A51002816F9 /* daemon.c in Sources */, 9C2071F310014D3200A07EA4 /* hci_transport_usb.c in Sources */, 9C7B5AC0100BD3340065D87E /* linked_list.c in Sources */, + 9C7B5B7F100D04450065D87E /* test.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/btstack.c b/src/btstack.c index fa421266e..249eab8d3 100644 --- a/src/btstack.c +++ b/src/btstack.c @@ -8,3 +8,19 @@ #include "btstack.h" +// init BTstack library +int bt_open(); + +// stop using BTstack library +int bt_close(); + +// send hci cmd packet +int hci_send_cmd(hci_cmd_t *cmd, ...); + +// send hci acl packet +int hci_send_acl_packet(uint8_t *packet, int size); + +// register packet and event handler +void bt_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); +void bt_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); + diff --git a/src/btstack.h b/src/btstack.h index b736d91b6..2ee52fb3c 100644 --- a/src/btstack.h +++ b/src/btstack.h @@ -7,3 +7,19 @@ * */ +// init BTstack library +int bt_open(); + +// stop using BTstack library +int bt_close(); + +// send hci cmd packet +int hci_send_cmd(hci_cmd_t *cmd, ...); + +// send hci acl packet +int hci_send_acl_packet(uint8_t *packet, int size); + +// register packet and event handler +void bt_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); +void bt_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); + diff --git a/src/daemon.c b/src/daemon.c index fa4e636a3..196180bd0 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -7,23 +7,15 @@ * */ +#include "../config.h" + #include #include #include #include -#ifdef __APPLE__ -#include -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_2_0 -#define __IPHONE__ -#endif -#endif - -#include "../config.h" - #include "hci.h" #include "hci_dump.h" - #include "l2cap.h" #include "linked_list.h" #include "run_loop.h" @@ -42,186 +34,6 @@ #include "hci_transport_usb.h" #endif -hci_con_handle_t con_handle_out = 0; -hci_con_handle_t con_handle_in = 0; -uint16_t dest_cid; - -#define BT_HID -// #define POWER_CYCLE_TEST -// #define MITM - - -void event_handler(uint8_t *packet, int size){ - // printf("Event type: %x, opcode: %x, other %x\n", packet[0], packet[3] | packet[4] << 8); - -#if defined(BT_HID) || defined(MITM) - bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard - - // bt stack activated, get started - if (packet[0] == BTSTACK_EVENT_HCI_WORKING) { - hci_send_cmd(&hci_write_class_of_device, 0x7A020C); // used on iPhone - } - - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_class_of_device) ) { - hci_send_cmd(&hci_write_local_name, "BT in the Middle"); - } - - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_local_name) ) { -#if 1 - hci_send_cmd(&hci_write_authentication_enable, 1); - } - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable) ) { -#endif -#if 0 - hci_send_cmd(&hci_write_inquiry_mode, 2); - } - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_inquiry_mode) ) { -#endif -#if 0 - hci_send_cmd(&hci_set_event_mask, 0xffffffff, 0x1fffffff); - } - - uint8_t eir[] = { - 0x0d, 0x09, 0x4d, 0x69, 0x6c, 0x61, 0x73, 0x20, 0x69, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x0f, 0x02, - 0x00, 0x12, 0x1f, 0x11, 0x07, 0x11, 0x2f, 0x11, 0x0a, 0x11, 0x16, 0x11, 0x15, 0x11, 0x27, 0xff, - 0x00, 0x4c, 0x02, 0x24, 0x02, 0x86, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - }; - if ( COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask) ) { -#endif -#if 0 - hci_send_cmd(&hci_write_extended_inquiry_response, 0, eir); - } - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_extended_inquiry_response) ) { -#endif -#if 0 - hci_send_cmd(&hci_write_simple_pairing_mode, 1); - } - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_simple_pairing_mode) ) { -#endif -#if 0 - hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 15, 0); - } - if (packet[0] == HCI_EVENT_INQUIRY_COMPLETE) -#endif -#if 1 - hci_send_cmd(&hci_create_connection, &addr, 0x18, 0, 0, 0, 0); -#endif - } - if (packet[0] == HCI_EVENT_CONNECTION_REQUEST){ - bt_flip_addr(addr, &packet[2]); - hci_send_cmd(&hci_accept_connection_request, &addr, 1); - } - - if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){ - printf("Please enter PIN 1234 on remote device\n"); - } - - // connection established -> start L2CAP conection - if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE){ - if (packet[2] == 0){ - // get new connection handle - if (! con_handle_out) { - con_handle_out = READ_BT_16(packet, 3); - } else { - con_handle_in = READ_BT_16(packet, 3); - } -#ifndef MITM - // request l2cap connection - printf("> CONNECTION REQUEST\n"); - l2cap_send_signaling_packet(con_handle_out, CONNECTION_REQUEST, sig_seq_nr++, 0x13, local_cid); -#else - printf("Connected to target device, please start!\n"); -#endif - - } - } -#endif - -#ifdef POWER_CYCLE_TEST - if (packet[0] == BTSTACK_EVENT_HCI_WORKING) { - hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 10, 0); - } - if (packet[0] == HCI_EVENT_INQUIRY_COMPLETE){ - // power cycle - hci_power_control( HCI_POWER_OFF ); - // - printf ("Power off!\n"); - sleep(10); - printf ("Restart!\n"); - hci_power_control( HCI_POWER_ON ); - } -#endif -} - -void acl_handler(uint8_t *packet, int size){ - -#ifdef MITM - // forward packet on "other" connection - hci_con_handle_t incoming_hdl = READ_BT_16(packet, 0) & 0xfff; - hci_con_handle_t outgoing_hdl = 0; -#if 0 - if (incoming_hdl == con_handle_in){ - outgoing_hdl = con_handle_out; - } else if (incoming_hdl == con_handle_out){ - outgoing_hdl = con_handle_in; - } else { - printf("Incoming acl packet on handle %u\n!", incoming_hdl); - } -#else - outgoing_hdl = incoming_hdl; -#endif - if (outgoing_hdl){ - bt_store_16( packet, 0, (READ_BT_16(packet, 0) & 0xf000) | outgoing_hdl); - hci_send_acl_packet(packet, size); - } - -#else - uint16_t source_cid; - uint16_t result; - uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 - // connection response - if (packet[8] == CONNECTION_RESPONSE){ - dest_cid = READ_BT_16(packet, 12); - source_cid = READ_BT_16(packet, 14); - result = READ_BT_16(packet, 16); - uint16_t status = READ_BT_16(packet, 18); - printf("< CONNECTION_RESPONSE: id %u, dest cid %u, src cid %u, result %u, status %u\n", packet[9], dest_cid, source_cid, result, status); - if (result == 0){ - printf("> CONFIGURE_REQUEST: id %u\n", sig_seq_nr); - l2cap_send_signaling_packet(con_handle_out, CONFIGURE_REQUEST, sig_seq_nr++, dest_cid, 0, 4, &config_options); - } - } - else if (packet[8] == CONFIGURE_RESPONSE){ - source_cid = READ_BT_16(packet, 12); - uint16_t flags = READ_BT_16(packet, 14); - result = READ_BT_16(packet, 16); - printf("< CONFIGURE_RESPONSE: id %u, src cid %u, flags %u, result %u!!!\n", packet[9], source_cid, flags, result); - hexdump(&packet[18], size-18); - } - else if (packet[8] == CONFIGURE_REQUEST){ - printf("< CONFIGURE_REQUEST: id %u\n", packet[9]); - hexdump(&packet[16], size-16); - printf("> CONFIGURE_RESPONSE: id %u\n", packet[9]); - l2cap_send_signaling_packet(con_handle_out, CONFIGURE_RESPONSE, packet[9], local_cid, 0, 0, size - 16, &packet[16]); - } - else { - printf("Unknown ACL ^^^ \n"); - } -#endif -} - static hci_transport_t * transport; static hci_uart_config_t config; @@ -244,6 +56,8 @@ int main (int argc, const char * argv[]){ control = &bt_control_iphone; #endif + // @TODO allow configuration per HCI CMD + // use logger: format HCI_DUMP_PACKETLOGGER or HCI_DUMP_BLUEZ hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); @@ -256,22 +70,21 @@ int main (int argc, const char * argv[]){ // // register callbacks // - hci_register_event_packet_handler(&event_handler); - hci_register_acl_packet_handler(&acl_handler); + hci_register_event_packet_handler(&socket_server_send_event_all); + hci_register_acl_packet_handler(&socket_server_send_acl_all); + // @TODO allow control per HCI CMD // turn on hci_power_control(HCI_POWER_ON); + // @TODO make choice of socket server configurable (TCP and/or Unix Domain Socket) + // @TODO make port and/or socket configurable per config.h + // create server socket_server_create_tcp(1919); // go! run_loop_execute(); -/* - while (1) { - libusb_handle_events(NULL); - hci_run(); - } - */ + return 0; } \ No newline at end of file