xref: /btstack/src/l2cap_signaling.c (revision b35f641c7f12d716b0e2e970eb512ae5fb5bd3f8)
1 /*
2  * Copyright (C) 2009 by Matthias Ringwald
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  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  l2cap_signaling.h
34  *
35  *  Created by Matthias Ringwald on 7/23/09.
36  */
37 
38 #include "l2cap_signaling.h"
39 
40 #include <string.h>
41 
42 static char *l2cap_signaling_commands_format[] = {
43 "D",    // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
44 "22",   // 0x02 connection request: PSM, Source CID
45 "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status
46 "22D",  // 0x04 config request: Dest CID, Flags, Configuration options
47 "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options
48 "22",   // 0x06 disconection request: Dest CID, Source CID
49 "22",   // 0x07 disconection response: Dest CID, Source CID
50 "D",    // 0x08 echo request: Data
51 "D",    // 0x09 echo response: Data
52 "2",    // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported}
53 "22D",  // 0x0b information response: InfoType, Result, Data
54 };
55 
56 uint8_t   sig_seq_nr  = 0xff;
57 uint16_t  source_cid  = 0x40;
58 
59 uint8_t l2cap_next_sig_id(void){
60     if (sig_seq_nr == 0xff) {
61         sig_seq_nr = 1;
62     } else {
63         sig_seq_nr++;
64     }
65     return sig_seq_nr;
66 }
67 
68 uint16_t l2cap_next_local_cid(void){
69     return source_cid++;
70 }
71 
72 uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
73 
74     // 0 - Connection handle : PB=10 : BC=00
75     bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
76     // 6 - L2CAP channel = 1
77     bt_store_16(acl_buffer, 6, 1);
78     // 8 - Code
79     acl_buffer[8] = cmd;
80     // 9 - id (!= 0 sequentially)
81     acl_buffer[9] = identifier;
82 
83     // 12 - L2CAP signaling parameters
84     uint16_t pos = 12;
85     const char *format = l2cap_signaling_commands_format[cmd-1];
86     uint16_t word;
87     uint8_t * ptr;
88     while (*format) {
89         switch(*format) {
90             case '1': //  8 bit value
91             case '2': // 16 bit value
92                 word = va_arg(argptr, int);
93                 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
94                 acl_buffer[pos++] = word & 0xff;
95                 if (*format == '2') {
96                     acl_buffer[pos++] = word >> 8;
97                 }
98                 break;
99             case 'D': // variable data. passed: len, ptr
100                 word = va_arg(argptr, int);
101                 ptr  = va_arg(argptr, uint8_t *);
102                 memcpy(&acl_buffer[pos], ptr, word);
103                 pos += word;
104                 break;
105             default:
106                 break;
107         }
108         format++;
109     };
110     va_end(argptr);
111 
112     // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length
113     // - the l2cap payload length is counted after the following channel id (only payload)
114 
115     // 2 - ACL length
116     bt_store_16(acl_buffer, 2,  pos - 4);
117     // 4 - L2CAP packet length
118     bt_store_16(acl_buffer, 4,  pos - 6 - 2);
119     // 10 - L2CAP signaling parameter length
120     bt_store_16(acl_buffer, 10, pos - 12);
121 
122     return pos;
123 }
124