From 31452debc2bfa22d26e123b5e39ddebfe4426157 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Thu, 16 Jul 2009 22:04:11 +0000 Subject: [PATCH] client/server: client cmd received, but crash during response --- project.xcodeproj/project.pbxproj | 101 ++++++++++++++++++++++++++++++ src/btstack.c | 11 +++- src/daemon.c | 1 + src/hci.c | 17 ++--- src/socket_server.c | 18 ++++-- src/socket_server.h | 1 + 6 files changed, 135 insertions(+), 14 deletions(-) diff --git a/project.xcodeproj/project.pbxproj b/project.xcodeproj/project.pbxproj index 4a8556492..6a6854b56 100644 --- a/project.xcodeproj/project.pbxproj +++ b/project.xcodeproj/project.pbxproj @@ -14,6 +14,14 @@ 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 */; }; + 9C7B5D00100FC9AA0065D87E /* bt_control_iphone.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECB820FCC85650085DAC5 /* bt_control_iphone.c */; }; + 9C7B5D01100FC9AE0065D87E /* btstack.c in Sources */ = {isa = PBXBuildFile; fileRef = 9CC813A10FFC0774002816F9 /* btstack.c */; }; + 9C7B5D02100FC9B50065D87E /* hci.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C46FC340FA906F700ABEF05 /* hci.c */; }; + 9C7B5D03100FC9BB0065D87E /* test.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7B5B7E100D04450065D87E /* test.c */; }; + 9C7B5D04100FC9C20065D87E /* hci_dump.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECBB40FCC95DD0085DAC5 /* hci_dump.c */; }; + 9C7B5D0D100FC9CE0065D87E /* linked_list.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7B5ABF100BD3340065D87E /* linked_list.c */; }; + 9C7B5D0E100FC9D00065D87E /* run_loop.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C1F0E980FDAE023008F472F /* run_loop.c */; }; + 9C7B5D12100FC9F00065D87E /* l2cap.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C88500C0FBF6702004980E4 /* l2cap.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 */; }; @@ -49,6 +57,7 @@ 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 = ""; }; + 9C7B5CF6100FC96A0065D87E /* clientTest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; name = clientTest; path = build/Debug/clientTest; 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 = ""; }; @@ -73,6 +82,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 9C7B5CF4100FC96A0065D87E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -125,6 +141,7 @@ isa = PBXGroup; children = ( 8DD76FB20486AB0100D96B5E /* project */, + 9C7B5CF6100FC96A0065D87E /* clientTest */, ); name = Products; sourceTree = ""; @@ -165,6 +182,22 @@ productReference = 8DD76FB20486AB0100D96B5E /* project */; productType = "com.apple.product-type.tool"; }; + 9C7B5CF5100FC96A0065D87E /* clientTest */ = { + isa = PBXNativeTarget; + buildConfigurationList = 9C7B5CFD100FC9910065D87E /* Build configuration list for PBXNativeTarget "clientTest" */; + buildPhases = ( + 9C7B5CF3100FC96A0065D87E /* Sources */, + 9C7B5CF4100FC96A0065D87E /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = clientTest; + productName = clientTest; + productReference = 9C7B5CF6100FC96A0065D87E /* clientTest */; + productType = "com.apple.product-type.tool"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -178,6 +211,7 @@ projectRoot = ""; targets = ( 8DD76FA90486AB0100D96B5E /* project */, + 9C7B5CF5100FC96A0065D87E /* clientTest */, ); }; /* End PBXProject section */ @@ -201,6 +235,21 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 9C7B5CF3100FC96A0065D87E /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 9C7B5D00100FC9AA0065D87E /* bt_control_iphone.c in Sources */, + 9C7B5D01100FC9AE0065D87E /* btstack.c in Sources */, + 9C7B5D02100FC9B50065D87E /* hci.c in Sources */, + 9C7B5D03100FC9BB0065D87E /* test.c in Sources */, + 9C7B5D04100FC9C20065D87E /* hci_dump.c in Sources */, + 9C7B5D0D100FC9CE0065D87E /* linked_list.c in Sources */, + 9C7B5D0E100FC9D00065D87E /* run_loop.c in Sources */, + 9C7B5D12100FC9F00065D87E /* l2cap.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ @@ -267,6 +316,48 @@ }; name = Release; }; + 9C7B5CF8100FC96E0065D87E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_MODEL_TUNING = G5; + GCC_OPTIMIZATION_LEVEL = 0; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = clientTest; + }; + name = Debug; + }; + 9C7B5CF9100FC96E0065D87E /* Debug iPhone */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_MODEL_TUNING = G5; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = clientTest; + }; + name = "Debug iPhone"; + }; + 9C7B5CFA100FC96E0065D87E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_ENABLE_FIX_AND_CONTINUE = NO; + GCC_MODEL_TUNING = G5; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = clientTest; + ZERO_LINK = NO; + }; + name = Release; + }; 9C8852C10FC360E8004980E4 /* Debug iPhone */ = { isa = XCBuildConfiguration; buildSettings = { @@ -322,6 +413,16 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 9C7B5CFD100FC9910065D87E /* Build configuration list for PBXNativeTarget "clientTest" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 9C7B5CF8100FC96E0065D87E /* Debug */, + 9C7B5CF9100FC96E0065D87E /* Debug iPhone */, + 9C7B5CFA100FC96E0065D87E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 08FB7793FE84155DC02AAC07 /* Project object */; diff --git a/src/btstack.c b/src/btstack.c index 909a00938..8fe7e3e64 100644 --- a/src/btstack.c +++ b/src/btstack.c @@ -14,6 +14,8 @@ #include #include +#include + typedef struct packet_header { uint16_t length; uint8_t type; @@ -54,6 +56,7 @@ static void (*acl_packet_handler) (uint8_t *packet, int size) = dummy_handler; int bt_open(){ // TCP struct protoent* tcp = getprotobyname("tcp"); + btstack_socket = socket(PF_INET, SOCK_STREAM, tcp->p_proto); if(btstack_socket == -1){ return -1; @@ -152,8 +155,14 @@ int bt_send_cmd(hci_cmd_t *cmd, ...){ va_start(argptr, cmd); uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr); va_end(argptr); + packet_header_t header; + bt_store_16( (uint8_t*) &header.length, 0, len); + header.type = HCI_COMMAND_DATA_PACKET; + hexdump( (uint8_t*)&header, sizeof(header)); // TODO use packet instead of tweaking sizeof(header)); + write(btstack_socket, (uint8_t*)&header, sizeof(header)); + hexdump( hci_cmd_buffer, len); + write(btstack_socket, hci_cmd_buffer, len); return len; - return 0; } // send hci acl packet diff --git a/src/daemon.c b/src/daemon.c index 6e5d4a465..29d21ede5 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -83,6 +83,7 @@ int main (int argc, const char * argv[]){ // create server socket_server_create_tcp(1919); + socket_server_register_process_callback(socket_server_connection_process); // go! run_loop_execute(); diff --git a/src/hci.c b/src/hci.c index 751b8a501..9216df67f 100644 --- a/src/hci.c +++ b/src/hci.c @@ -342,17 +342,25 @@ uint32_t hci_run(){ int hci_send_acl_packet(uint8_t *packet, int size){ + return hci_stack.hci_transport->send_acl_packet(packet, size); +} + +int hci_send_cmd_packet(uint8_t *packet, int size){ if (READ_CMD_OGF(packet) != OGF_BTSTACK) { - return hci_stack.hci_transport->send_acl_packet(packet, size); + hci_stack.num_cmd_packets--; + return hci_stack.hci_transport->send_cmd_packet(packet, size); } + hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size); + // BTstack internal commands uint8_t event[3]; switch (READ_CMD_OCF(packet)){ case HCI_BTSTACK_GET_STATE: - event[0] = HCI_EVENT_BTSTACK_STATE; + event[0] = HCI_EVENT_BTSTACK_STATE; event[1] = 1; event[2] = hci_stack.state; + hci_dump_packet( HCI_EVENT_PACKET, 0, event, 3); hci_stack.event_packet_handler(event, 3); break; default: @@ -363,11 +371,6 @@ int hci_send_acl_packet(uint8_t *packet, int size){ return 0; } -int hci_send_cmd_packet(uint8_t *packet, int size){ - hci_stack.num_cmd_packets--; - return hci_stack.hci_transport->send_cmd_packet(packet, size); -} - uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr){ hci_cmd_buffer[0] = cmd->opcode & 0xff; diff --git a/src/socket_server.c b/src/socket_server.c index 47e22a37b..24581f51a 100644 --- a/src/socket_server.c +++ b/src/socket_server.c @@ -92,8 +92,10 @@ static int socket_server_echo_process(struct data_source *ds, int ready) { int socket_server_connection_process(struct data_source *ds, int ready) { connection_t *conn = (connection_t *) ds; - int bytes_read = read(ds->fd, &conn->buffer[conn->bytes_read], - sizeof(packet_header_t) - conn->bytes_read); + int bytes_read = read(ds->fd, &conn->buffer[conn->bytes_read], conn->bytes_to_read); + + printf("socket_server_connection_process: state %u, new %u, read %u, toRead %u\n", conn->state, + bytes_read, conn->bytes_read, conn->bytes_to_read); if (bytes_read <= 0){ // free socket_server_free_connection( linked_item_get_user(&ds->item)); @@ -101,22 +103,23 @@ int socket_server_connection_process(struct data_source *ds, int ready) { } conn->bytes_read += bytes_read; conn->bytes_to_read -= bytes_read; + hexdump( conn->buffer, conn->bytes_read); if (conn->bytes_to_read > 0) { return 0; } switch (conn->state){ case SOCKET_W4_HEADER: conn->state = SOCKET_W4_DATA; - conn->bytes_to_read = READ_BT_16( conn->buffer, 0) + sizeof(packet_header_t); + conn->bytes_to_read = READ_BT_16( conn->buffer, 0); break; case SOCKET_W4_DATA: // process packet !!! switch (conn->buffer[2]){ case HCI_COMMAND_DATA_PACKET: - hci_send_cmd_packet(&conn->buffer[3], READ_BT_16( conn->buffer, 0)); + hci_send_cmd_packet(&conn->buffer[sizeof(packet_header_t)], READ_BT_16( conn->buffer, 0)); break; case HCI_ACL_DATA_PACKET: - hci_send_acl_packet(&conn->buffer[3], READ_BT_16( conn->buffer, 0)); + hci_send_acl_packet(&conn->buffer[sizeof(packet_header_t)], READ_BT_16( conn->buffer, 0)); break; default: break; @@ -140,6 +143,7 @@ int socket_server_send_packet_all(uint8_t type, uint8_t *packet, uint16_t size){ next = (connection_t *) it->item.next; // cache pointer to next connection_t to allow for removal write(it->ds.fd, &length, 2); write(it->ds.fd, &type, 1); + write(it->ds.fd, &type, 1); // padding for now write(it->ds.fd, packet, size); } return 0; @@ -167,7 +171,7 @@ static int socket_server_accept(struct data_source *socket_ds, int ready) { // init state machine conn->state = SOCKET_W4_HEADER; conn->bytes_read = 0; - conn->bytes_to_read = 2; + conn->bytes_to_read = sizeof(packet_header_t); /* New connection coming in! */ conn->ds.fd = accept(socket_ds->fd, NULL, NULL); @@ -179,6 +183,8 @@ static int socket_server_accept(struct data_source *socket_ds, int ready) { // non-blocking ? // socket_server_set_non_blocking(ds->fd); + printf("socket_server_accept new connection %u\n", conn->ds.fd); + // add this socket to the run_loop linked_item_set_user( &conn->ds.item, conn); run_loop_add( &conn->ds ); diff --git a/src/socket_server.h b/src/socket_server.h index 5a468632d..3e04639e9 100644 --- a/src/socket_server.h +++ b/src/socket_server.h @@ -24,6 +24,7 @@ data_source_t * socket_server_create_unix(char *path); * @todo: hack callback to allow data reception - replace with better architecture */ void socket_server_register_process_callback( int (*process_callback)(struct data_source *ds, int ready) ); +int socket_server_connection_process(struct data_source *ds, int ready); void socket_server_send_event_all(uint8_t *packet, uint16_t size); void socket_server_send_acl_all(uint8_t *packet, uint16_t size);