Skip to content

Commit 3ddcc88

Browse files
committed
rpc_wrappers: generic: added
Add a rpc wrapper that sends generic commands to a device. Useful for testing commands in repos that define their own extensions. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 0c76d3a commit 3ddcc88

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
3+
import ctypes
4+
5+
from infuse_iot.commands import InfuseRpcCommand
6+
from infuse_iot.util.ctypes import VLACompatLittleEndianStruct, bytes_to_uint8
7+
from infuse_iot.zephyr.errno import errno
8+
9+
10+
class generic(InfuseRpcCommand):
11+
NAME = "generic"
12+
HELP = "Generic RPC sender"
13+
DESCRIPTION = "Generic RPC sender"
14+
COMMAND_ID = -1
15+
16+
class response(VLACompatLittleEndianStruct):
17+
_fields_ = []
18+
vla_field = ("val", 0 * ctypes.c_uint8)
19+
_pack_ = 1
20+
21+
@classmethod
22+
def add_parser(cls, parser):
23+
parser.add_argument("--rpc", type=int, required=True, help="RPC command ID")
24+
parser.add_argument("--args", type=str, default="", help="Arguments as byte string")
25+
26+
def __init__(self, args):
27+
self.COMMAND_ID = args.rpc
28+
self._arg_bytes = bytes.fromhex(args.args)
29+
30+
def request_struct(self):
31+
class request(ctypes.LittleEndianStructure):
32+
_fields_ = [
33+
("val", len(self._arg_bytes) * ctypes.c_uint8),
34+
]
35+
_pack_ = 1
36+
37+
return request(bytes_to_uint8(self._arg_bytes))
38+
39+
def handle_response(self, return_code, response):
40+
print(f"Return Code: {return_code} ({errno.strerror(-return_code)})")
41+
if len(response.val) > 0:
42+
print(f" Response: {bytes(response.val).hex()}")
43+
else:
44+
print(" Response: None")

0 commit comments

Comments
 (0)