The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 17 | package com.android.server.wifi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.net.NetworkInfo; |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 20 | import android.net.wifi.SupplicantState; |
| 21 | import android.net.wifi.WifiManager; |
| 22 | import android.net.wifi.WifiSsid; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 23 | import android.net.wifi.p2p.WifiP2pConfig; |
| 24 | import android.net.wifi.p2p.WifiP2pDevice; |
| 25 | import android.net.wifi.p2p.WifiP2pGroup; |
Irfan Sheriff | 618455f | 2011-11-18 14:33:09 -0800 | [diff] [blame] | 26 | import android.net.wifi.p2p.WifiP2pProvDiscEvent; |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 27 | import android.net.wifi.p2p.nsd.WifiP2pServiceResponse; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 28 | import android.os.Message; |
| 29 | import android.util.Log; |
| 30 | |
Vinit Deshapnde | ffadfb9 | 2013-12-06 15:12:41 -0800 | [diff] [blame] | 31 | import com.android.server.wifi.p2p.WifiP2pService.P2pStatus; |
| 32 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 33 | import com.android.internal.util.Protocol; |
| 34 | import com.android.internal.util.StateMachine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 36 | import java.util.HashMap; |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 37 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import java.util.regex.Matcher; |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 39 | import java.util.regex.Pattern; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Listens for events from the wpa_supplicant server, and passes them on |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 43 | * to the {@link StateMachine} for handling. Runs in its own thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * |
| 45 | * @hide |
| 46 | */ |
| 47 | public class WifiMonitor { |
| 48 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 49 | private static final boolean DBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | private static final String TAG = "WifiMonitor"; |
| 51 | |
| 52 | /** Events we receive from the supplicant daemon */ |
| 53 | |
| 54 | private static final int CONNECTED = 1; |
| 55 | private static final int DISCONNECTED = 2; |
| 56 | private static final int STATE_CHANGE = 3; |
| 57 | private static final int SCAN_RESULTS = 4; |
| 58 | private static final int LINK_SPEED = 5; |
| 59 | private static final int TERMINATING = 6; |
| 60 | private static final int DRIVER_STATE = 7; |
Irfan Sheriff | b98d878 | 2011-01-19 15:09:14 -0800 | [diff] [blame] | 61 | private static final int EAP_FAILURE = 8; |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 62 | private static final int ASSOC_REJECT = 9; |
| 63 | private static final int UNKNOWN = 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
| 65 | /** All events coming from the supplicant start with this prefix */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 66 | private static final String EVENT_PREFIX_STR = "CTRL-EVENT-"; |
| 67 | private static final int EVENT_PREFIX_LEN_STR = EVENT_PREFIX_STR.length(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | /** All WPA events coming from the supplicant start with this prefix */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 70 | private static final String WPA_EVENT_PREFIX_STR = "WPA:"; |
| 71 | private static final String PASSWORD_MAY_BE_INCORRECT_STR = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | "pre-shared key may be incorrect"; |
| 73 | |
Irfan Sheriff | fcb659b | 2011-01-16 10:51:26 -0800 | [diff] [blame] | 74 | /* WPS events */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 75 | private static final String WPS_SUCCESS_STR = "WPS-SUCCESS"; |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 76 | |
| 77 | /* Format: WPS-FAIL msg=%d [config_error=%d] [reason=%d (%s)] */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 78 | private static final String WPS_FAIL_STR = "WPS-FAIL"; |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 79 | private static final String WPS_FAIL_PATTERN = |
| 80 | "WPS-FAIL msg=\\d+(?: config_error=(\\d+))?(?: reason=(\\d+))?"; |
| 81 | |
| 82 | /* config error code values for config_error=%d */ |
| 83 | private static final int CONFIG_MULTIPLE_PBC_DETECTED = 12; |
| 84 | private static final int CONFIG_AUTH_FAILURE = 18; |
| 85 | |
| 86 | /* reason code values for reason=%d */ |
| 87 | private static final int REASON_TKIP_ONLY_PROHIBITED = 1; |
| 88 | private static final int REASON_WEP_PROHIBITED = 2; |
| 89 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 90 | private static final String WPS_OVERLAP_STR = "WPS-OVERLAP-DETECTED"; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 91 | private static final String WPS_TIMEOUT_STR = "WPS-TIMEOUT"; |
Irfan Sheriff | fcb659b | 2011-01-16 10:51:26 -0800 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Names of events from wpa_supplicant (minus the prefix). In the |
| 95 | * format descriptions, * "<code>x</code>" |
| 96 | * designates a dynamic value that needs to be parsed out from the event |
| 97 | * string |
| 98 | */ |
| 99 | /** |
| 100 | * <pre> |
| 101 | * CTRL-EVENT-CONNECTED - Connection to xx:xx:xx:xx:xx:xx completed |
| 102 | * </pre> |
| 103 | * <code>xx:xx:xx:xx:xx:xx</code> is the BSSID of the associated access point |
| 104 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 105 | private static final String CONNECTED_STR = "CONNECTED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | /** |
| 107 | * <pre> |
| 108 | * CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys |
| 109 | * </pre> |
| 110 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 111 | private static final String DISCONNECTED_STR = "DISCONNECTED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | /** |
| 113 | * <pre> |
| 114 | * CTRL-EVENT-STATE-CHANGE x |
| 115 | * </pre> |
| 116 | * <code>x</code> is the numerical value of the new state. |
| 117 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 118 | private static final String STATE_CHANGE_STR = "STATE-CHANGE"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | /** |
| 120 | * <pre> |
| 121 | * CTRL-EVENT-SCAN-RESULTS ready |
| 122 | * </pre> |
| 123 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 124 | private static final String SCAN_RESULTS_STR = "SCAN-RESULTS"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * <pre> |
| 128 | * CTRL-EVENT-LINK-SPEED x Mb/s |
| 129 | * </pre> |
| 130 | * {@code x} is the link speed in Mb/sec. |
| 131 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 132 | private static final String LINK_SPEED_STR = "LINK-SPEED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | /** |
| 134 | * <pre> |
| 135 | * CTRL-EVENT-TERMINATING - signal x |
| 136 | * </pre> |
| 137 | * <code>x</code> is the signal that caused termination. |
| 138 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 139 | private static final String TERMINATING_STR = "TERMINATING"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | /** |
| 141 | * <pre> |
| 142 | * CTRL-EVENT-DRIVER-STATE state |
| 143 | * </pre> |
Irfan Sheriff | 1523da2 | 2011-06-10 14:00:27 -0700 | [diff] [blame] | 144 | * <code>state</code> can be HANGED |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 146 | private static final String DRIVER_STATE_STR = "DRIVER-STATE"; |
Irfan Sheriff | b98d878 | 2011-01-19 15:09:14 -0800 | [diff] [blame] | 147 | /** |
| 148 | * <pre> |
| 149 | * CTRL-EVENT-EAP-FAILURE EAP authentication failed |
| 150 | * </pre> |
| 151 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 152 | private static final String EAP_FAILURE_STR = "EAP-FAILURE"; |
Irfan Sheriff | b98d878 | 2011-01-19 15:09:14 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * This indicates an authentication failure on EAP FAILURE event |
| 156 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 157 | private static final String EAP_AUTH_FAILURE_STR = "EAP authentication failed"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 160 | * This indicates an assoc reject event |
| 161 | */ |
| 162 | private static final String ASSOC_REJECT_STR = "ASSOC-REJECT"; |
| 163 | |
| 164 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | * Regex pattern for extracting an Ethernet-style MAC address from a string. |
| 166 | * Matches a strings like the following:<pre> |
| 167 | * CTRL-EVENT-CONNECTED - Connection to 00:1e:58:ec:d5:6d completed (reauth) [id=1 id_str=]</pre> |
| 168 | */ |
| 169 | private static Pattern mConnectedEventPattern = |
| 170 | Pattern.compile("((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) .* \\[id=([0-9]+) "); |
| 171 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 172 | /** P2P events */ |
| 173 | private static final String P2P_EVENT_PREFIX_STR = "P2P"; |
| 174 | |
| 175 | /* P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13 pri_dev_type=1-0050F204-1 |
| 176 | name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 */ |
| 177 | private static final String P2P_DEVICE_FOUND_STR = "P2P-DEVICE-FOUND"; |
| 178 | |
| 179 | /* P2P-DEVICE-LOST p2p_dev_addr=42:fc:89:e1:e2:27 */ |
| 180 | private static final String P2P_DEVICE_LOST_STR = "P2P-DEVICE-LOST"; |
| 181 | |
Irfan Sheriff | c111d1c | 2012-03-28 15:59:30 -0700 | [diff] [blame] | 182 | /* P2P-FIND-STOPPED */ |
| 183 | private static final String P2P_FIND_STOPPED_STR = "P2P-FIND-STOPPED"; |
| 184 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 185 | /* P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 */ |
| 186 | private static final String P2P_GO_NEG_REQUEST_STR = "P2P-GO-NEG-REQUEST"; |
| 187 | |
| 188 | private static final String P2P_GO_NEG_SUCCESS_STR = "P2P-GO-NEG-SUCCESS"; |
| 189 | |
Irfan Sheriff | 9f452d0 | 2012-10-15 12:00:29 -0700 | [diff] [blame] | 190 | /* P2P-GO-NEG-FAILURE status=x */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 191 | private static final String P2P_GO_NEG_FAILURE_STR = "P2P-GO-NEG-FAILURE"; |
| 192 | |
| 193 | private static final String P2P_GROUP_FORMATION_SUCCESS_STR = |
| 194 | "P2P-GROUP-FORMATION-SUCCESS"; |
| 195 | |
| 196 | private static final String P2P_GROUP_FORMATION_FAILURE_STR = |
| 197 | "P2P-GROUP-FORMATION-FAILURE"; |
| 198 | |
| 199 | /* P2P-GROUP-STARTED p2p-wlan0-0 [client|GO] ssid="DIRECT-W8" freq=2437 |
| 200 | [psk=2182b2e50e53f260d04f3c7b25ef33c965a3291b9b36b455a82d77fd82ca15bc|passphrase="fKG4jMe3"] |
Yoshihiko Ikenaga | 0879d03 | 2012-08-16 23:37:36 +0900 | [diff] [blame] | 201 | go_dev_addr=fa:7b:7a:42:02:13 [PERSISTENT] */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 202 | private static final String P2P_GROUP_STARTED_STR = "P2P-GROUP-STARTED"; |
| 203 | |
| 204 | /* P2P-GROUP-REMOVED p2p-wlan0-0 [client|GO] reason=REQUESTED */ |
| 205 | private static final String P2P_GROUP_REMOVED_STR = "P2P-GROUP-REMOVED"; |
| 206 | |
| 207 | /* P2P-INVITATION-RECEIVED sa=fa:7b:7a:42:02:13 go_dev_addr=f8:7b:7a:42:02:13 |
| 208 | bssid=fa:7b:7a:42:82:13 unknown-network */ |
| 209 | private static final String P2P_INVITATION_RECEIVED_STR = "P2P-INVITATION-RECEIVED"; |
| 210 | |
| 211 | /* P2P-INVITATION-RESULT status=1 */ |
| 212 | private static final String P2P_INVITATION_RESULT_STR = "P2P-INVITATION-RESULT"; |
| 213 | |
| 214 | /* P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 |
| 215 | pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 |
| 216 | group_capab=0x0 */ |
| 217 | private static final String P2P_PROV_DISC_PBC_REQ_STR = "P2P-PROV-DISC-PBC-REQ"; |
Irfan Sheriff | 618455f | 2011-11-18 14:33:09 -0800 | [diff] [blame] | 218 | |
| 219 | /* P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36 */ |
| 220 | private static final String P2P_PROV_DISC_PBC_RSP_STR = "P2P-PROV-DISC-PBC-RESP"; |
| 221 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 222 | /* P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 |
| 223 | pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 |
| 224 | group_capab=0x0 */ |
| 225 | private static final String P2P_PROV_DISC_ENTER_PIN_STR = "P2P-PROV-DISC-ENTER-PIN"; |
| 226 | /* P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607 p2p_dev_addr=42:fc:89:e1:e2:27 |
| 227 | pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 |
| 228 | group_capab=0x0 */ |
| 229 | private static final String P2P_PROV_DISC_SHOW_PIN_STR = "P2P-PROV-DISC-SHOW-PIN"; |
Irfan Sheriff | c41096e | 2012-09-24 14:21:54 -0700 | [diff] [blame] | 230 | /* P2P-PROV-DISC-FAILURE p2p_dev_addr=42:fc:89:e1:e2:27 */ |
| 231 | private static final String P2P_PROV_DISC_FAILURE_STR = "P2P-PROV-DISC-FAILURE"; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 232 | |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Protocol format is as follows.<br> |
| 235 | * See the Table.62 in the WiFi Direct specification for the detail. |
| 236 | * ______________________________________________________________ |
| 237 | * | Length(2byte) | Type(1byte) | TransId(1byte)}| |
| 238 | * ______________________________________________________________ |
| 239 | * | status(1byte) | vendor specific(variable) | |
| 240 | * |
| 241 | * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 0300000101 |
| 242 | * length=3, service type=0(ALL Service), transaction id=1, |
| 243 | * status=1(service protocol type not available)<br> |
| 244 | * |
| 245 | * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 0300020201 |
| 246 | * length=3, service type=2(UPnP), transaction id=2, |
| 247 | * status=1(service protocol type not available) |
| 248 | * |
| 249 | * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 990002030010757569643a3131323 |
| 250 | * 2646534652d383537342d353961622d393332322d3333333435363738393034343a3 |
| 251 | * a75726e3a736368656d61732d75706e702d6f72673a736572766963653a436f6e746 |
| 252 | * 56e744469726563746f72793a322c757569643a36383539646564652d383537342d3 |
| 253 | * 53961622d393333322d3132333435363738393031323a3a75706e703a726f6f74646 |
| 254 | * 576696365 |
| 255 | * length=153,type=2(UPnP),transaction id=3,status=0 |
| 256 | * |
| 257 | * UPnP Protocol format is as follows. |
| 258 | * ______________________________________________________ |
| 259 | * | Version (1) | USN (Variable) | |
| 260 | * |
| 261 | * version=0x10(UPnP1.0) data=usn:uuid:1122de4e-8574-59ab-9322-33345678 |
| 262 | * 9044::urn:schemas-upnp-org:service:ContentDirectory:2,usn:uuid:6859d |
| 263 | * ede-8574-59ab-9332-123456789012::upnp:rootdevice |
| 264 | * |
| 265 | * P2P-SERV-DISC-RESP 58:17:0c:bc:dd:ca 21 1900010200045f6970 |
| 266 | * 70c00c000c01094d795072696e746572c027 |
| 267 | * length=25, type=1(Bonjour),transaction id=2,status=0 |
| 268 | * |
| 269 | * Bonjour Protocol format is as follows. |
| 270 | * __________________________________________________________ |
| 271 | * |DNS Name(Variable)|DNS Type(1)|Version(1)|RDATA(Variable)| |
| 272 | * |
| 273 | * DNS Name=_ipp._tcp.local.,DNS type=12(PTR), Version=1, |
| 274 | * RDATA=MyPrinter._ipp._tcp.local. |
| 275 | * |
| 276 | */ |
| 277 | private static final String P2P_SERV_DISC_RESP_STR = "P2P-SERV-DISC-RESP"; |
| 278 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 279 | private static final String HOST_AP_EVENT_PREFIX_STR = "AP"; |
Irfan Sheriff | bfed2d6 | 2011-12-09 12:40:04 -0800 | [diff] [blame] | 280 | /* AP-STA-CONNECTED 42:fc:89:a8:96:09 dev_addr=02:90:4c:a0:92:54 */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 281 | private static final String AP_STA_CONNECTED_STR = "AP-STA-CONNECTED"; |
| 282 | /* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 */ |
| 283 | private static final String AP_STA_DISCONNECTED_STR = "AP-STA-DISCONNECTED"; |
| 284 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 285 | /* Supplicant events reported to a state machine */ |
| 286 | private static final int BASE = Protocol.BASE_WIFI_MONITOR; |
| 287 | |
| 288 | /* Connection to supplicant established */ |
| 289 | public static final int SUP_CONNECTION_EVENT = BASE + 1; |
| 290 | /* Connection to supplicant lost */ |
| 291 | public static final int SUP_DISCONNECTION_EVENT = BASE + 2; |
| 292 | /* Network connection completed */ |
| 293 | public static final int NETWORK_CONNECTION_EVENT = BASE + 3; |
| 294 | /* Network disconnection completed */ |
| 295 | public static final int NETWORK_DISCONNECTION_EVENT = BASE + 4; |
| 296 | /* Scan results are available */ |
| 297 | public static final int SCAN_RESULTS_EVENT = BASE + 5; |
| 298 | /* Supplicate state changed */ |
| 299 | public static final int SUPPLICANT_STATE_CHANGE_EVENT = BASE + 6; |
| 300 | /* Password failure and EAP authentication failure */ |
| 301 | public static final int AUTHENTICATION_FAILURE_EVENT = BASE + 7; |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 302 | /* WPS success detected */ |
| 303 | public static final int WPS_SUCCESS_EVENT = BASE + 8; |
| 304 | /* WPS failure detected */ |
| 305 | public static final int WPS_FAIL_EVENT = BASE + 9; |
| 306 | /* WPS overlap detected */ |
| 307 | public static final int WPS_OVERLAP_EVENT = BASE + 10; |
| 308 | /* WPS timeout detected */ |
| 309 | public static final int WPS_TIMEOUT_EVENT = BASE + 11; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 310 | /* Driver was hung */ |
Irfan Sheriff | d3975a9 | 2012-02-24 10:54:13 -0800 | [diff] [blame] | 311 | public static final int DRIVER_HUNG_EVENT = BASE + 12; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 312 | |
| 313 | /* P2P events */ |
| 314 | public static final int P2P_DEVICE_FOUND_EVENT = BASE + 21; |
| 315 | public static final int P2P_DEVICE_LOST_EVENT = BASE + 22; |
| 316 | public static final int P2P_GO_NEGOTIATION_REQUEST_EVENT = BASE + 23; |
| 317 | public static final int P2P_GO_NEGOTIATION_SUCCESS_EVENT = BASE + 25; |
| 318 | public static final int P2P_GO_NEGOTIATION_FAILURE_EVENT = BASE + 26; |
| 319 | public static final int P2P_GROUP_FORMATION_SUCCESS_EVENT = BASE + 27; |
| 320 | public static final int P2P_GROUP_FORMATION_FAILURE_EVENT = BASE + 28; |
| 321 | public static final int P2P_GROUP_STARTED_EVENT = BASE + 29; |
| 322 | public static final int P2P_GROUP_REMOVED_EVENT = BASE + 30; |
| 323 | public static final int P2P_INVITATION_RECEIVED_EVENT = BASE + 31; |
| 324 | public static final int P2P_INVITATION_RESULT_EVENT = BASE + 32; |
| 325 | public static final int P2P_PROV_DISC_PBC_REQ_EVENT = BASE + 33; |
Irfan Sheriff | 618455f | 2011-11-18 14:33:09 -0800 | [diff] [blame] | 326 | public static final int P2P_PROV_DISC_PBC_RSP_EVENT = BASE + 34; |
| 327 | public static final int P2P_PROV_DISC_ENTER_PIN_EVENT = BASE + 35; |
| 328 | public static final int P2P_PROV_DISC_SHOW_PIN_EVENT = BASE + 36; |
Irfan Sheriff | c111d1c | 2012-03-28 15:59:30 -0700 | [diff] [blame] | 329 | public static final int P2P_FIND_STOPPED_EVENT = BASE + 37; |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 330 | public static final int P2P_SERV_DISC_RESP_EVENT = BASE + 38; |
Irfan Sheriff | c41096e | 2012-09-24 14:21:54 -0700 | [diff] [blame] | 331 | public static final int P2P_PROV_DISC_FAILURE_EVENT = BASE + 39; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 332 | |
| 333 | /* hostap events */ |
| 334 | public static final int AP_STA_DISCONNECTED_EVENT = BASE + 41; |
| 335 | public static final int AP_STA_CONNECTED_EVENT = BASE + 42; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | |
Deepthi Gowri | c1b631e | 2013-04-30 18:23:57 +0530 | [diff] [blame] | 337 | /* Indicates assoc reject event */ |
| 338 | public static final int ASSOCIATION_REJECTION_EVENT = BASE + 43; |
Irfan Sheriff | 05d7211 | 2010-01-27 15:45:27 -0800 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * This indicates a read error on the monitor socket conenction |
| 342 | */ |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 343 | private static final String WPA_RECV_ERROR_STR = "recv error"; |
Irfan Sheriff | 05d7211 | 2010-01-27 15:45:27 -0800 | [diff] [blame] | 344 | |
| 345 | /** |
Irfan Sheriff | 05d7211 | 2010-01-27 15:45:27 -0800 | [diff] [blame] | 346 | * Max errors before we close supplicant connection |
| 347 | */ |
| 348 | private static final int MAX_RECV_ERRORS = 10; |
| 349 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 350 | private final String mInterfaceName; |
| 351 | private final WifiNative mWifiNative; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 352 | private final StateMachine mStateMachine; |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 353 | private boolean mMonitoring; |
| 354 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 355 | // This is a global counter, since it's not monitor specific. However, the existing |
| 356 | // implementation forwards all "global" control events like CTRL-EVENT-TERMINATING |
| 357 | // to the p2p0 monitor. Is that expected ? It seems a bit surprising. |
| 358 | // |
| 359 | // TODO: If the p2p0 monitor isn't registered, the behaviour is even more surprising. |
| 360 | // The event will be dispatched to all monitors, and each of them will end up incrementing |
| 361 | // it in their dispatchXXX method. If we have 5 registered monitors (say), 2 consecutive |
| 362 | // recv errors will cause us to disconnect from the supplicant (instead of the intended 10). |
| 363 | // |
| 364 | // This variable is always accessed and modified under a WifiMonitorSingleton lock. |
| 365 | private static int sRecvErrors; |
| 366 | |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 367 | public WifiMonitor(StateMachine wifiStateMachine, WifiNative wifiNative) { |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 368 | if (DBG) Log.d(TAG, "Creating WifiMonitor"); |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 369 | mWifiNative = wifiNative; |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 370 | mInterfaceName = wifiNative.mInterfaceName; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 371 | mStateMachine = wifiStateMachine; |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 372 | mMonitoring = false; |
| 373 | |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 374 | WifiMonitorSingleton.sInstance.registerInterfaceMonitor(mInterfaceName, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | public void startMonitoring() { |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 378 | WifiMonitorSingleton.sInstance.startMonitoring(mInterfaceName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 381 | public void stopMonitoring() { |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 382 | WifiMonitorSingleton.sInstance.stopMonitoring(mInterfaceName); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | public void stopSupplicant() { |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 386 | WifiMonitorSingleton.sInstance.stopSupplicant(); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | public void killSupplicant(boolean p2pSupported) { |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 390 | WifiMonitorSingleton.sInstance.killSupplicant(p2pSupported); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | private static class WifiMonitorSingleton { |
Narayan Kamath | a2bcee6 | 2013-09-05 17:32:08 +0100 | [diff] [blame] | 394 | private static final WifiMonitorSingleton sInstance = new WifiMonitorSingleton(); |
| 395 | |
| 396 | private final HashMap<String, WifiMonitor> mIfaceMap = new HashMap<String, WifiMonitor>(); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 397 | private boolean mConnected = false; |
| 398 | private WifiNative mWifiNative; |
| 399 | |
| 400 | private WifiMonitorSingleton() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 402 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 403 | public synchronized void startMonitoring(String iface) { |
| 404 | WifiMonitor m = mIfaceMap.get(iface); |
| 405 | if (m == null) { |
| 406 | Log.e(TAG, "startMonitor called with unknown iface=" + iface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 410 | Log.d(TAG, "startMonitoring(" + iface + ") with mConnected = " + mConnected); |
| 411 | |
| 412 | if (mConnected) { |
| 413 | m.mMonitoring = true; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 414 | m.mStateMachine.sendMessage(SUP_CONNECTION_EVENT); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 415 | } else { |
| 416 | if (DBG) Log.d(TAG, "connecting to supplicant"); |
| 417 | int connectTries = 0; |
| 418 | while (true) { |
| 419 | if (mWifiNative.connectToSupplicant()) { |
| 420 | m.mMonitoring = true; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 421 | m.mStateMachine.sendMessage(SUP_CONNECTION_EVENT); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 422 | new MonitorThread(mWifiNative, this).start(); |
| 423 | mConnected = true; |
| 424 | break; |
| 425 | } |
| 426 | if (connectTries++ < 5) { |
| 427 | try { |
| 428 | Thread.sleep(1000); |
| 429 | } catch (InterruptedException ignore) { |
| 430 | } |
| 431 | } else { |
| 432 | mIfaceMap.remove(iface); |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 433 | m.mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT); |
Vinit Deshapnde | e8fabf9 | 2013-08-23 11:53:40 -0700 | [diff] [blame] | 434 | Log.e(TAG, "startMonitoring(" + iface + ") failed!"); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | public synchronized void stopMonitoring(String iface) { |
| 442 | WifiMonitor m = mIfaceMap.get(iface); |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 443 | if (DBG) Log.d(TAG, "stopMonitoring(" + iface + ") = " + m.mStateMachine); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 444 | m.mMonitoring = false; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 445 | m.mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | public synchronized void registerInterfaceMonitor(String iface, WifiMonitor m) { |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 449 | if (DBG) Log.d(TAG, "registerInterface(" + iface + "+" + m.mStateMachine + ")"); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 450 | mIfaceMap.put(iface, m); |
| 451 | if (mWifiNative == null) { |
| 452 | mWifiNative = m.mWifiNative; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | public synchronized void unregisterInterfaceMonitor(String iface) { |
| 457 | // REVIEW: When should we call this? If this isn't called, then WifiMonitor |
| 458 | // objects will remain in the mIfaceMap; and won't ever get deleted |
| 459 | |
| 460 | WifiMonitor m = mIfaceMap.remove(iface); |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 461 | if (DBG) Log.d(TAG, "unregisterInterface(" + iface + "+" + m.mStateMachine + ")"); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | public synchronized void stopSupplicant() { |
| 465 | mWifiNative.stopSupplicant(); |
| 466 | } |
| 467 | |
| 468 | public synchronized void killSupplicant(boolean p2pSupported) { |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 469 | WifiNative.killSupplicant(p2pSupported); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 470 | mConnected = false; |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 471 | for (WifiMonitor m : mIfaceMap.values()) { |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 472 | m.mMonitoring = false; |
| 473 | } |
| 474 | } |
| 475 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 476 | private synchronized boolean dispatchEvent(String eventStr) { |
| 477 | String iface; |
| 478 | if (eventStr.startsWith("IFNAME=")) { |
| 479 | int space = eventStr.indexOf(' '); |
| 480 | if (space != -1) { |
| 481 | iface = eventStr.substring(7, space); |
| 482 | if (!mIfaceMap.containsKey(iface) && iface.startsWith("p2p-")) { |
| 483 | // p2p interfaces are created dynamically, but we have |
| 484 | // only one P2p state machine monitoring all of them; look |
| 485 | // for it explicitly, and send messages there .. |
| 486 | iface = "p2p0"; |
| 487 | } |
| 488 | eventStr = eventStr.substring(space + 1); |
| 489 | } else { |
| 490 | // No point dispatching this event to any interface, the dispatched |
| 491 | // event string will begin with "IFNAME=" which dispatchEvent can't really |
| 492 | // do anything about. |
| 493 | Log.e(TAG, "Dropping malformed event (unparsable iface): " + eventStr); |
| 494 | return false; |
| 495 | } |
| 496 | } else { |
| 497 | // events without prefix belong to p2p0 monitor |
| 498 | iface = "p2p0"; |
| 499 | } |
| 500 | |
| 501 | if (DBG) Log.d(TAG, "Dispatching event to interface: " + iface); |
| 502 | |
| 503 | WifiMonitor m = mIfaceMap.get(iface); |
| 504 | if (m != null) { |
| 505 | if (m.mMonitoring) { |
| 506 | if (m.dispatchEvent(eventStr)) { |
| 507 | mConnected = false; |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | return false; |
| 512 | } else { |
| 513 | if (DBG) Log.d(TAG, "Dropping event because (" + iface + ") is stopped"); |
| 514 | return false; |
| 515 | } |
| 516 | } else { |
| 517 | if (DBG) Log.d(TAG, "Sending to all monitors because there's no matching iface"); |
| 518 | boolean done = false; |
| 519 | for (WifiMonitor monitor : mIfaceMap.values()) { |
| 520 | if (monitor.mMonitoring && monitor.dispatchEvent(eventStr)) { |
| 521 | done = true; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (done) { |
| 526 | mConnected = false; |
| 527 | } |
| 528 | |
| 529 | return done; |
| 530 | } |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | private static class MonitorThread extends Thread { |
| 535 | private final WifiNative mWifiNative; |
| 536 | private final WifiMonitorSingleton mWifiMonitorSingleton; |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 537 | |
| 538 | public MonitorThread(WifiNative wifiNative, WifiMonitorSingleton wifiMonitorSingleton) { |
| 539 | super("WifiMonitor"); |
| 540 | mWifiNative = wifiNative; |
| 541 | mWifiMonitorSingleton = wifiMonitorSingleton; |
| 542 | } |
| 543 | |
| 544 | public void run() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | //noinspection InfiniteLoopStatement |
| 546 | for (;;) { |
Irfan Sheriff | fc7f95a | 2012-01-04 14:50:09 -0800 | [diff] [blame] | 547 | String eventStr = mWifiNative.waitForEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | // Skip logging the common but mostly uninteresting scan-results event |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 550 | if (DBG && eventStr.indexOf(SCAN_RESULTS_STR) == -1) { |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 551 | Log.d(TAG, "Event [" + eventStr + "]"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 553 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 554 | if (mWifiMonitorSingleton.dispatchEvent(eventStr)) { |
| 555 | if (DBG) Log.d(TAG, "Disconnecting from the supplicant, no more events"); |
| 556 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 562 | /* @return true if the event was supplicant disconnection */ |
| 563 | private boolean dispatchEvent(String eventStr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 565 | if (!eventStr.startsWith(EVENT_PREFIX_STR)) { |
| 566 | if (eventStr.startsWith(WPA_EVENT_PREFIX_STR) && |
| 567 | 0 < eventStr.indexOf(PASSWORD_MAY_BE_INCORRECT_STR)) { |
| 568 | mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT); |
| 569 | } else if (eventStr.startsWith(WPS_SUCCESS_STR)) { |
| 570 | mStateMachine.sendMessage(WPS_SUCCESS_EVENT); |
| 571 | } else if (eventStr.startsWith(WPS_FAIL_STR)) { |
| 572 | handleWpsFailEvent(eventStr); |
| 573 | } else if (eventStr.startsWith(WPS_OVERLAP_STR)) { |
| 574 | mStateMachine.sendMessage(WPS_OVERLAP_EVENT); |
| 575 | } else if (eventStr.startsWith(WPS_TIMEOUT_STR)) { |
| 576 | mStateMachine.sendMessage(WPS_TIMEOUT_EVENT); |
| 577 | } else if (eventStr.startsWith(P2P_EVENT_PREFIX_STR)) { |
| 578 | handleP2pEvents(eventStr); |
| 579 | } else if (eventStr.startsWith(HOST_AP_EVENT_PREFIX_STR)) { |
| 580 | handleHostApEvents(eventStr); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 581 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 582 | else { |
| 583 | if (DBG) Log.w(TAG, "couldn't identify event type - " + eventStr); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | return false; |
| 586 | } |
| 587 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 588 | String eventName = eventStr.substring(EVENT_PREFIX_LEN_STR); |
| 589 | int nameEnd = eventName.indexOf(' '); |
| 590 | if (nameEnd != -1) |
| 591 | eventName = eventName.substring(0, nameEnd); |
| 592 | if (eventName.length() == 0) { |
| 593 | if (DBG) Log.i(TAG, "Received wpa_supplicant event with empty event name"); |
| 594 | return false; |
| 595 | } |
| 596 | /* |
| 597 | * Map event name into event enum |
| 598 | */ |
| 599 | int event; |
| 600 | if (eventName.equals(CONNECTED_STR)) |
| 601 | event = CONNECTED; |
| 602 | else if (eventName.equals(DISCONNECTED_STR)) |
| 603 | event = DISCONNECTED; |
| 604 | else if (eventName.equals(STATE_CHANGE_STR)) |
| 605 | event = STATE_CHANGE; |
| 606 | else if (eventName.equals(SCAN_RESULTS_STR)) |
| 607 | event = SCAN_RESULTS; |
| 608 | else if (eventName.equals(LINK_SPEED_STR)) |
| 609 | event = LINK_SPEED; |
| 610 | else if (eventName.equals(TERMINATING_STR)) |
| 611 | event = TERMINATING; |
| 612 | else if (eventName.equals(DRIVER_STATE_STR)) |
| 613 | event = DRIVER_STATE; |
| 614 | else if (eventName.equals(EAP_FAILURE_STR)) |
| 615 | event = EAP_FAILURE; |
| 616 | else if (eventName.equals(ASSOC_REJECT_STR)) |
| 617 | event = ASSOC_REJECT; |
| 618 | else |
| 619 | event = UNKNOWN; |
| 620 | |
| 621 | String eventData = eventStr; |
| 622 | if (event == DRIVER_STATE || event == LINK_SPEED) |
| 623 | eventData = eventData.split(" ")[1]; |
| 624 | else if (event == STATE_CHANGE || event == EAP_FAILURE) { |
| 625 | int ind = eventStr.indexOf(" "); |
| 626 | if (ind != -1) { |
| 627 | eventData = eventStr.substring(ind + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 629 | } else { |
| 630 | int ind = eventStr.indexOf(" - "); |
| 631 | if (ind != -1) { |
| 632 | eventData = eventStr.substring(ind + 3); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 636 | if (event == STATE_CHANGE) { |
| 637 | handleSupplicantStateChange(eventData); |
| 638 | } else if (event == DRIVER_STATE) { |
| 639 | handleDriverEvent(eventData); |
| 640 | } else if (event == TERMINATING) { |
| 641 | /** |
| 642 | * Close the supplicant connection if we see |
| 643 | * too many recv errors |
| 644 | */ |
| 645 | if (eventData.startsWith(WPA_RECV_ERROR_STR)) { |
| 646 | if (++sRecvErrors > MAX_RECV_ERRORS) { |
| 647 | if (DBG) { |
| 648 | Log.d(TAG, "too many recv errors, closing connection"); |
Irfan Sheriff | 86a5f5b | 2012-02-28 17:03:56 -0800 | [diff] [blame] | 649 | } |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 650 | } else { |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // notify and exit |
| 656 | mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT); |
| 657 | return true; |
| 658 | } else if (event == EAP_FAILURE) { |
| 659 | if (eventData.startsWith(EAP_AUTH_FAILURE_STR)) { |
| 660 | mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT); |
| 661 | } |
| 662 | } else if (event == ASSOC_REJECT) { |
| 663 | mStateMachine.sendMessage(ASSOCIATION_REJECTION_EVENT); |
| 664 | } else { |
| 665 | handleEvent(event, eventData); |
| 666 | } |
| 667 | sRecvErrors = 0; |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | private void handleDriverEvent(String state) { |
| 672 | if (state == null) { |
| 673 | return; |
| 674 | } |
| 675 | if (state.equals("HANGED")) { |
| 676 | mStateMachine.sendMessage(DRIVER_HUNG_EVENT); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Handle all supplicant events except STATE-CHANGE |
| 682 | * @param event the event type |
| 683 | * @param remainder the rest of the string following the |
| 684 | * event name and " — " |
| 685 | */ |
| 686 | void handleEvent(int event, String remainder) { |
| 687 | switch (event) { |
| 688 | case DISCONNECTED: |
| 689 | handleNetworkStateChange(NetworkInfo.DetailedState.DISCONNECTED, remainder); |
| 690 | break; |
| 691 | |
| 692 | case CONNECTED: |
| 693 | handleNetworkStateChange(NetworkInfo.DetailedState.CONNECTED, remainder); |
| 694 | break; |
| 695 | |
| 696 | case SCAN_RESULTS: |
| 697 | mStateMachine.sendMessage(SCAN_RESULTS_EVENT); |
| 698 | break; |
| 699 | |
| 700 | case UNKNOWN: |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | private void handleWpsFailEvent(String dataString) { |
| 706 | final Pattern p = Pattern.compile(WPS_FAIL_PATTERN); |
| 707 | Matcher match = p.matcher(dataString); |
| 708 | if (match.find()) { |
| 709 | String cfgErr = match.group(1); |
| 710 | String reason = match.group(2); |
| 711 | |
| 712 | if (reason != null) { |
| 713 | switch(Integer.parseInt(reason)) { |
| 714 | case REASON_TKIP_ONLY_PROHIBITED: |
| 715 | mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, |
| 716 | WifiManager.WPS_TKIP_ONLY_PROHIBITED, 0)); |
| 717 | return; |
| 718 | case REASON_WEP_PROHIBITED: |
| 719 | mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, |
| 720 | WifiManager.WPS_WEP_PROHIBITED, 0)); |
| 721 | return; |
| 722 | } |
| 723 | } |
| 724 | if (cfgErr != null) { |
| 725 | switch(Integer.parseInt(cfgErr)) { |
| 726 | case CONFIG_AUTH_FAILURE: |
| 727 | mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, |
| 728 | WifiManager.WPS_AUTH_FAILURE, 0)); |
| 729 | return; |
| 730 | case CONFIG_MULTIPLE_PBC_DETECTED: |
| 731 | mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, |
| 732 | WifiManager.WPS_OVERLAP_ERROR, 0)); |
| 733 | return; |
Irfan Sheriff | 21ba815 | 2012-04-04 16:22:21 -0700 | [diff] [blame] | 734 | } |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 737 | //For all other errors, return a generic internal error |
| 738 | mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, |
| 739 | WifiManager.ERROR, 0)); |
| 740 | } |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 741 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 742 | /* <event> status=<err> and the special case of <event> reason=FREQ_CONFLICT */ |
| 743 | private P2pStatus p2pError(String dataString) { |
| 744 | P2pStatus err = P2pStatus.UNKNOWN; |
| 745 | String[] tokens = dataString.split(" "); |
| 746 | if (tokens.length < 2) return err; |
| 747 | String[] nameValue = tokens[1].split("="); |
| 748 | if (nameValue.length != 2) return err; |
| 749 | |
| 750 | /* Handle the special case of reason=FREQ+CONFLICT */ |
| 751 | if (nameValue[1].equals("FREQ_CONFLICT")) { |
| 752 | return P2pStatus.NO_COMMON_CHANNEL; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 753 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 754 | try { |
| 755 | err = P2pStatus.valueOf(Integer.parseInt(nameValue[1])); |
| 756 | } catch (NumberFormatException e) { |
| 757 | e.printStackTrace(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 759 | return err; |
| 760 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 762 | /** |
| 763 | * Handle p2p events |
| 764 | */ |
| 765 | private void handleP2pEvents(String dataString) { |
| 766 | if (dataString.startsWith(P2P_DEVICE_FOUND_STR)) { |
| 767 | mStateMachine.sendMessage(P2P_DEVICE_FOUND_EVENT, new WifiP2pDevice(dataString)); |
| 768 | } else if (dataString.startsWith(P2P_DEVICE_LOST_STR)) { |
| 769 | mStateMachine.sendMessage(P2P_DEVICE_LOST_EVENT, new WifiP2pDevice(dataString)); |
| 770 | } else if (dataString.startsWith(P2P_FIND_STOPPED_STR)) { |
| 771 | mStateMachine.sendMessage(P2P_FIND_STOPPED_EVENT); |
| 772 | } else if (dataString.startsWith(P2P_GO_NEG_REQUEST_STR)) { |
| 773 | mStateMachine.sendMessage(P2P_GO_NEGOTIATION_REQUEST_EVENT, |
| 774 | new WifiP2pConfig(dataString)); |
| 775 | } else if (dataString.startsWith(P2P_GO_NEG_SUCCESS_STR)) { |
| 776 | mStateMachine.sendMessage(P2P_GO_NEGOTIATION_SUCCESS_EVENT); |
| 777 | } else if (dataString.startsWith(P2P_GO_NEG_FAILURE_STR)) { |
| 778 | mStateMachine.sendMessage(P2P_GO_NEGOTIATION_FAILURE_EVENT, p2pError(dataString)); |
| 779 | } else if (dataString.startsWith(P2P_GROUP_FORMATION_SUCCESS_STR)) { |
| 780 | mStateMachine.sendMessage(P2P_GROUP_FORMATION_SUCCESS_EVENT); |
| 781 | } else if (dataString.startsWith(P2P_GROUP_FORMATION_FAILURE_STR)) { |
| 782 | mStateMachine.sendMessage(P2P_GROUP_FORMATION_FAILURE_EVENT, p2pError(dataString)); |
| 783 | } else if (dataString.startsWith(P2P_GROUP_STARTED_STR)) { |
| 784 | mStateMachine.sendMessage(P2P_GROUP_STARTED_EVENT, new WifiP2pGroup(dataString)); |
| 785 | } else if (dataString.startsWith(P2P_GROUP_REMOVED_STR)) { |
| 786 | mStateMachine.sendMessage(P2P_GROUP_REMOVED_EVENT, new WifiP2pGroup(dataString)); |
| 787 | } else if (dataString.startsWith(P2P_INVITATION_RECEIVED_STR)) { |
| 788 | mStateMachine.sendMessage(P2P_INVITATION_RECEIVED_EVENT, |
| 789 | new WifiP2pGroup(dataString)); |
| 790 | } else if (dataString.startsWith(P2P_INVITATION_RESULT_STR)) { |
| 791 | mStateMachine.sendMessage(P2P_INVITATION_RESULT_EVENT, p2pError(dataString)); |
| 792 | } else if (dataString.startsWith(P2P_PROV_DISC_PBC_REQ_STR)) { |
| 793 | mStateMachine.sendMessage(P2P_PROV_DISC_PBC_REQ_EVENT, |
| 794 | new WifiP2pProvDiscEvent(dataString)); |
| 795 | } else if (dataString.startsWith(P2P_PROV_DISC_PBC_RSP_STR)) { |
| 796 | mStateMachine.sendMessage(P2P_PROV_DISC_PBC_RSP_EVENT, |
| 797 | new WifiP2pProvDiscEvent(dataString)); |
| 798 | } else if (dataString.startsWith(P2P_PROV_DISC_ENTER_PIN_STR)) { |
| 799 | mStateMachine.sendMessage(P2P_PROV_DISC_ENTER_PIN_EVENT, |
| 800 | new WifiP2pProvDiscEvent(dataString)); |
| 801 | } else if (dataString.startsWith(P2P_PROV_DISC_SHOW_PIN_STR)) { |
| 802 | mStateMachine.sendMessage(P2P_PROV_DISC_SHOW_PIN_EVENT, |
| 803 | new WifiP2pProvDiscEvent(dataString)); |
| 804 | } else if (dataString.startsWith(P2P_PROV_DISC_FAILURE_STR)) { |
| 805 | mStateMachine.sendMessage(P2P_PROV_DISC_FAILURE_EVENT); |
| 806 | } else if (dataString.startsWith(P2P_SERV_DISC_RESP_STR)) { |
| 807 | List<WifiP2pServiceResponse> list = WifiP2pServiceResponse.newInstance(dataString); |
| 808 | if (list != null) { |
| 809 | mStateMachine.sendMessage(P2P_SERV_DISC_RESP_EVENT, list); |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 810 | } else { |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 811 | Log.e(TAG, "Null service resp " + dataString); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Handle hostap events |
| 818 | */ |
| 819 | private void handleHostApEvents(String dataString) { |
| 820 | String[] tokens = dataString.split(" "); |
| 821 | /* AP-STA-CONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ |
| 822 | if (tokens[0].equals(AP_STA_CONNECTED_STR)) { |
| 823 | mStateMachine.sendMessage(AP_STA_CONNECTED_EVENT, new WifiP2pDevice(dataString)); |
| 824 | /* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ |
| 825 | } else if (tokens[0].equals(AP_STA_DISCONNECTED_STR)) { |
| 826 | mStateMachine.sendMessage(AP_STA_DISCONNECTED_EVENT, new WifiP2pDevice(dataString)); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Handle the supplicant STATE-CHANGE event |
| 832 | * @param dataString New supplicant state string in the format: |
| 833 | * id=network-id state=new-state |
| 834 | */ |
| 835 | private void handleSupplicantStateChange(String dataString) { |
| 836 | WifiSsid wifiSsid = null; |
| 837 | int index = dataString.lastIndexOf("SSID="); |
| 838 | if (index != -1) { |
| 839 | wifiSsid = WifiSsid.createFromAsciiEncoded( |
| 840 | dataString.substring(index + 5)); |
| 841 | } |
| 842 | String[] dataTokens = dataString.split(" "); |
| 843 | |
| 844 | String BSSID = null; |
| 845 | int networkId = -1; |
| 846 | int newState = -1; |
| 847 | for (String token : dataTokens) { |
| 848 | String[] nameValue = token.split("="); |
| 849 | if (nameValue.length != 2) { |
| 850 | continue; |
| 851 | } |
| 852 | |
| 853 | if (nameValue[0].equals("BSSID")) { |
| 854 | BSSID = nameValue[1]; |
| 855 | continue; |
| 856 | } |
| 857 | |
| 858 | int value; |
| 859 | try { |
| 860 | value = Integer.parseInt(nameValue[1]); |
| 861 | } catch (NumberFormatException e) { |
| 862 | continue; |
| 863 | } |
| 864 | |
| 865 | if (nameValue[0].equals("id")) { |
| 866 | networkId = value; |
| 867 | } else if (nameValue[0].equals("state")) { |
| 868 | newState = value; |
Vinit Deshapnde | c249c2c | 2013-08-08 10:38:53 -0700 | [diff] [blame] | 869 | } |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 870 | } |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 871 | |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 872 | if (newState == -1) return; |
| 873 | |
| 874 | SupplicantState newSupplicantState = SupplicantState.INVALID; |
| 875 | for (SupplicantState state : SupplicantState.values()) { |
| 876 | if (state.ordinal() == newState) { |
| 877 | newSupplicantState = state; |
| 878 | break; |
| 879 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
Narayan Kamath | 3f94640 | 2013-09-06 12:25:27 +0100 | [diff] [blame] | 881 | if (newSupplicantState == SupplicantState.INVALID) { |
| 882 | Log.w(TAG, "Invalid supplicant state: " + newState); |
| 883 | } |
| 884 | notifySupplicantStateChange(networkId, wifiSsid, BSSID, newSupplicantState); |
| 885 | } |
| 886 | |
| 887 | private void handleNetworkStateChange(NetworkInfo.DetailedState newState, String data) { |
| 888 | String BSSID = null; |
| 889 | int networkId = -1; |
| 890 | if (newState == NetworkInfo.DetailedState.CONNECTED) { |
| 891 | Matcher match = mConnectedEventPattern.matcher(data); |
| 892 | if (!match.find()) { |
| 893 | if (DBG) Log.d(TAG, "Could not find BSSID in CONNECTED event string"); |
| 894 | } else { |
| 895 | BSSID = match.group(1); |
| 896 | try { |
| 897 | networkId = Integer.parseInt(match.group(2)); |
| 898 | } catch (NumberFormatException e) { |
| 899 | networkId = -1; |
| 900 | } |
| 901 | } |
| 902 | notifyNetworkStateChange(newState, BSSID, networkId); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Send the state machine a notification that the state of Wifi connectivity |
| 908 | * has changed. |
| 909 | * @param newState the new network state |
| 910 | * @param BSSID when the new state is {@link NetworkInfo.DetailedState#CONNECTED}, |
| 911 | * this is the MAC address of the access point. Otherwise, it |
| 912 | * is {@code null}. |
| 913 | * @param netId the configured network on which the state change occurred |
| 914 | */ |
| 915 | void notifyNetworkStateChange(NetworkInfo.DetailedState newState, String BSSID, int netId) { |
| 916 | if (newState == NetworkInfo.DetailedState.CONNECTED) { |
| 917 | Message m = mStateMachine.obtainMessage(NETWORK_CONNECTION_EVENT, |
| 918 | netId, 0, BSSID); |
| 919 | mStateMachine.sendMessage(m); |
| 920 | } else { |
| 921 | Message m = mStateMachine.obtainMessage(NETWORK_DISCONNECTION_EVENT, |
| 922 | netId, 0, BSSID); |
| 923 | mStateMachine.sendMessage(m); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Send the state machine a notification that the state of the supplicant |
| 929 | * has changed. |
| 930 | * @param networkId the configured network on which the state change occurred |
| 931 | * @param wifiSsid network name |
| 932 | * @param BSSID network address |
| 933 | * @param newState the new {@code SupplicantState} |
| 934 | */ |
| 935 | void notifySupplicantStateChange(int networkId, WifiSsid wifiSsid, String BSSID, |
| 936 | SupplicantState newState) { |
| 937 | mStateMachine.sendMessage(mStateMachine.obtainMessage(SUPPLICANT_STATE_CHANGE_EVENT, |
| 938 | new StateChangeResult(networkId, wifiSsid, BSSID, newState))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | } |
| 940 | } |