Utility library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas thermostat_fan_demo--fan mtsas ... more

NOTE: MTS-Utils has moved to GitHub. This version will not be updated. For updates, go to the GitHub version.

Files at this revision

API Documentation at this revision

Comitter:
Mike Fiore
Date:
Wed Sep 09 11:59:59 2015 -0500
Parent:
13:4709c2dfcd11
Child:
15:ae12624eb600
Commit message:
update from git

Changed in this revision

MTSLog.h Show annotated file Show diff for this revision Revisions of this file
MTSText.cpp Show annotated file Show diff for this revision Revisions of this file
MTSText.h Show annotated file Show diff for this revision Revisions of this file
--- a/MTSLog.h	Mon Jun 08 15:30:12 2015 -0500
+++ b/MTSLog.h	Wed Sep 09 11:59:59 2015 -0500
@@ -3,30 +3,30 @@
 
 #ifdef MTS_DEBUG
 #define logFatal(format, ...) \
-    MTSLog::printMessage(MTSLog::FATAL_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::FATAL_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__)
 #define logError(format, ...) \
-    MTSLog::printMessage(MTSLog::ERROR_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::ERROR_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__)
 #define logWarning(format, ...) \
-    MTSLog::printMessage(MTSLog::WARNING_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::WARNING_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__)
 #define logInfo(format, ...) \
-    MTSLog::printMessage(MTSLog::INFO_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::INFO_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::INFO_LABEL, ##__VA_ARGS__)
 #define logDebug(format, ...) \
-    MTSLog::printMessage(MTSLog::DEBUG_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
 #define logTrace(format, ...) \
-    MTSLog::printMessage(MTSLog::TRACE_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::TRACE_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__)
 #else
 #define logFatal(format, ...) \
-    MTSLog::printMessage(MTSLog::FATAL_LEVEL, "[%s] " format "\r\n", MTSLog::FATAL_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "[%s] " format "\r\n", mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__)
 #define logError(format, ...) \
-    MTSLog::printMessage(MTSLog::ERROR_LEVEL, "[%s] " format "\r\n", MTSLog::ERROR_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "[%s] " format "\r\n", mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__)
 #define logWarning(format, ...) \
-    MTSLog::printMessage(MTSLog::WARNING_LEVEL, "[%s] " format "\r\n", MTSLog::WARNING_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "[%s] " format "\r\n", mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__)
 #define logInfo(format, ...) \
-    MTSLog::printMessage(MTSLog::INFO_LEVEL, "[%s] " format "\r\n", MTSLog::INFO_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "[%s] " format "\r\n", mts::MTSLog::INFO_LABEL, ##__VA_ARGS__)
 #define logDebug(format, ...) \
-    MTSLog::printMessage(MTSLog::DEBUG_LEVEL, "[%s] " format "\r\n", MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "[%s] " format "\r\n", mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
 #define logTrace(format, ...) \
-    MTSLog::printMessage(MTSLog::TRACE_LEVEL, "[%s] " format "\r\n", MTSLog::TRACE_LABEL, ##__VA_ARGS__)
+    mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "[%s] " format "\r\n", mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__)
 #endif
 
 namespace mts {
--- a/MTSText.cpp	Mon Jun 08 15:30:12 2015 -0500
+++ b/MTSText.cpp	Wed Sep 09 11:59:59 2015 -0500
@@ -4,6 +4,8 @@
 
 using namespace mts;
 
+std::string BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+
 std::string Text::getLine(const std::string& source, const size_t& start, size_t& cursor) {
     char delimiters[2];
     delimiters[0] = '\n';
@@ -92,6 +94,92 @@
 
     return str;
 }
+
+std::string Text::bin2base64(const uint8_t* data, size_t size) {
+    std::string out;
+    uint8_t b;
+
+    for (size_t i = 0; i < size; i+=3) {
+        b = (data[i] & 0xfc) >> 2;
+        out.push_back(BASE64CODE[b]);
+        b = (data[i] & 0x03) << 4;
+        if (i+1 < size) {
+            b |= (data[i+1] & 0xf0) >> 4;
+            out.push_back(BASE64CODE[b]);
+            b = (data[i + 1] & 0x0f) << 2;
+            if (i+2 < size) {
+                b |= (data[i+2] & 0xc0) >> 6;
+                out.push_back(BASE64CODE[b]);
+                b = data[i+2] & 0x3f;
+                out.push_back(BASE64CODE[b]);
+            } else {
+                out.push_back(BASE64CODE[b]);
+                out.append("=");
+            }
+        } else {
+            out.push_back(BASE64CODE[b]);
+            out.append("==");
+        }
+    }
+
+    return out;
+}
+
+std::string Text::bin2base64(const std::vector<uint8_t>& data) {
+    std::string out;
+    uint8_t b;
+
+    for (size_t i = 0; i < data.size(); i+=3) {
+        b = (data[i] & 0xfc) >> 2;
+        out.push_back(BASE64CODE[b]);
+        b = (data[i] & 0x03) << 4;
+        if (i+1 < data.size()) {
+            b |= (data[i+1] & 0xf0) >> 4;
+            out.push_back(BASE64CODE[b]);
+            b = (data[i + 1] & 0x0f) << 2;
+            if (i+2 < data.size()) {
+                b |= (data[i+2] & 0xc0) >> 6;
+                out.push_back(BASE64CODE[b]);
+                b = data[i+2] & 0x3f;
+                out.push_back(BASE64CODE[b]);
+            } else {
+                out.push_back(BASE64CODE[b]);
+                out.append("=");
+            }
+        } else {
+            out.push_back(BASE64CODE[b]);
+            out.append("==");
+        }
+    }
+
+    return out;
+}
+
+bool Text::base642bin(const std::string in, std::vector<uint8_t>& out) {
+
+    if (in.find_first_not_of(BASE64CODE) != std::string::npos) {
+        return false;
+    }
+
+    if (in.size() % 4 != 0) {
+        return false;
+    }
+
+    uint8_t a,b,c,d;
+
+    for (uint32_t i = 0; i < in.size(); i+=4) {
+        a = BASE64CODE.find(in[i]);
+        b = BASE64CODE.find(in[i+1]);
+        c = BASE64CODE.find(in[i+2]);
+        d = BASE64CODE.find(in[i+3]);
+        out.push_back(a << 2 | b >> 4);
+        out.push_back(b << 4 | c >> 2);
+        out.push_back(c << 6 | d);
+    }
+
+    return true;
+}
+
 
 void Text::ltrim(std::string& str, const char* args) {
     size_t startpos = str.find_first_not_of(args);
--- a/MTSText.h	Mon Jun 08 15:30:12 2015 -0500
+++ b/MTSText.h	Wed Sep 09 11:59:59 2015 -0500
@@ -53,11 +53,17 @@
 
     static std::string toUpper(const std::string str);
 
-    std::string float2String(double val, int precision);
+    static std::string float2String(double val, int precision);
 
     static std::string bin2hexString(const std::vector<uint8_t>& data, const char* delim = "", bool leadingZeros = false);
 
     static std::string bin2hexString(const uint8_t* data, const uint32_t len, const char* delim = "", bool leadingZeros = false);
+
+    static std::string bin2base64(const std::vector<uint8_t>& data);
+
+    static std::string bin2base64(const uint8_t* data, size_t size);
+
+    static bool base642bin(const std::string in, std::vector<uint8_t>& out);
 
     static void ltrim(std::string& str, const char* args);