Factory firmware for the MultiTech Dotbox (MTDOT-BOX) and EVB (MTDOT-EVB) products.

Dependencies:   NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Command.cpp Source File

Command.cpp

00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "Command.h"
00020 #include <algorithm>
00021 
00022 const char Command::newline[] = "\r\n";
00023 
00024 Command::Command(mDot* dot) : _dot(dot)
00025 {
00026     _usage = "NONE";
00027     _queryable = false;
00028 }
00029 
00030 Command::Command(mDot* dot, const char* name, const char* text, const char* desc) :
00031     _dot(dot), _name(name), _text(text), _desc(desc)
00032 {
00033     _usage = "NONE";
00034     _queryable = false;
00035 }
00036 
00037 std::string &Command::errorMessage()
00038 {
00039     return _errorMessage;
00040 }
00041 
00042 void Command::setErrorMessage(const char* message)
00043 {
00044     _errorMessage.assign(message);
00045 }
00046 
00047 void Command::setErrorMessage(const std::string& message)
00048 {
00049     _errorMessage.assign(message);
00050 }
00051 
00052 const std::string Command::usage() const
00053 {
00054     std::string usage(_text);
00055     usage.append(": ");
00056     usage.append(_usage);
00057     return usage;
00058 }
00059 
00060 const bool Command::queryable()
00061 {
00062     return _queryable;
00063 }
00064 
00065 void Command::readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len)
00066 {
00067     // if input length is greater than expected byte output
00068     // there must be a delimiter included
00069     if (input.length() > len * 2)
00070     {
00071         std::vector < std::string > bytes;
00072         if (input.find(" ") != std::string::npos)
00073             bytes = mts::Text::split(input, " ");
00074         else if (input.find(":") != std::string::npos)
00075             bytes = mts::Text::split(input, ":");
00076         else if (input.find("-") != std::string::npos)
00077             bytes = mts::Text::split(input, "-");
00078         else if (input.find(".") != std::string::npos)
00079             bytes = mts::Text::split(input, ".");
00080 
00081         if (bytes.size() != len) {
00082             return;
00083         }
00084 
00085         uint8_t temp;
00086         // Read in the key components...
00087         for (size_t i = 0; i < len; i++)
00088         {
00089             sscanf(bytes[i].c_str(), "%02x", &temp);
00090             out.push_back(temp);
00091         }
00092     }
00093     else
00094     {
00095         // no delims
00096         uint8_t temp;
00097 
00098         // Read in the key components...
00099         for (size_t i = 0; i < len; i++)
00100         {
00101             if (i * 2 < input.size())
00102             {
00103                 sscanf(input.substr(i * 2).c_str(), "%02x", &temp);
00104                 out.push_back(temp);
00105             }
00106         }
00107     }
00108 }
00109 
00110 bool Command::isHexString(const std::string& str, size_t bytes) {
00111     size_t numDelims = bytes - 1;
00112     size_t minSize = bytes * 2;
00113     size_t maxSize = minSize + numDelims;
00114 
00115     if (str.size() == minSize) {
00116         return str.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos;
00117     }
00118     else if (str.size() == maxSize) {
00119         if (str.find_first_of(":-.") == std::string::npos) {
00120             // no delim found
00121             return false;
00122         }
00123         if (str.find(":") != std::string::npos && std::count(str.begin(), str.end(), ':') != numDelims) {
00124             return false;
00125         }
00126         if (str.find(".") != std::string::npos && std::count(str.begin(), str.end(), '.') != numDelims) {
00127             return false;
00128         }
00129         if (str.find("-") != std::string::npos && std::count(str.begin(), str.end(), '-') != numDelims) {
00130             return false;
00131         }
00132 
00133         return str.find_first_not_of("0123456789abcdefABCDEF:-.") == std::string::npos;
00134     }   
00135 
00136     return false;
00137 }
00138 
00139 bool Command::verify(std::vector<std::string> args) {
00140     if (args.size() == 1)
00141         return true;
00142 
00143     setErrorMessage("Invalid arguments");
00144     return false;
00145 }
00146