update spec, and shares
[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 debug("interval: %d\n", tasks[ct].interval);
30
31 //Interrupt task
32 if(is_interrupt_task(&tasks[ct])) {
33
34 }
35
36 //Read tasklength
37 tasks[ct].tlen = read16();
38 debug("task interval: %d, length: %d\n",
39 tasks[ct].interval, tasks[ct].tlen);
40
41 if(tasks[ct].tlen > MAXTASKSIZE)
42 die("Task is too long: %d", tasks[ct].tlen);
43 //Read task bytecode
44 for(i = 0; i<tasks[ct].tlen; i++){
45 tasks[ct].bc[i] = read_byte();
46 }
47 //Return the task number for later removal
48 debug("Received a task of length %d", tasks[ct].tlen);
49 tasks[ct].used = true;
50 tasks[ct].lastrun = 0L;
51
52 write_byte('t');
53 write16(ct);
54 write_byte('\n');
55 }
56
57 bool is_interrupt_task(struct task *t)
58 {
59 return t->interval & (2 <<14);
60 }
61
62 bool had_interrupt(struct task* t)
63 {
64 //Not implemented yet...
65 return false;
66 (void)t;
67 }
68
69 void task_delete(void)
70 {
71 uint8_t c = read16();
72 tasks[c].used = false;
73 write_byte('d');
74 write16(c);
75 write_byte('\n');
76 }
77
78 struct task *task_get(int num)
79 {
80 return tasks[num].used ? &tasks[num] : NULL;
81 }