![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
AT terminal for the mDot using the USB debug port instead of the serial port.
Dependencies: MTS-Serial libmDot-mbed5
Fork of Dot-AT-Firmware by
CommandTerminal/CmdNetworkKey.cpp@18:f5d28d621bb0, 2018-03-02 (annotated)
- Committer:
- pferland
- Date:
- Fri Mar 02 20:01:38 2018 +0000
- Revision:
- 18:f5d28d621bb0
- Parent:
- 14:f9a77400b622
Added ability to force the AT terminal to use the USB debug port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:e52ae6584f1c | 1 | #include "CmdNetworkKey.h" |
Mike Fiore |
1:e52ae6584f1c | 2 | |
Mike Fiore |
14:f9a77400b622 | 3 | CmdNetworkKey::CmdNetworkKey() : |
Mike Fiore |
14:f9a77400b622 | 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))") |
Mike Fiore |
1:e52ae6584f1c | 5 | { |
Mike Fiore |
1:e52ae6584f1c | 6 | _queryable = true; |
Mike Fiore |
1:e52ae6584f1c | 7 | } |
Mike Fiore |
1:e52ae6584f1c | 8 | |
Mike Fiore |
1:e52ae6584f1c | 9 | uint32_t CmdNetworkKey::action(std::vector<std::string> args) |
Mike Fiore |
1:e52ae6584f1c | 10 | { |
Mike Fiore |
1:e52ae6584f1c | 11 | if (args.size() == 1) |
Mike Fiore |
1:e52ae6584f1c | 12 | { |
Mike Fiore |
14:f9a77400b622 | 13 | CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(CommandTerminal::Dot()->getNetworkKey(), ".").c_str()); |
Mike Fiore |
14:f9a77400b622 | 14 | if (!CommandTerminal::Dot()->getNetworkPassphrase().empty()) |
Mike Fiore |
14:f9a77400b622 | 15 | CommandTerminal::Serial()->writef("Passphrase: '%s'\r\n", CommandTerminal::Dot()->getNetworkPassphrase().c_str()); |
Mike Fiore |
1:e52ae6584f1c | 16 | } |
Mike Fiore |
1:e52ae6584f1c | 17 | else if (args.size() == 3) |
Mike Fiore |
1:e52ae6584f1c | 18 | { |
Mike Fiore |
1:e52ae6584f1c | 19 | if (args[1].find("1") == 0 && args[1].size() == 1) |
Mike Fiore |
1:e52ae6584f1c | 20 | { |
Mike Fiore |
1:e52ae6584f1c | 21 | std::string text; |
Mike Fiore |
1:e52ae6584f1c | 22 | if (args.size() > 3) |
Mike Fiore |
1:e52ae6584f1c | 23 | { |
Mike Fiore |
1:e52ae6584f1c | 24 | // passphrase was split on commas |
Mike Fiore |
1:e52ae6584f1c | 25 | for (size_t i = 2; i < args.size(); i++) |
Mike Fiore |
1:e52ae6584f1c | 26 | { |
Mike Fiore |
1:e52ae6584f1c | 27 | text.append(args[i]); |
Mike Fiore |
1:e52ae6584f1c | 28 | if (i < args.size() - 1) |
Mike Fiore |
1:e52ae6584f1c | 29 | text.append(","); |
Mike Fiore |
1:e52ae6584f1c | 30 | } |
Mike Fiore |
1:e52ae6584f1c | 31 | } |
Mike Fiore |
1:e52ae6584f1c | 32 | else |
Mike Fiore |
1:e52ae6584f1c | 33 | { |
Mike Fiore |
1:e52ae6584f1c | 34 | text = args[2]; |
Mike Fiore |
1:e52ae6584f1c | 35 | } |
Mike Fiore |
1:e52ae6584f1c | 36 | |
Mike Fiore |
14:f9a77400b622 | 37 | if (CommandTerminal::Dot()->setNetworkPassphrase(text) == mDot::MDOT_OK) |
Mike Fiore |
1:e52ae6584f1c | 38 | { |
Mike Fiore |
14:f9a77400b622 | 39 | CommandTerminal::Serial()->writef("Set Network Passphrase: "); |
Mike Fiore |
14:f9a77400b622 | 40 | CommandTerminal::Serial()->writef("%s\r\n", text.c_str()); |
Mike Fiore |
1:e52ae6584f1c | 41 | } |
Mike Fiore |
1:e52ae6584f1c | 42 | else |
Mike Fiore |
1:e52ae6584f1c | 43 | { |
Mike Fiore |
14:f9a77400b622 | 44 | CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError());; |
Mike Fiore |
1:e52ae6584f1c | 45 | return 1; |
Mike Fiore |
1:e52ae6584f1c | 46 | } |
Mike Fiore |
1:e52ae6584f1c | 47 | } |
Mike Fiore |
1:e52ae6584f1c | 48 | else |
Mike Fiore |
1:e52ae6584f1c | 49 | { |
Mike Fiore |
1:e52ae6584f1c | 50 | std::vector<uint8_t> NewKey; |
Mike Fiore |
1:e52ae6584f1c | 51 | readByteArray(args[2], NewKey, KEY_LENGTH); |
Mike Fiore |
14:f9a77400b622 | 52 | if (CommandTerminal::Dot()->setNetworkKey(NewKey) == mDot::MDOT_OK) |
Mike Fiore |
1:e52ae6584f1c | 53 | { |
Mike Fiore |
14:f9a77400b622 | 54 | CommandTerminal::Serial()->writef("Set Network Key: "); |
Mike Fiore |
14:f9a77400b622 | 55 | CommandTerminal::Serial()->writef("%s\r\n", mts::Text::bin2hexString(NewKey, ".").c_str()); |
Mike Fiore |
1:e52ae6584f1c | 56 | } |
Mike Fiore |
1:e52ae6584f1c | 57 | else |
Mike Fiore |
1:e52ae6584f1c | 58 | { |
Mike Fiore |
14:f9a77400b622 | 59 | CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError());; |
Mike Fiore |
1:e52ae6584f1c | 60 | return 1; |
Mike Fiore |
1:e52ae6584f1c | 61 | } |
Mike Fiore |
1:e52ae6584f1c | 62 | } |
Mike Fiore |
1:e52ae6584f1c | 63 | } |
Mike Fiore |
1:e52ae6584f1c | 64 | |
Mike Fiore |
1:e52ae6584f1c | 65 | return 0; |
Mike Fiore |
1:e52ae6584f1c | 66 | } |
Mike Fiore |
1:e52ae6584f1c | 67 | |
Mike Fiore |
1:e52ae6584f1c | 68 | bool CmdNetworkKey::verify(std::vector<std::string> args) |
Mike Fiore |
1:e52ae6584f1c | 69 | { |
Mike Fiore |
1:e52ae6584f1c | 70 | if (args.size() == 1) |
Mike Fiore |
1:e52ae6584f1c | 71 | return true; |
Mike Fiore |
1:e52ae6584f1c | 72 | |
Mike Fiore |
1:e52ae6584f1c | 73 | if (args.size() == 3) { |
Mike Fiore |
1:e52ae6584f1c | 74 | if (args[1] != "0" && args[1] != "1") { |
Mike Fiore |
14:f9a77400b622 | 75 | CommandTerminal::setErrorMessage("Invalid type, expects (0,1)"); |
Mike Fiore |
1:e52ae6584f1c | 76 | return false; |
Mike Fiore |
1:e52ae6584f1c | 77 | } |
Mike Fiore |
1:e52ae6584f1c | 78 | if (args[1] == "0" && !isHexString(args[2], 16)) { |
Mike Fiore |
14:f9a77400b622 | 79 | CommandTerminal::setErrorMessage("Invalid key, expects (hex:16)"); |
Mike Fiore |
1:e52ae6584f1c | 80 | return false; |
Mike Fiore |
1:e52ae6584f1c | 81 | } |
Mike Fiore |
1:e52ae6584f1c | 82 | |
Mike Fiore |
1:e52ae6584f1c | 83 | if (args[1] == "1" && args[2].size() < 8) { |
Mike Fiore |
14:f9a77400b622 | 84 | CommandTerminal::setErrorMessage("Invalid name, expects minimum 8 characters"); |
Mike Fiore |
1:e52ae6584f1c | 85 | return false; |
Mike Fiore |
1:e52ae6584f1c | 86 | } |
Mike Fiore |
1:e52ae6584f1c | 87 | |
Mike Fiore |
1:e52ae6584f1c | 88 | if (args[1] == "1" && (args[2].size() > 128 || args[2].size() < 8)) { |
Mike Fiore |
14:f9a77400b622 | 89 | CommandTerminal::setErrorMessage("Invalid passphrase, expects (string:8-128)"); |
Mike Fiore |
1:e52ae6584f1c | 90 | return false; |
Mike Fiore |
1:e52ae6584f1c | 91 | } |
Mike Fiore |
1:e52ae6584f1c | 92 | |
Mike Fiore |
1:e52ae6584f1c | 93 | return true; |
Mike Fiore |
1:e52ae6584f1c | 94 | } |
Mike Fiore |
1:e52ae6584f1c | 95 | |
Mike Fiore |
14:f9a77400b622 | 96 | CommandTerminal::setErrorMessage("Invalid arguments"); |
Mike Fiore |
1:e52ae6584f1c | 97 | return false; |
Mike Fiore |
1:e52ae6584f1c | 98 | } |
Mike Fiore |
1:e52ae6584f1c | 99 |