xref: /btstack/src/l2cap_signaling.c (revision 95cbd9471c2c32d8765b35e216a4ac177f6d4d90)
1 /*
2  *  l2cap_signaling.h
3  *
4  *  Created by Matthias Ringwald on 7/23/09.
5  */
6 
7 #include "l2cap_signaling.h"
8 
9 #include <stdarg.h>
10 
11 static char *l2cap_signaling_commands_format[] = {
12 "D",    // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
13 "22",   // 0x02 connection request: PSM, Source CID
14 "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status
15 "22D",  // 0x04 config request: Dest CID, Flags, Configuration options
16 "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options
17 "22",   // 0x06 disconection request: Dest CID, Source CID
18 "22",   // 0x07 disconection response: Dest CID, Source CID
19 "D",    // 0x08 echo request: Data
20 "D",    // 0x09 echo response: Data
21 "2",    // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported}
22 "22D",  // 0x0b information response: InfoType, Result, Data
23 };
24 
25 uint8_t   sig_seq_nr = 1;
26 uint16_t  local_cid  = 0x40;
27 
28 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){
29 
30     // 0 - Connection handle : PB=10 : BC=00
31     bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
32     // 6 - L2CAP channel = 1
33     bt_store_16(acl_buffer, 6, 1);
34     // 8 - Code
35     acl_buffer[8] = cmd;
36     // 9 - id (!= 0 sequentially)
37     acl_buffer[9] = identifier;
38 
39     // 12 - L2CAP signaling parameters
40     uint16_t pos = 12;
41     const char *format = l2cap_signaling_commands_format[cmd-1];
42     uint16_t word;
43     uint8_t * ptr;
44     while (*format) {
45         switch(*format) {
46             case '1': //  8 bit value
47             case '2': // 16 bit value
48                 word = va_arg(argptr, int);
49                 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
50                 acl_buffer[pos++] = word & 0xff;
51                 if (*format == '2') {
52                     acl_buffer[pos++] = word >> 8;
53                 }
54                 break;
55             case 'D': // variable data. passed: len, ptr
56                 word = va_arg(argptr, int);
57                 ptr  = va_arg(argptr, uint8_t *);
58                 memcpy(&acl_buffer[pos], ptr, word);
59                 pos += word;
60                 break;
61             default:
62                 break;
63         }
64         format++;
65     };
66     va_end(argptr);
67 
68     // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length
69     // - the l2cap payload length is counted after the following channel id (only payload)
70 
71     // 2 - ACL length
72     bt_store_16(acl_buffer, 2,  pos - 4);
73     // 4 - L2CAP packet length
74     bt_store_16(acl_buffer, 4,  pos - 6 - 2);
75     // 10 - L2CAP signaling parameter length
76     bt_store_16(acl_buffer, 10, pos - 12);
77 
78     return pos;
79 }
80