we now send gps locations into the mesh and update our nodedb

This commit is contained in:
geeksville 2020-02-06 08:49:33 -08:00
parent 9625bcbd9e
commit 241e262f5c
6 changed files with 67 additions and 28 deletions

View File

@ -59,6 +59,7 @@ until the phone pulls those packets. Ever so often power on bluetooth just so w
# Low priority # Low priority
* add a watchdog timer
* fix GPS.zeroOffset calculation it is wrong * fix GPS.zeroOffset calculation it is wrong
* handle millis() rollover in GPS.getTime - otherwise we will break after 50 days * handle millis() rollover in GPS.getTime - otherwise we will break after 50 days
* reneable the bluetooth battely level service on the T-BEAM, because we can read battery level there * reneable the bluetooth battely level service on the T-BEAM, because we can read battery level there

View File

@ -39,7 +39,7 @@ void GPS::loop()
24 * 60 * 60 * (date.month() * 31 + date.day() + 365 * (date.year() - 1970))) + 24 * 60 * 60 * (date.month() * 31 + date.day() + 365 * (date.year() - 1970))) +
time.centisecond() * 10; time.centisecond() * 10;
DEBUG_MSG("Setting time zero %lld", zeroOffset); DEBUG_MSG("Setting time zero %lld\n", zeroOffset);
} }
#endif #endif
} }

View File

@ -29,8 +29,17 @@ public:
delete[] buf; delete[] buf;
} }
/// Return a queable object which has been prefilled with zeros /// Return a queable object which has been prefilled with zeros. Panic if no buffer is available
T *allocZeroed(TickType_t maxWait = portMAX_DELAY) { T *allocZeroed() {
T *p = allocZeroed(0);
assert(p); // FIXME panic instead
return p;
}
/// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you probably don't want this version)
T *allocZeroed(TickType_t maxWait) {
T *p = dead.dequeuePtr(maxWait); T *p = dead.dequeuePtr(maxWait);
if(p) if(p)

View File

@ -6,6 +6,7 @@
#include "MeshService.h" #include "MeshService.h"
#include "MeshBluetoothService.h" #include "MeshBluetoothService.h"
#include "NodeDB.h" #include "NodeDB.h"
#include "GPS.h"
/* /*
receivedPacketQueue - this is a queue of messages we've received from the mesh, which we are keeping to deliver to the phone. receivedPacketQueue - this is a queue of messages we've received from the mesh, which we are keeping to deliver to the phone.
@ -18,19 +19,18 @@ a node number and keeping the current nodedb.
*/ */
MeshService service; MeshService service;
#define MAX_PACKETS 32 // max number of packets which can be in flight (either queued from reception or queued for sending) #define MAX_PACKETS 32 // max number of packets which can be in flight (either queued from reception or queued for sending)
#define MAX_RX_FROMRADIO 4 // max number of packets destined to our queue, we dispatch packets quickly so it doesn't need to be big #define MAX_RX_FROMRADIO 4 // max number of packets destined to our queue, we dispatch packets quickly so it doesn't need to be big
MeshService::MeshService() MeshService::MeshService()
: packetPool(MAX_PACKETS), : packetPool(MAX_PACKETS),
toPhoneQueue(MAX_RX_TOPHONE), toPhoneQueue(MAX_RX_TOPHONE),
fromRadioQueue(MAX_RX_FROMRADIO), fromRadioQueue(MAX_RX_FROMRADIO),
fromNum(0), fromNum(0),
radio(packetPool, fromRadioQueue) radio(packetPool, fromRadioQueue)
{ {
// assert(MAX_RX_TOPHONE == 32); // FIXME, delete this, just checking my clever macro // assert(MAX_RX_TOPHONE == 32); // FIXME, delete this, just checking my clever macro
} }
@ -38,9 +38,11 @@ MeshService::MeshService()
void MeshService::init() void MeshService::init()
{ {
nodeDB.init(); nodeDB.init();
if (!radio.init()) if (!radio.init())
DEBUG_MSG("radio init failed\n"); DEBUG_MSG("radio init failed\n");
gps.addObserver(this);
} }
/// Do idle processing (mostly processing messages which have been queued from the radio) /// Do idle processing (mostly processing messages which have been queued from the radio)
@ -50,14 +52,14 @@ void MeshService::loop()
MeshPacket *mp; MeshPacket *mp;
uint32_t oldFromNum = fromNum; uint32_t oldFromNum = fromNum;
while((mp = fromRadioQueue.dequeuePtr(0)) != NULL) { while ((mp = fromRadioQueue.dequeuePtr(0)) != NULL)
nodeDB.updateFrom(*mp); {
nodeDB.updateFrom(*mp); // update our DB state based off sniffing every RX packet from the radio
fromNum++; fromNum++;
assert(toPhoneQueue.enqueue(mp , 0) == pdTRUE); // FIXME, instead of failing for full queue, delete the oldest mssages assert(toPhoneQueue.enqueue(mp, 0) == pdTRUE); // FIXME, instead of failing for full queue, delete the oldest mssages
} }
if(oldFromNum != fromNum) // We don't want to generate extra notifies for multiple new packets if (oldFromNum != fromNum) // We don't want to generate extra notifies for multiple new packets
bluetoothNotifyFromNum(fromNum); bluetoothNotifyFromNum(fromNum);
} }
@ -71,7 +73,7 @@ void MeshService::handleToRadio(std::string s)
switch (r.which_variant) switch (r.which_variant)
{ {
case ToRadio_packet_tag: case ToRadio_packet_tag:
sendToMesh(r.variant.packet); sendToMesh(packetPool.allocCopy(r.variant.packet));
break; break;
default: default:
@ -81,12 +83,31 @@ void MeshService::handleToRadio(std::string s)
} }
} }
/// Send a packet into the mesh - note p is read only and should be copied into a pool based MeshPacket before void MeshService::sendToMesh(MeshPacket *p)
/// sending.
void MeshService::sendToMesh(const MeshPacket &pIn)
{ {
MeshPacket *pOut = packetPool.allocCopy(pIn); nodeDB.updateFrom(*p);
assert(pOut); // FIXME assert(radio.send(p) == pdTRUE);
assert(radio.send(pOut) == pdTRUE);
} }
void MeshService::onGPSChanged()
{
MeshPacket *p = packetPool.allocZeroed();
p->has_payload = true;
p->from = nodeDB.getNodeNum();
p->to = NODENUM_BROADCAST;
p->payload.which_variant = SubPacket_position_tag;
Position &pos = p->payload.variant.position;
if (gps.altitude.isValid())
pos.altitude = gps.altitude.value();
pos.latitude = gps.location.lat();
pos.longitude = gps.location.lng();
sendToMesh(p);
}
void MeshService::onNotify(Observable *o)
{
DEBUG_MSG("got gps notify\n");
onGPSChanged();
}

View File

@ -7,12 +7,13 @@
#include "MeshRadio.h" #include "MeshRadio.h"
#include "PointerQueue.h" #include "PointerQueue.h"
#include "MemoryPool.h" #include "MemoryPool.h"
#include "Observer.h"
/** /**
* Top level app for this service. keeps the mesh, the radio config and the queue of received packets. * Top level app for this service. keeps the mesh, the radio config and the queue of received packets.
* *
*/ */
class MeshService class MeshService: private Observer
{ {
MemoryPool<MeshPacket> packetPool; MemoryPool<MeshPacket> packetPool;
@ -58,10 +59,14 @@ public:
private: private:
/// Send a packet into the mesh - note p is read only and should be copied into a pool based MeshPacket before /// Send a packet into the mesh - note p must have been allocated from packetPool. We will return it to that pool after sending.
/// sending. /// This is the ONLY function you should use for sending messages into the mesh, because it also updates the nodedb cache
void sendToMesh(const MeshPacket &p); void sendToMesh(MeshPacket *p);
/// Called when our gps position has changed - updates nodedb and sends Location message out into the mesh
void onGPSChanged();
virtual void onNotify(Observable *o);
}; };
extern MeshService service; extern MeshService service;

View File

@ -17,6 +17,9 @@ public:
void observe(Observable *o); void observe(Observable *o);
private:
friend class Observable;
virtual void onNotify(Observable *o) = 0; virtual void onNotify(Observable *o) = 0;
}; };