Skip to content

SignalReplyModule & RangeTest mod added #6679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
SignalReplyModule & RangeTest mod added
  • Loading branch information
VilemR committed Apr 28, 2025
commit 00e0064a49443802280c816c3403743c6837f897
4 changes: 4 additions & 0 deletions src/modules/Modules.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "SignalReplyModule.h"
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_INPUTBROKER
#include "input/ExpressLRSFiveWay.h"
Expand Down Expand Up @@ -142,6 +143,9 @@ void setupModules()
// Note: if the rest of meshtastic doesn't need to explicitly use your module, you do not need to assign the instance
// to a global variable.


new SignalReplyModule();

#if !MESHTASTIC_EXCLUDE_REMOTEHARDWARE
new RemoteHardwareModule();
#endif
Expand Down
88 changes: 88 additions & 0 deletions src/modules/SignalReplyModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "SignalReplyModule.h"
#include "MeshService.h"
#include "configuration.h"
#include "main.h"

SignalReplyModule *signalReplyModule;

ProcessMessage SignalReplyModule::handleReceived(const meshtastic_MeshPacket &currentRequest)
{
auto &p = currentRequest.decoded;
char messageRequest[250];
for (size_t i = 0; i < p.payload.size; ++i)
{
messageRequest[i] = static_cast<char>(p.payload.bytes[i]);
}
messageRequest[p.payload.size] = '\0';

//Received text msg from=0x0, id=0xf50fd53d, msg=Ping
if ((strcasestr(messageRequest, const_cast<char *>("ping")) == 0 ||
strcasestr(messageRequest, const_cast<char *>("seq ")) == 0) &&
currentRequest.from != 0x0 && //fix 2025-05-08
currentRequest.from != nodeDB->getNodeNum())
{
int hopLimit = currentRequest.hop_limit;
int hopStart = currentRequest.hop_start;

char idSender[10];
char idReceipient[10];
snprintf(idSender, sizeof(idSender), "%d", currentRequest.from);
snprintf(idReceipient, sizeof(idReceipient), "%d", nodeDB->getNodeNum());

char messageReply[250];
meshtastic_NodeInfoLite *nodeSender = nodeDB->getMeshNode(currentRequest.from);
const char *username = nodeSender->has_user ? nodeSender->user.short_name : idSender;
meshtastic_NodeInfoLite *nodeReceiver = nodeDB->getMeshNode(nodeDB->getNodeNum());
const char *usernameja = nodeReceiver->has_user ? nodeReceiver->user.short_name : idReceipient;

LOG_ERROR("SignalReplyModule::handleReceived(): '%s' from %s.", messageRequest, username);

if (hopLimit != hopStart)
{
snprintf(messageReply, sizeof(messageReply), "%s: RSSI/SNR cannot be determined due to indirect connection through %d nodes!", username, (hopLimit - hopStart));
}
else
{
snprintf(messageReply, sizeof(messageReply), "Request '%s'->'%s' : RSSI %d dBm, SNR %.1f dB (@%s).", username, usernameja, currentRequest.rx_rssi, currentRequest.rx_snr, usernameja);
}

auto reply = allocDataPacket();
reply->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
reply->decoded.payload.size = strlen(messageReply);
reply->from = getFrom(&currentRequest);
reply->to = currentRequest.from;
reply->channel = currentRequest.channel;
reply->want_ack = (currentRequest.from != 0) ? currentRequest.want_ack : false;
if (currentRequest.priority == meshtastic_MeshPacket_Priority_UNSET)
{
reply->priority = meshtastic_MeshPacket_Priority_RELIABLE;
}
reply->id = generatePacketId();
memcpy(reply->decoded.payload.bytes, messageReply, reply->decoded.payload.size);
service->handleToRadio(*reply);
}
notifyObservers(&currentRequest);
return ProcessMessage::CONTINUE;
}

meshtastic_MeshPacket *SignalReplyModule::allocReply()
{
assert(currentRequest); // should always be !NULL
#ifdef DEBUG_PORT
auto req = *currentRequest;
auto &p = req.decoded;
// The incoming message is in p.payload
LOG_INFO("Received message from=0x%0x, id=%d, msg=%.*s", req.from, req.id, p.payload.size, p.payload.bytes);
#endif
screen->print("Send reply\n");
const char *replyStr = "Message Received";
auto reply = allocDataPacket(); // Allocate a packet for sending
reply->decoded.payload.size = strlen(replyStr); // You must specify how many bytes are in the reply
memcpy(reply->decoded.payload.bytes, replyStr, reply->decoded.payload.size);
return reply;
}

bool SignalReplyModule::wantPacket(const meshtastic_MeshPacket *p)
{
return MeshService::isTextPayload(p);
}
29 changes: 29 additions & 0 deletions src/modules/SignalReplyModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include "SinglePortModule.h"

/**
* A simple example module that just replies with "Message received" to any message it receives.
*/
class SignalReplyModule : public SinglePortModule, public Observable<const meshtastic_MeshPacket *>
{
public:
/** Constructor
* name is for debugging output
*/
SignalReplyModule() : SinglePortModule("XXXXMod", meshtastic_PortNum_TEXT_MESSAGE_APP) {}

//virtual ~SignalReplyModule() {}

protected:
/** For reply module we do all of our processing in the (normally optional)
* want_replies handling
*/

virtual meshtastic_MeshPacket *allocReply() override;
virtual bool wantPacket(const meshtastic_MeshPacket *p) override;
virtual ProcessMessage handleReceived(const meshtastic_MeshPacket &mp) override;


};

extern SignalReplyModule *signalReplyModule;
Loading