Merge branch 'master' of gitlab.science.ru.nl:mlubbers/mTask
[mTask.git] / client / client.c
index 0218fb7..b501e0f 100644 (file)
@@ -37,15 +37,25 @@ void read_message(void)
                case MSG_SDS_UPD:
                        debug("Receiving an sds update");
                        //TODO do something with the return value
-                       sds_update();
+                       c = read16();
+                       sds_update(c);
                        break;
                case MSG_SDS_DEL:
                        debug("Receiving a delete SDS request");
-                       sds_delete();
+                       c = read16();
+                       sds_delete(c);
+                       write_byte('a');
+                       write16(c);
+                       write_byte('\n');
                        break;
                case MSG_DEL_TASK:
                        debug("Receiving a delete task request");
-                       task_delete();
+                       c = read16();
+                       task_delete(c);
+                       //Write acknowledgement
+                       write_byte('d');
+                       write16(c);
+                       write_byte('\n');
                        break;
                case MSG_GET_TASK:
                        debug("Receiving a task");
@@ -63,6 +73,8 @@ void read_message(void)
                default:
                        debug("Unknown message: %X", c);
                }
+       } else {
+//             debug("No input");
        }
 }
 
@@ -73,36 +85,27 @@ void loop(void)
        if(getmillis()-loopmillis < LOOPDELAY){
                return;
        }
+       loopmillis = getmillis();
 #endif
-
-       int ct;
-       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;
-               }
+       unsigned long cyclestart = getmillis();
+       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;
+                       if(t->interval == 0){
+                               task_delete(t->taskid);
                        }
-                       curtask->lastrun = cyclestart;
+                       t->lastrun = cyclestart;
                }
        }
 }
@@ -117,11 +120,8 @@ int main(int argc, char *argv[]){
        gargv = argv;
 #endif
 
-       //Initialize systems
+       //Initialize device independant functionality
        real_setup();
-       sds_init();
-       task_init();
-       debug("sending device spec");
 
 #ifndef ARDUINO_ESP8266_NODEMCU
        while(true){