xref: /btstack/src/l2cap.c (revision 39bda6d5dbeb6c6c8d80a1b09d40c6b7f1272502)
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 
60*39bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
61*39bda6d5Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 10
62*39bda6d5Smatthias.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 
69*39bda6d5Smatthias.ringwald typedef enum {
70*39bda6d5Smatthias.ringwald     L2CAP_STATE_CLOSED = 1,           // no baseband
71*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
72*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
73*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
74*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONNECT_RSP, // from peer
75*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ,
76*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ,
77*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP,
78*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONFIG_REQ_RSP,
79*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_CONFIG_REQ,
80*39bda6d5Smatthias.ringwald     L2CAP_STATE_OPEN,
81*39bda6d5Smatthias.ringwald     L2CAP_STATE_WAIT_DISCONNECT,  // from application
82*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
83*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
84*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
85*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONFIG_REQ,
86*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP,
87*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP,
88*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
89*39bda6d5Smatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
90*39bda6d5Smatthias.ringwald } L2CAP_STATE;
91fcadd0caSmatthias.ringwald 
92*39bda6d5Smatthias.ringwald // info regarding an actual coneection
93*39bda6d5Smatthias.ringwald typedef struct {
94*39bda6d5Smatthias.ringwald     // linked list - assert: first field
95*39bda6d5Smatthias.ringwald     linked_item_t    item;
96*39bda6d5Smatthias.ringwald 
97*39bda6d5Smatthias.ringwald     L2CAP_STATE state;
98*39bda6d5Smatthias.ringwald 
99*39bda6d5Smatthias.ringwald     bd_addr_t address;
100*39bda6d5Smatthias.ringwald     hci_con_handle_t handle;
101*39bda6d5Smatthias.ringwald 
102*39bda6d5Smatthias.ringwald     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
103*39bda6d5Smatthias.ringwald     uint8_t   local_sig_id;     // own signaling identifier
104*39bda6d5Smatthias.ringwald 
105*39bda6d5Smatthias.ringwald     uint16_t  local_cid;
106*39bda6d5Smatthias.ringwald     uint16_t  remote_cid;
107*39bda6d5Smatthias.ringwald 
108*39bda6d5Smatthias.ringwald     uint16_t  local_mtu;
109*39bda6d5Smatthias.ringwald     uint16_t  remote_mtu;
110*39bda6d5Smatthias.ringwald 
111*39bda6d5Smatthias.ringwald     uint16_t  psm;
112*39bda6d5Smatthias.ringwald 
113*39bda6d5Smatthias.ringwald     uint8_t   packets_granted;    // number of L2CAP/ACL packets client is allowed to send
114*39bda6d5Smatthias.ringwald 
115*39bda6d5Smatthias.ringwald     uint8_t   reason; // used in decline internal
116*39bda6d5Smatthias.ringwald 
117*39bda6d5Smatthias.ringwald     // client connection
118*39bda6d5Smatthias.ringwald     void * connection;
119*39bda6d5Smatthias.ringwald 
120*39bda6d5Smatthias.ringwald     // internal connection
121*39bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
122*39bda6d5Smatthias.ringwald 
123*39bda6d5Smatthias.ringwald } l2cap_channel_t;
124*39bda6d5Smatthias.ringwald 
125*39bda6d5Smatthias.ringwald // info regarding potential connections
126*39bda6d5Smatthias.ringwald typedef struct {
127*39bda6d5Smatthias.ringwald     // linked list - assert: first field
128*39bda6d5Smatthias.ringwald     linked_item_t    item;
129*39bda6d5Smatthias.ringwald 
130*39bda6d5Smatthias.ringwald     // service id
131*39bda6d5Smatthias.ringwald     uint16_t  psm;
132*39bda6d5Smatthias.ringwald 
133*39bda6d5Smatthias.ringwald     // incoming MTU
134*39bda6d5Smatthias.ringwald     uint16_t mtu;
135*39bda6d5Smatthias.ringwald 
136*39bda6d5Smatthias.ringwald     // client connection
137*39bda6d5Smatthias.ringwald     void *connection;
138*39bda6d5Smatthias.ringwald 
139*39bda6d5Smatthias.ringwald     // internal connection
140*39bda6d5Smatthias.ringwald     btstack_packet_handler_t packet_handler;
141*39bda6d5Smatthias.ringwald 
142*39bda6d5Smatthias.ringwald } l2cap_service_t;
143*39bda6d5Smatthias.ringwald 
144*39bda6d5Smatthias.ringwald 
1452b83fb7dSmatthias.ringwald typedef struct l2cap_signaling_response {
1462b83fb7dSmatthias.ringwald     hci_con_handle_t handle;
1472b83fb7dSmatthias.ringwald     uint8_t  sig_id;
1482b83fb7dSmatthias.ringwald     uint8_t  code;
1492b83fb7dSmatthias.ringwald     uint16_t infoType;
1502b83fb7dSmatthias.ringwald } l2cap_signaling_response_t;
151*39bda6d5Smatthias.ringwald 
152*39bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
153*39bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
154*39bda6d5Smatthias.ringwald 
155*39bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1562b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1572b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1582b83fb7dSmatthias.ringwald 
1591e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
1601e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
1619d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
1621e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
16336944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
164808a48abSmatthias.ringwald static int new_credits_blocked = 0;
1651e6aba47Smatthias.ringwald 
166*39bda6d5Smatthias.ringwald 
167*39bda6d5Smatthias.ringwald // prototypes
168*39bda6d5Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel);
169*39bda6d5Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm);
170*39bda6d5Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
171*39bda6d5Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel);
172*39bda6d5Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel);
173*39bda6d5Smatthias.ringwald 
174*39bda6d5Smatthias.ringwald 
1751e6aba47Smatthias.ringwald void l2cap_init(){
1762784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
17711c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
178fcadd0caSmatthias.ringwald 
179808a48abSmatthias.ringwald     new_credits_blocked = 0;
1802b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
181808a48abSmatthias.ringwald 
182fcadd0caSmatthias.ringwald     //
1832718e2e7Smatthias.ringwald     // register callback with HCI
184fcadd0caSmatthias.ringwald     //
1852718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
186fcadd0caSmatthias.ringwald }
187fcadd0caSmatthias.ringwald 
188fcadd0caSmatthias.ringwald 
189fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
19036944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
191fcadd0caSmatthias.ringwald }
19236944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
193b502e1b0Smatthias.ringwald     packet_handler = handler;
1941e6aba47Smatthias.ringwald }
1951e6aba47Smatthias.ringwald 
19658de5610Smatthias.ringwald //  notify client/protocol handler
19758de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
19858de5610Smatthias.ringwald     if (channel->packet_handler) {
19958de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
20058de5610Smatthias.ringwald     } else {
20136944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
20258de5610Smatthias.ringwald     }
20358de5610Smatthias.ringwald }
20458de5610Smatthias.ringwald 
20558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
2069893b714Smatthias.ringwald     uint8_t event[21];
20758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
20858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
20958de5610Smatthias.ringwald     event[2] = status;
21058de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
21158de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
21258de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
21358de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
21458de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
2154c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
2164c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
21758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
21958de5610Smatthias.ringwald }
22058de5610Smatthias.ringwald 
22158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
22258de5610Smatthias.ringwald     uint8_t event[4];
22358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
22458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
22558de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
22658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
22858de5610Smatthias.ringwald }
22958de5610Smatthias.ringwald 
23058de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
23158de5610Smatthias.ringwald     uint8_t event[16];
23258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
23358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
23458de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
23558de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
23658de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
23758de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
23858de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
23958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24058de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2410af41d30Smatthias.ringwald }
242808a48abSmatthias.ringwald 
2436218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
2446218e6f1Smatthias.ringwald     // track credits
2456218e6f1Smatthias.ringwald     channel->packets_granted += credits;
246808a48abSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
2476218e6f1Smatthias.ringwald 
2486218e6f1Smatthias.ringwald     uint8_t event[5];
2496218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
2506218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
2516218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
2526218e6f1Smatthias.ringwald     event[4] = credits;
2536218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
2546218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2556218e6f1Smatthias.ringwald }
2566218e6f1Smatthias.ringwald 
257808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
258808a48abSmatthias.ringwald     new_credits_blocked = blocked;
259808a48abSmatthias.ringwald }
260808a48abSmatthias.ringwald 
26140d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
262808a48abSmatthias.ringwald 
263808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
264808a48abSmatthias.ringwald 
2658d371091Smatthias.ringwald     linked_item_t *it;
2668d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2678d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2688d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2690e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2704c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2718d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2728d371091Smatthias.ringwald         }
2738d371091Smatthias.ringwald     }
2748d371091Smatthias.ringwald }
2758d371091Smatthias.ringwald 
276b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
277f62db1e3Smatthias.ringwald     linked_item_t *it;
278f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2798d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
280b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
281f62db1e3Smatthias.ringwald             return channel;
282f62db1e3Smatthias.ringwald         }
283f62db1e3Smatthias.ringwald     }
284f62db1e3Smatthias.ringwald     return NULL;
285f62db1e3Smatthias.ringwald }
286f62db1e3Smatthias.ringwald 
2876b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2886b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2896b1fde37Smatthias.ringwald     if (!channel) return 0;
2906b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2916b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2926b1fde37Smatthias.ringwald }
2936b1fde37Smatthias.ringwald 
29496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
29596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
29696cbd662Smatthias.ringwald     if (channel) {
29796cbd662Smatthias.ringwald         return channel->remote_mtu;
29896cbd662Smatthias.ringwald     }
29996cbd662Smatthias.ringwald     return 0;
30096cbd662Smatthias.ringwald }
30196cbd662Smatthias.ringwald 
30258de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
303fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
30458de5610Smatthias.ringwald     va_list argptr;
30558de5610Smatthias.ringwald     va_start(argptr, identifier);
30658de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
30758de5610Smatthias.ringwald     va_end(argptr);
308808a48abSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet con %u!\n", handle);
30958de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
31058de5610Smatthias.ringwald }
31158de5610Smatthias.ringwald 
3126218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
3136218e6f1Smatthias.ringwald 
3146218e6f1Smatthias.ringwald     // check for free places on BT module
3158ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
3168ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
317808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3188ea03fa5Smatthias.ringwald     }
3196218e6f1Smatthias.ringwald     int err = 0;
3206218e6f1Smatthias.ringwald 
32158de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
32258de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
32358de5610Smatthias.ringwald     if (channel) {
3246218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
3256218e6f1Smatthias.ringwald             --channel->packets_granted;
326808a48abSmatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
327808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
3286218e6f1Smatthias.ringwald         } else {
3296218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
3306218e6f1Smatthias.ringwald         }
3316218e6f1Smatthias.ringwald 
33258de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
33358de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
33458de5610Smatthias.ringwald         // 2 - ACL length
33558de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
33658de5610Smatthias.ringwald         // 4 - L2CAP packet length
33758de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
33858de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
33958de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
34058de5610Smatthias.ringwald         // 8 - data
34158de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
3426218e6f1Smatthias.ringwald 
34358de5610Smatthias.ringwald         // send
3446218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
34558de5610Smatthias.ringwald     }
34691b99603Smatthias.ringwald 
34791b99603Smatthias.ringwald     l2cap_hand_out_credits();
34891b99603Smatthias.ringwald 
3496218e6f1Smatthias.ringwald     return err;
35058de5610Smatthias.ringwald }
35158de5610Smatthias.ringwald 
3528158c421Smatthias.ringwald // MARK: L2CAP_RUN
3532cd0be45Smatthias.ringwald // process outstanding signaling tasks
3542cd0be45Smatthias.ringwald void l2cap_run(void){
3552b83fb7dSmatthias.ringwald 
3562b83fb7dSmatthias.ringwald     // check pending signaling responses
3572b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3582b83fb7dSmatthias.ringwald 
35902b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3602b83fb7dSmatthias.ringwald 
3612b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3622b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3632b83fb7dSmatthias.ringwald         uint8_t infoType = signaling_responses[0].infoType;
3642b83fb7dSmatthias.ringwald 
3652b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3662b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3672b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3682b83fb7dSmatthias.ringwald                 break;
3692b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3702b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3712b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3722b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3732b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3742b83fb7dSmatthias.ringwald                 } else {
3752b83fb7dSmatthias.ringwald                     // all other types are not supported
3762b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3772b83fb7dSmatthias.ringwald                 }
3782b83fb7dSmatthias.ringwald                 break;
3792b83fb7dSmatthias.ringwald             default:
3802b83fb7dSmatthias.ringwald                 // should not happen
3812b83fb7dSmatthias.ringwald                 break;
3822b83fb7dSmatthias.ringwald         }
3832b83fb7dSmatthias.ringwald 
3842b83fb7dSmatthias.ringwald         // remove first item
3852b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3862b83fb7dSmatthias.ringwald         int i;
3872b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
3882b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
3892b83fb7dSmatthias.ringwald         }
3902b83fb7dSmatthias.ringwald     }
3912b83fb7dSmatthias.ringwald 
392ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3932cd0be45Smatthias.ringwald     linked_item_t *it;
3942cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3952cd0be45Smatthias.ringwald 
39602b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
39702b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3982cd0be45Smatthias.ringwald 
3992cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4002cd0be45Smatthias.ringwald         switch (channel->state){
4012cd0be45Smatthias.ringwald 
40202b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
40364472d52Smatthias.ringwald                 // send connection request - set state first
40464472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
40502b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
40602b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
40702b22dc4Smatthias.ringwald                 break;
40802b22dc4Smatthias.ringwald 
409e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
410b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
411e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
412e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
413e7ff783cSmatthias.ringwald                 free (channel);
414e7ff783cSmatthias.ringwald                 break;
415e7ff783cSmatthias.ringwald 
416552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
417b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
418552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
419552d92a1Smatthias.ringwald                 break;
420552d92a1Smatthias.ringwald 
4216fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4226fdcc387Smatthias.ringwald                 // success, start l2cap handshake
423b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
424b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4256fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
4266fdcc387Smatthias.ringwald                 break;
4276fdcc387Smatthias.ringwald 
428ae280e73Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
429ae280e73Smatthias.ringwald                 // after connection response was received
430b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
431ae280e73Smatthias.ringwald                 config_options[0] = 1; // MTU
432ae280e73Smatthias.ringwald                 config_options[1] = 2; // len param
433ae280e73Smatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
434b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
435ae280e73Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
436ae280e73Smatthias.ringwald                 break;
437ae280e73Smatthias.ringwald 
438b1988dceSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ:
439b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
440b1988dceSmatthias.ringwald                 config_options[0] = 1; // MTU
441b1988dceSmatthias.ringwald                 config_options[1] = 2; // len param
442b1988dceSmatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
443b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
444b1988dceSmatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
445b1988dceSmatthias.ringwald                 break;
446b1988dceSmatthias.ringwald 
447552d92a1Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP:
448b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
449552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
450552d92a1Smatthias.ringwald                 break;
451552d92a1Smatthias.ringwald 
452552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP:
453b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
454552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
455552d92a1Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);  // success
456552d92a1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
457552d92a1Smatthias.ringwald                 break;
458552d92a1Smatthias.ringwald 
459ae280e73Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
460b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
461552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ;
462552d92a1Smatthias.ringwald                 break;
463552d92a1Smatthias.ringwald 
464e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
465b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
466e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
467e7ff783cSmatthias.ringwald                 break;
468e7ff783cSmatthias.ringwald 
469e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
470b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
471b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4722cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4732cd0be45Smatthias.ringwald                 break;
4742cd0be45Smatthias.ringwald             default:
4752cd0be45Smatthias.ringwald                 break;
4762cd0be45Smatthias.ringwald         }
4772cd0be45Smatthias.ringwald     }
4782cd0be45Smatthias.ringwald }
4792cd0be45Smatthias.ringwald 
480fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
481fa8c92f6Smatthias.ringwald     return hci_max_acl_data_packet_length()-4;  // 4 bytes for L2CAP header
482fa8c92f6Smatthias.ringwald }
483fa8c92f6Smatthias.ringwald 
4841e6aba47Smatthias.ringwald // open outgoing L2CAP channel
48515470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
48615470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4871e6aba47Smatthias.ringwald 
4881e6aba47Smatthias.ringwald     // alloc structure
4891e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
4901e6aba47Smatthias.ringwald     // TODO: emit error event
4911e6aba47Smatthias.ringwald     if (!chan) return;
4921e6aba47Smatthias.ringwald 
4931d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
494fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
495fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4969775e25bSmatthias.ringwald     }
4979775e25bSmatthias.ringwald 
4981e6aba47Smatthias.ringwald     // fill in
4991e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
5001e6aba47Smatthias.ringwald     chan->psm = psm;
5011e6aba47Smatthias.ringwald     chan->handle = 0;
5021e6aba47Smatthias.ringwald     chan->connection = connection;
5036b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
5042784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
50515470d27Smatthias.ringwald     chan->local_mtu = mtu;
5066218e6f1Smatthias.ringwald     chan->packets_granted = 0;
5076218e6f1Smatthias.ringwald 
5081e6aba47Smatthias.ringwald     // set initial state
50902b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
510b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
511b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5121e6aba47Smatthias.ringwald 
5131e6aba47Smatthias.ringwald     // add to connections list
5141e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5151e6aba47Smatthias.ringwald 
51602b22dc4Smatthias.ringwald     l2cap_run();
51743625864Smatthias.ringwald }
51843625864Smatthias.ringwald 
519b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
520b35f641cSmatthias.ringwald     // find channel for local_cid
521b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
522f62db1e3Smatthias.ringwald     if (channel) {
523e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
524f62db1e3Smatthias.ringwald     }
5252cd0be45Smatthias.ringwald     // process
5262cd0be45Smatthias.ringwald     l2cap_run();
52743625864Smatthias.ringwald }
5281e6aba47Smatthias.ringwald 
529afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
53015ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
53115ec09bbSmatthias.ringwald     while (it->next){
53215ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
533b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
53402b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
535afde0c52Smatthias.ringwald                 // failure, forward error code
536afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
537afde0c52Smatthias.ringwald                 // discard channel
53815ec09bbSmatthias.ringwald                 it->next = it->next->next;
539afde0c52Smatthias.ringwald                 free (channel);
540afde0c52Smatthias.ringwald             }
54115ec09bbSmatthias.ringwald         } else {
54215ec09bbSmatthias.ringwald             it = it->next;
543afde0c52Smatthias.ringwald         }
544afde0c52Smatthias.ringwald     }
545afde0c52Smatthias.ringwald }
546afde0c52Smatthias.ringwald 
547afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
548afde0c52Smatthias.ringwald     linked_item_t *it;
549afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
550afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
551afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
55202b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
553b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5546fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
555afde0c52Smatthias.ringwald                 channel->handle = handle;
556b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
557afde0c52Smatthias.ringwald             }
558afde0c52Smatthias.ringwald         }
559afde0c52Smatthias.ringwald     }
5606fdcc387Smatthias.ringwald     // process
5616fdcc387Smatthias.ringwald     l2cap_run();
562afde0c52Smatthias.ringwald }
563b448a0e7Smatthias.ringwald 
564afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
565afde0c52Smatthias.ringwald 
566afde0c52Smatthias.ringwald     bd_addr_t address;
567afde0c52Smatthias.ringwald     hci_con_handle_t handle;
56836944dffSmatthias.ringwald     linked_item_t *it;
569afde0c52Smatthias.ringwald 
570afde0c52Smatthias.ringwald     switch(packet[0]){
571afde0c52Smatthias.ringwald 
572afde0c52Smatthias.ringwald         // handle connection complete events
573afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
574afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
575afde0c52Smatthias.ringwald             if (packet[2] == 0){
576afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
577afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
578afde0c52Smatthias.ringwald             } else {
579afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
580afde0c52Smatthias.ringwald             }
581afde0c52Smatthias.ringwald             break;
582afde0c52Smatthias.ringwald 
583afde0c52Smatthias.ringwald         // handle successful create connection cancel command
584afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
585afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
586afde0c52Smatthias.ringwald                 if (packet[5] == 0){
587afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
588afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
589afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
59003cfbabcSmatthias.ringwald                 }
5911e6aba47Smatthias.ringwald             }
59239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
59339d59809Smatthias.ringwald             break;
59439d59809Smatthias.ringwald 
59539d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
59639d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
597afde0c52Smatthias.ringwald             break;
59827a923d0Smatthias.ringwald 
5991e6aba47Smatthias.ringwald         // handle disconnection complete events
600afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
60127a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
602afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
60315ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
60415ec09bbSmatthias.ringwald             while (it->next){
60539ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
60627a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
60715ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6082060cf14Smatthias.ringwald                     it->next = it->next->next;
60915ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
61015ec09bbSmatthias.ringwald                     free (channel);
61115ec09bbSmatthias.ringwald                 } else {
61215ec09bbSmatthias.ringwald                     it = it->next;
61327a923d0Smatthias.ringwald                 }
61427a923d0Smatthias.ringwald             }
615afde0c52Smatthias.ringwald             break;
616fcadd0caSmatthias.ringwald 
6176218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
61802b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6198d371091Smatthias.ringwald             l2cap_hand_out_credits();
6206218e6f1Smatthias.ringwald             break;
6216218e6f1Smatthias.ringwald 
622ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
623afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
62436944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
62580ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
626ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
627ee091cf1Smatthias.ringwald             int used = 0;
628ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
629ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
630ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
631ee091cf1Smatthias.ringwald                     used = 1;
632ee091cf1Smatthias.ringwald                 }
633ee091cf1Smatthias.ringwald             }
63432ab9390Smatthias.ringwald             if (used) break;
63532ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6369edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
637afde0c52Smatthias.ringwald             break;
638ee091cf1Smatthias.ringwald 
639afde0c52Smatthias.ringwald         default:
640afde0c52Smatthias.ringwald             break;
641afde0c52Smatthias.ringwald     }
642afde0c52Smatthias.ringwald 
643afde0c52Smatthias.ringwald     // pass on
644b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6451e6aba47Smatthias.ringwald }
6461e6aba47Smatthias.ringwald 
647afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
648b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
649e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
650e7ff783cSmatthias.ringwald     l2cap_run();
65184836b65Smatthias.ringwald }
65284836b65Smatthias.ringwald 
653b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
654645658c9Smatthias.ringwald 
655fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
656645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
657645658c9Smatthias.ringwald     if (!service) {
658645658c9Smatthias.ringwald         // 0x0002 PSM not supported
659fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
660645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
661645658c9Smatthias.ringwald         return;
662645658c9Smatthias.ringwald     }
663645658c9Smatthias.ringwald 
664645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
665645658c9Smatthias.ringwald     if (!hci_connection) {
666fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
667645658c9Smatthias.ringwald         // TODO: emit error
668645658c9Smatthias.ringwald         return;
669645658c9Smatthias.ringwald     }
670645658c9Smatthias.ringwald     // alloc structure
671fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
672169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
673645658c9Smatthias.ringwald     // TODO: emit error event
674169f8b28Smatthias.ringwald     if (!channel) return;
675645658c9Smatthias.ringwald 
676645658c9Smatthias.ringwald     // fill in
677169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
678169f8b28Smatthias.ringwald     channel->psm = psm;
679169f8b28Smatthias.ringwald     channel->handle = handle;
680169f8b28Smatthias.ringwald     channel->connection = service->connection;
681f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
682b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
683b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
684fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6850a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
686761b0451Smatthias.ringwald     channel->packets_granted = 0;
687b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
688645658c9Smatthias.ringwald 
6891d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
690fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
691fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6929775e25bSmatthias.ringwald     }
6939775e25bSmatthias.ringwald 
694645658c9Smatthias.ringwald     // set initial state
695e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
696e405ae81Smatthias.ringwald 
697645658c9Smatthias.ringwald     // add to connections list
698169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
699645658c9Smatthias.ringwald 
700e405ae81Smatthias.ringwald     // emit incoming connection request
701e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
702e405ae81Smatthias.ringwald }
703645658c9Smatthias.ringwald 
704b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
705b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
706e405ae81Smatthias.ringwald     if (!channel) {
707fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
708e405ae81Smatthias.ringwald         return;
709e405ae81Smatthias.ringwald     }
710e405ae81Smatthias.ringwald 
711552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
712e405ae81Smatthias.ringwald 
713552d92a1Smatthias.ringwald     // process
714552d92a1Smatthias.ringwald     l2cap_run();
715e405ae81Smatthias.ringwald }
716645658c9Smatthias.ringwald 
717b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
718b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
719e405ae81Smatthias.ringwald     if (!channel) {
720fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
721e405ae81Smatthias.ringwald         return;
722e405ae81Smatthias.ringwald     }
723e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
724e7ff783cSmatthias.ringwald     channel->reason = reason;
725e7ff783cSmatthias.ringwald     l2cap_run();
726645658c9Smatthias.ringwald }
727645658c9Smatthias.ringwald 
7282784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
729b1988dceSmatthias.ringwald 
730b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
731b1988dceSmatthias.ringwald 
7322784b77dSmatthias.ringwald     // accept the other's configuration options
7333de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7343de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7353de7c0caSmatthias.ringwald     while (pos < end_pos){
7361dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7371dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7381dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7391dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7401dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
741fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7421dc511deSmatthias.ringwald         }
7431dc511deSmatthias.ringwald         pos += length;
7441dc511deSmatthias.ringwald     }
7452784b77dSmatthias.ringwald }
7462784b77dSmatthias.ringwald 
74700d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7481e6aba47Smatthias.ringwald 
74900d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
75000d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
75138e5900eSmatthias.ringwald     uint16_t result = 0;
7521e6aba47Smatthias.ringwald 
75364472d52Smatthias.ringwald     log_dbg("signaling handler code %u, state %u\n", code, channel->state);
754b35f641cSmatthias.ringwald 
7559a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7569a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7579a011532Smatthias.ringwald         switch (channel->state){
7589a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
7592b83fb7dSmatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP:
7609a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
7619a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ:
7629a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
7632b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ:
7642b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP:
7652b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
7669a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7672b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7689a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7699a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7709a011532Smatthias.ringwald                 break;
7719a011532Smatthias.ringwald 
7729a011532Smatthias.ringwald             default:
7739a011532Smatthias.ringwald                 // ignore in other states
7749a011532Smatthias.ringwald                 break;
7759a011532Smatthias.ringwald         }
7769a011532Smatthias.ringwald         return;
7779a011532Smatthias.ringwald     }
7789a011532Smatthias.ringwald 
7791e6aba47Smatthias.ringwald     switch (channel->state) {
7801e6aba47Smatthias.ringwald 
7811e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7821e6aba47Smatthias.ringwald             switch (code){
7831e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
78400d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
78538e5900eSmatthias.ringwald                     switch (result) {
78638e5900eSmatthias.ringwald                         case 0:
787169f8b28Smatthias.ringwald                             // successful connection
78800d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
789ae280e73Smatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
790d418e203Smatthias.ringwald #if 0
791d418e203Smatthias.ringwald     channel->state = L2CAP_STATE_OPEN;
792d418e203Smatthias.ringwald     l2cap_emit_channel_opened(channel, 0);  // success
793d418e203Smatthias.ringwald     l2cap_emit_credits(channel, 1);
794d418e203Smatthias.ringwald #endif
79538e5900eSmatthias.ringwald                             break;
79638e5900eSmatthias.ringwald                         case 1:
79738e5900eSmatthias.ringwald                             // connection pending. get some coffee
79838e5900eSmatthias.ringwald                             break;
79938e5900eSmatthias.ringwald                         default:
800eb920dbeSmatthias.ringwald                             // channel closed
801eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
802eb920dbeSmatthias.ringwald 
803f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
80438e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
805eb920dbeSmatthias.ringwald 
806eb920dbeSmatthias.ringwald                             // drop link key if security block
807eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
808eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
809eb920dbeSmatthias.ringwald                             }
810eb920dbeSmatthias.ringwald 
811eb920dbeSmatthias.ringwald                             // discard channel
812eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
813eb920dbeSmatthias.ringwald                             free (channel);
81438e5900eSmatthias.ringwald                             break;
8151e6aba47Smatthias.ringwald                     }
8161e6aba47Smatthias.ringwald                     break;
81738e5900eSmatthias.ringwald 
81838e5900eSmatthias.ringwald                 default:
8191e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
82038e5900eSmatthias.ringwald                     break;
8211e6aba47Smatthias.ringwald             }
8221e6aba47Smatthias.ringwald             break;
8231e6aba47Smatthias.ringwald 
824ae280e73Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
825ae280e73Smatthias.ringwald             switch (code) {
826ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
827ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
828ae280e73Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP;
829ae280e73Smatthias.ringwald                     break;
830ae280e73Smatthias.ringwald                 default:
831ae280e73Smatthias.ringwald                     //@TODO: implement other signaling packets
832ae280e73Smatthias.ringwald                     break;
833ae280e73Smatthias.ringwald             }
834ae280e73Smatthias.ringwald             break;
835ae280e73Smatthias.ringwald 
8365a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
8371e6aba47Smatthias.ringwald             switch (code) {
8381e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
8391e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
8401e6aba47Smatthias.ringwald                     break;
8415a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
8422784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
843552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP;
8445a67bd4aSmatthias.ringwald                     break;
8455a67bd4aSmatthias.ringwald                 default:
8465a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8475a67bd4aSmatthias.ringwald                     break;
8481e6aba47Smatthias.ringwald             }
8491e6aba47Smatthias.ringwald             break;
8501e6aba47Smatthias.ringwald 
8511e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
8521e6aba47Smatthias.ringwald             switch (code) {
8531e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
8542784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
855552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP;
856c8e4258aSmatthias.ringwald                     break;
8575a67bd4aSmatthias.ringwald                 default:
8585a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8595a67bd4aSmatthias.ringwald                     break;
8605a67bd4aSmatthias.ringwald             }
8615a67bd4aSmatthias.ringwald             break;
8625a67bd4aSmatthias.ringwald 
8635a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
8645a67bd4aSmatthias.ringwald             switch (code) {
8655a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
8665a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
8675a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
8686218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
8695a67bd4aSmatthias.ringwald                     break;
8705a67bd4aSmatthias.ringwald                 default:
8715a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8725a67bd4aSmatthias.ringwald                     break;
873c8e4258aSmatthias.ringwald             }
874c8e4258aSmatthias.ringwald             break;
875f62db1e3Smatthias.ringwald 
876f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
877f62db1e3Smatthias.ringwald             switch (code) {
878f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
87927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
88027a923d0Smatthias.ringwald                     break;
8815a67bd4aSmatthias.ringwald                 default:
8825a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8835a67bd4aSmatthias.ringwald                     break;
88427a923d0Smatthias.ringwald             }
88527a923d0Smatthias.ringwald             break;
88684836b65Smatthias.ringwald 
88784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
88884836b65Smatthias.ringwald             // @TODO handle incoming requests
88984836b65Smatthias.ringwald             break;
89084836b65Smatthias.ringwald 
89184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
89284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
89384836b65Smatthias.ringwald             break;
89410642e45Smatthias.ringwald         default:
89510642e45Smatthias.ringwald             break;
89627a923d0Smatthias.ringwald     }
897fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
89827a923d0Smatthias.ringwald }
89927a923d0Smatthias.ringwald 
90000d93d79Smatthias.ringwald 
90100d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
90200d93d79Smatthias.ringwald 
90300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
90400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
90500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
90600d93d79Smatthias.ringwald 
90700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
90800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
90900d93d79Smatthias.ringwald         return;
91000d93d79Smatthias.ringwald     }
91100d93d79Smatthias.ringwald 
91200d93d79Smatthias.ringwald     // general commands without an assigned channel
91300d93d79Smatthias.ringwald     switch(code) {
91400d93d79Smatthias.ringwald 
91500d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
91600d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
91700d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
91800d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
9192b83fb7dSmatthias.ringwald             return;
92000d93d79Smatthias.ringwald         }
92100d93d79Smatthias.ringwald 
92200d93d79Smatthias.ringwald         case ECHO_REQUEST: {
9232b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
9242b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
9252b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
9262b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
9272b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
9282b83fb7dSmatthias.ringwald             }
9292b83fb7dSmatthias.ringwald             l2cap_run();
9302b83fb7dSmatthias.ringwald             return;
93100d93d79Smatthias.ringwald         }
93200d93d79Smatthias.ringwald 
93300d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
9342b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
93500d93d79Smatthias.ringwald                 // we neither support connectionless L2CAP data nor support any flow control modes yet
93600d93d79Smatthias.ringwald                 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
9372b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
9382b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
9392b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
9402b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].infoType = infoType;
9412b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
94200d93d79Smatthias.ringwald             }
9432b83fb7dSmatthias.ringwald             l2cap_run();
9442b83fb7dSmatthias.ringwald             return;
94500d93d79Smatthias.ringwald         }
94600d93d79Smatthias.ringwald 
94700d93d79Smatthias.ringwald         default:
94800d93d79Smatthias.ringwald             break;
94900d93d79Smatthias.ringwald     }
95000d93d79Smatthias.ringwald 
95100d93d79Smatthias.ringwald 
95200d93d79Smatthias.ringwald     // Get potential destination CID
95300d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
95400d93d79Smatthias.ringwald 
95500d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
95600d93d79Smatthias.ringwald     linked_item_t *it;
95700d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
95800d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
95900d93d79Smatthias.ringwald         if (channel->handle == handle) {
96000d93d79Smatthias.ringwald             if (code & 1) {
961b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
962b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
96300d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9644e32727eSmatthias.ringwald                     break;
96500d93d79Smatthias.ringwald                 }
96600d93d79Smatthias.ringwald             } else {
967b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
96800d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
96900d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9704e32727eSmatthias.ringwald                     break;
97100d93d79Smatthias.ringwald                 }
97200d93d79Smatthias.ringwald             }
97300d93d79Smatthias.ringwald         }
97400d93d79Smatthias.ringwald     }
97500d93d79Smatthias.ringwald }
97600d93d79Smatthias.ringwald 
97700d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
97800d93d79Smatthias.ringwald 
97900d93d79Smatthias.ringwald     // Get Channel ID
98000d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
98100d93d79Smatthias.ringwald 
98200d93d79Smatthias.ringwald     // Signaling Packet?
98300d93d79Smatthias.ringwald     if (channel_id == 1) {
98400d93d79Smatthias.ringwald 
98500d93d79Smatthias.ringwald         // Get Connection
98600d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
98700d93d79Smatthias.ringwald 
98800d93d79Smatthias.ringwald         uint16_t command_offset = 8;
98900d93d79Smatthias.ringwald         while (command_offset < size) {
99000d93d79Smatthias.ringwald 
99100d93d79Smatthias.ringwald             // handle signaling commands
99200d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
99300d93d79Smatthias.ringwald 
99400d93d79Smatthias.ringwald             // increment command_offset
99500d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
99600d93d79Smatthias.ringwald         }
99764472d52Smatthias.ringwald 
99864472d52Smatthias.ringwald         l2cap_run();
99964472d52Smatthias.ringwald 
100000d93d79Smatthias.ringwald         return;
100100d93d79Smatthias.ringwald     }
100200d93d79Smatthias.ringwald 
100300d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
100400d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
100500d93d79Smatthias.ringwald     if (channel) {
100658de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
100700d93d79Smatthias.ringwald     }
100800d93d79Smatthias.ringwald }
100900d93d79Smatthias.ringwald 
10102718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
10112718e2e7Smatthias.ringwald     switch (packet_type) {
10122718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
10132718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
10142718e2e7Smatthias.ringwald             break;
10152718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
10162718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
10172718e2e7Smatthias.ringwald             break;
10182718e2e7Smatthias.ringwald         default:
10192718e2e7Smatthias.ringwald             break;
10202718e2e7Smatthias.ringwald     }
10212718e2e7Smatthias.ringwald }
102200d93d79Smatthias.ringwald 
102315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
102427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1025f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1026f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1027f62db1e3Smatthias.ringwald     // discard channel
1028f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1029f62db1e3Smatthias.ringwald     free (channel);
1030c8e4258aSmatthias.ringwald }
10311e6aba47Smatthias.ringwald 
10329d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1033c52bf64dSmatthias.ringwald     linked_item_t *it;
10349d9bbc01Smatthias.ringwald 
10359d9bbc01Smatthias.ringwald     // close open channels
10369d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
10379d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
10389d9bbc01Smatthias.ringwald         if ( service->psm == psm){
10399d9bbc01Smatthias.ringwald             return service;
10409d9bbc01Smatthias.ringwald         };
10419d9bbc01Smatthias.ringwald     }
10429d9bbc01Smatthias.ringwald     return NULL;
10439d9bbc01Smatthias.ringwald }
10449d9bbc01Smatthias.ringwald 
104536944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
10469d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
10479d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
10489d9bbc01Smatthias.ringwald     if (service) return;
10499d9bbc01Smatthias.ringwald 
10509d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
10519d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
10529d9bbc01Smatthias.ringwald     if (!service) return;
10539d9bbc01Smatthias.ringwald 
10549d9bbc01Smatthias.ringwald     // fill in
10559d9bbc01Smatthias.ringwald     service->psm = psm;
10569d9bbc01Smatthias.ringwald     service->mtu = mtu;
10579d9bbc01Smatthias.ringwald     service->connection = connection;
1058d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10599d9bbc01Smatthias.ringwald 
10609d9bbc01Smatthias.ringwald     // add to services list
10619d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
10629d9bbc01Smatthias.ringwald }
10639d9bbc01Smatthias.ringwald 
106436944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10659d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1066037d6e48Smatthias.ringwald     if (!service) return;
10679d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
10689d9bbc01Smatthias.ringwald     free(service);
10699d9bbc01Smatthias.ringwald }
10709d9bbc01Smatthias.ringwald 
10719d9bbc01Smatthias.ringwald //
107236944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10739d9bbc01Smatthias.ringwald     linked_item_t *it;
10749d9bbc01Smatthias.ringwald 
10753a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1076c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1077c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1078c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1079c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1080cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1081c52bf64dSmatthias.ringwald         }
1082c52bf64dSmatthias.ringwald     }
10839d9bbc01Smatthias.ringwald 
1084645658c9Smatthias.ringwald     // unregister services
108569025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
108669025de8Smatthias.ringwald     while (it->next) {
108769025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1088645658c9Smatthias.ringwald         if (service->connection == connection){
108969025de8Smatthias.ringwald             it->next = it->next->next;
10908149d2c7Smatthias.ringwald             free(service);
10918149d2c7Smatthias.ringwald         } else {
10928149d2c7Smatthias.ringwald             it = it->next;
1093645658c9Smatthias.ringwald         }
1094645658c9Smatthias.ringwald     }
1095cb8eb7e6Smatthias.ringwald 
1096cb8eb7e6Smatthias.ringwald     // process
1097cb8eb7e6Smatthias.ringwald     l2cap_run();
1098c52bf64dSmatthias.ringwald }
1099c52bf64dSmatthias.ringwald