Cleanup (Comments & Formatting) of S&F and PrositionPlugin.cpp

This commit is contained in:
Jm Casler 2021-11-28 19:41:34 -08:00
parent 937f67c4ec
commit d5506bb33c
3 changed files with 50 additions and 71 deletions

View File

@ -1,12 +1,11 @@
#include "configuration.h"
#include "PositionPlugin.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "RTC.h"
#include "Router.h"
#include "configuration.h"
#include "gps/GeoCoord.h"
PositionPlugin *positionPlugin;
PositionPlugin::PositionPlugin()
@ -32,23 +31,11 @@ bool PositionPlugin::handleReceivedProtobuf(const MeshPacket &mp, Position *pptr
}
// Log packet size and list of fields
DEBUG_MSG("POSITION node=%08x l=%d %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
getFrom(&mp),
mp.decoded.payload.size,
p.latitude_i ? "LAT ":"",
p.longitude_i ? "LON ":"",
p.altitude ? "MSL ":"",
p.altitude_hae ? "HAE ":"",
p.alt_geoid_sep ? "GEO ":"",
p.PDOP ? "PDOP ":"",
p.HDOP ? "HDOP ":"",
p.VDOP ? "VDOP ":"",
p.sats_in_view ? "SIV ":"",
p.fix_quality ? "FXQ ":"",
p.fix_type ? "FXT ":"",
p.pos_timestamp ? "PTS ":"",
p.time ? "TIME ":"",
p.battery_level ? "BAT ":"");
DEBUG_MSG("POSITION node=%08x l=%d %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", getFrom(&mp), mp.decoded.payload.size,
p.latitude_i ? "LAT " : "", p.longitude_i ? "LON " : "", p.altitude ? "MSL " : "", p.altitude_hae ? "HAE " : "",
p.alt_geoid_sep ? "GEO " : "", p.PDOP ? "PDOP " : "", p.HDOP ? "HDOP " : "", p.VDOP ? "VDOP " : "",
p.sats_in_view ? "SIV " : "", p.fix_quality ? "FXQ " : "", p.fix_type ? "FXT " : "", p.pos_timestamp ? "PTS " : "",
p.time ? "TIME " : "", p.battery_level ? "BAT " : "");
if (p.time) {
struct timeval tv;
@ -75,7 +62,7 @@ MeshPacket *PositionPlugin::allocReply()
uint32_t pos_flags = radioConfig.preferences.position_flags;
// Populate a Position struct with ONLY the requested fields
Position p = Position_init_default; // Start with an empty structure
Position p = Position_init_default; // Start with an empty structure
// lat/lon are unconditionally included - IF AVAILABLE!
p.latitude_i = node->position.latitude_i;
@ -139,7 +126,7 @@ void PositionPlugin::sendOurPosition(NodeNum dest, bool wantReplies)
int32_t PositionPlugin::runOnce()
{
NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum());
// radioConfig.preferences.position_broadcast_smart = true;
// We limit our GPS broadcasts to a max rate
@ -155,35 +142,34 @@ int32_t PositionPlugin::runOnce()
bool requestReplies = currentGeneration != radioGeneration;
currentGeneration = radioGeneration;
DEBUG_MSG("Sending pos@%x:6 to mesh (wantReplies=%d)\n",
node->position.pos_timestamp, requestReplies);
DEBUG_MSG("Sending pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
sendOurPosition(NODENUM_BROADCAST, requestReplies);
} else if (radioConfig.preferences.position_broadcast_smart == true) {
//NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
// NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
if (node->has_position && (node->position.latitude_i != 0 || node->position.longitude_i != 0) ) {
if (node->has_position && (node->position.latitude_i != 0 || node->position.longitude_i != 0)) {
float distance = GeoCoord::latLongToMeter(lastGpsLatitude * 1e-7, lastGpsLongitude * 1e-7,
node->position.latitude_i * 1e-7, node->position.longitude_i * 1e-7);
node->position.latitude_i * 1e-7, node->position.longitude_i * 1e-7);
// Please don't change this value. This accomodates for possible poor positioning
// in the event the GPS has a poor satelite lock.
const uint8_t distanceTravel = 150;
/* Please don't change these values. This accomodates for possible poor positioning
in the event the GPS has a poor satelite lock.
*/
const uint8_t distanceTravel = 150;
/* Minimum time between position updates.
Note: At an average walking speed of 3.5mph, it takes 90 seconds to travel 150 meters.
*/
const uint8_t timeTravel = 60;
const uint8_t timeTravel = 60;
// If the distance traveled since the last update is greater than 100 meters
// and it's been at least 60 seconds since the last update
if ((abs(distance) >= distanceTravel) && (lastGpsSend == 0 || now - timeTravel >= getPref_position_broadcast_secs() * 1000)) {
if ((abs(distance) >= distanceTravel) &&
(lastGpsSend == 0 || now - timeTravel >= getPref_position_broadcast_secs() * 1000)) {
bool requestReplies = currentGeneration != radioGeneration;
currentGeneration = radioGeneration;
DEBUG_MSG("Sending smart pos@%x:6 to mesh (wantReplies=%d)\n",
node->position.pos_timestamp, requestReplies);
DEBUG_MSG("Sending smart pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
sendOurPosition(NODENUM_BROADCAST, requestReplies);
}
}
}

View File

@ -8,9 +8,8 @@
#include "mesh/generated/storeforward.pb.h"
#include "plugins/PluginDev.h"
#include <Arduino.h>
#include <map>
#include <iterator>
#include <map>
StoreForwardPlugin *storeForwardPlugin;
@ -26,22 +25,22 @@ int32_t StoreForwardPlugin::runOnce()
if (this->busy) {
// Send out the message queue.
//DEBUG_MSG("--- --- --- In busy loop 1 %d\n", this->packetHistoryTXQueue_index);
// DEBUG_MSG("--- --- --- In busy loop 1 %d\n", this->packetHistoryTXQueue_index);
storeForwardPlugin->sendPayload(this->busyTo, this->packetHistoryTXQueue_index);
if (this->packetHistoryTXQueue_index == packetHistoryTXQueue_size) {
strcpy(this->routerMessage, "** S&F - Done");
storeForwardPlugin->sendMessage(this->busyTo, this->routerMessage);
//DEBUG_MSG("--- --- --- In busy loop - Done \n");
// DEBUG_MSG("--- --- --- In busy loop - Done \n");
this->packetHistoryTXQueue_index = 0;
this->busy = false;
} else {
this->packetHistoryTXQueue_index++;
}
}
// TODO: Dynamicly adjust the time this returns in the loop based on the size of the packets being actually transmitted.
// TODO: Dynamicly adjust the time this returns in the loop based on the size of the packets being actually
// transmitted.
return (this->packetTimeMax);
} else {
DEBUG_MSG("Store & Forward Plugin - Disabled (is_router = false)\n");
@ -84,7 +83,8 @@ void StoreForwardPlugin::populatePSRAM()
: (((ESP.getFreePsram() / 3) * 2) / sizeof(PacketHistoryStruct)));
this->packetHistory = static_cast<PacketHistoryStruct *>(ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct)));
this->packetHistoryTXQueue = static_cast<PacketHistoryStruct *>(ps_calloc(store_forward_plugin_replay_max_records, sizeof(PacketHistoryStruct)));
this->packetHistoryTXQueue =
static_cast<PacketHistoryStruct *>(ps_calloc(store_forward_plugin_replay_max_records, sizeof(PacketHistoryStruct)));
DEBUG_MSG("After PSRAM initilization:\n");
DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize());
@ -108,7 +108,6 @@ void StoreForwardPlugin::historySend(uint32_t msAgo, uint32_t to)
{
uint32_t packetsSent = 0;
uint32_t queueSize = storeForwardPlugin->historyQueueCreate(msAgo, to);
@ -125,9 +124,10 @@ void StoreForwardPlugin::historySend(uint32_t msAgo, uint32_t to)
}
}
uint32_t StoreForwardPlugin::historyQueueCreate(uint32_t msAgo, uint32_t to) {
//uint32_t packetHistoryTXQueueIndex = 0;
uint32_t StoreForwardPlugin::historyQueueCreate(uint32_t msAgo, uint32_t to)
{
// uint32_t packetHistoryTXQueueIndex = 0;
this->packetHistoryTXQueue_size = 0;
@ -147,24 +147,19 @@ uint32_t StoreForwardPlugin::historyQueueCreate(uint32_t msAgo, uint32_t to) {
TODO: The condition (this->packetHistory[i].to & 0xffffffff) == to) is not tested since
I don't have an easy way to target a specific user. Will need to do this soon.
*/
if ((this->packetHistory[i].to & 0xffffffff) == 0xffffffff
||
((this->packetHistory[i].to & 0xffffffff) == to)
) {
if ((this->packetHistory[i].to & 0xffffffff) == 0xffffffff || ((this->packetHistory[i].to & 0xffffffff) == to)) {
this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].time = this->packetHistory[i].time;
this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].to = this->packetHistory[i].to;
this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].from = this->packetHistory[i].from;
this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].payload_size = this->packetHistory[i].payload_size;
memcpy(this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].payload, this->packetHistory[i].payload, Constants_DATA_PAYLOAD_LEN);
memcpy(this->packetHistoryTXQueue[this->packetHistoryTXQueue_size].payload, this->packetHistory[i].payload,
Constants_DATA_PAYLOAD_LEN);
this->packetHistoryTXQueue_size++;
DEBUG_MSG("PacketHistoryStruct time=%d\n", this->packetHistory[i].time);
DEBUG_MSG("PacketHistoryStruct msg=%.*s\n", this->packetHistory[i].payload);
//DEBUG_MSG("PacketHistoryStruct msg=%.*s\n", this->packetHistoryTXQueue[packetHistoryTXQueueIndex].payload);
// DEBUG_MSG("PacketHistoryStruct msg=%.*s\n", this->packetHistoryTXQueue[packetHistoryTXQueueIndex].payload);
}
}
}
return this->packetHistoryTXQueue_size;
@ -222,10 +217,9 @@ void StoreForwardPlugin::sendMessage(NodeNum dest, char *str)
p->decoded.payload.size = strlen(str); // You must specify how many bytes are in the reply
memcpy(p->decoded.payload.bytes, str, strlen(str));
service.sendToMesh(p);
//HardwareMessage_init_default
// HardwareMessage_init_default
}
ProcessMessage StoreForwardPlugin::handleReceived(const MeshPacket &mp)
@ -237,12 +231,9 @@ ProcessMessage StoreForwardPlugin::handleReceived(const MeshPacket &mp)
StoreAndForwardMessage sfm = StoreAndForwardMessage_init_default;
switch(sfm.rr) {
switch (sfm.rr) {
}
auto &p = mp.decoded;
// The router node should not be sending messages as a client.
@ -261,8 +252,11 @@ ProcessMessage StoreForwardPlugin::handleReceived(const MeshPacket &mp)
} else {
storeForwardPlugin->historySend(1000 * 60, getFrom(&mp));
}
} else if ((p.payload.bytes[0] == 'S') && (p.payload.bytes[1] == 'F') && (p.payload.bytes[2] == 'm') && (p.payload.bytes[3] == 0x00)) {
strcpy(this->routerMessage, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456");
} else if ((p.payload.bytes[0] == 'S') && (p.payload.bytes[1] == 'F') && (p.payload.bytes[2] == 'm') &&
(p.payload.bytes[3] == 0x00)) {
strcpy(this->routerMessage, "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789012345678901234567890123456");
storeForwardPlugin->sendMessage(getFrom(&mp), this->routerMessage);
} else {
@ -319,9 +313,9 @@ StoreForwardPlugin::StoreForwardPlugin()
// Calculate the packet time.
// this->packetTimeMax = RadioLibInterface::instance->getPacketTime(Constants_DATA_PAYLOAD_LEN);
//RadioLibInterface::instance->getPacketTime(Constants_DATA_PAYLOAD_LEN);
//RadioLibInterface::instance->getPacketTime(Constants_DATA_PAYLOAD_LEN);
//RadioInterface::getPacketTime(500)l
// RadioLibInterface::instance->getPacketTime(Constants_DATA_PAYLOAD_LEN);
// RadioLibInterface::instance->getPacketTime(Constants_DATA_PAYLOAD_LEN);
// RadioInterface::getPacketTime(500)l
this->packetTimeMax = 2000;

View File

@ -19,7 +19,7 @@ struct PacketHistoryStruct {
class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThread
{
//bool firstTime = 1;
// bool firstTime = 1;
bool busy = 0;
uint32_t busyTo;
char routerMessage[80];
@ -32,9 +32,8 @@ class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThrea
PacketHistoryStruct *packetHistoryTXQueue;
uint32_t packetHistoryTXQueue_size;
uint32_t packetHistoryTXQueue_index = 0;
uint32_t packetTimeMax = 0;
uint32_t packetTimeMax = 0;
public:
StoreForwardPlugin();
@ -58,7 +57,7 @@ class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThrea
/*
Override the wantPortnum method.
*/
virtual bool wantPortnum(PortNum p) { return true; };
virtual bool wantPortnum(PortNum p) { return true; };
private:
void populatePSRAM();