xref: /btstack/test/gatt_client/mock.c (revision f64240626f876d0d26743bd7cf86aea807821076)
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  l2cap_can_send_connectionless_packet_now(void){
49 	return 1;
50 }
51 
52 
53 
54 uint8_t *l2cap_get_outgoing_buffer(void){
55 	// printf("l2cap_get_outgoing_buffer\n");
56 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
57 }
58 
59 uint16_t l2cap_max_mtu(void){
60 	// printf("l2cap_max_mtu\n");
61     return max_mtu;
62 }
63 
64 
65 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
66     // printf("mock: l2cap_register_fixed_channel, use att_packet_handler\n");
67 	att_packet_handler = packet_handler;
68 }
69 
70 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
71 	gatt_central_packet_handler = handler;
72 }
73 
74 int l2cap_reserve_packet_buffer(void){
75 	// printf("l2cap_reserve_packet_buffer\n");
76 	return 1;
77 }
78 
79 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
80 	// assert handle = current connection
81 	// assert cid = att
82 
83 	att_connection_t att_connection;
84 	att_init_connection(&att_connection);
85 	uint8_t response[max_mtu];
86 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
87 	if (response_len){
88 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
89 	}
90 	return 0;
91 }
92 
93 // int hci_send_cmd(const hci_cmd_t *cmd, ...){
94 // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
95 // 	return 0;
96 // }
97 
98 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
99 // 	return 1;
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