mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-10-15 15:19:59 +00:00
- Mesh, Dispatcher, Packet, packet pool and the core headers rebuilt from Arduino MeshCore's text; ZephCore changes re-applied, each fenced `// ZEPHCORE:` (diff vs upstream: 4540 -> 1147 lines) - upstream header layout; MainBoard gains handleCommand/loop and the other upstream members; RNG::nextInt back in Utils.cpp - packet logging moved to the roles' log hooks (helpers/PacketLog.h) - zero-hop direct ACKs deduplicated and length-checked as upstream - RadioIncludes.h -> src/, LoRaConfig.h -> helpers/ - .editorconfig: shared files keep upstream formatting Verified: 10 role builds, per-function disassembly review, bench DM/ACK regression (T1000-E, Wio L1, XIAO S3 on 1.17.4) incl. via live repeater.
136 lines
4.7 KiB
C++
136 lines
4.7 KiB
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <math.h>
|
|
|
|
// ZEPHCORE: neutralize STM32 CMSIS peripheral-instance macros (RNG, AES, ...)
|
|
// that collide with portable mesh identifiers. No-op off STM32.
|
|
#include <mesh/stm32_cmsis_fixup.h>
|
|
|
|
#define MAX_HASH_SIZE 8
|
|
#define PUB_KEY_SIZE 32
|
|
#define PRV_KEY_SIZE 64
|
|
#define SEED_SIZE 32
|
|
#define SIGNATURE_SIZE 64
|
|
#define MAX_ADVERT_DATA_SIZE 32
|
|
#define CIPHER_KEY_SIZE 16
|
|
#define CIPHER_BLOCK_SIZE 16
|
|
|
|
// V1
|
|
#define CIPHER_MAC_SIZE 2
|
|
#define PATH_HASH_SIZE 1
|
|
|
|
#define MAX_PACKET_PAYLOAD 184
|
|
#define MAX_GROUP_DATA_LENGTH (MAX_PACKET_PAYLOAD - CIPHER_BLOCK_SIZE - 3)
|
|
#define MAX_PATH_SIZE 64
|
|
#define MAX_TRANS_UNIT 255
|
|
|
|
#if MESH_DEBUG && ARDUINO
|
|
#include <Arduino.h>
|
|
#define MESH_DEBUG_PRINT(F, ...) Serial.printf("DEBUG: " F, ##__VA_ARGS__)
|
|
#define MESH_DEBUG_PRINTLN(F, ...) Serial.printf("DEBUG: " F "\n", ##__VA_ARGS__)
|
|
#elif defined(__ZEPHYR__)
|
|
// ZEPHCORE: upstream's debug prints become Zephyr debug logs, compiled out
|
|
// unless the file's log module is at DBG. The using file must register a
|
|
// log module (LOG_MODULE_REGISTER / LOG_MODULE_DECLARE).
|
|
#include <zephyr/logging/log.h>
|
|
#define MESH_DEBUG_PRINT(F, ...) LOG_DBG(F, ##__VA_ARGS__)
|
|
#define MESH_DEBUG_PRINTLN(F, ...) LOG_DBG(F, ##__VA_ARGS__)
|
|
#else
|
|
#define MESH_DEBUG_PRINT(...) {}
|
|
#define MESH_DEBUG_PRINTLN(...) {}
|
|
#endif
|
|
|
|
#if BRIDGE_DEBUG && ARDUINO
|
|
#define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__)
|
|
#else
|
|
#define BRIDGE_DEBUG_PRINTLN(...) {}
|
|
#endif
|
|
|
|
namespace mesh {
|
|
|
|
#define BD_STARTUP_NORMAL 0 // getStartupReason() codes
|
|
#define BD_STARTUP_RX_PACKET 1
|
|
|
|
class MainBoard {
|
|
public:
|
|
virtual uint16_t getBattMilliVolts() = 0;
|
|
// ZEPHCORE: battery percentage from the board's discharge curve.
|
|
virtual uint8_t getBattPercent() { return 0; }
|
|
virtual float getMCUTemperature() { return NAN; }
|
|
virtual bool setAdcMultiplier(float multiplier) { return false; };
|
|
virtual float getAdcMultiplier() const { return 0.0f; }
|
|
virtual const char* getManufacturerName() const = 0;
|
|
virtual void onBeforeTransmit() { }
|
|
virtual void onAfterTransmit() { }
|
|
// ZEPHCORE: a valid packet has just been received. Unlike the transmit pair
|
|
// this is a single edge, not a window: the packet is already over by the time
|
|
// the radio reports it, so an LED implementation fires a one-shot.
|
|
virtual void onPacketReceived() { }
|
|
virtual void reboot() = 0;
|
|
virtual void powerOff() { /* no op */ }
|
|
// Called by example setup() functions to signal that boot is complete.
|
|
// Boards may override to stop a boot-indicator LED sequence or similar.
|
|
// Default no-op: boards that don't care need not implement anything.
|
|
virtual void onBootComplete() { /* no op */ }
|
|
virtual uint32_t getIRQGpio() { return -1; } // not supported. Returns DIO1 (SX1262) and DIO0 (SX127x)
|
|
virtual void sleep(uint32_t secs) { /* no op */ }
|
|
virtual uint32_t getGpio() { return 0; }
|
|
virtual void setGpio(uint32_t values) {}
|
|
virtual uint8_t getStartupReason() const = 0;
|
|
virtual bool getBootloaderVersion(char* version, size_t max_len) { return false; }
|
|
virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported
|
|
|
|
// Power management interface (boards with power management override these)
|
|
virtual bool isPwrMgtInitialised() const { return false; }
|
|
virtual bool isExternalPowered() { return false; }
|
|
virtual uint16_t getBootVoltage() { return 0; }
|
|
virtual bool getWakeLpcompSupported() const { return false; }
|
|
virtual uint32_t getResetReason() const { return 0; }
|
|
virtual const char* getResetReasonString(uint32_t reason) { return "Not available"; }
|
|
virtual uint8_t getShutdownReason() const { return 0; }
|
|
virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
|
|
|
|
virtual bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply) { return false; }
|
|
|
|
// Called from the example main loops. Lets a board feed its watchdog and
|
|
// run periodic housekeeping. Default no-op.
|
|
virtual void loop() { /* no op */ }
|
|
};
|
|
|
|
/**
|
|
* An abstraction of the device's Realtime Clock.
|
|
*/
|
|
class RTCClock {
|
|
uint32_t last_unique;
|
|
protected:
|
|
RTCClock() { last_unique = 0; }
|
|
|
|
public:
|
|
/**
|
|
* \returns the current time. in UNIX epoch seconds.
|
|
*/
|
|
virtual uint32_t getCurrentTime() = 0;
|
|
|
|
/**
|
|
* \param time current time in UNIX epoch seconds.
|
|
*/
|
|
virtual void setCurrentTime(uint32_t time) = 0;
|
|
|
|
/**
|
|
* override in classes that need to periodically update internal state
|
|
*/
|
|
virtual void tick() { /* no op */}
|
|
|
|
uint32_t getCurrentTimeUnique() {
|
|
uint32_t t = getCurrentTime();
|
|
if (t <= last_unique) {
|
|
return ++last_unique;
|
|
}
|
|
return last_unique = t;
|
|
}
|
|
};
|
|
|
|
}
|