firm newest
Dependencies: MTS-Serial libmDot-dev-mbed5-deprecated
CommandTerminal/Command.cpp@0:3c869a8cb8f8, 2018-10-18 (annotated)
- Committer:
- nguyenhoang9x5555
- Date:
- Thu Oct 18 04:18:48 2018 +0000
- Revision:
- 0:3c869a8cb8f8
DOT AT FIRMWARE 18102018
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 1 | #include "Command.h" |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 2 | #include <algorithm> |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 3 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 4 | const char Command::newline[] = "\r\n"; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 5 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 6 | Command::Command() : _name(""), _text(""), _desc(""), _usage("NONE") |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 7 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 8 | _queryable = false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 9 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 10 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 11 | Command::Command(const char* name, const char* text, const char* desc, const char* usage) : |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 12 | _name(name), _text(text), _desc(desc), _usage(usage) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 13 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 14 | _queryable = false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 15 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 16 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 17 | std::string Command::usage() const |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 18 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 19 | std::string usage(_text); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 20 | usage.append(": "); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 21 | usage.append(_usage); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 22 | return usage; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 23 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 24 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 25 | bool Command::queryable() const |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 26 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 27 | return _queryable; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 28 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 29 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 30 | void Command::readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 31 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 32 | // if input length is greater than expected byte output |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 33 | // there must be a delimiter included |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 34 | if (input.length() > len * 2) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 35 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 36 | std::vector < std::string > bytes; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 37 | if (input.find(" ") != std::string::npos) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 38 | bytes = mts::Text::split(input, " "); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 39 | else if (input.find(":") != std::string::npos) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 40 | bytes = mts::Text::split(input, ":"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 41 | else if (input.find("-") != std::string::npos) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 42 | bytes = mts::Text::split(input, "-"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 43 | else if (input.find(".") != std::string::npos) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 44 | bytes = mts::Text::split(input, "."); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 45 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 46 | if (bytes.size() != len) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 47 | return; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 48 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 49 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 50 | int temp; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 51 | // Read in the key components... |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 52 | for (size_t i = 0; i < len; i++) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 53 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 54 | sscanf(bytes[i].c_str(), "%02x", &temp); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 55 | out.push_back(temp); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 56 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 57 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 58 | else |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 59 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 60 | // no delims |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 61 | int temp; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 62 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 63 | // Read in the key components... |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 64 | for (size_t i = 0; i < len; i++) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 65 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 66 | if (i * 2 < input.size()) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 67 | { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 68 | sscanf(input.substr(i * 2).c_str(), "%02x", &temp); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 69 | out.push_back(temp); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 70 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 71 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 72 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 73 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 74 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 75 | bool Command::isHexString(const std::string& str, size_t bytes) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 76 | int numDelims = bytes - 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 77 | size_t minSize = bytes * 2; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 78 | size_t maxSize = minSize + numDelims; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 79 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 80 | if (str.size() == minSize) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 81 | return str.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 82 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 83 | else if (str.size() == maxSize) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 84 | if (str.find_first_of(":-.") == std::string::npos) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 85 | // no delim found |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 86 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 87 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 88 | if (str.find(":") != std::string::npos && std::count(str.begin(), str.end(), ':') != numDelims) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 89 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 90 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 91 | if (str.find(".") != std::string::npos && std::count(str.begin(), str.end(), '.') != numDelims) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 92 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 93 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 94 | if (str.find("-") != std::string::npos && std::count(str.begin(), str.end(), '-') != numDelims) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 95 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 96 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 97 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 98 | return str.find_first_not_of("0123456789abcdefABCDEF:-.") == std::string::npos; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 99 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 100 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 101 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 102 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 103 |