esp32_loraprs/loraprs_service.h

118 wiersze
3.2 KiB
C
Czysty Zwykły widok Historia

2020-06-14 18:55:27 +00:00
#ifndef LORAPRS_SEVICE_H
#define LORAPRS_SERVICE_H
#include <Arduino.h>
#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <endian.h>
#include "BluetoothSerial.h"
2020-06-12 14:53:21 +00:00
#include "ax25_payload.h"
#include "kiss_processor.h"
2020-06-14 18:55:27 +00:00
#include "loraprs_config.h"
2020-06-14 18:55:27 +00:00
namespace LoraPrs {
class Service : public Kiss::Processor
{
public:
2020-06-14 18:55:27 +00:00
Service();
2019-05-09 16:13:08 +00:00
2020-06-14 18:55:27 +00:00
void setup(const Config &conf);
2019-05-09 16:13:08 +00:00
void loop();
private:
void setupWifi(const String &wifiName, const String &wifiKey);
void setupLora(long loraFreq, long bw, int sf, int cr, int pwr, int sync, bool enableCrc);
void setupBt(const String &btName);
2019-05-09 16:13:08 +00:00
2021-02-02 09:18:46 +00:00
void reconnectWifi() const;
bool reconnectAprsis();
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
void loraReceive(int packetSize);
void onAprsisDataAvailable();
void sendSignalReportEvent(int rssi, float snr);
2020-06-19 07:39:49 +00:00
void sendPeriodicBeacon();
void sendToAprsis(const String &aprsMessage);
bool sendAX25ToLora(const AX25::Payload &payload);
2021-02-02 09:18:46 +00:00
void processIncomingRawPacketAsServer(const byte *packet, int packetLength);
inline bool needsAprsis() const {
return !config_.IsClientMode && (config_.EnableRfToIs || config_.EnableIsToRf);
}
inline bool needsWifi() const { return needsAprsis(); }
inline bool needsBt() const { return config_.IsClientMode; }
inline bool needsBeacon() const { return !config_.IsClientMode && config_.EnableBeacon; }
protected:
virtual bool onRigTxBegin();
2021-02-02 15:57:36 +00:00
virtual void onRigTx(byte b);
virtual void onRigTxEnd();
virtual void onRigPacket(void *packet, int packetLength);
2021-02-02 15:57:36 +00:00
virtual void onSerialTx(byte b);
virtual bool onSerialRxHasData();
2021-02-02 15:57:36 +00:00
virtual bool onSerialRx(byte *b);
2019-05-09 16:13:08 +00:00
virtual void onControlCommand(Cmd cmd, byte value);
virtual void onRadioControlCommand(const std::vector<byte> &command);
private:
struct SetHardware {
2021-02-06 13:53:19 +00:00
uint32_t freq;
uint32_t bw;
uint16_t sf;
uint16_t cr;
uint16_t pwr;
uint16_t sync;
uint8_t crc;
} __attribute__((packed));
struct SignalReport {
int16_t rssi;
int16_t snr;
} __attribute__((packed));
private:
const String CfgLoraprsVersion = "LoRAPRS 0.1";
2020-06-20 09:50:57 +00:00
2021-02-02 09:18:46 +00:00
// processor config
const int CfgConnRetryMs = 500; // connection retry delay, e.g. wifi
const int CfgPollDelayMs = 5; // main loop delay
const int CfgWiFiConnRetryMaxTimes = 10; // wifi number of connection retries
const int CfgMaxAX25PayloadSize = 512; // maximum ax25 payload size
const int CfgFreqCorrMinHz = 1000; // correct if deviation is larger than this number
// NB! small value causes frequent corrections, which locks LoRa ISR
const int CfgMaxAprsInMessageSize = 255; // maximum aprsis to rf message size
2020-06-20 09:50:57 +00:00
// csma parameters, overriden with KISS commands
2021-02-02 16:10:12 +00:00
const long CfgCsmaPersistence = 100; // 255 for real time, lower for higher traffic
const long CfgCsmaSlotTimeMs = 500; // 0 for real time, otherwise set to average tx duration
2021-02-02 09:18:46 +00:00
private:
// config
Config config_;
2021-02-02 15:57:36 +00:00
String aprsLoginCommand_;
AX25::Callsign ownCallsign_;
// csma
byte csmaP_;
long csmaSlotTime_;
long csmaSlotTimePrev_;
// state
long previousBeaconMs_;
// peripherals
2019-05-09 16:13:08 +00:00
BluetoothSerial serialBt_;
WiFiClient aprsisConn_;
};
2020-06-14 18:55:27 +00:00
} // LoraPrs
#endif // LORAPRS_SERVICE_H