Updated to libmDot 1.0.8-1

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

Fork of MTDOT-BOX-EVB-Factory-Firmware by MultiTech

Committer:
jreiss
Date:
Wed Jul 13 19:14:04 2016 +0000
Revision:
6:ab581c4260e7
Parent:
1:71125aa00e33
Use DR enum values instead of SF values

Who changed what in which revision?

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