mock.c (33a05ac6520241b1a2207e1cbb4497d7f7e58d5a) | mock.c (a0ffb263e02c613f9155d5035f68cb0ad7b80d74) |
---|---|
1#include <stdint.h> 2#include <stdio.h> 3#include <stdlib.h> 4#include <string.h> 5 | 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" | 6#include "hci.h" 7#include "hci_dump.h" 8#include "l2cap.h" |
11#include "gatt_client.h" 12#include "sm.h" | |
13 | 9 |
10#include "ble/att_db.h" 11#include "ble/gatt_client.h" 12#include "ble/sm.h" 13 |
|
14static btstack_packet_handler_t att_packet_handler; | 14static btstack_packet_handler_t att_packet_handler; |
15static void (*registered_l2cap_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; | 15static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; |
16 | 16 |
17static linked_list_t connections; | 17static btstack_linked_list_t connections; |
18static const uint16_t max_mtu = 23; | 18static const uint16_t max_mtu = 23; |
19static uint8_t l2cap_stack_buffer[max_mtu]; | 19static uint8_t l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + max_mtu]; // pre buffer + HCI Header + L2CAP header |
20uint16_t gatt_client_handle = 0x40; 21 22uint16_t get_gatt_client_handle(void){ 23 return gatt_client_handle; 24} 25 26void mock_simulate_command_complete(const hci_cmd_t *cmd){ 27 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; | 20uint16_t gatt_client_handle = 0x40; 21 22uint16_t get_gatt_client_handle(void){ 23 return gatt_client_handle; 24} 25 26void mock_simulate_command_complete(const hci_cmd_t *cmd){ 27 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; |
28 registered_l2cap_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet)); | 28 registered_hci_event_handler(HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet)); |
29} 30 31void mock_simulate_hci_state_working(void){ 32 uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; | 29} 30 31void mock_simulate_hci_state_working(void){ 32 uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; |
33 registered_l2cap_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3); | 33 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3); |
34} 35 36void 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}; | 34} 35 36void 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 registered_l2cap_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet)); | 38 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); |
39} 40 41void mock_simulate_scan_response(void){ 42 uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; | 39} 40 41void mock_simulate_scan_response(void){ 42 uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
43 registered_l2cap_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet)); | 43 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); |
44} 45 | 44} 45 |
46le_command_status_t le_central_start_scan(void){ 47 return BLE_PERIPHERAL_OK; | 46void gap_start_scan(void){ |
48} | 47} |
49le_command_status_t le_central_stop_scan(void){ 50 return BLE_PERIPHERAL_OK; | 48void gap_stop_scan(void){ |
51} | 49} |
52le_command_status_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 53 return BLE_PERIPHERAL_OK; | 50uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 51 return 0; |
54} | 52} |
55void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ | 53void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ |
56} 57 58static void att_init_connection(att_connection_t * att_connection){ 59 att_connection->mtu = 23; 60 att_connection->max_mtu = 23; 61 att_connection->encryption_key_size = 0; 62 att_connection->authenticated = 0; 63 att_connection->authorized = 0; --- 20 unchanged lines hidden (view full) --- 84} 85 86void l2cap_init(){} 87 88void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 89 att_packet_handler = packet_handler; 90} 91 | 54} 55 56static void att_init_connection(att_connection_t * att_connection){ 57 att_connection->mtu = 23; 58 att_connection->max_mtu = 23; 59 att_connection->encryption_key_size = 0; 60 att_connection->authenticated = 0; 61 att_connection->authorized = 0; --- 20 unchanged lines hidden (view full) --- 82} 83 84void l2cap_init(){} 85 86void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 87 att_packet_handler = packet_handler; 88} 89 |
92void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 93 registered_l2cap_packet_handler = handler; | 90void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 91 registered_hci_event_handler = callback_handler->callback; |
94} 95 | 92} 93 |
96 | |
97int l2cap_reserve_packet_buffer(void){ 98 return 1; 99} 100 | 94int l2cap_reserve_packet_buffer(void){ 95 return 1; 96} 97 |
101int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ | 98int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ |
102 return 1; 103} 104 105int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 106 att_connection_t att_connection; 107 att_init_connection(&att_connection); 108 uint8_t response[max_mtu]; 109 uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]); 110 if (response_len){ 111 att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 112 } 113 return 0; 114} 115 116int sm_cmac_ready(void){ 117 return 1; 118} 119void sm_cmac_start(sm_key_t k, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8])){ | 99 return 1; 100} 101 102int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 103 att_connection_t att_connection; 104 att_init_connection(&att_connection); 105 uint8_t response[max_mtu]; 106 uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]); 107 if (response_len){ 108 att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 109 } 110 return 0; 111} 112 113int sm_cmac_ready(void){ 114 return 1; 115} 116void sm_cmac_start(sm_key_t k, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8])){ |
120 //sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); | 117 //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); |
121} 122int sm_le_device_index(uint16_t handle ){ 123 return -1; 124} 125 | 118} 119int sm_le_device_index(uint16_t handle ){ 120 return -1; 121} 122 |
126void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ | 123void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ |
127} 128 129// Set callback that will be executed when timer expires. | 124} 125 126// Set callback that will be executed when timer expires. |
130void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){ | 127void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ |
131} 132 133// Add/Remove timer source. | 128} 129 130// Add/Remove timer source. |
134void run_loop_add_timer(timer_source_t *timer){ | 131void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ |
135} 136 | 132} 133 |
137int run_loop_remove_timer(timer_source_t *timer){ | 134int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ |
138 return 1; 139} 140 141// todo: 142hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 143 printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 144 return NULL; 145} 146hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 147 printf("hci_connection_for_handle not implemented in mock backend\n"); 148 return NULL; 149} | 135 return 1; 136} 137 138// todo: 139hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 140 printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 141 return NULL; 142} 143hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 144 printf("hci_connection_for_handle not implemented in mock backend\n"); 145 return NULL; 146} |
150void hci_connections_get_iterator(linked_list_iterator_t *it){ | 147void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ |
151 // printf("hci_connections_get_iterator not implemented in mock backend\n"); | 148 // printf("hci_connections_get_iterator not implemented in mock backend\n"); |
152 linked_list_iterator_init(it, &connections); | 149 btstack_linked_list_iterator_init(it, &connections); |
153} 154 155// int hci_send_cmd(const hci_cmd_t *cmd, ...){ 156// // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 157// return 0; 158// } 159 160// int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ --- 13 unchanged lines hidden --- | 150} 151 152// int hci_send_cmd(const hci_cmd_t *cmd, ...){ 153// // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 154// return 0; 155// } 156 157// int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ --- 13 unchanged lines hidden --- |