Skip to content

Commit

Permalink
Merge pull request #315 from mcci-catena/issue314
Browse files Browse the repository at this point in the history
Fix #314: don't do (non-working) REJOIN; unjoin if out of sync
  • Loading branch information
terrillmoore authored May 20, 2019
2 parents d0c16df + 3b8d8d9 commit 7eed9b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/lmic/lmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ scan_mac_cmds(
}

// change the ADR ack request count, unless adr ack is diabled.
static void setAdrAckCount (s1_t count) {
static void setAdrAckCount (s2_t count) {
if (LMIC.adrAckReq != LINK_CHECK_OFF) {
LMIC.adrAckReq = count;
}
Expand Down Expand Up @@ -1807,7 +1807,7 @@ static void startJoining (xref2osjob_t osjob) {
// reset the joined-to-network state (and clean up)
void LMIC_unjoin(void) {
// reset any joining flags
LMIC.opmode &= ~(OP_SCAN|OP_REJOIN);
LMIC.opmode &= ~(OP_SCAN|OP_REJOIN|OP_UNJOIN);

// put us in unjoined state:
LMIC.devaddr = 0;
Expand All @@ -1820,11 +1820,12 @@ void LMIC_unjoin(void) {
bit_t LMIC_startJoining (void) {
if( LMIC.devaddr == 0 ) {
// There should be no TX/RX going on
ASSERT((LMIC.opmode & (OP_POLL|OP_TXRXPEND)) == 0);
// ASSERT((LMIC.opmode & (OP_POLL|OP_TXRXPEND)) == 0);
LMIC.opmode &= ~OP_POLL;
// Lift any previous duty limitation
LMIC.globalDutyRate = 0;
// Cancel scanning
LMIC.opmode &= ~(OP_SCAN|OP_REJOIN|OP_LINKDEAD|OP_NEXTCHNL);
LMIC.opmode &= ~(OP_SCAN|OP_UNJOIN|OP_REJOIN|OP_LINKDEAD|OP_NEXTCHNL);
// Setup state
LMIC.rejoinCnt = LMIC.txCnt = 0;
resetJoinParams();
Expand All @@ -1836,6 +1837,18 @@ bit_t LMIC_startJoining (void) {
}
return 0; // already joined
}

static void unjoinAndRejoin(xref2osjob_t osjob) {
LMIC_API_PARAMETER(osjob);
LMIC_unjoin();
LMIC_startJoining();
}

// do a deferred unjoin and rejoin, so not in engineupdate.
void LMIC_unjoinAndRejoin(void) {
os_setCallback(&LMIC.osjob, FUNC_ADDR(unjoinAndRejoin));
}

#endif // !DISABLE_JOIN


Expand Down Expand Up @@ -1920,14 +1933,10 @@ static bit_t processDnData (void) {
if( newDr == (dr_t)LMIC.datarate) {
// We are already at the minimum datarate
// if the link is already marked dead, we need to join.
// REJOIN sends a single join packet then brings back
// up the normal tx loop. If we miss the downlink for the
// join-accept, all the TXs until the next window will
// fail... so we leave in the OP_REJOIN, but set things
// to trigger a REJOIN after each uplink from here on.
LMIC.adrAckReq = LINK_CHECK_DEAD;
#if !defined(DISABLE_JOIN)
LMIC.opmode |= OP_REJOIN;
if ( LMIC.adrAckReq > LINK_CHECK_UNJOIN ) {
LMIC.opmode |= OP_UNJOIN;
}
#endif // !defined(DISABLE_JOIN)
} else {
// not in the dead state... let's wait another 32
Expand Down Expand Up @@ -2064,6 +2073,12 @@ static void engineUpdate (void) {
LMIC_startJoining();
return;
}
// we're joined but LinkTracking says we're out of luck...
if ( LMIC.devaddr != 0 && (LMIC.opmode & OP_UNJOIN) != 0 ) {
LMIC.opmode &= ~OP_UNJOIN;
LMIC_unjoinAndRejoin();
return;
}
#endif // !DISABLE_JOIN

ostime_t now = os_getTime();
Expand Down Expand Up @@ -2384,7 +2399,7 @@ void LMIC_setSession (u4_t netid, devaddr_t devaddr, xref2u1_t nwkKey, xref2u1_t

LMICbandplan_setSessionInitDefaultChannels();

LMIC.opmode &= ~(OP_JOINING|OP_TRACK|OP_REJOIN|OP_TXRXPEND|OP_PINGINI);
LMIC.opmode &= ~(OP_JOINING|OP_TRACK|OP_UNJOIN|OP_REJOIN|OP_TXRXPEND|OP_PINGINI);
LMIC.opmode |= OP_NEXTCHNL;
stateJustJoined();
// transition to the ADR_ACK_DELAY state.
Expand Down
6 changes: 5 additions & 1 deletion src/lmic/lmic.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ enum { MAX_RXSYMS = 100 }; // stop tracking beacon beyond this

enum { LINK_CHECK_CONT = 0 , // continue with this after reported dead link
LINK_CHECK_DEAD = 32 , // after this UP frames and no response to ack from NWK assume link is dead (ADR_ACK_DELAY)
LINK_CHECK_UNJOIN = 8192, // after this many UP frames and no response, switch to join.
LINK_CHECK_INIT = -64 , // UP frame count until we ask for ack (ADR_ACK_LIMIT)
LINK_CHECK_OFF =-128 }; // link check disabled

Expand Down Expand Up @@ -234,6 +235,7 @@ enum { OP_NONE = 0x0000,
OP_NEXTCHNL = 0x0800, // find a new channel
OP_LINKDEAD = 0x1000, // link was reported as dead
OP_TESTMODE = 0x2000, // developer test mode
OP_UNJOIN = 0x4000, // unjoin and rejoin on next engineUpdate().
};
// TX-RX transaction flags - report back to user
enum { TXRX_ACK = 0x80, // confirmed UP frame was acked
Expand Down Expand Up @@ -413,6 +415,8 @@ struct lmic_t {
u2_t opmode; // engineUpdate() operating mode flags
u2_t devNonce; // last generated nonce

s2_t adrAckReq; // counter for link integrity tracking (LINK_CHECK_OFF=off)

#if !defined(DISABLE_BEACONS)
s2_t drift; // last measured drift
s2_t lastDriftDiff;
Expand Down Expand Up @@ -448,7 +452,6 @@ struct lmic_t {
u1_t artKey[16]; // application router session key

u1_t dnConf; // dn frame confirm pending: LORA::FCT_ACK or 0
s1_t adrAckReq; // counter until we reset data rate (0=off)
u1_t adrChanged;

u1_t rxDelay; // Rx delay after TX
Expand Down Expand Up @@ -528,6 +531,7 @@ void LMIC_setAdrMode (bit_t enabled); // set ADR mode (if mobile turn
bit_t LMIC_startJoining (void);
void LMIC_tryRejoin (void);
void LMIC_unjoin (void);
void LMIC_unjoinAndRejoin (void);
#endif

void LMIC_shutdown (void);
Expand Down

0 comments on commit 7eed9b3

Please sign in to comment.