1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* Copyright (c) <2016> <MultiTech Systems>, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "Command.h"
#include <algorithm>
const char Command::newline[] = "\r\n";
Command::Command(mDot* dot) : _dot(dot)
{
_usage = "NONE";
_queryable = false;
}
Command::Command(mDot* dot, const char* name, const char* text, const char* desc) :
_dot(dot), _name(name), _text(text), _desc(desc)
{
_usage = "NONE";
_queryable = false;
}
std::string &Command::errorMessage()
{
return _errorMessage;
}
void Command::setErrorMessage(const char* message)
{
_errorMessage.assign(message);
}
void Command::setErrorMessage(const std::string& message)
{
_errorMessage.assign(message);
}
const std::string Command::usage() const
{
std::string usage(_text);
usage.append(": ");
usage.append(_usage);
return usage;
}
const bool Command::queryable()
{
return _queryable;
}
void Command::readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len)
{
// if input length is greater than expected byte output
// there must be a delimiter included
if (input.length() > len * 2)
{
std::vector < std::string > bytes;
if (input.find(" ") != std::string::npos)
bytes = mts::Text::split(input, " ");
else if (input.find(":") != std::string::npos)
bytes = mts::Text::split(input, ":");
else if (input.find("-") != std::string::npos)
bytes = mts::Text::split(input, "-");
else if (input.find(".") != std::string::npos)
bytes = mts::Text::split(input, ".");
if (bytes.size() != len) {
return;
}
uint8_t temp;
// Read in the key components...
for (size_t i = 0; i < len; i++)
{
sscanf(bytes[i].c_str(), "%02x", &temp);
out.push_back(temp);
}
}
else
{
// no delims
uint8_t temp;
// Read in the key components...
for (size_t i = 0; i < len; i++)
{
if (i * 2 < input.size())
{
sscanf(input.substr(i * 2).c_str(), "%02x", &temp);
out.push_back(temp);
}
}
}
}
bool Command::isHexString(const std::string& str, size_t bytes) {
size_t numDelims = bytes - 1;
size_t minSize = bytes * 2;
size_t maxSize = minSize + numDelims;
if (str.size() == minSize) {
return str.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos;
}
else if (str.size() == maxSize) {
if (str.find_first_of(":-.") == std::string::npos) {
// no delim found
return false;
}
if (str.find(":") != std::string::npos && std::count(str.begin(), str.end(), ':') != numDelims) {
return false;
}
if (str.find(".") != std::string::npos && std::count(str.begin(), str.end(), '.') != numDelims) {
return false;
}
if (str.find("-") != std::string::npos && std::count(str.begin(), str.end(), '-') != numDelims) {
return false;
}
return str.find_first_not_of("0123456789abcdefABCDEF:-.") == std::string::npos;
}
return false;
}
bool Command::verify(std::vector<std::string> args) {
if (args.size() == 1)
return true;
setErrorMessage("Invalid arguments");
return false;
}
|