reverted HTTPCLient debug back to defaulted off

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by Keith Ruenheck

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TestUDPSocket.h Source File

TestUDPSocket.h

00001 #ifndef TESTUDPSOCKET_H
00002 #define TESTUDPSOCKET_H
00003 
00004 #include "mtsas.h"
00005 
00006 using namespace mts;
00007 
00008 class TestUDPSocket : public TestCollection
00009 {
00010 public:
00011     TestUDPSocket();
00012     virtual void run();
00013 
00014 private:
00015     MTSSerialFlowControl* io;
00016     Cellular* radio;
00017     UDPSocket* sock;
00018 };
00019 
00020 //Must set test server and test port before running socket test
00021 const char UDP_TEST_SERVER[] = "";
00022 const int UDP_TEST_PORT = -1;
00023 
00024 //Test pattern can be changed to any alphanumeric pattern wanted
00025 const char TEST_PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
00026                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
00027                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
00028                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
00029                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
00030                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
00031                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
00032                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
00033                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n";
00034 
00035 TestUDPSocket::TestUDPSocket() : TestCollection("TestUDPSocket") {}
00036 
00037 void TestUDPSocket::run() {
00038     
00039     Endpoint remote;
00040     remote.set_address(UDP_TEST_SERVER, UDP_TEST_PORT);
00041     
00042     MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
00043     Test::start("Setup");
00044     io = new MTSSerialFlowControl(D8, D2, D3, D6);
00045     io->baud(115200);
00046     radio = CellularFactory::create(io);
00047     if (! radio) {
00048         Test::assertTrue(false);
00049     }
00050     radio->configureSignals(D4, D7, RESET);
00051     Transport::setTransport(radio);
00052 
00053     for (int i = 0; i < 10; i++) {
00054         if (i >= 10) {
00055             Test::assertTrue(false);
00056         }
00057         if (radio->setApn(APN) == MTS_SUCCESS) {
00058             break;
00059         } else {
00060             wait(1);
00061         }
00062     }
00063     
00064     for (int i = 0; i < 3; i++) {
00065         if (i >= 3) {
00066             Test::assertTrue(false);
00067         }
00068         if (radio->connect()) {
00069             break;
00070         } else {
00071             wait(1);
00072         }
00073     }
00074 
00075     for (int i = 0; i < 5; i++) {
00076         if (i >= 5) {
00077             Test::assertTrue(false);
00078         }
00079         if (radio->ping()) {
00080             break;
00081         } else {
00082             wait(1);
00083         }
00084     }
00085 
00086     sock = new UDPSocket();
00087     sock->set_blocking(false, 20000);
00088     Test::end();
00089     
00090     /** set up an UDP echo server using netcat
00091      * nc -e /bin/cat -l -u -p TEST_PORT
00092      * this should echo back anything you send to it.
00093      * Note: Listen port might close after each UDP packet
00094      * if testing UIP-type radios.
00095      */
00096     
00097     int bufsize = 1024;
00098     char buf[bufsize];
00099     int size = 0;
00100      
00101     for (int i = 0; i < 10; i++) {
00102         Test::start("Test UDP");
00103         logInfo("sending to remote");
00104         size = sock->sendTo(remote, (char*) TEST_PATTERN, sizeof(TEST_PATTERN));
00105         if (size != sizeof(TEST_PATTERN)) {
00106             logError("failed to send - only sent %d bytes", size);
00107             Test::assertTrue(false);
00108         }
00109         logInfo("receiving from remote");
00110         size = sock->receiveFrom(remote, buf, bufsize);
00111         if (size != sizeof(TEST_PATTERN)) {
00112             logError("failed to receive - only received %d bytes", size);
00113             Test::assertTrue(false);
00114         } else {
00115             for (int j = 0; j < sizeof(TEST_PATTERN); j++) {
00116                 if (buf[j] != TEST_PATTERN[j]) {
00117                     logError("patterns don't match at %d, pattern [%#02X] received [%#02X]", j, TEST_PATTERN[j], buf[j]);
00118                     Test::assertTrue(false);
00119                 }
00120             }
00121         }
00122         Test::end();
00123     }
00124     sock->close();
00125     radio->disconnect();
00126 }
00127 
00128 #endif