1#!/usr/bin/env python3
2#
3#   Copyright 2018 - 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.
16from acts import logger
17from acts.controllers.fuchsia_lib.base_lib import BaseLib
18
19COMMAND_SCAN = "wlan.scan"
20COMMAND_CONNECT = "wlan.connect"
21COMMAND_DISCONNECT = "wlan.disconnect"
22COMMAND_STATUS = "wlan.status"
23COMMAND_GET_IFACE_ID_LIST = "wlan.get_iface_id_list"
24COMMAND_DESTROY_IFACE = "wlan.destroy_iface"
25
26
27class FuchsiaWlanLib(BaseLib):
28    def __init__(self, addr, tc, client_id):
29        self.address = addr
30        self.test_counter = tc
31        self.client_id = client_id
32        self.log = logger.create_tagged_trace_logger(str(addr))
33
34    def wlanStartScan(self):
35        """ Starts a wlan scan
36
37        Returns:
38            scan results
39        """
40        test_cmd = COMMAND_SCAN
41        test_id = self.build_id(self.test_counter)
42        self.test_counter += 1
43
44        return self.send_command(test_id, test_cmd, {})
45
46    def wlanConnectToNetwork(self, target_ssid, target_pwd=None):
47        """ Triggers a network connection
48        Args:
49            target_ssid: the network to attempt a connection to
50            target_pwd: (optional) password for the target network
51
52        Returns:
53            boolean indicating if the connection was successful
54        """
55        test_cmd = COMMAND_CONNECT
56        test_args = {"target_ssid": target_ssid, "target_pwd": target_pwd}
57        test_id = self.build_id(self.test_counter)
58        self.test_counter += 1
59
60        return self.send_command(test_id, test_cmd, test_args)
61
62    def wlanDisconnect(self):
63        """ Disconnect any current wifi connections"""
64        test_cmd = COMMAND_DISCONNECT
65        test_id = self.build_id(self.test_counter)
66        self.test_counter += 1
67
68        return self.send_command(test_id, test_cmd, {})
69
70    def wlanDestroyIface(self, iface_id):
71        """ Destroy WLAN interface by ID.
72        Args:
73            iface_id: the interface id.
74
75        Returns:
76            Dictionary, service id if success, error if error.
77        """
78        test_cmd = COMMAND_DESTROY_IFACE
79        test_args = {"identifier": iface_id}
80        test_id = self.build_id(self.test_counter)
81        self.test_counter += 1
82
83        return self.send_command(test_id, test_cmd, test_args)
84
85    def wlanGetIfaceIdList(self):
86        """ Get a list if wlan interface IDs.
87
88        Returns:
89            Dictionary, service id if success, error if error.
90        """
91        test_cmd = COMMAND_GET_IFACE_ID_LIST
92        test_id = self.build_id(self.test_counter)
93        self.test_counter += 1
94
95        return self.send_command(test_id, test_cmd, {})
96
97    def wlanStatus(self):
98        """ Request connection status
99
100        Returns:
101            Client state summary containing WlanClientState and
102            status of various networks connections
103        """
104        test_cmd = COMMAND_STATUS
105        test_id = self.build_id(self.test_counter)
106        self.test_counter += 1
107
108        return self.send_command(test_id, test_cmd, {})
109