updates
[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 debug("Interval: ");
24 //Read interval
25 read16(fd, c, tasks[ct].interval);
26 //Read tasklength
27 debug("Length: ");
28 read16(fd, c, tasks[ct].tlen);
29
30 if(tasks[ct].tlen > MAXTASKSIZE)
31 die("Task is too long: %d\n", tasks[ct].tlen);
32 //Read task bytecode
33 for(int i = 0; i<tasks[ct].tlen; i++){
34 debug("Read %d\n", i);
35 read(fd, tasks[ct].bc+i, 1);
36 debug("t[][%i]: %d\n", i,
37 tasks[ct].bc[i]);
38 }
39 //Return the task number for later removal
40 debug("Received a task of length %d\n", tasks[ct].tlen);
41 tasks[ct].used = true;
42 return ct;
43 }
44
45 void task_delete(int fd)
46 {
47 read(fd, &c, 1);
48 tasks[c].used = false;
49 }
50
51 struct task *task_get(int num)
52 {
53 return tasks[num].used ? &tasks[num] : NULL;
54 }