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