1 # Copyright (c) 2020 Project CHIP Authors
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from pathlib import Path
17 import serial # type: ignore
20 from pw_hdlc.rpc import HdlcRpcClient, default_channels
22 # Point the script to the .proto file with our RPC services.
23 PROTO = Path(os.environ["PW_ROOT"], "pw_rpc/pw_rpc_protos/echo.proto")
25 MOBLY_CONTROLLER_CONFIG_NAME = "PigweedDevice"
28 class Error(Exception):
29 """This is the Exception class defined for all errors."""
33 def __init__(self, device_tty, baud, platform_module=None, platform_args=None):
34 ser = serial.Serial(device_tty, baud, timeout=0.01)
35 self.pw_rpc_client = HdlcRpcClient(lambda: ser.read(4096),
36 [PROTO], default_channels(ser.write))
38 print("Platform args: %s" % platform_args)
39 print("Platform module: %s" % platform_module)
41 m = importlib.import_module(platform_module)
42 create_platform_method = getattr(m, "create_platform")
43 self._platform = create_platform_method(platform_args)
46 return self.pw_rpc_client.rpcs().pw.rpc
54 """Initializes the CHIP devices based on the testbed configuration.
57 configs: a list of testbed configs.
60 a list of device objects
63 for config in configs:
64 _validate_config(config)
65 device = PigweedDevice(**config)
70 def destroy(unused_objs):
71 """Destroys the wearable objects.
74 unused_objs: a list of device objects.
79 def _validate_config(config):
80 """Verifies that a config dict for a CHIP device is valid.
83 config: A dict that is the configuration for a CHIP device.
86 chip_device.Error: Config file is not valid.
88 required_keys = ["device_tty", "baud"] # A placeholder.
89 for key in required_keys:
91 raise Error("Required key %s missing from config %s" % (key, config))