Library for LoRa communication using MultiTech MDOT.
Dependents: mDot_test_rx adc_sensor_lora mDotEVBM2X mDot_AT_firmware ... more
Function documentation is in mDot.h
Warning
Using libmDot 2.0.3 and above with an existing application may require a change in the MacEvent handler!
Compile applications with mbed v121 and mbed-rtos v116 libraries.
In AT Command Firmware remove line 803.
CommandTerminal/CommandTerminal.cpp
delete[] info->RxBuffer;
Likewise, if your application is handling events from the library asynchronously.
Revision 17:0da384ba484b, committed 2016-11-18
- Comitter:
- Leon Lindenfelser
- Date:
- Fri Nov 18 11:10:04 2016 -0600
- Parent:
- 16:e4f80db195b6
- Commit message:
- Previous libmDot version "16:e4f80db195b6" has the wrong header files for mDot.h and mDotEvent.h. Use with mbed version 121 and mbed-rtos version 116.
Changed in this revision
mDot.h | Show annotated file Show diff for this revision Revisions of this file |
mDotEvent.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r e4f80db195b6 -r 0da384ba484b mDot.h --- a/mDot.h Fri Nov 18 10:29:43 2016 -0600 +++ b/mDot.h Fri Nov 18 11:10:04 2016 -0600 @@ -5,16 +5,17 @@ #include "mbed.h" #include "rtos.h" -#include "Mote.h" #include <vector> #include <map> #include <string> class mDotEvent; +class LoRaMacEvent; class LoRaConfig; +class LoRaMac; +class MdotRadio; class mDot { - friend class mDotEvent; private: @@ -26,8 +27,6 @@ __WFI(); } - void initLora(); - void setLastError(const std::string& str); static bool validateBaudRate(const uint32_t& baud); @@ -62,11 +61,10 @@ static mDot* _instance; - lora::Mote _mote; + LoRaMac* _mac; + MdotRadio* _radio; + LoRaMacEvent* _events; LoRaConfig* _config; - lora::Settings _settings; - mDotEvent* _events; - Thread _idle_thread; std::string _last_error; static const uint32_t _baud_rates[]; @@ -116,7 +114,7 @@ } mdot_ret_code; enum JoinMode { - MANUAL = 0, + MANUAL, OTA, AUTO_OTA, PEER_TO_PEER @@ -160,11 +158,8 @@ }; enum FrequencyBands { - FB_868 = 0, - FB_915 = 1, - FB_EU868 = 0, // EU868 - FB_US915 = 1, // US915 - FB_AU915 = 2 + FB_868, // EU868 + FB_915 // US915 }; enum FrequencySubBands { @@ -205,7 +200,7 @@ typedef struct { int16_t fd; - char name[33]; + char name[30]; uint32_t size; } mdot_file; @@ -215,7 +210,6 @@ uint32_t Joins; uint32_t JoinFails; uint32_t MissedAcks; - uint32_t CRCErrors; } mdot_stats; typedef struct { @@ -338,10 +332,6 @@ */ bool getStandbyFlag(); - std::vector<uint16_t> getChannelMask(); - - int32_t setChannelMask(uint8_t offset, uint16_t mask); - /** Add a channel frequencies currently in use * @returns MDOT_OK */ @@ -561,14 +551,13 @@ */ void saveNetworkSession(); - /** Set number of times joining will retry each sub-band before changing - * to the next subband in US915 and AU915 + /** Set number of times joining will retry before giving up * @param retries must be between 0 - 255 * @returns MDOT_OK if success */ int32_t setJoinRetries(const uint8_t& retries); - /** Get number of times joining will retry each sub-band + /** Set number of times joining will retry before giving up * @returns join retries (0 - 255) */ uint8_t getJoinRetries(); @@ -691,42 +680,6 @@ */ uint32_t setJoinDelay(uint8_t delay); - /** Get join Rx1 datarate offset - * defaults to 0 - * @returns offset - */ - uint8_t getJoinRx1DataRateOffset(); - - /** Set join Rx1 datarate offset - * @param offset for datarate - * @return MDOT_OK if success - */ - uint32_t setJoinRx1DataRateOffset(uint8_t offset); - - /** Get join Rx2 datarate - * defaults to US:DR8, AU:DR8, EU:DR0 - * @returns datarate - */ - uint8_t getJoinRx2DataRate(); - - /** Set join Rx2 datarate - * @param datarate - * @return MDOT_OK if success - */ - uint32_t setJoinRx2DataRate(uint8_t datarate); - - /** Get join Rx2 frequency - * defaults US:923.3, AU:923.3, EU:869.525 - * @returns frequency - */ - uint32_t getJoinRx2Frequency(); - - /** Set join Rx2 frequency - * @param frequency - * @return MDOT_OK if success - */ - uint32_t setJoinRx2Frequency(uint32_t frequency); - /** Get rx delay in seconds * Defaults to 1 second * @returns number of seconds before response message is expected @@ -759,22 +712,11 @@ */ bool getDataPending(); - /** Get ack requested - * only valid after sending data to the gateway - * @returns true if server has requested ack - */ - bool getAckRequested(); - - /** Get is transmitting indicator + /** Get transmitting * @returns true if currently transmitting */ bool getIsTransmitting(); - /** Get is idle indicator - * @returns true if not currently transmitting, waiting or receiving - */ - bool getIsIdle(); - /** Set TX data rate * data rates affect maximum payload size * @param dr SF_7 - SF_12|DR0-DR7 for Europe, SF_7 - SF_10 | DR0-DR4 for United States @@ -792,6 +734,7 @@ */ uint32_t getRadioRandom(); + /** Get data rate spreading factor and bandwidth * EU868 Datarates * --------------- @@ -812,14 +755,6 @@ * DR3 - SF7BW125 * DR4 - SF8BW500 * - * AU915 Datarates - * --------------- - * DR0 - SF10BW125 - * DR1 - SF9BW125 - * DR2 - SF8BW125 - * DR3 - SF7BW125 - * DR4 - SF8BW500 - * * @returns spreading factor and bandwidth */ std::string getDateRateDetails(uint8_t rate); @@ -860,10 +795,6 @@ */ bool getTxWait(); - /** Cancel pending rx windows - */ - void cancelRxWindow(); - /** Get time on air * @returns the amount of time (in ms) it would take to send bytes bytes based on current configuration */ @@ -938,24 +869,6 @@ */ int32_t send(const std::vector<uint8_t>& data, const bool& blocking = true, const bool& highBw = false); - /** Inject mac command - * @param data a vector containing mac commands - * @returns MDOT_OK - */ - int32_t injectMacCommand(const std::vector<uint8_t>& data); - - /** - * Clear MAC command buffer to be sent in next uplink - * @returns MDOT_OK - */ - int32_t clearMacCommands(); - - /** - * Get MAC command buffer to be sent in next uplink - * @returns command bytes - */ - std::vector<uint8_t> getMacCommands(); - /** Fetch data received from the gateway * this function only checks to see if a packet has been received - it does not open a receive window * send() must be called before recv() @@ -1191,6 +1104,7 @@ int32_t setFlowControl(const bool& on); bool getFlowControl(); + // get/set serial clear on error // if enabled the data read from the serial port will be discarded if it cannot be sent or if the send fails // set function returns MDOT_OK if success @@ -1199,26 +1113,8 @@ // MTS_RADIO_DEBUG_COMMANDS - /** Disable Duty cycle - * enables or disables the duty cycle limitations - * **** ONLY TO BE USED FOR TESTINGS PURPOSES **** - * **** ALL DEPLOYABLE CODE MUST ADHERE TO LOCAL REGULATIONS **** - * **** THIS SETTING WILL NOT BE SAVED TO CONFIGURATION ***** - * @param val true to disable duty-cycle (default:false) - */ - int32_t setDisableDutyCycle(bool val); - - /** Disable Duty cycle - * **** ONLY TO BE USED FOR TESTINGS PURPOSES **** - * **** ALL DEPLOYABLE CODE MUST ADHERE TO LOCAL REGULATIONS **** - * **** THIS SETTING WILL NOT BE SAVED TO CONFIGURATION ***** - * @return true if duty-cycle is disabled (default:false) - */ - uint8_t getDisableDutyCycle(); - void openRxWindow(uint32_t timeout, uint8_t bandwidth = 0); - void closeRxWindow(); - void sendContinuous(bool enable=true); + void sendContinuous(); int32_t setDeviceId(const std::vector<uint8_t>& id); int32_t setFrequencyBand(const uint8_t& band); bool saveProtectedConfig(); @@ -1262,4 +1158,3 @@ }; #endif -
diff -r e4f80db195b6 -r 0da384ba484b mDotEvent.h --- a/mDotEvent.h Fri Nov 18 10:29:43 2016 -0600 +++ b/mDotEvent.h Fri Nov 18 11:10:04 2016 -0600 @@ -1,298 +1,29 @@ -#ifndef MDOT_EVENT_H -#define MDOT_EVENT_H - -#include "mDot.h" -#include "MacEvents.h" +#include "LoRaMacEvent.h" #include "MTSLog.h" -#include "MTSText.h" - -typedef union { - uint8_t Value; - struct { - uint8_t :1; - uint8_t Tx :1; - uint8_t Rx :1; - uint8_t RxData :1; - uint8_t RxSlot :2; - uint8_t LinkCheck :1; - uint8_t JoinAccept :1; - } Bits; -} LoRaMacEventFlags; -typedef enum { - LORAMAC_EVENT_INFO_STATUS_OK = 0, - LORAMAC_EVENT_INFO_STATUS_ERROR, - LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT, - LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT, - LORAMAC_EVENT_INFO_STATUS_RX_ERROR, - LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL, - LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL, - LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL, - LORAMAC_EVENT_INFO_STATUS_MIC_FAIL, -} LoRaMacEventInfoStatus; - -/*! - * LoRaMAC event information - */ -typedef struct { - LoRaMacEventInfoStatus Status; - lora::DownlinkControl Ctrl; - bool TxAckReceived; - uint8_t TxNbRetries; - uint8_t TxDatarate; - uint8_t RxPort; - uint8_t *RxBuffer; - uint8_t RxBufferSize; - int16_t RxRssi; - uint8_t RxSnr; - uint16_t Energy; - uint8_t DemodMargin; - uint8_t NbGateways; -} LoRaMacEventInfo; - -class mDotEvent: public lora::MacEvents { +class mDotEvent : public LoRaMacEvent { public: - mDotEvent() - : - LinkCheckAnsReceived(false), - DemodMargin(0), - NbGateways(0), - PacketReceived(false), - RxPort(0), - RxPayloadSize(0), - PongReceived(false), - PongRssi(0), - PongSnr(0), - AckReceived(false), - TxNbRetries(0) - { - memset(&_flags, 0, sizeof(LoRaMacEventFlags)); - memset(&_info, 0, sizeof(LoRaMacEventInfo)); - } - - virtual ~mDotEvent() { - } - - virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) { - if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { - std::string msg = "OK"; - switch (info->Status) { - case LORAMAC_EVENT_INFO_STATUS_ERROR: - msg = "ERROR"; - break; - case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: - msg = "TX_TIMEOUT"; - break; - case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: - msg = "RX_TIMEOUT"; - break; - case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: - msg = "RX_ERROR"; - break; - case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: - msg = "JOIN_FAIL"; - break; - case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL: - msg = "DOWNLINK_FAIL"; - break; - case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: - msg = "ADDRESS_FAIL"; - break; - case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: - msg = "MIC_FAIL"; - break; - default: - break; - } - logTrace("Event: %s", msg.c_str()); - - logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d", - flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept); - logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d", - info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize, - info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways); - } - } - - virtual void TxDone(uint8_t dr) { - RxPayloadSize = 0; - LinkCheckAnsReceived = false; - PacketReceived = false; - AckReceived = false; - PongReceived = false; - - logDebug("mDotEvent - TxDone"); - memset(&_flags, 0, sizeof(LoRaMacEventFlags)); - memset(&_info, 0, sizeof(LoRaMacEventInfo)); - - _flags.Bits.Tx = 1; - _info.TxDatarate = dr; - _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; - Notify(); - } - - void Notify() { - MacEvent(&_flags, &_info); - } - - virtual void TxTimeout(void) { - logDebug("mDotEvent - TxTimeout"); - - _flags.Bits.Tx = 1; - _info.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; - Notify(); - } + virtual ~mDotEvent() {} - virtual void JoinAccept(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { - logDebug("mDotEvent - JoinAccept"); - - _flags.Bits.Tx = 0; - _flags.Bits.JoinAccept = 1; - _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; - Notify(); - } - - virtual void JoinFailed(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { - logDebug("mDotEvent - JoinFailed"); - - _flags.Bits.Tx = 0; - _flags.Bits.JoinAccept = 1; - _info.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL; - Notify(); - } - - virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries = 0) { - logDebug("mDotEvent - PacketRx"); - RxPort = port; - PacketReceived = true; - - memcpy(RxPayload, payload, size); - RxPayloadSize = size; - - if (ctrl.Bits.Ack) { - AckReceived = true; - } - - if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { - std::string packet = mts::Text::bin2hexString(RxPayload, size); - logTrace("Payload: %s", packet.c_str()); - } + /*! + * MAC layer event callback prototype. + * + * \param [IN] flags Bit field indicating the MAC events occurred + * \param [IN] info Details about MAC events occurred + */ + virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) { + logDebug("mDotEvent"); - _flags.Bits.Tx = 0; - _flags.Bits.Rx = 1; - _flags.Bits.RxData = size > 0; - _flags.Bits.RxSlot = slot; - _info.RxBuffer = payload; - _info.RxBufferSize = size; - _info.RxPort = port; - _info.RxRssi = rssi; - _info.RxSnr = snr; - _info.TxAckReceived = AckReceived; - _info.TxAckReceived = retries; - _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; - Notify(); - } - - virtual void RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot) { - logDebug("mDotEvent - RxDone"); - } - - virtual void Pong(int16_t m_rssi, int8_t m_snr, int16_t s_rssi, int8_t s_snr) { - logDebug("mDotEvent - Pong"); - PongReceived = true; - PongRssi = s_rssi; - PongSnr = s_snr; - } + if (flags->Bits.Rx) { + logDebug("Rx"); - virtual void NetworkLinkCheck(int16_t m_rssi, int8_t m_snr, int8_t s_snr, uint8_t s_gateways) { - logDebug("mDotEvent - NetworkLinkCheck"); - LinkCheckAnsReceived = true; - DemodMargin = s_snr; - NbGateways = s_gateways; - - _flags.Bits.Tx = 0; - _flags.Bits.LinkCheck = 1; - _info.RxRssi = m_rssi; - _info.RxSnr = m_snr; - _info.DemodMargin = s_snr; - _info.NbGateways = s_gateways; - _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; - Notify(); - } - - virtual void RxTimeout(uint8_t slot) { - // logDebug("mDotEvent - RxTimeout"); - - _flags.Bits.Tx = 0; - _flags.Bits.RxSlot = slot; - _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT; - Notify(); - } - - virtual void RxError(uint8_t slot) { - logDebug("mDotEvent - RxError"); - - memset(&_flags, 0, sizeof(LoRaMacEventFlags)); - memset(&_info, 0, sizeof(LoRaMacEventInfo)); - - _flags.Bits.RxSlot = slot; - _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR; - Notify(); + // Event Object must delete RxBuffer + delete[] info->RxBuffer; + } } virtual uint8_t MeasureBattery(void) { return 255; } - - bool LinkCheckAnsReceived; - uint8_t DemodMargin; - uint8_t NbGateways; - - bool PacketReceived; - uint8_t RxPort; - uint8_t RxPayload[255]; - uint8_t RxPayloadSize; - - bool PongReceived; - int16_t PongRssi; - int16_t PongSnr; - - bool AckReceived; - uint8_t TxNbRetries; - - LoRaMacEventFlags& Flags() { - return _flags; - } - LoRaMacEventInfo& Info() { - return _info; - } - - private: - - LoRaMacEventFlags _flags; - LoRaMacEventInfo _info; - -// -// /*! -// * MAC layer event callback prototype. -// * -// * \param [IN] flags Bit field indicating the MAC events occurred -// * \param [IN] info Details about MAC events occurred -// */ -// virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) { -// logDebug("mDotEvent"); -// -// if (flags->Bits.Rx) { -// logDebug("Rx"); -// -// // Event Object must delete RxBuffer -// delete[] info->RxBuffer; -// } -// } -// - }; - -#endif // __MDOT_EVENT_H__ -