xref: /btstack/test/hfp/mock.c (revision 3edc84c5b6b1e23a3d103fe8ce1f6b5ad1df3498)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 //
40 // HFP BTstack Mocks
41 //
42 // *****************************************************************************
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "hci.h"
50 #include "hci_dump.h"
51 #include "classic/sdp_query_rfcomm.h"
52 #include "classic/rfcomm.h"
53 #include "classic/hfp_hf.h"
54 
55 #include "mock.h"
56 
57 static void *registered_sdp_app_context;
58 static uint8_t sdp_rfcomm_channel_nr = 1;
59 const char sdp_rfcomm_service_name[] = "BTstackMock";
60 static uint16_t rfcomm_cid = 1;
61 static uint8_t rfcomm_payload[200];
62 static uint16_t rfcomm_payload_len;
63 void * active_connection;
64 
65 void (*registered_rfcomm_packet_handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
66 void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context);
67 
68 uint8_t * get_rfcomm_payload(){
69 	return &rfcomm_payload[0];
70 }
71 
72 uint16_t get_rfcomm_payload_len(){
73 	return rfcomm_payload_len;
74 }
75 
76 static void prepare_rfcomm_buffer(uint8_t * data, int len){
77     if (len <= 0) return;
78     memset(&rfcomm_payload, 0, 200);
79 	int pos = 0;
80 
81     if (strncmp((char*)data, "AT", 2) == 0){
82         strncpy((char*)&rfcomm_payload[pos], (char*)data, len);
83         pos += len;
84     } else {
85     	rfcomm_payload[pos++] = '\r';
86 		rfcomm_payload[pos++] = '\n';
87         strncpy((char*)&rfcomm_payload[pos], (char*)data, len);
88         pos += len;
89 
90         if (memcmp((char*)data, "+BAC", 4) != 0 &&
91             memcmp((char*)data, "+BCS", 4) != 0){
92             rfcomm_payload[pos++] = '\r';
93             rfcomm_payload[pos++] = '\n';
94             rfcomm_payload[pos++] = 'O';
95             rfcomm_payload[pos++] = 'K';
96         }
97 
98     }
99 	rfcomm_payload[pos++] = '\r';
100     rfcomm_payload[pos++] = '\n';
101     rfcomm_payload[pos] = 0;
102 	rfcomm_payload_len = pos;
103 }
104 
105 static void print_without_newlines(uint8_t *data, uint16_t len){
106     int found_newline = 0;
107     int found_item = 0;
108 
109     for (int i=0; i<len; i++){
110         if (data[i] == '\r' || data[i] == '\n'){
111             if (!found_newline && found_item) printf("\n");
112             found_newline = 1;
113         } else {
114             printf("%c", data[i]);
115             found_newline = 0;
116             found_item = 1;
117         }
118     }
119     printf("\n");
120 }
121 
122 int  rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
123 	if (strncmp((char*)data, "AT", 2) == 0){
124 		printf("Verify HF state machine response: ");
125         print_without_newlines(data,len);
126 	} else {
127         printf("Verify AG state machine response: ");
128         print_without_newlines(data,len);
129 	}
130 	strncpy((char*)&rfcomm_payload[0], (char*)data, len);
131     rfcomm_payload_len = len;
132 	return 0;
133 }
134 
135 int hci_send_cmd(const hci_cmd_t *cmd, ...){
136 	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
137 	return 0;
138 }
139 
140 
141 void sdp_query_rfcomm_register_callback(void(*sdp_app_callback)(sdp_query_event_t * event, void * context), void * context){
142 	registered_sdp_app_callback = sdp_app_callback;
143 	registered_sdp_app_context = context;
144 }
145 
146 static void sdp_query_complete_response(uint8_t status){
147     sdp_query_complete_event_t complete_event = {
148         SDP_QUERY_COMPLETE,
149         status
150     };
151     (*registered_sdp_app_callback)((sdp_query_event_t*)&complete_event, registered_sdp_app_context);
152 }
153 
154 static void sdp_query_rfcomm_service_response(uint8_t status){
155     sdp_query_rfcomm_service_event_t service_event = {
156         SDP_QUERY_RFCOMM_SERVICE,
157         sdp_rfcomm_channel_nr,
158         (uint8_t *)sdp_rfcomm_service_name
159     };
160     (*registered_sdp_app_callback)((sdp_query_event_t*)&service_event, registered_sdp_app_context);
161 }
162 
163 void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){
164 	// printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
165 	sdp_query_rfcomm_service_response(0);
166 	sdp_query_complete_response(0);
167 }
168 
169 void rfcomm_create_channel_internal(void * connection, bd_addr_t addr, uint8_t channel){
170 	// RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
171 	// printf("rfcomm_create_channel_internal\n");
172 	active_connection = connection;
173     uint8_t event[16];
174     uint8_t pos = 0;
175     event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
176     event[pos++] = sizeof(event) - 2;
177     event[pos++] = 0;
178 
179     bt_flip_addr(&event[pos], addr); pos += 6;
180     bt_store_16(event,  pos, 1);   pos += 2;
181 	event[pos++] = 0;
182 
183 	bt_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
184 	bt_store_16(event, pos, 200); pos += 2;   // max frame size
185     (*registered_rfcomm_packet_handler)(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
186 }
187 
188 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
189 	return 1;
190 }
191 
192 void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
193 	uint8_t event[4];
194 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
195     event[1] = sizeof(event) - 2;
196     bt_store_16(event, 2, rfcomm_cid);
197     (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
198 }
199 
200 void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
201 	registered_rfcomm_packet_handler = handler;
202 }
203 
204 void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){
205 	printf("rfcomm_register_service_internal\n");
206 }
207 
208 
209 void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
210 	printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n");
211 }
212 
213 
214 void rfcomm_accept_connection_internal(uint16_t rfcomm_cid){
215 	printf("rfcomm_accept_connection_internal \n");
216 }
217 
218 
219 void inject_rfcomm_command_to_hf(uint8_t * data, int len){
220     if (memcmp((char*)data, "AT", 2) == 0) return;
221 
222     prepare_rfcomm_buffer(data, len);
223     if (data[0] == '+' || (data[0] == 'O' && data[1] == 'K')){
224         printf("Send cmd to HF state machine: %s\n", data);
225     } else {
226         printf("Trigger HF state machine - %s", data);
227     }
228     (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
229 }
230 
231 void inject_rfcomm_command_to_ag(uint8_t * data, int len){
232     if (data[0] == '+') return;
233 
234     prepare_rfcomm_buffer(data, len);
235     if (memcmp((char*)data, "AT", 2) == 0){
236         printf("Send cmd to AG state machine: %s\n", data);
237     } else {
238         printf("Trigger AG state machine - %s", data);
239     }
240     (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
241 }
242 
243 
244 
245 
246