EEN for Chris

Fork of MTS-Cellular by MultiTech

Committer:
igalt
Date:
Tue Aug 30 20:17:37 2016 +0000
Revision:
84:7587adabd8a5
Parent:
30:1326b623919a
Separated to mudules

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 2:10e72dce251d 1 #ifndef CELLUTILS_H
Mike Fiore 2:10e72dce251d 2 #define CELLUTILS_H
Mike Fiore 2:10e72dce251d 3
Mike Fiore 11:4e428f689069 4 #include "MTSBufferedIO.h"
Mike Fiore 11:4e428f689069 5 #include "MTSLog.h"
Mike Fiore 11:4e428f689069 6
Mike Fiore 11:4e428f689069 7 using namespace mts;
Mike Fiore 11:4e428f689069 8
Mike Fiore 2:10e72dce251d 9 //Special Payload Character Constants (ASCII Values)
Mike Fiore 2:10e72dce251d 10 const char ETX = 0x03; //Ends socket connection
Mike Fiore 2:10e72dce251d 11 const char DLE = 0x10; //Escapes ETX and DLE within Payload
Mike Fiore 2:10e72dce251d 12 const char CR = 0x0D; //Carriage Return
Mike Fiore 2:10e72dce251d 13 const char NL = 0x0A; //Newline
Mike Fiore 2:10e72dce251d 14 const char CTRL_Z = 0x1A; //Control-Z
Mike Fiore 2:10e72dce251d 15
Mike Fiore 2:10e72dce251d 16 /// An enumeration for common responses.
Mike Fiore 2:10e72dce251d 17 enum Code {
mfiore 18:fa0d8120f81f 18 MTS_SUCCESS, MTS_ERROR, MTS_FAILURE, MTS_NO_RESPONSE
Mike Fiore 2:10e72dce251d 19 };
Mike Fiore 2:10e72dce251d 20
Mike Fiore 2:10e72dce251d 21 /** A static method for getting a string representation for the Code
Mike Fiore 2:10e72dce251d 22 * enumeration.
Mike Fiore 2:10e72dce251d 23 *
Mike Fiore 2:10e72dce251d 24 * @param code a Code enumeration.
Mike Fiore 2:10e72dce251d 25 * @returns the enumeration name as a string.
Mike Fiore 2:10e72dce251d 26 */
Mike Fiore 2:10e72dce251d 27 static std::string getCodeNames(Code code)
Mike Fiore 2:10e72dce251d 28 {
Mike Fiore 2:10e72dce251d 29 switch(code) {
mfiore 18:fa0d8120f81f 30 case MTS_SUCCESS:
Mike Fiore 2:10e72dce251d 31 return "SUCCESS";
mfiore 18:fa0d8120f81f 32 case MTS_ERROR:
Mike Fiore 2:10e72dce251d 33 return "ERROR";
mfiore 18:fa0d8120f81f 34 case MTS_NO_RESPONSE:
Mike Fiore 2:10e72dce251d 35 return "NO_RESPONSE";
mfiore 18:fa0d8120f81f 36 case MTS_FAILURE:
Mike Fiore 2:10e72dce251d 37 return "FAILURE";
Mike Fiore 2:10e72dce251d 38 default:
Mike Fiore 2:10e72dce251d 39 return "UNKNOWN ENUM";
Mike Fiore 2:10e72dce251d 40 }
Mike Fiore 2:10e72dce251d 41 }
Mike Fiore 2:10e72dce251d 42
Mike Fiore 11:4e428f689069 43 static string sendCommand(MTSBufferedIO* io, const std::string& command, unsigned int timeoutMillis, char esc = CR)
Mike Fiore 11:4e428f689069 44 {
Mike Fiore 11:4e428f689069 45 if(io == NULL) {
Mike Fiore 11:4e428f689069 46 logError("MTSBufferedIO not set");
Mike Fiore 11:4e428f689069 47 return "";
Mike Fiore 11:4e428f689069 48 }
Mike Fiore 11:4e428f689069 49 io->rxClear();
Mike Fiore 11:4e428f689069 50 io->txClear();
Mike Fiore 11:4e428f689069 51 std::string result;
Mike Fiore 11:4e428f689069 52
Mike Fiore 11:4e428f689069 53 //Attempt to write command
Mike Fiore 11:4e428f689069 54 if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
Mike Fiore 11:4e428f689069 55 //Failed to write command
Mike Fiore 11:4e428f689069 56 if (command != "AT" && command != "at") {
Mike Fiore 11:4e428f689069 57 logError("failed to send command to radio within %d milliseconds", timeoutMillis);
Mike Fiore 11:4e428f689069 58 }
Mike Fiore 11:4e428f689069 59 return "";
Mike Fiore 11:4e428f689069 60 }
Mike Fiore 11:4e428f689069 61
Mike Fiore 11:4e428f689069 62 //Send Escape Character
Mike Fiore 11:4e428f689069 63 if (esc != 0x00) {
Mike Fiore 11:4e428f689069 64 if(io->write(esc, timeoutMillis) != 1) {
Mike Fiore 11:4e428f689069 65 if (command != "AT" && command != "at") {
Mike Fiore 11:4e428f689069 66 logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis);
Mike Fiore 11:4e428f689069 67 }
Mike Fiore 11:4e428f689069 68 return "";
Mike Fiore 11:4e428f689069 69 }
Mike Fiore 11:4e428f689069 70 }
Mike Fiore 11:4e428f689069 71
Mike Fiore 11:4e428f689069 72 int timer = 0;
Mike Fiore 11:4e428f689069 73 size_t previous = 0;
Mike Fiore 11:4e428f689069 74 char tmp[256];
Mike Fiore 11:4e428f689069 75 tmp[255] = 0;
Mike Fiore 11:4e428f689069 76 bool done = false;
Mike Fiore 11:4e428f689069 77 do {
Mike Fiore 11:4e428f689069 78 wait(0.1);
Mike Fiore 11:4e428f689069 79 timer += 100;
Mike Fiore 11:4e428f689069 80
Mike Fiore 11:4e428f689069 81 previous = result.size();
Mike Fiore 11:4e428f689069 82 //Make a non-blocking read call by passing timeout of zero
Mike Fiore 11:4e428f689069 83 int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant)
Mike Fiore 11:4e428f689069 84 if(size > 0) {
Mike Fiore 11:4e428f689069 85 result.append(tmp, size);
Mike Fiore 11:4e428f689069 86 }
Vanger 30:1326b623919a 87 done = (result.size() == previous && previous > 0);
Mike Fiore 11:4e428f689069 88 if(timer >= timeoutMillis) {
Mike Fiore 11:4e428f689069 89 if (command != "AT" && command != "at") {
Mike Fiore 11:4e428f689069 90 logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis);
Mike Fiore 11:4e428f689069 91 }
Mike Fiore 11:4e428f689069 92 done = true;
Mike Fiore 11:4e428f689069 93 }
Mike Fiore 11:4e428f689069 94 } while (!done);
Mike Fiore 11:4e428f689069 95
Mike Fiore 11:4e428f689069 96 return result;
Mike Fiore 11:4e428f689069 97 }
Mike Fiore 11:4e428f689069 98
Mike Fiore 2:10e72dce251d 99 #endif