xref: /btstack/test/gatt_client/mock.c (revision f64240626f876d0d26743bd7cf86aea807821076)
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>
74d890b2dS[email protected] #include "att.h"
8f45e14b1S[email protected] #include "hci.h"
9f45e14b1S[email protected] #include "hci_dump.h"
10f45e14b1S[email protected] #include "l2cap.h"
114f5e8608S[email protected] #include "ble_client.h"
124f5e8608S[email protected] 
134f5e8608S[email protected] static btstack_packet_handler_t att_packet_handler;
144f5e8608S[email protected] static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
154f5e8608S[email protected] 
16c442c7c2S[email protected] static const uint16_t max_mtu = 23;
17c442c7c2S[email protected] static uint8_t  l2cap_stack_buffer[max_mtu];
18ee988ca9S[email protected] uint16_t gatt_client_handle = 0x40;
194f5e8608S[email protected] 
209638642bS[email protected] void mock_simulate_command_complete(const hci_cmd_t *cmd){
214f5e8608S[email protected] 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
224f5e8608S[email protected] 		gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
234f5e8608S[email protected] }
244f5e8608S[email protected] 
259638642bS[email protected] void mock_simulate_hci_state_working(){
269638642bS[email protected] 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
279638642bS[email protected] 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
289638642bS[email protected] }
299638642bS[email protected] 
30c442c7c2S[email protected] void mock_simulate_connected(){
31c442c7c2S[email protected] 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
324f5e8608S[email protected] 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
334f5e8608S[email protected] }
34f45e14b1S[email protected] 
35c442c7c2S[email protected] 
36c442c7c2S[email protected] void mock_simulate_scan_response(){
37c442c7c2S[email protected] 	uint8_t packet[] = {0x3E, 0x0F, 0x02, 0x01, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x03, 0x02, 0x01, 0x05, 0xBC};
38c442c7c2S[email protected] 	gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
39c442c7c2S[email protected] }
40c442c7c2S[email protected] 
414d890b2dS[email protected] static void att_init_connection(att_connection_t * att_connection){
424d890b2dS[email protected]     att_connection->mtu = 23;
434d890b2dS[email protected]     att_connection->encryption_key_size = 0;
444d890b2dS[email protected]     att_connection->authenticated = 0;
454d890b2dS[email protected] 	att_connection->authorized = 0;
46c442c7c2S[email protected] }
47c442c7c2S[email protected] 
484f5e8608S[email protected] int  l2cap_can_send_connectionless_packet_now(void){
494f5e8608S[email protected] 	return 1;
50f45e14b1S[email protected] }
51f45e14b1S[email protected] 
52f45e14b1S[email protected] 
53f45e14b1S[email protected] 
54f45e14b1S[email protected] uint8_t *l2cap_get_outgoing_buffer(void){
55f5bdf8a1S[email protected] 	// printf("l2cap_get_outgoing_buffer\n");
56f45e14b1S[email protected] 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
57f45e14b1S[email protected] }
58f45e14b1S[email protected] 
59f45e14b1S[email protected] uint16_t l2cap_max_mtu(void){
60f5bdf8a1S[email protected] 	// printf("l2cap_max_mtu\n");
61c442c7c2S[email protected]     return max_mtu;
62f45e14b1S[email protected] }
63f45e14b1S[email protected] 
64f45e14b1S[email protected] 
65f45e14b1S[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
66ee988ca9S[email protected]     // printf("mock: l2cap_register_fixed_channel, use att_packet_handler\n");
674f5e8608S[email protected] 	att_packet_handler = packet_handler;
68f45e14b1S[email protected] }
69f45e14b1S[email protected] 
70f45e14b1S[email protected] void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
714f5e8608S[email protected] 	gatt_central_packet_handler = handler;
72f45e14b1S[email protected] }
73f45e14b1S[email protected] 
74f45e14b1S[email protected] int l2cap_reserve_packet_buffer(void){
75f5bdf8a1S[email protected] 	// printf("l2cap_reserve_packet_buffer\n");
76c442c7c2S[email protected] 	return 1;
77f45e14b1S[email protected] }
78f45e14b1S[email protected] 
79f45e14b1S[email protected] int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
804d890b2dS[email protected] 	// assert handle = current connection
814d890b2dS[email protected] 	// assert cid = att
824d890b2dS[email protected] 
834d890b2dS[email protected] 	att_connection_t att_connection;
844d890b2dS[email protected] 	att_init_connection(&att_connection);
854d890b2dS[email protected] 	uint8_t response[max_mtu];
864d890b2dS[email protected] 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
87ecedf63aS[email protected] 	if (response_len){
88ee988ca9S[email protected] 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
89ecedf63aS[email protected] 	}
90f45e14b1S[email protected] 	return 0;
91f45e14b1S[email protected] }
92f45e14b1S[email protected] 
93*f6424062S[email protected] // int hci_send_cmd(const hci_cmd_t *cmd, ...){
94*f6424062S[email protected] // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
95*f6424062S[email protected] // 	return 0;
96*f6424062S[email protected] // }
97f45e14b1S[email protected] 
98*f6424062S[email protected] // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
99*f6424062S[email protected] // 	return 1;
100*f6424062S[email protected] // }
1014f5e8608S[email protected] 
102*f6424062S[email protected] // void hci_disconnect_security_block(hci_con_handle_t con_handle){
103*f6424062S[email protected] // 	printf("hci_disconnect_security_block \n");
104*f6424062S[email protected] // }
105*f6424062S[email protected] 
106*f6424062S[email protected] // void hci_dump_log(const char * format, ...){
107*f6424062S[email protected] // 	printf("hci_disconnect_security_block \n");
108*f6424062S[email protected] // }
1094f5e8608S[email protected] 
110f45e14b1S[email protected] void l2cap_run(void){
111f45e14b1S[email protected] }
112