split up in header files and modules and start with sds storage support
[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] = {0};
10 uint8_t c;
11
12 int task_register(int fd)
13 {
14 uint8_t ct;
15
16 for(ct = 0; ct<MAXTASKS; ct++)
17 if(!tasks[ct].used)
18 break;
19 if(ct == MAXTASKS)
20 die("Trying to add too much tasks...\n");
21
22 memset(&tasks[ct], 0, sizeof(struct task));
23 //Read interval
24 read(fd, &c, 1);
25 tasks[ct].interval = 256*c;
26 read(fd, &c, 1);
27 tasks[ct].interval += c;
28 //Read tasklength
29 read(fd, &c, 1);
30 tasks[ct].tlen = 256*c;
31 read(fd, &c, 1);
32 tasks[ct].tlen += c;
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 debug("Read %d\n", i);
38 read(fd, tasks[ct].bc+i, 1);
39 debug("t[][%i]: %d\n", i,
40 tasks[ct].bc[i]);
41 }
42 //Return the task number for later removal
43 debug("Received a task of length %d\n", tasks[ct].tlen);
44 tasks[ct].used = true;
45 return ct;
46 }
47
48 void task_delete(int fd)
49 {
50 read(fd, &c, 1);
51 tasks[c].used = false;
52 }
53
54 struct task *task_get(int num)
55 {
56 return tasks[num].used ? &tasks[num] : NULL;
57 }