update
authorMart Lubbers <mart@martlubbers.net>
Wed, 5 Apr 2017 09:13:11 +0000 (11:13 +0200)
committerMart Lubbers <mart@martlubbers.net>
Wed, 5 Apr 2017 09:13:11 +0000 (11:13 +0200)
client/.gitignore
client/client.c
client/linux/client [deleted file]
client/mem.c [new file with mode: 0644]
client/task.c
client/task.h

index e6a8598..9308325 100644 (file)
@@ -3,3 +3,4 @@ main
 build
 *.bin
 .dep
+linux/client
index 6502fb7..59c611f 100644 (file)
@@ -79,34 +79,28 @@ void loop(void)
 #endif
        debug("Loop");
 
-       int ct;
+       struct task *t;
        long cyclestart;
-       struct task *curtask;
 
        read_message();
 
        //Run tasks
        cyclestart = getmillis();
-       for(ct = 0; ct<MAXTASKS; ct++){
-               //See whether the task is even in use
-               if((curtask = task_get(ct)) == NULL){
-//                     debug("Task %d not implemented\n", ct);
-                       continue;
-               }
+       for(struct task *t = task_head(); t != NULL; t = task_next(t)){
                //interrupt task
-               if(is_interrupt_task(curtask) && had_interrupt(curtask)){
-                       debug("Interrupt task %d not implemented", ct);
-                       run_task(curtask);
+               if(is_interrupt_task(t) && had_interrupt(t)){
+                       debug("Interrupt task %d not implemented", t->taskid);
+                       run_task(t);
                //Interval task, and interval passed
-               } else if(cyclestart-curtask->lastrun > curtask->interval){
-                       debug("Running interval task: %d", ct);
-                       run_task(curtask);
+               } else if(cyclestart-t->lastrun > t->interval){
+                       debug("Running interval task: %d", t->taskid);
+                       run_task(t);
 
                        //Oneshot task, thus disable
-                       if(curtask->interval == 0){
-                               curtask->used = false;
-                       }
-                       curtask->lastrun = cyclestart;
+//                     if(t->interval == 0){
+//                             curtask->used = false;
+//                     }
+                       t->lastrun = cyclestart;
                }
        }
 }
diff --git a/client/linux/client b/client/linux/client
deleted file mode 100755 (executable)
index b269fc2..0000000
Binary files a/client/linux/client and /dev/null differ
diff --git a/client/mem.c b/client/mem.c
new file mode 100644 (file)
index 0000000..38bd6bd
--- /dev/null
@@ -0,0 +1,3 @@
+#include <stdlib.h>
+
+struct task *task_head = NULL;
index 1c16ce5..12789ce 100644 (file)
@@ -5,52 +5,62 @@
 #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;
+struct task *head = NULL;
 
 void task_init(void)
 {
-       memset(&tasks, 0, sizeof(struct task)*MAXTASKS);
+       memset(&taskspace, 0, TASKSPACE);
+       head = NULL;
 }
 
 void task_register(void)
 {
-       uint8_t ct;
-       uint16_t i;
+       int i;
+       if(spacepointer+sizeof(struct task) > taskspace+TASKSPACE){
+               die("Out of memory... Not enough to allocate taskstruct");
+       }
 
-       for(ct = 0; ct<MAXTASKS; ct++)
-               if(!tasks[ct].used)
-                       break;
-       if(ct == MAXTASKS)
-               die("Trying to add too much tasks...");
+       //We haven't had a task before, therefore we initialize head
+       struct task *t = (struct task *)spacepointer;
+       if(head == NULL) {
+               head = t;
+       }
 
-       memset(&tasks[ct], 0, sizeof(struct task));
        //Read interval
-       tasks[ct].interval = read16();
-       debug("interval: %d\n", tasks[ct].interval);
+       t->interval = 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(spacepointer+t->tasklength > taskspace+TASKSPACE){
+               die("Out of memory... Not enough to allocate bytes");
+       }
+
+       spacepointer += t->tasklength;
 
-       if(tasks[ct].tlen > MAXTASKSIZE)
-               die("Task is too long: %d", tasks[ct].tlen);
        //Read task bytecode
-       for(i = 0; i<tasks[ct].tlen; i++){
-               tasks[ct].bc[i] = read_byte();
+       for(i = 0; i<t->tasklength; 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');
 }
 
@@ -66,16 +76,54 @@ 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 head;
+}
+
+struct task *task_next(struct task *t)
+{
+       if(t == NULL){
+               return NULL;
+       }
+
+       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(void)
 {
-       return tasks[num].used ? &tasks[num] : NULL;
+       uint8_t c = read16();
+       struct task *t = NULL;
+       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<spacepointer-end; i++){
+                       start[i] = end[i];
+               }
+
+               //Decrement the spacepointer
+               spacepointer -= end-start;
+
+               if(t == head){
+                       head = task_next(t);
+               }
+
+               //Write acknowledgement
+               write_byte('d');
+               write16(c);
+               write_byte('\n');
+       }
 }
index 6542b1c..375aafc 100644 (file)
@@ -1,8 +1,5 @@
 #ifndef TASK_H
 #define TASK_H
-
-#define MAXTASKSIZE 1024
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -11,20 +8,22 @@ extern "C" {
 #include <stdbool.h>
 
 struct task {
-       uint8_t bc[MAXTASKSIZE];
-       uint16_t tlen;
+       uint16_t tasklength;
        uint16_t interval;
-       long lastrun;
-       bool used;
+       unsigned long lastrun;
+       uint8_t taskid;
+       struct task *next;
+       uint8_t *bc;
 };
 
 bool is_interrupt_task(struct task* t);
 bool had_interrupt(struct task* t);
 
+struct task *task_head(void);
+struct task *task_next(struct task *t);
 void task_init(void);
 void task_register(void);
 void task_delete(void);
-struct task *task_get(int num);
 
 #ifdef __cplusplus
 }