mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-23 11:10:52 +00:00
begin plugin-api tutorial
This commit is contained in:
27
src/plugins/ReplyPlugin.cpp
Normal file
27
src/plugins/ReplyPlugin.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "configuration.h"
|
||||
#include "ReplyPlugin.h"
|
||||
#include "MeshService.h"
|
||||
#include "main.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
// Create an a static instance of our plugin - this registers with the plugin system
|
||||
ReplyPlugin replyPlugin;
|
||||
|
||||
bool ReplyPlugin::handleReceived(const MeshPacket &req)
|
||||
{
|
||||
auto &p = req.decoded.data;
|
||||
// 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);
|
||||
|
||||
screen->print("Sending reply\n");
|
||||
|
||||
const char *replyStr = "Message Received";
|
||||
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
|
||||
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
|
||||
}
|
||||
23
src/plugins/ReplyPlugin.h
Normal file
23
src/plugins/ReplyPlugin.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "SinglePortPlugin.h"
|
||||
|
||||
|
||||
/**
|
||||
* A simple example plugin that just replies with "Message received" to any message it receives.
|
||||
*/
|
||||
class ReplyPlugin : public SinglePortPlugin
|
||||
{
|
||||
public:
|
||||
/** Constructor
|
||||
* name is for debugging output
|
||||
*/
|
||||
ReplyPlugin() : SinglePortPlugin("reply", PortNum_REPLY_APP) {}
|
||||
|
||||
protected:
|
||||
|
||||
/** Called to handle a particular incoming message
|
||||
|
||||
@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);
|
||||
};
|
||||
Reference in New Issue
Block a user