diff --git a/docs/software/TODO.md b/docs/software/TODO.md index 80c818bf6..6088c535b 100644 --- a/docs/software/TODO.md +++ b/docs/software/TODO.md @@ -13,7 +13,7 @@ For app cleanup: * on python side print error messages if old position/user messages seen * on python side handle new position/user messages * DONE fix position sending to use new plugin -* Add SinglePortNumPlugin - as the new most useful baseclass +* DONE Add SinglePortNumPlugin - as the new most useful baseclass * DONE move positions into regular data packets (use new app framework) * DONE move user info into regular data packets (use new app framework) * test that positions, text messages and user info still work diff --git a/src/mesh/ProtobufPlugin.h b/src/mesh/ProtobufPlugin.h index 6d385553b..f6bac2345 100644 --- a/src/mesh/ProtobufPlugin.h +++ b/src/mesh/ProtobufPlugin.h @@ -1,5 +1,5 @@ #pragma once -#include "MeshPlugin.h" +#include "SinglePortPlugin.h" #include "Router.h" /** @@ -9,25 +9,20 @@ * If you are using protobufs to encode your packets (recommended) you can use this as a baseclass for your plugin * and avoid a bunch of boilerplate code. */ -template class ProtobufPlugin : private MeshPlugin +template class ProtobufPlugin : private SinglePortPlugin { const pb_msgdesc_t *fields; - PortNum ourPortNum; public: /** Constructor * name is for debugging output */ ProtobufPlugin(const char *_name, PortNum _ourPortNum, const pb_msgdesc_t *_fields) - : MeshPlugin(_name), fields(_fields), ourPortNum(_ourPortNum) + : SinglePortPlugin(_name, _ourPortNum), fields(_fields) { } protected: - /** - * @return true if you want to receive the specified portnum - */ - virtual bool wantPortnum(PortNum p) { return p == ourPortNum; } /** * Handle a received message, the data field in the message is already decoded and is provided diff --git a/src/mesh/SinglePortPlugin.h b/src/mesh/SinglePortPlugin.h new file mode 100644 index 000000000..9d7e41030 --- /dev/null +++ b/src/mesh/SinglePortPlugin.h @@ -0,0 +1,24 @@ +#pragma once +#include "MeshPlugin.h" + +/** + * Most plugins are only interested in sending/receving one particular portnum. This baseclass simplifies that common + * case. + */ +class SinglePortPlugin : public MeshPlugin +{ + protected: + PortNum ourPortNum; + + public: + /** Constructor + * name is for debugging output + */ + SinglePortPlugin(const char *_name, PortNum _ourPortNum) : MeshPlugin(_name), ourPortNum(_ourPortNum) {} + + protected: + /** + * @return true if you want to receive the specified portnum + */ + virtual bool wantPortnum(PortNum p) { return p == ourPortNum; } +}; diff --git a/src/plugins/TextMessagePlugin.h b/src/plugins/TextMessagePlugin.h index c213570f7..1a6a45076 100644 --- a/src/plugins/TextMessagePlugin.h +++ b/src/plugins/TextMessagePlugin.h @@ -1,23 +1,19 @@ #pragma once -#include "MeshPlugin.h" +#include "SinglePortPlugin.h" #include "Observer.h" /** * Text message handling for meshtastic - draws on the OLED display the most recent received message */ -class TextMessagePlugin : public MeshPlugin, public Observable +class TextMessagePlugin : public SinglePortPlugin, public Observable { public: /** Constructor * name is for debugging output */ - TextMessagePlugin() : MeshPlugin("text") {} + TextMessagePlugin() : SinglePortPlugin("text", PortNum_TEXT_MESSAGE_APP) {} protected: - /** - * @return true if you want to receive the specified portnum - */ - virtual bool wantPortnum(PortNum p) { return p == PortNum_TEXT_MESSAGE_APP; } /** Called to handle a particular incoming message