esp32_loraprs/loraprs_service.h

134 wiersze
2.8 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 <cppQueue.h>
#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-14 18:55:27 +00:00
namespace LoraPrs {
2020-06-14 18:55:27 +00:00
class Service
{
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();
void processTx();
void onLoraDataAvailable(int packetSize);
void onAprsisDataAvailable();
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);
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
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; }
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,
GetP,
GetSlotTime,
2019-05-09 16:13:08 +00:00
Escape
};
enum KissCmd {
// generic
2019-05-09 16:13:08 +00:00
Data = 0x00,
P = 0x02,
SlotTime = 0x03,
// 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
};
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
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;
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
const long CfgCsmaPersistence = 100;
const long CfgCsmaSlotTimeMs = 500;
2021-02-02 09:18:46 +00:00
private:
// config
Config config_;
byte csmaP_;
long csmaSlotTime_;
String aprsLoginCommand_;
AX25::Callsign ownCallsign_;
// kiss
2019-05-09 16:13:08 +00:00
KissState kissState_;
KissCmd kissCmd_;
2021-02-01 10:26:25 +00:00
std::shared_ptr<cppQueue>kissTxQueue_;
// 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