UDP_server example

Fork of mbed-os-example-udp-sockets by mbed_example

Revision:
1:22cf1a32372f
Parent:
0:cf516d904427
diff -r cf516d904427 -r 22cf1a32372f main.cpp
--- a/main.cpp	Mon Oct 23 21:26:36 2017 +0000
+++ b/main.cpp	Mon Dec 18 11:22:30 2017 +0000
@@ -1,48 +1,152 @@
+#define REMOTE_PORT 49100
+#define LOCAL_PORT 49101
+#define BUF_SIZE 8
+
 #include "mbed.h"
 #include "EthernetInterface.h"
- 
+#include <string>
+
 // Network interface
 EthernetInterface net;
- 
-// Time protocol implementation : Address: time.nist.gov UDPPort: 37  
- 
-typedef struct {
-    uint32_t secs;         // Transmit Time-stamp seconds.
-}ntp_packet;
- 
-int main() {
+SocketAddress client;
+UDPSocket server;
+
+//Threads
+Thread recv_thread;
+Thread send_thread;
+
+//Functions
+void onReceive( void );
+void onStream ( void );
+
+void captureImg( void );
+
+//Fields
+char in_data[BUF_SIZE];
+bool is_stopped;
+string client_IP_addr;
+
+
+int width = 640;
+int height = 480;
+std::string datagram = "";
+
+int main()
+{
     // Bring up the ethernet interface
     printf("UDP Socket example\n");
     if(0 != net.connect()) {
         printf("Error connecting\n");
         return -1;
     }
- 
+
     // Show the network address
     const char *ip = net.get_ip_address();
     printf("IP address is: %s\n", ip ? ip : "No IP");
-        
-    UDPSocket sock(&net);
-    SocketAddress sockAddr;
- 
-    char out_buffer[] = "time";
-    if(0 > sock.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer))) {
-        printf("Error sending data\n");
-        return -1;
+
+    server.open(&net);
+    int err = server.bind(LOCAL_PORT);
+    printf("Port status is: %d\n",err);
+
+    recv_thread.start(onReceive);
+
+    printf("listening has been started\n");
+    printf("\n");
+
+    while(1) {
+        wait(1);
+    }
+}
+
+void onReceive()
+{
+    int bytes;
+    while(1) {
+        memset(in_data, 0, sizeof(in_data));
+        bytes = server.recvfrom(&client, &in_data, BUF_SIZE);
+
+        printf("\n");
+        printf("bytes received: %d\n",bytes);
+        printf("string: %s\n",in_data);
+        client_IP_addr = client.get_ip_address();
+        printf("client address: %s\n", client_IP_addr.c_str());
+        printf("\n");
+
+        if (strcmp(in_data,"RUN") == 0) {
+            send_thread.start(onStream);
+
+            is_stopped = false;
+            send_thread.signal_set(0x1);
+
+            printf("stream on\n");
+
+        } else if (strcmp(in_data,"STP") == 0) {
+            is_stopped = true;
+            printf("stream off\n");
+        }
     }
-    
-    ntp_packet in_data;
-    int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
-    in_data.secs = ntohl( in_data.secs ) - 2208988800;    // 1900-1970
-    printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n", 
-                        (uint32_t)in_data.secs);
-    printf("Time = %s", ctime(( const time_t* )&in_data.secs));
-    
-    printf("Time Server Address: %s Port: %d\n\r", 
-                               sockAddr.get_ip_address(), sockAddr.get_port());
-    
-    // Close the socket and bring down the network interface
-    sock.close();
-    net.disconnect();
-    return 0;
+}
+
+void onStream()
+{
+    while(1) {
+        if(is_stopped) Thread::signal_wait(0x1);
+
+        /*/if(0 > server.sendto(client_IP_addr.c_str(), REMOTE_PORT, "TEST_STRING", sizeof("TEST_STRING"))) {
+            printf("error sending data\n");
+        } else printf("data has been sent\n");*/
+        captureImg();
+        //printf("shot\n");
+
+        wait(1);
+    }
+}
+
+/*void captureImg()
+{
+    bool isNew = false;
+    datagram = "S";
+    for(int x = 0; x < width; x++) {
+        datagram += "FF";
+    }
+    //server.sendto(client_IP_addr.c_str(), REMOTE_PORT, datagram.c_str(), strlen(datagram.c_str()));
+    //printf("strlen: %d\n",strlen(datagram.c_str()));
+
+    for(int y = 1; y < height; y++) {
+        if(isNew) {
+            datagram = "R";
+            isNew = false;
+        } else datagram += "R";
+
+        for(int x = 0; x < width; x++) {
+            if((y%2) != 0) datagram += "FF";
+            else datagram += "00";
+        }
+
+        if(((y+1)%30) == 0) {
+            if(0 > server.sendto(client_IP_addr.c_str(), REMOTE_PORT, datagram.c_str(), strlen(datagram.c_str()))) printf("data hasn't been sent\n");
+            else  printf("strlen: %d\n",strlen(datagram.c_str()));
+
+            isNew = true;
+        }
+    }
+}*/
+
+void captureImg()
+{
+    datagram = "S";
+    for(int x = 0; x < width; x++) {
+        datagram += "FF";
+    }
+    server.sendto(client_IP_addr.c_str(), REMOTE_PORT, datagram.c_str(), strlen(datagram.c_str()));
+    //printf("strlen: %d\n",strlen(datagram.c_str()));
+
+    for(int y = 1; y < height; y++) {
+        datagram = "R";
+        for(int x = 0; x < width; x++) {
+            if((y%2) != 0) datagram += "FF";
+            else datagram += "00";
+        }
+        server.sendto(client_IP_addr.c_str(), REMOTE_PORT, datagram.c_str(), strlen(datagram.c_str()));
+    }
 }
\ No newline at end of file