README.rst
1.. zephyr:code-sample:: zbus-remote-mock
2 :name: Remote mock sample
3 :relevant-api: zbus_apis
4
5 Publish to a zbus instance using UART as a bridge.
6
7Overview
8********
9
10This application demonstrates how a host script can publish to the zbus in an embedded device using the UART as a bridge.
11The device sends information to the script running on a computer host. Then, the script sends back information when it would simulate a publish to some channel. Finally, the script decodes the data exchanged and prints it to the stdout.
12
13With this approach, developers can implement tests using any language on a computer to talk directly via channels with threads running on a device. Furthermore, it gives the tests more controllability and observability since we can control and access what is sent and received by the script.
14
15Building and Running
16********************
17
18This project outputs to the console. It can be built and executed
19on :ref:`native_sim <native_sim>` as follows:
20
21.. zephyr-app-commands::
22 :zephyr-app: samples/subsys/zbus/remote_mock
23 :host-os: unix
24 :board: native_sim
25 :goals: run
26
27Sample Output
28=============
29
30.. code-block:: console
31
32 -- west build: running target run
33 [0/1] cd /.../zephyr/build/zephyr/zephyr.exe
34 uart_1 connected to pseudotty: /dev/pts/2
35 uart connected to pseudotty: /dev/pts/3
36 *** Booting Zephyr OS build zephyr-v3.1.0 ***
37 D: [Mock Proxy] Started.
38 D: [Mock Proxy RX] Started.
39 D: [Mock Proxy RX] Found sentence
40 D: Channel start_measurement claimed
41 D: Channel start_measurement finished
42 D: Publishing channel start_measurement
43 D: START processing channel start_measurement change
44 D: Channel start_measurement claimed
45 D: discard loopback event (channel start_measurement)
46 D: Channel start_measurement finished
47 D: FINISH processing channel start_measurement change
48 D: START processing channel sensor_data change
49 D: Channel sensor_data claimed
50 D: sending message to host (channel sensor_data)
51 D: Channel sensor_data finished
52 D: FINISH processing channel sensor_data change
53 D: sending sensor data err = 0
54
55 <repeats endlessly>
56
57Exit execution by pressing :kbd:`CTRL+C`.
58
59The :file:`remote_mock.py` script can be executed using the following command:
60
61.. code-block:: bash
62
63 python3.8 samples/subsys/zbus/remote_mock/remote_mock.py /dev/pts/2
64
65
66Note the run command above prints the value of pts port because it is running in
67:ref:`native_sim <native_sim>`.
68Look at the line indicating ``uart_1 connected to pseudotty: /dev/pts/2``.
69It can be different in your case. If you are using a board, read the documentation to get the
70correct port destination (in Linux is something like ``/dev/tty...`` or in Windows ``COM...``).
71
72From the remote mock (Python script), you would see something like this:
73
74.. code-block:: shell
75
76 Proxy PUB [start_measurement] -> start measurement
77 Proxy NOTIFY: [sensor_data] -> sensor value 1
78 Proxy PUB [start_measurement] -> start measurement
79 Proxy NOTIFY: [sensor_data] -> sensor value 2
80 Proxy PUB [start_measurement] -> start measurement
81 Proxy NOTIFY: [sensor_data] -> sensor value 3
82 Proxy PUB [start_measurement] -> start measurement
83 Proxy NOTIFY: [sensor_data] -> sensor value 4
84 Proxy PUB [start_measurement] -> start measurement
85 Proxy NOTIFY: [sensor_data] -> sensor value 5
86 Proxy PUB [start_measurement] -> start measurement
87 Proxy NOTIFY: [sensor_data] -> sensor value 6
88 Proxy PUB [start_measurement] -> start measurement
89 Proxy NOTIFY: [sensor_data] -> sensor value 7
90 Proxy PUB [start_measurement] -> start measurement
91 Proxy NOTIFY: [sensor_data] -> sensor value 8
92 Proxy PUB [start_measurement] -> start measurement
93 Proxy NOTIFY: [sensor_data] -> sensor value 9
94 Proxy PUB [start_measurement] -> start measurement
95 Proxy NOTIFY: [sensor_data] -> sensor value 10
96
97 <continues>
98
99Exit the remote mock script by pressing :kbd:`CTRL+C`.
100