X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Ftask.c;h=6e7ae0c39f044a7b86e3525d642797e2ed8f9c78;hb=HEAD;hp=dfdb71b423732bbcb43f362be64cb004df224612;hpb=c3bb3dde2bb886a9f86f9de5f4f96beb8d86b53d;p=mTask.git diff --git a/client/task.c b/client/task.c index dfdb71b..6e7ae0c 100644 --- a/client/task.c +++ b/client/task.c @@ -1,62 +1,62 @@ #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; void task_register(void) { - uint8_t ct; - uint16_t i; + 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(&tasks[ct])) { + 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(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; write_byte('t'); - write16(ct); + write16(t->taskid); + write16(mem_free()); write_byte('\n'); + debug("free memory: %lu\n", mem_free()); } bool is_interrupt_task(struct task *t) @@ -71,16 +71,43 @@ bool had_interrupt(struct task* t) (void)t; } -void task_delete(void) +struct task *task_head(void) { - uint8_t c = read16(); - tasks[c].used = false; - write_byte('d'); - write16(c); - write_byte('\n'); + 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; } -struct task *task_get(int num) +void task_delete(uint8_t c) { - return tasks[num].used ? &tasks[num] : NULL; + 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