Arduino LMIC 6.0.1
Arduino LoRaWAN(r) MAC in C
Loading...
Searching...
No Matches
lmic_session_state.h
1/*
2
3Module: lmic_session_state.h
4
5Function:
6 Save and restore the LMIC session state as an opaque blob.
7
8Copyright notice and license info:
9 See LICENSE file accompanying this project.
10
11Author:
12 Terry Moore, MCCI Corporation September 2026
13
14Description:
15 A client that wants a session to survive a reset or a sleep calls
16 LMIC_saveSessionState() after each transaction and stores the bytes
17 it gets; after the next LMIC_reset() it calls
18 LMIC_restoreSessionState() with the same bytes. The blob is opaque:
19 its version and region are inside it, and
20 LMIC_querySessionState() reports them.
21
22 The layout is the one Arduino-LoRaWAN has stored since 2021 as its
23 SessionStateV1, byte for byte, so blobs already in the field
24 restore. The LMIC reads V1 and V2 and writes V2; the two differ
25 only in the header tag. In V1 the channel-group duty-cycle divisor
26 was saved from the wrong field (mcci-catena/arduino-lorawan#231),
27 so a V1 restore takes it from the region defaults instead.
28
29 The channel part of the blob is a union with a discriminator and
30 three variants: configurable channels (16 frequencies and groups),
31 72 fixed channels, and 96 fixed channels.
32
33 All multi-byte fields are little-endian regardless of the host,
34 except the 24-bit packed frequencies, which are most-significant
35 byte first in units of 100 Hz, as V1 wrote them.
36
37 See mcci-catena/arduino-lmic#1089.
38
39*/
40
41#ifndef _lmic_session_state_h_ /* prevent multiple includes */
42#define _lmic_session_state_h_
43
44#include <stddef.h>
45
46#ifndef _lmic_h_
47# include "lmic.h"
48#endif
49
50LMIC_BEGIN_DECLS
51
52/****************************************************************************\
53|
54| Blob layout
55|
56| Offsets are in bytes from the start of the blob. Types: u1, u2, u4 are
57| unsigned little-endian; s2 is signed little-endian; f3 is a 24-bit
58| frequency, MSB first, in units of 100 Hz.
59|
60| Header and MAC state (44 bytes)
61|
62| 0 u1 Tag LMIC_SESSION_STATE_TAG_V1 or _V2
63| 1 u1 Size total size of the blob, 216
64| 2 u1 Region LMIC_REGION_* code
65| 3 u1 LinkDR LMIC.datarate
66| 4 u4 FCntUp LMIC.seqnoUp
67| 8 u4 FCntDown LMIC.seqnoDn
68| 12 u4 gpsTime reserved, zero
69| 16 u4 globalAvail LMIC.globalDutyAvail - now at save, in ticks;
70| restored as now + value, clamped at zero
71| 20 u4 Rx2Frequency LMIC.dn2Freq, Hz
72| 24 u4 PingFrequency LMIC.ping.freq, or zero without DISABLE_PING
73| 28 u2 ClientTag not interpreted (Arduino-LoRaWAN: country code)
74| 30 s2 LinkIntegrity LMIC.adrAckReq
75| 32 u1 TxPower LMIC.adrTxPow
76| 33 u1 Redundancy LMIC.upRepeat (NbTrans)
77| 34 u1 DutyCycle LMIC.globalDutyRate
78| 35 u1 Rx1DRoffset LMIC.rx1DrOffset
79| 36 u1 Rx2DataRate LMIC.dn2Dr
80| 37 u1 RxDelay LMIC.rxDelay
81| 38 u1 TxParam LMIC.txParam, or 0xFF without LMIC_ENABLE_TxParamSetupReq
82| 39 u1 BeaconChannel LMIC.bcnChnl, or zero with DISABLE_BEACONS
83| 40 u1 PingDr LMIC.ping.dr, or zero with DISABLE_PING
84| 41 u1 MacRxParamAns LMIC.dn2Ans
85| 42 u1 MacDlChannelAns LMIC.macDlChannelAns
86| 43 u1 MacRxTimingSetupAns LMIC.macRxTimingSetupAns
87|
88| Channels (172 bytes, from offset 44): a union with a discriminator at
89| 44 and the variant's own size at 45. The fixed-channel variants are
90| smaller than the union; the bytes after them are zero.
91|
92| 44 u1 Kind LMIC_SESSION_STATE_CHANNELS_* (the discriminator)
93| 45 u1 Size 172, 22 or 26 by kind
94|
95| Configurable-channel variant (kind 0, 172 bytes; offsets from 44)
96|
97| 2 -- padding, two bytes, zero
98| 4 u4 ChannelGroups two bits per channel: channel group of channel i
99| 8 u2 ChannelMap LMIC.channelMap
100| 10 u2 ChannelShuffleMap LMIC.channelShuffleMap
101| 12 u2[16] ChannelDrMap LMIC.channelDrMap
102| 44 f3[16] UplinkFreq LMIC.channelFreq[i] & ~3
103| 92 f3[16] DownlinkFreq LMIC.channelDlFreq[i], or zero
104| 140 8x4 Groups one per channel group, see below
105|
106| Channel group entry (8 bytes)
107|
108| 0 u2 txDutyDenom LMIC.bands[i].txcap (V1: unreliable, ignored on restore)
109| 2 u1 txPower LMIC.bands[i].txpow
110| 3 u1 lastChannel LMIC.bands[i].lastchnl
111| 4 u4 ostimeAvail LMIC.bands[i].avail - now at save, clamped at
112| zero; restored as now + value
113|
114| Fixed-channel variant, 72 channels (kind 1, 22 bytes; offsets from 44)
115|
116| 2 u1[10] ChannelMap LMIC.channelMap as bytes, channel i at bit i
117| 12 u1[10] ChannelShuffleMap LMIC.channelShuffleMap likewise
118|
119| Fixed-channel variant, 96 channels (kind 2, 26 bytes): as kind 1 with
120| 12-byte maps. Not produced by this version of the LMIC.
121|
122\****************************************************************************/
123
125enum { LMIC_SESSION_STATE_SIZE = 216 };
126
128enum {
129 LMIC_SESSION_STATE_TAG_NULL = 0,
130 LMIC_SESSION_STATE_TAG_V1 = 1,
131 LMIC_SESSION_STATE_TAG_V2 = 2,
132};
133
135enum {
136 LMIC_SESSION_STATE_CHANNELS_CONFIGURABLE = 0,
137 LMIC_SESSION_STATE_CHANNELS_FIXED72 = 1,
138 LMIC_SESSION_STATE_CHANNELS_FIXED96 = 2,
139};
140
142typedef enum lmic_session_state_result_e {
143 LMIC_SESSION_STATE_OK = 0,
144 LMIC_SESSION_STATE_BUFFER_TOO_SMALL,
145 LMIC_SESSION_STATE_BAD_TAG,
146 LMIC_SESSION_STATE_BAD_SIZE,
147 LMIC_SESSION_STATE_BAD_CHANNEL_VARIANT,
148 LMIC_SESSION_STATE_REGION_MISMATCH,
149 LMIC_SESSION_STATE_REGION_NOT_AVAILABLE,
150} lmic_session_state_result_t;
151
154 u1_t version;
155 u1_t region;
157 u1_t size;
159} lmic_session_state_info_t;
160
162size_t LMIC_getSessionStateSize(void);
163
171lmic_session_state_result_t LMIC_saveSessionState(
172 u1_t *pBuf, size_t nBuf, size_t *pnUsed, u2_t clientTag
173 );
174
181lmic_session_state_result_t LMIC_restoreSessionState(
182 const u1_t *pBuf, size_t nBuf
183 );
184
188lmic_session_state_result_t LMIC_querySessionState(
189 const u1_t *pBuf, size_t nBuf, lmic_session_state_info_t *pInfo
190 );
191
192LMIC_END_DECLS
193
194#endif /* _lmic_session_state_h_ */
LMIC API.
what LMIC_querySessionState() reports about a blob.
Definition lmic_session_state.h:153
u2_t clientTag
the client's tag, as passed to LMIC_saveSessionState()
Definition lmic_session_state.h:158
u1_t region
LMIC_REGION_* code.
Definition lmic_session_state.h:155
u1_t version
header tag, LMIC_SESSION_STATE_TAG_*
Definition lmic_session_state.h:154
u1_t channelKind
LMIC_SESSION_STATE_CHANNELS_*.
Definition lmic_session_state.h:156
u1_t size
total size of the blob, bytes
Definition lmic_session_state.h:157