xref: /btstack/src/l2cap.c (revision 2a544672afa4d2d9f469fdb949dd326d3f70b932)
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;
40483f196b3Smatthias.ringwald         // log_dbg("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4052cd0be45Smatthias.ringwald         switch (channel->state){
4062cd0be45Smatthias.ringwald 
40702b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
40864472d52Smatthias.ringwald                 // send connection request - set state first
40964472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
41002b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
41102b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
41202b22dc4Smatthias.ringwald                 break;
41302b22dc4Smatthias.ringwald 
414e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
415b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
416e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
417e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
418e7ff783cSmatthias.ringwald                 free (channel);
419e7ff783cSmatthias.ringwald                 break;
420e7ff783cSmatthias.ringwald 
421552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
422fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
423fa8473a4Smatthias.ringwald                 channel->state_var |= STATE_VAR_SEND_CONF_REQ;
424*2a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
425552d92a1Smatthias.ringwald                 break;
426552d92a1Smatthias.ringwald 
4276fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4286fdcc387Smatthias.ringwald                 // success, start l2cap handshake
429b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4306fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
431*2a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4326fdcc387Smatthias.ringwald                 break;
4336fdcc387Smatthias.ringwald 
434fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
435fa8473a4Smatthias.ringwald                 if (channel->state_var & STATE_VAR_SEND_CONF_RSP){
436fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_RSP;
437fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_RSP;
438fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
439fa8473a4Smatthias.ringwald                 }
440fa8473a4Smatthias.ringwald                 else if (channel->state_var & STATE_VAR_SEND_CONF_REQ){
441fa8473a4Smatthias.ringwald                     channel->state_var &= ~STATE_VAR_SEND_CONF_REQ;
44283f196b3Smatthias.ringwald                     channel->state_var |= STATE_VAR_SENT_CONF_REQ;
443b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
444ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
445ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
446ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
447b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
448fa8473a4Smatthias.ringwald                 }
449fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
450552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
451552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
452552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
453fa8473a4Smatthias.ringwald                 }
454552d92a1Smatthias.ringwald                 break;
455552d92a1Smatthias.ringwald 
456e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
457b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
458e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
459e7ff783cSmatthias.ringwald                 break;
460e7ff783cSmatthias.ringwald 
461e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
462b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4632cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
464*2a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4652cd0be45Smatthias.ringwald                 break;
4662cd0be45Smatthias.ringwald             default:
4672cd0be45Smatthias.ringwald                 break;
4682cd0be45Smatthias.ringwald         }
4692cd0be45Smatthias.ringwald     }
4702cd0be45Smatthias.ringwald }
4712cd0be45Smatthias.ringwald 
472fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
473fa8c92f6Smatthias.ringwald     return hci_max_acl_data_packet_length()-4;  // 4 bytes for L2CAP header
474fa8c92f6Smatthias.ringwald }
475fa8c92f6Smatthias.ringwald 
4761e6aba47Smatthias.ringwald // open outgoing L2CAP channel
47715470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
47815470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4791e6aba47Smatthias.ringwald 
4801e6aba47Smatthias.ringwald     // alloc structure
4811e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
4821e6aba47Smatthias.ringwald     // TODO: emit error event
4831e6aba47Smatthias.ringwald     if (!chan) return;
4841e6aba47Smatthias.ringwald 
4851d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
486fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
487fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4889775e25bSmatthias.ringwald     }
4899775e25bSmatthias.ringwald 
4901e6aba47Smatthias.ringwald     // fill in
4911e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4921e6aba47Smatthias.ringwald     chan->psm = psm;
4931e6aba47Smatthias.ringwald     chan->handle = 0;
4941e6aba47Smatthias.ringwald     chan->connection = connection;
4956b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4962784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
49715470d27Smatthias.ringwald     chan->local_mtu = mtu;
4986218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4996218e6f1Smatthias.ringwald 
5001e6aba47Smatthias.ringwald     // set initial state
50102b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
502fa8473a4Smatthias.ringwald     chan->state_var = 0;
503b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
504b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5051e6aba47Smatthias.ringwald 
5061e6aba47Smatthias.ringwald     // add to connections list
5071e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5081e6aba47Smatthias.ringwald 
50902b22dc4Smatthias.ringwald     l2cap_run();
51043625864Smatthias.ringwald }
51143625864Smatthias.ringwald 
512b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
513b35f641cSmatthias.ringwald     // find channel for local_cid
514b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
515f62db1e3Smatthias.ringwald     if (channel) {
516e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
517f62db1e3Smatthias.ringwald     }
5182cd0be45Smatthias.ringwald     // process
5192cd0be45Smatthias.ringwald     l2cap_run();
52043625864Smatthias.ringwald }
5211e6aba47Smatthias.ringwald 
522afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
52315ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
52415ec09bbSmatthias.ringwald     while (it->next){
52515ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
526b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
52702b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
528afde0c52Smatthias.ringwald                 // failure, forward error code
529afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
530afde0c52Smatthias.ringwald                 // discard channel
53115ec09bbSmatthias.ringwald                 it->next = it->next->next;
532afde0c52Smatthias.ringwald                 free (channel);
533afde0c52Smatthias.ringwald             }
53415ec09bbSmatthias.ringwald         } else {
53515ec09bbSmatthias.ringwald             it = it->next;
536afde0c52Smatthias.ringwald         }
537afde0c52Smatthias.ringwald     }
538afde0c52Smatthias.ringwald }
539afde0c52Smatthias.ringwald 
540afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
541afde0c52Smatthias.ringwald     linked_item_t *it;
542afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
543afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
544afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
54502b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
546b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5476fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
548afde0c52Smatthias.ringwald                 channel->handle = handle;
549b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
550afde0c52Smatthias.ringwald             }
551afde0c52Smatthias.ringwald         }
552afde0c52Smatthias.ringwald     }
5536fdcc387Smatthias.ringwald     // process
5546fdcc387Smatthias.ringwald     l2cap_run();
555afde0c52Smatthias.ringwald }
556b448a0e7Smatthias.ringwald 
557afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
558afde0c52Smatthias.ringwald 
559afde0c52Smatthias.ringwald     bd_addr_t address;
560afde0c52Smatthias.ringwald     hci_con_handle_t handle;
56136944dffSmatthias.ringwald     linked_item_t *it;
562afde0c52Smatthias.ringwald 
563afde0c52Smatthias.ringwald     switch(packet[0]){
564afde0c52Smatthias.ringwald 
565afde0c52Smatthias.ringwald         // handle connection complete events
566afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
567afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
568afde0c52Smatthias.ringwald             if (packet[2] == 0){
569afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
570afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
571afde0c52Smatthias.ringwald             } else {
572afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
573afde0c52Smatthias.ringwald             }
574afde0c52Smatthias.ringwald             break;
575afde0c52Smatthias.ringwald 
576afde0c52Smatthias.ringwald         // handle successful create connection cancel command
577afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
578afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
579afde0c52Smatthias.ringwald                 if (packet[5] == 0){
580afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
581afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
582afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
58303cfbabcSmatthias.ringwald                 }
5841e6aba47Smatthias.ringwald             }
58539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
58639d59809Smatthias.ringwald             break;
58739d59809Smatthias.ringwald 
58839d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
58939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
590afde0c52Smatthias.ringwald             break;
59127a923d0Smatthias.ringwald 
5921e6aba47Smatthias.ringwald         // handle disconnection complete events
593afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
59427a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
595afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
59615ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
59715ec09bbSmatthias.ringwald             while (it->next){
59839ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
59927a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
60015ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6012060cf14Smatthias.ringwald                     it->next = it->next->next;
60215ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
60315ec09bbSmatthias.ringwald                     free (channel);
60415ec09bbSmatthias.ringwald                 } else {
60515ec09bbSmatthias.ringwald                     it = it->next;
60627a923d0Smatthias.ringwald                 }
60727a923d0Smatthias.ringwald             }
608afde0c52Smatthias.ringwald             break;
609fcadd0caSmatthias.ringwald 
6106218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
61102b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6128d371091Smatthias.ringwald             l2cap_hand_out_credits();
6136218e6f1Smatthias.ringwald             break;
6146218e6f1Smatthias.ringwald 
615ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
616afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
61736944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
61880ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
619ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
620ee091cf1Smatthias.ringwald             int used = 0;
621ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
622ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
623ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
624ee091cf1Smatthias.ringwald                     used = 1;
625ee091cf1Smatthias.ringwald                 }
626ee091cf1Smatthias.ringwald             }
62732ab9390Smatthias.ringwald             if (used) break;
62832ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6299edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
630afde0c52Smatthias.ringwald             break;
631ee091cf1Smatthias.ringwald 
632afde0c52Smatthias.ringwald         default:
633afde0c52Smatthias.ringwald             break;
634afde0c52Smatthias.ringwald     }
635afde0c52Smatthias.ringwald 
636afde0c52Smatthias.ringwald     // pass on
637b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6381e6aba47Smatthias.ringwald }
6391e6aba47Smatthias.ringwald 
640afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
641b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
642e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
643e7ff783cSmatthias.ringwald     l2cap_run();
64484836b65Smatthias.ringwald }
64584836b65Smatthias.ringwald 
646b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
647645658c9Smatthias.ringwald 
648fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
649645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
650645658c9Smatthias.ringwald     if (!service) {
651645658c9Smatthias.ringwald         // 0x0002 PSM not supported
652fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
653645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
654645658c9Smatthias.ringwald         return;
655645658c9Smatthias.ringwald     }
656645658c9Smatthias.ringwald 
657645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
658645658c9Smatthias.ringwald     if (!hci_connection) {
659fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
660645658c9Smatthias.ringwald         // TODO: emit error
661645658c9Smatthias.ringwald         return;
662645658c9Smatthias.ringwald     }
663645658c9Smatthias.ringwald     // alloc structure
664fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
665169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
666645658c9Smatthias.ringwald     // TODO: emit error event
667169f8b28Smatthias.ringwald     if (!channel) return;
668645658c9Smatthias.ringwald 
669645658c9Smatthias.ringwald     // fill in
670169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
671169f8b28Smatthias.ringwald     channel->psm = psm;
672169f8b28Smatthias.ringwald     channel->handle = handle;
673169f8b28Smatthias.ringwald     channel->connection = service->connection;
674f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
675b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
676b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
677fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6780a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
679761b0451Smatthias.ringwald     channel->packets_granted = 0;
680b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
681645658c9Smatthias.ringwald 
6821d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
683fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
684fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6859775e25bSmatthias.ringwald     }
6869775e25bSmatthias.ringwald 
687645658c9Smatthias.ringwald     // set initial state
688e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
689fa8473a4Smatthias.ringwald     channel->state_var = 0;
690e405ae81Smatthias.ringwald 
691645658c9Smatthias.ringwald     // add to connections list
692169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
693645658c9Smatthias.ringwald 
694e405ae81Smatthias.ringwald     // emit incoming connection request
695e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
696e405ae81Smatthias.ringwald }
697645658c9Smatthias.ringwald 
698b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
699b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
700e405ae81Smatthias.ringwald     if (!channel) {
701fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
702e405ae81Smatthias.ringwald         return;
703e405ae81Smatthias.ringwald     }
704e405ae81Smatthias.ringwald 
705552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
706e405ae81Smatthias.ringwald 
707552d92a1Smatthias.ringwald     // process
708552d92a1Smatthias.ringwald     l2cap_run();
709e405ae81Smatthias.ringwald }
710645658c9Smatthias.ringwald 
711b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
712b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
713e405ae81Smatthias.ringwald     if (!channel) {
714fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
715e405ae81Smatthias.ringwald         return;
716e405ae81Smatthias.ringwald     }
717e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
718e7ff783cSmatthias.ringwald     channel->reason = reason;
719e7ff783cSmatthias.ringwald     l2cap_run();
720645658c9Smatthias.ringwald }
721645658c9Smatthias.ringwald 
7222784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
723b1988dceSmatthias.ringwald 
724b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
725b1988dceSmatthias.ringwald 
7262784b77dSmatthias.ringwald     // accept the other's configuration options
7273de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7283de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7293de7c0caSmatthias.ringwald     while (pos < end_pos){
7301dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7311dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7321dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7331dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7341dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
735fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7361dc511deSmatthias.ringwald         }
7371dc511deSmatthias.ringwald         pos += length;
7381dc511deSmatthias.ringwald     }
7392784b77dSmatthias.ringwald }
7402784b77dSmatthias.ringwald 
741fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
74283f196b3Smatthias.ringwald     // log_dbg("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
743fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
744fa8473a4Smatthias.ringwald     if ((channel->state_var & STATE_VAR_SENT_CONF_RSP) == 0) return 0;
745fa8473a4Smatthias.ringwald     return 1;
746fa8473a4Smatthias.ringwald }
747fa8473a4Smatthias.ringwald 
748fa8473a4Smatthias.ringwald 
74900d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7501e6aba47Smatthias.ringwald 
75100d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
75200d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
75338e5900eSmatthias.ringwald     uint16_t result = 0;
7541e6aba47Smatthias.ringwald 
75583f196b3Smatthias.ringwald     // log_dbg("signaling handler code %u, state %u\n", code, channel->state);
756b35f641cSmatthias.ringwald 
7579a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7589a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7599a011532Smatthias.ringwald         switch (channel->state){
760fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7619a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7622b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7639a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7649a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7659a011532Smatthias.ringwald                 break;
7669a011532Smatthias.ringwald 
7679a011532Smatthias.ringwald             default:
7689a011532Smatthias.ringwald                 // ignore in other states
7699a011532Smatthias.ringwald                 break;
7709a011532Smatthias.ringwald         }
7719a011532Smatthias.ringwald         return;
7729a011532Smatthias.ringwald     }
7739a011532Smatthias.ringwald 
7741e6aba47Smatthias.ringwald     switch (channel->state) {
7751e6aba47Smatthias.ringwald 
7761e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7771e6aba47Smatthias.ringwald             switch (code){
7781e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
77900d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
78038e5900eSmatthias.ringwald                     switch (result) {
78138e5900eSmatthias.ringwald                         case 0:
782169f8b28Smatthias.ringwald                             // successful connection
78300d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
784fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
785fa8473a4Smatthias.ringwald                             channel->state_var |= STATE_VAR_SEND_CONF_REQ;
78638e5900eSmatthias.ringwald                             break;
78738e5900eSmatthias.ringwald                         case 1:
78838e5900eSmatthias.ringwald                             // connection pending. get some coffee
78938e5900eSmatthias.ringwald                             break;
79038e5900eSmatthias.ringwald                         default:
791eb920dbeSmatthias.ringwald                             // channel closed
792eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
793eb920dbeSmatthias.ringwald 
794f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
79538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
796eb920dbeSmatthias.ringwald 
797eb920dbeSmatthias.ringwald                             // drop link key if security block
798eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
799eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
800eb920dbeSmatthias.ringwald                             }
801eb920dbeSmatthias.ringwald 
802eb920dbeSmatthias.ringwald                             // discard channel
803eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
804eb920dbeSmatthias.ringwald                             free (channel);
80538e5900eSmatthias.ringwald                             break;
8061e6aba47Smatthias.ringwald                     }
8071e6aba47Smatthias.ringwald                     break;
80838e5900eSmatthias.ringwald 
80938e5900eSmatthias.ringwald                 default:
8101e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
81138e5900eSmatthias.ringwald                     break;
8121e6aba47Smatthias.ringwald             }
8131e6aba47Smatthias.ringwald             break;
8141e6aba47Smatthias.ringwald 
815fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
816ae280e73Smatthias.ringwald             switch (code) {
817ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
818fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_REQ;
819fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_SEND_CONF_RSP;
820ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
821ae280e73Smatthias.ringwald                     break;
8221e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
823fa8473a4Smatthias.ringwald                     channel->state_var |= STATE_VAR_RCVD_CONF_RSP;
8245a67bd4aSmatthias.ringwald                     break;
8255a67bd4aSmatthias.ringwald                 default:
8265a67bd4aSmatthias.ringwald                     break;
8271e6aba47Smatthias.ringwald             }
828fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
829fa8473a4Smatthias.ringwald                 // for open:
8305a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
831fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8326218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
833c8e4258aSmatthias.ringwald             }
834c8e4258aSmatthias.ringwald             break;
835f62db1e3Smatthias.ringwald 
836f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
837f62db1e3Smatthias.ringwald             switch (code) {
838f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
83927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
84027a923d0Smatthias.ringwald                     break;
8415a67bd4aSmatthias.ringwald                 default:
8425a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8435a67bd4aSmatthias.ringwald                     break;
84427a923d0Smatthias.ringwald             }
84527a923d0Smatthias.ringwald             break;
84684836b65Smatthias.ringwald 
84784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
84884836b65Smatthias.ringwald             // @TODO handle incoming requests
84984836b65Smatthias.ringwald             break;
85084836b65Smatthias.ringwald 
85184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
85284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
85384836b65Smatthias.ringwald             break;
85410642e45Smatthias.ringwald         default:
85510642e45Smatthias.ringwald             break;
85627a923d0Smatthias.ringwald     }
857fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
85827a923d0Smatthias.ringwald }
85927a923d0Smatthias.ringwald 
86000d93d79Smatthias.ringwald 
86100d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
86200d93d79Smatthias.ringwald 
86300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
86400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
86500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
86600d93d79Smatthias.ringwald 
86700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
86800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
86900d93d79Smatthias.ringwald         return;
87000d93d79Smatthias.ringwald     }
87100d93d79Smatthias.ringwald 
87200d93d79Smatthias.ringwald     // general commands without an assigned channel
87300d93d79Smatthias.ringwald     switch(code) {
87400d93d79Smatthias.ringwald 
87500d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
87600d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
87700d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
87800d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8792b83fb7dSmatthias.ringwald             return;
88000d93d79Smatthias.ringwald         }
88100d93d79Smatthias.ringwald 
88200d93d79Smatthias.ringwald         case ECHO_REQUEST: {
8832b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
8842b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
8852b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
8862b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
8872b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
8882b83fb7dSmatthias.ringwald             }
8892b83fb7dSmatthias.ringwald             l2cap_run();
8902b83fb7dSmatthias.ringwald             return;
89100d93d79Smatthias.ringwald         }
89200d93d79Smatthias.ringwald 
89300d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
8942b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
89500d93d79Smatthias.ringwald                 // we neither support connectionless L2CAP data nor support any flow control modes yet
89600d93d79Smatthias.ringwald                 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
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[signaling_responses_pending].infoType = infoType;
9012b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
90200d93d79Smatthias.ringwald             }
9032b83fb7dSmatthias.ringwald             l2cap_run();
9042b83fb7dSmatthias.ringwald             return;
90500d93d79Smatthias.ringwald         }
90600d93d79Smatthias.ringwald 
90700d93d79Smatthias.ringwald         default:
90800d93d79Smatthias.ringwald             break;
90900d93d79Smatthias.ringwald     }
91000d93d79Smatthias.ringwald 
91100d93d79Smatthias.ringwald 
91200d93d79Smatthias.ringwald     // Get potential destination CID
91300d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
91400d93d79Smatthias.ringwald 
91500d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
91600d93d79Smatthias.ringwald     linked_item_t *it;
91700d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
91800d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
91900d93d79Smatthias.ringwald         if (channel->handle == handle) {
92000d93d79Smatthias.ringwald             if (code & 1) {
921b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
922b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
92300d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9244e32727eSmatthias.ringwald                     break;
92500d93d79Smatthias.ringwald                 }
92600d93d79Smatthias.ringwald             } else {
927b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
92800d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
92900d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9304e32727eSmatthias.ringwald                     break;
93100d93d79Smatthias.ringwald                 }
93200d93d79Smatthias.ringwald             }
93300d93d79Smatthias.ringwald         }
93400d93d79Smatthias.ringwald     }
93500d93d79Smatthias.ringwald }
93600d93d79Smatthias.ringwald 
93700d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
93800d93d79Smatthias.ringwald 
93900d93d79Smatthias.ringwald     // Get Channel ID
94000d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
94100d93d79Smatthias.ringwald 
94200d93d79Smatthias.ringwald     // Signaling Packet?
94300d93d79Smatthias.ringwald     if (channel_id == 1) {
94400d93d79Smatthias.ringwald 
94500d93d79Smatthias.ringwald         // Get Connection
94600d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
94700d93d79Smatthias.ringwald 
94800d93d79Smatthias.ringwald         uint16_t command_offset = 8;
94900d93d79Smatthias.ringwald         while (command_offset < size) {
95000d93d79Smatthias.ringwald 
95100d93d79Smatthias.ringwald             // handle signaling commands
95200d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
95300d93d79Smatthias.ringwald 
95400d93d79Smatthias.ringwald             // increment command_offset
95500d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
95600d93d79Smatthias.ringwald         }
95764472d52Smatthias.ringwald 
95864472d52Smatthias.ringwald         l2cap_run();
95964472d52Smatthias.ringwald 
96000d93d79Smatthias.ringwald         return;
96100d93d79Smatthias.ringwald     }
96200d93d79Smatthias.ringwald 
96300d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
96400d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
96500d93d79Smatthias.ringwald     if (channel) {
96658de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
96700d93d79Smatthias.ringwald     }
96800d93d79Smatthias.ringwald }
96900d93d79Smatthias.ringwald 
9702718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9712718e2e7Smatthias.ringwald     switch (packet_type) {
9722718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9732718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9742718e2e7Smatthias.ringwald             break;
9752718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9762718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9772718e2e7Smatthias.ringwald             break;
9782718e2e7Smatthias.ringwald         default:
9792718e2e7Smatthias.ringwald             break;
9802718e2e7Smatthias.ringwald     }
9812718e2e7Smatthias.ringwald }
98200d93d79Smatthias.ringwald 
98315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
98427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
985f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
986f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
987f62db1e3Smatthias.ringwald     // discard channel
988f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
989f62db1e3Smatthias.ringwald     free (channel);
990c8e4258aSmatthias.ringwald }
9911e6aba47Smatthias.ringwald 
9929d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
993c52bf64dSmatthias.ringwald     linked_item_t *it;
9949d9bbc01Smatthias.ringwald 
9959d9bbc01Smatthias.ringwald     // close open channels
9969d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9979d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9989d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9999d9bbc01Smatthias.ringwald             return service;
10009d9bbc01Smatthias.ringwald         };
10019d9bbc01Smatthias.ringwald     }
10029d9bbc01Smatthias.ringwald     return NULL;
10039d9bbc01Smatthias.ringwald }
10049d9bbc01Smatthias.ringwald 
100536944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
10069d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
10079d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
10089d9bbc01Smatthias.ringwald     if (service) return;
10099d9bbc01Smatthias.ringwald 
10109d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
10119d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
10129d9bbc01Smatthias.ringwald     if (!service) return;
10139d9bbc01Smatthias.ringwald 
10149d9bbc01Smatthias.ringwald     // fill in
10159d9bbc01Smatthias.ringwald     service->psm = psm;
10169d9bbc01Smatthias.ringwald     service->mtu = mtu;
10179d9bbc01Smatthias.ringwald     service->connection = connection;
1018d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10199d9bbc01Smatthias.ringwald 
10209d9bbc01Smatthias.ringwald     // add to services list
10219d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
10229d9bbc01Smatthias.ringwald }
10239d9bbc01Smatthias.ringwald 
102436944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10259d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1026037d6e48Smatthias.ringwald     if (!service) return;
10279d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
10289d9bbc01Smatthias.ringwald     free(service);
10299d9bbc01Smatthias.ringwald }
10309d9bbc01Smatthias.ringwald 
10319d9bbc01Smatthias.ringwald //
103236944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10339d9bbc01Smatthias.ringwald     linked_item_t *it;
10349d9bbc01Smatthias.ringwald 
10353a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1036c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1037c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1038c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1039c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1040cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1041c52bf64dSmatthias.ringwald         }
1042c52bf64dSmatthias.ringwald     }
10439d9bbc01Smatthias.ringwald 
1044645658c9Smatthias.ringwald     // unregister services
104569025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
104669025de8Smatthias.ringwald     while (it->next) {
104769025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1048645658c9Smatthias.ringwald         if (service->connection == connection){
104969025de8Smatthias.ringwald             it->next = it->next->next;
10508149d2c7Smatthias.ringwald             free(service);
10518149d2c7Smatthias.ringwald         } else {
10528149d2c7Smatthias.ringwald             it = it->next;
1053645658c9Smatthias.ringwald         }
1054645658c9Smatthias.ringwald     }
1055cb8eb7e6Smatthias.ringwald 
1056cb8eb7e6Smatthias.ringwald     // process
1057cb8eb7e6Smatthias.ringwald     l2cap_run();
1058c52bf64dSmatthias.ringwald }
1059c52bf64dSmatthias.ringwald