xref: /btstack/src/l2cap_signaling.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "l2cap_signaling.c"
39 
40 /*
41  *  l2cap_signaling.h
42  *
43  *  Created by Matthias Ringwald on 7/23/09.
44  */
45 
46 #include "l2cap_signaling.h"
47 #include "btstack_config.h"
48 #include "btstack_debug.h"
49 #include "hci.h"
50 
51 #include <string.h>
52 
53 static const char *l2cap_signaling_commands_format[] = {
54 "2D",    // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
55 "22",    // 0x02 connection request: PSM, Source CID
56 "2222",  // 0x03 connection response: Dest CID, Source CID, Result, Status
57 "22D",   // 0x04 config request: Dest CID, Flags, Configuration options
58 "222D",  // 0x05 config response: Source CID, Flags, Result, Configuration options
59 "22",    // 0x06 disconection request: Dest CID, Source CID
60 "22",    // 0x07 disconection response: Dest CID, Source CID
61 "D",     // 0x08 echo request: Data
62 "D",     // 0x09 echo response: Data
63 "2",     // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported}
64 "22D",   // 0x0b information response: InfoType, Result, Data
65 #ifdef ENABLE_BLE
66 NULL,    // 0x0c non-supported AMP command
67 NULL,    // 0x0d non-supported AMP command
68 NULL,    // 0x0e non-supported AMP command
69 NULL,    // 0x0f non-supported AMP command
70 NULL,    // 0x10 non-supported AMP command
71 NULL,    // 0x11 non-supported AMP command
72 "2222",  // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler
73 "2",     // 0x13 connection parameter update response: result
74 "22222", // 0X14 le credit based connection request: le psm, source cid, mtu, mps, initial credits
75 "22222", // 0x15 le credit based connection respone: dest cid, mtu, mps, initial credits, result
76 "22",    // 0x16 le flow control credit: source cid, credits
77 #endif
78 };
79 
80 static const unsigned int num_l2cap_commands = sizeof(l2cap_signaling_commands_format) / sizeof(const char *);
81 
82 uint8_t   sig_seq_nr  = 0xff;
83 uint16_t  source_cid  = 0x40;
84 
85 uint8_t l2cap_next_sig_id(void){
86     if (sig_seq_nr == 0xff) {
87         sig_seq_nr = 1;
88     } else {
89         sig_seq_nr++;
90     }
91     return sig_seq_nr;
92 }
93 
94 uint16_t l2cap_next_local_cid(void){
95     return source_cid++;
96 }
97 
98 static uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
99 
100     const char *format = NULL;
101     if (cmd > 0 && cmd <= num_l2cap_commands) {
102         format = l2cap_signaling_commands_format[cmd-1];
103     }
104     if (!format){
105         log_error("l2cap_create_signaling_internal: invalid command id 0x%02x", cmd);
106         return 0;
107     }
108 
109     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
110 
111     // 0 - Connection handle : PB=pb : BC=00
112     little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
113     // 6 - L2CAP channel = 1
114     little_endian_store_16(acl_buffer, 6, cid);
115     // 8 - Code
116     acl_buffer[8] = cmd;
117     // 9 - id (!= 0 sequentially)
118     acl_buffer[9] = identifier;
119 
120     // 12 - L2CAP signaling parameters
121     uint16_t pos = 12;
122     uint16_t word;
123     uint8_t * ptr;
124     while (*format) {
125         switch(*format) {
126             case '1': //  8 bit value
127             case '2': // 16 bit value
128                 word = va_arg(argptr, int);
129                 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
130                 acl_buffer[pos++] = word & 0xff;
131                 if (*format == '2') {
132                     acl_buffer[pos++] = word >> 8;
133                 }
134                 break;
135             case 'D': // variable data. passed: len, ptr
136                 word = va_arg(argptr, int);
137                 ptr  = va_arg(argptr, uint8_t *);
138                 memcpy(&acl_buffer[pos], ptr, word);
139                 pos += word;
140                 break;
141             default:
142                 break;
143         }
144         format++;
145     };
146     va_end(argptr);
147 
148     // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length
149     // - the l2cap payload length is counted after the following channel id (only payload)
150 
151     // 2 - ACL length
152     little_endian_store_16(acl_buffer, 2,  pos - 4);
153     // 4 - L2CAP packet length
154     little_endian_store_16(acl_buffer, 4,  pos - 6 - 2);
155     // 10 - L2CAP signaling parameter length
156     little_endian_store_16(acl_buffer, 10, pos - 12);
157 
158     return pos;
159 }
160 
161 uint16_t l2cap_create_signaling_classic(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
162     return l2cap_create_signaling_internal(acl_buffer, handle, 1, cmd, identifier, argptr);
163 }
164 
165 #ifdef ENABLE_BLE
166 uint16_t l2cap_create_signaling_le(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
167     return l2cap_create_signaling_internal(acl_buffer, handle, 5, cmd, identifier, argptr);
168 }
169 #endif
170