cancel
Showing results for 
Search instead for 
Did you mean: 

Has anyone successfully used NetX Duo websockets?

robmilne
Associate III

I lifted regression test code from the netxduo stack to attempt a websocket client, but the best I can achieve is a 400 response from the server.  Has anyone successfully using websockets with STM32 + netxduo?

The following code resides inside app_netxduo.c.  The thread is created TX_DONT_START and is resumed in the nx_app_thread_entry thread.  It dies on line 59 with a  NX_WEBSOCKET_INVALID_STATUS_CODE return.  I am trying to connect with a flask-socketio project running on a Ubuntu system.

 

 

#include "nx_websocket_client.h"

#define DEMO_STACK_SIZE      4096
#define TEST_SERVER_ADDRESS  IP_ADDRESS(192,168,100,2)
#define TEST_SERVER_PORT     8000
#define TEST_HOST_NAME       "192.168.100.2"
#define TEST_URI_PATH        "/ws"
#define TEST_PROTOCOL        " "

static UCHAR client_test_data[4] = { 0x11, 0x22, 0x33, 0x44 };

void thread_client_entry(ULONG thread_input)
{
    UINT            status;
    NX_PACKET       *packet_ptr;
    NXD_ADDRESS     server_ip_address;
    UINT            code;

    /* Create client socket.  */
    status = nx_tcp_socket_create(&NetXDuoEthIpInstance, &test_client,
                                  "Client Socket", NX_IP_NORMAL,
                                  NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE,
                                  WINDOW_SIZE, NX_NULL, NX_NULL);
    assert(NX_SUCCESS == status);

    /* Create WebSocket.  */
    status = nx_websocket_client_create(&client_websocket, (UCHAR *)" ",
                                        &NetXDuoEthIpInstance, &NxAppPool);
    assert(NX_SUCCESS == status);

    /* Give IP task and driver a chance to initialize the system.  */
    tx_thread_sleep(NX_IP_PERIODIC_RATE);

    /* Bind and connect to server.  */
    status = nx_tcp_client_socket_bind(&test_client, TEST_SERVER_PORT,
                                       NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);

    /* Wait test server started.
    while (!test_server_start)
    { */
        tx_thread_sleep(NX_IP_PERIODIC_RATE * 10);
    //}

    /* Set server IP address.  */
    server_ip_address.nxd_ip_address.v4 = TEST_SERVER_ADDRESS;
    server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;

    status = nxd_tcp_client_socket_connect(&test_client, &server_ip_address,
                                           TEST_SERVER_PORT, NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);

    /* Upgrade to websocket */
    status = nx_websocket_client_connect(&client_websocket, &test_client,
                                         (UCHAR *)TEST_HOST_NAME, sizeof(TEST_HOST_NAME) - 1,
                                         (UCHAR *)TEST_URI_PATH, sizeof(TEST_URI_PATH) - 1,
                                         (UCHAR *)TEST_PROTOCOL, sizeof(TEST_PROTOCOL) - 1,
                                         NX_WAIT_FOREVER);
    assert(NX_SUCCESS == status);

    status = nx_packet_allocate(&NxAppPool, &packet_ptr, NX_TCP_PACKET,
                                NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);

    /* Append and send data.  */
    packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
    packet_ptr -> nx_packet_length = 0;
    status = nx_packet_data_append(packet_ptr, client_test_data,
                                   sizeof(client_test_data), &NxAppPool,
                                   NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);
    status = nx_websocket_client_send(&client_websocket, packet_ptr,
                                      NX_WEBSOCKET_OPCODE_BINARY_FRAME,
                                      NX_TRUE, NX_WAIT_FOREVER);
    assert(NX_SUCCESS == status);

    /* Receive the responsed data from server.  */
    status = nx_websocket_client_receive(&client_websocket, &packet_ptr, &code,
                                         NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);
    /* The first response from the server shall be an unfragmented frame  */
    if (client_websocket.nx_websocket_client_frame_fragmented == NX_TRUE)
        assert(NX_SUCCESS == status);

    nx_packet_release(packet_ptr);

    status = nx_websocket_client_delete(&client_websocket);
    if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        assert(0);

    /* TCP Disconnect.  */
    status = nx_tcp_socket_disconnect(&test_client, NX_IP_PERIODIC_RATE);
    assert(NX_SUCCESS == status);

    nx_tcp_client_socket_unbind(&test_client);
    nx_tcp_socket_delete(&test_client);
}

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mbarg.1
Associate III

We have websocket server solution, it works great IPv4 & IPv6, text and binary messages, to and from, but it requires at least 2 modification to NetxDuo libraries -

  1. one to test incoming packets for websocket upgrade request and branch to dedicated function,
  2. second to test non-ascii reply (websockets messages are of this kind) and call another dedicated function, 
  3. third modification (recommended but not mandatory) to add a send TCP message to TCP Server Thread, to easy possible concurrency problems,

as there are no USER CODE entry points in libraries.

Code is now 2 year in field operaton, withstand any bad party attack.

This make a proprietary solution and not everybody is happy with that.

If you are interested, we can put some project online for evaluation.

 

View solution in original post

3 REPLIES 3
STea
ST Employee

Hello @robmilne ,

this is clearly not a hardware related issue, but a problem related to software implementation of  websocket client.

I would recommend you check the availability of the server manually when this problem happens and i would not recommend setting delays like this in your thread try implementing this example using callbacks to make it easier to debug and upgrade as well. use Wireshark to inspect the traffic will help you understand what is happening. 


Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
mbarg.1
Associate III

We have websocket server solution, it works great IPv4 & IPv6, text and binary messages, to and from, but it requires at least 2 modification to NetxDuo libraries -

  1. one to test incoming packets for websocket upgrade request and branch to dedicated function,
  2. second to test non-ascii reply (websockets messages are of this kind) and call another dedicated function, 
  3. third modification (recommended but not mandatory) to add a send TCP message to TCP Server Thread, to easy possible concurrency problems,

as there are no USER CODE entry points in libraries.

Code is now 2 year in field operaton, withstand any bad party attack.

This make a proprietary solution and not everybody is happy with that.

If you are interested, we can put some project online for evaluation.

 

robmilne
Associate III

Thank-you!  I would very much appreciate your sharing of a solution.  Perhaps the maintainer of the stack will adopt your changes so that future back ports won't be necessary.  Have you considered submitting a fork for their consideration?