Skip to content

Commit dc5fd9a

Browse files
committed
Added setSeqnoUp and getSessionKeys
These functions are useful when you have a way to store configuration on the node. You can use them to avoid joining on restart and to ensure that sequence numbers are monotonic.
1 parent c86f2e4 commit dc5fd9a

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

examples/ttn-otaa/ttn-otaa.ino

+23-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,29 @@ void onEvent (ev_t ev) {
8787
break;
8888
case EV_JOINED:
8989
Serial.println(F("EV_JOINED"));
90-
90+
{
91+
u4_t netid = 0;
92+
devaddr_t devaddr = 0;
93+
u1_t nwkKey[16];
94+
u1_t artKey[16];
95+
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
96+
Serial.print("netid: ");
97+
Serial.println(netid, DEC);
98+
Serial.print("devaddr: ");
99+
Serial.println(devaddr, HEX);
100+
Serial.print("artKey: ");
101+
for (int i=0; i<sizeof(artKey); ++i) {
102+
Serial.print(artKey[i], HEX);
103+
}
104+
Serial.println("");
105+
Serial.print("nwkKey: ");
106+
for (int i=0; i<sizeof(nwkKey); ++i) {
107+
Serial.print(nwkKey[i], HEX);
108+
}
109+
Serial.println("");
110+
111+
LMIC_setSeqnoUp(140);
112+
}
91113
// Disable link check validation (automatically enabled
92114
// during join, but not supported by TTN at this time).
93115
LMIC_setLinkCheckMode(0);

src/lmic/lmic.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -2364,10 +2364,29 @@ void LMIC_setClockError(u2_t error) {
23642364
LMIC.clockError = error;
23652365
}
23662366

2367-
// \brief return the current uplink sequence number.
2367+
// \brief return the uplink sequence number for the next transmission.
23682368
// This simple getter returns the uplink sequence number maintained by the LMIC engine.
2369-
// It's useful in debugging, as it allows you to correlate a debug trace event with
2369+
// The caller should store the value and restore it (see LMIC_setSeqnoUp) on
2370+
// LMIC initialization to ensure monotonically increasing sequence numbers.
2371+
// It's also useful in debugging, as it allows you to correlate a debug trace event with
23702372
// a specific packet sent over the air.
23712373
u4_t LMIC_getSeqnoUp(void) {
23722374
return LMIC.seqnoUp;
23732375
}
2376+
2377+
// \brief set the uplink sequence number for the next transmission.
2378+
// Use the function on startup to ensure that the next transmission uses
2379+
// a sequence number higher than the last transmission.
2380+
u4_t LMIC_setSeqnoUp(u4_t seq_no) {
2381+
u4_t last = LMIC.seqnoUp;
2382+
LMIC.seqnoUp = seq_no;
2383+
return last;
2384+
}
2385+
2386+
// \brief return the current session keys returned from join.
2387+
void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xref2u1_t artKey) {
2388+
*netid = LMIC.netid;
2389+
*devaddr = LMIC.devaddr;
2390+
memcpy(artKey, LMIC.artKey, sizeof(LMIC.artKey));
2391+
memcpy(nwkKey, LMIC.nwkKey, sizeof(LMIC.nwkKey));
2392+
}

src/lmic/lmic.h

+2
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ void LMIC_setLinkCheckMode (bit_t enabled);
307307
void LMIC_setClockError(u2_t error);
308308

309309
u4_t LMIC_getSeqnoUp (void);
310+
u4_t LMIC_setSeqnoUp (u4_t);
311+
void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xref2u1_t artKey);
310312

311313
// Declare onEvent() function, to make sure any definition will have the
312314
// C conventions, even when in a C++ file.

0 commit comments

Comments
 (0)