From 1f7b95a184f89e56f37b0fb84fff5c8fe20e5f38 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Mon, 25 Jan 2010 20:25:20 +0000 Subject: [PATCH] dispatch l2cap commands --- TODO.txt | 2 +- include/btstack/hci_cmds.h | 10 +++++++++- project.xcodeproj/project.pbxproj | 2 ++ src/daemon.c | 24 +++++++++++++++++++++++- src/hci.c | 15 ++++++++++++++- src/hci_cmds.c | 8 ++++---- src/l2cap.c | 8 ++++++++ src/l2cap.h | 4 ++-- 8 files changed, 63 insertions(+), 10 deletions(-) diff --git a/TODO.txt b/TODO.txt index fa5857968..f008f8f35 100644 --- a/TODO.txt +++ b/TODO.txt @@ -9,7 +9,7 @@ NEXT: - new commands - l2cap_register_service(psm, mtu) - l2cap_unregister_service(psm) - - l2cap_accept_connection( bd_addr_t, dest cid ) + - l2cap_accept_connection_internal( bd_addr_t, dest cid ) - l2cap_deny_connection( bd_addr_t, dest cid, reason ) - events - l2cap_incoming_connection(psm, handle, dest cid) diff --git a/include/btstack/hci_cmds.h b/include/btstack/hci_cmds.h index a377986af..f2b208051 100644 --- a/include/btstack/hci_cmds.h +++ b/include/btstack/hci_cmds.h @@ -115,8 +115,12 @@ // data: event (8), len(8), channel (16) #define L2CAP_EVENT_CHANNEL_CLOSED 0x71 +// data: event(8), len(8), address(48), handle (16), psm (16), dest cid(16) +#define L2CAP_EVENT_INCOMING_CONNECTION 0x72 + // data: event(8), len(8), handle(16) -#define L2CAP_EVENT_TIMEOUT_CHECK 0x72 +#define L2CAP_EVENT_TIMEOUT_CHECK 0x73 + // last HCI error is 0x3d // l2cap errors - enumeration by the command that created them @@ -205,5 +209,9 @@ extern hci_cmd_t hci_write_page_timeout; extern hci_cmd_t hci_write_scan_enable; extern hci_cmd_t hci_write_simple_pairing_mode; +extern hci_cmd_t l2cap_accept_connection; extern hci_cmd_t l2cap_create_channel; +extern hci_cmd_t l2cap_decline_connection; extern hci_cmd_t l2cap_disconnect; +extern hci_cmd_t l2cap_register_service; +extern hci_cmd_t l2cap_unregister_service; diff --git a/project.xcodeproj/project.pbxproj b/project.xcodeproj/project.pbxproj index f52d7226c..6d15a4f78 100644 --- a/project.xcodeproj/project.pbxproj +++ b/project.xcodeproj/project.pbxproj @@ -68,6 +68,7 @@ 9C46FC370FA906F700ABEF05 /* hci_transport_h4.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_transport_h4.h; path = src/hci_transport_h4.h; sourceTree = ""; }; 9C46FC380FA906F700ABEF05 /* hci_transport.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_transport.h; path = src/hci_transport.h; sourceTree = ""; }; 9C5BA8A0110B756500D79BBE /* hci_cmds.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_cmds.h; path = include/btstack/hci_cmds.h; sourceTree = ""; }; + 9C5BABAF110E28E900D79BBE /* l2cap-server.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; name = "l2cap-server.c"; path = "example/l2cap-server.c"; sourceTree = ""; }; 9C6459DE1037554B0081A00B /* platform_iphone.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = platform_iphone.h; path = src/platform_iphone.h; sourceTree = ""; }; 9C77E36410618B1D00F39DCF /* Makefile.in */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text; name = Makefile.in; path = SpringBoardAccess/Makefile.in; sourceTree = ""; }; 9C77E36710618C6500F39DCF /* SpringBoardAccess-test.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; name = "SpringBoardAccess-test.c"; path = "SpringBoardAccess/SpringBoardAccess-test.c"; sourceTree = ""; }; @@ -249,6 +250,7 @@ 9C7B5B81100D04520065D87E /* Example */ = { isa = PBXGroup; children = ( + 9C5BABAF110E28E900D79BBE /* l2cap-server.c */, 9CAA573610A5D87400D0E1A9 /* inquiry.c */, 9C13C9F610ECECE900B04243 /* mouse.c */, 9C994B8E106BEEB700C70311 /* rfcomm.c */, diff --git a/src/daemon.c b/src/daemon.c index abd89168e..a842320d8 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -99,7 +99,9 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui bd_addr_t addr; uint16_t cid; uint16_t psm; - uint8_t reason; + uint16_t mtu; + uint16_t handle; + uint8_t reason; // BTstack internal commands - 16 Bit OpCode, 8 Bit ParamLen, Params... switch (READ_CMD_OCF(packet)){ case BTSTACK_GET_STATE: @@ -141,6 +143,26 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui reason = packet[5]; l2cap_disconnect_internal(cid, reason); break; + case L2CAP_REGISTER_SERVICE: + psm = READ_BT_16(packet, 3); + mtu = READ_BT_16(packet, 5); + l2cap_register_service_internal(connection, psm, mtu); + break; + case L2CAP_UNREGISTER_SERVICE: + psm = READ_BT_16(packet, 3); + l2cap_unregister_service_internal(connection, psm); + break; + case L2CAP_ACCEPT_CONNECTION: + handle = READ_BT_16(packet, 3); + cid = READ_BT_16(packet, 5); + l2cap_accept_connection_internal(handle, cid); + break; + case L2CAP_DECLINE_CONNECTION: + handle = READ_BT_16(packet, 3); + cid = READ_BT_16(packet, 5); + reason = packet[7]; + l2cap_decline_connection_internal(handle, cid, reason); + break; default: //@TODO: log into hci dump as vendor specific "event" fprintf(stderr, "Error: command %u not implemented\n:", READ_CMD_OCF(packet)); diff --git a/src/hci.c b/src/hci.c index 6911ff62b..70c92d043 100644 --- a/src/hci.c +++ b/src/hci.c @@ -177,6 +177,7 @@ static void acl_handler(uint8_t *packet, int size){ static void event_handler(uint8_t *packet, int size){ bd_addr_t addr; hci_con_handle_t handle; + hci_connection_t * conn; switch (packet[0]) { @@ -186,11 +187,23 @@ static void event_handler(uint8_t *packet, int size){ hci_stack.num_cmd_packets = packet[2]; break; + case HCI_EVENT_CONNECTION_REQUEST: + bt_flip_addr(addr, &packet[5]); + printf("Connection_incoming: "); print_bd_addr(addr); printf("\n"); + conn = connection_for_address(addr); + if (!conn) { + conn = create_connection_for_addr(addr); + } + // TODO: check for malloc failure + conn->state = ACCEPTED_CONNECTION_REQUEST; + hci_send_cmd(&hci_accept_connection_request, 1); + break; + case HCI_EVENT_CONNECTION_COMPLETE: // Connection management bt_flip_addr(addr, &packet[5]); printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); - hci_connection_t * conn = connection_for_address(addr); + conn = connection_for_address(addr); if (conn) { if (!packet[2]){ conn->state = OPEN; diff --git a/src/hci_cmds.c b/src/hci_cmds.c index 3774e79ab..3baa06682 100644 --- a/src/hci_cmds.c +++ b/src/hci_cmds.c @@ -320,12 +320,12 @@ OPCODE(OGF_BTSTACK, L2CAP_UNREGISTER_SERVICE), "2" // @param psm (16) }; hci_cmd_t l2cap_accept_connection = { -OPCODE(OGF_BTSTACK, L2CAP_ACCEPT_CONNECTION), "B2" -// @param bd_addr(48), dest cid (16) +OPCODE(OGF_BTSTACK, L2CAP_ACCEPT_CONNECTION), "22" +// @param handle(16), dest cid (16) }; hci_cmd_t l2cap_decline_connection = { - OPCODE(OGF_BTSTACK, L2CAP_DECLINE_CONNECTION), "B2" - // @param bd_addr(48), dest cid (16) +OPCODE(OGF_BTSTACK, L2CAP_DECLINE_CONNECTION), "221" +// @param handle(16), dest cid (16), reason(8) }; diff --git a/src/l2cap.c b/src/l2cap.c index d9afdee72..4e79f19fc 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -436,6 +436,14 @@ void l2cap_close_connection(connection_t *connection){ } +void l2cap_accept_connection_internal(hci_con_handle_t handle, uint16_t dest_cid){ + printf("l2cap_accept_connection_internal called but not implemented yet 0x%x, 0x%x\n", handle, dest_cid); +} + +void l2cap_decline_connection_internal(hci_con_handle_t handle, uint16_t dest_cid, uint8_t reason){ + printf("l2cap_decline_connection_internal called but not implemented yet 0x%x, 0x%x, %u", handle, dest_cid,reason); +} + // notify client void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { uint8_t event[17]; diff --git a/src/l2cap.h b/src/l2cap.h index 0ca15060d..bf6727ab3 100644 --- a/src/l2cap.h +++ b/src/l2cap.h @@ -107,8 +107,8 @@ l2cap_service_t * l2cap_get_service(uint16_t psm); void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t); void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm); -void l2cap_accept_connection(bd_addr_t address, uint16_t dest_cid); -void l2cap_decline_connection(bd_addr_t address, uint16_t dest_cid, uint8_t reason); +void l2cap_accept_connection_internal(hci_con_handle_t handle, uint16_t dest_cid); +void l2cap_decline_connection_internal(hci_con_handle_t handle, uint16_t dest_cid, uint8_t reason); void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); void l2cap_emit_channel_closed(l2cap_channel_t *channel);