Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lws]: Add initial support for libwebsockets #718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

glmfe
Copy link
Collaborator

@glmfe glmfe commented Dec 19, 2024

Description

  • Add libwebsockets as component
  • Created simple client example
  • Created simple server echo example

@CLAassistant
Copy link

CLAassistant commented Dec 19, 2024

CLA assistant check
All committers have signed the CLA.

@glmfe glmfe changed the title Add libwebsockets as component [libwebsockets]: Add initial support for libwebsockets Jan 29, 2025
@glmfe glmfe force-pushed the feat/lws branch 3 times, most recently from 91c836c to b900029 Compare January 29, 2025 20:38
@glmfe glmfe self-assigned this Jan 29, 2025
@glmfe glmfe added the lws libwebsockets label Jan 29, 2025
@glmfe glmfe changed the title [libwebsockets]: Add initial support for libwebsockets [lws]: Add initial support for libwebsockets Jan 29, 2025
@glmfe glmfe force-pushed the feat/lws branch 6 times, most recently from 05aca45 to 860ee1e Compare January 30, 2025 03:52
@@ -3,6 +3,9 @@ idf_component_register()
option(LWS_WITH_EXPORT_LWSTARGETS "Export libwebsockets CMake targets. Disable if they conflict with an outer cmake project." OFF)
set(LWS_WITH_EXPORT_LWSTARGETS OFF)

option(LWS_PLAT_FREERTOS "Build for FreeRTOS" ON)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more common to have options set through Kconfig.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will try to remove the option lines

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried both cases:

  • Simply removing the "option(..)"
  • Moved the "sets" to after the "add_subditrectory".

In both cases I get this error:

image

which is acutally caused by the miss of the first setting
set(LWS_WITH_EXPORT_LWSTARGETS OFF)

@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can require a more recent version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to 3.16

@@ -0,0 +1,5 @@
version: "2.0.28~0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the version match the libwebsockets release.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will bump to lws version

url: https://github.com/espressif/esp-protocols/tree/master/components/libwebsockets
description: TODO:glmfe
dependencies:
idf: '>=5.1'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idf: '>=5.1'
idf: '>=5.0'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,7 @@
# Changelog

## [0.1.0](https://github.com/espressif/esp-protocols/commits/libwebsockets-v0.1.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version to match libwebsocket version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will bump to lws version

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

## Example Output

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing expected output.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

LWS_PROTOCOL_LIST_TERM
};

int app_main(int argc, const char **argv)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int app_main(int argc, const char **argv)
int app_main(void)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ESP_ERROR_CHECK(example_connect());

/* Configure WDT. */
esp_task_wdt_config_t esp_task_wdt_config = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you had watchdog issues?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was getting this error message constantly:

E (x) task_wdt: esp_task_wdt_reset(705): task not found

This was being raised by the wdt funtion "esp_task_wdt_reset". So basically, the LWS was kicking the watchdog but hadn't actually registered the task to begin with. So the solution was to add:

    TaskHandle_t handle = xTaskGetCurrentTaskHandle();
    esp_task_wdt_add(handle);

The code snippet above fixed this error message. However, a new issue might show up, where a timeout occurs if the task hangs for some reason. To avoid an unnecessary system reset, portMAX_DELAY is set, especially in these two scenarios:

  1. Client: The task stops to wait for the STDIN input. If this is manually done, it will reset the target.
  2. Server: If the task is waiting for a request, the system may hang if no request is received in time. One potential fix would be to set a shorter LWS timeout than the watchdog timer, ensuring that the timeout triggers before the watchdog and can reset it again. However, I encountered some connection instability when implementing this solution. If necessary, I can further investigate and refine this fix to improve stability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm positive that there might be a better solution for both cases than setting the WDT timer to max, but I don’t think it’s crucial for the outcome of this example. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@david-cermak Just tagging you since you also showed interest in this section. :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E (x) task_wdt: esp_task_wdt_reset(705): task not found

This error message? even if you removed everything related to the task watchdog? this is weird. I'd expect something like

... Task watchdog triggered. The following tasks did not reset the watchdog in time...

@@ -0,0 +1,12 @@
set(SRC_FILES "lws-client-echo.c") # Define source files
set(INCLUDE_DIRS ".") # Define include directories
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually make examples as projects not components.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the project is in the cmake one level up (client-echo/CmakeList.txt). It's based on esp_websocket_client/examples/target/

extern int __real_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
extern int __wrap_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);

int __wrap_mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you had to use wrap here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. mbedtls_ssl_handshake_step wrap: The issue here was that Libwebsockets wasn't properly handling the asynchronous responses MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE. Without this wrap, the handshake process would fail when it shouldn't, so we added the wrap to correctly handle these responses and prevent unnecessary failures.

  2. lws_adopt_descriptor_vhost wrap: In this case, the problem was that the struct member info.fi_wsi_name was never being set. On Linux, this didn't cause issues because the vsnprintf function simply printed a (null) string when given a null argument. However, for embedded systems, this behavior resulted in a panic because there was no filtering for null arguments. The wrap was added to address this issue by ensuring the fi_wsi_name was set to a dummy value and preventing the panic.

@@ -0,0 +1,202 @@

Apache License
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lws is distributed mostly under MIT, will probably need to switch to MIT license

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to MIT license used by LWS

Copy link
Collaborator

@david-cermak david-cermak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work overall!

#include "stdio.h"
#include <libwebsockets.h>

extern int __real_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed, but maybe useful to keep here (I'd just add a comment that we don't need the original handshake impl)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the comments, thanks!

#include <libwebsockets.h>

extern int __real_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
extern int __wrap_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not extern, right?

Suggested change
extern int __wrap_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
int __wrap_mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, not even necessary, removed :D

@@ -0,0 +1,49 @@
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick, also please cleanup the copyrights -- for new files use 2025

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@glmfe glmfe marked this pull request as ready for review January 31, 2025 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lws libwebsockets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants