xref: /btstack/test/l2cap-ecbm/l2cap_ecbm_test.cpp (revision af7c3ae6cd1e2bcfd24e6d0654508ecf775c9e5d)
1*af7c3ae6SMatthias Ringwald 
2*af7c3ae6SMatthias Ringwald // hal_cpu
3*af7c3ae6SMatthias Ringwald #include "hal_cpu.h"
4*af7c3ae6SMatthias Ringwald void hal_cpu_disable_irqs(void){}
5*af7c3ae6SMatthias Ringwald void hal_cpu_enable_irqs(void){}
6*af7c3ae6SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){}
7*af7c3ae6SMatthias Ringwald 
8*af7c3ae6SMatthias Ringwald // mock_sm.c
9*af7c3ae6SMatthias Ringwald #include "ble/sm.h"
10*af7c3ae6SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){}
11*af7c3ae6SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){}
12*af7c3ae6SMatthias Ringwald 
13*af7c3ae6SMatthias Ringwald // mock_hci_transport.h
14*af7c3ae6SMatthias Ringwald #include "hci_transport.h"
15*af7c3ae6SMatthias Ringwald void mock_hci_transport_receive_packet(uint8_t packet_type, uint8_t * packet, uint16_t size);
16*af7c3ae6SMatthias Ringwald const hci_transport_t * mock_hci_transport_mock_get_instance(void);
17*af7c3ae6SMatthias Ringwald 
18*af7c3ae6SMatthias Ringwald // mock_hci_transport.c
19*af7c3ae6SMatthias Ringwald #include <stddef.h>
20*af7c3ae6SMatthias Ringwald static uint8_t  mock_hci_transport_outgoing_packet_buffer[HCI_ACL_PAYLOAD_SIZE];
21*af7c3ae6SMatthias Ringwald static uint16_t mock_hci_transport_outgoing_packet_size;
22*af7c3ae6SMatthias Ringwald static uint8_t  mock_hci_transport_outgoing_packet_type;
23*af7c3ae6SMatthias Ringwald 
24*af7c3ae6SMatthias Ringwald static void (*mock_hci_transport_packet_handler)(uint8_t packet_type, uint8_t * packet, uint16_t size);
25*af7c3ae6SMatthias Ringwald static void mock_hci_transport_register_packet_handler(void (*packet_handler)(uint8_t packet_type, uint8_t * packet, uint16_t size)){
26*af7c3ae6SMatthias Ringwald     mock_hci_transport_packet_handler = packet_handler;
27*af7c3ae6SMatthias Ringwald }
28*af7c3ae6SMatthias Ringwald static int mock_hci_transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
29*af7c3ae6SMatthias Ringwald     mock_hci_transport_outgoing_packet_type = packet_type;
30*af7c3ae6SMatthias Ringwald     mock_hci_transport_outgoing_packet_size = size;
31*af7c3ae6SMatthias Ringwald     memcpy(mock_hci_transport_outgoing_packet_buffer, packet, size);
32*af7c3ae6SMatthias Ringwald     return 0;
33*af7c3ae6SMatthias Ringwald }
34*af7c3ae6SMatthias Ringwald const hci_transport_t * mock_hci_transport_mock_get_instance(void){
35*af7c3ae6SMatthias Ringwald     static hci_transport_t mock_hci_transport = {
36*af7c3ae6SMatthias Ringwald         /*  .transport.name                          = */  "mock",
37*af7c3ae6SMatthias Ringwald         /*  .transport.init                          = */  NULL,
38*af7c3ae6SMatthias Ringwald         /*  .transport.open                          = */  NULL,
39*af7c3ae6SMatthias Ringwald         /*  .transport.close                         = */  NULL,
40*af7c3ae6SMatthias Ringwald         /*  .transport.register_packet_handler       = */  &mock_hci_transport_register_packet_handler,
41*af7c3ae6SMatthias Ringwald         /*  .transport.can_send_packet_now           = */  NULL,
42*af7c3ae6SMatthias Ringwald         /*  .transport.send_packet                   = */  &mock_hci_transport_send_packet,
43*af7c3ae6SMatthias Ringwald         /*  .transport.set_baudrate                  = */  NULL,
44*af7c3ae6SMatthias Ringwald     };
45*af7c3ae6SMatthias Ringwald     return &mock_hci_transport;
46*af7c3ae6SMatthias Ringwald }
47*af7c3ae6SMatthias Ringwald void mock_hci_transport_receive_packet(uint8_t packet_type, const uint8_t * packet, uint16_t size){
48*af7c3ae6SMatthias Ringwald     (*mock_hci_transport_packet_handler)(packet_type, (uint8_t *) packet, size);
49*af7c3ae6SMatthias Ringwald }
50*af7c3ae6SMatthias Ringwald 
51*af7c3ae6SMatthias Ringwald // copy from hci.c
52*af7c3ae6SMatthias Ringwald static void mock_hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){
53*af7c3ae6SMatthias Ringwald     uint8_t event[6];
54*af7c3ae6SMatthias Ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
55*af7c3ae6SMatthias Ringwald     event[1] = sizeof(event) - 2u;
56*af7c3ae6SMatthias Ringwald     event[2] = 0; // status = OK
57*af7c3ae6SMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
58*af7c3ae6SMatthias Ringwald     event[5] = reason;
59*af7c3ae6SMatthias Ringwald     (*mock_hci_transport_packet_handler)(HCI_EVENT_PACKET, event, sizeof(event));
60*af7c3ae6SMatthias Ringwald }
61*af7c3ae6SMatthias Ringwald 
62*af7c3ae6SMatthias Ringwald //
63*af7c3ae6SMatthias Ringwald 
64*af7c3ae6SMatthias Ringwald #include <stdint.h>
65*af7c3ae6SMatthias Ringwald #include <stdio.h>
66*af7c3ae6SMatthias Ringwald #include <stdlib.h>
67*af7c3ae6SMatthias Ringwald #include <string.h>
68*af7c3ae6SMatthias Ringwald 
69*af7c3ae6SMatthias Ringwald #include "CppUTest/TestHarness.h"
70*af7c3ae6SMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
71*af7c3ae6SMatthias Ringwald #include "CppUTestExt/MockSupport.h"
72*af7c3ae6SMatthias Ringwald 
73*af7c3ae6SMatthias Ringwald #include "hci_dump.h"
74*af7c3ae6SMatthias Ringwald #include "btstack_debug.h"
75*af7c3ae6SMatthias Ringwald #include "l2cap.h"
76*af7c3ae6SMatthias Ringwald #include "btstack_memory.h"
77*af7c3ae6SMatthias Ringwald #include "btstack_run_loop_embedded.h"
78*af7c3ae6SMatthias Ringwald #include "hci_dump_posix_stdout.h"
79*af7c3ae6SMatthias Ringwald #include "btstack_event.h"
80*af7c3ae6SMatthias Ringwald 
81*af7c3ae6SMatthias Ringwald #define TEST_PACKET_SIZE       100
82*af7c3ae6SMatthias Ringwald #define HCI_CON_HANDLE_TEST_LE 0x0005
83*af7c3ae6SMatthias Ringwald #define HCI_CON_HANDLE_TEST_CLASSIC 0x0003
84*af7c3ae6SMatthias Ringwald #define TEST_PSM 0x1001
85*af7c3ae6SMatthias Ringwald 
86*af7c3ae6SMatthias Ringwald static bool l2cap_channel_accept_incoming;
87*af7c3ae6SMatthias Ringwald static uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS;
88*af7c3ae6SMatthias Ringwald static uint8_t data_channel_buffer_1[TEST_PACKET_SIZE];
89*af7c3ae6SMatthias Ringwald static uint8_t data_channel_buffer_2[TEST_PACKET_SIZE];
90*af7c3ae6SMatthias Ringwald static uint16_t l2cap_cids[10];
91*af7c3ae6SMatthias Ringwald static uint16_t num_l2cap_channel_opened;
92*af7c3ae6SMatthias Ringwald static uint16_t num_l2cap_channel_closed;
93*af7c3ae6SMatthias Ringwald static uint8_t * receive_buffers_2[] = { data_channel_buffer_1, data_channel_buffer_2 };
94*af7c3ae6SMatthias Ringwald static uint8_t * reconfigure_buffers_2[] = {data_channel_buffer_1, data_channel_buffer_2 };
95*af7c3ae6SMatthias Ringwald static uint8_t  received_packet[TEST_PACKET_SIZE];
96*af7c3ae6SMatthias Ringwald static uint16_t reconfigure_result;
97*af7c3ae6SMatthias Ringwald static btstack_packet_callback_registration_t l2cap_event_callback_registration;
98*af7c3ae6SMatthias Ringwald 
99*af7c3ae6SMatthias Ringwald // two channels with cids 0x041 and 0x042
100*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_conn_request_1[] = {
101*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x14, 0x00, 0x10, 0x00, 0x05, 0x00, 0x17, 0x01, 0x0c, 0x00, 0x01, 0x10, 0x64, 0x00,
102*af7c3ae6SMatthias Ringwald         0x30, 0x00, 0xff, 0xff, 0x41, 0x00, 0x42, 0x00
103*af7c3ae6SMatthias Ringwald };
104*af7c3ae6SMatthias Ringwald 
105*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_conn_request_1[] = {
106*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x14, 0x00, 0x10, 0x00, 0x01, 0x00, 0x17, 0x01, 0x0c, 0x00, 0x01, 0x10, 0x64, 0x00,
107*af7c3ae6SMatthias Ringwald         0x30, 0x00, 0xff, 0xff, 0x41, 0x00, 0x42, 0x00
108*af7c3ae6SMatthias Ringwald };
109*af7c3ae6SMatthias Ringwald 
110*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_conn_response_2_failed[] = {
111*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x14, 0x00, 0x10, 0x00, 0x05, 0x00, 0x18, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
112*af7c3ae6SMatthias Ringwald         0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00
113*af7c3ae6SMatthias Ringwald };
114*af7c3ae6SMatthias Ringwald 
115*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_conn_response_2_success[] = {
116*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x14, 0x00, 0x10, 0x00, 0x05, 0x00, 0x18, 0x01, 0x0c, 0x00, 0x64, 0x00, 0x30, 0x00,
117*af7c3ae6SMatthias Ringwald         0xff, 0xff, 0x00, 0x00, 0x41, 0x00, 0x42, 0x00
118*af7c3ae6SMatthias Ringwald };
119*af7c3ae6SMatthias Ringwald 
120*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_conn_response_2_success[] = {
121*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x14, 0x00, 0x10, 0x00, 0x01, 0x00, 0x18, 0x01, 0x0c, 0x00, 0x64, 0x00, 0x30, 0x00,
122*af7c3ae6SMatthias Ringwald         0xff, 0xff, 0x00, 0x00, 0x41, 0x00, 0x42, 0x00
123*af7c3ae6SMatthias Ringwald };
124*af7c3ae6SMatthias Ringwald 
125*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_credits[] = {
126*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00,  0x16, 0x02, 0x04, 0x00,  0x41, 0x00, 0x05, 0x00,
127*af7c3ae6SMatthias Ringwald };
128*af7c3ae6SMatthias Ringwald 
129*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_credits[] = {
130*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00,  0x16, 0x02, 0x04, 0x00,  0x41, 0x00, 0x05, 0x00,
131*af7c3ae6SMatthias Ringwald };
132*af7c3ae6SMatthias Ringwald 
133*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_single_packet[] = {
134*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0b, 0x00, 0x07, 0x00, 0x41, 0x00, 0x05, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f
135*af7c3ae6SMatthias Ringwald };
136*af7c3ae6SMatthias Ringwald 
137*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_single_packet[] = {
138*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0b, 0x00, 0x07, 0x00, 0x41, 0x00, 0x05, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f
139*af7c3ae6SMatthias Ringwald };
140*af7c3ae6SMatthias Ringwald 
141*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_disconnect_request_1[] = {
142*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x06, 0x02, 0x04, 0x00, 0x41, 0x00, 0x41, 0x00
143*af7c3ae6SMatthias Ringwald };
144*af7c3ae6SMatthias Ringwald 
145*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_disconnect_response_1[] = {
146*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x07, 0x02, 0x04, 0x00, 0x41, 0x00, 0x41, 0x00
147*af7c3ae6SMatthias Ringwald };
148*af7c3ae6SMatthias Ringwald 
149*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_disconnect_request_2[] = {
150*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x06, 0x03, 0x04, 0x00, 0x42, 0x00, 0x42, 0x00
151*af7c3ae6SMatthias Ringwald };
152*af7c3ae6SMatthias Ringwald 
153*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_disconnect_response_2[] = {
154*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x07, 0x03, 0x04, 0x00, 0x42, 0x00, 0x42, 0x00
155*af7c3ae6SMatthias Ringwald };
156*af7c3ae6SMatthias Ringwald 
157*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_disconnect_request_1[] = {
158*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00, 0x06, 0x02, 0x04, 0x00, 0x41, 0x00, 0x41, 0x00
159*af7c3ae6SMatthias Ringwald };
160*af7c3ae6SMatthias Ringwald 
161*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_disconnect_response_1[] = {
162*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00, 0x07, 0x02, 0x04, 0x00, 0x41, 0x00, 0x41, 0x00
163*af7c3ae6SMatthias Ringwald };
164*af7c3ae6SMatthias Ringwald 
165*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_disconnect_request_2[] = {
166*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00, 0x06, 0x03, 0x04, 0x00, 0x42, 0x00, 0x42, 0x00
167*af7c3ae6SMatthias Ringwald };
168*af7c3ae6SMatthias Ringwald 
169*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_disconnect_response_2[] = {
170*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00, 0x07, 0x03, 0x04, 0x00, 0x42, 0x00, 0x42, 0x00
171*af7c3ae6SMatthias Ringwald };
172*af7c3ae6SMatthias Ringwald 
173*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_renegotiate_request[] = {
174*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x10, 0x00, 0x0c, 0x00, 0x05, 0x00, 0x19, 0x02, 0x08, 0x00, 0x64, 0x00, 0x30, 0x00,
175*af7c3ae6SMatthias Ringwald         0x41, 0x00, 0x42, 0x00
176*af7c3ae6SMatthias Ringwald };
177*af7c3ae6SMatthias Ringwald 
178*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_renegotiate_request[] = {
179*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x10, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x19, 0x02, 0x08, 0x00, 0x64, 0x00, 0x30, 0x00,
180*af7c3ae6SMatthias Ringwald         0x41, 0x00, 0x42, 0x00
181*af7c3ae6SMatthias Ringwald };
182*af7c3ae6SMatthias Ringwald 
183*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_le_renegotiate_response[] = {
184*af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x0a, 0x00, 0x06, 0x00, 0x05, 0x00, 0x1a, 0x02, 0x02, 0x00, 0x00, 0x00
185*af7c3ae6SMatthias Ringwald };
186*af7c3ae6SMatthias Ringwald 
187*af7c3ae6SMatthias Ringwald const uint8_t l2cap_enhanced_data_channel_classic_renegotiate_response[] = {
188*af7c3ae6SMatthias Ringwald         0x03, 0x20, 0x0a, 0x00, 0x06, 0x00, 0x01, 0x00, 0x1a, 0x02, 0x02, 0x00, 0x00, 0x00
189*af7c3ae6SMatthias Ringwald };
190*af7c3ae6SMatthias Ringwald 
191*af7c3ae6SMatthias Ringwald static void fix_boundary_flags(uint8_t * packet, uint16_t size){
192*af7c3ae6SMatthias Ringwald     uint8_t acl_flags = packet[1] >> 4;
193*af7c3ae6SMatthias Ringwald     if (acl_flags == 0){
194*af7c3ae6SMatthias Ringwald         acl_flags = 2;  // first fragment
195*af7c3ae6SMatthias Ringwald     }
196*af7c3ae6SMatthias Ringwald     packet[1] = (packet[1] & 0x0f) | (acl_flags << 4);
197*af7c3ae6SMatthias Ringwald }
198*af7c3ae6SMatthias Ringwald 
199*af7c3ae6SMatthias Ringwald static void print_acl(const char * name, const uint8_t * packet, uint16_t size){
200*af7c3ae6SMatthias Ringwald     printf("const uint8_t %s[] = {", name);
201*af7c3ae6SMatthias Ringwald     uint16_t i;
202*af7c3ae6SMatthias Ringwald     for (i=0;i<size;i++){
203*af7c3ae6SMatthias Ringwald         if (i != 0){
204*af7c3ae6SMatthias Ringwald             printf(", ");
205*af7c3ae6SMatthias Ringwald         }
206*af7c3ae6SMatthias Ringwald         if ((i % 16) == 0){
207*af7c3ae6SMatthias Ringwald             printf("\n    ");
208*af7c3ae6SMatthias Ringwald         }
209*af7c3ae6SMatthias Ringwald         printf("0x%02x", packet[i]);
210*af7c3ae6SMatthias Ringwald     }
211*af7c3ae6SMatthias Ringwald     printf("\n};\n");
212*af7c3ae6SMatthias Ringwald }
213*af7c3ae6SMatthias Ringwald 
214*af7c3ae6SMatthias Ringwald // print packet for test inclusion
215*af7c3ae6SMatthias Ringwald // fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
216*af7c3ae6SMatthias Ringwald // print_acl("l2cap_enhanced_data_channel_le_single_packet", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
217*af7c3ae6SMatthias Ringwald 
218*af7c3ae6SMatthias Ringwald static void l2cap_channel_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
219*af7c3ae6SMatthias Ringwald     UNUSED(channel);
220*af7c3ae6SMatthias Ringwald     UNUSED(size);
221*af7c3ae6SMatthias Ringwald     uint16_t psm;
222*af7c3ae6SMatthias Ringwald     uint16_t cid;
223*af7c3ae6SMatthias Ringwald     uint16_t cids[5];
224*af7c3ae6SMatthias Ringwald     switch (packet_type) {
225*af7c3ae6SMatthias Ringwald         case HCI_EVENT_PACKET:
226*af7c3ae6SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
227*af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
228*af7c3ae6SMatthias Ringwald                     printf("L2CAP_EVENT_DATA_CHANNEL_INCOMING\n");
229*af7c3ae6SMatthias Ringwald                     cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet);
230*af7c3ae6SMatthias Ringwald                     if (l2cap_channel_accept_incoming){
231*af7c3ae6SMatthias Ringwald                         l2cap_ecbm_accept_channels(cid, 2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
232*af7c3ae6SMatthias Ringwald                     } else {
233*af7c3ae6SMatthias Ringwald                         // reject request by providing zero buffers
234*af7c3ae6SMatthias Ringwald                         l2cap_ecbm_accept_channels(cid, 0, 0, 0,NULL, NULL);
235*af7c3ae6SMatthias Ringwald                     }
236*af7c3ae6SMatthias Ringwald                     break;
237*af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
238*af7c3ae6SMatthias Ringwald                     l2cap_cids[num_l2cap_channel_opened] = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
239*af7c3ae6SMatthias Ringwald                     printf("L2CAP_EVENT_DATA_CHANNEL_OPENED - cid 0x%04x\n", l2cap_cids[num_l2cap_channel_opened] );
240*af7c3ae6SMatthias Ringwald                     num_l2cap_channel_opened++;
241*af7c3ae6SMatthias Ringwald                     break;
242*af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_ECBM_RECONFIGURATION_COMPLETE:
243*af7c3ae6SMatthias Ringwald                     reconfigure_result = l2cap_event_ecbm_reconfiguration_complete_get_reconfigure_result(packet);
244*af7c3ae6SMatthias Ringwald                     break;
245*af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
246*af7c3ae6SMatthias Ringwald                     num_l2cap_channel_closed++;
247*af7c3ae6SMatthias Ringwald                     break;
248*af7c3ae6SMatthias Ringwald                 default:
249*af7c3ae6SMatthias Ringwald                     break;
250*af7c3ae6SMatthias Ringwald             }
251*af7c3ae6SMatthias Ringwald             break;
252*af7c3ae6SMatthias Ringwald         case L2CAP_DATA_PACKET:
253*af7c3ae6SMatthias Ringwald             printf("Pacekt received (%u bytes): ", size);
254*af7c3ae6SMatthias Ringwald             printf_hexdump(packet, size);
255*af7c3ae6SMatthias Ringwald             memcpy(received_packet, packet, size);
256*af7c3ae6SMatthias Ringwald             break;
257*af7c3ae6SMatthias Ringwald         default:
258*af7c3ae6SMatthias Ringwald             break;
259*af7c3ae6SMatthias Ringwald     }
260*af7c3ae6SMatthias Ringwald }
261*af7c3ae6SMatthias Ringwald 
262*af7c3ae6SMatthias Ringwald TEST_GROUP(L2CAP_CHANNELS){
263*af7c3ae6SMatthias Ringwald     const hci_transport_t * hci_transport;
264*af7c3ae6SMatthias Ringwald     void setup(void){
265*af7c3ae6SMatthias Ringwald         btstack_memory_init();
266*af7c3ae6SMatthias Ringwald         btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
267*af7c3ae6SMatthias Ringwald         hci_transport = mock_hci_transport_mock_get_instance();
268*af7c3ae6SMatthias Ringwald         hci_init(hci_transport, NULL);
269*af7c3ae6SMatthias Ringwald         l2cap_init();
270*af7c3ae6SMatthias Ringwald         l2cap_event_callback_registration.callback = &l2cap_channel_packet_handler;
271*af7c3ae6SMatthias Ringwald         l2cap_add_event_handler(&l2cap_event_callback_registration);
272*af7c3ae6SMatthias Ringwald         l2cap_register_fixed_channel(&l2cap_channel_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
273*af7c3ae6SMatthias Ringwald         hci_dump_init(hci_dump_posix_stdout_get_instance());
274*af7c3ae6SMatthias Ringwald         num_l2cap_channel_opened = 0;
275*af7c3ae6SMatthias Ringwald         num_l2cap_channel_closed = 0;
276*af7c3ae6SMatthias Ringwald         memset(received_packet, 0, sizeof(received_packet));
277*af7c3ae6SMatthias Ringwald     }
278*af7c3ae6SMatthias Ringwald     void teardown(void){
279*af7c3ae6SMatthias Ringwald         l2cap_deinit();
280*af7c3ae6SMatthias Ringwald         hci_deinit();
281*af7c3ae6SMatthias Ringwald         btstack_memory_deinit();
282*af7c3ae6SMatthias Ringwald         btstack_run_loop_deinit();
283*af7c3ae6SMatthias Ringwald     }
284*af7c3ae6SMatthias Ringwald };
285*af7c3ae6SMatthias Ringwald 
286*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, no_connection){
287*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
288*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
289*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
290*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
291*af7c3ae6SMatthias Ringwald }
292*af7c3ae6SMatthias Ringwald 
293*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_2_success){
294*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
295*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
296*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
297*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
298*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
299*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
300*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
301*af7c3ae6SMatthias Ringwald }
302*af7c3ae6SMatthias Ringwald 
303*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_2_success){
304*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
305*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
306*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
307*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
308*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
309*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
310*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
311*af7c3ae6SMatthias Ringwald }
312*af7c3ae6SMatthias Ringwald 
313*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_no_service){
314*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
315*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_request_1, sizeof(l2cap_enhanced_data_channel_le_conn_request_1));
316*af7c3ae6SMatthias Ringwald }
317*af7c3ae6SMatthias Ringwald 
318*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_classic_no_service){
319*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
320*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_request_1, sizeof(l2cap_enhanced_data_channel_classic_conn_request_1));
321*af7c3ae6SMatthias Ringwald }
322*af7c3ae6SMatthias Ringwald 
323*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_1){
324*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
325*af7c3ae6SMatthias Ringwald     l2cap_ecbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, TEST_PACKET_SIZE, LEVEL_0);
326*af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = false;
327*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_request_1, sizeof(l2cap_enhanced_data_channel_le_conn_request_1));
328*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(0, num_l2cap_channel_opened);
329*af7c3ae6SMatthias Ringwald }
330*af7c3ae6SMatthias Ringwald 
331*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_2){
332*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
333*af7c3ae6SMatthias Ringwald     l2cap_ecbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, TEST_PACKET_SIZE, LEVEL_0);
334*af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = true;
335*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_request_1, sizeof(l2cap_enhanced_data_channel_le_conn_request_1));
336*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
337*af7c3ae6SMatthias Ringwald }
338*af7c3ae6SMatthias Ringwald 
339*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_receive_credits){
340*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
341*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
342*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
343*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
344*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
345*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
346*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
347*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_credits, sizeof(l2cap_enhanced_data_channel_le_credits));
348*af7c3ae6SMatthias Ringwald }
349*af7c3ae6SMatthias Ringwald 
350*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_receive_credits){
351*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
352*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
353*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
354*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
355*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
356*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
357*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
358*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_credits, sizeof(l2cap_enhanced_data_channel_classic_credits));
359*af7c3ae6SMatthias Ringwald }
360*af7c3ae6SMatthias Ringwald 
361*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_send_packet){
362*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
363*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
364*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
365*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
366*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
367*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
368*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
369*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_credits, sizeof(l2cap_enhanced_data_channel_le_credits));
370*af7c3ae6SMatthias Ringwald     status = l2cap_send(l2cap_cids[0], (const uint8_t *) "hello", 5);
371*af7c3ae6SMatthias Ringwald }
372*af7c3ae6SMatthias Ringwald 
373*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_send_packet){
374*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
375*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
376*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
377*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
378*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
379*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
380*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
381*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_credits, sizeof(l2cap_enhanced_data_channel_classic_credits));
382*af7c3ae6SMatthias Ringwald     status = l2cap_send(l2cap_cids[0], (const uint8_t *) "hello", 5);
383*af7c3ae6SMatthias Ringwald }
384*af7c3ae6SMatthias Ringwald 
385*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_receive_packet){
386*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
387*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
388*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
389*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
390*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
391*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
392*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
393*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_single_packet, sizeof(l2cap_enhanced_data_channel_le_single_packet));
394*af7c3ae6SMatthias Ringwald     MEMCMP_EQUAL("hello", received_packet, 5);
395*af7c3ae6SMatthias Ringwald }
396*af7c3ae6SMatthias Ringwald 
397*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_receive_packet){
398*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
399*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
400*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
401*af7c3ae6SMatthias Ringwald                                                     2, initial_credits, TEST_PACKET_SIZE, receive_buffers_2, cids);
402*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
403*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
404*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
405*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_single_packet, sizeof(l2cap_enhanced_data_channel_classic_single_packet));
406*af7c3ae6SMatthias Ringwald     MEMCMP_EQUAL("hello", received_packet, 5);
407*af7c3ae6SMatthias Ringwald }
408*af7c3ae6SMatthias Ringwald 
409*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_provide_credits){
410*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
411*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
412*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
413*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
414*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
415*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
416*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
417*af7c3ae6SMatthias Ringwald     status = l2cap_ecbm_provide_credits(l2cap_cids[0], 5);
418*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
419*af7c3ae6SMatthias Ringwald }
420*af7c3ae6SMatthias Ringwald 
421*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_provide_credits){
422*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
423*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
424*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
425*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
426*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
427*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
428*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
429*af7c3ae6SMatthias Ringwald     status = l2cap_ecbm_provide_credits(l2cap_cids[0], 5);
430*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
431*af7c3ae6SMatthias Ringwald }
432*af7c3ae6SMatthias Ringwald 
433*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_renegotiate_initiator){
434*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
435*af7c3ae6SMatthias Ringwald     reconfigure_result = 0xffff;
436*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
437*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
438*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
439*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
440*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
441*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
442*af7c3ae6SMatthias Ringwald     status = l2cap_ecbm_reconfigure_channels(2, l2cap_cids, TEST_PACKET_SIZE/2, reconfigure_buffers_2);
443*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
444*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_renegotiate_response, sizeof(l2cap_enhanced_data_channel_le_renegotiate_response));
445*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(0, reconfigure_result);
446*af7c3ae6SMatthias Ringwald }
447*af7c3ae6SMatthias Ringwald 
448*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_renegotiate_initiator){
449*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
450*af7c3ae6SMatthias Ringwald     reconfigure_result = 0xffff;
451*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
452*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
453*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
454*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
455*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
456*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
457*af7c3ae6SMatthias Ringwald     status = l2cap_ecbm_reconfigure_channels(2, l2cap_cids, TEST_PACKET_SIZE/2, reconfigure_buffers_2);
458*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
459*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_renegotiate_response, sizeof(l2cap_enhanced_data_channel_classic_renegotiate_response));
460*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(0, reconfigure_result);
461*af7c3ae6SMatthias Ringwald }
462*af7c3ae6SMatthias Ringwald 
463*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_renegotiate_responder){
464*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
465*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
466*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
467*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
468*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
469*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
470*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
471*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_renegotiate_request, sizeof(l2cap_enhanced_data_channel_le_renegotiate_request));
472*af7c3ae6SMatthias Ringwald     fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
473*af7c3ae6SMatthias Ringwald     print_acl("l2cap_enhanced_data_channel_le_renegotiate_response", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
474*af7c3ae6SMatthias Ringwald }
475*af7c3ae6SMatthias Ringwald 
476*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_renegotiate_responder){
477*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
478*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
479*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
480*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
481*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
482*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
483*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
484*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_renegotiate_request, sizeof(l2cap_enhanced_data_channel_classic_renegotiate_request));
485*af7c3ae6SMatthias Ringwald     fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
486*af7c3ae6SMatthias Ringwald     print_acl("l2cap_enhanced_data_channel_classic_renegotiate_response", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
487*af7c3ae6SMatthias Ringwald }
488*af7c3ae6SMatthias Ringwald 
489*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_disconnect_initiator){
490*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
491*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
492*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
493*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
494*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
495*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
496*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
497*af7c3ae6SMatthias Ringwald     l2cap_disconnect(l2cap_cids[0]);
498*af7c3ae6SMatthias Ringwald     l2cap_disconnect(l2cap_cids[1]);
499*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_disconnect_response_1, sizeof(l2cap_enhanced_data_channel_le_disconnect_response_1));
500*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_disconnect_response_2, sizeof(l2cap_enhanced_data_channel_le_disconnect_response_2));
501*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
502*af7c3ae6SMatthias Ringwald }
503*af7c3ae6SMatthias Ringwald 
504*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_disconnect_responder){
505*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
506*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
507*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
508*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
509*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
510*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
511*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
512*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_disconnect_request_1, sizeof(l2cap_enhanced_data_channel_le_disconnect_request_1));
513*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_disconnect_request_2, sizeof(l2cap_enhanced_data_channel_le_disconnect_request_2));
514*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
515*af7c3ae6SMatthias Ringwald }
516*af7c3ae6SMatthias Ringwald 
517*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_disconnect_initiator){
518*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
519*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
520*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
521*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
522*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
523*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
524*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
525*af7c3ae6SMatthias Ringwald     l2cap_disconnect(l2cap_cids[0]);
526*af7c3ae6SMatthias Ringwald     l2cap_disconnect(l2cap_cids[1]);
527*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_disconnect_response_1, sizeof(l2cap_enhanced_data_channel_classic_disconnect_response_1));
528*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_disconnect_response_2, sizeof(l2cap_enhanced_data_channel_classic_disconnect_response_2));
529*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
530*af7c3ae6SMatthias Ringwald }
531*af7c3ae6SMatthias Ringwald 
532*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_disconnect_responder){
533*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
534*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
535*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
536*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
537*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
538*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
539*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
540*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_disconnect_request_1, sizeof(l2cap_enhanced_data_channel_classic_disconnect_request_1));
541*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_disconnect_request_2, sizeof(l2cap_enhanced_data_channel_classic_disconnect_request_2));
542*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
543*af7c3ae6SMatthias Ringwald }
544*af7c3ae6SMatthias Ringwald 
545*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_le_hci_disconnect){
546*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
547*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
548*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, LEVEL_0, TEST_PSM,
549*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
550*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
551*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_le_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_le_conn_response_2_success));
552*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
553*af7c3ae6SMatthias Ringwald     mock_hci_emit_disconnection_complete(HCI_CON_HANDLE_TEST_LE, 0x13);
554*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
555*af7c3ae6SMatthias Ringwald }
556*af7c3ae6SMatthias Ringwald 
557*af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_classic_hci_disconnect){
558*af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
559*af7c3ae6SMatthias Ringwald     uint16_t cids[2];
560*af7c3ae6SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_CLASSIC, LEVEL_0, TEST_PSM,
561*af7c3ae6SMatthias Ringwald                                                     2, 0, TEST_PACKET_SIZE, receive_buffers_2, cids);
562*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
563*af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, l2cap_enhanced_data_channel_classic_conn_response_2_success, sizeof(l2cap_enhanced_data_channel_classic_conn_response_2_success));
564*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_opened);
565*af7c3ae6SMatthias Ringwald     mock_hci_emit_disconnection_complete(HCI_CON_HANDLE_TEST_CLASSIC, 0x13);
566*af7c3ae6SMatthias Ringwald     CHECK_EQUAL(2, num_l2cap_channel_closed);
567*af7c3ae6SMatthias Ringwald }
568*af7c3ae6SMatthias Ringwald 
569*af7c3ae6SMatthias Ringwald int main (int argc, const char * argv[]){
570*af7c3ae6SMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
571*af7c3ae6SMatthias Ringwald }
572