This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processes.cpp
254 lines (202 loc) · 5.72 KB
/
Processes.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#define FW_DEBUG
#include "Processes.h"
#include "CommandSet.h"
#include "State.h"
#include "addresses.h"
void Processes::setup()
{
/*
Dynamically allocate our array of processes. This is
risky on an Arduino with it's meagre 2kb of SRAM,
but it makes registering of processes cleaner,
at the expense of setup time.
Additionally it is important that none of the processes
malloc themselves memory whilst registering, as calls
to realloc to grow the process table will have to go
around that memory, wasting heap space.
*/
num_tasks = 0;
ptable_size = DFL_PROCESS_TABLE_SIZE;
tasks = (process**) malloc(ptable_size * sizeof(process*));
}
void Processes::run()
{
for (size_t i = 0; i < num_tasks; i++) {
if (tasks[i]->enabled && millis() >= (tasks[i]->last_run + tasks[i]->interval)){
tasks[i]->callback(tasks[i]->id);
tasks[i]->last_run = millis();
}
}
}
/**
Crash if we cannot safely continue
**/
void Processes::panic(int error)
{
switch (error) {
case PROCESS_ERR_OOM:
Serial.println(F("!! PANIC: OOM!"));
break;
default:
Serial.println(F("!! PANIC"));
}
/* Crash (AVR specific) */
while (true) asm volatile("nop\n");
}
/**
Print UNIX-like process status table
Thsi is expensive but should be run occasionally
**/
void Processes::status()
{
Serial.println();
Serial.println(F("pid \t enable \t interval \t last run \t callback \t label"));
Serial.println(F("--- \t ------ \t -------- \t -------- \t -------- \t -----"));
Serial.flush();
delay(10);
for (size_t i=0; i<num_tasks; i++) {
Serial.print(tasks[i]->id);
Serial.print(F("\t "));
delay(10);
tasks[i]->enabled ? Serial.print(F("true ")) : Serial.print(F("false"));
Serial.print(F("\t\t "));
delay(10);
Serial.print(tasks[i]->interval);
Serial.print(F("\t\t "));
delay(10);
Serial.print(tasks[i]->last_run);
Serial.print(F("\t\t "));
delay(10);
/* Apparently this cast is fine 0_o */
Serial.print((uint16_t) tasks[i]->callback, HEX);
Serial.print(F("\t\t "));
delay(10);
Serial.print(tasks[i]->label);
Serial.println();
Serial.flush();
delay(10);
}
Serial.println(F("--- \t ------ \t -------- \t -------- \t -------- \t -----"));
delay(10);
Serial.print(F("mem: "));
Serial.print(memcheck());
Serial.print(F(" free of "));
Serial.println(RAMEND - RAMSTART +1, DEC);
Serial.flush();
delay(10);
}
/**
Direct memory access is fun
**/
size_t Processes::memcheck()
{
size_t stackptr, heapptr;
stackptr = (size_t) malloc(4);
heapptr = stackptr;
free((void*) stackptr);
stackptr = (size_t)(&stackptr);
return stackptr - heapptr;
}
/*** PROCESS MANAGEMENT ROUTINES *******************************************************************/
size_t Processes::add(process* proc)
{
/*
Register a new process. The process struct should have already been allocated.
First process is 0, we always increment from there on.
@return: index of process in the process table
*/
if (num_tasks >= ptable_size)
grow_table(num_tasks - ptable_size +1);
// TODO: Check pointer logic
tasks[num_tasks] = proc;
proc->id = num_tasks++;
/* Process id is the number -1 as we zero index */
return num_tasks-1;
}
process* Processes::get_by_id(pid_t pid)
{
if (pid >= num_tasks)
return NULL;
return tasks[pid];
}
/**
Not particularly safe, but useful
Get a reference to a process by matching callback
**/
process* Processes::get_by_callback(void (*callback)(pid_t))
{
size_t num_hits = 0;
process* found_process;
process** tasks_p = tasks;
while (tasks_p != NULL){
if ((*tasks_p)->callback == callback) {
num_hits++;
found_process = *tasks_p++;
}
}
return num_hits == 1 ? found_process : NULL;
}
boolean Processes::is_enabled(pid_t pid)
{
if (pid >= num_tasks) {
return false;
} else {
return tasks[pid]->enabled;
}
}
/**
Used to get more memory for storing processes
**/
void Processes::grow_table(size_t num)
{
/*
Reallocate memory for ther process table, panicking if this does not work.
*/
ptable_size += num;
process** new_tasks = (process**) realloc(tasks, ptable_size * sizeof(process*));
if (new_tasks == NULL) panic(PROCESS_ERR_OOM);
tasks = new_tasks;
}
void Processes::disable(pid_t pid)
{
tasks[pid]->enabled = false;
}
/**
Enable a process and reset last_run in an attempt to make it fire immediately;
This is not guaranteed if the robot has only just started running
**/
void Processes::enable(pid_t pid)
{
tasks[pid]->enabled = true;
tasks[pid]->last_run = 0;
}
/*
* Update a process' callback and/or interval by id reference
* @return: -1 if failure, 0 if success
*/
int Processes::change(pid_t pid, void (*callback)(pid_t), unsigned long interval)
{
return change(pid, callback) | change(pid, interval);
}
int Processes::change(pid_t pid, void (*callback)(pid_t))
{
if (pid >= num_tasks) return -1;
tasks[pid]->callback = callback;
return 0;
}
int Processes::change(pid_t pid, unsigned long interval)
{
if (pid >= num_tasks) return -1;
tasks[pid]->interval = interval;
return 0;
}
/*
* Set a process' last run to the current time.
* Useful for state-machine like behaviour
* @return: -1 if failure, 0 if success
*/
int Processes::forward(pid_t pid) {
if (pid >= num_tasks) return -1;
tasks[pid]->last_run = millis();
return 0;
}