mDot AT-Firmware for testing
Dependencies: MTS-Serial libmDot-mbed5
Fork of Dot-AT-Firmware by
CommandTerminal/CmdNetworkAddress.cpp@1:e52ae6584f1c, 2015-06-25 (annotated)
- Committer:
- Mike Fiore
- Date:
- Thu Jun 25 10:23:41 2015 -0500
- Revision:
- 1:e52ae6584f1c
- Child:
- 9:ff62b20f7000
update debug, add command source files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:e52ae6584f1c | 1 | #include "CmdNetworkAddress.h" |
Mike Fiore |
1:e52ae6584f1c | 2 | #include <algorithm> |
Mike Fiore |
1:e52ae6584f1c | 3 | |
Mike Fiore |
1:e52ae6584f1c | 4 | CmdNetworkAddress::CmdNetworkAddress(mDot* dot, mts::MTSSerial& serial) : |
Mike Fiore |
1:e52ae6584f1c | 5 | Command(dot, "Network Address", "AT+NA", "Network address (devAddr in LoraMac) (4 bytes)"), _serial(serial) |
Mike Fiore |
1:e52ae6584f1c | 6 | { |
Mike Fiore |
1:e52ae6584f1c | 7 | _help = std::string(text()) + ": " + std::string(desc()); |
Mike Fiore |
1:e52ae6584f1c | 8 | _usage = "(hex:4)"; |
Mike Fiore |
1:e52ae6584f1c | 9 | _queryable = true; |
Mike Fiore |
1:e52ae6584f1c | 10 | } |
Mike Fiore |
1:e52ae6584f1c | 11 | |
Mike Fiore |
1:e52ae6584f1c | 12 | uint32_t CmdNetworkAddress::action(std::vector<std::string> args) |
Mike Fiore |
1:e52ae6584f1c | 13 | { |
Mike Fiore |
1:e52ae6584f1c | 14 | std::vector<uint8_t> addr; |
Mike Fiore |
1:e52ae6584f1c | 15 | |
Mike Fiore |
1:e52ae6584f1c | 16 | if (args.size() == 1) |
Mike Fiore |
1:e52ae6584f1c | 17 | { |
Mike Fiore |
1:e52ae6584f1c | 18 | if (_dot->getVerbose()) |
Mike Fiore |
1:e52ae6584f1c | 19 | _serial.writef("Network Address: "); |
Mike Fiore |
1:e52ae6584f1c | 20 | |
Mike Fiore |
1:e52ae6584f1c | 21 | addr = _dot->getNetworkAddress(); |
Mike Fiore |
1:e52ae6584f1c | 22 | |
Mike Fiore |
1:e52ae6584f1c | 23 | _serial.writef("%02x:%02x:%02x:%02x\r\n", addr[0], addr[1], addr[2], addr[3]); |
Mike Fiore |
1:e52ae6584f1c | 24 | } |
Mike Fiore |
1:e52ae6584f1c | 25 | else if (args.size() == 2) |
Mike Fiore |
1:e52ae6584f1c | 26 | { |
Mike Fiore |
1:e52ae6584f1c | 27 | int32_t code; |
Mike Fiore |
1:e52ae6584f1c | 28 | uint8_t temp; |
Mike Fiore |
1:e52ae6584f1c | 29 | uint32_t step = 2; |
Mike Fiore |
1:e52ae6584f1c | 30 | |
Mike Fiore |
1:e52ae6584f1c | 31 | if (args[1].find(":") != std::string::npos || args[1].find(".") != std::string::npos || args[1].find("-") != std::string::npos) |
Mike Fiore |
1:e52ae6584f1c | 32 | step = 3; |
Mike Fiore |
1:e52ae6584f1c | 33 | |
Mike Fiore |
1:e52ae6584f1c | 34 | // Convert the ASCII hex data to binary... |
Mike Fiore |
1:e52ae6584f1c | 35 | for (size_t i = 0; i < args[1].size(); i += step) |
Mike Fiore |
1:e52ae6584f1c | 36 | { |
Mike Fiore |
1:e52ae6584f1c | 37 | sscanf(&args[1][i], "%02x", &temp); |
Mike Fiore |
1:e52ae6584f1c | 38 | addr.push_back(temp); |
Mike Fiore |
1:e52ae6584f1c | 39 | } |
Mike Fiore |
1:e52ae6584f1c | 40 | |
Mike Fiore |
1:e52ae6584f1c | 41 | if ((code = _dot->setNetworkAddress(addr)) == mDot::MDOT_OK) { |
Mike Fiore |
1:e52ae6584f1c | 42 | _serial.writef("Set Network Address: "); |
Mike Fiore |
1:e52ae6584f1c | 43 | _serial.writef("%02x:%02x:%02x:%02x\r\n", addr[0], addr[1], addr[2], addr[3]); |
Mike Fiore |
1:e52ae6584f1c | 44 | } else { |
Mike Fiore |
1:e52ae6584f1c | 45 | std::string error = mDot::getReturnCodeString(code) + " - " + _dot->getLastError(); |
Mike Fiore |
1:e52ae6584f1c | 46 | setErrorMessage(error); |
Mike Fiore |
1:e52ae6584f1c | 47 | return 1; |
Mike Fiore |
1:e52ae6584f1c | 48 | } |
Mike Fiore |
1:e52ae6584f1c | 49 | } |
Mike Fiore |
1:e52ae6584f1c | 50 | |
Mike Fiore |
1:e52ae6584f1c | 51 | return 0; |
Mike Fiore |
1:e52ae6584f1c | 52 | } |
Mike Fiore |
1:e52ae6584f1c | 53 | |
Mike Fiore |
1:e52ae6584f1c | 54 | bool CmdNetworkAddress::verify(std::vector<std::string> args) |
Mike Fiore |
1:e52ae6584f1c | 55 | { |
Mike Fiore |
1:e52ae6584f1c | 56 | if (args.size() == 1) { |
Mike Fiore |
1:e52ae6584f1c | 57 | return true; |
Mike Fiore |
1:e52ae6584f1c | 58 | } |
Mike Fiore |
1:e52ae6584f1c | 59 | |
Mike Fiore |
1:e52ae6584f1c | 60 | if (args.size() == 2) { |
Mike Fiore |
1:e52ae6584f1c | 61 | if (!isHexString(args[1], 4)) |
Mike Fiore |
1:e52ae6584f1c | 62 | { |
Mike Fiore |
1:e52ae6584f1c | 63 | setErrorMessage("Invalid address, expects (hex:4)"); |
Mike Fiore |
1:e52ae6584f1c | 64 | return false; |
Mike Fiore |
1:e52ae6584f1c | 65 | } |
Mike Fiore |
1:e52ae6584f1c | 66 | |
Mike Fiore |
1:e52ae6584f1c | 67 | return true; |
Mike Fiore |
1:e52ae6584f1c | 68 | } |
Mike Fiore |
1:e52ae6584f1c | 69 | |
Mike Fiore |
1:e52ae6584f1c | 70 | setErrorMessage("Invalid arguments"); |
Mike Fiore |
1:e52ae6584f1c | 71 | return false; |
Mike Fiore |
1:e52ae6584f1c | 72 | } |