MicroLabo / Mbed OS mbed-Dot-AT-Firmware

Dependencies:   MTS-Serial libmDot-mbed5

Fork of Dot-AT-Firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Command.cpp Source File

Command.cpp

00001 #include "Command.h"
00002 #include <algorithm>
00003 
00004 const char Command::newline[] = "\r\n";
00005 
00006 Command::Command() : _name(""), _text(""), _desc(""), _usage("NONE")
00007 {
00008     _queryable = false;
00009 }
00010 
00011 Command::Command(const char* name, const char* text, const char* desc, const char* usage) :
00012     _name(name), _text(text), _desc(desc), _usage(usage)
00013 {
00014     _queryable = false;
00015 }
00016 
00017 std::string Command::usage() const
00018 {
00019     std::string usage(_text);
00020     usage.append(": ");
00021     usage.append(_usage);
00022     return usage;
00023 }
00024 
00025 bool Command::queryable() const
00026 {
00027     return _queryable;
00028 }
00029 
00030 void Command::readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len)
00031 {
00032     // if input length is greater than expected byte output
00033     // there must be a delimiter included
00034     if (input.length() > len * 2)
00035     {
00036         std::vector < std::string > bytes;
00037         if (input.find(" ") != std::string::npos)
00038             bytes = mts::Text::split(input, " ");
00039         else if (input.find(":") != std::string::npos)
00040             bytes = mts::Text::split(input, ":");
00041         else if (input.find("-") != std::string::npos)
00042             bytes = mts::Text::split(input, "-");
00043         else if (input.find(".") != std::string::npos)
00044             bytes = mts::Text::split(input, ".");
00045 
00046         if (bytes.size() != len) {
00047             return;
00048         }
00049 
00050         int temp;
00051         // Read in the key components...
00052         for (size_t i = 0; i < len; i++)
00053         {
00054             sscanf(bytes[i].c_str(), "%02x", &temp);
00055             out.push_back(temp);
00056         }
00057     }
00058     else
00059     {
00060         // no delims
00061         int temp;
00062 
00063         // Read in the key components...
00064         for (size_t i = 0; i < len; i++)
00065         {
00066             if (i * 2 < input.size())
00067             {
00068                 sscanf(input.substr(i * 2).c_str(), "%02x", &temp);
00069                 out.push_back(temp);
00070             }
00071         }
00072     }
00073 }
00074 
00075 bool Command::isHexString(const std::string& str, size_t bytes) {
00076     int numDelims = bytes - 1;
00077     size_t minSize = bytes * 2;
00078     size_t maxSize = minSize + numDelims;
00079 
00080     if (str.size() == minSize) {
00081         return str.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos;
00082     }
00083     else if (str.size() == maxSize) {
00084         if (str.find_first_of(":-.") == std::string::npos) {
00085             // no delim found
00086             return false;
00087         }
00088         if (str.find(":") != std::string::npos && std::count(str.begin(), str.end(), ':') != numDelims) {
00089             return false;
00090         }
00091         if (str.find(".") != std::string::npos && std::count(str.begin(), str.end(), '.') != numDelims) {
00092             return false;
00093         }
00094         if (str.find("-") != std::string::npos && std::count(str.begin(), str.end(), '-') != numDelims) {
00095             return false;
00096         }
00097 
00098         return str.find_first_not_of("0123456789abcdefABCDEF:-.") == std::string::npos;
00099     }   
00100 
00101     return false;
00102 }
00103