Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MTS-Cellular by
Utils/CellUtils.h@11:4e428f689069, 2014-05-22 (annotated)
- Committer:
- Mike Fiore
- Date:
- Thu May 22 09:26:51 2014 -0500
- Revision:
- 11:4e428f689069
- Parent:
- 2:10e72dce251d
- Child:
- 18:fa0d8120f81f
add CellularFactory to automatically detect and create right type of Cellular class
add static sendCommand function to CellUtils so we can talk to radio before instantiating Cellular
add Cellular::Radio param to UIP constructor
Who changed what in which revision?
| User | Revision | Line number | New 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 { |
| Mike Fiore |
2:10e72dce251d | 18 | SUCCESS, ERROR, FAILURE, 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) { |
| Mike Fiore |
2:10e72dce251d | 30 | case SUCCESS: |
| Mike Fiore |
2:10e72dce251d | 31 | return "SUCCESS"; |
| Mike Fiore |
2:10e72dce251d | 32 | case ERROR: |
| Mike Fiore |
2:10e72dce251d | 33 | return "ERROR"; |
| Mike Fiore |
2:10e72dce251d | 34 | case NO_RESPONSE: |
| Mike Fiore |
2:10e72dce251d | 35 | return "NO_RESPONSE"; |
| Mike Fiore |
2:10e72dce251d | 36 | case 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 | } |
| Mike Fiore |
11:4e428f689069 | 87 | done = (result.size() == previous); |
| 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 |
