zephyr/samples/subsys/zbus/uart_bridge
Alberto Escolar Piedras cb53e40ff9 drivers uart_native_posix: rename to native_pty and support N instances
Rename the driver from uart_native_posix to uart_native_pty.
Including renaming the DTS compatible, and kconfig options, deprecating
the old ones.

And refactor the driver, generalizing it, so we can have any number of
instances.
Note that, unfortunately generalizing to N instances cannot be done
without a degree of backwards compatibility breakage: This driver was
born with all its configuration and selection of the instances based on
kconfig.
When the driver was made to use DT, it was done in a way that required
both DT and kconfig needing to manually coherently enable the 2nd UART.
This has now been fixed, which it means only DT is used to decide how
many instances are avaliable, and UART_NATIVE_POSIX_PORT_1_ENABLE is
just ignored.

Including:
* Deprecate UART_NATIVE_WAIT_PTS_READY_ENABLE: the options is always on
  now as it has no practical drawbacks.
* Deprecate UART_NATIVE_POSIX_PORT_1_ENABLE: DTS intanciation defines it
  being available now.
* Rename a few functions and in general shorten pseudo-tty/pseudo-
  terminal to PTY instead of PTTY.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-03-11 18:54:02 +01:00
..
boards drivers uart_native_posix: rename to native_pty and support N instances 2025-03-11 18:54:02 +01:00
src
CMakeLists.txt
decoder.py
prj.conf
README.rst
sample.yaml

.. zephyr:code-sample:: zbus-uart-bridge
   :name: UART bridge
   :relevant-api: zbus_apis

   Redirect channel events to the host over UART.

Overview
********

This simple application demonstrates a UART redirection of all channel events to the host.
The device sends information to the script running on a computer host. The script decodes it and prints it to the stdout.

Building and Running
********************

This project outputs to the console. It can be built and executed
on native_sim as follows:

.. zephyr-app-commands::
   :zephyr-app: samples/subsys/zbus/uart_bridge
   :host-os: unix
   :board: native_sim
   :goals: run

Sample Output
=============

.. code-block:: console

    [0/1] cd .../zephyr/build/zephyr/zephyr.exe
    uart_1 connected to pseudotty: /dev/pts/2
    uart connected to pseudotty: /dev/pts/3
    *** Booting Zephyr OS build zephyr-v3.1.0 ***
    D: Core sending start measurement with status 0
    D: START processing channel start_measurement change
    D: FINISH processing channel start_measurement change
    D: Peripheral sending sensor data
    D: START processing channel sensor_data change
    D: FINISH processing channel sensor_data change
    D: Bridge Started
    D: Channel start_measurement claimed
    D: Channel start_measurement finished
    D: Bridge send start_measurement
    D: Channel sensor_data claimed
    D: Channel sensor_data finished
    D: Bridge send sensor_data
    D: Core sending start measurement with status 1
    D: START processing channel start_measurement change
    D: FINISH processing channel start_measurement change
    D: Channel start_measurement claimed
    D: Channel start_measurement finished
    D: Bridge send start_measurement
    D: Peripheral sending sensor data
    D: START processing channel sensor_data change
    D: FINISH processing channel sensor_data change
    D: Channel sensor_data claimed
    D: Channel sensor_data finished
    D: Bridge send sensor_data

    <repeats endlessly>

Exit execution by pressing :kbd:`CTRL+C`.

The :file:`decoder.py` script can be executed using the following command:

.. code-block:: bash

    python3.8 samples/subsys/zbus/uart_bridge/decoder.py /dev/pts/2


Note the run command above prints the value of pts port because it is running in
:ref:`native_sim <native_sim>`.
Look at the line indicating ``uart_1 connected to pseudotty: /dev/pts/2``.
It can be different in your case. If you are using a board, read the documentation to get the
correct port destination (in Linux is something like ``/dev/tty...`` or in Windows ``COM...``).

From the serial decoder (Python script), you would see something like this:

.. code-block:: shell

    PUB [sensor_data] -> b'\xc5\x01\x00\x00\xb2\x11\x00\x00'
    PUB [start_measurement] -> b'\x00'
    PUB [sensor_data] -> b'\xc6\x01\x00\x00\xbc\x11\x00\x00'
    PUB [start_measurement] -> b'\x01'
    PUB [sensor_data] -> b'\xc7\x01\x00\x00\xc6\x11\x00\x00'
    PUB [start_measurement] -> b'\x00'
    PUB [sensor_data] -> b'\xc8\x01\x00\x00\xd0\x11\x00\x00'
    PUB [start_measurement] -> b'\x01'
    PUB [sensor_data] -> b'\xc9\x01\x00\x00\xda\x11\x00\x00'
    PUB [start_measurement] -> b'\x00'
    PUB [sensor_data] -> b'\xca\x01\x00\x00\xe4\x11\x00\x00'
    PUB [start_measurement] -> b'\x01'
    PUB [sensor_data] -> b'\xcb\x01\x00\x00\xee\x11\x00\x00'
    PUB [start_measurement] -> b'\x00'
    PUB [sensor_data] -> b'\xcc\x01\x00\x00\xf8\x11\x00\x00'
    PUB [start_measurement] -> b'\x01'
    PUB [sensor_data] -> b'\xcd\x01\x00\x00\x02\x12\x00\x00'
    PUB [start_measurement] -> b'\x00'
    PUB [sensor_data] -> b'\xce\x01\x00\x00\x0c\x12\x00\x00'
    PUB [start_measurement] -> b'\x01'
    PUB [sensor_data] -> b'\xcf\x01\x00\x00\x16\x12\x00\x00'
    PUB [start_measurement] -> b'\x00'

Exit the decoder script by pressing :kbd:`CTRL+C`.