| #!/usr/bin/env python3 |
| # |
| # Copyright 2022 The Fuchsia Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| import ipaddress |
| |
| from honeydew.interfaces.device_classes.fuchsia_device import ( |
| FuchsiaDevice as HdFuchsiaDevice, |
| ) |
| |
| from antlion import logger |
| from antlion.controllers.fuchsia_lib.device_lib import DeviceLib |
| from antlion.controllers.fuchsia_lib.hardware_power_statecontrol_lib import ( |
| FuchsiaHardwarePowerStatecontrolLib, |
| ) |
| from antlion.controllers.fuchsia_lib.logging_lib import FuchsiaLoggingLib |
| from antlion.controllers.fuchsia_lib.netstack.netstack_lib import FuchsiaNetstackLib |
| from antlion.controllers.fuchsia_lib.ssh import FuchsiaSSHProvider, SSHError |
| from antlion.controllers.fuchsia_lib.wlan_ap_policy_lib import FuchsiaWlanApPolicyLib |
| from antlion.controllers.fuchsia_lib.wlan_deprecated_configuration_lib import ( |
| FuchsiaWlanDeprecatedConfigurationLib, |
| ) |
| from antlion.controllers.fuchsia_lib.wlan_lib import FuchsiaWlanLib |
| from antlion.controllers.fuchsia_lib.wlan_policy_lib import FuchsiaWlanPolicyLib |
| from antlion.net import wait_for_port |
| |
| DEFAULT_SL4F_PORT = 80 |
| START_SL4F_V2_CMD = "start_sl4f" |
| |
| |
| class SL4F: |
| """Module for Fuchsia devices to interact with the SL4F tool. |
| |
| Attributes: |
| ssh: Transport to start and stop SL4F. |
| address: http address for SL4F server including SL4F port. |
| log: Logger for the device-specific instance of SL4F. |
| """ |
| |
| def __init__( |
| self, |
| ssh: FuchsiaSSHProvider, |
| port: int = DEFAULT_SL4F_PORT, |
| honeydew_fd: HdFuchsiaDevice | None = None, |
| ) -> None: |
| """ |
| Args: |
| ssh: Transport to start and stop SL4F. |
| port: Port for the SL4F server to listen on. |
| """ |
| self.honeydew_fd = honeydew_fd |
| ip = ipaddress.ip_address(ssh.config.host_name) |
| if ip.version == 4: |
| self.address = f"http://{ip}:{port}" |
| elif ip.version == 6: |
| self.address = f"http://[{ip}]:{port}" |
| |
| self.log = logger.create_tagged_trace_logger(f"SL4F | {self.address}") |
| |
| try: |
| ssh.stop_component("sl4f") |
| ssh.run(START_SL4F_V2_CMD).stdout |
| except SSHError: |
| # TODO(fxbug.dev/42181764) Remove support to run SL4F in CFv1 mode |
| # once ACTS no longer use images that comes with only CFv1 SL4F. |
| self.log.warn( |
| "Running SL4F in CFv1 mode, " |
| "this is deprecated for images built after 5/9/2022, " |
| "see https://fxbug.dev/42157029 for more info." |
| ) |
| ssh.stop_component("sl4f") |
| ssh.start_v1_component("sl4f") |
| |
| if not self.honeydew_fd: |
| try: |
| wait_for_port(ssh.config.host_name, port) |
| self.log.info("SL4F server is reachable") |
| except TimeoutError as e: |
| raise TimeoutError("SL4F server is unreachable") from e |
| else: |
| # Honeydew has its own transport-specific logic |
| self.log.info("Using Honeydew controller") |
| |
| self._init_libraries(self.honeydew_fd) |
| |
| def _init_libraries(self, honeydew_fd: HdFuchsiaDevice | None = None) -> None: |
| # Grab commands from DeviceLib |
| self.device_lib = DeviceLib(self.address) |
| |
| # Grab commands from FuchsiaHardwarePowerStatecontrolLib |
| self.hardware_power_statecontrol_lib = FuchsiaHardwarePowerStatecontrolLib( |
| self.address |
| ) |
| |
| # Grab commands from FuchsiaLoggingLib |
| self.logging_lib = FuchsiaLoggingLib(self.address) |
| |
| # Grab commands from FuchsiaNetstackLib |
| self.netstack_lib = FuchsiaNetstackLib(self.address) |
| |
| # Grab commands from FuchsiaWlanApPolicyLib |
| self.wlan_ap_policy_lib = FuchsiaWlanApPolicyLib(self.address) |
| |
| # Grabs command from FuchsiaWlanDeprecatedConfigurationLib |
| self.wlan_deprecated_configuration_lib = FuchsiaWlanDeprecatedConfigurationLib( |
| self.address |
| ) |
| |
| # Grab commands from FuchsiaWlanLib |
| self.wlan_lib = FuchsiaWlanLib(self.address) |
| |
| # Grab commands from FuchsiaWlanPolicyLib |
| self.wlan_policy_lib = FuchsiaWlanPolicyLib(self.address, honeydew_fd) |