diff options
Diffstat (limited to 'tool/mbed/mbed-sdk/libraries/tests/net/helloworld')
6 files changed, 240 insertions, 0 deletions
diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_receive/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_receive/main.cpp new file mode 100644 index 0000000000..9cee345519 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_receive/main.cpp @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +const int BROADCAST_PORT = 58083; + +int main() { + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + UDPSocket socket; + socket.bind(BROADCAST_PORT); + socket.set_broadcasting(); + + Endpoint broadcaster; + char buffer[256]; + while (true) { + printf("\nWait for packet...\n"); + int n = socket.receiveFrom(broadcaster, buffer, sizeof(buffer)); + buffer[n] = '\0'; + printf("Packet from \"%s\": %s\n", broadcaster.get_address(), buffer); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_send/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_send/main.cpp new file mode 100644 index 0000000000..4632c63ff0 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/broadcast_send/main.cpp @@ -0,0 +1,25 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +const int BROADCAST_PORT = 58083; + +int main() { + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + UDPSocket sock; + sock.init(); + sock.set_broadcasting(); + + Endpoint broadcast; + broadcast.set_address("255.255.255.255", BROADCAST_PORT); + + char out_buffer[] = "very important data"; + + while (true) { + printf("Broadcasting...\n"); + sock.sendTo(broadcast, out_buffer, sizeof(out_buffer)); + Thread::wait(1000); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_receive/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_receive/main.cpp new file mode 100644 index 0000000000..0398bdd1fd --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_receive/main.cpp @@ -0,0 +1,27 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +const char* MCAST_GRP = "224.1.1.1"; +const int MCAST_PORT = 5007; + +int main() { + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + UDPSocket server; + server.bind(MCAST_PORT); + if (server.join_multicast_group(MCAST_GRP) != 0) { + printf("Error joining the multicast group\n"); + while (true) {} + } + + Endpoint client; + char buffer[256]; + while (true) { + printf("\nWait for packet...\n"); + int n = server.receiveFrom(client, buffer, sizeof(buffer)); + + printf("Packet from \"%s\": %s\n", client.get_address(), buffer); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_send/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_send/main.cpp new file mode 100644 index 0000000000..df41c6d80a --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/multicast_send/main.cpp @@ -0,0 +1,24 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +const char* MCAST_GRP = "224.1.1.1"; +const int MCAST_PORT = 5007; + +int main() { + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + UDPSocket sock; + sock.init(); + + Endpoint multicast_group; + multicast_group.set_address(MCAST_GRP, MCAST_PORT); + + char out_buffer[] = "very important data"; + while (true) { + printf("Multicast to group: %s\n", MCAST_GRP); + sock.sendTo(multicast_group, out_buffer, sizeof(out_buffer)); + Thread::wait(1000); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/tcpclient/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/tcpclient/main.cpp new file mode 100644 index 0000000000..303f547aa1 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/tcpclient/main.cpp @@ -0,0 +1,85 @@ +#include <algorithm> +#include "mbed.h" +#include "EthernetInterface.h" +#include "test_env.h" + +namespace { + // Test connection information + const char *HTTP_SERVER_NAME = "developer.mbed.org"; + const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt"; + const int HTTP_SERVER_PORT = 80; + const int RECV_BUFFER_SIZE = 512; + + // Test related data + const char *HTTP_OK_STR = "200 OK"; + const char *HTTP_HELLO_STR = "Hello world!"; + + // Test buffers + char buffer[RECV_BUFFER_SIZE] = {0}; +} + +bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { + const char *f = std::search(first, last, s_first, s_last); + return (f != last); +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(TCP client hello world); + MBED_HOSTTEST_START("NET_1"); + + bool result = false; + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + printf("TCP client IP Address is %s\r\n", eth.getIPAddress()); + + TCPSocketConnection sock; + if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { + printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); + + // We are constructing GET command like this: + // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n + strcpy(buffer, "GET http://"); + strcat(buffer, HTTP_SERVER_NAME); + strcat(buffer, HTTP_SERVER_FILE_PATH); + strcat(buffer, " HTTP/1.0\n\n"); + // Send GET command + sock.send_all(buffer, strlen(buffer)); + + // Server will respond with HTTP GET's success code + bool found_200_ok = false; + { + const int ret = sock.receive(buffer, sizeof(buffer) - 1); + buffer[ret] = '\0'; + // Find 200 OK HTTP status in reply + found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); + printf("HTTP: Received %d chars from server\r\n", ret); + printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); + printf("HTTP: Received massage:\r\n\r\n"); + printf("%s", buffer); + } + + // Server will respond with requested file content + bool found_hello = false; + { + const int ret = sock.receive(buffer, sizeof(buffer) - 1); + buffer[ret] = '\0'; + // Find Hello World! in reply + found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); + printf("HTTP: Received %d chars from server\r\n", ret); + printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); + printf("HTTP: Received massage:\r\n\r\n"); + printf("%s", buffer); + } + + if (found_200_ok && found_hello) { + result = true; + } + } + + sock.close(); + eth.disconnect(); + MBED_HOSTTEST_RESULT(result); +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/udpclient/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/udpclient/main.cpp new file mode 100644 index 0000000000..59dad6db6f --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/udpclient/main.cpp @@ -0,0 +1,56 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "test_env.h" + +namespace { + const char *HTTP_SERVER_NAME = "utcnist.colorado.edu"; + const int HTTP_SERVER_PORT = 37; + const float YEARS_TO_PASS = 114.0; +} + + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(NIST Internet Time Service); + MBED_HOSTTEST_START("NET_2"); + + bool result = false; + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + printf("UDP client IP Address is %s\n", eth.getIPAddress()); + + UDPSocket sock; + sock.init(); + + Endpoint nist; + nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT); + + char out_buffer[] = "plop"; // Does not matter + sock.sendTo(nist, out_buffer, sizeof(out_buffer)); + + union { + char in_buffer_tab[4]; + unsigned int in_buffer_uint; + }; + + const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab)); + if (n > 0) { + result = true; + const unsigned int timeRes = ntohl(in_buffer_uint); + const float years = timeRes / 60.0 / 60.0 / 24.0 / 365.0; + const float days = timeRes / 24.0 / 60.0 / 60.0; + printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port()); + printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]"); + printf("UDP: %.2f days since 01/01/1900 00:00 GMT ... %s\r\n", days, timeRes > 0 ? "[OK]" : "[FAIL]"); + printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]"); + + if (years < YEARS_TO_PASS) { + result = false; + } + } + sock.close(); + eth.disconnect(); + MBED_HOSTTEST_RESULT(result); +} |