RadioLib
Universal wireless communication library for Arduino
Loading...
Searching...
No Matches
LoRaWANPacMan.h
1#if !defined(_RADIOLIB_LORAWAN_PACMAN_H) && !RADIOLIB_EXCLUDE_LORAWAN
2#define _RADIOLIB_LORAWAN_PACMAN_H
3
4#include "LoRaWAN.h"
5
6// Number of known TSxxx packages
7#define RADIOLIB_LORAWAN_NUM_PACKAGES (8)
8
9// Package ID numbering as per TS008 (TS011 ID is made up)
10#define RADIOLIB_LORAWAN_PACKAGE_TS003 (1)
11#define RADIOLIB_LORAWAN_PACKAGE_TS004 (3)
12#define RADIOLIB_LORAWAN_PACKAGE_TS005 (2)
13#define RADIOLIB_LORAWAN_PACKAGE_TS006 (4)
14#define RADIOLIB_LORAWAN_PACKAGE_TS007 (0)
15#define RADIOLIB_LORAWAN_PACKAGE_TS009 (5)
16#define RADIOLIB_LORAWAN_PACKAGE_TS011 (6)
17
18// Package FPort (specified or recommended)
19#define RADIOLIB_LORAWAN_FPORT_TS003 (202)
20#define RADIOLIB_LORAWAN_FPORT_TS004 (201)
21#define RADIOLIB_LORAWAN_FPORT_TS005 (200)
22#define RADIOLIB_LORAWAN_FPORT_TS006 (203)
23#define RADIOLIB_LORAWAN_FPORT_TS007 (225)
24#define RADIOLIB_LORAWAN_FPORT_TS009 (224)
25#define RADIOLIB_LORAWAN_FPORT_TS011 (226)
26
27// TS007 Multi-Package Access package (PackageIdentifier 0) commands and constants
28#define RADIOLIB_LORAWAN_TS007_CID_PKG_VERSION (0x00)
29#define RADIOLIB_LORAWAN_TS007_CID_DEV_PACKAGE (0x01)
30#define RADIOLIB_LORAWAN_TS007_CID_MULTI_PACK_BUFFER (0x02)
31#define RADIOLIB_LORAWAN_TS007_PACKAGE_VERSION_NUM (1)
32#define RADIOLIB_LORAWAN_TS007_ERROR (0xFF)
33
34// A PackageID field has bit 7 set; CommandIDs are always < 128
35#define RADIOLIB_LORAWAN_TS007_PACKAGE_ID_FLAG (0x80)
36
37// Maximum size of the ANS buffer (TS007 section 3.1)
38#define RADIOLIB_LORAWAN_TS007_ANS_BUFFER_SIZE (128)
39
40// Forward-declare the manager so it can be friended by LoRaWANPackage
42
47class LoRaWANPackage {
48 public:
49
50 typedef RadioLibTime_t (*GetSecondsCb_t)();
51
52 // copy constructor is removed to prevent users from creating copies of this class
53 LoRaWANPackage(const LoRaWANPackage& obj) = delete;
54
65 virtual size_t processData(const uint8_t* dataIn, size_t lenIn, uint8_t* dataOut, size_t* lenOut, LoRaWANEvent_t* event);
66
75 virtual bool handleTask(RadioLibTime_t* tNext, bool* uplinkDue);
76
83 virtual size_t buildUplink(uint8_t* dataOut);
84
90 if(this->getSeconds_cb != NULL) {
91 return(this->getSeconds_cb());
92 }
93 return(0);
94 }
95
96#if !RADIOLIB_GODMODE
97 protected:
98#endif
99
107 LoRaWANPackage(uint8_t ts, LoRaWANPackageManager* pacMan, RadioLibHal* hal, LoRaWANNode* node, GetSecondsCb_t secondsCb);
108
109 uint8_t packageIdentifier;
110 uint8_t packageVersion;
111
112 LoRaWANPackageManager* pacMan;
113 RadioLibHal* hal;
114 LoRaWANNode* lorawanNode;
115 GetSecondsCb_t getSeconds_cb;
116
117 // allow LoRaWANPackageManager to access the protected members
119};
120
126 public:
127
128 typedef RadioLibTime_t (*GetSecondsCb_t)();
129 typedef void (*SetSecondsCb_t)(RadioLibTime_t seconds);
130 typedef void (*DelaySecondsCb_t)(RadioLibTime_t seconds);
131 typedef void (*UplinkIntervalCb_t)(RadioLibTime_t intervalSeconds);
132 typedef void (*ConfirmedCb_t)(bool confirmed);
133 typedef void (*RebootCb_t)();
134
141 LoRaWANPackageManager(RadioLibHal* hal, LoRaWANNode* node, GetSecondsCb_t secondsCb);
142
143 // Package enable methods
150 int16_t enableTS003(uint8_t fPort, SetSecondsCb_t setSecondsFunc);
151
156 int16_t enableTS007();
157
167 int16_t enableTS009(PhysicalLayer* radio, DelaySecondsCb_t delayCb, UplinkIntervalCb_t intervalCb, ConfirmedCb_t confirmedCb, RebootCb_t rebootCb);
168
169 // TS003
174 void requestAppTime();
175
183 int16_t processDownlink(const uint8_t* data, size_t len, LoRaWANEvent_t* eventDown);
184
193 bool handleTask(RadioLibTime_t* tNext);
194
202 bool getUplinkData(uint8_t* dataOut, size_t* lenOut, uint8_t* fPort);
203
204 bool isEnabledFPort(uint8_t fPort) {
205 for(uint8_t i = 0; i < RADIOLIB_LORAWAN_NUM_PACKAGES; i++) {
206 if(this->enabledPackages[i] && this->packagePorts[i] == fPort) {
207 return(true);
208 }
209 }
210 return(false);
211 }
212
213#if !RADIOLIB_GODMODE
214 protected:
215#endif
216
217 // Set up transmission of a MultiPackBufferReq answer (TS007 section 4.4).
218 // Uses the persistent ANS buffer; sets the transmit cursor or the error flag.
219 void handleMultiPackBufferReq(uint8_t startByte, uint8_t stopByte);
220
221 // Process the TS007 multi-package access package's own commands
222 // (PackageVersionReq, DevPackageReq).
223 size_t processPackageManagerData(const uint8_t* dataDown, size_t lenDown, uint8_t* ansOut, size_t* ansLen);
224
225 RadioLibHal* hal;
226 LoRaWANNode* lorawanNode;
227 LoRaWANPackage* packages[RADIOLIB_LORAWAN_NUM_PACKAGES];
228 uint8_t packagePorts[RADIOLIB_LORAWAN_NUM_PACKAGES];
229 bool enabledPackages[RADIOLIB_LORAWAN_NUM_PACKAGES];
230
231 // Time handler functions
232 GetSecondsCb_t getSecondsCb;
233
234 // Staging buffer for a plain (non multi-package) uplink: a dedicated-FPort answer
235 // or a scheduled package uplink. Packages write here (via the dataOut buffer they
236 // are handed); it is overwritten on each newly staged uplink.
237 uint8_t ansBuffer[255] = { 0 };
238 size_t ansBufferLen = 0;
239 uint8_t ansFPort = 0; // FPort on which the staged uplink will be sent (225 = TS007)
240
241 // Persistent TS007 ANS buffer (answers only, no Command Token). Kept in memory for
242 // on-demand retransmission via MultiPackBufferReq until a new multi-package command
243 // set is received. Separate from ansBuffer.
244 uint8_t ts007Buffer[RADIOLIB_LORAWAN_TS007_ANS_BUFFER_SIZE] = { 0 };
245 size_t ts007BufferLen = 0;
246 uint8_t commandToken = 0; // TS007 Command Token to append to FPort 225 uplinks
247
248 // Transmission state for a pending uplink (TS007 fragments are sent over several
249 // calls; a plain package uplink is sent in one)
250 bool pendingUplink = false; // a staged uplink is due now
251 uint8_t pendingPackage = 0; // package index that builds its uplink at send time
252 size_t txCursor = 0; // next BaseByte to transmit (TS007 fragmentation)
253 size_t txStop = 0; // last TS007 ANS buffer index to transmit (inclusive)
254 bool sendFragmented = false; // always wrap in MultiPackBufferFrag (Req response)
255 bool indexError = false; // next uplink is the [0x02][0xFF] error frame
256
257};
258
259#endif
LoRaWAN-compatible node (class A device).
Definition LoRaWAN.h:557
Common interface for all application packages.
Definition LoRaWANPacMan.h:47
virtual size_t processData(const uint8_t *dataIn, size_t lenIn, uint8_t *dataOut, size_t *lenOut, LoRaWANEvent_t *event)
Process a single package command, writing any answer into the buffer provided by the package manager.
Definition LoRaWANPacMan.cpp:14
virtual size_t buildUplink(uint8_t *dataOut)
Build the uplink that handleTask() reported as due, at the moment of transmission,...
Definition LoRaWANPacMan.cpp:32
virtual bool handleTask(RadioLibTime_t *tNext, bool *uplinkDue)
Execute any scheduled task that is due now and report when this package next needs servicing....
Definition LoRaWANPacMan.cpp:25
RadioLibTime_t getSeconds()
Get current time via the stored callback.
Definition LoRaWANPacMan.h:89
Interface to manage multiple LoRaWAN application packages.
Definition LoRaWANPacMan.h:125
bool getUplinkData(uint8_t *dataOut, size_t *lenOut, uint8_t *fPort)
Fetch uplink data from the first package reporting an UPLINK task.
Definition LoRaWANPacMan.cpp:396
int16_t enableTS003(uint8_t fPort, SetSecondsCb_t setSecondsFunc)
Enable TS003 Application Time Synchronization package.
Definition LoRaWANPacMan.cpp:54
int16_t enableTS009(PhysicalLayer *radio, DelaySecondsCb_t delayCb, UplinkIntervalCb_t intervalCb, ConfirmedCb_t confirmedCb, RebootCb_t rebootCb)
Enable TS009 Certification Protocol package.
Definition LoRaWANPacMan.cpp:97
void requestAppTime()
Send an application time request.
Definition LoRaWANPacMan.cpp:130
int16_t enableTS007()
Enable TS007 Multi-package Access Control package.
Definition LoRaWANPacMan.cpp:81
LoRaWANPackageManager(RadioLibHal *hal, LoRaWANNode *node, GetSecondsCb_t secondsCb)
Create a package manager.
Definition LoRaWANPacMan.cpp:40
bool handleTask(RadioLibTime_t *tNext)
Execute any due tasks across all packages and report when the manager next needs servicing.
Definition LoRaWANPacMan.cpp:333
int16_t processDownlink(const uint8_t *data, size_t len, LoRaWANEvent_t *eventDown)
Process a multi-package downlink payload (TS007 section 3.1).
Definition LoRaWANPacMan.cpp:139
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition PhysicalLayer.h:402
Hardware abstraction library base interface.
Definition Hal.h:18
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition TypeDef.h:783
Structure to save extra information about uplink/downlink event.
Definition LoRaWAN.h:514