firmware/src/plugins/PositionPlugin.cpp

63 lines
1.9 KiB
C++
Raw Normal View History

#include "PositionPlugin.h"
2020-12-03 08:48:44 +00:00
#include "MeshService.h"
#include "NodeDB.h"
2020-12-03 08:48:44 +00:00
#include "RTC.h"
#include "Router.h"
#include "configuration.h"
PositionPlugin *positionPlugin;
2020-12-03 08:48:44 +00:00
bool PositionPlugin::handleReceivedProtobuf(const MeshPacket &mp, const Position &p)
{
2020-12-03 08:48:44 +00:00
// FIXME - we currently update position data in the DB only if the message was a broadcast or destined to us
// it would be better to update even if the message was destined to others.
if (p.time) {
struct timeval tv;
uint32_t secs = p.time;
tv.tv_sec = secs;
tv.tv_usec = 0;
perhapsSetRTC(RTCQualityFromNet, &tv);
}
2020-12-03 08:48:44 +00:00
nodeDB.updatePosition(mp.from, p);
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
{
NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
assert(node->has_position);
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)
{
// cancel any not yet sent (now stale) position packets
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
service.cancelSending(prevPacketId);
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;
p->priority = MeshPacket_Priority_BACKGROUND;
prevPacketId = p->id;
2020-12-03 08:48:44 +00:00
service.sendToMesh(p);
}