mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
Initial commit
This commit is contained in:
commit
6c7efdd0f6
59 changed files with 8604 additions and 0 deletions
129
src/Dispatcher.h
Normal file
129
src/Dispatcher.h
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
#pragma once
|
||||
|
||||
#include <MeshCore.h>
|
||||
#include <Identity.h>
|
||||
#include <Packet.h>
|
||||
#include <Utils.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
/**
|
||||
* \brief Abstraction of local/volatile clock with Millisecond granularity.
|
||||
*/
|
||||
class MillisecondClock {
|
||||
public:
|
||||
virtual unsigned long getMillis() = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Abstraction of this device's packet radio.
|
||||
*/
|
||||
class Radio {
|
||||
public:
|
||||
virtual void begin() { }
|
||||
|
||||
/**
|
||||
* \brief polls for incoming raw packet.
|
||||
* \param bytes destination to store incoming raw packet.
|
||||
* \param sz maximum packet size allowed.
|
||||
* \returns 0 if no incoming data, otherwise length of complete packet received.
|
||||
*/
|
||||
virtual int recvRaw(uint8_t* bytes, int sz) = 0;
|
||||
|
||||
/**
|
||||
* \returns estimated transmit air-time needed for packet of 'len_bytes', in milliseconds.
|
||||
*/
|
||||
virtual uint32_t getEstAirtimeFor(int len_bytes) = 0;
|
||||
|
||||
/**
|
||||
* \brief starts the raw packet send. (no wait)
|
||||
* \param bytes the raw packet data
|
||||
* \param len the length in bytes
|
||||
*/
|
||||
virtual void startSendRaw(const uint8_t* bytes, int len) = 0;
|
||||
|
||||
/**
|
||||
* \returns true if the previous 'startSendRaw()' completed successfully.
|
||||
*/
|
||||
virtual bool isSendComplete() = 0;
|
||||
|
||||
/**
|
||||
* \brief a hook for doing any necessary clean up after transmit.
|
||||
*/
|
||||
virtual void onSendFinished() = 0;
|
||||
|
||||
/**
|
||||
* \returns true if the radio is currently mid-receive of a packet.
|
||||
*/
|
||||
virtual bool isReceiving() { return false; }
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief An abstraction for managing instances of Packets (eg. in a static pool),
|
||||
* and for managing the outbound packet queue.
|
||||
*/
|
||||
class PacketManager {
|
||||
public:
|
||||
virtual Packet* allocNew() = 0;
|
||||
virtual void free(Packet* packet) = 0;
|
||||
|
||||
virtual void queueOutbound(Packet* packet, uint8_t priority, uint32_t scheduled_for) = 0;
|
||||
virtual Packet* getNextOutbound(uint32_t now) = 0; // by priority
|
||||
virtual int getOutboundCount() const = 0;
|
||||
virtual int getFreeCount() const = 0;
|
||||
virtual Packet* getOutboundByIdx(int i) = 0;
|
||||
virtual Packet* removeOutboundByIdx(int i) = 0;
|
||||
};
|
||||
|
||||
typedef uint32_t DispatcherAction;
|
||||
|
||||
#define ACTION_RELEASE (0)
|
||||
#define ACTION_MANUAL_HOLD (1)
|
||||
#define ACTION_RETRANSMIT(pri) (((uint32_t)1 + (pri))<<24)
|
||||
#define ACTION_RETRANSMIT_DELAYED(pri, _delay) ((((uint32_t)1 + (pri))<<24) | (_delay))
|
||||
|
||||
/**
|
||||
* \brief The low-level task that manages detecting incoming Packets, and the queueing
|
||||
* and scheduling of outbound Packets.
|
||||
*/
|
||||
class Dispatcher {
|
||||
Packet* outbound; // current outbound packet
|
||||
unsigned long outbound_expiry, outbound_start, total_air_time;
|
||||
unsigned long next_tx_time;
|
||||
|
||||
protected:
|
||||
PacketManager* _mgr;
|
||||
Radio* _radio;
|
||||
MillisecondClock* _ms;
|
||||
|
||||
Dispatcher(Radio& radio, MillisecondClock& ms, PacketManager& mgr)
|
||||
: _radio(&radio), _ms(&ms), _mgr(&mgr)
|
||||
{
|
||||
outbound = NULL; total_air_time = 0; next_tx_time = 0;
|
||||
}
|
||||
|
||||
virtual DispatcherAction onRecvPacket(Packet* pkt) = 0;
|
||||
virtual void onPacketSent(Packet* packet);
|
||||
virtual float getAirtimeBudgetFactor() const;
|
||||
|
||||
public:
|
||||
void begin();
|
||||
void loop();
|
||||
|
||||
Packet* obtainNewPacket();
|
||||
void releasePacket(Packet* packet);
|
||||
void sendPacket(Packet* packet, uint8_t priority, uint32_t delay_millis=0);
|
||||
|
||||
unsigned long getTotalAirTime() const { return total_air_time; } // in milliseconds
|
||||
|
||||
// helper methods
|
||||
bool millisHasNowPassed(unsigned long timestamp) const;
|
||||
unsigned long futureMillis(int millis_from_now) const;
|
||||
|
||||
private:
|
||||
void checkRecv();
|
||||
void checkSend();
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue