robustify task and sds initialization
[mTask.git] / int / task.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include "misc.h"
7 #include "task.h"
8
9 struct task tasks[MAXTASKS];
10 uint8_t c;
11
12 void task_init()
13 {
14 memset(&tasks, 0, sizeof(struct task)*MAXTASKS);
15 }
16
17 int task_register(int fd)
18 {
19 uint8_t ct;
20
21 for(ct = 0; ct<MAXTASKS; ct++)
22 if(!tasks[ct].used)
23 break;
24 if(ct == MAXTASKS)
25 die("Trying to add too much tasks...\n");
26
27 memset(&tasks[ct], 0, sizeof(struct task));
28 //Read interval
29 read16(fd, c, tasks[ct].interval);
30 //Read tasklength
31 read16(fd, c, tasks[ct].tlen);
32
33 if(tasks[ct].tlen > MAXTASKSIZE)
34 die("Task is too long: %d\n", tasks[ct].tlen);
35 //Read task bytecode
36 for(int i = 0; i<tasks[ct].tlen; i++){
37 read(fd, tasks[ct].bc+i, 1);
38 debug("t[][%i]: 0x%02x %d\n", i,
39 tasks[ct].bc[i], tasks[ct].bc[i]);
40 }
41 //Return the task number for later removal
42 debug("Received a task of length %d\n", tasks[ct].tlen);
43 tasks[ct].used = true;
44 return ct;
45 }
46
47 void task_delete(int fd)
48 {
49 read(fd, &c, 1);
50 tasks[c].used = false;
51 }
52
53 struct task *task_get(int num)
54 {
55 return tasks[num].used ? &tasks[num] : NULL;
56 }