-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_boot_code.c
168 lines (158 loc) · 4.57 KB
/
post_boot_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void MXC_Delay(uint32_t us);
typedef uint8_t i2c_addr_t;
int secure_send(uint8_t address, uint8_t *buffer, uint8_t len);
int secure_receive(i2c_addr_t address, uint8_t *buffer);
int get_provisioned_ids(uint32_t *buffer);
#define LED1 0
#define LED2 1
#define LED3 2
void LED_On(unsigned int idx);
void LED_Off(unsigned int idx);
void LED_Toggle(unsigned int idx);
void ap() {
uint8_t post_boot_buffer[256];
printf("Post boot: Insulin Pump started!\n");
uint32_t post_boot_component_ids[10];
int post_boot_component_cnt;
post_boot_component_cnt = get_provisioned_ids(post_boot_component_ids);
uint8_t post_boot_sensor_addr = 0;
uint8_t post_boot_actuator_addr = 0;
for (int i = 0; i < post_boot_component_cnt; i++) {
uint8_t addr = (uint8_t)(post_boot_component_ids[i] & 0xFF);
post_boot_buffer[0] = 0;
secure_send(addr, post_boot_buffer, 1);
secure_receive(addr, post_boot_buffer);
switch (post_boot_buffer[0]) {
case 0:
post_boot_sensor_addr = addr;
break;
case 1:
post_boot_actuator_addr = addr;
break;
}
}
uint32_t sensor_values[5] = {0, 0, 0, 0, 0};
int array_index = 0;
while (true) {
post_boot_buffer[0] = 1;
secure_send(post_boot_sensor_addr, post_boot_buffer, 1);
secure_receive(post_boot_sensor_addr, post_boot_buffer);
sensor_values[array_index] = *(uint32_t *)post_boot_buffer;
array_index = (array_index + 1) % 5;
uint32_t sensor_sum = 0;
for (int i = 0; i < 5; i++) {
sensor_sum += sensor_values[i];
}
float sensor_avg = ((float)sensor_sum) / 5.0;
if (sensor_avg > 128.0) {
post_boot_buffer[0] = 1;
post_boot_buffer[1] = 1;
secure_send(post_boot_actuator_addr, post_boot_buffer, 2);
secure_receive(post_boot_actuator_addr, post_boot_buffer);
printf("%%success: %s\n%%", post_boot_buffer);
} else {
post_boot_buffer[0] = 1;
post_boot_buffer[1] = 0;
secure_send(post_boot_actuator_addr, post_boot_buffer, 2);
}
MXC_Delay(500000);
}
}
void c0() {
uint8_t post_boot_buffer[256];
while (true) {
secure_receive(post_boot_buffer);
switch (post_boot_buffer[0]) {
case 0:
post_boot_buffer[0] = 1;
secure_send(post_boot_buffer, 1);
break;
case 1:
if (post_boot_buffer[1] == 1) {
LED_On(LED1);
LED_On(LED2);
LED_On(LED3);
const char *post_boot_flag =
"289f0cb9ffd3cfaf9b2a29c3edca4ab2a763f95359af6bfff2dc045e21b7125";
strcpy((char *)post_boot_buffer, post_boot_flag);
secure_send(post_boot_buffer, strlen(post_boot_flag) + 1);
} else {
LED_Off(LED1);
LED_Off(LED2);
LED_Off(LED3);
}
break;
}
}
}
void c1() {
uint8_t post_boot_buffer[256];
while (true) {
secure_receive(post_boot_buffer);
switch (post_boot_buffer[0]) {
case 0:
post_boot_buffer[0] = 1;
secure_send(post_boot_buffer, 1);
break;
case 1:
if (post_boot_buffer[1] == 1) {
LED_On(LED1);
LED_On(LED2);
LED_On(LED3);
const char *post_boot_flag =
"7a13ead272ef49b007d4cb5f8cf2c3089ec945d31b16ef7869960d5eee65292";
strcpy((char *)post_boot_buffer, post_boot_flag);
secure_send(post_boot_buffer, strlen(post_boot_flag) + 1);
} else {
LED_Off(LED1);
LED_Off(LED2);
LED_Off(LED3);
}
break;
}
}
}
void c2() {
uint8_t post_boot_buffer[256];
int array_index = 0;
uint32_t sensor_values[10] = {150, 150, 150, 150, 150,
150, 150, 150, 150, 150};
while (true) {
secure_receive(post_boot_buffer);
switch (post_boot_buffer[0]) {
case 0:
post_boot_buffer[0] = 0;
secure_send(post_boot_buffer, 1);
break;
case 1:
*(uint32_t *)post_boot_buffer = sensor_values[array_index];
secure_send(post_boot_buffer, sizeof(uint32_t));
array_index = (array_index + 1) % 10;
break;
}
}
}
void c3() {
uint8_t post_boot_buffer[256];
int array_index = 0;
uint32_t sensor_values[10] = {150, 150, 150, 150, 150,
150, 150, 150, 150, 150};
while (true) {
secure_receive(post_boot_buffer);
switch (post_boot_buffer[0]) {
case 0:
post_boot_buffer[0] = 0;
secure_send(post_boot_buffer, 1);
break;
case 1:
*(uint32_t *)post_boot_buffer = sensor_values[array_index];
secure_send(post_boot_buffer, sizeof(uint32_t));
array_index = (array_index + 1) % 10;
break;
}
}
}