03 - RPC

This example illustrates how to create and call RPCs using pw_rpc and pw_console.

Trying out the RPC example

  1. Build the test with pw build or pw watch.

  2. Launch the RPC example on a device, or using the simulated device.

    STM32F429I_DISC1 (Linux/MacOS)

    • Flash rpc_main.elf:

      pw flash --device STM32-Discovery out/gn/stm32f429i_disc1_stm32cube.size_optimized/obj/examples/03_rpc/bin/rpc_main.elf
      

      Note

      We don’t yet have OpenOCD for Windows. See b/300986008 for updates.

    Raspberry Pi Pico (RP2404) (Windows/Linux/MacOS)

    • Flash ./out/gn/rp2040.size_optimized/obj/examples/03_rpc/rpc_main.uf2:

      1. Reboot the Pico into BOOTSEL mode by holding the bootsel button when plugging into USB.

      2. Copy ./out/gn/rp2040.size_optimized/obj/examples/03_rpc/rpc_main.uf2 to your Pi Pico.

      Note

      It is also possible to flash a Pico board with picotool. We will be adding support for that in this repo soon. See b/300321451 for updates.

    • Open pw_console.

      pw console -d /dev/ttyACM0 -b 115200 --token-databases out/gn/stm32f429i_disc1_stm32cube.size_optimized/obj/examples/03_rpc/bin/rpc_main.elf
      

      Tip

      On macOS, your device will look like /dev/cu.usbmodem2141403, but will most likely end with a different number.

    Simulated device (all platforms)

    Start the simulated device with the following command:

    pw device-sim ./out/gn/host_device_simulator.speed_optimized/obj/examples/03_rpc/bin/rpc_main
    
  3. In the Python Repl pane, use an RPC to request the device’s UUID.

    >>> device.rpcs.rpc_example.DeviceInfo.GetUuid()
    

    You should see it fail because the device does not yet have a UUID set:

    (Status.NOT_FOUND, rpc_example.DeviceUuid())
    

    In the Device Logs pane, you’ll see a log message like the following:

    UUID request received, but this device has no UUID yet
    
  4. In the Python Repl pane, set the device’s UUID.

    >>> device.rpcs.rpc_example.DeviceInfo.SetUuid(uuid=b'\xab\xcd\ef\x01\x23\x45\x67\x89')
    

    It should succeed with the following response:

    (Status.OK, pw.protobuf.Empty())
    
  5. In the Python Repl pane, use an RPC to request the device’s UUID again.

    This time, the device should respond with the UUID you set using the previous command:

    (Status.OK, rpc_example.DeviceUuid(uuid=b'\xAB\xCD\xEF\x01\x23\x45\x67\x89'))
    
  6. Try setting the UUID to a much longer string of bytes and see what happens!

  7. When you’re finished, you can type quit in the Python Repl pane to exit.