first test
Dependents: LoRaWAN-lmic-app_tjm
Fork of LMiC by
Diff: lmic.cpp
- Revision:
- 5:464c1f2d6cbb
- Parent:
- 4:85b2b647cb64
- Child:
- 6:eed5fd627a2b
--- a/lmic.cpp Thu Nov 26 17:17:08 2015 +0000 +++ b/lmic.cpp Tue Feb 23 15:34:27 2016 +0000 @@ -11,6 +11,7 @@ //! \file #include "lmic.h" +#include "debug.h" #if !defined(MINRX_SYMS) #define MINRX_SYMS 5 @@ -133,7 +134,9 @@ // ================================================================================ // BEG AES -static void micB0 (u4_t devaddr, u4_t seqno, int dndir, int len) { +static void micB0 (u4_t devaddr, u4_t seqno, int dndir, int len) +{ + debug("micB0 enter\r\n"); os_clearMem(AESaux,16); AESaux[0] = 0x49; AESaux[5] = dndir?1:0; @@ -143,14 +146,18 @@ } -static int aes_verifyMic (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t pdu, int len) { +static int aes_verifyMic (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t pdu, int len) +{ + debug("aes_verifyMic enter\r\n"); micB0(devaddr, seqno, dndir, len); os_copyMem(AESkey,key,16); return os_aes(AES_MIC, pdu, len) == os_rmsbf4(pdu+len); } -static void aes_appendMic (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t pdu, int len) { +static void aes_appendMic (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t pdu, int len) +{ + debug("aes_appendMic enter\r\n"); micB0(devaddr, seqno, dndir, len); os_copyMem(AESkey,key,16); // MSB because of internal structure of AES @@ -158,26 +165,34 @@ } -static void aes_appendMic0 (xref2u1_t pdu, int len) { - os_getDevKey(AESkey); +static void aes_appendMic0 (xref2u1_t pdu, int len) +{ + debug("aes_appendMic0 enter\r\n"); + os_getDevKey(AESkey); os_wmsbf4(pdu+len, os_aes(AES_MIC|AES_MICNOAUX, pdu, len)); // MSB because of internal structure of AES } -static int aes_verifyMic0 (xref2u1_t pdu, int len) { +static int aes_verifyMic0 (xref2u1_t pdu, int len) +{ + debug("aes_verifyMic0 enter\r\n"); os_getDevKey(AESkey); return os_aes(AES_MIC|AES_MICNOAUX, pdu, len) == os_rmsbf4(pdu+len); } -static void aes_encrypt (xref2u1_t pdu, int len) { +static void aes_encrypt (xref2u1_t pdu, int len) +{ + debug("aes_encrypt enter\r\n"); os_getDevKey(AESkey); os_aes(AES_ENC, pdu, len); } -static void aes_cipher (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t payload, int len) { - if( len <= 0 ) +static void aes_cipher (xref2cu1_t key, u4_t devaddr, u4_t seqno, int dndir, xref2u1_t payload, int len) +{ + debug("aes_cipher enter\r\n"); + if( len <= 0 ) return; os_clearMem(AESaux, 16); AESaux[0] = AESaux[15] = 1; // mode=cipher / dir=down / block counter=1 @@ -189,7 +204,9 @@ } -static void aes_sessKeys (u2_t devnonce, xref2cu1_t artnonce, xref2u1_t nwkkey, xref2u1_t artkey) { +static void aes_sessKeys (u2_t devnonce, xref2cu1_t artnonce, xref2u1_t nwkkey, xref2u1_t artkey) +{ + debug("aes_sessKeys enter\r\n"); os_clearMem(nwkkey, 16); nwkkey[0] = 0x01; os_copyMem(nwkkey+1, artnonce, LEN_ARTNONCE+LEN_NETID); @@ -273,12 +290,16 @@ { 141-141, 141-138, 141-135 } // SF12 }; -int getSensitivity (rps_t rps) { +int getSensitivity (rps_t rps) +{ + debug("getSensitivity enter\r\n"); return -141 + SENSITIVITY[getSf(rps)][getBw(rps)]; } -ostime_t calcAirTime (rps_t rps, u1_t plen) { - u1_t bw = getBw(rps); // 0,1,2 = 125,250,500kHz +ostime_t calcAirTime (rps_t rps, u1_t plen) + { + debug("calcAirTime enter\r\n"); + u1_t bw = getBw(rps); // 0,1,2 = 125,250,500kHz u1_t sf = getSf(rps); // 0=FSK, 1..6 = SF7..12 if( sf == FSK ) { return (plen+/*preamble*/5+/*syncword*/3+/*len*/1+/*crc*/2) * /*bits/byte*/8 @@ -392,7 +413,9 @@ }; -static ostime_t calcRxWindow (u1_t secs, dr_t dr) { +static ostime_t calcRxWindow (u1_t secs, dr_t dr) +{ + debug("calcRxWindow enter\r\n"); ostime_t rxoff, err; if( secs==0 ) { // aka 128 secs (next becaon) @@ -412,7 +435,9 @@ // Setup beacon RX parameters assuming we have an error of ms (aka +/-(ms/2)) -static void calcBcnRxWindowFromMillis (u1_t ms, bit_t ini) { +static void calcBcnRxWindowFromMillis (u1_t ms, bit_t ini) +{ + debug("calcBcnRxWindowFromMillis enter\r\n"); if( ini ) { LMIC.drift = 0; LMIC.maxDriftDiff = 0; @@ -426,7 +451,9 @@ // Setup scheduled RX window (ping/multicast slot) -static void rxschedInit (xref2rxsched_t rxsched) { +static void rxschedInit (xref2rxsched_t rxsched) +{ + debug("rxschedInit enter\r\n"); os_clearMem(AESkey,16); os_clearMem(LMIC.frame+8,8); os_wlsbf4(LMIC.frame, LMIC.bcninfo.time); @@ -443,7 +470,9 @@ } -static bit_t rxschedNext (xref2rxsched_t rxsched, ostime_t cando) { +static bit_t rxschedNext (xref2rxsched_t rxsched, ostime_t cando) +{ + debug("rxschedNext enter\r\n"); again: if( rxsched->rxtime - cando >= 0 ) return 1; @@ -461,7 +490,9 @@ } -static ostime_t rndDelay (u1_t secSpan) { +static ostime_t rndDelay (u1_t secSpan) +{ + debug("rndDelay enter\r\n"); u2_t r = os_getRndU2(); ostime_t delay = r; if( delay > OSTICKS_PER_SEC ) @@ -472,7 +503,9 @@ } -static void txDelay (ostime_t reftime, u1_t secSpan) { +static void txDelay (ostime_t reftime, u1_t secSpan) +{ + debug("txDelay enter\r\n"); reftime += rndDelay(secSpan); if( LMIC.globalDutyRate == 0 || (reftime - LMIC.globalDutyAvail) > 0 ) { LMIC.globalDutyAvail = reftime; @@ -481,7 +514,9 @@ } -static void setDrJoin (u1_t reason, u1_t dr) { +static void setDrJoin (u1_t reason, u1_t dr) +{ + debug("setDrJoin enter\r\n"); EV(drChange, INFO, (e_.reason = reason, e_.deveui = MAIN::CDEV->getEui(), e_.dr = dr|DR_PAGE, @@ -493,8 +528,10 @@ } -static void setDrTxpow (u1_t reason, u1_t dr, s1_t pow) { - EV(drChange, INFO, (e_.reason = reason, +static void setDrTxpow (u1_t reason, u1_t dr, s1_t pow) +{ + debug("setDrTxpow enter\r\n"); + EV(drChange, INFO, (e_.reason = reason, e_.deveui = MAIN::CDEV->getEui(), e_.dr = dr|DR_PAGE, e_.txpow = pow, @@ -514,12 +551,16 @@ } -void LMIC_stopPingable (void) { - LMIC.opmode &= ~(OP_PINGABLE|OP_PINGINI); +void LMIC_stopPingable (void) +{ + debug("LMIC_stopPingable enter\r\n"); + LMIC.opmode &= ~(OP_PINGABLE|OP_PINGINI); } -void LMIC_setPingable (u1_t intvExp) { +void LMIC_setPingable (u1_t intvExp) +{ + debug("LMIC_setPingable enter\r\n"); // Change setting LMIC.ping.intvExp = (intvExp & 0x7); LMIC.opmode |= OP_PINGABLE; @@ -736,6 +777,7 @@ static void initDefaultChannels (void) { + debug("initDefaultChannels enter\r\n"); #ifdef CHNL_HYBRID int idx = CHNL_HYBRID >> 1; LMIC.channelMap[0] = 0x0000; @@ -761,7 +803,9 @@ LMIC.adrTxPow = LMIC.txpow_limit; } -static u4_t convFreq (xref2u1_t ptr) { +static u4_t convFreq (xref2u1_t ptr) +{ + debug("convFreq enter\r\n"); u4_t freq = (os_rlsbf4(ptr-1) >> 8) * 100; if( freq < US915_FREQ_MIN || freq > US915_FREQ_MAX ) freq = 0; @@ -769,8 +813,10 @@ } -bit_t LMIC_setupChannel (u1_t chidx, u4_t freq, u2_t drmap, s1_t band) { - if( chidx < 72 || chidx >= 72+MAX_XCHANNELS ) +bit_t LMIC_setupChannel (u1_t chidx, u4_t freq, u2_t drmap, s1_t band) +{ + debug("LMIC_setupChannel enter\r\n"); + if( chidx < 72 || chidx >= 72+MAX_XCHANNELS ) return 0; // channels 0..71 are hardwired chidx -= 72; LMIC.xchFreq[chidx] = freq; @@ -779,13 +825,18 @@ return 1; } -void LMIC_disableChannel (u1_t channel) { - if( channel < 72+MAX_XCHANNELS ) +void LMIC_disableChannel (u1_t channel) +{ + debug("LMIC_disableChannel enter\r\n"); + if( channel < 72+MAX_XCHANNELS ) LMIC.channelMap[channel/4] &= ~(1<<(channel&0xF)); } -static u1_t mapChannels (u1_t chpage, u2_t chmap) { - if( chpage == MCMD_LADR_CHP_125ON || chpage == MCMD_LADR_CHP_125OFF ) { +static u1_t mapChannels (u1_t chpage, u2_t chmap) +{ + debug("mapChannels enter\r\n"); + if( chpage == MCMD_LADR_CHP_125ON || chpage == MCMD_LADR_CHP_125OFF ) + { u2_t en125 = chpage == MCMD_LADR_CHP_125ON ? 0xFFFF : 0x0000; for( u1_t u=0; u<4; u++ ) LMIC.channelMap[u] = en125; @@ -798,7 +849,9 @@ return 1; } -static void updateTx (ostime_t txbeg) { +static void updateTx (ostime_t txbeg) +{ + debug("updateTx enter\r\n"); u1_t chnl = LMIC.txChnl; #ifdef JOIN_REQ_DEBUG printf("chnl%d ", chnl); @@ -857,7 +910,9 @@ // US does not have duty cycling - return now as earliest TX time #define nextTx(now) (_nextTx(),(now)) -static void _nextTx (void) { +static void _nextTx (void) +{ + debug("_nextTx enter\r\n"); u1_t prev_ch = LMIC.txChnl; u1_t tries = 0; u1_t en_cnt; @@ -927,7 +982,9 @@ LMIC.rps = dndr2rps(LMIC.dndr); \ } -static void initJoinLoop (void) { +static void initJoinLoop (void) +{ +debug("initJoinLoop enter\r\n"); LMIC.chRnd = 0; #ifdef CHNL_HYBRID LMIC.joinBlockChnl = 0; @@ -945,8 +1002,10 @@ setDrJoin(DRCHG_SET, DR_SF7); } -static ostime_t nextJoinState (void) { - u1_t failed = 0; +static ostime_t nextJoinState (void) +{ + debug("nextJoinState enter\r\n"); + u1_t failed = 0; if( LMIC.datarate == DR_SF8C ) { // attempted 500khz channel, try 125khz channel in next block @@ -987,12 +1046,16 @@ #endif -static void runEngineUpdate (xref2osjob_t osjob) { +static void runEngineUpdate (xref2osjob_t osjob) +{ + debug("runEngineUpdate enter\r\n"); engineUpdate(); } -static void reportEvent (ev_t ev) { +static void reportEvent (ev_t ev) +{ + debug("reportEvent enter %d\r\n",ev); EV(devCond, INFO, (e_.reason = EV::devCond_t::LMIC_EV, e_.eui = MAIN::CDEV->getEui(), e_.info = ev)); @@ -2180,6 +2243,7 @@ void LMIC_init (void) { + debug("LMIC_init enter\r\n"); LMIC.opmode = OP_SHUTDOWN; }