xref: /btstack/test/gatt_client/mock.c (revision 4f5e860821f810e72f49ff490bf03fcab76dbdfc)
1f45e14b1S[email protected] #include <stdint.h>
2f45e14b1S[email protected] #include <stdio.h>
3f45e14b1S[email protected] #include <stdlib.h>
4f45e14b1S[email protected] #include <string.h>
5f45e14b1S[email protected] 
6f45e14b1S[email protected] #include <btstack/btstack.h>
7f45e14b1S[email protected] #include "hci.h"
8f45e14b1S[email protected] #include "hci_dump.h"
9f45e14b1S[email protected] #include "l2cap.h"
10*4f5e8608S[email protected] #include "ble_client.h"
11*4f5e8608S[email protected] 
12*4f5e8608S[email protected] static btstack_packet_handler_t att_packet_handler;
13*4f5e8608S[email protected] static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
14*4f5e8608S[email protected] 
15*4f5e8608S[email protected] static uint8_t  l2cap_stack_buffer[20];
16*4f5e8608S[email protected] static uint16_t max_l2cap_data_packet_length = 20;
17*4f5e8608S[email protected] 
18*4f5e8608S[email protected] void mock_command_complete(const hci_cmd_t *cmd){
19*4f5e8608S[email protected] 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
20*4f5e8608S[email protected] 		gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
21*4f5e8608S[email protected] }
22*4f5e8608S[email protected] 
23*4f5e8608S[email protected] void mock_simulate_scan_response(){
24*4f5e8608S[email protected] 	uint8_t packet[] = { 0x3E, 0x0F, 0x02, 0x01, 0x00, 0x01, 0x71, 0xA1, 0xE6, 0x80, 0x48, 0x61, 0x03, 0x02, 0x01, 0x1A, 0xB7};
25*4f5e8608S[email protected] 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
26*4f5e8608S[email protected] }
27f45e14b1S[email protected] 
28f45e14b1S[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
29f45e14b1S[email protected] 	return 1;
30f45e14b1S[email protected] }
31f45e14b1S[email protected] 
32*4f5e8608S[email protected] int  l2cap_can_send_connectionless_packet_now(void){
33*4f5e8608S[email protected] 	return 1;
34f45e14b1S[email protected] }
35f45e14b1S[email protected] 
36f45e14b1S[email protected] int hci_send_cmd(const hci_cmd_t *cmd, ...){
37f45e14b1S[email protected] 	printf("hci_send_cmd \n");
38*4f5e8608S[email protected] 
39*4f5e8608S[email protected] 	if (cmd == &hci_le_set_scan_enable){
40*4f5e8608S[email protected] 		mock_command_complete(cmd);
41*4f5e8608S[email protected] 		mock_simulate_scan_response();
42*4f5e8608S[email protected] 	}
43f45e14b1S[email protected] 	return 0;
44f45e14b1S[email protected] }
45f45e14b1S[email protected] 
46f45e14b1S[email protected] 
47f45e14b1S[email protected] uint8_t *l2cap_get_outgoing_buffer(void){
48f45e14b1S[email protected] 	printf("l2cap_get_outgoing_buffer \n");
49f45e14b1S[email protected] 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
50f45e14b1S[email protected] }
51f45e14b1S[email protected] 
52f45e14b1S[email protected] 
53f45e14b1S[email protected] uint16_t l2cap_max_mtu(void){
54f45e14b1S[email protected]     printf("l2cap_max_mtu \n");
55f45e14b1S[email protected]     return max_l2cap_data_packet_length;
56f45e14b1S[email protected] }
57f45e14b1S[email protected] 
58f45e14b1S[email protected] 
59f45e14b1S[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
60*4f5e8608S[email protected] 	att_packet_handler = packet_handler;
61f45e14b1S[email protected] }
62f45e14b1S[email protected] 
63f45e14b1S[email protected] void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
64*4f5e8608S[email protected] 	gatt_central_packet_handler = handler;
65*4f5e8608S[email protected] 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
66*4f5e8608S[email protected] 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
67f45e14b1S[email protected] }
68f45e14b1S[email protected] 
69f45e14b1S[email protected] int hci_reserve_packet_buffer(void){
70f45e14b1S[email protected] 	printf("hci_reserve_packet_buffer \n");
71f45e14b1S[email protected] 	return 1;
72f45e14b1S[email protected] }
73f45e14b1S[email protected] 
74f45e14b1S[email protected] int l2cap_reserve_packet_buffer(void){
75f45e14b1S[email protected] 	printf("l2cap_reserve_packet_buffer \n");
76f45e14b1S[email protected]     return hci_reserve_packet_buffer();
77f45e14b1S[email protected] }
78f45e14b1S[email protected] 
79f45e14b1S[email protected] int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
80f45e14b1S[email protected] 	printf("l2cap_send_prepared_connectionless \n");
81f45e14b1S[email protected] 	return 0;
82f45e14b1S[email protected] }
83f45e14b1S[email protected] 
84f45e14b1S[email protected] 
85*4f5e8608S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
86*4f5e8608S[email protected] 	printf("hci_disconnect_security_block \n");
87*4f5e8608S[email protected] }
88*4f5e8608S[email protected] 
89*4f5e8608S[email protected] void hci_dump_log(const char * format, ...){
90*4f5e8608S[email protected] 	printf("hci_disconnect_security_block \n");
91*4f5e8608S[email protected] }
92*4f5e8608S[email protected] 
93f45e14b1S[email protected] void l2cap_run(void){
94f45e14b1S[email protected] }
95