-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
194 lines (164 loc) · 4.16 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright (c) 2018, Tiny Mesh AS - https://tiny-mesh.com
* All rights reserved.
*/
#include "main.h"
#include "attributes.h"
#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
/**
* Send a tick event, signaling to all processes that a processing cycle have
* completed.
*/
PROCESS(tick_handler, ev) {
static event outgoing = {event_tick, NULL};
usleep(1000);
send_event(outgoing);
}
/**
* Check for IO on standard input
*/
PROCESS(io_handler, ev) {
static unsigned char buf[128];
static event outgoing = {event_input, &buf[0]};
PROCESS_YIELD(event_tick != ev.type);
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
// we don't accept multi byte charaters. This means only ASCII values will be
// sent and code drops any multi-key (ie Alt+S) or special keys (ie arrow keys)
if (1 == read(0, buf, 4)) {
recv_byte(buf[0]);
}
}
/**
* Packet interface
*
* - Accept to get / set attribute values
* - Accept packet to reset attribute table
* - Accept packets to shutdown
*
* Packets:
* // get attribute 1 from table 2
* GET 2 1
*
* // Set attribute 0 in table 3
* GET 3 0 99
*
* // Reset table 3
* RESET 3
*
* // Shutdown the process
* QUIT
*/
static void write_bytes(unsigned char *data, int size) {
for (int i = 0; i < size; i++) {
write_byte(data[i]);
}
write_byte('\n');
}
#define ERR_INPUT "ERROR INPUT"
#define ERR_NO_ATTR "ERROR NO-ATTR"
#define ERR_INVALID "ERROR INVALID"
#define RESPOND(var, str) { \
memcpy(var, str, sizeof(str)); \
write_bytes((unsigned char *) var, sizeof(str)); \
}
PROCESS(attributes_handler, ev) {
unsigned char res[128] = "";
PROCESS_YIELD(event_input != ev.type);
if (0 == memcmp(ev.data, "GET ", 4)) {
if (ev.size != 8) {
RESPOND(&res, ERR_INPUT);
reset_input();
return;
} else {
int table = ((unsigned char *) ev.data)[4] - 48;
int attr = ((unsigned char *) ev.data)[6] - 48;
int value = attr_get(table, attr);
if (-1 == value || -2 == value) {
RESPOND(&res, ERR_NO_ATTR);
reset_input();
return;
} else {
memcpy(&res, "ATTR ", 5);
res[5] = (unsigned char) value + 48;
res[6] = 0;
write_bytes((unsigned char *) &res, 7);
reset_input();
return;
}
}
} else if (0 == memcmp(ev.data, "SET ", 4)) {
if (ev.size != 10) {
RESPOND(&res, ERR_INPUT);
reset_input();
return;
} else {
int table = ((unsigned char *) ev.data)[4] - 48;
int attr = ((unsigned char *) ev.data)[6] - 48;
int value = ((unsigned char *) ev.data)[8] - 48;
int setValue = attr_set(table, attr, value);
if (-1 == value || -2 == value) {
RESPOND(&res, ERR_NO_ATTR);
reset_input();
return;
}
else{
memcpy(&res, "SUCCESS ", 8);
res[8] = 0;
write_bytes((unsigned char *) &res, 9);
reset_input();
return;
}
}
}
else if (0 == memcmp(ev.data, "RESET ", 6)){
if (ev.size != 8) {
RESPOND(&res, ERR_INPUT);
reset_input();
return;
} else {
int table = ((unsigned char *) ev.data)[6] - 48;
int value = attr_reset(table);
if (-1 == value) {
RESPOND(&res, ERR_NO_ATTR);
}
else {
memcpy(&res, "SUCCESS ", 8);
res[8] = 0;
write_bytes((unsigned char *) &res, 9);
}
reset_input();
return;
}
}
else if (0 == memcmp(ev.data, "QUIT ", 5)){
exit(0);
}
RESPOND(&res, ERR_INVALID);
reset_input();
}
void main() {
/* register all the tasks to be scheduled */
void (*tasklist[])() = {tick_handler, io_handler, attributes_handler};
int taskcount;
int buf;
PRINT("start\n");
queue_init();
set_term_raw();
// set the first tick
queue_insert((event) {.type = event_tick});
while (1) {
event ev = queue_pop();
if (event_quit == ev.type) {
break;
}
for (taskcount = 0; taskcount < NUMTASKS(tasklist); taskcount++) {
(*tasklist[taskcount])(ev);
}
}
PRINT("end\n");
}