From ab282765d42a05470dfc240f93a0ecd31876662e Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 1 Oct 2022 12:06:59 +0200 Subject: [PATCH] Let SimRadio start receive a packet if its PortNum is Simulator_App --- src/mesh/MeshService.cpp | 23 +++++++++++++++++++++++ src/mesh/MeshService.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index 825252402..99411f91d 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -123,6 +123,29 @@ void MeshService::reloadOwner() */ void MeshService::handleToRadio(MeshPacket &p) { + #ifdef ARCH_PORTDUINO + // Simulates device is receiving a packet via the LoRa chip + if (p.decoded.portnum == PortNum_SIMULATOR_APP) { + // Simulator packet (=Compressed packet) is encapsulated in a MeshPacket, so need to unwrap first + Compressed scratch; + Compressed *decoded = NULL; + if (p.which_payload_variant == MeshPacket_decoded_tag) { + memset(&scratch, 0, sizeof(scratch)); + p.decoded.payload.size = pb_decode_from_bytes(p.decoded.payload.bytes, p.decoded.payload.size, &Compressed_msg, &scratch); + if (p.decoded.payload.size) { + decoded = &scratch; + // Extract the original payload and replace + memcpy(&p.decoded.payload, &decoded->data, sizeof(decoded->data)); + // Switch the port from PortNum_SIMULATOR_APP back to the original PortNum + p.decoded.portnum = decoded->portnum; + } else + DEBUG_MSG("Error decoding protobuf for simulator message!\n"); + } + // Let SimRadio receive as if it did via its LoRa chip + SimRadio::instance->startReceive(&p); + return; + } + #endif if (p.from != 0) { // We don't let phones assign nodenums to their sent messages DEBUG_MSG("Warning: phone tried to pick a nodenum, we don't allow that.\n"); p.from = 0; diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index 45559d3a2..15c3799c8 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -10,6 +10,9 @@ #include "MeshTypes.h" #include "Observer.h" #include "PointerQueue.h" +#ifdef ARCH_PORTDUINO +#include "mesh/SimRadio.h" +#endif /** * Top level app for this service. keeps the mesh, the radio config and the queue of received packets.