use hci_con_handle_t more

This commit is contained in:
Matthias Ringwald 2016-02-19 13:57:49 +01:00
parent 711e6c8033
commit fc64f94a83
21 changed files with 212 additions and 210 deletions

View File

@ -61,7 +61,7 @@ uint16_t hid_interrupt = 0;
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t event_addr; bd_addr_t event_addr;
uint16_t handle; hci_con_handle_t con_handle;
uint16_t psm; uint16_t psm;
uint16_t local_cid; uint16_t local_cid;
uint16_t remote_cid; uint16_t remote_cid;
@ -112,12 +112,12 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint
// data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) // data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16)
reverse_bd_addr(&packet[2], reverse_bd_addr(&packet[2],
event_addr); event_addr);
handle = little_endian_read_16(packet, 8); con_handle = little_endian_read_16(packet, 8);
psm = little_endian_read_16(packet, 10); psm = little_endian_read_16(packet, 10);
local_cid = little_endian_read_16(packet, 12); local_cid = little_endian_read_16(packet, 12);
remote_cid = little_endian_read_16(packet, 14); remote_cid = little_endian_read_16(packet, 14);
printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
bd_addr_to_str(event_addr), handle, psm, local_cid, remote_cid); bd_addr_to_str(event_addr), con_handle, psm, local_cid, remote_cid);
// accept // accept
bt_send_cmd(&l2cap_accept_connection_cmd, local_cid); bt_send_cmd(&l2cap_accept_connection_cmd, local_cid);
@ -150,10 +150,10 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint
event_addr); event_addr);
psm = little_endian_read_16(packet, 11); psm = little_endian_read_16(packet, 11);
local_cid = little_endian_read_16(packet, 13); local_cid = little_endian_read_16(packet, 13);
handle = little_endian_read_16(packet, 9); con_handle = little_endian_read_16(packet, 9);
if (packet[2] == 0) { if (packet[2] == 0) {
printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
bd_addr_to_str(event_addr), handle, psm, local_cid, little_endian_read_16(packet, 15)); bd_addr_to_str(event_addr), con_handle, psm, local_cid, little_endian_read_16(packet, 15));
if (psm == PSM_HID_CONTROL){ if (psm == PSM_HID_CONTROL){
hid_control = local_cid; hid_control = local_cid;

View File

@ -80,7 +80,7 @@ void timer_handler(struct btstack_timer_source *ts){
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t event_addr; bd_addr_t event_addr;
uint16_t handle; hci_con_handle_t con_handle;
uint16_t psm; uint16_t psm;
uint16_t local_cid; uint16_t local_cid;
char pin[20]; char pin[20];
@ -126,11 +126,11 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint
// data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) // data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16)
reverse_bd_addr(&packet[2], reverse_bd_addr(&packet[2],
event_addr); event_addr);
handle = little_endian_read_16(packet, 8); con_handle = little_endian_read_16(packet, 8);
psm = little_endian_read_16(packet, 10); psm = little_endian_read_16(packet, 10);
local_cid = little_endian_read_16(packet, 12); local_cid = little_endian_read_16(packet, 12);
// remote_cid = little_endian_read_16(packet, 14); // remote_cid = little_endian_read_16(packet, 14);
printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x\n", bd_addr_to_str(event_addr), handle, psm, local_cid); printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x\n", bd_addr_to_str(event_addr), con_handle, psm, local_cid);
// accept // accept
bt_send_cmd(&l2cap_accept_connection_cmd, local_cid); bt_send_cmd(&l2cap_accept_connection_cmd, local_cid);
break; break;
@ -163,10 +163,10 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint
event_addr); event_addr);
psm = little_endian_read_16(packet, 11); psm = little_endian_read_16(packet, 11);
local_cid = little_endian_read_16(packet, 13); local_cid = little_endian_read_16(packet, 13);
handle = little_endian_read_16(packet, 9); con_handle = little_endian_read_16(packet, 9);
if (packet[2] == 0) { if (packet[2] == 0) {
printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
bd_addr_to_str(event_addr), handle, psm, local_cid, little_endian_read_16(packet, 15)); bd_addr_to_str(event_addr), con_handle, psm, local_cid, little_endian_read_16(packet, 15));
} else { } else {
printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]); printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]);
} }

View File

@ -774,10 +774,10 @@ static void sdp_emit_service_registered(void *connection, uint32_t handle, uint8
#ifdef ENABLE_BLE #ifdef ENABLE_BLE
btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(uint16_t handle) { btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(hci_con_handle_t con_handle) {
btstack_linked_list_iterator_t it; btstack_linked_list_iterator_t it;
if (!gatt_client_helpers) return NULL; if (!gatt_client_helpers) return NULL;
log_info("daemon_get_gatt_client_helper for handle 0x%02x", handle); log_info("daemon_get_gatt_client_helper for handle 0x%02x", con_handle);
btstack_linked_list_iterator_init(&it, &gatt_client_helpers); btstack_linked_list_iterator_init(&it, &gatt_client_helpers);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
@ -786,31 +786,31 @@ btstack_linked_list_gatt_client_helper_t * daemon_get_gatt_client_helper(uint16_
log_info("daemon_get_gatt_client_helper gatt_client_helpers null item"); log_info("daemon_get_gatt_client_helper gatt_client_helpers null item");
break; break;
} }
if (item->con_handle == handle){ if (item->con_handle == con_handle){
return item; return item;
} }
} }
log_info("daemon_get_gatt_client_helper for handle 0x%02x is NULL.", handle); log_info("daemon_get_gatt_client_helper for handle 0x%02x is NULL.", con_handle);
return NULL; return NULL;
} }
static void send_gatt_query_complete(connection_t * connection, uint16_t handle, uint8_t status){ static void send_gatt_query_complete(connection_t * connection, hci_con_handle_t con_handle, uint8_t status){
// @format H1 // @format H1
uint8_t event[5]; uint8_t event[5];
event[0] = GATT_EVENT_QUERY_COMPLETE; event[0] = GATT_EVENT_QUERY_COMPLETE;
event[1] = 3; event[1] = 3;
little_endian_store_16(event, 2, handle); little_endian_store_16(event, 2, con_handle);
event[4] = status; event[4] = status;
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); socket_connection_send_packet(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
} }
static void send_gatt_mtu_event(connection_t * connection, uint16_t handle, uint16_t mtu){ static void send_gatt_mtu_event(connection_t * connection, hci_con_handle_t con_handle, uint16_t mtu){
uint8_t event[6]; uint8_t event[6];
int pos = 0; int pos = 0;
event[pos++] = GATT_EVENT_MTU; event[pos++] = GATT_EVENT_MTU;
event[pos++] = sizeof(event) - 2; event[pos++] = sizeof(event) - 2;
little_endian_store_16(event, pos, handle); little_endian_store_16(event, pos, con_handle);
pos += 2; pos += 2;
little_endian_store_16(event, pos, mtu); little_endian_store_16(event, pos, mtu);
pos += 2; pos += 2;
@ -819,31 +819,31 @@ static void send_gatt_mtu_event(connection_t * connection, uint16_t handle, uint
} }
btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(connection_t *connection, uint8_t *packet, int track_active_connection) { btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(connection_t *connection, uint8_t *packet, int track_active_connection) {
hci_con_handle_t handle = little_endian_read_16(packet, 3); hci_con_handle_t con_handle = little_endian_read_16(packet, 3);
log_info("daemon_setup_gatt_client_request for handle 0x%02x", handle); log_info("daemon_setup_gatt_client_request for handle 0x%02x", con_handle);
hci_connection_t * hci_con = hci_connection_for_handle(handle); hci_connection_t * hci_con = hci_connection_for_handle(con_handle);
if ((hci_con == NULL) || (hci_con->state != OPEN)){ if ((hci_con == NULL) || (hci_con->state != OPEN)){
send_gatt_query_complete(connection, handle, GATT_CLIENT_NOT_CONNECTED); send_gatt_query_complete(connection, con_handle, GATT_CLIENT_NOT_CONNECTED);
return NULL; return NULL;
} }
btstack_linked_list_gatt_client_helper_t * helper = daemon_get_gatt_client_helper(handle); btstack_linked_list_gatt_client_helper_t * helper = daemon_get_gatt_client_helper(con_handle);
if (!helper){ if (!helper){
log_info("helper does not exist"); log_info("helper does not exist");
helper = malloc(sizeof(btstack_linked_list_gatt_client_helper_t)); helper = malloc(sizeof(btstack_linked_list_gatt_client_helper_t));
if (!helper) return NULL; if (!helper) return NULL;
memset(helper, 0, sizeof(btstack_linked_list_gatt_client_helper_t)); memset(helper, 0, sizeof(btstack_linked_list_gatt_client_helper_t));
helper->con_handle = handle; helper->con_handle = con_handle;
btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) helper); btstack_linked_list_add(&gatt_client_helpers, (btstack_linked_item_t *) helper);
} }
if (track_active_connection && helper->active_connection){ if (track_active_connection && helper->active_connection){
send_gatt_query_complete(connection, handle, GATT_CLIENT_BUSY); send_gatt_query_complete(connection, con_handle, GATT_CLIENT_BUSY);
return NULL; return NULL;
} }
daemon_add_gatt_client_handle(connection, handle); daemon_add_gatt_client_handle(connection, con_handle);
if (track_active_connection){ if (track_active_connection){
// remember connection responsible for this request // remember connection responsible for this request
@ -1806,8 +1806,8 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
// we receive a HCI event packet in disguise // we receive a HCI event packet in disguise
if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){ if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){
log_info("daemon hack: handle disconnection_complete in handle_gatt_client_event instead of main hci event packet handler"); log_info("daemon hack: handle disconnection_complete in handle_gatt_client_event instead of main hci event packet handler");
uint16_t handle = little_endian_read_16(packet, 3); hci_con_handle_t con_handle = little_endian_read_16(packet, 3);
daemon_remove_gatt_client_helper(handle); daemon_remove_gatt_client_helper(con_handle);
return; return;
} }

View File

@ -129,7 +129,7 @@ extern "C" void hal_cpu_enable_irqs_and_sleep(void) { }
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t addr; bd_addr_t addr;
uint16_t handle; hci_con_handle_t con_handle;
switch (packet_type) { switch (packet_type) {
@ -148,8 +148,8 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
case HCI_EVENT_DISCONNECTION_COMPLETE: case HCI_EVENT_DISCONNECTION_COMPLETE:
if (bleDeviceDisconnectedCallback) { if (bleDeviceDisconnectedCallback) {
handle = little_endian_read_16(packet, 3); con_handle = little_endian_read_16(packet, 3);
BLEDevice device(handle); BLEDevice device(con_handle);
(*bleDeviceDisconnectedCallback)(&device); (*bleDeviceDisconnectedCallback)(&device);
} }
le_peripheral_todos |= SET_ADVERTISEMENT_ENABLED; le_peripheral_todos |= SET_ADVERTISEMENT_ENABLED;
@ -174,14 +174,14 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
case HCI_EVENT_LE_META: case HCI_EVENT_LE_META:
switch (packet[2]) { switch (packet[2]) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
handle = little_endian_read_16(packet, 4); con_handle = little_endian_read_16(packet, 4);
printf("Connection complete, handle 0x%04x\n", handle); printf("Connection complete, con_handle 0x%04x\n", con_handle);
btstack_run_loop_remove_timer(&connection_timer); btstack_run_loop_remove_timer(&connection_timer);
if (!bleDeviceConnectedCallback) break; if (!bleDeviceConnectedCallback) break;
if (packet[3]){ if (packet[3]){
(*bleDeviceConnectedCallback)(BLE_STATUS_CONNECTION_ERROR, NULL); (*bleDeviceConnectedCallback)(BLE_STATUS_CONNECTION_ERROR, NULL);
} else { } else {
BLEDevice device(handle); BLEDevice device(con_handle);
(*bleDeviceConnectedCallback)(BLE_STATUS_OK, &device); (*bleDeviceConnectedCallback)(BLE_STATUS_OK, &device);
} }
break; break;
@ -222,7 +222,7 @@ static void gatt_client_callback(uint8_t packet_type, uint8_t * packet, uint16_t
if (packet[0] < GATT_EVENT_QUERY_COMPLETE) return; if (packet[0] < GATT_EVENT_QUERY_COMPLETE) return;
if (packet[0] > GATT_EVENT_MTU) return; if (packet[0] > GATT_EVENT_MTU) return;
uint16_t con_handle = little_endian_read_16(packet, 2); hci_con_handle_t con_handle = little_endian_read_16(packet, 2);
uint8_t status; uint8_t status;
uint8_t * value; uint8_t * value;
uint16_t value_handle; uint16_t value_handle;
@ -526,7 +526,7 @@ const gatt_client_service_t * BLEService::getService(void){
// discovery of services and characteristics // discovery of services and characteristics
BLEDevice::BLEDevice(void){ BLEDevice::BLEDevice(void){
} }
BLEDevice::BLEDevice(uint16_t handle) BLEDevice::BLEDevice(hci_con_handle_t handle)
: handle(handle){ : handle(handle){
} }
uint16_t BLEDevice::getHandle(void){ uint16_t BLEDevice::getHandle(void){

View File

@ -3,7 +3,7 @@ BTSTACK_ROOT=../../..
CC = @CC@ CC = @CC@
LDFLAGS = @LDFLAGS@ -lBTstack -L../src LDFLAGS = @LDFLAGS@ -lBTstack -L../src
CFLAGS = @CFLAGS@ \ CFLAGS = @CFLAGS@ \
-I$(BTSTACK_ROOT)/platform/daemon \ -I$(BTSTACK_ROOT)/platform/daemon/src \
-I$(BTSTACK_ROOT)/platform/posix \ -I$(BTSTACK_ROOT)/platform/posix \
-I$(BTSTACK_ROOT)/src \ -I$(BTSTACK_ROOT)/src \
-I.. -I..

View File

@ -152,9 +152,9 @@ static int att_find_handle(att_iterator_t *it, uint16_t handle){
} }
// experimental client API // experimental client API
uint16_t att_uuid_for_handle(uint16_t handle){ uint16_t att_uuid_for_handle(uint16_t attribute_handle){
att_iterator_t it; att_iterator_t it;
int ok = att_find_handle(&it, handle); int ok = att_find_handle(&it, attribute_handle);
if (!ok) return 0; if (!ok) return 0;
if (it.flags & ATT_PROPERTY_UUID128) return 0; if (it.flags & ATT_PROPERTY_UUID128) return 0;
return little_endian_read_16(it.uuid, 0); return little_endian_read_16(it.uuid, 0);

View File

@ -122,11 +122,13 @@ uint16_t att_handle_request(att_connection_t * att_connection,
/* /*
* @brief setup value notification in response buffer for a given handle and value * @brief setup value notification in response buffer for a given handle and value
* @param att_connection * @param att_connection
* @param value, value_len: new attribute value * @param attribute_handle
* @param value
* @param value_len
* @param response_buffer for notification * @param response_buffer for notification
*/ */
uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection, uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
uint16_t handle, uint16_t attribute_handle,
uint8_t *value, uint8_t *value,
uint16_t value_len, uint16_t value_len,
uint8_t * response_buffer); uint8_t * response_buffer);
@ -134,11 +136,13 @@ uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection
/* /*
* @brief setup value indication in response buffer for a given handle and value * @brief setup value indication in response buffer for a given handle and value
* @param att_connection * @param att_connection
* @param value, value_len: new attribute value * @param attribute_handle
* @param value
* @param value_len
* @param response_buffer for indication * @param response_buffer for indication
*/ */
uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection, uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
uint16_t handle, uint16_t attribute_handle,
uint8_t *value, uint8_t *value,
uint16_t value_len, uint16_t value_len,
uint8_t * response_buffer); uint8_t * response_buffer);
@ -149,7 +153,7 @@ uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
void att_clear_transaction_queue(att_connection_t * att_connection); void att_clear_transaction_queue(att_connection_t * att_connection);
// experimental client API // experimental client API
uint16_t att_uuid_for_handle(uint16_t handle); uint16_t att_uuid_for_handle(uint16_t attribute_handle);
#if defined __cplusplus #if defined __cplusplus
} }

View File

@ -104,8 +104,8 @@ void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
* @brief can send packet for client * @brief can send packet for client
* @param handle * @param handle
*/ */
int att_dispatch_client_can_send_now(uint16_t handle){ int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){
int res = l2cap_can_send_fixed_channel_packet_now(handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); int res = l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
if (!res){ if (!res){
att_client_waiting_for_can_send =1; att_client_waiting_for_can_send =1;
} }
@ -116,8 +116,8 @@ int att_dispatch_client_can_send_now(uint16_t handle){
* @brief can send packet for server * @brief can send packet for server
* @param handle * @param handle
*/ */
int att_dispatch_server_can_send_now(uint16_t handle){ int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){
int res = l2cap_can_send_fixed_channel_packet_now(handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); int res = l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
if (!res){ if (!res){
att_server_waiting_for_can_send =1; att_server_waiting_for_can_send =1;
} }

View File

@ -63,15 +63,15 @@ void att_dispatch_register_server(btstack_packet_handler_t packet_handler);
/** /**
* @brief can send packet for client * @brief can send packet for client
* @param handle * @param con_handle
*/ */
int att_dispatch_client_can_send_now(uint16_t handle); int att_dispatch_client_can_send_now(hci_con_handle_t con_handle);
/** /**
* @brief can send packet for server * @brief can send packet for server
* @param handle * @param con_handle
*/ */
int att_dispatch_server_can_send_now(uint16_t handle); int att_dispatch_server_can_send_now(hci_con_handle_t con_handle);
#if defined __cplusplus #if defined __cplusplus

View File

@ -111,7 +111,7 @@ static void att_handle_value_indication_notify_client(uint8_t status, uint16_t c
(*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
} }
static void att_emit_mtu_event(uint16_t handle, uint16_t mtu){ static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
if (!att_client_packet_handler) return; if (!att_client_packet_handler) return;
@ -119,7 +119,7 @@ static void att_emit_mtu_event(uint16_t handle, uint16_t mtu){
int pos = 0; int pos = 0;
event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
event[pos++] = sizeof(event) - 2; event[pos++] = sizeof(event) - 2;
little_endian_store_16(event, pos, handle); little_endian_store_16(event, pos, con_handle);
pos += 2; pos += 2;
little_endian_store_16(event, pos, mtu); little_endian_store_16(event, pos, mtu);
pos += 2; pos += 2;
@ -383,28 +383,28 @@ int att_server_can_send_packet_now(void){
return att_dispatch_server_can_send_now(att_connection.con_handle); return att_dispatch_server_can_send_now(att_connection.con_handle);
} }
int att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len){ int att_server_notify(uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
l2cap_reserve_packet_buffer(); l2cap_reserve_packet_buffer();
uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
uint16_t size = att_prepare_handle_value_notification(&att_connection, handle, value, value_len, packet_buffer); uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer);
return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
} }
int att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len){ int att_server_indicate(uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
// track indication // track indication
att_handle_value_indication_handle = handle; att_handle_value_indication_handle = attribute_handle;
btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout);
btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
btstack_run_loop_add_timer(&att_handle_value_indication_timer); btstack_run_loop_add_timer(&att_handle_value_indication_timer);
l2cap_reserve_packet_buffer(); l2cap_reserve_packet_buffer();
uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
uint16_t size = att_prepare_handle_value_indication(&att_connection, handle, value, value_len, packet_buffer); uint16_t size = att_prepare_handle_value_indication(&att_connection, attribute_handle, value, value_len, packet_buffer);
l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
return 0; return 0;
} }

View File

@ -69,15 +69,21 @@ int att_server_can_send_packet_now(void);
/* /*
* @brief notify client about attribute value change * @brief notify client about attribute value change
* @param attribute_handle
* @param value
* @param value_len
* @return 0 if ok, error otherwise * @return 0 if ok, error otherwise
*/ */
int att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len); int att_server_notify(uint16_t attribute_handle, uint8_t *value, uint16_t value_len);
/* /*
* @brief indicate value change to client. client is supposed to reply with an indication_response * @brief indicate value change to client. client is supposed to reply with an indication_response
* @param attribute_handle
* @param value
* @param value_len
* @return 0 if ok, error otherwise * @return 0 if ok, error otherwise
*/ */
int att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len); int att_server_indicate(uint16_t attribute_handle, uint8_t *value, uint16_t value_len);
/* API_END */ /* API_END */

View File

@ -104,12 +104,12 @@ static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
static void gatt_client_timeout_handler(btstack_timer_source_t * timer){ static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
gatt_client_t * peripheral = gatt_client_for_timer(timer); gatt_client_t * peripheral = gatt_client_for_timer(timer);
if (!peripheral) return; if (!peripheral) return;
log_info("GATT client timeout handle, handle 0x%02x", peripheral->handle); log_info("GATT client timeout handle, handle 0x%02x", peripheral->con_handle);
gatt_client_report_error_if_pending(peripheral, ATT_ERROR_TIMEOUT); gatt_client_report_error_if_pending(peripheral, ATT_ERROR_TIMEOUT);
} }
static void gatt_client_timeout_start(gatt_client_t * peripheral){ static void gatt_client_timeout_start(gatt_client_t * peripheral){
log_info("GATT client timeout start, handle 0x%02x", peripheral->handle); log_info("GATT client timeout start, handle 0x%02x", peripheral->con_handle);
btstack_run_loop_remove_timer(&peripheral->gc_timeout); btstack_run_loop_remove_timer(&peripheral->gc_timeout);
btstack_run_loop_set_timer_handler(&peripheral->gc_timeout, gatt_client_timeout_handler); btstack_run_loop_set_timer_handler(&peripheral->gc_timeout, gatt_client_timeout_handler);
btstack_run_loop_set_timer(&peripheral->gc_timeout, 30000); // 30 seconds sm timeout btstack_run_loop_set_timer(&peripheral->gc_timeout, 30000); // 30 seconds sm timeout
@ -117,7 +117,7 @@ static void gatt_client_timeout_start(gatt_client_t * peripheral){
} }
static void gatt_client_timeout_stop(gatt_client_t * peripheral){ static void gatt_client_timeout_stop(gatt_client_t * peripheral){
log_info("GATT client timeout stop, handle 0x%02x", peripheral->handle); log_info("GATT client timeout stop, handle 0x%02x", peripheral->con_handle);
btstack_run_loop_remove_timer(&peripheral->gc_timeout); btstack_run_loop_remove_timer(&peripheral->gc_timeout);
} }
@ -125,7 +125,7 @@ static gatt_client_t * get_gatt_client_context_for_handle(uint16_t handle){
btstack_linked_item_t *it; btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){ for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){
gatt_client_t * peripheral = (gatt_client_t *) it; gatt_client_t * peripheral = (gatt_client_t *) it;
if (peripheral->handle == handle){ if (peripheral->con_handle == handle){
return peripheral; return peripheral;
} }
} }
@ -143,7 +143,7 @@ static gatt_client_t * provide_context_for_conn_handle(hci_con_handle_t con_hand
if (!context) return NULL; if (!context) return NULL;
// init state // init state
memset(context, 0, sizeof(gatt_client_t)); memset(context, 0, sizeof(gatt_client_t));
context->handle = con_handle; context->con_handle = con_handle;
context->mtu = ATT_DEFAULT_MTU; context->mtu = ATT_DEFAULT_MTU;
context->mtu_state = SEND_MTU_EXCHANGE; context->mtu_state = SEND_MTU_EXCHANGE;
context->gatt_client_state = P_READY; context->gatt_client_state = P_READY;
@ -167,14 +167,14 @@ static int is_ready(gatt_client_t * context){
return context->gatt_client_state == P_READY; return context->gatt_client_state == P_READY;
} }
int gatt_client_is_ready(uint16_t handle){ int gatt_client_is_ready(hci_con_handle_t con_handle){
gatt_client_t * context = provide_context_for_conn_handle(handle); gatt_client_t * context = provide_context_for_conn_handle(con_handle);
if (!context) return 0; if (!context) return 0;
return is_ready(context); return is_ready(context);
} }
uint8_t gatt_client_get_mtu(uint16_t handle, uint16_t * mtu){ uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
gatt_client_t * context = provide_context_for_conn_handle(handle); gatt_client_t * context = provide_context_for_conn_handle(con_handle);
if (context && context->mtu_state == MTU_EXCHANGED){ if (context && context->mtu_state == MTU_EXCHANGED){
*mtu = context->mtu; *mtu = context->mtu;
return 0; return 0;
@ -340,19 +340,19 @@ static uint16_t write_blob_length(gatt_client_t * peripheral){
} }
static void send_gatt_services_request(gatt_client_t *peripheral){ static void send_gatt_services_request(gatt_client_t *peripheral){
att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_GROUP_TYPE_REQUEST, GATT_PRIMARY_SERVICE_UUID, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_GROUP_TYPE_REQUEST, GATT_PRIMARY_SERVICE_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
static void send_gatt_by_uuid_request(gatt_client_t *peripheral, uint16_t attribute_group_type){ static void send_gatt_by_uuid_request(gatt_client_t *peripheral, uint16_t attribute_group_type){
if (peripheral->uuid16){ if (peripheral->uuid16){
uint8_t uuid16[2]; uint8_t uuid16[2];
little_endian_store_16(uuid16, 0, peripheral->uuid16); little_endian_store_16(uuid16, 0, peripheral->uuid16);
att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid16, 2); att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid16, 2);
return; return;
} }
uint8_t uuid128[16]; uint8_t uuid128[16];
reverse_128(peripheral->uuid128, uuid128); reverse_128(peripheral->uuid128, uuid128);
att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid128, 16); att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid128, 16);
} }
static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){ static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){
@ -360,71 +360,71 @@ static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){
} }
static void send_gatt_included_service_uuid_request(gatt_client_t *peripheral){ static void send_gatt_included_service_uuid_request(gatt_client_t *peripheral){
att_read_request(ATT_READ_REQUEST, peripheral->handle, peripheral->query_start_handle); att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->query_start_handle);
} }
static void send_gatt_included_service_request(gatt_client_t *peripheral){ static void send_gatt_included_service_request(gatt_client_t *peripheral){
att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_INCLUDE_SERVICE_UUID, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_INCLUDE_SERVICE_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
static void send_gatt_characteristic_request(gatt_client_t *peripheral){ static void send_gatt_characteristic_request(gatt_client_t *peripheral){
att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CHARACTERISTICS_UUID, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CHARACTERISTICS_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
static void send_gatt_characteristic_descriptor_request(gatt_client_t *peripheral){ static void send_gatt_characteristic_descriptor_request(gatt_client_t *peripheral){
att_find_information_request(ATT_FIND_INFORMATION_REQUEST, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_find_information_request(ATT_FIND_INFORMATION_REQUEST, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
static void send_gatt_read_characteristic_value_request(gatt_client_t *peripheral){ static void send_gatt_read_characteristic_value_request(gatt_client_t *peripheral){
att_read_request(ATT_READ_REQUEST, peripheral->handle, peripheral->attribute_handle); att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle);
} }
static void send_gatt_read_by_type_request(gatt_client_t * peripheral){ static void send_gatt_read_by_type_request(gatt_client_t * peripheral){
if (peripheral->uuid16){ if (peripheral->uuid16){
att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid16, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid16, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} else { } else {
att_read_by_type_or_group_request_for_uuid128(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid128, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid128(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid128, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
} }
static void send_gatt_read_blob_request(gatt_client_t *peripheral){ static void send_gatt_read_blob_request(gatt_client_t *peripheral){
att_read_blob_request(ATT_READ_BLOB_REQUEST, peripheral->handle, peripheral->attribute_handle, peripheral->attribute_offset); att_read_blob_request(ATT_READ_BLOB_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset);
} }
static void send_gatt_read_multiple_request(gatt_client_t * peripheral){ static void send_gatt_read_multiple_request(gatt_client_t * peripheral){
att_read_multiple_request(peripheral->handle, peripheral->read_multiple_handle_count, peripheral->read_multiple_handles); att_read_multiple_request(peripheral->con_handle, peripheral->read_multiple_handle_count, peripheral->read_multiple_handles);
} }
static void send_gatt_write_attribute_value_request(gatt_client_t * peripheral){ static void send_gatt_write_attribute_value_request(gatt_client_t * peripheral){
att_write_request(ATT_WRITE_REQUEST, peripheral->handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value); att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value);
} }
static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * peripheral){ static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * peripheral){
att_write_request(ATT_WRITE_REQUEST, peripheral->handle, peripheral->client_characteristic_configuration_handle, 2, peripheral->client_characteristic_configuration_value); att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->client_characteristic_configuration_handle, 2, peripheral->client_characteristic_configuration_value);
} }
static void send_gatt_prepare_write_request(gatt_client_t * peripheral){ static void send_gatt_prepare_write_request(gatt_client_t * peripheral){
att_prepare_write_request(ATT_PREPARE_WRITE_REQUEST, peripheral->handle, peripheral->attribute_handle, peripheral->attribute_offset, write_blob_length(peripheral), peripheral->attribute_value); att_prepare_write_request(ATT_PREPARE_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset, write_blob_length(peripheral), peripheral->attribute_value);
} }
static void send_gatt_execute_write_request(gatt_client_t * peripheral){ static void send_gatt_execute_write_request(gatt_client_t * peripheral){
att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->handle, 1); att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 1);
} }
static void send_gatt_cancel_prepared_write_request(gatt_client_t * peripheral){ static void send_gatt_cancel_prepared_write_request(gatt_client_t * peripheral){
att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->handle, 0); att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 0);
} }
static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * peripheral){ static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * peripheral){
att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, peripheral->handle, peripheral->start_group_handle, peripheral->end_group_handle); att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
} }
static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * peripheral){ static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * peripheral){
att_read_request(ATT_READ_REQUEST, peripheral->handle, peripheral->attribute_handle); att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle);
} }
static void send_gatt_signed_write_request(gatt_client_t * peripheral, uint32_t sign_counter){ static void send_gatt_signed_write_request(gatt_client_t * peripheral, uint32_t sign_counter){
att_signed_write_request(ATT_SIGNED_WRITE_COMMAND, peripheral->handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value, sign_counter, peripheral->cmac); att_signed_write_request(ATT_SIGNED_WRITE_COMMAND, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value, sign_counter, peripheral->cmac);
} }
static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){ static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
@ -480,7 +480,7 @@ static void emit_gatt_complete_event(gatt_client_t * peripheral, uint8_t status)
uint8_t packet[5]; uint8_t packet[5];
packet[0] = GATT_EVENT_QUERY_COMPLETE; packet[0] = GATT_EVENT_QUERY_COMPLETE;
packet[1] = 3; packet[1] = 3;
little_endian_store_16(packet, 2, peripheral->handle); little_endian_store_16(packet, 2, peripheral->con_handle);
packet[4] = status; packet[4] = status;
emit_event_new(peripheral->callback, packet, sizeof(packet)); emit_event_new(peripheral->callback, packet, sizeof(packet));
} }
@ -490,7 +490,7 @@ static void emit_gatt_service_query_result_event(gatt_client_t * peripheral, uin
uint8_t packet[24]; uint8_t packet[24];
packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT; packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT;
packet[1] = sizeof(packet) - 2; packet[1] = sizeof(packet) - 2;
little_endian_store_16(packet, 2, peripheral->handle); little_endian_store_16(packet, 2, peripheral->con_handle);
/// ///
little_endian_store_16(packet, 4, start_group_handle); little_endian_store_16(packet, 4, start_group_handle);
little_endian_store_16(packet, 6, end_group_handle); little_endian_store_16(packet, 6, end_group_handle);
@ -503,7 +503,7 @@ static void emit_gatt_included_service_query_result_event(gatt_client_t * periph
uint8_t packet[26]; uint8_t packet[26];
packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT; packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT;
packet[1] = sizeof(packet) - 2; packet[1] = sizeof(packet) - 2;
little_endian_store_16(packet, 2, peripheral->handle); little_endian_store_16(packet, 2, peripheral->con_handle);
/// ///
little_endian_store_16(packet, 4, include_handle); little_endian_store_16(packet, 4, include_handle);
// //
@ -519,7 +519,7 @@ static void emit_gatt_characteristic_query_result_event(gatt_client_t * peripher
uint8_t packet[28]; uint8_t packet[28];
packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT; packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT;
packet[1] = sizeof(packet) - 2; packet[1] = sizeof(packet) - 2;
little_endian_store_16(packet, 2, peripheral->handle); little_endian_store_16(packet, 2, peripheral->con_handle);
/// ///
little_endian_store_16(packet, 4, start_handle); little_endian_store_16(packet, 4, start_handle);
little_endian_store_16(packet, 6, value_handle); little_endian_store_16(packet, 6, value_handle);
@ -535,7 +535,7 @@ static void emit_gatt_all_characteristic_descriptors_result_event(
uint8_t packet[22]; uint8_t packet[22];
packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT; packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT;
packet[1] = sizeof(packet) - 2; packet[1] = sizeof(packet) - 2;
little_endian_store_16(packet, 2, peripheral->handle); little_endian_store_16(packet, 2, peripheral->con_handle);
/// ///
little_endian_store_16(packet, 4, descriptor_handle); little_endian_store_16(packet, 4, descriptor_handle);
reverse_128(uuid128, &packet[6]); reverse_128(uuid128, &packet[6]);
@ -562,7 +562,7 @@ static void report_gatt_services(gatt_client_t * peripheral, uint8_t * packet,
} }
emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, uuid128); emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, uuid128);
} }
// log_info("report_gatt_services for %02X done", peripheral->handle); // log_info("report_gatt_services for %02X done", peripheral->con_handle);
} }
// helper // helper
@ -673,24 +673,24 @@ static void report_gatt_indication(hci_con_handle_t con_handle, uint16_t value_h
// @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * value, uint16_t length){ static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * value, uint16_t length){
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->handle, attribute_handle, value, length); uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value, length);
emit_event_new(peripheral->callback, packet, characteristic_value_event_header_size + length); emit_event_new(peripheral->callback, packet, characteristic_value_event_header_size + length);
} }
// @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
static void report_gatt_long_characteristic_value_blob(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){ static void report_gatt_long_characteristic_value_blob(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){
uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->handle, attribute_handle, value_offset, blob, blob_length); uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value_offset, blob, blob_length);
if (!packet) return; if (!packet) return;
emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size);
} }
static void report_gatt_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){ static void report_gatt_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->handle, descriptor_handle, value, value_length); uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value, value_length);
emit_event_new(peripheral->callback, packet, value_length + 8); emit_event_new(peripheral->callback, packet, value_length + 8);
} }
static void report_gatt_long_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){ static void report_gatt_long_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){
uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->handle, descriptor_handle, value_offset, blob, blob_length); uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value_offset, blob, blob_length);
if (!packet) return; if (!packet) return;
emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size);
} }
@ -797,14 +797,14 @@ static void gatt_client_run(void){
gatt_client_t * peripheral = (gatt_client_t *) it; gatt_client_t * peripheral = (gatt_client_t *) it;
if (!att_dispatch_server_can_send_now(peripheral->handle)) return; if (!att_dispatch_server_can_send_now(peripheral->con_handle)) return;
// log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state); // log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state);
switch (peripheral->mtu_state) { switch (peripheral->mtu_state) {
case SEND_MTU_EXCHANGE:{ case SEND_MTU_EXCHANGE:{
peripheral->mtu_state = SENT_MTU_EXCHANGE; peripheral->mtu_state = SENT_MTU_EXCHANGE;
att_exchange_mtu_request(peripheral->handle); att_exchange_mtu_request(peripheral->con_handle);
return; return;
} }
case SENT_MTU_EXCHANGE: case SENT_MTU_EXCHANGE:
@ -815,7 +815,7 @@ static void gatt_client_run(void){
if (peripheral->send_confirmation){ if (peripheral->send_confirmation){
peripheral->send_confirmation = 0; peripheral->send_confirmation = 0;
att_confirmation(peripheral->handle); att_confirmation(peripheral->con_handle);
return; return;
} }
@ -1612,10 +1612,10 @@ uint8_t gatt_client_write_value_of_characteristic_without_response(btstack_packe
if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG; if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG;
if (!att_dispatch_server_can_send_now(peripheral->handle)) return GATT_CLIENT_BUSY; if (!att_dispatch_server_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY;
peripheral->callback = callback; peripheral->callback = callback;
att_write_request(ATT_WRITE_COMMAND, peripheral->handle, value_handle, value_length, value); att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value);
return 0; return 0;
} }

View File

@ -137,7 +137,7 @@ typedef struct gatt_client{
// user callback // user callback
btstack_packet_handler_t callback; btstack_packet_handler_t callback;
uint16_t handle; hci_con_handle_t con_handle;
uint8_t address_type; uint8_t address_type;
bd_addr_t address; bd_addr_t address;
@ -216,12 +216,12 @@ void gatt_client_init(void);
/** /**
* @brief MTU is available after the first query has completed. If status is equal to 0, it returns the real value, otherwise the default value of 23. * @brief MTU is available after the first query has completed. If status is equal to 0, it returns the real value, otherwise the default value of 23.
*/ */
uint8_t gatt_client_get_mtu(uint16_t handle, uint16_t * mtu); uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu);
/** /**
* @brief Returns if the GATT client is ready to receive a query. It is used with daemon. * @brief Returns if the GATT client is ready to receive a query. It is used with daemon.
*/ */
int gatt_client_is_ready(uint16_t handle); int gatt_client_is_ready(hci_con_handle_t con_handle);
/** /**
* @brief Discovers all primary services. For each found service, an le_service_event_t with type set to GATT_EVENT_SERVICE_QUERY_RESULT will be generated and passed to the registered callback. The gatt_complete_event_t, with type set to GATT_EVENT_QUERY_COMPLETE, marks the end of discovery. * @brief Discovers all primary services. For each found service, an le_service_event_t with type set to GATT_EVENT_SERVICE_QUERY_RESULT will be generated and passed to the registered callback. The gatt_complete_event_t, with type set to GATT_EVENT_QUERY_COMPLETE, marks the end of discovery.

View File

@ -487,8 +487,8 @@ void hfp_handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]); hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]);
remove_hfp_connection_context(context); remove_hfp_connection_context(context);
} else { } else {
context->con_handle = little_endian_read_16(packet, 9); context->acl_handle = little_endian_read_16(packet, 9);
printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE con_handle 0x%02x\n", context->con_handle); printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE con_handle 0x%02x\n", context->acl_handle);
context->rfcomm_cid = little_endian_read_16(packet, 12); context->rfcomm_cid = little_endian_read_16(packet, 12);
uint16_t mtu = little_endian_read_16(packet, 14); uint16_t mtu = little_endian_read_16(packet, 14);
@ -505,8 +505,6 @@ void hfp_handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
default: default:
break; break;
} }
// forward event to app, to learn about con_handle
(*hfp_callback)(packet, size);
} }
break; break;
@ -634,13 +632,7 @@ void hfp_handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
} }
break; break;
case HCI_EVENT_INQUIRY_RESULT: default:
case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
case HCI_EVENT_INQUIRY_COMPLETE:
case DAEMON_EVENT_REMOTE_NAME_CACHED:
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
// forward inquiry events to app - TODO: replace with new event handler architecture
(*hfp_callback)(packet, size);
break; break;
} }
} }

View File

@ -464,8 +464,8 @@ typedef struct hfp_connection {
btstack_linked_item_t item; btstack_linked_item_t item;
bd_addr_t remote_addr; bd_addr_t remote_addr;
hci_con_handle_t con_handle; hci_con_handle_t acl_handle;
uint16_t sco_handle; hci_con_handle_t sco_handle;
uint16_t rfcomm_channel_nr; uint16_t rfcomm_channel_nr;
uint16_t rfcomm_cid; uint16_t rfcomm_cid;

View File

@ -604,7 +604,7 @@ static int codecs_exchange_state_machine(hfp_connection_t * context){
static void hfp_init_link_settings(hfp_connection_t * context){ static void hfp_init_link_settings(hfp_connection_t * context){
// determine highest possible link setting // determine highest possible link setting
context->link_setting = HFP_LINK_SETTINGS_D1; context->link_setting = HFP_LINK_SETTINGS_D1;
if (hci_remote_esco_supported(context->con_handle)){ if (hci_remote_esco_supported(context->acl_handle)){
context->link_setting = HFP_LINK_SETTINGS_S3; context->link_setting = HFP_LINK_SETTINGS_S3;
if ((context->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) if ((context->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
&& (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){ && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){
@ -802,7 +802,7 @@ static int hfp_ag_run_for_audio_connection(hfp_connection_t * context){
if (context->establish_audio_connection){ if (context->establish_audio_connection){
context->state = HFP_W4_SCO_CONNECTED; context->state = HFP_W4_SCO_CONNECTED;
context->establish_audio_connection = 0; context->establish_audio_connection = 0;
hfp_setup_synchronous_connection(context->con_handle, context->link_setting); hfp_setup_synchronous_connection(context->acl_handle, context->link_setting);
return 1; return 1;
} }
return 0; return 0;
@ -825,7 +825,7 @@ static void hfp_timeout_handler(btstack_timer_source_t * timer){
hfp_connection_t * context = hfp_ag_context_for_timer(timer); hfp_connection_t * context = hfp_ag_context_for_timer(timer);
if (!context) return; if (!context) return;
log_info("HFP start ring timeout, con handle 0x%02x", context->con_handle); log_info("HFP start ring timeout, con handle 0x%02x", context->acl_handle);
context->ag_ring = 1; context->ag_ring = 1;
context->ag_send_clip = hfp_gsm_clip_type() && context->clip_enabled; context->ag_send_clip = hfp_gsm_clip_type() && context->clip_enabled;
@ -843,7 +843,7 @@ static void hfp_timeout_start(hfp_connection_t * context){
} }
static void hfp_timeout_stop(hfp_connection_t * context){ static void hfp_timeout_stop(hfp_connection_t * context){
log_info("HFP stop ring timeout, con handle 0x%02x", context->con_handle); log_info("HFP stop ring timeout, con handle 0x%02x", context->acl_handle);
btstack_run_loop_remove_timer(&context->hfp_timeout); btstack_run_loop_remove_timer(&context->hfp_timeout);
} }

View File

@ -525,7 +525,7 @@ static int hfp_hf_run_for_audio_connection(hfp_connection_t * context){
if (context->establish_audio_connection){ if (context->establish_audio_connection){
context->state = HFP_W4_SCO_CONNECTED; context->state = HFP_W4_SCO_CONNECTED;
context->establish_audio_connection = 0; context->establish_audio_connection = 0;
hfp_setup_synchronous_connection(context->con_handle, context->link_setting); hfp_setup_synchronous_connection(context->acl_handle, context->link_setting);
return 1; return 1;
} }
@ -782,7 +782,7 @@ static void hfp_run_for_context(hfp_connection_t * context){
static void hfp_init_link_settings(hfp_connection_t * context){ static void hfp_init_link_settings(hfp_connection_t * context){
// determine highest possible link setting // determine highest possible link setting
context->link_setting = HFP_LINK_SETTINGS_D1; context->link_setting = HFP_LINK_SETTINGS_D1;
if (hci_remote_esco_supported(context->con_handle)){ if (hci_remote_esco_supported(context->acl_handle)){
context->link_setting = HFP_LINK_SETTINGS_S3; context->link_setting = HFP_LINK_SETTINGS_S3;
if ((hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)) if ((hfp_supported_features & (1<<HFP_HFSF_ESCO_S4))
&& (context->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){ && (context->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){

View File

@ -83,7 +83,7 @@ static void hci_power_control_off(void);
static void hci_state_reset(void); static void hci_state_reset(void);
static void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); static void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status);
static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); static void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason);
static void hci_emit_nr_connections_changed(void); static void hci_emit_nr_connections_changed(void);
static void hci_emit_hci_open_failed(void); static void hci_emit_hci_open_failed(void);
static void hci_emit_discoverable_enabled(uint8_t enabled); static void hci_emit_discoverable_enabled(uint8_t enabled);
@ -2958,13 +2958,13 @@ static void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status)
hci_emit_event(event, sizeof(event), 1); hci_emit_event(event, sizeof(event), 1);
} }
static void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, uint16_t conn_handle, uint8_t status){ static void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){
uint8_t event[21]; uint8_t event[21];
event[0] = HCI_EVENT_LE_META; event[0] = HCI_EVENT_LE_META;
event[1] = sizeof(event) - 2; event[1] = sizeof(event) - 2;
event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
event[3] = status; event[3] = status;
little_endian_store_16(event, 4, conn_handle); little_endian_store_16(event, 4, con_handle);
event[6] = 0; // TODO: role event[6] = 0; // TODO: role
event[7] = address_type; event[7] = address_type;
reverse_bd_addr(address, &event[8]); reverse_bd_addr(address, &event[8]);
@ -2975,12 +2975,12 @@ static void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t addr
hci_emit_event(event, sizeof(event), 1); hci_emit_event(event, sizeof(event), 1);
} }
static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){
uint8_t event[6]; uint8_t event[6];
event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
event[1] = sizeof(event) - 2; event[1] = sizeof(event) - 2;
event[2] = 0; // status = OK event[2] = 0; // status = OK
little_endian_store_16(event, 3, handle); little_endian_store_16(event, 3, con_handle);
event[5] = reason; event[5] = reason;
hci_emit_event(event, sizeof(event), 1); hci_emit_event(event, sizeof(event), 1);
} }

View File

@ -335,7 +335,7 @@ typedef struct sm_pairing_packet {
// connection info available as long as connection exists // connection info available as long as connection exists
typedef struct sm_connection { typedef struct sm_connection {
uint16_t sm_handle; hci_con_handle_t sm_handle;
uint8_t sm_role; // 0 - IamMaster, 1 = IamSlave uint8_t sm_role; // 0 - IamMaster, 1 = IamSlave
uint8_t sm_security_request_received; uint8_t sm_security_request_received;
uint8_t sm_bonding_requested; uint8_t sm_bonding_requested;

View File

@ -168,14 +168,14 @@ static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, ui
void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u", log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
status, bd_addr_to_str(channel->address), channel->handle, channel->psm, status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
uint8_t event[23]; uint8_t event[23];
event[0] = L2CAP_EVENT_CHANNEL_OPENED; event[0] = L2CAP_EVENT_CHANNEL_OPENED;
event[1] = sizeof(event) - 2; event[1] = sizeof(event) - 2;
event[2] = status; event[2] = status;
reverse_bd_addr(channel->address, &event[3]); reverse_bd_addr(channel->address, &event[3]);
little_endian_store_16(event, 9, channel->handle); little_endian_store_16(event, 9, channel->con_handle);
little_endian_store_16(event, 11, channel->psm); little_endian_store_16(event, 11, channel->psm);
little_endian_store_16(event, 13, channel->local_cid); little_endian_store_16(event, 13, channel->local_cid);
little_endian_store_16(event, 15, channel->remote_cid); little_endian_store_16(event, 15, channel->remote_cid);
@ -187,7 +187,7 @@ void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
// if channel opened successfully, also send can send now if possible // if channel opened successfully, also send can send now if possible
if (status) return; if (status) return;
if (hci_can_send_acl_packet_now(channel->handle)){ if (hci_can_send_acl_packet_now(channel->con_handle)){
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
} else { } else {
channel->waiting_for_can_send_now = 1; channel->waiting_for_can_send_now = 1;
@ -206,12 +206,12 @@ void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
void l2cap_emit_connection_request(l2cap_channel_t *channel) { void l2cap_emit_connection_request(l2cap_channel_t *channel) {
log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid);
uint8_t event[16]; uint8_t event[16];
event[0] = L2CAP_EVENT_INCOMING_CONNECTION; event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
event[1] = sizeof(event) - 2; event[1] = sizeof(event) - 2;
reverse_bd_addr(channel->address, &event[2]); reverse_bd_addr(channel->address, &event[2]);
little_endian_store_16(event, 8, channel->handle); little_endian_store_16(event, 8, channel->con_handle);
little_endian_store_16(event, 10, channel->psm); little_endian_store_16(event, 10, channel->psm);
little_endian_store_16(event, 12, channel->local_cid); little_endian_store_16(event, 12, channel->local_cid);
little_endian_store_16(event, 14, channel->remote_cid); little_endian_store_16(event, 14, channel->remote_cid);
@ -229,11 +229,11 @@ static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uin
packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
} }
static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
uint8_t event[6]; uint8_t event[6];
event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
event[1] = 4; event[1] = 4;
little_endian_store_16(event, 2, handle); little_endian_store_16(event, 2, con_handle);
little_endian_store_16(event, 4, result); little_endian_store_16(event, 4, result);
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
if (!l2cap_event_packet_handler) return; if (!l2cap_event_packet_handler) return;
@ -255,7 +255,7 @@ static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
int l2cap_can_send_packet_now(uint16_t local_cid){ int l2cap_can_send_packet_now(uint16_t local_cid){
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return 0; if (!channel) return 0;
int can_send = hci_can_send_acl_packet_now(channel->handle); int can_send = hci_can_send_acl_packet_now(channel->con_handle);
if (!can_send){ if (!can_send){
channel->waiting_for_can_send_now = 1; channel->waiting_for_can_send_now = 1;
} }
@ -265,15 +265,15 @@ int l2cap_can_send_packet_now(uint16_t local_cid){
int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ int l2cap_can_send_prepared_packet_now(uint16_t local_cid){
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return 0; if (!channel) return 0;
int can_send = hci_can_send_prepared_acl_packet_now(channel->handle); int can_send = hci_can_send_prepared_acl_packet_now(channel->con_handle);
if (!can_send){ if (!can_send){
channel->waiting_for_can_send_now = 1; channel->waiting_for_can_send_now = 1;
} }
return can_send; return can_send;
} }
int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
int can_send = hci_can_send_acl_packet_now(handle); int can_send = hci_can_send_acl_packet_now(con_handle);
if (!can_send){ if (!can_send){
int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
if (index >= 0){ if (index >= 0){
@ -413,19 +413,19 @@ int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
return -1; // TODO: define error return -1; // TODO: define error
} }
if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
return BTSTACK_ACL_BUFFERS_FULL; return BTSTACK_ACL_BUFFERS_FULL;
} }
log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
// 0 - Connection handle : PB=pb : BC=00 // 0 - Connection handle : PB=pb : BC=00
little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
// 2 - ACL length // 2 - ACL length
little_endian_store_16(acl_buffer, 2, len + 4); little_endian_store_16(acl_buffer, 2, len + 4);
// 4 - L2CAP packet length // 4 - L2CAP packet length
@ -438,26 +438,26 @@ int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
return err; return err;
} }
int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
if (!hci_is_packet_buffer_reserved()){ if (!hci_is_packet_buffer_reserved()){
log_error("l2cap_send_prepared_connectionless called without reserving packet first"); log_error("l2cap_send_prepared_connectionless called without reserving packet first");
return BTSTACK_ACL_BUFFERS_FULL; return BTSTACK_ACL_BUFFERS_FULL;
} }
if (!hci_can_send_prepared_acl_packet_now(handle)){ if (!hci_can_send_prepared_acl_packet_now(con_handle)){
log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
return BTSTACK_ACL_BUFFERS_FULL; return BTSTACK_ACL_BUFFERS_FULL;
} }
log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
// 0 - Connection handle : PB=pb : BC=00 // 0 - Connection handle : PB=pb : BC=00
little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
// 2 - ACL length // 2 - ACL length
little_endian_store_16(acl_buffer, 2, len + 4); little_endian_store_16(acl_buffer, 2, len + 4);
// 4 - L2CAP packet length // 4 - L2CAP packet length
@ -483,7 +483,7 @@ int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
} }
if (!hci_can_send_acl_packet_now(channel->handle)){ if (!hci_can_send_acl_packet_now(channel->con_handle)){
log_info("l2cap_send cid 0x%02x, cannot send", local_cid); log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
return BTSTACK_ACL_BUFFERS_FULL; return BTSTACK_ACL_BUFFERS_FULL;
} }
@ -496,9 +496,9 @@ int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
return l2cap_send_prepared(local_cid, len); return l2cap_send_prepared(local_cid, len);
} }
int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
if (!hci_can_send_acl_packet_now(handle)){ if (!hci_can_send_acl_packet_now(con_handle)){
log_info("l2cap_send cid 0x%02x, cannot send", cid); log_info("l2cap_send cid 0x%02x, cannot send", cid);
return BTSTACK_ACL_BUFFERS_FULL; return BTSTACK_ACL_BUFFERS_FULL;
} }
@ -508,11 +508,11 @@ int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint
memcpy(&acl_buffer[8], data, len); memcpy(&acl_buffer[8], data, len);
return l2cap_send_prepared_connectionless(handle, cid, len); return l2cap_send_prepared_connectionless(con_handle, cid, len);
} }
int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
} }
static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
@ -611,10 +611,10 @@ static void l2cap_run(void){
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
} }
break; break;
@ -627,9 +627,9 @@ static void l2cap_run(void){
break; break;
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
channel->state = L2CAP_STATE_INVALID; channel->state = L2CAP_STATE_INVALID;
l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
// discard channel - l2cap_finialize_channel_close without sending l2cap close event // discard channel - l2cap_finialize_channel_close without sending l2cap close event
l2cap_stop_rtx(channel); l2cap_stop_rtx(channel);
btstack_linked_list_iterator_remove(&it); btstack_linked_list_iterator_remove(&it);
@ -637,23 +637,23 @@ static void l2cap_run(void){
break; break;
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
channel->state = L2CAP_STATE_CONFIG; channel->state = L2CAP_STATE_CONFIG;
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
break; break;
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
// success, start l2cap handshake // success, start l2cap handshake
channel->local_sig_id = l2cap_next_sig_id(); channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
l2cap_start_rtx(channel); l2cap_start_rtx(channel);
break; break;
case L2CAP_STATE_CONFIG: case L2CAP_STATE_CONFIG:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
uint16_t flags = 0; uint16_t flags = 0;
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
@ -663,15 +663,15 @@ static void l2cap_run(void){
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
} }
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
} else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
config_options[0] = 1; // MTU config_options[0] = 1; // MTU
config_options[1] = 2; // len param config_options[1] = 2; // len param
little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
} else { } else {
l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
} }
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
} }
@ -682,7 +682,7 @@ static void l2cap_run(void){
config_options[0] = 1; // MTU config_options[0] = 1; // MTU
config_options[1] = 2; // len param config_options[1] = 2; // len param
little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
l2cap_start_rtx(channel); l2cap_start_rtx(channel);
} }
if (l2cap_channel_ready_for_open(channel)){ if (l2cap_channel_ready_for_open(channel)){
@ -692,18 +692,18 @@ static void l2cap_run(void){
break; break;
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
channel->state = L2CAP_STATE_INVALID; channel->state = L2CAP_STATE_INVALID;
l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
// we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :) // we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :)
l2cap_finialize_channel_close(channel); // -- remove from list l2cap_finialize_channel_close(channel); // -- remove from list
break; break;
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
if (!hci_can_send_acl_packet_now(channel->handle)) break; if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
channel->local_sig_id = l2cap_next_sig_id(); channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_DISCONNECT; channel->state = L2CAP_STATE_WAIT_DISCONNECT;
l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
break; break;
default: default:
break; break;
@ -748,11 +748,11 @@ uint16_t l2cap_max_le_mtu(void){
return l2cap_max_mtu(); return l2cap_max_mtu();
} }
static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
log_info("l2cap_handle_connection_complete expected state"); log_info("l2cap_handle_connection_complete expected state");
// success, start l2cap handshake // success, start l2cap handshake
channel->handle = handle; channel->con_handle = con_handle;
// check remote SSP feature first // check remote SSP feature first
channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
} }
@ -763,10 +763,10 @@ static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * ch
// we have been waiting for remote supported features, if both support SSP, // we have been waiting for remote supported features, if both support SSP,
log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm)); log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm));
if (gap_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
// request security level 2 // request security level 2
channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
gap_request_security_level(channel->handle, LEVEL_2); gap_request_security_level(channel->con_handle, LEVEL_2);
return; return;
} }
// fine, go ahead // fine, go ahead
@ -785,50 +785,50 @@ uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd
log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
// alloc structure // alloc structure
l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
if (!chan) { if (!channel) {
return BTSTACK_MEMORY_ALLOC_FAILED; return BTSTACK_MEMORY_ALLOC_FAILED;
} }
// Init memory (make valgrind happy) // Init memory (make valgrind happy)
memset(chan, 0, sizeof(l2cap_channel_t)); memset(channel, 0, sizeof(l2cap_channel_t));
// limit local mtu to max acl packet length - l2cap header // limit local mtu to max acl packet length - l2cap header
if (mtu > l2cap_max_mtu()) { if (mtu > l2cap_max_mtu()) {
mtu = l2cap_max_mtu(); mtu = l2cap_max_mtu();
} }
// fill in // fill in
bd_addr_copy(chan->address, address); bd_addr_copy(channel->address, address);
chan->psm = psm; channel->psm = psm;
chan->handle = 0; channel->con_handle = 0;
chan->packet_handler = channel_packet_handler; channel->packet_handler = channel_packet_handler;
chan->remote_mtu = L2CAP_MINIMAL_MTU; channel->remote_mtu = L2CAP_MINIMAL_MTU;
chan->local_mtu = mtu; channel->local_mtu = mtu;
chan->local_cid = l2cap_next_local_cid(); channel->local_cid = l2cap_next_local_cid();
// set initial state // set initial state
chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
chan->remote_sig_id = L2CAP_SIG_ID_INVALID; channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
chan->local_sig_id = L2CAP_SIG_ID_INVALID; channel->local_sig_id = L2CAP_SIG_ID_INVALID;
chan->required_security_level = LEVEL_0; channel->required_security_level = LEVEL_0;
// add to connections list // add to connections list
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan); btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
// store local_cid // store local_cid
if (out_local_cid){ if (out_local_cid){
*out_local_cid = chan->local_cid; *out_local_cid = channel->local_cid;
} }
// check if hci connection is already usable // check if hci connection is already usable
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
if (conn){ if (conn){
log_info("l2cap_create_channel, hci connection already exists"); log_info("l2cap_create_channel, hci connection already exists");
l2cap_handle_connection_complete(conn->con_handle, chan); l2cap_handle_connection_complete(conn->con_handle, channel);
// check if remote supported fearures are already received // check if remote supported fearures are already received
if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
l2cap_handle_remote_supported_features_received(chan); l2cap_handle_remote_supported_features_received(channel);
} }
} }
@ -891,7 +891,7 @@ static void l2cap_notify_channel_can_send(void){
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!channel->waiting_for_can_send_now) continue; if (!channel->waiting_for_can_send_now) continue;
if (!hci_can_send_acl_packet_now(channel->handle)) continue; if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
channel->waiting_for_can_send_now = 0; channel->waiting_for_can_send_now = 0;
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
} }
@ -955,7 +955,7 @@ static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *
btstack_linked_list_iterator_init(&it, &l2cap_channels); btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->handle != handle) continue; if (channel->con_handle != handle) continue;
l2cap_emit_channel_closed(channel); l2cap_emit_channel_closed(channel);
l2cap_stop_rtx(channel); l2cap_stop_rtx(channel);
btstack_linked_list_iterator_remove(&it); btstack_linked_list_iterator_remove(&it);
@ -979,7 +979,7 @@ static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *
btstack_linked_list_iterator_init(&it, &l2cap_channels); btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->handle != handle) continue; if (channel->con_handle != handle) continue;
hci_con_used = 1; hci_con_used = 1;
break; break;
} }
@ -993,7 +993,7 @@ static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *
btstack_linked_list_iterator_init(&it, &l2cap_channels); btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->handle != handle) continue; if (channel->con_handle != handle) continue;
l2cap_handle_remote_supported_features_received(channel); l2cap_handle_remote_supported_features_received(channel);
break; break;
} }
@ -1005,7 +1005,7 @@ static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *
btstack_linked_list_iterator_init(&it, &l2cap_channels); btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->handle != handle) continue; if (channel->con_handle != handle) continue;
log_info("l2cap - state %u", channel->state); log_info("l2cap - state %u", channel->state);
@ -1093,7 +1093,7 @@ static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig
// fill in // fill in
bd_addr_copy(channel->address, hci_connection->address); bd_addr_copy(channel->address, hci_connection->address);
channel->psm = psm; channel->psm = psm;
channel->handle = handle; channel->con_handle = handle;
channel->packet_handler = service->packet_handler; channel->packet_handler = service->packet_handler;
channel->local_cid = l2cap_next_local_cid(); channel->local_cid = l2cap_next_local_cid();
channel->remote_cid = source_cid; channel->remote_cid = source_cid;
@ -1365,7 +1365,7 @@ static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t *
btstack_linked_list_iterator_init(&it, &l2cap_channels); btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){ while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->handle != handle) continue; if (channel->con_handle != handle) continue;
if (code & 1) { if (code & 1) {
// match odd commands (responses) by previous signaling identifier // match odd commands (responses) by previous signaling identifier
if (channel->local_sig_id == sig_id) { if (channel->local_sig_id == sig_id) {

View File

@ -113,7 +113,7 @@ typedef struct {
// info // info
bd_addr_t address; bd_addr_t address;
hci_con_handle_t handle; hci_con_handle_t con_handle;
uint8_t remote_sig_id; // used by other side, needed for delayed response uint8_t remote_sig_id; // used by other side, needed for delayed response
uint8_t local_sig_id; // own signaling identifier uint8_t local_sig_id; // own signaling identifier
@ -166,12 +166,12 @@ typedef struct l2cap_signaling_response {
void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id);
int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id); int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id);
int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len);
int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len);
// PTS Testing // PTS Testing
int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len); int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len);
void l2cap_require_security_level_2_for_outgoing_sdp(void); void l2cap_require_security_level_2_for_outgoing_sdp(void);
/* API_START */ /* API_START */