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.
main.cpp@70:6563866f9466, 2016-04-20 (annotated)
- Committer:
- Christopher Haster
- Date:
- Wed Apr 20 20:41:17 2016 -0500
- Revision:
- 70:6563866f9466
- Parent:
- 66:6a20c7e57306
Updated dependencies
Who changed what in which revision?
User | Revision | Line number | New 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" |
Christopher Haster |
66:6a20c7e57306 | 18 | #include "rtos.h" |
geky | 44:d7c5a56450a1 | 19 | #include "LWIPInterface.h" |
Christopher Haster |
30:f80540b6e2db | 20 | #include "TCPSocket.h" |
sam_grove | 0:1984a177ff56 | 21 | |
Christopher Haster |
65:826ec2bbec51 | 22 | LWIPInterface eth; |
Christopher Haster |
66:6a20c7e57306 | 23 | Semaphore network_sem(1); |
Christopher Haster |
65:826ec2bbec51 | 24 | |
Christopher Haster |
66:6a20c7e57306 | 25 | DigitalOut blink_led(LED_GREEN); |
Christopher Haster |
66:6a20c7e57306 | 26 | DigitalOut network_led(LED_BLUE); |
Christopher Haster |
66:6a20c7e57306 | 27 | |
Christopher Haster |
65:826ec2bbec51 | 28 | void blink() |
sam_grove | 50:a9926b8b21fe | 29 | { |
Christopher Haster |
66:6a20c7e57306 | 30 | blink_led = !blink_led; |
Christopher Haster |
66:6a20c7e57306 | 31 | } |
Christopher Haster |
66:6a20c7e57306 | 32 | |
Christopher Haster |
66:6a20c7e57306 | 33 | void network_callback() |
Christopher Haster |
66:6a20c7e57306 | 34 | { |
Christopher Haster |
66:6a20c7e57306 | 35 | network_led = !network_led; |
Christopher Haster |
66:6a20c7e57306 | 36 | network_sem.release(); |
sam_grove | 50:a9926b8b21fe | 37 | } |
Christopher Haster |
58:7033f5893210 | 38 | |
sam_grove | 0:1984a177ff56 | 39 | int main() |
sam_grove | 20:4cb9ef3b0cc9 | 40 | { |
Christopher Haster |
65:826ec2bbec51 | 41 | Ticker blinky; |
Christopher Haster |
65:826ec2bbec51 | 42 | blinky.attach(blink, 0.4f); |
Christopher Haster |
65:826ec2bbec51 | 43 | |
bridadan | 14:c47437f5dae8 | 44 | printf("NetworkSocketAPI Example\r\n"); |
Christopher Haster |
58:7033f5893210 | 45 | |
geky | 44:d7c5a56450a1 | 46 | eth.connect(); |
Christopher Haster |
58:7033f5893210 | 47 | const char *ip = eth.get_ip_address(); |
Christopher Haster |
58:7033f5893210 | 48 | const char *mac = eth.get_mac_address(); |
Christopher Haster |
65:826ec2bbec51 | 49 | printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
Christopher Haster |
65:826ec2bbec51 | 50 | printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
geky | 59:14d6716bc772 | 51 | |
Christopher Haster |
65:826ec2bbec51 | 52 | SocketAddress addr(ð, "mbed.org"); |
Christopher Haster |
65:826ec2bbec51 | 53 | printf("mbed.org resolved to: %s\r\n", addr.get_ip_address()); |
sarahmarshy | 22:1d355289fc18 | 54 | |
Christopher Haster |
66:6a20c7e57306 | 55 | TCPSocket ipv4socket(ð); |
Christopher Haster |
66:6a20c7e57306 | 56 | ipv4socket.connect("4.ifcfg.me", 23); |
Christopher Haster |
66:6a20c7e57306 | 57 | ipv4socket.set_blocking(false); |
Christopher Haster |
66:6a20c7e57306 | 58 | ipv4socket.attach(network_callback); |
Christopher Haster |
66:6a20c7e57306 | 59 | |
Christopher Haster |
66:6a20c7e57306 | 60 | TCPSocket ipv6socket(ð); |
Christopher Haster |
66:6a20c7e57306 | 61 | ipv6socket.connect("6.ifcfg.me", 23); |
Christopher Haster |
66:6a20c7e57306 | 62 | ipv6socket.set_blocking(false); |
Christopher Haster |
66:6a20c7e57306 | 63 | ipv6socket.attach(network_callback); |
sarahmarshy | 21:979b1db5d7da | 64 | |
Christopher Haster |
66:6a20c7e57306 | 65 | // Tries to get both an IPv4 and IPv6 address |
Christopher Haster |
66:6a20c7e57306 | 66 | while (network_sem.wait(2500) > 0) { |
Christopher Haster |
66:6a20c7e57306 | 67 | int count; |
Christopher Haster |
66:6a20c7e57306 | 68 | char buffer[64]; |
Christopher Haster |
66:6a20c7e57306 | 69 | |
Christopher Haster |
66:6a20c7e57306 | 70 | count = ipv4socket.recv(buffer, sizeof buffer); |
Christopher Haster |
66:6a20c7e57306 | 71 | if (count >= 0) { |
Christopher Haster |
66:6a20c7e57306 | 72 | printf("public IPv4 address is: %.15s\r\n", &buffer[15]); |
Christopher Haster |
66:6a20c7e57306 | 73 | } |
Christopher Haster |
66:6a20c7e57306 | 74 | |
Christopher Haster |
66:6a20c7e57306 | 75 | count = ipv6socket.recv(buffer, sizeof buffer); |
Christopher Haster |
66:6a20c7e57306 | 76 | if (count >= 0) { |
Christopher Haster |
66:6a20c7e57306 | 77 | printf("public IPv6 address is: %.39s\r\n", &buffer[15]); |
Christopher Haster |
66:6a20c7e57306 | 78 | } |
Christopher Haster |
66:6a20c7e57306 | 79 | } |
Christopher Haster |
65:826ec2bbec51 | 80 | |
Christopher Haster |
66:6a20c7e57306 | 81 | ipv4socket.close(); |
Christopher Haster |
66:6a20c7e57306 | 82 | ipv6socket.close(); |
geky | 44:d7c5a56450a1 | 83 | eth.disconnect(); |
Christopher Haster |
30:f80540b6e2db | 84 | |
Christopher Haster |
65:826ec2bbec51 | 85 | printf("Done\r\n"); |
sam_grove | 0:1984a177ff56 | 86 | } |