AT command firmware for MultiTech Dot devices.

Fork of mDot_AT_firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CmdNetworkKey.cpp Source File

CmdNetworkKey.cpp

00001 #include "CmdNetworkKey.h"
00002 
00003 CmdNetworkKey::CmdNetworkKey() :
00004 #if MTS_CMD_TERM_VERBOSE
00005     Command("Network Key", "AT+NK", "Configured network key/passphrase (App Key in LoRaWAN) ##  AT+NK=0/2,hex  AT+NK=1,passphrase (Net key = cmac(passphrase)) (16 bytes)", "(0/2,(hex:16)),(1,(string:128))")
00006 #else
00007     Command("AT+NK")
00008 #endif
00009 {
00010     _queryable = true;
00011 }
00012 
00013 uint32_t CmdNetworkKey::action(const std::vector<std::string>& args)
00014 {
00015     char arg1 = '\0';
00016     if ((args.size() >= 2) && (args[1].size() == 1)) {
00017         arg1 = args[1][0];
00018     }
00019 
00020     if ((args.size() == 1) || ((args.size() == 3) && (args[2].size() == 1) && (args[2][0] == '?'))) {
00021 #if defined(TARGET_MTS_MDOT_F411RE) || defined(TARGET_XDOT_L151CC) || defined(TARGET_XDOT_MAX32670)
00022         if (arg1 == '2') {
00023             CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(CommandTerminal::Dot()->getProtectedAppKey(), ".").c_str());
00024         } else
00025 #endif
00026         {
00027             CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(CommandTerminal::Dot()->getNetworkKey(), ".").c_str());
00028             if (!CommandTerminal::Dot()->getNetworkPassphrase().empty())
00029                 CommandTerminal::Serial()->writef("Passphrase: '%s'\r\n", CommandTerminal::Dot()->getNetworkPassphrase().c_str());
00030         }
00031     } else if (args.size() >= 3) {
00032         if (arg1 == '1')
00033         {
00034             std::string text = args[2];
00035             // passphrase was split on commas
00036             for (size_t i = 3; i < args.size(); i++)
00037             {
00038                 text.append(",");
00039                 text.append(args[i]);
00040             }
00041 
00042             if (CommandTerminal::Dot()->setNetworkPassphrase(text) == mDot::MDOT_OK)
00043             {
00044 #if MTS_CMD_TERM_VERBOSE
00045                 CommandTerminal::Serial()->writef("Set Network Passphrase: ");
00046                 CommandTerminal::Serial()->writef("%s\r\n", text.c_str());
00047 #endif
00048             }
00049             else
00050             {
00051                 return 1;
00052             }
00053         }
00054         else
00055         {
00056             std::vector<uint8_t> NewKey;
00057             readByteArray(args[2], NewKey, KEY_LENGTH);
00058             if (arg1 != '2') {
00059                 if (CommandTerminal::Dot()->setNetworkKey(NewKey) == mDot::MDOT_OK)
00060                 {
00061 #if MTS_CMD_TERM_VERBOSE
00062                     CommandTerminal::Serial()->writef("Set Network Key: ");
00063                     CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str());
00064 #endif
00065                 }
00066                 else
00067                 {
00068                     return 1;
00069                 }
00070             } else {
00071 #if defined(TARGET_MTS_MDOT_F411RE) || defined(TARGET_XDOT_L151CC) || defined(TARGET_XDOT_MAX32670)
00072 
00073                 if (CommandTerminal::Dot()->setProtectedAppKey(NewKey) == mDot::MDOT_OK)
00074                 {
00075 #if MTS_CMD_TERM_VERBOSE
00076                     CommandTerminal::Serial()->writef("Set Protected AppKey: ");
00077                     CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str());
00078 #endif
00079                 }
00080                 else
00081                 {
00082                     return 1;
00083                 }
00084 #endif
00085             }
00086         }
00087     }
00088 
00089     return 0;
00090 }
00091 
00092 bool CmdNetworkKey::verify(const std::vector<std::string>& args)
00093 {
00094     if (args.size() == 1)
00095         return true;
00096     if (args.size() > 1 && args[1].size() == 1) {
00097 
00098         char arg1 = args[1][0];
00099 
00100         if (args.size() == 2 && arg1 == '2')
00101             return true;
00102 
00103         if (args.size() >= 3) {
00104             if (arg1 != '0' && arg1 != '1' && arg1 != '2') {
00105 #if MTS_CMD_TERM_VERBOSE
00106             CommandTerminal::setErrorMessage("Invalid type, expects (0,1,2)");
00107 #endif
00108                 return false;
00109             }
00110 
00111             if ((arg1 == '0' || arg1 == '2') && args[2] != "?" && !isHexString(args[2], KEY_LENGTH)) {
00112 #if MTS_CMD_TERM_VERBOSE
00113             CommandTerminal::setErrorMessage("Invalid key, expects (hex:16)");
00114 #endif
00115                 return false;
00116             } else {
00117                 if (args[2].size() < 8) {
00118                     if (!((args[2].size() == 1) && (args[2][0] == '?'))) {
00119 #if MTS_CMD_TERM_VERBOSE
00120             CommandTerminal::setErrorMessage("Invalid name, expects minimum 8 characters");
00121 #endif
00122                         return false;
00123                     }
00124                 } else if (args[2].size() > 128) {
00125 #if MTS_CMD_TERM_VERBOSE
00126             CommandTerminal::setErrorMessage("Invalid passphrase, expects (string:8-128)");
00127 #endif
00128                     return false;
00129                 }
00130             }
00131 
00132             return true;
00133         }
00134     }
00135 
00136 #if MTS_CMD_TERM_VERBOSE
00137     CommandTerminal::setErrorMessage("Invalid arguments");
00138 #endif
00139     return false;
00140 }
00141