import android.net.wifi.NetworkUpdateResult;
import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
import android.os.Environment;
+import android.os.FileObserver;
import android.os.Message;
import android.os.Handler;
import android.os.HandlerThread;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
+import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
private static final String TAG = "WifiConfigStore";
private static final boolean DBG = true;
+ private static final String SUPPLICANT_CONFIG_FILE = "/data/misc/wifi/wpa_supplicant.conf";
+
/* configured networks with network id as the key */
private HashMap<Integer, WifiConfiguration> mConfiguredNetworks =
new HashMap<Integer, WifiConfiguration>();
private static final String EOS = "eos";
private final LocalLog mLocalLog;
+ WpaConfigFileObserver mFileObserver;
private WifiNative mWifiNative;
private final KeyStore mKeyStore = KeyStore.getInstance();
mWifiNative = wn;
if (DBG) {
- mLocalLog = new LocalLog(1024); // takes about 64 K
- mWifiNative.setLocalLog(mLocalLog);
+ mLocalLog = mWifiNative.getLocalLog();
+ mFileObserver = new WpaConfigFileObserver();
+ mFileObserver.startWatching();
+ }
+ }
+
+ class WpaConfigFileObserver extends FileObserver {
+
+ public WpaConfigFileObserver() {
+ super(SUPPLICANT_CONFIG_FILE, CLOSE_WRITE);
+ }
+
+ @Override
+ public void onEvent(int event, String path) {
+ if (event == CLOSE_WRITE) {
+ File file = new File(SUPPLICANT_CONFIG_FILE);
+ localLog("wpa_supplicant.conf changed; new size = " + file.length());
+ }
}
}
+
/**
* Fetch the list of configured networks
* and enable all stored networks in supplicant.
if (result.getNetworkId() != WifiConfiguration.INVALID_NETWORK_ID) {
sendConfiguredNetworksChangedBroadcast(mConfiguredNetworks.get(result.getNetworkId()),
result.isNewNetwork ? WifiManager.CHANGE_REASON_ADDED :
- WifiManager.CHANGE_REASON_CONFIG_CHANGE);
+ WifiManager.CHANGE_REASON_CONFIG_CHANGE);
}
return result.getNetworkId();
}
try {
config.networkId = Integer.parseInt(result[0]);
} catch(NumberFormatException e) {
+ loge("Failed to read network-id '" + result[0] + "'");
continue;
}
if (result.length > 3) {
sendConfiguredNetworksChangedBroadcast();
localLog("loadConfiguredNetworks loaded " + mNetworkIds.size() + " networks");
+
+ if (mNetworkIds.size() == 0) {
+ // no networks? Lets log if the wpa_supplicant.conf file contents
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(SUPPLICANT_CONFIG_FILE));
+ localLog("--- Begin wpa_supplicant.conf Contents ---");
+ for (String line = reader.readLine(); line != null; line = reader.readLine()) {
+ localLog(line);
+ }
+ localLog("--- End wpa_supplicant.conf Contents ---");
+ } catch (FileNotFoundException e) {
+ localLog("Could not open " + SUPPLICANT_CONFIG_FILE + ", " + e);
+ } catch (IOException e) {
+ localLog("Could not read " + SUPPLICANT_CONFIG_FILE + ", " + e);
+ } finally {
+ try {
+ if (reader != null) {
+ reader.close();
+ }
+ } catch (IOException e) {
+ // Just ignore the fact that we couldn't close
+ }
+ }
+ }
}
/* Mark all networks except specified netId as disabled */
}
- private LocalLog mLocalLog;
+ private static final LocalLog mLocalLog = new LocalLog(1024);
+ private int mCmdId;
- public void setLocalLog(LocalLog l) {
- mLocalLog = l;
+ public LocalLog getLocalLog() {
+ return mLocalLog;
+ }
+
+ private int getNewCmdId() {
+ synchronized (mLocalLog) {
+ return mCmdId++;
+ }
}
private void localLog(String s) {
}
public boolean connectToSupplicant() {
+ localLog(mInterfacePrefix + "connectToSupplicant");
return connectToSupplicantNative();
}
public void closeSupplicantConnection() {
+ localLog(mInterfacePrefix + "closeSupplicantConnection");
closeSupplicantConnectionNative();
}
private boolean doBooleanCommand(String command) {
if (DBG) Log.d(mTAG, "doBoolean: " + command);
- return doBooleanCommandNative(mInterfacePrefix + command);
+ int cmdId = getNewCmdId();
+ localLog(cmdId + "->" + mInterfacePrefix + command);
+ boolean result = doBooleanCommandNative(mInterfacePrefix + command);
+ localLog(cmdId + "<-" + result);
+ return result;
}
private int doIntCommand(String command) {
if (DBG) Log.d(mTAG, "doInt: " + command);
- return doIntCommandNative(mInterfacePrefix + command);
+ int cmdId = getNewCmdId();
+ localLog(cmdId + "->" + mInterfacePrefix + command);
+ int result = doIntCommandNative(mInterfacePrefix + command);
+ localLog(cmdId + "<-" + result);
+ return result;
}
private String doStringCommand(String command) {
if (DBG) Log.d(mTAG, "doString: " + command);
+ int cmdId = getNewCmdId();
+ localLog(cmdId + "->" + mInterfacePrefix + command);
+ String result = doStringCommandNative(mInterfacePrefix + command);
+ localLog(cmdId + "<-" + result);
+ return result;
+ }
+
+ private String doStringCommandWithoutLogging(String command) {
+ if (DBG) Log.d(mTAG, "doString: " + command);
return doStringCommandNative(mInterfacePrefix + command);
}
}
public String listNetworks() {
- localLog("LIST_NETWORKS");
return doStringCommand("LIST_NETWORKS");
}
public int addNetwork() {
- localLog("ADD_NETWORK");
return doIntCommand("ADD_NETWORK");
}
public boolean setNetworkVariable(int netId, String name, String value) {
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
- localLog("SET_NETWORK " + netId + " " + name + "=" + value);
return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
}
public String getNetworkVariable(int netId, String name) {
if (TextUtils.isEmpty(name)) return null;
- return doStringCommand("GET_NETWORK " + netId + " " + name);
+
+ // GET_NETWORK will likely flood the logs ...
+ return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
}
public boolean removeNetwork(int netId) {
- localLog("REMOVE_NETWORK " + netId);
return doBooleanCommand("REMOVE_NETWORK " + netId);
}
public boolean enableNetwork(int netId, boolean disableOthers) {
if (disableOthers) {
- localLog("SELECT_NETWORK " + netId);
return doBooleanCommand("SELECT_NETWORK " + netId);
} else {
- localLog("ENABLE_NETWORK " + netId);
return doBooleanCommand("ENABLE_NETWORK " + netId);
}
}
public boolean disableNetwork(int netId) {
- localLog("DISABLE_NETWORK " + netId);
return doBooleanCommand("DISABLE_NETWORK " + netId);
}
public boolean reconnect() {
- localLog("RECONNECT");
return doBooleanCommand("RECONNECT");
}
* MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
*/
public String scanResults(int sid) {
- return doStringCommand("BSS RANGE=" + sid + "- MASK=0x21987");
+ return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x21987");
}
/**
}
public boolean saveConfig() {
- localLog("SAVE_CONFIG");
return doBooleanCommand("SAVE_CONFIG");
}
* FREQUENCY=0
*/
public String signalPoll() {
- return doStringCommand("SIGNAL_POLL");
+ return doStringCommandWithoutLogging("SIGNAL_POLL");
}
/** Example outout: