make plugin reply handling simpler

This commit is contained in:
Kevin Hester 2020-12-13 12:57:37 +08:00
parent ad8bcba5ef
commit ee8f4de5ab
5 changed files with 29 additions and 15 deletions

View File

@ -11,8 +11,9 @@ For app cleanup:
* DONE update android code: https://developer.android.com/topic/libraries/view-binding/migration * DONE update android code: https://developer.android.com/topic/libraries/view-binding/migration
* only do wantReplies once per packet type, if we change network settings force it again * only do wantReplies once per packet type, if we change network settings force it again
* test GPIO watch * test GPIO watch
* DONE make hello world example service * writeup docs on gpio
* make python ping command * make python ping command
* DONE make hello world example service
* DONE have python tool check max packet size before sending to device * DONE have python tool check max packet size before sending to device
* DONE if request was sent reliably, send reply reliably * DONE if request was sent reliably, send reply reliably
* DONE require a recent python api to talk to these new device loads * DONE require a recent python api to talk to these new device loads
@ -20,8 +21,8 @@ For app cleanup:
* DONE fix handleIncomingPosition * DONE fix handleIncomingPosition
* DONE move want_replies handling into plugins * DONE move want_replies handling into plugins
* DONE on android for received positions handle either old or new positions / user messages * DONE on android for received positions handle either old or new positions / user messages
* on android side send old or new positions as needed / user messages * DONE on android side send old or new positions as needed / user messages
* test python side handle new position/user messages * DONE test python side handle new position/user messages
* DONE make a gpio example. --gpiowrb 4 1, --gpiord 0x444, --gpiowatch 0x3ff * DONE make a gpio example. --gpiowrb 4 1, --gpiord 0x444, --gpiowatch 0x3ff
* DONE fix position sending to use new plugin * DONE fix position sending to use new plugin
* DONE Add SinglePortNumPlugin - as the new most useful baseclass * DONE Add SinglePortNumPlugin - as the new most useful baseclass

View File

@ -5,6 +5,8 @@
std::vector<MeshPlugin *> *MeshPlugin::plugins; std::vector<MeshPlugin *> *MeshPlugin::plugins;
const MeshPacket *MeshPlugin::currentRequest;
MeshPlugin::MeshPlugin(const char *_name) : name(_name) MeshPlugin::MeshPlugin(const char *_name) : name(_name)
{ {
// Can't trust static initalizer order, so we check each time // Can't trust static initalizer order, so we check each time
@ -27,11 +29,13 @@ void MeshPlugin::callPlugins(const MeshPacket &mp)
// DEBUG_MSG("In call plugins\n"); // DEBUG_MSG("In call plugins\n");
for (auto i = plugins->begin(); i != plugins->end(); ++i) { for (auto i = plugins->begin(); i != plugins->end(); ++i) {
auto &pi = **i; auto &pi = **i;
pi.currentRequest = &mp;
if (pi.wantPortnum(mp.decoded.data.portnum)) { if (pi.wantPortnum(mp.decoded.data.portnum)) {
bool handled = pi.handleReceived(mp); bool handled = pi.handleReceived(mp);
// Possibly send replies (unless we are handling a locally generated message) // Possibly send replies
if (mp.decoded.want_response && mp.from != nodeDB.getNodeNum()) if (mp.decoded.want_response)
pi.sendResponse(mp); pi.sendResponse(mp);
DEBUG_MSG("Plugin %s handled=%d\n", pi.name, handled); DEBUG_MSG("Plugin %s handled=%d\n", pi.name, handled);
@ -41,6 +45,7 @@ void MeshPlugin::callPlugins(const MeshPacket &mp)
else { else {
DEBUG_MSG("Plugin %s not interested\n", pi.name); DEBUG_MSG("Plugin %s not interested\n", pi.name);
} }
pi.currentRequest = NULL;
} }
} }

View File

@ -31,6 +31,15 @@ class MeshPlugin
protected: protected:
const char *name; const char *name;
/**
* If this plugin is currently handling a request currentRequest will be preset
* to the packet with the request. This is mostly useful for reply handlers.
*
* Note: this can be static because we are guaranteed to be processing only one
* plugin at a time.
*/
static const MeshPacket *currentRequest;
/** /**
* 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

View File

@ -1,6 +1,6 @@
#include "configuration.h"
#include "ReplyPlugin.h" #include "ReplyPlugin.h"
#include "MeshService.h" #include "MeshService.h"
#include "configuration.h"
#include "main.h" #include "main.h"
#include <assert.h> #include <assert.h>
@ -8,8 +8,10 @@
// Create an a static instance of our plugin - this registers with the plugin system // Create an a static instance of our plugin - this registers with the plugin system
ReplyPlugin replyPlugin; ReplyPlugin replyPlugin;
bool ReplyPlugin::handleReceived(const MeshPacket &req) MeshPacket *ReplyPlugin::allocReply()
{ {
assert(currentRequest); // should always be !NULL
auto req = *currentRequest;
auto &p = req.decoded.data; auto &p = req.decoded.data;
// The incoming message is in p.payload // The incoming message is in p.payload
DEBUG_MSG("Received message from=0x%0x, id=%d, msg=%.*s\n", req.from, req.id, p.payload.size, p.payload.bytes); DEBUG_MSG("Received message from=0x%0x, id=%d, msg=%.*s\n", req.from, req.id, p.payload.size, p.payload.bytes);
@ -17,11 +19,9 @@ bool ReplyPlugin::handleReceived(const MeshPacket &req)
screen->print("Sending reply\n"); screen->print("Sending reply\n");
const char *replyStr = "Message Received"; const char *replyStr = "Message Received";
auto reply = allocDataPacket(); // Allocate a packet for sending auto reply = allocDataPacket(); // Allocate a packet for sending
reply->decoded.data.payload.size = strlen(replyStr); // You must specify how many bytes are in the reply reply->decoded.data.payload.size = strlen(replyStr); // You must specify how many bytes are in the reply
memcpy(reply->decoded.data.payload.bytes, replyStr, reply->decoded.data.payload.size); memcpy(reply->decoded.data.payload.bytes, replyStr, reply->decoded.data.payload.size);
setReplyTo(reply, req); // Set packet params so that this packet is marked as a reply to a previous request
service.sendToMesh(reply); // Queue the reply for sending
return true; // We handled it return reply;
} }

View File

@ -15,9 +15,8 @@ class ReplyPlugin : public SinglePortPlugin
protected: protected:
/** Called to handle a particular incoming message /** For reply plugin we do all of our processing in the (normally optional)
* want_replies handling
@return true if you've guaranteed you've handled this message and no other handlers should be considered for it
*/ */
virtual bool handleReceived(const MeshPacket &mp); virtual MeshPacket *allocReply();
}; };