diff --git a/lib/api/GwApi.h b/lib/api/GwApi.h index cb54cc3..af3b010 100644 --- a/lib/api/GwApi.h +++ b/lib/api/GwApi.h @@ -61,6 +61,7 @@ class GwApi{ protected: virtual bool iset(const String &file, const String &name, Ptr v) = 0; virtual Ptr iget(const String &name, int &result) = 0; + virtual bool iclaim(const String &name, const String &task)=0; public: template bool set(const T &v){ @@ -71,6 +72,10 @@ class GwApi{ res=-1; return T(); } + template + bool claim(const String &task){ + return false; + } }; class Status{ public: @@ -248,7 +253,12 @@ class GwApi{ }\ type *tp=(type*)ptr.get(); \ return type(*tp); \ - } + }\ + template<> \ + inline bool GwApi::TaskInterfaces::claim(const String &task) {\ + return iclaim(#type,task);\ + }\ + #ifndef DECLARE_TASKIF #define DECLARE_TASKIF(type) DECLARE_TASKIF_IMPL(type) #endif diff --git a/lib/buttontask/GwButtonTask.cpp b/lib/buttontask/GwButtonTask.cpp index 9ea4beb..72b84cf 100644 --- a/lib/buttontask/GwButtonTask.cpp +++ b/lib/buttontask/GwButtonTask.cpp @@ -110,4 +110,14 @@ void handleButtons(GwApi *api){ } vTaskDelete(NULL); #endif +} + +void initButtons(GwApi *api){ + #ifndef GWBUTTON_PIN + api->getLogger()->logDebug(GwLog::LOG,"no buttons defined, no button task"); + return; + #endif + const String taskname("buttonTask"); + api->addUserTask(handleButtons,taskname); + api->taskInterfaces()->claim(taskname); } \ No newline at end of file diff --git a/lib/buttontask/GwButtonTask.h b/lib/buttontask/GwButtonTask.h index 76a3301..853e6c6 100644 --- a/lib/buttontask/GwButtonTask.h +++ b/lib/buttontask/GwButtonTask.h @@ -2,6 +2,6 @@ #define _GWBUTTONTASK_H #include "GwApi.h" //task function -void handleButtons(GwApi *param); -DECLARE_USERTASK(handleButtons); +void initButtons(GwApi *param); +DECLARE_INITFUNCTION(initButtons); #endif \ No newline at end of file diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp index 189ebcd..0473910 100644 --- a/lib/usercode/GwUserCode.cpp +++ b/lib/usercode/GwUserCode.cpp @@ -124,6 +124,7 @@ class TaskInterfacesStorage{ } if (it->second.task != task){ LOG_DEBUG(GwLog::ERROR,"TaskInterfaces: invalid set %s wrong task, expected %s , got %s",name.c_str(),it->second.task.c_str(),task.c_str()); + return false; } auto vit=values.find(name); if (vit != values.end()){ @@ -150,15 +151,32 @@ class TaskInterfacesStorage{ class TaskInterfacesImpl : public GwApi::TaskInterfaces{ String task; TaskInterfacesStorage *storage; + GwLog *logger; + bool isInit=false; public: - TaskInterfacesImpl(const String &n,TaskInterfacesStorage *s): - task(n),storage(s){} + TaskInterfacesImpl(const String &n,TaskInterfacesStorage *s, GwLog *l,bool i): + task(n),storage(s),isInit(i),logger(l){} virtual bool iset(const String &file, const String &name, Ptr v){ return storage->set(file,name,task,v); } virtual Ptr iget(const String &name, int &result){ return storage->get(name,result); } + virtual bool iclaim(const String &name, const String &task){ + if (! isInit) return false; + auto it=registrations().find(name); + if (it == registrations().end()){ + LOG_DEBUG(GwLog::ERROR,"unable to claim interface %s for task %s, not registered",name.c_str(),task.c_str()); + return false; + } + if (!it->second.task.isEmpty()){ + LOG_DEBUG(GwLog::ERROR,"unable to claim interface %s for task %s, already claimed by %s",name.c_str(),task.c_str(),it->second.task.c_str()); + return false; + } + it->second.task=task; + LOG_DEBUG(GwLog::LOG,"claimed interface %s for task %s",name.c_str(),task.c_str()); + return true; + } }; @@ -187,7 +205,7 @@ public: this->mainLock=mainLock; this->name=name; localLock=xSemaphoreCreateMutex(); - interfaces=new TaskInterfacesImpl(name,s); + interfaces=new TaskInterfacesImpl(name,s,api->getLogger(),init); isInit=init; } virtual GwRequestQueue *getQueue()