Skip to content

Commit 279ff2d

Browse files
committed
Fix #418: add strict LMIC_send apis
1 parent 174a292 commit 279ff2d

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

examples/compliance-otaa-halconfig/compliance-otaa-halconfig.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ void do_send(osjob_t* j){
541541
Serial.println(F("test mode, not sending"));
542542
} else {
543543
// Prepare upstream data transmission at the next possible time.
544-
if (LMIC_sendWithCallback(1, mydata, sizeof(mydata)-1, 0, sendComplete, j) == 0) {
544+
if (LMIC_sendWithCallback_strict(1, mydata, sizeof(mydata)-1, 0, sendComplete, j) == 0) {
545545
Serial.println(F("Packet queued"));
546546
} else {
547547
Serial.println(F("Packet queue failure; sleeping"));

src/lmic/lmic.c

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,9 +2723,49 @@ void LMIC_clrTxData (void) {
27232723
engineUpdate();
27242724
}
27252725

2726+
dr_t LMIC_feasibleDataRateForFrame(dr_t dr, u1_t payloadSize) {
2727+
if (payloadSize > MAX_LEN_PAYLOAD) {
2728+
return dr;
2729+
}
2730+
2731+
const u1_t frameSize = payloadSize + OFF_DAT_OPTS + 5;
2732+
dr_t trialDr, nextDr;
2733+
2734+
for (trialDr = dr; ;) {
2735+
if (! LMICbandplan_isDataRateFeasible(trialDr))
2736+
break;
2737+
u1_t maxSizeThisDr = LMICbandplan_maxFrameLen(trialDr);
2738+
if (maxSizeThisDr == 0) {
2739+
break;
2740+
} else if (frameSize <= maxSizeThisDr) {
2741+
// we found one that is feasible!
2742+
return trialDr;
2743+
}
2744+
// try the next DR
2745+
nextDr = incDR(trialDr);
2746+
if (nextDr == trialDr)
2747+
break;
2748+
trialDr = nextDr;
2749+
}
2750+
2751+
// if we get here, we didn't find a working dr.
2752+
return dr;
2753+
}
2754+
2755+
static void adjustDrForFrame(u1_t len) {
2756+
dr_t newDr = LMIC_feasibleDataRateForFrame(LMIC.datarate, len);
2757+
if (newDr != LMIC.datarate) {
2758+
setDrTxpow(DRCHG_FRAMESIZE, newDr, KEEP_TXPOW);
2759+
}
2760+
}
27262761

27272762
void LMIC_setTxData (void) {
2728-
LMICOS_logEventUint32("LMIC_setTxData", (LMIC.pendTxPort << 24u) | (LMIC.pendTxConf << 16u) | (LMIC.pendTxLen << 0u));
2763+
adjustDrForFrame(LMIC.pendTxLen);
2764+
LMIC_setTxData_strict();
2765+
}
2766+
2767+
void LMIC_setTxData_strict (void) {
2768+
LMICOS_logEventUint32(__func__, (LMIC.pendTxPort << 24u) | (LMIC.pendTxConf << 16u) | (LMIC.pendTxLen << 0u));
27292769
LMIC.opmode |= OP_TXDATA;
27302770
if( (LMIC.opmode & OP_JOINING) == 0 ) {
27312771
LMIC.txCnt = 0; // reset the confirmed uplink FSM
@@ -2735,8 +2775,14 @@ void LMIC_setTxData (void) {
27352775
}
27362776

27372777

2738-
// send a message w/o callback
2778+
// send a message, attempting to adjust TX data rate
27392779
lmic_tx_error_t LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed) {
2780+
adjustDrForFrame(dlen);
2781+
return LMIC_setTxData2_strict(port, data, dlen, confirmed);
2782+
}
2783+
2784+
// send a message w/o callback; do not adjust data rate
2785+
lmic_tx_error_t LMIC_setTxData2_strict (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed) {
27402786
if ( LMIC.opmode & OP_TXDATA ) {
27412787
// already have a message queued
27422788
return LMIC_ERROR_TX_BUSY;
@@ -2748,7 +2794,7 @@ lmic_tx_error_t LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t conf
27482794
LMIC.pendTxConf = confirmed;
27492795
LMIC.pendTxPort = port;
27502796
LMIC.pendTxLen = dlen;
2751-
LMIC_setTxData();
2797+
LMIC_setTxData_strict();
27522798
if ( (LMIC.opmode & OP_TXDATA) == 0 ) {
27532799
if (LMIC.txrxFlags & TXRX_LENERR) {
27542800
return LMIC_ERROR_TX_NOT_FEASIBLE;
@@ -2760,10 +2806,19 @@ lmic_tx_error_t LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t conf
27602806
return 0;
27612807
}
27622808

2763-
// send a message with callback
2809+
// send a message with callback; try to adjust data rate
27642810
lmic_tx_error_t LMIC_sendWithCallback (
27652811
u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed,
27662812
lmic_txmessage_cb_t *pCb, void *pUserData
2813+
) {
2814+
adjustDrForFrame(dlen);
2815+
return LMIC_sendWithCallback_strict(port, data, dlen, confirmed, pCb, pUserData);
2816+
}
2817+
2818+
// send a message with callback; do not adjust datarate
2819+
lmic_tx_error_t LMIC_sendWithCallback_strict (
2820+
u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed,
2821+
lmic_txmessage_cb_t *pCb, void *pUserData
27672822
) {
27682823
lmic_tx_error_t const result = LMIC_setTxData2(port, data, dlen, confirmed);
27692824
if (result == 0) {

src/lmic/lmic.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct lmic_saved_adr_state_s {
178178
typedef struct lmic_saved_adr_state_s lmic_saved_adr_state_t;
179179

180180
// Keep in sync with evdefs.hpp::drChange
181-
enum { DRCHG_SET, DRCHG_NOJACC, DRCHG_NOACK, DRCHG_NOADRACK, DRCHG_NWKCMD };
181+
enum { DRCHG_SET, DRCHG_NOJACC, DRCHG_NOACK, DRCHG_NOADRACK, DRCHG_NWKCMD, DRCHG_FRAMESIZE };
182182
enum { KEEP_TXPOW = -128 };
183183

184184

@@ -614,8 +614,11 @@ void LMIC_init (void);
614614
void LMIC_reset (void);
615615
void LMIC_clrTxData (void);
616616
void LMIC_setTxData (void);
617+
void LMIC_setTxData_strict(void);
617618
lmic_tx_error_t LMIC_setTxData2(u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed);
619+
lmic_tx_error_t LMIC_setTxData2_strict(u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed);
618620
lmic_tx_error_t LMIC_sendWithCallback(u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed, lmic_txmessage_cb_t *pCb, void *pUserData);
621+
lmic_tx_error_t LMIC_sendWithCallback_strict(u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed, lmic_txmessage_cb_t *pCb, void *pUserData);
619622
void LMIC_sendAlive (void);
620623

621624
#if !defined(DISABLE_BEACONS)

src/lmic/lmic_compliance.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static void acSendUplink(void) {
686686

687687
// don't try to send if busy; might be sending echo message.
688688
lmic_tx_error_t const eSend =
689-
LMIC_sendWithCallback(
689+
LMIC_sendWithCallback_strict(
690690
LORAWAN_PORT_COMPLIANCE,
691691
payload, sizeof(payload),
692692
/* confirmed? */
@@ -725,7 +725,7 @@ static void sendUplinkCompleteCb(void *pUserData, int fSuccess) {
725725
static void acSendUplinkBuffer(void) {
726726
// send uplink data.
727727
lmic_tx_error_t const eSend =
728-
LMIC_sendWithCallback(
728+
LMIC_sendWithCallback_strict(
729729
LORAWAN_PORT_COMPLIANCE,
730730
LMIC_Compliance.uplinkMessage, LMIC_Compliance.uplinkSize,
731731
/* confirmed? */ (LMIC_Compliance.fsmFlags & LMIC_COMPLIANCE_FSM_CONFIRM) != 0,

0 commit comments

Comments
 (0)