Skip to content

Commit 8e8798d

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Fix connect & save of invalid networks" into jb-dev
2 parents 2d589ff + b3e96c5 commit 8e8798d

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

wifi/java/android/net/wifi/WifiConfigStore.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,6 @@ void enableAllNetworks() {
194194
}
195195
}
196196

197-
/**
198-
* Selects the specified network config for connection. This involves
199-
* addition/update of the specified config, updating the priority of
200-
* all the networks and enabling the given network while disabling others.
201-
*
202-
* Selecting a network will leave the other networks disabled and
203-
* a call to enableAllNetworks() needs to be issued upon a connection
204-
* or a failure event from supplicant
205-
*
206-
* @param config The configuration details in WifiConfiguration
207-
* @return the networkId now associated with the specified configuration
208-
*/
209-
int selectNetwork(WifiConfiguration config) {
210-
if (config != null) {
211-
NetworkUpdateResult result = addOrUpdateNetworkNative(config);
212-
int netId = result.getNetworkId();
213-
if (netId != INVALID_NETWORK_ID) {
214-
selectNetwork(netId);
215-
} else {
216-
loge("Failed to update network " + config);
217-
}
218-
return netId;
219-
}
220-
return INVALID_NETWORK_ID;
221-
}
222197

223198
/**
224199
* Selects the specified network for connection. This involves
@@ -230,8 +205,11 @@ int selectNetwork(WifiConfiguration config) {
230205
* or a failure event from supplicant
231206
*
232207
* @param netId network to select for connection
208+
* @return false if the network id is invalid
233209
*/
234-
void selectNetwork(int netId) {
210+
boolean selectNetwork(int netId) {
211+
if (netId == INVALID_NETWORK_ID) return false;
212+
235213
// Reset the priority of each network at start or if it goes too high.
236214
if (mLastPriority == -1 || mLastPriority > 1000000) {
237215
for(WifiConfiguration config : mConfiguredNetworks.values()) {
@@ -256,6 +234,7 @@ void selectNetwork(int netId) {
256234

257235
/* Avoid saving the config & sending a broadcast to prevent settings
258236
* from displaying a disabled list of networks */
237+
return true;
259238
}
260239

261240
/**
@@ -265,6 +244,12 @@ void selectNetwork(int netId) {
265244
* @return network update result
266245
*/
267246
NetworkUpdateResult saveNetwork(WifiConfiguration config) {
247+
// A new network cannot have null SSID
248+
if (config == null || (config.networkId == INVALID_NETWORK_ID &&
249+
config.SSID == null)) {
250+
return new NetworkUpdateResult(INVALID_NETWORK_ID);
251+
}
252+
268253
boolean newNetwork = (config.networkId == INVALID_NETWORK_ID);
269254
NetworkUpdateResult result = addOrUpdateNetworkNative(config);
270255
int netId = result.getNetworkId();

wifi/java/android/net/wifi/WifiStateMachine.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,35 +2849,33 @@ public boolean processMessage(Message message) {
28492849
mWifiNative.reassociate();
28502850
break;
28512851
case WifiManager.CONNECT_NETWORK:
2852+
/* The connect message can contain a network id passed as arg1 on message or
2853+
* or a config passed as obj on message.
2854+
* For a new network, a config is passed to create and connect.
2855+
* For an existing network, a network id is passed
2856+
*/
28522857
int netId = message.arg1;
28532858
WifiConfiguration config = (WifiConfiguration) message.obj;
28542859

2855-
/* We connect to a specific network by issuing a select
2856-
* to the WifiConfigStore. This enables the network,
2857-
* while disabling all other networks in the supplicant.
2858-
* Disabling a connected network will cause a disconnection
2859-
* from the network. A reconnectCommand() will then initiate
2860-
* a connection to the enabled network.
2861-
*/
2860+
/* Save the network config */
28622861
if (config != null) {
2863-
netId = mWifiConfigStore.selectNetwork(config);
2864-
} else {
2865-
mWifiConfigStore.selectNetwork(netId);
2862+
NetworkUpdateResult result = mWifiConfigStore.saveNetwork(config);
2863+
netId = result.getNetworkId();
28662864
}
28672865

2868-
/* The state tracker handles enabling networks upon completion/failure */
2869-
mSupplicantStateTracker.sendMessage(WifiManager.CONNECT_NETWORK);
2870-
2871-
if (mWifiNative.reconnect()) {
2866+
if (mWifiConfigStore.selectNetwork(netId) &&
2867+
mWifiNative.reconnect()) {
2868+
/* The state tracker handles enabling networks upon completion/failure */
2869+
mSupplicantStateTracker.sendMessage(WifiManager.CONNECT_NETWORK);
28722870
replyToMessage(message, WifiManager.CONNECT_NETWORK_SUCCEEDED);
2871+
/* Expect a disconnection from the old connection */
2872+
transitionTo(mDisconnectingState);
28732873
} else {
2874-
loge("Failed to initiate connection");
2874+
loge("Failed to connect config: " + config + " netId: " + netId);
28752875
replyToMessage(message, WifiManager.CONNECT_NETWORK_FAILED,
28762876
WifiManager.ERROR);
2877+
break;
28772878
}
2878-
2879-
/* Expect a disconnection from the old connection */
2880-
transitionTo(mDisconnectingState);
28812879
break;
28822880
case WifiManager.START_WPS:
28832881
WpsInfo wpsInfo = (WpsInfo) message.obj;

0 commit comments

Comments
 (0)