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