xref: /btstack/src/l2cap.c (revision 2d00edd4aa4b9295ae0c72ee712a568b115fb6a9)
143625864Smatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
3343625864Smatthias.ringwald  *  l2cap.c
3443625864Smatthias.ringwald  *
3543625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
3643625864Smatthias.ringwald  *
3743625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
3843625864Smatthias.ringwald  */
3943625864Smatthias.ringwald 
4043625864Smatthias.ringwald #include "l2cap.h"
41645658c9Smatthias.ringwald #include "hci.h"
422b3c6c9bSmatthias.ringwald #include "hci_dump.h"
436218e6f1Smatthias.ringwald #include "debug.h"
4443625864Smatthias.ringwald 
4543625864Smatthias.ringwald #include <stdarg.h>
4643625864Smatthias.ringwald #include <string.h>
4743625864Smatthias.ringwald 
4843625864Smatthias.ringwald #include <stdio.h>
4943625864Smatthias.ringwald 
506f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
516f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
526f60b3f4Smatthias.ringwald 
532784b77dSmatthias.ringwald // minimum signaling MTU
542784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48
551dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672
562784b77dSmatthias.ringwald 
574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
594c744e21Smatthias.ringwald 
6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
6139bda6d5Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 10
6239bda6d5Smatthias.ringwald 
6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6800d93d79Smatthias.ringwald 
6939bda6d5Smatthias.ringwald typedef enum {
7039bda6d5Smatthias.ringwald     L2CAP_STATE_CLOSED = 1,           // no baseband
7139bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
7239bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
7339bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
7439bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONNECT_RSP, // from peer
75fa8473a4Smatthias.ringwald     L2CAP_STATE_CONFIG,
7639bda6d5Smatthias.ringwald     L2CAP_STATE_OPEN,
7739bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_DISCONNECT,  // from application
7839bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
7939bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
8039bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
8139bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
8239bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
8339bda6d5Smatthias.ringwald } L2CAP_STATE;
84fcadd0caSmatthias.ringwald 
85fa8473a4Smatthias.ringwald typedef enum {
867d3b3569Smatthias.ringwald     STATE_VAR_NONE          = 0,
87fa8473a4Smatthias.ringwald     STATE_VAR_RCVD_CONF_REQ = 1 << 0,
88fa8473a4Smatthias.ringwald     STATE_VAR_RCVD_CONF_RSP = 1 << 1,
89fa8473a4Smatthias.ringwald     STATE_VAR_SEND_CONF_REQ = 1 << 2,
90fa8473a4Smatthias.ringwald     STATE_VAR_SEND_CONF_RSP = 1 << 3,
91fa8473a4Smatthias.ringwald     STATE_VAR_SENT_CONF_REQ = 1 << 4,
92fa8473a4Smatthias.ringwald     STATE_VAR_SENT_CONF_RSP = 1 << 5,
93fa8473a4Smatthias.ringwald } L2CAP_CHANNEL_STATE_VAR;
94fa8473a4Smatthias.ringwald 
9539bda6d5Smatthias.ringwald // info regarding an actual coneection
9639bda6d5Smatthias.ringwald typedef struct {
9739bda6d5Smatthias.ringwald     // linked list - assert: first field
9839bda6d5Smatthias.ringwald     linked_item_t    item;
9939bda6d5Smatthias.ringwald 
10039bda6d5Smatthias.ringwald     L2CAP_STATE state;
101fa8473a4Smatthias.ringwald     L2CAP_CHANNEL_STATE_VAR state_var;
10239bda6d5Smatthias.ringwald 
10339bda6d5Smatthias.ringwald     bd_addr_t address;
10439bda6d5Smatthias.ringwald     hci_con_handle_t handle;
10539bda6d5Smatthias.ringwald 
10639bda6d5Smatthias.ringwald     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
10739bda6d5Smatthias.ringwald     uint8_t   local_sig_id;     // own signaling identifier
10839bda6d5Smatthias.ringwald 
10939bda6d5Smatthias.ringwald     uint16_t  local_cid;
11039bda6d5Smatthias.ringwald     uint16_t  remote_cid;
11139bda6d5Smatthias.ringwald 
11239bda6d5Smatthias.ringwald     uint16_t  local_mtu;
11339bda6d5Smatthias.ringwald     uint16_t  remote_mtu;
11439bda6d5Smatthias.ringwald 
11539bda6d5Smatthias.ringwald     uint16_t  psm;
11639bda6d5Smatthias.ringwald 
11739bda6d5Smatthias.ringwald     uint8_t   packets_granted;    // number of L2CAP/ACL packets client is allowed to send
11839bda6d5Smatthias.ringwald 
11939bda6d5Smatthias.ringwald     uint8_t   reason; // used in decline internal
12039bda6d5Smatthias.ringwald 
12139bda6d5Smatthias.ringwald     // client connection
12239bda6d5Smatthias.ringwald     void * connection;
12339bda6d5Smatthias.ringwald 
12439bda6d5Smatthias.ringwald     // internal connection
12539bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
12639bda6d5Smatthias.ringwald 
12739bda6d5Smatthias.ringwald } l2cap_channel_t;
12839bda6d5Smatthias.ringwald 
12939bda6d5Smatthias.ringwald // info regarding potential connections
13039bda6d5Smatthias.ringwald typedef struct {
13139bda6d5Smatthias.ringwald     // linked list - assert: first field
13239bda6d5Smatthias.ringwald     linked_item_t    item;
13339bda6d5Smatthias.ringwald 
13439bda6d5Smatthias.ringwald     // service id
13539bda6d5Smatthias.ringwald     uint16_t  psm;
13639bda6d5Smatthias.ringwald 
13739bda6d5Smatthias.ringwald     // incoming MTU
13839bda6d5Smatthias.ringwald     uint16_t mtu;
13939bda6d5Smatthias.ringwald 
14039bda6d5Smatthias.ringwald     // client connection
14139bda6d5Smatthias.ringwald     void *connection;
14239bda6d5Smatthias.ringwald 
14339bda6d5Smatthias.ringwald     // internal connection
14439bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
14539bda6d5Smatthias.ringwald 
14639bda6d5Smatthias.ringwald } l2cap_service_t;
14739bda6d5Smatthias.ringwald 
14839bda6d5Smatthias.ringwald 
1492b83fb7dSmatthias.ringwald typedef struct l2cap_signaling_response {
1502b83fb7dSmatthias.ringwald     hci_con_handle_t handle;
1512b83fb7dSmatthias.ringwald     uint8_t  sig_id;
1522b83fb7dSmatthias.ringwald     uint8_t  code;
1532b83fb7dSmatthias.ringwald     uint16_t infoType;
1542b83fb7dSmatthias.ringwald } l2cap_signaling_response_t;
15539bda6d5Smatthias.ringwald 
15639bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
15739bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
15839bda6d5Smatthias.ringwald 
15939bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1602b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1612b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1622b83fb7dSmatthias.ringwald 
1631e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
1641e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
1659d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
1661e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
16736944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
168808a48abSmatthias.ringwald static int new_credits_blocked = 0;
1691e6aba47Smatthias.ringwald 
17039bda6d5Smatthias.ringwald 
17139bda6d5Smatthias.ringwald // prototypes
172fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
173fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
174fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
175fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
176fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
177fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
17839bda6d5Smatthias.ringwald 
17939bda6d5Smatthias.ringwald 
1801e6aba47Smatthias.ringwald void l2cap_init(){
1812784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
18211c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
183fcadd0caSmatthias.ringwald 
184808a48abSmatthias.ringwald     new_credits_blocked = 0;
1852b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
186808a48abSmatthias.ringwald 
187fcadd0caSmatthias.ringwald     //
1882718e2e7Smatthias.ringwald     // register callback with HCI
189fcadd0caSmatthias.ringwald     //
1902718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
191fcadd0caSmatthias.ringwald }
192fcadd0caSmatthias.ringwald 
193fcadd0caSmatthias.ringwald 
194fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
19536944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
196fcadd0caSmatthias.ringwald }
19736944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
198b502e1b0Smatthias.ringwald     packet_handler = handler;
1991e6aba47Smatthias.ringwald }
2001e6aba47Smatthias.ringwald 
20158de5610Smatthias.ringwald //  notify client/protocol handler
20258de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
20358de5610Smatthias.ringwald     if (channel->packet_handler) {
20458de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
20558de5610Smatthias.ringwald     } else {
20636944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
20758de5610Smatthias.ringwald     }
20858de5610Smatthias.ringwald }
20958de5610Smatthias.ringwald 
21058de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
2119893b714Smatthias.ringwald     uint8_t event[21];
21258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
21358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
21458de5610Smatthias.ringwald     event[2] = status;
21558de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
21658de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
21758de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
21858de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
21958de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
2204c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
2214c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
22258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22358de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
22458de5610Smatthias.ringwald }
22558de5610Smatthias.ringwald 
22658de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
22758de5610Smatthias.ringwald     uint8_t event[4];
22858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
22958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
23058de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
23158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
23358de5610Smatthias.ringwald }
23458de5610Smatthias.ringwald 
23558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
23658de5610Smatthias.ringwald     uint8_t event[16];
23758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
23858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
23958de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
24058de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
24158de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
24258de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
24358de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
24458de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24558de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2460af41d30Smatthias.ringwald }
247808a48abSmatthias.ringwald 
2486218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
2496218e6f1Smatthias.ringwald     // track credits
2506218e6f1Smatthias.ringwald     channel->packets_granted += credits;
251808a48abSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
2526218e6f1Smatthias.ringwald 
2536218e6f1Smatthias.ringwald     uint8_t event[5];
2546218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
2556218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
2566218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
2576218e6f1Smatthias.ringwald     event[4] = credits;
2586218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
2596218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2606218e6f1Smatthias.ringwald }
2616218e6f1Smatthias.ringwald 
262808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
263808a48abSmatthias.ringwald     new_credits_blocked = blocked;
264808a48abSmatthias.ringwald }
265808a48abSmatthias.ringwald 
26640d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
267808a48abSmatthias.ringwald 
268808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
269808a48abSmatthias.ringwald 
2708d371091Smatthias.ringwald     linked_item_t *it;
2718d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2728d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2738d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2740e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2754c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2768d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2778d371091Smatthias.ringwald         }
2788d371091Smatthias.ringwald     }
2798d371091Smatthias.ringwald }
2808d371091Smatthias.ringwald 
281b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
282f62db1e3Smatthias.ringwald     linked_item_t *it;
283f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2848d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
285b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
286f62db1e3Smatthias.ringwald             return channel;
287f62db1e3Smatthias.ringwald         }
288f62db1e3Smatthias.ringwald     }
289f62db1e3Smatthias.ringwald     return NULL;
290f62db1e3Smatthias.ringwald }
291f62db1e3Smatthias.ringwald 
2926b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2936b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2946b1fde37Smatthias.ringwald     if (!channel) return 0;
2956b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2966b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2976b1fde37Smatthias.ringwald }
2986b1fde37Smatthias.ringwald 
29996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
30096cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
30196cbd662Smatthias.ringwald     if (channel) {
30296cbd662Smatthias.ringwald         return channel->remote_mtu;
30396cbd662Smatthias.ringwald     }
30496cbd662Smatthias.ringwald     return 0;
30596cbd662Smatthias.ringwald }
30696cbd662Smatthias.ringwald 
30758de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
308fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
30958de5610Smatthias.ringwald     va_list argptr;
31058de5610Smatthias.ringwald     va_start(argptr, identifier);
31158de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
31258de5610Smatthias.ringwald     va_end(argptr);
313808a48abSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet con %u!\n", handle);
31458de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
31558de5610Smatthias.ringwald }
31658de5610Smatthias.ringwald 
3176218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
3186218e6f1Smatthias.ringwald 
3196218e6f1Smatthias.ringwald     // check for free places on BT module
3208ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
3218ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
322808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3238ea03fa5Smatthias.ringwald     }
3246218e6f1Smatthias.ringwald     int err = 0;
3256218e6f1Smatthias.ringwald 
32658de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
32758de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
32858de5610Smatthias.ringwald     if (channel) {
3296218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
3306218e6f1Smatthias.ringwald             --channel->packets_granted;
331808a48abSmatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
332808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
3336218e6f1Smatthias.ringwald         } else {
3346218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
3356218e6f1Smatthias.ringwald         }
3366218e6f1Smatthias.ringwald 
33758de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
33858de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
33958de5610Smatthias.ringwald         // 2 - ACL length
34058de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
34158de5610Smatthias.ringwald         // 4 - L2CAP packet length
34258de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
34358de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
34458de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
34558de5610Smatthias.ringwald         // 8 - data
34658de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
3476218e6f1Smatthias.ringwald 
34858de5610Smatthias.ringwald         // send
3496218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
35058de5610Smatthias.ringwald     }
35191b99603Smatthias.ringwald 
35291b99603Smatthias.ringwald     l2cap_hand_out_credits();
35391b99603Smatthias.ringwald 
3546218e6f1Smatthias.ringwald     return err;
35558de5610Smatthias.ringwald }
35658de5610Smatthias.ringwald 
3578158c421Smatthias.ringwald // MARK: L2CAP_RUN
3582cd0be45Smatthias.ringwald // process outstanding signaling tasks
3592cd0be45Smatthias.ringwald void l2cap_run(void){
3602b83fb7dSmatthias.ringwald 
3612b83fb7dSmatthias.ringwald     // check pending signaling responses
3622b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3632b83fb7dSmatthias.ringwald 
36402b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3652b83fb7dSmatthias.ringwald 
3662b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3672b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3682b83fb7dSmatthias.ringwald         uint8_t infoType = signaling_responses[0].infoType;
3692b83fb7dSmatthias.ringwald 
3702b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3712b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3722b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3732b83fb7dSmatthias.ringwald                 break;
3742b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3752b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3762b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3772b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3782b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3792b83fb7dSmatthias.ringwald                 } else {
3802b83fb7dSmatthias.ringwald                     // all other types are not supported
3812b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3822b83fb7dSmatthias.ringwald                 }
3832b83fb7dSmatthias.ringwald                 break;
3842b83fb7dSmatthias.ringwald             default:
3852b83fb7dSmatthias.ringwald                 // should not happen
3862b83fb7dSmatthias.ringwald                 break;
3872b83fb7dSmatthias.ringwald         }
3882b83fb7dSmatthias.ringwald 
3892b83fb7dSmatthias.ringwald         // remove first item
3902b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3912b83fb7dSmatthias.ringwald         int i;
3922b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
3932b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
3942b83fb7dSmatthias.ringwald         }
3952b83fb7dSmatthias.ringwald     }
3962b83fb7dSmatthias.ringwald 
397ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3982cd0be45Smatthias.ringwald     linked_item_t *it;
3992cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
4002cd0be45Smatthias.ringwald 
40102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
40202b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4032cd0be45Smatthias.ringwald 
4042cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4056e6710ebSmatthias.ringwald 
40616acce23Smatthias.ringwald         // log_dbg("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4076e6710ebSmatthias.ringwald 
4082cd0be45Smatthias.ringwald         switch (channel->state){
4092cd0be45Smatthias.ringwald 
41002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
41164472d52Smatthias.ringwald                 // send connection request - set state first
41264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
41302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
41402b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
41502b22dc4Smatthias.ringwald                 break;
41602b22dc4Smatthias.ringwald 
417e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
418b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
419e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
420e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
421e7ff783cSmatthias.ringwald                 free (channel);
422e7ff783cSmatthias.ringwald                 break;
423e7ff783cSmatthias.ringwald 
424552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
425fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
426fa8473a4Smatthias.ringwald                 channel->state_var |= STATE_VAR_SEND_CONF_REQ;
4272a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
428552d92a1Smatthias.ringwald                 break;
429552d92a1Smatthias.ringwald 
4306fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4316fdcc387Smatthias.ringwald                 // success, start l2cap handshake
432b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4336fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
4342a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4356fdcc387Smatthias.ringwald                 break;
4366fdcc387Smatthias.ringwald 
437fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
438fa8473a4Smatthias.ringwald                 if (channel->state_var & STATE_VAR_SEND_CONF_RSP){
439fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_RSP;
440fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_RSP;
441fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
442fa8473a4Smatthias.ringwald                 }
443fa8473a4Smatthias.ringwald                 else if (channel->state_var & STATE_VAR_SEND_CONF_REQ){
444fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_REQ;
44583f196b3Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_REQ;
446b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
447ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
448ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
449ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
450b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
451fa8473a4Smatthias.ringwald                 }
452fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
453552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
454552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
455552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
456fa8473a4Smatthias.ringwald                 }
457552d92a1Smatthias.ringwald                 break;
458552d92a1Smatthias.ringwald 
459e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
460b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
461e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
462e7ff783cSmatthias.ringwald                 break;
463e7ff783cSmatthias.ringwald 
464e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
465b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4662cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4672a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4682cd0be45Smatthias.ringwald                 break;
4692cd0be45Smatthias.ringwald             default:
4702cd0be45Smatthias.ringwald                 break;
4712cd0be45Smatthias.ringwald         }
4722cd0be45Smatthias.ringwald     }
4732cd0be45Smatthias.ringwald }
4742cd0be45Smatthias.ringwald 
475fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
476fa8c92f6Smatthias.ringwald     return hci_max_acl_data_packet_length()-4;  // 4 bytes for L2CAP header
477fa8c92f6Smatthias.ringwald }
478fa8c92f6Smatthias.ringwald 
4791e6aba47Smatthias.ringwald // open outgoing L2CAP channel
48015470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
48115470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4821e6aba47Smatthias.ringwald 
4831e6aba47Smatthias.ringwald     // alloc structure
4841e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
4851e6aba47Smatthias.ringwald     // TODO: emit error event
4861e6aba47Smatthias.ringwald     if (!chan) return;
4871e6aba47Smatthias.ringwald 
4881d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
489fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
490fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4919775e25bSmatthias.ringwald     }
4929775e25bSmatthias.ringwald 
4931e6aba47Smatthias.ringwald     // fill in
4941e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4951e6aba47Smatthias.ringwald     chan->psm = psm;
4961e6aba47Smatthias.ringwald     chan->handle = 0;
4971e6aba47Smatthias.ringwald     chan->connection = connection;
4986b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4992784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
50015470d27Smatthias.ringwald     chan->local_mtu = mtu;
5016218e6f1Smatthias.ringwald     chan->packets_granted = 0;
5026218e6f1Smatthias.ringwald 
5031e6aba47Smatthias.ringwald     // set initial state
50402b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
5057d3b3569Smatthias.ringwald     chan->state_var = STATE_VAR_NONE;
506b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
507b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5081e6aba47Smatthias.ringwald 
5091e6aba47Smatthias.ringwald     // add to connections list
5101e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5111e6aba47Smatthias.ringwald 
51202b22dc4Smatthias.ringwald     l2cap_run();
51343625864Smatthias.ringwald }
51443625864Smatthias.ringwald 
515b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
516b35f641cSmatthias.ringwald     // find channel for local_cid
517b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
518f62db1e3Smatthias.ringwald     if (channel) {
519e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
520f62db1e3Smatthias.ringwald     }
5212cd0be45Smatthias.ringwald     // process
5222cd0be45Smatthias.ringwald     l2cap_run();
52343625864Smatthias.ringwald }
5241e6aba47Smatthias.ringwald 
525afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
52615ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
52715ec09bbSmatthias.ringwald     while (it->next){
52815ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
529b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
53002b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
531afde0c52Smatthias.ringwald                 // failure, forward error code
532afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
533afde0c52Smatthias.ringwald                 // discard channel
53415ec09bbSmatthias.ringwald                 it->next = it->next->next;
535afde0c52Smatthias.ringwald                 free (channel);
536afde0c52Smatthias.ringwald             }
53715ec09bbSmatthias.ringwald         } else {
53815ec09bbSmatthias.ringwald             it = it->next;
539afde0c52Smatthias.ringwald         }
540afde0c52Smatthias.ringwald     }
541afde0c52Smatthias.ringwald }
542afde0c52Smatthias.ringwald 
543afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
544afde0c52Smatthias.ringwald     linked_item_t *it;
545afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
546afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
547afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
54802b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
549b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5506fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
551afde0c52Smatthias.ringwald                 channel->handle = handle;
552b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
553afde0c52Smatthias.ringwald             }
554afde0c52Smatthias.ringwald         }
555afde0c52Smatthias.ringwald     }
5566fdcc387Smatthias.ringwald     // process
5576fdcc387Smatthias.ringwald     l2cap_run();
558afde0c52Smatthias.ringwald }
559b448a0e7Smatthias.ringwald 
560afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
561afde0c52Smatthias.ringwald 
562afde0c52Smatthias.ringwald     bd_addr_t address;
563afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5646e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
56536944dffSmatthias.ringwald     linked_item_t *it;
566*2d00edd4Smatthias.ringwald     int hci_con_used;
567afde0c52Smatthias.ringwald 
568afde0c52Smatthias.ringwald     switch(packet[0]){
569afde0c52Smatthias.ringwald 
570afde0c52Smatthias.ringwald         // handle connection complete events
571afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
572afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
573afde0c52Smatthias.ringwald             if (packet[2] == 0){
574afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
575afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
576afde0c52Smatthias.ringwald             } else {
577afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
578afde0c52Smatthias.ringwald             }
579afde0c52Smatthias.ringwald             break;
580afde0c52Smatthias.ringwald 
581afde0c52Smatthias.ringwald         // handle successful create connection cancel command
582afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
583afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
584afde0c52Smatthias.ringwald                 if (packet[5] == 0){
585afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
586afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
587afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
58803cfbabcSmatthias.ringwald                 }
5891e6aba47Smatthias.ringwald             }
59039d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
59139d59809Smatthias.ringwald             break;
59239d59809Smatthias.ringwald 
59339d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
59439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
595afde0c52Smatthias.ringwald             break;
59627a923d0Smatthias.ringwald 
5971e6aba47Smatthias.ringwald         // handle disconnection complete events
598afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
59927a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
600afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
60115ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
60215ec09bbSmatthias.ringwald             while (it->next){
60339ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
60427a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
60515ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6062060cf14Smatthias.ringwald                     it->next = it->next->next;
60715ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
60815ec09bbSmatthias.ringwald                     free (channel);
60915ec09bbSmatthias.ringwald                 } else {
61015ec09bbSmatthias.ringwald                     it = it->next;
61127a923d0Smatthias.ringwald                 }
61227a923d0Smatthias.ringwald             }
613afde0c52Smatthias.ringwald             break;
614fcadd0caSmatthias.ringwald 
6156218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
61602b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6178d371091Smatthias.ringwald             l2cap_hand_out_credits();
6186218e6f1Smatthias.ringwald             break;
6196218e6f1Smatthias.ringwald 
620ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
621afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
62236944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
62380ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
624*2d00edd4Smatthias.ringwald             hci_con_used = 0;
625ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
626ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
627ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
628*2d00edd4Smatthias.ringwald                     hci_con_used = 1;
629ee091cf1Smatthias.ringwald                 }
630ee091cf1Smatthias.ringwald             }
631*2d00edd4Smatthias.ringwald             if (hci_con_used) break;
63232ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6339edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
634afde0c52Smatthias.ringwald             break;
635ee091cf1Smatthias.ringwald 
6366e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
6376e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
6386e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
6396e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
6406e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
6416e6710ebSmatthias.ringwald                 }
6426e6710ebSmatthias.ringwald             }
6436e6710ebSmatthias.ringwald             break;
6446e6710ebSmatthias.ringwald 
645afde0c52Smatthias.ringwald         default:
646afde0c52Smatthias.ringwald             break;
647afde0c52Smatthias.ringwald     }
648afde0c52Smatthias.ringwald 
649afde0c52Smatthias.ringwald     // pass on
650b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6511e6aba47Smatthias.ringwald }
6521e6aba47Smatthias.ringwald 
653afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
654b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
655e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
656e7ff783cSmatthias.ringwald     l2cap_run();
65784836b65Smatthias.ringwald }
65884836b65Smatthias.ringwald 
659b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
660645658c9Smatthias.ringwald 
661fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
662645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
663645658c9Smatthias.ringwald     if (!service) {
664645658c9Smatthias.ringwald         // 0x0002 PSM not supported
665fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
666645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
667645658c9Smatthias.ringwald         return;
668645658c9Smatthias.ringwald     }
669645658c9Smatthias.ringwald 
670645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
671645658c9Smatthias.ringwald     if (!hci_connection) {
672fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
673645658c9Smatthias.ringwald         // TODO: emit error
674645658c9Smatthias.ringwald         return;
675645658c9Smatthias.ringwald     }
676645658c9Smatthias.ringwald     // alloc structure
677fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
678169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
679645658c9Smatthias.ringwald     // TODO: emit error event
680169f8b28Smatthias.ringwald     if (!channel) return;
681645658c9Smatthias.ringwald 
682645658c9Smatthias.ringwald     // fill in
683169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
684169f8b28Smatthias.ringwald     channel->psm = psm;
685169f8b28Smatthias.ringwald     channel->handle = handle;
686169f8b28Smatthias.ringwald     channel->connection = service->connection;
687f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
688b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
689b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
690fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6910a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
692761b0451Smatthias.ringwald     channel->packets_granted = 0;
693b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
694645658c9Smatthias.ringwald 
6951d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
696fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
697fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6989775e25bSmatthias.ringwald     }
6999775e25bSmatthias.ringwald 
700645658c9Smatthias.ringwald     // set initial state
701e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
7027d3b3569Smatthias.ringwald     channel->state_var = STATE_VAR_NONE;
703e405ae81Smatthias.ringwald 
704645658c9Smatthias.ringwald     // add to connections list
705169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
706645658c9Smatthias.ringwald 
707e405ae81Smatthias.ringwald     // emit incoming connection request
708e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
709e405ae81Smatthias.ringwald }
710645658c9Smatthias.ringwald 
711b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
712b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
713e405ae81Smatthias.ringwald     if (!channel) {
714fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
715e405ae81Smatthias.ringwald         return;
716e405ae81Smatthias.ringwald     }
717e405ae81Smatthias.ringwald 
718552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
719e405ae81Smatthias.ringwald 
720552d92a1Smatthias.ringwald     // process
721552d92a1Smatthias.ringwald     l2cap_run();
722e405ae81Smatthias.ringwald }
723645658c9Smatthias.ringwald 
724b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
725b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
726e405ae81Smatthias.ringwald     if (!channel) {
727fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
728e405ae81Smatthias.ringwald         return;
729e405ae81Smatthias.ringwald     }
730e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
731e7ff783cSmatthias.ringwald     channel->reason = reason;
732e7ff783cSmatthias.ringwald     l2cap_run();
733645658c9Smatthias.ringwald }
734645658c9Smatthias.ringwald 
7352784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
736b1988dceSmatthias.ringwald 
737b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
738b1988dceSmatthias.ringwald 
7392784b77dSmatthias.ringwald     // accept the other's configuration options
7403de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7413de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7423de7c0caSmatthias.ringwald     while (pos < end_pos){
7431dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7441dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7451dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7461dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7471dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
748fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7491dc511deSmatthias.ringwald         }
7501dc511deSmatthias.ringwald         pos += length;
7511dc511deSmatthias.ringwald     }
7522784b77dSmatthias.ringwald }
7532784b77dSmatthias.ringwald 
754fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
75583f196b3Smatthias.ringwald     // log_dbg("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
756fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
757fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_SENT_CONF_RSP) == 0) return 0;
758fa8473a4Smatthias.ringwald     return 1;
759fa8473a4Smatthias.ringwald }
760fa8473a4Smatthias.ringwald 
761fa8473a4Smatthias.ringwald 
76200d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7631e6aba47Smatthias.ringwald 
76400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
76500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
76638e5900eSmatthias.ringwald     uint16_t result = 0;
7671e6aba47Smatthias.ringwald 
7686e6710ebSmatthias.ringwald     log_dbg("signaling handler code %u, state %u\n", code, channel->state);
769b35f641cSmatthias.ringwald 
7709a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7719a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7729a011532Smatthias.ringwald         switch (channel->state){
773fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7749a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7752b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7769a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7779a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7789a011532Smatthias.ringwald                 break;
7799a011532Smatthias.ringwald 
7809a011532Smatthias.ringwald             default:
7819a011532Smatthias.ringwald                 // ignore in other states
7829a011532Smatthias.ringwald                 break;
7839a011532Smatthias.ringwald         }
7849a011532Smatthias.ringwald         return;
7859a011532Smatthias.ringwald     }
7869a011532Smatthias.ringwald 
7871e6aba47Smatthias.ringwald     switch (channel->state) {
7881e6aba47Smatthias.ringwald 
7891e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7901e6aba47Smatthias.ringwald             switch (code){
7911e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
79200d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
79338e5900eSmatthias.ringwald                     switch (result) {
79438e5900eSmatthias.ringwald                         case 0:
795169f8b28Smatthias.ringwald                             // successful connection
79600d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
797fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
798fa8473a4Smatthias.ringwald                             channel->state_var |= STATE_VAR_SEND_CONF_REQ;
79938e5900eSmatthias.ringwald                             break;
80038e5900eSmatthias.ringwald                         case 1:
80138e5900eSmatthias.ringwald                             // connection pending. get some coffee
80238e5900eSmatthias.ringwald                             break;
80338e5900eSmatthias.ringwald                         default:
804eb920dbeSmatthias.ringwald                             // channel closed
805eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
806eb920dbeSmatthias.ringwald 
807f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
80838e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
809eb920dbeSmatthias.ringwald 
810eb920dbeSmatthias.ringwald                             // drop link key if security block
811eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
812eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
813eb920dbeSmatthias.ringwald                             }
814eb920dbeSmatthias.ringwald 
815eb920dbeSmatthias.ringwald                             // discard channel
816eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
817eb920dbeSmatthias.ringwald                             free (channel);
81838e5900eSmatthias.ringwald                             break;
8191e6aba47Smatthias.ringwald                     }
8201e6aba47Smatthias.ringwald                     break;
82138e5900eSmatthias.ringwald 
82238e5900eSmatthias.ringwald                 default:
8231e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
82438e5900eSmatthias.ringwald                     break;
8251e6aba47Smatthias.ringwald             }
8261e6aba47Smatthias.ringwald             break;
8271e6aba47Smatthias.ringwald 
828fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
829ae280e73Smatthias.ringwald             switch (code) {
830ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
831fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_REQ;
832fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SEND_CONF_RSP;
833ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
834ae280e73Smatthias.ringwald                     break;
8351e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
836fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_RSP;
8375a67bd4aSmatthias.ringwald                     break;
8385a67bd4aSmatthias.ringwald                 default:
8395a67bd4aSmatthias.ringwald                     break;
8401e6aba47Smatthias.ringwald             }
841fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
842fa8473a4Smatthias.ringwald                 // for open:
8435a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
844fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8456218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
846c8e4258aSmatthias.ringwald             }
847c8e4258aSmatthias.ringwald             break;
848f62db1e3Smatthias.ringwald 
849f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
850f62db1e3Smatthias.ringwald             switch (code) {
851f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
85227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
85327a923d0Smatthias.ringwald                     break;
8545a67bd4aSmatthias.ringwald                 default:
8555a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8565a67bd4aSmatthias.ringwald                     break;
85727a923d0Smatthias.ringwald             }
85827a923d0Smatthias.ringwald             break;
85984836b65Smatthias.ringwald 
86084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
86184836b65Smatthias.ringwald             // @TODO handle incoming requests
86284836b65Smatthias.ringwald             break;
86384836b65Smatthias.ringwald 
86484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
86584836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
86684836b65Smatthias.ringwald             break;
86710642e45Smatthias.ringwald         default:
86810642e45Smatthias.ringwald             break;
86927a923d0Smatthias.ringwald     }
870fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
87127a923d0Smatthias.ringwald }
87227a923d0Smatthias.ringwald 
87300d93d79Smatthias.ringwald 
87400d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
87500d93d79Smatthias.ringwald 
87600d93d79Smatthias.ringwald     // get code, signalind identifier and command len
87700d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
87800d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
87900d93d79Smatthias.ringwald 
88000d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
88100d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
88200d93d79Smatthias.ringwald         return;
88300d93d79Smatthias.ringwald     }
88400d93d79Smatthias.ringwald 
88500d93d79Smatthias.ringwald     // general commands without an assigned channel
88600d93d79Smatthias.ringwald     switch(code) {
88700d93d79Smatthias.ringwald 
88800d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
88900d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
89000d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
89100d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8922b83fb7dSmatthias.ringwald             return;
89300d93d79Smatthias.ringwald         }
89400d93d79Smatthias.ringwald 
89500d93d79Smatthias.ringwald         case ECHO_REQUEST: {
8962b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
8972b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
8982b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
8992b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
9002b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
9012b83fb7dSmatthias.ringwald             }
9022b83fb7dSmatthias.ringwald             l2cap_run();
9032b83fb7dSmatthias.ringwald             return;
90400d93d79Smatthias.ringwald         }
90500d93d79Smatthias.ringwald 
90600d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
9072b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
90800d93d79Smatthias.ringwald                 // we neither support connectionless L2CAP data nor support any flow control modes yet
90900d93d79Smatthias.ringwald                 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
9102b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
9112b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
9122b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
9132b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].infoType = infoType;
9142b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
91500d93d79Smatthias.ringwald             }
9162b83fb7dSmatthias.ringwald             l2cap_run();
9172b83fb7dSmatthias.ringwald             return;
91800d93d79Smatthias.ringwald         }
91900d93d79Smatthias.ringwald 
92000d93d79Smatthias.ringwald         default:
92100d93d79Smatthias.ringwald             break;
92200d93d79Smatthias.ringwald     }
92300d93d79Smatthias.ringwald 
92400d93d79Smatthias.ringwald 
92500d93d79Smatthias.ringwald     // Get potential destination CID
92600d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
92700d93d79Smatthias.ringwald 
92800d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
92900d93d79Smatthias.ringwald     linked_item_t *it;
93000d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
93100d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
93200d93d79Smatthias.ringwald         if (channel->handle == handle) {
93300d93d79Smatthias.ringwald             if (code & 1) {
934b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
935b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
93600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9374e32727eSmatthias.ringwald                     break;
93800d93d79Smatthias.ringwald                 }
93900d93d79Smatthias.ringwald             } else {
940b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
94100d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
94200d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9434e32727eSmatthias.ringwald                     break;
94400d93d79Smatthias.ringwald                 }
94500d93d79Smatthias.ringwald             }
94600d93d79Smatthias.ringwald         }
94700d93d79Smatthias.ringwald     }
94800d93d79Smatthias.ringwald }
94900d93d79Smatthias.ringwald 
95000d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
95100d93d79Smatthias.ringwald 
95200d93d79Smatthias.ringwald     // Get Channel ID
95300d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
95400d93d79Smatthias.ringwald 
95500d93d79Smatthias.ringwald     // Signaling Packet?
95600d93d79Smatthias.ringwald     if (channel_id == 1) {
95700d93d79Smatthias.ringwald 
95800d93d79Smatthias.ringwald         // Get Connection
95900d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
96000d93d79Smatthias.ringwald 
96100d93d79Smatthias.ringwald         uint16_t command_offset = 8;
96200d93d79Smatthias.ringwald         while (command_offset < size) {
96300d93d79Smatthias.ringwald 
96400d93d79Smatthias.ringwald             // handle signaling commands
96500d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
96600d93d79Smatthias.ringwald 
96700d93d79Smatthias.ringwald             // increment command_offset
96800d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
96900d93d79Smatthias.ringwald         }
97064472d52Smatthias.ringwald 
97164472d52Smatthias.ringwald         l2cap_run();
97264472d52Smatthias.ringwald 
97300d93d79Smatthias.ringwald         return;
97400d93d79Smatthias.ringwald     }
97500d93d79Smatthias.ringwald 
97600d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
97700d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
97800d93d79Smatthias.ringwald     if (channel) {
97958de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
98000d93d79Smatthias.ringwald     }
98100d93d79Smatthias.ringwald }
98200d93d79Smatthias.ringwald 
9832718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9842718e2e7Smatthias.ringwald     switch (packet_type) {
9852718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9862718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9872718e2e7Smatthias.ringwald             break;
9882718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9892718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9902718e2e7Smatthias.ringwald             break;
9912718e2e7Smatthias.ringwald         default:
9922718e2e7Smatthias.ringwald             break;
9932718e2e7Smatthias.ringwald     }
9942718e2e7Smatthias.ringwald }
99500d93d79Smatthias.ringwald 
99615ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
99727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
998f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
999f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1000f62db1e3Smatthias.ringwald     // discard channel
1001f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1002f62db1e3Smatthias.ringwald     free (channel);
1003c8e4258aSmatthias.ringwald }
10041e6aba47Smatthias.ringwald 
10059d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1006c52bf64dSmatthias.ringwald     linked_item_t *it;
10079d9bbc01Smatthias.ringwald 
10089d9bbc01Smatthias.ringwald     // close open channels
10099d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
10109d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
10119d9bbc01Smatthias.ringwald         if ( service->psm == psm){
10129d9bbc01Smatthias.ringwald             return service;
10139d9bbc01Smatthias.ringwald         };
10149d9bbc01Smatthias.ringwald     }
10159d9bbc01Smatthias.ringwald     return NULL;
10169d9bbc01Smatthias.ringwald }
10179d9bbc01Smatthias.ringwald 
101836944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
10199d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
10209d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
10219d9bbc01Smatthias.ringwald     if (service) return;
10229d9bbc01Smatthias.ringwald 
10239d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
10249d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
10259d9bbc01Smatthias.ringwald     if (!service) return;
10269d9bbc01Smatthias.ringwald 
10279d9bbc01Smatthias.ringwald     // fill in
10289d9bbc01Smatthias.ringwald     service->psm = psm;
10299d9bbc01Smatthias.ringwald     service->mtu = mtu;
10309d9bbc01Smatthias.ringwald     service->connection = connection;
1031d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10329d9bbc01Smatthias.ringwald 
10339d9bbc01Smatthias.ringwald     // add to services list
10349d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
10359d9bbc01Smatthias.ringwald }
10369d9bbc01Smatthias.ringwald 
103736944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10389d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1039037d6e48Smatthias.ringwald     if (!service) return;
10409d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
10419d9bbc01Smatthias.ringwald     free(service);
10429d9bbc01Smatthias.ringwald }
10439d9bbc01Smatthias.ringwald 
10449d9bbc01Smatthias.ringwald //
104536944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10469d9bbc01Smatthias.ringwald     linked_item_t *it;
10479d9bbc01Smatthias.ringwald 
10483a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1049c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1050c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1051c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1052c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1053cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1054c52bf64dSmatthias.ringwald         }
1055c52bf64dSmatthias.ringwald     }
10569d9bbc01Smatthias.ringwald 
1057645658c9Smatthias.ringwald     // unregister services
105869025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
105969025de8Smatthias.ringwald     while (it->next) {
106069025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1061645658c9Smatthias.ringwald         if (service->connection == connection){
106269025de8Smatthias.ringwald             it->next = it->next->next;
10638149d2c7Smatthias.ringwald             free(service);
10648149d2c7Smatthias.ringwald         } else {
10658149d2c7Smatthias.ringwald             it = it->next;
1066645658c9Smatthias.ringwald         }
1067645658c9Smatthias.ringwald     }
1068cb8eb7e6Smatthias.ringwald 
1069cb8eb7e6Smatthias.ringwald     // process
1070cb8eb7e6Smatthias.ringwald     l2cap_run();
1071c52bf64dSmatthias.ringwald }
1072c52bf64dSmatthias.ringwald