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_SCAN_FOR_BSS_INFO = "wlan.scan_for_bss_info"
21COMMAND_CONNECT = "wlan.connect"
22COMMAND_DISCONNECT = "wlan.disconnect"
23COMMAND_STATUS = "wlan.status"
24COMMAND_GET_IFACE_ID_LIST = "wlan.get_iface_id_list"
25COMMAND_GET_PHY_ID_LIST = "wlan.get_phy_id_list"
26COMMAND_DESTROY_IFACE = "wlan.destroy_iface"
27COMMAND_GET_COUNTRY = "wlan_phy.get_country"
28COMMAND_GET_DEV_PATH = "wlan_phy.get_dev_path"
29COMMAND_QUERY_IFACE = "wlan.query_iface"
30
31
32class FuchsiaWlanLib(BaseLib):
33
34    def __init__(self, addr: str) -> None:
35        super().__init__(addr, "wlan")
36
37    def wlanStartScan(self):
38        """ Starts a wlan scan
39
40        Returns:
41            scan results
42        """
43        test_cmd = COMMAND_SCAN
44
45        return self.send_command(test_cmd, {})
46
47    def wlanScanForBSSInfo(self):
48        """ Scans and returns BSS info
49
50        Returns:
51            A dict mapping each seen SSID to a list of BSS Description IE
52            blocks, one for each BSS observed in the network
53        """
54        test_cmd = COMMAND_SCAN_FOR_BSS_INFO
55
56        return self.send_command(test_cmd, {})
57
58    def wlanConnectToNetwork(self,
59                             target_ssid,
60                             target_bss_desc,
61                             target_pwd=None):
62        """ Triggers a network connection
63        Args:
64            target_ssid: the network to attempt a connection to
65            target_pwd: (optional) password for the target network
66
67        Returns:
68            boolean indicating if the connection was successful
69        """
70        test_cmd = COMMAND_CONNECT
71        test_args = {
72            "target_ssid": target_ssid,
73            "target_pwd": target_pwd,
74            "target_bss_desc": target_bss_desc
75        }
76
77        return self.send_command(test_cmd, test_args)
78
79    def wlanDisconnect(self):
80        """ Disconnect any current wifi connections"""
81        test_cmd = COMMAND_DISCONNECT
82
83        return self.send_command(test_cmd, {})
84
85    def wlanDestroyIface(self, iface_id):
86        """ Destroy WLAN interface by ID.
87        Args:
88            iface_id: the interface id.
89
90        Returns:
91            Dictionary, service id if success, error if error.
92        """
93        test_cmd = COMMAND_DESTROY_IFACE
94        test_args = {"identifier": iface_id}
95
96        return self.send_command(test_cmd, test_args)
97
98    def wlanGetIfaceIdList(self):
99        """ Get a list if wlan interface IDs.
100
101        Returns:
102            Dictionary, service id if success, error if error.
103        """
104        test_cmd = COMMAND_GET_IFACE_ID_LIST
105
106        return self.send_command(test_cmd, {})
107
108    def wlanPhyIdList(self):
109        """ Get a list if wlan phy IDs.
110
111        Returns:
112            List of IDs if success, error if error.
113        """
114        test_cmd = COMMAND_GET_PHY_ID_LIST
115
116        return self.send_command(test_cmd, {})
117
118    def wlanStatus(self, iface_id=None):
119        """ Request connection status
120
121        Args:
122            iface_id: unsigned 16-bit int, the wlan interface id
123                (defaults to None)
124
125        Returns:
126            Client state summary containing WlanClientState and
127            status of various networks connections
128        """
129        test_cmd = COMMAND_STATUS
130        test_args = {}
131        if iface_id:
132            test_args = {'iface_id': iface_id}
133
134        return self.send_command(test_cmd, test_args)
135
136    def wlanGetCountry(self, phy_id):
137        """ Reads the currently configured country for `phy_id`.
138
139        Args:
140            phy_id: unsigned 16-bit integer.
141
142        Returns:
143            Dictionary, String if success, error if error.
144        """
145        test_cmd = COMMAND_GET_COUNTRY
146        test_args = {"phy_id": phy_id}
147
148        return self.send_command(test_cmd, test_args)
149
150    def wlanGetDevPath(self, phy_id):
151        """ Queries the device path for `phy_id`.
152
153        Args:
154            phy_id: unsigned 16-bit integer.
155
156        Returns:
157            Dictionary, String if success, error if error.
158        """
159        test_cmd = COMMAND_GET_DEV_PATH
160        test_args = {"phy_id": phy_id}
161
162        return self.send_command(test_cmd, test_args)
163
164    def wlanQueryInterface(self, iface_id):
165        """ Retrieves interface info for given wlan iface id.
166
167        Args:
168            iface_id: unsigned 16-bit int, the wlan interface id.
169
170        Returns:
171            Dictionary, containing interface id, role, phy_id, phy_assigned_id
172            and mac addr.
173        """
174        test_cmd = COMMAND_QUERY_IFACE
175        test_args = {'iface_id': iface_id}
176
177        return self.send_command(test_cmd, test_args)
178