Shoalhaven Water / Mbed OS Cloudtracker

Dependencies:   libmDot-mbed5 ISL29011

Committer:
aidanwynn
Date:
Tue Jun 01 05:57:28 2021 +0000
Revision:
48:c8f7a4ad94b4
Parent:
47:2bc515eeaa18
Updated version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 14:19fae4509473 1 #ifndef __RADIO_EVENT_H__
Mike Fiore 14:19fae4509473 2 #define __RADIO_EVENT_H__
Mike Fiore 14:19fae4509473 3
Mike Fiore 14:19fae4509473 4 #include "dot_util.h"
Mike Fiore 14:19fae4509473 5 #include "mDotEvent.h"
Jason Reiss 34:f6486829a451 6 #include "Fota.h"
aidanwynn 47:2bc515eeaa18 7 #include <string.h>
aidanwynn 47:2bc515eeaa18 8
aidanwynn 47:2bc515eeaa18 9 extern bool rejoin;
aidanwynn 47:2bc515eeaa18 10 extern uint16_t rain_total;
aidanwynn 47:2bc515eeaa18 11 extern float uplinkInterval;
aidanwynn 47:2bc515eeaa18 12 extern uint8_t downlink_msg;
Mike Fiore 14:19fae4509473 13
Mike Fiore 14:19fae4509473 14 class RadioEvent : public mDotEvent
Mike Fiore 14:19fae4509473 15 {
Jason Reiss 34:f6486829a451 16
Mike Fiore 14:19fae4509473 17 public:
Mike Fiore 14:19fae4509473 18 RadioEvent() {}
Jason Reiss 34:f6486829a451 19
Mike Fiore 14:19fae4509473 20 virtual ~RadioEvent() {}
Jason Reiss 34:f6486829a451 21
Jason Reiss 40:3cb0ed329c6e 22 virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int16_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries, uint32_t address, bool dupRx) {
aidanwynn 47:2bc515eeaa18 23 mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, dupRx);
aidanwynn 47:2bc515eeaa18 24
Jason Reiss 34:f6486829a451 25 #if ACTIVE_EXAMPLE == FOTA_EXAMPLE
Jason Reiss 34:f6486829a451 26 if(port == 200 || port == 201 || port == 202) {
Jason Reiss 34:f6486829a451 27 Fota::getInstance()->processCmd(payload, port, size);
Jason Reiss 34:f6486829a451 28 }
Jason Reiss 34:f6486829a451 29 #endif
Jason Reiss 34:f6486829a451 30 }
Jason Reiss 34:f6486829a451 31
Mike Fiore 14:19fae4509473 32 /*!
Mike Fiore 14:19fae4509473 33 * MAC layer event callback prototype.
Mike Fiore 14:19fae4509473 34 *
Mike Fiore 14:19fae4509473 35 * \param [IN] flags Bit field indicating the MAC events occurred
Mike Fiore 14:19fae4509473 36 * \param [IN] info Details about MAC events occurred
Mike Fiore 14:19fae4509473 37 */
Mike Fiore 14:19fae4509473 38 virtual void MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) {
Jason Reiss 34:f6486829a451 39
Mike Fiore 14:19fae4509473 40 if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
Mike Fiore 14:19fae4509473 41 std::string msg = "OK";
Mike Fiore 14:19fae4509473 42 switch (info->Status) {
Mike Fiore 14:19fae4509473 43 case LORAMAC_EVENT_INFO_STATUS_ERROR:
Mike Fiore 14:19fae4509473 44 msg = "ERROR";
Mike Fiore 14:19fae4509473 45 break;
Mike Fiore 14:19fae4509473 46 case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT:
Mike Fiore 14:19fae4509473 47 msg = "TX_TIMEOUT";
Mike Fiore 14:19fae4509473 48 break;
Mike Fiore 14:19fae4509473 49 case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT:
Mike Fiore 14:19fae4509473 50 msg = "RX_TIMEOUT";
Mike Fiore 14:19fae4509473 51 break;
Mike Fiore 14:19fae4509473 52 case LORAMAC_EVENT_INFO_STATUS_RX_ERROR:
Mike Fiore 14:19fae4509473 53 msg = "RX_ERROR";
Mike Fiore 14:19fae4509473 54 break;
Mike Fiore 14:19fae4509473 55 case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL:
Mike Fiore 14:19fae4509473 56 msg = "JOIN_FAIL";
Mike Fiore 14:19fae4509473 57 break;
Mike Fiore 14:19fae4509473 58 case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL:
Mike Fiore 14:19fae4509473 59 msg = "DOWNLINK_FAIL";
Mike Fiore 14:19fae4509473 60 break;
Mike Fiore 14:19fae4509473 61 case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL:
Mike Fiore 14:19fae4509473 62 msg = "ADDRESS_FAIL";
Mike Fiore 14:19fae4509473 63 break;
Mike Fiore 14:19fae4509473 64 case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL:
Mike Fiore 14:19fae4509473 65 msg = "MIC_FAIL";
Mike Fiore 14:19fae4509473 66 break;
Mike Fiore 14:19fae4509473 67 default:
Mike Fiore 14:19fae4509473 68 break;
Mike Fiore 14:19fae4509473 69 }
Mike Fiore 14:19fae4509473 70 logTrace("Event: %s", msg.c_str());
Jason Reiss 34:f6486829a451 71
Mike Fiore 14:19fae4509473 72 logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d",
Mike Fiore 14:19fae4509473 73 flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept);
Mike Fiore 14:19fae4509473 74 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 14:19fae4509473 75 info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize,
Mike Fiore 14:19fae4509473 76 info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways);
Mike Fiore 14:19fae4509473 77 }
Jason Reiss 34:f6486829a451 78
Mike Fiore 14:19fae4509473 79 if (flags->Bits.Rx) {
Jason Reiss 34:f6486829a451 80
Mike Fiore 14:19fae4509473 81 logDebug("Rx %d bytes", info->RxBufferSize);
Mike Fiore 14:19fae4509473 82 if (info->RxBufferSize > 0) {
aidanwynn 47:2bc515eeaa18 83 //#if ACTIVE_EXAMPLE != FOTA_EXAMPLE
Jason Reiss 34:f6486829a451 84 // print RX data as string and hexadecimal
Mike Fiore 14:19fae4509473 85 std::string rx((const char*)info->RxBuffer, info->RxBufferSize);
aidanwynn 47:2bc515eeaa18 86 //printf("Rx data: %s [%s]\r\n", rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
aidanwynn 47:2bc515eeaa18 87
aidanwynn 47:2bc515eeaa18 88 /// - SECTION FOR DOWNLINK HANDLING - ////////////////////////////////////////////////////////////////////////////////////////////////////
aidanwynn 47:2bc515eeaa18 89 std::string rxData = mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str();
aidanwynn 47:2bc515eeaa18 90 logInfo("Rx data: [%s]\r", mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
aidanwynn 47:2bc515eeaa18 91
aidanwynn 47:2bc515eeaa18 92 if(rxData.compare("01") == 0){
aidanwynn 47:2bc515eeaa18 93 logInfo("Command received: [01]");
aidanwynn 47:2bc515eeaa18 94 logInfo("Soft reset of the system");
aidanwynn 47:2bc515eeaa18 95 dot->resetCpu();
aidanwynn 47:2bc515eeaa18 96 }
aidanwynn 47:2bc515eeaa18 97 else if(rxData.compare("02") == 0){
aidanwynn 47:2bc515eeaa18 98 logInfo("Command received: [02]");
aidanwynn 47:2bc515eeaa18 99 logInfo("Setting rain_total to 0");
aidanwynn 47:2bc515eeaa18 100 rain_total = 0;
aidanwynn 47:2bc515eeaa18 101 downlink_msg = 0x02;
aidanwynn 47:2bc515eeaa18 102 }
aidanwynn 47:2bc515eeaa18 103 else if(rxData.compare("03") == 0){
aidanwynn 47:2bc515eeaa18 104 logInfo("Command received: [03]");
aidanwynn 47:2bc515eeaa18 105 logInfo("Setting uplinkInterval to 15mins");
aidanwynn 47:2bc515eeaa18 106 uplinkInterval = 15;
aidanwynn 47:2bc515eeaa18 107 downlink_msg = 0x03;
aidanwynn 47:2bc515eeaa18 108 }
aidanwynn 47:2bc515eeaa18 109 else if(rxData.compare("04") == 0){
aidanwynn 47:2bc515eeaa18 110 logInfo("Command received: [04]");
aidanwynn 47:2bc515eeaa18 111 logInfo("Setting uplinkInterval to 30mins");
aidanwynn 47:2bc515eeaa18 112 uplinkInterval = 30;
aidanwynn 47:2bc515eeaa18 113 downlink_msg = 0x04;
aidanwynn 47:2bc515eeaa18 114 }
aidanwynn 47:2bc515eeaa18 115 else if(rxData.compare("05") == 0){
aidanwynn 47:2bc515eeaa18 116 logInfo("Command received: [05]");
aidanwynn 47:2bc515eeaa18 117 logInfo("Setting uplinkInterval to 60mins");
aidanwynn 47:2bc515eeaa18 118 uplinkInterval = 60;
aidanwynn 47:2bc515eeaa18 119 downlink_msg = 0x05;
aidanwynn 47:2bc515eeaa18 120 }
aidanwynn 47:2bc515eeaa18 121 else if(rxData.compare("06") == 0){
aidanwynn 47:2bc515eeaa18 122 logInfo("Command received: [06]");
aidanwynn 47:2bc515eeaa18 123 logInfo("Setting uplinkInterval to 1min (for debugging)");
aidanwynn 47:2bc515eeaa18 124 uplinkInterval = 1;
aidanwynn 47:2bc515eeaa18 125 downlink_msg = 0x06;
aidanwynn 47:2bc515eeaa18 126 }
aidanwynn 47:2bc515eeaa18 127 else if(rxData.compare("07") == 0){
aidanwynn 47:2bc515eeaa18 128 logInfo("Command received: [07]");
aidanwynn 47:2bc515eeaa18 129 logInfo("Setting ACK to: 0 (off)");
aidanwynn 47:2bc515eeaa18 130 if (dot->setAck(0) != mDot::MDOT_OK) {
aidanwynn 47:2bc515eeaa18 131 logError("failed to set acks to %u", 0);
aidanwynn 47:2bc515eeaa18 132 }
aidanwynn 47:2bc515eeaa18 133 }
aidanwynn 47:2bc515eeaa18 134 else if(rxData.compare("08") == 0){
aidanwynn 47:2bc515eeaa18 135 logInfo("Command received: [08]");
aidanwynn 47:2bc515eeaa18 136 logInfo("Setting ACK to: 1");
aidanwynn 47:2bc515eeaa18 137 if (dot->setAck(1) != mDot::MDOT_OK) {
aidanwynn 47:2bc515eeaa18 138 logError("failed to set acks to %u", 1);
aidanwynn 47:2bc515eeaa18 139 }
aidanwynn 47:2bc515eeaa18 140 }
aidanwynn 47:2bc515eeaa18 141 else if(rxData.compare("09") == 0){
aidanwynn 47:2bc515eeaa18 142 logInfo("Command received: [09]");
aidanwynn 47:2bc515eeaa18 143 logInfo("Setting ACK to: 3");
aidanwynn 47:2bc515eeaa18 144 if (dot->setAck(3) != mDot::MDOT_OK) {
aidanwynn 47:2bc515eeaa18 145 logError("failed to set acks to %u", 3);
aidanwynn 47:2bc515eeaa18 146 }
aidanwynn 47:2bc515eeaa18 147 }
aidanwynn 47:2bc515eeaa18 148 else if(rxData.compare("0a") == 0){
aidanwynn 47:2bc515eeaa18 149 logInfo("Command received: [0a]");
aidanwynn 47:2bc515eeaa18 150 logInfo("Setting ACK to: 5");
aidanwynn 47:2bc515eeaa18 151 if (dot->setAck(5) != mDot::MDOT_OK) {
aidanwynn 47:2bc515eeaa18 152 logError("failed to set acks to %u", 5);
aidanwynn 47:2bc515eeaa18 153 }
aidanwynn 47:2bc515eeaa18 154 }
aidanwynn 47:2bc515eeaa18 155 else if(rxData.compare("0b") == 0){
aidanwynn 47:2bc515eeaa18 156 logInfo("Command received: [0b]");
aidanwynn 47:2bc515eeaa18 157 logInfo("Setting ACK to: 8 (max)");
aidanwynn 47:2bc515eeaa18 158 if (dot->setAck(8) != mDot::MDOT_OK) {
aidanwynn 47:2bc515eeaa18 159 logError("failed to set acks to %u", 8);
aidanwynn 47:2bc515eeaa18 160 }
aidanwynn 47:2bc515eeaa18 161 }
aidanwynn 47:2bc515eeaa18 162 else{
aidanwynn 47:2bc515eeaa18 163 logInfo("Unknown downlink command received");
aidanwynn 47:2bc515eeaa18 164 downlink_msg = 0xFF;
aidanwynn 47:2bc515eeaa18 165 }
aidanwynn 47:2bc515eeaa18 166
aidanwynn 47:2bc515eeaa18 167 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
aidanwynn 47:2bc515eeaa18 168 //#endif
Mike Fiore 14:19fae4509473 169 }
Mike Fiore 14:19fae4509473 170 }
Mike Fiore 14:19fae4509473 171 }
Jason Reiss 41:67feacfab49c 172
Jason Reiss 41:67feacfab49c 173 #if ACTIVE_EXAMPLE == FOTA_EXAMPLE
Jason Reiss 41:67feacfab49c 174 virtual void ServerTime(uint32_t seconds, uint8_t sub_seconds) {
Jason Reiss 41:67feacfab49c 175 mDotEvent::ServerTime(seconds, sub_seconds);
Jason Reiss 41:67feacfab49c 176
Jason Reiss 41:67feacfab49c 177 Fota::getInstance()->setClockOffset(seconds);
Jason Reiss 41:67feacfab49c 178 }
Jason Reiss 41:67feacfab49c 179 #endif
Mike Fiore 14:19fae4509473 180 };
Mike Fiore 14:19fae4509473 181
Mike Fiore 14:19fae4509473 182 #endif
Mike Fiore 14:19fae4509473 183