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:
Jason Reiss
Date:
Wed Aug 31 11:56:20 2016 -0500
Revision:
17:dd0f69240713
Parent:
15:b50f92f1c6ff
Child:
18:aa39c917aa3f
Query channel plan for datarate details, remove US915 and EU868 from library

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,
Jason Reiss 17:dd0f69240713 263 LORA_MAC_COMMAND_ERROR = 17,
Jason Reiss 17:dd0f69240713 264 LORA_MAX_PAYLOAD_EXCEEDED = 18
jreiss 15:b50f92f1c6ff 265 };
jreiss 15:b50f92f1c6ff 266
jreiss 15:b50f92f1c6ff 267 /**
jreiss 15:b50f92f1c6ff 268 * State for Link
jreiss 15:b50f92f1c6ff 269 */
jreiss 15:b50f92f1c6ff 270 enum LinkState {
jreiss 15:b50f92f1c6ff 271 LINK_IDLE = 0, //!< Link ready to send or receive
jreiss 15:b50f92f1c6ff 272 LINK_TX, //!< Link is busy sending
jreiss 15:b50f92f1c6ff 273 LINK_ACK_TX, //!< Link is busy resending after missed ACK
jreiss 15:b50f92f1c6ff 274 LINK_REP_TX, //!< Link is busy repeating
jreiss 15:b50f92f1c6ff 275 LINK_RX, //!< Link has receive window open
jreiss 15:b50f92f1c6ff 276 LINK_RX1, //!< Link has first received window open
jreiss 15:b50f92f1c6ff 277 LINK_RX2, //!< Link has second received window open
jreiss 15:b50f92f1c6ff 278 LINK_RXC, //!< Link has class C received window open
jreiss 15:b50f92f1c6ff 279 LINK_P2P, //!< Link is busy sending
jreiss 15:b50f92f1c6ff 280 };
jreiss 15:b50f92f1c6ff 281
jreiss 15:b50f92f1c6ff 282 /**
jreiss 15:b50f92f1c6ff 283 * State for MAC
jreiss 15:b50f92f1c6ff 284 */
jreiss 15:b50f92f1c6ff 285 enum MacState {
jreiss 15:b50f92f1c6ff 286 MAC_IDLE,
jreiss 15:b50f92f1c6ff 287 MAC_RX1,
jreiss 15:b50f92f1c6ff 288 MAC_RX2,
jreiss 15:b50f92f1c6ff 289 MAC_RXC,
jreiss 15:b50f92f1c6ff 290 MAC_TX,
jreiss 15:b50f92f1c6ff 291 MAC_JOIN
jreiss 15:b50f92f1c6ff 292 };
jreiss 15:b50f92f1c6ff 293
jreiss 15:b50f92f1c6ff 294 /**
jreiss 15:b50f92f1c6ff 295 * Operation class for device
jreiss 15:b50f92f1c6ff 296 */
jreiss 15:b50f92f1c6ff 297 enum MoteClass {
jreiss 15:b50f92f1c6ff 298 CLASS_A = 0x00, //!< Device can only receive in windows opened after a transmit
jreiss 15:b50f92f1c6ff 299 CLASS_B = 0x01, //!< Device can receive in windows sychronized with gateway beacon
jreiss 15:b50f92f1c6ff 300 CLASS_C = 0x02 //!< Device can receive any time when not transmitting
jreiss 15:b50f92f1c6ff 301 };
jreiss 15:b50f92f1c6ff 302
jreiss 15:b50f92f1c6ff 303 /**
jreiss 15:b50f92f1c6ff 304 * Direction of a packet
jreiss 15:b50f92f1c6ff 305 */
jreiss 15:b50f92f1c6ff 306 enum Direction {
jreiss 15:b50f92f1c6ff 307 DIR_UP = 0, //!< Packet is sent from mote to gateway
jreiss 15:b50f92f1c6ff 308 DIR_DOWN = 1, //!< Packet was received from gateway
jreiss 15:b50f92f1c6ff 309 DIR_PEER = 2 //!< Packet was received from peer
jreiss 15:b50f92f1c6ff 310 };
jreiss 15:b50f92f1c6ff 311
jreiss 15:b50f92f1c6ff 312
jreiss 15:b50f92f1c6ff 313 /**
jreiss 15:b50f92f1c6ff 314 * Received window used by Link
jreiss 15:b50f92f1c6ff 315 */
jreiss 15:b50f92f1c6ff 316 enum ReceiveWindows {
jreiss 15:b50f92f1c6ff 317 RX_1 = 1, //!< First receive window
jreiss 15:b50f92f1c6ff 318 RX_2, //!< Second receive window
jreiss 15:b50f92f1c6ff 319 RX_BEACON, //!< Beacon receive window
jreiss 15:b50f92f1c6ff 320 RX_SLOT, //!< Beacon Slot receive window
jreiss 15:b50f92f1c6ff 321 RX_TEST
jreiss 15:b50f92f1c6ff 322 };
jreiss 15:b50f92f1c6ff 323
jreiss 15:b50f92f1c6ff 324 /**
jreiss 15:b50f92f1c6ff 325 * Datarate range for a Channel
jreiss 15:b50f92f1c6ff 326 */
jreiss 15:b50f92f1c6ff 327 typedef union {
jreiss 15:b50f92f1c6ff 328 int8_t Value;
jreiss 15:b50f92f1c6ff 329 struct {
jreiss 15:b50f92f1c6ff 330 int8_t Min :4;
jreiss 15:b50f92f1c6ff 331 int8_t Max :4;
jreiss 15:b50f92f1c6ff 332 } Fields;
jreiss 15:b50f92f1c6ff 333 } DatarateRange;
jreiss 15:b50f92f1c6ff 334
jreiss 15:b50f92f1c6ff 335 /**
jreiss 15:b50f92f1c6ff 336 * Datarate used for transmitting and receiving
jreiss 15:b50f92f1c6ff 337 */
jreiss 15:b50f92f1c6ff 338 typedef struct Datarate {
jreiss 15:b50f92f1c6ff 339 uint8_t Index;
jreiss 15:b50f92f1c6ff 340 uint8_t Bandwidth;
jreiss 15:b50f92f1c6ff 341 uint8_t Coderate;
jreiss 15:b50f92f1c6ff 342 uint8_t PreambleLength;
jreiss 15:b50f92f1c6ff 343 uint8_t SpreadingFactor;
jreiss 15:b50f92f1c6ff 344 uint8_t Crc;
jreiss 15:b50f92f1c6ff 345 uint8_t TxIQ;
jreiss 15:b50f92f1c6ff 346 uint8_t RxIQ;
jreiss 15:b50f92f1c6ff 347 uint8_t SymbolTimeout();
jreiss 15:b50f92f1c6ff 348 Datarate();
jreiss 15:b50f92f1c6ff 349 } Datarate;
jreiss 15:b50f92f1c6ff 350
jreiss 15:b50f92f1c6ff 351 /**
jreiss 15:b50f92f1c6ff 352 * Channel used for transmitting
jreiss 15:b50f92f1c6ff 353 */
jreiss 15:b50f92f1c6ff 354 typedef struct {
jreiss 15:b50f92f1c6ff 355 uint8_t Index;
jreiss 15:b50f92f1c6ff 356 uint32_t Frequency;
jreiss 15:b50f92f1c6ff 357 DatarateRange DrRange;
jreiss 15:b50f92f1c6ff 358 } Channel;
jreiss 15:b50f92f1c6ff 359
jreiss 15:b50f92f1c6ff 360 /**
jreiss 15:b50f92f1c6ff 361 * Receive window
jreiss 15:b50f92f1c6ff 362 */
jreiss 15:b50f92f1c6ff 363 typedef struct {
jreiss 15:b50f92f1c6ff 364 uint8_t Index;
jreiss 15:b50f92f1c6ff 365 uint32_t Frequency;
jreiss 15:b50f92f1c6ff 366 uint8_t DatarateIndex;
jreiss 15:b50f92f1c6ff 367 } RxWindow;
jreiss 15:b50f92f1c6ff 368
jreiss 15:b50f92f1c6ff 369 /**
jreiss 15:b50f92f1c6ff 370 * Duty band for limiting time-on-air for regional regulations
jreiss 15:b50f92f1c6ff 371 */
jreiss 15:b50f92f1c6ff 372 typedef struct {
jreiss 15:b50f92f1c6ff 373 uint8_t Index;
jreiss 15:b50f92f1c6ff 374 uint32_t FrequencyMin;
jreiss 15:b50f92f1c6ff 375 uint32_t FrequencyMax;
jreiss 15:b50f92f1c6ff 376 uint8_t PowerMax;
jreiss 15:b50f92f1c6ff 377 uint16_t DutyCycle; //!< Multiplier of time on air, 0:100%, 1:50%, 2:33%, 10:10%, 100:1%, 1000,0.1%
jreiss 15:b50f92f1c6ff 378 uint32_t TimeOffEnd; //!< Timestamp when this band will be available
jreiss 15:b50f92f1c6ff 379 } DutyBand;
jreiss 15:b50f92f1c6ff 380
jreiss 15:b50f92f1c6ff 381 /**
jreiss 15:b50f92f1c6ff 382 * Device configuration
jreiss 15:b50f92f1c6ff 383 */
jreiss 15:b50f92f1c6ff 384 typedef struct {
jreiss 15:b50f92f1c6ff 385 uint8_t FrequencyBand; //!< Used to choose ChannelPlan
jreiss 15:b50f92f1c6ff 386 uint8_t EUI[8]; //!< Unique identifier assigned to device
jreiss 15:b50f92f1c6ff 387 } DeviceConfig;
jreiss 15:b50f92f1c6ff 388
jreiss 15:b50f92f1c6ff 389 /**
jreiss 15:b50f92f1c6ff 390 * Network configuration
jreiss 15:b50f92f1c6ff 391 */
jreiss 15:b50f92f1c6ff 392 typedef struct {
jreiss 15:b50f92f1c6ff 393 uint8_t Mode; //!< PUBLIC, PRIVATE or PEER_TO_PEER network mode
jreiss 15:b50f92f1c6ff 394 uint8_t Class; //!< Operating class of device
jreiss 15:b50f92f1c6ff 395 uint8_t EUI[8]; //!< Network ID or AppEUI
jreiss 15:b50f92f1c6ff 396 uint8_t Key[16]; //!< Network Key or AppKey
jreiss 15:b50f92f1c6ff 397 uint8_t JoinDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 398 uint8_t RxDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 399 uint8_t ChannelGroup; //!< ChannelGroup used for US915 hybrid operation 0:72 channels, 1:1-8 channels ...
jreiss 15:b50f92f1c6ff 400 uint8_t AckAttempts; //!< Number of attempts to send packet and receive an ACK from server
jreiss 15:b50f92f1c6ff 401 uint8_t Retries; //!< Number of times to resend a packet without receiving an ACK, redundancy
jreiss 15:b50f92f1c6ff 402 uint8_t ADREnabled; //!< Enable adaptive datarate
jreiss 15:b50f92f1c6ff 403 uint8_t CADEnabled; //!< Enable listen before talk/channel activity detection
jreiss 15:b50f92f1c6ff 404 uint8_t RepeaterMode; //!< Limit payloads to repeater compatible sizes
jreiss 15:b50f92f1c6ff 405 uint8_t TxPower; //!< Default radio output power in dBm
jreiss 15:b50f92f1c6ff 406 uint8_t TxPowerMax; //!< Max transmit power
jreiss 15:b50f92f1c6ff 407 uint8_t TxDatarate; //!< Datarate for P2P transmit
jreiss 15:b50f92f1c6ff 408 uint32_t TxFrequency; //!< Frequency for P2P transmit
jreiss 15:b50f92f1c6ff 409 int8_t AntennaGain; //!< Antenna Gain
jreiss 15:b50f92f1c6ff 410 uint8_t DisableEncryption; //!< Disable Encryption
jreiss 15:b50f92f1c6ff 411 uint8_t DisableCRC; //!< Disable CRC on uplink packets
jreiss 15:b50f92f1c6ff 412 uint16_t P2PACKTimeout;
jreiss 15:b50f92f1c6ff 413 uint16_t P2PACKBackoff;
jreiss 15:b50f92f1c6ff 414 uint8_t JoinRx1DatarateOffset; //!< Offset for datarate for first window
jreiss 15:b50f92f1c6ff 415 uint32_t JoinRx2Frequency; //!< Frequency used in second window
jreiss 15:b50f92f1c6ff 416 uint8_t JoinRx2DatarateIndex; //!< Datarate for second window
jreiss 15:b50f92f1c6ff 417 } NetworkConfig;
jreiss 15:b50f92f1c6ff 418
jreiss 15:b50f92f1c6ff 419 /**
jreiss 15:b50f92f1c6ff 420 * Network session info
jreiss 15:b50f92f1c6ff 421 * Some settings are acquired in join message and others may be changed through Mac Commands from server
jreiss 15:b50f92f1c6ff 422 */
jreiss 15:b50f92f1c6ff 423 typedef struct {
jreiss 15:b50f92f1c6ff 424 uint8_t Joined; //!< State of session
jreiss 15:b50f92f1c6ff 425 uint8_t Rx1DatarateOffset; //!< Offset for datarate for first window
jreiss 15:b50f92f1c6ff 426 uint32_t Rx2Frequency; //!< Frequency used in second window
jreiss 15:b50f92f1c6ff 427 uint8_t Rx2DatarateIndex; //!< Datarate for second window
jreiss 15:b50f92f1c6ff 428 uint8_t TxPower; //!< Current total radiated output power in dBm
jreiss 15:b50f92f1c6ff 429 uint8_t TxDatarate; //!< Current datarate can be changed when ADR is enabled
jreiss 15:b50f92f1c6ff 430 uint32_t Address; //!< Network address
jreiss 15:b50f92f1c6ff 431 uint32_t NetworkID; //!< Network ID 24-bits
jreiss 15:b50f92f1c6ff 432 uint8_t NetworkSessionKey[16]; //!< Network session key
jreiss 15:b50f92f1c6ff 433 uint8_t ApplicationSessionKey[16]; //!< Data session key
jreiss 15:b50f92f1c6ff 434 uint16_t ChannelMask[4]; //!< Current channel mask
jreiss 15:b50f92f1c6ff 435 uint16_t ChannelMask500k; //!< Current channel mask for 500k channels
jreiss 15:b50f92f1c6ff 436 uint32_t DownlinkCounter; //!< Downlink counter of last packet received from server
jreiss 15:b50f92f1c6ff 437 uint32_t UplinkCounter; //!< Uplink counter of last packet received from server
jreiss 15:b50f92f1c6ff 438 uint8_t Redundancy; //!< Number of time to repeat an uplink
jreiss 15:b50f92f1c6ff 439 uint8_t MaxDutyCycle; //!< Current Max Duty Cycle value
jreiss 15:b50f92f1c6ff 440 uint32_t JoinTimeOnAir; //!< Balance of time on air used during join attempts
jreiss 15:b50f92f1c6ff 441 uint32_t JoinTimeOffEnd; //!< RTC time of next join attempt
jreiss 15:b50f92f1c6ff 442 uint32_t JoinFirstAttempt; //!< RTC time of first failed join attempt
jreiss 15:b50f92f1c6ff 443 uint32_t AggregatedTimeOffEnd; //!< Time off air expiration for aggregate duty cycle
jreiss 15:b50f92f1c6ff 444 uint16_t AggregateDutyCycle; //!< Used for enforcing time-on-air
jreiss 15:b50f92f1c6ff 445 uint8_t AckCounter; //!< Current number of packets sent without ACK from server
jreiss 15:b50f92f1c6ff 446 uint8_t AdrCounter; //!< Current number of packets received without downlink from server
jreiss 15:b50f92f1c6ff 447 uint8_t RxDelay; //!< Number of seconds to wait before 1st RX Window
jreiss 15:b50f92f1c6ff 448 uint8_t CommandBuffer[COMMANDS_BUFFER_SIZE]; //!< Buffer to hold Mac Commands and parameters to be sent in next packet
jreiss 15:b50f92f1c6ff 449 uint8_t CommandBufferIndex; //!< Index to place next Mac Command, also current size of Command Buffer
jreiss 15:b50f92f1c6ff 450 bool SrvRequestedAck; //!< Indicator of ACK requested by server in last packet received
jreiss 15:b50f92f1c6ff 451 bool DataPending; //!< Indicator of data pending at server
jreiss 15:b50f92f1c6ff 452 uint8_t RxTimingSetupReqReceived; //!< Indicator that RxTimingSetupAns should be included in uplink
jreiss 15:b50f92f1c6ff 453 uint8_t RxParamSetupReqAnswer; //!< Indicator that RxParamSetupAns should be included in uplink
jreiss 15:b50f92f1c6ff 454 } NetworkSession;
jreiss 15:b50f92f1c6ff 455
jreiss 15:b50f92f1c6ff 456 /**
jreiss 15:b50f92f1c6ff 457 * Multicast session info
jreiss 15:b50f92f1c6ff 458 */
jreiss 15:b50f92f1c6ff 459 typedef struct {
jreiss 15:b50f92f1c6ff 460 uint32_t Address; //!< Network address
jreiss 15:b50f92f1c6ff 461 uint8_t NetworkSessionKey[16]; //!< Network session key
jreiss 15:b50f92f1c6ff 462 uint8_t DataSessionKey[16]; //!< Data session key
jreiss 15:b50f92f1c6ff 463 uint32_t DownlinkCounter; //!< Downlink counter of last packet received from server
jreiss 15:b50f92f1c6ff 464 } MulticastSession;
jreiss 15:b50f92f1c6ff 465
jreiss 15:b50f92f1c6ff 466 /**
jreiss 15:b50f92f1c6ff 467 * Application configuration
jreiss 15:b50f92f1c6ff 468 */
jreiss 15:b50f92f1c6ff 469 typedef struct {
jreiss 15:b50f92f1c6ff 470 uint8_t Port; //!< Port used by application
jreiss 15:b50f92f1c6ff 471 uint8_t AppEUI; //!< Application ID
jreiss 15:b50f92f1c6ff 472 uint8_t AppKey[16]; //!< Application Key
jreiss 15:b50f92f1c6ff 473 } ApplicationConfig;
jreiss 15:b50f92f1c6ff 474
jreiss 15:b50f92f1c6ff 475 /**
jreiss 15:b50f92f1c6ff 476 * Statistics of current network session
jreiss 15:b50f92f1c6ff 477 */
jreiss 15:b50f92f1c6ff 478 typedef struct Statistics {
jreiss 15:b50f92f1c6ff 479 uint32_t Up; //!< Number of uplink packets sent
jreiss 15:b50f92f1c6ff 480 uint32_t Down; //!< Number of downlink packets received
jreiss 15:b50f92f1c6ff 481 uint32_t Joins; //!< Number of join requests sent
jreiss 15:b50f92f1c6ff 482 uint32_t JoinFails; //!< Number of join requests without response or invalid response
jreiss 15:b50f92f1c6ff 483 uint32_t MissedAcks; //!< Number of missed acknowledgement attempts of confirmed packets
jreiss 15:b50f92f1c6ff 484 uint32_t CRCErrors; //!< Number of CRC errors in received packets
jreiss 15:b50f92f1c6ff 485 int32_t AvgCount; //!< Number of packets used to compute rolling average of RSSI and SNR
jreiss 15:b50f92f1c6ff 486 int16_t Rssi; //!< RSSI of last packet received
jreiss 15:b50f92f1c6ff 487 int16_t RssiMin; //!< Minimum RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 488 int16_t RssiMax; //!< Maximum RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 489 int16_t RssiAvg; //!< Rolling average RSSI of last AvgCount packets
jreiss 15:b50f92f1c6ff 490 int16_t Snr; //!< SNR of last packet received
jreiss 15:b50f92f1c6ff 491 int16_t SnrMin; //!< Minimum SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 492 int16_t SnrMax; //!< Maximum SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 493 int16_t SnrAvg; //!< Rolling average SNR of last AvgCount packets
jreiss 15:b50f92f1c6ff 494 } Statistics;
jreiss 15:b50f92f1c6ff 495
jreiss 15:b50f92f1c6ff 496 /**
jreiss 15:b50f92f1c6ff 497 * Testing settings
jreiss 15:b50f92f1c6ff 498 */
jreiss 15:b50f92f1c6ff 499 typedef struct {
jreiss 15:b50f92f1c6ff 500 uint8_t TestMode;
jreiss 15:b50f92f1c6ff 501 uint8_t SkipMICCheck;
jreiss 15:b50f92f1c6ff 502 uint8_t DisableDutyCycle;
jreiss 15:b50f92f1c6ff 503 uint8_t DisableRx1;
jreiss 15:b50f92f1c6ff 504 uint8_t DisableRx2;
jreiss 15:b50f92f1c6ff 505 uint8_t FixedUplinkCounter;
jreiss 15:b50f92f1c6ff 506 uint8_t DisableRandomJoinDatarate;
jreiss 15:b50f92f1c6ff 507 } Testing;
jreiss 15:b50f92f1c6ff 508
jreiss 15:b50f92f1c6ff 509 /**
jreiss 15:b50f92f1c6ff 510 * Combination of device, network, testing settings and statistics
jreiss 15:b50f92f1c6ff 511 */
jreiss 15:b50f92f1c6ff 512 typedef struct {
jreiss 15:b50f92f1c6ff 513 DeviceConfig Device;
jreiss 15:b50f92f1c6ff 514 NetworkConfig Network;
jreiss 15:b50f92f1c6ff 515 NetworkSession Session;
jreiss 15:b50f92f1c6ff 516 ApplicationConfig Applications[MAX_APPS];
jreiss 15:b50f92f1c6ff 517 MulticastSession Multicast[MAX_MULTICAST_SESSIONS];
jreiss 15:b50f92f1c6ff 518 Statistics Stats;
jreiss 15:b50f92f1c6ff 519 Testing Test;
jreiss 15:b50f92f1c6ff 520 } Settings;
jreiss 15:b50f92f1c6ff 521
jreiss 15:b50f92f1c6ff 522 /**
jreiss 15:b50f92f1c6ff 523 * Downlink settings sent in Join Accept message
jreiss 15:b50f92f1c6ff 524 */
jreiss 15:b50f92f1c6ff 525 typedef union {
jreiss 15:b50f92f1c6ff 526 uint8_t Value;
jreiss 15:b50f92f1c6ff 527 struct {
jreiss 15:b50f92f1c6ff 528 uint8_t Rx2Datarate :4;
jreiss 15:b50f92f1c6ff 529 uint8_t Rx1Offset :3;
jreiss 15:b50f92f1c6ff 530 uint8_t RFU :1;
jreiss 15:b50f92f1c6ff 531 };
jreiss 15:b50f92f1c6ff 532 } DownlinkSettings;
jreiss 15:b50f92f1c6ff 533
jreiss 15:b50f92f1c6ff 534 /**
jreiss 15:b50f92f1c6ff 535 * Frame structure for Join Request
jreiss 15:b50f92f1c6ff 536 */
jreiss 15:b50f92f1c6ff 537 typedef struct {
jreiss 15:b50f92f1c6ff 538 uint8_t Type;
jreiss 15:b50f92f1c6ff 539 uint8_t AppEUI[8];
jreiss 15:b50f92f1c6ff 540 uint8_t DevEUI[8];
jreiss 15:b50f92f1c6ff 541 uint8_t Nonce[2];
jreiss 15:b50f92f1c6ff 542 uint8_t MIC[4];
jreiss 15:b50f92f1c6ff 543 } JoinRequestFrame;
jreiss 15:b50f92f1c6ff 544
jreiss 15:b50f92f1c6ff 545 /**
jreiss 15:b50f92f1c6ff 546 * Mac header of uplink and downlink packets
jreiss 15:b50f92f1c6ff 547 */
jreiss 15:b50f92f1c6ff 548 typedef union {
jreiss 15:b50f92f1c6ff 549 uint8_t Value;
jreiss 15:b50f92f1c6ff 550 struct {
jreiss 15:b50f92f1c6ff 551 uint8_t Major :2;
jreiss 15:b50f92f1c6ff 552 uint8_t RFU :3;
jreiss 15:b50f92f1c6ff 553 uint8_t MType :3;
jreiss 15:b50f92f1c6ff 554 } Bits;
jreiss 15:b50f92f1c6ff 555 } MacHeader;
jreiss 15:b50f92f1c6ff 556
jreiss 15:b50f92f1c6ff 557 /**
jreiss 15:b50f92f1c6ff 558 * Frame control field of uplink packets
jreiss 15:b50f92f1c6ff 559 */
jreiss 15:b50f92f1c6ff 560 typedef union {
jreiss 15:b50f92f1c6ff 561 uint8_t Value;
jreiss 15:b50f92f1c6ff 562 struct {
jreiss 15:b50f92f1c6ff 563 uint8_t OptionsLength :4;
jreiss 15:b50f92f1c6ff 564 uint8_t ClassB :1;
jreiss 15:b50f92f1c6ff 565 uint8_t Ack :1;
jreiss 15:b50f92f1c6ff 566 uint8_t AdrAckReq :1;
jreiss 15:b50f92f1c6ff 567 uint8_t Adr :1;
jreiss 15:b50f92f1c6ff 568 } Bits;
jreiss 15:b50f92f1c6ff 569 } UplinkControl;
jreiss 15:b50f92f1c6ff 570
jreiss 15:b50f92f1c6ff 571 /**
jreiss 15:b50f92f1c6ff 572 * Frame control field of downlink packets
jreiss 15:b50f92f1c6ff 573 */
jreiss 15:b50f92f1c6ff 574 typedef union {
jreiss 15:b50f92f1c6ff 575 uint8_t Value;
jreiss 15:b50f92f1c6ff 576 struct {
jreiss 15:b50f92f1c6ff 577 uint8_t OptionsLength :4;
jreiss 15:b50f92f1c6ff 578 uint8_t FPending :1;
jreiss 15:b50f92f1c6ff 579 uint8_t Ack :1;
jreiss 15:b50f92f1c6ff 580 uint8_t RFU :1;
jreiss 15:b50f92f1c6ff 581 uint8_t Adr :1;
jreiss 15:b50f92f1c6ff 582 } Bits;
jreiss 15:b50f92f1c6ff 583 } DownlinkControl;
jreiss 15:b50f92f1c6ff 584
jreiss 15:b50f92f1c6ff 585 /**
jreiss 15:b50f92f1c6ff 586 * Frame type of packet
jreiss 15:b50f92f1c6ff 587 */
jreiss 15:b50f92f1c6ff 588 typedef enum {
jreiss 15:b50f92f1c6ff 589 FRAME_TYPE_JOIN_REQ = 0x00,
jreiss 15:b50f92f1c6ff 590 FRAME_TYPE_JOIN_ACCEPT = 0x01,
jreiss 15:b50f92f1c6ff 591 FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
jreiss 15:b50f92f1c6ff 592 FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
jreiss 15:b50f92f1c6ff 593 FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
jreiss 15:b50f92f1c6ff 594 FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
jreiss 15:b50f92f1c6ff 595 FRAME_TYPE_RFU = 0x06,
jreiss 15:b50f92f1c6ff 596 FRAME_TYPE_PROPRIETARY = 0x07,
jreiss 15:b50f92f1c6ff 597 } FrameType;
jreiss 15:b50f92f1c6ff 598
jreiss 15:b50f92f1c6ff 599 /**
jreiss 15:b50f92f1c6ff 600 * LoRaWAN mote MAC commands
jreiss 15:b50f92f1c6ff 601 */
jreiss 15:b50f92f1c6ff 602 typedef enum {
jreiss 15:b50f92f1c6ff 603 /* Class A */
jreiss 15:b50f92f1c6ff 604 MOTE_MAC_LINK_CHECK_REQ = 0x02,
jreiss 15:b50f92f1c6ff 605 MOTE_MAC_LINK_ADR_ANS = 0x03,
jreiss 15:b50f92f1c6ff 606 MOTE_MAC_DUTY_CYCLE_ANS = 0x04,
jreiss 15:b50f92f1c6ff 607 MOTE_MAC_RX_PARAM_SETUP_ANS = 0x05,
jreiss 15:b50f92f1c6ff 608 MOTE_MAC_DEV_STATUS_ANS = 0x06,
jreiss 15:b50f92f1c6ff 609 MOTE_MAC_NEW_CHANNEL_ANS = 0x07,
jreiss 15:b50f92f1c6ff 610 MOTE_MAC_RX_TIMING_SETUP_ANS = 0x08,
jreiss 15:b50f92f1c6ff 611
jreiss 15:b50f92f1c6ff 612 /* Class B */
jreiss 15:b50f92f1c6ff 613 MOTE_MAC_PING_SLOT_INFO_REQ = 0x09,
jreiss 15:b50f92f1c6ff 614 MOTE_MAC_PING_SLOT_FREQ_ANS = 0x0a,
jreiss 15:b50f92f1c6ff 615 MOTE_MAC_PING_SLOT_CHANNEL_ANS = 0x0a,
jreiss 15:b50f92f1c6ff 616 MOTE_MAC_BEACON_TIMING_REQ = 0x0b,
jreiss 15:b50f92f1c6ff 617 MOTE_MAC_BEACON_FREQ_ANS = 0x0c,
jreiss 15:b50f92f1c6ff 618
jreiss 15:b50f92f1c6ff 619 /* Multitech */
jreiss 15:b50f92f1c6ff 620 MOTE_MAC_PING_REQ = 0x80,
jreiss 15:b50f92f1c6ff 621 MOTE_MAC_CHANGE_CLASS = 0x81,
jreiss 15:b50f92f1c6ff 622 MOTE_MAC_MULTIPART_START_REQ = 0x82,
jreiss 15:b50f92f1c6ff 623 MOTE_MAC_MULTIPART_START_ANS = 0x83,
jreiss 15:b50f92f1c6ff 624 MOTE_MAC_MULTIPART_CHUNK = 0x84,
jreiss 15:b50f92f1c6ff 625 MOTE_MAC_MULTIPART_END_REQ = 0x85,
jreiss 15:b50f92f1c6ff 626 MOTE_MAC_MULTIPART_END_ANS = 0x86
jreiss 15:b50f92f1c6ff 627 } MoteCommand;
jreiss 15:b50f92f1c6ff 628
jreiss 15:b50f92f1c6ff 629 /*!
jreiss 15:b50f92f1c6ff 630 * LoRaWAN server MAC commands
jreiss 15:b50f92f1c6ff 631 */
jreiss 15:b50f92f1c6ff 632 typedef enum {
jreiss 15:b50f92f1c6ff 633 /* Class A */
jreiss 15:b50f92f1c6ff 634 SRV_MAC_LINK_CHECK_ANS = 0x02,
jreiss 15:b50f92f1c6ff 635 SRV_MAC_LINK_ADR_REQ = 0x03,
jreiss 15:b50f92f1c6ff 636 SRV_MAC_DUTY_CYCLE_REQ = 0x04,
jreiss 15:b50f92f1c6ff 637 SRV_MAC_RX_PARAM_SETUP_REQ = 0x05,
jreiss 15:b50f92f1c6ff 638 SRV_MAC_DEV_STATUS_REQ = 0x06,
jreiss 15:b50f92f1c6ff 639 SRV_MAC_NEW_CHANNEL_REQ = 0x07,
jreiss 15:b50f92f1c6ff 640 SRV_MAC_RX_TIMING_SETUP_REQ = 0x08,
jreiss 15:b50f92f1c6ff 641
jreiss 15:b50f92f1c6ff 642 /* Class B */
jreiss 15:b50f92f1c6ff 643 SRV_MAC_PING_SLOT_INFO_ANS = 0x09,
jreiss 15:b50f92f1c6ff 644 SRV_MAC_PING_SLOT_FREQ_REQ = 0x0a,
jreiss 15:b50f92f1c6ff 645 SRV_MAC_PING_SLOT_CHANNEL_REQ = 0x0a,
jreiss 15:b50f92f1c6ff 646 SRV_MAC_BEACON_TIMING_ANS = 0x0b,
jreiss 15:b50f92f1c6ff 647 SRV_MAC_BEACON_FREQ_REQ = 0x0c,
jreiss 15:b50f92f1c6ff 648
jreiss 15:b50f92f1c6ff 649 /* Multitech */
jreiss 15:b50f92f1c6ff 650 SRV_MAC_PING_ANS = 0x80,
jreiss 15:b50f92f1c6ff 651 SRV_MAC_CHANGE_CLASS = 0x81,
jreiss 15:b50f92f1c6ff 652 SRV_MAC_MULTIPART_START_REQ = 0x82,
jreiss 15:b50f92f1c6ff 653 SRV_MAC_MULTIPART_START_ANS = 0x83,
jreiss 15:b50f92f1c6ff 654 SRV_MAC_MULTIPART_CHUNK = 0x84,
jreiss 15:b50f92f1c6ff 655 SRV_MAC_MULTIPART_END_REQ = 0x85,
jreiss 15:b50f92f1c6ff 656 SRV_MAC_MULTIPART_END_ANS = 0x86
jreiss 15:b50f92f1c6ff 657 } ServerCommand;
jreiss 15:b50f92f1c6ff 658
jreiss 15:b50f92f1c6ff 659 /**
jreiss 15:b50f92f1c6ff 660 * Random seed for software RNG
jreiss 15:b50f92f1c6ff 661 */
jreiss 15:b50f92f1c6ff 662 void srand(uint32_t seed);
jreiss 15:b50f92f1c6ff 663
jreiss 15:b50f92f1c6ff 664 /**
jreiss 15:b50f92f1c6ff 665 * Software RNG for consistent results across differing hardware
jreiss 15:b50f92f1c6ff 666 */
jreiss 15:b50f92f1c6ff 667 int rand(void);
jreiss 15:b50f92f1c6ff 668
jreiss 15:b50f92f1c6ff 669 /**
jreiss 15:b50f92f1c6ff 670 * Generate random number bounded by min and max
jreiss 15:b50f92f1c6ff 671 */
jreiss 15:b50f92f1c6ff 672 int32_t rand_r(int32_t min, int32_t max);
jreiss 15:b50f92f1c6ff 673
jreiss 15:b50f92f1c6ff 674 uint8_t CountBits(uint16_t mask);
jreiss 15:b50f92f1c6ff 675
jreiss 15:b50f92f1c6ff 676 /**
jreiss 15:b50f92f1c6ff 677 * Copy 3-bytes network order from array into LSB of integer value
jreiss 15:b50f92f1c6ff 678 */
jreiss 15:b50f92f1c6ff 679 void CopyNetIDtoInt(const uint8_t* arr, uint32_t& val);
jreiss 15:b50f92f1c6ff 680
jreiss 15:b50f92f1c6ff 681 /**
jreiss 15:b50f92f1c6ff 682 * Copy LSB 3-bytes from integer value into array network order
jreiss 15:b50f92f1c6ff 683 */
jreiss 15:b50f92f1c6ff 684 void CopyNetIDtoArray(uint32_t val, uint8_t* arr);
jreiss 15:b50f92f1c6ff 685
jreiss 15:b50f92f1c6ff 686 /**
jreiss 15:b50f92f1c6ff 687 * Copy 4-bytes network order from array in to integer value
jreiss 15:b50f92f1c6ff 688 */
jreiss 15:b50f92f1c6ff 689 void CopyAddrtoInt(const uint8_t* arr, uint32_t& val);
jreiss 15:b50f92f1c6ff 690
jreiss 15:b50f92f1c6ff 691 /**
jreiss 15:b50f92f1c6ff 692 * Copy 4-bytes from integer in to array network order
jreiss 15:b50f92f1c6ff 693 */
jreiss 15:b50f92f1c6ff 694 void CopyAddrtoArray(uint32_t val, uint8_t* arr);
jreiss 15:b50f92f1c6ff 695
jreiss 15:b50f92f1c6ff 696 /**
jreiss 15:b50f92f1c6ff 697 * Copy 3-bytes network order from array into integer value and multiply by 100
jreiss 15:b50f92f1c6ff 698 */
jreiss 15:b50f92f1c6ff 699 void CopyFreqtoInt(const uint8_t* arr, uint32_t& freq);
jreiss 15:b50f92f1c6ff 700
jreiss 15:b50f92f1c6ff 701 /**
jreiss 15:b50f92f1c6ff 702 * Reverse memory copy
jreiss 15:b50f92f1c6ff 703 */
jreiss 15:b50f92f1c6ff 704 void memcpy_r(uint8_t *dst, const uint8_t *src, size_t n);
jreiss 15:b50f92f1c6ff 705
jreiss 15:b50f92f1c6ff 706 }
jreiss 15:b50f92f1c6ff 707
jreiss 15:b50f92f1c6ff 708 #endif
jreiss 15:b50f92f1c6ff 709