Peter Ferland / MTS-Cellular_lat1

Fork of MTS-Cellular by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellUtils.h Source File

CellUtils.h

00001 #ifndef CELLUTILS_H
00002 #define CELLUTILS_H
00003 
00004 #include "MTSBufferedIO.h"
00005 #include "MTSLog.h"
00006 
00007 using namespace mts;
00008 
00009 //Special Payload Character Constants (ASCII Values)
00010 const char ETX    = 0x03;   //Ends socket connection
00011 const char DLE    = 0x10;   //Escapes ETX and DLE within Payload
00012 const char CR     = 0x0D;   //Carriage Return
00013 const char NL     = 0x0A;   //Newline
00014 const char CTRL_Z = 0x1A;   //Control-Z
00015 
00016 /// An enumeration for common responses.
00017 enum Code {
00018     MTS_SUCCESS, MTS_ERROR, MTS_FAILURE, MTS_NO_RESPONSE
00019 };
00020 
00021 /** A static method for getting a string representation for the Code
00022 * enumeration.
00023 *
00024 * @param code a Code enumeration.
00025 * @returns the enumeration name as a string.
00026 */
00027 static std::string getCodeNames(Code code)
00028 {
00029     switch(code) {
00030         case MTS_SUCCESS:
00031             return "SUCCESS";
00032         case MTS_ERROR:
00033             return "ERROR";
00034         case MTS_NO_RESPONSE:
00035             return "NO_RESPONSE";
00036         case MTS_FAILURE:
00037             return "FAILURE";
00038         default:
00039             return "UNKNOWN ENUM";
00040     }
00041 }
00042 
00043 static string sendCommand(MTSBufferedIO* io, const std::string& command, unsigned int timeoutMillis, char esc = CR)
00044 {
00045     if(io == NULL) {
00046         logError("MTSBufferedIO not set");
00047         return "";
00048     }
00049     io->rxClear();
00050     io->txClear();
00051     std::string result;
00052 
00053     //Attempt to write command
00054     if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
00055         //Failed to write command
00056         if (command != "AT" && command != "at") {
00057             logError("failed to send command to radio within %d milliseconds", timeoutMillis);
00058         }
00059         return "";
00060     }
00061 
00062     //Send Escape Character
00063     if (esc != 0x00) {
00064         if(io->write(esc, timeoutMillis) != 1) {
00065             if (command != "AT" && command != "at") {
00066                 logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis);
00067             }
00068             return "";
00069         }
00070     }
00071 
00072     int timer = 0;
00073     size_t previous = 0;
00074     char tmp[256];
00075     tmp[255] = 0;
00076     bool done = false;
00077     do {
00078         wait(0.1);
00079         timer += 100;
00080 
00081         previous = result.size();
00082         //Make a non-blocking read call by passing timeout of zero
00083         int size = io->read(tmp,255,0);    //1 less than allocated (timeout is instant)
00084         if(size > 0) {
00085             result.append(tmp, size);
00086         }
00087         done =  (result.size() == previous && previous > 0);
00088         if(timer >= timeoutMillis) {
00089             if (command != "AT" && command != "at") {
00090                 logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis);
00091             }
00092             done = true;
00093         }
00094     } while (!done);
00095 
00096     return result;
00097 }
00098 
00099 #endif