1# Lint as: python3
2#!/usr/bin/env python3
3#
4#   Copyright 2020 - The Android Open Source Project
5#
6#   Licensed under the Apache License, Version 2.0 (the "License");
7#   you may not use this file except in compliance with the License.
8#   You may obtain a copy of the License at
9#
10#       http://www.apache.org/licenses/LICENSE-2.0
11#
12#   Unless required by applicable law or agreed to in writing, software
13#   distributed under the License is distributed on an "AS IS" BASIS,
14#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15#   See the License for the specific language governing permissions and
16#   limitations under the License.
17#
18#   This class provides pipeline betweem python tests and WLAN policy facade.
19
20from acts import logger
21from acts.controllers.fuchsia_lib.base_lib import BaseLib
22
23COMMAND_START_CLIENT_CONNECTIONS = "wlan_policy.start_client_connections"
24COMMAND_STOP_CLIENT_CONNECTIONS = "wlan_policy.stop_client_connections"
25COMMAND_SCAN_FOR_NETWORKS = "wlan_policy.scan_for_networks"
26COMMAND_SAVE_NETWORK = "wlan_policy.save_network"
27COMMAND_REMOVE_NETWORK = "wlan_policy.remove_network"
28COMMAND_REMOVE_ALL_NETWORKS = "wlan_policy.remove_all_networks"
29COMMAND_GET_SAVED_NETWORKS = "wlan_policy.get_saved_networks"
30COMMAND_CONNECT = "wlan_policy.connect"
31COMMAND_CREATE_CLIENT_CONTROLLER = "wlan_policy.create_client_controller"
32COMMAND_SET_NEW_LISTENER = "wlan_policy.set_new_update_listener"
33COMMAND_REMOVE_ALL_NETWORKS = "wlan_policy.remove_all_networks"
34COMMAND_GET_UPDATE = "wlan_policy.get_update"
35
36
37class FuchsiaWlanPolicyLib(BaseLib):
38
39    def __init__(self, addr: str) -> None:
40        super().__init__(addr, "wlan_policy")
41
42    def wlanStartClientConnections(self):
43        """ Enables device to initiate connections to networks """
44
45        test_cmd = COMMAND_START_CLIENT_CONNECTIONS
46
47        return self.send_command(test_cmd, {})
48
49    def wlanStopClientConnections(self):
50        """ Disables device for initiating connections to networks """
51
52        test_cmd = COMMAND_STOP_CLIENT_CONNECTIONS
53
54        return self.send_command(test_cmd, {})
55
56    def wlanScanForNetworks(self):
57        """ Scans for networks that can be connected to
58                Returns:
59                    A list of network names and security types
60         """
61
62        test_cmd = COMMAND_SCAN_FOR_NETWORKS
63
64        return self.send_command(test_cmd, {})
65
66    def wlanSaveNetwork(self, target_ssid, security_type, target_pwd=None):
67        """ Saveds a network to the device for future connections
68                Args:
69                    target_ssid: the network to attempt a connection to
70                    security_type: the security protocol of the network
71                    target_pwd: (optional) credential being saved with the network. No password
72                                is equivalent to empty string.
73
74                Returns:
75                    boolean indicating if the connection was successful
76        """
77        if not target_pwd:
78            target_pwd = ''
79        test_cmd = COMMAND_SAVE_NETWORK
80        test_args = {
81            "target_ssid": target_ssid,
82            "security_type": str(security_type).lower(),
83            "target_pwd": target_pwd
84        }
85
86        return self.send_command(test_cmd, test_args)
87
88    def wlanRemoveNetwork(self, target_ssid, security_type, target_pwd=None):
89        """ Removes or "forgets" a network from saved networks
90                Args:
91                    target_ssid: the network to attempt a connection to
92                    security_type: the security protocol of the network
93                    target_pwd: (optional) credential of the network to remove. No password and
94                                empty string are equivalent.
95        """
96        if not target_pwd:
97            target_pwd = ''
98        test_cmd = COMMAND_REMOVE_NETWORK
99        test_args = {
100            "target_ssid": target_ssid,
101            "security_type": str(security_type).lower(),
102            "target_pwd": target_pwd
103        }
104
105        return self.send_command(test_cmd, test_args)
106
107    def wlanRemoveAllNetworks(self):
108        """ Removes or "forgets" all networks from saved networks
109                Returns:
110                    A boolean indicating if the action was successful
111        """
112
113        test_cmd = COMMAND_REMOVE_ALL_NETWORKS
114
115        return self.send_command(test_cmd, {})
116
117    def wlanGetSavedNetworks(self):
118        """ Gets networks saved on device. Any PSK of a saved network will be
119            lower case regardless of how it was saved.
120                Returns:
121                    A list of saved network names and security protocols
122        """
123
124        test_cmd = COMMAND_GET_SAVED_NETWORKS
125
126        return self.send_command(test_cmd, {})
127
128    def wlanConnect(self, target_ssid, security_type):
129        """ Triggers connection to a network
130                Args:
131                    target_ssid: the network to attempt a connection to. Must have been previously
132                                 saved in order for a successful connection to happen.
133                    security_type: the security protocol of the network
134
135            Returns:
136                    boolean indicating if the connection was successful
137        """
138
139        test_cmd = COMMAND_CONNECT
140        test_args = {
141            "target_ssid": target_ssid,
142            "security_type": str(security_type).lower()
143        }
144
145        return self.send_command(test_cmd, test_args)
146
147    def wlanCreateClientController(self):
148        """ Initializes the client controller of the facade that is used to make Client Controller
149            API calls
150        """
151        test_cmd = COMMAND_CREATE_CLIENT_CONTROLLER
152
153        return self.send_command(test_cmd, {})
154
155    def wlanSetNewListener(self):
156        """ Sets the update listener stream of the facade to a new stream so that updates will be
157            reset. Intended to be used between tests so that the behaviour of updates in a test is
158            independent from previous tests.
159        """
160        test_cmd = COMMAND_SET_NEW_LISTENER
161
162        return self.send_command(test_cmd, {})
163
164    def wlanRemoveAllNetworks(self):
165        """ Deletes all saved networks on the device. Relies directly on the get_saved_networks and
166            remove_network commands
167        """
168        test_cmd = COMMAND_REMOVE_ALL_NETWORKS
169
170        return self.send_command(test_cmd, {})
171
172    def wlanGetUpdate(self, timeout=30):
173        """ Gets one client listener update. This call will return with an update immediately the
174            first time the update listener is initialized by setting a new listener or by creating
175            a client controller before setting a new listener. Subsequent calls will hang until
176            there is an update.
177            Returns:
178                An update of connection status. If there is no error, the result is a dict with a
179                structure that matches the FIDL ClientStateSummary struct given for updates.
180        """
181        test_cmd = COMMAND_GET_UPDATE
182
183        return self.send_command(test_cmd, {}, response_timeout=timeout)
184