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 CmdNetworkId.cpp Source File

CmdNetworkId.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 "CmdNetworkId.h"
00020 
00021 CmdNetworkId::CmdNetworkId(mDot* dot, mts::MTSSerial& serial) :
00022     Command(dot, "Network ID", "AT+NI", "Configured Network EUI/Name (App EUI in LoraMac) AT+NI=0,hex AT+NI=1,network_name  (Net ID = crc64(network_name)) (8 bytes)"),
00023             _serial(serial)
00024 {
00025     _help = std::string(text()) + ": " + std::string(desc());
00026     _usage = "(0,(hex:8)),(1,(string:128))";
00027     _queryable = true;
00028 }
00029 
00030 uint32_t CmdNetworkId::action(std::vector<std::string> args)
00031 {
00032     if (args.size() == 1)
00033     {
00034         if (_dot->getVerbose())
00035             _serial.writef("Network ID: ");
00036 
00037         _serial.writef("%s\r\n", mts::Text::bin2hexString(_dot->getNetworkId(), ":").c_str());
00038 
00039         if (!_dot->getNetworkName().empty())
00040             _serial.writef("Passphrase: '%s'\r\n", _dot->getNetworkName().c_str());
00041     }
00042     else
00043     {
00044         int32_t code;
00045 
00046         if (args[1].find("1") == 0 && args[1].size() == 1)
00047         {
00048             std::string text;
00049             if (args.size() > 3)
00050             {
00051                 // passphrase was split on commas
00052                 for (size_t i = 2; i < args.size(); i++)
00053                 {
00054                     text.append(args[i]);
00055                     if (i < args.size() - 1)
00056                         text.append(",");
00057                 }
00058             }
00059             else
00060             {
00061                 text = args[2];
00062             }
00063 
00064             if ((code = _dot->setNetworkName(text)) == mDot::MDOT_OK)
00065             {
00066                 _serial.writef("Set Network Name: ");
00067                 _serial.writef("%s\r\n", text.c_str());
00068             }
00069             else
00070             {
00071                 std::string error = mDot::getReturnCodeString(code) + " - " + _dot->getLastError();
00072                 setErrorMessage(error);
00073                 return 1;
00074             }
00075 
00076         }
00077         else
00078         {
00079             std::vector<uint8_t> NewKey;
00080             readByteArray(args[2], NewKey, EUI_LENGTH);
00081             if ((code = _dot->setNetworkId(NewKey)) == mDot::MDOT_OK)
00082             {
00083                 _serial.writef("Set Network ID: ");
00084                 _serial.writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str());
00085             }
00086             else
00087             {
00088                 std::string error = mDot::getReturnCodeString(code) + " - " + _dot->getLastError();
00089                 setErrorMessage(error);
00090                 return 1;
00091             }
00092         }
00093     }
00094 
00095     return 0;
00096 }
00097 
00098 bool CmdNetworkId::verify(std::vector<std::string> args)
00099 {
00100     if (args.size() == 1)
00101         return true;
00102 
00103     if (args.size() == 3) {
00104         if (args[1] != "0" && args[1] != "1") {
00105             setErrorMessage("Invalid type, expects (0,1)");
00106             return false;
00107         }
00108         if (args[1] == "0" && !isHexString(args[2], 8)) {
00109             setErrorMessage("Invalid ID, expects (hex:8");
00110             return false;
00111         }
00112 
00113         if (args[1] == "1" && args[2].size() < 8) {
00114             setErrorMessage("Invalid name, expects minimum 8 characters");
00115             return false;
00116         }
00117 
00118         if (args[1] == "1" && args[2].size() > 128) {
00119             setErrorMessage("Invalid name, expects (string:128)");
00120             return false;
00121         }
00122 
00123         return true;
00124     }
00125 
00126     setErrorMessage("Invalid arguments");
00127     return false;
00128 }