• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
37def main(argv):
38    if len(argv) > 1:
39        raise app.UsageError('Too many command-line arguments.')
40
41
42if __name__ == '__main__':
43    app.run(main)
44
45
46class FuchsiaWlanPolicyLib(BaseLib):
47    def __init__(self, addr, tc, client_id):
48        self.address = addr
49        self.test_counter = tc
50        self.client_id = client_id
51        self.log = logger.create_tagged_trace_logger(str(addr))
52
53    def wlanStartClientConnections(self):
54        """ Enables device to initiate connections to networks """
55
56        test_cmd = COMMAND_START_CLIENT_CONNECTIONS
57        test_id = self.build_id(self.test_counter)
58        self.test_counter += 1
59
60        return self.send_command(test_id, test_cmd, {})
61
62    def wlanStopClientConnections(self):
63        """ Disables device for initiating connections to networks """
64
65        test_cmd = COMMAND_STOP_CLIENT_CONNECTIONS
66        test_id = self.build_id(self.test_counter)
67        self.test_counter += 1
68
69        return self.send_command(test_id, test_cmd, {})
70
71    def wlanScanForNetworks(self):
72        """ Scans for networks that can be connected to
73                Returns:
74                    A list of network names and security types
75         """
76
77        test_cmd = COMMAND_SCAN_FOR_NETWORKS
78        test_id = self.build_id(self.test_counter)
79        self.test_counter += 1
80
81        return self.send_command(test_id, test_cmd, {})
82
83    def wlanSaveNetwork(self, target_ssid, security_type, target_pwd=None):
84        """ Saveds a network to the device for future connections
85                Args:
86                    target_ssid: the network to attempt a connection to
87                    security_type: the security protocol of the network
88                    target_pwd: (optional) credential being saved with the network. No password
89                                is equivalent to empty string.
90
91                Returns:
92                    boolean indicating if the connection was successful
93        """
94        if not target_pwd:
95            target_pwd = ''
96        test_cmd = COMMAND_SAVE_NETWORK
97        test_id = self.build_id(self.test_counter)
98        self.test_counter += 1
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_id, test_cmd, test_args)
106
107    def wlanRemoveNetwork(self, target_ssid, security_type, target_pwd=None):
108        """ Removes or "forgets" a network from saved networks
109                Args:
110                    target_ssid: the network to attempt a connection to
111                    security_type: the security protocol of the network
112                    target_pwd: (optional) credential of the network to remove. No password and
113                                empty string are equivalent.
114        """
115        if not target_pwd:
116            target_pwd = ''
117        test_cmd = COMMAND_REMOVE_NETWORK
118        test_id = self.build_id(self.test_counter)
119        self.test_counter += 1
120        test_args = {
121            "target_ssid": target_ssid,
122            "security_type": str(security_type).lower(),
123            "target_pwd": target_pwd
124        }
125
126        return self.send_command(test_id, test_cmd, test_args)
127
128    def wlanRemoveAllNetworks(self):
129        """ Removes or "forgets" all networks from saved networks
130                Returns:
131                    A boolean indicating if the action was successful
132        """
133
134        test_cmd = COMMAND_REMOVE_ALL_NETWORKS
135        test_id = self.build_id(self.test_counter)
136        self.test_counter += 1
137
138        return self.send_command(test_id, test_cmd, {})
139
140    def wlanGetSavedNetworks(self):
141        """ Gets networks saved on device. Any PSK of a saved network will be
142            lower case regardless of how it was saved.
143                Returns:
144                    A list of saved network names and security protocols
145        """
146
147        test_cmd = COMMAND_GET_SAVED_NETWORKS
148        test_id = self.build_id(self.test_counter)
149        self.test_counter += 1
150
151        return self.send_command(test_id, test_cmd, {})
152
153    def wlanConnect(self, target_ssid, security_type):
154        """ Triggers connection to a network
155                Args:
156                    target_ssid: the network to attempt a connection to. Must have been previously
157                                 saved in order for a successful connection to happen.
158                    security_type: the security protocol of the network
159
160            Returns:
161                    boolean indicating if the connection was successful
162        """
163
164        test_cmd = COMMAND_CONNECT
165        test_id = self.build_id(self.test_counter)
166        self.test_counter += 1
167        test_args = {
168            "target_ssid": target_ssid,
169            "security_type": str(security_type).lower()
170        }
171
172        return self.send_command(test_id, test_cmd, test_args)
173
174    def wlanCreateClientController(self):
175        """ Initializes the client controller of the facade that is used to make Client Controller
176            API calls
177        """
178        test_cmd = COMMAND_CREATE_CLIENT_CONTROLLER
179        test_id = self.build_id(self.test_counter)
180        self.test_counter += 1
181
182        return self.send_command(test_id, test_cmd, {})
183
184    def wlanSetNewListener(self):
185        """ Sets the update listener stream of the facade to a new stream so that updates will be
186            reset. Intended to be used between tests so that the behaviour of updates in a test is
187            independent from previous tests.
188        """
189        test_cmd = COMMAND_SET_NEW_LISTENER
190        test_id = self.build_id(self.test_counter)
191        self.test_counter += 1
192
193        return self.send_command(test_id, test_cmd, {})
194
195    def wlanRemoveAllNetworks(self):
196        """ Deletes all saved networks on the device. Relies directly on the get_saved_networks and
197            remove_network commands
198        """
199        test_cmd = COMMAND_REMOVE_ALL_NETWORKS
200        test_id = self.build_id(self.test_counter)
201        self.test_counter += 1
202
203        return self.send_command(test_id, test_cmd, {})
204
205    def wlanGetUpdate(self, timeout=30):
206        """ Gets one client listener update. This call will return with an update immediately the
207            first time the update listener is initialized by setting a new listener or by creating
208            a client controller before setting a new listener. Subsequent calls will hang until
209            there is an update.
210            Returns:
211                An update of connection status. If there is no error, the result is a dict with a
212                structure that matches the FIDL ClientStateSummary struct given for updates.
213        """
214        test_cmd = COMMAND_GET_UPDATE
215        test_id = self.build_id(self.test_counter)
216        self.test_counter += 1
217
218        return self.send_command(test_id,
219                                 test_cmd, {},
220                                 response_timeout=timeout)