static wiced_result_t wiced_websocket_tls_connect( wiced_websocket_t* websocket,wiced_ip_address_t* address ) { wiced_result_t result = WICED_ERROR; wiced_tls_init_simple_context( &websocket_tls_context, NULL ); WICED_VERIFY( wiced_tcp_create_socket( &websocket->socket, WICED_STA_INTERFACE ) ); wiced_tcp_register_callbacks( &websocket->socket, websocket->on_open, NULL, websocket->on_close ); wiced_tcp_enable_tls( &websocket->socket, &websocket_tls_context ); wiced_tcp_bind( &websocket->socket, 443 ); result = wiced_tcp_connect( &websocket->socket, address, 443, 10000 ); if ( result != WICED_SUCCESS ) { websocket->error_type=WEBSOCKET_CLIENT_CONNECT_ERROR; websocket->on_error( websocket ); wiced_tcp_delete_socket( &websocket->socket ); return ( result ); } return ( result ); }
int platform_network_connect(Network* n, char* hostname, int port) { wiced_ip_address_t ip_address; wiced_result_t rc = -1; int attempt = 1; do { platform_printf("trying to resolve hostname (attempt %d)\n", attempt); if ((rc = wiced_hostname_lookup(hostname, &ip_address, 10000)) == WICED_SUCCESS) { platform_printf("hostname resolved\n"); break; } } while (++attempt <= 5); if (rc != WICED_SUCCESS) { platform_printf("failed to resolve ip address of %s, rc = %d\n", hostname, rc); return rc; } if (n->tls_enabled) wiced_tls_init_simple_context(&n->tls_context, hostname); /* Create a TCP socket */ rc = wiced_tcp_create_socket(&n->socket, WICED_STA_INTERFACE); if (rc != WICED_SUCCESS) { platform_printf("tcp socket creation failed, rc = %d\n", rc); return rc; } if (n->tls_enabled) wiced_tcp_enable_tls(&n->socket, &n->tls_context); rc = wiced_tcp_connect(&n->socket, &ip_address, port, 5000); if (rc != WICED_SUCCESS) { platform_printf("unable to establish connection to %s:%d, rc = %d\n", hostname, port, rc); goto exit; } rc = wiced_tcp_stream_init(&n->stream, &n->socket); if (rc != WICED_SUCCESS) { platform_printf("unable to init tcp stream, rc = %d\n", rc); goto exit; } rc = WICED_SUCCESS; exit: if (rc != WICED_SUCCESS) platform_network_disconnect(n); return rc; }
wiced_result_t xively_open_feed( xively_feed_t* feed ) { wiced_result_t result; if ( is_ip_address_resolved == WICED_FALSE ) { WICED_VERIFY( wiced_hostname_lookup( HOST_NAME, &xively_server_ip_address, DNS_TIMEOUT ) ); is_ip_address_resolved = WICED_TRUE; } wiced_tls_init_simple_context( &feed->tls_context, NULL ); WICED_VERIFY( wiced_tcp_create_socket( &feed->socket, WICED_STA_INTERFACE ) ); result = wiced_tcp_bind( &feed->socket, WICED_ANY_PORT ); if ( result != WICED_SUCCESS ) { wiced_tcp_delete_socket( &feed->socket ); return result; } result = wiced_tcp_enable_tls( &feed->socket, &feed->tls_context ); if ( result != WICED_SUCCESS ) { wiced_tls_deinit_context( &feed->tls_context ); wiced_tcp_delete_socket( &feed->socket ); return result; } result = wiced_tcp_connect( &feed->socket, &xively_server_ip_address, HTTPS_PORT, SOCKET_CONNECT_TIMEOUT ); if ( result != WICED_SUCCESS ) { wiced_tcp_delete_socket( &feed->socket ); return result; } return WICED_SUCCESS; }