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
17from acts.controllers.fuchsia_lib.base_lib import BaseLib
18
19
20class FuchsiaWlanApPolicyLib(BaseLib):
21
22    def __init__(self, addr: str) -> None:
23        super().__init__(addr, "wlan_ap_policy")
24
25    def wlanStartAccessPoint(self, target_ssid, security_type, target_pwd,
26                             connectivity_mode, operating_band):
27        """ Start an Access Point.
28                Args:
29                    target_ssid: the network to attempt a connection to
30                    security_type: the security protocol of the network. Possible inputs:
31                    	"none", "wep", "wpa", "wpa2", "wpa3"
32                    target_pwd: (optional) credential being saved with the network. No password
33                                is equivalent to empty string.
34                    connectivity_mode: the connectivity mode to use. Possible inputs:
35                    	"local_only", "unrestricted"
36                    operating_band: The operating band to use. Possible inputs:
37                    	"any", "only_2_4_ghz", "only_5_ghz"
38
39                Returns:
40                    boolean indicating if the action was successful
41        """
42
43        test_cmd = "wlan_ap_policy.start_access_point"
44
45        test_args = {
46            "target_ssid": target_ssid,
47            "security_type": security_type.lower(),
48            "target_pwd": target_pwd,
49            "connectivity_mode": connectivity_mode,
50            "operating_band": operating_band,
51        }
52
53        return self.send_command(test_cmd, test_args)
54
55    def wlanStopAccessPoint(self, target_ssid, security_type, target_pwd=""):
56        """ Stops an active Access Point.
57                Args:
58                    target_ssid: the network to attempt a connection to
59                    security_type: the security protocol of the network
60                    target_pwd: (optional) credential being saved with the network. No password
61                                is equivalent to empty string.
62
63                Returns:
64                    boolean indicating if the action was successful
65        """
66
67        test_cmd = "wlan_ap_policy.stop_access_point"
68
69        test_args = {
70            "target_ssid": target_ssid,
71            "security_type": security_type.lower(),
72            "target_pwd": target_pwd
73        }
74
75        return self.send_command(test_cmd, test_args)
76
77    def wlanStopAllAccessPoint(self):
78        """ Stops all Access Points
79
80                Returns:
81                    boolean indicating if the actions were successful
82        """
83
84        test_cmd = "wlan_ap_policy.stop_all_access_points"
85
86        test_args = {}
87
88        return self.send_command(test_cmd, test_args)
89
90    def wlanSetNewListener(self):
91        """ Sets the update listener stream of the facade to a new stream so that updates will be
92            reset. Intended to be used between tests so that the behaviour of updates in a test is
93            independent from previous tests.
94        """
95        test_cmd = "wlan_ap_policy.set_new_update_listener"
96
97        return self.send_command(test_cmd, {})
98
99    def wlanGetUpdate(self, timeout=30):
100        """ Gets a list of AP state updates. This call will return with an update immediately the
101            first time the update listener is initialized by setting a new listener or by creating
102            a client controller before setting a new listener. Subsequent calls will hang until
103            there is an update.
104            Returns:
105                A list of AP state updated. If there is no error, the result is a list with a
106                structure that matches the FIDL AccessPointState struct given for updates.
107        """
108        test_cmd = "wlan_ap_policy.get_update"
109
110        return self.send_command(test_cmd, {}, response_timeout=timeout)
111