increased chunk size

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by MultiTech

Committer:
mfiore
Date:
Fri Aug 22 16:16:13 2014 +0000
Revision:
17:cbfa7314af15
Parent:
16:dbe80ac199f5
Parent:
13:45b7e18e33ab
Child:
35:c85bea9cb713
merge changes

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