X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Ftask.c;h=c5fc41da28dae7d1b2aab345f4dd5e66d2e8e73b;hb=09b207a39b7791098daafd7d87c3ad9d3db3e19f;hp=9ef54033662c5a5a7a280dc83e996f1cbc4036db;hpb=55afb005ced3bba3813163596cdc7288a318a3c2;p=mTask.git diff --git a/client/task.c b/client/task.c index 9ef5403..c5fc41d 100644 --- a/client/task.c +++ b/client/task.c @@ -1,68 +1,120 @@ #include #include -#ifndef STM -#include -#include -#endif - #include "task.h" +#include "spec.h" #include "interface.h" -struct task tasks[MAXTASKS]; +#define TASKSPACE 128 + +uint8_t taskspace[TASKSPACE] = {0}; +uint8_t *spacepointer = (uint8_t *)&taskspace; +uint8_t taskid = 0; void task_init(void) { - memset(&tasks, 0, sizeof(struct task)*MAXTASKS); + memset(&taskspace, 0, TASKSPACE); } void task_register(void) { - uint8_t ct; - uint16_t i; + debug("free memory: %lu\n", ((uint8_t *)taskspace)-spacepointer+TASKSPACE); + int i; + if(spacepointer+sizeof(struct task) > taskspace+TASKSPACE){ + 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(spacepointer+t->tasklength > taskspace+TASKSPACE){ + die("Out of memory... Not enough to allocate bytes"); + } + + spacepointer += sizeof(struct task); + t->bc = spacepointer; + spacepointer += 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++; write_byte('t'); - write16(ct); + write16(t->taskid); write_byte('\n'); + debug("free memory: %lu\n", ((uint8_t *)taskspace)+TASKSPACE-spacepointer); } -void task_delete(void) +bool is_interrupt_task(struct task *t) { - uint8_t c = read_byte(); - tasks[c].used = false; - write_byte('d'); - write16(c); - write_byte('\n'); + return t->interval & (2 <<14); +} + +bool had_interrupt(struct task* t) +{ + //Not implemented yet... + return false; + (void)t; +} + +struct task *task_head(void) +{ + return spacepointer == (uint8_t *)&taskspace ? NULL + : (struct task *)&taskspace; +} + +struct task *task_next(struct task *t) +{ + uint8_t *next = (uint8_t *)t; + next += sizeof(struct task); + next += t->tasklength; + + return next >= spacepointer ? 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); + debug("spacepointer: %p", spacepointer); + struct task *t = task_head(); + while(t != NULL){ + if(t->taskid == c){ + break; + } + t = task_next(t); + } + + 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", spacepointer-end); + for(int i = 0; i