MultiTech / libxDot-dev-mbed5-deprecated

Dependents:   Dot-Examples Dot-AT-Firmware Dot-Examples TEST_FF1705 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Lora.h Source File

Lora.h

00001 /**   __  ___     ____  _    ______        __     ____         __                  ____
00002  *   /  |/  /_ __/ / /_(_)__/_  __/__ ____/ /    / __/_ _____ / /____ __ _  ___   /  _/__  ____
00003  *  / /|_/ / // / / __/ /___// / / -_) __/ _ \  _\ \/ // (_-</ __/ -_)  ' \(_-<  _/ // _ \/ __/  __
00004  * /_/  /_/\_,_/_/\__/_/    /_/  \__/\__/_//_/ /___/\_, /___/\__/\__/_/_/_/___/ /___/_//_/\__/  /_/
00005  * Copyright (C) 2015 by Multi-Tech Systems        /___/
00006  *
00007  *
00008  * @author Jason Reiss
00009  * @date   10-31-2015
00010  * @brief  lora namespace defines global settings, structures and enums for the lora library
00011  *
00012  * @details
00013  *
00014  *
00015  *
00016  */
00017 
00018 #ifndef __LORA_H__
00019 #define __LORA_H__
00020 
00021 #include "mbed.h"
00022 #include <assert.h>
00023 #include "MTSLog.h"
00024 //#include <cstring>
00025 #include <inttypes.h>
00026 
00027 namespace lora {
00028 
00029     /**
00030      * Frequency bandwidth of a Datarate, higher bandwidth gives higher datarate
00031      */
00032     enum Bandwidth {
00033         BW_125,
00034         BW_250,
00035         BW_500,
00036         BW_FSK = 50
00037     };
00038 
00039     /**
00040      * Spreading factor of a Datarate, lower spreading factor gives higher datarate
00041      */
00042     enum SpreadingFactors {
00043         SF_6 = 6,
00044         SF_7,
00045         SF_8,
00046         SF_9,
00047         SF_10,
00048         SF_11,
00049         SF_12,
00050         SF_FSK,
00051         SF_INVALID
00052     };
00053 
00054     /**
00055      * Datarates for use with ChannelPlan
00056      */
00057     enum Datarates {
00058         DR_0 = 0,
00059         DR_1,
00060         DR_2,
00061         DR_3,
00062         DR_4,
00063         DR_5,
00064         DR_6,
00065         DR_7,
00066         DR_8,
00067         DR_9,
00068         DR_10,
00069         DR_11,
00070         DR_12,
00071         DR_13,
00072         DR_14,
00073         DR_15
00074     };
00075 
00076     const uint8_t MIN_DATARATE = (uint8_t) DR_0;             //!< Minimum datarate
00077 
00078 
00079     const uint8_t MAX_PHY_PACKET_SIZE = 255;                    //!< Maximum size for a packet
00080     const uint8_t MAX_APPS = 8;                                 //!< Maximum number of apps sessions to save
00081     const uint8_t MAX_MULTICAST_SESSIONS = 8;                   //!< Maximum number of multicast sessions to save
00082     const uint8_t EUI_SIZE = 8;                                 //!< Number of bytes in an EUI
00083     const uint8_t KEY_SIZE = 16;                                //!< Number of bytes in an AES key
00084 
00085     const uint8_t DEFAULT_NUM_CHANNELS = 16;                    //!< Default number of channels in a plan
00086     const uint8_t DEFAULT_RX1_DR_OFFSET = 0;                    //!< Default datarate offset for first rx window
00087     const uint8_t DEFAULT_RX2_DATARATE = 0;                     //!< Default datarate for second rx window
00088     const uint8_t DEFAULT_TX_POWER = 14;                        //!< Default transmit power
00089     const uint8_t DEFAULT_CODE_RATE = 1;                        //!< Default coding rate 1:4/5, 2:4/6, 3:4/7, 4:4/8
00090     const uint8_t DEFAULT_PREAMBLE_LEN = 8;                     //!< Default preamble length
00091 
00092     const int32_t MAX_FCNT_GAP = 16384;                         //!< Maximum allowed gap in sequence numbers before roll-over
00093 
00094     const uint16_t PRIVATE_JOIN_DELAY = 1000;                   //!< Default join delay used for private network
00095     const uint16_t PUBLIC_JOIN_DELAY = 5000;                    //!< Default join delay used for public network
00096     const uint16_t DEFAULT_JOIN_DELAY = PRIVATE_JOIN_DELAY;     //!< Default join delay1
00097     const uint16_t DEFAULT_RX_DELAY = 1000;                     //!< Default delay for first receive window
00098     const uint16_t DEFAULT_RX_TIMEOUT = 3000;                   //!< Default timeout for receive windows
00099 
00100     const uint8_t HI_DR_SYMBOL_TIMEOUT = 12;                    //!< Symbol timeout for receive at datarate with SF < 11
00101     const uint8_t LO_DR_SYMBOL_TIMEOUT = 8;                     //!< Symbol timeout for receive at datarate with SF > 10
00102 
00103     const uint16_t RX2_DELAY_OFFSET = 1000;                     //!< Delay between first and second window
00104     const uint16_t RXC_OFFSET = 50;                             //!< Time between end of RXC after TX and RX1
00105 
00106     const uint16_t BEACON_PREAMBLE_LENGTH = 10U;                //!< Beacon preamble length
00107     const uint16_t DEFAULT_BEACON_PERIOD = 128U;                //!< Default period of the beacon (in seconds)
00108     const uint16_t PING_SLOT_LENGTH = 30U;                      //!< Duration of each class B ping slot (in milliseconds)
00109     const uint16_t BEACON_RESERVED_TIME = 2120U;                //!< Time reserved for beacon broadcast (in milliseconds)
00110     const uint16_t BEACON_GUARD_TIME = 3000U;                   //!< Guard time before beacon transmission where no ping slots can be scheduled (in milliseconds)
00111     const uint32_t MAX_BEACONLESS_OP_TIME = 7200U;              //!< Maximum time to operate in class B since last beacon received (in seconds)
00112     const uint16_t MAX_CLASS_B_WINDOW_GROWTH = 3U;              //!< Maximum window growth factor for beacons and ping slots in beacon-less operation
00113     const uint16_t DEFAULT_PING_NB = 1U;                        //!< Default number of ping slots per beacon interval
00114 
00115     const int16_t DEFAULT_FREE_CHAN_RSSI_THRESHOLD = -90;       //!< Threshold for channel activity detection (CAD) dBm
00116 
00117     const uint8_t CHAN_MASK_SIZE = 16;                          //!< Number of bits in a channel mask
00118     const uint8_t COMMANDS_BUFFER_SIZE = 15;                    //!< Size of Mac Command buffer
00119 
00120     const uint8_t PKT_HEADER = 0;                               //!< Index to packet mHdr field
00121     const uint8_t PKT_ADDRESS = 1;                              //!< Index to first byte of packet address field
00122     const uint8_t PKT_FRAME_CONTROL = PKT_ADDRESS + 4;          //!< Index to packet fCtrl field @see UplinkControl
00123     const uint8_t PKT_FRAME_COUNTER = PKT_FRAME_CONTROL + 1;    //!< Index to packet frame counter field
00124     const uint8_t PKT_OPTIONS_START = PKT_FRAME_COUNTER + 2;    //!< Index to start of optional mac commands
00125 
00126     const uint8_t PKT_JOIN_APP_NONCE = 1;                       //!< Index to application nonce in Join Accept message
00127     const uint8_t PKT_JOIN_NETWORK_ID = 4;                      //!< Index to network id in Join Accept message
00128     const uint8_t PKT_JOIN_NETWORK_ADDRESS = 7;                 //!< Index to network address in Join Accept message
00129     const uint8_t PKT_JOIN_DL_SETTINGS = 11;                    //!< Index to downlink settings in Join Accept message
00130     const uint8_t PKT_JOIN_RX_DELAY = 12;                       //!< Index to rx delay in Join Accept message
00131 
00132     const uint8_t DEFAULT_ADR_ACK_LIMIT = 64;                   //!< Number of packets without ADR ACK Request
00133     const uint8_t DEFAULT_ADR_ACK_DELAY = 32;                   //!< Number of packets to expect ADR ACK Response within
00134 
00135     const uint16_t ACK_TIMEOUT = 2000;                          //!< Base millisecond timeout to resend after missed ACK
00136     const uint16_t ACK_TIMEOUT_RND = 1000;                      //!< Random millisecond adjustment to resend after missed ACK
00137 
00138     const uint8_t FRAME_OVERHEAD = 13;                          //!< Bytes of network info overhead in a frame
00139 
00140     const uint16_t MAX_OFF_AIR_WAIT = 5000U;                    //!< Max time in ms to block for a duty cycle restriction to expire before erroring out
00141     /**
00142      * Settings for type of network
00143      *
00144      * PRIVATE_MTS - Sync Word 0x12, US/AU Downlink frequencies per Frequency Sub Band
00145      * PUBLIC_LORAWAN - Sync Word 0x34
00146      * PRIVATE_LORAWAN - Sync Word 0x12
00147      * PEER_TO_PEER - Sync Word 0x56 used for Dot to Dot communication
00148      *
00149      * Join Delay window settings are independent of Network Type setting
00150      */
00151     enum NetworkType {
00152         PRIVATE_MTS = 0,
00153         PUBLIC_LORAWAN = 1,
00154         PRIVATE_LORAWAN = 2,
00155         PEER_TO_PEER = 4
00156     };
00157 
00158     /**
00159      * Enum for on/off settings
00160      */
00161     enum Enabled {
00162         OFF = 0,
00163         ON = 1
00164     };
00165 
00166     /**
00167      * Return status of mac functions
00168      */
00169     enum MacStatus {
00170         LORA_OK = 0,
00171         LORA_ERROR = 1,
00172         LORA_JOIN_ERROR = 2,
00173         LORA_SEND_ERROR = 3,
00174         LORA_MIC_ERROR = 4,
00175         LORA_ADDRESS_ERROR = 5,
00176         LORA_NO_CHANS_ENABLED = 6,
00177         LORA_COMMAND_BUFFER_FULL = 7,
00178         LORA_UNKNOWN_MAC_COMMAND = 8,
00179         LORA_ADR_OFF = 9,
00180         LORA_BUSY = 10,
00181         LORA_LINK_BUSY = 11,
00182         LORA_RADIO_BUSY = 12,
00183         LORA_BUFFER_FULL = 13,
00184         LORA_JOIN_BACKOFF = 14,
00185         LORA_NO_FREE_CHAN = 15,
00186         LORA_AGGREGATED_DUTY_CYCLE = 16,
00187         LORA_MAC_COMMAND_ERROR = 17,
00188         LORA_MAX_PAYLOAD_EXCEEDED = 18,
00189         LORA_LBT_CHANNEL_BUSY = 19
00190     };
00191 
00192     /**
00193      * State for Link
00194      */
00195     enum LinkState {
00196         LINK_IDLE = 0,  //!< Link ready to send or receive
00197         LINK_TX,        //!< Link is busy sending
00198         LINK_ACK_TX,    //!< Link is busy resending after missed ACK
00199         LINK_REP_TX,    //!< Link is busy repeating
00200         LINK_RX,        //!< Link has receive window open
00201         LINK_RX1,       //!< Link has first received window open
00202         LINK_RX2,       //!< Link has second received window open
00203         LINK_RXC,       //!< Link has class C received window open
00204         LINK_P2P,       //!< Link is busy sending
00205     };
00206 
00207     /**
00208      * State for MAC
00209      */
00210     enum MacState {
00211         MAC_IDLE,
00212         MAC_RX1,
00213         MAC_RX2,
00214         MAC_RXC,
00215         MAC_TX,
00216         MAC_JOIN
00217     };
00218 
00219     /**
00220      * Operation class for device
00221      */
00222     enum MoteClass {
00223         CLASS_A = 0x00, //!< Device can only receive in windows opened after a transmit
00224         CLASS_B = 0x01, //!< Device can receive in windows sychronized with gateway beacon
00225         CLASS_C = 0x02  //!< Device can receive any time when not transmitting
00226     };
00227 
00228     /**
00229      * Direction of a packet
00230      */
00231     enum Direction {
00232         DIR_UP = 0,     //!< Packet is sent from mote to gateway
00233         DIR_DOWN = 1,   //!< Packet was received from gateway
00234         DIR_PEER = 2    //!< Packet was received from peer
00235     };
00236 
00237 
00238     /**
00239      * Receive window used by Link
00240      */
00241     enum ReceiveWindows {
00242         RX_1 = 1,           //!< First receive window
00243         RX_2,               //!< Second receive window
00244         RX_BEACON,          //!< Beacon receive window
00245         RX_SLOT,            //!< Ping slot receive window
00246         RXC,                //!< Class C continuous window
00247         RX_TEST
00248     };
00249 
00250     /**
00251      * Beacon info descriptors for the GwSpecific Info field
00252      */
00253     enum BeaconInfoDesc {
00254         GPS_FIRST_ANTENNA = 0,    //!< GPS coordinates of the gateway's first antenna
00255         GPS_SECOND_ANTENNA,       //!< GPS coordinates of the gateway's second antenna
00256         GPS_THIRD_ANTENNA,        //!< GPS coordinates of the gateway's third antenna
00257     };
00258 
00259     /**
00260      * Datarate range for a Channel
00261      */
00262     typedef union {
00263             int8_t Value;
00264             struct {
00265                     int8_t Min :4;
00266                     int8_t Max :4;
00267             } Fields;
00268     } DatarateRange;
00269 
00270     /**
00271      * Datarate used for transmitting and receiving
00272      */
00273     typedef struct Datarate {
00274             uint8_t Index;
00275             uint8_t Bandwidth;
00276             uint8_t Coderate;
00277             uint8_t PreambleLength;
00278             uint8_t SpreadingFactor;
00279             uint8_t Crc;
00280             uint8_t TxIQ;
00281             uint8_t RxIQ;
00282             uint8_t SymbolTimeout();
00283             float Timeout();
00284             Datarate();
00285     } Datarate;
00286 
00287     /**
00288      * Channel used for transmitting
00289      */
00290     typedef struct {
00291             uint8_t Index;
00292             uint32_t Frequency;
00293             DatarateRange DrRange;
00294     } Channel;
00295 
00296     /**
00297      * Receive window
00298      */
00299     typedef struct {
00300             uint8_t Index;
00301             uint32_t Frequency;
00302             uint8_t DatarateIndex;
00303     } RxWindow;
00304 
00305     /**
00306      * Duty band for limiting time-on-air for regional regulations
00307      */
00308     typedef struct {
00309             uint8_t Index;
00310             uint32_t FrequencyMin;
00311             uint32_t FrequencyMax;
00312             uint8_t PowerMax;
00313             uint16_t DutyCycle;          //!< Multiplier of time on air, 0:100%, 1:50%, 2:33%, 10:10%, 100:1%, 1000,0.1%
00314             uint32_t TimeOffEnd;         //!< Timestamp when this band will be available
00315     } DutyBand;
00316 
00317     /**
00318      * Beacon data content (w/o CRCs and RFUs)
00319      */
00320     typedef struct {
00321         uint32_t Time;
00322         uint8_t InfoDesc;
00323         uint32_t Latitude;
00324         uint32_t Longitude;
00325     } BeaconData_t;
00326 
00327     /**
00328      * Device configuration
00329      */
00330     typedef struct {
00331             uint8_t FrequencyBand;      //!< Used to choose ChannelPlan
00332             uint8_t EUI[8];             //!< Unique identifier assigned to device
00333     } DeviceConfig;
00334 
00335     /**
00336      * Network configuration
00337      */
00338     typedef struct {
00339             uint8_t Mode;               //!< PUBLIC, PRIVATE or PEER_TO_PEER network mode
00340             uint8_t Class;              //!< Operating class of device
00341             uint8_t EUI[8];             //!< Network ID or AppEUI
00342             uint8_t Key[16];            //!< Network Key or AppKey
00343             uint8_t JoinDelay;          //!< Number of seconds to wait before 1st RX Window
00344             uint8_t RxDelay;            //!< Number of seconds to wait before 1st RX Window
00345             uint8_t FrequencySubBand;   //!< FrequencySubBand used for US915 hybrid operation 0:72 channels, 1:1-8 channels ...
00346             uint8_t AckAttempts;        //!< Number of attempts to send packet and receive an ACK from server
00347             uint8_t Retries;            //!< Number of times to resend a packet without receiving an ACK, redundancy
00348             uint8_t ADREnabled;         //!< Enable adaptive datarate
00349             uint16_t AdrAckLimit;       //!< Number of uplinks without a downlink to allow before setting ADRACKReq
00350             uint16_t AdrAckDelay;       //!< Number of downlinks to expect ADR ACK Response within
00351             uint8_t CADEnabled;         //!< Enable listen before talk/channel activity detection
00352             uint8_t RepeaterMode;       //!< Limit payloads to repeater compatible sizes
00353             uint8_t TxPower;            //!< Default radio output power in dBm
00354             uint8_t TxPowerMax;         //!< Max transmit power
00355             uint8_t TxDatarate;         //!< Datarate for P2P transmit
00356             uint32_t TxFrequency;       //!< Frequency for P2P transmit
00357             int8_t AntennaGain;         //!< Antenna Gain
00358             uint8_t DisableEncryption;  //!< Disable Encryption
00359             uint8_t DisableCRC;         //!< Disable CRC on uplink packets
00360             uint16_t P2PACKTimeout;
00361             uint16_t P2PACKBackoff;
00362             uint8_t JoinRx1DatarateOffset;  //!< Offset for datarate for first window
00363             uint32_t JoinRx2Frequency;      //!< Frequency used in second window
00364             uint8_t JoinRx2DatarateIndex;   //!< Datarate for second window
00365             uint8_t PingPeriodicity;        //!< Number of ping slots to open in a beacon interval (2^(7-PingPeriodicity))
00366     } NetworkConfig;
00367 
00368     /**
00369      * Network session info
00370      * Some settings are acquired in join message and others may be changed through Mac Commands from server
00371      */
00372     typedef struct {
00373             uint8_t Joined;                     //!< State of session
00374             uint8_t Class;                      //!< Operating class of device
00375             uint8_t Rx1DatarateOffset;          //!< Offset for datarate for first window
00376             uint32_t Rx2Frequency;              //!< Frequency used in second window
00377             uint8_t Rx2DatarateIndex;           //!< Datarate for second window
00378             uint32_t BeaconFrequency;           //!< Frequency used for the beacon window
00379             bool BeaconFreqHop;                 //!< Beacon frequency hopping enable
00380             uint32_t PingSlotFrequency;         //!< Frequency used for ping slot windows
00381             uint8_t PingSlotDatarateIndex;      //!< Datarate for the ping slots
00382             bool PingSlotFreqHop;               //!< Ping slot frequency hopping enable
00383             uint8_t TxPower;                    //!< Current total radiated output power in dBm
00384             uint8_t TxDatarate;                 //!< Current datarate can be changed when ADR is enabled
00385             uint32_t Address;                   //!< Network address
00386             uint32_t NetworkID;                 //!< Network ID 24-bits
00387             uint8_t NetworkSessionKey[16];      //!< Network session key
00388             uint8_t ApplicationSessionKey[16];  //!< Data session key
00389             uint16_t ChannelMask[4];            //!< Current channel mask
00390             uint16_t ChannelMask500k;           //!< Current channel mask for 500k channels
00391             uint32_t DownlinkCounter;           //!< Downlink counter of last packet received from server
00392             uint32_t UplinkCounter;             //!< Uplink counter of last packet received from server
00393             uint8_t Redundancy;                 //!< Number of time to repeat an uplink
00394             uint8_t MaxDutyCycle;               //!< Current Max Duty Cycle value
00395             uint32_t JoinTimeOnAir;              //!< Balance of time on air used during join attempts
00396             uint32_t JoinTimeOffEnd;            //!< RTC time of next join attempt
00397             uint32_t JoinFirstAttempt;          //!< RTC time of first failed join attempt
00398             uint32_t AggregatedTimeOffEnd;      //!< Time off air expiration for aggregate duty cycle
00399             uint16_t AggregateDutyCycle;        //!< Used for enforcing time-on-air
00400             uint8_t AckCounter;                 //!< Current number of packets sent without ACK from server
00401             uint8_t AdrCounter;                 //!< Current number of packets received without downlink from server
00402             uint8_t RxDelay;                    //!< Number of seconds to wait before 1st RX Window
00403             uint8_t CommandBuffer[COMMANDS_BUFFER_SIZE]; //!< Buffer to hold Mac Commands and parameters to be sent in next packet
00404             uint8_t CommandBufferIndex;         //!< Index to place next Mac Command, also current size of Command Buffer
00405             bool SrvRequestedAck;               //!< Indicator of ACK requested by server in last packet received
00406             bool DataPending;                   //!< Indicator of data pending at server
00407             uint8_t RxTimingSetupReqReceived;   //!< Indicator that RxTimingSetupAns should be included in uplink
00408             uint8_t RxParamSetupReqAnswer;      //!< Indicator that RxParamSetupAns should be included in uplink
00409             uint8_t DlChannelReqAnswer;         //!< Indicator that DlChannelAns should be included in uplink
00410             uint8_t DownlinkDwelltime;          //!< On air dwell time for downlink packets 0:NONE,1:400ms
00411             uint8_t UplinkDwelltime;            //!< On air dwell time for uplink packets 0:NONE,1:400ms
00412             uint8_t Max_EIRP;                   //!< Maximum allowed EIRP for uplink
00413     } NetworkSession;
00414 
00415     /**
00416      * Multicast session info
00417      */
00418     typedef struct {
00419             uint32_t Address;               //!< Network address
00420             uint8_t NetworkSessionKey[16];  //!< Network session key
00421             uint8_t DataSessionKey[16];     //!< Data session key
00422             uint32_t DownlinkCounter;       //!< Downlink counter of last packet received from server
00423     } MulticastSession;
00424 
00425     /**
00426      * Statistics of current network session
00427      */
00428     typedef struct Statistics {
00429             uint32_t Up;                    //!< Number of uplink packets sent
00430             uint32_t Down;                  //!< Number of downlink packets received
00431             uint32_t Joins;                 //!< Number of join requests sent
00432             uint32_t JoinFails;             //!< Number of join requests without response or invalid response
00433             uint32_t MissedAcks;            //!< Number of missed acknowledgement attempts of confirmed packets
00434             uint32_t CRCErrors;             //!< Number of CRC errors in received packets
00435             int32_t AvgCount;              //!< Number of packets used to compute rolling average of RSSI and SNR
00436             int16_t Rssi;                   //!< RSSI of last packet received
00437             int16_t RssiMin;                //!< Minimum RSSI of last AvgCount packets
00438             int16_t RssiMax;                //!< Maximum RSSI of last AvgCount packets
00439             int16_t RssiAvg;                //!< Rolling average RSSI of last AvgCount packets
00440             int16_t Snr;                     //!< SNR of last packet received
00441             int16_t SnrMin;                  //!< Minimum SNR of last AvgCount packets
00442             int16_t SnrMax;                  //!< Maximum SNR of last AvgCount packets
00443             int16_t SnrAvg;                  //!< Rolling average SNR of last AvgCount packets
00444     } Statistics;
00445 
00446     /**
00447      *  Testing settings
00448      */
00449     typedef struct {
00450             uint8_t TestMode;
00451             uint8_t SkipMICCheck;
00452             uint8_t DisableDutyCycle;
00453             uint8_t DisableRx1;
00454             uint8_t DisableRx2;
00455             uint8_t FixedUplinkCounter;
00456             uint8_t DisableRandomJoinDatarate;
00457     } Testing;
00458 
00459     /**
00460      * Combination of device, network, testing settings and statistics
00461      */
00462     typedef struct {
00463             DeviceConfig Device;
00464             NetworkConfig Network;
00465             NetworkSession Session;
00466             MulticastSession Multicast[MAX_MULTICAST_SESSIONS];
00467             Statistics Stats;
00468             Testing Test;
00469     } Settings;
00470 
00471     /**
00472      * Downlink settings sent in Join Accept message
00473      */
00474     typedef union {
00475             uint8_t Value;
00476             struct {
00477                     uint8_t Rx2Datarate :4;
00478                     uint8_t Rx1Offset :3;
00479                     uint8_t RFU :1;
00480             };
00481     } DownlinkSettings;
00482 
00483     /**
00484      * Frame structure for Join Request
00485      */
00486     typedef struct {
00487             uint8_t Type;
00488             uint8_t AppEUI[8];
00489             uint8_t DevEUI[8];
00490             uint8_t Nonce[2];
00491             uint8_t MIC[4];
00492     } JoinRequestFrame;
00493 
00494     /**
00495      * Mac header of uplink and downlink packets
00496      */
00497     typedef union {
00498             uint8_t Value;
00499             struct {
00500                     uint8_t Major :2;
00501                     uint8_t RFU :3;
00502                     uint8_t MType :3;
00503             } Bits;
00504     } MacHeader;
00505 
00506     /**
00507      * Frame control field of uplink packets
00508      */
00509     typedef union {
00510             uint8_t Value;
00511             struct {
00512                     uint8_t OptionsLength :4;
00513                     uint8_t ClassB :1;
00514                     uint8_t Ack :1;
00515                     uint8_t AdrAckReq :1;
00516                     uint8_t Adr :1;
00517             } Bits;
00518     } UplinkControl;
00519 
00520     /**
00521      * Frame control field of downlink packets
00522      */
00523     typedef union {
00524             uint8_t Value;
00525             struct {
00526                     uint8_t OptionsLength :4;
00527                     uint8_t FPending :1;
00528                     uint8_t Ack :1;
00529                     uint8_t RFU :1;
00530                     uint8_t Adr :1;
00531             } Bits;
00532     } DownlinkControl;
00533 
00534     /**
00535      * Frame type of packet
00536      */
00537     typedef enum {
00538         FRAME_TYPE_JOIN_REQ = 0x00,
00539         FRAME_TYPE_JOIN_ACCEPT = 0x01,
00540         FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
00541         FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
00542         FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
00543         FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
00544         FRAME_TYPE_RFU = 0x06,
00545         FRAME_TYPE_PROPRIETARY = 0x07,
00546     } FrameType;
00547 
00548     /**
00549      * LoRaWAN mote MAC commands
00550      */
00551     typedef enum {
00552         /* Class A */
00553         MOTE_MAC_LINK_CHECK_REQ = 0x02,
00554         MOTE_MAC_LINK_ADR_ANS = 0x03,
00555         MOTE_MAC_DUTY_CYCLE_ANS = 0x04,
00556         MOTE_MAC_RX_PARAM_SETUP_ANS = 0x05,
00557         MOTE_MAC_DEV_STATUS_ANS = 0x06,
00558         MOTE_MAC_NEW_CHANNEL_ANS = 0x07,
00559         MOTE_MAC_RX_TIMING_SETUP_ANS = 0x08,
00560         MOTE_MAC_TX_PARAM_SETUP_ANS = 0x09,
00561         MOTE_MAC_DL_CHANNEL_ANS = 0x0A,
00562         MOTE_MAC_REKEY_IND = 0x0B,
00563         MOTE_MAC_ADR_PARAM_SETUP_ANS = 0x0C,
00564         MOTE_MAC_DEVICE_TIME_REQ = 0x0D,
00565         MOTE_MAC_REJOIN_PARAM_SETUP_ANS = 0x0F,
00566 
00567         /* Class B */
00568         MOTE_MAC_PING_SLOT_INFO_REQ = 0x10,
00569         MOTE_MAC_PING_SLOT_CHANNEL_ANS = 0x11,
00570         MOTE_MAC_BEACON_TIMING_REQ = 0x12,
00571         MOTE_MAC_BEACON_FREQ_ANS = 0x13,
00572 
00573         /* Multitech */
00574         MOTE_MAC_PING_REQ = 0x80,
00575         MOTE_MAC_CHANGE_CLASS = 0x81,
00576         MOTE_MAC_MULTIPART_START_REQ = 0x82,
00577         MOTE_MAC_MULTIPART_START_ANS = 0x83,
00578         MOTE_MAC_MULTIPART_CHUNK = 0x84,
00579         MOTE_MAC_MULTIPART_END_REQ = 0x85,
00580         MOTE_MAC_MULTIPART_END_ANS = 0x86
00581     } MoteCommand;
00582 
00583     /*!
00584      * LoRaWAN server MAC commands
00585      */
00586     typedef enum {
00587         /* Class A */
00588         SRV_MAC_LINK_CHECK_ANS = 0x02,
00589         SRV_MAC_LINK_ADR_REQ = 0x03,
00590         SRV_MAC_DUTY_CYCLE_REQ = 0x04,
00591         SRV_MAC_RX_PARAM_SETUP_REQ = 0x05,
00592         SRV_MAC_DEV_STATUS_REQ = 0x06,
00593         SRV_MAC_NEW_CHANNEL_REQ = 0x07,
00594         SRV_MAC_RX_TIMING_SETUP_REQ = 0x08,
00595         SRV_MAC_TX_PARAM_SETUP_REQ = 0x09,
00596         SRV_MAC_DL_CHANNEL_REQ = 0x0A,
00597         SRV_MAC_REKEY_CONF = 0x0B,
00598         SRV_MAC_ADR_PARAM_SETUP_REQ = 0x0C,
00599         SRV_MAC_DEVICE_TIME_ANS = 0x0D,
00600         SRV_MAC_FORCE_REJOIN_REQ = 0x0E,
00601         SRV_MAC_REJOIN_PARAM_SETUP_REQ = 0x0F,
00602 
00603         /* Class B */
00604         SRV_MAC_PING_SLOT_INFO_ANS = 0x10,
00605         SRV_MAC_PING_SLOT_CHANNEL_REQ = 0x11,
00606         SRV_MAC_BEACON_TIMING_ANS = 0x12,
00607         SRV_MAC_BEACON_FREQ_REQ = 0x13,
00608 
00609         /* Multitech */
00610         SRV_MAC_PING_ANS = 0x80,
00611         SRV_MAC_CHANGE_CLASS = 0x81,
00612         SRV_MAC_MULTIPART_START_REQ = 0x82,
00613         SRV_MAC_MULTIPART_START_ANS = 0x83,
00614         SRV_MAC_MULTIPART_CHUNK = 0x84,
00615         SRV_MAC_MULTIPART_END_REQ = 0x85,
00616         SRV_MAC_MULTIPART_END_ANS = 0x86
00617     } ServerCommand;
00618 
00619     /**
00620      * Radio configuration options
00621      */
00622     typedef enum RadioCfg {
00623         NO_RADIO_CFG,
00624         TX_RADIO_CFG,
00625         RX_RADIO_CFG
00626     } RadioCfg_t;
00627 
00628     /**
00629      * Random seed for software RNG
00630      */
00631     void srand(uint32_t seed);
00632 
00633     /**
00634      * Software RNG for consistent results across differing hardware
00635      */
00636     int rand(void);
00637 
00638     /**
00639      * Generate random number bounded by min and max
00640      */
00641     int32_t rand_r(int32_t min, int32_t max);
00642 
00643     uint8_t CountBits(uint16_t mask);
00644 
00645     /**
00646      * Copy 3-bytes network order from array into LSB of integer value
00647      */
00648     void CopyNetIDtoInt(const uint8_t* arr, uint32_t& val);
00649 
00650     /**
00651      * Copy LSB 3-bytes from integer value into array network order
00652      */
00653     void CopyNetIDtoArray(uint32_t val, uint8_t* arr);
00654 
00655     /**
00656      * Copy 4-bytes network order from array in to integer value
00657      */
00658     void CopyAddrtoInt(const uint8_t* arr, uint32_t& val);
00659 
00660     /**
00661      * Copy 4-bytes from integer in to array network order
00662      */
00663     void CopyAddrtoArray(uint32_t val, uint8_t* arr);
00664 
00665     /**
00666      * Copy 3-bytes network order from array into integer value and multiply by 100
00667      */
00668     void CopyFreqtoInt(const uint8_t* arr, uint32_t& freq);
00669 
00670     /**
00671      * Reverse memory copy
00672      */
00673     void memcpy_r(uint8_t *dst, const uint8_t *src, size_t n);
00674 
00675 }
00676 
00677 #endif
00678