First commit

Fork of libmDot by MultiTech

Committer:
Mike Fiore
Date:
Tue Jul 07 14:32:54 2015 -0500
Revision:
5:0bfe6a650513
Parent:
4:94969e981dcc
Child:
6:390fc83d588d
update from revision 8f17f909cd6be946ce224094891e211901ac4836 - allow a DigitalOut pointer to be passed in and used as activity LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:c62615f15125 1 /************************************************
mfiore 0:c62615f15125 2 * MultiTech MTDOT Library
mfiore 0:c62615f15125 3 * Copyright (c) 2015 MultiTech Systems
mfiore 0:c62615f15125 4 *
mfiore 0:c62615f15125 5 * See LICENSE file for license information
mfiore 0:c62615f15125 6 ***********************************************/
mfiore 0:c62615f15125 7
mfiore 0:c62615f15125 8 #ifndef MDOT_H
mfiore 0:c62615f15125 9 #define MDOT_H
mfiore 0:c62615f15125 10
mfiore 0:c62615f15125 11 #include "mbed.h"
mfiore 0:c62615f15125 12 #include "rtos.h"
mfiore 0:c62615f15125 13 #include <vector>
mfiore 0:c62615f15125 14 #include <map>
mfiore 0:c62615f15125 15 #include <string>
mfiore 0:c62615f15125 16
mfiore 0:c62615f15125 17 class LoRaMacEvent;
mfiore 0:c62615f15125 18 class LoRaConfig;
mfiore 0:c62615f15125 19 class LoRaMac;
mfiore 0:c62615f15125 20 class MdotRadio;
mfiore 0:c62615f15125 21
mfiore 0:c62615f15125 22
mfiore 0:c62615f15125 23 class mDot {
mfiore 0:c62615f15125 24
mfiore 0:c62615f15125 25 private:
mfiore 0:c62615f15125 26
mfiore 0:c62615f15125 27 mDot();
mfiore 0:c62615f15125 28 ~mDot();
mfiore 0:c62615f15125 29
mfiore 0:c62615f15125 30 static void idle(void const* args) {
mfiore 0:c62615f15125 31 while (1)
mfiore 0:c62615f15125 32 __WFI();
mfiore 0:c62615f15125 33 }
mfiore 0:c62615f15125 34
mfiore 0:c62615f15125 35 void setLastError(const std::string& str);
mfiore 0:c62615f15125 36
mfiore 0:c62615f15125 37 static bool validateBaudRate(const uint32_t& baud);
mfiore 0:c62615f15125 38 static bool validateFrequencySubBand(const uint8_t& band);
mfiore 0:c62615f15125 39 bool validateDataRate(const uint8_t& dr);
mfiore 0:c62615f15125 40
mfiore 0:c62615f15125 41 int32_t joinBase(const uint32_t& retries);
mfiore 0:c62615f15125 42 int32_t sendBase(const std::vector<uint8_t>& data, const bool& confirmed = false, const bool& blocking = true, const bool& highBw = false);
mfiore 0:c62615f15125 43 void waitForPacket();
mfiore 0:c62615f15125 44 void waitForLinkCheck();
mfiore 0:c62615f15125 45
mfiore 0:c62615f15125 46 void setActivityLedState(const uint8_t& state);
mfiore 0:c62615f15125 47 uint8_t getActivityLedState();
mfiore 0:c62615f15125 48
mfiore 0:c62615f15125 49 void blinkActivityLed(void) {
Mike Fiore 4:94969e981dcc 50 if (_activity_led) {
Mike Fiore 4:94969e981dcc 51 int val = _activity_led->read();
Mike Fiore 4:94969e981dcc 52 _activity_led->write(!val);
Mike Fiore 4:94969e981dcc 53 }
mfiore 0:c62615f15125 54 }
mfiore 0:c62615f15125 55
mfiore 0:c62615f15125 56 mDot(const mDot&);
mfiore 0:c62615f15125 57 mDot& operator=(const mDot&);
mfiore 0:c62615f15125 58
mfiore 0:c62615f15125 59 static mDot* _instance;
mfiore 0:c62615f15125 60
mfiore 0:c62615f15125 61 LoRaMac* _mac;
mfiore 0:c62615f15125 62 MdotRadio* _radio;
mfiore 0:c62615f15125 63 LoRaMacEvent* _events;
mfiore 0:c62615f15125 64 LoRaConfig* _config;
mfiore 0:c62615f15125 65 Thread _idle_thread;
mfiore 0:c62615f15125 66 std::string _last_error;
mfiore 0:c62615f15125 67 static const uint32_t _baud_rates[];
mfiore 0:c62615f15125 68 uint8_t _activity_led_state;
mfiore 0:c62615f15125 69 Ticker _tick;
Mike Fiore 4:94969e981dcc 70 DigitalOut* _activity_led;
Mike Fiore 4:94969e981dcc 71 bool _activity_led_enable;
Mike Fiore 4:94969e981dcc 72 PinName _activity_led_pin;
Mike Fiore 5:0bfe6a650513 73 bool _activity_led_external;
mfiore 0:c62615f15125 74 uint16_t _linkFailCount;
mfiore 0:c62615f15125 75
mfiore 0:c62615f15125 76 typedef enum {
mfiore 0:c62615f15125 77 OFF, ON, BLINK,
mfiore 0:c62615f15125 78 } state;
mfiore 0:c62615f15125 79
mfiore 0:c62615f15125 80 public:
mfiore 0:c62615f15125 81
mfiore 0:c62615f15125 82 typedef enum {
mfiore 0:c62615f15125 83 MDOT_OK = 0,
mfiore 0:c62615f15125 84 MDOT_INVALID_PARAM = -1,
mfiore 0:c62615f15125 85 MDOT_TX_ERROR = -2,
mfiore 0:c62615f15125 86 MDOT_RX_ERROR = -3,
mfiore 0:c62615f15125 87 MDOT_JOIN_ERROR = -4,
mfiore 0:c62615f15125 88 MDOT_TIMEOUT = -5,
mfiore 0:c62615f15125 89 MDOT_NOT_JOINED = -6,
mfiore 0:c62615f15125 90 MDOT_ENCRYPTION_DISABLED = -7,
mfiore 0:c62615f15125 91 MDOT_NO_FREE_CHAN = -8,
mfiore 0:c62615f15125 92 MDOT_ERROR = -1024,
mfiore 0:c62615f15125 93 } mdot_ret_code;
mfiore 0:c62615f15125 94
mfiore 0:c62615f15125 95 enum JoinMode {
mfiore 0:c62615f15125 96 MANUAL, OTA, AUTO_OTA
mfiore 0:c62615f15125 97 };
mfiore 0:c62615f15125 98
mfiore 0:c62615f15125 99 enum Mode {
mfiore 0:c62615f15125 100 COMMAND_MODE, SERIAL_MODE
mfiore 0:c62615f15125 101 };
mfiore 0:c62615f15125 102
mfiore 0:c62615f15125 103 enum RX_Output {
mfiore 0:c62615f15125 104 HEXADECIMAL, BINARY
mfiore 0:c62615f15125 105 };
mfiore 0:c62615f15125 106
mfiore 0:c62615f15125 107 enum DataRates {
mfiore 0:c62615f15125 108 SF_12, SF_11, SF_10, SF_9, SF_8, SF_7, SF_7H, SF_50
mfiore 0:c62615f15125 109 };
mfiore 0:c62615f15125 110
mfiore 0:c62615f15125 111 enum FrequencyBands {
mfiore 0:c62615f15125 112 FB_868, FB_915
mfiore 0:c62615f15125 113 };
mfiore 0:c62615f15125 114
mfiore 0:c62615f15125 115 enum FrequencySubBands {
mfiore 0:c62615f15125 116 FSB_ALL, FSB_1, FSB_2, FSB_3, FSB_4, FSB_5, FSB_6, FSB_7, FSB_8
mfiore 0:c62615f15125 117 };
mfiore 0:c62615f15125 118
mfiore 0:c62615f15125 119 typedef struct {
mfiore 0:c62615f15125 120 uint32_t Up;
mfiore 0:c62615f15125 121 uint32_t Down;
mfiore 0:c62615f15125 122 uint32_t Joins;
mfiore 0:c62615f15125 123 uint32_t JoinFails;
mfiore 0:c62615f15125 124 uint32_t MissedAcks;
mfiore 0:c62615f15125 125 } mdot_stats;
mfiore 0:c62615f15125 126
mfiore 0:c62615f15125 127 typedef struct {
mfiore 0:c62615f15125 128 int16_t last;
mfiore 0:c62615f15125 129 int16_t min;
mfiore 0:c62615f15125 130 int16_t max;
mfiore 0:c62615f15125 131 int16_t avg;
mfiore 0:c62615f15125 132 } rssi_stats;
mfiore 0:c62615f15125 133
mfiore 0:c62615f15125 134 typedef struct {
mfiore 0:c62615f15125 135 int8_t last;
mfiore 0:c62615f15125 136 int8_t min;
mfiore 0:c62615f15125 137 int8_t max;
mfiore 0:c62615f15125 138 int8_t avg;
mfiore 0:c62615f15125 139 } snr_stats;
mfiore 0:c62615f15125 140
mfiore 0:c62615f15125 141 typedef struct {
mfiore 0:c62615f15125 142 bool status;
mfiore 0:c62615f15125 143 int32_t dBm;
mfiore 0:c62615f15125 144 uint32_t gateways;
mfiore 0:c62615f15125 145 std::vector<uint8_t> payload;
mfiore 0:c62615f15125 146 } link_check;
mfiore 0:c62615f15125 147
mfiore 0:c62615f15125 148 typedef struct {
mfiore 0:c62615f15125 149 int32_t status;
mfiore 0:c62615f15125 150 int16_t rssi;
mfiore 0:c62615f15125 151 int16_t snr;
mfiore 0:c62615f15125 152 } ping_response;
mfiore 0:c62615f15125 153
mfiore 0:c62615f15125 154 static const uint8_t MaxLengths_915[];
mfiore 0:c62615f15125 155 static const uint8_t MaxLengths_868[];
mfiore 0:c62615f15125 156
mfiore 0:c62615f15125 157 static std::string JoinModeStr(uint8_t mode);
mfiore 0:c62615f15125 158 static std::string ModeStr(uint8_t mode);
mfiore 0:c62615f15125 159 static std::string RxOutputStr(uint8_t format);
mfiore 0:c62615f15125 160 static std::string DataRateStr(uint8_t rate);
mfiore 0:c62615f15125 161 static std::string FrequencyBandStr(uint8_t band);
mfiore 0:c62615f15125 162 static std::string FrequencySubBandStr(uint8_t band);
mfiore 0:c62615f15125 163
mfiore 0:c62615f15125 164 /** Get a handle to the singleton object
mfiore 0:c62615f15125 165 * @returns pointer to mDot object
mfiore 0:c62615f15125 166 */
mfiore 0:c62615f15125 167 static mDot* getInstance();
mfiore 0:c62615f15125 168
mfiore 0:c62615f15125 169 /** Get library version information
mfiore 0:c62615f15125 170 * @returns string containing library version information
mfiore 0:c62615f15125 171 */
mfiore 0:c62615f15125 172 std::string getId();
mfiore 0:c62615f15125 173
mfiore 0:c62615f15125 174 /** Perform a soft reset of the system
mfiore 0:c62615f15125 175 */
mfiore 0:c62615f15125 176 void resetCpu();
mfiore 0:c62615f15125 177
mfiore 0:c62615f15125 178 /** Reset config to factory default
mfiore 0:c62615f15125 179 */
mfiore 0:c62615f15125 180 void resetConfig();
mfiore 0:c62615f15125 181
mfiore 0:c62615f15125 182 /** Save config data to non volatile memory
mfiore 0:c62615f15125 183 * @returns true if success, false if failure
mfiore 0:c62615f15125 184 */
mfiore 0:c62615f15125 185 bool saveConfig();
mfiore 0:c62615f15125 186
mfiore 0:c62615f15125 187 /** Set the log level for the library
mfiore 0:c62615f15125 188 * options are:
mfiore 0:c62615f15125 189 * NONE_LEVEL - logging is off at this level
mfiore 0:c62615f15125 190 * FATAL_LEVEL - only critical errors will be reported
mfiore 0:c62615f15125 191 * ERROR_LEVEL
mfiore 0:c62615f15125 192 * WARNING_LEVEL
mfiore 0:c62615f15125 193 * INFO_LEVEL
mfiore 0:c62615f15125 194 * DEBUG_LEVEL
mfiore 0:c62615f15125 195 * TRACE_LEVEL - every detail will be reported
mfiore 0:c62615f15125 196 * @param level the level to log at
mfiore 0:c62615f15125 197 * @returns MDOT_OK if success
mfiore 0:c62615f15125 198 */
mfiore 0:c62615f15125 199 int32_t setLogLevel(const uint8_t& level);
mfiore 0:c62615f15125 200
mfiore 0:c62615f15125 201 /** Get the current log level for the library
mfiore 0:c62615f15125 202 * @returns current log level
mfiore 0:c62615f15125 203 */
Mike Fiore 4:94969e981dcc 204 uint8_t getLogLevel();
Mike Fiore 4:94969e981dcc 205
Mike Fiore 4:94969e981dcc 206 /** Enable or disable the activity LED.
Mike Fiore 4:94969e981dcc 207 * @param enable true to enable the LED, false to disable
Mike Fiore 4:94969e981dcc 208 */
Mike Fiore 4:94969e981dcc 209 void setActivityLedEnable(const bool& enable);
Mike Fiore 4:94969e981dcc 210
Mike Fiore 4:94969e981dcc 211 /** Find out if the activity LED is enabled
Mike Fiore 4:94969e981dcc 212 * @returns true if activity LED is enabled, false if disabled
Mike Fiore 4:94969e981dcc 213 */
Mike Fiore 4:94969e981dcc 214 bool getActivityLedEnable();
Mike Fiore 4:94969e981dcc 215
Mike Fiore 4:94969e981dcc 216 /** Use a different pin for the activity LED.
Mike Fiore 4:94969e981dcc 217 * The default is XBEE_RSSI.
Mike Fiore 4:94969e981dcc 218 * @param pin the new pin to use
Mike Fiore 4:94969e981dcc 219 */
Mike Fiore 4:94969e981dcc 220 void setActivityLedPin(const PinName& pin);
Mike Fiore 4:94969e981dcc 221
Mike Fiore 5:0bfe6a650513 222 /** Use an external DigitalOut object for the activity LED.
Mike Fiore 5:0bfe6a650513 223 * The pointer must stay valid!
Mike Fiore 5:0bfe6a650513 224 * @param pin the DigitalOut object to use
Mike Fiore 5:0bfe6a650513 225 */
Mike Fiore 5:0bfe6a650513 226 void setActivityLedPin(DigitalOut* pin);
Mike Fiore 5:0bfe6a650513 227
Mike Fiore 4:94969e981dcc 228 /** Find out what pin the activity LED is on
Mike Fiore 4:94969e981dcc 229 * @returns the pin the activity LED is using
Mike Fiore 4:94969e981dcc 230 */
Mike Fiore 4:94969e981dcc 231 PinName getActivityLedPin();
mfiore 0:c62615f15125 232
mfiore 0:c62615f15125 233 /** Get list of channel frequencies currently in use
mfiore 0:c62615f15125 234 * @returns vector of channels currently in use
mfiore 0:c62615f15125 235 */
mfiore 0:c62615f15125 236 std::vector<uint32_t> getChannels();
mfiore 0:c62615f15125 237
mfiore 0:c62615f15125 238 /** Get frequency band
mfiore 0:c62615f15125 239 * @returns FB_915 if configured for United States, FB_868 if configured for Europe
mfiore 0:c62615f15125 240 */
mfiore 0:c62615f15125 241 uint8_t getFrequencyBand();
mfiore 0:c62615f15125 242
mfiore 0:c62615f15125 243 /** Set frequency sub band
mfiore 0:c62615f15125 244 * only applicable if frequency band is set for United States (FB_915)
mfiore 0:c62615f15125 245 * sub band 0 will allow the radio to use all 64 channels
mfiore 0:c62615f15125 246 * sub band 1 - 8 will allow the radio to use the 8 channels in that sub band
mfiore 0:c62615f15125 247 * for use with Conduit gateway and MTAC_LORA, use sub bands 1 - 8, not sub band 0
mfiore 0:c62615f15125 248 * @param band the sub band to use (0 - 8)
mfiore 0:c62615f15125 249 * @returns MDOT_OK if success
mfiore 0:c62615f15125 250 */
mfiore 0:c62615f15125 251 int32_t setFrequencySubBand(const uint8_t& band);
mfiore 0:c62615f15125 252
mfiore 0:c62615f15125 253 /** Get frequency sub band
mfiore 0:c62615f15125 254 * @returns frequency sub band currently in use
mfiore 0:c62615f15125 255 */
mfiore 0:c62615f15125 256 uint8_t getFrequencySubBand();
mfiore 0:c62615f15125 257
mfiore 0:c62615f15125 258 /** Enable/disable public network mode
mfiore 0:c62615f15125 259 * for use with Conduit gateway and MTAC_LORA, disable public network mode
mfiore 0:c62615f15125 260 * @param on should be true to enable public network mode
mfiore 0:c62615f15125 261 * @returns MDOT_OK if success
mfiore 0:c62615f15125 262 */
mfiore 0:c62615f15125 263 int32_t setPublicNetwork(const bool& on);
mfiore 0:c62615f15125 264
mfiore 0:c62615f15125 265 /** Get public network mode
mfiore 0:c62615f15125 266 * @returns true if public network mode is enabled
mfiore 0:c62615f15125 267 */
mfiore 0:c62615f15125 268 bool getPublicNetwork();
mfiore 0:c62615f15125 269
mfiore 0:c62615f15125 270 /** Get the device ID
mfiore 0:c62615f15125 271 * @returns vector containing the device ID (size 8)
mfiore 0:c62615f15125 272 */
mfiore 0:c62615f15125 273 std::vector<uint8_t> getDeviceId();
mfiore 0:c62615f15125 274
mfiore 0:c62615f15125 275 /** Set network address
mfiore 0:c62615f15125 276 * for use with MANUAL network join mode, will be assigned in OTA & AUTO_OTA modes
mfiore 0:c62615f15125 277 * @param addr a vector of 4 bytes
mfiore 0:c62615f15125 278 * @returns MDOT_OK if success
mfiore 0:c62615f15125 279 */
mfiore 0:c62615f15125 280 int32_t setNetworkAddress(const std::vector<uint8_t>& addr);
mfiore 0:c62615f15125 281
mfiore 0:c62615f15125 282 /** Get network address
mfiore 0:c62615f15125 283 * @returns vector containing network address (size 4)
mfiore 0:c62615f15125 284 */
mfiore 0:c62615f15125 285 std::vector<uint8_t> getNetworkAddress();
mfiore 0:c62615f15125 286
mfiore 0:c62615f15125 287 /** Set network session key
mfiore 0:c62615f15125 288 * for use with MANUAL network join mode, will be assigned in OTA & AUTO_OTA modes
mfiore 0:c62615f15125 289 * @param key a vector of 16 bytes
mfiore 0:c62615f15125 290 * @returns MDOT_OK if success
mfiore 0:c62615f15125 291 */
mfiore 0:c62615f15125 292 int32_t setNetworkSessionKey(const std::vector<uint8_t>& key);
mfiore 0:c62615f15125 293
mfiore 0:c62615f15125 294 /** Get network session key
mfiore 0:c62615f15125 295 * @returns vector containing network session key (size 16)
mfiore 0:c62615f15125 296 */
mfiore 0:c62615f15125 297 std::vector<uint8_t> getNetworkSessionKey();
mfiore 0:c62615f15125 298
mfiore 0:c62615f15125 299 /** Set data session key
mfiore 0:c62615f15125 300 * for use with MANUAL network join mode, will be assigned in OTA & AUTO_OTA modes
mfiore 0:c62615f15125 301 * @param key a vector of 16 bytes
mfiore 0:c62615f15125 302 * @returns MDOT_OK if success
mfiore 0:c62615f15125 303 */
mfiore 0:c62615f15125 304 int32_t setDataSessionKey(const std::vector<uint8_t>& key);
mfiore 0:c62615f15125 305
mfiore 0:c62615f15125 306 /** Get data session key
mfiore 0:c62615f15125 307 * @returns vector containing data session key (size 16)
mfiore 0:c62615f15125 308 */
mfiore 0:c62615f15125 309 std::vector<uint8_t> getDataSessionKey();
mfiore 0:c62615f15125 310
mfiore 0:c62615f15125 311 /** Set network name
mfiore 0:c62615f15125 312 * for use with OTA & AUTO_OTA network join modes
mfiore 0:c62615f15125 313 * generates network ID (crc64 of name) automatically
mfiore 0:c62615f15125 314 * @param name a string of of at least 8 bytes and no more than 128 bytes
mfiore 0:c62615f15125 315 * @return MDOT_OK if success
mfiore 0:c62615f15125 316 */
mfiore 0:c62615f15125 317 int32_t setNetworkName(const std::string& name);
mfiore 0:c62615f15125 318
mfiore 0:c62615f15125 319 /** Get network name
mfiore 0:c62615f15125 320 * @return string containing network name (size 8 to 128)
mfiore 0:c62615f15125 321 */
mfiore 0:c62615f15125 322 std::string getNetworkName();
mfiore 0:c62615f15125 323
mfiore 0:c62615f15125 324 /** Set network ID
mfiore 0:c62615f15125 325 * for use with OTA & AUTO_OTA network join modes
mfiore 0:c62615f15125 326 * setting network ID via this function sets network name to empty
mfiore 0:c62615f15125 327 * @param id a vector of 8 bytes
mfiore 0:c62615f15125 328 * @returns MDOT_OK if success
mfiore 0:c62615f15125 329 */
mfiore 0:c62615f15125 330 int32_t setNetworkId(const std::vector<uint8_t>& id);
mfiore 0:c62615f15125 331
mfiore 0:c62615f15125 332 /** Get network ID
mfiore 0:c62615f15125 333 * @returns vector containing network ID (size 8)
mfiore 0:c62615f15125 334 */
mfiore 0:c62615f15125 335 std::vector<uint8_t> getNetworkId();
mfiore 0:c62615f15125 336
mfiore 0:c62615f15125 337 /** Set network passphrase
mfiore 0:c62615f15125 338 * for use with OTA & AUTO_OTA network join modes
mfiore 0:c62615f15125 339 * generates network key (cmac of passphrase) automatically
mfiore 0:c62615f15125 340 * @param name a string of of at least 8 bytes and no more than 128 bytes
mfiore 0:c62615f15125 341 * @return MDOT_OK if success
mfiore 0:c62615f15125 342 */
mfiore 0:c62615f15125 343 int32_t setNetworkPassphrase(const std::string& passphrase);
mfiore 0:c62615f15125 344
mfiore 0:c62615f15125 345 /** Get network passphrase
mfiore 0:c62615f15125 346 * @return string containing network passphrase (size 8 to 128)
mfiore 0:c62615f15125 347 */
mfiore 0:c62615f15125 348 std::string getNetworkPassphrase();
mfiore 0:c62615f15125 349
mfiore 0:c62615f15125 350 /** Set network key
mfiore 0:c62615f15125 351 * for use with OTA & AUTO_OTA network join modes
mfiore 0:c62615f15125 352 * setting network key via this function sets network passphrase to empty
mfiore 0:c62615f15125 353 * @param id a vector of 16 bytes
mfiore 0:c62615f15125 354 * @returns MDOT_OK if success
mfiore 0:c62615f15125 355 */
mfiore 0:c62615f15125 356 int32_t setNetworkKey(const std::vector<uint8_t>& id);
mfiore 0:c62615f15125 357
mfiore 0:c62615f15125 358 /** Get network key
mfiore 0:c62615f15125 359 * @returns a vector containing network key (size 16)
mfiore 0:c62615f15125 360 */
mfiore 0:c62615f15125 361 std::vector<uint8_t> getNetworkKey();
mfiore 0:c62615f15125 362
mfiore 0:c62615f15125 363 /** Attempt to join network
mfiore 0:c62615f15125 364 * retries according to configuration set by setJoinRetries()
mfiore 0:c62615f15125 365 * @returns MDOT_OK if success
mfiore 0:c62615f15125 366 */
mfiore 0:c62615f15125 367 int32_t joinNetwork();
mfiore 0:c62615f15125 368
mfiore 0:c62615f15125 369 /** Attempts to join network once
mfiore 0:c62615f15125 370 * @returns MDOT_OK if success
mfiore 0:c62615f15125 371 */
mfiore 0:c62615f15125 372 int32_t joinNetworkOnce();
mfiore 0:c62615f15125 373
mfiore 0:c62615f15125 374 /** Resets current network session, essentially disconnecting from the network
mfiore 0:c62615f15125 375 * has no effect for MANUAL network join mode
mfiore 0:c62615f15125 376 */
mfiore 0:c62615f15125 377 void resetNetworkSession();
mfiore 0:c62615f15125 378
mfiore 0:c62615f15125 379 /** Set number of times joining will retry before giving up
mfiore 0:c62615f15125 380 * @param retries must be between 0 - 255
mfiore 0:c62615f15125 381 * @returns MDOT_OK if success
mfiore 0:c62615f15125 382 */
mfiore 0:c62615f15125 383 int32_t setJoinRetries(const uint8_t& retries);
mfiore 0:c62615f15125 384
mfiore 0:c62615f15125 385 /** Set number of times joining will retry before giving up
mfiore 0:c62615f15125 386 * @returns join retries (0 - 255)
mfiore 0:c62615f15125 387 */
mfiore 0:c62615f15125 388 uint8_t getJoinRetries();
mfiore 0:c62615f15125 389
mfiore 0:c62615f15125 390 /** Set network join mode
mfiore 0:c62615f15125 391 * MANUAL: set network address and session keys manually
mfiore 0:c62615f15125 392 * OTA: User sets network name and passphrase, then attempts to join
mfiore 0:c62615f15125 393 * AUTO_OTA: same as OTA, but network sessions can be saved and restored
mfiore 0:c62615f15125 394 * @param mode MANUAL, OTA, or AUTO_OTA
mfiore 0:c62615f15125 395 * @returns MDOT_OK if success
mfiore 0:c62615f15125 396 */
mfiore 0:c62615f15125 397 int32_t setJoinMode(const uint8_t& mode);
mfiore 0:c62615f15125 398
mfiore 0:c62615f15125 399 /** Get network join mode
mfiore 0:c62615f15125 400 * @returns MANUAL, OTA, or AUTO_OTA
mfiore 0:c62615f15125 401 */
mfiore 0:c62615f15125 402 uint8_t getJoinMode();
mfiore 0:c62615f15125 403
mfiore 0:c62615f15125 404 /** Get network join status
mfiore 0:c62615f15125 405 * @returns true if currently joined to network
mfiore 0:c62615f15125 406 */
mfiore 0:c62615f15125 407 bool getNetworkJoinStatus();
mfiore 0:c62615f15125 408
mfiore 0:c62615f15125 409 /** Do a network link check
mfiore 0:c62615f15125 410 * application data may be returned in response to a network link check command
mfiore 0:c62615f15125 411 * @returns link_check structure containing success, dBm above noise floor, gateways in range, and packet payload
mfiore 0:c62615f15125 412 */
mfiore 0:c62615f15125 413 link_check networkLinkCheck();
mfiore 0:c62615f15125 414
mfiore 0:c62615f15125 415 /** Set network link check count to perform automatic link checks every count packets
mfiore 0:c62615f15125 416 * only applicable if ACKs are disabled
mfiore 0:c62615f15125 417 * @param count must be between 0 - 255
mfiore 0:c62615f15125 418 * @returns MDOT_OK if success
mfiore 0:c62615f15125 419 */
mfiore 0:c62615f15125 420 int32_t setLinkCheckCount(const uint8_t& count);
mfiore 0:c62615f15125 421
mfiore 0:c62615f15125 422 /** Get network link check count
mfiore 0:c62615f15125 423 * @returns count (0 - 255)
mfiore 0:c62615f15125 424 */
mfiore 0:c62615f15125 425 uint8_t getLinkCheckCount();
mfiore 0:c62615f15125 426
mfiore 0:c62615f15125 427 /** Set network link check threshold, number of link check failures or missed acks to tolerate
mfiore 0:c62615f15125 428 * before considering network connection lost
mfiore 0:c62615f15125 429 * @pararm count must be between 0 - 255
mfiore 0:c62615f15125 430 * @returns MDOT_OK if success
mfiore 0:c62615f15125 431 */
mfiore 0:c62615f15125 432 int32_t setLinkCheckThreshold(const uint8_t& count);
mfiore 0:c62615f15125 433
mfiore 0:c62615f15125 434 /** Get network link check threshold
mfiore 0:c62615f15125 435 * @returns threshold (0 - 255)
mfiore 0:c62615f15125 436 */
mfiore 0:c62615f15125 437 uint8_t getLinkCheckThreshold();
mfiore 0:c62615f15125 438
mfiore 0:c62615f15125 439 /** Get UpLinkCounter
mfiore 0:c62615f15125 440 * @returns number of packets sent to the gateway during this network session (sequence number)
mfiore 0:c62615f15125 441 */
mfiore 0:c62615f15125 442 uint32_t getUpLinkCounter();
mfiore 0:c62615f15125 443
mfiore 0:c62615f15125 444 /** Enable/disable AES encryption
mfiore 0:c62615f15125 445 * AES encryption must be enabled for use with Conduit gateway and MTAC_LORA card
mfiore 0:c62615f15125 446 * @param on true for AES encryption to be enabled
mfiore 0:c62615f15125 447 * @returns MDOT_OK if success
mfiore 0:c62615f15125 448 */
mfiore 0:c62615f15125 449 int32_t setAesEncryption(const bool& on);
mfiore 0:c62615f15125 450
mfiore 0:c62615f15125 451 /** Get AES encryption
mfiore 0:c62615f15125 452 * @returns true if AES encryption is enabled
mfiore 0:c62615f15125 453 */
mfiore 0:c62615f15125 454 bool getAesEncryption();
mfiore 0:c62615f15125 455
mfiore 0:c62615f15125 456 /** Get RSSI stats
mfiore 0:c62615f15125 457 * @returns rssi_stats struct containing last, min, max, and avg RSSI
mfiore 0:c62615f15125 458 */
mfiore 0:c62615f15125 459 rssi_stats getRssiStats();
mfiore 0:c62615f15125 460
mfiore 0:c62615f15125 461 /** Get SNR stats
mfiore 0:c62615f15125 462 * @returns snr_stats struct containing last, min, max, and avg SNR
mfiore 0:c62615f15125 463 */
mfiore 0:c62615f15125 464 snr_stats getSnrStats();
mfiore 0:c62615f15125 465
mfiore 0:c62615f15125 466 /** Get ms until next free channel
mfiore 0:c62615f15125 467 * only applicable for European models, US models return 0
mfiore 0:c62615f15125 468 * @returns time (ms) until a channel is free to use for transmitting
mfiore 0:c62615f15125 469 */
mfiore 0:c62615f15125 470 uint32_t getNextTxMs();
mfiore 0:c62615f15125 471
mfiore 0:c62615f15125 472 /** Get data pending
mfiore 0:c62615f15125 473 * only valid after sending data to the gateway
mfiore 0:c62615f15125 474 * @returns true if server has available packet(s)
mfiore 0:c62615f15125 475 */
mfiore 0:c62615f15125 476 bool getDataPending();
mfiore 0:c62615f15125 477
mfiore 0:c62615f15125 478 /** Get transmitting
mfiore 0:c62615f15125 479 * @returns true if currently transmitting
mfiore 0:c62615f15125 480 */
mfiore 0:c62615f15125 481 bool getIsTransmitting();
mfiore 0:c62615f15125 482
mfiore 0:c62615f15125 483 /** Set TX data rate
mfiore 0:c62615f15125 484 * data rates affect maximum payload size
mfiore 0:c62615f15125 485 * @param dr SF_7 - SF_12 for Europe, SF_7 - SF_10 for United States
mfiore 0:c62615f15125 486 * @returns MDOT_OK if success
mfiore 0:c62615f15125 487 */
mfiore 0:c62615f15125 488 int32_t setTxDataRate(const uint8_t& dr);
mfiore 0:c62615f15125 489
mfiore 0:c62615f15125 490 /** Get TX data rate
mfiore 0:c62615f15125 491 * @returns current TX data rate (SF_7 - SF_12)
mfiore 0:c62615f15125 492 */
mfiore 0:c62615f15125 493 uint8_t getTxDataRate();
mfiore 0:c62615f15125 494
mfiore 0:c62615f15125 495 /** Set TX power
mfiore 0:c62615f15125 496 * power affects maximum range
mfiore 0:c62615f15125 497 * @param power 2 dBm - 20 dBm
mfiore 0:c62615f15125 498 * @returns MDOT_OK if success
mfiore 0:c62615f15125 499 */
mfiore 0:c62615f15125 500 int32_t setTxPower(const uint32_t& power);
mfiore 0:c62615f15125 501
mfiore 0:c62615f15125 502 /** Get TX power
mfiore 0:c62615f15125 503 * @returns TX power (2 dBm - 20 dBm)
mfiore 0:c62615f15125 504 */
mfiore 0:c62615f15125 505 uint32_t getTxPower();
mfiore 0:c62615f15125 506
mfiore 0:c62615f15125 507 /** Enable/disable TX waiting for rx windows
mfiore 0:c62615f15125 508 * when enabled, send calls will block until a packet is received or RX timeout
mfiore 0:c62615f15125 509 * @param enable set to true if expecting responses to transmitted packets
mfiore 0:c62615f15125 510 * @returns MDOT_OK if success
mfiore 0:c62615f15125 511 */
mfiore 0:c62615f15125 512 int32_t setTxWait(const bool& enable);
mfiore 0:c62615f15125 513
mfiore 0:c62615f15125 514 /** Get TX wait
mfiore 0:c62615f15125 515 * @returns true if TX wait is enabled
mfiore 0:c62615f15125 516 */
mfiore 0:c62615f15125 517 bool getTxWait();
mfiore 0:c62615f15125 518
mfiore 0:c62615f15125 519 /** Get time on air
mfiore 0:c62615f15125 520 * @returns the amount of time (in ms) it would take to send bytes bytes based on current configuration
mfiore 0:c62615f15125 521 */
mfiore 0:c62615f15125 522 uint32_t getTimeOnAir(uint8_t bytes);
mfiore 0:c62615f15125 523
mfiore 0:c62615f15125 524 /** Get min frequency
mfiore 0:c62615f15125 525 * @returns minimum frequency based on current configuration
mfiore 0:c62615f15125 526 */
mfiore 0:c62615f15125 527 uint32_t getMinFrequency();
mfiore 0:c62615f15125 528
mfiore 0:c62615f15125 529 /** Get max frequency
mfiore 0:c62615f15125 530 * @returns maximum frequency based on current configuration
mfiore 0:c62615f15125 531 */
mfiore 0:c62615f15125 532 uint32_t getMaxFrequency();
mfiore 0:c62615f15125 533
mfiore 0:c62615f15125 534 /** Set forward error correction bytes
mfiore 0:c62615f15125 535 * @param bytes 1 - 4 bytes
mfiore 0:c62615f15125 536 * @returns MDOT_OK if success
mfiore 0:c62615f15125 537 */
mfiore 0:c62615f15125 538 int32_t setFec(const uint8_t& bytes);
mfiore 0:c62615f15125 539
mfiore 0:c62615f15125 540 /** Get forward error correction bytes
mfiore 0:c62615f15125 541 * @returns bytes (1 - 4)
mfiore 0:c62615f15125 542 */
mfiore 0:c62615f15125 543 uint8_t getFec();
mfiore 0:c62615f15125 544
mfiore 0:c62615f15125 545 /** Enable/disable CRC checking of packets
mfiore 0:c62615f15125 546 * CRC checking must be enabled for use with Conduit gateway and MTAC_LORA card
mfiore 0:c62615f15125 547 * @param on set to true to enable CRC checking
mfiore 0:c62615f15125 548 * @returns MDOT_OK if success
mfiore 0:c62615f15125 549 */
mfiore 0:c62615f15125 550 int32_t setCrc(const bool& on);
mfiore 0:c62615f15125 551
mfiore 0:c62615f15125 552 /** Get CRC checking
mfiore 0:c62615f15125 553 * @returns true if CRC checking is enabled
mfiore 0:c62615f15125 554 */
mfiore 0:c62615f15125 555 bool getCrc();
mfiore 0:c62615f15125 556
mfiore 0:c62615f15125 557 /** Set ack
mfiore 0:c62615f15125 558 * @param retries 0 to disable acks, otherwise 1 - 8
mfiore 0:c62615f15125 559 * @returns MDOT_OK if success
mfiore 0:c62615f15125 560 */
mfiore 0:c62615f15125 561 int32_t setAck(const uint8_t& retries);
mfiore 0:c62615f15125 562
mfiore 0:c62615f15125 563 /** Get ack
mfiore 0:c62615f15125 564 * @returns 0 if acks are disabled, otherwise retries (1 - 8)
mfiore 0:c62615f15125 565 */
mfiore 0:c62615f15125 566 uint8_t getAck();
mfiore 0:c62615f15125 567
mfiore 0:c62615f15125 568 /** Send data to the gateway
mfiore 0:c62615f15125 569 * validates data size (based on spreading factor)
mfiore 0:c62615f15125 570 * @param data a vector of up to 242 bytes (may be less based on spreading factor)
mfiore 0:c62615f15125 571 * @returns MDOT_OK if packet was sent successfully (ACKs disabled), or if an ACK was received (ACKs enabled)
mfiore 0:c62615f15125 572 */
mfiore 0:c62615f15125 573 int32_t send(const std::vector<uint8_t>& data, const bool& blocking = true, const bool& highBw = false);
mfiore 0:c62615f15125 574
mfiore 0:c62615f15125 575 /** Fetch data received from the gateway
mfiore 0:c62615f15125 576 * this function only checks to see if a packet has been received - it does not open a receive window
mfiore 0:c62615f15125 577 * send() must be called before recv()
mfiore 0:c62615f15125 578 * @param data a vector to put the received data into
mfiore 0:c62615f15125 579 * @returns MDOT_OK if packet was successfully received
mfiore 0:c62615f15125 580 */
mfiore 0:c62615f15125 581 int32_t recv(std::vector<uint8_t>& data);
mfiore 0:c62615f15125 582
mfiore 0:c62615f15125 583 /** Ping
mfiore 0:c62615f15125 584 * status will be MDOT_OK if ping succeeded
mfiore 0:c62615f15125 585 * @returns ping_response struct containing status, RSSI, and SNR
mfiore 0:c62615f15125 586 */
mfiore 0:c62615f15125 587 ping_response ping();
mfiore 0:c62615f15125 588
mfiore 0:c62615f15125 589 /** Get return code string
mfiore 0:c62615f15125 590 * @returns string containing a description of the given error code
mfiore 0:c62615f15125 591 */
mfiore 0:c62615f15125 592 static std::string getReturnCodeString(const int32_t& code);
mfiore 0:c62615f15125 593
mfiore 0:c62615f15125 594 /** Get last error
mfiore 0:c62615f15125 595 * @returns string explaining the last error that occured
mfiore 0:c62615f15125 596 */
mfiore 0:c62615f15125 597 std::string getLastError();
mfiore 0:c62615f15125 598
mfiore 0:c62615f15125 599 /******************************************
mfiore 0:c62615f15125 600 * THESE FEATURES ARE NOT FULLY IMPLEMENTED
mfiore 0:c62615f15125 601 *****************************************/
mfiore 0:c62615f15125 602 void sleep();
mfiore 0:c62615f15125 603 void wakeup();
mfiore 0:c62615f15125 604
mfiore 0:c62615f15125 605 // get/set adaptive data rate
mfiore 0:c62615f15125 606 // configure data rates and power levels based on signal to noise information from gateway
mfiore 0:c62615f15125 607 // true == adaptive data rate is on
mfiore 0:c62615f15125 608 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 609 int32_t setAdr(const bool& on);
mfiore 0:c62615f15125 610 bool getAdr();
mfiore 0:c62615f15125 611
mfiore 0:c62615f15125 612 /*************************************************************************
mfiore 0:c62615f15125 613 * The following functions are only used by the AT command application and
mfiore 0:c62615f15125 614 * should not be used by standard applications consuming the mDot library
mfiore 0:c62615f15125 615 ************************************************************************/
mfiore 0:c62615f15125 616
mfiore 0:c62615f15125 617 // set/get configured baud rate for command port
mfiore 0:c62615f15125 618 // only for use in conjunction with AT interface
mfiore 0:c62615f15125 619 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 620 int32_t setBaud(const uint32_t& baud);
mfiore 0:c62615f15125 621 uint32_t getBaud();
mfiore 0:c62615f15125 622
mfiore 0:c62615f15125 623 // set/get baud rate for debug port
mfiore 0:c62615f15125 624 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 625 int32_t setDebugBaud(const uint32_t& baud);
mfiore 0:c62615f15125 626 uint32_t getDebugBaud();
mfiore 0:c62615f15125 627
mfiore 0:c62615f15125 628 // set/get command terminal echo
mfiore 0:c62615f15125 629 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 630 int32_t setEcho(const bool& on);
mfiore 0:c62615f15125 631 bool getEcho();
mfiore 0:c62615f15125 632
mfiore 0:c62615f15125 633 // set/get command terminal verbose mode
mfiore 0:c62615f15125 634 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 635 int32_t setVerbose(const bool& on);
mfiore 0:c62615f15125 636 bool getVerbose();
mfiore 0:c62615f15125 637
mfiore 0:c62615f15125 638 // set/get startup mode
mfiore 0:c62615f15125 639 // COMMAND_MODE (default), starts up ready to accept AT commands
mfiore 0:c62615f15125 640 // SERIAL_MODE, read serial data and send it as LoRa packets
mfiore 0:c62615f15125 641 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 642 int32_t setStartUpMode(const uint8_t& mode);
mfiore 0:c62615f15125 643 uint8_t getStartUpMode();
mfiore 0:c62615f15125 644
mfiore 0:c62615f15125 645 int32_t setRxDataRate(const uint8_t& dr);
mfiore 0:c62615f15125 646 uint8_t getRxDataRate();
mfiore 0:c62615f15125 647
mfiore 0:c62615f15125 648 // get/set TX/RX frequency
mfiore 0:c62615f15125 649 // if frequency band == FB_868 (Europe), must be between 863000000 - 870000000
mfiore 0:c62615f15125 650 // if frequency band == FB_915 (United States), must be between 902000000-928000000
mfiore 0:c62615f15125 651 // if set to 0, device will hop frequencies
mfiore 0:c62615f15125 652 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 653 int32_t setTxFrequency(const uint32_t& freq);
mfiore 0:c62615f15125 654 uint32_t getTxFrequency();
mfiore 0:c62615f15125 655 int32_t setRxFrequency(const uint32_t& freq);
mfiore 0:c62615f15125 656 uint32_t getRxFrequency();
mfiore 0:c62615f15125 657
mfiore 0:c62615f15125 658 // get/set TX/RX inverted
mfiore 0:c62615f15125 659 // true == signal is inverted
mfiore 0:c62615f15125 660 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 661 int32_t setTxInverted(const bool& on);
mfiore 0:c62615f15125 662 bool getTxInverted();
mfiore 0:c62615f15125 663 int32_t setRxInverted(const bool& on);
mfiore 0:c62615f15125 664 bool getRxInverted();
mfiore 0:c62615f15125 665
mfiore 0:c62615f15125 666 // get/set RX output mode
mfiore 0:c62615f15125 667 // valid options are HEXADECIMAL and BINARY
mfiore 0:c62615f15125 668 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 669 int32_t setRxOutput(const uint8_t& mode);
mfiore 0:c62615f15125 670 uint8_t getRxOutput();
mfiore 0:c62615f15125 671
mfiore 0:c62615f15125 672 // get/set serial wake interval
mfiore 0:c62615f15125 673 // valid values are 2 s - INT_MAX (2147483647) s
mfiore 0:c62615f15125 674 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 675 int32_t setSerialWakeInterval(const uint32_t& interval);
mfiore 0:c62615f15125 676 uint32_t getSerialWakeInterval();
mfiore 0:c62615f15125 677
mfiore 0:c62615f15125 678 // get/set serial wake delay
mfiore 0:c62615f15125 679 // valid values are 2 ms - INT_MAX (2147483647) ms
mfiore 0:c62615f15125 680 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 681 int32_t setSerialWakeDelay(const uint32_t& delay);
mfiore 0:c62615f15125 682 uint32_t getSerialWakeDelay();
mfiore 0:c62615f15125 683
mfiore 0:c62615f15125 684 // get/set serial receive timeout
mfiore 0:c62615f15125 685 // valid values are 0 ms - 65000 ms
mfiore 0:c62615f15125 686 // set function returns MDOT_OK if success
mfiore 0:c62615f15125 687 int32_t setSerialReceiveTimeout(const uint16_t& timeout);
mfiore 0:c62615f15125 688 uint16_t getSerialReceiveTimeout();
mfiore 0:c62615f15125 689
mfiore 0:c62615f15125 690 uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
mfiore 0:c62615f15125 691
mfiore 0:c62615f15125 692 // MTS_RADIO_DEBUG_COMMANDS
mfiore 0:c62615f15125 693 int32_t setDeviceId(const std::vector<uint8_t>& id);
mfiore 0:c62615f15125 694 int32_t setFrequencyBand(const uint8_t& band);
mfiore 0:c62615f15125 695 bool saveProtectedConfig();
mfiore 0:c62615f15125 696 void resetRadio();
mfiore 0:c62615f15125 697 int32_t setRadioMode(const uint8_t& mode);
mfiore 0:c62615f15125 698 std::map<uint8_t, uint8_t> dumpRegisters();
mfiore 0:c62615f15125 699 void eraseFlash();
mfiore 0:c62615f15125 700
mfiore 0:c62615f15125 701 mdot_stats getStats();
mfiore 0:c62615f15125 702
mfiore 0:c62615f15125 703 private:
mfiore 0:c62615f15125 704 mdot_stats _stats;
mfiore 0:c62615f15125 705
mfiore 0:c62615f15125 706 };
mfiore 0:c62615f15125 707
mfiore 0:c62615f15125 708 #endif