From 1ab70aca8b327ed09861b6ff41f86f129556fd15 Mon Sep 17 00:00:00 2001
From: andreas <andreas@wellenvogel.de>
Date: Sat, 16 Nov 2024 20:36:28 +0100
Subject: [PATCH] intermediate: running sensor tasks inits after all user init
 tasks

---
 lib/api/GwApi.h             | 8 +++++++-
 lib/iictask/GwIicTask.h     | 2 +-
 lib/spitask/GwSpiTask.h     | 2 +-
 lib/usercode/GwUserCode.cpp | 9 ++++++---
 lib/usercode/GwUserCode.h   | 7 +++++--
 5 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib/api/GwApi.h b/lib/api/GwApi.h
index 5156454..88f9690 100644
--- a/lib/api/GwApi.h
+++ b/lib/api/GwApi.h
@@ -240,7 +240,10 @@ static void checkDef(T... args){};
 #define DECLARE_USERTASK_PARAM(task,...)
 #endif
 #ifndef DECLARE_INITFUNCTION
-#define DECLARE_INITFUNCTION(task)
+#define DECLARE_INITFUNCTION(task,...)
+#endif
+#ifndef DECLARE_INITFUNCTION_ORDER
+#define DECLARE_INITFUNCTION_ORDER(task,...)
 #endif
 #ifndef DECLARE_CAPABILITY
 #define DECLARE_CAPABILITY(name,value)
@@ -322,5 +325,8 @@ class ConfiguredSensors : public GwApi::TaskInterfaces::Base{
 };
 DECLARE_TASKIF(ConfiguredSensors);    
 
+//order for late init functions
+//all user tasks should have lower orders (default: 0)
+#define GWLATEORDER 9999
 
 #endif
diff --git a/lib/iictask/GwIicTask.h b/lib/iictask/GwIicTask.h
index 608262b..74f935b 100644
--- a/lib/iictask/GwIicTask.h
+++ b/lib/iictask/GwIicTask.h
@@ -3,5 +3,5 @@
 #include "GwApi.h"
 #include "GwSensor.h"
 void initIicTask(GwApi *api);
-DECLARE_INITFUNCTION(initIicTask);
+DECLARE_INITFUNCTION_ORDER(initIicTask,GWLATEORDER);
 #endif
\ No newline at end of file
diff --git a/lib/spitask/GwSpiTask.h b/lib/spitask/GwSpiTask.h
index ddcac8b..0714a31 100644
--- a/lib/spitask/GwSpiTask.h
+++ b/lib/spitask/GwSpiTask.h
@@ -16,5 +16,5 @@
 #define _GWSPITASK_H
 #include "GwApi.h"
 void initSpiTask(GwApi *api);
-DECLARE_INITFUNCTION(initSpiTask);
+DECLARE_INITFUNCTION_ORDER(initSpiTask,GWLATEORDER);
 #endif
\ No newline at end of file
diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp
index 44af1d4..08164b6 100644
--- a/lib/usercode/GwUserCode.cpp
+++ b/lib/usercode/GwUserCode.cpp
@@ -1,6 +1,7 @@
 #define DECLARE_USERTASK(task) GwUserTaskDef __##task##__(task,#task);
 #define DECLARE_USERTASK_PARAM(task,...) GwUserTaskDef __##task##__(task,#task,__VA_ARGS__);
 #define DECLARE_INITFUNCTION(task) GwInitTask __Init##task##__(task,#task);
+#define DECLARE_INITFUNCTION_ORDER(task,order) GwInitTask __Init##task##__(task,#task,order);
 #define DECLARE_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,#value);
 #define DECLARE_STRING_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,value); 
 #define DECLARE_TASKIF(type) \
@@ -43,8 +44,8 @@ class GwInitTask{
         GwInitTask(TaskFunction_t task, String name){
             initTasks.push_back(GwUserTask(name,task));
         }
-        GwInitTask(GwUserTaskFunction task, String name){
-            initTasks.push_back(GwUserTask(name,task));
+        GwInitTask(GwUserTaskFunction task, String name,int order=0){
+            initTasks.push_back(GwUserTask(name,task,GwUserTask::DEFAULT_STACKSIZE,order));
         }
 };
 class GwUserCapability{
@@ -341,7 +342,6 @@ public:
             sensors->sensors.add(sensor);
             return true;
         });
-        api->getLogger()->logDebug(GwLog::LOG,"adding sensor %s returns %d",sensor->prefix.c_str(),(int)rt);
     }
 };
 
@@ -378,6 +378,9 @@ void GwUserCode::startUserTasks(int baseId){
     }
 }
 void GwUserCode::startInitTasks(int baseId){
+    std::sort(initTasks.begin(),initTasks.end(),[](const GwUserTask &a, const GwUserTask &b){
+        return a.order < b.order;    
+    });
     LOG_DEBUG(GwLog::DEBUG,"starting %d user init tasks",initTasks.size());
     for (auto it=initTasks.begin();it != initTasks.end();it++){
         LOG_DEBUG(GwLog::LOG,"starting user init task %s with id %d",it->name.c_str(),baseId);
diff --git a/lib/usercode/GwUserCode.h b/lib/usercode/GwUserCode.h
index 94e745d..c055411 100644
--- a/lib/usercode/GwUserCode.h
+++ b/lib/usercode/GwUserCode.h
@@ -15,22 +15,25 @@ class GwApiInternal : public GwApi{
 };
 class GwUserTask{
     public:
+        static const int DEFAULT_STACKSIZE=2000;
         String name;
         TaskFunction_t task=NULL;
         GwUserTaskFunction usertask=NULL;
         bool isUserTask=false;
         GwApiInternal *api=NULL;
         int stackSize=2000;
-        GwUserTask(String name,TaskFunction_t task,int stackSize=2000){
+        int order=0;
+        GwUserTask(String name,TaskFunction_t task,int stackSize=DEFAULT_STACKSIZE){
             this->name=name;
             this->task=task;
             this->stackSize=stackSize;
         }
-        GwUserTask(String name, GwUserTaskFunction task,int stackSize=2000){
+        GwUserTask(String name, GwUserTaskFunction task,int stackSize=DEFAULT_STACKSIZE, int order=0){
             this->name=name;
             this->usertask=task;
             this->isUserTask=true;
             this->stackSize=stackSize;
+            this->order=order;
         }
 };