1*1d3bd1e5SMatthias Ringwald
2*1d3bd1e5SMatthias Ringwald // *****************************************************************************
3*1d3bd1e5SMatthias Ringwald //
4*1d3bd1e5SMatthias Ringwald // test rfcomm query tests
5*1d3bd1e5SMatthias Ringwald //
6*1d3bd1e5SMatthias Ringwald // *****************************************************************************
7*1d3bd1e5SMatthias Ringwald
8*1d3bd1e5SMatthias Ringwald #include "btstack_config.h"
9*1d3bd1e5SMatthias Ringwald
10*1d3bd1e5SMatthias Ringwald #include <stdint.h>
11*1d3bd1e5SMatthias Ringwald #include <stdio.h>
12*1d3bd1e5SMatthias Ringwald #include <stdlib.h>
13*1d3bd1e5SMatthias Ringwald #include <string.h>
14*1d3bd1e5SMatthias Ringwald
15*1d3bd1e5SMatthias Ringwald #include "btstack_event.h"
16*1d3bd1e5SMatthias Ringwald #include "btstack_memory.h"
17*1d3bd1e5SMatthias Ringwald #include "btstack_run_loop.h"
18*1d3bd1e5SMatthias Ringwald #include "bluetooth_sdp.h"
19*1d3bd1e5SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
20*1d3bd1e5SMatthias Ringwald #include "classic/sdp_util.h"
21*1d3bd1e5SMatthias Ringwald #include "classic/spp_server.h"
22*1d3bd1e5SMatthias Ringwald #include "hci.h"
23*1d3bd1e5SMatthias Ringwald #include "hci_cmd.h"
24*1d3bd1e5SMatthias Ringwald #include "hci_dump.h"
25*1d3bd1e5SMatthias Ringwald #include "l2cap.h"
26*1d3bd1e5SMatthias Ringwald #include "mock.h"
27*1d3bd1e5SMatthias Ringwald
28*1d3bd1e5SMatthias Ringwald #include "CppUTest/TestHarness.h"
29*1d3bd1e5SMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
30*1d3bd1e5SMatthias Ringwald
31*1d3bd1e5SMatthias Ringwald static uint8_t service_index;
32*1d3bd1e5SMatthias Ringwald static uint8_t channel_nr[10];
33*1d3bd1e5SMatthias Ringwald static char* service_name[10];
34*1d3bd1e5SMatthias Ringwald
35*1d3bd1e5SMatthias Ringwald static uint8_t sdp_test_record_list[] = { 0x36, 0x02, 0xE7, 0x35, 0x48,
36*1d3bd1e5SMatthias Ringwald 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x0A, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19,
37*1d3bd1e5SMatthias Ringwald 0x01, 0x00, 0x09, 0x00, 0x19, 0x35, 0x06, 0x19, 0x00, 0x19, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05,
38*1d3bd1e5SMatthias Ringwald 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0D, 0x09,
39*1d3bd1e5SMatthias Ringwald 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x11, 0x41, 0x32, 0x44, 0x50, 0x20, 0x41, 0x75, 0x64, 0x69,
40*1d3bd1e5SMatthias Ringwald 0x6F, 0x20, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x35, 0x43, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
41*1d3bd1e5SMatthias Ringwald 0x11, 0x0C, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x17, 0x35,
42*1d3bd1e5SMatthias Ringwald 0x06, 0x19, 0x00, 0x17, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09,
43*1d3bd1e5SMatthias Ringwald 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0E, 0x09, 0x01, 0x03, 0x09, 0x01, 0x00, 0x25,
44*1d3bd1e5SMatthias Ringwald 0x0C, 0x41, 0x56, 0x52, 0x43, 0x50, 0x20, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x35, 0x56, 0x09,
45*1d3bd1e5SMatthias Ringwald 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x05, 0x09, 0x00, 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01,
46*1d3bd1e5SMatthias Ringwald 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0A, 0x35, 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05,
47*1d3bd1e5SMatthias Ringwald 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A,
48*1d3bd1e5SMatthias Ringwald 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x05, 0x09, 0x01, 0x00,
49*1d3bd1e5SMatthias Ringwald 0x09, 0x01, 0x00, 0x25, 0x10, 0x4F, 0x42, 0x45, 0x58, 0x20, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74,
50*1d3bd1e5SMatthias Ringwald 0x20, 0x50, 0x75, 0x73, 0x68, 0x35, 0x5A, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x1F, 0x19,
51*1d3bd1e5SMatthias Ringwald 0x12, 0x03, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00,
52*1d3bd1e5SMatthias Ringwald 0x03, 0x08, 0x02, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09,
53*1d3bd1e5SMatthias Ringwald 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x06, 0x19, 0x11,
54*1d3bd1e5SMatthias Ringwald 0x1E,
55*1d3bd1e5SMatthias Ringwald 0x09, 0x01, 0x05, 0x09, 0x01,
56*1d3bd1e5SMatthias Ringwald 0x00, 0x25, 0x18, 0x48, 0x61, 0x6E, 0x64, 0x73, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, 0x41, 0x75,
57*1d3bd1e5SMatthias Ringwald 0x64, 0x69, 0x6F, 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x27, 0x09, 0x00, 0x05,
58*1d3bd1e5SMatthias Ringwald 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x01, 0x00, 0x25, 0x1A, 0x41, 0x70, 0x70, 0x6C, 0x65, 0x20,
59*1d3bd1e5SMatthias Ringwald 0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62,
60*1d3bd1e5SMatthias Ringwald 0x75, 0x74, 0x65, 0x73, 0x35, 0x58, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x06, 0x09, 0x00,
61*1d3bd1e5SMatthias Ringwald 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0F, 0x35,
62*1d3bd1e5SMatthias Ringwald 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35,
63*1d3bd1e5SMatthias Ringwald 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35,
64*1d3bd1e5SMatthias Ringwald 0x06, 0x19, 0x11, 0x06, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x12, 0x4F, 0x42, 0x45, 0x58,
65*1d3bd1e5SMatthias Ringwald 0x20, 0x46, 0x69, 0x6C, 0x65, 0x20, 0x54, 0x72, 0x61, 0x6E, 0x73, 0x66, 0x65, 0x72, 0x35, 0x53,
66*1d3bd1e5SMatthias Ringwald 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x01, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19,
67*1d3bd1e5SMatthias Ringwald 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x03, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10,
68*1d3bd1e5SMatthias Ringwald 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09,
69*1d3bd1e5SMatthias Ringwald 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x01, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25,
70*1d3bd1e5SMatthias Ringwald 0x12, 0x42, 0x6C, 0x75, 0x65, 0x74, 0x6F, 0x6F, 0x74, 0x68, 0x2D, 0x50, 0x44, 0x41, 0x2D, 0x53,
71*1d3bd1e5SMatthias Ringwald 0x79, 0x6E, 0x63, 0x35, 0x59, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x12, 0x19, 0x12, 0x03,
72*1d3bd1e5SMatthias Ringwald 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08,
73*1d3bd1e5SMatthias Ringwald 0x04, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65,
74*1d3bd1e5SMatthias Ringwald 0x6E,
75*1d3bd1e5SMatthias Ringwald 0x09, 0x00, 0x6A, 0x09, 0x01,
76*1d3bd1e5SMatthias Ringwald 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x08, 0x09, 0x01, 0x02, 0x09, 0x01,
77*1d3bd1e5SMatthias Ringwald 0x00, 0x25, 0x15, 0x48, 0x65, 0x61, 0x64, 0x73, 0x65, 0x74, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6F,
78*1d3bd1e5SMatthias Ringwald 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x6F, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
79*1d3bd1e5SMatthias Ringwald 0x11, 0x17, 0x09, 0x00, 0x04, 0x35, 0x1E, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x0F, 0x35,
80*1d3bd1e5SMatthias Ringwald 0x14, 0x19, 0x00, 0x0F, 0x09, 0x01, 0x00, 0x35, 0x0C, 0x09, 0x08, 0x00, 0x09, 0x08, 0x06, 0x09,
81*1d3bd1e5SMatthias Ringwald 0x86, 0xDD, 0x09, 0x88, 0x0B, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06,
82*1d3bd1e5SMatthias Ringwald 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08,
83*1d3bd1e5SMatthias Ringwald 0x35, 0x06, 0x19, 0x11, 0x17, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x1C, 0x47, 0x72, 0x6F,
84*1d3bd1e5SMatthias Ringwald 0x75, 0x70, 0x20, 0x41, 0x64, 0x2D, 0x68, 0x6F, 0x63, 0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72,
85*1d3bd1e5SMatthias Ringwald 0x6B, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65
86*1d3bd1e5SMatthias Ringwald };
87*1d3bd1e5SMatthias Ringwald
88*1d3bd1e5SMatthias Ringwald
handle_query_rfcomm_event(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)89*1d3bd1e5SMatthias Ringwald void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
90*1d3bd1e5SMatthias Ringwald switch (packet[0]){
91*1d3bd1e5SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE:
92*1d3bd1e5SMatthias Ringwald channel_nr[service_index] = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
93*1d3bd1e5SMatthias Ringwald service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1);
94*1d3bd1e5SMatthias Ringwald strncpy(service_name[service_index], sdp_event_query_rfcomm_service_get_name(packet), SDP_SERVICE_NAME_LEN);
95*1d3bd1e5SMatthias Ringwald service_name[service_index][SDP_SERVICE_NAME_LEN] = 0;
96*1d3bd1e5SMatthias Ringwald // printf("CALLBACK: Service name: '%s', RFCOMM port %u, service index %d\n", service_name[service_index], channel_nr[service_index], service_index);
97*1d3bd1e5SMatthias Ringwald service_index++;
98*1d3bd1e5SMatthias Ringwald break;
99*1d3bd1e5SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE:
100*1d3bd1e5SMatthias Ringwald // printf("CALLBACK: Client query response done. \n");
101*1d3bd1e5SMatthias Ringwald break;
102*1d3bd1e5SMatthias Ringwald }
103*1d3bd1e5SMatthias Ringwald }
104*1d3bd1e5SMatthias Ringwald
105*1d3bd1e5SMatthias Ringwald
TEST_GROUP(SDPClient)106*1d3bd1e5SMatthias Ringwald TEST_GROUP(SDPClient){
107*1d3bd1e5SMatthias Ringwald uint8_t spp_buffer[sizeof(sdp_test_record_list)];
108*1d3bd1e5SMatthias Ringwald bd_addr_t address;
109*1d3bd1e5SMatthias Ringwald
110*1d3bd1e5SMatthias Ringwald void setup(void){
111*1d3bd1e5SMatthias Ringwald service_index = 0;
112*1d3bd1e5SMatthias Ringwald sdp_client_reset(); // avoid "not ready" warning
113*1d3bd1e5SMatthias Ringwald }
114*1d3bd1e5SMatthias Ringwald };
115*1d3bd1e5SMatthias Ringwald
116*1d3bd1e5SMatthias Ringwald
TEST(SDPClient,QueryRFCOMMWithSyntheticData)117*1d3bd1e5SMatthias Ringwald TEST(SDPClient, QueryRFCOMMWithSyntheticData){
118*1d3bd1e5SMatthias Ringwald const char* expected_name[] = {"SDP Response Test1", "SDP Response Test2", "SDP Response Test3"};
119*1d3bd1e5SMatthias Ringwald uint8_t expected_channel[] = {10, 11, 12};
120*1d3bd1e5SMatthias Ringwald int record_nr = sizeof(expected_channel)/sizeof(uint8_t);
121*1d3bd1e5SMatthias Ringwald int i;
122*1d3bd1e5SMatthias Ringwald
123*1d3bd1e5SMatthias Ringwald // start query using public API although data will be injected
124*1d3bd1e5SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, address, 0x1234);
125*1d3bd1e5SMatthias Ringwald
126*1d3bd1e5SMatthias Ringwald de_create_sequence(spp_buffer);
127*1d3bd1e5SMatthias Ringwald for (i=0; i<record_nr; i++){
128*1d3bd1e5SMatthias Ringwald uint8_t * record_start = de_push_sequence(spp_buffer);
129*1d3bd1e5SMatthias Ringwald spp_create_sdp_record(record_start, 0x10001, expected_channel[i], expected_name[i]);
130*1d3bd1e5SMatthias Ringwald de_pop_sequence(spp_buffer, record_start);
131*1d3bd1e5SMatthias Ringwald }
132*1d3bd1e5SMatthias Ringwald
133*1d3bd1e5SMatthias Ringwald sdp_parser_handle_chunk(spp_buffer, de_get_len(spp_buffer));
134*1d3bd1e5SMatthias Ringwald
135*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(record_nr, service_index);
136*1d3bd1e5SMatthias Ringwald for (i=0; i<service_index; i++){
137*1d3bd1e5SMatthias Ringwald STRCMP_EQUAL(expected_name[i], service_name[i]);
138*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(expected_channel[i], channel_nr[i]);
139*1d3bd1e5SMatthias Ringwald }
140*1d3bd1e5SMatthias Ringwald }
141*1d3bd1e5SMatthias Ringwald
TEST(SDPClient,QueryRFCOMMWithMacOSXData)142*1d3bd1e5SMatthias Ringwald TEST(SDPClient, QueryRFCOMMWithMacOSXData){
143*1d3bd1e5SMatthias Ringwald
144*1d3bd1e5SMatthias Ringwald const char* expected_name[] = {"OBEX Object Push",
145*1d3bd1e5SMatthias Ringwald "Hands Free Audio Gat", "OBEX File Transfer",
146*1d3bd1e5SMatthias Ringwald "Bluetooth-PDA-Sync", "Headset Audio Gatewa"};
147*1d3bd1e5SMatthias Ringwald uint8_t expected_channel[] = {10, 2, 15, 3, 4};
148*1d3bd1e5SMatthias Ringwald
149*1d3bd1e5SMatthias Ringwald // start query using public API although data will be injected
150*1d3bd1e5SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, address, 0x1234);
151*1d3bd1e5SMatthias Ringwald
152*1d3bd1e5SMatthias Ringwald // de_dump_data_element(sdp_test_record_list);
153*1d3bd1e5SMatthias Ringwald sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
154*1d3bd1e5SMatthias Ringwald
155*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(5, service_index);
156*1d3bd1e5SMatthias Ringwald int i;
157*1d3bd1e5SMatthias Ringwald for (i=0; i<service_index; i++){
158*1d3bd1e5SMatthias Ringwald STRCMP_EQUAL(expected_name[i], service_name[i]);
159*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(expected_channel[i], channel_nr[i]);
160*1d3bd1e5SMatthias Ringwald }
161*1d3bd1e5SMatthias Ringwald }
162*1d3bd1e5SMatthias Ringwald
TEST(SDPClient,QueryClassListValid)163*1d3bd1e5SMatthias Ringwald TEST(SDPClient, QueryClassListValid){
164*1d3bd1e5SMatthias Ringwald // start query using public API although data will be injected
165*1d3bd1e5SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, address, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY);
166*1d3bd1e5SMatthias Ringwald
167*1d3bd1e5SMatthias Ringwald // de_dump_data_element(sdp_test_record_list);
168*1d3bd1e5SMatthias Ringwald sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
169*1d3bd1e5SMatthias Ringwald
170*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(1, service_index);
171*1d3bd1e5SMatthias Ringwald }
172*1d3bd1e5SMatthias Ringwald
TEST(SDPClient,QueryClassListInvalid)173*1d3bd1e5SMatthias Ringwald TEST(SDPClient, QueryClassListInvalid){
174*1d3bd1e5SMatthias Ringwald // start query using public API although data will be injected
175*1d3bd1e5SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, address, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
176*1d3bd1e5SMatthias Ringwald sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
177*1d3bd1e5SMatthias Ringwald
178*1d3bd1e5SMatthias Ringwald CHECK_EQUAL(0, service_index);
179*1d3bd1e5SMatthias Ringwald }
180*1d3bd1e5SMatthias Ringwald
main(int argc,const char * argv[])181*1d3bd1e5SMatthias Ringwald int main (int argc, const char * argv[]){
182*1d3bd1e5SMatthias Ringwald return CommandLineTestRunner::RunAllTests(argc, argv);
183*1d3bd1e5SMatthias Ringwald }
184