0d9b125fad6c7d1025c5ff14aee3824bfa9b201f
[mTask.git] / client / task.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "task.h"
5 #include "spec.h"
6 #include "interface.h"
7
8 struct task tasks[MAXTASKS];
9
10 void task_init(void)
11 {
12 memset(&tasks, 0, sizeof(struct task)*MAXTASKS);
13 }
14
15 void task_register(void)
16 {
17 uint8_t ct;
18 uint16_t i;
19
20 for(ct = 0; ct<MAXTASKS; ct++)
21 if(!tasks[ct].used)
22 break;
23 if(ct == MAXTASKS)
24 die("Trying to add too much tasks...");
25
26 memset(&tasks[ct], 0, sizeof(struct task));
27 //Read interval
28 tasks[ct].interval = read16();
29
30 //Interrupt task
31 if(is_interrupt_task(&tasks[ct])) {
32
33 }
34
35 //Read tasklength
36 tasks[ct].tlen = read16();
37 debug("task interval: %d, length: %d\n",
38 tasks[ct].interval, tasks[ct].tlen);
39
40 if(tasks[ct].tlen > MAXTASKSIZE)
41 die("Task is too long: %d", tasks[ct].tlen);
42 //Read task bytecode
43 for(i = 0; i<tasks[ct].tlen; i++){
44 tasks[ct].bc[i] = read_byte();
45 // debug("t[][%i]: 0x%02x %d", i,
46 // tasks[ct].bc[i], tasks[ct].bc[i]);
47 }
48 //Return the task number for later removal
49 debug("Received a task of length %d", tasks[ct].tlen);
50 tasks[ct].used = true;
51 tasks[ct].lastrun = 0L;
52
53 write_byte('t');
54 write16(ct);
55 write_byte('\n');
56 }
57
58 bool is_interrupt_task(struct task *t)
59 {
60 return t->interval & (2 <<14);
61 }
62
63 bool had_interrupt(struct task* t)
64 {
65 //Not implemented yet...
66 return false;
67 (void)t;
68 }
69
70 void task_delete(void)
71 {
72 uint8_t c = read16();
73 tasks[c].used = false;
74 write_byte('d');
75 write16(c);
76 write_byte('\n');
77 }
78
79 struct task *task_get(int num)
80 {
81 return tasks[num].used ? &tasks[num] : NULL;
82 }