diff options
author | Irfan Sheriff <isheriff@google.com> | 2011-05-19 14:57:54 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2011-05-19 14:57:54 -0700 |
commit | a4815c0ec06c293ea9f50b2b925d5a98d11f1143 (patch) | |
tree | 4d9fd7728d48b91cfb9c179c0d7d5ebd5e72710b /wifi/java/android | |
parent | f0c6ddd99b6770ee4de74ce42134f9f56b8ebc5d (diff) | |
parent | 9c9088a54f6dee7a13dcc7a12297137147b01baa (diff) | |
download | frameworks_base-a4815c0ec06c293ea9f50b2b925d5a98d11f1143.zip frameworks_base-a4815c0ec06c293ea9f50b2b925d5a98d11f1143.tar.gz frameworks_base-a4815c0ec06c293ea9f50b2b925d5a98d11f1143.tar.bz2 |
am 9c9088a5: Merge "Move softap config handling to WifiConfigStore" into honeycomb-mr2
* commit '9c9088a54f6dee7a13dcc7a12297137147b01baa':
Move softap config handling to WifiConfigStore
Diffstat (limited to 'wifi/java/android')
-rw-r--r-- | wifi/java/android/net/wifi/IWifiManager.aidl | 2 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/WifiApConfigStore.java | 181 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/WifiManager.java | 3 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/WifiStateMachine.java | 102 |
4 files changed, 251 insertions, 37 deletions
diff --git a/wifi/java/android/net/wifi/IWifiManager.aidl b/wifi/java/android/net/wifi/IWifiManager.aidl index 16a61db..0757efd 100644 --- a/wifi/java/android/net/wifi/IWifiManager.aidl +++ b/wifi/java/android/net/wifi/IWifiManager.aidl @@ -87,7 +87,7 @@ interface IWifiManager void releaseMulticastLock(); - boolean setWifiApEnabled(in WifiConfiguration wifiConfig, boolean enable); + void setWifiApEnabled(in WifiConfiguration wifiConfig, boolean enable); int getWifiApEnabledState(); diff --git a/wifi/java/android/net/wifi/WifiApConfigStore.java b/wifi/java/android/net/wifi/WifiApConfigStore.java new file mode 100644 index 0000000..bb5427d --- /dev/null +++ b/wifi/java/android/net/wifi/WifiApConfigStore.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net.wifi; + +import android.content.Context; +import android.net.wifi.WifiConfiguration.KeyMgmt; +import android.os.Environment; +import android.os.Message; +import android.os.Handler; +import android.os.HandlerThread; +import android.util.Log; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.util.UUID; + +import com.android.internal.R; + + +/** + * Provides API to the WifiStateMachine for doing read/write access + * to soft access point configuration + */ +class WifiApConfigStore { + + private static Context sContext; + private static final String TAG = "WifiApConfigStore"; + + private static final String AP_CONFIG_FILE = Environment.getDataDirectory() + + "/misc/wifi/softap.conf"; + + private static final int AP_CONFIG_FILE_VERSION = 1; + + private static WifiConfiguration sApConfig = new WifiConfiguration(); + private static final Object sApConfigLock = new Object(); + + private static FileReadWriteHandler sFileReadWriteHandler; + private static final int READ_AP_CONFIG = 1; + private static final int WRITE_AP_CONFIG = 2; + + static void initialize(Context context) { + sContext = context; + + /* File operations happen on a seperate thread */ + HandlerThread configThread = new HandlerThread("WifiApConfigStore"); + configThread.start(); + sFileReadWriteHandler = new FileReadWriteHandler(configThread.getLooper()); + Message.obtain(sFileReadWriteHandler, READ_AP_CONFIG).sendToTarget(); + } + + + static void setApConfiguration(WifiConfiguration config) { + synchronized (sApConfigLock) { + sApConfig = config; + } + Message.obtain(sFileReadWriteHandler, WRITE_AP_CONFIG, new WifiConfiguration(config)) + .sendToTarget(); + } + + static WifiConfiguration getApConfiguration() { + synchronized (sApConfigLock) { + return new WifiConfiguration(sApConfig); + } + } + + /** + * File read/write handler + */ + private static class FileReadWriteHandler extends Handler { + + public FileReadWriteHandler(android.os.Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case WRITE_AP_CONFIG: + writeApConfiguration((WifiConfiguration) msg.obj); + break; + case READ_AP_CONFIG: + readApConfiguration(); + break; + default: + Log.e(TAG, "Unknown command in FileReadWriteHandler: " + msg); + break; + } + } + + private static void writeApConfiguration(final WifiConfiguration config) { + DataOutputStream out = null; + try { + out = new DataOutputStream(new BufferedOutputStream( + new FileOutputStream(AP_CONFIG_FILE))); + + out.writeInt(AP_CONFIG_FILE_VERSION); + out.writeUTF(config.SSID); + int authType = config.getAuthType(); + out.writeInt(authType); + if(authType != KeyMgmt.NONE) { + out.writeUTF(config.preSharedKey); + } + } catch (IOException e) { + Log.e(TAG, "Error writing hotspot configuration" + e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) {} + } + } + } + + private static void readApConfiguration() { + DataInputStream in = null; + try { + WifiConfiguration config = new WifiConfiguration(); + in = new DataInputStream(new BufferedInputStream(new FileInputStream( + AP_CONFIG_FILE))); + + int version = in.readInt(); + if (version != 1) { + Log.e(TAG, "Bad version on hotspot configuration file, set defaults"); + setDefaultApConfiguration(); + return; + } + config.SSID = in.readUTF(); + int authType = in.readInt(); + config.allowedKeyManagement.set(authType); + if (authType != KeyMgmt.NONE) { + config.preSharedKey = in.readUTF(); + } + synchronized (sApConfigLock) { + sApConfig = config; + } + } catch (IOException ignore) { + setDefaultApConfiguration(); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) {} + } + } + } + + /* Generate a default WPA2 based configuration with a random password. + We are changing the Wifi Ap configuration storage from secure settings to a + flat file accessible only by the system. A WPA2 based default configuration + will keep the device secure after the update */ + private static void setDefaultApConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.SSID = sContext.getString(R.string.wifi_tether_configure_ssid_default); + config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); + String randomUUID = UUID.randomUUID().toString(); + //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx + config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9,13); + setApConfiguration(config); + } + } +} diff --git a/wifi/java/android/net/wifi/WifiManager.java b/wifi/java/android/net/wifi/WifiManager.java index 2e49a77..949e80f 100644 --- a/wifi/java/android/net/wifi/WifiManager.java +++ b/wifi/java/android/net/wifi/WifiManager.java @@ -898,7 +898,8 @@ public class WifiManager { */ public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { try { - return mService.setWifiApEnabled(wifiConfig, enabled); + mService.setWifiApEnabled(wifiConfig, enabled); + return true; } catch (RemoteException e) { return false; } diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java index a6b1a2c..f2211d2 100644 --- a/wifi/java/android/net/wifi/WifiStateMachine.java +++ b/wifi/java/android/net/wifi/WifiStateMachine.java @@ -200,8 +200,12 @@ public class WifiStateMachine extends StateMachine { static final int CMD_START_AP = BASE + 21; /* Stop the soft access point */ static final int CMD_STOP_AP = BASE + 22; + /* Set the soft access point configuration */ + static final int CMD_SET_AP_CONFIG = BASE + 23; + /* Get the soft access point configuration */ + static final int CMD_GET_AP_CONFIG = BASE + 24; - static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE = BASE + 23; + static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE = BASE + 25; /* Supplicant events */ /* Connection to supplicant established */ @@ -355,6 +359,10 @@ public class WifiStateMachine extends StateMachine { private static final int MIN_RSSI = -200; private static final int MAX_RSSI = 256; + /* Constants to indicate if soft ap is running or stopped */ + private static final int SOFT_AP_STOPPED = 0; + private static final int SOFT_AP_RUNNING = 1; + /* Default parent state */ private State mDefaultState = new DefaultState(); /* Temporary initial state */ @@ -593,6 +601,17 @@ public class WifiStateMachine extends StateMachine { } } + public void setWifiApConfiguration(WifiConfiguration config) { + sendMessage(obtainMessage(CMD_SET_AP_CONFIG, config)); + } + + public WifiConfiguration syncGetWifiApConfiguration(AsyncChannel channel) { + Message resultMsg = channel.sendMessageSynchronously(CMD_GET_AP_CONFIG); + WifiConfiguration ret = (WifiConfiguration) resultMsg.obj; + resultMsg.recycle(); + return ret; + } + /** * TODO: doc */ @@ -1517,7 +1536,31 @@ public class WifiStateMachine extends StateMachine { */ WifiNative.disconnectCommand(); WifiNative.reconnectCommand(); + } + private boolean startSoftApWithConfig(WifiConfiguration config, int currentStatus) { + if (config == null) { + config = WifiApConfigStore.getApConfiguration(); + } else { + WifiApConfigStore.setApConfiguration(config); + } + try { + if (currentStatus == SOFT_AP_STOPPED) { + nwService.startAccessPoint(config, mInterfaceName, SOFTAP_IFACE); + } else if (currentStatus == SOFT_AP_RUNNING) { + nwService.setAccessPoint(config, mInterfaceName, SOFTAP_IFACE); + } + } catch (Exception e) { + Log.e(TAG, "Exception in softap start " + e); + try { + nwService.stopAccessPoint(); + nwService.startAccessPoint(config, mInterfaceName, SOFTAP_IFACE); + } catch (Exception ee) { + Log.e(TAG, "Exception during softap restart : " + ee); + return false; + } + } + return true; } @@ -1655,6 +1698,13 @@ public class WifiStateMachine extends StateMachine { case CMD_ENABLE_BACKGROUND_SCAN: mEnableBackgroundScan = (message.arg1 == 1); break; + case CMD_SET_AP_CONFIG: + WifiApConfigStore.setApConfiguration((WifiConfiguration) message.obj); + break; + case CMD_GET_AP_CONFIG: + WifiConfiguration config = WifiApConfigStore.getApConfiguration(); + mReplyChannel.replyToMessage(message, message.what, config); + break; /* Discard */ case CMD_LOAD_DRIVER: case CMD_UNLOAD_DRIVER: @@ -1717,6 +1767,8 @@ public class WifiStateMachine extends StateMachine { // 50021 wifi_state_changed (custom|1|5) EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName()); + WifiApConfigStore.initialize(mContext); + if (WifiNative.isDriverLoaded()) { transitionTo(mDriverLoadedState); } @@ -1829,26 +1881,14 @@ public class WifiStateMachine extends StateMachine { } break; case CMD_START_AP: - try { - nwService.startAccessPoint((WifiConfiguration) message.obj, - mInterfaceName, - SOFTAP_IFACE); - } catch (Exception e) { - Log.e(TAG, "Exception in softap start " + e); - try { - nwService.stopAccessPoint(); - nwService.startAccessPoint((WifiConfiguration) message.obj, - mInterfaceName, - SOFTAP_IFACE); - } catch (Exception ee) { - Log.e(TAG, "Exception during softap restart : " + ee); - sendMessage(obtainMessage(CMD_UNLOAD_DRIVER, WIFI_AP_STATE_FAILED, 0)); - break; - } + if (startSoftApWithConfig((WifiConfiguration) message.obj, SOFT_AP_STOPPED)) { + Log.d(TAG, "Soft AP start successful"); + setWifiApState(WIFI_AP_STATE_ENABLED); + transitionTo(mSoftApStartedState); + } else { + Log.d(TAG, "Soft AP start failed"); + sendMessage(obtainMessage(CMD_UNLOAD_DRIVER, WIFI_AP_STATE_FAILED, 0)); } - Log.d(TAG, "Soft AP start successful"); - setWifiApState(WIFI_AP_STATE_ENABLED); - transitionTo(mSoftApStartedState); break; default: return NOT_HANDLED; @@ -2999,21 +3039,13 @@ public class WifiStateMachine extends StateMachine { break; case CMD_START_AP: Log.d(TAG,"SoftAP set on a running access point"); - try { - nwService.setAccessPoint((WifiConfiguration) message.obj, - mInterfaceName, - SOFTAP_IFACE); - } catch(Exception e) { - Log.e(TAG, "Exception in softap set " + e); - try { - nwService.stopAccessPoint(); - nwService.startAccessPoint((WifiConfiguration) message.obj, - mInterfaceName, - SOFTAP_IFACE); - } catch (Exception ee) { - Log.e(TAG, "Could not restart softap after set failed " + ee); - sendMessage(obtainMessage(CMD_UNLOAD_DRIVER, WIFI_AP_STATE_FAILED, 0)); - } + if (startSoftApWithConfig((WifiConfiguration) message.obj, SOFT_AP_RUNNING)) { + Log.d(TAG, "Soft AP start successful"); + setWifiApState(WIFI_AP_STATE_ENABLED); + transitionTo(mSoftApStartedState); + } else { + Log.d(TAG, "Soft AP start failed"); + sendMessage(obtainMessage(CMD_UNLOAD_DRIVER, WIFI_AP_STATE_FAILED, 0)); } break; /* Fail client mode operation when soft AP is enabled */ |