mock.c (1e35c04d3c3de9a93ac28089ec3f155e106e73a6) mock.c (5b69ff6c7f867d1786a2f1a568e8e58d5acdbc20)
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

--- 32 unchanged lines hidden (view full) ---

41//
42// *****************************************************************************
43
44#include <stdint.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48
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

--- 32 unchanged lines hidden (view full) ---

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>
49#include "hci.h"
50#include "hci_dump.h"
50#include "hci.h"
51#include "hci_dump.h"
51#include "classic/sdp_query_rfcomm.h"
52#include "classic/rfcomm.h"
53#include "classic/hfp_hf.h"
52#include "sdp_query_rfcomm.h"
53#include "rfcomm.h"
54#include "hfp_hf.h"
54
55#include "mock.h"
56
57static void *registered_sdp_app_context;
58static uint8_t sdp_rfcomm_channel_nr = 1;
59const char sdp_rfcomm_service_name[] = "BTstackMock";
60static uint16_t rfcomm_cid = 1;
61static bd_addr_t dev_addr;
62static uint16_t sco_handle = 10;
63static uint8_t rfcomm_payload[200];
64static uint16_t rfcomm_payload_len = 0;
65
66static uint8_t outgoing_rfcomm_payload[200];
67static uint16_t outgoing_rfcomm_payload_len = 0;
68
55
56#include "mock.h"
57
58static void *registered_sdp_app_context;
59static uint8_t sdp_rfcomm_channel_nr = 1;
60const char sdp_rfcomm_service_name[] = "BTstackMock";
61static uint16_t rfcomm_cid = 1;
62static bd_addr_t dev_addr;
63static uint16_t sco_handle = 10;
64static uint8_t rfcomm_payload[200];
65static uint16_t rfcomm_payload_len = 0;
66
67static uint8_t outgoing_rfcomm_payload[200];
68static uint16_t outgoing_rfcomm_payload_len = 0;
69
70static uint8_t rfcomm_reserved_buffer[1000];
71
69void * active_connection;
70hfp_connection_t * hfp_context;
71
72void * active_connection;
73hfp_connection_t * hfp_context;
74
72void (*registered_rfcomm_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
75void (*registered_rfcomm_packet_handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
73void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context);
74
75uint8_t * get_rfcomm_payload(){
76 return &rfcomm_payload[0];
77}
78
79uint16_t get_rfcomm_payload_len(){
80 return rfcomm_payload_len;

--- 23 unchanged lines hidden (view full) ---

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
76void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context);
77
78uint8_t * get_rfcomm_payload(){
79 return &rfcomm_payload[0];
80}
81
82uint16_t get_rfcomm_payload_len(){
83 return rfcomm_payload_len;

--- 23 unchanged lines hidden (view full) ---

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
112static void print_without_newlines(uint8_t *data, uint16_t len){
115void print_without_newlines(uint8_t *data, uint16_t len);
116void print_without_newlines(uint8_t *data, uint16_t len){
113 int found_newline = 0;
114 int found_item = 0;
115
116 for (int i=0; i<len; i++){
117 if (data[i] == '\r' || data[i] == '\n'){
118 if (!found_newline && found_item) printf("\n");
119 found_newline = 1;
120 } else {
121 printf("%c", data[i]);
122 found_newline = 0;
123 found_item = 1;
124 }
125 }
126 printf("\n");
127}
128
129extern "C" void l2cap_init(void){}
130
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
133extern "C" void l2cap_init(void){}
134
131extern "C" void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
135extern "C" void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
132}
133
136}
137
134
135int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
136 int start_command_offset = 2;
137 int end_command_offset = 2;
138
139 if (strncmp((char*)data, "AT", 2) == 0){
140 start_command_offset = 0;
141 }
142

--- 7 unchanged lines hidden (view full) ---

150 strncpy((char*)&rfcomm_payload[0], (char*)data, len);
151 rfcomm_payload_len = len;
152 }
153
154 //print_without_newlines(rfcomm_payload,rfcomm_payload_len);
155 return 0;
156}
157
138int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
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

--- 7 unchanged lines hidden (view full) ---

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
161int rfcomm_reserve_packet_buffer(void){
162 return 1;
163};
164void rfcomm_release_packet_buffer(void){};
165uint8_t * rfcomm_get_outgoing_buffer(void) {
166 return rfcomm_reserved_buffer;
167}
168uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
169 return sizeof(rfcomm_reserved_buffer);
170}
171int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){
172 printf("--- rfcomm_send_prepared with len %u ---\n", len);
173 return rfcomm_send_internal(rfcomm_cid, rfcomm_reserved_buffer, len);
174}
175
158static void hci_event_sco_complete(){
159 uint8_t event[19];
160 uint8_t pos = 0;
161 event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
162 event[pos++] = sizeof(event) - 2;
163
164 event[pos++] = 0; //status
165 bt_store_16(event, pos, sco_handle); pos += 2; // sco handle
166 bt_flip_addr(&event[pos], dev_addr); pos += 6;
167
168 event[pos++] = 0; // link_type
169 event[pos++] = 0; // transmission_interval
170 event[pos++] = 0; // retransmission_interval
171
172 bt_store_16(event, pos, 0); pos += 2; // rx_packet_length
173 bt_store_16(event, pos, 0); pos += 2; // tx_packet_length
174
175 event[pos++] = 0; // air_mode
176static void hci_event_sco_complete(){
177 uint8_t event[19];
178 uint8_t pos = 0;
179 event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
180 event[pos++] = sizeof(event) - 2;
181
182 event[pos++] = 0; //status
183 bt_store_16(event, pos, sco_handle); pos += 2; // sco handle
184 bt_flip_addr(&event[pos], dev_addr); pos += 6;
185
186 event[pos++] = 0; // link_type
187 event[pos++] = 0; // transmission_interval
188 event[pos++] = 0; // retransmission_interval
189
190 bt_store_16(event, pos, 0); pos += 2; // rx_packet_length
191 bt_store_16(event, pos, 0); pos += 2; // tx_packet_length
192
193 event[pos++] = 0; // air_mode
176 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
194 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
177}
178
179int hci_send_cmd(const hci_cmd_t *cmd, ...){
180 //printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
181 if (cmd->opcode == 0x428){
182 hci_event_sco_complete();
183 }
184 return 0;

--- 24 unchanged lines hidden (view full) ---

209
210void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){
211 // printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
212 sdp_query_rfcomm_service_response(0);
213 sdp_query_complete_response(0);
214}
215
216
195}
196
197int hci_send_cmd(const hci_cmd_t *cmd, ...){
198 //printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
199 if (cmd->opcode == 0x428){
200 hci_event_sco_complete();
201 }
202 return 0;

--- 24 unchanged lines hidden (view full) ---

227
228void 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
217uint8_t rfcomm_create_channel(bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
235void rfcomm_create_channel_internal(void * connection, bd_addr_t addr, uint8_t channel){
218 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
236 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
237 active_connection = connection;
219 uint8_t event[16];
220 uint8_t pos = 0;
221 event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
222 event[pos++] = sizeof(event) - 2;
223 event[pos++] = 0;
224
225 bt_flip_addr(&event[pos], addr);
226 memcpy(dev_addr, addr, 6);
227 pos += 6;
228
229 bt_store_16(event, pos, 1); pos += 2;
230 event[pos++] = 0;
231
232 bt_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID
233 bt_store_16(event, pos, 200); pos += 2; // max frame size
238 uint8_t event[16];
239 uint8_t pos = 0;
240 event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
241 event[pos++] = sizeof(event) - 2;
242 event[pos++] = 0;
243
244 bt_flip_addr(&event[pos], addr);
245 memcpy(dev_addr, addr, 6);
246 pos += 6;
247
248 bt_store_16(event, pos, 1); pos += 2;
249 event[pos++] = 0;
250
251 bt_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID
252 bt_store_16(event, pos, 200); pos += 2; // max frame size
234 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
235
236 if (out_cid){
237 *out_cid = rfcomm_cid;
238 }
239 return 0;
253 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
240}
241
242int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
243 return 1;
244}
245
246void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
247 uint8_t event[4];
248 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
249 event[1] = sizeof(event) - 2;
250 bt_store_16(event, 2, rfcomm_cid);
254}
255
256int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
257 return 1;
258}
259
260void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
261 uint8_t event[4];
262 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
263 event[1] = sizeof(event) - 2;
264 bt_store_16(event, 2, rfcomm_cid);
251 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
265 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
252}
253
266}
267
254void rfcomm_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
268void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
255 registered_rfcomm_packet_handler = handler;
256}
257
269 registered_rfcomm_packet_handler = handler;
270}
271
258uint8_t rfcomm_register_service(uint8_t channel, uint16_t max_frame_size){
259 printf("rfcomm_register_service\n");
260 return 0;
272void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){
273 printf("rfcomm_register_service_internal\n");
261}
262
263
264void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
265 printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n");
266}
267
268

--- 16 unchanged lines hidden (view full) ---

285
286void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
287 uint8_t event[6];
288 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
289 event[1] = sizeof(event) - 2;
290 event[2] = 0; // status = OK
291 bt_store_16(event, 3, handle);
292 event[5] = reason;
274}
275
276
277void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
278 printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n");
279}
280
281

--- 16 unchanged lines hidden (view full) ---

298
299void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
300 uint8_t event[6];
301 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
302 event[1] = sizeof(event) - 2;
303 event[2] = 0; // status = OK
304 bt_store_16(event, 3, handle);
305 event[5] = reason;
293 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
306 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
294}
295
307}
308
296uint8_t gap_disconnect(hci_con_handle_t handle){
309le_command_status_t gap_disconnect(hci_con_handle_t handle){
297 hci_emit_disconnection_complete(handle, 0);
310 hci_emit_disconnection_complete(handle, 0);
298 return 0;
311 return BLE_PERIPHERAL_OK;
299}
300
301uint16_t hci_get_sco_voice_setting(){
302 return 0x40;
303}
304
305int hci_remote_eSCO_supported(hci_con_handle_t handle){
306 return 0;

--- 32 unchanged lines hidden (view full) ---

339 if (data[0] == '+') return;
340
341 add_new_lines_to_hfp_command(data, len);
342 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
343}
344
345
346
312}
313
314uint16_t hci_get_sco_voice_setting(){
315 return 0x40;
316}
317
318int hci_remote_eSCO_supported(hci_con_handle_t handle){
319 return 0;

--- 32 unchanged lines hidden (view full) ---

352 if (data[0] == '+') return;
353
354 add_new_lines_to_hfp_command(data, len);
355 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
356}
357
358
359
360