Pedro Campos
/
SERVOS_V0_3
test
main.cpp@0:cf516d904427, 2017-10-23 (annotated)
- Committer:
- deepikabhavnani
- Date:
- Mon Oct 23 21:26:36 2017 +0000
- Revision:
- 0:cf516d904427
- Child:
- 1:da6afd392792
UDP Socket example - Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
deepikabhavnani | 0:cf516d904427 | 1 | #include "mbed.h" |
deepikabhavnani | 0:cf516d904427 | 2 | #include "EthernetInterface.h" |
deepikabhavnani | 0:cf516d904427 | 3 | |
deepikabhavnani | 0:cf516d904427 | 4 | // Network interface |
deepikabhavnani | 0:cf516d904427 | 5 | EthernetInterface net; |
deepikabhavnani | 0:cf516d904427 | 6 | |
deepikabhavnani | 0:cf516d904427 | 7 | // Time protocol implementation : Address: time.nist.gov UDPPort: 37 |
deepikabhavnani | 0:cf516d904427 | 8 | |
deepikabhavnani | 0:cf516d904427 | 9 | typedef struct { |
deepikabhavnani | 0:cf516d904427 | 10 | uint32_t secs; // Transmit Time-stamp seconds. |
deepikabhavnani | 0:cf516d904427 | 11 | }ntp_packet; |
deepikabhavnani | 0:cf516d904427 | 12 | |
deepikabhavnani | 0:cf516d904427 | 13 | int main() { |
deepikabhavnani | 0:cf516d904427 | 14 | // Bring up the ethernet interface |
deepikabhavnani | 0:cf516d904427 | 15 | printf("UDP Socket example\n"); |
deepikabhavnani | 0:cf516d904427 | 16 | if(0 != net.connect()) { |
deepikabhavnani | 0:cf516d904427 | 17 | printf("Error connecting\n"); |
deepikabhavnani | 0:cf516d904427 | 18 | return -1; |
deepikabhavnani | 0:cf516d904427 | 19 | } |
deepikabhavnani | 0:cf516d904427 | 20 | |
deepikabhavnani | 0:cf516d904427 | 21 | // Show the network address |
deepikabhavnani | 0:cf516d904427 | 22 | const char *ip = net.get_ip_address(); |
deepikabhavnani | 0:cf516d904427 | 23 | printf("IP address is: %s\n", ip ? ip : "No IP"); |
deepikabhavnani | 0:cf516d904427 | 24 | |
deepikabhavnani | 0:cf516d904427 | 25 | UDPSocket sock(&net); |
deepikabhavnani | 0:cf516d904427 | 26 | SocketAddress sockAddr; |
deepikabhavnani | 0:cf516d904427 | 27 | |
deepikabhavnani | 0:cf516d904427 | 28 | char out_buffer[] = "time"; |
deepikabhavnani | 0:cf516d904427 | 29 | if(0 > sock.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer))) { |
deepikabhavnani | 0:cf516d904427 | 30 | printf("Error sending data\n"); |
deepikabhavnani | 0:cf516d904427 | 31 | return -1; |
deepikabhavnani | 0:cf516d904427 | 32 | } |
deepikabhavnani | 0:cf516d904427 | 33 | |
deepikabhavnani | 0:cf516d904427 | 34 | ntp_packet in_data; |
deepikabhavnani | 0:cf516d904427 | 35 | int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet)); |
deepikabhavnani | 0:cf516d904427 | 36 | in_data.secs = ntohl( in_data.secs ) - 2208988800; // 1900-1970 |
deepikabhavnani | 0:cf516d904427 | 37 | printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n", |
deepikabhavnani | 0:cf516d904427 | 38 | (uint32_t)in_data.secs); |
deepikabhavnani | 0:cf516d904427 | 39 | printf("Time = %s", ctime(( const time_t* )&in_data.secs)); |
deepikabhavnani | 0:cf516d904427 | 40 | |
deepikabhavnani | 0:cf516d904427 | 41 | printf("Time Server Address: %s Port: %d\n\r", |
deepikabhavnani | 0:cf516d904427 | 42 | sockAddr.get_ip_address(), sockAddr.get_port()); |
deepikabhavnani | 0:cf516d904427 | 43 | |
deepikabhavnani | 0:cf516d904427 | 44 | // Close the socket and bring down the network interface |
deepikabhavnani | 0:cf516d904427 | 45 | sock.close(); |
deepikabhavnani | 0:cf516d904427 | 46 | net.disconnect(); |
deepikabhavnani | 0:cf516d904427 | 47 | return 0; |
deepikabhavnani | 0:cf516d904427 | 48 | } |