コード例 #1
0
ファイル: https_client.c プロジェクト: 119/bcm-wiced-sdk
void application_start( )
{
    wiced_ip_address_t ip_address;
    wiced_result_t     result;

    wiced_init( );
    wiced_network_up(WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL);

    /* Configure the device */
    /* wiced_configure_device( app_config ); */ /* Config bypassed in local makefile and wifi_config_dct.h */

    WPRINT_APP_INFO( ( "Resolving IP address of HTTPS server\n" ) );
    wiced_hostname_lookup("www.google.com", &ip_address, 10000);
    WPRINT_APP_INFO( ( "Server is at %u.%u.%u.%u\n",   (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 24),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 16),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 8),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 0) ) );

    WPRINT_APP_INFO( ( "Getting '/'...\n" ) );

    /* Initialize the root CA certificate */
    wiced_tls_init_root_ca_certificates( google_root_ca_certificate );

    result = wiced_https_get( &ip_address, SIMPLE_GET_REQUEST, buffer, BUFFER_LENGTH, "www.google.com" );
    if ( result == WICED_SUCCESS )
    {
        WPRINT_APP_INFO( ( "Server returned\n%s", buffer ) );
    }
    else
    {
        WPRINT_APP_INFO( ( "Get failed: %u\n", result ) );
    }

    wiced_deinit();
}
コード例 #2
0
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;
}
コード例 #3
0
/*
 * 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;
}
コード例 #4
0
ファイル: xively.c プロジェクト: fishbaoz/wiced-emw3165
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;
}
コード例 #5
0
ファイル: websocket.c プロジェクト: moderepo/WICED-SDK
wiced_result_t open_websocket(websocket_handshake_t *hs, websocket_msg_handler_t binary_msg_handler, websocket_msg_handler_t text_msg_handler, void *ctx) {
    wiced_result_t ret;
    wiced_ip_address_t host_ip;
    wiced_tls_context_t tls_ctx;
    wiced_tcp_socket_t sock;
    wiced_tcp_stream_t stream;
    char *buf = NULL;
    uint32_t i;
    https_header_t *header;

    WPRINT_LIB_INFO( ("Starting WebSocket handshake to https://%s%s\n", hs->hostname, hs->path) );

    ret = wiced_hostname_lookup(hs->hostname, &host_ip, DNS_TIMEOUT);
    if (ret != WICED_SUCCESS) {
        WPRINT_LIB_INFO( ("DNS lookup failed for %s (err=%u)\n", hs->hostname, ret) );
        return ret;
    }

    wiced_tls_init_context(&tls_ctx, NULL, NULL);
    wiced_tcp_create_socket(&sock, WICED_STA_INTERFACE);
    wiced_tcp_enable_tls(&sock, &tls_ctx);

    {
        char ip_str[48];
        ip_to_str(&host_ip, ip_str);
        WPRINT_LIB_INFO( ("Establishing TLS connection to %s port %d\n", ip_str, HTTPS_PORT) );
    }

    ret = wiced_tcp_connect(&sock, &host_ip, HTTPS_PORT, HTTPS_CONNECT_TIMEOUT);
    if (ret != WICED_SUCCESS) {
        WPRINT_LIB_INFO( ("Failed to create TCP connection (err=%u)\n", ret) );
        wiced_tcp_delete_socket(&sock);
        return ret;
    }

    do {
        ret = wiced_tcp_stream_init(&stream, &sock);
        if (ret != WICED_SUCCESS) {
            WPRINT_LIB_INFO( ("Failed to initialize TCP stream (err=%u)\n", ret) );
            break;
        }

        buf = (char *)malloc(STREAM_BUF_SIZE);

        snprintf(buf, STREAM_BUF_SIZE, "GET %s HTTP/1.1\r\n", hs->path);
        WRITE_STREAM(stream, buf, ret);

        // Required headers.
        snprintf(buf, STREAM_BUF_SIZE,
                 "Host: %s\r\n" \
                 "Connection: upgrade\r\n" \
                 "Upgrade: websocket\r\n" \
                 "Sec-WebSocket-Key: %s\r\n" \
                 "Sec-WebSocket-Version: 13\r\n",
                 hs->hostname, hs->key);
        WRITE_STREAM(stream, buf, ret);

        // Additional headers.
        for (i = 0; i < hs->num_headers; i++) {
            header = &(hs->headers[i]);
            snprintf(buf, STREAM_BUF_SIZE, "%s: %s\r\n", header->name, header->value);
            WRITE_STREAM(stream, buf, ret);
        }

        if (i < hs->num_headers) {
            break;
        }

        strcpy(buf, "\r\n");
        WRITE_STREAM(stream, buf, ret);

        ret = wiced_tcp_stream_flush(&stream);
        if (ret != WICED_SUCCESS) {
            break;
        }

        ret = process_handshake_response(hs, &sock);

    } while (WICED_FALSE);

    if (buf) {
        free(buf);
    }

    wiced_tcp_stream_deinit(&stream);

    if (ret == WICED_SUCCESS) {
        WPRINT_LIB_INFO( ("WebSocket handshake OK\n") );
        process_frames(&sock, binary_msg_handler, text_msg_handler, ctx);
        WPRINT_LIB_INFO( ("Closing WebSocket.\n") );
    }
    else {
        WPRINT_LIB_INFO( ("WebSocket handshake failed (err=%u)\n", ret) );
    }

    wiced_tcp_disconnect(&sock);
    wiced_tcp_delete_socket(&sock);

    return ret;
}
コード例 #6
0
void application_start( void )
{
    wiced_ip_address_t  ip_address;
    wiced_result_t      result;

    /* We need three headers - host, content type, and content length */
    http_header_field_t header[3];
    /* Header 0 is the Host header */
    header[0].field        = HTTP_HEADER_HOST;
    header[0].field_length = strlen( HTTP_HEADER_HOST );
    header[0].value        = SERVER_HOST;
    header[0].value_length = strlen( SERVER_HOST );
    /* Header 1 is the content type (JSON) */
    header[1].field        =  HTTP_HEADER_CONTENT_TYPE;
    header[1].field_length = strlen( HTTP_HEADER_CONTENT_TYPE );
    header[1].value        = "application/json";
    header[1].value_length = strlen( "application/json" );
    /* Header 2 is the content length. In this case, it is the length of the JSON message */
    sprintf(json_len,"%d", strlen(JSON_MSG)); /* Calculate the length of the JSON message and store the value as a string */
    header[2].field        = HTTP_HEADER_CONTENT_LENGTH;
    header[2].field_length = strlen( HTTP_HEADER_CONTENT_LENGTH );
    header[2].value        = json_len; // This holds the length of the JSON message as a sting containing the decimal value
    header[2].value_length = strlen( json_len ); // This is the length of the string that holds the JSON message size. For example, if the JSON is 12 characters, this would be "2" because the string "12" is 2 characters long.

    wiced_init( );

    /* This semaphore will be used to wait for one request to finish before re-initializing and starting the next one */
    wiced_rtos_init_semaphore(&httpWait);

    wiced_network_up(WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL);

    WPRINT_APP_INFO( ( "Resolving IP address of %s\n", SERVER_HOST ) );
    wiced_hostname_lookup( SERVER_HOST, &ip_address, DNS_TIMEOUT_MS, WICED_STA_INTERFACE );
    WPRINT_APP_INFO( ( "%s is at %u.%u.%u.%u\n", SERVER_HOST,
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 24),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 16),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 8),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 0) ) );

    /* Initialize client */
    http_client_init( &client, WICED_STA_INTERFACE, event_handler, NULL );
    client.peer_cn = NULL; /* If you set hostname, library will make sure subject name in the server certificate is matching with host name you are trying to connect. Pass NULL if you don't want to enable this check */

    /* Connect to the server */
    if ( ( result = http_client_connect( &client, (const wiced_ip_address_t*)&ip_address, SERVER_PORT, HTTP_NO_SECURITY, CONNECT_TIMEOUT_MS ) ) == WICED_SUCCESS )
    {
        connected = WICED_TRUE;
        WPRINT_APP_INFO( ( "Connected to %s\n", SERVER_HOST ) );
    }
    else
    {
        WPRINT_APP_INFO( ( "Error: failed to connect to server: %u\n", result) );
        return; /* Connection failed - exit program */
    }

    /* Send a POST to resource /anything */
    http_request_init( &request, &client, HTTP_POST, "/post", HTTP_1_1 );
    http_request_write_header( &request, &header[0], 3 ); // We need 3 headers
    http_request_write_end_header( &request );
    http_request_write( &request, (uint8_t* ) JSON_MSG, strlen(JSON_MSG)); /* Write the content */
    http_request_flush( &request );

    wiced_rtos_get_semaphore(&httpWait, WICED_WAIT_FOREVER); /* Wait for this request to complete before going on */
    /* Disconnect from the server and deinit since we are done now */
    http_client_disconnect( &client );
    http_client_deinit( &client );
}
コード例 #7
0
ファイル: websocket.c プロジェクト: 119/bcm-wiced-sdk
static wiced_result_t wiced_websocket_common_connect(wiced_websocket_t* websocket, wiced_websocket_handshake_fields_t* websocket_header_fields, wiced_bool_t use_secure_connection)
{
    wiced_result_t      result = WICED_ERROR;
    wiced_ip_address_t  address;


    if( websocket != NULL )
    {

        if ( !mutex_initialised )
        {
            /*create handhsake mutex. There can only be one connection in a connecting state*/
            wiced_rtos_init_mutex( &handshake_lock );
            mutex_initialised = WICED_TRUE;
        }

        /*as per rfc 6455 specification, there can not be more then one socket in a connecting state*/
        wiced_rtos_lock_mutex( &handshake_lock );

        /*perform DNS lookup*/
        result = wiced_hostname_lookup( websocket_header_fields->host, &address, 10000 );
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_DNS_RESOLVE_ERROR );

        /*register websocket*/
        result = wiced_register_websocket( websocket );
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_NO_AVAILABLE_SOCKET );

        wiced_update_socket_state( websocket,WEBSOCKET_CONNECTING );

        /*establish secure/non secure tcp connection to server*/
        if (use_secure_connection)
        {
            result = wiced_websocket_tls_connect( websocket, &address );
        }
        else
        {
            result = wiced_websocket_connect( websocket, &address );
        }
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_CLIENT_CONNECT_ERROR );

        result = wiced_establish_websocket_handshake(websocket, websocket_header_fields);
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_SERVER_HANDSHAKE_RESPONSE_INVALID );

        /* If a sub protocol was requested, extract it from the handshake */
        if( websocket_header_fields->sec_websocket_protocol != NULL )
        {
            result = wiced_get_websocket_subprotocol( websocket->subprotocol );
            IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_SUBPROTOCOL_NOT_SUPPORTED );
        }

        wiced_tcp_register_callbacks( &websocket->socket, NULL, wiced_on_message_callback, wiced_on_close_callback );

        wiced_update_socket_state( websocket,WEBSOCKET_OPEN );
        websocket->on_open( websocket );

        /*release handhsake mutex. There can only be one connection in a connecting state*/
        wiced_rtos_unlock_mutex(&handshake_lock);

    }

    return result;

}