firm newest

Dependencies:   MTS-Serial libmDot-dev-mbed5-deprecated

Committer:
nguyenhoang9x5555
Date:
Mon Oct 22 01:56:09 2018 +0000
Revision:
1:c1c6549b9333
Parent:
0:3c869a8cb8f8
hey

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nguyenhoang9x5555 0:3c869a8cb8f8 1 #include "CmdNetworkKey.h"
nguyenhoang9x5555 0:3c869a8cb8f8 2
nguyenhoang9x5555 0:3c869a8cb8f8 3 CmdNetworkKey::CmdNetworkKey() :
nguyenhoang9x5555 0:3c869a8cb8f8 4 Command("Network Key", "AT+NK", "Configured network key/passphrase (App Key in LoRaWAN) ## AT+NK=0,hex AT+NK=1,passphrase (Net key = cmac(passphrase)) (16 bytes)", "(0,(hex:16)),(1,(string:128))")
nguyenhoang9x5555 0:3c869a8cb8f8 5 {
nguyenhoang9x5555 0:3c869a8cb8f8 6 _queryable = true;
nguyenhoang9x5555 0:3c869a8cb8f8 7 }
nguyenhoang9x5555 0:3c869a8cb8f8 8
nguyenhoang9x5555 0:3c869a8cb8f8 9 uint32_t CmdNetworkKey::action(std::vector<std::string> args)
nguyenhoang9x5555 0:3c869a8cb8f8 10 {
nguyenhoang9x5555 0:3c869a8cb8f8 11 if (args.size() == 1)
nguyenhoang9x5555 0:3c869a8cb8f8 12 {
nguyenhoang9x5555 0:3c869a8cb8f8 13 CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(CommandTerminal::Dot()->getNetworkKey(), ".").c_str());
nguyenhoang9x5555 0:3c869a8cb8f8 14 if (!CommandTerminal::Dot()->getNetworkPassphrase().empty())
nguyenhoang9x5555 0:3c869a8cb8f8 15 CommandTerminal::Serial()->writef("Passphrase: '%s'\r\n", CommandTerminal::Dot()->getNetworkPassphrase().c_str());
nguyenhoang9x5555 0:3c869a8cb8f8 16 }
nguyenhoang9x5555 0:3c869a8cb8f8 17 else if (args.size() == 3)
nguyenhoang9x5555 0:3c869a8cb8f8 18 {
nguyenhoang9x5555 0:3c869a8cb8f8 19 if (args[1].find("1") == 0 && args[1].size() == 1)
nguyenhoang9x5555 0:3c869a8cb8f8 20 {
nguyenhoang9x5555 0:3c869a8cb8f8 21 std::string text;
nguyenhoang9x5555 0:3c869a8cb8f8 22 if (args.size() > 3)
nguyenhoang9x5555 0:3c869a8cb8f8 23 {
nguyenhoang9x5555 0:3c869a8cb8f8 24 // passphrase was split on commas
nguyenhoang9x5555 0:3c869a8cb8f8 25 for (size_t i = 2; i < args.size(); i++)
nguyenhoang9x5555 0:3c869a8cb8f8 26 {
nguyenhoang9x5555 0:3c869a8cb8f8 27 text.append(args[i]);
nguyenhoang9x5555 0:3c869a8cb8f8 28 if (i < args.size() - 1)
nguyenhoang9x5555 0:3c869a8cb8f8 29 text.append(",");
nguyenhoang9x5555 0:3c869a8cb8f8 30 }
nguyenhoang9x5555 0:3c869a8cb8f8 31 }
nguyenhoang9x5555 0:3c869a8cb8f8 32 else
nguyenhoang9x5555 0:3c869a8cb8f8 33 {
nguyenhoang9x5555 0:3c869a8cb8f8 34 text = args[2];
nguyenhoang9x5555 0:3c869a8cb8f8 35 }
nguyenhoang9x5555 0:3c869a8cb8f8 36
nguyenhoang9x5555 0:3c869a8cb8f8 37 if (CommandTerminal::Dot()->setNetworkPassphrase(text) == mDot::MDOT_OK)
nguyenhoang9x5555 0:3c869a8cb8f8 38 {
nguyenhoang9x5555 0:3c869a8cb8f8 39 CommandTerminal::Serial()->writef("Set Network Passphrase: ");
nguyenhoang9x5555 0:3c869a8cb8f8 40 CommandTerminal::Serial()->writef("%s\r\n", text.c_str());
nguyenhoang9x5555 0:3c869a8cb8f8 41 }
nguyenhoang9x5555 0:3c869a8cb8f8 42 else
nguyenhoang9x5555 0:3c869a8cb8f8 43 {
nguyenhoang9x5555 0:3c869a8cb8f8 44 CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError());;
nguyenhoang9x5555 0:3c869a8cb8f8 45 return 1;
nguyenhoang9x5555 0:3c869a8cb8f8 46 }
nguyenhoang9x5555 0:3c869a8cb8f8 47 }
nguyenhoang9x5555 0:3c869a8cb8f8 48 else
nguyenhoang9x5555 0:3c869a8cb8f8 49 {
nguyenhoang9x5555 0:3c869a8cb8f8 50 std::vector<uint8_t> NewKey;
nguyenhoang9x5555 0:3c869a8cb8f8 51 readByteArray(args[2], NewKey, KEY_LENGTH);
nguyenhoang9x5555 0:3c869a8cb8f8 52 if (CommandTerminal::Dot()->setNetworkKey(NewKey) == mDot::MDOT_OK)
nguyenhoang9x5555 0:3c869a8cb8f8 53 {
nguyenhoang9x5555 0:3c869a8cb8f8 54 CommandTerminal::Serial()->writef("Set Network Key: ");
nguyenhoang9x5555 0:3c869a8cb8f8 55 CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str());
nguyenhoang9x5555 0:3c869a8cb8f8 56 }
nguyenhoang9x5555 0:3c869a8cb8f8 57 else
nguyenhoang9x5555 0:3c869a8cb8f8 58 {
nguyenhoang9x5555 0:3c869a8cb8f8 59 CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError());;
nguyenhoang9x5555 0:3c869a8cb8f8 60 return 1;
nguyenhoang9x5555 0:3c869a8cb8f8 61 }
nguyenhoang9x5555 0:3c869a8cb8f8 62 }
nguyenhoang9x5555 0:3c869a8cb8f8 63 }
nguyenhoang9x5555 0:3c869a8cb8f8 64
nguyenhoang9x5555 0:3c869a8cb8f8 65 return 0;
nguyenhoang9x5555 0:3c869a8cb8f8 66 }
nguyenhoang9x5555 0:3c869a8cb8f8 67
nguyenhoang9x5555 0:3c869a8cb8f8 68 bool CmdNetworkKey::verify(std::vector<std::string> args)
nguyenhoang9x5555 0:3c869a8cb8f8 69 {
nguyenhoang9x5555 0:3c869a8cb8f8 70 if (args.size() == 1)
nguyenhoang9x5555 0:3c869a8cb8f8 71 return true;
nguyenhoang9x5555 0:3c869a8cb8f8 72
nguyenhoang9x5555 0:3c869a8cb8f8 73 if (args.size() == 3) {
nguyenhoang9x5555 0:3c869a8cb8f8 74 if (args[1] != "0" && args[1] != "1") {
nguyenhoang9x5555 0:3c869a8cb8f8 75 CommandTerminal::setErrorMessage("Invalid type, expects (0,1)");
nguyenhoang9x5555 0:3c869a8cb8f8 76 return false;
nguyenhoang9x5555 0:3c869a8cb8f8 77 }
nguyenhoang9x5555 0:3c869a8cb8f8 78 if (args[1] == "0" && !isHexString(args[2], 16)) {
nguyenhoang9x5555 0:3c869a8cb8f8 79 CommandTerminal::setErrorMessage("Invalid key, expects (hex:16)");
nguyenhoang9x5555 0:3c869a8cb8f8 80 return false;
nguyenhoang9x5555 0:3c869a8cb8f8 81 }
nguyenhoang9x5555 0:3c869a8cb8f8 82
nguyenhoang9x5555 0:3c869a8cb8f8 83 if (args[1] == "1" && args[2].size() < 8) {
nguyenhoang9x5555 0:3c869a8cb8f8 84 CommandTerminal::setErrorMessage("Invalid name, expects minimum 8 characters");
nguyenhoang9x5555 0:3c869a8cb8f8 85 return false;
nguyenhoang9x5555 0:3c869a8cb8f8 86 }
nguyenhoang9x5555 0:3c869a8cb8f8 87
nguyenhoang9x5555 0:3c869a8cb8f8 88 if (args[1] == "1" && (args[2].size() > 128 || args[2].size() < 8)) {
nguyenhoang9x5555 0:3c869a8cb8f8 89 CommandTerminal::setErrorMessage("Invalid passphrase, expects (string:8-128)");
nguyenhoang9x5555 0:3c869a8cb8f8 90 return false;
nguyenhoang9x5555 0:3c869a8cb8f8 91 }
nguyenhoang9x5555 0:3c869a8cb8f8 92
nguyenhoang9x5555 0:3c869a8cb8f8 93 return true;
nguyenhoang9x5555 0:3c869a8cb8f8 94 }
nguyenhoang9x5555 0:3c869a8cb8f8 95
nguyenhoang9x5555 0:3c869a8cb8f8 96 CommandTerminal::setErrorMessage("Invalid arguments");
nguyenhoang9x5555 0:3c869a8cb8f8 97 return false;
nguyenhoang9x5555 0:3c869a8cb8f8 98 }
nguyenhoang9x5555 0:3c869a8cb8f8 99