1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
43
44
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ... */
#define BTSTACK_FILE__ "l2cap_signaling.c"
/* ... */
#include "l2cap_signaling.h"
#include "btstack_config.h"
#include "btstack_debug.h"
#include "hci.h"
#include <string.h>
5 includes
uint16_t l2cap_create_signaling_packet(uint8_t * acl_buffer, hci_con_handle_t handle, uint8_t pb_flags, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
static const char *l2cap_signaling_commands_format[] = {
"2D",
"22",
"2222",
"22D",
"222D",
"22",
"22",
"D",
"D",
"2",
"22D",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
"2222",
"2",
"22222",
"22222",
"22",
"2222C",
"2222C",
"22C",
"2",
#ifdef UNIT_TEST
"M",
#endif
...};
btstack_assert(0 < cmd);
uint8_t cmd_index = (uint8_t) cmd;
static const uint8_t num_l2cap_commands = (uint8_t) (sizeof(l2cap_signaling_commands_format) / sizeof(const char *));
btstack_assert(cmd_index <= num_l2cap_commands);
UNUSED(num_l2cap_commands);
const char *format = l2cap_signaling_commands_format[cmd_index -1u];
btstack_assert(format != NULL);
little_endian_store_16(acl_buffer, 0u, handle | (pb_flags << 12u) | (0u << 14u));
little_endian_store_16(acl_buffer, 6, cid);
acl_buffer[8] = cmd_index;
acl_buffer[9] = identifier;
uint16_t pos = 12;
uint16_t word;
uint8_t * ptr_u8;
uint16_t * ptr_u16;
while (*format) {
switch(*format) {
case '2':
word = va_arg(argptr, int);
acl_buffer[pos++] = word & 0xffu;
acl_buffer[pos++] = word >> 8;
break;case '2':
case 'C':
ptr_u16 = va_arg(argptr, uint16_t *);
while (*ptr_u16 != 0xffff){
little_endian_store_16(acl_buffer, pos, *ptr_u16);
ptr_u16++;
pos += 2;
}while (*ptr_u16 != 0xffff) { ... }
break;case 'C':
case 'D':
word = va_arg(argptr, int);
ptr_u8 = va_arg(argptr, uint8_t *);
(void)memcpy(&acl_buffer[pos], ptr_u8, word);
pos += word;
break;case 'D':
default:
btstack_unreachable();
break;default
}switch (*format) { ... }
format++;
}while (*format) { ... };
va_end(argptr);
little_endian_store_16(acl_buffer, 2u, pos - 4u);
little_endian_store_16(acl_buffer, 4u, pos - 6u - 2u);
little_endian_store_16(acl_buffer, 10u, pos - 12u);
return pos;
}{ ... }