tmedicci opened a new pull request, #3540:
URL: https://github.com/apache/nuttx-apps/pull/3540

   ## Summary
   
   <!-- This field should contain a summary of the changes. It will be 
pre-filled with the commit's message and descriptions. Adjust it accordingly -->
   
   * examples/lws_echo: Add a simple websocket server example
     * Adds a simple websocket server example that echoes data back to the 
client using libwebsockets.
   
   * netutils/libwebsockets: Fix error regarding building with CMake
     * Prior to this change, if an empty folder existed instead of the actual 
libwebsockets source, the build would fail. That was changed to 
   check for an actual file instead, avoid such kind of errors.
   
   * netutils/libwebsockets: Enable libwebsockets server
     * Enables building libwebsockets server based on a new Kconfig entry.
   
   * netutils/libwebsockets: Remove hard dependency on mbedTLS
     * libwebsockets can be built without TLS support. To allow this, it was 
necessary to create a specific Kconfig option that enables TLS
   support if OPENSSL_MBEDTLS_WRAPPER is enabled. Otherwise, TLS is not 
supported (but libwebsockets can still be used with plain ws://
   connections).
   
   ## Impact
   
   Impact on user: Yes. Enable using `libwebsockets` on simpler applications, 
including creating a websocket server.
   
   Impact on build: Yes. Enable using both CMake and Make build system.
   
   Impact on hardware: No.
   
   Impact on documentation: No.
   
   Impact on security: No.
   
   Impact on compatibility: No.
   
   ## Testing
   
   This PR can be tested using, for instance, 
`esp32p4-function-ev-board:ethernet` defconfig using both CMake and Make:
    
   ### Building
   
   #### CMake-based
   
   ```
   rm -rf build && cmake -B build 
-DBOARD_CONFIG=esp32p4-function-ev-board:ethernet -GNinja && kconfig-tweak 
--file build/.config --enable CONFIG_ALLOW_BSD_COMPONENTS && kconfig-tweak 
--file build/.config --enable CONFIG_DEV_URANDOM && kconfig-tweak --file 
build/.config --enable CONFIG_EVENT_FD && kconfig-tweak --file build/.config 
--enable CONFIG_PSEUDOFS_SOFTLINKS && kconfig-tweak --file build/.config 
--enable CONFIG_NETUTILS_LIBWEBSOCKETS && kconfig-tweak --file build/.config 
--enable CONFIG_NETUTILS_LIBWEBSOCKETS_SERVER && kconfig-tweak --file 
build/.config --enable CONFIG_EXAMPLES_LWS_ECHO && cmake --build build -t 
olddefconfig && cmake --build build && ESPTOOL_PORT=/dev/ttyACM0 cmake --build 
build -t flash
   ```
   
   #### Make-based
   
   ```
   make distclean && ./tools/configure.sh -S esp32p4-function-ev-board:ethernet 
&& kconfig-tweak --enable CONFIG_ALLOW_BSD_COMPONENTS && kconfig-tweak --enable 
CONFIG_DEV_URANDOM && kconfig-tweak --enable CONFIG_EVENT_FD && kconfig-tweak 
--enable CONFIG_PSEUDOFS_SOFTLINKS && kconfig-tweak --enable 
CONFIG_NETUTILS_LIBWEBSOCKETS && kconfig-tweak --enable 
CONFIG_NETUTILS_LIBWEBSOCKETS_SERVER && kconfig-tweak --enable 
CONFIG_EXAMPLES_LWS_ECHO && make olddefconfig -s -j && make -j bootloader && 
make flash ESPTOOL_PORT=/dev/ttyACM0 -s -j$(nproc)
   ```
   
   ### Running
   
   The test is done by connecting the device to a known network and running the 
`lws_echo` application on NuttX and a test script on the host PC:
   
   #### NuttX
   
   Check the serial terminal using `picocom` (or any other similar software):
   
   ```
   picocom -b 115200 /dev/ttyUSB1
   ```
   
   Then, on NSH, run `lws_echo &`:
   
   ```
   nsh> lws_echo &
   lws_echo [7:100]
   nsh> lws_echo: starting WebSocket echo server on port 9000
   lws_echo: connect with ws://<device-ip>:9000 (subprotocol echo-protocol)
   ```
   
   #### Host PC
   
   Run the following Python script on the Host PC (adjust the device's IP 
address set at the `uri` var):
   
   ```
   import asyncio
   import websockets
   
   async def test():
       uri = "ws://10.0.10.50:9000"
       async with websockets.connect(
           uri, subprotocols=["echo-protocol"], open_timeout=5
       ) as ws:
           msg = "Hello libwebsockets no-TLS!"
           await ws.send(msg)
           reply = await ws.recv()
           print(f"Sent: {msg}")
           print(f"Recv: {reply}")
           assert reply == msg
           print("PASS")
   
   asyncio.run(test())
   ```
   
   And run it with:
   
   ```
   python websocket_test.py
   ```
   
   ### Results
   
   #### NuttX
   
   ```
   [2019/11/29 00:00:45:3100] N: localhost: lws_create_context: LWS: 
4.3.1-unknown, NET CLI SRV H1 H2 WS ConMon IPv6-absent
   [2019/11/29 00:00:45:3100] N: localhost: __lws_lc_tag:  ++ [wsi|0|pipe] (1)
   [2019/11/29 00:00:45:3100] N: localhost: __lws_lc_tag:  ++ 
[vh|0|localhost||9000] (1)
   [2019/11/29 00:00:45:3200] N: [vh|0|localhost||9000]: lws_socket_bind: 
source ads 0.0.0.0
   [2019/11/29 00:00:45:3300] N: localhost: __lws_lc_tag:  ++ 
[wsi|1|listen|localhost||9000] (2)
   lws_echo: server running
   [2019/11/29 00:00:54:9700] N: localhost: __lws_lc_taglws_echo: client 
connected
   lws_echo: client disconnected
   :  ++ [wsisrv|0|adopted] (1)
   [2019/11/29 00:00:55:0200] N: localhost: __lws_lc_untag:  -- 
[wsisrv|0|adopted] (0) 50.000ms
   ```
   
   #### Host PC
   
   The Host PC will echo back the previously sent message:
   
   ```
   $ python websocket_test.py
   Sent: Hello libwebsockets no-TLS!
   Recv: Hello libwebsockets no-TLS!
   PASS
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to