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>
|
|
|
|
#include <LoRa.h>
|
|
|
|
#include <WiFi.h>
|
2021-01-29 11:47:04 +00:00
|
|
|
#include <cppQueue.h>
|
2019-04-25 06:17:02 +00:00
|
|
|
|
|
|
|
#include "BluetoothSerial.h"
|
2020-06-12 14:53:21 +00:00
|
|
|
#include "ax25_payload.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
|
|
|
|
2020-06-14 18:55:27 +00:00
|
|
|
class Service
|
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-01-29 11:47:04 +00:00
|
|
|
void processTx();
|
|
|
|
|
2020-06-12 14:34:23 +00:00
|
|
|
void onLoraDataAvailable(int packetSize);
|
2020-06-13 22:15:05 +00:00
|
|
|
void onAprsisDataAvailable();
|
2020-06-12 14:34:23 +00:00
|
|
|
|
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-01-29 11:47:04 +00:00
|
|
|
|
|
|
|
bool kissReceiveByte(unsigned char rxByte);
|
|
|
|
bool kissProcessCommand(unsigned char rxByte);
|
2020-02-11 09:00:39 +00:00
|
|
|
void kissResetState();
|
2019-05-09 16:13:08 +00:00
|
|
|
|
2021-02-02 08:26:11 +00:00
|
|
|
inline bool needsAprsis() const {
|
|
|
|
return !config_.IsClientMode && (config_.EnableRfToIs || config_.EnableIsToRf);
|
|
|
|
}
|
2020-06-14 14:59:35 +00:00
|
|
|
inline bool needsWifi() const { return needsAprsis(); }
|
2021-02-02 08:26:11 +00:00
|
|
|
inline bool needsBt() const { return config_.IsClientMode; }
|
|
|
|
inline bool needsBeacon() const { return !config_.IsClientMode && config_.EnableBeacon; }
|
2020-06-14 14:59:35 +00:00
|
|
|
|
2019-05-09 16:13:08 +00:00
|
|
|
private:
|
|
|
|
enum KissMarker {
|
|
|
|
Fend = 0xc0,
|
|
|
|
Fesc = 0xdb,
|
|
|
|
Tfend = 0xdc,
|
|
|
|
Tfesc = 0xdd
|
|
|
|
};
|
|
|
|
|
|
|
|
enum KissState {
|
|
|
|
Void = 0,
|
|
|
|
GetCmd,
|
|
|
|
GetData,
|
2020-12-08 20:22:08 +00:00
|
|
|
GetP,
|
2020-12-09 10:05:48 +00:00
|
|
|
GetSlotTime,
|
2019-05-09 16:13:08 +00:00
|
|
|
Escape
|
|
|
|
};
|
|
|
|
|
|
|
|
enum KissCmd {
|
2021-02-02 08:26:11 +00:00
|
|
|
|
|
|
|
// generic
|
2019-05-09 16:13:08 +00:00
|
|
|
Data = 0x00,
|
2020-12-08 20:22:08 +00:00
|
|
|
P = 0x02,
|
2020-12-09 10:05:48 +00:00
|
|
|
SlotTime = 0x03,
|
2021-02-02 08:26:11 +00:00
|
|
|
|
|
|
|
// extended to modem
|
|
|
|
Frequency = 0x10,
|
|
|
|
Bandwidth = 0x11,
|
|
|
|
Power = 0x12,
|
|
|
|
SyncWord = 0x13,
|
|
|
|
SpreadingFactor = 0x14,
|
|
|
|
CodingRate = 0x15,
|
|
|
|
EnableCrc = 0x16,
|
|
|
|
|
|
|
|
// extended events from modem
|
|
|
|
SignalLevel = 0x30,
|
|
|
|
|
|
|
|
// end of cmds
|
2019-05-09 16:13:08 +00:00
|
|
|
NoCmd = 0x80
|
|
|
|
};
|
|
|
|
|
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
|
|
|
// module pinouts
|
|
|
|
const byte CfgPinSs = 5;
|
|
|
|
const byte CfgPinRst = 26;
|
|
|
|
const byte CfgPinDio0 = 14;
|
|
|
|
|
|
|
|
// processor config
|
2021-01-29 13:16:41 +00:00
|
|
|
const int CfgConnRetryMs = 500;
|
2020-12-08 13:29:02 +00:00
|
|
|
const int CfgPollDelayMs = 5;
|
2021-01-29 13:22:51 +00:00
|
|
|
const int CfgLoraTxQueueSize = 4096;
|
2021-02-01 10:11:50 +00:00
|
|
|
const int CfgWiFiConnRetryMaxTimes = 10;
|
2020-06-20 09:50:57 +00:00
|
|
|
|
2021-02-02 09:18:46 +00:00
|
|
|
// csma paramters, use lower value for high traffic, use 255 for real time
|
2020-12-08 13:15:28 +00:00
|
|
|
const long CfgCsmaPersistence = 100;
|
|
|
|
const long CfgCsmaSlotTimeMs = 500;
|
2021-02-02 09:18:46 +00:00
|
|
|
|
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_;
|
|
|
|
byte csmaP_;
|
|
|
|
long csmaSlotTime_;
|
|
|
|
String aprsLoginCommand_;
|
2020-06-17 07:42:17 +00:00
|
|
|
AX25::Callsign ownCallsign_;
|
2020-06-14 14:59:35 +00:00
|
|
|
|
2021-02-02 08:26:11 +00:00
|
|
|
// kiss
|
2019-05-09 16:13:08 +00:00
|
|
|
KissState kissState_;
|
2020-11-27 18:44:50 +00:00
|
|
|
KissCmd kissCmd_;
|
2021-02-01 10:26:25 +00:00
|
|
|
std::shared_ptr<cppQueue>kissTxQueue_;
|
2021-02-02 08:26:11 +00:00
|
|
|
|
|
|
|
// state
|
|
|
|
long previousBeaconMs_;
|
|
|
|
|
2020-06-14 14:59:35 +00:00
|
|
|
// peripherals
|
2019-05-09 16:13:08 +00:00
|
|
|
BluetoothSerial serialBt_;
|
2020-06-12 14:34:23 +00:00
|
|
|
WiFiClient aprsisConn_;
|
2019-04-25 06:17:02 +00:00
|
|
|
};
|
|
|
|
|
2020-06-14 18:55:27 +00:00
|
|
|
} // LoraPrs
|
|
|
|
|
|
|
|
#endif // LORAPRS_SERVICE_H
|