LMiC LoRa Semtech + Nucleo version sending every 10secs

Dependents:   LoRaWAN-test-10secs

Fork of LMiC by Alcatel-Lucent IoT Development

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lmic.h Source File

lmic.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * Copyright (c) 2014-2015 IBM Corporation.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the Eclipse Public License v1.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.eclipse.org/legal/epl-v10.html
00007  *
00008  * Contributors:
00009  *    IBM Zurich Research Lab - initial API, implementation and documentation
00010  *******************************************************************************/
00011 
00012 //! @file
00013 //! @brief LMIC API
00014 
00015 #ifndef _lmic_h_
00016 #define _lmic_h_
00017 
00018 // MBED compiler options
00019 #define CFG_eu868                                   1
00020 //#define CFG_us915                                   0
00021 
00022 #define USE_SMTC_RADIO_DRIVER                       1
00023 
00024 //#define CFG_sx1272_radio                            0
00025 #define CFG_sx1276_radio                            1
00026 // End MBED compiler options
00027 
00028 #include "oslmic.h "
00029 #include "lorabase.h"
00030 
00031 // LMIC version
00032 #define LMIC_VERSION_MAJOR 1
00033 #define LMIC_VERSION_MINOR 4
00034 #define LMIC_VERSION_BUILD 1426605786
00035 
00036 enum { MAX_FRAME_LEN      =  64 };   //!< Library cap on max frame length
00037 //enum { MAX_FRAME_LEN      =  96 };   //!< Library cap on max frame length
00038 enum { TXCONF_ATTEMPTS    =   8 };   //!< Transmit attempts for confirmed frames
00039 enum { MAX_MISSED_BCNS    =  20 };   // threshold for triggering rejoin requests
00040 enum { MAX_RXSYMS         = 100 };   // stop tracking beacon beyond this
00041 
00042 enum { LINK_CHECK_CONT    =   6 ,    // continue with this after reported dead link
00043        LINK_CHECK_DEAD    =  12 ,    // after this UP frames and no response from NWK assume link is dead
00044        LINK_CHECK_INIT    = -12 ,    // UP frame count until we inc datarate
00045        LINK_CHECK_OFF     =-128 };   // link check disabled
00046 
00047 /*enum { TIME_RESYNC        =  6*128 }; // secs
00048 enum { TXRX_GUARD_ms      =  6000 };  // msecs - don't start TX-RX transaction before beacon
00049 enum { JOIN_GUARD_ms      =  9000 };  // msecs - don't start Join Req/Acc transaction before beacon
00050 enum { TXRX_BCNEXT_secs   =     2 };  // secs - earliest start after beacon time
00051 enum { RETRY_PERIOD_secs  =     3 };  // secs - random period for retrying a confirmed send*/
00052 
00053 //PANY
00054 enum { TIME_RESYNC        =  6*128 }; // secs
00055 enum { TXRX_GUARD_ms      =  1000 };  // msecs - don't start TX-RX transaction before beacon
00056 enum { JOIN_GUARD_ms      =  1000 };  // msecs - don't start Join Req/Acc transaction before beacon
00057 enum { TXRX_BCNEXT_secs   =     1 };  // secs - earliest start after beacon time
00058 enum { RETRY_PERIOD_secs  =     1 };  // secs - random period for retrying a confirmed send
00059 //PANY
00060 #if defined(CFG_eu868) // EU868 spectrum ====================================================
00061 
00062 enum { MAX_CHANNELS = 16 };      //!< Max supported channels
00063 enum { MAX_BANDS    =  4 };
00064 
00065 enum { LIMIT_CHANNELS = (1<<4) };   // EU868 will never have more channels
00066 //! \internal
00067 struct band_t {
00068     u2_t     txcap;     // duty cycle limitation: 1/txcap
00069     s1_t     txpow;     // maximum TX power
00070     u1_t     lastchnl;  // last used channel
00071     ostime_t avail;     // channel is blocked until this time
00072 };
00073 TYPEDEF_xref2band_t; //!< \internal
00074 
00075 #elif defined(CFG_us915)  // US915 spectrum =================================================
00076 
00077 enum { MAX_XCHANNELS = 2 };      // extra channels in RAM, channels 0-71 are immutable 
00078 enum { MAX_TXPOW_125kHz = 30 };
00079 
00080 #endif // ==========================================================================
00081 
00082 // Keep in sync with evdefs.hpp::drChange
00083 enum { DRCHG_SET, DRCHG_NOJACC, DRCHG_NOACK, DRCHG_NOADRACK, DRCHG_NWKCMD };
00084 enum { KEEP_TXPOW = -128 };
00085 
00086 
00087 //! \internal
00088 struct rxsched_t {
00089     u1_t     dr;
00090     u1_t     intvExp;   // 0..7
00091     u1_t     slot;      // runs from 0 to 128
00092     u1_t     rxsyms;
00093     ostime_t rxbase;
00094     ostime_t rxtime;    // start of next spot
00095     u4_t     freq;
00096 };
00097 TYPEDEF_xref2rxsched_t;  //!< \internal
00098 
00099 
00100 //! Parsing and tracking states of beacons.
00101 enum { BCN_NONE    = 0x00,   //!< No beacon received
00102        BCN_PARTIAL = 0x01,   //!< Only first (common) part could be decoded (info,lat,lon invalid/previous)
00103        BCN_FULL    = 0x02,   //!< Full beacon decoded
00104        BCN_NODRIFT = 0x04,   //!< No drift value measured yet
00105        BCN_NODDIFF = 0x08 }; //!< No differential drift measured yet
00106 //! Information about the last and previous beacons.
00107 struct bcninfo_t {
00108     ostime_t txtime;  //!< Time when the beacon was sent
00109     s1_t     rssi;    //!< Adjusted RSSI value of last received beacon
00110     s1_t     snr;     //!< Scaled SNR value of last received beacon
00111     u1_t     flags;   //!< Last beacon reception and tracking states. See BCN_* values.
00112     u4_t     time;    //!< GPS time in seconds of last beacon (received or surrogate)
00113     //
00114     u1_t     info;    //!< Info field of last beacon (valid only if BCN_FULL set)
00115     s4_t     lat;     //!< Lat field of last beacon (valid only if BCN_FULL set)
00116     s4_t     lon;     //!< Lon field of last beacon (valid only if BCN_FULL set)
00117 };
00118 
00119 // purpose of receive window - lmic_t.rxState
00120 enum { RADIO_RST=0, RADIO_TX=1, RADIO_RX=2, RADIO_RXON=3 };
00121 // Netid values /  lmic_t.netid
00122 enum { NETID_NONE=(int)~0U, NETID_MASK=(int)0xFFFFFF };
00123 // MAC operation modes (lmic_t.opmode).
00124 enum { OP_NONE     = 0x0000,
00125        OP_SCAN     = 0x0001, // radio scan to find a beacon
00126        OP_TRACK    = 0x0002, // track my networks beacon (netid)
00127        OP_JOINING  = 0x0004, // device joining in progress (blocks other activities)
00128        OP_TXDATA   = 0x0008, // TX user data (buffered in pendTxData)
00129        OP_POLL     = 0x0010, // send empty UP frame to ACK confirmed DN/fetch more DN data
00130        OP_REJOIN   = 0x0020, // occasionally send JOIN REQUEST
00131        OP_SHUTDOWN = 0x0040, // prevent MAC from doing anything
00132        OP_TXRXPEND = 0x0080, // TX/RX transaction pending
00133        OP_RNDTX    = 0x0100, // prevent TX lining up after a beacon
00134        OP_PINGINI  = 0x0200, // pingable is initialized and scheduling active
00135        OP_PINGABLE = 0x0400, // we're pingable
00136        OP_NEXTCHNL = 0x0800, // find a new channel
00137        OP_LINKDEAD = 0x1000, // link was reported as dead
00138        OP_TESTMODE = 0x2000, // developer test mode
00139 };
00140 // TX-RX transaction flags - report back to user
00141 enum { TXRX_ACK    = 0x80,   // confirmed UP frame was acked
00142        TXRX_NACK   = 0x40,   // confirmed UP frame was not acked
00143        TXRX_NOPORT = 0x20,   // set if a frame with a port was RXed, clr if no frame/no port
00144        TXRX_PORT   = 0x10,   // set if a frame with a port was RXed, LMIC.frame[LMIC.dataBeg-1] => port
00145        TXRX_DNW1   = 0x01,   // received in 1st DN slot
00146        TXRX_DNW2   = 0x02,   // received in 2dn DN slot
00147        TXRX_PING   = 0x04 }; // received in a scheduled RX slot
00148 // Event types for event callback
00149 enum _ev_t { EV_SCAN_TIMEOUT=1, EV_BEACON_FOUND,
00150              EV_BEACON_MISSED, EV_BEACON_TRACKED, EV_JOINING,
00151              EV_JOINED, EV_RFU1, EV_JOIN_FAILED, EV_REJOIN_FAILED,
00152              EV_TXCOMPLETE, EV_LOST_TSYNC, EV_RESET,
00153              EV_RXCOMPLETE, EV_LINK_DEAD, EV_LINK_ALIVE };
00154 typedef enum _ev_t ev_t;
00155 
00156 
00157 struct lmic_t {
00158     // Radio settings TX/RX (also accessed by HAL)
00159     ostime_t    txend;
00160     ostime_t    rxtime;
00161     u4_t        freq;
00162     s1_t        rssi;
00163     s1_t        snr;
00164     rps_t       rps;
00165     u1_t        rxsyms;
00166     u1_t        dndr;
00167     s1_t        txpow;     // dBm
00168 
00169     osjob_t     osjob;
00170 
00171     // Channel scheduling
00172 #if defined(CFG_eu868)
00173     band_t      bands[MAX_BANDS];
00174     u4_t        channelFreq[MAX_CHANNELS];
00175     u2_t        channelDrMap[MAX_CHANNELS];
00176     u2_t        channelMap;
00177 #elif defined(CFG_us915)
00178     u4_t        xchFreq[MAX_XCHANNELS];    // extra channel frequencies (if device is behind a repeater)
00179     u2_t        xchDrMap[MAX_XCHANNELS];   // extra channel datarate ranges  ---XXX: ditto
00180     u2_t        channelMap[(72+MAX_XCHANNELS+15)/16];  // enabled bits
00181     u2_t        chRnd;        // channel randomizer
00182 #endif
00183     u1_t        txChnl;          // channel for next TX
00184     u1_t        globalDutyRate;  // max rate: 1/2^k
00185     ostime_t    globalDutyAvail; // time device can send again
00186     
00187     u4_t        netid;        // current network id (~0 - none)
00188     u2_t        opmode;
00189     u1_t        upRepeat;     // configured up repeat
00190     s1_t        adrTxPow;     // ADR adjusted TX power
00191     u1_t        datarate;     // current data rate
00192     u1_t        errcr;        // error coding rate (used for TX only)
00193     u1_t        rejoinCnt;    // adjustment for rejoin datarate
00194     s2_t        drift;        // last measured drift
00195     s2_t        lastDriftDiff;
00196     s2_t        maxDriftDiff;
00197 
00198     u1_t        pendTxPort;
00199     u1_t        pendTxConf;   // confirmed data
00200     u1_t        pendTxLen;    // +0x80 = confirmed
00201     u1_t        pendTxData[MAX_LEN_PAYLOAD];
00202 
00203     u2_t        devNonce;     // last generated nonce
00204     u1_t        nwkKey[16];   // network session key
00205     u1_t        artKey[16];   // application router session key
00206     devaddr_t   devaddr;
00207     u4_t        seqnoDn;      // device level down stream seqno
00208     u4_t        seqnoUp;
00209 
00210     u1_t        dnConf;       // dn frame confirm pending: LORA::FCT_ACK or 0
00211     s1_t        adrAckReq;    // counter until we reset data rate (0=off)
00212     u1_t        adrChanged;
00213 
00214     u1_t        margin;
00215     bit_t       ladrAns;      // link adr adapt answer pending
00216     bit_t       devsAns;      // device status answer pending
00217     u1_t        adrEnabled;
00218     u1_t        moreData;     // NWK has more data pending
00219     bit_t       dutyCapAns;   // have to ACK duty cycle settings
00220     u1_t        snchAns;      // answer set new channel
00221     // 2nd RX window (after up stream)
00222     u1_t        dn2Dr;
00223     u4_t        dn2Freq;
00224     u1_t        dn2Ans;       // 0=no answer pend, 0x80+ACKs
00225 
00226     // Class B state
00227     u1_t        missedBcns;   // unable to track last N beacons
00228     u1_t        bcninfoTries; // how often to try (scan mode only)
00229     u1_t        pingSetAns;   // answer set cmd and ACK bits
00230     rxsched_t   ping;         // pingable setup
00231 
00232     // Public part of MAC state
00233     u1_t        txCnt;
00234     u1_t        txrxFlags;  // transaction flags (TX-RX combo)
00235     u1_t        dataBeg;    // 0 or start of data (dataBeg-1 is port)
00236     u1_t        dataLen;    // 0 no data or zero length data, >0 byte count of data
00237     u1_t        frame[MAX_LEN_FRAME];
00238 
00239     u1_t        bcnChnl;
00240     u1_t        bcnRxsyms;    // 
00241     ostime_t    bcnRxtime;
00242     bcninfo_t   bcninfo;      // Last received beacon info
00243 };
00244 //! \var struct lmic_t LMIC
00245 //! The state of LMIC MAC layer is encapsulated in this variable.
00246 DECLARE_LMIC; //!< \internal
00247 
00248 //! Construct a bit map of allowed datarates from drlo to drhi (both included). 
00249 #define DR_RANGE_MAP(drlo,drhi) (((u2_t)0xFFFF<<(drlo)) & ((u2_t)0xFFFF>>(15-(drhi))))
00250 #if defined(CFG_eu868)
00251 enum { BAND_MILLI=0, BAND_CENTI=1, BAND_DECI=2, BAND_AUX=3 };
00252 bit_t LMIC_setupBand (u1_t bandidx, s1_t txpow, u2_t txcap);
00253 #endif
00254 bit_t LMIC_setupChannel (u1_t channel, u4_t freq, u2_t drmap, s1_t band);
00255 void  LMIC_disableChannel (u1_t channel);
00256 
00257 void  LMIC_setDrTxpow   (dr_t dr, s1_t txpow);  // set default/start DR/txpow
00258 void  LMIC_setAdrMode   (bit_t enabled);        // set ADR mode (if mobile turn off)
00259 bit_t LMIC_startJoining (void);
00260 
00261 void  LMIC_shutdown     (void);
00262 void  LMIC_init         (void);
00263 void  LMIC_reset        (void);
00264 void  LMIC_clrTxData    (void);
00265 void  LMIC_setTxData    (void);
00266 int   LMIC_setTxData2   (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed);
00267 void  LMIC_sendAlive    (void);
00268 
00269 bit_t LMIC_enableTracking  (u1_t tryBcnInfo);
00270 void  LMIC_disableTracking (void);
00271 
00272 void  LMIC_stopPingable  (void);
00273 void  LMIC_setPingable   (u1_t intvExp);
00274 void  LMIC_tryRejoin     (void);
00275 
00276 void LMIC_setSession (u4_t netid, devaddr_t devaddr, xref2u1_t nwkKey, xref2u1_t artKey);
00277 void LMIC_setLinkCheckMode (bit_t enabled);
00278 
00279 // Special APIs - for development or testing
00280 // !!!See implementation for caveats!!!
00281 
00282 #endif // _lmic_h_