add task deletion and acknowledgements
[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 void task_register(void)
20 {
21 uint8_t ct;
22 uint16_t i;
23
24 for(ct = 0; ct<MAXTASKS; ct++)
25 if(!tasks[ct].used)
26 break;
27 if(ct == MAXTASKS)
28 die("Trying to add too much tasks...");
29
30 memset(&tasks[ct], 0, sizeof(struct task));
31 //Read interval
32 tasks[ct].interval = read16();
33 //Read tasklength
34 tasks[ct].tlen = read16();
35 debug("task interval: %d, length: %d\n",
36 tasks[ct].interval, tasks[ct].tlen);
37
38 if(tasks[ct].tlen > MAXTASKSIZE)
39 die("Task is too long: %d", tasks[ct].tlen);
40 //Read task bytecode
41 for(i = 0; i<tasks[ct].tlen; i++){
42 tasks[ct].bc[i] = read_byte();
43 // debug("t[][%i]: 0x%02x %d", i,
44 // tasks[ct].bc[i], tasks[ct].bc[i]);
45 }
46 //Return the task number for later removal
47 debug("Received a task of length %d", tasks[ct].tlen);
48 tasks[ct].used = true;
49 tasks[ct].lastrun = 0L;
50
51 write_byte('t');
52 write16(ct);
53 write_byte('\n');
54 }
55
56 void task_delete(void)
57 {
58 uint8_t c = read16();
59 tasks[c].used = false;
60 write_byte('d');
61 write16(c);
62 write_byte('\n');
63 }
64
65 struct task *task_get(int num)
66 {
67 return tasks[num].used ? &tasks[num] : NULL;
68 }