2020-11-28 05:51:51 +00:00
|
|
|
#include "PositionPlugin.h"
|
2020-12-03 08:48:44 +00:00
|
|
|
#include "MeshService.h"
|
2020-11-28 05:51:51 +00:00
|
|
|
#include "NodeDB.h"
|
2020-12-03 08:48:44 +00:00
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
2020-11-28 05:51:51 +00:00
|
|
|
|
2021-01-08 05:15:49 +00:00
|
|
|
PositionPlugin *positionPlugin;
|
2020-11-28 05:51:51 +00:00
|
|
|
|
2021-02-14 04:26:51 +00:00
|
|
|
PositionPlugin::PositionPlugin()
|
|
|
|
: ProtobufPlugin("position", PortNum_POSITION_APP, Position_fields), concurrency::OSThread("PositionPlugin")
|
|
|
|
{
|
2021-03-27 08:17:01 +00:00
|
|
|
isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others
|
|
|
|
setIntervalFromNow(60 * 1000); // Send our initial position 60 seconds after we start (to give GPS time to setup)
|
2021-02-14 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 11:04:41 +00:00
|
|
|
bool PositionPlugin::handleReceivedProtobuf(const MeshPacket &mp, const Position *pptr)
|
2020-11-28 05:51:51 +00:00
|
|
|
{
|
2021-02-17 11:04:41 +00:00
|
|
|
auto p = *pptr;
|
2020-12-03 08:48:44 +00:00
|
|
|
|
2020-12-05 02:00:46 +00:00
|
|
|
if (p.time) {
|
|
|
|
struct timeval tv;
|
|
|
|
uint32_t secs = p.time;
|
|
|
|
|
|
|
|
tv.tv_sec = secs;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
perhapsSetRTC(RTCQualityFromNet, &tv);
|
|
|
|
}
|
|
|
|
|
2021-03-05 02:19:27 +00:00
|
|
|
nodeDB.updatePosition(getFrom(&mp), p);
|
2020-11-28 05:51:51 +00:00
|
|
|
|
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
}
|
2020-12-03 08:48:44 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
MeshPacket *PositionPlugin::allocReply()
|
2020-12-03 08:48:44 +00:00
|
|
|
{
|
2021-01-04 01:59:53 +00:00
|
|
|
NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
|
|
|
|
assert(node->has_position);
|
2021-02-14 03:37:32 +00:00
|
|
|
|
|
|
|
Position p = node->position;
|
|
|
|
|
|
|
|
// Strip out any time information before sending packets to other nodes - to keep the wire size small (and because other
|
|
|
|
// nodes shouldn't trust it anyways) Note: we allow a device with a local GPS to include the time, so that gpsless
|
|
|
|
// devices can get time.
|
|
|
|
if (getRTCQuality() < RTCQualityGPS) {
|
|
|
|
DEBUG_MSG("Stripping time %u from position send\n", p.time);
|
|
|
|
p.time = 0;
|
|
|
|
} else
|
|
|
|
DEBUG_MSG("Providing time to mesh %u\n", p.time);
|
|
|
|
|
|
|
|
return allocDataProtobuf(p);
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PositionPlugin::sendOurPosition(NodeNum dest, bool wantReplies)
|
|
|
|
{
|
2021-02-11 09:39:53 +00:00
|
|
|
// cancel any not yet sent (now stale) position packets
|
2021-02-14 03:37:32 +00:00
|
|
|
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
|
2021-02-11 09:39:53 +00:00
|
|
|
service.cancelSending(prevPacketId);
|
2021-02-11 11:00:17 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
MeshPacket *p = allocReply();
|
2020-12-03 08:48:44 +00:00
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
2021-02-11 11:00:17 +00:00
|
|
|
p->priority = MeshPacket_Priority_BACKGROUND;
|
2021-02-11 09:39:53 +00:00
|
|
|
prevPacketId = p->id;
|
2020-12-03 08:48:44 +00:00
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
2021-02-14 03:57:48 +00:00
|
|
|
|
2021-02-14 04:26:51 +00:00
|
|
|
int32_t PositionPlugin::runOnce()
|
|
|
|
{
|
2021-02-14 03:57:48 +00:00
|
|
|
|
|
|
|
// We limit our GPS broadcasts to a max rate
|
|
|
|
uint32_t now = millis();
|
|
|
|
if (lastGpsSend == 0 || now - lastGpsSend >= getPref_position_broadcast_secs() * 1000) {
|
|
|
|
lastGpsSend = now;
|
|
|
|
|
|
|
|
// If we changed channels, ask everyone else for their latest info
|
|
|
|
bool requestReplies = currentGeneration != radioGeneration;
|
|
|
|
currentGeneration = radioGeneration;
|
|
|
|
|
|
|
|
DEBUG_MSG("Sending position to mesh (wantReplies=%d)\n", requestReplies);
|
|
|
|
sendOurPosition(NODENUM_BROADCAST, requestReplies);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 5000; // to save power only wake for our callback occasionally
|
|
|
|
}
|