Library for LoRa communication using MultiTech MDOT. Not sure why it wants to republish this

Fork of libmDot by MultiTech

Committer:
jreiss
Date:
Thu Aug 18 16:07:10 2016 +0000
Revision:
15:b50f92f1c6ff
Parent:
13:0f89ec319c18
update libmDot to 2.0.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jreiss 15:b50f92f1c6ff 1 #ifndef MDOT_EVENT_H
jreiss 15:b50f92f1c6ff 2 #define MDOT_EVENT_H
jreiss 15:b50f92f1c6ff 3
jreiss 15:b50f92f1c6ff 4 #include "mDot.h"
jreiss 15:b50f92f1c6ff 5 #include "MacEvents.h"
Mike Fiore 13:0f89ec319c18 6 #include "MTSLog.h"
jreiss 15:b50f92f1c6ff 7 #include "MTSText.h"
Mike Fiore 13:0f89ec319c18 8
jreiss 15:b50f92f1c6ff 9 typedef union {
jreiss 15:b50f92f1c6ff 10 uint8_t Value;
jreiss 15:b50f92f1c6ff 11 struct {
jreiss 15:b50f92f1c6ff 12 uint8_t :1;
jreiss 15:b50f92f1c6ff 13 uint8_t Tx :1;
jreiss 15:b50f92f1c6ff 14 uint8_t Rx :1;
jreiss 15:b50f92f1c6ff 15 uint8_t RxData :1;
jreiss 15:b50f92f1c6ff 16 uint8_t RxSlot :2;
jreiss 15:b50f92f1c6ff 17 uint8_t LinkCheck :1;
jreiss 15:b50f92f1c6ff 18 uint8_t JoinAccept :1;
jreiss 15:b50f92f1c6ff 19 } Bits;
jreiss 15:b50f92f1c6ff 20 } LoRaMacEventFlags;
jreiss 15:b50f92f1c6ff 21
jreiss 15:b50f92f1c6ff 22 typedef enum {
jreiss 15:b50f92f1c6ff 23 LORAMAC_EVENT_INFO_STATUS_OK = 0,
jreiss 15:b50f92f1c6ff 24 LORAMAC_EVENT_INFO_STATUS_ERROR,
jreiss 15:b50f92f1c6ff 25 LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT,
jreiss 15:b50f92f1c6ff 26 LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT,
jreiss 15:b50f92f1c6ff 27 LORAMAC_EVENT_INFO_STATUS_RX_ERROR,
jreiss 15:b50f92f1c6ff 28 LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL,
jreiss 15:b50f92f1c6ff 29 LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL,
jreiss 15:b50f92f1c6ff 30 LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL,
jreiss 15:b50f92f1c6ff 31 LORAMAC_EVENT_INFO_STATUS_MIC_FAIL,
jreiss 15:b50f92f1c6ff 32 } LoRaMacEventInfoStatus;
jreiss 15:b50f92f1c6ff 33
jreiss 15:b50f92f1c6ff 34 /*!
jreiss 15:b50f92f1c6ff 35 * LoRaMAC event information
jreiss 15:b50f92f1c6ff 36 */
jreiss 15:b50f92f1c6ff 37 typedef struct {
jreiss 15:b50f92f1c6ff 38 LoRaMacEventInfoStatus Status;
jreiss 15:b50f92f1c6ff 39 lora::DownlinkControl Ctrl;
jreiss 15:b50f92f1c6ff 40 bool TxAckReceived;
jreiss 15:b50f92f1c6ff 41 uint8_t TxNbRetries;
jreiss 15:b50f92f1c6ff 42 uint8_t TxDatarate;
jreiss 15:b50f92f1c6ff 43 uint8_t RxPort;
jreiss 15:b50f92f1c6ff 44 uint8_t *RxBuffer;
jreiss 15:b50f92f1c6ff 45 uint8_t RxBufferSize;
jreiss 15:b50f92f1c6ff 46 int16_t RxRssi;
jreiss 15:b50f92f1c6ff 47 uint8_t RxSnr;
jreiss 15:b50f92f1c6ff 48 uint16_t Energy;
jreiss 15:b50f92f1c6ff 49 uint8_t DemodMargin;
jreiss 15:b50f92f1c6ff 50 uint8_t NbGateways;
jreiss 15:b50f92f1c6ff 51 } LoRaMacEventInfo;
jreiss 15:b50f92f1c6ff 52
jreiss 15:b50f92f1c6ff 53 class mDotEvent: public lora::MacEvents {
Mike Fiore 13:0f89ec319c18 54 public:
Mike Fiore 13:0f89ec319c18 55
jreiss 15:b50f92f1c6ff 56 mDotEvent()
jreiss 15:b50f92f1c6ff 57 :
jreiss 15:b50f92f1c6ff 58 LinkCheckAnsReceived(false),
jreiss 15:b50f92f1c6ff 59 DemodMargin(0),
jreiss 15:b50f92f1c6ff 60 NbGateways(0),
jreiss 15:b50f92f1c6ff 61 PacketReceived(false),
jreiss 15:b50f92f1c6ff 62 RxPort(0),
jreiss 15:b50f92f1c6ff 63 RxPayloadSize(0),
jreiss 15:b50f92f1c6ff 64 PongReceived(false),
jreiss 15:b50f92f1c6ff 65 PongRssi(0),
jreiss 15:b50f92f1c6ff 66 PongSnr(0),
jreiss 15:b50f92f1c6ff 67 AckReceived(false),
jreiss 15:b50f92f1c6ff 68 TxNbRetries(0)
jreiss 15:b50f92f1c6ff 69 {
jreiss 15:b50f92f1c6ff 70 memset(&_flags, 0, sizeof(LoRaMacEventFlags));
jreiss 15:b50f92f1c6ff 71 memset(&_info, 0, sizeof(LoRaMacEventInfo));
jreiss 15:b50f92f1c6ff 72 }
Mike Fiore 13:0f89ec319c18 73
jreiss 15:b50f92f1c6ff 74 virtual ~mDotEvent() {
jreiss 15:b50f92f1c6ff 75 }
jreiss 15:b50f92f1c6ff 76
Mike Fiore 13:0f89ec319c18 77 virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) {
jreiss 15:b50f92f1c6ff 78 if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
jreiss 15:b50f92f1c6ff 79 std::string msg = "OK";
jreiss 15:b50f92f1c6ff 80 switch (info->Status) {
jreiss 15:b50f92f1c6ff 81 case LORAMAC_EVENT_INFO_STATUS_ERROR:
jreiss 15:b50f92f1c6ff 82 msg = "ERROR";
jreiss 15:b50f92f1c6ff 83 break;
jreiss 15:b50f92f1c6ff 84 case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT:
jreiss 15:b50f92f1c6ff 85 msg = "TX_TIMEOUT";
jreiss 15:b50f92f1c6ff 86 break;
jreiss 15:b50f92f1c6ff 87 case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT:
jreiss 15:b50f92f1c6ff 88 msg = "RX_TIMEOUT";
jreiss 15:b50f92f1c6ff 89 break;
jreiss 15:b50f92f1c6ff 90 case LORAMAC_EVENT_INFO_STATUS_RX_ERROR:
jreiss 15:b50f92f1c6ff 91 msg = "RX_ERROR";
jreiss 15:b50f92f1c6ff 92 break;
jreiss 15:b50f92f1c6ff 93 case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL:
jreiss 15:b50f92f1c6ff 94 msg = "JOIN_FAIL";
jreiss 15:b50f92f1c6ff 95 break;
jreiss 15:b50f92f1c6ff 96 case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL:
jreiss 15:b50f92f1c6ff 97 msg = "DOWNLINK_FAIL";
jreiss 15:b50f92f1c6ff 98 break;
jreiss 15:b50f92f1c6ff 99 case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL:
jreiss 15:b50f92f1c6ff 100 msg = "ADDRESS_FAIL";
jreiss 15:b50f92f1c6ff 101 break;
jreiss 15:b50f92f1c6ff 102 case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL:
jreiss 15:b50f92f1c6ff 103 msg = "MIC_FAIL";
jreiss 15:b50f92f1c6ff 104 break;
jreiss 15:b50f92f1c6ff 105 default:
jreiss 15:b50f92f1c6ff 106 break;
jreiss 15:b50f92f1c6ff 107 }
jreiss 15:b50f92f1c6ff 108 logTrace("Event: %s", msg.c_str());
jreiss 15:b50f92f1c6ff 109
jreiss 15:b50f92f1c6ff 110 logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d",
jreiss 15:b50f92f1c6ff 111 flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept);
jreiss 15:b50f92f1c6ff 112 logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d",
jreiss 15:b50f92f1c6ff 113 info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize,
jreiss 15:b50f92f1c6ff 114 info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways);
jreiss 15:b50f92f1c6ff 115 }
jreiss 15:b50f92f1c6ff 116 }
jreiss 15:b50f92f1c6ff 117
jreiss 15:b50f92f1c6ff 118 virtual void TxDone(uint8_t dr) {
jreiss 15:b50f92f1c6ff 119 RxPayloadSize = 0;
jreiss 15:b50f92f1c6ff 120 LinkCheckAnsReceived = false;
jreiss 15:b50f92f1c6ff 121 PacketReceived = false;
jreiss 15:b50f92f1c6ff 122 AckReceived = false;
jreiss 15:b50f92f1c6ff 123 PongReceived = false;
jreiss 15:b50f92f1c6ff 124
jreiss 15:b50f92f1c6ff 125 logDebug("mDotEvent - TxDone");
jreiss 15:b50f92f1c6ff 126 memset(&_flags, 0, sizeof(LoRaMacEventFlags));
jreiss 15:b50f92f1c6ff 127 memset(&_info, 0, sizeof(LoRaMacEventInfo));
jreiss 15:b50f92f1c6ff 128
jreiss 15:b50f92f1c6ff 129 _flags.Bits.Tx = 1;
jreiss 15:b50f92f1c6ff 130 _info.TxDatarate = dr;
jreiss 15:b50f92f1c6ff 131 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK;
jreiss 15:b50f92f1c6ff 132 Notify();
jreiss 15:b50f92f1c6ff 133 }
jreiss 15:b50f92f1c6ff 134
jreiss 15:b50f92f1c6ff 135 void Notify() {
jreiss 15:b50f92f1c6ff 136 MacEvent(&_flags, &_info);
jreiss 15:b50f92f1c6ff 137 }
jreiss 15:b50f92f1c6ff 138
jreiss 15:b50f92f1c6ff 139 virtual void TxTimeout(void) {
jreiss 15:b50f92f1c6ff 140 logDebug("mDotEvent - TxTimeout");
jreiss 15:b50f92f1c6ff 141
jreiss 15:b50f92f1c6ff 142 _flags.Bits.Tx = 1;
jreiss 15:b50f92f1c6ff 143 _info.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
jreiss 15:b50f92f1c6ff 144 Notify();
jreiss 15:b50f92f1c6ff 145 }
jreiss 15:b50f92f1c6ff 146
jreiss 15:b50f92f1c6ff 147 virtual void JoinAccept(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
jreiss 15:b50f92f1c6ff 148 logDebug("mDotEvent - JoinAccept");
jreiss 15:b50f92f1c6ff 149
jreiss 15:b50f92f1c6ff 150 _flags.Bits.Tx = 0;
jreiss 15:b50f92f1c6ff 151 _flags.Bits.JoinAccept = 1;
jreiss 15:b50f92f1c6ff 152 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK;
jreiss 15:b50f92f1c6ff 153 Notify();
jreiss 15:b50f92f1c6ff 154 }
jreiss 15:b50f92f1c6ff 155
jreiss 15:b50f92f1c6ff 156 virtual void JoinFailed(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
jreiss 15:b50f92f1c6ff 157 logDebug("mDotEvent - JoinFailed");
Mike Fiore 13:0f89ec319c18 158
jreiss 15:b50f92f1c6ff 159 _flags.Bits.Tx = 0;
jreiss 15:b50f92f1c6ff 160 _flags.Bits.JoinAccept = 1;
jreiss 15:b50f92f1c6ff 161 _info.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
jreiss 15:b50f92f1c6ff 162 Notify();
jreiss 15:b50f92f1c6ff 163 }
jreiss 15:b50f92f1c6ff 164
jreiss 15:b50f92f1c6ff 165 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) {
jreiss 15:b50f92f1c6ff 166 logDebug("mDotEvent - PacketRx");
jreiss 15:b50f92f1c6ff 167 RxPort = port;
jreiss 15:b50f92f1c6ff 168 PacketReceived = true;
jreiss 15:b50f92f1c6ff 169
jreiss 15:b50f92f1c6ff 170 memcpy(RxPayload, payload, size);
jreiss 15:b50f92f1c6ff 171 RxPayloadSize = size;
jreiss 15:b50f92f1c6ff 172
jreiss 15:b50f92f1c6ff 173 if (ctrl.Bits.Ack) {
jreiss 15:b50f92f1c6ff 174 AckReceived = true;
jreiss 15:b50f92f1c6ff 175 }
jreiss 15:b50f92f1c6ff 176
jreiss 15:b50f92f1c6ff 177 if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
jreiss 15:b50f92f1c6ff 178 std::string packet = mts::Text::bin2hexString(RxPayload, size);
jreiss 15:b50f92f1c6ff 179 logTrace("Payload: %s", packet.c_str());
jreiss 15:b50f92f1c6ff 180 }
jreiss 15:b50f92f1c6ff 181
jreiss 15:b50f92f1c6ff 182 _flags.Bits.Tx = 0;
jreiss 15:b50f92f1c6ff 183 _flags.Bits.Rx = 1;
jreiss 15:b50f92f1c6ff 184 _flags.Bits.RxData = size > 0;
jreiss 15:b50f92f1c6ff 185 _flags.Bits.RxSlot = slot;
jreiss 15:b50f92f1c6ff 186 _info.RxBuffer = payload;
jreiss 15:b50f92f1c6ff 187 _info.RxBufferSize = size;
jreiss 15:b50f92f1c6ff 188 _info.RxPort = port;
jreiss 15:b50f92f1c6ff 189 _info.RxRssi = rssi;
jreiss 15:b50f92f1c6ff 190 _info.RxSnr = snr;
jreiss 15:b50f92f1c6ff 191 _info.TxAckReceived = AckReceived;
jreiss 15:b50f92f1c6ff 192 _info.TxAckReceived = retries;
jreiss 15:b50f92f1c6ff 193 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK;
jreiss 15:b50f92f1c6ff 194 Notify();
jreiss 15:b50f92f1c6ff 195 }
jreiss 15:b50f92f1c6ff 196
jreiss 15:b50f92f1c6ff 197 virtual void RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot) {
jreiss 15:b50f92f1c6ff 198 logDebug("mDotEvent - RxDone");
jreiss 15:b50f92f1c6ff 199 }
Mike Fiore 13:0f89ec319c18 200
jreiss 15:b50f92f1c6ff 201 virtual void Pong(int16_t m_rssi, int8_t m_snr, int16_t s_rssi, int8_t s_snr) {
jreiss 15:b50f92f1c6ff 202 logDebug("mDotEvent - Pong");
jreiss 15:b50f92f1c6ff 203 PongReceived = true;
jreiss 15:b50f92f1c6ff 204 PongRssi = s_rssi;
jreiss 15:b50f92f1c6ff 205 PongSnr = s_snr;
jreiss 15:b50f92f1c6ff 206 }
jreiss 15:b50f92f1c6ff 207
jreiss 15:b50f92f1c6ff 208 virtual void NetworkLinkCheck(int16_t m_rssi, int8_t m_snr, int8_t s_snr, uint8_t s_gateways) {
jreiss 15:b50f92f1c6ff 209 logDebug("mDotEvent - NetworkLinkCheck");
jreiss 15:b50f92f1c6ff 210 LinkCheckAnsReceived = true;
jreiss 15:b50f92f1c6ff 211 DemodMargin = s_snr;
jreiss 15:b50f92f1c6ff 212 NbGateways = s_gateways;
jreiss 15:b50f92f1c6ff 213
jreiss 15:b50f92f1c6ff 214 _flags.Bits.Tx = 0;
jreiss 15:b50f92f1c6ff 215 _flags.Bits.LinkCheck = 1;
jreiss 15:b50f92f1c6ff 216 _info.RxRssi = m_rssi;
jreiss 15:b50f92f1c6ff 217 _info.RxSnr = m_snr;
jreiss 15:b50f92f1c6ff 218 _info.DemodMargin = s_snr;
jreiss 15:b50f92f1c6ff 219 _info.NbGateways = s_gateways;
jreiss 15:b50f92f1c6ff 220 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK;
jreiss 15:b50f92f1c6ff 221 Notify();
jreiss 15:b50f92f1c6ff 222 }
jreiss 15:b50f92f1c6ff 223
jreiss 15:b50f92f1c6ff 224 virtual void RxTimeout(uint8_t slot) {
jreiss 15:b50f92f1c6ff 225 // logDebug("mDotEvent - RxTimeout");
jreiss 15:b50f92f1c6ff 226
jreiss 15:b50f92f1c6ff 227 _flags.Bits.Tx = 0;
jreiss 15:b50f92f1c6ff 228 _flags.Bits.RxSlot = slot;
jreiss 15:b50f92f1c6ff 229 _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
jreiss 15:b50f92f1c6ff 230 Notify();
jreiss 15:b50f92f1c6ff 231 }
jreiss 15:b50f92f1c6ff 232
jreiss 15:b50f92f1c6ff 233 virtual void RxError(uint8_t slot) {
jreiss 15:b50f92f1c6ff 234 logDebug("mDotEvent - RxError");
jreiss 15:b50f92f1c6ff 235
jreiss 15:b50f92f1c6ff 236 memset(&_flags, 0, sizeof(LoRaMacEventFlags));
jreiss 15:b50f92f1c6ff 237 memset(&_info, 0, sizeof(LoRaMacEventInfo));
jreiss 15:b50f92f1c6ff 238
jreiss 15:b50f92f1c6ff 239 _flags.Bits.RxSlot = slot;
jreiss 15:b50f92f1c6ff 240 _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
jreiss 15:b50f92f1c6ff 241 Notify();
Mike Fiore 13:0f89ec319c18 242 }
Mike Fiore 13:0f89ec319c18 243
Mike Fiore 13:0f89ec319c18 244 virtual uint8_t MeasureBattery(void) {
Mike Fiore 13:0f89ec319c18 245 return 255;
Mike Fiore 13:0f89ec319c18 246 }
jreiss 15:b50f92f1c6ff 247
jreiss 15:b50f92f1c6ff 248 bool LinkCheckAnsReceived;
jreiss 15:b50f92f1c6ff 249 uint8_t DemodMargin;
jreiss 15:b50f92f1c6ff 250 uint8_t NbGateways;
jreiss 15:b50f92f1c6ff 251
jreiss 15:b50f92f1c6ff 252 bool PacketReceived;
jreiss 15:b50f92f1c6ff 253 uint8_t RxPort;
jreiss 15:b50f92f1c6ff 254 uint8_t RxPayload[255];
jreiss 15:b50f92f1c6ff 255 uint8_t RxPayloadSize;
jreiss 15:b50f92f1c6ff 256
jreiss 15:b50f92f1c6ff 257 bool PongReceived;
jreiss 15:b50f92f1c6ff 258 int16_t PongRssi;
jreiss 15:b50f92f1c6ff 259 int16_t PongSnr;
jreiss 15:b50f92f1c6ff 260
jreiss 15:b50f92f1c6ff 261 bool AckReceived;
jreiss 15:b50f92f1c6ff 262 uint8_t TxNbRetries;
jreiss 15:b50f92f1c6ff 263
jreiss 15:b50f92f1c6ff 264 LoRaMacEventFlags& Flags() {
jreiss 15:b50f92f1c6ff 265 return _flags;
jreiss 15:b50f92f1c6ff 266 }
jreiss 15:b50f92f1c6ff 267 LoRaMacEventInfo& Info() {
jreiss 15:b50f92f1c6ff 268 return _info;
jreiss 15:b50f92f1c6ff 269 }
jreiss 15:b50f92f1c6ff 270
jreiss 15:b50f92f1c6ff 271 private:
jreiss 15:b50f92f1c6ff 272
jreiss 15:b50f92f1c6ff 273 LoRaMacEventFlags _flags;
jreiss 15:b50f92f1c6ff 274 LoRaMacEventInfo _info;
jreiss 15:b50f92f1c6ff 275
jreiss 15:b50f92f1c6ff 276 //
jreiss 15:b50f92f1c6ff 277 // /*!
jreiss 15:b50f92f1c6ff 278 // * MAC layer event callback prototype.
jreiss 15:b50f92f1c6ff 279 // *
jreiss 15:b50f92f1c6ff 280 // * \param [IN] flags Bit field indicating the MAC events occurred
jreiss 15:b50f92f1c6ff 281 // * \param [IN] info Details about MAC events occurred
jreiss 15:b50f92f1c6ff 282 // */
jreiss 15:b50f92f1c6ff 283 // virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) {
jreiss 15:b50f92f1c6ff 284 // logDebug("mDotEvent");
jreiss 15:b50f92f1c6ff 285 //
jreiss 15:b50f92f1c6ff 286 // if (flags->Bits.Rx) {
jreiss 15:b50f92f1c6ff 287 // logDebug("Rx");
jreiss 15:b50f92f1c6ff 288 //
jreiss 15:b50f92f1c6ff 289 // // Event Object must delete RxBuffer
jreiss 15:b50f92f1c6ff 290 // delete[] info->RxBuffer;
jreiss 15:b50f92f1c6ff 291 // }
jreiss 15:b50f92f1c6ff 292 // }
jreiss 15:b50f92f1c6ff 293 //
jreiss 15:b50f92f1c6ff 294
Mike Fiore 13:0f89ec319c18 295 };
jreiss 15:b50f92f1c6ff 296
jreiss 15:b50f92f1c6ff 297 #endif // __MDOT_EVENT_H__
jreiss 15:b50f92f1c6ff 298