xref: /btstack/test/gatt_client/mock.c (revision 57fe2af8bc419922cc1501d966d5e7036aae5025)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <btstack/btstack.h>
7 #include "att.h"
8 #include "hci.h"
9 #include "hci_dump.h"
10 #include "l2cap.h"
11 #include "gatt_client.h"
12 #include "sm.h"
13 
14 static btstack_packet_handler_t att_packet_handler;
15 static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
16 
17 
18 static const uint16_t max_mtu = 23;
19 static uint8_t  l2cap_stack_buffer[max_mtu];
20 uint16_t gatt_client_handle = 0x40;
21 
22 uint16_t get_gatt_client_handle(void){
23 	return gatt_client_handle;
24 }
25 
26 void mock_simulate_command_complete(const hci_cmd_t *cmd){
27 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
28 		gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
29 }
30 
31 void mock_simulate_hci_state_working(void){
32 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
33 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
34 }
35 
36 void mock_simulate_connected(void){
37 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
38 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
39 }
40 
41 
42 void mock_simulate_scan_response(void){
43 	uint8_t packet[] = {0x3E, 0x0F, 0x02, 0x01, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x03, 0x02, 0x01, 0x05, 0xBC};
44 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
45 }
46 
47 static void att_init_connection(att_connection_t * att_connection){
48     att_connection->mtu = 23;
49     att_connection->encryption_key_size = 0;
50     att_connection->authenticated = 0;
51 	att_connection->authorized = 0;
52 }
53 
54 int  l2cap_can_send_connectionless_packet_now(void){
55 	return 1;
56 }
57 
58 
59 uint8_t *l2cap_get_outgoing_buffer(void){
60 	// printf("l2cap_get_outgoing_buffer\n");
61 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
62 }
63 
64 uint16_t l2cap_max_mtu(void){
65 	// printf("l2cap_max_mtu\n");
66     return max_mtu;
67 }
68 
69 uint16_t l2cap_max_le_mtu(void){
70 	// printf("l2cap_max_mtu\n");
71     return max_mtu;
72 }
73 
74 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
75     att_packet_handler = packet_handler;
76 }
77 
78 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
79 	gatt_central_packet_handler = handler;
80 }
81 
82 int l2cap_reserve_packet_buffer(void){
83 	return 1;
84 }
85 
86 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
87 	return 1;
88 }
89 
90 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
91 	att_connection_t att_connection;
92 	att_init_connection(&att_connection);
93 	uint8_t response[max_mtu];
94 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
95 	if (response_len){
96 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
97 	}
98 	return 0;
99 }
100 
101 int  sm_cmac_ready(void){
102 	return 1;
103 }
104 void sm_cmac_start(sm_key_t k, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8])){
105 	//sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
106 }
107 int sm_le_device_index(uint16_t handle ){
108 	return -1;
109 }
110 
111 void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
112 }
113 
114 // Set callback that will be executed when timer expires.
115 void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){
116 }
117 
118 // Add/Remove timer source.
119 void run_loop_add_timer(timer_source_t *timer){
120 }
121 
122 int  run_loop_remove_timer(timer_source_t *timer){
123 	return 1;
124 }
125 
126 
127 // int hci_send_cmd(const hci_cmd_t *cmd, ...){
128 // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
129 // 	return 0;
130 // }
131 
132 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
133 // 	return 1;
134 // }
135 
136 // void hci_disconnect_security_block(hci_con_handle_t con_handle){
137 // 	printf("hci_disconnect_security_block \n");
138 // }
139 
140 // void hci_dump_log(const char * format, ...){
141 // 	printf("hci_disconnect_security_block \n");
142 // }
143 
144 void l2cap_run(void){
145 }
146