Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MTS-Socket by
Test/TestUDPSocket.h@14:7643ed024fe8, 2014-07-23 (annotated)
- Committer:
- Vanger
- Date:
- Wed Jul 23 16:01:16 2014 +0000
- Revision:
- 14:7643ed024fe8
- Parent:
- 12:9efe3cc3cb6c
- Child:
- 16:dbe80ac199f5
Tweaked response checking in TestTCPSocketConnection.h to exit early if correct response found. ; TestUDPSocket.h added socket close and disconnect
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mfiore | 9:b2e3862705fc | 1 | #ifndef TESTUDPSOCKET_H |
| mfiore | 9:b2e3862705fc | 2 | #define TESTUDPSOCKET_H |
| mfiore | 9:b2e3862705fc | 3 | |
| mfiore | 9:b2e3862705fc | 4 | #include "mtsas.h" |
| mfiore | 9:b2e3862705fc | 5 | |
| mfiore | 9:b2e3862705fc | 6 | using namespace mts; |
| mfiore | 9:b2e3862705fc | 7 | |
| mfiore | 9:b2e3862705fc | 8 | class TestUDPSocket : public TestCollection |
| mfiore | 9:b2e3862705fc | 9 | { |
| mfiore | 9:b2e3862705fc | 10 | public: |
| mfiore | 9:b2e3862705fc | 11 | TestUDPSocket(); |
| mfiore | 9:b2e3862705fc | 12 | virtual void run(); |
| mfiore | 9:b2e3862705fc | 13 | |
| mfiore | 9:b2e3862705fc | 14 | private: |
| mfiore | 9:b2e3862705fc | 15 | MTSSerialFlowControl* io; |
| mfiore | 9:b2e3862705fc | 16 | Cellular* radio; |
| mfiore | 9:b2e3862705fc | 17 | UDPSocket* sock; |
| mfiore | 9:b2e3862705fc | 18 | }; |
| mfiore | 9:b2e3862705fc | 19 | |
| Vanger | 14:7643ed024fe8 | 20 | //Must set test server and test port before running socket test |
| mfiore | 9:b2e3862705fc | 21 | const char UDP_TEST_SERVER[] = ""; |
| mfiore | 9:b2e3862705fc | 22 | const int UDP_TEST_PORT = -1; |
| mfiore | 9:b2e3862705fc | 23 | |
| Vanger | 14:7643ed024fe8 | 24 | //Test pattern can be changed to any alphanumeric pattern wanted |
| mfiore | 9:b2e3862705fc | 25 | const char TEST_PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
| mfiore | 9:b2e3862705fc | 26 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
| mfiore | 9:b2e3862705fc | 27 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
| mfiore | 9:b2e3862705fc | 28 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
| mfiore | 9:b2e3862705fc | 29 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
| mfiore | 9:b2e3862705fc | 30 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
| mfiore | 9:b2e3862705fc | 31 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
| mfiore | 9:b2e3862705fc | 32 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
| mfiore | 9:b2e3862705fc | 33 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n"; |
| mfiore | 9:b2e3862705fc | 34 | |
| mfiore | 9:b2e3862705fc | 35 | TestUDPSocket::TestUDPSocket() : TestCollection("TestUDPSocket") {} |
| mfiore | 9:b2e3862705fc | 36 | |
| mfiore | 9:b2e3862705fc | 37 | void TestUDPSocket::run() { |
| mfiore | 10:42220b7df921 | 38 | const char APN[] = ""; |
| mfiore | 10:42220b7df921 | 39 | |
| mfiore | 9:b2e3862705fc | 40 | Endpoint remote; |
| mfiore | 9:b2e3862705fc | 41 | remote.set_address(UDP_TEST_SERVER, UDP_TEST_PORT); |
| mfiore | 9:b2e3862705fc | 42 | |
| mfiore | 9:b2e3862705fc | 43 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
| mfiore | 9:b2e3862705fc | 44 | Test::start("Setup"); |
| mfiore | 9:b2e3862705fc | 45 | io = new MTSSerialFlowControl(D8, D2, D3, D6); |
| mfiore | 9:b2e3862705fc | 46 | io->baud(115200); |
| mfiore | 9:b2e3862705fc | 47 | radio = CellularFactory::create(io); |
| mfiore | 9:b2e3862705fc | 48 | if (! radio) { |
| mfiore | 9:b2e3862705fc | 49 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 50 | } |
| mfiore | 9:b2e3862705fc | 51 | radio->configureSignals(D4, D7, RESET); |
| mfiore | 9:b2e3862705fc | 52 | Transport::setTransport(radio); |
| mfiore | 9:b2e3862705fc | 53 | |
| mfiore | 9:b2e3862705fc | 54 | for (int i = 0; i < 10; i++) { |
| mfiore | 9:b2e3862705fc | 55 | if (i >= 10) { |
| mfiore | 9:b2e3862705fc | 56 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 57 | } |
| mfiore | 12:9efe3cc3cb6c | 58 | if (radio->setApn(APN) == MTS_SUCCESS) { |
| mfiore | 9:b2e3862705fc | 59 | break; |
| mfiore | 9:b2e3862705fc | 60 | } else { |
| mfiore | 9:b2e3862705fc | 61 | wait(1); |
| mfiore | 9:b2e3862705fc | 62 | } |
| mfiore | 9:b2e3862705fc | 63 | } |
| Vanger | 14:7643ed024fe8 | 64 | |
| mfiore | 9:b2e3862705fc | 65 | for (int i = 0; i < 3; i++) { |
| mfiore | 9:b2e3862705fc | 66 | if (i >= 3) { |
| mfiore | 9:b2e3862705fc | 67 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 68 | } |
| mfiore | 9:b2e3862705fc | 69 | if (radio->connect()) { |
| mfiore | 9:b2e3862705fc | 70 | break; |
| mfiore | 9:b2e3862705fc | 71 | } else { |
| mfiore | 9:b2e3862705fc | 72 | wait(1); |
| mfiore | 9:b2e3862705fc | 73 | } |
| mfiore | 9:b2e3862705fc | 74 | } |
| mfiore | 9:b2e3862705fc | 75 | |
| mfiore | 9:b2e3862705fc | 76 | for (int i = 0; i < 5; i++) { |
| mfiore | 9:b2e3862705fc | 77 | if (i >= 5) { |
| mfiore | 9:b2e3862705fc | 78 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 79 | } |
| mfiore | 9:b2e3862705fc | 80 | if (radio->ping()) { |
| mfiore | 9:b2e3862705fc | 81 | break; |
| mfiore | 9:b2e3862705fc | 82 | } else { |
| mfiore | 9:b2e3862705fc | 83 | wait(1); |
| mfiore | 9:b2e3862705fc | 84 | } |
| mfiore | 9:b2e3862705fc | 85 | } |
| mfiore | 9:b2e3862705fc | 86 | |
| mfiore | 9:b2e3862705fc | 87 | sock = new UDPSocket(); |
| Vanger | 14:7643ed024fe8 | 88 | sock->set_blocking(false, 20000); |
| mfiore | 9:b2e3862705fc | 89 | Test::end(); |
| mfiore | 9:b2e3862705fc | 90 | |
| Vanger | 14:7643ed024fe8 | 91 | /** set up an UDP echo server using netcat |
| mfiore | 9:b2e3862705fc | 92 | * nc -e /bin/cat -l -u -p TEST_PORT |
| Vanger | 14:7643ed024fe8 | 93 | * this should echo back anything you send to it. |
| Vanger | 14:7643ed024fe8 | 94 | * Note: Listen port might close after each UDP packet |
| Vanger | 14:7643ed024fe8 | 95 | * if testing UIP-type radios. |
| mfiore | 9:b2e3862705fc | 96 | */ |
| mfiore | 9:b2e3862705fc | 97 | |
| mfiore | 9:b2e3862705fc | 98 | int bufsize = 1024; |
| mfiore | 9:b2e3862705fc | 99 | char buf[bufsize]; |
| mfiore | 9:b2e3862705fc | 100 | int size = 0; |
| mfiore | 9:b2e3862705fc | 101 | |
| mfiore | 9:b2e3862705fc | 102 | for (int i = 0; i < 10; i++) { |
| mfiore | 9:b2e3862705fc | 103 | Test::start("Test UDP"); |
| mfiore | 9:b2e3862705fc | 104 | logInfo("sending to remote"); |
| mfiore | 9:b2e3862705fc | 105 | size = sock->sendTo(remote, (char*) TEST_PATTERN, sizeof(TEST_PATTERN)); |
| mfiore | 9:b2e3862705fc | 106 | if (size != sizeof(TEST_PATTERN)) { |
| mfiore | 9:b2e3862705fc | 107 | logError("failed to send - only sent %d bytes", size); |
| mfiore | 9:b2e3862705fc | 108 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 109 | } |
| mfiore | 9:b2e3862705fc | 110 | logInfo("receiving from remote"); |
| mfiore | 9:b2e3862705fc | 111 | size = sock->receiveFrom(remote, buf, bufsize); |
| mfiore | 9:b2e3862705fc | 112 | if (size != sizeof(TEST_PATTERN)) { |
| mfiore | 9:b2e3862705fc | 113 | logError("failed to receive - only received %d bytes", size); |
| mfiore | 9:b2e3862705fc | 114 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 115 | } else { |
| mfiore | 9:b2e3862705fc | 116 | for (int j = 0; j < sizeof(TEST_PATTERN); j++) { |
| mfiore | 9:b2e3862705fc | 117 | if (buf[j] != TEST_PATTERN[j]) { |
| mfiore | 9:b2e3862705fc | 118 | logError("patterns don't match at %d, pattern [%#02X] received [%#02X]", j, TEST_PATTERN[j], buf[j]); |
| mfiore | 9:b2e3862705fc | 119 | Test::assertTrue(false); |
| mfiore | 9:b2e3862705fc | 120 | } |
| mfiore | 9:b2e3862705fc | 121 | } |
| mfiore | 9:b2e3862705fc | 122 | } |
| mfiore | 9:b2e3862705fc | 123 | Test::end(); |
| mfiore | 9:b2e3862705fc | 124 | wait(5); |
| mfiore | 9:b2e3862705fc | 125 | } |
| Vanger | 14:7643ed024fe8 | 126 | sock->close(); |
| Vanger | 14:7643ed024fe8 | 127 | radio->disconnect(); |
| mfiore | 9:b2e3862705fc | 128 | } |
| mfiore | 9:b2e3862705fc | 129 | |
| mfiore | 9:b2e3862705fc | 130 | #endif |
