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_GET_PHY_ID_LIST = "wlan.get_phy_id_list"
25COMMAND_DESTROY_IFACE = "wlan.destroy_iface"
26COMMAND_GET_COUNTRY = "wlan_phy.get_country"
27COMMAND_QUERY_IFACE = "wlan.query_iface"
28
29
30class FuchsiaWlanLib(BaseLib):
31    def __init__(self, addr, tc, client_id):
32        self.address = addr
33        self.test_counter = tc
34        self.client_id = client_id
35        self.log = logger.create_tagged_trace_logger(str(addr))
36
37    def wlanStartScan(self):
38        """ Starts a wlan scan
39
40        Returns:
41            scan results
42        """
43        test_cmd = COMMAND_SCAN
44        test_id = self.build_id(self.test_counter)
45        self.test_counter += 1
46
47        return self.send_command(test_id, test_cmd, {})
48
49    def wlanConnectToNetwork(self, target_ssid, target_pwd=None):
50        """ Triggers a network connection
51        Args:
52            target_ssid: the network to attempt a connection to
53            target_pwd: (optional) password for the target network
54
55        Returns:
56            boolean indicating if the connection was successful
57        """
58        test_cmd = COMMAND_CONNECT
59        test_args = {"target_ssid": target_ssid, "target_pwd": target_pwd}
60        test_id = self.build_id(self.test_counter)
61        self.test_counter += 1
62
63        return self.send_command(test_id, test_cmd, test_args)
64
65    def wlanDisconnect(self):
66        """ Disconnect any current wifi connections"""
67        test_cmd = COMMAND_DISCONNECT
68        test_id = self.build_id(self.test_counter)
69        self.test_counter += 1
70
71        return self.send_command(test_id, test_cmd, {})
72
73    def wlanDestroyIface(self, iface_id):
74        """ Destroy WLAN interface by ID.
75        Args:
76            iface_id: the interface id.
77
78        Returns:
79            Dictionary, service id if success, error if error.
80        """
81        test_cmd = COMMAND_DESTROY_IFACE
82        test_args = {"identifier": iface_id}
83        test_id = self.build_id(self.test_counter)
84        self.test_counter += 1
85
86        return self.send_command(test_id, test_cmd, test_args)
87
88    def wlanGetIfaceIdList(self):
89        """ Get a list if wlan interface IDs.
90
91        Returns:
92            Dictionary, service id if success, error if error.
93        """
94        test_cmd = COMMAND_GET_IFACE_ID_LIST
95        test_id = self.build_id(self.test_counter)
96        self.test_counter += 1
97
98        return self.send_command(test_id, test_cmd, {})
99
100    def wlanPhyIdList(self):
101        """ Get a list if wlan phy IDs.
102
103        Returns:
104            List of IDs if success, error if error.
105        """
106        test_cmd = COMMAND_GET_PHY_ID_LIST
107        test_id = self.build_id(self.test_counter)
108        self.test_counter += 1
109
110        return self.send_command(test_id, test_cmd, {})
111
112    def wlanStatus(self):
113        """ Request connection status
114
115        Returns:
116            Client state summary containing WlanClientState and
117            status of various networks connections
118        """
119        test_cmd = COMMAND_STATUS
120        test_id = self.build_id(self.test_counter)
121        self.test_counter += 1
122
123        return self.send_command(test_id, test_cmd, {})
124
125    def wlanGetCountry(self, phy_id):
126        """ Reads the currently configured country for `phy_id`.
127
128        Args:
129            phy_id: unsigned 16-bit integer.
130
131        Returns:
132            Dictionary, String if success, error if error.
133        """
134        test_cmd = COMMAND_GET_COUNTRY
135        test_args = {"phy_id": phy_id}
136        test_id = self.build_id(self.test_counter)
137        self.test_counter += 1
138
139        return self.send_command(test_id, test_cmd, test_args)
140
141    def wlanQueryInterface(self, iface_id):
142        """ Retrieves interface info for given wlan iface id.
143
144        Args:
145            iface_id: unsigned 16-bit int, the wlan interface id.
146
147        Returns:
148            Dictionary, containing interface id, role, phy_id, phy_assigned_id
149            and mac addr.
150        """
151        test_cmd = COMMAND_QUERY_IFACE
152        test_args = {'iface_id': iface_id}
153        test_id = self.build_id(self.test_counter)
154        self.test_counter += 1
155
156        return self.send_command(test_id, test_cmd, test_args)
157