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