Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/EasyIP.h@45:b9ead235ab7c, 2014-08-01 (annotated)
- Committer:
- Vanger
- Date:
- Fri Aug 01 15:51:49 2014 +0000
- Revision:
- 45:b9ead235ab7c
- Parent:
- 44:bf07f3d85822
- Child:
- 49:1fc51c53cebf
Changed the response code parsing at the end of sendCommand() in Cellular.cpp to also check for UIP responses; (Removed apn from EasyIP.h example code section)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:f155d94d6f3a | 1 | #ifndef SMC_H |
Mike Fiore |
1:f155d94d6f3a | 2 | #define SMC_H |
Mike Fiore |
1:f155d94d6f3a | 3 | |
Vanger | 26:2b769ed8de4f | 4 | #include <string> |
Vanger | 26:2b769ed8de4f | 5 | #include <vector> |
Vanger | 26:2b769ed8de4f | 6 | |
Vanger | 26:2b769ed8de4f | 7 | #include "MTSBufferedIO.h" |
Vanger | 26:2b769ed8de4f | 8 | #include "Cellular.h" |
Vanger | 26:2b769ed8de4f | 9 | |
Vanger | 26:2b769ed8de4f | 10 | namespace mts |
Vanger | 26:2b769ed8de4f | 11 | { |
Vanger | 33:3b6f3904dde0 | 12 | /** This is a class for communicating without a Multi-Tech Systems SocketModem iCell. Instead, |
Vanger | 33:3b6f3904dde0 | 13 | * it uses the hayes command set to directly implement the same UIP interface to the radio. |
Vanger | 33:3b6f3904dde0 | 14 | * (See the UIP class and documentation, "UIP.h") |
Vanger | 33:3b6f3904dde0 | 15 | * This class supports four main types of cellular radio interactions including: |
Vanger | 33:3b6f3904dde0 | 16 | * configuration and status AT-command processing, SMS processing, TCP Socket data connections, |
Vanger | 33:3b6f3904dde0 | 17 | * and UDP data connections. It should be noted that the radio can not process commands or |
Vanger | 33:3b6f3904dde0 | 18 | * SMS messages while having an open data connection at the same time. The concurrent |
Vanger | 33:3b6f3904dde0 | 19 | * capability may be added in a future release. This class also inherits from IPStack |
Vanger | 33:3b6f3904dde0 | 20 | * providing a common set of commands for communication devices that have an onboard |
Vanger | 33:3b6f3904dde0 | 21 | * IP Stack. It is also integrated with the standard mbed Sockets package and can therefore |
Vanger | 33:3b6f3904dde0 | 22 | * be used seamlessly with clients and services built on top of this interface already within |
Vanger | 33:3b6f3904dde0 | 23 | * the mbed library. |
Vanger | 33:3b6f3904dde0 | 24 | * The default baud rate for the cellular radio is 115200 bps. |
Vanger | 43:91c5e53f508f | 25 | * |
Vanger | 43:91c5e53f508f | 26 | * @code |
Vanger | 43:91c5e53f508f | 27 | * #include "mbed.h" |
Vanger | 43:91c5e53f508f | 28 | * #include "mtsas.h" |
Vanger | 43:91c5e53f508f | 29 | * #include "TCPSocketConnection.h" |
Vanger | 43:91c5e53f508f | 30 | * |
Vanger | 43:91c5e53f508f | 31 | * int main(){ |
Vanger | 43:91c5e53f508f | 32 | * //Modify to match your apn if you are using an HSPA radio with a SIM card |
Vanger | 45:b9ead235ab7c | 33 | * const char APN[] = ""; |
Vanger | 43:91c5e53f508f | 34 | * |
Vanger | 43:91c5e53f508f | 35 | * //Sets the log level to INFO, which is about midway on priority levels |
Vanger | 43:91c5e53f508f | 36 | * //Possible levels: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE, NONE |
Vanger | 43:91c5e53f508f | 37 | * MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
Vanger | 43:91c5e53f508f | 38 | * |
Vanger | 43:91c5e53f508f | 39 | * // STMicro Nucelo F401RE |
Vanger | 43:91c5e53f508f | 40 | * // The supported jumper configurations of the MTSAS do not line up with |
Vanger | 43:91c5e53f508f | 41 | * // the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 43:91c5e53f508f | 42 | * // pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 43:91c5e53f508f | 43 | * // and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 43:91c5e53f508f | 44 | * // Serial1 TX (Shield pin D8). |
Vanger | 43:91c5e53f508f | 45 | * // Uncomment the following line to use the STMicro Nuceleo F401RE |
Vanger | 43:91c5e53f508f | 46 | * MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
Vanger | 43:91c5e53f508f | 47 | * |
Vanger | 43:91c5e53f508f | 48 | * // Freescale KL46Z |
Vanger | 43:91c5e53f508f | 49 | * // To configure the pins for the Freescale KL46Z board, use configuration B |
Vanger | 43:91c5e53f508f | 50 | * // for the SocketModem. |
Vanger | 43:91c5e53f508f | 51 | * // Uncomment the following line to use the Freescale KL46Z board |
Vanger | 43:91c5e53f508f | 52 | * // MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
Vanger | 43:91c5e53f508f | 53 | * |
Vanger | 43:91c5e53f508f | 54 | * // Freescale KL64F |
Vanger | 43:91c5e53f508f | 55 | * // To configure the pins for the Freescale KL46Z board, use configuration A |
Vanger | 43:91c5e53f508f | 56 | * // for the SocketModem. |
Vanger | 43:91c5e53f508f | 57 | * // Uncomment te following line to use the Freescale KL46F board |
Vanger | 43:91c5e53f508f | 58 | * // MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
Vanger | 43:91c5e53f508f | 59 | * |
Vanger | 43:91c5e53f508f | 60 | * // Sets the baudrate for communicating with the radio |
Vanger | 43:91c5e53f508f | 61 | * io->baud(115200); |
Vanger | 43:91c5e53f508f | 62 | * |
Vanger | 43:91c5e53f508f | 63 | * // Sets up the interfacing with the radio through the MTS library |
Vanger | 43:91c5e53f508f | 64 | * Cellular* radio = CellularFactory::create(io); |
Vanger | 43:91c5e53f508f | 65 | * radio->configureSignals(D4, D7, RESET); |
Vanger | 43:91c5e53f508f | 66 | * Transport::setTransport(radio); |
Vanger | 43:91c5e53f508f | 67 | * |
Vanger | 43:91c5e53f508f | 68 | * // Sets the APN on the device (if necessary) |
Vanger | 43:91c5e53f508f | 69 | * for (int i = 0; i < 10; i++) { |
Vanger | 43:91c5e53f508f | 70 | * if (i >= 10) { |
Vanger | 43:91c5e53f508f | 71 | * logError("Failed to set APN to %s", APN); |
Vanger | 43:91c5e53f508f | 72 | * } |
Vanger | 43:91c5e53f508f | 73 | * if (radio->setApn(APN) == MTS_SUCCESS) { |
Vanger | 43:91c5e53f508f | 74 | * logInfo("Successfully set APN to %s", APN); |
Vanger | 43:91c5e53f508f | 75 | * break; |
Vanger | 43:91c5e53f508f | 76 | * } else { |
Vanger | 43:91c5e53f508f | 77 | * wait(1); |
Vanger | 43:91c5e53f508f | 78 | * } |
Vanger | 43:91c5e53f508f | 79 | * } |
Vanger | 43:91c5e53f508f | 80 | * |
Vanger | 43:91c5e53f508f | 81 | * //Establish PPP link |
Vanger | 43:91c5e53f508f | 82 | * for (int i = 0; i < 10; i++) { |
Vanger | 43:91c5e53f508f | 83 | * if (i >= 10) { |
Vanger | 43:91c5e53f508f | 84 | * logError("Failed to establish PPP link"); |
Vanger | 43:91c5e53f508f | 85 | * } |
Vanger | 43:91c5e53f508f | 86 | * if (radio->connect() == true) { |
Vanger | 43:91c5e53f508f | 87 | * logInfo("Successfully established PPP link"); |
Vanger | 43:91c5e53f508f | 88 | * break; |
Vanger | 43:91c5e53f508f | 89 | * } else { |
Vanger | 43:91c5e53f508f | 90 | * wait(1); |
Vanger | 43:91c5e53f508f | 91 | * } |
Vanger | 43:91c5e53f508f | 92 | * } |
Vanger | 43:91c5e53f508f | 93 | * |
Vanger | 43:91c5e53f508f | 94 | * //Ping google.com (optional) |
Vanger | 43:91c5e53f508f | 95 | * for (int i = 0; i < 10; i++) { |
Vanger | 43:91c5e53f508f | 96 | * if (i >= 10) { |
Vanger | 43:91c5e53f508f | 97 | * logError("Failed to ping www.google.com"); |
Vanger | 43:91c5e53f508f | 98 | * } |
Vanger | 43:91c5e53f508f | 99 | * if (radio->ping("www.google.com") == true) { |
Vanger | 43:91c5e53f508f | 100 | * logInfo("Successfully pinged www.google.com"); |
Vanger | 43:91c5e53f508f | 101 | * break; |
Vanger | 43:91c5e53f508f | 102 | * } else { |
Vanger | 43:91c5e53f508f | 103 | * wait(1); |
Vanger | 43:91c5e53f508f | 104 | * } |
Vanger | 43:91c5e53f508f | 105 | * } |
Vanger | 43:91c5e53f508f | 106 | * |
Vanger | 43:91c5e53f508f | 107 | * //Used for packet verification from server's data response |
Vanger | 43:91c5e53f508f | 108 | * const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"; |
Vanger | 43:91c5e53f508f | 109 | * const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
Vanger | 43:91c5e53f508f | 110 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
Vanger | 43:91c5e53f508f | 111 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
Vanger | 43:91c5e53f508f | 112 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
Vanger | 43:91c5e53f508f | 113 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
Vanger | 43:91c5e53f508f | 114 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
Vanger | 43:91c5e53f508f | 115 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
Vanger | 43:91c5e53f508f | 116 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
Vanger | 43:91c5e53f508f | 117 | * "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n"; |
Vanger | 43:91c5e53f508f | 118 | * |
Vanger | 43:91c5e53f508f | 119 | * const char MENU_LINE1[] = "send ascii pattern until keypress"; |
Vanger | 43:91c5e53f508f | 120 | * const char MENU[] = "1 send ascii pattern until keypress" |
Vanger | 43:91c5e53f508f | 121 | * "2 send ascii pattern (numbered)" |
Vanger | 43:91c5e53f508f | 122 | * "3 send pattern and close socket" |
Vanger | 43:91c5e53f508f | 123 | * "4 send [ETX] and wait for keypress" |
Vanger | 43:91c5e53f508f | 124 | * "5 send [DLE] and wait for keypress" |
Vanger | 43:91c5e53f508f | 125 | * "6 send all hex values (00-FF)" |
Vanger | 43:91c5e53f508f | 126 | * "q quit" |
Vanger | 43:91c5e53f508f | 127 | * ">:"; |
Vanger | 43:91c5e53f508f | 128 | * |
Vanger | 43:91c5e53f508f | 129 | * const char TCP_TEST_SERVER[] = "204.26.122.5"; |
Vanger | 43:91c5e53f508f | 130 | * const int TCP_TEST_PORT = 7000; |
Vanger | 43:91c5e53f508f | 131 | * |
Vanger | 43:91c5e53f508f | 132 | * //Creates TCP socket pointer instance |
Vanger | 43:91c5e53f508f | 133 | * TCPSocketConnection* sock = new TCPSocketConnection(); |
Vanger | 43:91c5e53f508f | 134 | * //Turns off read_blocking and sets socket timeout to 2s |
Vanger | 43:91c5e53f508f | 135 | * sock->set_blocking(false, 2); |
Vanger | 43:91c5e53f508f | 136 | * |
Vanger | 43:91c5e53f508f | 137 | * Timer tmr; //Used for timeouts |
Vanger | 43:91c5e53f508f | 138 | * int bytesRead = 0; //Number of bytes read |
Vanger | 43:91c5e53f508f | 139 | * const int readSize = 1024; //Size of buffer |
Vanger | 43:91c5e53f508f | 140 | * char buffer[readSize] = {0}; //Read buffer |
Vanger | 43:91c5e53f508f | 141 | * string result; //Result as a string |
Vanger | 43:91c5e53f508f | 142 | * |
Vanger | 43:91c5e53f508f | 143 | * //Open TCP socket |
Vanger | 43:91c5e53f508f | 144 | * for (int i = 0; i < 5; i++) { |
Vanger | 43:91c5e53f508f | 145 | * if (i >= 5) { |
Vanger | 43:91c5e53f508f | 146 | * logError("Failed to open socket"); |
Vanger | 43:91c5e53f508f | 147 | * } |
Vanger | 43:91c5e53f508f | 148 | * if (! sock->connect(TCP_TEST_SERVER, TCP_TEST_PORT)) { |
Vanger | 43:91c5e53f508f | 149 | * logInfo("Opened TCP server"); |
Vanger | 43:91c5e53f508f | 150 | * break; |
Vanger | 43:91c5e53f508f | 151 | * } else { |
Vanger | 43:91c5e53f508f | 152 | * wait(1); |
Vanger | 43:91c5e53f508f | 153 | * } |
Vanger | 43:91c5e53f508f | 154 | * } |
Vanger | 43:91c5e53f508f | 155 | * |
Vanger | 43:91c5e53f508f | 156 | * //Waiting for menu from remote server |
Vanger | 43:91c5e53f508f | 157 | * logInfo("Receiving Menu"); |
Vanger | 43:91c5e53f508f | 158 | * tmr.reset(); |
Vanger | 43:91c5e53f508f | 159 | * tmr.start(); |
Vanger | 43:91c5e53f508f | 160 | * do { |
Vanger | 43:91c5e53f508f | 161 | * bytesRead = sock->receive(buffer, readSize); |
Vanger | 43:91c5e53f508f | 162 | * if (bytesRead > 0) { |
Vanger | 43:91c5e53f508f | 163 | * result.append(buffer, bytesRead); |
Vanger | 43:91c5e53f508f | 164 | * } |
Vanger | 43:91c5e53f508f | 165 | * logInfo("Total Bytes Read: %d", result.size()); |
Vanger | 43:91c5e53f508f | 166 | * if(result.find(MENU_LINE1) != std::string::npos) { |
Vanger | 43:91c5e53f508f | 167 | * break; |
Vanger | 43:91c5e53f508f | 168 | * } |
Vanger | 43:91c5e53f508f | 169 | * } while(tmr.read() <= 40); |
Vanger | 43:91c5e53f508f | 170 | * |
Vanger | 43:91c5e53f508f | 171 | * wait(5); |
Vanger | 43:91c5e53f508f | 172 | * |
Vanger | 43:91c5e53f508f | 173 | * logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
Vanger | 43:91c5e53f508f | 174 | * |
Vanger | 43:91c5e53f508f | 175 | * //Checking that menu was successfully received |
Vanger | 43:91c5e53f508f | 176 | * size_t pos = result.find(MENU_LINE1); |
Vanger | 43:91c5e53f508f | 177 | * if(pos != string::npos) { |
Vanger | 43:91c5e53f508f | 178 | * logInfo("Found Menu 1st Line"); |
Vanger | 43:91c5e53f508f | 179 | * } else { |
Vanger | 43:91c5e53f508f | 180 | * logError("Failed To Find Menu 1st Line"); |
Vanger | 43:91c5e53f508f | 181 | * sock->close(); |
Vanger | 43:91c5e53f508f | 182 | * return 0; |
Vanger | 43:91c5e53f508f | 183 | * } |
Vanger | 43:91c5e53f508f | 184 | * |
Vanger | 43:91c5e53f508f | 185 | * result.clear(); |
Vanger | 43:91c5e53f508f | 186 | * |
Vanger | 43:91c5e53f508f | 187 | * //Sends a response of '2' back to choose option 2 from the menu |
Vanger | 43:91c5e53f508f | 188 | * logInfo("Writing To Socket: 2"); |
Vanger | 43:91c5e53f508f | 189 | * if(sock->send("2\r\n", 3) == 3) { |
Vanger | 43:91c5e53f508f | 190 | * logInfo("Successfully Wrote '2'"); |
Vanger | 43:91c5e53f508f | 191 | * } else { |
Vanger | 43:91c5e53f508f | 192 | * logError("Failed To Write '2'"); |
Vanger | 43:91c5e53f508f | 193 | * sock->close(); |
Vanger | 43:91c5e53f508f | 194 | * return 0; |
Vanger | 43:91c5e53f508f | 195 | * } |
Vanger | 43:91c5e53f508f | 196 | * logInfo("Expecting 'how many ? >:'"); |
Vanger | 43:91c5e53f508f | 197 | * tmr.reset(); |
Vanger | 43:91c5e53f508f | 198 | * tmr.start(); |
Vanger | 43:91c5e53f508f | 199 | * do { |
Vanger | 43:91c5e53f508f | 200 | * bytesRead = sock->receive(buffer, readSize); |
Vanger | 43:91c5e53f508f | 201 | * if (bytesRead > 0) { |
Vanger | 43:91c5e53f508f | 202 | * result.append(buffer, bytesRead); |
Vanger | 43:91c5e53f508f | 203 | * } |
Vanger | 43:91c5e53f508f | 204 | * logInfo("Total Bytes Read: %d", result.size()); |
Vanger | 43:91c5e53f508f | 205 | * if(result.find("how many") != std::string::npos) { |
Vanger | 43:91c5e53f508f | 206 | * break; |
Vanger | 43:91c5e53f508f | 207 | * } |
Vanger | 43:91c5e53f508f | 208 | * } while(tmr.read() <= 40); |
Vanger | 43:91c5e53f508f | 209 | * |
Vanger | 43:91c5e53f508f | 210 | * logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
Vanger | 43:91c5e53f508f | 211 | * |
Vanger | 43:91c5e53f508f | 212 | * //Sends 2 to have the server send the pattern twice |
Vanger | 43:91c5e53f508f | 213 | * if(result.find("how many") != std::string::npos) { |
Vanger | 43:91c5e53f508f | 214 | * logInfo("Successfully Found 'how many'"); |
Vanger | 43:91c5e53f508f | 215 | * logInfo("Writing To Socket: 2"); |
Vanger | 43:91c5e53f508f | 216 | * if(sock->send("2\r\n", 3) == 3) { |
Vanger | 43:91c5e53f508f | 217 | * logInfo("Successfully wrote '2'"); |
Vanger | 43:91c5e53f508f | 218 | * } else { |
Vanger | 43:91c5e53f508f | 219 | * logError("Failed to write '2'"); |
Vanger | 43:91c5e53f508f | 220 | * sock->close(); |
Vanger | 43:91c5e53f508f | 221 | * return 0; |
Vanger | 43:91c5e53f508f | 222 | * } |
Vanger | 43:91c5e53f508f | 223 | * } else { |
Vanger | 43:91c5e53f508f | 224 | * logError("didn't receive 'how many'"); |
Vanger | 43:91c5e53f508f | 225 | * sock->close(); |
Vanger | 43:91c5e53f508f | 226 | * return 0; |
Vanger | 43:91c5e53f508f | 227 | * } |
Vanger | 43:91c5e53f508f | 228 | * |
Vanger | 43:91c5e53f508f | 229 | * result.clear(); |
Vanger | 43:91c5e53f508f | 230 | * |
Vanger | 43:91c5e53f508f | 231 | * //Receives data from request sent to server |
Vanger | 43:91c5e53f508f | 232 | * logInfo("Receiving Data"); |
Vanger | 43:91c5e53f508f | 233 | * tmr.reset(); |
Vanger | 43:91c5e53f508f | 234 | * tmr.start(); |
Vanger | 43:91c5e53f508f | 235 | * do { |
Vanger | 43:91c5e53f508f | 236 | * bytesRead = sock->receive(buffer, readSize); |
Vanger | 43:91c5e53f508f | 237 | * if (bytesRead > 0) { |
Vanger | 43:91c5e53f508f | 238 | * result.append(buffer, bytesRead); |
Vanger | 43:91c5e53f508f | 239 | * } |
Vanger | 43:91c5e53f508f | 240 | * logInfo("Total Bytes Read: %d", result.size()); |
Vanger | 43:91c5e53f508f | 241 | * if(result.size() >= 1645) { |
Vanger | 43:91c5e53f508f | 242 | * break; |
Vanger | 43:91c5e53f508f | 243 | * } |
Vanger | 43:91c5e53f508f | 244 | * } while(tmr.read() <= 40); |
Vanger | 43:91c5e53f508f | 245 | * |
Vanger | 43:91c5e53f508f | 246 | * logInfo("Received Data: [%d] [%s]", result.size(), result.c_str()); |
Vanger | 43:91c5e53f508f | 247 | * |
Vanger | 43:91c5e53f508f | 248 | * //Compares received data with expected data |
Vanger | 43:91c5e53f508f | 249 | * pos = result.find(PATTERN_LINE1); |
Vanger | 43:91c5e53f508f | 250 | * if(pos != string::npos) { |
Vanger | 43:91c5e53f508f | 251 | * int patternSize = sizeof(PATTERN) - 1; |
Vanger | 43:91c5e53f508f | 252 | * const char* ptr = &result.data()[pos]; |
Vanger | 43:91c5e53f508f | 253 | * bool match = true; |
Vanger | 43:91c5e53f508f | 254 | * for(int i = 0; i < patternSize; i++) { |
Vanger | 43:91c5e53f508f | 255 | * if(PATTERN[i] != ptr[i]) { |
Vanger | 43:91c5e53f508f | 256 | * logError("1st Pattern Doesn't Match At [%d]", i); |
Vanger | 43:91c5e53f508f | 257 | * logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
Vanger | 43:91c5e53f508f | 258 | * match = false; |
Vanger | 43:91c5e53f508f | 259 | * break; |
Vanger | 43:91c5e53f508f | 260 | * } |
Vanger | 43:91c5e53f508f | 261 | * } |
Vanger | 43:91c5e53f508f | 262 | * if(match) { |
Vanger | 43:91c5e53f508f | 263 | * logInfo("Found 1st Pattern"); |
Vanger | 43:91c5e53f508f | 264 | * } else { |
Vanger | 43:91c5e53f508f | 265 | * logError("Failed To Find 1st Pattern"); |
Vanger | 43:91c5e53f508f | 266 | * sock->close(); |
Vanger | 43:91c5e53f508f | 267 | * return 0; |
Vanger | 43:91c5e53f508f | 268 | * } |
Vanger | 43:91c5e53f508f | 269 | * |
Vanger | 43:91c5e53f508f | 270 | * pos = result.find(PATTERN_LINE1, pos + patternSize); |
Vanger | 43:91c5e53f508f | 271 | * if(pos != std::string::npos) { |
Vanger | 43:91c5e53f508f | 272 | * ptr = &result.data()[pos]; |
Vanger | 43:91c5e53f508f | 273 | * match = true; |
Vanger | 43:91c5e53f508f | 274 | * for(int i = 0; i < patternSize; i++) { |
Vanger | 43:91c5e53f508f | 275 | * if(PATTERN[i] != ptr[i]) { |
Vanger | 43:91c5e53f508f | 276 | * logError("2nd Pattern Doesn't Match At [%d]", i); |
Vanger | 43:91c5e53f508f | 277 | * logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
Vanger | 43:91c5e53f508f | 278 | * match = false; |
Vanger | 43:91c5e53f508f | 279 | * break; |
Vanger | 43:91c5e53f508f | 280 | * } |
Vanger | 43:91c5e53f508f | 281 | * } |
Vanger | 43:91c5e53f508f | 282 | * if(match) { |
Vanger | 43:91c5e53f508f | 283 | * logInfo("Found 2nd Pattern"); |
Vanger | 43:91c5e53f508f | 284 | * } else { |
Vanger | 43:91c5e53f508f | 285 | * logError("Failed To Find 2nd Pattern"); |
Vanger | 43:91c5e53f508f | 286 | * sock->close(); |
Vanger | 43:91c5e53f508f | 287 | * return 0; |
Vanger | 43:91c5e53f508f | 288 | * } |
Vanger | 43:91c5e53f508f | 289 | * } |
Vanger | 43:91c5e53f508f | 290 | * } else { |
Vanger | 43:91c5e53f508f | 291 | * logError("Failed To Find Pattern 1st Line"); |
Vanger | 43:91c5e53f508f | 292 | * sock->close(); |
Vanger | 43:91c5e53f508f | 293 | * return 0; |
Vanger | 43:91c5e53f508f | 294 | * } |
Vanger | 43:91c5e53f508f | 295 | * |
Vanger | 43:91c5e53f508f | 296 | * //Clears the result, and closes the socket connection. |
Vanger | 43:91c5e53f508f | 297 | * result.clear(); |
Vanger | 43:91c5e53f508f | 298 | * sock->close(); |
Vanger | 43:91c5e53f508f | 299 | * |
Vanger | 43:91c5e53f508f | 300 | * //Disconnect ppp link |
Vanger | 43:91c5e53f508f | 301 | * radio->disconnect(); |
Vanger | 43:91c5e53f508f | 302 | * |
Vanger | 43:91c5e53f508f | 303 | * logInfo("End of example code"); |
Vanger | 43:91c5e53f508f | 304 | * return 0; |
Vanger | 43:91c5e53f508f | 305 | * } |
Vanger | 44:bf07f3d85822 | 306 | * @endcode |
Vanger | 33:3b6f3904dde0 | 307 | */ |
Vanger | 33:3b6f3904dde0 | 308 | class EasyIP : public Cellular |
Vanger | 26:2b769ed8de4f | 309 | { |
Vanger | 26:2b769ed8de4f | 310 | public: |
Vanger | 33:3b6f3904dde0 | 311 | /** This static function is used to create or get a reference to a |
Vanger | 26:2b769ed8de4f | 312 | * Cellular object. Cellular uses the singleton pattern, which means |
Vanger | 26:2b769ed8de4f | 313 | * that you can only have one existing at a time. The first time you |
Vanger | 26:2b769ed8de4f | 314 | * call getInstance this method creates a new uninitialized Cellular |
Vanger | 26:2b769ed8de4f | 315 | * object and returns it. All future calls to this method will return |
Vanger | 26:2b769ed8de4f | 316 | * a reference to the instance created during the first call. Note that |
Vanger | 26:2b769ed8de4f | 317 | * you must call init on the returned instance before mnaking any other |
Vanger | 26:2b769ed8de4f | 318 | * calls. If using this class's bindings to any of the Socket package |
Vanger | 26:2b769ed8de4f | 319 | * classes like TCPSocketConnection, you must call this method and the |
Vanger | 26:2b769ed8de4f | 320 | * init method on the returned object first, before even creating the |
Vanger | 26:2b769ed8de4f | 321 | * other objects. |
Vanger | 26:2b769ed8de4f | 322 | * |
Vanger | 26:2b769ed8de4f | 323 | * @returns a reference to the single Cellular obect that has been created. |
Vanger | 26:2b769ed8de4f | 324 | */ |
Vanger | 26:2b769ed8de4f | 325 | EasyIP(Radio type); |
Vanger | 34:7d412c989964 | 326 | |
Vanger | 26:2b769ed8de4f | 327 | /** Destructs a Cellular object and frees all related resources. |
Vanger | 26:2b769ed8de4f | 328 | */ |
Vanger | 26:2b769ed8de4f | 329 | ~EasyIP(); |
Vanger | 34:7d412c989964 | 330 | |
Vanger | 33:3b6f3904dde0 | 331 | /** Initializes the MTS IO buffer |
Vanger | 33:3b6f3904dde0 | 332 | */ |
Vanger | 26:2b769ed8de4f | 333 | virtual bool init(MTSBufferedIO* io); |
Vanger | 33:3b6f3904dde0 | 334 | |
Vanger | 33:3b6f3904dde0 | 335 | /** PPP connect command. |
Vanger | 33:3b6f3904dde0 | 336 | * Connects the radio to the cellular network. |
Vanger | 33:3b6f3904dde0 | 337 | * |
Vanger | 33:3b6f3904dde0 | 338 | * @returns true if PPP connection to the network succeeded, |
Vanger | 33:3b6f3904dde0 | 339 | * false if the PPP connection failed. |
Vanger | 33:3b6f3904dde0 | 340 | */ |
Vanger | 26:2b769ed8de4f | 341 | virtual bool connect(); |
Vanger | 33:3b6f3904dde0 | 342 | |
Vanger | 33:3b6f3904dde0 | 343 | /** PPP disconnect command. |
Vanger | 33:3b6f3904dde0 | 344 | * Disconnects from the PPP network, and will also close active socket |
Vanger | 33:3b6f3904dde0 | 345 | * connection if open. |
Vanger | 33:3b6f3904dde0 | 346 | */ |
Vanger | 26:2b769ed8de4f | 347 | virtual void disconnect(); |
Vanger | 33:3b6f3904dde0 | 348 | |
Vanger | 33:3b6f3904dde0 | 349 | /** Checks if the radio is connected to the cell network. |
Vanger | 33:3b6f3904dde0 | 350 | * Checks antenna signal, cell tower registration, and context activation |
Vanger | 33:3b6f3904dde0 | 351 | * before finally pinging (4 pings, 32 bytes each) to confirm PPP connection |
Vanger | 33:3b6f3904dde0 | 352 | * to network. Will return true if there is an open socket connection as well. |
Vanger | 33:3b6f3904dde0 | 353 | * |
Vanger | 33:3b6f3904dde0 | 354 | * @returns true if there is a PPP connection to the cell network, false |
Vanger | 33:3b6f3904dde0 | 355 | * if there is no PPP connection to the cell network. |
Vanger | 33:3b6f3904dde0 | 356 | */ |
Vanger | 26:2b769ed8de4f | 357 | virtual bool isConnected(); |
Vanger | 33:3b6f3904dde0 | 358 | |
Vanger | 33:3b6f3904dde0 | 359 | /** Resets the radio/modem. |
Vanger | 33:3b6f3904dde0 | 360 | * Disconnects all active PPP and socket connections to do so. |
Vanger | 33:3b6f3904dde0 | 361 | */ |
Vanger | 26:2b769ed8de4f | 362 | virtual void reset(); |
Vanger | 26:2b769ed8de4f | 363 | |
Vanger | 26:2b769ed8de4f | 364 | // TCP and UDP Socket related commands |
Vanger | 26:2b769ed8de4f | 365 | // For behavior of the following methods refer to IPStack.h documentation |
Vanger | 26:2b769ed8de4f | 366 | virtual bool bind(unsigned int port); |
Vanger | 26:2b769ed8de4f | 367 | virtual bool open(const std::string& address, unsigned int port, Mode mode); |
Vanger | 26:2b769ed8de4f | 368 | virtual bool isOpen(); |
Vanger | 26:2b769ed8de4f | 369 | virtual bool close(); |
Vanger | 31:529db15abda7 | 370 | virtual int read(char* data, int max, int timeout = -1); |
Vanger | 26:2b769ed8de4f | 371 | virtual int write(const char* data, int length, int timeout = -1); |
Vanger | 26:2b769ed8de4f | 372 | virtual unsigned int readable(); |
Vanger | 26:2b769ed8de4f | 373 | virtual unsigned int writeable(); |
Vanger | 33:3b6f3904dde0 | 374 | virtual bool ping(const std::string& address = "8.8.8.8"); //Google DNS server used as default ping address |
Vanger | 26:2b769ed8de4f | 375 | virtual std::string getDeviceIP(); |
Vanger | 33:3b6f3904dde0 | 376 | virtual bool setDeviceIP(std::string address = "DHCP"); |
Vanger | 26:2b769ed8de4f | 377 | |
Vanger | 33:3b6f3904dde0 | 378 | /** Sets the APN |
Vanger | 33:3b6f3904dde0 | 379 | * |
Vanger | 33:3b6f3904dde0 | 380 | * @param apn c-string of the APN to use |
Vanger | 33:3b6f3904dde0 | 381 | * |
Vanger | 33:3b6f3904dde0 | 382 | * @returns MTS_SUCCESS if the APN was set, or is not needed, else it |
Vanger | 33:3b6f3904dde0 | 383 | * returns the result of the AT command sent to the radio to set the APN |
Vanger | 33:3b6f3904dde0 | 384 | */ |
Vanger | 26:2b769ed8de4f | 385 | virtual Code setApn(const std::string& apn); |
Vanger | 33:3b6f3904dde0 | 386 | |
Vanger | 26:2b769ed8de4f | 387 | /** A method for configuring command ehco capability on the radio. This command |
Vanger | 26:2b769ed8de4f | 388 | * sets whether sent characters are echoed back from the radio, in which case you |
Vanger | 26:2b769ed8de4f | 389 | * will receive back every command you send. |
Vanger | 26:2b769ed8de4f | 390 | * |
Vanger | 26:2b769ed8de4f | 391 | * @param state if true echo will be turned off, otherwise it will be turned on. |
Vanger | 26:2b769ed8de4f | 392 | * @returns the standard AT Code enumeration. |
Vanger | 26:2b769ed8de4f | 393 | */ |
Vanger | 27:ec44d5a9544f | 394 | virtual Code echo(bool state); |
Vanger | 26:2b769ed8de4f | 395 | |
Vanger | 26:2b769ed8de4f | 396 | /** This method can be used to trade socket functionality for performance. |
Vanger | 33:3b6f3904dde0 | 397 | * Can disable checking socket closed messages from the data socket, and thus the socket |
Vanger | 33:3b6f3904dde0 | 398 | * will only be visibly closed to the local side if the radio is explicitly checked, or |
Vanger | 33:3b6f3904dde0 | 399 | * the socket is closed by the local side through the use of physical pin manipulation. |
Vanger | 33:3b6f3904dde0 | 400 | * |
Vanger | 33:3b6f3904dde0 | 401 | * Uses the Hayes escape sequence (1 second pause, "+++", 1 second pause) to exit the socket |
Vanger | 33:3b6f3904dde0 | 402 | * connection to check if a received "NO CARRIER" string is from the radio indicating the socket |
Vanger | 33:3b6f3904dde0 | 403 | * has been closed, or is merely part of the data stream. Should not occur very often, however, if |
Vanger | 33:3b6f3904dde0 | 404 | * data carrying the string "NO CARRIER" is going to be transmitted frequently, then the socket should |
Vanger | 33:3b6f3904dde0 | 405 | * be set closeable and physical-socket-closing-means be used instead to reduce the large amount of |
Vanger | 33:3b6f3904dde0 | 406 | * overhead switching from checking the validity of the "NO CARRIER" message being and indication of |
Vanger | 33:3b6f3904dde0 | 407 | * the socket connection being closed. |
Vanger | 26:2b769ed8de4f | 408 | * |
Vanger | 26:2b769ed8de4f | 409 | * @param enabled set to true if you want the socket closeable, otherwise false. The default |
Vanger | 26:2b769ed8de4f | 410 | * is true. |
Vanger | 26:2b769ed8de4f | 411 | * @returns the standard AT Code enumeration. |
Vanger | 26:2b769ed8de4f | 412 | */ |
Vanger | 33:3b6f3904dde0 | 413 | virtual Code setSocketCloseable(bool enabled = true); |
Vanger | 33:3b6f3904dde0 | 414 | |
Vanger | 33:3b6f3904dde0 | 415 | private: |
Vanger | 33:3b6f3904dde0 | 416 | /** Function that sends +++ to the radio to exit data mode |
Vanger | 33:3b6f3904dde0 | 417 | * returns true if it successfully exits from online mode, else |
Vanger | 33:3b6f3904dde0 | 418 | * it returns false. Used due to the fact that AT commands |
Vanger | 33:3b6f3904dde0 | 419 | * cannot be sent while in data mode. |
Vanger | 33:3b6f3904dde0 | 420 | * |
Vanger | 33:3b6f3904dde0 | 421 | * @returns true if the radio dropped from data mode to commande mode |
Vanger | 33:3b6f3904dde0 | 422 | * or is already in command mode (socket is still open in the background), |
Vanger | 33:3b6f3904dde0 | 423 | * and false if the radio failed to switch to command mode. |
Vanger | 33:3b6f3904dde0 | 424 | */ |
Vanger | 33:3b6f3904dde0 | 425 | virtual bool sendEscapeCommand(); |
Vanger | 33:3b6f3904dde0 | 426 | |
Vanger | 33:3b6f3904dde0 | 427 | /** Switches to command mode, queries the status of the socket connection, |
Vanger | 33:3b6f3904dde0 | 428 | * and then returns back to the active socket connection (if still open) |
Vanger | 33:3b6f3904dde0 | 429 | * |
Vanger | 33:3b6f3904dde0 | 430 | * @returns true if a socket is currently open, otherwise it returns false |
Vanger | 33:3b6f3904dde0 | 431 | */ |
Vanger | 33:3b6f3904dde0 | 432 | virtual bool socketCheck(); |
Vanger | 26:2b769ed8de4f | 433 | }; |
Vanger | 26:2b769ed8de4f | 434 | |
Vanger | 26:2b769ed8de4f | 435 | } |
Vanger | 26:2b769ed8de4f | 436 | |
Vanger | 33:3b6f3904dde0 | 437 | #endif /* SMC_H */ |