Пример #1
0
void application_start(void)
{
    /* Initialise the device and WICED framework */
    wiced_init( );

    /* Bring up the network interface */
    wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL );

    /* Create a TCP server socket */
    if (wiced_tcp_create_socket(&tcp_server_handle.socket, WICED_STA_INTERFACE) != WICED_SUCCESS)
    {
        WPRINT_APP_INFO(("TCP socket creation failed\n"));
    }

    if (wiced_tcp_listen( &tcp_server_handle.socket, TCP_SERVER_LISTEN_PORT ) != WICED_SUCCESS)
    {
        WPRINT_APP_INFO(("TCP server socket initialization failed\n"));
        wiced_tcp_delete_socket(&tcp_server_handle.socket);
        return;
    }

    /* Start a tcp server thread */
    WPRINT_APP_INFO(("Creating tcp server thread \n"));
    wiced_rtos_create_thread(&tcp_thread, TCP_SERVER_THREAD_PRIORITY, "Demo tcp server", tcp_server_thread_main, TCP_SERVER_STACK_SIZE, &tcp_server_handle);

}
Пример #2
0
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 );
}
Пример #3
0
 void close()
 {
     if (open) {
         wiced_tcp_disconnect(this);
         open = false;
     }
     wiced_tcp_delete_socket(this);
     memset(this, 0, sizeof(wiced_tcp_socket_t));
     packet.dispose_packet();
 }
Пример #4
0
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
wiced_result_t ota_server_start( ota_server_t* server, uint16_t port, const ota_http_page_t* page_database, wiced_interface_t interface )
{
    memset( server, 0, sizeof( *server ) );

    /* Create the TCP socket */
    WICED_VERIFY(wiced_tcp_create_socket( &server->socket, interface ));
    if (wiced_tcp_listen( (wiced_tcp_socket_t*) &server->socket, port ) != WICED_SUCCESS)
    {
        wiced_tcp_delete_socket(&server->socket);
        return WICED_ERROR;
    }

    server->page_database = page_database;
    return wiced_rtos_create_thread(&server->thread, OTA_SERVER_THREAD_PRIORITY, "HTTPserver", ota_server_thread_main, OTA_SERVER_STACK_SIZE, server);
}
Пример #6
0
wiced_result_t wiced_wlu_server_eth_start( int tcp_server_port , char** argv)
{
    wiced_result_t result;
    UNUSED_PARAMETER(argv);
    wiced_assert("wlu_server already started", (wlu_server.started == WICED_FALSE));

    wlu_server.started                  = WICED_TRUE;
    wlu_server.eth_started              = WICED_TRUE;
    wlu_server.eth_port                 = tcp_server_port;



    // WWD_WLAN_KEEP_AWAKE( );

    /* Bring up the network interface */
    result = wiced_network_up( WICED_ETHERNET_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL );

    if ( result != WICED_SUCCESS )
    {
        WPRINT_APP_INFO(("Failed to connect to Ethernet.\n"));
    }

    /* Create a TCP socket */
    if ( wiced_tcp_create_socket( &tcp_client_socket, WICED_ETHERNET_INTERFACE ) != WICED_SUCCESS )
    {
        WPRINT_APP_INFO( ("TCP socket creation failed\n") );
    }
    if (wiced_tcp_listen( &tcp_client_socket, tcp_server_port ) != WICED_SUCCESS)
    {
        WPRINT_APP_INFO(("TCP server socket initialization failed\n"));
        wiced_tcp_delete_socket(&tcp_client_socket);
        return WICED_ERROR;
    }

    wiced_rtos_create_thread( &wlu_server.thread, WLU_SERVER_THREAD_PRIORITY, "wlu_eth_server", wlu_server_thread, WLU_SERVER_STACK_SIZE, &wlu_server );

    return WICED_SUCCESS;
}
Пример #7
0
wiced_result_t xively_close_feed( xively_feed_t* feed )
{
    wiced_tcp_disconnect( &feed->socket );
    return wiced_tcp_delete_socket( &feed->socket );
}
Пример #8
0
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;
}
Пример #9
0
void platform_network_disconnect(Network* n)
{
    wiced_tcp_disconnect(&n->socket);
    wiced_tcp_stream_deinit(&n->stream);
    wiced_tcp_delete_socket(&n->socket);
}
Пример #10
0
static void ota_server_thread_main(uint32_t arg)
{
    ota_server_t*          server          = (ota_server_t*) arg;
    uint32_t               total_body_size = 0;
    int                    i               = 0;

    server->reboot_required = WICED_FALSE;
    while ( server->quit != WICED_TRUE )
    {
        wiced_packet_t* temp_packet = NULL;

        /* Wait for a connection */
        wiced_result_t result = wiced_tcp_accept( &server->socket );
        if ( result == WICED_SUCCESS )
        {
            for(;;)
            {
                if ( wiced_tcp_receive( &server->socket, &temp_packet, WICED_WAIT_FOREVER ) == WICED_SUCCESS )
                {
                    char* request_string;
                    uint16_t request_length;
                    uint16_t available_data_length;

                    if( server->state == READING_HEADER )
                    {
                        uint8_t         temp   = 0;
                        wiced_result_t  result = WICED_ERROR;

                        if ( temp_packet == NULL )
                        {
                            goto disconnect;
                        }
                        init_request(server);

                        server->request.request_packets[server->request.current_packet_index] = temp_packet;


                        wiced_packet_get_data(temp_packet, 0, (uint8_t**)&request_string, &request_length, &available_data_length);

                        /* Check that this is a GET or POST request, abort everything else */
                        if ( ( strstr( request_string, "GET" ) == 0 ) && ( strstr( request_string, "POST") == 0 ) )
                        {
                            result = WICED_ERROR;
                            wiced_packet_delete(temp_packet);
                            goto disconnect;
                        }

                        /* Get header pointer and header size */
                        server->request.header_ptr = (uint8_t*)request_string;
                        server->request.header_size = ( (char*)strstr( (char*)request_string, crlfcrlf ) + strlen( crlfcrlf ) ) - (char*)request_string;
                        if( server->request.header_size == strlen( crlfcrlf ) )
                        {
                            goto disconnect;
                        }

                        /* Get content length */
                        server->request.content_length = 0;
                        if( strstr( request_string, "Content-Length") != NULL )
                        {
                            uint8_t* content_length_value = (uint8_t*)strstr( request_string, "Content-Length") + strlen("Content-Length:");
                            server->request.content_length = atoi((const char*)content_length_value);
                        }

                        temp = request_string[ server->request.header_size ];
                        request_string[ server->request.header_size ] ='\0';
                        request_string[ server->request.header_size ] = temp;

                        /* Get request type and the url */
                        result = get_http_request_type_and_url( (uint8_t*)request_string, request_length, &server->request);
                        if ( result == WICED_ERROR )
                        {
                           goto disconnect;
                        }

                        server->state = READING_BODY;
                    }

                    if( server->state == READING_BODY )
                    {
                        http_body_chunk_t*          current_body_chunk = &server->request.body_chunks[server->request.current_packet_index];

                        if( server->request.current_packet_index != 0 )
                        {
                            server->request.request_packets[server->request.current_packet_index] = temp_packet;
                        }

                        wiced_packet_get_data(temp_packet, 0, (uint8_t**)&request_string, &request_length, &available_data_length);

                        if( server->request.current_packet_index == 0 )
                        {
                            current_body_chunk->data = server->request.header_ptr + server->request.header_size;
                            current_body_chunk->size = ( request_string + request_length ) - (char*)current_body_chunk->data;
                        }
                        else
                        {
                            current_body_chunk->data = (uint8_t*)request_string;
                            current_body_chunk->size = request_length;
                        }
                        /* calculate total combined size of all body chunks which belongs to this message */
                        total_body_size = 0;

                        for( i = 0; i < ( server->request.current_packet_index + 1 ) ; i++ )
                        {
                            total_body_size+= server->request.body_chunks[i].size;
                        }

                        server->request.current_packet_index++;

                        /* Check whether the combined size of the previous chunks and the current one is equal to the content length received in the first packet */
                        if( total_body_size == server->request.content_length )
                        {
                            ota_server_process_request( server, &server->socket );
                            /* Delete all packets belonging to the message */
                            for( i = 0; i < server->request.current_packet_index; i++ )
                            {
                                wiced_packet_delete(server->request.request_packets[i]);
                            }
                            server->state = READING_HEADER;
                            break;
                        }
                    }
                }
                else
                {
                    goto disconnect;
                }
            }
        }
disconnect:
        wiced_tcp_disconnect( &server->socket );
    }

    wiced_tcp_delete_socket( &server->socket );
    if( server->reboot_required == WICED_TRUE )
    {
        /* Give some for the response to be sent properly */
        wiced_rtos_delay_milliseconds(2000);

        /* Perform a reboot!!! */
        wiced_framework_reboot();
    }
    WICED_END_OF_CURRENT_THREAD( );
}