예제 #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
/**
 * Create a new socket handle.
 * @param family    Must be {@code AF_INET}
 * @param type      Either SOCK_DGRAM or SOCK_STREAM
 * @param protocol  Either IPPROTO_UDP or IPPROTO_TCP
 * @return
 */
sock_handle_t socket_create(uint8_t family, uint8_t type, uint8_t protocol, uint16_t port, network_interface_t nif)
{
    if (family!=AF_INET || !((type==SOCK_DGRAM && protocol==IPPROTO_UDP) || (type==SOCK_STREAM && protocol==IPPROTO_TCP)))
        return SOCKET_INVALID;

    sock_handle_t result = SOCKET_INVALID;
    socket_t* socket = new socket_t();
    if (socket) {
        wiced_result_t wiced_result;
        socket->set_type((protocol==IPPROTO_UDP ? socket_t::UDP : socket_t::TCP));
        if (protocol==IPPROTO_TCP) {
            wiced_result = wiced_tcp_create_socket(tcp(socket), wiced_wlan_interface(nif));
        }
        else {
            wiced_result = wiced_udp_create_socket(udp(socket), port, wiced_wlan_interface(nif));
        }
        if (wiced_result!=WICED_SUCCESS) {
            socket->set_type(socket_t::NONE);
            delete socket;
            result = as_sock_result(wiced_result);
        }
        else {
            SocketListLock lock(list_for(socket));
            add_socket(socket);
            result = as_sock_result(socket);
        }
    }
    return result;
}
예제 #3
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 );
}
예제 #4
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;
}
예제 #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 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;
}
예제 #7
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;
}
// The nonsecure server thread
static void tcp_server_nonsecure_thread_main(wiced_thread_arg_t arg)
{
    wiced_result_t result;
    wiced_tcp_stream_t stream;                      // The TCP stream
    wiced_tcp_socket_t socket;
    uint8_t rbuffer[MAX_LEGAL_MSG];

    char returnMessage[128]; // better use less than 128 bytes
    // setup the server by creating the socket and hooking it to the correct TCP Port
    result = wiced_tcp_create_socket(&socket, INTERFACE);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Create socket failed\n"));
        return; // this is a bad outcome
    }

    wiced_tcp_stream_init(&stream,&socket);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Init stream failed\n"));
        return; // this is a bad outcome
    }

    result = wiced_tcp_listen( &socket, TCP_SERVER_NONSECURE_LISTEN_PORT );
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Listen socket failed\n"));
        return;
    }


    while (1 )
    {

        result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection

        if(result != WICED_SUCCESS) // this occurs if the accept times out
            continue;

        nonsecureConnectionCount += 1;

        /// Figure out which client is talking to us... and on which port
        wiced_ip_address_t peerAddress;
        uint16_t	peerPort;
        wiced_tcp_server_peer(&socket,&peerAddress,&peerPort);

        uint32_t dataReadCount;
        wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms

        processClientCommand(rbuffer, dataReadCount ,returnMessage);

        displayResult(peerAddress,peerPort,returnMessage);


        // send response and close things up
        wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage));
        wiced_tcp_stream_flush(&stream);
        wiced_tcp_disconnect(&socket); // disconnect the connection

        wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left
        wiced_tcp_stream_init(&stream,&socket); // setup for next connection

    }
}
예제 #9
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;
}
static void tcp_server_thread_main(wiced_thread_arg_t arg)
{
    wiced_bool_t wwepSecurity = (wiced_bool_t)arg;

    wiced_result_t result;
    wiced_tcp_stream_t stream;                      // The TCP stream
    wiced_tcp_socket_t socket;
    platform_dct_security_t *dct_security;
    wiced_tls_identity_t tls_identity;
    wiced_tls_context_t tls_context;
    uint8_t rbuffer[MAX_LEGAL_MSG];

    char returnMessage[128]; // better use less than 128 bytes
    // setup the server by creating the socket and hooking it to the correct TCP Port
    result = wiced_tcp_create_socket(&socket, INTERFACE);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Create socket failed\n"));
        return; // this is a bad outcome
    }

    if(wwepSecurity == WICED_TRUE)
    {
        WPRINT_APP_INFO(("Starting secure\n"));

    }
    else
    {
        WPRINT_APP_INFO(("Starting non-secure\n"));
    }

    result = wiced_tcp_listen( &socket, (wwepSecurity == WICED_TRUE)?TCP_SERVER_SECURE_LISTEN_PORT:TCP_SERVER_NONSECURE_LISTEN_PORT );
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Listen socket failed\n"));
        return;
    }

    if(wwepSecurity == WICED_TRUE)
    {
        /* Lock the DCT to allow us to access the certificate and key */
        WPRINT_APP_INFO(( "Read the certificate Key from DCT\n" ));
        result = wiced_dct_read_lock( (void**) &dct_security, WICED_FALSE, DCT_SECURITY_SECTION, 0, sizeof( *dct_security ) );
        if ( result != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(("Unable to lock DCT to read certificate\n"));
            return;
        }

        /* Setup TLS identity */
        result = wiced_tls_init_identity( &tls_identity, dct_security->private_key, strlen( dct_security->private_key ), (uint8_t*) dct_security->certificate, strlen( dct_security->certificate ) );
        if ( result != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(( "Unable to initialize TLS identity. Error = [%d]\n", result ));
            return;
        }

    }
    else
    {
        wiced_tcp_stream_init(&stream,&socket);
        if(WICED_SUCCESS != result)
        {
            WPRINT_APP_INFO(("Init stream failed\n"));
            return; // this is a bad outcome
        }
    }

    while (1 )
    {
        if(wwepSecurity == WICED_TRUE)
        {
            result = wiced_tls_init_context( &tls_context, &tls_identity, NULL );
            if(result != WICED_SUCCESS)
            {
                WPRINT_APP_INFO(("Init context failed %d",result));
                return;
            }

            result = wiced_tcp_enable_tls(&socket,&tls_context);

            if(result != WICED_SUCCESS)
            {
                WPRINT_APP_INFO(("Enabling TLS failed %d",result));
                return;
            }

            wiced_tcp_stream_init(&stream,&socket);
            if(WICED_SUCCESS != result)
            {
                WPRINT_APP_INFO(("Init stream failed\n"));
                return; // this is a bad outcome
            }
        }

        result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection

        if(result != WICED_SUCCESS) // this occurs if the accept times out
            continue;

        if(wwepSecurity == WICED_TRUE)
            secureConnectionCount += 1;
        else
            nonsecureConnectionCount += 1;

        /// Figure out which client is talking to us... and on which port
        wiced_ip_address_t peerAddress;
        uint16_t	peerPort;
        wiced_tcp_server_peer(&socket,&peerAddress,&peerPort);

        uint32_t dataReadCount;
        wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms
        processClientCommand(rbuffer, dataReadCount ,returnMessage);

        displayResult(peerAddress,peerPort,returnMessage);


        // send response and close things up
        wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage));
        wiced_tcp_stream_flush(&stream);
        wiced_tcp_disconnect(&socket); // disconnect the connection

        if(wwepSecurity == WICED_TRUE)
        {
            wiced_tls_deinit_context(&tls_context);
        }

        wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left
        wiced_tcp_stream_init(&stream,&socket); // setup for next connection

    }
}