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_client_rfcomm.h" 52 #include "classic/rfcomm.h" 53 #include "classic/hfp_hf.h" 54 #include "classic/sdp_client.h" 55 #include "classic/sdp_client_rfcomm.h" 56 57 #include "mock.h" 58 59 static uint8_t sdp_rfcomm_channel_nr = 1; 60 const char sdp_rfcomm_service_name[] = "BTstackMock"; 61 static uint16_t rfcomm_cid = 1; 62 static bd_addr_t dev_addr; 63 static uint16_t sco_handle = 10; 64 static uint8_t rfcomm_payload[1000]; 65 static uint16_t rfcomm_payload_len = 0; 66 67 static uint8_t outgoing_rfcomm_payload[1000]; 68 static uint16_t outgoing_rfcomm_payload_len = 0; 69 70 static uint8_t rfcomm_reserved_buffer[1000]; 71 72 hfp_connection_t * hfp_context; 73 74 void (*registered_hci_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 75 void (*registered_rfcomm_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 76 void (*registered_sdp_app_callback)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 77 78 uint8_t * get_rfcomm_payload(void){ 79 return &rfcomm_payload[0]; 80 } 81 82 uint16_t get_rfcomm_payload_len(void){ 83 return rfcomm_payload_len; 84 } 85 86 static int hfp_command_start_index = 0; 87 88 89 int has_more_hfp_commands(int start_command_offset, int end_command_offset){ 90 int has_cmd = get_rfcomm_payload_len() - hfp_command_start_index >= 2 + start_command_offset + end_command_offset; 91 //printf("has more: payload len %d, start %d, has more %d\n", get_rfcomm_payload_len(), hfp_command_start_index, has_cmd); 92 return has_cmd; 93 } 94 95 char * get_next_hfp_command(int start_command_offset, int end_command_offset){ 96 //printf("get next: payload len %d, start %d\n", get_rfcomm_payload_len(), hfp_command_start_index); 97 char * data = (char *)(&get_rfcomm_payload()[hfp_command_start_index + start_command_offset]); 98 int data_len = get_rfcomm_payload_len() - hfp_command_start_index - start_command_offset; 99 100 int i; 101 102 for (i = 0; i < data_len; i++){ 103 if ( *(data+i) == '\r' || *(data+i) == '\n' ) { 104 data[i]=0; 105 // update state 106 //printf("!!! command %s\n", data); 107 hfp_command_start_index = hfp_command_start_index + i + start_command_offset + end_command_offset; 108 return data; 109 } 110 } 111 printf("should not got here\n"); 112 return NULL; 113 } 114 115 void print_without_newlines(uint8_t *data, uint16_t len); 116 void print_without_newlines(uint8_t *data, uint16_t len){ 117 int found_newline = 0; 118 int found_item = 0; 119 120 for (int i=0; i<len; i++){ 121 if (data[i] == '\r' || data[i] == '\n'){ 122 if (!found_newline && found_item) printf("\n"); 123 found_newline = 1; 124 } else { 125 printf("%c", data[i]); 126 found_newline = 0; 127 found_item = 1; 128 } 129 } 130 printf("\n"); 131 } 132 133 extern "C" void l2cap_init(void){} 134 extern "C" void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 135 registered_hci_packet_handler = callback_handler->callback; 136 } 137 138 int rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 139 140 // printf("mock: rfcomm send: "); 141 // print_without_newlines(data, len); 142 143 int start_command_offset = 2; 144 int end_command_offset = 2; 145 146 if (strncmp((char*)data, "AT", 2) == 0){ 147 start_command_offset = 0; 148 } 149 150 if (has_more_hfp_commands(start_command_offset, end_command_offset)){ 151 //printf("Buffer response: "); 152 strncpy((char*)&rfcomm_payload[rfcomm_payload_len], (char*)data, len); 153 rfcomm_payload_len += len; 154 } else { 155 hfp_command_start_index = 0; 156 //printf("Copy response: "); 157 strncpy((char*)&rfcomm_payload[0], (char*)data, len); 158 rfcomm_payload_len = len; 159 } 160 161 // print_without_newlines(rfcomm_payload,rfcomm_payload_len); 162 return 0; 163 } 164 165 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid){ 166 167 // printf("mock: rfcomm_request_can_send_now_event\n"); 168 169 uint8_t event[] = { RFCOMM_EVENT_CAN_SEND_NOW, 2, 0, 0}; 170 little_endian_store_16(event, 2, rfcomm_cid); 171 registered_rfcomm_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 172 } 173 174 int rfcomm_reserve_packet_buffer(void){ 175 // printf("mock: rfcomm_reserve_packet_buffer\n"); 176 return 1; 177 }; 178 void rfcomm_release_packet_buffer(void){}; 179 uint8_t * rfcomm_get_outgoing_buffer(void) { 180 return rfcomm_reserved_buffer; 181 } 182 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){ 183 return sizeof(rfcomm_reserved_buffer); 184 } 185 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){ 186 // printf("--- rfcomm_send_prepared with len %u ---\n", len); 187 return rfcomm_send(rfcomm_cid, rfcomm_reserved_buffer, len); 188 } 189 190 static void hci_event_sco_complete(void){ 191 uint8_t event[19]; 192 uint8_t pos = 0; 193 event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE; 194 event[pos++] = sizeof(event) - 2; 195 196 event[pos++] = 0; //status 197 little_endian_store_16(event, pos, sco_handle); pos += 2; // sco handle 198 reverse_bd_addr(dev_addr, &event[pos]); pos += 6; 199 200 event[pos++] = 0; // link_type 201 event[pos++] = 0; // transmission_interval 202 event[pos++] = 0; // retransmission_interval 203 204 little_endian_store_16(event, pos, 0); pos += 2; // rx_packet_length 205 little_endian_store_16(event, pos, 0); pos += 2; // tx_packet_length 206 207 event[pos++] = 0; // air_mode 208 (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 209 } 210 211 int hci_send_cmd(const hci_cmd_t *cmd, ...){ 212 //printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 213 if (cmd->opcode == 0x428){ 214 hci_event_sco_complete(); 215 } 216 return 0; 217 } 218 219 int hci_can_send_command_packet_now(void){ 220 return 1; 221 } 222 223 static void sdp_query_complete_response(uint8_t status){ 224 uint8_t event[3]; 225 event[0] = SDP_EVENT_QUERY_COMPLETE; 226 event[1] = 1; 227 event[2] = status; 228 (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 229 } 230 231 static void sdp_client_query_rfcomm_service_response(uint8_t status){ 232 int sdp_service_name_len = strlen(sdp_rfcomm_service_name); 233 uint8_t event[3+SDP_SERVICE_NAME_LEN+1]; 234 event[0] = SDP_EVENT_QUERY_RFCOMM_SERVICE; 235 event[1] = sdp_service_name_len + 1; 236 event[2] = sdp_rfcomm_channel_nr; 237 memcpy(&event[3], sdp_rfcomm_service_name, sdp_service_name_len); 238 event[3+sdp_service_name_len] = 0; 239 (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 240 } 241 242 uint8_t sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(btstack_packet_handler_t callback, bd_addr_t remote, uint16_t uuid){ 243 // printf("sdp_client_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback); 244 registered_sdp_app_callback = callback; 245 sdp_client_query_rfcomm_service_response(0); 246 sdp_query_complete_response(0); 247 return 0; 248 } 249 250 uint8_t sdp_client_register_query_callback(btstack_context_callback_registration_t * callback_registration){ 251 (callback_registration->callback)(callback_registration->context); 252 return ERROR_CODE_SUCCESS; 253 } 254 255 uint8_t rfcomm_create_channel(btstack_packet_handler_t handler, bd_addr_t addr, uint8_t channel, uint16_t * out_cid){ 256 257 // printf("mock: rfcomm_create_channel addr %s\n", bd_addr_to_str(addr)); 258 259 registered_rfcomm_packet_handler = handler; 260 261 // RFCOMM_EVENT_CHANNEL_OPENED 262 uint8_t event[16]; 263 uint8_t pos = 0; 264 event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED; 265 event[pos++] = sizeof(event) - 2; 266 event[pos++] = 0; 267 268 reverse_bd_addr(addr, &event[pos]); 269 memcpy(dev_addr, addr, 6); 270 pos += 6; 271 272 little_endian_store_16(event, pos, 1); pos += 2; 273 event[pos++] = 0; 274 275 little_endian_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID 276 little_endian_store_16(event, pos, 200); pos += 2; // max frame size 277 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos); 278 279 if (out_cid){ 280 *out_cid = rfcomm_cid; 281 } 282 return 0; 283 } 284 285 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){ 286 // printf("mock: rfcomm_can_send_packet_now\n"); 287 return 1; 288 } 289 290 void rfcomm_disconnect(uint16_t rfcomm_cid){ 291 uint8_t event[4]; 292 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED; 293 event[1] = sizeof(event) - 2; 294 little_endian_store_16(event, 2, rfcomm_cid); 295 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); 296 } 297 298 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){ 299 // printf("rfcomm_register_service\n"); 300 registered_rfcomm_packet_handler = handler; 301 return 0; 302 } 303 304 305 uint8_t sdp_client_query_rfcomm_channel_and_name_for_search_pattern(btstack_packet_handler_t callback, bd_addr_t remote, const uint8_t * des_serviceSearchPattern){ 306 // printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n"); 307 return 0; 308 } 309 310 311 void rfcomm_accept_connection(uint16_t rfcomm_cid){ 312 // printf("rfcomm_accept_connection \n"); 313 } 314 315 void rfcomm_decline_connection(uint16_t rfcomm_cid){ 316 // printf("rfcomm_accept_connection \n"); 317 } 318 319 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 320 } 321 322 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 323 return 0; 324 } 325 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 326 } 327 328 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 329 } 330 331 332 static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 333 uint8_t event[6]; 334 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 335 event[1] = sizeof(event) - 2; 336 event[2] = 0; // status = OK 337 little_endian_store_16(event, 3, handle); 338 event[5] = reason; 339 (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 340 } 341 342 uint8_t gap_disconnect(hci_con_handle_t handle){ 343 hci_emit_disconnection_complete(handle, 0); 344 return 0; 345 } 346 347 uint16_t hci_get_sco_voice_setting(void){ 348 return 0x40; 349 } 350 351 int hci_remote_esco_supported(hci_con_handle_t handle){ 352 return 0; 353 } 354 355 static void add_new_lines_to_hfp_command(uint8_t * data, int len){ 356 if (len <= 0) return; 357 memset(&outgoing_rfcomm_payload, 0, 200); 358 int pos = 0; 359 360 if (strncmp((char*)data, "AT", 2) == 0){ 361 strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len); 362 pos += len; 363 } else { 364 outgoing_rfcomm_payload[pos++] = '\r'; 365 outgoing_rfcomm_payload[pos++] = '\n'; 366 strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len); 367 pos += len; 368 } 369 outgoing_rfcomm_payload[pos++] = '\r'; 370 outgoing_rfcomm_payload[pos++] = '\n'; 371 outgoing_rfcomm_payload[pos] = 0; 372 outgoing_rfcomm_payload_len = pos; 373 } 374 375 void inject_hfp_command_to_hf(uint8_t * data, int len){ 376 if (memcmp((char*)data, "AT", 2) == 0) return; 377 add_new_lines_to_hfp_command(data, len); 378 // printf("inject_hfp_command_to_hf to HF: "); 379 // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len); 380 (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len); 381 382 } 383 384 void inject_hfp_command_to_ag(uint8_t * data, int len){ 385 if (data[0] == '+') return; 386 387 add_new_lines_to_hfp_command(data, len); 388 389 // printf("mock: inject command to ag: "); 390 // print_without_newlines(data, len); 391 392 (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len); 393 } 394 395 396 int hci_extended_sco_link_supported(void){ 397 return 1; 398 } 399