A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
cellular/Cellular.h@71:82205735732b, 2013-12-26 (annotated)
- Committer:
- sgodinez
- Date:
- Thu Dec 26 16:12:49 2013 +0000
- Revision:
- 71:82205735732b
- Parent:
- 68:c490e4a51778
- Child:
- 82:5aa75004e553
Dropped Cellular::Code::CELL_ scope. Changed OK to SUCCESS. Moved common constants to Var.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jengbrecht | 0:563b70517320 | 1 | #ifndef CELLULAR_H |
jengbrecht | 0:563b70517320 | 2 | #define CELLULAR_H |
jengbrecht | 0:563b70517320 | 3 | |
sgodinez | 11:134435d8a2d5 | 4 | #include "IPStack.h" |
sgodinez | 11:134435d8a2d5 | 5 | #include "MTSBufferedIO.h" |
jengbrecht | 0:563b70517320 | 6 | #include "mbed.h" |
jengbrecht | 0:563b70517320 | 7 | #include <string> |
sgodinez | 4:6561c9128c6f | 8 | #include <vector> |
jengbrecht | 0:563b70517320 | 9 | |
jengbrecht | 56:e5e5351f14b3 | 10 | namespace mts |
jengbrecht | 56:e5e5351f14b3 | 11 | { |
mfiore | 39:6e94520a3217 | 12 | |
jengbrecht | 36:bb6b293c7495 | 13 | /** This is a class for communicating with a Multi-Tech Systems SocketModem iCell. The |
jengbrecht | 27:8e6188cbcfd4 | 14 | * SocketModem iCell is a family of carrier certified embedded cellular radio modules with |
jengbrecht | 27:8e6188cbcfd4 | 15 | * a common hardware footprint and AT command set for built in IP-stack functionality. |
jengbrecht | 36:bb6b293c7495 | 16 | * This class supports three main types of cellular radio interactions including: |
jengbrecht | 27:8e6188cbcfd4 | 17 | * configuration and status AT command processing, SMS processing, and TCP/UDP Socket |
jengbrecht | 36:bb6b293c7495 | 18 | * data connections. It should be noted that the radio can not process commands or |
jengbrecht | 27:8e6188cbcfd4 | 19 | * SMS messages while having an open data connection at the same time. The concurrent |
jengbrecht | 27:8e6188cbcfd4 | 20 | * capability may be added in a future release. This class also inherits from IPStack |
jengbrecht | 27:8e6188cbcfd4 | 21 | * providing a common set of commands for communication devices that have an onboard |
jengbrecht | 27:8e6188cbcfd4 | 22 | * IP Stack. It is also integrated with the standard Mbed Sockets package and can therefore |
jengbrecht | 27:8e6188cbcfd4 | 23 | * be used seamlessly with clients and services built on top of this interface already within |
jengbrecht | 56:e5e5351f14b3 | 24 | * the mbed library. |
jengbrecht | 36:bb6b293c7495 | 25 | * |
jengbrecht | 27:8e6188cbcfd4 | 26 | * All of the following examples use the Pin Names for the Freedom KL46Z board coupled with |
jengbrecht | 36:bb6b293c7495 | 27 | * the SocketModem Shield Arduino compatible board. Please chage Pin Names accordingly to |
jengbrecht | 27:8e6188cbcfd4 | 28 | * match your hardware configuration. It also assumes the use of RTS/CTS hardware handshaking |
jengbrecht | 27:8e6188cbcfd4 | 29 | * using GPIOs. To disable this you will need to change settings on the radio module and |
jengbrecht | 27:8e6188cbcfd4 | 30 | * and use the MTSSerial class instead of MTSSerialFlowControl. The default baud rate for the |
jengbrecht | 27:8e6188cbcfd4 | 31 | * cellular radio is 115200 bps. |
jengbrecht | 36:bb6b293c7495 | 32 | * |
jengbrecht | 27:8e6188cbcfd4 | 33 | * The following set of example code demonstrates how to send and receive configuration and |
jengbrecht | 62:83ccef1e94db | 34 | * status AT commands with the radio, create a data connection and test it: |
jengbrecht | 27:8e6188cbcfd4 | 35 | * @code |
jengbrecht | 27:8e6188cbcfd4 | 36 | * #include "mbed.h" |
jengbrecht | 27:8e6188cbcfd4 | 37 | * #include "Cellular.h" |
jengbrecht | 27:8e6188cbcfd4 | 38 | * #include "MTSSerialFlowControl.h" |
jengbrecht | 36:bb6b293c7495 | 39 | * |
jengbrecht | 62:83ccef1e94db | 40 | * using namespace mts; |
jengbrecht | 62:83ccef1e94db | 41 | * |
jengbrecht | 27:8e6188cbcfd4 | 42 | * main() { |
jengbrecht | 27:8e6188cbcfd4 | 43 | * //Wait for radio to boot up |
jengbrecht | 62:83ccef1e94db | 44 | * wait(30); |
jengbrecht | 36:bb6b293c7495 | 45 | * |
jengbrecht | 27:8e6188cbcfd4 | 46 | * //Setup serial interface to radio |
jengbrecht | 27:8e6188cbcfd4 | 47 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
jengbrecht | 27:8e6188cbcfd4 | 48 | * serial->baud(115200); |
jengbrecht | 27:8e6188cbcfd4 | 49 | * |
jengbrecht | 27:8e6188cbcfd4 | 50 | * //Setup Cellular class |
jengbrecht | 56:e5e5351f14b3 | 51 | * Cellular* cellular = Cellular::getInstance(); |
jengbrecht | 56:e5e5351f14b3 | 52 | * cellular->init(serial); |
jengbrecht | 36:bb6b293c7495 | 53 | * |
jengbrecht | 62:83ccef1e94db | 54 | * //Run status and configuration commands |
jengbrecht | 62:83ccef1e94db | 55 | * printf("Start Status and Configuration Commands\n\r"); |
jengbrecht | 62:83ccef1e94db | 56 | * printf("Test: %s\n\r", Cellular::getCodeNames(cellular->test()).c_str()); |
jengbrecht | 62:83ccef1e94db | 57 | * printf("Signal Strength: %d\n\r", cellular->getSignalStrength()); |
jengbrecht | 62:83ccef1e94db | 58 | * printf("Registration State: %s\n\r", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); |
jengbrecht | 62:83ccef1e94db | 59 | * printf("Send Basic Command (AT): %s\n\r", Cellular::getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); |
jengbrecht | 62:83ccef1e94db | 60 | * printf("Send Command (AT+CSQ): %s\n\r", cellular->sendCommand("AT+CSQ", 1000).c_str()); |
jengbrecht | 62:83ccef1e94db | 61 | * |
jengbrecht | 27:8e6188cbcfd4 | 62 | * //Start Test |
jengbrecht | 62:83ccef1e94db | 63 | * printf("Start Network Connectivity Test\n\r"); |
jengbrecht | 62:83ccef1e94db | 64 | * printf("Set APN: %s\n\r", cellular->getCodeNames(cellular->setApn("wap.cingular")).c_str()); //Use APN from service provider!!! |
jengbrecht | 36:bb6b293c7495 | 65 | * |
jengbrecht | 27:8e6188cbcfd4 | 66 | * //Setup a data connection |
jengbrecht | 27:8e6188cbcfd4 | 67 | * printf("Attempting to Connect\n\r"); |
jengbrecht | 27:8e6188cbcfd4 | 68 | * while (cellular->connect()) { |
jengbrecht | 27:8e6188cbcfd4 | 69 | * wait(1); |
jengbrecht | 27:8e6188cbcfd4 | 70 | * } |
jengbrecht | 62:83ccef1e94db | 71 | * printf("Connected to the Network!\n\r"); |
jengbrecht | 36:bb6b293c7495 | 72 | * |
jengbrecht | 27:8e6188cbcfd4 | 73 | * //Try pinging default server "8.8.8.8" |
jengbrecht | 27:8e6188cbcfd4 | 74 | * printf("Ping Valid: %s\n\r", cellular->ping() ? "true" : "false"); |
jengbrecht | 36:bb6b293c7495 | 75 | * |
jengbrecht | 27:8e6188cbcfd4 | 76 | * printf("End Program\n\r"); |
jengbrecht | 27:8e6188cbcfd4 | 77 | * } |
jengbrecht | 27:8e6188cbcfd4 | 78 | * @endcode |
jengbrecht | 36:bb6b293c7495 | 79 | * |
jengbrecht | 62:83ccef1e94db | 80 | * The following set of example code demonstrates how process SMS messages: |
jengbrecht | 62:83ccef1e94db | 81 | * @code |
jengbrecht | 62:83ccef1e94db | 82 | * #include "mbed.h" |
jengbrecht | 62:83ccef1e94db | 83 | * #include "Cellular.h" |
jengbrecht | 62:83ccef1e94db | 84 | * #include "MTSSerialFlowControl.h" |
jengbrecht | 62:83ccef1e94db | 85 | * |
jengbrecht | 62:83ccef1e94db | 86 | * using namespace mts; |
jengbrecht | 62:83ccef1e94db | 87 | * |
jengbrecht | 62:83ccef1e94db | 88 | * main() { |
jengbrecht | 62:83ccef1e94db | 89 | * //Wait for radio to boot up |
jengbrecht | 62:83ccef1e94db | 90 | * wait(30); |
jengbrecht | 62:83ccef1e94db | 91 | * |
jengbrecht | 62:83ccef1e94db | 92 | * //Setup serial interface to radio |
jengbrecht | 62:83ccef1e94db | 93 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
jengbrecht | 62:83ccef1e94db | 94 | * serial->baud(115200); |
jengbrecht | 62:83ccef1e94db | 95 | * |
jengbrecht | 62:83ccef1e94db | 96 | * //Setup Cellular class |
jengbrecht | 62:83ccef1e94db | 97 | * Cellular* cellular = Cellular::getInstance(); |
jengbrecht | 62:83ccef1e94db | 98 | * cellular->init(serial); |
jengbrecht | 62:83ccef1e94db | 99 | * |
jengbrecht | 62:83ccef1e94db | 100 | * //Start test |
jengbrecht | 62:83ccef1e94db | 101 | * printf("Test: %s\n\r", Cellular::getCodeNames(cellular->test()).c_str()); |
jengbrecht | 62:83ccef1e94db | 102 | * |
jengbrecht | 62:83ccef1e94db | 103 | * //Send SMS Message |
jengbrecht | 62:83ccef1e94db | 104 | * Cellular::Code code; |
jengbrecht | 62:83ccef1e94db | 105 | * std::string sMsg("Hello from Multi-Tech MBED!"); |
jengbrecht | 62:83ccef1e94db | 106 | * std::string sPhoneNum("16128675309"); //Put your phone number here or leave Jenny's... |
jengbrecht | 62:83ccef1e94db | 107 | * |
jengbrecht | 62:83ccef1e94db | 108 | * printf("Sending message [%s] to [%s]\r\n", sMsg.c_str(), sPhoneNum.c_str()); |
jengbrecht | 62:83ccef1e94db | 109 | * code = cellular->sendSMS(sPhoneNum, sMsg); |
jengbrecht | 62:83ccef1e94db | 110 | * |
jengbrecht | 62:83ccef1e94db | 111 | * if(code != Cellular::CELL_OK) { |
jengbrecht | 62:83ccef1e94db | 112 | * printf("Error during SMS send [%s]\r\n", Cellular::getCodeNames(code).c_str()); |
jengbrecht | 62:83ccef1e94db | 113 | * } else { |
jengbrecht | 62:83ccef1e94db | 114 | * printf("Success!\r\n"); |
jengbrecht | 62:83ccef1e94db | 115 | * } |
jengbrecht | 62:83ccef1e94db | 116 | * |
jengbrecht | 62:83ccef1e94db | 117 | * //Try and receive SMS messages |
jengbrecht | 62:83ccef1e94db | 118 | * //To determine your radio's phone number send yourself an SMS and check the received # |
jengbrecht | 62:83ccef1e94db | 119 | * printf("Checking Received Messages\r\n"); |
jengbrecht | 62:83ccef1e94db | 120 | * std::vector<Cellular::Sms> vSms = cellular->getReceivedSms(); |
jengbrecht | 62:83ccef1e94db | 121 | * printf("\r\n"); |
jengbrecht | 62:83ccef1e94db | 122 | * for(unsigned int i = 0; i < vSms.size(); i++) { |
jengbrecht | 62:83ccef1e94db | 123 | * printf("[%d][%s][%s][%s]\r\n", i, vSms[i].timestamp.c_str(), |
jengbrecht | 62:83ccef1e94db | 124 | * vSms[i].phoneNumber.c_str(), vSms[i].message.c_str()); |
jengbrecht | 62:83ccef1e94db | 125 | * } |
jengbrecht | 62:83ccef1e94db | 126 | * printf("End Program\n\r"); |
jengbrecht | 62:83ccef1e94db | 127 | * } |
jengbrecht | 62:83ccef1e94db | 128 | * @endcode |
jengbrecht | 62:83ccef1e94db | 129 | * |
jengbrecht | 27:8e6188cbcfd4 | 130 | * The following set of example code demonstrates how to setup and use a TCP socket connection |
jengbrecht | 27:8e6188cbcfd4 | 131 | * using the native commands from this class: |
jengbrecht | 27:8e6188cbcfd4 | 132 | * @code |
jengbrecht | 27:8e6188cbcfd4 | 133 | * #include "mbed.h" |
jengbrecht | 62:83ccef1e94db | 134 | * #include "Cellular.h" |
jengbrecht | 62:83ccef1e94db | 135 | * #include "MTSSerialFlowControl.h" |
jengbrecht | 62:83ccef1e94db | 136 | * |
jengbrecht | 62:83ccef1e94db | 137 | * using namespace mts; |
jengbrecht | 62:83ccef1e94db | 138 | * |
jengbrecht | 62:83ccef1e94db | 139 | * main() { |
jengbrecht | 62:83ccef1e94db | 140 | * //Define connection parameters |
jengbrecht | 62:83ccef1e94db | 141 | * Cellular::Code code; |
jengbrecht | 62:83ccef1e94db | 142 | * const int TEST_PORT = 7000; |
jengbrecht | 62:83ccef1e94db | 143 | * //const std::string TEST_SERVER("204.26.122.5"); |
jengbrecht | 62:83ccef1e94db | 144 | * const std::string TEST_SERVER("ws://echo.websocket.org"); |
jengbrecht | 62:83ccef1e94db | 145 | * |
jengbrecht | 62:83ccef1e94db | 146 | * //Wait for radio to boot up |
jengbrecht | 62:83ccef1e94db | 147 | * wait(30); |
jengbrecht | 62:83ccef1e94db | 148 | * |
jengbrecht | 62:83ccef1e94db | 149 | * //Setup serial interface to radio |
jengbrecht | 62:83ccef1e94db | 150 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
jengbrecht | 62:83ccef1e94db | 151 | * serial->baud(115200); |
jengbrecht | 62:83ccef1e94db | 152 | * |
jengbrecht | 62:83ccef1e94db | 153 | * //Setup Cellular class |
jengbrecht | 62:83ccef1e94db | 154 | * Cellular* cellular = Cellular::getInstance(); |
jengbrecht | 62:83ccef1e94db | 155 | * cellular->init(serial); |
jengbrecht | 62:83ccef1e94db | 156 | * |
jengbrecht | 62:83ccef1e94db | 157 | * //Start test |
jengbrecht | 62:83ccef1e94db | 158 | * printf("Test: %s\n\r", Cellular::getCodeNames(cellular->test()).c_str()); |
jengbrecht | 62:83ccef1e94db | 159 | * |
jengbrecht | 62:83ccef1e94db | 160 | * printf("Setting APN\r\n"); |
jengbrecht | 62:83ccef1e94db | 161 | * code = cellular->setApn("wap.cingular"); // Use from your service provider |
jengbrecht | 62:83ccef1e94db | 162 | * if(code == Cellular::CELL_OK) { |
jengbrecht | 62:83ccef1e94db | 163 | * printf("Success!\r\n"); |
jengbrecht | 62:83ccef1e94db | 164 | * } else { |
jengbrecht | 62:83ccef1e94db | 165 | * printf("Error during APN setup [%s]\r\n", cellular->getCodeNames(code).c_str()); |
jengbrecht | 62:83ccef1e94db | 166 | * } |
jengbrecht | 62:83ccef1e94db | 167 | * |
jengbrecht | 62:83ccef1e94db | 168 | * printf("Signal Strength: %d\n\r", cellular->getSignalStrength()); |
jengbrecht | 62:83ccef1e94db | 169 | * printf("Registration State: %s\n\r", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); |
jengbrecht | 62:83ccef1e94db | 170 | * |
jengbrecht | 62:83ccef1e94db | 171 | * //Setup a data connection |
jengbrecht | 62:83ccef1e94db | 172 | * printf("Attempting to Connect\n\r"); |
jengbrecht | 62:83ccef1e94db | 173 | * while (cellular->connect()) { |
jengbrecht | 62:83ccef1e94db | 174 | * wait(1); |
jengbrecht | 62:83ccef1e94db | 175 | * } |
jengbrecht | 62:83ccef1e94db | 176 | * printf("Connected to the Network!\n\r"); |
jengbrecht | 62:83ccef1e94db | 177 | * |
jengbrecht | 62:83ccef1e94db | 178 | * printf("Opening a TCP Socket\r\n"); |
jengbrecht | 62:83ccef1e94db | 179 | * if(cellular->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { |
jengbrecht | 62:83ccef1e94db | 180 | * printf("Success!\r\n"); |
jengbrecht | 62:83ccef1e94db | 181 | * } else { |
jengbrecht | 62:83ccef1e94db | 182 | * printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT); |
jengbrecht | 62:83ccef1e94db | 183 | * } |
jengbrecht | 62:83ccef1e94db | 184 | * |
jengbrecht | 62:83ccef1e94db | 185 | * printf("Writing to socket\r\n"); |
jengbrecht | 62:83ccef1e94db | 186 | * char data[] = "My Test Echo Message!!!"; |
jengbrecht | 62:83ccef1e94db | 187 | * int bytesWritten = cellular->write(data, sizeof(data), 10000); |
jengbrecht | 62:83ccef1e94db | 188 | * if(bytesWritten == sizeof(data)) { |
jengbrecht | 62:83ccef1e94db | 189 | * printf("Successfully wrote message!\r\n"); |
jengbrecht | 62:83ccef1e94db | 190 | * } else { |
jengbrecht | 62:83ccef1e94db | 191 | * printf("Failed to write message!\r\n"); |
jengbrecht | 62:83ccef1e94db | 192 | * } |
jengbrecht | 62:83ccef1e94db | 193 | * |
jengbrecht | 62:83ccef1e94db | 194 | * printf("Waiting 5 seconds\r\n"); |
jengbrecht | 62:83ccef1e94db | 195 | * wait(5); |
jengbrecht | 62:83ccef1e94db | 196 | * |
jengbrecht | 62:83ccef1e94db | 197 | * printf("Reading from socket for 10 seconds\r\n"); |
jengbrecht | 62:83ccef1e94db | 198 | * char response[sizeof(data)]; |
jengbrecht | 62:83ccef1e94db | 199 | * int bytesRead = cellular->read(response, sizeof(data), 10000); |
jengbrecht | 62:83ccef1e94db | 200 | * printf("READ: [%d] [%s]\r\n", bytesRead, response); |
jengbrecht | 62:83ccef1e94db | 201 | * |
jengbrecht | 62:83ccef1e94db | 202 | * printf("Closing socket\r\n"); |
jengbrecht | 62:83ccef1e94db | 203 | * cellular->close(); |
jengbrecht | 62:83ccef1e94db | 204 | * |
jengbrecht | 62:83ccef1e94db | 205 | * printf("Disconnecting\r\n"); |
jengbrecht | 62:83ccef1e94db | 206 | * cellular->disconnect(); |
jengbrecht | 62:83ccef1e94db | 207 | * |
jengbrecht | 62:83ccef1e94db | 208 | * printf("End Program\n\r"); |
jengbrecht | 62:83ccef1e94db | 209 | * } |
jengbrecht | 27:8e6188cbcfd4 | 210 | * @endcode |
jengbrecht | 27:8e6188cbcfd4 | 211 | * |
jengbrecht | 27:8e6188cbcfd4 | 212 | * The following set of example code demonstrates how to setup and use a TCP socket connection |
jengbrecht | 27:8e6188cbcfd4 | 213 | * using the Mbed compatible Socket interfaces. |
jengbrecht | 27:8e6188cbcfd4 | 214 | * @code |
jengbrecht | 27:8e6188cbcfd4 | 215 | * #include "mbed.h" |
jengbrecht | 27:8e6188cbcfd4 | 216 | * @endcode |
jengbrecht | 27:8e6188cbcfd4 | 217 | */ |
jengbrecht | 23:bc6f98a1eb22 | 218 | |
mfiore | 39:6e94520a3217 | 219 | class Cellular : virtual mts::IPStack |
jengbrecht | 0:563b70517320 | 220 | { |
jengbrecht | 0:563b70517320 | 221 | public: |
jengbrecht | 0:563b70517320 | 222 | |
jengbrecht | 27:8e6188cbcfd4 | 223 | /// An enumeration of radio registration states with a cell tower. |
jengbrecht | 0:563b70517320 | 224 | enum Registration { |
jengbrecht | 0:563b70517320 | 225 | NOT_REGISTERED, REGISTERED, SEARCHING, DENIED, UNKNOWN, ROAMING |
jengbrecht | 0:563b70517320 | 226 | }; |
jengbrecht | 0:563b70517320 | 227 | |
jengbrecht | 56:e5e5351f14b3 | 228 | /** This structure contains the data for an SMS message. |
jengbrecht | 56:e5e5351f14b3 | 229 | */ |
sgodinez | 4:6561c9128c6f | 230 | struct Sms { |
jengbrecht | 56:e5e5351f14b3 | 231 | /// Message Phone Number |
jengbrecht | 27:8e6188cbcfd4 | 232 | std::string phoneNumber; |
jengbrecht | 56:e5e5351f14b3 | 233 | /// Message Body |
jengbrecht | 27:8e6188cbcfd4 | 234 | std::string message; |
jengbrecht | 56:e5e5351f14b3 | 235 | /// Message Timestamp |
jengbrecht | 27:8e6188cbcfd4 | 236 | std::string timestamp; |
sgodinez | 4:6561c9128c6f | 237 | }; |
jengbrecht | 27:8e6188cbcfd4 | 238 | |
jengbrecht | 27:8e6188cbcfd4 | 239 | /** Destructs a Cellular object and frees all related resources. |
jengbrecht | 27:8e6188cbcfd4 | 240 | */ |
jengbrecht | 0:563b70517320 | 241 | ~Cellular(); |
jengbrecht | 27:8e6188cbcfd4 | 242 | |
jengbrecht | 56:e5e5351f14b3 | 243 | /** This static function is used to create or get a reference to a |
jengbrecht | 56:e5e5351f14b3 | 244 | * Cellular object. Cellular uses the singleton pattern, which means |
jengbrecht | 56:e5e5351f14b3 | 245 | * that you can only have one existing at a time. The first time you |
jengbrecht | 56:e5e5351f14b3 | 246 | * call getInstance this method creates a new uninitialized Cellular |
jengbrecht | 56:e5e5351f14b3 | 247 | * object and returns it. All future calls to this method will return |
jengbrecht | 56:e5e5351f14b3 | 248 | * a reference to the instance created during the first call. Note that |
jengbrecht | 56:e5e5351f14b3 | 249 | * you must call init on the returned instance before mnaking any other |
jengbrecht | 56:e5e5351f14b3 | 250 | * calls. If using this class'e bindings to any of the Socket package |
jengbrecht | 56:e5e5351f14b3 | 251 | * classes like TCPSocketConnection, you must call this method and the |
jengbrecht | 56:e5e5351f14b3 | 252 | * init method on the returned object first, before even creating the |
jengbrecht | 56:e5e5351f14b3 | 253 | * other objects. |
jengbrecht | 56:e5e5351f14b3 | 254 | * |
jengbrecht | 56:e5e5351f14b3 | 255 | * @returns a reference to the single Cellular obect that has been created. |
jengbrecht | 56:e5e5351f14b3 | 256 | */ |
sgodinez | 19:38794784e009 | 257 | static Cellular* getInstance(); |
jengbrecht | 56:e5e5351f14b3 | 258 | |
jengbrecht | 56:e5e5351f14b3 | 259 | /** This method initializes the object with the underlying radio |
jengbrecht | 56:e5e5351f14b3 | 260 | * interface to use. Note that this function MUST be called before |
jengbrecht | 56:e5e5351f14b3 | 261 | * any other calls will function correctly on an Cellular object. Also |
jengbrecht | 56:e5e5351f14b3 | 262 | * note that MTSBufferedIO is abstract, so you must use one of |
jengbrecht | 56:e5e5351f14b3 | 263 | * its inherited classes like MTSSerial or MTSSerialFlowControl. |
jengbrecht | 56:e5e5351f14b3 | 264 | * |
jengbrecht | 56:e5e5351f14b3 | 265 | * @param io the buffered io interface that is attached to the cellular |
jengbrecht | 56:e5e5351f14b3 | 266 | * radio. |
jengbrecht | 56:e5e5351f14b3 | 267 | * @returns true if the init was successful, otherwise false. |
jengbrecht | 56:e5e5351f14b3 | 268 | */ |
jengbrecht | 56:e5e5351f14b3 | 269 | bool init(MTSBufferedIO* io); |
jengbrecht | 0:563b70517320 | 270 | |
jengbrecht | 36:bb6b293c7495 | 271 | // Radio link related commands |
jengbrecht | 27:8e6188cbcfd4 | 272 | /** This method establishes a data connection on the cellular radio. |
jengbrecht | 27:8e6188cbcfd4 | 273 | * Note that before calling you must have an activated radio and if |
jengbrecht | 27:8e6188cbcfd4 | 274 | * using a SIM card set the APN using the setApn method. The APN can |
jengbrecht | 27:8e6188cbcfd4 | 275 | * be obtained from your cellular service provider. |
jengbrecht | 27:8e6188cbcfd4 | 276 | * |
jengbrecht | 27:8e6188cbcfd4 | 277 | * @returns true if the connection was successfully established, otherwise |
jengbrecht | 27:8e6188cbcfd4 | 278 | * false on an error. |
jengbrecht | 27:8e6188cbcfd4 | 279 | */ |
jengbrecht | 27:8e6188cbcfd4 | 280 | virtual bool connect(); |
jengbrecht | 27:8e6188cbcfd4 | 281 | |
jengbrecht | 27:8e6188cbcfd4 | 282 | /** This method is used to stop a previously established cellular data connection. |
jengbrecht | 27:8e6188cbcfd4 | 283 | */ |
sgodinez | 11:134435d8a2d5 | 284 | virtual void disconnect(); |
jengbrecht | 27:8e6188cbcfd4 | 285 | |
jengbrecht | 27:8e6188cbcfd4 | 286 | /** This method is used to check if the radio currently has a data connection |
jengbrecht | 27:8e6188cbcfd4 | 287 | * established. |
jengbrecht | 27:8e6188cbcfd4 | 288 | * |
jengbrecht | 27:8e6188cbcfd4 | 289 | * @returns true if a data connection exists, otherwise false. |
jengbrecht | 27:8e6188cbcfd4 | 290 | */ |
sgodinez | 11:134435d8a2d5 | 291 | virtual bool isConnected(); |
jengbrecht | 56:e5e5351f14b3 | 292 | |
jengbrecht | 36:bb6b293c7495 | 293 | // TCP and UDP Socket related commands |
jengbrecht | 36:bb6b293c7495 | 294 | // For behavior of the following methods refer to IPStack.h documentation |
sgodinez | 11:134435d8a2d5 | 295 | virtual bool bind(unsigned int port); |
sgodinez | 11:134435d8a2d5 | 296 | virtual bool open(const std::string& address, unsigned int port, Mode mode); |
sgodinez | 11:134435d8a2d5 | 297 | virtual bool isOpen(); |
sgodinez | 17:2d7c4ea7491b | 298 | virtual bool close(); |
sgodinez | 11:134435d8a2d5 | 299 | virtual int read(char* data, int max, int timeout = -1); |
sgodinez | 41:81d035fb0b6a | 300 | virtual int write(const char* data, int length, int timeout = -1); |
sgodinez | 11:134435d8a2d5 | 301 | virtual unsigned int readable(); |
sgodinez | 11:134435d8a2d5 | 302 | virtual unsigned int writeable(); |
jengbrecht | 27:8e6188cbcfd4 | 303 | |
sgodinez | 11:134435d8a2d5 | 304 | //Other |
mfiore | 29:7408b1bdad37 | 305 | /** A method to reset the Multi-Tech Socket Modem. This command brings down the |
mfiore | 29:7408b1bdad37 | 306 | * PPP link if it is up. After this function is called, at least 30 seconds should |
mfiore | 29:7408b1bdad37 | 307 | * be allowed for the cellular radio to come back up before any other Cellular |
mfiore | 29:7408b1bdad37 | 308 | * functions are called. |
mfiore | 29:7408b1bdad37 | 309 | */ |
mfiore | 29:7408b1bdad37 | 310 | /** this needs to be investigated. After we tell the radio to reset and wait 30 seconds, |
mfiore | 29:7408b1bdad37 | 311 | * we can't seem to get it to respond to even a simple signal strength query. |
mfiore | 29:7408b1bdad37 | 312 | */ |
jengbrecht | 27:8e6188cbcfd4 | 313 | virtual void reset(); |
sgodinez | 11:134435d8a2d5 | 314 | |
sgodinez | 11:134435d8a2d5 | 315 | //Cellular Radio Specific |
jengbrecht | 27:8e6188cbcfd4 | 316 | /** A method for sending a generic AT command to the radio. Note that you cannot |
jengbrecht | 27:8e6188cbcfd4 | 317 | * send commands and have a data connection at the same time. |
jengbrecht | 36:bb6b293c7495 | 318 | * |
jengbrecht | 27:8e6188cbcfd4 | 319 | * @param command the command to send to the radio without the escape character. |
jengbrecht | 27:8e6188cbcfd4 | 320 | * @param timeoutMillis the time in millis to wait for a response before returning. |
jengbrecht | 27:8e6188cbcfd4 | 321 | * @param esc escape character to add at the end of the command, defaults to |
sgodinez | 71:82205735732b | 322 | * carriage return (CR). Does not append any character if esc == 0. |
jengbrecht | 27:8e6188cbcfd4 | 323 | * @returns all data received from the radio after the command as a string. |
jengbrecht | 27:8e6188cbcfd4 | 324 | */ |
sgodinez | 71:82205735732b | 325 | std::string sendCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); |
jengbrecht | 36:bb6b293c7495 | 326 | |
jengbrecht | 36:bb6b293c7495 | 327 | /** A method for sending a basic AT command to the radio. A basic AT command is |
jengbrecht | 27:8e6188cbcfd4 | 328 | * one that simply has a response of either OK or ERROR without any other information. |
jengbrecht | 27:8e6188cbcfd4 | 329 | * Note that you cannot send commands and have a data connection at the same time. |
jengbrecht | 36:bb6b293c7495 | 330 | * |
jengbrecht | 27:8e6188cbcfd4 | 331 | * @param command the command to send to the radio without the escape character. |
jengbrecht | 27:8e6188cbcfd4 | 332 | * @param timeoutMillis the time in millis to wait for a response before returning. |
jengbrecht | 27:8e6188cbcfd4 | 333 | * @param esc escape character to add at the end of the command, defaults to |
jengbrecht | 27:8e6188cbcfd4 | 334 | * carriage return (CR). |
jengbrecht | 27:8e6188cbcfd4 | 335 | * @returns the standard AT Code enumeration. |
jengbrecht | 27:8e6188cbcfd4 | 336 | */ |
sgodinez | 71:82205735732b | 337 | Code sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); |
sgodinez | 11:134435d8a2d5 | 338 | |
jengbrecht | 23:bc6f98a1eb22 | 339 | /** A method for testing command access to the radio. This method sends the |
jengbrecht | 27:8e6188cbcfd4 | 340 | * command "AT" to the radio, which is a standard radio test to see if you |
jengbrecht | 23:bc6f98a1eb22 | 341 | * have command access to the radio. |
jengbrecht | 23:bc6f98a1eb22 | 342 | * |
jengbrecht | 23:bc6f98a1eb22 | 343 | * @returns the standard AT Code enumeration. |
jengbrecht | 23:bc6f98a1eb22 | 344 | */ |
sgodinez | 11:134435d8a2d5 | 345 | Code test(); |
jengbrecht | 27:8e6188cbcfd4 | 346 | |
jengbrecht | 23:bc6f98a1eb22 | 347 | /** A method for configuring command ehco capability on the radio. This command |
jengbrecht | 23:bc6f98a1eb22 | 348 | * sets whether sent characters are echoed back from the radio, in which case you |
jengbrecht | 23:bc6f98a1eb22 | 349 | * will receive back every command you send. |
jengbrecht | 23:bc6f98a1eb22 | 350 | * |
jengbrecht | 23:bc6f98a1eb22 | 351 | * @param state if true echo will be turned off, otherwise it will be turned on. |
jengbrecht | 23:bc6f98a1eb22 | 352 | * @returns the standard AT Code enumeration. |
jengbrecht | 23:bc6f98a1eb22 | 353 | */ |
mfiore | 29:7408b1bdad37 | 354 | Code echo(bool state); |
jengbrecht | 36:bb6b293c7495 | 355 | |
jengbrecht | 23:bc6f98a1eb22 | 356 | /** A method for getting the signal strength of the radio. This method allows you to |
jengbrecht | 23:bc6f98a1eb22 | 357 | * get a value that maps to signal strength in dBm. Here 0-1 is Poor, 2-9 is Marginal, |
jengbrecht | 23:bc6f98a1eb22 | 358 | * 10-14 is Ok, 15-19 is Good, and 20+ is Excellent. If you get a result of 99 the |
jengbrecht | 27:8e6188cbcfd4 | 359 | * signal strength is not known or not detectable. |
jengbrecht | 23:bc6f98a1eb22 | 360 | * |
jengbrecht | 23:bc6f98a1eb22 | 361 | * @returns an integer representing the signal strength. |
jengbrecht | 23:bc6f98a1eb22 | 362 | */ |
jengbrecht | 0:563b70517320 | 363 | int getSignalStrength(); |
jengbrecht | 36:bb6b293c7495 | 364 | |
jengbrecht | 27:8e6188cbcfd4 | 365 | /** This method is used to check the registration state of the radio with the cell tower. |
jengbrecht | 27:8e6188cbcfd4 | 366 | * If not appropriatley registered with the tower you cannot make a cellular connection. |
jengbrecht | 27:8e6188cbcfd4 | 367 | * |
jengbrecht | 27:8e6188cbcfd4 | 368 | * @returns the registration state as an enumeration type. |
jengbrecht | 27:8e6188cbcfd4 | 369 | */ |
sgodinez | 5:93e889a5abc6 | 370 | Registration getRegistration(); |
jengbrecht | 27:8e6188cbcfd4 | 371 | |
jengbrecht | 27:8e6188cbcfd4 | 372 | /** This method is used to set the radios APN if using a SIM card. Note that the APN |
jengbrecht | 27:8e6188cbcfd4 | 373 | * must be set correctly before you can make a data connection. The APN for your SIM |
jengbrecht | 27:8e6188cbcfd4 | 374 | * can be obtained by contacting your cellular service provider. |
jengbrecht | 27:8e6188cbcfd4 | 375 | * |
jengbrecht | 27:8e6188cbcfd4 | 376 | * @param the APN as a string. |
jengbrecht | 27:8e6188cbcfd4 | 377 | * @returns the standard AT Code enumeration. |
jengbrecht | 27:8e6188cbcfd4 | 378 | */ |
sgodinez | 11:134435d8a2d5 | 379 | Code setApn(const std::string& apn); |
jengbrecht | 27:8e6188cbcfd4 | 380 | |
jengbrecht | 27:8e6188cbcfd4 | 381 | /** This method is used to set the DNS which enables the use of URLs instead |
jengbrecht | 27:8e6188cbcfd4 | 382 | * of IP addresses when making a socket connection. |
jengbrecht | 27:8e6188cbcfd4 | 383 | * |
jengbrecht | 27:8e6188cbcfd4 | 384 | * @param the DNS server address as a string in form xxx.xxx.xxx.xxx. |
jengbrecht | 27:8e6188cbcfd4 | 385 | * @returns the standard AT Code enumeration. |
jengbrecht | 27:8e6188cbcfd4 | 386 | */ |
sgodinez | 41:81d035fb0b6a | 387 | Code setDns(const std::string& primary, const std::string& secondary = "0.0.0.0"); |
jengbrecht | 27:8e6188cbcfd4 | 388 | |
jengbrecht | 27:8e6188cbcfd4 | 389 | /** This method is used test network connectivity by pinging a server. |
jengbrecht | 27:8e6188cbcfd4 | 390 | * |
jengbrecht | 27:8e6188cbcfd4 | 391 | * @param address the address of the server in format xxx.xxx.xxx.xxx. |
jengbrecht | 27:8e6188cbcfd4 | 392 | * @returns true if the ping was successful, otherwise false. |
jengbrecht | 27:8e6188cbcfd4 | 393 | */ |
jengbrecht | 23:bc6f98a1eb22 | 394 | bool ping(const std::string& address = "8.8.8.8"); |
jengbrecht | 56:e5e5351f14b3 | 395 | |
jengbrecht | 56:e5e5351f14b3 | 396 | /** This method can be used to trade socket functionality for performance. |
jengbrecht | 56:e5e5351f14b3 | 397 | * In order to enable a socket connection to be closed by the client side programtically, |
jengbrecht | 56:e5e5351f14b3 | 398 | * this class must process all read and write data on the socket to guard the special |
jengbrecht | 56:e5e5351f14b3 | 399 | * escape character used to close an open socket connection. It is recommened that you |
jengbrecht | 56:e5e5351f14b3 | 400 | * use the default of true unless the overhead of these operations is too significant. |
jengbrecht | 56:e5e5351f14b3 | 401 | * |
jengbrecht | 56:e5e5351f14b3 | 402 | * @param enabled set to true if you want the socket closeable, otherwise false. The default |
jengbrecht | 56:e5e5351f14b3 | 403 | * is true. |
jengbrecht | 56:e5e5351f14b3 | 404 | * @returns the standard AT Code enumeration. |
jengbrecht | 56:e5e5351f14b3 | 405 | */ |
sgodinez | 17:2d7c4ea7491b | 406 | Code setSocketCloseable(bool enabled = true); //ETX closes socket (ETX and DLE in payload are escaped with DLE) |
jengbrecht | 27:8e6188cbcfd4 | 407 | |
jengbrecht | 27:8e6188cbcfd4 | 408 | /** This method is used to send an SMS message. Note that you cannot send an |
jengbrecht | 27:8e6188cbcfd4 | 409 | * SMS message and have a data connection open at the same time. |
jengbrecht | 27:8e6188cbcfd4 | 410 | * |
jengbrecht | 27:8e6188cbcfd4 | 411 | * @param phoneNumber the phone number to send the message to as a string. |
jengbrecht | 27:8e6188cbcfd4 | 412 | * @param message the text message to be sent. |
jengbrecht | 27:8e6188cbcfd4 | 413 | * @returns the standard AT Code enumeration. |
jengbrecht | 27:8e6188cbcfd4 | 414 | */ |
jengbrecht | 27:8e6188cbcfd4 | 415 | Code sendSMS(const std::string& phoneNumber, const std::string& message); |
jengbrecht | 36:bb6b293c7495 | 416 | |
jengbrecht | 27:8e6188cbcfd4 | 417 | /** This method is used to send an SMS message. Note that you cannot send an |
jengbrecht | 27:8e6188cbcfd4 | 418 | * SMS message and have a data connection open at the same time. |
jengbrecht | 27:8e6188cbcfd4 | 419 | * |
jengbrecht | 27:8e6188cbcfd4 | 420 | * @param sms an Sms struct that contains all SMS transaction information. |
jengbrecht | 27:8e6188cbcfd4 | 421 | * @returns the standard AT Code enumeration. |
jengbrecht | 27:8e6188cbcfd4 | 422 | */ |
sgodinez | 4:6561c9128c6f | 423 | Code sendSMS(const Sms& sms); |
jengbrecht | 36:bb6b293c7495 | 424 | |
jengbrecht | 56:e5e5351f14b3 | 425 | /** This method retrieves all of the SMS messages currently available for |
jengbrecht | 56:e5e5351f14b3 | 426 | * this phone number. |
jengbrecht | 27:8e6188cbcfd4 | 427 | * |
jengbrecht | 56:e5e5351f14b3 | 428 | * @returns a vector of existing SMS messages each as an Sms struct. |
jengbrecht | 27:8e6188cbcfd4 | 429 | */ |
sgodinez | 4:6561c9128c6f | 430 | std::vector<Cellular::Sms> getReceivedSms(); |
jengbrecht | 56:e5e5351f14b3 | 431 | |
jengbrecht | 56:e5e5351f14b3 | 432 | /** This method can be used to remove/delete all received SMS messages |
jengbrecht | 56:e5e5351f14b3 | 433 | * even if they have never been retrieved or read. |
jengbrecht | 56:e5e5351f14b3 | 434 | * |
jengbrecht | 56:e5e5351f14b3 | 435 | * @returns the standard AT Code enumeration. |
jengbrecht | 56:e5e5351f14b3 | 436 | */ |
sgodinez | 4:6561c9128c6f | 437 | Code deleteAllReceivedSms(); |
jengbrecht | 56:e5e5351f14b3 | 438 | |
jengbrecht | 56:e5e5351f14b3 | 439 | /** This method can be used to remove/delete all received SMS messages |
jengbrecht | 56:e5e5351f14b3 | 440 | * that have been retrieved by the user through the getReceivedSms method. |
jengbrecht | 56:e5e5351f14b3 | 441 | * Messages that have not been retrieved yet will be unaffected. |
jengbrecht | 56:e5e5351f14b3 | 442 | * |
jengbrecht | 56:e5e5351f14b3 | 443 | * @returns the standard AT Code enumeration. |
jengbrecht | 56:e5e5351f14b3 | 444 | */ |
sgodinez | 4:6561c9128c6f | 445 | Code deleteOnlyReceivedReadSms(); |
sgodinez | 19:38794784e009 | 446 | |
jengbrecht | 56:e5e5351f14b3 | 447 | /** A static method for getting a string representation for the Code |
jengbrecht | 56:e5e5351f14b3 | 448 | * enumeration. |
jengbrecht | 56:e5e5351f14b3 | 449 | * |
jengbrecht | 56:e5e5351f14b3 | 450 | * @param code a Code enumeration. |
jengbrecht | 56:e5e5351f14b3 | 451 | * @returns the enumeration name as a string. |
jengbrecht | 56:e5e5351f14b3 | 452 | */ |
jengbrecht | 56:e5e5351f14b3 | 453 | static std::string getCodeNames(Code code); |
jengbrecht | 56:e5e5351f14b3 | 454 | |
jengbrecht | 56:e5e5351f14b3 | 455 | /** A static method for getting a string representation for the Registration |
jengbrecht | 56:e5e5351f14b3 | 456 | * enumeration. |
jengbrecht | 56:e5e5351f14b3 | 457 | * |
jengbrecht | 56:e5e5351f14b3 | 458 | * @param code a Registration enumeration. |
jengbrecht | 56:e5e5351f14b3 | 459 | * @returns the enumeration name as a string. |
jengbrecht | 56:e5e5351f14b3 | 460 | */ |
jengbrecht | 56:e5e5351f14b3 | 461 | static std::string getRegistrationNames(Registration registration); |
jengbrecht | 27:8e6188cbcfd4 | 462 | |
jengbrecht | 0:563b70517320 | 463 | private: |
jengbrecht | 23:bc6f98a1eb22 | 464 | static Cellular* instance; //Static pointer to the single Cellular object. |
sgodinez | 19:38794784e009 | 465 | |
jengbrecht | 23:bc6f98a1eb22 | 466 | MTSBufferedIO* io; //IO interface obect that the radio is accessed through. |
jengbrecht | 23:bc6f98a1eb22 | 467 | bool echoMode; //Specifies if the echo mode is currently enabled. |
jengbrecht | 27:8e6188cbcfd4 | 468 | |
jengbrecht | 23:bc6f98a1eb22 | 469 | bool pppConnected; //Specifies if a PPP session is currently connected. |
jengbrecht | 23:bc6f98a1eb22 | 470 | std::string apn; //A string that holds the APN for the radio. |
jengbrecht | 27:8e6188cbcfd4 | 471 | |
jengbrecht | 56:e5e5351f14b3 | 472 | Mode mode; //The current socket Mode. |
jengbrecht | 23:bc6f98a1eb22 | 473 | bool socketOpened; //Specifies if a Socket is presently opened. |
jengbrecht | 23:bc6f98a1eb22 | 474 | bool socketCloseable; //Specifies is a Socket can be closed. |
jengbrecht | 27:8e6188cbcfd4 | 475 | unsigned int local_port; //Holds the local port for socket connections. |
jengbrecht | 27:8e6188cbcfd4 | 476 | std::string local_address; //Holds the local address for socket connections. |
jengbrecht | 27:8e6188cbcfd4 | 477 | unsigned int host_port; //Holds the remote port for socket connections. |
jengbrecht | 27:8e6188cbcfd4 | 478 | std::string host_address; //Holds the remote address for socket connections. |
jengbrecht | 56:e5e5351f14b3 | 479 | DigitalIn* dcd; //Maps to the radios dcd signal |
jengbrecht | 56:e5e5351f14b3 | 480 | DigitalOut* dtr; //Maps to the radios dtr signal |
sgodinez | 11:134435d8a2d5 | 481 | |
jengbrecht | 23:bc6f98a1eb22 | 482 | Cellular(); //Private constructor, use the getInstance() method. |
mfiore | 29:7408b1bdad37 | 483 | Cellular(MTSBufferedIO* io); //Private constructor, use the getInstance() method. |
jengbrecht | 0:563b70517320 | 484 | }; |
jengbrecht | 0:563b70517320 | 485 | |
mfiore | 39:6e94520a3217 | 486 | } |
mfiore | 39:6e94520a3217 | 487 | |
jengbrecht | 0:563b70517320 | 488 | #endif /* CELLULAR_H */ |