/**
 * Do what is needed to finalize the connection.
 * @return
 */
wlan_result_t wlan_connect_finalize()
{
    const static_ip_config_t& ip_config = *wlan_fetch_saved_ip_config();

    // enable connection from stored profiles
    wlan_result_t result = wiced_interface_up(WICED_STA_INTERFACE);
    if (!result) {
        HAL_WLAN_notify_connected();
        wiced_ip_setting_t settings;
        wiced_ip_address_t dns;

        switch (IPAddressSource(ip_config.config_mode)) {
            case STATIC_IP:
                to_wiced_ip_address(settings.ip_address, ip_config.host);
                to_wiced_ip_address(settings.netmask, ip_config.netmask);
                to_wiced_ip_address(settings.gateway, ip_config.gateway);
                result = wiced_network_up(WICED_STA_INTERFACE, WICED_USE_STATIC_IP, &settings);
                if (!result) {
                    if (to_wiced_ip_address(dns, ip_config.dns1))
                        dns_client_add_server_address(dns);
                    if (to_wiced_ip_address(dns, ip_config.dns2))
                        dns_client_add_server_address(dns);
                }
            default:
                result = wiced_network_up(WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL);
                break;
        }
    }
    else
    {
        wiced_network_down(WICED_STA_INTERFACE);
    }
    // DHCP happens synchronously
    HAL_WLAN_notify_dhcp(!result);
    wiced_network_up_cancel = 0;
    return result;
}
/*
 * Function:    wilddog_gethostbyname
 * Description: wilddog gethostbyname function, it use the interface in wiced platform.
 * Input:        host: The pointer of host string.
 * Output:      addr: The pointer of the Wilddog_Address_T.
 * Return:      If success, return 0; else return -1.
*/
int wilddog_gethostbyname( Wilddog_Address_T* addr, char* host )
{
    wiced_ip_address_t address;
    uint32_t tmpIp = 0;
    uint32_t dns_ip;

    addr->len = 4;
    /* Add free DNS server (114.114.114.114) */
    dns_ip = WILDDOG_MAKE_IPV4(114,114,114,114);
    SET_IPV4_ADDRESS( address, &dns_ip );
    dns_client_add_server_address( address );
    if ( wiced_hostname_lookup( host, &address, 4000 ) != WICED_SUCCESS )
    {
        return -1;
    }
    tmpIp = address.ip.v4;
    tmpIp = wilddog_htonl(tmpIp);
    memcpy( addr->ip, &tmpIp, 4 );

    return 0;
}