xref: /btstack/src/l2cap.c (revision 16acce23c62fe579d4e4fc480e834136ccca4dcc)
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 {
86fa8473a4Smatthias.ringwald     STATE_VAR_RCVD_CONF_REQ = 1 << 0,
87fa8473a4Smatthias.ringwald     STATE_VAR_RCVD_CONF_RSP = 1 << 1,
88fa8473a4Smatthias.ringwald     STATE_VAR_SEND_CONF_REQ = 1 << 2,
89fa8473a4Smatthias.ringwald     STATE_VAR_SEND_CONF_RSP = 1 << 3,
90fa8473a4Smatthias.ringwald     STATE_VAR_SENT_CONF_REQ = 1 << 4,
91fa8473a4Smatthias.ringwald     STATE_VAR_SENT_CONF_RSP = 1 << 5,
92fa8473a4Smatthias.ringwald } L2CAP_CHANNEL_STATE_VAR;
93fa8473a4Smatthias.ringwald 
9439bda6d5Smatthias.ringwald // info regarding an actual coneection
9539bda6d5Smatthias.ringwald typedef struct {
9639bda6d5Smatthias.ringwald     // linked list - assert: first field
9739bda6d5Smatthias.ringwald     linked_item_t    item;
9839bda6d5Smatthias.ringwald 
9939bda6d5Smatthias.ringwald     L2CAP_STATE state;
100fa8473a4Smatthias.ringwald     L2CAP_CHANNEL_STATE_VAR state_var;
10139bda6d5Smatthias.ringwald 
10239bda6d5Smatthias.ringwald     bd_addr_t address;
10339bda6d5Smatthias.ringwald     hci_con_handle_t handle;
10439bda6d5Smatthias.ringwald 
10539bda6d5Smatthias.ringwald     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
10639bda6d5Smatthias.ringwald     uint8_t   local_sig_id;     // own signaling identifier
10739bda6d5Smatthias.ringwald 
10839bda6d5Smatthias.ringwald     uint16_t  local_cid;
10939bda6d5Smatthias.ringwald     uint16_t  remote_cid;
11039bda6d5Smatthias.ringwald 
11139bda6d5Smatthias.ringwald     uint16_t  local_mtu;
11239bda6d5Smatthias.ringwald     uint16_t  remote_mtu;
11339bda6d5Smatthias.ringwald 
11439bda6d5Smatthias.ringwald     uint16_t  psm;
11539bda6d5Smatthias.ringwald 
11639bda6d5Smatthias.ringwald     uint8_t   packets_granted;    // number of L2CAP/ACL packets client is allowed to send
11739bda6d5Smatthias.ringwald 
11839bda6d5Smatthias.ringwald     uint8_t   reason; // used in decline internal
11939bda6d5Smatthias.ringwald 
12039bda6d5Smatthias.ringwald     // client connection
12139bda6d5Smatthias.ringwald     void * connection;
12239bda6d5Smatthias.ringwald 
12339bda6d5Smatthias.ringwald     // internal connection
12439bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
12539bda6d5Smatthias.ringwald 
12639bda6d5Smatthias.ringwald } l2cap_channel_t;
12739bda6d5Smatthias.ringwald 
12839bda6d5Smatthias.ringwald // info regarding potential connections
12939bda6d5Smatthias.ringwald typedef struct {
13039bda6d5Smatthias.ringwald     // linked list - assert: first field
13139bda6d5Smatthias.ringwald     linked_item_t    item;
13239bda6d5Smatthias.ringwald 
13339bda6d5Smatthias.ringwald     // service id
13439bda6d5Smatthias.ringwald     uint16_t  psm;
13539bda6d5Smatthias.ringwald 
13639bda6d5Smatthias.ringwald     // incoming MTU
13739bda6d5Smatthias.ringwald     uint16_t mtu;
13839bda6d5Smatthias.ringwald 
13939bda6d5Smatthias.ringwald     // client connection
14039bda6d5Smatthias.ringwald     void *connection;
14139bda6d5Smatthias.ringwald 
14239bda6d5Smatthias.ringwald     // internal connection
14339bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
14439bda6d5Smatthias.ringwald 
14539bda6d5Smatthias.ringwald } l2cap_service_t;
14639bda6d5Smatthias.ringwald 
14739bda6d5Smatthias.ringwald 
1482b83fb7dSmatthias.ringwald typedef struct l2cap_signaling_response {
1492b83fb7dSmatthias.ringwald     hci_con_handle_t handle;
1502b83fb7dSmatthias.ringwald     uint8_t  sig_id;
1512b83fb7dSmatthias.ringwald     uint8_t  code;
1522b83fb7dSmatthias.ringwald     uint16_t infoType;
1532b83fb7dSmatthias.ringwald } l2cap_signaling_response_t;
15439bda6d5Smatthias.ringwald 
15539bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
15639bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
15739bda6d5Smatthias.ringwald 
15839bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1592b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1602b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1612b83fb7dSmatthias.ringwald 
1621e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
1631e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
1649d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
1651e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
16636944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
167808a48abSmatthias.ringwald static int new_credits_blocked = 0;
1681e6aba47Smatthias.ringwald 
16939bda6d5Smatthias.ringwald 
17039bda6d5Smatthias.ringwald // prototypes
171fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
172fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
173fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
174fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
175fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
176fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
17739bda6d5Smatthias.ringwald 
17839bda6d5Smatthias.ringwald 
1791e6aba47Smatthias.ringwald void l2cap_init(){
1802784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
18111c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
182fcadd0caSmatthias.ringwald 
183808a48abSmatthias.ringwald     new_credits_blocked = 0;
1842b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
185808a48abSmatthias.ringwald 
186fcadd0caSmatthias.ringwald     //
1872718e2e7Smatthias.ringwald     // register callback with HCI
188fcadd0caSmatthias.ringwald     //
1892718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
190fcadd0caSmatthias.ringwald }
191fcadd0caSmatthias.ringwald 
192fcadd0caSmatthias.ringwald 
193fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
19436944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
195fcadd0caSmatthias.ringwald }
19636944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
197b502e1b0Smatthias.ringwald     packet_handler = handler;
1981e6aba47Smatthias.ringwald }
1991e6aba47Smatthias.ringwald 
20058de5610Smatthias.ringwald //  notify client/protocol handler
20158de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
20258de5610Smatthias.ringwald     if (channel->packet_handler) {
20358de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
20458de5610Smatthias.ringwald     } else {
20536944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
20658de5610Smatthias.ringwald     }
20758de5610Smatthias.ringwald }
20858de5610Smatthias.ringwald 
20958de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
2109893b714Smatthias.ringwald     uint8_t event[21];
21158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
21258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
21358de5610Smatthias.ringwald     event[2] = status;
21458de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
21558de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
21658de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
21758de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
21858de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
2194c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
2204c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
22158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
22358de5610Smatthias.ringwald }
22458de5610Smatthias.ringwald 
22558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
22658de5610Smatthias.ringwald     uint8_t event[4];
22758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
22858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
22958de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
23058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23158de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
23258de5610Smatthias.ringwald }
23358de5610Smatthias.ringwald 
23458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
23558de5610Smatthias.ringwald     uint8_t event[16];
23658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
23758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
23858de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
23958de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
24058de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
24158de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
24258de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
24358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24458de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2450af41d30Smatthias.ringwald }
246808a48abSmatthias.ringwald 
2476218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
2486218e6f1Smatthias.ringwald     // track credits
2496218e6f1Smatthias.ringwald     channel->packets_granted += credits;
250808a48abSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
2516218e6f1Smatthias.ringwald 
2526218e6f1Smatthias.ringwald     uint8_t event[5];
2536218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
2546218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
2556218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
2566218e6f1Smatthias.ringwald     event[4] = credits;
2576218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
2586218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2596218e6f1Smatthias.ringwald }
2606218e6f1Smatthias.ringwald 
261808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
262808a48abSmatthias.ringwald     new_credits_blocked = blocked;
263808a48abSmatthias.ringwald }
264808a48abSmatthias.ringwald 
26540d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
266808a48abSmatthias.ringwald 
267808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
268808a48abSmatthias.ringwald 
2698d371091Smatthias.ringwald     linked_item_t *it;
2708d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2718d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2728d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2730e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2744c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2758d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2768d371091Smatthias.ringwald         }
2778d371091Smatthias.ringwald     }
2788d371091Smatthias.ringwald }
2798d371091Smatthias.ringwald 
280b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
281f62db1e3Smatthias.ringwald     linked_item_t *it;
282f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2838d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
284b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
285f62db1e3Smatthias.ringwald             return channel;
286f62db1e3Smatthias.ringwald         }
287f62db1e3Smatthias.ringwald     }
288f62db1e3Smatthias.ringwald     return NULL;
289f62db1e3Smatthias.ringwald }
290f62db1e3Smatthias.ringwald 
2916b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2926b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2936b1fde37Smatthias.ringwald     if (!channel) return 0;
2946b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2956b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2966b1fde37Smatthias.ringwald }
2976b1fde37Smatthias.ringwald 
29896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
29996cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
30096cbd662Smatthias.ringwald     if (channel) {
30196cbd662Smatthias.ringwald         return channel->remote_mtu;
30296cbd662Smatthias.ringwald     }
30396cbd662Smatthias.ringwald     return 0;
30496cbd662Smatthias.ringwald }
30596cbd662Smatthias.ringwald 
30658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
307fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
30858de5610Smatthias.ringwald     va_list argptr;
30958de5610Smatthias.ringwald     va_start(argptr, identifier);
31058de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
31158de5610Smatthias.ringwald     va_end(argptr);
312808a48abSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet con %u!\n", handle);
31358de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
31458de5610Smatthias.ringwald }
31558de5610Smatthias.ringwald 
3166218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
3176218e6f1Smatthias.ringwald 
3186218e6f1Smatthias.ringwald     // check for free places on BT module
3198ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
3208ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
321808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3228ea03fa5Smatthias.ringwald     }
3236218e6f1Smatthias.ringwald     int err = 0;
3246218e6f1Smatthias.ringwald 
32558de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
32658de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
32758de5610Smatthias.ringwald     if (channel) {
3286218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
3296218e6f1Smatthias.ringwald             --channel->packets_granted;
330808a48abSmatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
331808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
3326218e6f1Smatthias.ringwald         } else {
3336218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
3346218e6f1Smatthias.ringwald         }
3356218e6f1Smatthias.ringwald 
33658de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
33758de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
33858de5610Smatthias.ringwald         // 2 - ACL length
33958de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
34058de5610Smatthias.ringwald         // 4 - L2CAP packet length
34158de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
34258de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
34358de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
34458de5610Smatthias.ringwald         // 8 - data
34558de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
3466218e6f1Smatthias.ringwald 
34758de5610Smatthias.ringwald         // send
3486218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
34958de5610Smatthias.ringwald     }
35091b99603Smatthias.ringwald 
35191b99603Smatthias.ringwald     l2cap_hand_out_credits();
35291b99603Smatthias.ringwald 
3536218e6f1Smatthias.ringwald     return err;
35458de5610Smatthias.ringwald }
35558de5610Smatthias.ringwald 
3568158c421Smatthias.ringwald // MARK: L2CAP_RUN
3572cd0be45Smatthias.ringwald // process outstanding signaling tasks
3582cd0be45Smatthias.ringwald void l2cap_run(void){
3592b83fb7dSmatthias.ringwald 
3602b83fb7dSmatthias.ringwald     // check pending signaling responses
3612b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3622b83fb7dSmatthias.ringwald 
36302b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3642b83fb7dSmatthias.ringwald 
3652b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3662b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3672b83fb7dSmatthias.ringwald         uint8_t infoType = signaling_responses[0].infoType;
3682b83fb7dSmatthias.ringwald 
3692b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3702b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3712b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3722b83fb7dSmatthias.ringwald                 break;
3732b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3742b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3752b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3762b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3772b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3782b83fb7dSmatthias.ringwald                 } else {
3792b83fb7dSmatthias.ringwald                     // all other types are not supported
3802b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3812b83fb7dSmatthias.ringwald                 }
3822b83fb7dSmatthias.ringwald                 break;
3832b83fb7dSmatthias.ringwald             default:
3842b83fb7dSmatthias.ringwald                 // should not happen
3852b83fb7dSmatthias.ringwald                 break;
3862b83fb7dSmatthias.ringwald         }
3872b83fb7dSmatthias.ringwald 
3882b83fb7dSmatthias.ringwald         // remove first item
3892b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3902b83fb7dSmatthias.ringwald         int i;
3912b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
3922b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
3932b83fb7dSmatthias.ringwald         }
3942b83fb7dSmatthias.ringwald     }
3952b83fb7dSmatthias.ringwald 
396ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3972cd0be45Smatthias.ringwald     linked_item_t *it;
3982cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3992cd0be45Smatthias.ringwald 
40002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
40102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4022cd0be45Smatthias.ringwald 
4032cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4046e6710ebSmatthias.ringwald 
405*16acce23Smatthias.ringwald         // log_dbg("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4066e6710ebSmatthias.ringwald 
4072cd0be45Smatthias.ringwald         switch (channel->state){
4082cd0be45Smatthias.ringwald 
40902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
41064472d52Smatthias.ringwald                 // send connection request - set state first
41164472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
41202b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
41302b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
41402b22dc4Smatthias.ringwald                 break;
41502b22dc4Smatthias.ringwald 
416e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
417b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
418e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
419e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
420e7ff783cSmatthias.ringwald                 free (channel);
421e7ff783cSmatthias.ringwald                 break;
422e7ff783cSmatthias.ringwald 
423552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
424fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
425fa8473a4Smatthias.ringwald                 channel->state_var |= STATE_VAR_SEND_CONF_REQ;
4262a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
427552d92a1Smatthias.ringwald                 break;
428552d92a1Smatthias.ringwald 
4296fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4306fdcc387Smatthias.ringwald                 // success, start l2cap handshake
431b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4326fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
4332a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4346fdcc387Smatthias.ringwald                 break;
4356fdcc387Smatthias.ringwald 
436fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
437fa8473a4Smatthias.ringwald                 if (channel->state_var & STATE_VAR_SEND_CONF_RSP){
438fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_RSP;
439fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_RSP;
440fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
441fa8473a4Smatthias.ringwald                 }
442fa8473a4Smatthias.ringwald                 else if (channel->state_var & STATE_VAR_SEND_CONF_REQ){
443fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_REQ;
44483f196b3Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_REQ;
445b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
446ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
447ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
448ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
449b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
450fa8473a4Smatthias.ringwald                 }
451fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
452552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
453552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
454552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
455fa8473a4Smatthias.ringwald                 }
456552d92a1Smatthias.ringwald                 break;
457552d92a1Smatthias.ringwald 
458e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
459b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
460e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
461e7ff783cSmatthias.ringwald                 break;
462e7ff783cSmatthias.ringwald 
463e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
464b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4652cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4662a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4672cd0be45Smatthias.ringwald                 break;
4682cd0be45Smatthias.ringwald             default:
4692cd0be45Smatthias.ringwald                 break;
4702cd0be45Smatthias.ringwald         }
4712cd0be45Smatthias.ringwald     }
4722cd0be45Smatthias.ringwald }
4732cd0be45Smatthias.ringwald 
474fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
475fa8c92f6Smatthias.ringwald     return hci_max_acl_data_packet_length()-4;  // 4 bytes for L2CAP header
476fa8c92f6Smatthias.ringwald }
477fa8c92f6Smatthias.ringwald 
4781e6aba47Smatthias.ringwald // open outgoing L2CAP channel
47915470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
48015470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4811e6aba47Smatthias.ringwald 
4821e6aba47Smatthias.ringwald     // alloc structure
4831e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
4841e6aba47Smatthias.ringwald     // TODO: emit error event
4851e6aba47Smatthias.ringwald     if (!chan) return;
4861e6aba47Smatthias.ringwald 
4871d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
488fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
489fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4909775e25bSmatthias.ringwald     }
4919775e25bSmatthias.ringwald 
4921e6aba47Smatthias.ringwald     // fill in
4931e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4941e6aba47Smatthias.ringwald     chan->psm = psm;
4951e6aba47Smatthias.ringwald     chan->handle = 0;
4961e6aba47Smatthias.ringwald     chan->connection = connection;
4976b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4982784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
49915470d27Smatthias.ringwald     chan->local_mtu = mtu;
5006218e6f1Smatthias.ringwald     chan->packets_granted = 0;
5016218e6f1Smatthias.ringwald 
5021e6aba47Smatthias.ringwald     // set initial state
50302b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
504fa8473a4Smatthias.ringwald     chan->state_var = 0;
505b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
506b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5071e6aba47Smatthias.ringwald 
5081e6aba47Smatthias.ringwald     // add to connections list
5091e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5101e6aba47Smatthias.ringwald 
51102b22dc4Smatthias.ringwald     l2cap_run();
51243625864Smatthias.ringwald }
51343625864Smatthias.ringwald 
514b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
515b35f641cSmatthias.ringwald     // find channel for local_cid
516b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
517f62db1e3Smatthias.ringwald     if (channel) {
518e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
519f62db1e3Smatthias.ringwald     }
5202cd0be45Smatthias.ringwald     // process
5212cd0be45Smatthias.ringwald     l2cap_run();
52243625864Smatthias.ringwald }
5231e6aba47Smatthias.ringwald 
524afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
52515ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
52615ec09bbSmatthias.ringwald     while (it->next){
52715ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
528b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
52902b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
530afde0c52Smatthias.ringwald                 // failure, forward error code
531afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
532afde0c52Smatthias.ringwald                 // discard channel
53315ec09bbSmatthias.ringwald                 it->next = it->next->next;
534afde0c52Smatthias.ringwald                 free (channel);
535afde0c52Smatthias.ringwald             }
53615ec09bbSmatthias.ringwald         } else {
53715ec09bbSmatthias.ringwald             it = it->next;
538afde0c52Smatthias.ringwald         }
539afde0c52Smatthias.ringwald     }
540afde0c52Smatthias.ringwald }
541afde0c52Smatthias.ringwald 
542afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
543afde0c52Smatthias.ringwald     linked_item_t *it;
544afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
545afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
546afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
54702b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
548b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5496fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
550afde0c52Smatthias.ringwald                 channel->handle = handle;
551b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
552afde0c52Smatthias.ringwald             }
553afde0c52Smatthias.ringwald         }
554afde0c52Smatthias.ringwald     }
5556fdcc387Smatthias.ringwald     // process
5566fdcc387Smatthias.ringwald     l2cap_run();
557afde0c52Smatthias.ringwald }
558b448a0e7Smatthias.ringwald 
559afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
560afde0c52Smatthias.ringwald 
561afde0c52Smatthias.ringwald     bd_addr_t address;
562afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5636e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
56436944dffSmatthias.ringwald     linked_item_t *it;
565afde0c52Smatthias.ringwald 
566afde0c52Smatthias.ringwald     switch(packet[0]){
567afde0c52Smatthias.ringwald 
568afde0c52Smatthias.ringwald         // handle connection complete events
569afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
570afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
571afde0c52Smatthias.ringwald             if (packet[2] == 0){
572afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
573afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
574afde0c52Smatthias.ringwald             } else {
575afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
576afde0c52Smatthias.ringwald             }
577afde0c52Smatthias.ringwald             break;
578afde0c52Smatthias.ringwald 
579afde0c52Smatthias.ringwald         // handle successful create connection cancel command
580afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
581afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
582afde0c52Smatthias.ringwald                 if (packet[5] == 0){
583afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
584afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
585afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
58603cfbabcSmatthias.ringwald                 }
5871e6aba47Smatthias.ringwald             }
58839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
58939d59809Smatthias.ringwald             break;
59039d59809Smatthias.ringwald 
59139d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
59239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
593afde0c52Smatthias.ringwald             break;
59427a923d0Smatthias.ringwald 
5951e6aba47Smatthias.ringwald         // handle disconnection complete events
596afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
59727a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
598afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
59915ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
60015ec09bbSmatthias.ringwald             while (it->next){
60139ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
60227a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
60315ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6042060cf14Smatthias.ringwald                     it->next = it->next->next;
60515ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
60615ec09bbSmatthias.ringwald                     free (channel);
60715ec09bbSmatthias.ringwald                 } else {
60815ec09bbSmatthias.ringwald                     it = it->next;
60927a923d0Smatthias.ringwald                 }
61027a923d0Smatthias.ringwald             }
611afde0c52Smatthias.ringwald             break;
612fcadd0caSmatthias.ringwald 
6136218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
61402b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6158d371091Smatthias.ringwald             l2cap_hand_out_credits();
6166218e6f1Smatthias.ringwald             break;
6176218e6f1Smatthias.ringwald 
618ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
619afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
62036944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
62180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
622ee091cf1Smatthias.ringwald             int used = 0;
623ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
624ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
625ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
626ee091cf1Smatthias.ringwald                     used = 1;
627ee091cf1Smatthias.ringwald                 }
628ee091cf1Smatthias.ringwald             }
62932ab9390Smatthias.ringwald             if (used) break;
63032ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6319edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
632afde0c52Smatthias.ringwald             break;
633ee091cf1Smatthias.ringwald 
6346e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
6356e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
6366e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
6376e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
6386e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
6396e6710ebSmatthias.ringwald                 }
6406e6710ebSmatthias.ringwald             }
6416e6710ebSmatthias.ringwald             break;
6426e6710ebSmatthias.ringwald 
643afde0c52Smatthias.ringwald         default:
644afde0c52Smatthias.ringwald             break;
645afde0c52Smatthias.ringwald     }
646afde0c52Smatthias.ringwald 
647afde0c52Smatthias.ringwald     // pass on
648b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6491e6aba47Smatthias.ringwald }
6501e6aba47Smatthias.ringwald 
651afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
652b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
653e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
654e7ff783cSmatthias.ringwald     l2cap_run();
65584836b65Smatthias.ringwald }
65684836b65Smatthias.ringwald 
657b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
658645658c9Smatthias.ringwald 
659fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
660645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
661645658c9Smatthias.ringwald     if (!service) {
662645658c9Smatthias.ringwald         // 0x0002 PSM not supported
663fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
664645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
665645658c9Smatthias.ringwald         return;
666645658c9Smatthias.ringwald     }
667645658c9Smatthias.ringwald 
668645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
669645658c9Smatthias.ringwald     if (!hci_connection) {
670fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
671645658c9Smatthias.ringwald         // TODO: emit error
672645658c9Smatthias.ringwald         return;
673645658c9Smatthias.ringwald     }
674645658c9Smatthias.ringwald     // alloc structure
675fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
676169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
677645658c9Smatthias.ringwald     // TODO: emit error event
678169f8b28Smatthias.ringwald     if (!channel) return;
679645658c9Smatthias.ringwald 
680645658c9Smatthias.ringwald     // fill in
681169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
682169f8b28Smatthias.ringwald     channel->psm = psm;
683169f8b28Smatthias.ringwald     channel->handle = handle;
684169f8b28Smatthias.ringwald     channel->connection = service->connection;
685f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
686b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
687b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
688fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6890a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
690761b0451Smatthias.ringwald     channel->packets_granted = 0;
691b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
692645658c9Smatthias.ringwald 
6931d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
694fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
695fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6969775e25bSmatthias.ringwald     }
6979775e25bSmatthias.ringwald 
698645658c9Smatthias.ringwald     // set initial state
699e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
700fa8473a4Smatthias.ringwald     channel->state_var = 0;
701e405ae81Smatthias.ringwald 
702645658c9Smatthias.ringwald     // add to connections list
703169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
704645658c9Smatthias.ringwald 
705e405ae81Smatthias.ringwald     // emit incoming connection request
706e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
707e405ae81Smatthias.ringwald }
708645658c9Smatthias.ringwald 
709b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
710b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
711e405ae81Smatthias.ringwald     if (!channel) {
712fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
713e405ae81Smatthias.ringwald         return;
714e405ae81Smatthias.ringwald     }
715e405ae81Smatthias.ringwald 
716552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
717e405ae81Smatthias.ringwald 
718552d92a1Smatthias.ringwald     // process
719552d92a1Smatthias.ringwald     l2cap_run();
720e405ae81Smatthias.ringwald }
721645658c9Smatthias.ringwald 
722b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
723b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
724e405ae81Smatthias.ringwald     if (!channel) {
725fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
726e405ae81Smatthias.ringwald         return;
727e405ae81Smatthias.ringwald     }
728e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
729e7ff783cSmatthias.ringwald     channel->reason = reason;
730e7ff783cSmatthias.ringwald     l2cap_run();
731645658c9Smatthias.ringwald }
732645658c9Smatthias.ringwald 
7332784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
734b1988dceSmatthias.ringwald 
735b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
736b1988dceSmatthias.ringwald 
7372784b77dSmatthias.ringwald     // accept the other's configuration options
7383de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7393de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7403de7c0caSmatthias.ringwald     while (pos < end_pos){
7411dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7421dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7431dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7441dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7451dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
746fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7471dc511deSmatthias.ringwald         }
7481dc511deSmatthias.ringwald         pos += length;
7491dc511deSmatthias.ringwald     }
7502784b77dSmatthias.ringwald }
7512784b77dSmatthias.ringwald 
752fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
75383f196b3Smatthias.ringwald     // log_dbg("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
754fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
755fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_SENT_CONF_RSP) == 0) return 0;
756fa8473a4Smatthias.ringwald     return 1;
757fa8473a4Smatthias.ringwald }
758fa8473a4Smatthias.ringwald 
759fa8473a4Smatthias.ringwald 
76000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7611e6aba47Smatthias.ringwald 
76200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
76300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
76438e5900eSmatthias.ringwald     uint16_t result = 0;
7651e6aba47Smatthias.ringwald 
7666e6710ebSmatthias.ringwald     log_dbg("signaling handler code %u, state %u\n", code, channel->state);
767b35f641cSmatthias.ringwald 
7689a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7699a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7709a011532Smatthias.ringwald         switch (channel->state){
771fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7729a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7732b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7749a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7759a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7769a011532Smatthias.ringwald                 break;
7779a011532Smatthias.ringwald 
7789a011532Smatthias.ringwald             default:
7799a011532Smatthias.ringwald                 // ignore in other states
7809a011532Smatthias.ringwald                 break;
7819a011532Smatthias.ringwald         }
7829a011532Smatthias.ringwald         return;
7839a011532Smatthias.ringwald     }
7849a011532Smatthias.ringwald 
7851e6aba47Smatthias.ringwald     switch (channel->state) {
7861e6aba47Smatthias.ringwald 
7871e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7881e6aba47Smatthias.ringwald             switch (code){
7891e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
79000d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
79138e5900eSmatthias.ringwald                     switch (result) {
79238e5900eSmatthias.ringwald                         case 0:
793169f8b28Smatthias.ringwald                             // successful connection
79400d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
795fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
796fa8473a4Smatthias.ringwald                             channel->state_var |= STATE_VAR_SEND_CONF_REQ;
79738e5900eSmatthias.ringwald                             break;
79838e5900eSmatthias.ringwald                         case 1:
79938e5900eSmatthias.ringwald                             // connection pending. get some coffee
80038e5900eSmatthias.ringwald                             break;
80138e5900eSmatthias.ringwald                         default:
802eb920dbeSmatthias.ringwald                             // channel closed
803eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
804eb920dbeSmatthias.ringwald 
805f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
80638e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
807eb920dbeSmatthias.ringwald 
808eb920dbeSmatthias.ringwald                             // drop link key if security block
809eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
810eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
811eb920dbeSmatthias.ringwald                             }
812eb920dbeSmatthias.ringwald 
813eb920dbeSmatthias.ringwald                             // discard channel
814eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
815eb920dbeSmatthias.ringwald                             free (channel);
81638e5900eSmatthias.ringwald                             break;
8171e6aba47Smatthias.ringwald                     }
8181e6aba47Smatthias.ringwald                     break;
81938e5900eSmatthias.ringwald 
82038e5900eSmatthias.ringwald                 default:
8211e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
82238e5900eSmatthias.ringwald                     break;
8231e6aba47Smatthias.ringwald             }
8241e6aba47Smatthias.ringwald             break;
8251e6aba47Smatthias.ringwald 
826fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
827ae280e73Smatthias.ringwald             switch (code) {
828ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
829fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_REQ;
830fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SEND_CONF_RSP;
831ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
832ae280e73Smatthias.ringwald                     break;
8331e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
834fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_RSP;
8355a67bd4aSmatthias.ringwald                     break;
8365a67bd4aSmatthias.ringwald                 default:
8375a67bd4aSmatthias.ringwald                     break;
8381e6aba47Smatthias.ringwald             }
839fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
840fa8473a4Smatthias.ringwald                 // for open:
8415a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
842fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8436218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
844c8e4258aSmatthias.ringwald             }
845c8e4258aSmatthias.ringwald             break;
846f62db1e3Smatthias.ringwald 
847f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
848f62db1e3Smatthias.ringwald             switch (code) {
849f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
85027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
85127a923d0Smatthias.ringwald                     break;
8525a67bd4aSmatthias.ringwald                 default:
8535a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8545a67bd4aSmatthias.ringwald                     break;
85527a923d0Smatthias.ringwald             }
85627a923d0Smatthias.ringwald             break;
85784836b65Smatthias.ringwald 
85884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
85984836b65Smatthias.ringwald             // @TODO handle incoming requests
86084836b65Smatthias.ringwald             break;
86184836b65Smatthias.ringwald 
86284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
86384836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
86484836b65Smatthias.ringwald             break;
86510642e45Smatthias.ringwald         default:
86610642e45Smatthias.ringwald             break;
86727a923d0Smatthias.ringwald     }
868fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
86927a923d0Smatthias.ringwald }
87027a923d0Smatthias.ringwald 
87100d93d79Smatthias.ringwald 
87200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
87300d93d79Smatthias.ringwald 
87400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
87500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
87600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
87700d93d79Smatthias.ringwald 
87800d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
87900d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
88000d93d79Smatthias.ringwald         return;
88100d93d79Smatthias.ringwald     }
88200d93d79Smatthias.ringwald 
88300d93d79Smatthias.ringwald     // general commands without an assigned channel
88400d93d79Smatthias.ringwald     switch(code) {
88500d93d79Smatthias.ringwald 
88600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
88700d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
88800d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
88900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8902b83fb7dSmatthias.ringwald             return;
89100d93d79Smatthias.ringwald         }
89200d93d79Smatthias.ringwald 
89300d93d79Smatthias.ringwald         case ECHO_REQUEST: {
8942b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
8952b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
8962b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
8972b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
8982b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
8992b83fb7dSmatthias.ringwald             }
9002b83fb7dSmatthias.ringwald             l2cap_run();
9012b83fb7dSmatthias.ringwald             return;
90200d93d79Smatthias.ringwald         }
90300d93d79Smatthias.ringwald 
90400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
9052b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
90600d93d79Smatthias.ringwald                 // we neither support connectionless L2CAP data nor support any flow control modes yet
90700d93d79Smatthias.ringwald                 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
9082b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
9092b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
9102b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
9112b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].infoType = infoType;
9122b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
91300d93d79Smatthias.ringwald             }
9142b83fb7dSmatthias.ringwald             l2cap_run();
9152b83fb7dSmatthias.ringwald             return;
91600d93d79Smatthias.ringwald         }
91700d93d79Smatthias.ringwald 
91800d93d79Smatthias.ringwald         default:
91900d93d79Smatthias.ringwald             break;
92000d93d79Smatthias.ringwald     }
92100d93d79Smatthias.ringwald 
92200d93d79Smatthias.ringwald 
92300d93d79Smatthias.ringwald     // Get potential destination CID
92400d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
92500d93d79Smatthias.ringwald 
92600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
92700d93d79Smatthias.ringwald     linked_item_t *it;
92800d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
92900d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
93000d93d79Smatthias.ringwald         if (channel->handle == handle) {
93100d93d79Smatthias.ringwald             if (code & 1) {
932b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
933b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
93400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9354e32727eSmatthias.ringwald                     break;
93600d93d79Smatthias.ringwald                 }
93700d93d79Smatthias.ringwald             } else {
938b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
93900d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
94000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9414e32727eSmatthias.ringwald                     break;
94200d93d79Smatthias.ringwald                 }
94300d93d79Smatthias.ringwald             }
94400d93d79Smatthias.ringwald         }
94500d93d79Smatthias.ringwald     }
94600d93d79Smatthias.ringwald }
94700d93d79Smatthias.ringwald 
94800d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
94900d93d79Smatthias.ringwald 
95000d93d79Smatthias.ringwald     // Get Channel ID
95100d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
95200d93d79Smatthias.ringwald 
95300d93d79Smatthias.ringwald     // Signaling Packet?
95400d93d79Smatthias.ringwald     if (channel_id == 1) {
95500d93d79Smatthias.ringwald 
95600d93d79Smatthias.ringwald         // Get Connection
95700d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
95800d93d79Smatthias.ringwald 
95900d93d79Smatthias.ringwald         uint16_t command_offset = 8;
96000d93d79Smatthias.ringwald         while (command_offset < size) {
96100d93d79Smatthias.ringwald 
96200d93d79Smatthias.ringwald             // handle signaling commands
96300d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
96400d93d79Smatthias.ringwald 
96500d93d79Smatthias.ringwald             // increment command_offset
96600d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
96700d93d79Smatthias.ringwald         }
96864472d52Smatthias.ringwald 
96964472d52Smatthias.ringwald         l2cap_run();
97064472d52Smatthias.ringwald 
97100d93d79Smatthias.ringwald         return;
97200d93d79Smatthias.ringwald     }
97300d93d79Smatthias.ringwald 
97400d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
97500d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
97600d93d79Smatthias.ringwald     if (channel) {
97758de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
97800d93d79Smatthias.ringwald     }
97900d93d79Smatthias.ringwald }
98000d93d79Smatthias.ringwald 
9812718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9822718e2e7Smatthias.ringwald     switch (packet_type) {
9832718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9842718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9852718e2e7Smatthias.ringwald             break;
9862718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9872718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9882718e2e7Smatthias.ringwald             break;
9892718e2e7Smatthias.ringwald         default:
9902718e2e7Smatthias.ringwald             break;
9912718e2e7Smatthias.ringwald     }
9922718e2e7Smatthias.ringwald }
99300d93d79Smatthias.ringwald 
99415ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
99527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
996f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
997f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
998f62db1e3Smatthias.ringwald     // discard channel
999f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1000f62db1e3Smatthias.ringwald     free (channel);
1001c8e4258aSmatthias.ringwald }
10021e6aba47Smatthias.ringwald 
10039d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1004c52bf64dSmatthias.ringwald     linked_item_t *it;
10059d9bbc01Smatthias.ringwald 
10069d9bbc01Smatthias.ringwald     // close open channels
10079d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
10089d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
10099d9bbc01Smatthias.ringwald         if ( service->psm == psm){
10109d9bbc01Smatthias.ringwald             return service;
10119d9bbc01Smatthias.ringwald         };
10129d9bbc01Smatthias.ringwald     }
10139d9bbc01Smatthias.ringwald     return NULL;
10149d9bbc01Smatthias.ringwald }
10159d9bbc01Smatthias.ringwald 
101636944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
10179d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
10189d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
10199d9bbc01Smatthias.ringwald     if (service) return;
10209d9bbc01Smatthias.ringwald 
10219d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
10229d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
10239d9bbc01Smatthias.ringwald     if (!service) return;
10249d9bbc01Smatthias.ringwald 
10259d9bbc01Smatthias.ringwald     // fill in
10269d9bbc01Smatthias.ringwald     service->psm = psm;
10279d9bbc01Smatthias.ringwald     service->mtu = mtu;
10289d9bbc01Smatthias.ringwald     service->connection = connection;
1029d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10309d9bbc01Smatthias.ringwald 
10319d9bbc01Smatthias.ringwald     // add to services list
10329d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
10339d9bbc01Smatthias.ringwald }
10349d9bbc01Smatthias.ringwald 
103536944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10369d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1037037d6e48Smatthias.ringwald     if (!service) return;
10389d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
10399d9bbc01Smatthias.ringwald     free(service);
10409d9bbc01Smatthias.ringwald }
10419d9bbc01Smatthias.ringwald 
10429d9bbc01Smatthias.ringwald //
104336944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10449d9bbc01Smatthias.ringwald     linked_item_t *it;
10459d9bbc01Smatthias.ringwald 
10463a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1047c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1048c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1049c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1050c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1051cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1052c52bf64dSmatthias.ringwald         }
1053c52bf64dSmatthias.ringwald     }
10549d9bbc01Smatthias.ringwald 
1055645658c9Smatthias.ringwald     // unregister services
105669025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
105769025de8Smatthias.ringwald     while (it->next) {
105869025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1059645658c9Smatthias.ringwald         if (service->connection == connection){
106069025de8Smatthias.ringwald             it->next = it->next->next;
10618149d2c7Smatthias.ringwald             free(service);
10628149d2c7Smatthias.ringwald         } else {
10638149d2c7Smatthias.ringwald             it = it->next;
1064645658c9Smatthias.ringwald         }
1065645658c9Smatthias.ringwald     }
1066cb8eb7e6Smatthias.ringwald 
1067cb8eb7e6Smatthias.ringwald     // process
1068cb8eb7e6Smatthias.ringwald     l2cap_run();
1069c52bf64dSmatthias.ringwald }
1070c52bf64dSmatthias.ringwald