X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Ftask.c;h=6e7ae0c39f044a7b86e3525d642797e2ed8f9c78;hb=HEAD;hp=4ac8ce5075d8af8912c80c0ed6ea4a8faf6be22a;hpb=6c8939998c64aafb8cdfa40e52a227bf72767648;p=mTask.git diff --git a/client/task.c b/client/task.c index 4ac8ce5..6e7ae0c 100644 --- a/client/task.c +++ b/client/task.c @@ -1,61 +1,113 @@ #include #include -#ifndef STM -#include -#include -#endif - #include "task.h" +#include "spec.h" #include "interface.h" +#include "mem.h" -struct task tasks[MAXTASKS]; +extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds; -void task_init(void) -{ - memset(&tasks, 0, sizeof(struct task)*MAXTASKS); -} +uint8_t taskid = 0; -int task_register(void) +void task_register(void) { - uint8_t ct; + debug("free memory: %lu\n", mem_free()); + int i; + if(mem_task+sizeof(struct task) > mem_sds){ + die("Out of memory... Not enough to allocate taskstruct"); + } - for(ct = 0; ctinterval = read16(); + debug("interval: %d\n", t->interval); + + //Interrupt task + if(is_interrupt_task(t)) { + + } + //Read tasklength - tasks[ct].tlen = read16(); - debug("task interval: %d, length: %d\n", - tasks[ct].interval, tasks[ct].tlen); + t->tasklength = read16(); + debug("task interval: %d, length: %d\n", t->interval, t->tasklength); + + if(mem_task+t->tasklength > mem_sds){ + die("Out of memory... Not enough to allocate bytes"); + } + + mem_task += sizeof(struct task); + t->bc = mem_task; + mem_task += t->tasklength; - if(tasks[ct].tlen > MAXTASKSIZE) - die("Task is too long: %d", tasks[ct].tlen); //Read task bytecode - for(unsigned int i = 0; itasklength; i++){ + t->bc[i] = read_byte(); } + //Return the task number for later removal - debug("Received a task of length %d", tasks[ct].tlen); - tasks[ct].used = true; - tasks[ct].lastrun = 0L; + debug("Received a task of length %d", t->tasklength); + t->lastrun = 0L; + t->taskid = taskid++; + t->value = 0; - return ct; + write_byte('t'); + write16(t->taskid); + write16(mem_free()); + write_byte('\n'); + debug("free memory: %lu\n", mem_free()); } -void task_delete(void) +bool is_interrupt_task(struct task *t) { - tasks[read_byte()].used = false; + return t->interval & (2 <<14); } -struct task *task_get(int num) +bool had_interrupt(struct task* t) { - return tasks[num].used ? &tasks[num] : NULL; + //Not implemented yet... + return false; + (void)t; +} + +struct task *task_head(void) +{ + return mem_task == mem_bottom ? NULL : (struct task *)mem_bottom; +} + +struct task *task_next(struct task *t) +{ + uint8_t *next = (uint8_t *)t + sizeof(struct task) + t->tasklength; + return next >= mem_task ? NULL : (struct task *)next; +} + +void task_delete(uint8_t c) +{ + debug("Going to delete task: %i", c); + struct task *t = task_head(); + while(t != NULL){ + if(t->taskid == c){ + break; + } + t = task_next(t); + } + //Write deletion spec + write_byte('d'); + write16(c); + write_byte('\n'); + + if(t != NULL){ + //We found the task, now we move everything from the end of the task up + //to the spacepointer right + uint8_t *start = (uint8_t *)t; + uint8_t *end = start + sizeof(struct task) + t->tasklength; + debug("Moving %lu bytes\n", mem_task-end); + for(int i = 0; i