esp32_loraprs/loraprs_service.h

162 wiersze
4.5 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 <DebugLog.h>
2021-10-22 18:30:09 +00:00
// When USE_RADIOLIB is defined then RadioLib will be used, otherwise arduino-LoRa will be used
// When using RadioLib, default module is SX1278, if you are using
// different module then update loraprs_service.h and loraprs_service.cpp
// search for SX1278 and replace with your module name
#define USE_RADIOLIB
#ifdef USE_RADIOLIB
#include <RadioLib.h>
#else
#include <LoRa.h>
#endif
#include <WiFi.h>
#include <endian.h>
#include "BluetoothSerial.h"
#include "ble_serial.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();
2021-10-21 18:25:35 +00:00
void attachKissNetworkClient();
2021-10-20 17:53:50 +00:00
bool isLoraRxBusy();
#ifdef USE_RADIOLIB
void onLoraDataAvailable();
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr();
#else
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
void loraReceive(int packetSize);
#endif
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 // only in server mode
&& (config_.EnableRfToIs || config_.EnableIsToRf) // rx/tx igate enabled
&& !config_.WifiEnableAp; // wifi is NOT in AP mode
}
inline bool needsWifi() const {
return needsAprsis() // aprsis is needed
|| config_.KissEnableTcpIp; // or kiss over tcp ip is enabled
}
inline bool needsBt() const {
return (config_.IsClientMode || config_.BtName.length() > 0) // client mode or name must be specified
&& !config_.UsbSerialEnable; // inactive in usb serial mode
}
inline bool needsBeacon() const {
return !config_.IsClientMode // beaconing only in apris gate / server mode
&& config_.EnableBeacon; // beacon must be explicitly enabled
}
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);
2021-10-20 09:58:43 +00:00
virtual void onRebootCommand();
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
2021-10-20 09:58:43 +00:00
const int CfgConnRetryMaxTimes = 10; // number of connection retries
const int CfgMaxAX25PayloadSize = 512; // maximum ax25 payload size
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-10-21 12:18:11 +00:00
// kiss static parameters
2021-10-21 18:25:35 +00:00
const int CfgKissPort = 8001; // kiss tcp/ip server port
private:
// config
Config config_;
2021-02-02 15:57:36 +00:00
String aprsLoginCommand_;
AX25::Callsign ownCallsign_;
2021-10-16 06:45:02 +00:00
bool isImplicitHeaderMode_;
2021-02-02 15:57:36 +00:00
// csma
byte csmaP_;
long csmaSlotTime_;
long csmaSlotTimePrev_;
// state
long previousBeaconMs_;
// peripherals
static byte rxBuf_[256];
#ifdef USE_RADIOLIB
static bool interruptEnabled_;
CircularBuffer<uint8_t, 256> txQueue_;
static std::shared_ptr<SX1278> radio_;
#endif
2019-05-09 16:13:08 +00:00
BluetoothSerial serialBt_;
BLESerial serialBLE_;
WiFiClient aprsisConn_;
2021-10-21 18:25:35 +00:00
2021-10-21 12:18:11 +00:00
std::shared_ptr<WiFiServer> kissServer_;
2021-10-21 18:25:35 +00:00
WiFiClient kissConn_;
bool isKissConn_;
};
2020-06-14 18:55:27 +00:00
} // LoraPrs
#endif // LORAPRS_SERVICE_H