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