l2cap.c (d2bf13739d87038d501837d099c8fe9a2d455e49) l2cap.c (01f33f4af1715157f75de0218bdb20ccf6952236)
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

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

163static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
164static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
165static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
166static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
167static uint8_t l2cap_classic_send(l2cap_channel_t * channel, const uint8_t *data, uint16_t len);
168#endif
169#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
170static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
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

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

163static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
164static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
165static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
166static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
167static uint8_t l2cap_classic_send(l2cap_channel_t * channel, const uint8_t *data, uint16_t len);
168#endif
169#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
170static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
171static void l2cap_cbm_emit_channel_closed(l2cap_channel_t * channel);
172static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel);
173static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel);
174static void l2cap_cbm_finialize_channel_close(l2cap_channel_t *channel);
175static inline l2cap_service_t * l2cap_cbm_get_service(uint16_t le_psm);
176#endif
177#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
178static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size);
179static void l2cap_credit_based_send_pdu(l2cap_channel_t *channel);

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

2742}
2743#endif
2744
2745#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
2746static void l2cap_handle_hci_le_disconnect_event(l2cap_channel_t * channel){
2747 if (l2cap_send_open_failed_on_hci_disconnect(channel)){
2748 l2cap_cbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2749 } else {
171static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel);
172static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel);
173static void l2cap_cbm_finialize_channel_close(l2cap_channel_t *channel);
174static inline l2cap_service_t * l2cap_cbm_get_service(uint16_t le_psm);
175#endif
176#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
177static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size);
178static void l2cap_credit_based_send_pdu(l2cap_channel_t *channel);

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

2741}
2742#endif
2743
2744#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
2745static void l2cap_handle_hci_le_disconnect_event(l2cap_channel_t * channel){
2746 if (l2cap_send_open_failed_on_hci_disconnect(channel)){
2747 l2cap_cbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2748 } else {
2750 l2cap_cbm_emit_channel_closed(channel);
2749 l2cap_emit_channel_closed(channel);
2751 }
2752 l2cap_free_channel_entry(channel);
2753}
2754#endif
2755
2756#ifdef ENABLE_CLASSIC
2757static void l2cap_check_classic_timeout(hci_con_handle_t handle){
2758 if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) {

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

2889 l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2890 break;
2891 case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
2892 case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
2893 // emit reconfigure failure - result = 0xffff
2894 l2cap_ecbm_emit_reconfigure_complete(channel, 0xffff);
2895 break;
2896 default:
2750 }
2751 l2cap_free_channel_entry(channel);
2752}
2753#endif
2754
2755#ifdef ENABLE_CLASSIC
2756static void l2cap_check_classic_timeout(hci_con_handle_t handle){
2757 if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) {

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

2888 l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
2889 break;
2890 case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
2891 case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
2892 // emit reconfigure failure - result = 0xffff
2893 l2cap_ecbm_emit_reconfigure_complete(channel, 0xffff);
2894 break;
2895 default:
2897 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ECBM_CHANNEL_CLOSED);
2896 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
2898 break;
2899 }
2900 l2cap_free_channel_entry(channel);
2901 break;
2902#endif
2903 default:
2904 break;
2905 }

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

4832 if (done) {
4833 channel->send_sdu_buffer = NULL;
4834 }
4835
4836 hci_send_acl_packet_buffer(8u + pos);
4837
4838 if (done) {
4839 // send done event
2897 break;
2898 }
2899 l2cap_free_channel_entry(channel);
2900 break;
2901#endif
2902 default:
2903 break;
2904 }

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

4831 if (done) {
4832 channel->send_sdu_buffer = NULL;
4833 }
4834
4835 hci_send_acl_packet_buffer(8u + pos);
4836
4837 if (done) {
4838 // send done event
4840 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CBM_PACKET_SENT);
4839 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_PACKET_SENT);
4841 // inform about can send now
4842 l2cap_credit_based_notify_channel_can_send(channel);
4843 }
4844}
4845
4846static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size){
4847
4848 if (size > channel->remote_mtu){

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

4974 }
4975}
4976
4977static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel){
4978 if (!channel->waiting_for_can_send_now) return;
4979 if (channel->send_sdu_buffer) return;
4980 channel->waiting_for_can_send_now = 0;
4981 log_debug("le can send now, local_cid 0x%x", channel->local_cid);
4840 // inform about can send now
4841 l2cap_credit_based_notify_channel_can_send(channel);
4842 }
4843}
4844
4845static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size){
4846
4847 if (size > channel->remote_mtu){

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

4973 }
4974}
4975
4976static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel){
4977 if (!channel->waiting_for_can_send_now) return;
4978 if (channel->send_sdu_buffer) return;
4979 channel->waiting_for_can_send_now = 0;
4980 log_debug("le can send now, local_cid 0x%x", channel->local_cid);
4982 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CBM_CAN_SEND_NOW);
4981 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CAN_SEND_NOW);
4983}
4984#endif
4985
4986#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
4987// 1BH2222
4988static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel) {
4989 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",
4990 channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);

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

5017 little_endian_store_16(event, 13, channel->psm);
5018 little_endian_store_16(event, 15, channel->local_cid);
5019 little_endian_store_16(event, 17, channel->remote_cid);
5020 little_endian_store_16(event, 19, channel->local_mtu);
5021 little_endian_store_16(event, 21, channel->remote_mtu);
5022 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5023 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5024}
4982}
4983#endif
4984
4985#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
4986// 1BH2222
4987static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel) {
4988 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",
4989 channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);

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

5016 little_endian_store_16(event, 13, channel->psm);
5017 little_endian_store_16(event, 15, channel->local_cid);
5018 little_endian_store_16(event, 17, channel->remote_cid);
5019 little_endian_store_16(event, 19, channel->local_mtu);
5020 little_endian_store_16(event, 21, channel->remote_mtu);
5021 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5022 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5023}
5025// 2
5026static void l2cap_cbm_emit_channel_closed(l2cap_channel_t * channel){
5027 log_info("closed local_cid 0x%x", channel->local_cid);
5028 uint8_t event[4];
5029 event[0] = L2CAP_EVENT_CBM_CHANNEL_CLOSED;
5030 event[1] = sizeof(event) - 2u;
5031 little_endian_store_16(event, 2, channel->local_cid);
5032 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5033 l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
5034}
5035
5036// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
5037void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){
5038 channel->state = L2CAP_STATE_CLOSED;
5039 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
5040 // discard channel
5041 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
5042 l2cap_free_channel_entry(channel);

--- 491 unchanged lines hidden ---
5024
5025// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
5026void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){
5027 channel->state = L2CAP_STATE_CLOSED;
5028 l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
5029 // discard channel
5030 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
5031 l2cap_free_channel_entry(channel);

--- 491 unchanged lines hidden ---