add l2cap_create_channel that return local cid instead of setting connection_t field

This commit is contained in:
Matthias Ringwald 2015-11-13 16:47:02 +01:00
parent 117f0209cd
commit 9077cb155a
2 changed files with 93 additions and 4 deletions

View File

@ -747,7 +747,6 @@ static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t *
log_info("l2cap_handle_connection_complete expected state");
// success, start l2cap handshake
channel->handle = handle;
channel->local_cid = l2cap_next_local_cid();
// check remote SSP feature first
channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
}
@ -768,6 +767,71 @@ static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * ch
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
}
/**
* @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
* @param packet_handler
* @param address
* @param psm
* @param mtu
* @param local_cid
*/
uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){
log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
// alloc structure
l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
if (!chan) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}
// Init memory (make valgrind happy)
memset(chan, 0, sizeof(l2cap_channel_t));
// limit local mtu to max acl packet length - l2cap header
if (mtu > l2cap_max_mtu()) {
mtu = l2cap_max_mtu();
}
// fill in
BD_ADDR_COPY(chan->address, address);
chan->psm = psm;
chan->handle = 0;
chan->packet_handler = channel_packet_handler;
chan->remote_mtu = L2CAP_MINIMAL_MTU;
chan->local_mtu = mtu;
chan->packets_granted = 0;
chan->local_cid = l2cap_next_local_cid();
// set initial state
chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
chan->local_sig_id = L2CAP_SIG_ID_INVALID;
chan->required_security_level = LEVEL_0;
// add to connections list
linked_list_add(&l2cap_channels, (linked_item_t *) chan);
// store local_cid
if (out_local_cid){
*out_local_cid = chan->local_cid;
}
// check if hci connection is already usable
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
if (conn){
log_info("l2cap_create_channel_internal, hci connection already exists");
l2cap_handle_connection_complete(conn->con_handle, chan);
// check if remote supported fearures are already received
if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
l2cap_handle_remote_supported_features_received(chan);
}
}
l2cap_run();
return 0;
}
// open outgoing L2CAP channel
void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t channel_packet_handler,
bd_addr_t address, uint16_t psm, uint16_t mtu){
@ -784,6 +848,7 @@ void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t c
l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
return;
}
// Init memory (make valgrind happy)
memset(chan, 0, sizeof(l2cap_channel_t));
// limit local mtu to max acl packet length - l2cap header
@ -800,6 +865,7 @@ void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t c
chan->remote_mtu = L2CAP_MINIMAL_MTU;
chan->local_mtu = mtu;
chan->packets_granted = 0;
chan->local_cid = l2cap_next_local_cid();
// set initial state
chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;

View File

@ -227,8 +227,14 @@ void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t pa
/**
* @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
* @param packet_handler
* @param address
* @param psm
* @param mtu
* @param local_cid
* @param status
*/
void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu);
uint8_t l2cap_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid);
/**
* @brief Disconnects L2CAP channel with given identifier.
@ -248,12 +254,12 @@ 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.
*/
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);
void 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.
*/
void l2cap_unregister_service_internal(void *connection, uint16_t psm);
void l2cap_unregister_service(uint16_t psm);
/**
* @brief Accepts/Deny incoming L2CAP connection.
@ -289,6 +295,23 @@ int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uin
/* API_END */
// deprecated...
/**
* @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
*/
void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu);
/**
* @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);
#if 0
// to be implemented soon