mock.c (aa4dd81512bdd8f8b864bc33bc306b33426f2192) mock.c (b51c851cc9d1d214734076a917fb20e3bcb1d8cc)
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;
65void * active_connection;
66hfp_connection_t * hfp_context;
67
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;
66void * active_connection;
67hfp_connection_t * hfp_context;
68
68void (*registered_rfcomm_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69void (*registered_rfcomm_packet_handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context);
70
71uint8_t * get_rfcomm_payload(){
72 return &rfcomm_payload[0];
73}
74
75uint16_t get_rfcomm_payload_len(){
76 return rfcomm_payload_len;

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

119 found_item = 1;
120 }
121 }
122 printf("\n");
123}
124
125extern "C" void l2cap_init(void){}
126
70void (*registered_sdp_app_callback)(sdp_query_event_t * event, void * context);
71
72uint8_t * get_rfcomm_payload(){
73 return &rfcomm_payload[0];
74}
75
76uint16_t get_rfcomm_payload_len(){
77 return rfcomm_payload_len;

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

120 found_item = 1;
121 }
122 }
123 printf("\n");
124}
125
126extern "C" void l2cap_init(void){}
127
127extern "C" void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
128extern "C" void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
128}
129
130
131int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
132 if (strncmp((char*)data, "AT", 2) == 0){
133 printf("Verify HF state machine response: ");
134 print_without_newlines(data,len);
135 } else {

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

154 event[pos++] = 0; // link_type
155 event[pos++] = 0; // transmission_interval
156 event[pos++] = 0; // retransmission_interval
157
158 bt_store_16(event, pos, 0); pos += 2; // rx_packet_length
159 bt_store_16(event, pos, 0); pos += 2; // tx_packet_length
160
161 event[pos++] = 0; // air_mode
129}
130
131
132int 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 {

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

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
162 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
163 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
163}
164
165int hci_send_cmd(const hci_cmd_t *cmd, ...){
166 printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
167 if (cmd->opcode == 0x428){
168 hci_event_sco_complete();
169 }
170 return 0;

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

195
196void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){
197 // printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
198 sdp_query_rfcomm_service_response(0);
199 sdp_query_complete_response(0);
200}
201
202
164}
165
166int 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;

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

196
197void 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
203uint8_t rfcomm_create_channel(bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
204void rfcomm_create_channel_internal(void * connection, bd_addr_t addr, uint8_t channel){
204 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
205 // RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
206 active_connection = connection;
205 uint8_t event[16];
206 uint8_t pos = 0;
207 event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
208 event[pos++] = sizeof(event) - 2;
209 event[pos++] = 0;
210
211 bt_flip_addr(&event[pos], addr);
212 memcpy(dev_addr, addr, 6);
213 pos += 6;
214
215 bt_store_16(event, pos, 1); pos += 2;
216 event[pos++] = 0;
217
218 bt_store_16(event, pos, rfcomm_cid); pos += 2; // channel ID
219 bt_store_16(event, pos, 200); pos += 2; // max frame size
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
220 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
221
222 if (out_cid){
223 *out_cid = rfcomm_cid;
224 }
225 return 0;
222 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
226}
227
228int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
229 return 1;
230}
231
232void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
233 uint8_t event[4];
234 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
235 event[1] = sizeof(event) - 2;
236 bt_store_16(event, 2, rfcomm_cid);
223}
224
225int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
226 return 1;
227}
228
229void 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);
237 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
234 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
238}
239
235}
236
240void rfcomm_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
237void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
241 registered_rfcomm_packet_handler = handler;
242}
243
238 registered_rfcomm_packet_handler = handler;
239}
240
244uint8_t rfcomm_register_service(uint8_t channel, uint16_t max_frame_size){
245 printf("rfcomm_register_service\n");
246 return 0;
241void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){
242 printf("rfcomm_register_service_internal\n");
247}
248
249
250void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
251 printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n");
252}
253
254

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

271
272void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
273 uint8_t event[6];
274 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
275 event[1] = sizeof(event) - 2;
276 event[2] = 0; // status = OK
277 bt_store_16(event, 3, handle);
278 event[5] = reason;
243}
244
245
246void 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

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

267
268void 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;
279 (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
275 (*registered_rfcomm_packet_handler)(active_connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
280}
281
276}
277
282uint8_t gap_disconnect(hci_con_handle_t handle){
278le_command_status_t gap_disconnect(hci_con_handle_t handle){
283 hci_emit_disconnection_complete(handle, 0);
279 hci_emit_disconnection_complete(handle, 0);
284 return 0;
280 return BLE_PERIPHERAL_OK;
285}
286
287uint16_t hci_get_sco_voice_setting(){
288 return 0x40;
289}
290
281}
282
283uint16_t hci_get_sco_voice_setting(){
284 return 0x40;
285}
286
287int hci_remote_eSCO_supported(hci_con_handle_t handle){
288 return 0;
289}
290
291void 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 }
291void 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)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
300 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
301}
302
303void 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 }
301}
302
303void 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)( RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
312 (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &rfcomm_payload[0], rfcomm_payload_len);
313}
314
315
316
313}
314
315
316
317