Files
ZephCore/zephcore/include/mesh/StaticPoolPacketManager.h
T
Rastislav VysokyandClaude Opus 5.5 3acad8cafe core: hooks for a node on two radios
No behaviour change for the existing roles.

- StaticPoolPacketManager: one send queue per manager over one pool.
- Dispatcher::queueRetransmit() and Mesh::routeDirectRecvAcks() are
  overridable, so a retransmit can go out on another radio.
- Dispatcher::holdTxForOtherRadio(): defers a transmit without counting
  toward RX-stuck recovery; false unless a role overrides it.
- Mesh::macFails(): the failed-datagram budget, overridable so two Meshes
  of one node spend one budget.
- LoRaRadio: a TX-wait stack per radio (array sized by the lora0/lora1
  aliases) and isReceivingPacket() for the other radio's hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:16:17 +01:00

143 lines
4.8 KiB
C++

#pragma once
#include <mesh/Dispatcher.h>
#include <string.h> // ZEPHCORE: memset in PacketQueue
// ZEPHCORE: no heap in the packet path. The pool and its free list are static and
// live in the .cpp, shared by every manager; each manager owns its send queue, so
// two Dispatchers (one per radio) never drain each other's packets. Sizes are
// compile-time (CONFIG_ZEPHCORE_PACKET_POOL_SIZE, 32 without Kconfig).
#ifdef CONFIG_ZEPHCORE_PACKET_POOL_SIZE
#define ZC_PACKET_QUEUE_SIZE CONFIG_ZEPHCORE_PACKET_POOL_SIZE
#else
#define ZC_PACKET_QUEUE_SIZE 32
#endif
class StaticPoolPacketManager : public mesh::PacketManager {
public:
// ZEPHCORE: declared here (was file-local) so a manager can own its send queue.
struct PacketQueue {
mesh::Packet* _table[ZC_PACKET_QUEUE_SIZE];
uint8_t _pri_table[ZC_PACKET_QUEUE_SIZE];
uint32_t _schedule_table[ZC_PACKET_QUEUE_SIZE];
int _num;
PacketQueue() : _num(0) {
memset(_table, 0, sizeof(_table));
}
int count() const { return _num; }
mesh::Packet* itemAt(int i) const {
if (i < 0 || i >= _num) return NULL;
return _table[i];
}
int countBefore(uint32_t now) const {
if (now == 0xFFFFFFFF) return _num; // sentinel: count all entries regardless of schedule
int n = 0;
for (int j = 0; j < _num; j++) {
if ((int32_t)(_schedule_table[j] - now) > 0) continue; // scheduled for future... ignore for now
n++;
}
return n;
}
mesh::Packet* get(uint32_t now) {
uint8_t min_pri = 0xFF;
int best_idx = -1;
for (int j = 0; j < _num; j++) {
if ((int32_t)(_schedule_table[j] - now) > 0) continue; // scheduled for future... ignore for now
if (_pri_table[j] < min_pri) { // select most important priority amongst non-future entries
min_pri = _pri_table[j];
best_idx = j;
}
}
if (best_idx < 0) return NULL; // empty, or all items are still in the future
return removeByIdx(best_idx);
}
mesh::Packet* removeByIdx(int i) {
if (i < 0 || i >= _num) return NULL; // invalid index
mesh::Packet* item = _table[i];
_num--;
while (i < _num) {
_table[i] = _table[i+1];
_pri_table[i] = _pri_table[i+1];
_schedule_table[i] = _schedule_table[i+1];
i++;
}
return item;
}
bool add(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) {
if (_num == ZC_PACKET_QUEUE_SIZE) {
return false;
}
_table[_num] = packet;
_pri_table[_num] = priority;
_schedule_table[_num] = scheduled_for;
_num++;
return true;
}
// ZEPHCORE: eviction and schedule access (re-queue priority).
int findLowestPriority() const { // least important entry, -1 when empty
if (_num == 0) return -1;
uint8_t worst = 0;
int idx = 0;
for (int j = 0; j < _num; j++) {
if (_pri_table[j] >= worst) {
worst = _pri_table[j];
idx = j;
}
}
return idx;
}
uint32_t scheduleAt(int i) const { return (i < _num) ? _schedule_table[i] : 0; }
uint8_t peekPriority(uint32_t now) const { // what get(now) would return; 0xFF if none due
uint8_t best = 0xFF;
for (int j = 0; j < _num; j++) {
if ((int32_t)(_schedule_table[j] - now) > 0) continue;
if (_pri_table[j] < best) best = _pri_table[j];
}
return best;
}
bool addFront(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) { // index 0: first among equals
if (_num == ZC_PACKET_QUEUE_SIZE) return false;
for (int j = _num; j > 0; j--) {
_table[j] = _table[j-1];
_pri_table[j] = _pri_table[j-1];
_schedule_table[j] = _schedule_table[j-1];
}
_table[0] = packet;
_pri_table[0] = priority;
_schedule_table[0] = scheduled_for;
_num++;
return true;
}
};
private:
PacketQueue send_queue; // ZEPHCORE: per manager; the pool is shared
public:
mesh::Packet* allocNew() override;
void free(mesh::Packet* packet) override;
void queueOutbound(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) override;
mesh::Packet* getNextOutbound(uint32_t now) override;
int getOutboundCount(uint32_t now) const override;
int getOutboundTotal() const override;
int getFreeCount() const override;
mesh::Packet* getOutboundByIdx(int i) override;
mesh::Packet* removeOutboundByIdx(int i) override;
// ZEPHCORE: no inbound queue (see mesh::PacketManager); schedule access instead.
uint32_t getOutboundSchedule(int i) const override;
uint8_t peekNextOutboundPriority(uint32_t now) const override;
void requeueOutbound(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) override; // ZEPHCORE
uint32_t getAllocFails() const override; // ZEPHCORE
};