reverted HTTPCLient debug back to defaulted off

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by Keith Ruenheck

Committer:
JonB
Date:
Fri Mar 11 16:46:34 2016 +0000
Revision:
51:d4ac1662a2df
Parent:
35:c85bea9cb713
reverted HTTPCLient debug back to defaulted off

Who changed what in which revision?

UserRevisionLine numberNew 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
mfiore 7:08b474178245 51 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
mfiore 7:08b474178245 52 Test::start("Setup");
mfiore 8:a3b41ec82e63 53 io = new MTSSerialFlowControl(D8, D2, D3, D6);
mfiore 7:08b474178245 54 io->baud(115200);
mfiore 7:08b474178245 55 radio = CellularFactory::create(io);
mfiore 7:08b474178245 56 if (! radio) {
mfiore 7:08b474178245 57 Test::assertTrue(false);
mfiore 7:08b474178245 58 }
mfiore 8:a3b41ec82e63 59 radio->configureSignals(D4, D7, RESET);
mfiore 7:08b474178245 60 Transport::setTransport(radio);
mfiore 8:a3b41ec82e63 61
mfiore 7:08b474178245 62 for (int i = 0; i < 10; i++) {
mfiore 7:08b474178245 63 if (i >= 10) {
mfiore 7:08b474178245 64 Test::assertTrue(false);
mfiore 7:08b474178245 65 }
mfiore 12:9efe3cc3cb6c 66 if (radio->setApn(APN) == MTS_SUCCESS) {
mfiore 7:08b474178245 67 break;
mfiore 7:08b474178245 68 } else {
mfiore 7:08b474178245 69 wait(1);
mfiore 7:08b474178245 70 }
mfiore 7:08b474178245 71 }
mfiore 7:08b474178245 72 for (int i = 0; i < 3; i++) {
mfiore 7:08b474178245 73 if (i >= 3) {
mfiore 7:08b474178245 74 Test::assertTrue(false);
mfiore 7:08b474178245 75 }
mfiore 7:08b474178245 76 if (radio->connect()) {
mfiore 7:08b474178245 77 break;
mfiore 7:08b474178245 78 } else {
mfiore 7:08b474178245 79 wait(1);
mfiore 7:08b474178245 80 }
mfiore 7:08b474178245 81 }
mfiore 8:a3b41ec82e63 82
mfiore 7:08b474178245 83 for (int i = 0; i < 5; i++) {
mfiore 7:08b474178245 84 if (i >= 5) {
mfiore 7:08b474178245 85 Test::assertTrue(false);
mfiore 7:08b474178245 86 }
mfiore 7:08b474178245 87 if (radio->ping()) {
mfiore 7:08b474178245 88 break;
mfiore 7:08b474178245 89 } else {
mfiore 7:08b474178245 90 wait(1);
mfiore 7:08b474178245 91 }
mfiore 7:08b474178245 92 }
mfiore 8:a3b41ec82e63 93
mfiore 7:08b474178245 94 sock = new TCPSocketConnection();
mfiore 8:a3b41ec82e63 95 sock->set_blocking(false, 2);
mfiore 7:08b474178245 96 Test::end();
mfiore 8:a3b41ec82e63 97
mfiore 8:a3b41ec82e63 98 for (int i = 0; i < 10; i++) {
mfiore 8:a3b41ec82e63 99 Test::start("Test TCP");
mfiore 8:a3b41ec82e63 100 Test::assertTrue(runIteration());
mfiore 8:a3b41ec82e63 101 Test::end();
mfiore 7:08b474178245 102 }
mfiore 13:45b7e18e33ab 103
mfiore 13:45b7e18e33ab 104 radio->disconnect();
mfiore 7:08b474178245 105 }
mfiore 7:08b474178245 106
mfiore 8:a3b41ec82e63 107 bool TestTCPSocketConnection::runIteration()
mfiore 8:a3b41ec82e63 108 {
mfiore 7:08b474178245 109 Timer tmr;
mfiore 7:08b474178245 110 int bytesRead = 0;
mfiore 7:08b474178245 111 const int readSize = 1024;
mfiore 7:08b474178245 112 char buffer[readSize] = {0};
mfiore 7:08b474178245 113 string result;
mfiore 8:a3b41ec82e63 114
mfiore 7:08b474178245 115 for (int i = 0; i < 5; i++) {
mfiore 7:08b474178245 116 if (i >= 5) {
mfiore 7:08b474178245 117 return false;
mfiore 7:08b474178245 118 }
mfiore 9:b2e3862705fc 119 if (! sock->connect(TCP_TEST_SERVER, TCP_TEST_PORT)) {
mfiore 7:08b474178245 120 break;
mfiore 7:08b474178245 121 } else {
mfiore 7:08b474178245 122 wait(1);
mfiore 7:08b474178245 123 }
mfiore 7:08b474178245 124 }
mfiore 8:a3b41ec82e63 125
mfiore 8:a3b41ec82e63 126 logInfo("Receiving Menu");
mfiore 8:a3b41ec82e63 127 tmr.reset();
mfiore 8:a3b41ec82e63 128 tmr.start();
mfiore 8:a3b41ec82e63 129 do {
mfiore 8:a3b41ec82e63 130 bytesRead = sock->receive(buffer, readSize);
mfiore 8:a3b41ec82e63 131 if (bytesRead > 0) {
mfiore 8:a3b41ec82e63 132 result.append(buffer, bytesRead);
mfiore 8:a3b41ec82e63 133 }
mfiore 8:a3b41ec82e63 134 logInfo("Total Bytes Read: %d", result.size());
Vanger 16:dbe80ac199f5 135 if(result.find(MENU) != std::string::npos) {
Vanger 14:7643ed024fe8 136 break;
Vanger 14:7643ed024fe8 137 }
Vanger 14:7643ed024fe8 138 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 139
mfiore 8:a3b41ec82e63 140 logInfo("Received: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 141
mfiore 8:a3b41ec82e63 142 size_t pos = result.find(MENU_LINE1);
mfiore 8:a3b41ec82e63 143 if(pos != string::npos) {
mfiore 8:a3b41ec82e63 144 logInfo("Found Menu 1st Line");
mfiore 8:a3b41ec82e63 145 } else {
mfiore 8:a3b41ec82e63 146 logError("Failed To Find Menu 1st Line");
mfiore 8:a3b41ec82e63 147 sock->close();
mfiore 8:a3b41ec82e63 148 return false;
mfiore 8:a3b41ec82e63 149 }
mfiore 8:a3b41ec82e63 150
mfiore 8:a3b41ec82e63 151 result.clear();
mfiore 8:a3b41ec82e63 152
mfiore 8:a3b41ec82e63 153 logInfo("Writing To Socket: 2");
mfiore 8:a3b41ec82e63 154 if(sock->send("2\r\n", 3) == 3) {
mfiore 8:a3b41ec82e63 155 logInfo("Successfully Wrote '2'");
mfiore 8:a3b41ec82e63 156 } else {
mfiore 8:a3b41ec82e63 157 logError("Failed To Write '2'");
mfiore 8:a3b41ec82e63 158 sock->close();
mfiore 8:a3b41ec82e63 159 return false;
mfiore 8:a3b41ec82e63 160 }
mfiore 8:a3b41ec82e63 161 logInfo("Expecting 'how many ? >:'");
mfiore 8:a3b41ec82e63 162 tmr.reset();
mfiore 8:a3b41ec82e63 163 tmr.start();
mfiore 8:a3b41ec82e63 164 do {
mfiore 8:a3b41ec82e63 165 bytesRead = sock->receive(buffer, readSize);
mfiore 8:a3b41ec82e63 166 if (bytesRead > 0) {
mfiore 8:a3b41ec82e63 167 result.append(buffer, bytesRead);
mfiore 8:a3b41ec82e63 168 }
mfiore 8:a3b41ec82e63 169 logInfo("Total Bytes Read: %d", result.size());
Vanger 14:7643ed024fe8 170 if(result.find("how many") != std::string::npos) {
Vanger 14:7643ed024fe8 171 break;
Vanger 14:7643ed024fe8 172 }
Vanger 14:7643ed024fe8 173 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 174
mfiore 8:a3b41ec82e63 175 logInfo("Received: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 176
mfiore 8:a3b41ec82e63 177 if(result.find("how many") != std::string::npos) {
mfiore 8:a3b41ec82e63 178 logInfo("Successfully Found 'how many'");
mfiore 8:a3b41ec82e63 179 logInfo("Writing To Socket: 2");
mfiore 8:a3b41ec82e63 180 if(sock->send("2\r\n", 3) == 3) {
mfiore 8:a3b41ec82e63 181 logInfo("Successfully wrote '2'");
mfiore 8:a3b41ec82e63 182 } else {
mfiore 8:a3b41ec82e63 183 logError("Failed to write '2'");
mfiore 8:a3b41ec82e63 184 sock->close();
mfiore 8:a3b41ec82e63 185 return false;
mfiore 8:a3b41ec82e63 186 }
mfiore 8:a3b41ec82e63 187 } else {
mfiore 8:a3b41ec82e63 188 logError("didn't receive 'how many'");
mfiore 8:a3b41ec82e63 189 sock->close();
mfiore 8:a3b41ec82e63 190 return false;
mfiore 8:a3b41ec82e63 191 }
mfiore 8:a3b41ec82e63 192
mfiore 8:a3b41ec82e63 193 result.clear();
mfiore 8:a3b41ec82e63 194
mfiore 7:08b474178245 195 logInfo("Receiving Data");
mfiore 7:08b474178245 196 tmr.reset();
mfiore 7:08b474178245 197 tmr.start();
mfiore 7:08b474178245 198 do {
mfiore 7:08b474178245 199 bytesRead = sock->receive(buffer, readSize);
mfiore 7:08b474178245 200 if (bytesRead > 0) {
mfiore 7:08b474178245 201 result.append(buffer, bytesRead);
mfiore 7:08b474178245 202 }
mfiore 7:08b474178245 203 logInfo("Total Bytes Read: %d", result.size());
Vanger 14:7643ed024fe8 204 if(result.size() >= 1645) {
Vanger 14:7643ed024fe8 205 break;
Vanger 14:7643ed024fe8 206 }
Vanger 14:7643ed024fe8 207 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 208
mfiore 7:08b474178245 209 logInfo("Received Data: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 210
mfiore 7:08b474178245 211 pos = result.find(PATTERN_LINE1);
mfiore 7:08b474178245 212 if(pos != string::npos) {
mfiore 7:08b474178245 213 int patternSize = sizeof(PATTERN) - 1;
mfiore 7:08b474178245 214 const char* ptr = &result.data()[pos];
mfiore 7:08b474178245 215 bool match = true;
mfiore 7:08b474178245 216 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 217 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 218 logError("1st Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 219 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 220 match = false;
mfiore 8:a3b41ec82e63 221 break;
mfiore 7:08b474178245 222 }
mfiore 7:08b474178245 223 }
mfiore 7:08b474178245 224 if(match) {
mfiore 8:a3b41ec82e63 225 logInfo("Found 1st Pattern");
mfiore 8:a3b41ec82e63 226 } else {
mfiore 8:a3b41ec82e63 227 logError("Failed To Find 1st Pattern");
mfiore 8:a3b41ec82e63 228 sock->close();
mfiore 8:a3b41ec82e63 229 return false;
mfiore 7:08b474178245 230 }
mfiore 8:a3b41ec82e63 231
mfiore 7:08b474178245 232 pos = result.find(PATTERN_LINE1, pos + patternSize);
mfiore 7:08b474178245 233 if(pos != std::string::npos) {
mfiore 7:08b474178245 234 ptr = &result.data()[pos];
mfiore 7:08b474178245 235 match = true;
mfiore 7:08b474178245 236 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 237 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 238 logError("2nd Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 239 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 240 match = false;
mfiore 8:a3b41ec82e63 241 break;
mfiore 7:08b474178245 242 }
mfiore 7:08b474178245 243 }
mfiore 7:08b474178245 244 if(match) {
mfiore 8:a3b41ec82e63 245 logInfo("Found 2nd Pattern");
mfiore 8:a3b41ec82e63 246 } else {
mfiore 8:a3b41ec82e63 247 logError("Failed To Find 2nd Pattern");
mfiore 8:a3b41ec82e63 248 sock->close();
mfiore 8:a3b41ec82e63 249 return false;
mfiore 7:08b474178245 250 }
mfiore 7:08b474178245 251 }
mfiore 8:a3b41ec82e63 252 } else {
mfiore 8:a3b41ec82e63 253 logError("Failed To Find Pattern 1st Line");
mfiore 8:a3b41ec82e63 254 sock->close();
mfiore 8:a3b41ec82e63 255 return false;
mfiore 7:08b474178245 256 }
mfiore 8:a3b41ec82e63 257
mfiore 7:08b474178245 258 result.clear();
mfiore 7:08b474178245 259 sock->close();
Vanger 14:7643ed024fe8 260 radio->disconnect();
mfiore 7:08b474178245 261 return true;
mfiore 7:08b474178245 262 }
mfiore 7:08b474178245 263
mfiore 7:08b474178245 264 #endif