xref: /btstack/src/l2cap_signaling.c (revision b398e4aaf1c4a8e906f8e40706e98cf572340a59)
195cbd947Smatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 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.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
3895cbd947Smatthias.ringwald  *  l2cap_signaling.h
3995cbd947Smatthias.ringwald  *
4095cbd947Smatthias.ringwald  *  Created by Matthias Ringwald on 7/23/09.
4195cbd947Smatthias.ringwald  */
4295cbd947Smatthias.ringwald 
4395cbd947Smatthias.ringwald #include "l2cap_signaling.h"
44bde315ceS[email protected] #include "btstack-config.h"
45*b398e4aaS[email protected] #include "hci.h"
4695cbd947Smatthias.ringwald 
47b48a17ffSmatthias.ringwald #include <string.h>
48b48a17ffSmatthias.ringwald 
4995b6ea6eS[email protected] static const char *l2cap_signaling_commands_format[] = {
505ca8d57bS[email protected] "2D",    // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
5195cbd947Smatthias.ringwald "22",   // 0x02 connection request: PSM, Source CID
5295cbd947Smatthias.ringwald "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status
5395cbd947Smatthias.ringwald "22D",  // 0x04 config request: Dest CID, Flags, Configuration options
5495cbd947Smatthias.ringwald "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options
5595cbd947Smatthias.ringwald "22",   // 0x06 disconection request: Dest CID, Source CID
5695cbd947Smatthias.ringwald "22",   // 0x07 disconection response: Dest CID, Source CID
5795cbd947Smatthias.ringwald "D",    // 0x08 echo request: Data
5895cbd947Smatthias.ringwald "D",    // 0x09 echo response: Data
5995cbd947Smatthias.ringwald "2",    // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported}
6095cbd947Smatthias.ringwald "22D",  // 0x0b information response: InfoType, Result, Data
619e795b48S[email protected] #ifdef HAVE_BLE
629e795b48S[email protected] // skip 6 not supported signaling pdus, see below
639e795b48S[email protected] "2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler
649e795b48S[email protected] "2",    // 0x13 connection parameter update response: result
659e795b48S[email protected] #endif
6695cbd947Smatthias.ringwald };
6795cbd947Smatthias.ringwald 
68da269baaSmatthias.ringwald uint8_t   sig_seq_nr  = 0xff;
69bb53b291Smatthias.ringwald uint16_t  source_cid  = 0x40;
7095cbd947Smatthias.ringwald 
71da269baaSmatthias.ringwald uint8_t l2cap_next_sig_id(void){
72da269baaSmatthias.ringwald     if (sig_seq_nr == 0xff) {
73da269baaSmatthias.ringwald         sig_seq_nr = 1;
74da269baaSmatthias.ringwald     } else {
75da269baaSmatthias.ringwald         sig_seq_nr++;
76da269baaSmatthias.ringwald     }
77da269baaSmatthias.ringwald     return sig_seq_nr;
78da269baaSmatthias.ringwald }
79da269baaSmatthias.ringwald 
80b35f641cSmatthias.ringwald uint16_t l2cap_next_local_cid(void){
81bb53b291Smatthias.ringwald     return source_cid++;
82da269baaSmatthias.ringwald }
83da269baaSmatthias.ringwald 
84c61af878S[email protected] 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){
8595cbd947Smatthias.ringwald 
86e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
87e9772277S[email protected] 
88e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
89e9772277S[email protected]     bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
9095cbd947Smatthias.ringwald     // 6 - L2CAP channel = 1
91c61af878S[email protected]     bt_store_16(acl_buffer, 6, cid);
9295cbd947Smatthias.ringwald     // 8 - Code
9395cbd947Smatthias.ringwald     acl_buffer[8] = cmd;
9495cbd947Smatthias.ringwald     // 9 - id (!= 0 sequentially)
9595cbd947Smatthias.ringwald     acl_buffer[9] = identifier;
9695cbd947Smatthias.ringwald 
9795cbd947Smatthias.ringwald     // 12 - L2CAP signaling parameters
9895cbd947Smatthias.ringwald     uint16_t pos = 12;
999e795b48S[email protected]     // skip AMP commands
1009e795b48S[email protected]     if (cmd >= CONNECTION_PARAMETER_UPDATE_REQUEST){
1019e795b48S[email protected]         cmd -= 6;
1029e795b48S[email protected]     }
10395cbd947Smatthias.ringwald     const char *format = l2cap_signaling_commands_format[cmd-1];
10495cbd947Smatthias.ringwald     uint16_t word;
10595cbd947Smatthias.ringwald     uint8_t * ptr;
10695cbd947Smatthias.ringwald     while (*format) {
10795cbd947Smatthias.ringwald         switch(*format) {
10895cbd947Smatthias.ringwald             case '1': //  8 bit value
10995cbd947Smatthias.ringwald             case '2': // 16 bit value
11095cbd947Smatthias.ringwald                 word = va_arg(argptr, int);
11195cbd947Smatthias.ringwald                 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
11295cbd947Smatthias.ringwald                 acl_buffer[pos++] = word & 0xff;
11395cbd947Smatthias.ringwald                 if (*format == '2') {
11495cbd947Smatthias.ringwald                     acl_buffer[pos++] = word >> 8;
11595cbd947Smatthias.ringwald                 }
11695cbd947Smatthias.ringwald                 break;
11795cbd947Smatthias.ringwald             case 'D': // variable data. passed: len, ptr
11895cbd947Smatthias.ringwald                 word = va_arg(argptr, int);
11995cbd947Smatthias.ringwald                 ptr  = va_arg(argptr, uint8_t *);
12095cbd947Smatthias.ringwald                 memcpy(&acl_buffer[pos], ptr, word);
12195cbd947Smatthias.ringwald                 pos += word;
12295cbd947Smatthias.ringwald                 break;
12395cbd947Smatthias.ringwald             default:
12495cbd947Smatthias.ringwald                 break;
12595cbd947Smatthias.ringwald         }
12695cbd947Smatthias.ringwald         format++;
12795cbd947Smatthias.ringwald     };
12895cbd947Smatthias.ringwald     va_end(argptr);
12995cbd947Smatthias.ringwald 
13095cbd947Smatthias.ringwald     // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length
13195cbd947Smatthias.ringwald     // - the l2cap payload length is counted after the following channel id (only payload)
13295cbd947Smatthias.ringwald 
13395cbd947Smatthias.ringwald     // 2 - ACL length
13495cbd947Smatthias.ringwald     bt_store_16(acl_buffer, 2,  pos - 4);
13595cbd947Smatthias.ringwald     // 4 - L2CAP packet length
13695cbd947Smatthias.ringwald     bt_store_16(acl_buffer, 4,  pos - 6 - 2);
13795cbd947Smatthias.ringwald     // 10 - L2CAP signaling parameter length
13895cbd947Smatthias.ringwald     bt_store_16(acl_buffer, 10, pos - 12);
13995cbd947Smatthias.ringwald 
14095cbd947Smatthias.ringwald     return pos;
14195cbd947Smatthias.ringwald }
142ab2b01dcS[email protected] 
143c61af878S[email protected] 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){
144c61af878S[email protected]     return l2cap_create_signaling_internal(acl_buffer, handle, 1, cmd, identifier, argptr);
145c61af878S[email protected] }
146c61af878S[email protected] 
147ab2b01dcS[email protected] #ifdef HAVE_BLE
148c61af878S[email protected] 
149c61af878S[email protected] 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){
150c61af878S[email protected]     return l2cap_create_signaling_internal(acl_buffer, handle, 5, cmd, identifier, argptr);
151c61af878S[email protected] }
152c61af878S[email protected] 
153ab2b01dcS[email protected] uint16_t l2cap_le_create_connection_parameter_update_request(uint8_t * acl_buffer, uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){
154e9772277S[email protected] 
155e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
156e9772277S[email protected] 
157e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
158e9772277S[email protected]     bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
159ab2b01dcS[email protected]     // 6 - L2CAP LE Signaling channel = 5
160ab2b01dcS[email protected]     bt_store_16(acl_buffer, 6, 5);
161ab2b01dcS[email protected]     // 8 - Code
162ab2b01dcS[email protected]     acl_buffer[8] = CONNECTION_PARAMETER_UPDATE_REQUEST;
163ab2b01dcS[email protected]     // 9 - id (!= 0 sequentially)
164ab2b01dcS[email protected]     acl_buffer[9] = 1;
165ab2b01dcS[email protected]     uint16_t pos = 12;
166ab2b01dcS[email protected]     bt_store_16(acl_buffer, pos, interval_min);
167ab2b01dcS[email protected]     pos += 2;
168ab2b01dcS[email protected]     bt_store_16(acl_buffer, pos, interval_max);
169ab2b01dcS[email protected]     pos += 2;
170ab2b01dcS[email protected]     bt_store_16(acl_buffer, pos, slave_latency);
171ab2b01dcS[email protected]     pos += 2;
172ab2b01dcS[email protected]     bt_store_16(acl_buffer, pos, timeout_multiplier);
173ab2b01dcS[email protected]     pos += 2;
174ab2b01dcS[email protected]     // 2 - ACL length
175ab2b01dcS[email protected]     bt_store_16(acl_buffer, 2,  pos - 4);
176ab2b01dcS[email protected]     // 4 - L2CAP packet length
177ab2b01dcS[email protected]     bt_store_16(acl_buffer, 4,  pos - 6 - 2);
178ab2b01dcS[email protected]     // 10 - L2CAP signaling parameter length
179ab2b01dcS[email protected]     bt_store_16(acl_buffer, 10, pos - 12);
180ab2b01dcS[email protected]     return pos;
181ab2b01dcS[email protected] }
182ab2b01dcS[email protected] #endif
183