diff --git a/lib/api/GwApi.h b/lib/api/GwApi.h index 6b99390..9209895 100644 --- a/lib/api/GwApi.h +++ b/lib/api/GwApi.h @@ -7,6 +7,8 @@ #include "GwBoatData.h" #include "GwXDRMappings.h" #include +class GwApi; +typedef void (*GwUserTaskFunction)(GwApi *); //API to be used for additional tasks class GwApi{ public: @@ -158,6 +160,13 @@ class GwApi{ virtual bool addXdrMapping(const GwXDRMappingDef &)=0; virtual void addCapability(const String &name, const String &value)=0; + /** + * add a user task + * this allows you decide based on defines/config if a user task really should be added + * so this is the preferred solution over DECLARE_USERTASK + * The name should be similar to the function name of the user task (although not mandatory) + */ + virtual bool addUserTask(GwUserTaskFunction task,const String Name, int stackSize=2000)=0; /** * not thread safe methods diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index ea742b1..79064de 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -36,12 +36,13 @@ class SHT3XConfig{ tempSource=(tN2kTempSource)(config->getInt(GwConfigDefinitions::SHT3XTempSource)); } }; - +void runIicTask(GwApi *api); void initIicTask(GwApi *api){ GwLog *logger=api->getLogger(); #ifndef _GWIIC return; #endif + bool addTask=false; #ifdef GWSHT3X api->addCapability("SHT3X","true"); LOG_DEBUG(GwLog::LOG,"GWSHT3X configured, adding capability and xdr mappings"); @@ -70,7 +71,11 @@ void initIicTask(GwApi *api){ xdr.xdrName=sht3xConfig.tempTransducer; api->addXdrMapping(xdr); } + if (sht3xConfig.tempActive || sht3xConfig.humidActive) addTask=true; #endif + if (addTask){ + api->addUserTask(runIicTask,"iicTask",3000); + } } void runIicTask(GwApi *api){ GwLog *logger=api->getLogger(); diff --git a/lib/iictask/GwIicTask.h b/lib/iictask/GwIicTask.h index 728f7ab..e26eb28 100644 --- a/lib/iictask/GwIicTask.h +++ b/lib/iictask/GwIicTask.h @@ -1,8 +1,6 @@ #ifndef _GWIICTASK_H #define _GWIICTASK_H #include "GwApi.h" -void runIicTask(GwApi *api); void initIicTask(GwApi *api); -DECLARE_USERTASK_PARAM(runIicTask,3000); DECLARE_INITFUNCTION(initIicTask); #endif \ No newline at end of file diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp index 7eb13c7..5fcc21b 100644 --- a/lib/usercode/GwUserCode.cpp +++ b/lib/usercode/GwUserCode.cpp @@ -3,6 +3,9 @@ #define DECLARE_INITFUNCTION(task) GwInitTask __Init##task##__(task,#task); #define DECLARE_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,#value); #define DECLARE_STRING_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,value); +#define DECLARE_TASKIF(task,type) \ + DECLARE_TASKIF_IMPL(task,type) \ + GwIreg __register##type(#task,__FILE__,#type) #include "GwUserCode.h" #include "GwSynchronized.h" @@ -17,6 +20,14 @@ std::vector userTasks; std::vector initTasks; GwUserCode::Capabilities userCapabilities; + +template +bool taskExists(V &list, const String &name){ + for (auto it=list.begin();it!=list.end();it++){ + if (it->name == name) return true; + } + return false; +} class RegEntry{ public: String file; @@ -54,9 +65,6 @@ class GwIreg{ } }; -#define DECLARE_TASKIF(task,type) \ - DECLARE_TASKIF_IMPL(task,type) \ - GwIreg __register##type(#task,__FILE__,#type) class GwUserTaskDef{ @@ -290,6 +298,19 @@ public: if (! isInit) return; userCapabilities[name]=value; } + virtual bool addUserTask(GwUserTaskFunction task,const String tname, int stackSize=2000){ + if (! isInit){ + api->getLogger()->logDebug(GwLog::ERROR,"trying to add a user task %s outside init",tname.c_str()); + return false; + } + if (taskExists(userTasks,name)){ + api->getLogger()->logDebug(GwLog::ERROR,"trying to add a user task %s that already exists",tname.c_str()); + return false; + } + userTasks.push_back(GwUserTask(tname,task,stackSize)); + api->getLogger()->logDebug(GwLog::LOG,"adding user task %s",tname.c_str()); + return true; + } }; diff --git a/lib/usercode/GwUserCode.h b/lib/usercode/GwUserCode.h index 548d0d1..a218bc9 100644 --- a/lib/usercode/GwUserCode.h +++ b/lib/usercode/GwUserCode.h @@ -5,7 +5,6 @@ #include "GwApi.h" #include "GwJsonDocument.h" class GwLog; -typedef void (*GwUserTaskFunction)(GwApi *); class GwApiInternal : public GwApi{ public: diff --git a/src/main.cpp b/src/main.cpp index aaeb10e..05c4349 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -328,6 +328,9 @@ public: return xdrMappings.addFixedMapping(mapping); } virtual void addCapability(const String &name, const String &value){} + virtual bool addUserTask(GwUserTaskFunction task,const String Name, int stackSize=2000){ + return false; + } }; bool delayedRestart(){