l2cap.c (b8bf1755110d8895952df06fbc69f4ab7788e64d) l2cap.c (9da9850b83fde69f1dc00ad3fb029803b8844b54)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 1010 unchanged lines hidden (view full) ---

1019 btstack_linked_list_add_tail(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
1020}
1021
1022void l2cap_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){
1023 btstack_linked_list_remove(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
1024}
1025
1026static void l2cap_emit_event(uint8_t *event, uint16_t size) {
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 1010 unchanged lines hidden (view full) ---

1019 btstack_linked_list_add_tail(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
1020}
1021
1022void l2cap_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){
1023 btstack_linked_list_remove(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
1024}
1025
1026static void l2cap_emit_event(uint8_t *event, uint16_t size) {
1027 hci_dump_packet( HCI_EVENT_PACKET, 1, event, size);
1027 hci_dump_btstack_event( event, size);
1028 // dispatch to all event handlers
1029 btstack_linked_list_iterator_t it;
1030 btstack_linked_list_iterator_init(&it, &l2cap_event_handlers);
1031 while (btstack_linked_list_iterator_has_next(&it)){
1032 btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
1033 entry->callback(HCI_EVENT_PACKET, 0, event, size);
1034 }
1035}

--- 76 unchanged lines hidden (view full) ---

1112}
1113
1114static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
1115 log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
1116 uint8_t event[4];
1117 event[0] = L2CAP_EVENT_CAN_SEND_NOW;
1118 event[1] = sizeof(event) - 2u;
1119 little_endian_store_16(event, 2, channel);
1028 // dispatch to all event handlers
1029 btstack_linked_list_iterator_t it;
1030 btstack_linked_list_iterator_init(&it, &l2cap_event_handlers);
1031 while (btstack_linked_list_iterator_has_next(&it)){
1032 btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
1033 entry->callback(HCI_EVENT_PACKET, 0, event, size);
1034 }
1035}

--- 76 unchanged lines hidden (view full) ---

1112}
1113
1114static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
1115 log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
1116 uint8_t event[4];
1117 event[0] = L2CAP_EVENT_CAN_SEND_NOW;
1118 event[1] = sizeof(event) - 2u;
1119 little_endian_store_16(event, 2, channel);
1120 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
1120 hci_dump_btstack_event( event, sizeof(event));
1121 packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
1122}
1123
1124#ifdef L2CAP_USES_CHANNELS
1125static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
1126 (* (channel->packet_handler))(type, channel->local_cid, data, size);
1127}
1128
1129static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
1130 uint8_t event[4];
1131 event[0] = event_code;
1132 event[1] = sizeof(event) - 2u;
1133 little_endian_store_16(event, 2, channel->local_cid);
1121 packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
1122}
1123
1124#ifdef L2CAP_USES_CHANNELS
1125static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
1126 (* (channel->packet_handler))(type, channel->local_cid, data, size);
1127}
1128
1129static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
1130 uint8_t event[4];
1131 event[0] = event_code;
1132 event[1] = sizeof(event) - 2u;
1133 little_endian_store_16(event, 2, channel->local_cid);
1134 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
1134 hci_dump_btstack_event( event, sizeof(event));
1135 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1136}
1137#endif
1138
1139#ifdef ENABLE_CLASSIC
1140void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1141 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",
1142 status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,

--- 15 unchanged lines hidden (view full) ---

1158 log_info("ERTM mode %u, fcs enabled %u", channel->mode, channel->fcs_option);
1159 event[24] = channel->mode;
1160 event[25] = channel->fcs_option;
1161
1162#else
1163 event[24] = L2CAP_CHANNEL_MODE_BASIC;
1164 event[25] = 0;
1165#endif
1135 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1136}
1137#endif
1138
1139#ifdef ENABLE_CLASSIC
1140void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1141 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",
1142 status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,

--- 15 unchanged lines hidden (view full) ---

1158 log_info("ERTM mode %u, fcs enabled %u", channel->mode, channel->fcs_option);
1159 event[24] = channel->mode;
1160 event[25] = channel->fcs_option;
1161
1162#else
1163 event[24] = L2CAP_CHANNEL_MODE_BASIC;
1164 event[25] = 0;
1165#endif
1166 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
1166 hci_dump_btstack_event( event, sizeof(event));
1167 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1168}
1169
1170static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
1171 log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
1172 bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid);
1173 uint8_t event[16];
1174 event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
1175 event[1] = sizeof(event) - 2;
1176 reverse_bd_addr(channel->address, &event[2]);
1177 little_endian_store_16(event, 8, channel->con_handle);
1178 little_endian_store_16(event, 10, channel->psm);
1179 little_endian_store_16(event, 12, channel->local_cid);
1180 little_endian_store_16(event, 14, channel->remote_cid);
1167 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1168}
1169
1170static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
1171 log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
1172 bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid);
1173 uint8_t event[16];
1174 event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
1175 event[1] = sizeof(event) - 2;
1176 reverse_bd_addr(channel->address, &event[2]);
1177 little_endian_store_16(event, 8, channel->con_handle);
1178 little_endian_store_16(event, 10, channel->psm);
1179 little_endian_store_16(event, 12, channel->local_cid);
1180 little_endian_store_16(event, 14, channel->remote_cid);
1181 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
1181 hci_dump_btstack_event( event, sizeof(event));
1182 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1183}
1184
1185static void l2cap_handle_channel_open_failed(l2cap_channel_t * channel, uint8_t status){
1186#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
1187 // emit ertm buffer released, as it's not needed. if in basic mode, it was either not allocated or already released
1188 if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
1189 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);

--- 4072 unchanged lines hidden (view full) ---

5262 event[1] = sizeof(event) - 2u;
5263 event[2] = channel->address_type;
5264 reverse_bd_addr(channel->address, &event[3]);
5265 little_endian_store_16(event, 9, channel->con_handle);
5266 little_endian_store_16(event, 11, channel->psm);
5267 little_endian_store_16(event, 13, channel->local_cid);
5268 little_endian_store_16(event, 15, channel->remote_cid);
5269 little_endian_store_16(event, 17, channel->remote_mtu);
1182 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1183}
1184
1185static void l2cap_handle_channel_open_failed(l2cap_channel_t * channel, uint8_t status){
1186#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
1187 // emit ertm buffer released, as it's not needed. if in basic mode, it was either not allocated or already released
1188 if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
1189 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);

--- 4072 unchanged lines hidden (view full) ---

5262 event[1] = sizeof(event) - 2u;
5263 event[2] = channel->address_type;
5264 reverse_bd_addr(channel->address, &event[3]);
5265 little_endian_store_16(event, 9, channel->con_handle);
5266 little_endian_store_16(event, 11, channel->psm);
5267 little_endian_store_16(event, 13, channel->local_cid);
5268 little_endian_store_16(event, 15, channel->remote_cid);
5269 little_endian_store_16(event, 17, channel->remote_mtu);
5270 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
5270 hci_dump_btstack_event( event, sizeof(event));
5271 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5272}
5273// 11BH22222
5274static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
5275 log_info("opened le channel status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u",
5276 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
5277 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
5278 uint8_t event[23];

--- 4 unchanged lines hidden (view full) ---

5283 reverse_bd_addr(channel->address, &event[4]);
5284 little_endian_store_16(event, 10, channel->con_handle);
5285 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
5286 little_endian_store_16(event, 13, channel->psm);
5287 little_endian_store_16(event, 15, channel->local_cid);
5288 little_endian_store_16(event, 17, channel->remote_cid);
5289 little_endian_store_16(event, 19, channel->local_mtu);
5290 little_endian_store_16(event, 21, channel->remote_mtu);
5271 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5272}
5273// 11BH22222
5274static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
5275 log_info("opened le channel status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u",
5276 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
5277 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
5278 uint8_t event[23];

--- 4 unchanged lines hidden (view full) ---

5283 reverse_bd_addr(channel->address, &event[4]);
5284 little_endian_store_16(event, 10, channel->con_handle);
5285 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
5286 little_endian_store_16(event, 13, channel->psm);
5287 little_endian_store_16(event, 15, channel->local_cid);
5288 little_endian_store_16(event, 17, channel->remote_cid);
5289 little_endian_store_16(event, 19, channel->local_mtu);
5290 little_endian_store_16(event, 21, channel->remote_mtu);
5291 hci_dump_packet( HCI_EVENT_PACKET, 1, event, sizeof(event));
5291 hci_dump_btstack_event( event, sizeof(event));
5292 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5293}
5294
5295// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
5296void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){
5297 channel->state = L2CAP_STATE_CLOSED;
5298 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
5299 // discard channel

--- 548 unchanged lines hidden ---
5292 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5293}
5294
5295// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
5296void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){
5297 channel->state = L2CAP_STATE_CLOSED;
5298 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
5299 // discard channel

--- 548 unchanged lines hidden ---