directly use SemaphoreHandle_t as pointer

This commit is contained in:
andreas 2024-11-24 16:16:35 +01:00
parent 8cb012eac0
commit d33592cfdf
5 changed files with 29 additions and 28 deletions

View File

@ -156,11 +156,11 @@ class ExampleWebData{
vSemaphoreDelete(lock); vSemaphoreDelete(lock);
} }
void set(int v){ void set(int v){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
data=v; data=v;
} }
int get(){ int get(){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
return data; return data;
} }
}; };

View File

@ -246,7 +246,7 @@ void GwTcpClient::resolveHost(String host)
{ {
LOG_DEBUG(GwLog::LOG,"start resolving %s",host.c_str()); LOG_DEBUG(GwLog::LOG,"start resolving %s",host.c_str());
{ {
GWSYNCHRONIZED(&locker); GWSYNCHRONIZED(locker);
resolvedAddress.resolved = false; resolvedAddress.resolved = false;
} }
state = C_RESOLVING; state = C_RESOLVING;
@ -283,12 +283,12 @@ void GwTcpClient::resolveHost(String host)
void GwTcpClient::setResolved(IPAddress addr, bool valid){ void GwTcpClient::setResolved(IPAddress addr, bool valid){
LOG_DEBUG(GwLog::LOG,"setResolved %s, valid=%s", LOG_DEBUG(GwLog::LOG,"setResolved %s, valid=%s",
addr.toString().c_str(),(valid?"true":"false")); addr.toString().c_str(),(valid?"true":"false"));
GWSYNCHRONIZED(&locker); GWSYNCHRONIZED(locker);
resolvedAddress.address=addr; resolvedAddress.address=addr;
resolvedAddress.resolved=valid; resolvedAddress.resolved=valid;
state=C_RESOLVED; state=C_RESOLVED;
} }
GwTcpClient::ResolvedAddress GwTcpClient::getResolved(){ GwTcpClient::ResolvedAddress GwTcpClient::getResolved(){
GWSYNCHRONIZED(&locker); GWSYNCHRONIZED(locker);
return resolvedAddress; return resolvedAddress;
} }

View File

@ -75,7 +75,7 @@ class TaskInterfacesStorage{
lock=xSemaphoreCreateMutex(); lock=xSemaphoreCreateMutex();
} }
bool set(const String &name, GwApi::TaskInterfaces::Ptr v){ bool set(const String &name, GwApi::TaskInterfaces::Ptr v){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
auto vit=values.find(name); auto vit=values.find(name);
if (vit != values.end()){ if (vit != values.end()){
vit->second.updates++; vit->second.updates++;
@ -90,7 +90,7 @@ class TaskInterfacesStorage{
return true; return true;
} }
GwApi::TaskInterfaces::Ptr get(const String &name, int &result){ GwApi::TaskInterfaces::Ptr get(const String &name, int &result){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
auto it = values.find(name); auto it = values.find(name);
if (it == values.end()) if (it == values.end())
{ {
@ -102,7 +102,7 @@ class TaskInterfacesStorage{
} }
bool update(const String &name, std::function<GwApi::TaskInterfaces::Ptr(GwApi::TaskInterfaces::Ptr)>f){ bool update(const String &name, std::function<GwApi::TaskInterfaces::Ptr(GwApi::TaskInterfaces::Ptr)>f){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
auto vit=values.find(name); auto vit=values.find(name);
bool rt=false; bool rt=false;
int mode=0; int mode=0;
@ -160,7 +160,7 @@ class TaskApi : public GwApiInternal
{ {
GwApiInternal *api=nullptr; GwApiInternal *api=nullptr;
int sourceId; int sourceId;
SemaphoreHandle_t *mainLock; SemaphoreHandle_t mainLock;
SemaphoreHandle_t localLock; SemaphoreHandle_t localLock;
std::map<int,GwCounter<String>> counter; std::map<int,GwCounter<String>> counter;
std::map<String,GwApi::HandlerFunction> webHandlers; std::map<String,GwApi::HandlerFunction> webHandlers;
@ -172,7 +172,7 @@ class TaskApi : public GwApiInternal
public: public:
TaskApi(GwApiInternal *api, TaskApi(GwApiInternal *api,
int sourceId, int sourceId,
SemaphoreHandle_t *mainLock, SemaphoreHandle_t mainLock,
const String &name, const String &name,
TaskInterfacesStorage *s, TaskInterfacesStorage *s,
bool init=false) bool init=false)
@ -236,14 +236,14 @@ public:
vSemaphoreDelete(localLock); vSemaphoreDelete(localLock);
}; };
virtual void fillStatus(GwJsonDocument &status){ virtual void fillStatus(GwJsonDocument &status){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
if (! counterUsed) return; if (! counterUsed) return;
for (auto it=counter.begin();it != counter.end();it++){ for (auto it=counter.begin();it != counter.end();it++){
it->second.toJson(status); it->second.toJson(status);
} }
}; };
virtual int getJsonSize(){ virtual int getJsonSize(){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
if (! counterUsed) return 0; if (! counterUsed) return 0;
int rt=0; int rt=0;
for (auto it=counter.begin();it != counter.end();it++){ for (auto it=counter.begin();it != counter.end();it++){
@ -252,7 +252,7 @@ public:
return rt; return rt;
}; };
virtual void increment(int idx,const String &name,bool failed=false){ virtual void increment(int idx,const String &name,bool failed=false){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
counterUsed=true; counterUsed=true;
auto it=counter.find(idx); auto it=counter.find(idx);
if (it == counter.end()) return; if (it == counter.end()) return;
@ -260,18 +260,18 @@ public:
else (it->second.add(name)); else (it->second.add(name));
}; };
virtual void reset(int idx){ virtual void reset(int idx){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
counterUsed=true; counterUsed=true;
auto it=counter.find(idx); auto it=counter.find(idx);
if (it == counter.end()) return; if (it == counter.end()) return;
it->second.reset(); it->second.reset();
}; };
virtual void remove(int idx){ virtual void remove(int idx){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
counter.erase(idx); counter.erase(idx);
} }
virtual int addCounter(const String &name){ virtual int addCounter(const String &name){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
counterUsed=true; counterUsed=true;
counterIdx++; counterIdx++;
//avoid the need for an empty counter constructor //avoid the need for an empty counter constructor
@ -289,7 +289,7 @@ public:
return api->addXdrMapping(def); return api->addXdrMapping(def);
} }
virtual void registerRequestHandler(const String &url,HandlerFunction handler){ virtual void registerRequestHandler(const String &url,HandlerFunction handler){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
webHandlers[url]=handler; webHandlers[url]=handler;
} }
virtual void addCapability(const String &name, const String &value){ virtual void addCapability(const String &name, const String &value){
@ -316,7 +316,7 @@ public:
{ {
GwApi::HandlerFunction handler; GwApi::HandlerFunction handler;
{ {
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(localLock);
auto it = webHandlers.find(url); auto it = webHandlers.find(url);
if (it == webHandlers.end()) if (it == webHandlers.end())
{ {
@ -345,10 +345,9 @@ public:
} }
}; };
GwUserCode::GwUserCode(GwApiInternal *api,SemaphoreHandle_t *mainLock){ GwUserCode::GwUserCode(GwApiInternal *api){
this->logger=api->getLogger(); this->logger=api->getLogger();
this->api=api; this->api=api;
this->mainLock=mainLock;
this->taskData=new TaskInterfacesStorage(this->logger); this->taskData=new TaskInterfacesStorage(this->logger);
} }
GwUserCode::~GwUserCode(){ GwUserCode::~GwUserCode(){

View File

@ -41,13 +41,14 @@ class TaskInterfacesStorage;
class GwUserCode{ class GwUserCode{
GwLog *logger; GwLog *logger;
GwApiInternal *api; GwApiInternal *api;
SemaphoreHandle_t *mainLock; SemaphoreHandle_t mainLock=nullptr;
TaskInterfacesStorage *taskData; TaskInterfacesStorage *taskData;
void startAddOnTask(GwApiInternal *api,GwUserTask *task,int sourceId,String name); void startAddOnTask(GwApiInternal *api,GwUserTask *task,int sourceId,String name);
public: public:
~GwUserCode(); ~GwUserCode();
typedef std::map<String,String> Capabilities; typedef std::map<String,String> Capabilities;
GwUserCode(GwApiInternal *api, SemaphoreHandle_t *mainLock); GwUserCode(GwApiInternal *api);
void begin(SemaphoreHandle_t mainLock){this->mainLock=mainLock;}
void startUserTasks(int baseId); void startUserTasks(int baseId);
void startInitTasks(int baseId); void startInitTasks(int baseId);
void startAddonTask(String name,TaskFunction_t task, int id); void startAddonTask(String name,TaskFunction_t task, int id);

View File

@ -235,17 +235,17 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg, int sourceId,bool conv
class CalibrationValues { class CalibrationValues {
using Map=std::map<String,double>; using Map=std::map<String,double>;
Map values; Map values;
SemaphoreHandle_t lock; SemaphoreHandle_t lock=nullptr;
public: public:
CalibrationValues(){ CalibrationValues(){
lock=xSemaphoreCreateMutex(); lock=xSemaphoreCreateMutex();
} }
void set(const String &name,double value){ void set(const String &name,double value){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
values[name]=value; values[name]=value;
} }
bool get(const String &name, double &value){ bool get(const String &name, double &value){
GWSYNCHRONIZED(&lock); GWSYNCHRONIZED(lock);
auto it=values.find(name); auto it=values.find(name);
if (it==values.end()) return false; if (it==values.end()) return false;
value=it->second; value=it->second;
@ -373,7 +373,7 @@ bool delayedRestart(){
},"reset",2000,&logger,0,NULL) == pdPASS; },"reset",2000,&logger,0,NULL) == pdPASS;
} }
ApiImpl *apiImpl=new ApiImpl(MIN_USER_TASK); ApiImpl *apiImpl=new ApiImpl(MIN_USER_TASK);
GwUserCode userCodeHandler(apiImpl,&mainLock); GwUserCode userCodeHandler(apiImpl);
#define JSON_OK "{\"status\":\"OK\"}" #define JSON_OK "{\"status\":\"OK\"}"
#define JSON_INVALID_PASS F("{\"status\":\"invalid password\"}") #define JSON_INVALID_PASS F("{\"status\":\"invalid password\"}")
@ -788,6 +788,7 @@ void setup() {
logger.setWriter(new DefaultLogWriter()); logger.setWriter(new DefaultLogWriter());
#endif #endif
boatData.begin(); boatData.begin();
userCodeHandler.begin(mainLock);
userCodeHandler.startInitTasks(MIN_USER_TASK); userCodeHandler.startInitTasks(MIN_USER_TASK);
channels.preinit(); channels.preinit();
config.stopChanges(); config.stopChanges();
@ -937,7 +938,7 @@ void setup() {
logger.logDebug(GwLog::LOG,"starting addon tasks"); logger.logDebug(GwLog::LOG,"starting addon tasks");
logger.flush(); logger.flush();
{ {
GWSYNCHRONIZED(&mainLock); GWSYNCHRONIZED(mainLock);
userCodeHandler.startUserTasks(MIN_USER_TASK); userCodeHandler.startUserTasks(MIN_USER_TASK);
} }
timers.addAction(HEAP_REPORT_TIME,[](){ timers.addAction(HEAP_REPORT_TIME,[](){
@ -967,7 +968,7 @@ void handleSendAndRead(bool handleRead){
void loopRun() { void loopRun() {
//logger.logDebug(GwLog::DEBUG,"main loop start"); //logger.logDebug(GwLog::DEBUG,"main loop start");
monitor.reset(); monitor.reset();
GWSYNCHRONIZED(&mainLock); GWSYNCHRONIZED(mainLock);
logger.flush(); logger.flush();
monitor.setTime(1); monitor.setTime(1);
gwWifi.loop(); gwWifi.loop();