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

Committer:
Christopher Haster
Date:
2016-04-05
Revision:
58:7033f5893210
Parent:
57:fb2f8e188383
Child:
59:14d6716bc772

File content as of revision 58:7033f5893210:

/* NetworkSocketAPI Example Program
 * Copyright (c) 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "LWIPInterface.h"
#include "TCPSocket.h"

DigitalOut myled(LED_GREEN);
void flash()
{
    myled = !myled;
}

LWIPInterface eth;
TCPSocket socket(&eth);

int main()
{
    Ticker t;
    t.attach(flash, 0.4f);
    printf("NetworkSocketAPI Example\r\n");

    eth.connect();

    const char *ip = eth.get_ip_address();
    const char *mac = eth.get_mac_address();
    printf("IP Address is: %s\r\n", (ip) ? ip : "No IP");
    printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC");

    socket.connect("time-a.nist.gov", 37);

    char recieved[100] = {0};
    int32_t size = 0;
    size = socket.recv(recieved, sizeof(recieved));

    socket.close();
    eth.disconnect();

    printf("Recieved: %ld bytes, %02x%02x%02x%02x\r\n", size,
           recieved[0], recieved[1], recieved[2], recieved[3]);
    printf("NetworkSocketAPI Example Finished\r\n");
}