libmDot0mbed5

Fork of libmDot-mbed5 by MultiTech

Committer:
jreiss
Date:
Wed Aug 17 18:39:02 2016 +0000
Revision:
17:306ffaa5d79b
Parent:
16:b630e18103e5
Child:
19:f3a46d2bb9b3
update library to 2.0.2

Who changed what in which revision?

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