1#!/usr/bin/env python3
2#
3#   Copyright 2020 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import datetime
18import http
19import requests
20
21import acts.controllers.fuchsia_lib.base_lib as base_lib
22
23HW_PWR_STATE_CONTROL_TIMEOUT = 5
24
25
26class FuchsiaHardwarePowerStatecontrolLib(base_lib.BaseLib):
27    def __init__(self, addr, tc, client_id):
28        self.address = addr
29        self.test_counter = tc
30        self.client_id = client_id
31
32    def suspendReboot(self, timeout=HW_PWR_STATE_CONTROL_TIMEOUT):
33        """Call Suspend Reboot.
34
35        Returns:
36            None if success.
37        """
38        test_cmd = "hardware_power_statecontrol_facade.SuspendReboot"
39        test_args = {}
40        test_id = self.build_id(self.test_counter)
41        self.test_counter += 1
42        try:
43            response = self.send_command(test_id,
44                                         test_cmd,
45                                         test_args,
46                                         response_timeout=timeout)
47        except (requests.exceptions.ReadTimeout,
48                http.client.RemoteDisconnected, base_lib.DeviceOffline):
49            return
50        return response
51
52    def suspendRebootBootloader(self, timeout=HW_PWR_STATE_CONTROL_TIMEOUT):
53        """Call Suspend Reboot Bootloader
54
55        Returns:
56            None if success.
57        """
58        test_cmd = "hardware_power_statecontrol_facade.SuspendRebootBootloader"
59        test_args = {}
60        test_id = self.build_id(self.test_counter)
61        self.test_counter += 1
62        try:
63            response = self.send_command(test_id,
64                                         test_cmd,
65                                         test_args,
66                                         response_timeout=timeout)
67        except (requests.exceptions.ReadTimeout,
68                http.client.RemoteDisconnected, base_lib.DeviceOffline):
69            return
70        return response
71
72    def suspendPoweroff(self, timeout=HW_PWR_STATE_CONTROL_TIMEOUT):
73        """Call Suspend Poweroff
74
75        Returns:
76            None if success.
77        """
78        test_cmd = "hardware_power_statecontrol_facade.SuspendPoweroff"
79        test_args = {}
80        test_id = self.build_id(self.test_counter)
81        self.test_counter += 1
82        try:
83            response = self.send_command(test_id,
84                                         test_cmd,
85                                         test_args,
86                                         response_timeout=timeout)
87        except (requests.exceptions.ReadTimeout,
88                http.client.RemoteDisconnected, base_lib.DeviceOffline):
89            return
90        return response
91
92    def suspendMexec(self, timeout=HW_PWR_STATE_CONTROL_TIMEOUT):
93        """Call Suspend Mexec
94
95        Returns:
96            None if success.
97        """
98        test_cmd = "hardware_power_statecontrol_facade.SuspendMexec"
99        test_args = {}
100        test_id = self.build_id(self.test_counter)
101        self.test_counter += 1
102        try:
103            response = self.send_command(test_id,
104                                         test_cmd,
105                                         test_args,
106                                         response_timeout=timeout)
107        except (requests.exceptions.ReadTimeout,
108                http.client.RemoteDisconnected, base_lib.DeviceOffline):
109            return
110        return response
111
112    def suspendRam(self, timeout=HW_PWR_STATE_CONTROL_TIMEOUT):
113        """Call Suspend Ram
114
115        Returns:
116            None if success.
117        """
118        test_cmd = "hardware_power_statecontrol_facade.SuspendRam"
119        test_args = {}
120        test_id = self.build_id(self.test_counter)
121        self.test_counter += 1
122        try:
123            response = self.send_command(test_id,
124                                         test_cmd,
125                                         test_args,
126                                         response_timeout=timeout)
127        except (requests.exceptions.ReadTimeout,
128                http.client.RemoteDisconnected, base_lib.DeviceOffline):
129            return
130        return response
131