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
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 self.pw_rpc_client = HdlcRpcClient(serial.Serial(device_tty, baud), [PROTO])
36 print("Platform args: %s" % platform_args)
37 print("Platform module: %s" % platform_module)
39 m = importlib.import_module(platform_module)
40 create_platform_method = getattr(m, "create_platform")
41 self._platform = create_platform_method(platform_args)
44 return self.pw_rpc_client.rpcs().pw.rpc
52 """Initializes the CHIP devices based on the testbed configuration.
55 configs: a list of testbed configs.
58 a list of device objects
61 for config in configs:
62 _validate_config(config)
63 device = PigweedDevice(**config)
68 def destroy(unused_objs):
69 """Destroys the wearable objects.
72 unused_objs: a list of device objects.
77 def _validate_config(config):
78 """Verifies that a config dict for a CHIP device is valid.
81 config: A dict that is the configuration for a CHIP device.
84 chip_device.Error: Config file is not valid.
86 required_keys = ["device_tty", "baud"] # A placeholder.
87 for key in required_keys:
89 raise Error("Required key %s missing from config %s" % (key, config))