increased chunk size

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by MultiTech

Committer:
Vanger
Date:
Mon Aug 11 15:43:15 2014 +0000
Revision:
16:dbe80ac199f5
Parent:
15:8b5fbb926688
Child:
17:cbfa7314af15
Removed some unecessary wait() calls in TestTCPSocketConnection.h and TestUDPSocketConnection.h; Changed TestTCPSocketConnection.h to look for whole menu before moving on, rather than just first menu line.

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
Vanger 15:8b5fbb926688 43 const char TCP_TEST_SERVER[] = "204.26.122.5";
Vanger 15:8b5fbb926688 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 12:9efe3cc3cb6c 67 if (radio->setApn(APN) == MTS_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());
Vanger 16:dbe80ac199f5 134 if(result.find(MENU) != std::string::npos) {
Vanger 14:7643ed024fe8 135 break;
Vanger 14:7643ed024fe8 136 }
Vanger 14:7643ed024fe8 137 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 138
mfiore 8:a3b41ec82e63 139 logInfo("Received: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 140
mfiore 8:a3b41ec82e63 141 size_t pos = result.find(MENU_LINE1);
mfiore 8:a3b41ec82e63 142 if(pos != string::npos) {
mfiore 8:a3b41ec82e63 143 logInfo("Found Menu 1st Line");
mfiore 8:a3b41ec82e63 144 } else {
mfiore 8:a3b41ec82e63 145 logError("Failed To Find Menu 1st Line");
mfiore 8:a3b41ec82e63 146 sock->close();
mfiore 8:a3b41ec82e63 147 return false;
mfiore 8:a3b41ec82e63 148 }
mfiore 8:a3b41ec82e63 149
mfiore 8:a3b41ec82e63 150 result.clear();
mfiore 8:a3b41ec82e63 151
mfiore 8:a3b41ec82e63 152 logInfo("Writing To Socket: 2");
mfiore 8:a3b41ec82e63 153 if(sock->send("2\r\n", 3) == 3) {
mfiore 8:a3b41ec82e63 154 logInfo("Successfully Wrote '2'");
mfiore 8:a3b41ec82e63 155 } else {
mfiore 8:a3b41ec82e63 156 logError("Failed To Write '2'");
mfiore 8:a3b41ec82e63 157 sock->close();
mfiore 8:a3b41ec82e63 158 return false;
mfiore 8:a3b41ec82e63 159 }
mfiore 8:a3b41ec82e63 160 logInfo("Expecting 'how many ? >:'");
mfiore 8:a3b41ec82e63 161 tmr.reset();
mfiore 8:a3b41ec82e63 162 tmr.start();
mfiore 8:a3b41ec82e63 163 do {
mfiore 8:a3b41ec82e63 164 bytesRead = sock->receive(buffer, readSize);
mfiore 8:a3b41ec82e63 165 if (bytesRead > 0) {
mfiore 8:a3b41ec82e63 166 result.append(buffer, bytesRead);
mfiore 8:a3b41ec82e63 167 }
mfiore 8:a3b41ec82e63 168 logInfo("Total Bytes Read: %d", result.size());
Vanger 14:7643ed024fe8 169 if(result.find("how many") != std::string::npos) {
Vanger 14:7643ed024fe8 170 break;
Vanger 14:7643ed024fe8 171 }
Vanger 14:7643ed024fe8 172 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 173
mfiore 8:a3b41ec82e63 174 logInfo("Received: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 175
mfiore 8:a3b41ec82e63 176 if(result.find("how many") != std::string::npos) {
mfiore 8:a3b41ec82e63 177 logInfo("Successfully Found 'how many'");
mfiore 8:a3b41ec82e63 178 logInfo("Writing To Socket: 2");
mfiore 8:a3b41ec82e63 179 if(sock->send("2\r\n", 3) == 3) {
mfiore 8:a3b41ec82e63 180 logInfo("Successfully wrote '2'");
mfiore 8:a3b41ec82e63 181 } else {
mfiore 8:a3b41ec82e63 182 logError("Failed to write '2'");
mfiore 8:a3b41ec82e63 183 sock->close();
mfiore 8:a3b41ec82e63 184 return false;
mfiore 8:a3b41ec82e63 185 }
mfiore 8:a3b41ec82e63 186 } else {
mfiore 8:a3b41ec82e63 187 logError("didn't receive 'how many'");
mfiore 8:a3b41ec82e63 188 sock->close();
mfiore 8:a3b41ec82e63 189 return false;
mfiore 8:a3b41ec82e63 190 }
mfiore 8:a3b41ec82e63 191
mfiore 8:a3b41ec82e63 192 result.clear();
mfiore 8:a3b41ec82e63 193
mfiore 7:08b474178245 194 logInfo("Receiving Data");
mfiore 7:08b474178245 195 tmr.reset();
mfiore 7:08b474178245 196 tmr.start();
mfiore 7:08b474178245 197 do {
mfiore 7:08b474178245 198 bytesRead = sock->receive(buffer, readSize);
mfiore 7:08b474178245 199 if (bytesRead > 0) {
mfiore 7:08b474178245 200 result.append(buffer, bytesRead);
mfiore 7:08b474178245 201 }
mfiore 7:08b474178245 202 logInfo("Total Bytes Read: %d", result.size());
Vanger 14:7643ed024fe8 203 if(result.size() >= 1645) {
Vanger 14:7643ed024fe8 204 break;
Vanger 14:7643ed024fe8 205 }
Vanger 14:7643ed024fe8 206 } while(tmr.read() <= 40);
mfiore 8:a3b41ec82e63 207
mfiore 7:08b474178245 208 logInfo("Received Data: [%d] [%s]", result.size(), result.c_str());
mfiore 8:a3b41ec82e63 209
mfiore 7:08b474178245 210 pos = result.find(PATTERN_LINE1);
mfiore 7:08b474178245 211 if(pos != string::npos) {
mfiore 7:08b474178245 212 int patternSize = sizeof(PATTERN) - 1;
mfiore 7:08b474178245 213 const char* ptr = &result.data()[pos];
mfiore 7:08b474178245 214 bool match = true;
mfiore 7:08b474178245 215 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 216 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 217 logError("1st Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 218 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 219 match = false;
mfiore 8:a3b41ec82e63 220 break;
mfiore 7:08b474178245 221 }
mfiore 7:08b474178245 222 }
mfiore 7:08b474178245 223 if(match) {
mfiore 8:a3b41ec82e63 224 logInfo("Found 1st Pattern");
mfiore 8:a3b41ec82e63 225 } else {
mfiore 8:a3b41ec82e63 226 logError("Failed To Find 1st Pattern");
mfiore 8:a3b41ec82e63 227 sock->close();
mfiore 8:a3b41ec82e63 228 return false;
mfiore 7:08b474178245 229 }
mfiore 8:a3b41ec82e63 230
mfiore 7:08b474178245 231 pos = result.find(PATTERN_LINE1, pos + patternSize);
mfiore 7:08b474178245 232 if(pos != std::string::npos) {
mfiore 7:08b474178245 233 ptr = &result.data()[pos];
mfiore 7:08b474178245 234 match = true;
mfiore 7:08b474178245 235 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 236 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 237 logError("2nd Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 238 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 239 match = false;
mfiore 8:a3b41ec82e63 240 break;
mfiore 7:08b474178245 241 }
mfiore 7:08b474178245 242 }
mfiore 7:08b474178245 243 if(match) {
mfiore 8:a3b41ec82e63 244 logInfo("Found 2nd Pattern");
mfiore 8:a3b41ec82e63 245 } else {
mfiore 8:a3b41ec82e63 246 logError("Failed To Find 2nd Pattern");
mfiore 8:a3b41ec82e63 247 sock->close();
mfiore 8:a3b41ec82e63 248 return false;
mfiore 7:08b474178245 249 }
mfiore 7:08b474178245 250 }
mfiore 8:a3b41ec82e63 251 } else {
mfiore 8:a3b41ec82e63 252 logError("Failed To Find Pattern 1st Line");
mfiore 8:a3b41ec82e63 253 sock->close();
mfiore 8:a3b41ec82e63 254 return false;
mfiore 7:08b474178245 255 }
mfiore 8:a3b41ec82e63 256
mfiore 7:08b474178245 257 result.clear();
mfiore 7:08b474178245 258 sock->close();
Vanger 14:7643ed024fe8 259 radio->disconnect();
mfiore 7:08b474178245 260 return true;
mfiore 7:08b474178245 261 }
mfiore 7:08b474178245 262
mfiore 7:08b474178245 263 #endif