fix static initializer bug with mesh plugins

This commit is contained in:
Kevin Hester 2020-11-28 13:25:03 +08:00
parent 0b0d293a66
commit 5138aff4b2
3 changed files with 25 additions and 9 deletions

2
proto

@ -1 +1 @@
Subproject commit 95ef921604cdab46e32adc245a8d72c7bdbbe319 Subproject commit 7c1016b8a01d4d019c4239fe46624dee7bde22c7

View File

@ -1,10 +1,18 @@
#include "MeshPlugin.h" #include "MeshPlugin.h"
#include <assert.h> #include <assert.h>
std::vector<MeshPlugin *> MeshPlugin::plugins; std::vector<MeshPlugin *> *MeshPlugin::plugins;
MeshPlugin::MeshPlugin(const char *_name) : name(_name) { MeshPlugin::MeshPlugin(const char *_name) : name(_name)
plugins.push_back(this); {
// Can't trust static initalizer order, so we check each time
if(!plugins)
plugins = new std::vector<MeshPlugin *>();
plugins->push_back(this);
}
void MeshPlugin::setup() {
} }
MeshPlugin::~MeshPlugin() MeshPlugin::~MeshPlugin()
@ -14,9 +22,17 @@ MeshPlugin::~MeshPlugin()
void MeshPlugin::callPlugins(const MeshPacket &mp) void MeshPlugin::callPlugins(const MeshPacket &mp)
{ {
for (auto i = plugins.begin(); i != plugins.end(); ++i) { DEBUG_MSG("In call plugins\n");
if ((*i)->wantPortnum(mp.decoded.data.portnum)) for (auto i = plugins->begin(); i != plugins->end(); ++i) {
if ((*i)->handleReceived(mp)) 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; break;
}
else {
DEBUG_MSG("Plugin %s not interested\n", pi.name);
}
} }
} }

View File

@ -16,7 +16,7 @@ class MeshPlugin
{ {
const char *name; const char *name;
static std::vector<MeshPlugin *> plugins; static std::vector<MeshPlugin *> *plugins;
public: public:
/** Constructor /** Constructor
@ -30,7 +30,7 @@ class MeshPlugin
* Initialize your plugin. This setup function is called once after all hardware and mesh protocol layers have * Initialize your plugin. This setup function is called once after all hardware and mesh protocol layers have
* been initialized * been initialized
*/ */
virtual void setup() {} virtual void setup();
/** /**
* @return true if you want to receive the specified portnum * @return true if you want to receive the specified portnum