Jan Kamidra
/
UDP_Echo
UDP echo with thread
main.cpp@7:94dccd29cac9, 2022-02-19 (annotated)
- Committer:
- JohnnyK
- Date:
- Sat Feb 19 23:25:31 2022 +0000
- Revision:
- 7:94dccd29cac9
- Parent:
- 6:06a897d0f950
- Child:
- 8:1f35cbb54ee7
Test from KeilStudio
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 6:06a897d0f950 | 1 | #include "mbed.h" //MbedOS 6.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 | 7:94dccd29cac9 | 23 | printf("UDP thread starting...\n"); |
JohnnyK | 5:58fe518111d7 | 24 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
JohnnyK | 5:58fe518111d7 | 25 | int net_stat; |
JohnnyK | 2:01b03970605d | 26 | #ifndef ROUTER |
JohnnyK | 5:58fe518111d7 | 27 | net->disconnect(); |
JohnnyK | 5:58fe518111d7 | 28 | net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY); |
JohnnyK | 5:58fe518111d7 | 29 | printf("set IP status: %i\n",net_stat); |
JohnnyK | 2:01b03970605d | 30 | #endif |
JohnnyK | 5:58fe518111d7 | 31 | net_stat = net->connect(); |
JohnnyK | 5:58fe518111d7 | 32 | printf("connect status: %i\n",net_stat); |
JohnnyK | 3:9ab431fc54e2 | 33 | |
JohnnyK | 3:9ab431fc54e2 | 34 | SocketAddress ip; |
JohnnyK | 5:58fe518111d7 | 35 | net->get_ip_address(&ip); |
JohnnyK | 3:9ab431fc54e2 | 36 | const char *p_ip = ip.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 37 | printf("IP address: %s\n", p_ip ? p_ip : "None"); |
JohnnyK | 3:9ab431fc54e2 | 38 | SocketAddress mask; |
JohnnyK | 5:58fe518111d7 | 39 | net->get_netmask(&mask); |
JohnnyK | 3:9ab431fc54e2 | 40 | const char *p_mask = mask.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 41 | printf("Netmask: %s\n", p_mask ? p_mask : "None"); |
JohnnyK | 3:9ab431fc54e2 | 42 | SocketAddress gateway; |
JohnnyK | 5:58fe518111d7 | 43 | net->get_gateway(&gateway); |
JohnnyK | 3:9ab431fc54e2 | 44 | const char *p_gateway = gateway.get_ip_address(); |
JohnnyK | 3:9ab431fc54e2 | 45 | printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); |
JohnnyK | 3:9ab431fc54e2 | 46 | |
JohnnyK | 0:9f56faada164 | 47 | // Check ip |
JohnnyK | 4:2579ef3b025f | 48 | if(ip){ |
JohnnyK | 3:9ab431fc54e2 | 49 | UDPSocket sock; |
JohnnyK | 5:58fe518111d7 | 50 | sock.open(net); |
JohnnyK | 2:01b03970605d | 51 | sock.bind(LOCALPORT); |
JohnnyK | 5:58fe518111d7 | 52 | SocketAddress targetAddr(ADDRESS,REMOTEPORT); |
JohnnyK | 2:01b03970605d | 53 | printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT); |
JohnnyK | 3:9ab431fc54e2 | 54 | char buffer[] = "Hello World, UDP Echo"; |
JohnnyK | 3:9ab431fc54e2 | 55 | printf("Sending message '%s' to (%s)\n",buffer,targetAddr.get_ip_address()); |
JohnnyK | 5:58fe518111d7 | 56 | // first message |
JohnnyK | 3:9ab431fc54e2 | 57 | sock.sendto(targetAddr, buffer, sizeof(buffer)); |
JohnnyK | 0:9f56faada164 | 58 | printf("Waiting for message...\n"); |
JohnnyK | 0:9f56faada164 | 59 | |
JohnnyK | 5:58fe518111d7 | 60 | SocketAddress addr; |
JohnnyK | 0:9f56faada164 | 61 | while(1) { |
JohnnyK | 5:58fe518111d7 | 62 | char in_buffer[BUFFSIZE]; |
JohnnyK | 0:9f56faada164 | 63 | int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer)); |
JohnnyK | 0:9f56faada164 | 64 | in_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 65 | printf("Recieved: %s\n", in_buffer); |
JohnnyK | 5:58fe518111d7 | 66 | char out_buffer[BUFFSIZE]; |
JohnnyK | 0:9f56faada164 | 67 | n = sprintf(out_buffer,"Echo - %s", in_buffer); |
JohnnyK | 0:9f56faada164 | 68 | out_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 69 | printf("Send back: %s\n", out_buffer); |
JohnnyK | 5:58fe518111d7 | 70 | addr.set_port(REMOTEPORT); |
JohnnyK | 5:58fe518111d7 | 71 | sock.sendto(addr, out_buffer, sizeof(out_buffer)); |
JohnnyK | 0:9f56faada164 | 72 | led2 =! led2; |
JohnnyK | 0:9f56faada164 | 73 | } |
JohnnyK | 0:9f56faada164 | 74 | }else{ |
JohnnyK | 0:9f56faada164 | 75 | printf("No IP\n"); |
JohnnyK | 5:58fe518111d7 | 76 | net->disconnect(); |
JohnnyK | 0:9f56faada164 | 77 | printf("Thread end\n"); |
JohnnyK | 0:9f56faada164 | 78 | } |
JohnnyK | 0:9f56faada164 | 79 | } |
JohnnyK | 0:9f56faada164 | 80 | |
JohnnyK | 0:9f56faada164 | 81 | |
JohnnyK | 0:9f56faada164 | 82 | int main() { |
JohnnyK | 7:94dccd29cac9 | 83 | printf("UDP echo example\n"); |
JohnnyK | 0:9f56faada164 | 84 | thread.start(callback(udpEcho)); |
JohnnyK | 6:06a897d0f950 | 85 | ThisThread::sleep_for(1s); |
JohnnyK | 0:9f56faada164 | 86 | |
JohnnyK | 0:9f56faada164 | 87 | while(1) { |
JohnnyK | 0:9f56faada164 | 88 | led1 =! led1; |
JohnnyK | 6:06a897d0f950 | 89 | ThisThread::sleep_for(1s); |
JohnnyK | 0:9f56faada164 | 90 | } |
JohnnyK | 0:9f56faada164 | 91 | } |