2020-06-14 18:55:27 +00:00
|
|
|
#ifndef LORAPRS_SEVICE_H
|
|
|
|
#define LORAPRS_SERVICE_H
|
2019-04-25 06:17:02 +00:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SPI.h>
|
2021-10-22 19:09:17 +00:00
|
|
|
#include <DebugLog.h>
|
2021-10-20 10:20:19 +00:00
|
|
|
|
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
|
|
|
|
|
2021-10-20 10:20:19 +00:00
|
|
|
#ifdef USE_RADIOLIB
|
|
|
|
#include <RadioLib.h>
|
|
|
|
#else
|
2019-04-25 06:17:02 +00:00
|
|
|
#include <LoRa.h>
|
2021-10-20 10:20:19 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-25 06:17:02 +00:00
|
|
|
#include <WiFi.h>
|
2021-02-05 16:57:06 +00:00
|
|
|
#include <endian.h>
|
2019-04-25 06:17:02 +00:00
|
|
|
|
|
|
|
#include "BluetoothSerial.h"
|
2021-06-08 05:33:40 +00:00
|
|
|
#include "ble_serial.h"
|
2020-06-12 14:53:21 +00:00
|
|
|
#include "ax25_payload.h"
|
2021-02-02 15:53:28 +00:00
|
|
|
#include "kiss_processor.h"
|
2020-06-14 18:55:27 +00:00
|
|
|
#include "loraprs_config.h"
|
2020-06-12 14:34:23 +00:00
|
|
|
|
2020-06-14 18:55:27 +00:00
|
|
|
namespace LoraPrs {
|
2019-04-25 06:17:02 +00:00
|
|
|
|
2021-02-02 15:53:28 +00:00
|
|
|
class Service : public Kiss::Processor
|
2019-04-25 06:17:02 +00:00
|
|
|
{
|
|
|
|
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:
|
2020-06-01 07:43:13 +00:00
|
|
|
void setupWifi(const String &wifiName, const String &wifiKey);
|
2021-02-02 07:31:45 +00:00
|
|
|
void setupLora(long loraFreq, long bw, int sf, int cr, int pwr, int sync, bool enableCrc);
|
2020-06-01 07:43:13 +00:00
|
|
|
void setupBt(const String &btName);
|
2019-05-09 16:13:08 +00:00
|
|
|
|
2021-02-02 09:18:46 +00:00
|
|
|
void reconnectWifi() const;
|
2020-06-12 14:34:23 +00:00
|
|
|
bool reconnectAprsis();
|
2021-10-21 18:25:35 +00:00
|
|
|
void attachKissNetworkClient();
|
2021-02-12 14:33:25 +00:00
|
|
|
|
2021-10-20 17:53:50 +00:00
|
|
|
bool isLoraRxBusy();
|
2021-10-20 10:20:19 +00:00
|
|
|
#ifdef USE_RADIOLIB
|
|
|
|
void onLoraDataAvailable();
|
|
|
|
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr();
|
|
|
|
#else
|
2021-02-12 14:33:25 +00:00
|
|
|
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
|
|
|
|
void loraReceive(int packetSize);
|
2021-10-20 10:20:19 +00:00
|
|
|
#endif
|
2020-06-13 22:15:05 +00:00
|
|
|
void onAprsisDataAvailable();
|
2021-10-22 19:20:17 +00:00
|
|
|
|
2021-02-05 16:57:06 +00:00
|
|
|
void sendSignalReportEvent(int rssi, float snr);
|
2020-06-19 07:39:49 +00:00
|
|
|
void sendPeriodicBeacon();
|
2021-02-02 08:32:32 +00:00
|
|
|
void sendToAprsis(const String &aprsMessage);
|
2021-01-29 11:47:04 +00:00
|
|
|
bool sendAX25ToLora(const AX25::Payload &payload);
|
2021-02-02 09:18:46 +00:00
|
|
|
void processIncomingRawPacketAsServer(const byte *packet, int packetLength);
|
2021-10-22 19:20:17 +00:00
|
|
|
|
2021-02-02 08:26:11 +00:00
|
|
|
inline bool needsAprsis() const {
|
2021-10-21 12:43:38 +00:00
|
|
|
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
|
2021-02-02 08:26:11 +00:00
|
|
|
}
|
2021-10-22 19:20:17 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-14 14:59:35 +00:00
|
|
|
|
2021-02-02 15:53:28 +00:00
|
|
|
protected:
|
|
|
|
virtual bool onRigTxBegin();
|
2021-02-02 15:57:36 +00:00
|
|
|
virtual void onRigTx(byte b);
|
2021-02-02 15:53:28 +00:00
|
|
|
virtual void onRigTxEnd();
|
2021-02-12 14:33:25 +00:00
|
|
|
virtual void onRigPacket(void *packet, int packetLength);
|
|
|
|
|
2021-02-02 15:57:36 +00:00
|
|
|
virtual void onSerialTx(byte b);
|
2021-02-02 15:53:28 +00:00
|
|
|
virtual bool onSerialRxHasData();
|
2021-02-02 15:57:36 +00:00
|
|
|
virtual bool onSerialRx(byte *b);
|
2019-05-09 16:13:08 +00:00
|
|
|
|
2021-02-02 15:53:28 +00:00
|
|
|
virtual void onControlCommand(Cmd cmd, byte value);
|
2021-02-05 16:57:06 +00:00
|
|
|
virtual void onRadioControlCommand(const std::vector<byte> &command);
|
2021-10-20 09:58:43 +00:00
|
|
|
virtual void onRebootCommand();
|
2021-02-05 16:57:06 +00:00
|
|
|
|
|
|
|
private:
|
2021-02-08 18:30:23 +00:00
|
|
|
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;
|
2021-02-05 16:57:06 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2021-02-08 18:30:23 +00:00
|
|
|
struct SignalReport {
|
|
|
|
int16_t rssi;
|
|
|
|
int16_t snr;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2021-02-02 15:53:28 +00:00
|
|
|
private:
|
2019-04-25 06:17:02 +00:00
|
|
|
const String CfgLoraprsVersion = "LoRAPRS 0.1";
|
2020-06-20 09:50:57 +00:00
|
|
|
|
2021-02-02 09:18:46 +00:00
|
|
|
// processor config
|
2021-02-12 14:33:25 +00:00
|
|
|
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
|
2021-02-12 14:33:25 +00:00
|
|
|
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
|
|
|
|
2021-02-05 16:57:06 +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
|
2019-04-25 06:17:02 +00:00
|
|
|
private:
|
2020-06-14 14:59:35 +00:00
|
|
|
// config
|
2021-02-02 08:26:11 +00:00
|
|
|
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
|
2021-02-02 08:26:11 +00:00
|
|
|
byte csmaP_;
|
|
|
|
long csmaSlotTime_;
|
2021-02-02 15:53:28 +00:00
|
|
|
long csmaSlotTimePrev_;
|
2020-06-14 14:59:35 +00:00
|
|
|
|
2021-02-02 08:26:11 +00:00
|
|
|
// state
|
|
|
|
long previousBeaconMs_;
|
|
|
|
|
2020-06-14 14:59:35 +00:00
|
|
|
// peripherals
|
2021-10-20 10:20:19 +00:00
|
|
|
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_;
|
2021-06-08 05:33:40 +00:00
|
|
|
BLESerial serialBLE_;
|
2020-06-12 14:34:23 +00:00
|
|
|
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_;
|
2019-04-25 06:17:02 +00:00
|
|
|
};
|
|
|
|
|
2020-06-14 18:55:27 +00:00
|
|
|
} // LoraPrs
|
|
|
|
|
|
|
|
#endif // LORAPRS_SERVICE_H
|