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_GET_SAVED_NETWORKS = "wlan_policy.get_saved_networks"
29COMMAND_CONNECT = "wlan_policy.connect"
30COMMAND_CREATE_CLIENT_CONTROLLER = "wlan_policy.create_client_controller"
31
32
33def main(argv):
34    if len(argv) > 1:
35        raise app.UsageError('Too many command-line arguments.')
36
37
38if __name__ == '__main__':
39    app.run(main)
40
41
42class FuchsiaWlanPolicyLib(BaseLib):
43    def __init__(self, addr, tc, client_id):
44        self.address = addr
45        self.test_counter = tc
46        self.client_id = client_id
47        self.log = logger.create_tagged_trace_logger(str(addr))
48
49    def wlanStartClientConnections(self):
50        """ Enables device to initiate connections to networks """
51
52        test_cmd = COMMAND_START_CLIENT_CONNECTIONS
53        test_id = self.build_id(self.test_counter)
54        self.test_counter += 1
55
56        return self.send_command(test_id, test_cmd, {})
57
58    def wlanStopClientConnections(self):
59        """ Disables device for initiating connections to networks """
60
61        test_cmd = COMMAND_STOP_CLIENT_CONNECTIONS
62        test_id = self.build_id(self.test_counter)
63        self.test_counter += 1
64
65        return self.send_command(test_id, test_cmd, {})
66
67    def wlanScanForNetworks(self):
68        """ Scans for networks that can be connected to
69                Returns:
70                    A list of network names and security types
71         """
72
73        test_cmd = COMMAND_SCAN_FOR_NETWORKS
74        test_id = self.build_id(self.test_counter)
75        self.test_counter += 1
76
77        return self.send_command(test_id, test_cmd, {})
78
79    def wlanSaveNetwork(self, target_ssid, security_type, target_pwd=None):
80        """ Saveds a network to the device for future connections
81                Args:
82                    target_ssid: the network to attempt a connection to
83                    security_type: the security protocol of the network
84                    target_pwd: (optional) credential being saved with the network. No password
85                                is equivalent to empty string.
86
87                Returns:
88                    boolean indicating if the connection was successful
89        """
90        if not target_pwd:
91            target_pwd = ''
92        test_cmd = COMMAND_SAVE_NETWORK
93        test_id = self.build_id(self.test_counter)
94        self.test_counter += 1
95        test_args = {
96            "target_ssid": target_ssid,
97            "security_type": str(security_type).lower(),
98            "target_pwd": target_pwd
99        }
100
101        return self.send_command(test_id, test_cmd, test_args)
102
103    def wlanRemoveNetwork(self, target_ssid, security_type, target_pwd=None):
104        """ Removes or "forgets" a network from saved networks
105                Args:
106                    target_ssid: the network to attempt a connection to
107                    security_type: the security protocol of the network
108                    target_pwd: (optional) credential of the network to remove. No password and
109                                empty string are equivalent.
110        """
111        if not target_pwd:
112            target_pwd = ''
113        test_cmd = COMMAND_REMOVE_NETWORK
114        test_id = self.build_id(self.test_counter)
115        self.test_counter += 1
116        test_args = {
117            "target_ssid": target_ssid,
118            "security_type": str(security_type).lower(),
119            "target_pwd": target_pwd
120        }
121
122        return self.send_command(test_id, test_cmd, test_args)
123
124    def wlanGetSavedNetworks(self):
125        """ Gets networks saved on device
126                Returns:
127                    A list of saved network names and security protocols
128        """
129
130        test_cmd = COMMAND_GET_SAVED_NETWORKS
131        test_id = self.build_id(self.test_counter)
132        self.test_counter += 1
133
134        return self.send_command(test_id, test_cmd, {})
135
136    def wlanConnect(self, target_ssid, security_type):
137        """ Triggers connection to a network
138                Args:
139                    target_ssid: the network to attempt a connection to. Must have been previously
140                                 saved in order for a successful connection to happen.
141                    security_type: the security protocol of the network
142
143                Returns:
144                    boolean indicating if the connection was successful
145        """
146
147        test_cmd = COMMAND_CONNECT
148        test_id = self.build_id(self.test_counter)
149        self.test_counter += 1
150        test_args = {
151            "target_ssid": target_ssid,
152            "security_type": str(security_type).lower()
153        }
154
155        return self.send_command(test_id, test_cmd, test_args)
156
157    def wlanCreateClientController(self):
158        """ Initializes the client controller of the facade that is used to make Client Controller
159            API calls
160        """
161        test_cmd = COMMAND_CREATE_CLIENT_CONTROLLER
162        test_id = self.build_id(self.test_counter)
163        self.test_counter += 1
164
165        return self.send_command(test_id, test_cmd, {})