Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / integrations / mobly / build / lib / chip_mobly / pigweed_device.py
1 # Copyright (c) 2020 Project CHIP Authors
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import os
16 from pathlib import Path
17 import serial # type: ignore
18 import importlib
19
20 from pw_hdlc.rpc import HdlcRpcClient
21
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")
24
25 MOBLY_CONTROLLER_CONFIG_NAME = "PigweedDevice"
26
27
28 class Error(Exception):
29     """This is the Exception class defined for all errors."""
30
31
32 class PigweedDevice:
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])
35         self._platform = None
36         print("Platform args: %s" % platform_args)
37         print("Platform module: %s" % platform_module)
38         if 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)
42
43     def rpcs(self):
44         return self.pw_rpc_client.rpcs().pw.rpc
45
46     @property
47     def platform(self):
48         return self._platform
49
50
51 def create(configs):
52     """Initializes the CHIP devices based on the testbed configuration.
53
54     Args:
55       configs: a list of testbed configs.
56
57     Returns:
58       a list of device objects
59     """
60     objs = []
61     for config in configs:
62         _validate_config(config)
63         device = PigweedDevice(**config)
64         objs.append(device)
65     return objs
66
67
68 def destroy(unused_objs):
69     """Destroys the wearable objects.
70
71     Args:
72       unused_objs: a list of device objects.
73     """
74     pass
75
76
77 def _validate_config(config):
78     """Verifies that a config dict for a CHIP device is valid.
79
80     Args:
81       config: A dict that is the configuration for a CHIP device.
82
83     Raises:
84       chip_device.Error: Config file is not valid.
85     """
86     required_keys = ["device_tty", "baud"]  # A placeholder.
87     for key in required_keys:
88         if key not in config:
89             raise Error("Required key %s missing from config %s" % (key, config))