2020-02-02 20:45:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "mesh.pb.h"
|
|
|
|
#include "MeshRadio.h"
|
|
|
|
#include "PointerQueue.h"
|
|
|
|
#include "MemoryPool.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Top level app for this service. keeps the mesh, the radio config and the queue of received packets.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class MeshService
|
|
|
|
{
|
|
|
|
MemoryPool<MeshPacket> packetPool;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// Packets which have just arrived from the radio, ready to be processed by this service and possibly
|
2020-02-03 04:54:40 +00:00
|
|
|
/// forwarded to the phone.
|
|
|
|
PointerQueue<MeshPacket> fromRadioQueue;
|
|
|
|
|
|
|
|
/// The current nonce for the newest packet which has been queued for the phone
|
|
|
|
uint32_t fromNum;
|
2020-02-02 20:45:32 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
MeshRadio radio;
|
|
|
|
|
|
|
|
MeshService();
|
|
|
|
|
|
|
|
void init();
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
/// Given a ToRadio buffer (from bluetooth) parse it and properly handle it (setup radio, owner or send packet into the mesh)
|
|
|
|
void handleToRadio(std::string s);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Send a packet into the mesh - note p is read only and should be copied into a pool based MeshPacket before
|
|
|
|
/// sending.
|
|
|
|
void sendToMesh(const MeshPacket &p);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
extern MeshService service;
|
|
|
|
|