For bug fixes
Fork of MTS-Socket by
Test/TestTCPSocketConnection.h@11:892ffac4946b, 2014-06-19 (annotated)
- Committer:
- mfiore
- Date:
- Thu Jun 19 13:13:20 2014 +0000
- Revision:
- 11:892ffac4946b
- Parent:
- 10:42220b7df921
- Child:
- 12:9efe3cc3cb6c
keep address and port of TCP test server since the test is specifically for how that server behaves
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 7:08b474178245 | 1 | #ifndef TESTTCPSOCKETCONNECTION_H |
mfiore | 7:08b474178245 | 2 | #define TESTTCPSOCKETCONNECTION_H |
mfiore | 7:08b474178245 | 3 | |
mfiore | 7:08b474178245 | 4 | #include "mtsas.h" |
mfiore | 7:08b474178245 | 5 | #include <string> |
mfiore | 7:08b474178245 | 6 | |
mfiore | 7:08b474178245 | 7 | using namespace mts; |
mfiore | 7:08b474178245 | 8 | |
mfiore | 7:08b474178245 | 9 | class TestTCPSocketConnection : public TestCollection |
mfiore | 7:08b474178245 | 10 | { |
mfiore | 7:08b474178245 | 11 | public: |
mfiore | 7:08b474178245 | 12 | TestTCPSocketConnection(); |
mfiore | 7:08b474178245 | 13 | virtual void run(); |
mfiore | 8:a3b41ec82e63 | 14 | |
mfiore | 7:08b474178245 | 15 | private: |
mfiore | 8:a3b41ec82e63 | 16 | bool runIteration(); |
mfiore | 7:08b474178245 | 17 | MTSSerialFlowControl* io; |
mfiore | 7:08b474178245 | 18 | Cellular* radio; |
mfiore | 7:08b474178245 | 19 | TCPSocketConnection* sock; |
mfiore | 7:08b474178245 | 20 | }; |
mfiore | 7:08b474178245 | 21 | |
mfiore | 8:a3b41ec82e63 | 22 | const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"; |
mfiore | 8:a3b41ec82e63 | 23 | const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 8:a3b41ec82e63 | 24 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 8:a3b41ec82e63 | 25 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 8:a3b41ec82e63 | 26 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 8:a3b41ec82e63 | 27 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 8:a3b41ec82e63 | 28 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 8:a3b41ec82e63 | 29 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 8:a3b41ec82e63 | 30 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 8:a3b41ec82e63 | 31 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n"; |
mfiore | 7:08b474178245 | 32 | |
mfiore | 8:a3b41ec82e63 | 33 | const char MENU_LINE1[] = "send ascii pattern until keypress"; |
mfiore | 7:08b474178245 | 34 | const char MENU[] = "1 send ascii pattern until keypress" |
mfiore | 7:08b474178245 | 35 | "2 send ascii pattern (numbered)" |
mfiore | 7:08b474178245 | 36 | "3 send pattern and close socket" |
mfiore | 7:08b474178245 | 37 | "4 send [ETX] and wait for keypress" |
mfiore | 7:08b474178245 | 38 | "5 send [DLE] and wait for keypress" |
mfiore | 7:08b474178245 | 39 | "6 send all hex values (00-FF)" |
mfiore | 7:08b474178245 | 40 | "q quit" |
mfiore | 7:08b474178245 | 41 | ">:"; |
mfiore | 7:08b474178245 | 42 | |
mfiore | 11:892ffac4946b | 43 | const char TCP_TEST_SERVER[] = "204.26.122.5"; |
mfiore | 11:892ffac4946b | 44 | const int TCP_TEST_PORT = 7000; |
mfiore | 7:08b474178245 | 45 | |
mfiore | 7:08b474178245 | 46 | TestTCPSocketConnection::TestTCPSocketConnection() : TestCollection("TestTCPSocketConnection") {} |
mfiore | 7:08b474178245 | 47 | |
mfiore | 8:a3b41ec82e63 | 48 | void TestTCPSocketConnection::run() |
mfiore | 8:a3b41ec82e63 | 49 | { |
mfiore | 10:42220b7df921 | 50 | const char APN[] = ""; |
mfiore | 10:42220b7df921 | 51 | |
mfiore | 7:08b474178245 | 52 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
mfiore | 7:08b474178245 | 53 | Test::start("Setup"); |
mfiore | 8:a3b41ec82e63 | 54 | io = new MTSSerialFlowControl(D8, D2, D3, D6); |
mfiore | 7:08b474178245 | 55 | io->baud(115200); |
mfiore | 7:08b474178245 | 56 | radio = CellularFactory::create(io); |
mfiore | 7:08b474178245 | 57 | if (! radio) { |
mfiore | 7:08b474178245 | 58 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 59 | } |
mfiore | 8:a3b41ec82e63 | 60 | radio->configureSignals(D4, D7, RESET); |
mfiore | 7:08b474178245 | 61 | Transport::setTransport(radio); |
mfiore | 8:a3b41ec82e63 | 62 | |
mfiore | 7:08b474178245 | 63 | for (int i = 0; i < 10; i++) { |
mfiore | 7:08b474178245 | 64 | if (i >= 10) { |
mfiore | 7:08b474178245 | 65 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 66 | } |
mfiore | 10:42220b7df921 | 67 | if (radio->setApn(APN) == SUCCESS) { |
mfiore | 7:08b474178245 | 68 | break; |
mfiore | 7:08b474178245 | 69 | } else { |
mfiore | 7:08b474178245 | 70 | wait(1); |
mfiore | 7:08b474178245 | 71 | } |
mfiore | 7:08b474178245 | 72 | } |
mfiore | 7:08b474178245 | 73 | for (int i = 0; i < 3; i++) { |
mfiore | 7:08b474178245 | 74 | if (i >= 3) { |
mfiore | 7:08b474178245 | 75 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 76 | } |
mfiore | 7:08b474178245 | 77 | if (radio->connect()) { |
mfiore | 7:08b474178245 | 78 | break; |
mfiore | 7:08b474178245 | 79 | } else { |
mfiore | 7:08b474178245 | 80 | wait(1); |
mfiore | 7:08b474178245 | 81 | } |
mfiore | 7:08b474178245 | 82 | } |
mfiore | 8:a3b41ec82e63 | 83 | |
mfiore | 7:08b474178245 | 84 | for (int i = 0; i < 5; i++) { |
mfiore | 7:08b474178245 | 85 | if (i >= 5) { |
mfiore | 7:08b474178245 | 86 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 87 | } |
mfiore | 7:08b474178245 | 88 | if (radio->ping()) { |
mfiore | 7:08b474178245 | 89 | break; |
mfiore | 7:08b474178245 | 90 | } else { |
mfiore | 7:08b474178245 | 91 | wait(1); |
mfiore | 7:08b474178245 | 92 | } |
mfiore | 7:08b474178245 | 93 | } |
mfiore | 8:a3b41ec82e63 | 94 | |
mfiore | 7:08b474178245 | 95 | sock = new TCPSocketConnection(); |
mfiore | 8:a3b41ec82e63 | 96 | sock->set_blocking(false, 2); |
mfiore | 7:08b474178245 | 97 | Test::end(); |
mfiore | 8:a3b41ec82e63 | 98 | |
mfiore | 8:a3b41ec82e63 | 99 | for (int i = 0; i < 10; i++) { |
mfiore | 8:a3b41ec82e63 | 100 | Test::start("Test TCP"); |
mfiore | 8:a3b41ec82e63 | 101 | Test::assertTrue(runIteration()); |
mfiore | 8:a3b41ec82e63 | 102 | Test::end(); |
mfiore | 7:08b474178245 | 103 | } |
mfiore | 7:08b474178245 | 104 | } |
mfiore | 7:08b474178245 | 105 | |
mfiore | 8:a3b41ec82e63 | 106 | bool TestTCPSocketConnection::runIteration() |
mfiore | 8:a3b41ec82e63 | 107 | { |
mfiore | 7:08b474178245 | 108 | Timer tmr; |
mfiore | 7:08b474178245 | 109 | int bytesRead = 0; |
mfiore | 7:08b474178245 | 110 | const int readSize = 1024; |
mfiore | 7:08b474178245 | 111 | char buffer[readSize] = {0}; |
mfiore | 7:08b474178245 | 112 | string result; |
mfiore | 8:a3b41ec82e63 | 113 | |
mfiore | 7:08b474178245 | 114 | for (int i = 0; i < 5; i++) { |
mfiore | 7:08b474178245 | 115 | if (i >= 5) { |
mfiore | 7:08b474178245 | 116 | return false; |
mfiore | 7:08b474178245 | 117 | } |
mfiore | 9:b2e3862705fc | 118 | if (! sock->connect(TCP_TEST_SERVER, TCP_TEST_PORT)) { |
mfiore | 7:08b474178245 | 119 | break; |
mfiore | 7:08b474178245 | 120 | } else { |
mfiore | 7:08b474178245 | 121 | wait(1); |
mfiore | 7:08b474178245 | 122 | } |
mfiore | 7:08b474178245 | 123 | } |
mfiore | 8:a3b41ec82e63 | 124 | |
mfiore | 8:a3b41ec82e63 | 125 | logInfo("Receiving Menu"); |
mfiore | 8:a3b41ec82e63 | 126 | tmr.reset(); |
mfiore | 8:a3b41ec82e63 | 127 | tmr.start(); |
mfiore | 8:a3b41ec82e63 | 128 | do { |
mfiore | 8:a3b41ec82e63 | 129 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 8:a3b41ec82e63 | 130 | if (bytesRead > 0) { |
mfiore | 8:a3b41ec82e63 | 131 | result.append(buffer, bytesRead); |
mfiore | 8:a3b41ec82e63 | 132 | } |
mfiore | 8:a3b41ec82e63 | 133 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 134 | } while(tmr.read() <= 5); |
mfiore | 7:08b474178245 | 135 | |
mfiore | 8:a3b41ec82e63 | 136 | wait(5); |
mfiore | 8:a3b41ec82e63 | 137 | |
mfiore | 8:a3b41ec82e63 | 138 | logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 139 | |
mfiore | 8:a3b41ec82e63 | 140 | size_t pos = result.find(MENU_LINE1); |
mfiore | 8:a3b41ec82e63 | 141 | if(pos != string::npos) { |
mfiore | 8:a3b41ec82e63 | 142 | logInfo("Found Menu 1st Line"); |
mfiore | 8:a3b41ec82e63 | 143 | } else { |
mfiore | 8:a3b41ec82e63 | 144 | logError("Failed To Find Menu 1st Line"); |
mfiore | 8:a3b41ec82e63 | 145 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 146 | return false; |
mfiore | 8:a3b41ec82e63 | 147 | } |
mfiore | 8:a3b41ec82e63 | 148 | |
mfiore | 8:a3b41ec82e63 | 149 | result.clear(); |
mfiore | 8:a3b41ec82e63 | 150 | |
mfiore | 8:a3b41ec82e63 | 151 | logInfo("Writing To Socket: 2"); |
mfiore | 8:a3b41ec82e63 | 152 | if(sock->send("2\r\n", 3) == 3) { |
mfiore | 8:a3b41ec82e63 | 153 | logInfo("Successfully Wrote '2'"); |
mfiore | 8:a3b41ec82e63 | 154 | } else { |
mfiore | 8:a3b41ec82e63 | 155 | logError("Failed To Write '2'"); |
mfiore | 8:a3b41ec82e63 | 156 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 157 | return false; |
mfiore | 8:a3b41ec82e63 | 158 | } |
mfiore | 8:a3b41ec82e63 | 159 | logInfo("Expecting 'how many ? >:'"); |
mfiore | 8:a3b41ec82e63 | 160 | tmr.reset(); |
mfiore | 8:a3b41ec82e63 | 161 | tmr.start(); |
mfiore | 8:a3b41ec82e63 | 162 | do { |
mfiore | 8:a3b41ec82e63 | 163 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 8:a3b41ec82e63 | 164 | if (bytesRead > 0) { |
mfiore | 8:a3b41ec82e63 | 165 | result.append(buffer, bytesRead); |
mfiore | 8:a3b41ec82e63 | 166 | } |
mfiore | 8:a3b41ec82e63 | 167 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 168 | } while(tmr.read() <= 3); |
mfiore | 8:a3b41ec82e63 | 169 | |
mfiore | 8:a3b41ec82e63 | 170 | logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 171 | |
mfiore | 8:a3b41ec82e63 | 172 | if(result.find("how many") != std::string::npos) { |
mfiore | 8:a3b41ec82e63 | 173 | logInfo("Successfully Found 'how many'"); |
mfiore | 8:a3b41ec82e63 | 174 | logInfo("Writing To Socket: 2"); |
mfiore | 8:a3b41ec82e63 | 175 | if(sock->send("2\r\n", 3) == 3) { |
mfiore | 8:a3b41ec82e63 | 176 | logInfo("Successfully wrote '2'"); |
mfiore | 8:a3b41ec82e63 | 177 | } else { |
mfiore | 8:a3b41ec82e63 | 178 | logError("Failed to write '2'"); |
mfiore | 8:a3b41ec82e63 | 179 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 180 | return false; |
mfiore | 8:a3b41ec82e63 | 181 | } |
mfiore | 8:a3b41ec82e63 | 182 | } else { |
mfiore | 8:a3b41ec82e63 | 183 | logError("didn't receive 'how many'"); |
mfiore | 8:a3b41ec82e63 | 184 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 185 | return false; |
mfiore | 8:a3b41ec82e63 | 186 | } |
mfiore | 8:a3b41ec82e63 | 187 | |
mfiore | 8:a3b41ec82e63 | 188 | result.clear(); |
mfiore | 8:a3b41ec82e63 | 189 | |
mfiore | 7:08b474178245 | 190 | logInfo("Receiving Data"); |
mfiore | 7:08b474178245 | 191 | tmr.reset(); |
mfiore | 7:08b474178245 | 192 | tmr.start(); |
mfiore | 7:08b474178245 | 193 | do { |
mfiore | 7:08b474178245 | 194 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 7:08b474178245 | 195 | if (bytesRead > 0) { |
mfiore | 7:08b474178245 | 196 | result.append(buffer, bytesRead); |
mfiore | 7:08b474178245 | 197 | } |
mfiore | 7:08b474178245 | 198 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 199 | } while(tmr.read() <= 8); |
mfiore | 8:a3b41ec82e63 | 200 | |
mfiore | 7:08b474178245 | 201 | logInfo("Received Data: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 202 | |
mfiore | 7:08b474178245 | 203 | pos = result.find(PATTERN_LINE1); |
mfiore | 7:08b474178245 | 204 | if(pos != string::npos) { |
mfiore | 7:08b474178245 | 205 | int patternSize = sizeof(PATTERN) - 1; |
mfiore | 7:08b474178245 | 206 | const char* ptr = &result.data()[pos]; |
mfiore | 7:08b474178245 | 207 | bool match = true; |
mfiore | 7:08b474178245 | 208 | for(int i = 0; i < patternSize; i++) { |
mfiore | 7:08b474178245 | 209 | if(PATTERN[i] != ptr[i]) { |
mfiore | 7:08b474178245 | 210 | logError("1st Pattern Doesn't Match At [%d]", i); |
mfiore | 7:08b474178245 | 211 | logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
mfiore | 7:08b474178245 | 212 | match = false; |
mfiore | 8:a3b41ec82e63 | 213 | break; |
mfiore | 7:08b474178245 | 214 | } |
mfiore | 7:08b474178245 | 215 | } |
mfiore | 7:08b474178245 | 216 | if(match) { |
mfiore | 8:a3b41ec82e63 | 217 | logInfo("Found 1st Pattern"); |
mfiore | 8:a3b41ec82e63 | 218 | } else { |
mfiore | 8:a3b41ec82e63 | 219 | logError("Failed To Find 1st Pattern"); |
mfiore | 8:a3b41ec82e63 | 220 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 221 | return false; |
mfiore | 7:08b474178245 | 222 | } |
mfiore | 8:a3b41ec82e63 | 223 | |
mfiore | 7:08b474178245 | 224 | pos = result.find(PATTERN_LINE1, pos + patternSize); |
mfiore | 7:08b474178245 | 225 | if(pos != std::string::npos) { |
mfiore | 7:08b474178245 | 226 | ptr = &result.data()[pos]; |
mfiore | 7:08b474178245 | 227 | match = true; |
mfiore | 7:08b474178245 | 228 | for(int i = 0; i < patternSize; i++) { |
mfiore | 7:08b474178245 | 229 | if(PATTERN[i] != ptr[i]) { |
mfiore | 7:08b474178245 | 230 | logError("2nd Pattern Doesn't Match At [%d]", i); |
mfiore | 7:08b474178245 | 231 | logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
mfiore | 7:08b474178245 | 232 | match = false; |
mfiore | 8:a3b41ec82e63 | 233 | break; |
mfiore | 7:08b474178245 | 234 | } |
mfiore | 7:08b474178245 | 235 | } |
mfiore | 7:08b474178245 | 236 | if(match) { |
mfiore | 8:a3b41ec82e63 | 237 | logInfo("Found 2nd Pattern"); |
mfiore | 8:a3b41ec82e63 | 238 | } else { |
mfiore | 8:a3b41ec82e63 | 239 | logError("Failed To Find 2nd Pattern"); |
mfiore | 8:a3b41ec82e63 | 240 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 241 | return false; |
mfiore | 7:08b474178245 | 242 | } |
mfiore | 7:08b474178245 | 243 | } |
mfiore | 8:a3b41ec82e63 | 244 | } else { |
mfiore | 8:a3b41ec82e63 | 245 | logError("Failed To Find Pattern 1st Line"); |
mfiore | 8:a3b41ec82e63 | 246 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 247 | return false; |
mfiore | 7:08b474178245 | 248 | } |
mfiore | 8:a3b41ec82e63 | 249 | |
mfiore | 7:08b474178245 | 250 | result.clear(); |
mfiore | 7:08b474178245 | 251 | sock->close(); |
mfiore | 7:08b474178245 | 252 | return true; |
mfiore | 7:08b474178245 | 253 | } |
mfiore | 7:08b474178245 | 254 | |
mfiore | 7:08b474178245 | 255 | #endif |