WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #if !FEATURE_IPV4
00002     #error [NOT_SUPPORTED] IPV4 not supported for this target
00003 #endif
00004 
00005 #include "mbed.h"
00006 #include "EthernetInterface.h"
00007 #include "UDPSocket.h"
00008 #include "greentea-client/test_env.h"
00009 
00010 #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE
00011 #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 256
00012 #endif
00013 
00014 namespace {
00015     char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
00016     char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
00017     const char ASCII_MAX = '~' - ' ';
00018     const int ECHO_LOOPS = 16;
00019 }
00020 
00021 void prep_buffer(char *tx_buffer, size_t tx_size) {
00022     for (size_t i=0; i<tx_size; ++i) {
00023         tx_buffer[i] = (rand() % 10) + '0';
00024     }
00025 }
00026 
00027 int main() {
00028     GREENTEA_SETUP(20, "udp_echo_client");
00029 
00030     EthernetInterface eth;
00031     eth.connect();
00032     printf("UDP client IP Address is %s\n", eth.get_ip_address());
00033 
00034     greentea_send_kv("target_ip", eth.get_ip_address());
00035 
00036     bool result = true;
00037 
00038     char recv_key[] = "host_port";
00039     char ipbuf[60] = {0};
00040     char portbuf[16] = {0};
00041     unsigned int port = 0;
00042 
00043     UDPSocket sock;
00044     sock.open(&eth);
00045 
00046     greentea_send_kv("host_ip", " ");
00047     greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
00048 
00049     greentea_send_kv("host_port", " ");
00050     greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
00051     sscanf(portbuf, "%u", &port);
00052 
00053     printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
00054 
00055     SocketAddress addr(ipbuf, port);
00056 
00057     for (int i=0; i < ECHO_LOOPS; ++i) {
00058         prep_buffer(tx_buffer, sizeof(tx_buffer));
00059         const int ret = sock.sendto(addr, tx_buffer, sizeof(tx_buffer));
00060         printf("[%02d] sent...%d Bytes \n", i, ret);
00061 
00062         const int n = sock.recvfrom(&addr, rx_buffer, sizeof(rx_buffer));
00063         printf("[%02d] recv...%d Bytes \n", i, n);
00064 
00065         if (memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer))) {
00066             result = false;
00067             break;
00068         }
00069     }
00070 
00071     sock.close();
00072     eth.disconnect();
00073     GREENTEA_TESTSUITE_RESULT(result);
00074 }