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