From 5138aff4b256f924d0785d42bc4f56acdc6702cf Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sat, 28 Nov 2020 13:25:03 +0800 Subject: [PATCH] fix static initializer bug with mesh plugins --- proto | 2 +- src/mesh/MeshPlugin.cpp | 28 ++++++++++++++++++++++------ src/mesh/MeshPlugin.h | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/proto b/proto index 95ef92160..7c1016b8a 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 95ef921604cdab46e32adc245a8d72c7bdbbe319 +Subproject commit 7c1016b8a01d4d019c4239fe46624dee7bde22c7 diff --git a/src/mesh/MeshPlugin.cpp b/src/mesh/MeshPlugin.cpp index 8e99f8d79..853124981 100644 --- a/src/mesh/MeshPlugin.cpp +++ b/src/mesh/MeshPlugin.cpp @@ -1,10 +1,18 @@ #include "MeshPlugin.h" #include -std::vector MeshPlugin::plugins; +std::vector *MeshPlugin::plugins; -MeshPlugin::MeshPlugin(const char *_name) : name(_name) { - plugins.push_back(this); +MeshPlugin::MeshPlugin(const char *_name) : name(_name) +{ + // Can't trust static initalizer order, so we check each time + if(!plugins) + plugins = new std::vector(); + + plugins->push_back(this); +} + +void MeshPlugin::setup() { } MeshPlugin::~MeshPlugin() @@ -14,9 +22,17 @@ MeshPlugin::~MeshPlugin() void MeshPlugin::callPlugins(const MeshPacket &mp) { - for (auto i = plugins.begin(); i != plugins.end(); ++i) { - if ((*i)->wantPortnum(mp.decoded.data.portnum)) - if ((*i)->handleReceived(mp)) + DEBUG_MSG("In call plugins\n"); + for (auto i = plugins->begin(); i != plugins->end(); ++i) { + auto &pi = **i; + if (pi.wantPortnum(mp.decoded.data.portnum)) { + bool handled = pi.handleReceived(mp); + DEBUG_MSG("Plugin %s handled=%d\n", pi.name, handled); + if (handled) break; + } + else { + DEBUG_MSG("Plugin %s not interested\n", pi.name); + } } } \ No newline at end of file diff --git a/src/mesh/MeshPlugin.h b/src/mesh/MeshPlugin.h index 812e80c17..19189fb52 100644 --- a/src/mesh/MeshPlugin.h +++ b/src/mesh/MeshPlugin.h @@ -16,7 +16,7 @@ class MeshPlugin { const char *name; - static std::vector plugins; + static std::vector *plugins; public: /** Constructor @@ -30,7 +30,7 @@ class MeshPlugin * Initialize your plugin. This setup function is called once after all hardware and mesh protocol layers have * been initialized */ - virtual void setup() {} + virtual void setup(); /** * @return true if you want to receive the specified portnum