xref: /btstack/test/gatt_client/mock.c (revision ee988ca98374e7cb5bc61ccf5766ebb5f40ae14f)
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 "ble_client.h"
12 
13 static btstack_packet_handler_t att_packet_handler;
14 static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
15 
16 static const uint16_t max_mtu = 23;
17 static uint8_t  l2cap_stack_buffer[max_mtu];
18 uint16_t gatt_client_handle = 0x40;
19 
20 void mock_simulate_command_complete(const hci_cmd_t *cmd){
21 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
22 		gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
23 }
24 
25 void mock_simulate_hci_state_working(){
26 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
27 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
28 }
29 
30 void mock_simulate_connected(){
31 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
32 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
33 }
34 
35 
36 void mock_simulate_scan_response(){
37 	uint8_t packet[] = {0x3E, 0x0F, 0x02, 0x01, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x03, 0x02, 0x01, 0x05, 0xBC};
38 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
39 }
40 
41 static void att_init_connection(att_connection_t * att_connection){
42     att_connection->mtu = 23;
43     att_connection->encryption_key_size = 0;
44     att_connection->authenticated = 0;
45 	att_connection->authorized = 0;
46 }
47 
48 int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
49 	return 1;
50 }
51 
52 int  l2cap_can_send_connectionless_packet_now(void){
53 	return 1;
54 }
55 
56 int hci_send_cmd(const hci_cmd_t *cmd, ...){
57 //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
58 	return 0;
59 }
60 
61 
62 uint8_t *l2cap_get_outgoing_buffer(void){
63 	// printf("l2cap_get_outgoing_buffer\n");
64 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
65 }
66 
67 uint16_t l2cap_max_mtu(void){
68 	// printf("l2cap_max_mtu\n");
69     return max_mtu;
70 }
71 
72 
73 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
74     // printf("mock: l2cap_register_fixed_channel, use att_packet_handler\n");
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 	// printf("l2cap_reserve_packet_buffer\n");
84 	return 1;
85 }
86 
87 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
88 	// assert handle = current connection
89 	// assert cid = att
90 
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 
102 void hci_disconnect_security_block(hci_con_handle_t con_handle){
103 	printf("hci_disconnect_security_block \n");
104 }
105 
106 void hci_dump_log(const char * format, ...){
107 	printf("hci_disconnect_security_block \n");
108 }
109 
110 void l2cap_run(void){
111 }
112