AT command firmware for MultiTech Dot devices.
Fork of mDot_AT_firmware by
Command.cpp
00001 #include "Command.h" 00002 #include <algorithm> 00003 00004 const char Command::newline[] = "\r\n"; 00005 00006 #if MTS_CMD_TERM_VERBOSE 00007 Command::Command() : _name(""), _text(""), _desc(""), _usage("NONE") 00008 #else 00009 Command::Command() : _text("") 00010 #endif 00011 { 00012 _queryable = false; 00013 } 00014 00015 #if MTS_CMD_TERM_VERBOSE 00016 Command::Command(const char* name, const char* text, const char* desc, const char* usage) : 00017 _name(name), _text(text), _desc(desc), _usage(usage) 00018 #else 00019 Command::Command(const char* text) : 00020 _text(text) 00021 #endif 00022 { 00023 _queryable = false; 00024 } 00025 00026 bool Command::verify(const std::vector<std::string>& args) { 00027 if (args.size() == 1) 00028 return true; 00029 00030 #if MTS_CMD_TERM_VERBOSE 00031 CommandTerminal::setErrorMessage("Invalid arguments"); 00032 #endif 00033 return false; 00034 } 00035 00036 00037 #if MTS_CMD_TERM_VERBOSE 00038 std::string Command::usage() const 00039 { 00040 std::string usage(_text); 00041 usage.append(": "); 00042 usage.append(_usage); 00043 return usage; 00044 } 00045 #endif 00046 00047 bool Command::queryable() const 00048 { 00049 return _queryable; 00050 } 00051 00052 void Command::readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len) 00053 { 00054 // if input length is greater than expected byte output 00055 // there must be a delimiter included 00056 if (input.length() > len * 2) 00057 { 00058 std::vector < std::string > bytes; 00059 00060 00061 char delims[] = " :-."; 00062 00063 for (size_t i = 0; i < sizeof(delims); ++i) { 00064 if (input.find(delims[i]) != std::string::npos) { 00065 bytes = mts::Text::split(input, delims[i]); 00066 break; 00067 } 00068 } 00069 00070 if (bytes.size() != len) { 00071 return; 00072 } 00073 00074 int temp; 00075 // Read in the key components... 00076 for (size_t i = 0; i < len; i++) 00077 { 00078 sscanf(bytes[i].c_str(), "%02x", &temp); 00079 out.push_back(temp); 00080 } 00081 } 00082 else 00083 { 00084 // no delims 00085 int temp; 00086 00087 // Read in the key components... 00088 for (size_t i = 0; i < len; i++) 00089 { 00090 if (i * 2 < input.size()) 00091 { 00092 sscanf(input.substr(i * 2).c_str(), "%02x", &temp); 00093 out.push_back(temp); 00094 } 00095 } 00096 } 00097 } 00098 00099 bool Command::isHexString(const std::string& str, size_t bytes) { 00100 int numDelims = bytes - 1; 00101 size_t minSize = bytes * 2; 00102 size_t maxSize = minSize + numDelims; 00103 00104 if (str.size() == minSize) { 00105 return str.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos; 00106 } 00107 else if (str.size() == maxSize) { 00108 char delims[] = ":-."; 00109 if (str.find_first_of(delims) == std::string::npos) { 00110 // no delim found 00111 return false; 00112 } 00113 00114 for (size_t i = 0; i < sizeof(delims); ++i) { 00115 if (str.find(delims[i]) != std::string::npos && std::count(str.begin(), str.end(), delims[i]) != numDelims) { 00116 return false; 00117 } 00118 } 00119 00120 return str.find_first_not_of("0123456789abcdefABCDEF:-.") == std::string::npos; 00121 } 00122 00123 return false; 00124 } 00125 00126 00127 int Command::strToDataRate(const std::string& str) 00128 { 00129 std::string dr = mts::Text::toUpper(str); 00130 00131 int datarate = -1; 00132 uint8_t i; 00133 00134 int res = sscanf(dr.c_str(), "%d", &datarate); 00135 00136 if (res == 0) { 00137 for (i = 0; i < 24; i++) { 00138 if (mDot::DataRateStr(i).find(dr) != std::string::npos) { 00139 datarate = i; 00140 break; 00141 } 00142 } 00143 } 00144 return datarate; 00145 } 00146 00147 00148 bool Command::printRecvData() 00149 { 00150 bool recvd = false; 00151 std::vector<uint8_t> rx_data; 00152 if (CommandTerminal::Dot()->recv(rx_data) == mDot::MDOT_OK) { 00153 if (!rx_data.empty()) { 00154 recvd = true; 00155 std::string formatted_data = CommandTerminal::formatPacketData(rx_data, CommandTerminal::Dot()->getRxOutput()); 00156 CommandTerminal::Serial()->write(formatted_data.c_str(), formatted_data.length()); 00157 CommandTerminal::Serial()->write(Command::newline, sizeof(Command::newline)); 00158 } 00159 } 00160 return recvd; 00161 }
Generated on Wed Dec 6 2023 19:34:05 by 1.7.2