From be2053a63652b4e6cdbbb8c1a9c9003c96e8326a Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 13 Nov 2015 21:32:29 +0100 Subject: [PATCH] use l2cap_register instead of l2cap_register_internal, move service registered event generation to daemon --- platform/daemon/src/daemon.c | 14 +++++++++++++- src/classic/bnep.c | 2 +- src/classic/rfcomm.c | 2 +- src/classic/sdp.c | 2 +- src/l2cap.c | 30 ++++++++---------------------- src/l2cap.h | 13 ++++--------- test/pts/l2cap_test.c | 2 +- 7 files changed, 29 insertions(+), 36 deletions(-) diff --git a/platform/daemon/src/daemon.c b/platform/daemon/src/daemon.c index 4b602c325..4a3fcb69c 100644 --- a/platform/daemon/src/daemon.c +++ b/platform/daemon/src/daemon.c @@ -602,6 +602,17 @@ static void send_l2cap_connection_open_failed(connection_t * connection, bd_addr socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); } +static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ + log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); + uint8_t event[5]; + event[0] = L2CAP_EVENT_SERVICE_REGISTERED; + event[1] = sizeof(event) - 2; + event[2] = status; + bt_store_16(event, 3, psm); + hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); + socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); +} + static void send_rfcomm_create_channel_failed(void * connection, bd_addr_t addr, uint8_t server_channel, uint8_t status){ // emit error - see rfcom.c:rfcomm_emit_channel_open_failed_outgoing_memory(..) uint8_t event[16]; @@ -823,7 +834,8 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui case L2CAP_REGISTER_SERVICE: psm = READ_BT_16(packet, 3); mtu = READ_BT_16(packet, 5); - l2cap_register_service_internal(connection, NULL, psm, mtu, LEVEL_0); + status = l2cap_register_service(NULL, psm, mtu, LEVEL_0); + l2cap_emit_service_registered(connection, status, psm); break; case L2CAP_UNREGISTER_SERVICE: psm = READ_BT_16(packet, 3); diff --git a/src/classic/bnep.c b/src/classic/bnep.c index 57f6b2c3d..fd001e3c7 100644 --- a/src/classic/bnep.c +++ b/src/classic/bnep.c @@ -1616,7 +1616,7 @@ void bnep_register_service(void * connection, uint16_t service_uuid, uint16_t ma memset(service, 0, sizeof(bnep_service_t)); /* register with l2cap if not registered before, max MTU */ - l2cap_register_service_internal(NULL, bnep_packet_handler, PSM_BNEP, 0xffff, bnep_security_level); + l2cap_register_service(bnep_packet_handler, PSM_BNEP, 0xffff, bnep_security_level); /* Setup the service struct */ service->connection = connection; diff --git a/src/classic/rfcomm.c b/src/classic/rfcomm.c index ef1f553e7..ae0ba16c4 100644 --- a/src/classic/rfcomm.c +++ b/src/classic/rfcomm.c @@ -2258,7 +2258,7 @@ static void rfcomm_register_service2(void * connection, uint8_t channel, uint16_ // register with l2cap if not registered before, max MTU if (linked_list_empty(&rfcomm_services)){ - l2cap_register_service_internal(NULL, rfcomm_packet_handler, PSM_RFCOMM, 0xffff, rfcomm_security_level); + l2cap_register_service(rfcomm_packet_handler, PSM_RFCOMM, 0xffff, rfcomm_security_level); } // fill in diff --git a/src/classic/sdp.c b/src/classic/sdp.c index 6d8c1cf43..23cc52e46 100644 --- a/src/classic/sdp.c +++ b/src/classic/sdp.c @@ -76,7 +76,7 @@ static uint16_t sdp_response_size = 0; void sdp_init(void){ // register with l2cap psm sevices - max MTU - l2cap_register_service_internal(NULL, sdp_packet_handler, PSM_SDP, 0xffff, LEVEL_0); + l2cap_register_service(sdp_packet_handler, PSM_SDP, 0xffff, LEVEL_0); } // register packet handler diff --git a/src/l2cap.c b/src/l2cap.c index d58790f60..7972d1e64 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -189,17 +189,6 @@ static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uin (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); } -static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ - log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); - uint8_t event[5]; - event[0] = L2CAP_EVENT_SERVICE_REGISTERED; - event[1] = sizeof(event) - 2; - event[2] = status; - bt_store_16(event, 3, psm); - hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); - (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); -} - static void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); @@ -1535,32 +1524,30 @@ static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ return l2cap_get_service_internal(&l2cap_services, psm); } -void l2cap_register_service_internal(void *connection, btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ + +uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ - log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); + log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); // check for alread registered psm // TODO: emit error event l2cap_service_t *service = l2cap_get_service(psm); if (service) { - log_error("l2cap_register_service_internal: PSM %u already registered", psm); - l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); - return; + log_error("l2cap_register_service: PSM %u already registered", psm); + return L2CAP_SERVICE_ALREADY_REGISTERED; } // alloc structure // TODO: emit error event service = btstack_memory_l2cap_service_get(); if (!service) { - log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); - l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); - return; + log_error("l2cap_register_service: no memory for l2cap_service_t"); + return BTSTACK_MEMORY_ALLOC_FAILED; } // fill in service->psm = psm; service->mtu = mtu; - service->connection = connection; service->packet_handler = service_packet_handler; service->required_security_level = security_level; @@ -1570,8 +1557,7 @@ void l2cap_register_service_internal(void *connection, btstack_packet_handler_t // enable page scan hci_connectable_control(1); - // done - l2cap_emit_service_registered(connection, 0, psm); + return 0; } void l2cap_unregister_service_internal(void *connection, uint16_t psm){ diff --git a/src/l2cap.h b/src/l2cap.h index 0a7137b84..ce3399c76 100644 --- a/src/l2cap.h +++ b/src/l2cap.h @@ -184,7 +184,7 @@ typedef struct { uint16_t mps; // client connection - void *connection; + void * connection; // internal connection btstack_packet_handler_t packet_handler; @@ -252,9 +252,9 @@ uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); /** - * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter. + * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. */ -void l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); +uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); /** * @brief Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. @@ -297,15 +297,10 @@ int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uin // deprecated... -/** - * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter. - */ -void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); - /** * @brief Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. */ -void l2cap_unregister_service_internal(void *connection, uint16_t psm); +void l2cap_unregister_service_internal(void * connection, uint16_t psm); #if 0 diff --git a/test/pts/l2cap_test.c b/test/pts/l2cap_test.c index 9d18f63f3..accc2f0aa 100644 --- a/test/pts/l2cap_test.c +++ b/test/pts/l2cap_test.c @@ -162,7 +162,7 @@ int btstack_main(int argc, const char * argv[]){ l2cap_init(); l2cap_register_packet_handler(&packet_handler2); - l2cap_register_service_internal(NULL, packet_handler, PSM_SDP, 100, LEVEL_0); + l2cap_register_service(packet_handler, PSM_SDP, 100, LEVEL_0); // turn on! hci_power_control(HCI_POWER_ON);