MultiTech / libmDot-Custom

Fork of libmDot-custom by Jason Reiss

Information

Library has been updated to mbed 5.x

The LoRaWAN Certification test mode implementation is built-in to libmDot code. When a start test mode packet is received the library will not return until test mode has ended.

Warning

This library is currently in BETA release. Unresolved issues may be experienced. Software is provided as is with no expectation of support of bug fixes in the future. Please report issues found through the mbed website or directly to support.multitech.com.

Changing of channel plan parameters may cause the device to no longer respect local RF regulations. Please use caution and respect your local regulations.

In AT Command Firmware remove line 803.

CommandTerminal/CommandTerminal.cpp

        delete[] info->RxBuffer;

Likewise, if your application is handling events from the library asynchronously.

Creating new channel plans

Copy EU868 or US915 custom channel plan as a starting point

Import programmDot_AT_firmware_CUSTOM

AT Firmware with custom channel plan support

EU868 provides a framework for a DYNAMIC channel plan with duty-cycle limitations

US915 provides a framework for a FIXED channel plan

RADIO_POWERS are measured output in dBm for each radio tx power setting.

Additional MAC Commands can be implemented by overriding the HandleMacCommand function.

Steps

  1. Setup datarates, duty-cycle bands and channels in ChannelPlan_* constructor
  2. Modify GetJoinDatarate and CalculateJoinBackoff to change join datarates and backoff
  3. Customize HandleJoinAccept datarates
  4. Use GetRxWindow(int) to define how the device open Rx window 1 and 2
  5. GetNextChannel will pick a channel from the enabled channel at the current datarate before each TX
Committer:
jreiss
Date:
Thu Aug 18 16:07:10 2016 +0000
Revision:
15:b50f92f1c6ff
Child:
17:dd0f69240713
update libmDot to 2.0.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jreiss 15:b50f92f1c6ff 1 /** __ ___ ____ _ ______ __ ____ __ ____
jreiss 15:b50f92f1c6ff 2 * / |/ /_ __/ / /_(_)__/_ __/__ ____/ / / __/_ _____ / /____ __ _ ___ / _/__ ____
jreiss 15:b50f92f1c6ff 3 * / /|_/ / // / / __/ /___// / / -_) __/ _ \ _\ \/ // (_-</ __/ -_) ' \(_-< _/ // _ \/ __/ __
jreiss 15:b50f92f1c6ff 4 * /_/ /_/\_,_/_/\__/_/ /_/ \__/\__/_//_/ /___/\_, /___/\__/\__/_/_/_/___/ /___/_//_/\__/ /_/
jreiss 15:b50f92f1c6ff 5 * Copyright (C) 2015 by Multi-Tech Systems /___/
jreiss 15:b50f92f1c6ff 6 *
jreiss 15:b50f92f1c6ff 7 *
jreiss 15:b50f92f1c6ff 8 * @author Jason Reiss
jreiss 15:b50f92f1c6ff 9 * @date 10-31-2015
jreiss 15:b50f92f1c6ff 10 * @brief lora namespace defines global settings, structures and enums for the lora library
jreiss 15:b50f92f1c6ff 11 *
jreiss 15:b50f92f1c6ff 12 * @details
jreiss 15:b50f92f1c6ff 13 *
jreiss 15:b50f92f1c6ff 14 *
jreiss 15:b50f92f1c6ff 15 *
jreiss 15:b50f92f1c6ff 16 */
jreiss 15:b50f92f1c6ff 17
jreiss 15:b50f92f1c6ff 18 #ifndef __LORA_H__
jreiss 15:b50f92f1c6ff 19 #define __LORA_H__
jreiss 15:b50f92f1c6ff 20
jreiss 15:b50f92f1c6ff 21 #include "mbed.h"
jreiss 15:b50f92f1c6ff 22 #include <assert.h>
jreiss 15:b50f92f1c6ff 23 #include "MTSLog.h"
jreiss 15:b50f92f1c6ff 24 //#include <cstring>
jreiss 15:b50f92f1c6ff 25 #include <inttypes.h>
jreiss 15:b50f92f1c6ff 26
jreiss 15:b50f92f1c6ff 27 namespace lora {
jreiss 15:b50f92f1c6ff 28
jreiss 15:b50f92f1c6ff 29 /**
jreiss 15:b50f92f1c6ff 30 * Frequency bandwidth of a Datarate, higher bandwidth gives higher datarate
jreiss 15:b50f92f1c6ff 31 */
jreiss 15:b50f92f1c6ff 32 enum Bandwidth {
jreiss 15:b50f92f1c6ff 33 BW_125,
jreiss 15:b50f92f1c6ff 34 BW_250,
jreiss 15:b50f92f1c6ff 35 BW_500,
jreiss 15:b50f92f1c6ff 36 BW_FSK = 50
jreiss 15:b50f92f1c6ff 37 };
jreiss 15:b50f92f1c6ff 38
jreiss 15:b50f92f1c6ff 39 /**
jreiss 15:b50f92f1c6ff 40 * Spreading factor of a Datarate, lower spreading factor gives higher datarate
jreiss 15:b50f92f1c6ff 41 */
jreiss 15:b50f92f1c6ff 42 enum SpreadingFactors {
jreiss 15:b50f92f1c6ff 43 SF_6 = 6,
jreiss 15:b50f92f1c6ff 44 SF_7,
jreiss 15:b50f92f1c6ff 45 SF_8,
jreiss 15:b50f92f1c6ff 46 SF_9,
jreiss 15:b50f92f1c6ff 47 SF_10,
jreiss 15:b50f92f1c6ff 48 SF_11,
jreiss 15:b50f92f1c6ff 49 SF_12,
jreiss 15:b50f92f1c6ff 50 SF_FSK,
jreiss 15:b50f92f1c6ff 51 SF_INVALID
jreiss 15:b50f92f1c6ff 52 };
jreiss 15:b50f92f1c6ff 53
jreiss 15:b50f92f1c6ff 54 /**
jreiss 15:b50f92f1c6ff 55 * Datarates for use with ChannelPlan
jreiss 15:b50f92f1c6ff 56 */
jreiss 15:b50f92f1c6ff 57 enum Datarates {
jreiss 15:b50f92f1c6ff 58 DR_0 = 0,
jreiss 15:b50f92f1c6ff 59 DR_1,
jreiss 15:b50f92f1c6ff 60 DR_2,
jreiss 15:b50f92f1c6ff 61 DR_3,
jreiss 15:b50f92f1c6ff 62 DR_4,
jreiss 15:b50f92f1c6ff 63 DR_5,
jreiss 15:b50f92f1c6ff 64 DR_6,
jreiss 15:b50f92f1c6ff 65 DR_7,
jreiss 15:b50f92f1c6ff 66 DR_8,
jreiss 15:b50f92f1c6ff 67 DR_9,
jreiss 15:b50f92f1c6ff 68 DR_10,
jreiss 15:b50f92f1c6ff 69 DR_11,
jreiss 15:b50f92f1c6ff 70 DR_12,
jreiss 15:b50f92f1c6ff 71 DR_13,
jreiss 15:b50f92f1c6ff 72 DR_14,
jreiss 15:b50f92f1c6ff 73 DR_15
jreiss 15:b50f92f1c6ff 74 };
jreiss 15:b50f92f1c6ff 75
jreiss 15:b50f92f1c6ff 76 const uint8_t MIN_DATARATE = (uint8_t) DR_0; //!< Minimum datarate
jreiss 15:b50f92f1c6ff 77
jreiss 15:b50f92f1c6ff 78
jreiss 15:b50f92f1c6ff 79 const uint8_t MAX_PHY_PACKET_SIZE = 255; //!< Maximum size for a packet
jreiss 15:b50f92f1c6ff 80 const uint8_t MAX_APPS = 8; //!< Maximum number of apps sessions to save
jreiss 15:b50f92f1c6ff 81 const uint8_t MAX_MULTICAST_SESSIONS = 8; //!< Maximum number of multicast sessions to save
jreiss 15:b50f92f1c6ff 82 const uint8_t EUI_SIZE = 8; //!< Number of bytes in an EUI
jreiss 15:b50f92f1c6ff 83 const uint8_t KEY_SIZE = 16; //!< Number of bytes in an AES key
jreiss 15:b50f92f1c6ff 84
jreiss 15:b50f92f1c6ff 85 const uint8_t DEFAULT_NUM_CHANNELS = 16; //!< Default number of channels in a plan
jreiss 15:b50f92f1c6ff 86 const uint8_t DEFAULT_RX1_DR_OFFSET = 0; //!< Default datarate offset for first rx window
jreiss 15:b50f92f1c6ff 87 const uint8_t DEFAULT_RX2_DATARATE = 0; //!< Default datarate for second rx window
jreiss 15:b50f92f1c6ff 88 const uint8_t DEFAULT_TX_POWER = 14; //!< Default transmit power
jreiss 15:b50f92f1c6ff 89 const uint8_t DEFAULT_CODE_RATE = 1; //!< Default coding rate 1:4/5, 2:4/6, 3:4/7, 4:4/8
jreiss 15:b50f92f1c6ff 90 const uint8_t DEFAULT_PREAMBLE_LEN = 8; //!< Default preamble length
jreiss 15:b50f92f1c6ff 91
jreiss 15:b50f92f1c6ff 92 const int32_t MAX_FCNT_GAP = 16384; //!< Maximum allowed gap in sequence numbers before roll-over
jreiss 15:b50f92f1c6ff 93
jreiss 15:b50f92f1c6ff 94 const uint16_t PRIVATE_JOIN_DELAY = 1000; //!< Default join delay used for private network
jreiss 15:b50f92f1c6ff 95 const uint16_t PUBLIC_JOIN_DELAY = 5000; //!< Default join delay used for public network
jreiss 15:b50f92f1c6ff 96 const uint16_t DEFAULT_JOIN_DELAY = PRIVATE_JOIN_DELAY; //!< Default join delay1
jreiss 15:b50f92f1c6ff 97 const uint16_t DEFAULT_RX_DELAY = 1000; //!< Default delay for first receive window
jreiss 15:b50f92f1c6ff 98 const uint16_t DEFAULT_RX_TIMEOUT = 3000; //!< Default timeout for receive windows
jreiss 15:b50f92f1c6ff 99
jreiss 15:b50f92f1c6ff 100 const uint8_t HI_DR_SYMBOL_TIMEOUT = 12; //!< Symbol timeout for receive at datarate with SF < 11
jreiss 15:b50f92f1c6ff 101 const uint8_t LO_DR_SYMBOL_TIMEOUT = 8; //!< Symbol timeout for receive at datarate with SF > 10
jreiss 15:b50f92f1c6ff 102
jreiss 15:b50f92f1c6ff 103 const uint16_t RX2_DELAY_OFFSET = 1000; //!< Delay between first and second window
jreiss 15:b50f92f1c6ff 104 const uint16_t RXC_OFFSET = 50; //!< Time between end of RXC after TX and RX1
jreiss 15:b50f92f1c6ff 105
jreiss 15:b50f92f1c6ff 106 const uint8_t US915_125K_NUM_CHANS = 64; //!< Number of 125k channels in US915 channel plan
jreiss 15:b50f92f1c6ff 107 const uint8_t US915_500K_NUM_CHANS = 8; //!< Number of 500k channels in US915 channel plan
jreiss 15:b50f92f1c6ff 108
jreiss 15:b50f92f1c6ff 109 const uint32_t US915_125K_FREQ_BASE = 902300000; //!< Frequency base for 125k US915 uplink channels
jreiss 15:b50f92f1c6ff 110 const uint32_t US915_125K_FREQ_STEP = 200000; //!< Frequency step for 125k US915 uplink channels
jreiss 15:b50f92f1c6ff 111
jreiss 15:b50f92f1c6ff 112 const uint32_t US915_500K_FREQ_BASE = 903000000; //!< Frequency base for 500k US915 uplink channels
jreiss 15:b50f92f1c6ff 113 const uint32_t US915_500K_FREQ_STEP = 1600000; //!< Frequency step for 500k US915 uplink channels
jreiss 15:b50f92f1c6ff 114
jreiss 15:b50f92f1c6ff 115 const uint32_t US915_500K_DBASE = 923300000; //!< Frequency base for 500k US915 downlink channels
jreiss 15:b50f92f1c6ff 116 const uint32_t US915_500K_DSTEP = 600000; //!< Frequency step for 500k US915 downlink channels
jreiss 15:b50f92f1c6ff 117
jreiss 15:b50f92f1c6ff 118 const uint32_t US915_FREQ_MIN = 902000000;
jreiss 15:b50f92f1c6ff 119 const uint32_t US915_FREQ_MAX = 928000000;
jreiss 15:b50f92f1c6ff 120
jreiss 15:b50f92f1c6ff 121 const uint8_t US915_MIN_DATARATE = (uint8_t) DR_0; //!< Minimum transmit datarate for US915
jreiss 15:b50f92f1c6ff 122 const uint8_t US915_MAX_DATARATE = (uint8_t) DR_4; //!< Maximum transmit datarate for US915
jreiss 15:b50f92f1c6ff 123
jreiss 15:b50f92f1c6ff 124 const uint8_t US915_MIN_DATARATE_OFFSET = (uint8_t) 0; //!< Minimum transmit datarate for US915
jreiss 15:b50f92f1c6ff 125 const uint8_t US915_MAX_DATARATE_OFFSET = (uint8_t) 3; //!< Maximum transmit datarate for US915
jreiss 15:b50f92f1c6ff 126
jreiss 15:b50f92f1c6ff 127 const uint8_t AU915_125K_NUM_CHANS = 64; //!< Number of 125k channels in AU915 channel plan
jreiss 15:b50f92f1c6ff 128 const uint8_t AU915_500K_NUM_CHANS = 8; //!< Number of 500k channels in AU915 channel plan
jreiss 15:b50f92f1c6ff 129
jreiss 15:b50f92f1c6ff 130 const uint32_t AU915_125K_FREQ_BASE = 915200000; //!< Frequency base for 125k AU915 uplink channels
jreiss 15:b50f92f1c6ff 131 const uint32_t AU915_125K_FREQ_STEP = 200000; //!< Frequency step for 125k AU915 uplink channels
jreiss 15:b50f92f1c6ff 132
jreiss 15:b50f92f1c6ff 133 const uint32_t AU915_500K_FREQ_BASE = 915900000; //!< Frequency base for 500k AU915 uplink channels
jreiss 15:b50f92f1c6ff 134 const uint32_t AU915_500K_FREQ_STEP = 1600000; //!< Frequency step for 500k AU915 uplink channels
jreiss 15:b50f92f1c6ff 135
jreiss 15:b50f92f1c6ff 136 const uint32_t AU915_500K_DBASE = 923300000; //!< Frequency base for 500k AU915 downlink channels
jreiss 15:b50f92f1c6ff 137 const uint32_t AU915_500K_DSTEP = 600000; //!< Frequency step for 500k AU915 downlink channels
jreiss 15:b50f92f1c6ff 138
jreiss 15:b50f92f1c6ff 139 const uint32_t AU915_FREQ_MIN = 915000000;
jreiss 15:b50f92f1c6ff 140 const uint32_t AU915_FREQ_MAX = 928000000;
jreiss 15:b50f92f1c6ff 141
jreiss 15:b50f92f1c6ff 142 const uint8_t AU915_MIN_DATARATE = (uint8_t) DR_0; //!< Minimum transmit datarate for AU915
jreiss 15:b50f92f1c6ff 143 const uint8_t AU915_MAX_DATARATE = (uint8_t) DR_4; //!< Maximum transmit datarate for AU915
jreiss 15:b50f92f1c6ff 144
jreiss 15:b50f92f1c6ff 145 const uint8_t AU915_MIN_DATARATE_OFFSET = (uint8_t) 0; //!< Minimum transmit datarate for AU915
jreiss 15:b50f92f1c6ff 146 const uint8_t AU915_MAX_DATARATE_OFFSET = (uint8_t) 3; //!< Maximum transmit datarate for AU915
jreiss 15:b50f92f1c6ff 147
jreiss 15:b50f92f1c6ff 148 const uint8_t EU868_125K_NUM_CHANS = 16; //!< Number of 125k channels in EU868 channel plan
jreiss 15:b50f92f1c6ff 149 const uint8_t EU868_DEFAULT_NUM_CHANS = 3; //!< Number of defualt channels in EU868 channel plan
jreiss 15:b50f92f1c6ff 150 const uint32_t EU868_125K_FREQ_BASE = 868100000; //!< Frequency base for 125k EU868 uplink channels
jreiss 15:b50f92f1c6ff 151 const uint32_t EU868_125K_FREQ_STEP = 200000; //!< Frequency step for 125k EU868 uplink channels
jreiss 15:b50f92f1c6ff 152 const uint32_t EU868_RX2_FREQ = 869525000; //!< Frequency default for second rx window in EU868
jreiss 15:b50f92f1c6ff 153
jreiss 15:b50f92f1c6ff 154 const uint8_t EU868_TX_POWER_MAX = 14; //!< Max power for EU868 channel plan
jreiss 15:b50f92f1c6ff 155
jreiss 15:b50f92f1c6ff 156 // 0.1% duty cycle 863-868
jreiss 15:b50f92f1c6ff 157 // Limiting to 865-868 allows for 1% duty cycle
jreiss 15:b50f92f1c6ff 158 const uint32_t EU868_MILLI_FREQ_MIN = 865000000;
jreiss 15:b50f92f1c6ff 159 const uint32_t EU868_MILLI_FREQ_MAX = 868000000;
jreiss 15:b50f92f1c6ff 160
jreiss 15:b50f92f1c6ff 161 const uint32_t EU868_MILLI_1_FREQ_MIN = 868700000;
jreiss 15:b50f92f1c6ff 162 const uint32_t EU868_MILLI_1_FREQ_MAX = 869200000;
jreiss 15:b50f92f1c6ff 163
jreiss 15:b50f92f1c6ff 164 // 1% duty cycle
jreiss 15:b50f92f1c6ff 165 const uint32_t EU868_CENTI_FREQ_MIN = 868000000;
jreiss 15:b50f92f1c6ff 166 const uint32_t EU868_CENTI_FREQ_MAX = 868600000;
jreiss 15:b50f92f1c6ff 167
jreiss 15:b50f92f1c6ff 168 // 10% duty cycle
jreiss 15:b50f92f1c6ff 169 const uint32_t EU868_DECI_FREQ_MIN = 869400000;
jreiss 15:b50f92f1c6ff 170 const uint32_t EU868_DECI_FREQ_MAX = 869650000;
jreiss 15:b50f92f1c6ff 171
jreiss 15:b50f92f1c6ff 172 // Below 7dBm there is no duty cycle for these frequencies
jreiss 15:b50f92f1c6ff 173 // Up to 14dBm there is 1% duty cycle
jreiss 15:b50f92f1c6ff 174 const uint32_t EU868_VAR_FREQ_MIN = 869700000;
jreiss 15:b50f92f1c6ff 175 const uint32_t EU868_VAR_FREQ_MAX = 870000000;
jreiss 15:b50f92f1c6ff 176
jreiss 15:b50f92f1c6ff 177 const uint32_t EU868_FREQ_MIN = 863000000;
jreiss 15:b50f92f1c6ff 178 const uint32_t EU868_FREQ_MAX = 870000000;
jreiss 15:b50f92f1c6ff 179
jreiss 15:b50f92f1c6ff 180 const uint8_t EU868_MIN_DATARATE = (uint8_t) DR_0; //!< Minimum transmit datarate for EU868
jreiss 15:b50f92f1c6ff 181 const uint8_t EU868_MAX_DATARATE = (uint8_t) DR_7; //!< Maximum transmit datarate for EU868
jreiss 15:b50f92f1c6ff 182
jreiss 15:b50f92f1c6ff 183 const uint8_t EU868_MIN_DATARATE_OFFSET = (uint8_t) 0; //!< Minimum transmit datarate for US915
jreiss 15:b50f92f1c6ff 184 const uint8_t EU868_MAX_DATARATE_OFFSET = (uint8_t) 5; //!< Maximum transmit datarate for US915
jreiss 15:b50f92f1c6ff 185
jreiss 15:b50f92f1c6ff 186 const int16_t DEFAULT_FREE_CHAN_RSSI_THRESHOLD = -90; //!< Threshold for channel activity detection (CAD) dBm
jreiss 15:b50f92f1c6ff 187
jreiss 15:b50f92f1c6ff 188 const uint8_t CHAN_MASK_SIZE = 16; //!< Number of bits in a channel mask
jreiss 15:b50f92f1c6ff 189 const uint8_t COMMANDS_BUFFER_SIZE = 15; //!< Size of Mac Command buffer
jreiss 15:b50f92f1c6ff 190
jreiss 15:b50f92f1c6ff 191 const uint8_t PKT_HEADER = 0; //!< Index to packet mHdr field
jreiss 15:b50f92f1c6ff 192 const uint8_t PKT_ADDRESS = 1; //!< Index to first byte of packet address field
jreiss 15:b50f92f1c6ff 193 const uint8_t PKT_FRAME_CONTROL = PKT_ADDRESS + 4; //!< Index to packet fCtrl field @see UplinkControl
jreiss 15:b50f92f1c6ff 194 const uint8_t PKT_FRAME_COUNTER = PKT_FRAME_CONTROL + 1; //!< Index to packet frame counter field
jreiss 15:b50f92f1c6ff 195 const uint8_t PKT_OPTIONS_START = PKT_FRAME_COUNTER + 2; //!< Index to start of optional mac commands
jreiss 15:b50f92f1c6ff 196
jreiss 15:b50f92f1c6ff 197 const uint8_t PKT_JOIN_APP_NONCE = 1; //!< Index to application nonce in Join Accept message
jreiss 15:b50f92f1c6ff 198 const uint8_t PKT_JOIN_NETWORK_ID = 4; //!< Index to network id in Join Accept message
jreiss 15:b50f92f1c6ff 199 const uint8_t PKT_JOIN_NETWORK_ADDRESS = 7; //!< Index to network address in Join Accept message
jreiss 15:b50f92f1c6ff 200 const uint8_t PKT_JOIN_DL_SETTINGS = 11; //!< Index to downlink settings in Join Accept message
jreiss 15:b50f92f1c6ff 201 const uint8_t PKT_JOIN_RX_DELAY = 12; //!< Index to rx delay in Join Accept message
jreiss 15:b50f92f1c6ff 202
jreiss 15:b50f92f1c6ff 203 const uint8_t ADR_ACK_LIMIT = 64; //!< Number of packets without ADR ACK Request
jreiss 15:b50f92f1c6ff 204 const uint8_t ADR_ACK_DELAY = 32; //!< Number of packets to expect ADR ACK Response within
jreiss 15:b50f92f1c6ff 205
jreiss 15:b50f92f1c6ff 206 const uint16_t ACK_TIMEOUT = 2000; //!< Base millisecond timeout to resend after missed ACK
jreiss 15:b50f92f1c6ff 207 const uint16_t ACK_TIMEOUT_RND = 1000; //!< Random millisecond adjustment to resend after missed ACK
jreiss 15:b50f92f1c6ff 208
jreiss 15:b50f92f1c6ff 209 const uint8_t FRAME_OVERHEAD = 13; //!< Bytes of network info overhead in a frame
jreiss 15:b50f92f1c6ff 210
jreiss 15:b50f92f1c6ff 211 /**
jreiss 15:b50f92f1c6ff 212 * Settings to choose ChannelPlan
jreiss 15:b50f92f1c6ff 213 */
jreiss 15:b50f92f1c6ff 214 enum FrequencyBand {
jreiss 15:b50f92f1c6ff 215 EU868, //!< EU 863-870 16 channel uplink
jreiss 15:b50f92f1c6ff 216 US915, //!< US 902-928 64-125k/8-500k uplink and 8-500k downlink channels
jreiss 15:b50f92f1c6ff 217 AU915, //!< US 915-928 64-125k/8-500k uplink and 8-500k downlink channels
jreiss 15:b50f92f1c6ff 218 CN779, //!<
jreiss 15:b50f92f1c6ff 219 CN470, //!<
jreiss 15:b50f92f1c6ff 220 EU433, //!<
jreiss 15:b50f92f1c6ff 221 };
jreiss 15:b50f92f1c6ff 222
jreiss 15:b50f92f1c6ff 223 /**
jreiss 15:b50f92f1c6ff 224 * Settings for type of network
jreiss 15:b50f92f1c6ff 225 * PUBLIC - defaults to 5/6 second join windows and 0x34 sync word
jreiss 15:b50f92f1c6ff 226 * PRIVATE - defaults to 1/2 second join windows and 0x12 sync word
jreiss 15:b50f92f1c6ff 227 */
jreiss 15:b50f92f1c6ff 228 enum NetworkType {
jreiss 15:b50f92f1c6ff 229 PRIVATE = 0,
jreiss 15:b50f92f1c6ff 230 PUBLIC = 1,
jreiss 15:b50f92f1c6ff 231 PEER_TO_PEER = 4
jreiss 15:b50f92f1c6ff 232 };
jreiss 15:b50f92f1c6ff 233
jreiss 15:b50f92f1c6ff 234 /**
jreiss 15:b50f92f1c6ff 235 * Enum for on/off settings
jreiss 15:b50f92f1c6ff 236 */
jreiss 15:b50f92f1c6ff 237 enum Enabled {
jreiss 15:b50f92f1c6ff 238 OFF = 0,
jreiss 15:b50f92f1c6ff 239 ON = 1
jreiss 15:b50f92f1c6ff 240 };
jreiss 15:b50f92f1c6ff 241
jreiss 15:b50f92f1c6ff 242 /**
jreiss 15:b50f92f1c6ff 243 * Return status of mac functions
jreiss 15:b50f92f1c6ff 244 */
jreiss 15:b50f92f1c6ff 245 enum MacStatus {
jreiss 15:b50f92f1c6ff 246 LORA_OK = 0,
jreiss 15:b50f92f1c6ff 247 LORA_ERROR = 1,
jreiss 15:b50f92f1c6ff 248 LORA_JOIN_ERROR = 2,
jreiss 15:b50f92f1c6ff 249 LORA_SEND_ERROR = 3,
jreiss 15:b50f92f1c6ff 250 LORA_MIC_ERROR = 4,
jreiss 15:b50f92f1c6ff 251 LORA_ADDRESS_ERROR = 5,
jreiss 15:b50f92f1c6ff 252 LORA_NO_CHANS_ENABLED = 6,
jreiss 15:b50f92f1c6ff 253 LORA_COMMAND_BUFFER_FULL = 7,
jreiss 15:b50f92f1c6ff 254 LORA_UNKNOWN_MAC_COMMAND = 8,
jreiss 15:b50f92f1c6ff 255 LORA_ADR_OFF = 9,
jreiss 15:b50f92f1c6ff 256 LORA_BUSY = 10,
jreiss 15:b50f92f1c6ff 257 LORA_LINK_BUSY = 11,
jreiss 15:b50f92f1c6ff 258 LORA_RADIO_BUSY = 12,
jreiss 15:b50f92f1c6ff 259 LORA_BUFFER_FULL = 13,
jreiss 15:b50f92f1c6ff 260 LORA_JOIN_BACKOFF = 14,
jreiss 15:b50f92f1c6ff 261 LORA_NO_FREE_CHAN = 15,
jreiss 15:b50f92f1c6ff 262 LORA_AGGREGATED_DUTY_CYCLE = 16,
jreiss 15:b50f92f1c6ff 263 LORA_MAX_PAYLOAD_EXCEEDED
jreiss 15:b50f92f1c6ff 264 };
jreiss 15:b50f92f1c6ff 265
jreiss 15:b50f92f1c6ff 266 /**
jreiss 15:b50f92f1c6ff 267 * State for Link
jreiss 15:b50f92f1c6ff 268 */
jreiss 15:b50f92f1c6ff 269 enum LinkState {
jreiss 15:b50f92f1c6ff 270 LINK_IDLE = 0, //!< Link ready to send or receive
jreiss 15:b50f92f1c6ff 271 LINK_TX, //!< Link is busy sending
jreiss 15:b50f92f1c6ff 272 LINK_ACK_TX, //!< Link is busy resending after missed ACK
jreiss 15:b50f92f1c6ff 273 LINK_REP_TX, //!< Link is busy repeating
jreiss 15:b50f92f1c6ff 274 LINK_RX, //!< Link has receive window open
jreiss 15:b50f92f1c6ff 275 LINK_RX1, //!< Link has first received window open
jreiss 15:b50f92f1c6ff 276 LINK_RX2, //!< Link has second received window open
jreiss 15:b50f92f1c6ff 277 LINK_RXC, //!< Link has class C received window open
jreiss 15:b50f92f1c6ff 278 LINK_P2P, //!< Link is busy sending
jreiss 15:b50f92f1c6ff 279 };
jreiss 15:b50f92f1c6ff 280
jreiss 15:b50f92f1c6ff 281 /**
jreiss 15:b50f92f1c6ff 282 * State for MAC
jreiss 15:b50f92f1c6ff 283 */
jreiss 15:b50f92f1c6ff 284 enum MacState {
jreiss 15:b50f92f1c6ff 285 MAC_IDLE,
jreiss 15:b50f92f1c6ff 286 MAC_RX1,
jreiss 15:b50f92f1c6ff 287 MAC_RX2,
jreiss 15:b50f92f1c6ff 288 MAC_RXC,
jreiss 15:b50f92f1c6ff 289 MAC_TX,
jreiss 15:b50f92f1c6ff 290 MAC_JOIN
jreiss 15:b50f92f1c6ff 291 };
jreiss 15:b50f92f1c6ff 292
jreiss 15:b50f92f1c6ff 293 /**
jreiss 15:b50f92f1c6ff 294 * Operation class for device
jreiss 15:b50f92f1c6ff 295 */
jreiss 15:b50f92f1c6ff 296 enum MoteClass {
jreiss 15:b50f92f1c6ff 297 CLASS_A = 0x00, //!< Device can only receive in windows opened after a transmit
jreiss 15:b50f92f1c6ff 298 CLASS_B = 0x01, //!< Device can receive in windows sychronized with gateway beacon
jreiss 15:b50f92f1c6ff 299 CLASS_C = 0x02 //!< Device can receive any time when not transmitting
jreiss 15:b50f92f1c6ff 300 };
jreiss 15:b50f92f1c6ff 301
jreiss 15:b50f92f1c6ff 302 /**
jreiss 15:b50f92f1c6ff 303 * Direction of a packet
jreiss 15:b50f92f1c6ff 304 */
jreiss 15:b50f92f1c6ff 305 enum Direction {
jreiss 15:b50f92f1c6ff 306 DIR_UP = 0, //!< Packet is sent from mote to gateway
jreiss 15:b50f92f1c6ff 307 DIR_DOWN = 1, //!< Packet was received from gateway
jreiss 15:b50f92f1c6ff 308 DIR_PEER = 2 //!< Packet was received from peer
jreiss 15:b50f92f1c6ff 309 };
jreiss 15:b50f92f1c6ff 310
jreiss 15:b50f92f1c6ff 311
jreiss 15:b50f92f1c6ff 312 /**
jreiss 15:b50f92f1c6ff 313 * Received window used by Link
jreiss 15:b50f92f1c6ff 314 */
jreiss 15:b50f92f1c6ff 315 enum ReceiveWindows {
jreiss 15:b50f92f1c6ff 316 RX_1 = 1, //!< First receive window
jreiss 15:b50f92f1c6ff 317 RX_2, //!< Second receive window
jreiss 15:b50f92f1c6ff 318 RX_BEACON, //!< Beacon receive window
jreiss 15:b50f92f1c6ff 319 RX_SLOT, //!< Beacon Slot receive window
jreiss 15:b50f92f1c6ff 320 RX_TEST
jreiss 15:b50f92f1c6ff 321 };
jreiss 15:b50f92f1c6ff 322
jreiss 15:b50f92f1c6ff 323 /**
jreiss 15:b50f92f1c6ff 324 * Datarate range for a Channel
jreiss 15:b50f92f1c6ff 325 */
jreiss 15:b50f92f1c6ff 326 typedef union {
jreiss 15:b50f92f1c6ff 327 int8_t Value;
jreiss 15:b50f92f1c6ff 328 struct {
jreiss 15:b50f92f1c6ff 329 int8_t Min :4;
jreiss 15:b50f92f1c6ff 330 int8_t Max :4;
jreiss 15:b50f92f1c6ff 331 } Fields;
jreiss 15:b50f92f1c6ff 332 } DatarateRange;
jreiss 15:b50f92f1c6ff 333
jreiss 15:b50f92f1c6ff 334 /**
jreiss 15:b50f92f1c6ff 335 * Datarate used for transmitting and receiving
jreiss 15:b50f92f1c6ff 336 */
jreiss 15:b50f92f1c6ff 337 typedef struct Datarate {
jreiss 15:b50f92f1c6ff 338 uint8_t Index;
jreiss 15:b50f92f1c6ff 339 uint8_t Bandwidth;
jreiss 15:b50f92f1c6ff 340 uint8_t Coderate;
jreiss 15:b50f92f1c6ff 341 uint8_t PreambleLength;
jreiss 15:b50f92f1c6ff 342 uint8_t SpreadingFactor;
jreiss 15:b50f92f1c6ff 343 uint8_t Crc;
jreiss 15:b50f92f1c6ff 344 uint8_t TxIQ;
jreiss 15:b50f92f1c6ff 345 uint8_t RxIQ;
jreiss 15:b50f92f1c6ff 346 uint8_t SymbolTimeout();
jreiss 15:b50f92f1c6ff 347 Datarate();
jreiss 15:b50f92f1c6ff 348 } Datarate;
jreiss 15:b50f92f1c6ff 349
jreiss 15:b50f92f1c6ff 350 /**
jreiss 15:b50f92f1c6ff 351 * Channel used for transmitting
jreiss 15:b50f92f1c6ff 352 */
jreiss 15:b50f92f1c6ff 353 typedef struct {
jreiss 15:b50f92f1c6ff 354 uint8_t Index;
jreiss 15:b50f92f1c6ff 355 uint32_t Frequency;
jreiss 15:b50f92f1c6ff 356 DatarateRange DrRange;
jreiss 15:b50f92f1c6ff 357 } Channel;
jreiss 15:b50f92f1c6ff 358
jreiss 15:b50f92f1c6ff 359 /**
jreiss 15:b50f92f1c6ff 360 * Receive window
jreiss 15:b50f92f1c6ff 361 */
jreiss 15:b50f92f1c6ff 362 typedef struct {
jreiss 15:b50f92f1c6ff 363 uint8_t Index;
jreiss 15:b50f92f1c6ff 364 uint32_t Frequency;
jreiss 15:b50f92f1c6ff 365 uint8_t DatarateIndex;
jreiss 15:b50f92f1c6ff 366 } RxWindow;
jreiss 15:b50f92f1c6ff 367
jreiss 15:b50f92f1c6ff 368 /**
jreiss 15:b50f92f1c6ff 369 * Duty band for limiting time-on-air for regional regulations
jreiss 15:b50f92f1c6ff 370 */
jreiss 15:b50f92f1c6ff 371 typedef struct {
jreiss 15:b50f92f1c6ff 372 uint8_t Index;
jreiss 15:b50f92f1c6ff 373 uint32_t FrequencyMin;
jreiss 15:b50f92f1c6ff 374 uint32_t FrequencyMax;
jreiss 15:b50f92f1c6ff 375 uint8_t PowerMax;
jreiss 15:b50f92f1c6ff 376 uint16_t DutyCycle; //!< Multiplier of time on air, 0:100%, 1:50%, 2:33%, 10:10%, 100:1%, 1000,0.1%
jreiss 15:b50f92f1c6ff 377 uint32_t TimeOffEnd; //!< Timestamp when this band will be available
jreiss 15:b50f92f1c6ff 378 } DutyBand;
jreiss 15:b50f92f1c6ff 379
jreiss 15:b50f92f1c6ff 380 /**
jreiss 15:b50f92f1c6ff 381 * Device configuration
jreiss 15:b50f92f1c6ff 382 */
jreiss 15:b50f92f1c6ff 383 typedef struct {
jreiss 15:b50f92f1c6ff 384 uint8_t FrequencyBand; //!< Used to choose ChannelPlan
jreiss 15:b50f92f1c6ff 385 uint8_t EUI[8]; //!< Unique identifier assigned to device
jreiss 15:b50f92f1c6ff 386 } DeviceConfig;
jreiss 15:b50f92f1c6ff 387
jreiss 15:b50f92f1c6ff 388 /**
jreiss 15:b50f92f1c6ff 389 * Network configuration
jreiss 15:b50f92f1c6ff 390 */
jreiss 15:b50f92f1c6ff 391 typedef struct {
jreiss 15:b50f92f1c6ff 392 uint8_t Mode; //!< PUBLIC, PRIVATE or PEER_TO_PEER network mode
jreiss 15:b50f92f1c6ff 393 uint8_t Class; //!< Operating class of device
jreiss 15:b50f92f1c6ff 394 uint8_t EUI[8]; //!< Network ID or AppEUI
jreiss 15:b50f92f1c6ff 395 uint8_t Key[16]; //!< Network Key or AppKey
jreiss 15:b50f92f1c6ff 396 uint8_t JoinDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 397 uint8_t RxDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 398 uint8_t ChannelGroup; //!< ChannelGroup used for US915 hybrid operation 0:72 channels, 1:1-8 channels ...
jreiss 15:b50f92f1c6ff 399 uint8_t AckAttempts; //!< Number of attempts to send packet and receive an ACK from server
jreiss 15:b50f92f1c6ff 400 uint8_t Retries; //!< Number of times to resend a packet without receiving an ACK, redundancy
jreiss 15:b50f92f1c6ff 401 uint8_t ADREnabled; //!< Enable adaptive datarate
jreiss 15:b50f92f1c6ff 402 uint8_t CADEnabled; //!< Enable listen before talk/channel activity detection
jreiss 15:b50f92f1c6ff 403 uint8_t RepeaterMode; //!< Limit payloads to repeater compatible sizes
jreiss 15:b50f92f1c6ff 404 uint8_t TxPower; //!< Default radio output power in dBm
jreiss 15:b50f92f1c6ff 405 uint8_t TxPowerMax; //!< Max transmit power
jreiss 15:b50f92f1c6ff 406 uint8_t TxDatarate; //!< Datarate for P2P transmit
jreiss 15:b50f92f1c6ff 407 uint32_t TxFrequency; //!< Frequency for P2P transmit
jreiss 15:b50f92f1c6ff 408 int8_t AntennaGain; //!< Antenna Gain
jreiss 15:b50f92f1c6ff 409 uint8_t DisableEncryption; //!< Disable Encryption
jreiss 15:b50f92f1c6ff 410 uint8_t DisableCRC; //!< Disable CRC on uplink packets
jreiss 15:b50f92f1c6ff 411 uint16_t P2PACKTimeout;
jreiss 15:b50f92f1c6ff 412 uint16_t P2PACKBackoff;
jreiss 15:b50f92f1c6ff 413 uint8_t JoinRx1DatarateOffset; //!< Offset for datarate for first window
jreiss 15:b50f92f1c6ff 414 uint32_t JoinRx2Frequency; //!< Frequency used in second window
jreiss 15:b50f92f1c6ff 415 uint8_t JoinRx2DatarateIndex; //!< Datarate for second window
jreiss 15:b50f92f1c6ff 416 } NetworkConfig;
jreiss 15:b50f92f1c6ff 417
jreiss 15:b50f92f1c6ff 418 /**
jreiss 15:b50f92f1c6ff 419 * Network session info
jreiss 15:b50f92f1c6ff 420 * Some settings are acquired in join message and others may be changed through Mac Commands from server
jreiss 15:b50f92f1c6ff 421 */
jreiss 15:b50f92f1c6ff 422 typedef struct {
jreiss 15:b50f92f1c6ff 423 uint8_t Joined; //!< State of session
jreiss 15:b50f92f1c6ff 424 uint8_t Rx1DatarateOffset; //!< Offset for datarate for first window
jreiss 15:b50f92f1c6ff 425 uint32_t Rx2Frequency; //!< Frequency used in second window
jreiss 15:b50f92f1c6ff 426 uint8_t Rx2DatarateIndex; //!< Datarate for second window
jreiss 15:b50f92f1c6ff 427 uint8_t TxPower; //!< Current total radiated output power in dBm
jreiss 15:b50f92f1c6ff 428 uint8_t TxDatarate; //!< Current datarate can be changed when ADR is enabled
jreiss 15:b50f92f1c6ff 429 uint32_t Address; //!< Network address
jreiss 15:b50f92f1c6ff 430 uint32_t NetworkID; //!< Network ID 24-bits
jreiss 15:b50f92f1c6ff 431 uint8_t NetworkSessionKey[16]; //!< Network session key
jreiss 15:b50f92f1c6ff 432 uint8_t ApplicationSessionKey[16]; //!< Data session key
jreiss 15:b50f92f1c6ff 433 uint16_t ChannelMask[4]; //!< Current channel mask
jreiss 15:b50f92f1c6ff 434 uint16_t ChannelMask500k; //!< Current channel mask for 500k channels
jreiss 15:b50f92f1c6ff 435 uint32_t DownlinkCounter; //!< Downlink counter of last packet received from server
jreiss 15:b50f92f1c6ff 436 uint32_t UplinkCounter; //!< Uplink counter of last packet received from server
jreiss 15:b50f92f1c6ff 437 uint8_t Redundancy; //!< Number of time to repeat an uplink
jreiss 15:b50f92f1c6ff 438 uint8_t MaxDutyCycle; //!< Current Max Duty Cycle value
jreiss 15:b50f92f1c6ff 439 uint32_t JoinTimeOnAir; //!< Balance of time on air used during join attempts
jreiss 15:b50f92f1c6ff 440 uint32_t JoinTimeOffEnd; //!< RTC time of next join attempt
jreiss 15:b50f92f1c6ff 441 uint32_t JoinFirstAttempt; //!< RTC time of first failed join attempt
jreiss 15:b50f92f1c6ff 442 uint32_t AggregatedTimeOffEnd; //!< Time off air expiration for aggregate duty cycle
jreiss 15:b50f92f1c6ff 443 uint16_t AggregateDutyCycle; //!< Used for enforcing time-on-air
jreiss 15:b50f92f1c6ff 444 uint8_t AckCounter; //!< Current number of packets sent without ACK from server
jreiss 15:b50f92f1c6ff 445 uint8_t AdrCounter; //!< Current number of packets received without downlink from server
jreiss 15:b50f92f1c6ff 446 uint8_t RxDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 447 uint8_t CommandBuffer[COMMANDS_BUFFER_SIZE]; //!< Buffer to hold Mac Commands and parameters to be sent in next packet
jreiss 15:b50f92f1c6ff 448 uint8_t CommandBufferIndex; //!< Index to place next Mac Command, also current size of Command Buffer
jreiss 15:b50f92f1c6ff 449 bool SrvRequestedAck; //!< Indicator of ACK requested by server in last packet received
jreiss 15:b50f92f1c6ff 450 bool DataPending; //!< Indicator of data pending at server
jreiss 15:b50f92f1c6ff 451 uint8_t RxTimingSetupReqReceived; //!< Indicator that RxTimingSetupAns should be included in uplink
jreiss 15:b50f92f1c6ff 452 uint8_t RxParamSetupReqAnswer; //!< Indicator that RxParamSetupAns should be included in uplink
jreiss 15:b50f92f1c6ff 453 } NetworkSession;
jreiss 15:b50f92f1c6ff 454
jreiss 15:b50f92f1c6ff 455 /**
jreiss 15:b50f92f1c6ff 456 * Multicast session info
jreiss 15:b50f92f1c6ff 457 */
jreiss 15:b50f92f1c6ff 458 typedef struct {
jreiss 15:b50f92f1c6ff 459 uint32_t Address; //!< Network address
jreiss 15:b50f92f1c6ff 460 uint8_t NetworkSessionKey[16]; //!< Network session key
jreiss 15:b50f92f1c6ff 461 uint8_t DataSessionKey[16]; //!< Data session key
jreiss 15:b50f92f1c6ff 462 uint32_t DownlinkCounter; //!< Downlink counter of last packet received from server
jreiss 15:b50f92f1c6ff 463 } MulticastSession;
jreiss 15:b50f92f1c6ff 464
jreiss 15:b50f92f1c6ff 465 /**
jreiss 15:b50f92f1c6ff 466 * Application configuration
jreiss 15:b50f92f1c6ff 467 */
jreiss 15:b50f92f1c6ff 468 typedef struct {
jreiss 15:b50f92f1c6ff 469 uint8_t Port; //!< Port used by application
jreiss 15:b50f92f1c6ff 470 uint8_t AppEUI; //!< Application ID
jreiss 15:b50f92f1c6ff 471 uint8_t AppKey[16]; //!< Application Key
jreiss 15:b50f92f1c6ff 472 } ApplicationConfig;
jreiss 15:b50f92f1c6ff 473
jreiss 15:b50f92f1c6ff 474 /**
jreiss 15:b50f92f1c6ff 475 * Statistics of current network session
jreiss 15:b50f92f1c6ff 476 */
jreiss 15:b50f92f1c6ff 477 typedef struct Statistics {
jreiss 15:b50f92f1c6ff 478 uint32_t Up; //!< Number of uplink packets sent
jreiss 15:b50f92f1c6ff 479 uint32_t Down; //!< Number of downlink packets received
jreiss 15:b50f92f1c6ff 480 uint32_t Joins; //!< Number of join requests sent
jreiss 15:b50f92f1c6ff 481 uint32_t JoinFails; //!< Number of join requests without response or invalid response
jreiss 15:b50f92f1c6ff 482 uint32_t MissedAcks; //!< Number of missed acknowledgement attempts of confirmed packets
jreiss 15:b50f92f1c6ff 483 uint32_t CRCErrors; //!< Number of CRC errors in received packets
jreiss 15:b50f92f1c6ff 484 int32_t AvgCount; //!< Number of packets used to compute rolling average of RSSI and SNR
jreiss 15:b50f92f1c6ff 485 int16_t Rssi; //!< RSSI of last packet received
jreiss 15:b50f92f1c6ff 486 int16_t RssiMin; //!< Minimum RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 487 int16_t RssiMax; //!< Maximum RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 488 int16_t RssiAvg; //!< Rolling average RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 489 int16_t Snr; //!< SNR of last packet received
jreiss 15:b50f92f1c6ff 490 int16_t SnrMin; //!< Minimum SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 491 int16_t SnrMax; //!< Maximum SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 492 int16_t SnrAvg; //!< Rolling average SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 493 } Statistics;
jreiss 15:b50f92f1c6ff 494
jreiss 15:b50f92f1c6ff 495 /**
jreiss 15:b50f92f1c6ff 496 * Testing settings
jreiss 15:b50f92f1c6ff 497 */
jreiss 15:b50f92f1c6ff 498 typedef struct {
jreiss 15:b50f92f1c6ff 499 uint8_t TestMode;
jreiss 15:b50f92f1c6ff 500 uint8_t SkipMICCheck;
jreiss 15:b50f92f1c6ff 501 uint8_t DisableDutyCycle;
jreiss 15:b50f92f1c6ff 502 uint8_t DisableRx1;
jreiss 15:b50f92f1c6ff 503 uint8_t DisableRx2;
jreiss 15:b50f92f1c6ff 504 uint8_t FixedUplinkCounter;
jreiss 15:b50f92f1c6ff 505 uint8_t DisableRandomJoinDatarate;
jreiss 15:b50f92f1c6ff 506 } Testing;
jreiss 15:b50f92f1c6ff 507
jreiss 15:b50f92f1c6ff 508 /**
jreiss 15:b50f92f1c6ff 509 * Combination of device, network, testing settings and statistics
jreiss 15:b50f92f1c6ff 510 */
jreiss 15:b50f92f1c6ff 511 typedef struct {
jreiss 15:b50f92f1c6ff 512 DeviceConfig Device;
jreiss 15:b50f92f1c6ff 513 NetworkConfig Network;
jreiss 15:b50f92f1c6ff 514 NetworkSession Session;
jreiss 15:b50f92f1c6ff 515 ApplicationConfig Applications[MAX_APPS];
jreiss 15:b50f92f1c6ff 516 MulticastSession Multicast[MAX_MULTICAST_SESSIONS];
jreiss 15:b50f92f1c6ff 517 Statistics Stats;
jreiss 15:b50f92f1c6ff 518 Testing Test;
jreiss 15:b50f92f1c6ff 519 } Settings;
jreiss 15:b50f92f1c6ff 520
jreiss 15:b50f92f1c6ff 521 /**
jreiss 15:b50f92f1c6ff 522 * Downlink settings sent in Join Accept message
jreiss 15:b50f92f1c6ff 523 */
jreiss 15:b50f92f1c6ff 524 typedef union {
jreiss 15:b50f92f1c6ff 525 uint8_t Value;
jreiss 15:b50f92f1c6ff 526 struct {
jreiss 15:b50f92f1c6ff 527 uint8_t Rx2Datarate :4;
jreiss 15:b50f92f1c6ff 528 uint8_t Rx1Offset :3;
jreiss 15:b50f92f1c6ff 529 uint8_t RFU :1;
jreiss 15:b50f92f1c6ff 530 };
jreiss 15:b50f92f1c6ff 531 } DownlinkSettings;
jreiss 15:b50f92f1c6ff 532
jreiss 15:b50f92f1c6ff 533 /**
jreiss 15:b50f92f1c6ff 534 * Frame structure for Join Request
jreiss 15:b50f92f1c6ff 535 */
jreiss 15:b50f92f1c6ff 536 typedef struct {
jreiss 15:b50f92f1c6ff 537 uint8_t Type;
jreiss 15:b50f92f1c6ff 538 uint8_t AppEUI[8];
jreiss 15:b50f92f1c6ff 539 uint8_t DevEUI[8];
jreiss 15:b50f92f1c6ff 540 uint8_t Nonce[2];
jreiss 15:b50f92f1c6ff 541 uint8_t MIC[4];
jreiss 15:b50f92f1c6ff 542 } JoinRequestFrame;
jreiss 15:b50f92f1c6ff 543
jreiss 15:b50f92f1c6ff 544 /**
jreiss 15:b50f92f1c6ff 545 * Mac header of uplink and downlink packets
jreiss 15:b50f92f1c6ff 546 */
jreiss 15:b50f92f1c6ff 547 typedef union {
jreiss 15:b50f92f1c6ff 548 uint8_t Value;
jreiss 15:b50f92f1c6ff 549 struct {
jreiss 15:b50f92f1c6ff 550 uint8_t Major :2;
jreiss 15:b50f92f1c6ff 551 uint8_t RFU :3;
jreiss 15:b50f92f1c6ff 552 uint8_t MType :3;
jreiss 15:b50f92f1c6ff 553 } Bits;
jreiss 15:b50f92f1c6ff 554 } MacHeader;
jreiss 15:b50f92f1c6ff 555
jreiss 15:b50f92f1c6ff 556 /**
jreiss 15:b50f92f1c6ff 557 * Frame control field of uplink packets
jreiss 15:b50f92f1c6ff 558 */
jreiss 15:b50f92f1c6ff 559 typedef union {
jreiss 15:b50f92f1c6ff 560 uint8_t Value;
jreiss 15:b50f92f1c6ff 561 struct {
jreiss 15:b50f92f1c6ff 562 uint8_t OptionsLength :4;
jreiss 15:b50f92f1c6ff 563 uint8_t ClassB :1;
jreiss 15:b50f92f1c6ff 564 uint8_t Ack :1;
jreiss 15:b50f92f1c6ff 565 uint8_t AdrAckReq :1;
jreiss 15:b50f92f1c6ff 566 uint8_t Adr :1;
jreiss 15:b50f92f1c6ff 567 } Bits;
jreiss 15:b50f92f1c6ff 568 } UplinkControl;
jreiss 15:b50f92f1c6ff 569
jreiss 15:b50f92f1c6ff 570 /**
jreiss 15:b50f92f1c6ff 571 * Frame control field of downlink packets
jreiss 15:b50f92f1c6ff 572 */
jreiss 15:b50f92f1c6ff 573 typedef union {
jreiss 15:b50f92f1c6ff 574 uint8_t Value;
jreiss 15:b50f92f1c6ff 575 struct {
jreiss 15:b50f92f1c6ff 576 uint8_t OptionsLength :4;
jreiss 15:b50f92f1c6ff 577 uint8_t FPending :1;
jreiss 15:b50f92f1c6ff 578 uint8_t Ack :1;
jreiss 15:b50f92f1c6ff 579 uint8_t RFU :1;
jreiss 15:b50f92f1c6ff 580 uint8_t Adr :1;
jreiss 15:b50f92f1c6ff 581 } Bits;
jreiss 15:b50f92f1c6ff 582 } DownlinkControl;
jreiss 15:b50f92f1c6ff 583
jreiss 15:b50f92f1c6ff 584 /**
jreiss 15:b50f92f1c6ff 585 * Frame type of packet
jreiss 15:b50f92f1c6ff 586 */
jreiss 15:b50f92f1c6ff 587 typedef enum {
jreiss 15:b50f92f1c6ff 588 FRAME_TYPE_JOIN_REQ = 0x00,
jreiss 15:b50f92f1c6ff 589 FRAME_TYPE_JOIN_ACCEPT = 0x01,
jreiss 15:b50f92f1c6ff 590 FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
jreiss 15:b50f92f1c6ff 591 FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
jreiss 15:b50f92f1c6ff 592 FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
jreiss 15:b50f92f1c6ff 593 FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
jreiss 15:b50f92f1c6ff 594 FRAME_TYPE_RFU = 0x06,
jreiss 15:b50f92f1c6ff 595 FRAME_TYPE_PROPRIETARY = 0x07,
jreiss 15:b50f92f1c6ff 596 } FrameType;
jreiss 15:b50f92f1c6ff 597
jreiss 15:b50f92f1c6ff 598 /**
jreiss 15:b50f92f1c6ff 599 * LoRaWAN mote MAC commands
jreiss 15:b50f92f1c6ff 600 */
jreiss 15:b50f92f1c6ff 601 typedef enum {
jreiss 15:b50f92f1c6ff 602 /* Class A */
jreiss 15:b50f92f1c6ff 603 MOTE_MAC_LINK_CHECK_REQ = 0x02,
jreiss 15:b50f92f1c6ff 604 MOTE_MAC_LINK_ADR_ANS = 0x03,
jreiss 15:b50f92f1c6ff 605 MOTE_MAC_DUTY_CYCLE_ANS = 0x04,
jreiss 15:b50f92f1c6ff 606 MOTE_MAC_RX_PARAM_SETUP_ANS = 0x05,
jreiss 15:b50f92f1c6ff 607 MOTE_MAC_DEV_STATUS_ANS = 0x06,
jreiss 15:b50f92f1c6ff 608 MOTE_MAC_NEW_CHANNEL_ANS = 0x07,
jreiss 15:b50f92f1c6ff 609 MOTE_MAC_RX_TIMING_SETUP_ANS = 0x08,
jreiss 15:b50f92f1c6ff 610
jreiss 15:b50f92f1c6ff 611 /* Class B */
jreiss 15:b50f92f1c6ff 612 MOTE_MAC_PING_SLOT_INFO_REQ = 0x09,
jreiss 15:b50f92f1c6ff 613 MOTE_MAC_PING_SLOT_FREQ_ANS = 0x0a,
jreiss 15:b50f92f1c6ff 614 MOTE_MAC_PING_SLOT_CHANNEL_ANS = 0x0a,
jreiss 15:b50f92f1c6ff 615 MOTE_MAC_BEACON_TIMING_REQ = 0x0b,
jreiss 15:b50f92f1c6ff 616 MOTE_MAC_BEACON_FREQ_ANS = 0x0c,
jreiss 15:b50f92f1c6ff 617
jreiss 15:b50f92f1c6ff 618 /* Multitech */
jreiss 15:b50f92f1c6ff 619 MOTE_MAC_PING_REQ = 0x80,
jreiss 15:b50f92f1c6ff 620 MOTE_MAC_CHANGE_CLASS = 0x81,
jreiss 15:b50f92f1c6ff 621 MOTE_MAC_MULTIPART_START_REQ = 0x82,
jreiss 15:b50f92f1c6ff 622 MOTE_MAC_MULTIPART_START_ANS = 0x83,
jreiss 15:b50f92f1c6ff 623 MOTE_MAC_MULTIPART_CHUNK = 0x84,
jreiss 15:b50f92f1c6ff 624 MOTE_MAC_MULTIPART_END_REQ = 0x85,
jreiss 15:b50f92f1c6ff 625 MOTE_MAC_MULTIPART_END_ANS = 0x86
jreiss 15:b50f92f1c6ff 626 } MoteCommand;
jreiss 15:b50f92f1c6ff 627
jreiss 15:b50f92f1c6ff 628 /*!
jreiss 15:b50f92f1c6ff 629 * LoRaWAN server MAC commands
jreiss 15:b50f92f1c6ff 630 */
jreiss 15:b50f92f1c6ff 631 typedef enum {
jreiss 15:b50f92f1c6ff 632 /* Class A */
jreiss 15:b50f92f1c6ff 633 SRV_MAC_LINK_CHECK_ANS = 0x02,
jreiss 15:b50f92f1c6ff 634 SRV_MAC_LINK_ADR_REQ = 0x03,
jreiss 15:b50f92f1c6ff 635 SRV_MAC_DUTY_CYCLE_REQ = 0x04,
jreiss 15:b50f92f1c6ff 636 SRV_MAC_RX_PARAM_SETUP_REQ = 0x05,
jreiss 15:b50f92f1c6ff 637 SRV_MAC_DEV_STATUS_REQ = 0x06,
jreiss 15:b50f92f1c6ff 638 SRV_MAC_NEW_CHANNEL_REQ = 0x07,
jreiss 15:b50f92f1c6ff 639 SRV_MAC_RX_TIMING_SETUP_REQ = 0x08,
jreiss 15:b50f92f1c6ff 640
jreiss 15:b50f92f1c6ff 641 /* Class B */
jreiss 15:b50f92f1c6ff 642 SRV_MAC_PING_SLOT_INFO_ANS = 0x09,
jreiss 15:b50f92f1c6ff 643 SRV_MAC_PING_SLOT_FREQ_REQ = 0x0a,
jreiss 15:b50f92f1c6ff 644 SRV_MAC_PING_SLOT_CHANNEL_REQ = 0x0a,
jreiss 15:b50f92f1c6ff 645 SRV_MAC_BEACON_TIMING_ANS = 0x0b,
jreiss 15:b50f92f1c6ff 646 SRV_MAC_BEACON_FREQ_REQ = 0x0c,
jreiss 15:b50f92f1c6ff 647
jreiss 15:b50f92f1c6ff 648 /* Multitech */
jreiss 15:b50f92f1c6ff 649 SRV_MAC_PING_ANS = 0x80,
jreiss 15:b50f92f1c6ff 650 SRV_MAC_CHANGE_CLASS = 0x81,
jreiss 15:b50f92f1c6ff 651 SRV_MAC_MULTIPART_START_REQ = 0x82,
jreiss 15:b50f92f1c6ff 652 SRV_MAC_MULTIPART_START_ANS = 0x83,
jreiss 15:b50f92f1c6ff 653 SRV_MAC_MULTIPART_CHUNK = 0x84,
jreiss 15:b50f92f1c6ff 654 SRV_MAC_MULTIPART_END_REQ = 0x85,
jreiss 15:b50f92f1c6ff 655 SRV_MAC_MULTIPART_END_ANS = 0x86
jreiss 15:b50f92f1c6ff 656 } ServerCommand;
jreiss 15:b50f92f1c6ff 657
jreiss 15:b50f92f1c6ff 658 /**
jreiss 15:b50f92f1c6ff 659 * Random seed for software RNG
jreiss 15:b50f92f1c6ff 660 */
jreiss 15:b50f92f1c6ff 661 void srand(uint32_t seed);
jreiss 15:b50f92f1c6ff 662
jreiss 15:b50f92f1c6ff 663 /**
jreiss 15:b50f92f1c6ff 664 * Software RNG for consistent results across differing hardware
jreiss 15:b50f92f1c6ff 665 */
jreiss 15:b50f92f1c6ff 666 int rand(void);
jreiss 15:b50f92f1c6ff 667
jreiss 15:b50f92f1c6ff 668 /**
jreiss 15:b50f92f1c6ff 669 * Generate random number bounded by min and max
jreiss 15:b50f92f1c6ff 670 */
jreiss 15:b50f92f1c6ff 671 int32_t rand_r(int32_t min, int32_t max);
jreiss 15:b50f92f1c6ff 672
jreiss 15:b50f92f1c6ff 673 uint8_t CountBits(uint16_t mask);
jreiss 15:b50f92f1c6ff 674
jreiss 15:b50f92f1c6ff 675 /**
jreiss 15:b50f92f1c6ff 676 * Copy 3-bytes network order from array into LSB of integer value
jreiss 15:b50f92f1c6ff 677 */
jreiss 15:b50f92f1c6ff 678 void CopyNetIDtoInt(const uint8_t* arr, uint32_t& val);
jreiss 15:b50f92f1c6ff 679
jreiss 15:b50f92f1c6ff 680 /**
jreiss 15:b50f92f1c6ff 681 * Copy LSB 3-bytes from integer value into array network order
jreiss 15:b50f92f1c6ff 682 */
jreiss 15:b50f92f1c6ff 683 void CopyNetIDtoArray(uint32_t val, uint8_t* arr);
jreiss 15:b50f92f1c6ff 684
jreiss 15:b50f92f1c6ff 685 /**
jreiss 15:b50f92f1c6ff 686 * Copy 4-bytes network order from array in to integer value
jreiss 15:b50f92f1c6ff 687 */
jreiss 15:b50f92f1c6ff 688 void CopyAddrtoInt(const uint8_t* arr, uint32_t& val);
jreiss 15:b50f92f1c6ff 689
jreiss 15:b50f92f1c6ff 690 /**
jreiss 15:b50f92f1c6ff 691 * Copy 4-bytes from integer in to array network order
jreiss 15:b50f92f1c6ff 692 */
jreiss 15:b50f92f1c6ff 693 void CopyAddrtoArray(uint32_t val, uint8_t* arr);
jreiss 15:b50f92f1c6ff 694
jreiss 15:b50f92f1c6ff 695 /**
jreiss 15:b50f92f1c6ff 696 * Copy 3-bytes network order from array into integer value and multiply by 100
jreiss 15:b50f92f1c6ff 697 */
jreiss 15:b50f92f1c6ff 698 void CopyFreqtoInt(const uint8_t* arr, uint32_t& freq);
jreiss 15:b50f92f1c6ff 699
jreiss 15:b50f92f1c6ff 700 /**
jreiss 15:b50f92f1c6ff 701 * Reverse memory copy
jreiss 15:b50f92f1c6ff 702 */
jreiss 15:b50f92f1c6ff 703 void memcpy_r(uint8_t *dst, const uint8_t *src, size_t n);
jreiss 15:b50f92f1c6ff 704
jreiss 15:b50f92f1c6ff 705 }
jreiss 15:b50f92f1c6ff 706
jreiss 15:b50f92f1c6ff 707 #endif
jreiss 15:b50f92f1c6ff 708
jreiss 15:b50f92f1c6ff 709