Factory firmware for the MultiTech Dotbox (MTDOT-BOX) and EVB (MTDOT-EVB) products.

Dependencies:   NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CmdNetworkAddress.cpp Source File

CmdNetworkAddress.cpp

00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "CmdNetworkAddress.h"
00020 #include <algorithm>
00021 
00022 CmdNetworkAddress::CmdNetworkAddress(mDot* dot, mts::MTSSerial& serial) :
00023         Command(dot, "Network Address", "AT+NA", "Network address (devAddr in LoraMac) (4 bytes)"), _serial(serial)
00024 {
00025     _help = std::string(text()) + ": " + std::string(desc());
00026     _usage = "(hex:4)";
00027     _queryable = true;
00028 }
00029 
00030 uint32_t CmdNetworkAddress::action(std::vector<std::string> args)
00031 {
00032     std::vector<uint8_t> addr;
00033 
00034     if (args.size() == 1)
00035     {
00036         if (_dot->getVerbose())
00037             _serial.writef("Network Address: ");
00038 
00039         addr = _dot->getNetworkAddress();
00040 
00041         _serial.writef("%02x:%02x:%02x:%02x\r\n", addr[0], addr[1], addr[2], addr[3]);
00042     }
00043     else if (args.size() == 2)
00044     {
00045         int32_t code;
00046         uint8_t temp;
00047         uint32_t step = 2;
00048 
00049         if (args[1].find(":") != std::string::npos || args[1].find(".") != std::string::npos || args[1].find("-") != std::string::npos)
00050             step = 3;
00051 
00052         // Convert the ASCII hex data to binary...
00053         for (size_t i = 0; i < args[1].size(); i += step) 
00054         {
00055             sscanf(&args[1][i], "%02x", &temp);
00056             addr.push_back(temp);
00057         }
00058 
00059         if ((code = _dot->setNetworkAddress(addr)) == mDot::MDOT_OK) {
00060             _serial.writef("Set Network Address: ");
00061             _serial.writef("%02x:%02x:%02x:%02x\r\n", addr[0], addr[1], addr[2], addr[3]);
00062         } else {
00063             std::string error = mDot::getReturnCodeString(code) + " - " + _dot->getLastError();
00064             setErrorMessage(error);
00065             return 1;
00066         }
00067     }
00068 
00069     return 0;
00070 }
00071 
00072 bool CmdNetworkAddress::verify(std::vector<std::string> args)
00073 {
00074     if (args.size() == 1) {
00075         return true;
00076     }
00077 
00078     if (args.size() == 2) {
00079         if (!isHexString(args[1], 4))
00080         {
00081             setErrorMessage("Invalid address, expects (hex:4)");
00082             return false;
00083         }
00084 
00085         return true;
00086     }
00087 
00088     setErrorMessage("Invalid arguments");
00089     return false;
00090 }