AT command firmware for MultiTech Dot devices.

Fork of mDot_AT_firmware by MultiTech

Dot Library Not Included!

Because these example programs can be used for both mDot and xDot devices, the LoRa stack is not included. The libmDot library should be imported if building for mDot devices. The libxDot library should be imported if building for xDot devices. The AT firmware was last tested with mbed-os-5.4.7. Using a version past mbed-os-5.4.7 will cause the build to fail. The library used with the AT firmware has to match the mbed-os version.

Dot Library Version 3 Updates

Dot Library versions 3.x.x require a channel plan to be injected into the stack. The Dot-Examples and Dot-AT-Firmware do this by defining a macro called "CHANNEL_PLAN" that controls the channel plan that will be used in the examples. Available channel plans will be in the Dot Library repository in the plans folder.

Revision 20 and earlier of Dot-Examples and revision 15 and earlier of Dot-AT-Firmware should be used with Dot Library versions prior to 3.0.0.

Fota Library

Th Fota Library must be added to compile for mDot 3.1.0 with Fota support. Latest dev libraries and 3.2.0 release will include Fota with libmDot/libxDot.

AT Firmware Description

This AT Firmware is what ships on mDot and xDot devices. It provides an AT command interface for using the mDot or xDot for LoRa communication.

AT command documentation can be found on Multitech.com.

The firmware changelog can be found here. The library changelog can be found here.

Dot Libraries

Dot Library Limitations

The commit messages in libmDot-mbed5 and libmDot-dev-mbed5 specify the version of the Dot library the commit contains and the version of mbed-os it was compiled against. We recommend building your application with the version of mbed-os specified in the commit message of the version of the Dot library you're using. This will ensure that you don't run into any runtime issues caused by differences in the mbed-os versions.

Stable and development libraries are available for both mDot and xDot platforms. The library chosen must match the target platform. Compiling for the mDot platform with the xDot library or vice versa will not succeed.

mDot Library

Development library for mDot.

libmDot-dev

Stable library for mDot.

libmDot

xDot Library

Development library for xDot.

libxDot-dev

Stable library for xDot.

libxDot

Revision:
27:5fafd3b26ac3
Parent:
23:4f0a981c0349
Child:
28:c222ca8383f4
--- a/CommandTerminal/CommandTerminal.cpp	Fri May 03 18:49:18 2019 +0000
+++ b/CommandTerminal/CommandTerminal.cpp	Fri Sep 11 13:16:33 2020 -0500
@@ -1,5 +1,5 @@
 #include "ctype.h"
-#include "CmdFactory.h"
+#include "CommandFactory.h"
 #include "CommandTerminal.h"
 #include "Command.h"
 #include "MTSLog.h"
@@ -40,6 +40,8 @@
 
 static bool serial_data_mode = false;
 static bool peer_to_peer = false;
+static bool command_processing = false;
+static bool urc_enabled = false;
 
 std::string CommandTerminal::_errorMessage = "";
 
@@ -48,6 +50,14 @@
 static uint8_t f_data[252]; //max payload 242 plus 10 bytes for format
 static uint32_t _rxAddress = 0;
 
+#if defined(TARGET_MTS_MDOT_F411RE)
+DigitalOut _packet_rx_pin(D12);
+DigitalOut _join_status_pin(A2);
+#else
+DigitalOut _packet_rx_pin(GPIO1);
+DigitalOut _join_status_pin(GPIO0);
+#endif
+
 void CommandTerminal::setErrorMessage(const char* message) {
     _errorMessage.assign(message);
 }
@@ -95,8 +105,8 @@
     write(newline);
 
     Command *cmd = NULL;
-    for (int i = 0; i < CmdFactory::NUMBER_OF_CMDS; i++) {
-        cmd = CmdFactory::Create(static_cast<CmdFactory::CmdId_t>(i));
+    for (int i = 0; i < CommandFactory::NUMBER_OF_CMDS; i++) {
+        cmd = CommandFactory::Create(static_cast<CommandFactory::CmdId_t>(i));
         name = cmd->name();
         text = cmd->text();
         desc = cmd->desc();
@@ -204,7 +214,7 @@
 
             if (_dot->getIsIdle()) {
                 logDebug("Received serial data, sending out radio.");
-                if(_dot->getRxOutput() == mDot::EXTENDED) {
+                if(_dot->getRxOutput() == mDot::EXTENDED || _dot->getRxOutput() == mDot::EXTENDED_HEX) {
                     formatPacketSDSend(serial_buffer);
                 }
                 if (_dot->send(serial_buffer, false) != mDot::MDOT_OK) {
@@ -216,9 +226,9 @@
                 } else {
 
                     // wait for send to finish
-                    while (!_dot->getIsIdle() && !_serialp->escaped()) {
-                        osDelay(10);
-                    }
+                    do {
+                        osDelay(50);
+                    } while (!_dot->getIsIdle() && !_serialp->escaped());
 
                     // call recv to wait for any packet before sending again
                     if (!_serialp->escaped()) {
@@ -229,8 +239,8 @@
                     // Clear the serial buffer if send is success
                     serial_buffer.clear();
 
-                    // In class C mode pending data will be sent automatically without uplink
-                    if (_dot->getClass() == "A") {
+                    // In P2P and Class B & C mode pending data will be sent automatically without uplink
+                    if (peer_to_peer != true && _dot->getClass() == "A") {
                         if (_dot->getDataPending()) {
                             logDebug("Data is pending");
                             goto L_SEND;
@@ -421,6 +431,9 @@
 
     if (_dot->getJoinMode() == mDot::PEER_TO_PEER) {
         peer_to_peer = true;
+        Fota::getInstance()->enable(false);
+        CommandTerminal::Dot()->clearMacCommands();
+        CommandTerminal::Dot()->setTxDataRate(CommandTerminal::Dot()->getTxDataRate());
     } else {
         peer_to_peer = false;
     }
@@ -448,7 +461,9 @@
 
             ch = '\0';
 
-            wait(0.00001); // 10 us
+            if (_mode != mDot::SERIAL_MODE) {
+                ThisThread::sleep_for(10); // 10 ms
+            }
             _serial.escaped();
         }
 
@@ -534,6 +549,9 @@
             continue;
         }
 
+        _packet_rx_pin = 0;
+        command_processing = true;
+
         // parse command and args
         args.clear();
 
@@ -591,6 +609,28 @@
         } else if (args[0].find("AT&K3") == 0 && args[0].length() == 5) {
             _dot->setFlowControl(true);
             write(done);
+        } else if (args[0].find("AT+URC") == 0 && args[0].length() == 6) {
+            if (args.size() >= 2 && (args[1] != "?" && args[1] != "0" && args[1] != "1")) {
+                write("Invalid argument\r\n");
+                write(error);
+            } else if (args.size() == 2) {
+                if (args.size() > 1 && args[1] == "?") {
+                    write("(0:disable,1:enable)\r\n");
+                } else {
+                    urc_enabled = (args[1] == "1");
+                }
+            } else {
+                writef("%d\r\n", urc_enabled);
+            }
+            write(done);
+        } else if (args[0].find("AT+LW") == 0) {
+            if (args.size() >= 2 && (args[1] != "?")) {
+                write("Invalid argument\r\n");
+                write(error);
+            } else {
+                writef("%s\r\n", _dot->getMACVersion());
+                write(done);
+            }
         } else if (args[0] == "AT+SD") {
             if (_dot->getNetworkJoinStatus()) {
                 logDebug("Enter Serial Mode");
@@ -604,7 +644,7 @@
                 write(error);
             }
         } else if (args[0] == "AT+SLEEP") {
-            if (args.size() > 2 && (args[1] != "?")) {
+            if (args.size() >= 2 && (args[1] != "?" && args[1] != "0" && args[1] != "1")) {
                 write("Invalid argument\r\n");
                 write(error);
             } else {
@@ -652,8 +692,8 @@
 
             // search for command
             Command *cmd = NULL;
-            for (int i = 0; i < CmdFactory::NUMBER_OF_CMDS; i++) {
-                cmd = CmdFactory::Create(static_cast<CmdFactory::CmdId_t>(i));
+            for (int i = 0; i < CommandFactory::NUMBER_OF_CMDS; i++) {
+                cmd = CommandFactory::Create(static_cast<CommandFactory::CmdId_t>(i));
 
                 // match CMD or CMD? syntax if command is queryable
                 if (lookfor == cmd->text() && (!query || (query && cmd->queryable()))) {
@@ -688,6 +728,10 @@
             }
         }
 
+        _join_status_pin = CommandTerminal::Dot()->getNetworkJoinStatus();
+        command_processing = false;
+
+
 #if defined(TARGET_MTS_MDOT_F411RE)
         if (history.size() == 0 || history.front() != command)
             history.push_front(command);
@@ -966,7 +1010,10 @@
             return std::string(data.begin(), data.end());
 
         case(mDot::EXTENDED):
-            return formatPacket(data);
+            return formatPacket(data, false);
+
+        case(mDot::EXTENDED_HEX):
+            return formatPacket(data, true);
 
         default:
             return "";
@@ -999,27 +1046,53 @@
 void CommandTerminal::wakeup(void) {
 }
 
-void CommandTerminal::RadioEvent::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, uint32_t address, bool dupRx) {
+void CommandTerminal::RadioEvent::RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int16_t snr, lora::DownlinkControl ctrl, uint8_t slot) {
+    mDotEvent::RxDone(payload, size, rssi, snr, ctrl, slot);
+    logDebug("RadioEvent - RxDone");
+}
+
+void CommandTerminal::RadioEvent::RxTimeout(uint8_t slot) {
+    mDotEvent::RxTimeout(slot);
+    _packet_rx_pin = 0;
+}
+
+void CommandTerminal::RadioEvent::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) {
     mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, dupRx);
+    _rxAddress = address;
+
     if(port == 200 || port == 201 || port == 202) {
         Fota::getInstance()->processCmd(payload, port, size);
+        if (CommandTerminal::Dot()->getRxOutput() < mDot::EXTENDED) {
+            return;
+        }
     }
+
+    _packet_rx_pin = 1;
+
     if (serial_data_mode && port != 0) {
-        logDebug("Rx %d bytes", size);
         if (size > 0) {
-            if(CommandTerminal::Dot()->getRxOutput() == mDot::EXTENDED) {
-                formatPacket(RxPayload, size);
-                _rxAddress = address;
+            while (!CommandTerminal::Serial()->writeable()) ;
+            if (CommandTerminal::Dot()->getRxOutput() >= mDot::EXTENDED) {
+                formatPacket(RxPayload, size, CommandTerminal::Dot()->getRxOutput()  == mDot::EXTENDED_HEX);
             } else {
                 CommandTerminal::Serial()->write((char*) RxPayload, RxPayloadSize);
             }
         }
         if (!CommandTerminal::Serial()->readable()
             && (_dot->getAckRequested()
-                || _dot->hasMacCommands())
-            && (_dot->getClass() == "C" || _dot->getSettings()->Session.Class == lora::CLASS_B)) {
+                || (peer_to_peer == false && _dot->hasMacCommands()))
+            && (peer_to_peer || _dot->getClass() == "C" || _dot->getSettings()->Session.Class == lora::CLASS_B)) {
             _sendAck = true;
         }
+    } else if (urc_enabled) {
+        if (!command_processing) {
+            while (!CommandTerminal::Serial()->writeable()) ;
+            if (CommandTerminal::Dot()->getRxOutput() >= mDot::EXTENDED) {
+                formatPacket(RxPayload, size, CommandTerminal::Dot()->getRxOutput() == mDot::EXTENDED_HEX);
+            } else {
+                CommandTerminal::Serial()->writef("RECV\r\n");
+            }
+        }
     }
 }
 
@@ -1031,7 +1104,7 @@
     _battery_level = battery_level;
 }
 
-void CommandTerminal::formatPacket(uint8_t* payload, uint16_t size) {
+void CommandTerminal::formatPacket(uint8_t* payload, uint16_t size, bool hex) {
     uint32_t fcnt = _dot->getDownLinkCounter();
 
     if(_dot->getAckRequested()) {
@@ -1053,11 +1126,16 @@
     for(int i = 0; i < size; i++)
         f_data[i+10] = payload[i];
 
-    CommandTerminal::Serial()->write((char*)f_data, size + 10);
+    if (hex) {
+        std::string data = "RECV " + mts::Text::bin2hexString(f_data, size + 10) + "\r\n";
+        CommandTerminal::Serial()->write(data.c_str(), data.size());
+    } else {
+        CommandTerminal::Serial()->write((char*)f_data, size + 10);
+    }
 }
 
 
-std::string CommandTerminal::formatPacket(std::vector<uint8_t> payload) {
+std::string CommandTerminal::formatPacket(std::vector<uint8_t> payload, bool hex) {
     uint32_t fcnt = _dot->getDownLinkCounter();
 
     if(_dot->getAckRequested()) {
@@ -1076,16 +1154,33 @@
     f_data[8] = (fcnt >> 24) & 0xFF;
     f_data[9] = _events->RxPort;
 
-    for(int i = 0; i < payload.size(); i++)
+    for(size_t i = 0; i < payload.size(); i++)
         f_data[i+10] = payload.at(i);
 
-    std::string ret((char*)f_data, payload.size() + 10);
-    return ret;
+    if (hex) {
+        return mts::Text::bin2hexString(f_data, payload.size() + 10);
+    } else {
+        return std::string((char*)f_data, payload.size() + 10);
+    }
+
 }
 
 
 
 void CommandTerminal::formatPacketSDSend(std::vector<uint8_t> &payload) {
+    if (_dot->getRxOutput() == mDot::EXTENDED_HEX) {
+        int temp;
+        std::vector<uint8_t> converted;
+
+        for (size_t i=0; i < payload.size(); i+=2) {
+            if (sscanf((char*)&payload[i], "%2x", &temp) == 1) {
+                converted.push_back((uint8_t)temp);
+            }
+        }
+
+        payload = converted;
+    }
+
     if(payload.size() >= 3) {
         _dot->setAppPort(payload[0]);
         _dot->setRepeat(0);
@@ -1102,6 +1197,7 @@
         }
         payload.erase(payload.begin(), payload.begin()+3);
     }
+
 }