Skip to content

"rpc" decorator for virtual devices fails to pack response if resp_format is not specified. #942

@dvksirin

Description

@dvksirin

Here is truncated example from tutorial:

from iotile.core.hw.virtual import SimpleVirtualDevice, rpc

class DemoVirtualDevice(SimpleVirtualDevice):
    def __init__(self, args):
        super(DemoVirtualDevice, self).__init__(1, 'Demo01')
        self.temp = 273

    @rpc(8, 0x8002, "L")
    def set_temperature(self, new_temp):
        """Set the current temperature of the device in degrees kelvin"""

        self.temp = new_temp
        return []

When "resp_format" keyword argument for "rpc" is not specified it returns not changed value returned by decorated method or "[]" if returned value is None. But should return bytes according to its docstring description

....
Similarly, the function being decorated should return an iterable of results that
will be encoded into a 20 byte response buffer by struct.pack using resp_format as
the format string.

It causes an error.
Here is traceback I got when executed the example code from the tutorial

(coretools) dvk@sirin-dvk:~/test/test_component$ iotile hw --port=virtual:./demo_device.py connect 1 controller set_temperature 10
Traceback (most recent call last):
  File "/home/dvk/projects/coretools/iotilecore/iotile/core/scripts/iotile_script.py", line 206, in main
    finished = shell.invoke(line)
  File "/home/dvk/.virtualenvs/coretools/lib/python3.6/site-packages/typedargs/shell.py", line 489, in invoke
    val, line, finished = self.invoke_one(line)
  File "/home/dvk/.virtualenvs/coretools/lib/python3.6/site-packages/typedargs/shell.py", line 452, in invoke_one
    val = func(*posargs, **kwargs)
  File "</home/dvk/.virtualenvs/coretools/lib/python3.6/site-packages/decorator.py:decorator-gen-41>", line 2, in set_temperature
  File "/home/dvk/.virtualenvs/coretools/lib/python3.6/site-packages/typedargs/utils.py", line 32, in _check_and_execute
    retval = func(*convargs, **convkw)
  File "/home/dvk/test/test_component/python/demo_proxy.py", line 31, in set_temperature
    self.rpc_v2(0x8002, "L", "", new_temp)
  File "/home/dvk/projects/coretools/iotilecore/iotile/core/hw/proxy/proxy.py", line 99, in rpc_v2
    return unpack_rpc_payload(result_format, payload)
  File "/home/dvk/projects/coretools/iotilecore/iotile/core/hw/virtual/common_types.py", line 140, in unpack_rpc_payload
    return struct.unpack(code, payload)
TypeError: a bytes-like object is required, not 'list'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions