Non-blocking example of the LWIPInterface

Dependencies:   LWIPInterface NetworkSocketAPI mbed-rtos mbed

Non-blocking example for the NetworkSocketAPI. Attempts to get the device's public facing IP address from ifcfg.me using the Happy Eyeballs algorithm to fetch an IPv4 or IPv6 address.

Committer:
geky
Date:
Tue Apr 05 23:40:09 2016 +0000
Revision:
59:14d6716bc772
Parent:
58:7033f5893210
Child:
65:826ec2bbec51
Updated dependencies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:1984a177ff56 1 /* NetworkSocketAPI Example Program
sam_grove 0:1984a177ff56 2 * Copyright (c) 2015 ARM Limited
sam_grove 0:1984a177ff56 3 *
sam_grove 0:1984a177ff56 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:1984a177ff56 5 * you may not use this file except in compliance with the License.
sam_grove 0:1984a177ff56 6 * You may obtain a copy of the License at
sam_grove 0:1984a177ff56 7 *
sam_grove 0:1984a177ff56 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:1984a177ff56 9 *
sam_grove 0:1984a177ff56 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:1984a177ff56 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:1984a177ff56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:1984a177ff56 13 * See the License for the specific language governing permissions and
sam_grove 0:1984a177ff56 14 * limitations under the License.
sam_grove 0:1984a177ff56 15 */
sam_grove 0:1984a177ff56 16
sam_grove 0:1984a177ff56 17 #include "mbed.h"
geky 44:d7c5a56450a1 18 #include "LWIPInterface.h"
Christopher Haster 30:f80540b6e2db 19 #include "TCPSocket.h"
sam_grove 0:1984a177ff56 20
Christopher Haster 32:28a909a3748e 21 DigitalOut myled(LED_GREEN);
sam_grove 50:a9926b8b21fe 22 void flash()
sam_grove 50:a9926b8b21fe 23 {
sam_grove 50:a9926b8b21fe 24 myled = !myled;
sam_grove 50:a9926b8b21fe 25 }
Christopher Haster 58:7033f5893210 26
geky 44:d7c5a56450a1 27 LWIPInterface eth;
sam_grove 2:7283ce112304 28
sam_grove 0:1984a177ff56 29 int main()
sam_grove 20:4cb9ef3b0cc9 30 {
sam_grove 20:4cb9ef3b0cc9 31 Ticker t;
sam_grove 24:471a07e886ae 32 t.attach(flash, 0.4f);
bridadan 14:c47437f5dae8 33 printf("NetworkSocketAPI Example\r\n");
Christopher Haster 58:7033f5893210 34
geky 44:d7c5a56450a1 35 eth.connect();
sam_grove 50:a9926b8b21fe 36
Christopher Haster 58:7033f5893210 37 const char *ip = eth.get_ip_address();
Christopher Haster 58:7033f5893210 38 const char *mac = eth.get_mac_address();
Christopher Haster 30:f80540b6e2db 39 printf("IP Address is: %s\r\n", (ip) ? ip : "No IP");
Christopher Haster 30:f80540b6e2db 40 printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC");
geky 59:14d6716bc772 41
geky 59:14d6716bc772 42 SocketAddress addr(&eth, "time-a.nist.gov", 37);
geky 59:14d6716bc772 43 printf("time-a.nist.gov resolved to: %s\r\n", addr.get_ip_address());
sarahmarshy 22:1d355289fc18 44
geky 59:14d6716bc772 45 TCPSocket socket(&eth);
Christopher Haster 58:7033f5893210 46 socket.connect("time-a.nist.gov", 37);
sarahmarshy 21:979b1db5d7da 47
sam_grove 24:471a07e886ae 48 char recieved[100] = {0};
Christopher Haster 30:f80540b6e2db 49 int32_t size = 0;
Christopher Haster 30:f80540b6e2db 50 size = socket.recv(recieved, sizeof(recieved));
Christopher Haster 30:f80540b6e2db 51
Christopher Haster 30:f80540b6e2db 52 socket.close();
geky 44:d7c5a56450a1 53 eth.disconnect();
Christopher Haster 30:f80540b6e2db 54
Christopher Haster 30:f80540b6e2db 55 printf("Recieved: %ld bytes, %02x%02x%02x%02x\r\n", size,
sam_grove 50:a9926b8b21fe 56 recieved[0], recieved[1], recieved[2], recieved[3]);
Christopher Haster 58:7033f5893210 57 printf("NetworkSocketAPI Example Finished\r\n");
sam_grove 0:1984a177ff56 58 }