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, 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 108 void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ 109 } 110 111 // Set callback that will be executed when timer expires. 112 void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){ 113 } 114 115 // Add/Remove timer source. 116 void run_loop_add_timer(timer_source_t *timer){ 117 } 118 119 int run_loop_remove_timer(timer_source_t *timer){ 120 return 1; 121 } 122 123 124 // int hci_send_cmd(const hci_cmd_t *cmd, ...){ 125 // // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 126 // return 0; 127 // } 128 129 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 130 // return 1; 131 // } 132 133 // void hci_disconnect_security_block(hci_con_handle_t con_handle){ 134 // printf("hci_disconnect_security_block \n"); 135 // } 136 137 // void hci_dump_log(const char * format, ...){ 138 // printf("hci_disconnect_security_block \n"); 139 // } 140 141 void l2cap_run(void){ 142 } 143