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