use l2cap_register instead of l2cap_register_internal, move service registered event generation to daemon

This commit is contained in:
Matthias Ringwald 2015-11-13 21:32:29 +01:00
parent 6551847ef9
commit be2053a636
7 changed files with 29 additions and 36 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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){

View File

@ -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

View File

@ -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);