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