xref: /aosp_15_r20/external/pigweed/pw_hdlc/rpc_example/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_hdlc-rpc-example:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=====================
4*61c4878aSAndroid Build Coastguard WorkerRPC over HDLC example
5*61c4878aSAndroid Build Coastguard Worker=====================
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_hdlc
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThe :ref:`module-pw_hdlc` module includes an example of bringing up a
10*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_rpc` server that can be used to invoke RPCs. The example code
11*61c4878aSAndroid Build Coastguard Workeris located at `//pw_hdlc/rpc_example/
12*61c4878aSAndroid Build Coastguard Worker<https://cs.opensource.google/pigweed/pigweed/+/main:pw_hdlc/rpc_example/>`_.
13*61c4878aSAndroid Build Coastguard WorkerThis tutorial walks through invoking RPCs interactively and with a script using
14*61c4878aSAndroid Build Coastguard Workerthe RPC over HDLC example.
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard WorkerThe example implementation of the ``system_server`` facade from ``pw_rpc``
17*61c4878aSAndroid Build Coastguard Workersends HDLC-encoded RPC packets via ``pw_sys_io``. It has blocking sends and
18*61c4878aSAndroid Build Coastguard Workerreads so it is not suitable for performance-sensitive applications. This
19*61c4878aSAndroid Build Coastguard Workermostly serves as a simplistic example for quickly bringing up RPC over HDLC
20*61c4878aSAndroid Build Coastguard Workeron bare-metal targets.
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker.. note::
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard Worker   This tutorial assumes that you've got a :ref:`target-stm32f429i-disc1`
25*61c4878aSAndroid Build Coastguard Worker   board, but the instructions work with any target that has implemented
26*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_sys_io`.
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker-----------
29*61c4878aSAndroid Build Coastguard WorkerGet started
30*61c4878aSAndroid Build Coastguard Worker-----------
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker1. Set up your board
33*61c4878aSAndroid Build Coastguard Worker====================
34*61c4878aSAndroid Build Coastguard WorkerConnect the board you'll be communicating with. For the
35*61c4878aSAndroid Build Coastguard Worker:ref:`target-stm32f429i-disc1` board, connect the mini USB port, and note which
36*61c4878aSAndroid Build Coastguard Workerserial device it appears as (e.g. ``/dev/ttyACM0``).
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker2. Build Pigweed
39*61c4878aSAndroid Build Coastguard Worker================
40*61c4878aSAndroid Build Coastguard WorkerActivate the Pigweed environment and run the default build.
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker   . ./activate.sh
45*61c4878aSAndroid Build Coastguard Worker   pw package install nanopb
46*61c4878aSAndroid Build Coastguard Worker   gn gen out --args='dir_pw_third_party_nanopb="//environment/packages/nanopb"'
47*61c4878aSAndroid Build Coastguard Worker   ninja -C out
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker3. Flash the firmware image
50*61c4878aSAndroid Build Coastguard Worker===========================
51*61c4878aSAndroid Build Coastguard WorkerAfter a successful build, the binary for the example will be located at
52*61c4878aSAndroid Build Coastguard Worker``//out/<toolchain>/obj/pw_hdlc/rpc_example/bin/rpc_example.elf``.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard WorkerFlash this image to your board. If you are using the
55*61c4878aSAndroid Build Coastguard Worker:ref:`target-stm32f429i-disc1` board you can flash the image with
56*61c4878aSAndroid Build Coastguard Worker`OpenOCD <http://openocd.org>`_.
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker   openocd -f \
61*61c4878aSAndroid Build Coastguard Worker     targets/stm32f429i_disc1/py/stm32f429i_disc1_utils/openocd_stm32f4xx.cfg \
62*61c4878aSAndroid Build Coastguard Worker     -c "program \
63*61c4878aSAndroid Build Coastguard Worker     out/stm32f429i_disc1_debug/obj/pw_hdlc/rpc_example/bin/rpc_example.elf \
64*61c4878aSAndroid Build Coastguard Worker     verify reset exit"
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker4. Invoke RPCs from an interactive console
67*61c4878aSAndroid Build Coastguard Worker==========================================
68*61c4878aSAndroid Build Coastguard WorkerThe RPC console uses :ref:`module-pw_console` to make a rich interactive
69*61c4878aSAndroid Build Coastguard Workerconsole for working with ``pw_rpc``. Run the RPC console with the following
70*61c4878aSAndroid Build Coastguard Workercommand, replacing ``/dev/ttyACM0`` with the correct serial device for your
71*61c4878aSAndroid Build Coastguard Workerboard.
72*61c4878aSAndroid Build Coastguard Worker
73*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker   pw-system-console --no-rpc-logging --proto-globs pw_rpc/echo.proto \
76*61c4878aSAndroid Build Coastguard Worker     --device /dev/ttyACM0
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard WorkerRPCs may be accessed through the predefined ``rpcs`` variable. RPCs are
79*61c4878aSAndroid Build Coastguard Workerorganized by their protocol buffer package and RPC service, as defined in a
80*61c4878aSAndroid Build Coastguard Worker.proto file. The ``Echo`` method is part of the ``EchoService``, which
81*61c4878aSAndroid Build Coastguard Workeris in the ``pw.rpc`` package. To invoke it synchronously, call
82*61c4878aSAndroid Build Coastguard Worker``rpcs.pw.rpc.EchoService.Echo()``:
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker.. code-block:: pycon
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker   >>> device.rpcs.pw.rpc.EchoService.Echo(msg='Hello, world!')
87*61c4878aSAndroid Build Coastguard Worker   (Status.OK, pw.rpc.EchoMessage(msg='Hello, world!'))
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker5. Invoke RPCs with a script
90*61c4878aSAndroid Build Coastguard Worker============================
91*61c4878aSAndroid Build Coastguard WorkerRPCs may also be invoked from Python scripts. Close the RPC console if it is
92*61c4878aSAndroid Build Coastguard Workerrunning, and execute the example script. Set the ``--device`` argument to the
93*61c4878aSAndroid Build Coastguard Workerserial port for your device.
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker   python pw_hdlc/rpc_example/example_script.py --device /dev/ttyACM0
98*61c4878aSAndroid Build Coastguard Worker
99*61c4878aSAndroid Build Coastguard WorkerYou should see this output:
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker   The status was Status.OK
104*61c4878aSAndroid Build Coastguard Worker   The payload was msg: "Hello"
105*61c4878aSAndroid Build Coastguard Worker
106*61c4878aSAndroid Build Coastguard Worker   The device says: Goodbye!
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard Worker-------------------------
109*61c4878aSAndroid Build Coastguard WorkerLocal RPC example project
110*61c4878aSAndroid Build Coastguard Worker-------------------------
111*61c4878aSAndroid Build Coastguard WorkerThis example is similar to the above example, except it uses a socket to
112*61c4878aSAndroid Build Coastguard Workerconnect a server to a client, both running on the host.
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard Worker1. Build Pigweed
115*61c4878aSAndroid Build Coastguard Worker================
116*61c4878aSAndroid Build Coastguard WorkerActivate the Pigweed environment and build the code.
117*61c4878aSAndroid Build Coastguard Worker
118*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
119*61c4878aSAndroid Build Coastguard Worker
120*61c4878aSAndroid Build Coastguard Worker   . ./activate.sh
121*61c4878aSAndroid Build Coastguard Worker   pw package install nanopb
122*61c4878aSAndroid Build Coastguard Worker   gn gen out --args='dir_pw_third_party_nanopb="//environment/packages/nanopb"'
123*61c4878aSAndroid Build Coastguard Worker   ninja -C out
124*61c4878aSAndroid Build Coastguard Worker
125*61c4878aSAndroid Build Coastguard Worker2. Start the server
126*61c4878aSAndroid Build Coastguard Worker===================
127*61c4878aSAndroid Build Coastguard WorkerRun a ``pw_rpc`` server in one terminal window.
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard Worker   ./out/pw_strict_host_clang_debug/obj/pw_hdlc/rpc_example/bin/rpc_example
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker3. Start the client
134*61c4878aSAndroid Build Coastguard Worker===================
135*61c4878aSAndroid Build Coastguard WorkerIn a separate Pigweed-activated terminal, run the ``pw-system-console`` RPC
136*61c4878aSAndroid Build Coastguard Workerclient with ``--proto-globs`` set to ``pw_rpc/echo.proto``. Additional protos
137*61c4878aSAndroid Build Coastguard Workercan be added if needed.
138*61c4878aSAndroid Build Coastguard Worker
139*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard Worker   pw-system-console --no-rpc-logging --proto-globs pw_rpc/echo.proto \
142*61c4878aSAndroid Build Coastguard Worker     --socket-addr default
143*61c4878aSAndroid Build Coastguard Worker
144*61c4878aSAndroid Build Coastguard Worker.. tip::
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker   The ``--socket-addr`` value may be replaced with an IP and port separated by
147*61c4878aSAndroid Build Coastguard Worker   a colon, e.g. ``127.0.0.1:33000``. If using a Unix socket, the path to the
148*61c4878aSAndroid Build Coastguard Worker   file follows ``file:``, e.g. ``file:/path/to/unix/socket``. Unix socket
149*61c4878aSAndroid Build Coastguard Worker   Python support is pending, see `python/cpython#77589
150*61c4878aSAndroid Build Coastguard Worker   <https://github.com/python/cpython/issues/77589>`_.
151*61c4878aSAndroid Build Coastguard Worker
152*61c4878aSAndroid Build Coastguard Worker.. tip::
153*61c4878aSAndroid Build Coastguard Worker
154*61c4878aSAndroid Build Coastguard Worker   The default RPC channel ID (``1``) can be overriden with ``--channel-id``.
155*61c4878aSAndroid Build Coastguard Worker
156*61c4878aSAndroid Build Coastguard Worker4. Invoke RPCs from the client
157*61c4878aSAndroid Build Coastguard Worker==============================
158*61c4878aSAndroid Build Coastguard WorkerInvoke RPCs from the interactive console on the client side.
159*61c4878aSAndroid Build Coastguard Worker
160*61c4878aSAndroid Build Coastguard Worker.. code-block:: pycon
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker   >>> device.rpcs.pw.rpc.EchoService.Echo(msg='Hello, world!')
163*61c4878aSAndroid Build Coastguard Worker   (Status.OK, pw.rpc.EchoMessage(msg='Hello, world!'))
164*61c4878aSAndroid Build Coastguard Worker
165*61c4878aSAndroid Build Coastguard Worker.. seealso::
166*61c4878aSAndroid Build Coastguard Worker
167*61c4878aSAndroid Build Coastguard Worker   - The :ref:`module-pw_console`
168*61c4878aSAndroid Build Coastguard Worker     :bdg-ref-primary-line:`module-pw_console-user_guide` for more info on using
169*61c4878aSAndroid Build Coastguard Worker     the the pw_console UI.
170*61c4878aSAndroid Build Coastguard Worker
171*61c4878aSAndroid Build Coastguard Worker   - The target docs for other RPC-enabled application examples:
172*61c4878aSAndroid Build Coastguard Worker
173*61c4878aSAndroid Build Coastguard Worker     - :bdg-ref-primary-line:`target-host-device-simulator`
174*61c4878aSAndroid Build Coastguard Worker     - :bdg-ref-primary-line:`target-rp2040`
175*61c4878aSAndroid Build Coastguard Worker     - :bdg-ref-primary-line:`target-stm32f429i-disc1-stm32cube`
176*61c4878aSAndroid Build Coastguard Worker
177*61c4878aSAndroid Build Coastguard Worker-----------------
178*61c4878aSAndroid Build Coastguard WorkerMore pw_hdlc docs
179*61c4878aSAndroid Build Coastguard Worker-----------------
180*61c4878aSAndroid Build Coastguard Worker.. include:: ../docs.rst
181*61c4878aSAndroid Build Coastguard Worker   :start-after: .. pw_hdlc-nav-start
182*61c4878aSAndroid Build Coastguard Worker   :end-before: .. pw_hdlc-nav-end
183