l2cap.c (8d8936a60e96fdf2e1dedb4085859674df5106d4) l2cap.c (dfc5d4aed94bed8234f94d1fd37178a70255f1ba)
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

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

1986#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
1987
1988// 11BH22222
1989static void l2cap_ecbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1990 log_info("opened ecbm 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",
1991 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
1992 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
1993 uint8_t event[23];
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

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

1986#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
1987
1988// 11BH22222
1989static void l2cap_ecbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1990 log_info("opened ecbm 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",
1991 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
1992 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
1993 uint8_t event[23];
1994 event[0] = L2CAP_EVENT_DATA_CHANNEL_OPENED;
1994 event[0] = L2CAP_EVENT_ECBM_CHANNEL_OPENED;
1995 event[1] = sizeof(event) - 2u;
1996 event[2] = status;
1997 event[3] = channel->address_type;
1998 reverse_bd_addr(channel->address, &event[4]);
1999 little_endian_store_16(event, 10, channel->con_handle);
2000 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
2001 little_endian_store_16(event, 13, channel->psm);
2002 little_endian_store_16(event, 15, channel->local_cid);
2003 little_endian_store_16(event, 17, channel->remote_cid);
2004 little_endian_store_16(event, 19, channel->local_mtu);
2005 little_endian_store_16(event, 21, channel->remote_mtu);
2006 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
2007 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2008}
2009
2010static void l2cap_ecbm_emit_reconfigure_complete(l2cap_channel_t *channel, uint16_t result) {
2011 // emit event
2012 uint8_t event[6];
1995 event[1] = sizeof(event) - 2u;
1996 event[2] = status;
1997 event[3] = channel->address_type;
1998 reverse_bd_addr(channel->address, &event[4]);
1999 little_endian_store_16(event, 10, channel->con_handle);
2000 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
2001 little_endian_store_16(event, 13, channel->psm);
2002 little_endian_store_16(event, 15, channel->local_cid);
2003 little_endian_store_16(event, 17, channel->remote_cid);
2004 little_endian_store_16(event, 19, channel->local_mtu);
2005 little_endian_store_16(event, 21, channel->remote_mtu);
2006 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
2007 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2008}
2009
2010static void l2cap_ecbm_emit_reconfigure_complete(l2cap_channel_t *channel, uint16_t result) {
2011 // emit event
2012 uint8_t event[6];
2013 event[0] = L2CAP_EVENT_DATA_CHANNEL_RECONFIGURATION_COMPLETE;
2013 event[0] = L2CAP_EVENT_ECBM_RECONFIGURATION_COMPLETE;
2014 event[1] = sizeof(event) - 2;
2015 little_endian_store_16(event, 2, channel->local_cid);
2016 little_endian_store_16(event, 4, result);
2017 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2018}
2019
2020static void l2cap_ecbm_run_channels(void) {
2021 hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;

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

2819 l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2820 break;
2821 case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
2822 case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
2823 // emit reconfigure failure - result = 0xffff
2824 l2cap_ecbm_emit_reconfigure_complete(channel, 0xffff);
2825 break;
2826 default:
2014 event[1] = sizeof(event) - 2;
2015 little_endian_store_16(event, 2, channel->local_cid);
2016 little_endian_store_16(event, 4, result);
2017 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2018}
2019
2020static void l2cap_ecbm_run_channels(void) {
2021 hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;

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

2819 l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2820 break;
2821 case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
2822 case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
2823 // emit reconfigure failure - result = 0xffff
2824 l2cap_ecbm_emit_reconfigure_complete(channel, 0xffff);
2825 break;
2826 default:
2827 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_DATA_CHANNEL_CLOSED);
2827 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ECBM_CHANNEL_OPENED);
2828 break;
2829 }
2830 l2cap_free_channel_entry(channel);
2831 break;
2832#endif
2833 default:
2834 break;
2835 }

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

3806 if (a_local_cid == 0) {
3807 l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
3808 num_channels_and_signaling_cid, result);
3809 return 1;
3810 }
3811
3812 // emit incoming data connection event
3813 uint8_t event[16];
2828 break;
2829 }
2830 l2cap_free_channel_entry(channel);
2831 break;
2832#endif
2833 default:
2834 break;
2835 }

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

3806 if (a_local_cid == 0) {
3807 l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
3808 num_channels_and_signaling_cid, result);
3809 return 1;
3810 }
3811
3812 // emit incoming data connection event
3813 uint8_t event[16];
3814 event[0] = L2CAP_EVENT_DATA_CHANNEL_INCOMING;
3814 event[0] = L2CAP_EVENT_ECBM_INCOMING_CONNECTION;
3815 event[1] = sizeof(event) - 2;
3816 event[2] = connection->address_type;
3817 reverse_bd_addr(connection->address, &event[3]);
3818 little_endian_store_16(event, 9, connection->con_handle);
3819 little_endian_store_16(event, 11, spsm);
3820 event[13] = num_channels;
3821 little_endian_store_16(event, 14, a_local_cid);
3822 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));

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

3924 // update channels
3925 for (i = 0; i < num_channels; i++) {
3926 uint16_t remote_cid = little_endian_read_16(command, 8 + (i * sizeof(uint16_t)));
3927 l2cap_channel_t *channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
3928 channel->remote_mps = new_mps;
3929 channel->remote_mtu = new_mtu;
3930 // emit event
3931 uint8_t event[8];
3815 event[1] = sizeof(event) - 2;
3816 event[2] = connection->address_type;
3817 reverse_bd_addr(connection->address, &event[3]);
3818 little_endian_store_16(event, 9, connection->con_handle);
3819 little_endian_store_16(event, 11, spsm);
3820 event[13] = num_channels;
3821 little_endian_store_16(event, 14, a_local_cid);
3822 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));

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

3924 // update channels
3925 for (i = 0; i < num_channels; i++) {
3926 uint16_t remote_cid = little_endian_read_16(command, 8 + (i * sizeof(uint16_t)));
3927 l2cap_channel_t *channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
3928 channel->remote_mps = new_mps;
3929 channel->remote_mtu = new_mtu;
3930 // emit event
3931 uint8_t event[8];
3932 event[0] = L2CAP_EVENT_DATA_CHANNEL_RECONFIGURED;
3932 event[0] = L2CAP_EVENT_ECBM_RECONFIGURED;
3933 event[1] = sizeof(event) - 2;
3934 little_endian_store_16(event, 2, channel->local_cid);
3935 little_endian_store_16(event, 4, new_mtu);
3936 little_endian_store_16(event, 6, new_mps);
3937 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
3938 }
3939
3940 // send accept

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

4762 if (done) {
4763 channel->send_sdu_buffer = NULL;
4764 }
4765
4766 hci_send_acl_packet_buffer(8u + pos);
4767
4768 if (done) {
4769 // send done event
3933 event[1] = sizeof(event) - 2;
3934 little_endian_store_16(event, 2, channel->local_cid);
3935 little_endian_store_16(event, 4, new_mtu);
3936 little_endian_store_16(event, 6, new_mps);
3937 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
3938 }
3939
3940 // send accept

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

4762 if (done) {
4763 channel->send_sdu_buffer = NULL;
4764 }
4765
4766 hci_send_acl_packet_buffer(8u + pos);
4767
4768 if (done) {
4769 // send done event
4770 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT);
4770 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CBM_PACKET_SENT);
4771 // inform about can send now
4772 l2cap_cbm_notify_channel_can_send(channel);
4773 }
4774}
4775
4776static uint8_t l2cap_credit_based_send_data(uint16_t local_cid, const uint8_t * data, uint16_t size){
4777
4778 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);

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

4923
4924#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
4925
4926static void l2cap_cbm_notify_channel_can_send(l2cap_channel_t *channel){
4927 if (!channel->waiting_for_can_send_now) return;
4928 if (channel->send_sdu_buffer) return;
4929 channel->waiting_for_can_send_now = 0;
4930 log_debug("le can send now, local_cid 0x%x", channel->local_cid);
4771 // inform about can send now
4772 l2cap_cbm_notify_channel_can_send(channel);
4773 }
4774}
4775
4776static uint8_t l2cap_credit_based_send_data(uint16_t local_cid, const uint8_t * data, uint16_t size){
4777
4778 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);

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

4923
4924#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
4925
4926static void l2cap_cbm_notify_channel_can_send(l2cap_channel_t *channel){
4927 if (!channel->waiting_for_can_send_now) return;
4928 if (channel->send_sdu_buffer) return;
4929 channel->waiting_for_can_send_now = 0;
4930 log_debug("le can send now, local_cid 0x%x", channel->local_cid);
4931 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW);
4931 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CBM_CAN_SEND_NOW);
4932}
4933
4934// 1BH2222
4935static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel) {
4936 log_info("le incoming addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u",
4937 channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
4938 uint8_t event[19];
4932}
4933
4934// 1BH2222
4935static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel) {
4936 log_info("le incoming addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u",
4937 channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
4938 uint8_t event[19];
4939 event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION;
4939 event[0] = L2CAP_EVENT_CBM_INCOMING_CONNECTION;
4940 event[1] = sizeof(event) - 2u;
4941 event[2] = channel->address_type;
4942 reverse_bd_addr(channel->address, &event[3]);
4943 little_endian_store_16(event, 9, channel->con_handle);
4944 little_endian_store_16(event, 11, channel->psm);
4945 little_endian_store_16(event, 13, channel->local_cid);
4946 little_endian_store_16(event, 15, channel->remote_cid);
4947 little_endian_store_16(event, 17, channel->remote_mtu);
4948 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4949 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4950}
4951// 11BH22222
4952static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
4953 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",
4954 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
4955 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
4956 uint8_t event[23];
4940 event[1] = sizeof(event) - 2u;
4941 event[2] = channel->address_type;
4942 reverse_bd_addr(channel->address, &event[3]);
4943 little_endian_store_16(event, 9, channel->con_handle);
4944 little_endian_store_16(event, 11, channel->psm);
4945 little_endian_store_16(event, 13, channel->local_cid);
4946 little_endian_store_16(event, 15, channel->remote_cid);
4947 little_endian_store_16(event, 17, channel->remote_mtu);
4948 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4949 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4950}
4951// 11BH22222
4952static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
4953 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",
4954 status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
4955 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
4956 uint8_t event[23];
4957 event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED;
4957 event[0] = L2CAP_EVENT_CBM_CHANNEL_OPENED;
4958 event[1] = sizeof(event) - 2u;
4959 event[2] = status;
4960 event[3] = channel->address_type;
4961 reverse_bd_addr(channel->address, &event[4]);
4962 little_endian_store_16(event, 10, channel->con_handle);
4963 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
4964 little_endian_store_16(event, 13, channel->psm);
4965 little_endian_store_16(event, 15, channel->local_cid);
4966 little_endian_store_16(event, 17, channel->remote_cid);
4967 little_endian_store_16(event, 19, channel->local_mtu);
4968 little_endian_store_16(event, 21, channel->remote_mtu);
4969 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4970 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4971}
4972// 2
4973static void l2cap_cbm_emit_channel_closed(l2cap_channel_t * channel){
4974 log_info("closed local_cid 0x%x", channel->local_cid);
4975 uint8_t event[4];
4958 event[1] = sizeof(event) - 2u;
4959 event[2] = status;
4960 event[3] = channel->address_type;
4961 reverse_bd_addr(channel->address, &event[4]);
4962 little_endian_store_16(event, 10, channel->con_handle);
4963 event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
4964 little_endian_store_16(event, 13, channel->psm);
4965 little_endian_store_16(event, 15, channel->local_cid);
4966 little_endian_store_16(event, 17, channel->remote_cid);
4967 little_endian_store_16(event, 19, channel->local_mtu);
4968 little_endian_store_16(event, 21, channel->remote_mtu);
4969 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4970 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4971}
4972// 2
4973static void l2cap_cbm_emit_channel_closed(l2cap_channel_t * channel){
4974 log_info("closed local_cid 0x%x", channel->local_cid);
4975 uint8_t event[4];
4976 event[0] = L2CAP_EVENT_LE_CHANNEL_CLOSED;
4976 event[0] = L2CAP_EVENT_CBM_CHANNEL_CLOSED;
4977 event[1] = sizeof(event) - 2u;
4978 little_endian_store_16(event, 2, channel->local_cid);
4979 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4980 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4981}
4982
4983// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
4984void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){

--- 551 unchanged lines hidden ---
4977 event[1] = sizeof(event) - 2u;
4978 little_endian_store_16(event, 2, channel->local_cid);
4979 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
4980 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
4981}
4982
4983// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
4984void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){

--- 551 unchanged lines hidden ---