Jan Kamidra
/
UDP_Echo
UDP echo with thread
main.cpp@3:9ab431fc54e2, 2020-05-08 (annotated)
- Committer:
- JohnnyK
- Date:
- Fri May 08 12:03:27 2020 +0000
- Revision:
- 3:9ab431fc54e2
- Parent:
- 2:01b03970605d
- Child:
- 4:2579ef3b025f
Reworked according to documentation 5.15
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 3:9ab431fc54e2 | 1 | #include "mbed.h" //MbedOS 5.15 |
JohnnyK | 0:9f56faada164 | 2 | #include "EthernetInterface.h" |
JohnnyK | 0:9f56faada164 | 3 | |
JohnnyK | 2:01b03970605d | 4 | #define ROUTER |
JohnnyK | 2:01b03970605d | 5 | #ifndef ROUTER |
JohnnyK | 3:9ab431fc54e2 | 6 | #define IP "192.168.1.1" //Here place your defined IP of Mbed |
JohnnyK | 3:9ab431fc54e2 | 7 | #define GATEWAY "0.0.0.0" |
JohnnyK | 3:9ab431fc54e2 | 8 | #define MASK "255.255.255.0" |
JohnnyK | 2:01b03970605d | 9 | #endif |
JohnnyK | 3:9ab431fc54e2 | 10 | #define ADDRESS "10.0.1.29" //Here place IP of your PC. Run cmd.exe and write command ipconfig |
JohnnyK | 3:9ab431fc54e2 | 11 | #define LOCALPORT 20 |
JohnnyK | 3:9ab431fc54e2 | 12 | #define REMOTEPORT 2000 |
JohnnyK | 0:9f56faada164 | 13 | |
JohnnyK | 0:9f56faada164 | 14 | Thread thread; |
JohnnyK | 0:9f56faada164 | 15 | DigitalOut led1(LED1); |
JohnnyK | 0:9f56faada164 | 16 | DigitalOut led2(LED2); |
JohnnyK | 3:9ab431fc54e2 | 17 | SocketAddress targetAddr(ADDRESS,REMOTEPORT); |
JohnnyK | 0:9f56faada164 | 18 | |
JohnnyK | 0:9f56faada164 | 19 | void udpEcho(){ |
JohnnyK | 0:9f56faada164 | 20 | EthernetInterface eth; |
JohnnyK | 2:01b03970605d | 21 | int eth_stat; |
JohnnyK | 2:01b03970605d | 22 | #ifndef ROUTER |
JohnnyK | 2:01b03970605d | 23 | eth.disconnect(); |
JohnnyK | 2:01b03970605d | 24 | eth_stat = eth.set_network(IP,MASK,GATEWAY); |
JohnnyK | 2:01b03970605d | 25 | printf("set IP status: %i\n",eth_stat); |
JohnnyK | 2:01b03970605d | 26 | #endif |
JohnnyK | 2:01b03970605d | 27 | eth_stat = eth.connect(); |
JohnnyK | 2:01b03970605d | 28 | printf("connect status: %i\n",eth_stat); |
JohnnyK | 3:9ab431fc54e2 | 29 | |
JohnnyK | 3:9ab431fc54e2 | 30 | SocketAddress ip; |
JohnnyK | 3:9ab431fc54e2 | 31 | eth.get_ip_address(&ip); |
JohnnyK | 3:9ab431fc54e2 | 32 | const char *p_ip = ip.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 33 | printf("IP address: %s\n", p_ip ? p_ip : "None"); |
JohnnyK | 3:9ab431fc54e2 | 34 | SocketAddress mask; |
JohnnyK | 3:9ab431fc54e2 | 35 | eth.get_netmask(&mask); |
JohnnyK | 3:9ab431fc54e2 | 36 | const char *p_mask = mask.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 37 | printf("Netmask: %s\n", p_mask ? p_mask : "None"); |
JohnnyK | 3:9ab431fc54e2 | 38 | SocketAddress gateway; |
JohnnyK | 3:9ab431fc54e2 | 39 | eth.get_gateway(&gateway); |
JohnnyK | 3:9ab431fc54e2 | 40 | const char *p_gateway = gateway.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 41 | printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); |
JohnnyK | 3:9ab431fc54e2 | 42 | |
JohnnyK | 0:9f56faada164 | 43 | // Check ip |
JohnnyK | 2:01b03970605d | 44 | if(ip){// Check ip |
JohnnyK | 3:9ab431fc54e2 | 45 | UDPSocket sock; |
JohnnyK | 3:9ab431fc54e2 | 46 | SocketAddress addr; |
JohnnyK | 0:9f56faada164 | 47 | sock.open(ð); |
JohnnyK | 2:01b03970605d | 48 | sock.bind(LOCALPORT); |
JohnnyK | 2:01b03970605d | 49 | printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT); |
JohnnyK | 3:9ab431fc54e2 | 50 | char buffer[] = "Hello World, UDP Echo"; |
JohnnyK | 3:9ab431fc54e2 | 51 | printf("Sending message '%s' to (%s)\n",buffer,targetAddr.get_ip_address()); |
JohnnyK | 3:9ab431fc54e2 | 52 | sock.sendto(targetAddr, buffer, sizeof(buffer)); |
JohnnyK | 0:9f56faada164 | 53 | printf("Waiting for message...\n"); |
JohnnyK | 0:9f56faada164 | 54 | |
JohnnyK | 0:9f56faada164 | 55 | while(1) { |
JohnnyK | 0:9f56faada164 | 56 | char in_buffer[80]; |
JohnnyK | 0:9f56faada164 | 57 | int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer)); |
JohnnyK | 0:9f56faada164 | 58 | in_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 59 | printf("Recieved: %s\n", in_buffer); |
JohnnyK | 0:9f56faada164 | 60 | char out_buffer[80]; |
JohnnyK | 0:9f56faada164 | 61 | n = sprintf(out_buffer,"Echo - %s", in_buffer); |
JohnnyK | 0:9f56faada164 | 62 | out_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 63 | printf("Send back: %s\n", out_buffer); |
JohnnyK | 3:9ab431fc54e2 | 64 | sock.sendto(targetAddr, out_buffer, sizeof(out_buffer)); |
JohnnyK | 0:9f56faada164 | 65 | led2 =! led2; |
JohnnyK | 0:9f56faada164 | 66 | } |
JohnnyK | 0:9f56faada164 | 67 | }else{ |
JohnnyK | 0:9f56faada164 | 68 | printf("No IP\n"); |
JohnnyK | 0:9f56faada164 | 69 | //eth.disconnect(); |
JohnnyK | 0:9f56faada164 | 70 | printf("Thread end\n"); |
JohnnyK | 0:9f56faada164 | 71 | } |
JohnnyK | 0:9f56faada164 | 72 | } |
JohnnyK | 0:9f56faada164 | 73 | |
JohnnyK | 0:9f56faada164 | 74 | |
JohnnyK | 0:9f56faada164 | 75 | int main() { |
JohnnyK | 1:9c712ebb90d3 | 76 | printf("UDP echo starting...\n"); |
JohnnyK | 0:9f56faada164 | 77 | thread.start(callback(udpEcho)); |
JohnnyK | 3:9ab431fc54e2 | 78 | ThisThread::sleep_for(1000); |
JohnnyK | 0:9f56faada164 | 79 | |
JohnnyK | 0:9f56faada164 | 80 | while(1) { |
JohnnyK | 0:9f56faada164 | 81 | led1 =! led1; |
JohnnyK | 3:9ab431fc54e2 | 82 | ThisThread::sleep_for(1000); |
JohnnyK | 0:9f56faada164 | 83 | } |
JohnnyK | 0:9f56faada164 | 84 | } |