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 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 #include <list>
00005 #include <string>
00006 
00007 /**
00008 * How to use:
00009 * make.py -m LPC1768 -t ARM -d E:\ -n NET_14
00010 * udp_link_layer_auto.py -p COM20 -d E:\ -t 10
00011 */
00012 
00013 // Evil globals
00014 namespace {
00015     // IP and port used to store HOST address info
00016     char host_address[32] = {0};
00017     volatile int host_port = 0;
00018 
00019     const int ECHO_SERVER_PORT = 7;
00020     const int BUFFER_SIZE = 64;
00021 
00022     // Forwarding packet queue
00023     std::list<std::string> datagram_queue;
00024 
00025     // Statistics (mbed)
00026     volatile int received_packets = 0;
00027     volatile int forwarded_packets = 0;
00028     volatile int max_queue_len = 0;
00029 
00030     Mutex cli_serv_mutex;
00031     // cli_serv_mutex.lock(); // LOCK
00032     // cli_serv_mutex.unlock(); // LOCK
00033 }
00034 
00035 void udp_server_task(void const *argument)
00036 {
00037     DigitalOut indicator(LED1);
00038     UDPSocket server;
00039 
00040     server.bind(ECHO_SERVER_PORT);
00041     // printf("[udp_server_task] Start\r\n");
00042 
00043     Endpoint client;
00044     char buffer[BUFFER_SIZE] = { 0 };
00045     while (true) {
00046         //printf("[udp_server_task] Wait for packet...\r\n");
00047         int n = server.receiveFrom(client, buffer, sizeof(buffer));
00048         if (n > 0) {
00049             //printf("[udp_server_task] Received packet from: %s\r\n", client.get_address());
00050             const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n;
00051             buffer[buffer_string_end_index] = '\0';
00052             //printf("[udp_server_task] Server received: %s\r\n", buffer);
00053             if (host_port == 0) {
00054                 strcpy(host_address, client.get_address());
00055                 host_port = ECHO_SERVER_PORT + 1;
00056                 //printf("[udp_server_task] Set host address and port: %s:%d\r\n", host_address, host_port);
00057             }
00058             // Dispatch data to client for sending to test HOST
00059             cli_serv_mutex.lock(); // LOCK
00060             // Push to datagram queue
00061             datagram_queue.push_front(std::string(buffer));
00062             max_queue_len = datagram_queue.size() > max_queue_len ? datagram_queue.size() : max_queue_len;
00063             received_packets++;
00064             cli_serv_mutex.unlock(); // LOCK
00065             indicator = !indicator;
00066         }
00067     }
00068 }
00069 
00070 void udp_client_task(void const *argument)
00071 {
00072     while (host_port == 0) {
00073         // Waiting for HOST port notification
00074     }
00075 
00076     DigitalOut indicator(LED2);
00077     UDPSocket socket;
00078     socket.init();
00079 
00080     Endpoint echo_server;
00081     echo_server.set_address(host_address, host_port);
00082     //printf("[udp_client_task] Start: %s:%d\r\n", host_address, host_port);
00083 
00084     while (1) {
00085         std::string datagram;
00086         bool sent_datagram = false;
00087         cli_serv_mutex.lock(); // LOCK
00088         if (datagram_queue.size() > 0) {
00089             // POP from datagram queue
00090             datagram = datagram_queue.back();
00091             datagram_queue.pop_back();
00092             sent_datagram = true;
00093         }
00094         cli_serv_mutex.unlock(); // LOCK
00095         if (sent_datagram) {
00096             //printf("[udp_client_task] Forwarded datagram: %s\r\n", datagram.c_str());
00097             socket.sendTo(echo_server, (char *)datagram.c_str(), datagram.length());
00098             forwarded_packets++;
00099             indicator = !indicator;
00100         }
00101     }
00102 }
00103 
00104 int main(void)
00105 {
00106     EthernetInterface eth;
00107 
00108     eth.init(); //Use DHCP
00109     eth.connect();
00110     printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT);
00111 
00112     Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);
00113     Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);
00114 
00115     // Control TCP server to get MBED statistics
00116     {
00117         char buffer[BUFFER_SIZE] = {0};
00118         const int TELNET_SERVER_PORT = 23;
00119         const int BUFFER_SIZE = 256;
00120         TCPSocketServer server;
00121         server.bind(TELNET_SERVER_PORT);
00122         server.listen();
00123 
00124         while (true) {
00125             printf("MBED: Wait for new connection...\r\n");
00126             TCPSocketConnection client;
00127             server.accept(client);
00128             client.set_blocking(false, 1500); // Timeout after (1.5)s
00129             printf("MBED: Connection from: %s\r\n", client.get_address());
00130 
00131             while (true) {
00132                 int n = client.receive(buffer, sizeof(buffer));
00133                 //if (n <= 0) break;
00134                 if (n > 0) {
00135                     // printf("Recv %d chars\r\n", n);
00136                     const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n;
00137                     buffer[buffer_string_end_index] = '\0';
00138                     // client.send_all(buffer, strlen(buffer));
00139                     if (strncmp(buffer, "stat", 4) == 0) {
00140                         sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d",
00141                                 received_packets, forwarded_packets, max_queue_len);
00142                         client.send_all(buffer, strlen(buffer));
00143                         // printf("%s", buffer);
00144                     }
00145                 }
00146                 //if (n <= 0) break;
00147             }
00148             client.close();
00149         }
00150     }
00151 }