Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of libmDot-custom by
mDotEvent.h
00001 #ifndef MDOT_EVENT_H 00002 #define MDOT_EVENT_H 00003 00004 #include "mDot.h" 00005 #include "MacEvents.h" 00006 #include "MTSLog.h" 00007 #include "MTSText.h" 00008 00009 typedef union { 00010 uint8_t Value; 00011 struct { 00012 uint8_t :1; 00013 uint8_t Tx :1; 00014 uint8_t Rx :1; 00015 uint8_t RxData :1; 00016 uint8_t RxSlot :2; 00017 uint8_t LinkCheck :1; 00018 uint8_t JoinAccept :1; 00019 } Bits; 00020 } LoRaMacEventFlags; 00021 00022 typedef enum { 00023 LORAMAC_EVENT_INFO_STATUS_OK = 0, 00024 LORAMAC_EVENT_INFO_STATUS_ERROR, 00025 LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT, 00026 LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT, 00027 LORAMAC_EVENT_INFO_STATUS_RX_ERROR, 00028 LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL, 00029 LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL, 00030 LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL, 00031 LORAMAC_EVENT_INFO_STATUS_MIC_FAIL, 00032 } LoRaMacEventInfoStatus; 00033 00034 /*! 00035 * LoRaMAC event information 00036 */ 00037 typedef struct { 00038 LoRaMacEventInfoStatus Status; 00039 lora::DownlinkControl Ctrl; 00040 bool TxAckReceived; 00041 uint8_t TxNbRetries; 00042 uint8_t TxDatarate; 00043 uint8_t RxPort; 00044 uint8_t *RxBuffer; 00045 uint8_t RxBufferSize; 00046 int16_t RxRssi; 00047 uint8_t RxSnr; 00048 uint16_t Energy; 00049 uint8_t DemodMargin; 00050 uint8_t NbGateways; 00051 } LoRaMacEventInfo ; 00052 00053 class mDotEvent: public lora::MacEvents { 00054 public: 00055 00056 mDotEvent() 00057 : 00058 LinkCheckAnsReceived(false), 00059 DemodMargin(0), 00060 NbGateways(0), 00061 PacketReceived(false), 00062 RxPort(0), 00063 RxPayloadSize(0), 00064 PongReceived(false), 00065 PongRssi(0), 00066 PongSnr(0), 00067 AckReceived(false), 00068 TxNbRetries(0) 00069 { 00070 memset(&_flags, 0, sizeof(LoRaMacEventFlags)); 00071 memset(&_info, 0, sizeof(LoRaMacEventInfo )); 00072 } 00073 00074 virtual ~mDotEvent() { 00075 } 00076 00077 virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) { 00078 if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { 00079 std::string msg = "OK"; 00080 switch (info->Status) { 00081 case LORAMAC_EVENT_INFO_STATUS_ERROR: 00082 msg = "ERROR"; 00083 break; 00084 case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: 00085 msg = "TX_TIMEOUT"; 00086 break; 00087 case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: 00088 msg = "RX_TIMEOUT"; 00089 break; 00090 case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: 00091 msg = "RX_ERROR"; 00092 break; 00093 case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: 00094 msg = "JOIN_FAIL"; 00095 break; 00096 case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL: 00097 msg = "DOWNLINK_FAIL"; 00098 break; 00099 case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: 00100 msg = "ADDRESS_FAIL"; 00101 break; 00102 case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: 00103 msg = "MIC_FAIL"; 00104 break; 00105 default: 00106 break; 00107 } 00108 logTrace("Event: %s", msg.c_str()); 00109 00110 logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d", 00111 flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept); 00112 logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d", 00113 info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize, 00114 info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways); 00115 } 00116 } 00117 00118 virtual void TxDone(uint8_t dr) { 00119 RxPayloadSize = 0; 00120 LinkCheckAnsReceived = false; 00121 PacketReceived = false; 00122 AckReceived = false; 00123 PongReceived = false; 00124 TxNbRetries = 0; 00125 00126 logDebug("mDotEvent - TxDone"); 00127 memset(&_flags, 0, sizeof(LoRaMacEventFlags)); 00128 memset(&_info, 0, sizeof(LoRaMacEventInfo )); 00129 00130 _flags.Bits.Tx = 1; 00131 _info.TxDatarate = dr; 00132 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; 00133 Notify(); 00134 } 00135 00136 void Notify() { 00137 MacEvent(&_flags, &_info); 00138 } 00139 00140 virtual void TxTimeout(void) { 00141 logDebug("mDotEvent - TxTimeout"); 00142 00143 _flags.Bits.Tx = 1; 00144 _info.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; 00145 Notify(); 00146 } 00147 00148 virtual void JoinAccept(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { 00149 logDebug("mDotEvent - JoinAccept"); 00150 00151 _flags.Bits.Tx = 0; 00152 _flags.Bits.JoinAccept = 1; 00153 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; 00154 Notify(); 00155 } 00156 00157 virtual void JoinFailed(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { 00158 logDebug("mDotEvent - JoinFailed"); 00159 00160 _flags.Bits.Tx = 0; 00161 _flags.Bits.JoinAccept = 1; 00162 _info.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL; 00163 Notify(); 00164 } 00165 00166 virtual void MissedAck(uint8_t retries) { 00167 logDebug("mDotEvent - MissedAck : retries %u", retries); 00168 TxNbRetries = retries; 00169 _info.TxNbRetries = retries; 00170 } 00171 00172 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) { 00173 logDebug("mDotEvent - PacketRx"); 00174 RxPort = port; 00175 PacketReceived = true; 00176 00177 memcpy(RxPayload, payload, size); 00178 RxPayloadSize = size; 00179 00180 if (ctrl.Bits.Ack) { 00181 AckReceived = true; 00182 } 00183 00184 if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { 00185 std::string packet = mts::Text::bin2hexString(RxPayload, size); 00186 logTrace("Payload: %s", packet.c_str()); 00187 } 00188 00189 _flags.Bits.Tx = 0; 00190 _flags.Bits.Rx = 1; 00191 _flags.Bits.RxData = size > 0; 00192 _flags.Bits.RxSlot = slot; 00193 _info.RxBuffer = payload; 00194 _info.RxBufferSize = size; 00195 _info.RxPort = port; 00196 _info.RxRssi = rssi; 00197 _info.RxSnr = snr; 00198 _info.TxAckReceived = AckReceived; 00199 _info.TxNbRetries = retries; 00200 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; 00201 Notify(); 00202 } 00203 00204 virtual void RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot) { 00205 logDebug("mDotEvent - RxDone"); 00206 } 00207 00208 virtual void Pong(int16_t m_rssi, int8_t m_snr, int16_t s_rssi, int8_t s_snr) { 00209 logDebug("mDotEvent - Pong"); 00210 PongReceived = true; 00211 PongRssi = s_rssi; 00212 PongSnr = s_snr; 00213 } 00214 00215 virtual void NetworkLinkCheck(int16_t m_rssi, int8_t m_snr, int8_t s_snr, uint8_t s_gateways) { 00216 logDebug("mDotEvent - NetworkLinkCheck"); 00217 LinkCheckAnsReceived = true; 00218 DemodMargin = s_snr; 00219 NbGateways = s_gateways; 00220 00221 _flags.Bits.Tx = 0; 00222 _flags.Bits.LinkCheck = 1; 00223 _info.RxRssi = m_rssi; 00224 _info.RxSnr = m_snr; 00225 _info.DemodMargin = s_snr; 00226 _info.NbGateways = s_gateways; 00227 _info.Status = LORAMAC_EVENT_INFO_STATUS_OK; 00228 Notify(); 00229 } 00230 00231 virtual void RxTimeout(uint8_t slot) { 00232 // logDebug("mDotEvent - RxTimeout"); 00233 00234 _flags.Bits.Tx = 0; 00235 _flags.Bits.RxSlot = slot; 00236 _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT; 00237 Notify(); 00238 } 00239 00240 virtual void RxError(uint8_t slot) { 00241 logDebug("mDotEvent - RxError"); 00242 00243 memset(&_flags, 0, sizeof(LoRaMacEventFlags)); 00244 memset(&_info, 0, sizeof(LoRaMacEventInfo )); 00245 00246 _flags.Bits.RxSlot = slot; 00247 _info.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR; 00248 Notify(); 00249 } 00250 00251 virtual uint8_t MeasureBattery(void) { 00252 return 255; 00253 } 00254 00255 bool LinkCheckAnsReceived; 00256 uint8_t DemodMargin; 00257 uint8_t NbGateways; 00258 00259 bool PacketReceived; 00260 uint8_t RxPort; 00261 uint8_t RxPayload[255]; 00262 uint8_t RxPayloadSize; 00263 00264 bool PongReceived; 00265 int16_t PongRssi; 00266 int16_t PongSnr; 00267 00268 bool AckReceived; 00269 uint8_t TxNbRetries; 00270 00271 LoRaMacEventFlags& Flags() { 00272 return _flags; 00273 } 00274 LoRaMacEventInfo & Info() { 00275 return _info; 00276 } 00277 00278 private: 00279 00280 LoRaMacEventFlags _flags; 00281 LoRaMacEventInfo _info; 00282 00283 // 00284 // /*! 00285 // * MAC layer event callback prototype. 00286 // * 00287 // * \param [IN] flags Bit field indicating the MAC events occurred 00288 // * \param [IN] info Details about MAC events occurred 00289 // */ 00290 // virtual void MacEvent(LoRaMacEventFlags *flags, LoRaMacEventInfo *info) { 00291 // logDebug("mDotEvent"); 00292 // 00293 // if (flags->Bits.Rx) { 00294 // logDebug("Rx"); 00295 // 00296 // // Event Object must delete RxBuffer 00297 // delete[] info->RxBuffer; 00298 // } 00299 // } 00300 // 00301 00302 }; 00303 00304 #endif // __MDOT_EVENT_H__ 00305
Generated on Tue Jul 12 2022 22:21:41 by
1.7.2
