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.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 uint8_t rfcomm_payload[200]; 63 static uint16_t rfcomm_payload_len; 64 void * active_connection; 65 66 void (*registered_rfcomm_packet_handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 67 void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context); 68 69 uint8_t * get_rfcomm_payload(){ 70 return &rfcomm_payload[0]; 71 } 72 73 uint16_t get_rfcomm_payload_len(){ 74 return rfcomm_payload_len; 75 } 76 77 static void prepare_rfcomm_buffer(uint8_t * data, int len){ 78 if (len <= 0) return; 79 memset(&rfcomm_payload, 0, 200); 80 int pos = 0; 81 82 if (strncmp((char*)data, "AT", 2) == 0){ 83 strncpy((char*)&rfcomm_payload[pos], (char*)data, len); 84 pos += len; 85 } else { 86 rfcomm_payload[pos++] = '\r'; 87 rfcomm_payload[pos++] = '\n'; 88 strncpy((char*)&rfcomm_payload[pos], (char*)data, len); 89 pos += len; 90 91 if (memcmp((char*)data, "+BAC", 4) != 0 && 92 memcmp((char*)data, "+BCS", 4) != 0){ 93 rfcomm_payload[pos++] = '\r'; 94 rfcomm_payload[pos++] = '\n'; 95 rfcomm_payload[pos++] = 'O'; 96 rfcomm_payload[pos++] = 'K'; 97 } 98 99 } 100 rfcomm_payload[pos++] = '\r'; 101 rfcomm_payload[pos++] = '\n'; 102 rfcomm_payload[pos] = 0; 103 rfcomm_payload_len = pos; 104 } 105 106 static void print_without_newlines(uint8_t *data, uint16_t len){ 107 int found_newline = 0; 108 int found_item = 0; 109 110 for (int i=0; i<len; i++){ 111 if (data[i] == '\r' || data[i] == '\n'){ 112 if (!found_newline && found_item) printf("\n"); 113 found_newline = 1; 114 } else { 115 printf("%c", data[i]); 116 found_newline = 0; 117 found_item = 1; 118 } 119 } 120 printf("\n"); 121 } 122 123 int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 124 if (strncmp((char*)data, "AT", 2) == 0){ 125 printf("Verify HF state machine response: "); 126 print_without_newlines(data,len); 127 } else { 128 printf("Verify AG state machine response: "); 129 print_without_newlines(data,len); 130 } 131 strncpy((char*)&rfcomm_payload[0], (char*)data, len); 132 rfcomm_payload_len = len; 133 return 0; 134 } 135 136 int hci_send_cmd(const hci_cmd_t *cmd, ...){ 137 printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 138 return 0; 139 } 140 141 142 void sdp_query_rfcomm_register_callback(void(*sdp_app_callback)(sdp_query_event_t * event, void * context), void * context){ 143 registered_sdp_app_callback = sdp_app_callback; 144 registered_sdp_app_context = context; 145 } 146 147 static void sdp_query_complete_response(uint8_t status){ 148 sdp_query_complete_event_t complete_event = { 149 SDP_QUERY_COMPLETE, 150 status 151 }; 152 (*registered_sdp_app_callback)((sdp_query_event_t*)&complete_event, registered_sdp_app_context); 153 } 154 155 static void sdp_query_rfcomm_service_response(uint8_t status){ 156 sdp_query_rfcomm_service_event_t service_event = { 157 SDP_QUERY_RFCOMM_SERVICE, 158 sdp_rfcomm_channel_nr, 159 (uint8_t *)sdp_rfcomm_service_name 160 }; 161 (*registered_sdp_app_callback)((sdp_query_event_t*)&service_event, registered_sdp_app_context); 162 } 163 164 void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){ 165 // printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback); 166 sdp_query_rfcomm_service_response(0); 167 sdp_query_complete_response(0); 168 } 169 170 void rfcomm_create_channel_internal(void * connection, bd_addr_t addr, uint8_t channel){ 171 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE 172 // printf("rfcomm_create_channel_internal\n"); 173 active_connection = connection; 174 uint8_t event[16]; 175 uint8_t pos = 0; 176 event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE; 177 event[pos++] = sizeof(event) - 2; 178 event[pos++] = 0; 179 180 bt_flip_addr(&event[pos], addr); pos += 6; 181 bt_store_16(event, pos, 1); pos += 2; 182 event[pos++] = 0; 183 184 bt_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID 185 bt_store_16(event, pos, 200); pos += 2; // max frame size 186 (*registered_rfcomm_packet_handler)(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos); 187 } 188 189 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){ 190 return 1; 191 } 192 193 void rfcomm_disconnect_internal(uint16_t rfcomm_cid){ 194 uint8_t event[4]; 195 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED; 196 event[1] = sizeof(event) - 2; 197 bt_store_16(event, 2, rfcomm_cid); 198 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); 199 } 200 201 void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 202 registered_rfcomm_packet_handler = handler; 203 } 204 205 void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){ 206 printf("rfcomm_register_service_internal\n"); 207 } 208 209 210 void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){ 211 printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n"); 212 } 213 214 215 void rfcomm_accept_connection_internal(uint16_t rfcomm_cid){ 216 printf("rfcomm_accept_connection_internal \n"); 217 } 218 219 220 void inject_rfcomm_command_to_hf(uint8_t * data, int len){ 221 if (memcmp((char*)data, "AT", 2) == 0) return; 222 223 prepare_rfcomm_buffer(data, len); 224 if (data[0] == '+' || (data[0] == 'O' && data[1] == 'K')){ 225 printf("Send cmd to HF state machine: %s\n", data); 226 } else { 227 printf("Trigger HF state machine - %s", data); 228 } 229 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len); 230 } 231 232 void inject_rfcomm_command_to_ag(uint8_t * data, int len){ 233 if (data[0] == '+') return; 234 235 prepare_rfcomm_buffer(data, len); 236 if (memcmp((char*)data, "AT", 2) == 0){ 237 printf("Send cmd to AG state machine: %s\n", data); 238 } else { 239 printf("Trigger AG state machine - %s", data); 240 } 241 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len); 242 } 243 244 245 246 247