UDP echo with thread

Committer:
JohnnyK
Date:
Sat May 09 11:17:48 2020 +0000
Revision:
5:58fe518111d7
Parent:
4:2579ef3b025f
Child:
6:06a897d0f950
Reworked to NetworkInterface instead of NetworkInterface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 3:9ab431fc54e2 1 #include "mbed.h" //MbedOS 5.15
JohnnyK 5:58fe518111d7 2 #include "NetworkInterface.h"
JohnnyK 5:58fe518111d7 3
JohnnyK 5:58fe518111d7 4 #define BUFFSIZE 50
JohnnyK 0:9f56faada164 5
JohnnyK 2:01b03970605d 6 #define ROUTER
JohnnyK 2:01b03970605d 7 #ifndef ROUTER
JohnnyK 4:2579ef3b025f 8 #define IP "192.168.1.1" //Here place your Static IP of Mbed, when you want it to connect directly to PC
JohnnyK 3:9ab431fc54e2 9 #define GATEWAY "0.0.0.0"
JohnnyK 3:9ab431fc54e2 10 #define MASK "255.255.255.0"
JohnnyK 2:01b03970605d 11 #endif
JohnnyK 5:58fe518111d7 12 #define ADDRESS "192.168.1.10" /*Here place IP of your PC. Run cmd.exe and write command ipconfig.
JohnnyK 5:58fe518111d7 13 If the address will be incorrect, the first message will be lost
JohnnyK 5:58fe518111d7 14 and connection will be established with first received message from opposite side.*/
JohnnyK 3:9ab431fc54e2 15 #define LOCALPORT 20
JohnnyK 3:9ab431fc54e2 16 #define REMOTEPORT 2000
JohnnyK 0:9f56faada164 17
JohnnyK 0:9f56faada164 18 Thread thread;
JohnnyK 0:9f56faada164 19 DigitalOut led1(LED1);
JohnnyK 0:9f56faada164 20 DigitalOut led2(LED2);
JohnnyK 0:9f56faada164 21
JohnnyK 0:9f56faada164 22 void udpEcho(){
JohnnyK 5:58fe518111d7 23 NetworkInterface *net = NetworkInterface::get_default_instance();
JohnnyK 5:58fe518111d7 24 int net_stat;
JohnnyK 2:01b03970605d 25 #ifndef ROUTER
JohnnyK 5:58fe518111d7 26 net->disconnect();
JohnnyK 5:58fe518111d7 27 net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
JohnnyK 5:58fe518111d7 28 printf("set IP status: %i\n",net_stat);
JohnnyK 2:01b03970605d 29 #endif
JohnnyK 5:58fe518111d7 30 net_stat = net->connect();
JohnnyK 5:58fe518111d7 31 printf("connect status: %i\n",net_stat);
JohnnyK 3:9ab431fc54e2 32
JohnnyK 3:9ab431fc54e2 33 SocketAddress ip;
JohnnyK 5:58fe518111d7 34 net->get_ip_address(&ip);
JohnnyK 3:9ab431fc54e2 35 const char *p_ip = ip.get_ip_address();
JohnnyK 3:9ab431fc54e2 36 printf("IP address: %s\n", p_ip ? p_ip : "None");
JohnnyK 3:9ab431fc54e2 37 SocketAddress mask;
JohnnyK 5:58fe518111d7 38 net->get_netmask(&mask);
JohnnyK 3:9ab431fc54e2 39 const char *p_mask = mask.get_ip_address();
JohnnyK 3:9ab431fc54e2 40 printf("Netmask: %s\n", p_mask ? p_mask : "None");
JohnnyK 3:9ab431fc54e2 41 SocketAddress gateway;
JohnnyK 5:58fe518111d7 42 net->get_gateway(&gateway);
JohnnyK 3:9ab431fc54e2 43 const char *p_gateway = gateway.get_ip_address();
JohnnyK 3:9ab431fc54e2 44 printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
JohnnyK 3:9ab431fc54e2 45
JohnnyK 0:9f56faada164 46 // Check ip
JohnnyK 4:2579ef3b025f 47 if(ip){
JohnnyK 3:9ab431fc54e2 48 UDPSocket sock;
JohnnyK 5:58fe518111d7 49 sock.open(net);
JohnnyK 2:01b03970605d 50 sock.bind(LOCALPORT);
JohnnyK 5:58fe518111d7 51 SocketAddress targetAddr(ADDRESS,REMOTEPORT);
JohnnyK 2:01b03970605d 52 printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT);
JohnnyK 3:9ab431fc54e2 53 char buffer[] = "Hello World, UDP Echo";
JohnnyK 3:9ab431fc54e2 54 printf("Sending message '%s' to (%s)\n",buffer,targetAddr.get_ip_address());
JohnnyK 5:58fe518111d7 55 // first message
JohnnyK 3:9ab431fc54e2 56 sock.sendto(targetAddr, buffer, sizeof(buffer));
JohnnyK 0:9f56faada164 57 printf("Waiting for message...\n");
JohnnyK 0:9f56faada164 58
JohnnyK 5:58fe518111d7 59 SocketAddress addr;
JohnnyK 0:9f56faada164 60 while(1) {
JohnnyK 5:58fe518111d7 61 char in_buffer[BUFFSIZE];
JohnnyK 0:9f56faada164 62 int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer));
JohnnyK 0:9f56faada164 63 in_buffer[n] = '\0';
JohnnyK 0:9f56faada164 64 printf("Recieved: %s\n", in_buffer);
JohnnyK 5:58fe518111d7 65 char out_buffer[BUFFSIZE];
JohnnyK 0:9f56faada164 66 n = sprintf(out_buffer,"Echo - %s", in_buffer);
JohnnyK 0:9f56faada164 67 out_buffer[n] = '\0';
JohnnyK 0:9f56faada164 68 printf("Send back: %s\n", out_buffer);
JohnnyK 5:58fe518111d7 69 addr.set_port(REMOTEPORT);
JohnnyK 5:58fe518111d7 70 sock.sendto(addr, out_buffer, sizeof(out_buffer));
JohnnyK 0:9f56faada164 71 led2 =! led2;
JohnnyK 0:9f56faada164 72 }
JohnnyK 0:9f56faada164 73 }else{
JohnnyK 0:9f56faada164 74 printf("No IP\n");
JohnnyK 5:58fe518111d7 75 net->disconnect();
JohnnyK 0:9f56faada164 76 printf("Thread end\n");
JohnnyK 0:9f56faada164 77 }
JohnnyK 0:9f56faada164 78 }
JohnnyK 0:9f56faada164 79
JohnnyK 0:9f56faada164 80
JohnnyK 0:9f56faada164 81 int main() {
JohnnyK 1:9c712ebb90d3 82 printf("UDP echo starting...\n");
JohnnyK 0:9f56faada164 83 thread.start(callback(udpEcho));
JohnnyK 3:9ab431fc54e2 84 ThisThread::sleep_for(1000);
JohnnyK 0:9f56faada164 85
JohnnyK 0:9f56faada164 86 while(1) {
JohnnyK 0:9f56faada164 87 led1 =! led1;
JohnnyK 3:9ab431fc54e2 88 ThisThread::sleep_for(1000);
JohnnyK 0:9f56faada164 89 }
JohnnyK 0:9f56faada164 90 }