2020-02-02 20:45:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <assert.h>
|
2020-04-15 03:22:27 +00:00
|
|
|
#include <string>
|
2020-02-02 20:45:32 +00:00
|
|
|
|
|
|
|
#include "MemoryPool.h"
|
2020-03-19 02:15:51 +00:00
|
|
|
#include "MeshRadio.h"
|
2020-04-17 16:48:54 +00:00
|
|
|
#include "MeshTypes.h"
|
2020-02-06 16:49:33 +00:00
|
|
|
#include "Observer.h"
|
2020-03-19 02:15:51 +00:00
|
|
|
#include "PointerQueue.h"
|
|
|
|
#include "mesh.pb.h"
|
2020-02-02 20:45:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Top level app for this service. keeps the mesh, the radio config and the queue of received packets.
|
2020-03-19 02:15:51 +00:00
|
|
|
*
|
2020-02-02 20:45:32 +00:00
|
|
|
*/
|
2020-04-10 19:18:48 +00:00
|
|
|
class MeshService
|
2020-02-02 20:45:32 +00:00
|
|
|
{
|
2020-04-10 19:18:48 +00:00
|
|
|
CallbackObserver<MeshService, void *> gpsObserver = CallbackObserver<MeshService, void *>(this, &MeshService::onGPSChanged);
|
2020-04-17 16:48:54 +00:00
|
|
|
CallbackObserver<MeshService, const MeshPacket *> packetReceivedObserver =
|
|
|
|
CallbackObserver<MeshService, const MeshPacket *>(this, &MeshService::handleFromRadio);
|
2020-04-10 19:18:48 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
/// received packets waiting for the phone to process them
|
|
|
|
/// FIXME, change to a DropOldestQueue and keep a count of the number of dropped packets to ensure
|
|
|
|
/// we never hang because android hasn't been there in a while
|
2020-02-03 04:54:40 +00:00
|
|
|
/// FIXME - save this to flash on deep sleep
|
2020-02-02 20:45:32 +00:00
|
|
|
PointerQueue<MeshPacket> toPhoneQueue;
|
|
|
|
|
2020-02-03 04:54:40 +00:00
|
|
|
/// The current nonce for the newest packet which has been queued for the phone
|
2020-04-10 19:18:48 +00:00
|
|
|
uint32_t fromNum = 0;
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-04-17 16:48:54 +00:00
|
|
|
/// Updated in loop() to detect when fromNum changes
|
|
|
|
uint32_t oldFromNum = 0;
|
2020-04-14 18:40:49 +00:00
|
|
|
|
2020-04-17 16:48:54 +00:00
|
|
|
public:
|
2020-04-14 18:40:49 +00:00
|
|
|
/// Called when some new packets have arrived from one of the radios
|
2020-04-10 19:18:48 +00:00
|
|
|
Observable<uint32_t> fromNumChanged;
|
|
|
|
|
2020-04-14 18:40:49 +00:00
|
|
|
/// Called when radio config has changed (radios should observe this and set their hardware as required)
|
|
|
|
Observable<void *> configChanged;
|
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
MeshService();
|
|
|
|
|
|
|
|
void init();
|
2020-03-19 02:15:51 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
/// Do idle processing (mostly processing messages which have been queued from the radio)
|
|
|
|
void loop();
|
|
|
|
|
2020-02-03 02:33:46 +00:00
|
|
|
/// Return the next packet destined to the phone. FIXME, somehow use fromNum to allow the phone to retry the
|
|
|
|
/// last few packets if needs to.
|
|
|
|
MeshPacket *getForPhone() { return toPhoneQueue.dequeuePtr(0); }
|
|
|
|
|
|
|
|
/// Allows the bluetooth handler to free packets after they have been sent
|
|
|
|
void releaseToPool(MeshPacket *p) { packetPool.release(p); }
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-04-22 21:55:36 +00:00
|
|
|
/**
|
|
|
|
* Given a ToRadio buffer parse it and properly handle it (setup radio, owner or send packet into the mesh)
|
|
|
|
* Called by PhoneAPI.handleToRadio. Note: p is a scratch buffer, this function is allowed to write to it but it can not keep
|
|
|
|
* a reference
|
|
|
|
*/
|
|
|
|
void handleToRadio(MeshPacket &p);
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-02-04 05:17:35 +00:00
|
|
|
/// The radioConfig object just changed, call this to force the hw to change to the new settings
|
2020-02-11 19:56:48 +00:00
|
|
|
void reloadConfig();
|
2020-02-04 05:17:35 +00:00
|
|
|
|
|
|
|
/// The owner User record just got updated, update our node DB and broadcast the info into the mesh
|
2020-02-06 19:07:44 +00:00
|
|
|
void reloadOwner() { sendOurOwner(); }
|
2020-03-19 02:15:51 +00:00
|
|
|
|
|
|
|
/// Called when the user wakes up our GUI, normally sends our latest location to the mesh (if we have it), otherwise at least
|
|
|
|
/// sends our owner
|
2020-03-25 20:09:12 +00:00
|
|
|
void sendNetworkPing(NodeNum dest, bool wantReplies = false);
|
2020-02-12 22:07:06 +00:00
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
/// Send our owner info to a particular node
|
2020-03-25 20:09:12 +00:00
|
|
|
void sendOurOwner(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
|
2020-03-19 02:15:51 +00:00
|
|
|
|
|
|
|
private:
|
2020-02-12 22:07:06 +00:00
|
|
|
/// Broadcasts our last known position
|
2020-03-25 20:09:12 +00:00
|
|
|
void sendOurPosition(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
/// Send a packet into the mesh - note p must have been allocated from packetPool. We will return it to that pool after
|
|
|
|
/// sending. This is the ONLY function you should use for sending messages into the mesh, because it also updates the nodedb
|
|
|
|
/// cache
|
2020-02-06 16:49:33 +00:00
|
|
|
void sendToMesh(MeshPacket *p);
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-02-06 16:49:33 +00:00
|
|
|
/// Called when our gps position has changed - updates nodedb and sends Location message out into the mesh
|
2020-04-10 19:40:44 +00:00
|
|
|
/// returns 0 to allow futher processing
|
|
|
|
int onGPSChanged(void *arg);
|
2020-02-08 17:39:26 +00:00
|
|
|
|
2020-04-17 16:48:54 +00:00
|
|
|
/// Handle a packet that just arrived from the radio. This method does _not_ free the provided packet. If it needs
|
|
|
|
/// to keep the packet around it makes a copy
|
|
|
|
int handleFromRadio(const MeshPacket *p);
|
2020-02-18 01:47:01 +00:00
|
|
|
|
2020-02-13 03:58:44 +00:00
|
|
|
/// handle a user packet that just arrived on the radio, return NULL if we should not process this packet at all
|
2020-04-17 16:48:54 +00:00
|
|
|
const MeshPacket *handleFromRadioUser(const MeshPacket *mp);
|
2020-02-26 17:00:53 +00:00
|
|
|
|
|
|
|
/// look at inbound packets and if they contain a position with time, possibly set our clock
|
2020-04-17 16:48:54 +00:00
|
|
|
void handleIncomingPosition(const MeshPacket *mp);
|
2020-02-02 20:45:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern MeshService service;
|