xref: /btstack/test/mesh/mock.c (revision c9921182ab4b1f83e3e5c671446dca5ffdf45b90)
1 
2 #include "mock.h"
3 
4 #include <stdint.h>
5 #include <string.h>
6 
7 #include "ble/att_db.h"
8 #include "hci.h"
9 #include "hci_dump.h"
10 #include "l2cap.h"
11 #include "rijndael.h"
12 
13 
14 static btstack_linked_list_t event_packet_handlers;
15 
16 static uint8_t  packet_buffer[256];
17 static uint16_t packet_buffer_len = 0;
18 
19 static uint8_t aes128_cyphertext[16];
20 
21 static int report_aes128;
22 static int report_random;
23 
24 // #define ENABLE_PACKET_LOGGER
25 // #define ENABLE_AES128_LOGGER
26 
27 void mock_init(void){
28 	 srand(0);
29 }
30 
31 uint8_t * mock_packet_buffer(void){
32 	return packet_buffer;
33 }
34 
35 void mock_clear_packet_buffer(void){
36 	packet_buffer_len = 0;
37 	memset(packet_buffer, 0, sizeof(packet_buffer));
38 }
39 
40 static void dump_packet(int packet_type, uint8_t * buffer, uint16_t size){
41 #ifdef ENABLE_PACKET_LOGGER
42 	static int packet_counter = 1;
43 	char var_name[80];
44 	sprintf(var_name, "test_%s_packet_%02u", packet_type == HCI_COMMAND_DATA_PACKET ? "command" : "acl", packet_counter);
45 	printf("uint8_t %s[] = { ", var_name);
46 	for (int i = 0; i < size ; i++){
47 		if ((i % 16) == 0) printf("\n    ");
48 		printf ("0x%02x, ", buffer[i]);
49 	}
50 	printf("};\n");
51 	packet_counter++;
52 #else
53 UNUSED(packet_type);
54 UNUSED(buffer);
55 UNUSED(size);
56 #endif
57 }
58 
59 static void aes128_calc_cyphertext(uint8_t key[16], uint8_t plaintext[16], uint8_t cyphertext[16]){
60 	uint32_t rk[RKLENGTH(KEYBITS)];
61 	int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS);
62 	rijndaelEncrypt(rk, nrounds, plaintext, cyphertext);
63 }
64 
65 void mock_simulate_hci_event(uint8_t * packet, uint16_t size){
66 	dump_packet(HCI_EVENT_PACKET, packet, size);
67 	hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
68     // dispatch to all event handlers
69     btstack_linked_list_iterator_t it;
70     btstack_linked_list_iterator_init(&it, &event_packet_handlers);
71     while (btstack_linked_list_iterator_has_next(&it)){
72         btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
73         entry->callback(HCI_EVENT_PACKET, 0, packet, size);
74     }
75 }
76 
77 static void aes128_report_result(void){
78 	uint8_t le_enc_result[22];
79 	uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 };
80 	memcpy (le_enc_result, enc1_data, 6);
81 	reverse_128(aes128_cyphertext, &le_enc_result[6]);
82 	mock_simulate_hci_event(le_enc_result, sizeof(le_enc_result));
83 }
84 
85 static void random_report_result(void){
86 	uint8_t le_rand_result[14];
87 	uint8_t rand_complete[] = { 0x0e, 0x0c, 0x01, 0x18, 0x20, 0x00 };
88 	memcpy (le_rand_result, rand_complete, 6);
89 	int i;
90 	for (i=0;i<8;i++){
91 		le_rand_result[6+i]= rand();
92 	}
93 	mock_simulate_hci_event(le_rand_result, sizeof(le_rand_result));
94 }
95 
96 int mock_process_hci_cmd(void){
97 	if (report_aes128){
98 		report_aes128 = 0;
99 		aes128_report_result();
100 		return 1;
101 	}
102 	if (report_random){
103 		report_random = 0;
104 		random_report_result();
105 		return 1;
106 	}
107 	return 0;
108 }
109 
110 void mock_simulate_hci_state_working(void){
111 	uint8_t packet[] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
112 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
113 }
114 
115 int hci_can_send_command_packet_now(void){
116 	return 1;
117 }
118 
119 int hci_send_cmd(const hci_cmd_t *cmd, ...){
120     va_list argptr;
121     va_start(argptr, cmd);
122     uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
123     va_end(argptr);
124 	hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
125 	dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
126 	packet_buffer_len = len;
127 
128 	// track le encrypt and le rand
129 	if (cmd->opcode ==  hci_le_encrypt.opcode){
130 	    uint8_t * key_flipped = &packet_buffer[3];
131 	    uint8_t key[16];
132 		reverse_128(key_flipped, key);
133 	    uint8_t * plaintext_flipped = &packet_buffer[19];
134 	    uint8_t plaintext[16];
135  		reverse_128(plaintext_flipped, plaintext);
136 	    aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
137 	    report_aes128 = 1;
138 #ifdef ENABLE_AES128_LOGGER
139 	    printf("AES128 Operation\n");
140 	    printf("Key:    "); printf_hexdump(key, 16);
141 	    printf("Plain:  "); printf_hexdump(plaintext, 16);
142 	    printf("Cipher: "); printf_hexdump(aes128_cyphertext, 16);
143 #endif
144 	}
145 	if (cmd->opcode == hci_le_rand.opcode){
146 		report_random = 1;
147 	}
148 	return 0;
149 }
150 
151 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
152     btstack_linked_list_add_tail(&event_packet_handlers, (btstack_linked_item_t*) callback_handler);
153 }
154 
155 HCI_STATE hci_get_state(void){
156 	return HCI_STATE_WORKING;
157 }
158 
159 void btstack_run_loop_add_timer(btstack_timer_source_t * ts){
160     UNUSED(ts);
161 }
162 int btstack_run_loop_remove_timer(btstack_timer_source_t * ts){
163     UNUSED(ts);
164 	return 0;
165 }
166 void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout){
167     UNUSED(ts);
168     UNUSED(timeout);
169 }
170 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*fn)(btstack_timer_source_t * ts)){
171     UNUSED(ts);
172     UNUSED(fn);
173 }
174 static void * timer_context;
175 void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context){
176     UNUSED(ts);
177 	timer_context = context;
178 }
179 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts){
180     UNUSED(ts);
181 	return timer_context;
182 }
183 void hci_halting_defer(void){
184 }
185 
186