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

CmdNetworkKey.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 "CmdNetworkKey.h"
00020 
00021 CmdNetworkKey::CmdNetworkKey(mDot* dot, mts::MTSSerial& serial) :
00022     Command(dot, "Network Key", "AT+NK", "Configured network key/passphrase (App Key in LoraMac) AT+NK=0,hex  AT+NK=1,passphrase (Net key = cmac(passphrase)) (16 bytes)"),
00023     _serial(serial)
00024 {
00025     _help = std::string(text()) + ": " + std::string(desc());
00026     _usage = "(0,(hex:16)),(1,(string:128))";
00027     _queryable = true;
00028 }
00029 
00030 uint32_t CmdNetworkKey::action(std::vector<std::string> args)
00031 {
00032     if (args.size() == 1)
00033     {
00034         if (_dot->getVerbose())
00035             _serial.writef("Network Key: ");
00036 
00037         _serial.writef("%s\r\n", mts::Text::bin2hexString(_dot->getNetworkKey(), ".").c_str());
00038         if (!_dot->getNetworkPassphrase().empty())
00039             _serial.writef("Passphrase: '%s'\r\n", _dot->getNetworkPassphrase().c_str());
00040 
00041     }
00042     else if (args.size() == 3)
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->setNetworkPassphrase(text)) == mDot::MDOT_OK)
00065             {
00066                 _serial.writef("Set Network Passphrase: ");
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         else
00077         {
00078             std::vector<uint8_t> NewKey;
00079             readByteArray(args[2], NewKey, KEY_LENGTH);
00080             if ((code = _dot->setNetworkKey(NewKey)) == mDot::MDOT_OK)
00081             {
00082                 _serial.writef("Set Network Key: ");
00083                 _serial.writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str());
00084             }
00085             else
00086             {
00087                 std::string error = mDot::getReturnCodeString(code) + " - " + _dot->getLastError();
00088                 setErrorMessage(error);
00089                 return 1;
00090             }
00091         }
00092     }
00093 
00094     return 0;
00095 }
00096 
00097 bool CmdNetworkKey::verify(std::vector<std::string> args)
00098 {
00099     if (args.size() == 1)
00100         return true;
00101 
00102     if (args.size() == 3) {
00103         if (args[1] != "0" && args[1] != "1") {
00104             setErrorMessage("Invalid type, expects (0,1)");
00105             return false;
00106         }
00107         if (args[1] == "0" && !isHexString(args[2], 16)) {
00108             setErrorMessage("Invalid key, expects (hex:16)");
00109             return false;
00110         }
00111 
00112         if (args[1] == "1" && args[2].size() < 8) {
00113             setErrorMessage("Invalid name, expects minimum 8 characters");
00114             return false;
00115         }
00116 
00117         if (args[1] == "1" && (args[2].size() > 128 || args[2].size() < 8)) {
00118             setErrorMessage("Invalid passphrase, expects (string:8-128)");
00119             return false;
00120         }
00121 
00122         return true;
00123     }
00124 
00125     setErrorMessage("Invalid arguments");
00126     return false;
00127 }
00128