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