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 <btstack/btstack.h> 50 #include "hci.h" 51 #include "hci_dump.h" 52 #include "sdp_query_rfcomm.h" 53 #include "rfcomm.h" 54 #include "hfp_hf.h" 55 56 #include "mock.h" 57 58 static void *registered_sdp_app_context; 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[200]; 65 static uint16_t rfcomm_payload_len; 66 void * active_connection; 67 hfp_connection_t * hfp_context; 68 69 void (*registered_rfcomm_packet_handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 70 void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context); 71 72 uint8_t * get_rfcomm_payload(){ 73 return &rfcomm_payload[0]; 74 } 75 76 uint16_t get_rfcomm_payload_len(){ 77 return rfcomm_payload_len; 78 } 79 80 static void prepare_rfcomm_buffer(uint8_t * data, int len){ 81 if (len <= 0) return; 82 memset(&rfcomm_payload, 0, 200); 83 int pos = 0; 84 85 if (strncmp((char*)data, "AT", 2) == 0){ 86 strncpy((char*)&rfcomm_payload[pos], (char*)data, len); 87 pos += len; 88 } else { 89 rfcomm_payload[pos++] = '\r'; 90 rfcomm_payload[pos++] = '\n'; 91 strncpy((char*)&rfcomm_payload[pos], (char*)data, len); 92 pos += len; 93 94 if (memcmp((char*)data, "+BAC", 4) != 0 && 95 memcmp((char*)data, "+BCS", 4) != 0){ 96 rfcomm_payload[pos++] = '\r'; 97 rfcomm_payload[pos++] = '\n'; 98 rfcomm_payload[pos++] = 'O'; 99 rfcomm_payload[pos++] = 'K'; 100 } 101 102 } 103 rfcomm_payload[pos++] = '\r'; 104 rfcomm_payload[pos++] = '\n'; 105 rfcomm_payload[pos] = 0; 106 rfcomm_payload_len = pos; 107 } 108 109 static void print_without_newlines(uint8_t *data, uint16_t len){ 110 int found_newline = 0; 111 int found_item = 0; 112 113 for (int i=0; i<len; i++){ 114 if (data[i] == '\r' || data[i] == '\n'){ 115 if (!found_newline && found_item) printf("\n"); 116 found_newline = 1; 117 } else { 118 printf("%c", data[i]); 119 found_newline = 0; 120 found_item = 1; 121 } 122 } 123 printf("\n"); 124 } 125 126 extern "C" void l2cap_init(void){} 127 128 extern "C" void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 129 } 130 131 132 int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 133 if (strncmp((char*)data, "AT", 2) == 0){ 134 printf("Verify HF state machine response: "); 135 print_without_newlines(data,len); 136 } else { 137 printf("Verify AG state machine response: "); 138 print_without_newlines(data,len); 139 } 140 strncpy((char*)&rfcomm_payload[0], (char*)data, len); 141 rfcomm_payload_len = len; 142 return 0; 143 } 144 145 static void hci_event_sco_complete(){ 146 uint8_t event[19]; 147 uint8_t pos = 0; 148 event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE; 149 event[pos++] = sizeof(event) - 2; 150 151 event[pos++] = 0; //status 152 bt_store_16(event, pos, sco_handle); pos += 2; // sco handle 153 bt_flip_addr(&event[pos], dev_addr); pos += 6; 154 155 event[pos++] = 0; // link_type 156 event[pos++] = 0; // transmission_interval 157 event[pos++] = 0; // retransmission_interval 158 159 bt_store_16(event, pos, 0); pos += 2; // rx_packet_length 160 bt_store_16(event, pos, 0); pos += 2; // tx_packet_length 161 162 event[pos++] = 0; // air_mode 163 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 164 } 165 166 int hci_send_cmd(const hci_cmd_t *cmd, ...){ 167 printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 168 if (cmd->opcode == 0x428){ 169 hci_event_sco_complete(); 170 } 171 return 0; 172 } 173 174 175 void sdp_query_rfcomm_register_callback(void(*sdp_app_callback)(sdp_query_event_t * event, void * context), void * context){ 176 registered_sdp_app_callback = sdp_app_callback; 177 registered_sdp_app_context = context; 178 } 179 180 static void sdp_query_complete_response(uint8_t status){ 181 sdp_query_complete_event_t complete_event = { 182 SDP_QUERY_COMPLETE, 183 status 184 }; 185 (*registered_sdp_app_callback)((sdp_query_event_t*)&complete_event, registered_sdp_app_context); 186 } 187 188 static void sdp_query_rfcomm_service_response(uint8_t status){ 189 sdp_query_rfcomm_service_event_t service_event = { 190 SDP_QUERY_RFCOMM_SERVICE, 191 sdp_rfcomm_channel_nr, 192 (uint8_t *)sdp_rfcomm_service_name 193 }; 194 (*registered_sdp_app_callback)((sdp_query_event_t*)&service_event, registered_sdp_app_context); 195 } 196 197 void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){ 198 // printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback); 199 sdp_query_rfcomm_service_response(0); 200 sdp_query_complete_response(0); 201 } 202 203 204 void rfcomm_create_channel_internal(void * connection, bd_addr_t addr, uint8_t channel){ 205 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE 206 active_connection = connection; 207 uint8_t event[16]; 208 uint8_t pos = 0; 209 event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE; 210 event[pos++] = sizeof(event) - 2; 211 event[pos++] = 0; 212 213 bt_flip_addr(&event[pos], addr); 214 memcpy(dev_addr, addr, 6); 215 pos += 6; 216 217 bt_store_16(event, pos, 1); pos += 2; 218 event[pos++] = 0; 219 220 bt_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID 221 bt_store_16(event, pos, 200); pos += 2; // max frame size 222 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos); 223 } 224 225 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){ 226 return 1; 227 } 228 229 void rfcomm_disconnect_internal(uint16_t rfcomm_cid){ 230 uint8_t event[4]; 231 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED; 232 event[1] = sizeof(event) - 2; 233 bt_store_16(event, 2, rfcomm_cid); 234 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); 235 } 236 237 void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 238 registered_rfcomm_packet_handler = handler; 239 } 240 241 void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){ 242 printf("rfcomm_register_service_internal\n"); 243 } 244 245 246 void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){ 247 printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n"); 248 } 249 250 251 void rfcomm_accept_connection_internal(uint16_t rfcomm_cid){ 252 printf("rfcomm_accept_connection_internal \n"); 253 } 254 255 void run_loop_add_timer(timer_source_t *timer){ 256 } 257 258 int run_loop_remove_timer(timer_source_t *timer){ 259 return 0; 260 } 261 void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){ 262 } 263 264 void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ 265 } 266 267 268 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 269 uint8_t event[6]; 270 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 271 event[1] = sizeof(event) - 2; 272 event[2] = 0; // status = OK 273 bt_store_16(event, 3, handle); 274 event[5] = reason; 275 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 276 } 277 278 le_command_status_t gap_disconnect(hci_con_handle_t handle){ 279 hci_emit_disconnection_complete(handle, 0); 280 return BLE_PERIPHERAL_OK; 281 } 282 283 uint16_t hci_get_sco_voice_setting(){ 284 return 0x40; 285 } 286 287 int hci_remote_eSCO_supported(hci_con_handle_t handle){ 288 return 0; 289 } 290 291 void inject_rfcomm_command_to_hf(uint8_t * data, int len){ 292 if (memcmp((char*)data, "AT", 2) == 0) return; 293 294 prepare_rfcomm_buffer(data, len); 295 if (data[0] == '+' || (data[0] == 'O' && data[1] == 'K')){ 296 printf("Send cmd to HF state machine: %s\n", data); 297 } else { 298 printf("Trigger HF state machine - %s", data); 299 } 300 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len); 301 } 302 303 void inject_rfcomm_command_to_ag(uint8_t * data, int len){ 304 if (data[0] == '+') return; 305 306 prepare_rfcomm_buffer(data, len); 307 if (memcmp((char*)data, "AT", 2) == 0){ 308 printf("Send cmd to AG state machine: %s\n", data); 309 } else { 310 printf("Trigger AG state machine - %s", data); 311 } 312 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len); 313 } 314 315 316 317 318