2012-04-09 07:44:53 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2012-04-09 07:47:42 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-04-19 21:33:36 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-07-29 09:59:53 +00:00
|
|
|
Cu.import("resource://gre/modules/systemlibs.js");
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
var DEBUG = false; // set to true to show debug messages.
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
const WIFIWORKER_CONTRACTID = "@mozilla.org/wifi/worker;1";
|
|
|
|
const WIFIWORKER_CID = Components.ID("{a14e8977-d259-433a-a88d-58dd44657e5b}");
|
|
|
|
|
|
|
|
const WIFIWORKER_WORKER = "resource://gre/modules/wifi_worker.js";
|
|
|
|
|
2012-04-19 21:33:36 +00:00
|
|
|
const kNetworkInterfaceStateChangedTopic = "network-interface-state-changed";
|
2012-09-04 04:26:56 +00:00
|
|
|
const kMozSettingsChangedObserverTopic = "mozsettings-changed";
|
2012-04-19 21:33:36 +00:00
|
|
|
|
2012-08-28 19:14:26 +00:00
|
|
|
const MAX_RETRIES_ON_AUTHENTICATION_FAILURE = 2;
|
|
|
|
const MAX_SUPPLICANT_LOOP_ITERATIONS = 4;
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
// Settings DB path for wifi
|
|
|
|
const SETTINGS_WIFI_ENABLED = "wifi.enabled";
|
|
|
|
const SETTINGS_WIFI_DEBUG_ENABLED = "wifi.debugging.enabled";
|
|
|
|
// Settings DB path for Wifi tethering.
|
|
|
|
const SETTINGS_WIFI_TETHERING_ENABLED = "tethering.wifi.enabled";
|
|
|
|
const SETTINGS_WIFI_SSID = "tethering.wifi.ssid";
|
|
|
|
const SETTINGS_WIFI_SECURITY_TYPE = "tethering.wifi.security.type";
|
|
|
|
const SETTINGS_WIFI_SECURITY_PASSWORD = "tethering.wifi.security.password";
|
|
|
|
const SETTINGS_WIFI_IP = "tethering.wifi.ip";
|
|
|
|
const SETTINGS_WIFI_PREFIX = "tethering.wifi.prefix";
|
|
|
|
const SETTINGS_WIFI_DHCPSERVER_STARTIP = "tethering.wifi.dhcpserver.startip";
|
|
|
|
const SETTINGS_WIFI_DHCPSERVER_ENDIP = "tethering.wifi.dhcpserver.endip";
|
|
|
|
const SETTINGS_WIFI_DNS1 = "tethering.wifi.dns1";
|
|
|
|
const SETTINGS_WIFI_DNS2 = "tethering.wifi.dns2";
|
|
|
|
|
2013-06-19 10:57:58 +00:00
|
|
|
// Settings DB path for USB tethering.
|
|
|
|
const SETTINGS_USB_DHCPSERVER_STARTIP = "tethering.usb.dhcpserver.startip";
|
|
|
|
const SETTINGS_USB_DHCPSERVER_ENDIP = "tethering.usb.dhcpserver.endip";
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
// Default value for WIFI tethering.
|
|
|
|
const DEFAULT_WIFI_IP = "192.168.1.1";
|
|
|
|
const DEFAULT_WIFI_PREFIX = "24";
|
|
|
|
const DEFAULT_WIFI_DHCPSERVER_STARTIP = "192.168.1.10";
|
|
|
|
const DEFAULT_WIFI_DHCPSERVER_ENDIP = "192.168.1.30";
|
|
|
|
const DEFAULT_WIFI_SSID = "FirefoxHotspot";
|
|
|
|
const DEFAULT_WIFI_SECURITY_TYPE = "open";
|
|
|
|
const DEFAULT_WIFI_SECURITY_PASSWORD = "1234567890";
|
|
|
|
const DEFAULT_DNS1 = "8.8.8.8";
|
|
|
|
const DEFAULT_DNS2 = "8.8.4.4";
|
|
|
|
|
2013-06-19 10:57:58 +00:00
|
|
|
// Default value for USB tethering.
|
|
|
|
const DEFAULT_USB_DHCPSERVER_STARTIP = "192.168.0.10";
|
|
|
|
const DEFAULT_USB_DHCPSERVER_ENDIP = "192.168.0.30";
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
const WIFI_FIRMWARE_AP = "AP";
|
|
|
|
const WIFI_FIRMWARE_STATION = "STA";
|
|
|
|
const WIFI_SECURITY_TYPE_NONE = "open";
|
|
|
|
const WIFI_SECURITY_TYPE_WPA_PSK = "wpa-psk";
|
|
|
|
const WIFI_SECURITY_TYPE_WPA2_PSK = "wpa2-psk";
|
|
|
|
|
|
|
|
const NETWORK_INTERFACE_UP = "up";
|
|
|
|
const NETWORK_INTERFACE_DOWN = "down";
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
const DEFAULT_WLAN_INTERFACE = "wlan0";
|
|
|
|
|
|
|
|
const DRIVER_READY_WAIT = 2000;
|
|
|
|
|
|
|
|
const SUPP_PROP = "init.svc.wpa_supplicant";
|
|
|
|
const WPA_SUPPLICANT = "wpa_supplicant";
|
|
|
|
const DHCP_PROP = "init.svc.dhcpcd";
|
|
|
|
const DHCP = "dhcpcd";
|
|
|
|
|
2012-04-19 21:33:36 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gNetworkManager",
|
|
|
|
"@mozilla.org/network/manager;1",
|
|
|
|
"nsINetworkManager");
|
|
|
|
|
2012-09-04 04:26:56 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gSettingsService",
|
|
|
|
"@mozilla.org/settingsService;1",
|
|
|
|
"nsISettingsService");
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// A note about errors and error handling in this file:
|
|
|
|
// The libraries that we use in this file are intended for C code. For
|
|
|
|
// C code, it is natural to return -1 for errors and 0 for success.
|
|
|
|
// Therefore, the code that interacts directly with the worker uses this
|
|
|
|
// convention (note: command functions do get boolean results since the
|
|
|
|
// command always succeeds and we do a string/boolean check for the
|
|
|
|
// expected results).
|
|
|
|
var WifiManager = (function() {
|
2012-11-22 10:29:24 +00:00
|
|
|
function getStartupPrefs() {
|
|
|
|
return {
|
|
|
|
sdkVersion: parseInt(libcutils.property_get("ro.build.version.sdk"), 10),
|
2013-04-11 03:52:48 +00:00
|
|
|
unloadDriverEnabled: libcutils.property_get("ro.moz.wifi.unloaddriver") === "1",
|
2013-07-29 09:59:53 +00:00
|
|
|
schedScanRecovery: libcutils.property_get("ro.moz.wifi.sched_scan_recover") === "false" ? false : true,
|
|
|
|
driverDelay: libcutils.property_get("ro.moz.wifi.driverDelay"),
|
|
|
|
ifname: libcutils.property_get("wifi.interface")
|
2012-11-22 10:29:24 +00:00
|
|
|
};
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
let {sdkVersion, unloadDriverEnabled, schedScanRecovery, driverDelay, ifname} = getStartupPrefs();
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2013-08-27 20:54:51 +00:00
|
|
|
let wifiListener = {
|
|
|
|
onWaitEvent: function(event) {
|
|
|
|
if (handleEvent(event)) {
|
|
|
|
waitForEvent();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onCommand: function(event) {
|
|
|
|
onmessageresult(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let wifiService = Cc["@mozilla.org/wifi/service;1"];
|
|
|
|
if (wifiService) {
|
|
|
|
wifiService = wifiService.getService(Ci.nsIWifiProxyService);
|
|
|
|
wifiService.start(wifiListener);
|
|
|
|
} else {
|
|
|
|
debug("No wifi service component available!");
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
var manager = {};
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.ifname = ifname;
|
|
|
|
// Emulator build runs to here.
|
|
|
|
// The debug() should only be used after WifiManager.
|
|
|
|
if (!ifname) {
|
|
|
|
manager.ifname = DEFAULT_WLAN_INTERFACE;
|
|
|
|
}
|
2012-11-22 10:29:24 +00:00
|
|
|
manager.schedScanRecovery = schedScanRecovery;
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.driverDelay = driverDelay ? parseInt(driverDelay, 10) : DRIVER_READY_WAIT;
|
2012-11-05 22:29:07 +00:00
|
|
|
|
2013-08-27 20:54:51 +00:00
|
|
|
// Callbacks to invoke when a reply arrives from the wifi service.
|
2012-04-09 07:44:53 +00:00
|
|
|
var controlCallbacks = Object.create(null);
|
|
|
|
var idgen = 0;
|
|
|
|
|
|
|
|
function controlMessage(obj, callback) {
|
|
|
|
var id = idgen++;
|
|
|
|
obj.id = id;
|
|
|
|
if (callback)
|
|
|
|
controlCallbacks[id] = callback;
|
2013-08-27 20:54:51 +00:00
|
|
|
wifiService.sendCommand(obj);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 20:54:51 +00:00
|
|
|
let onmessageresult = function(data) {
|
2012-04-09 07:44:53 +00:00
|
|
|
var id = data.id;
|
|
|
|
var callback = controlCallbacks[id];
|
|
|
|
if (callback) {
|
|
|
|
callback(data);
|
|
|
|
delete controlCallbacks[id];
|
|
|
|
}
|
2013-08-27 20:54:51 +00:00
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
// Polling the status worker
|
|
|
|
var recvErrors = 0;
|
|
|
|
|
|
|
|
function waitForEvent() {
|
2013-08-27 20:54:51 +00:00
|
|
|
wifiService.waitForEvent();
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// Commands to the control worker.
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
function voidControlMessage(cmd, callback) {
|
|
|
|
controlMessage({ cmd: cmd }, function (data) {
|
|
|
|
callback(data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-23 21:41:29 +00:00
|
|
|
var driverLoaded = false;
|
2013-08-15 02:58:45 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function loadDriver(callback) {
|
2012-07-23 21:41:29 +00:00
|
|
|
if (driverLoaded) {
|
|
|
|
callback(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
voidControlMessage("load_driver", function(status) {
|
|
|
|
driverLoaded = (status >= 0);
|
|
|
|
callback(status)
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function unloadDriver(callback) {
|
2013-04-11 03:52:48 +00:00
|
|
|
if (!unloadDriverEnabled) {
|
|
|
|
// Unloading drivers is generally unnecessary and
|
|
|
|
// can trigger bugs in some drivers.
|
|
|
|
// On properly written drivers, bringing the interface
|
|
|
|
// down powers down the interface.
|
|
|
|
callback(0);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-23 21:41:29 +00:00
|
|
|
|
|
|
|
voidControlMessage("unload_driver", function(status) {
|
|
|
|
driverLoaded = (status < 0);
|
|
|
|
callback(status);
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function startSupplicant(callback) {
|
|
|
|
voidControlMessage("start_supplicant", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function terminateSupplicant(callback) {
|
|
|
|
doBooleanCommand("TERMINATE", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopSupplicant(callback) {
|
|
|
|
voidControlMessage("stop_supplicant", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectToSupplicant(callback) {
|
|
|
|
voidControlMessage("connect_to_supplicant", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeSupplicantConnection(callback) {
|
|
|
|
voidControlMessage("close_supplicant_connection", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doCommand(request, callback) {
|
|
|
|
controlMessage({ cmd: "command", request: request }, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doIntCommand(request, callback) {
|
|
|
|
doCommand(request, function(data) {
|
|
|
|
callback(data.status ? -1 : (data.reply|0));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doBooleanCommand(request, expected, callback) {
|
|
|
|
doCommand(request, function(data) {
|
|
|
|
callback(data.status ? false : (data.reply == expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doStringCommand(request, callback) {
|
|
|
|
doCommand(request, function(data) {
|
|
|
|
callback(data.status ? null : data.reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function listNetworksCommand(callback) {
|
|
|
|
doStringCommand("LIST_NETWORKS", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNetworkCommand(callback) {
|
|
|
|
doIntCommand("ADD_NETWORK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNetworkVariableCommand(netId, name, value, callback) {
|
|
|
|
doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNetworkVariableCommand(netId, name, callback) {
|
|
|
|
doStringCommand("GET_NETWORK " + netId + " " + name, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeNetworkCommand(netId, callback) {
|
|
|
|
doBooleanCommand("REMOVE_NETWORK " + netId, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableNetworkCommand(netId, disableOthers, callback) {
|
|
|
|
doBooleanCommand((disableOthers ? "SELECT_NETWORK " : "ENABLE_NETWORK ") + netId, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableNetworkCommand(netId, callback) {
|
|
|
|
doBooleanCommand("DISABLE_NETWORK " + netId, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function statusCommand(callback) {
|
|
|
|
doStringCommand("STATUS", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function pingCommand(callback) {
|
|
|
|
doBooleanCommand("PING", "PONG", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function scanResultsCommand(callback) {
|
|
|
|
doStringCommand("SCAN_RESULTS", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function disconnectCommand(callback) {
|
|
|
|
doBooleanCommand("DISCONNECT", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reconnectCommand(callback) {
|
|
|
|
doBooleanCommand("RECONNECT", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reassociateCommand(callback) {
|
|
|
|
doBooleanCommand("REASSOCIATE", "OK", callback);
|
|
|
|
}
|
|
|
|
|
2012-10-25 23:27:10 +00:00
|
|
|
// A note about background scanning:
|
|
|
|
// Normally, background scanning shouldn't be necessary as wpa_supplicant
|
|
|
|
// has the capability to automatically schedule its own scans at appropriate
|
|
|
|
// intervals. However, with some drivers, this appears to get stuck after
|
|
|
|
// three scans, so we enable the driver's background scanning to work around
|
|
|
|
// that when we're not connected to any network. This ensures that we'll
|
|
|
|
// automatically reconnect to networks if one falls out of range.
|
|
|
|
var reEnableBackgroundScan = false;
|
2012-11-05 22:29:07 +00:00
|
|
|
|
|
|
|
// NB: This is part of the internal API.
|
|
|
|
manager.backgroundScanEnabled = false;
|
2012-10-25 23:27:10 +00:00
|
|
|
function setBackgroundScan(enable, callback) {
|
|
|
|
var doEnable = (enable === "ON");
|
2012-11-05 22:29:07 +00:00
|
|
|
if (doEnable === manager.backgroundScanEnabled) {
|
2012-10-25 23:27:10 +00:00
|
|
|
callback(false, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
manager.backgroundScanEnabled = doEnable;
|
|
|
|
doBooleanCommand("SET pno " + (manager.backgroundScanEnabled ? "1" : "0"),
|
|
|
|
"OK",
|
2012-10-25 23:27:10 +00:00
|
|
|
function(ok) {
|
|
|
|
callback(true, ok);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
var scanModeActive = false;
|
|
|
|
|
|
|
|
function doSetScanModeCommand(setActive, callback) {
|
|
|
|
doBooleanCommand(setActive ? "DRIVER SCAN-ACTIVE" : "DRIVER SCAN-PASSIVE", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function scanCommand(forceActive, callback) {
|
|
|
|
if (forceActive && !scanModeActive) {
|
2012-07-12 22:09:18 +00:00
|
|
|
// Note: we ignore errors from doSetScanMode.
|
|
|
|
doSetScanModeCommand(true, function(ignore) {
|
2012-10-25 23:27:10 +00:00
|
|
|
setBackgroundScan("OFF", function(turned, ignore) {
|
|
|
|
reEnableBackgroundScan = turned;
|
|
|
|
doBooleanCommand("SCAN", "OK", function(ok) {
|
|
|
|
doSetScanModeCommand(false, function(ignore) {
|
|
|
|
// The result of scanCommand is the result of the actual SCAN
|
|
|
|
// request.
|
|
|
|
callback(ok);
|
|
|
|
});
|
2012-07-12 22:09:18 +00:00
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
doBooleanCommand("SCAN", "OK", callback);
|
|
|
|
}
|
|
|
|
|
2012-10-31 00:35:34 +00:00
|
|
|
var debugEnabled = false;
|
|
|
|
function setLogLevel(level, callback) {
|
|
|
|
doBooleanCommand("LOG_LEVEL " + level, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function syncDebug() {
|
|
|
|
if (debugEnabled !== DEBUG) {
|
|
|
|
let wanted = DEBUG;
|
|
|
|
setLogLevel(wanted ? "DEBUG" : "INFO", function(ok) {
|
|
|
|
if (ok)
|
|
|
|
debugEnabled = wanted;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLogLevel(callback) {
|
|
|
|
doStringCommand("LOG_LEVEL", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDebugEnabled(callback) {
|
|
|
|
getLogLevel(function(level) {
|
|
|
|
if (level === null) {
|
|
|
|
debug("Unable to get wpa_supplicant's log level");
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lines = level.split("\n");
|
|
|
|
for (let i = 0; i < lines.length; ++i) {
|
|
|
|
let match = /Current level: (.*)/.exec(lines[i]);
|
|
|
|
if (match) {
|
|
|
|
debugEnabled = match[1].toLowerCase() === "debug";
|
|
|
|
callback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're here, we didn't get the current level.
|
|
|
|
callback(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function setScanModeCommand(setActive, callback) {
|
|
|
|
scanModeActive = setActive;
|
|
|
|
doSetScanModeCommand(setActive, callback);
|
|
|
|
}
|
|
|
|
|
2012-08-16 22:19:51 +00:00
|
|
|
function wpsPbcCommand(callback) {
|
|
|
|
doBooleanCommand("WPS_PBC", "OK", callback);
|
|
|
|
}
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
function wpsPinCommand(detail, callback) {
|
|
|
|
doStringCommand("WPS_PIN " +
|
|
|
|
(detail.bssid === undefined ? "any" : detail.bssid) +
|
|
|
|
(detail.pin === undefined ? "" : (" " + detail.pin)),
|
2012-08-22 19:41:31 +00:00
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|
2012-08-16 22:19:51 +00:00
|
|
|
function wpsCancelCommand(callback) {
|
|
|
|
doBooleanCommand("WPS_CANCEL", "OK", callback);
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function startDriverCommand(callback) {
|
|
|
|
doBooleanCommand("DRIVER START", "OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopDriverCommand(callback) {
|
|
|
|
doBooleanCommand("DRIVER STOP", "OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
function startPacketFiltering(callback) {
|
|
|
|
doBooleanCommand("DRIVER RXFILTER-ADD 0", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-ADD 1", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-ADD 3", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-START", "OK", callback)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopPacketFiltering(callback) {
|
|
|
|
doBooleanCommand("DRIVER RXFILTER-STOP", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-REMOVE 3", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-REMOVE 1", "OK", function(ok) {
|
|
|
|
ok && doBooleanCommand("DRIVER RXFILTER-REMOVE 0", "OK", callback)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doGetRssiCommand(cmd, callback) {
|
|
|
|
doCommand(cmd, function(data) {
|
|
|
|
var rssi = -200;
|
|
|
|
|
|
|
|
if (!data.status) {
|
|
|
|
// If we are associating, the reply is "OK".
|
|
|
|
var reply = data.reply;
|
|
|
|
if (reply != "OK") {
|
|
|
|
// Format is: <SSID> rssi XX". SSID can contain spaces.
|
|
|
|
var offset = reply.lastIndexOf("rssi ");
|
|
|
|
if (offset !== -1)
|
|
|
|
rssi = reply.substr(offset + 5) | 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(rssi);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRssiCommand(callback) {
|
|
|
|
doGetRssiCommand("DRIVER RSSI", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRssiApproxCommand(callback) {
|
|
|
|
doGetRssiCommand("DRIVER RSSI-APPROX", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLinkSpeedCommand(callback) {
|
|
|
|
doStringCommand("DRIVER LINKSPEED", function(reply) {
|
|
|
|
if (reply)
|
|
|
|
reply = reply.split(" ")[1] | 0; // Format: LinkSpeed XX
|
|
|
|
callback(reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-13 01:56:39 +00:00
|
|
|
function getConnectionInfoGB(callback) {
|
|
|
|
var rval = {};
|
|
|
|
getRssiApproxCommand(function(rssi) {
|
|
|
|
rval.rssi = rssi;
|
|
|
|
getLinkSpeedCommand(function(linkspeed) {
|
|
|
|
rval.linkspeed = linkspeed;
|
|
|
|
callback(rval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConnectionInfoICS(callback) {
|
|
|
|
doStringCommand("SIGNAL_POLL", function(reply) {
|
|
|
|
if (!reply) {
|
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rval = {};
|
|
|
|
var lines = reply.split("\n");
|
|
|
|
for (let i = 0; i < lines.length; ++i) {
|
|
|
|
let [key, value] = lines[i].split("=");
|
|
|
|
switch (key.toUpperCase()) {
|
|
|
|
case "RSSI":
|
|
|
|
rval.rssi = value | 0;
|
|
|
|
break;
|
|
|
|
case "LINKSPEED":
|
|
|
|
rval.linkspeed = value | 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(rval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function getMacAddressCommand(callback) {
|
|
|
|
doStringCommand("DRIVER MACADDR", function(reply) {
|
|
|
|
if (reply)
|
|
|
|
reply = reply.split(" ")[2]; // Format: Macaddr = XX.XX.XX.XX.XX.XX
|
|
|
|
callback(reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:27:58 +00:00
|
|
|
function setPowerModeCommandICS(mode, callback) {
|
2012-09-24 09:05:47 +00:00
|
|
|
doBooleanCommand("DRIVER POWERMODE " + (mode === "AUTO" ? 0 : 1), "OK", callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:27:58 +00:00
|
|
|
function setPowerModeCommandJB(mode, callback) {
|
|
|
|
doBooleanCommand("SET ps " + (mode === "AUTO" ? 1 : 0), "OK", callback);
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function getPowerModeCommand(callback) {
|
|
|
|
doStringCommand("DRIVER GETPOWER", function(reply) {
|
|
|
|
if (reply)
|
|
|
|
reply = (reply.split()[2]|0); // Format: powermode = XX
|
|
|
|
callback(reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNumAllowedChannelsCommand(numChannels, callback) {
|
|
|
|
doBooleanCommand("DRIVER SCAN-CHANNELS " + numChannels, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNumAllowedChannelsCommand(callback) {
|
|
|
|
doStringCommand("DRIVER SCAN-CHANNELS", function(reply) {
|
|
|
|
if (reply)
|
|
|
|
reply = (reply.split()[2]|0); // Format: Scan-Channels = X
|
|
|
|
callback(reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setBluetoothCoexistenceModeCommand(mode, callback) {
|
|
|
|
doBooleanCommand("DRIVER BTCOEXMODE " + mode, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setBluetoothCoexistenceScanModeCommand(mode, callback) {
|
|
|
|
doBooleanCommand("DRIVER BTCOEXSCAN-" + (mode ? "START" : "STOP"), "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveConfigCommand(callback) {
|
|
|
|
// Make sure we never write out a value for AP_SCAN other than 1
|
|
|
|
doBooleanCommand("AP_SCAN 1", "OK", function(ok) {
|
|
|
|
doBooleanCommand("SAVE_CONFIG", "OK", callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function reloadConfigCommand(callback) {
|
|
|
|
doBooleanCommand("RECONFIGURE", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setScanResultHandlingCommand(mode, callback) {
|
|
|
|
doBooleanCommand("AP_SCAN " + mode, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToBlacklistCommand(bssid, callback) {
|
|
|
|
doBooleanCommand("BLACKLIST " + bssid, "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearBlacklistCommand(callback) {
|
|
|
|
doBooleanCommand("BLACKLIST clear", "OK", callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSuspendOptimizationsCommand(enabled, callback) {
|
|
|
|
doBooleanCommand("DRIVER SETSUSPENDOPT " + (enabled ? 0 : 1), "OK", callback);
|
|
|
|
}
|
|
|
|
|
2013-08-27 20:54:55 +00:00
|
|
|
// Wrapper around libcutils.property_set that returns true if setting the
|
|
|
|
// value was successful.
|
|
|
|
// Note that the callback is not called asynchronously.
|
2012-04-09 07:44:53 +00:00
|
|
|
function setProperty(key, value, callback) {
|
2013-08-27 20:54:55 +00:00
|
|
|
let ok = true;
|
|
|
|
try {
|
|
|
|
libcutils.property_set(key, value);
|
|
|
|
} catch(e) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
callback(ok);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableInterface(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_enable", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableInterface(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_disable", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addHostRoute(ifname, route, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_add_host_route", ifname: ifname, route: route }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeHostRoutes(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_remove_host_routes", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDefaultRoute(ifname, route, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_set_default_route", ifname: ifname, route: route }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultRoute(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_get_default_route", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.route);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeDefaultRoute(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_remove_default_route", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetConnections(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "ifc_reset_connections", ifname: ifname }, function(data) {
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-22 10:22:25 +00:00
|
|
|
var httpProxyConfig = Object.create(null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a network, configure http proxy when using wifi.
|
|
|
|
* @param network A network object to update http proxy
|
|
|
|
* @param info Info should have following field:
|
|
|
|
* - httpProxyHost ip address of http proxy.
|
|
|
|
* - httpProxyPort port of http proxy, set 0 to use default port 8080.
|
|
|
|
* @param callback callback function.
|
|
|
|
*/
|
|
|
|
function configureHttpProxy(network, info, callback) {
|
|
|
|
if (!network)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let networkKey = getNetworkKey(network);
|
|
|
|
|
|
|
|
if (!info || info.httpProxyHost === "") {
|
|
|
|
delete httpProxyConfig[networkKey];
|
|
|
|
} else {
|
|
|
|
httpProxyConfig[networkKey] = network;
|
|
|
|
httpProxyConfig[networkKey].httpProxyHost = info.httpProxyHost;
|
|
|
|
httpProxyConfig[networkKey].httpProxyPort = info.httpProxyPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHttpProxyNetwork(network) {
|
|
|
|
if (!network)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let networkKey = getNetworkKey(network);
|
|
|
|
return ((networkKey in httpProxyConfig) ? httpProxyConfig : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setHttpProxy(network) {
|
|
|
|
if (!network)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gNetworkManager.setNetworkProxy(network);
|
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
var staticIpConfig = Object.create(null);
|
|
|
|
function setStaticIpMode(network, info, callback) {
|
|
|
|
let setNetworkKey = getNetworkKey(network);
|
|
|
|
let curNetworkKey = null;
|
|
|
|
let currentNetwork = Object.create(null);
|
|
|
|
currentNetwork.netId = manager.connectionInfo.id;
|
|
|
|
|
|
|
|
manager.getNetworkConfiguration(currentNetwork, function (){
|
|
|
|
curNetworkKey = getNetworkKey(currentNetwork);
|
|
|
|
|
|
|
|
// Add additional information to static ip configuration
|
|
|
|
// It is used to compatiable with information dhcp callback.
|
|
|
|
info.ipaddr = stringToIp(info.ipaddr_str);
|
|
|
|
info.gateway = stringToIp(info.gateway_str);
|
|
|
|
info.mask_str = makeMask(info.maskLength);
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
info.dns1 = stringToIp("dns1_str" in info ? info.dns1_str : "");
|
|
|
|
info.dns2 = stringToIp("dns2_str" in info ? info.dns2_str : "");
|
|
|
|
info.proxy = stringToIp("proxy_str" in info ? info.proxy_str : "");
|
|
|
|
|
|
|
|
staticIpConfig[setNetworkKey] = info;
|
|
|
|
|
|
|
|
// If the ssid of current connection is the same as configured ssid
|
|
|
|
// It means we need update current connection to use static IP address.
|
|
|
|
if (setNetworkKey == curNetworkKey) {
|
|
|
|
// Use configureInterface directly doesn't work, the network iterface
|
|
|
|
// and routing table is changed but still cannot connect to network
|
|
|
|
// so the workaround here is disable interface the enable again to
|
|
|
|
// trigger network reconnect with static ip.
|
|
|
|
disableInterface(manager.ifname, function (ok) {
|
|
|
|
enableInterface(manager.ifname, function (ok) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
var dhcpInfo = null;
|
2013-06-24 03:02:30 +00:00
|
|
|
function runDhcp(ifname) {
|
|
|
|
debug("Run Dhcp");
|
2012-04-09 07:44:53 +00:00
|
|
|
controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) {
|
|
|
|
dhcpInfo = data.status ? null : data;
|
2013-06-24 03:02:30 +00:00
|
|
|
runIpConfig(ifname, dhcpInfo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runStaticIp(ifname, key) {
|
|
|
|
debug("Run static ip");
|
|
|
|
|
|
|
|
// Read static ip information from settings.
|
|
|
|
let staticIpInfo;
|
|
|
|
|
|
|
|
if (!(key in staticIpConfig))
|
|
|
|
return;
|
|
|
|
|
|
|
|
staticIpInfo = staticIpConfig[key];
|
|
|
|
|
|
|
|
// Stop dhcpd when use static IP
|
|
|
|
if (dhcpInfo != null) {
|
|
|
|
stopDhcp(manager.ifname, function() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set ip, mask length, gateway, dns to network interface
|
|
|
|
configureInterface(ifname,
|
|
|
|
staticIpInfo.ipaddr,
|
|
|
|
staticIpInfo.maskLength,
|
|
|
|
staticIpInfo.gateway,
|
|
|
|
staticIpInfo.dns1,
|
|
|
|
staticIpInfo.dns2, function (data) {
|
|
|
|
runIpConfig(ifname, staticIpInfo);
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
function stopProcess(service, process, callback) {
|
|
|
|
var count = 0;
|
|
|
|
var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
function tick() {
|
|
|
|
let result = libcutils.property_get(service);
|
|
|
|
if (result === null) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result === "stopped" || ++count >= 5) {
|
|
|
|
// Either we succeeded or ran out of time.
|
|
|
|
timer = null;
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else it's still running, continue waiting.
|
|
|
|
timer.initWithCallback(tick, 1000, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
setProperty("ctl.stop", process, tick);
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function stopDhcp(ifname, callback) {
|
2013-07-29 09:59:53 +00:00
|
|
|
// This function does exactly what dhcp_stop does. Unforunately, if we call
|
|
|
|
// this function twice before the previous callback is returned. We may block
|
|
|
|
// our self waiting for the callback. It slows down the wifi startup procedure.
|
|
|
|
// Therefore, we have to roll our own version here.
|
|
|
|
let dhcpService = DHCP_PROP + "_" + ifname;
|
|
|
|
let suffix = (ifname.substr(0, 3) === "p2p") ? "p2p" : ifname;
|
|
|
|
let processName = DHCP + "_" + suffix;
|
|
|
|
stopProcess(dhcpService, processName, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function releaseDhcpLease(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "dhcp_release_lease", ifname: ifname }, function(data) {
|
2012-04-09 04:13:28 +00:00
|
|
|
dhcpInfo = null;
|
2012-04-09 07:44:53 +00:00
|
|
|
notify("dhcplost");
|
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDhcpError(callback) {
|
|
|
|
controlMessage({ cmd: "dhcp_get_errmsg" }, function(data) {
|
|
|
|
callback(data.error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function configureInterface(ifname, ipaddr, mask, gateway, dns1, dns2, callback) {
|
2013-08-27 20:54:51 +00:00
|
|
|
let message = { cmd: "ifc_configure", ifname: ifname,
|
2012-04-09 07:44:53 +00:00
|
|
|
ipaddr: ipaddr, mask: mask, gateway: gateway,
|
2013-08-27 20:54:51 +00:00
|
|
|
dns1: dns1, dns2: dns2};
|
|
|
|
controlMessage(message, function(data) {
|
2012-04-09 07:44:53 +00:00
|
|
|
callback(!data.status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runDhcpRenew(ifname, callback) {
|
|
|
|
controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) {
|
|
|
|
if (!data.status)
|
|
|
|
dhcpInfo = data;
|
|
|
|
callback(data.status ? null : data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var suppressEvents = false;
|
|
|
|
function notify(eventName, eventObject) {
|
|
|
|
if (suppressEvents)
|
|
|
|
return;
|
|
|
|
var handler = manager["on" + eventName];
|
|
|
|
if (handler) {
|
|
|
|
if (!eventObject)
|
|
|
|
eventObject = ({});
|
|
|
|
handler.call(eventObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyStateChange(fields) {
|
2012-06-27 15:09:16 +00:00
|
|
|
// If we're already in the COMPLETED state, we might receive events from
|
|
|
|
// the supplicant that tell us that we're re-authenticating or reminding
|
|
|
|
// us that we're associated to a network. In those cases, we don't need to
|
|
|
|
// do anything, so just ignore them.
|
|
|
|
if (manager.state === "COMPLETED" &&
|
|
|
|
fields.state !== "DISCONNECTED" &&
|
|
|
|
fields.state !== "INTERFACE_DISABLED" &&
|
|
|
|
fields.state !== "INACTIVE" &&
|
|
|
|
fields.state !== "SCANNING") {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 23:27:10 +00:00
|
|
|
|
|
|
|
// Stop background scanning if we're trying to connect to a network.
|
2012-11-05 22:29:07 +00:00
|
|
|
if (manager.backgroundScanEnabled &&
|
2012-10-25 23:27:10 +00:00
|
|
|
(fields.state === "ASSOCIATING" ||
|
|
|
|
fields.state === "ASSOCIATED" ||
|
|
|
|
fields.state === "FOUR_WAY_HANDSHAKE" ||
|
|
|
|
fields.state === "GROUP_HANDSHAKE" ||
|
|
|
|
fields.state === "COMPLETED")) {
|
|
|
|
setBackgroundScan("OFF", function() {});
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
fields.prevState = manager.state;
|
|
|
|
manager.state = fields.state;
|
|
|
|
|
2012-08-28 19:14:26 +00:00
|
|
|
// Detect wpa_supplicant's loop iterations.
|
|
|
|
manager.supplicantLoopDetection(fields.prevState, fields.state);
|
2012-04-09 07:44:53 +00:00
|
|
|
notify("statechange", fields);
|
2012-06-27 15:09:16 +00:00
|
|
|
return true;
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 14:08:52 +00:00
|
|
|
function parseStatus(status) {
|
2012-04-09 07:44:53 +00:00
|
|
|
if (status === null) {
|
|
|
|
debug("Unable to get wpa supplicant's status");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ssid;
|
|
|
|
var bssid;
|
|
|
|
var state;
|
|
|
|
var ip_address;
|
|
|
|
var id;
|
|
|
|
|
|
|
|
var lines = status.split("\n");
|
|
|
|
for (let i = 0; i < lines.length; ++i) {
|
|
|
|
let [key, value] = lines[i].split("=");
|
|
|
|
switch (key) {
|
|
|
|
case "wpa_state":
|
|
|
|
state = value;
|
|
|
|
break;
|
|
|
|
case "ssid":
|
|
|
|
ssid = value;
|
|
|
|
break;
|
|
|
|
case "bssid":
|
|
|
|
bssid = value;
|
|
|
|
break;
|
|
|
|
case "ip_address":
|
|
|
|
ip_address = value;
|
|
|
|
break;
|
|
|
|
case "id":
|
|
|
|
id = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bssid && ssid) {
|
|
|
|
manager.connectionInfo.bssid = bssid;
|
|
|
|
manager.connectionInfo.ssid = ssid;
|
|
|
|
manager.connectionInfo.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ip_address)
|
|
|
|
dhcpInfo = { ip_address: ip_address };
|
|
|
|
|
|
|
|
notifyStateChange({ state: state, fromStatus: true });
|
2012-10-25 23:27:10 +00:00
|
|
|
|
|
|
|
// If we parse the status and the supplicant has already entered the
|
2012-11-05 22:29:07 +00:00
|
|
|
// COMPLETED state, then we need to set up DHCP right away.
|
|
|
|
if (state === "COMPLETED")
|
|
|
|
onconnected();
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect to the supplicant
|
|
|
|
var connectTries = 0;
|
|
|
|
var retryTimer = null;
|
|
|
|
function connectCallback(ok) {
|
|
|
|
if (ok === 0) {
|
|
|
|
// Tell the event worker to start waiting for events.
|
|
|
|
retryTimer = null;
|
2013-08-09 05:07:56 +00:00
|
|
|
connectTries = 0;
|
2012-08-30 14:08:52 +00:00
|
|
|
didConnectSupplicant(function(){});
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (connectTries++ < 3) {
|
2013-06-24 03:02:30 +00:00
|
|
|
// Try again in 5 seconds.
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!retryTimer)
|
|
|
|
retryTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
retryTimer.initWithCallback(function(timer) {
|
2012-06-28 10:01:51 +00:00
|
|
|
connectToSupplicant(connectCallback);
|
|
|
|
}, 5000, Ci.nsITimer.TYPE_ONE_SHOT);
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
retryTimer = null;
|
2013-08-09 05:07:56 +00:00
|
|
|
connectTries = 0;
|
2012-12-24 09:23:16 +00:00
|
|
|
notify("supplicantlost", { success: false });
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 04:13:28 +00:00
|
|
|
manager.connectionDropped = function(callback) {
|
2013-05-02 10:02:33 +00:00
|
|
|
// Reset network interface when connection drop
|
|
|
|
configureInterface(manager.ifname, 0, 0, 0, 0, 0, function (data) {
|
|
|
|
});
|
|
|
|
|
2012-04-09 04:13:28 +00:00
|
|
|
// If we got disconnected, kill the DHCP client in preparation for
|
|
|
|
// reconnection.
|
|
|
|
resetConnections(manager.ifname, function() {
|
|
|
|
stopDhcp(manager.ifname, function() {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.start = function() {
|
2012-11-02 03:18:11 +00:00
|
|
|
debug("detected SDK version " + sdkVersion);
|
2012-08-30 14:08:52 +00:00
|
|
|
connectToSupplicant(connectCallback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 14:08:52 +00:00
|
|
|
function onconnected() {
|
2012-04-19 21:33:36 +00:00
|
|
|
// For now we do our own DHCP. In the future, this should be handed
|
|
|
|
// off to the Network Manager.
|
2013-06-24 03:02:30 +00:00
|
|
|
let currentNetwork = Object.create(null);
|
|
|
|
currentNetwork.netId = manager.connectionInfo.id;
|
|
|
|
|
|
|
|
manager.getNetworkConfiguration(currentNetwork, function (){
|
|
|
|
let key = getNetworkKey(currentNetwork);
|
|
|
|
if (staticIpConfig &&
|
|
|
|
(key in staticIpConfig) &&
|
|
|
|
staticIpConfig[key].enabled) {
|
|
|
|
debug("Run static ip");
|
|
|
|
runStaticIp(manager.ifname, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runDhcp(manager.ifname);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runIpConfig(name, data) {
|
|
|
|
if (!data) {
|
|
|
|
debug("IP config failed to run");
|
|
|
|
notify("networkconnected", { info: data });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setProperty("net." + name + ".dns1", ipToString(data.dns1),
|
|
|
|
function(ok) {
|
|
|
|
if (!ok) {
|
|
|
|
debug("Unable to set net.<ifname>.dns1");
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-24 03:02:30 +00:00
|
|
|
setProperty("net." + name + ".dns2", ipToString(data.dns2),
|
2012-04-19 21:33:36 +00:00
|
|
|
function(ok) {
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!ok) {
|
2013-06-24 03:02:30 +00:00
|
|
|
debug("Unable to set net.<ifname>.dns2");
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-24 03:02:30 +00:00
|
|
|
setProperty("net." + name + ".gw", ipToString(data.gateway),
|
2012-04-19 21:33:36 +00:00
|
|
|
function(ok) {
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!ok) {
|
2013-06-24 03:02:30 +00:00
|
|
|
debug("Unable to set net.<ifname>.gw");
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-24 03:02:30 +00:00
|
|
|
notify("networkconnected", { info: data });
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var supplicantStatesMap = (sdkVersion >= 15) ?
|
|
|
|
["DISCONNECTED", "INTERFACE_DISABLED", "INACTIVE", "SCANNING",
|
|
|
|
"AUTHENTICATING", "ASSOCIATING", "ASSOCIATED", "FOUR_WAY_HANDSHAKE",
|
|
|
|
"GROUP_HANDSHAKE", "COMPLETED"]
|
|
|
|
:
|
|
|
|
["DISCONNECTED", "INACTIVE", "SCANNING", "ASSOCIATING",
|
|
|
|
"ASSOCIATED", "FOUR_WAY_HANDSHAKE", "GROUP_HANDSHAKE",
|
|
|
|
"COMPLETED", "DORMANT", "UNINITIALIZED"];
|
|
|
|
|
|
|
|
var driverEventMap = { STOPPED: "driverstopped", STARTED: "driverstarted", HANGED: "driverhung" };
|
|
|
|
|
2012-11-23 10:30:33 +00:00
|
|
|
manager.getCurrentNetworkId = function (ssid, callback) {
|
|
|
|
manager.getConfiguredNetworks(function(networks) {
|
|
|
|
if (!networks) {
|
|
|
|
debug("Unable to get configured networks");
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
for (let net in networks) {
|
|
|
|
let network = networks[net];
|
|
|
|
// Trying to get netId from
|
|
|
|
// 1. CURRENT network.
|
|
|
|
// 2. Trying to associate with SSID 'ssid' event
|
|
|
|
if (network.status === "CURRENT" ||
|
|
|
|
(ssid && ssid === dequote(network.ssid))) {
|
|
|
|
return callback(net);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// Handle events sent to us by the event worker.
|
2012-04-09 07:44:53 +00:00
|
|
|
function handleEvent(event) {
|
|
|
|
debug("Event coming in: " + event);
|
2012-08-16 22:19:51 +00:00
|
|
|
if (event.indexOf("CTRL-EVENT-") !== 0 && event.indexOf("WPS") !== 0) {
|
2012-11-06 06:08:46 +00:00
|
|
|
// Handle connection fail exception on WEP-128, while password length
|
|
|
|
// is not 5 nor 13 bytes.
|
|
|
|
if (event.indexOf("Association request to the driver failed") !== -1) {
|
|
|
|
notify("passwordmaybeincorrect");
|
|
|
|
if (manager.authenticationFailuresCount > MAX_RETRIES_ON_AUTHENTICATION_FAILURE) {
|
|
|
|
manager.authenticationFailuresCount = 0;
|
2012-11-23 10:30:33 +00:00
|
|
|
notify("disconnected", {ssid: manager.connectionInfo.ssid});
|
2012-11-06 06:08:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
if (event.indexOf("WPA:") == 0 &&
|
|
|
|
event.indexOf("pre-shared key may be incorrect") != -1) {
|
|
|
|
notify("passwordmaybeincorrect");
|
|
|
|
}
|
|
|
|
|
2012-11-12 02:34:49 +00:00
|
|
|
// This is ugly, but we need to grab the SSID here. BSSID is not guaranteed
|
|
|
|
// to be provided, so don't grab BSSID here.
|
|
|
|
var match = /Trying to associate with.*SSID[ =]'(.*)'/.exec(event);
|
2012-04-09 07:44:53 +00:00
|
|
|
if (match) {
|
2012-11-12 02:34:49 +00:00
|
|
|
debug("Matched: " + match[1] + "\n");
|
|
|
|
manager.connectionInfo.ssid = match[1];
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var space = event.indexOf(" ");
|
|
|
|
var eventData = event.substr(0, space + 1);
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-STATE-CHANGE") === 0) {
|
2013-06-24 03:02:30 +00:00
|
|
|
// Parse the event data.
|
2012-04-09 07:44:53 +00:00
|
|
|
var fields = {};
|
|
|
|
var tokens = event.substr(space + 1).split(" ");
|
|
|
|
for (var n = 0; n < tokens.length; ++n) {
|
|
|
|
var kv = tokens[n].split("=");
|
|
|
|
if (kv.length === 2)
|
|
|
|
fields[kv[0]] = kv[1];
|
|
|
|
}
|
|
|
|
if (!("state" in fields))
|
|
|
|
return true;
|
|
|
|
fields.state = supplicantStatesMap[fields.state];
|
|
|
|
|
|
|
|
// The BSSID field is only valid in the ASSOCIATING and ASSOCIATED
|
2012-06-28 13:40:05 +00:00
|
|
|
// states, except when we "reauth", except this seems to depend on the
|
|
|
|
// driver, so simply check to make sure that we don't have a null BSSID.
|
|
|
|
if (fields.BSSID !== "00:00:00:00:00:00")
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.connectionInfo.bssid = fields.BSSID;
|
2012-06-28 13:40:05 +00:00
|
|
|
|
2012-11-01 09:07:25 +00:00
|
|
|
if (notifyStateChange(fields) && fields.state === "COMPLETED") {
|
|
|
|
onconnected();
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-DRIVER-STATE") === 0) {
|
|
|
|
var handlerName = driverEventMap[eventData];
|
|
|
|
if (handlerName)
|
|
|
|
notify(handlerName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-TERMINATING") === 0) {
|
|
|
|
// If the monitor socket is closed, we have already stopped the
|
|
|
|
// supplicant and we can stop waiting for more events and
|
2012-06-28 10:01:51 +00:00
|
|
|
// simply exit here (we don't have to notify about having lost
|
|
|
|
// the connection).
|
|
|
|
if (eventData.indexOf("connection closed") !== -1) {
|
2012-12-24 09:23:16 +00:00
|
|
|
notify("supplicantlost", { success: true });
|
2012-04-09 07:44:53 +00:00
|
|
|
return false;
|
2012-06-28 10:01:51 +00:00
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
// As long we haven't seen too many recv errors yet, we
|
2013-06-24 03:02:30 +00:00
|
|
|
// will keep going for a bit longer.
|
2012-04-09 07:44:53 +00:00
|
|
|
if (eventData.indexOf("recv error") !== -1 && ++recvErrors < 10)
|
|
|
|
return true;
|
|
|
|
|
2012-05-11 20:05:51 +00:00
|
|
|
notifyStateChange({ state: "DISCONNECTED", BSSID: null, id: -1 });
|
2012-12-24 09:23:16 +00:00
|
|
|
notify("supplicantlost", { success: true });
|
2012-04-09 07:44:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-DISCONNECTED") === 0) {
|
2012-08-28 19:14:26 +00:00
|
|
|
var token = event.split(" ")[1];
|
|
|
|
var bssid = token.split("=")[1];
|
|
|
|
if (manager.authenticationFailuresCount > MAX_RETRIES_ON_AUTHENTICATION_FAILURE) {
|
|
|
|
manager.authenticationFailuresCount = 0;
|
2012-11-23 10:30:33 +00:00
|
|
|
notify("disconnected", {ssid: manager.connectionInfo.ssid});
|
2012-08-28 19:14:26 +00:00
|
|
|
}
|
2012-11-23 10:30:33 +00:00
|
|
|
manager.connectionInfo.bssid = null;
|
|
|
|
manager.connectionInfo.ssid = null;
|
|
|
|
manager.connectionInfo.id = -1;
|
2012-04-09 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-06 06:08:46 +00:00
|
|
|
// Association reject is triggered mostly on incorrect WEP key.
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-ASSOC-REJECT") === 0) {
|
|
|
|
notify("passwordmaybeincorrect");
|
|
|
|
if (manager.authenticationFailuresCount > MAX_RETRIES_ON_AUTHENTICATION_FAILURE) {
|
|
|
|
manager.authenticationFailuresCount = 0;
|
2012-11-23 10:30:33 +00:00
|
|
|
notify("disconnected", {ssid: manager.connectionInfo.ssid});
|
2012-11-06 06:08:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 08:42:54 +00:00
|
|
|
if (eventData.indexOf("CTRL-EVENT-EAP-FAILURE") === 0) {
|
|
|
|
if (event.indexOf("EAP authentication failed") !== -1) {
|
|
|
|
notify("passwordmaybeincorrect");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
if (eventData.indexOf("CTRL-EVENT-CONNECTED") === 0) {
|
2012-11-12 02:34:49 +00:00
|
|
|
// Format: CTRL-EVENT-CONNECTED - Connection to 00:1e:58:ec:d5:6d completed (reauth) [id=1 id_str=]
|
|
|
|
var bssid = event.split(" ")[4];
|
2013-06-24 03:02:30 +00:00
|
|
|
|
|
|
|
var keyword = "id=";
|
|
|
|
var id = event.substr(event.indexOf(keyword) + keyword.length).split(" ")[0];
|
2012-11-12 02:34:49 +00:00
|
|
|
// Read current BSSID here, it will always being provided.
|
2013-06-24 03:02:30 +00:00
|
|
|
manager.connectionInfo.id = id;
|
2012-11-12 02:34:49 +00:00
|
|
|
manager.connectionInfo.bssid = bssid;
|
2012-04-09 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("CTRL-EVENT-SCAN-RESULTS") === 0) {
|
|
|
|
debug("Notifying of scan results available");
|
2012-10-25 23:27:10 +00:00
|
|
|
if (reEnableBackgroundScan) {
|
|
|
|
reEnableBackgroundScan = false;
|
|
|
|
setBackgroundScan("ON", function() {});
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
notify("scanresultsavailable");
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-16 22:19:51 +00:00
|
|
|
if (eventData.indexOf("WPS-TIMEOUT") === 0) {
|
|
|
|
notifyStateChange({ state: "WPS_TIMEOUT", BSSID: null, id: -1 });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("WPS-FAIL") === 0) {
|
|
|
|
notifyStateChange({ state: "WPS_FAIL", BSSID: null, id: -1 });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (eventData.indexOf("WPS-OVERLAP-DETECTED") === 0) {
|
|
|
|
notifyStateChange({ state: "WPS_OVERLAP_DETECTED", BSSID: null, id: -1 });
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-24 03:02:30 +00:00
|
|
|
// Unknown event.
|
2012-04-09 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function killSupplicant(callback) {
|
|
|
|
// It is interesting to note that this function does exactly what
|
|
|
|
// wifi_stop_supplicant does. Unforunately, on the Galaxy S2, Samsung
|
|
|
|
// changed that function in a way that means that it doesn't recognize
|
|
|
|
// wpa_supplicant as already running. Therefore, we have to roll our own
|
|
|
|
// version here.
|
2013-07-29 09:59:53 +00:00
|
|
|
stopProcess(SUPP_PROP, WPA_SUPPLICANT, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 14:08:52 +00:00
|
|
|
function didConnectSupplicant(callback) {
|
2012-04-09 07:44:53 +00:00
|
|
|
waitForEvent();
|
|
|
|
|
|
|
|
// Load up the supplicant state.
|
2012-10-31 00:35:34 +00:00
|
|
|
getDebugEnabled(function(ok) {
|
|
|
|
syncDebug();
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
statusCommand(function(status) {
|
2012-08-30 14:08:52 +00:00
|
|
|
parseStatus(status);
|
2012-08-27 02:29:46 +00:00
|
|
|
notify("supplicantconnection");
|
2012-04-09 07:44:53 +00:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareForStartup(callback) {
|
2013-07-29 09:59:53 +00:00
|
|
|
let status = libcutils.property_get(DHCP_PROP + "_" + manager.ifname);
|
|
|
|
if (status !== "running") {
|
|
|
|
tryStopSupplicant();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-30 14:08:52 +00:00
|
|
|
manager.connectionDropped(function() {
|
2013-07-29 09:59:53 +00:00
|
|
|
tryStopSupplicant();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ignore any errors and kill any currently-running supplicants. On some
|
|
|
|
// phones, stopSupplicant won't work for a supplicant that we didn't
|
|
|
|
// start, so we hand-roll it here.
|
|
|
|
function tryStopSupplicant () {
|
|
|
|
let status = libcutils.property_get(SUPP_PROP);
|
|
|
|
if (status !== "running") {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-30 14:08:52 +00:00
|
|
|
suppressEvents = true;
|
|
|
|
killSupplicant(function() {
|
|
|
|
disableInterface(manager.ifname, function (ok) {
|
|
|
|
suppressEvents = false;
|
|
|
|
callback();
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-07-29 09:59:53 +00:00
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// Initial state.
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.state = "UNINITIALIZED";
|
2013-01-17 12:26:17 +00:00
|
|
|
manager.tetheringState = "UNINITIALIZED";
|
2012-06-28 10:01:51 +00:00
|
|
|
manager.enabled = false;
|
|
|
|
manager.supplicantStarted = false;
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.connectionInfo = { ssid: null, bssid: null, id: -1 };
|
2012-08-28 19:14:26 +00:00
|
|
|
manager.authenticationFailuresCount = 0;
|
|
|
|
manager.loopDetectionCount = 0;
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-11-26 09:56:44 +00:00
|
|
|
var waitForDriverReadyTimer = null;
|
|
|
|
function cancelWaitForDriverReadyTimer() {
|
|
|
|
if (waitForDriverReadyTimer) {
|
|
|
|
waitForDriverReadyTimer.cancel();
|
|
|
|
waitForDriverReadyTimer = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function createWaitForDriverReadyTimer(onTimeout) {
|
|
|
|
waitForDriverReadyTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
waitForDriverReadyTimer.initWithCallback(onTimeout,
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.driverDelay,
|
2012-11-26 09:56:44 +00:00
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
};
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// Public interface of the wifi service.
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.setWifiEnabled = function(enable, callback) {
|
2012-07-02 12:05:17 +00:00
|
|
|
if (enable === manager.enabled) {
|
2012-06-28 10:01:51 +00:00
|
|
|
callback("no change");
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
2013-01-17 12:26:17 +00:00
|
|
|
manager.state = "INITIALIZING";
|
2013-07-29 09:59:53 +00:00
|
|
|
// Register as network interface.
|
|
|
|
WifiNetworkInterface.name = manager.ifname;
|
|
|
|
if (!WifiNetworkInterface.registered) {
|
|
|
|
gNetworkManager.registerNetworkInterface(WifiNetworkInterface);
|
|
|
|
WifiNetworkInterface.registered = true;
|
|
|
|
}
|
|
|
|
WifiNetworkInterface.state = Ci.nsINetworkInterface.NETWORK_STATE_DISCONNECTED;
|
|
|
|
WifiNetworkInterface.ip = null;
|
|
|
|
WifiNetworkInterface.netmask = null;
|
|
|
|
WifiNetworkInterface.broadcast = null;
|
|
|
|
WifiNetworkInterface.gateway = null;
|
|
|
|
WifiNetworkInterface.dns1 = null;
|
|
|
|
WifiNetworkInterface.dns2 = null;
|
|
|
|
Services.obs.notifyObservers(WifiNetworkInterface,
|
|
|
|
kNetworkInterfaceStateChangedTopic,
|
|
|
|
null);
|
|
|
|
prepareForStartup(function() {
|
|
|
|
loadDriver(function (status) {
|
|
|
|
if (status < 0) {
|
|
|
|
callback(status);
|
|
|
|
manager.state = "UNINITIALIZED";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gNetworkManager.setWifiOperationMode(manager.ifname,
|
|
|
|
WIFI_FIRMWARE_STATION,
|
|
|
|
function (status) {
|
2013-07-30 08:07:13 +00:00
|
|
|
if (status) {
|
2012-04-09 07:44:53 +00:00
|
|
|
callback(status);
|
2013-01-17 12:26:17 +00:00
|
|
|
manager.state = "UNINITIALIZED";
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-07-30 08:07:13 +00:00
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
function doStartSupplicant() {
|
|
|
|
cancelWaitForDriverReadyTimer();
|
|
|
|
startSupplicant(function (status) {
|
|
|
|
if (status < 0) {
|
|
|
|
unloadDriver(function() {
|
|
|
|
callback(status);
|
2012-06-28 12:36:13 +00:00
|
|
|
});
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.state = "UNINITIALIZED";
|
|
|
|
return;
|
|
|
|
}
|
2013-08-16 09:45:14 +00:00
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.supplicantStarted = true;
|
|
|
|
enableInterface(manager.ifname, function (ok) {
|
|
|
|
callback(ok ? 0 : -1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Driver startup on certain platforms takes longer than it takes for us
|
|
|
|
// to return from loadDriver, so wait 2 seconds before starting
|
|
|
|
// the supplicant to give it a chance to start.
|
|
|
|
if (manager.driverDelay > 0) {
|
2013-07-30 08:07:13 +00:00
|
|
|
createWaitForDriverReadyTimer(doStartSupplicant);
|
2013-07-29 09:59:53 +00:00
|
|
|
} else {
|
|
|
|
doStartSupplicant();
|
|
|
|
}
|
2013-07-30 08:07:13 +00:00
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Note these following calls ignore errors. If we fail to kill the
|
|
|
|
// supplicant gracefully, then we need to continue telling it to die
|
|
|
|
// until it does.
|
|
|
|
terminateSupplicant(function (ok) {
|
2012-04-09 04:13:28 +00:00
|
|
|
manager.connectionDropped(function () {
|
|
|
|
stopSupplicant(function (status) {
|
|
|
|
closeSupplicantConnection(function () {
|
2012-08-15 00:00:43 +00:00
|
|
|
manager.state = "UNINITIALIZED";
|
2012-04-09 04:13:28 +00:00
|
|
|
disableInterface(manager.ifname, function (ok) {
|
|
|
|
unloadDriver(callback);
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:31:26 +00:00
|
|
|
// Get wifi interface and load wifi driver when enable Ap mode.
|
2013-05-16 11:39:58 +00:00
|
|
|
manager.setWifiApEnabled = function(enabled, configuration, callback) {
|
2012-09-04 04:26:56 +00:00
|
|
|
if (enabled) {
|
2013-01-17 12:26:17 +00:00
|
|
|
manager.tetheringState = "INITIALIZING";
|
2013-07-29 09:59:53 +00:00
|
|
|
loadDriver(function (status) {
|
|
|
|
if (status < 0) {
|
2013-01-17 12:26:17 +00:00
|
|
|
callback();
|
|
|
|
manager.tetheringState = "UNINITIALIZED";
|
2012-08-29 19:31:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-26 09:56:44 +00:00
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
function doStartWifiTethering() {
|
|
|
|
cancelWaitForDriverReadyTimer();
|
|
|
|
WifiNetworkInterface.name = manager.ifname;
|
|
|
|
gNetworkManager.setWifiTethering(enabled, WifiNetworkInterface,
|
|
|
|
configuration, function(result) {
|
|
|
|
if (result) {
|
|
|
|
manager.tetheringState = "UNINITIALIZED";
|
|
|
|
} else {
|
|
|
|
manager.tetheringState = "COMPLETED";
|
|
|
|
}
|
|
|
|
// Pop out current request.
|
|
|
|
callback();
|
|
|
|
// Should we fire a dom event if we fail to set wifi tethering ?
|
|
|
|
debug("Enable Wifi tethering result: " + (result ? result : "successfully"));
|
|
|
|
});
|
|
|
|
}
|
2013-08-16 09:45:14 +00:00
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
// Driver startup on certain platforms takes longer than it takes
|
|
|
|
// for us to return from loadDriver, so wait 2 seconds before
|
|
|
|
// turning on Wifi tethering.
|
|
|
|
createWaitForDriverReadyTimer(doStartWifiTethering);
|
2012-08-29 19:31:26 +00:00
|
|
|
});
|
|
|
|
} else {
|
2013-05-16 11:39:58 +00:00
|
|
|
gNetworkManager.setWifiTethering(enabled, WifiNetworkInterface,
|
|
|
|
configuration, function(result) {
|
2012-09-13 07:19:47 +00:00
|
|
|
// Should we fire a dom event if we fail to set wifi tethering ?
|
|
|
|
debug("Disable Wifi tethering result: " + (result ? result : "successfully"));
|
|
|
|
// Unload wifi driver even if we fail to control wifi tethering.
|
|
|
|
unloadDriver(function(status) {
|
|
|
|
if (status < 0) {
|
|
|
|
debug("Fail to unload wifi driver");
|
|
|
|
}
|
2013-01-17 12:26:17 +00:00
|
|
|
manager.tetheringState = "UNINITIALIZED";
|
|
|
|
callback();
|
2012-09-13 07:19:47 +00:00
|
|
|
});
|
2012-08-29 19:31:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.disconnect = disconnectCommand;
|
|
|
|
manager.reconnect = reconnectCommand;
|
|
|
|
manager.reassociate = reassociateCommand;
|
|
|
|
|
|
|
|
var networkConfigurationFields = [
|
|
|
|
"ssid", "bssid", "psk", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
|
|
|
|
"wep_tx_keyidx", "priority", "key_mgmt", "scan_ssid", "disabled",
|
2013-08-05 20:25:40 +00:00
|
|
|
"identity", "password", "auth_alg", "phase1", "phase2", "eap", "pin",
|
|
|
|
"pcsc"
|
2012-04-09 07:44:53 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
manager.getNetworkConfiguration = function(config, callback) {
|
|
|
|
var netId = config.netId;
|
|
|
|
var done = 0;
|
|
|
|
for (var n = 0; n < networkConfigurationFields.length; ++n) {
|
|
|
|
let fieldName = networkConfigurationFields[n];
|
|
|
|
getNetworkVariableCommand(netId, fieldName, function(value) {
|
|
|
|
if (value !== null)
|
|
|
|
config[fieldName] = value;
|
|
|
|
if (++done == networkConfigurationFields.length)
|
|
|
|
callback(config);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
manager.setNetworkConfiguration = function(config, callback) {
|
|
|
|
var netId = config.netId;
|
|
|
|
var done = 0;
|
|
|
|
var errors = 0;
|
|
|
|
for (var n = 0; n < networkConfigurationFields.length; ++n) {
|
|
|
|
let fieldName = networkConfigurationFields[n];
|
|
|
|
if (!(fieldName in config) ||
|
|
|
|
// These fields are special: We can't retrieve them from the
|
|
|
|
// supplicant, and often we have a star in our config. In that case,
|
|
|
|
// we need to avoid overwriting the correct password with a *.
|
|
|
|
(fieldName === "password" ||
|
|
|
|
fieldName === "wep_key0" ||
|
|
|
|
fieldName === "psk") &&
|
|
|
|
config[fieldName] === '*') {
|
|
|
|
++done;
|
|
|
|
} else {
|
|
|
|
setNetworkVariableCommand(netId, fieldName, config[fieldName], function(ok) {
|
|
|
|
if (!ok)
|
|
|
|
++errors;
|
|
|
|
if (++done == networkConfigurationFields.length)
|
|
|
|
callback(errors == 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 03:02:30 +00:00
|
|
|
// If config didn't contain any of the fields we want, don't lose the error callback.
|
2012-04-09 07:44:53 +00:00
|
|
|
if (done == networkConfigurationFields.length)
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
manager.getConfiguredNetworks = function(callback) {
|
|
|
|
listNetworksCommand(function (reply) {
|
|
|
|
var networks = Object.create(null);
|
|
|
|
var lines = reply.split("\n");
|
|
|
|
if (lines.length === 1) {
|
|
|
|
// We need to make sure we call the callback even if there are no
|
|
|
|
// configured networks.
|
|
|
|
callback(networks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var done = 0;
|
|
|
|
var errors = 0;
|
|
|
|
for (var n = 1; n < lines.length; ++n) {
|
|
|
|
var result = lines[n].split("\t");
|
|
|
|
var netId = result[0];
|
|
|
|
var config = networks[netId] = { netId: netId };
|
|
|
|
switch (result[3]) {
|
|
|
|
case "[CURRENT]":
|
|
|
|
config.status = "CURRENT";
|
|
|
|
break;
|
|
|
|
case "[DISABLED]":
|
|
|
|
config.status = "DISABLED";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
config.status = "ENABLED";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
manager.getNetworkConfiguration(config, function (ok) {
|
|
|
|
if (!ok)
|
|
|
|
++errors;
|
|
|
|
if (++done == lines.length - 1) {
|
|
|
|
if (errors) {
|
2013-06-24 03:02:30 +00:00
|
|
|
// If an error occured, delete the new netId.
|
2012-04-09 07:44:53 +00:00
|
|
|
removeNetworkCommand(netId, function() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(networks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
manager.addNetwork = function(config, callback) {
|
|
|
|
addNetworkCommand(function (netId) {
|
|
|
|
config.netId = netId;
|
|
|
|
manager.setNetworkConfiguration(config, function (ok) {
|
|
|
|
if (!ok) {
|
|
|
|
removeNetworkCommand(netId, function() { callback(false); });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(ok);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
manager.updateNetwork = function(config, callback) {
|
|
|
|
manager.setNetworkConfiguration(config, callback);
|
|
|
|
}
|
|
|
|
manager.removeNetwork = function(netId, callback) {
|
|
|
|
removeNetworkCommand(netId, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ipToString(n) {
|
|
|
|
return String((n >> 0) & 0xFF) + "." +
|
|
|
|
((n >> 8) & 0xFF) + "." +
|
|
|
|
((n >> 16) & 0xFF) + "." +
|
|
|
|
((n >> 24) & 0xFF);
|
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
function stringToIp(string) {
|
|
|
|
let ip = 0;
|
|
|
|
let start, end = -1;
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
start = end + 1;
|
|
|
|
end = string.indexOf(".", start);
|
|
|
|
if (end == -1) {
|
|
|
|
end = string.length;
|
|
|
|
}
|
|
|
|
let num = parseInt(string.slice(start, end), 10);
|
|
|
|
if (isNaN(num)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ip |= num << (i * 8);
|
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
function swap32(n) {
|
|
|
|
return (((n >> 24) & 0xFF) << 0) |
|
|
|
|
(((n >> 16) & 0xFF) << 8) |
|
|
|
|
(((n >> 8) & 0xFF) << 16) |
|
|
|
|
(((n >> 0) & 0xFF) << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ntohl(n) {
|
|
|
|
return swap32(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeMask(len) {
|
|
|
|
let mask = 0;
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
|
|
mask |= (0x80000000 >> i);
|
|
|
|
}
|
|
|
|
return ntohl(mask);
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.saveConfig = function(callback) {
|
|
|
|
saveConfigCommand(callback);
|
|
|
|
}
|
|
|
|
manager.enableNetwork = function(netId, disableOthers, callback) {
|
|
|
|
enableNetworkCommand(netId, disableOthers, callback);
|
|
|
|
}
|
|
|
|
manager.disableNetwork = function(netId, callback) {
|
|
|
|
disableNetworkCommand(netId, callback);
|
|
|
|
}
|
|
|
|
manager.getMacAddress = getMacAddressCommand;
|
|
|
|
manager.getScanResults = scanResultsCommand;
|
|
|
|
manager.setScanMode = function(mode, callback) {
|
|
|
|
setScanModeCommand(mode === "active", callback);
|
|
|
|
}
|
2012-10-25 23:27:10 +00:00
|
|
|
manager.setBackgroundScan = setBackgroundScan;
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.scan = scanCommand;
|
2012-08-16 22:19:51 +00:00
|
|
|
manager.wpsPbc = wpsPbcCommand;
|
2012-08-22 19:41:31 +00:00
|
|
|
manager.wpsPin = wpsPinCommand;
|
2012-08-16 22:19:51 +00:00
|
|
|
manager.wpsCancel = wpsCancelCommand;
|
2013-07-09 20:27:58 +00:00
|
|
|
manager.setPowerMode = (sdkVersion >= 16)
|
|
|
|
? setPowerModeCommandJB
|
|
|
|
: setPowerModeCommandICS;
|
2013-08-22 10:22:25 +00:00
|
|
|
manager.getHttpProxyNetwork = getHttpProxyNetwork;
|
|
|
|
manager.setHttpProxy = setHttpProxy;
|
|
|
|
manager.configureHttpProxy = configureHttpProxy;
|
2012-09-24 09:05:47 +00:00
|
|
|
manager.setSuspendOptimizations = setSuspendOptimizationsCommand;
|
2013-06-24 03:02:30 +00:00
|
|
|
manager.setStaticIpMode = setStaticIpMode;
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.getRssiApprox = getRssiApproxCommand;
|
|
|
|
manager.getLinkSpeed = getLinkSpeedCommand;
|
2012-04-09 03:32:17 +00:00
|
|
|
manager.getDhcpInfo = function() { return dhcpInfo; }
|
2012-07-13 01:56:39 +00:00
|
|
|
manager.getConnectionInfo = (sdkVersion >= 15)
|
|
|
|
? getConnectionInfoICS
|
|
|
|
: getConnectionInfoGB;
|
2012-08-28 19:14:26 +00:00
|
|
|
|
|
|
|
manager.isHandShakeState = function(state) {
|
|
|
|
switch (state) {
|
|
|
|
case "AUTHENTICATING":
|
|
|
|
case "ASSOCIATING":
|
|
|
|
case "ASSOCIATED":
|
|
|
|
case "FOUR_WAY_HANDSHAKE":
|
|
|
|
case "GROUP_HANDSHAKE":
|
|
|
|
return true;
|
|
|
|
case "DORMANT":
|
|
|
|
case "COMPLETED":
|
|
|
|
case "DISCONNECTED":
|
|
|
|
case "INTERFACE_DISABLED":
|
|
|
|
case "INACTIVE":
|
|
|
|
case "SCANNING":
|
|
|
|
case "UNINITIALIZED":
|
|
|
|
case "INVALID":
|
|
|
|
case "CONNECTED":
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-31 00:35:34 +00:00
|
|
|
manager.syncDebug = syncDebug;
|
2012-08-28 19:14:26 +00:00
|
|
|
manager.stateOrdinal = function(state) {
|
|
|
|
return supplicantStatesMap.indexOf(state);
|
|
|
|
}
|
|
|
|
manager.supplicantLoopDetection = function(prevState, state) {
|
|
|
|
var isPrevStateInHandShake = manager.isHandShakeState(prevState);
|
|
|
|
var isStateInHandShake = manager.isHandShakeState(state);
|
|
|
|
|
|
|
|
if (isPrevStateInHandShake) {
|
|
|
|
if (isStateInHandShake) {
|
|
|
|
// Increase the count only if we are in the loop.
|
|
|
|
if (manager.stateOrdinal(state) > manager.stateOrdinal(prevState)) {
|
|
|
|
manager.loopDetectionCount++;
|
|
|
|
}
|
|
|
|
if (manager.loopDetectionCount > MAX_SUPPLICANT_LOOP_ITERATIONS) {
|
2012-11-23 10:30:33 +00:00
|
|
|
notify("disconnected", {ssid: manager.connectionInfo.ssid});
|
2012-08-28 19:14:26 +00:00
|
|
|
manager.loopDetectionCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// From others state to HandShake state. Reset the count.
|
|
|
|
if (isStateInHandShake) {
|
|
|
|
manager.loopDetectionCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
return manager;
|
|
|
|
})();
|
|
|
|
|
2012-11-08 05:34:12 +00:00
|
|
|
// Get unique key for a network, now the key is created by escape(SSID)+Security.
|
|
|
|
// So networks of same SSID but different security mode can be identified.
|
|
|
|
function getNetworkKey(network)
|
|
|
|
{
|
|
|
|
var ssid = "",
|
|
|
|
encryption = "OPEN";
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
if ("security" in network) {
|
2012-11-08 05:34:12 +00:00
|
|
|
// manager network object, represents an AP
|
|
|
|
// object structure
|
|
|
|
// {
|
|
|
|
// .ssid : SSID of AP
|
2013-07-08 11:41:48 +00:00
|
|
|
// .security[] : "WPA-PSK" for WPA-PSK
|
2012-11-08 05:34:12 +00:00
|
|
|
// "WPA-EAP" for WPA-EAP
|
|
|
|
// "WEP" for WEP
|
|
|
|
// "" for OPEN
|
|
|
|
// other keys
|
|
|
|
// }
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
var security = network.security;
|
2012-11-08 05:34:12 +00:00
|
|
|
ssid = network.ssid;
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
for (let j = 0; j < security.length; j++) {
|
|
|
|
if (security[j] === "WPA-PSK") {
|
2012-11-08 05:34:12 +00:00
|
|
|
encryption = "WPA-PSK";
|
|
|
|
break;
|
2013-07-08 11:41:48 +00:00
|
|
|
} else if (security[j] === "WPA-EAP") {
|
2012-11-08 05:34:12 +00:00
|
|
|
encryption = "WPA-EAP";
|
|
|
|
break;
|
2013-07-08 11:41:48 +00:00
|
|
|
} else if (security[j] === "WEP") {
|
2012-11-08 05:34:12 +00:00
|
|
|
encryption = "WEP";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ("key_mgmt" in network) {
|
|
|
|
// configure network object, represents a network
|
|
|
|
// object structure
|
|
|
|
// {
|
|
|
|
// .ssid : SSID of network, quoted
|
|
|
|
// .key_mgmt : Encryption type
|
|
|
|
// "WPA-PSK" for WPA-PSK
|
|
|
|
// "WPA-EAP" for WPA-EAP
|
|
|
|
// "NONE" for WEP/OPEN
|
|
|
|
// .auth_alg : Encryption algorithm(WEP mode only)
|
|
|
|
// "OPEN_SHARED" for WEP
|
|
|
|
// other keys
|
|
|
|
// }
|
|
|
|
var key_mgmt = network.key_mgmt,
|
|
|
|
auth_alg = network.auth_alg;
|
|
|
|
ssid = dequote(network.ssid);
|
|
|
|
|
|
|
|
if (key_mgmt == "WPA-PSK") {
|
|
|
|
encryption = "WPA-PSK";
|
|
|
|
} else if (key_mgmt == "WPA-EAP") {
|
|
|
|
encryption = "WPA-EAP";
|
|
|
|
} else if (key_mgmt == "NONE" && auth_alg === "OPEN SHARED") {
|
|
|
|
encryption = "WEP";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ssid here must be dequoted, and it's safer to esacpe it.
|
|
|
|
// encryption won't be empty and always be assigned one of the followings :
|
|
|
|
// "OPEN"/"WEP"/"WPA-PSK"/"WPA-EAP".
|
2013-06-24 03:02:30 +00:00
|
|
|
// So for a invalid network object, the returned key will be "OPEN".
|
2012-11-08 05:34:12 +00:00
|
|
|
return escape(ssid) + encryption;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function getKeyManagement(flags) {
|
|
|
|
var types = [];
|
|
|
|
if (!flags)
|
|
|
|
return types;
|
|
|
|
|
|
|
|
if (/\[WPA2?-PSK/.test(flags))
|
|
|
|
types.push("WPA-PSK");
|
|
|
|
if (/\[WPA2?-EAP/.test(flags))
|
|
|
|
types.push("WPA-EAP");
|
|
|
|
if (/\[WEP/.test(flags))
|
|
|
|
types.push("WEP");
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
function getCapabilities(flags) {
|
|
|
|
var types = [];
|
|
|
|
if (!flags)
|
|
|
|
return types;
|
|
|
|
|
|
|
|
if (/\[WPS/.test(flags))
|
|
|
|
types.push("WPS");
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// These constants shamelessly ripped from WifiManager.java
|
|
|
|
// strength is the value returned by scan_results. It is nominally in dB. We
|
|
|
|
// transform it into a percentage for clients looking to simply show a
|
|
|
|
// relative indication of the strength of a network.
|
|
|
|
const MIN_RSSI = -100;
|
|
|
|
const MAX_RSSI = -55;
|
|
|
|
|
|
|
|
function calculateSignal(strength) {
|
|
|
|
// Some wifi drivers represent their signal strengths as 8-bit integers, so
|
|
|
|
// in order to avoid negative numbers, they add 256 to the actual values.
|
|
|
|
// While we don't *know* that this is the case here, we make an educated
|
|
|
|
// guess.
|
|
|
|
if (strength > 0)
|
|
|
|
strength -= 256;
|
|
|
|
|
|
|
|
if (strength <= MIN_RSSI)
|
|
|
|
return 0;
|
|
|
|
if (strength >= MAX_RSSI)
|
|
|
|
return 100;
|
|
|
|
return Math.floor(((strength - MIN_RSSI) / (MAX_RSSI - MIN_RSSI)) * 100);
|
|
|
|
}
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
function Network(ssid, security, password, capabilities) {
|
2012-04-09 07:44:53 +00:00
|
|
|
this.ssid = ssid;
|
2013-07-08 11:41:48 +00:00
|
|
|
this.security = security;
|
2012-09-27 18:18:34 +00:00
|
|
|
|
|
|
|
if (typeof password !== "undefined")
|
|
|
|
this.password = password;
|
2013-07-08 11:41:48 +00:00
|
|
|
if (capabilities !== undefined)
|
|
|
|
this.capabilities = capabilities;
|
2012-09-27 18:18:34 +00:00
|
|
|
// TODO connected here as well?
|
|
|
|
|
|
|
|
this.__exposedProps__ = Network.api;
|
|
|
|
}
|
|
|
|
|
|
|
|
Network.api = {
|
|
|
|
ssid: "r",
|
2013-07-08 11:41:48 +00:00
|
|
|
security: "r",
|
2012-09-27 18:18:34 +00:00
|
|
|
capabilities: "r",
|
|
|
|
known: "r",
|
|
|
|
|
|
|
|
password: "rw",
|
|
|
|
keyManagement: "rw",
|
|
|
|
psk: "rw",
|
|
|
|
identity: "rw",
|
2012-09-27 18:41:31 +00:00
|
|
|
wep: "rw",
|
2013-05-30 03:17:07 +00:00
|
|
|
hidden: "rw",
|
|
|
|
eap: "rw",
|
2013-08-05 20:25:40 +00:00
|
|
|
pin: "rw",
|
2013-05-30 03:17:07 +00:00
|
|
|
phase1: "rw",
|
|
|
|
phase2: "rw"
|
2012-09-27 18:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Note: We never use ScanResult.prototype, so the fact that it's unrelated to
|
|
|
|
// Network.prototype is OK.
|
|
|
|
function ScanResult(ssid, bssid, flags, signal) {
|
2013-07-08 11:41:48 +00:00
|
|
|
Network.call(this, ssid, getKeyManagement(flags), undefined,
|
|
|
|
getCapabilities(flags));
|
2012-04-09 07:44:53 +00:00
|
|
|
this.bssid = bssid;
|
2012-06-01 21:09:59 +00:00
|
|
|
this.signalStrength = signal;
|
|
|
|
this.relSignalStrength = calculateSignal(Number(signal));
|
2012-07-02 08:58:17 +00:00
|
|
|
|
|
|
|
this.__exposedProps__ = ScanResult.api;
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 08:58:17 +00:00
|
|
|
// XXX This should probably live in the DOM-facing side, but it's hard to do
|
|
|
|
// there, so we stick this here.
|
|
|
|
ScanResult.api = {
|
|
|
|
bssid: "r",
|
|
|
|
signalStrength: "r",
|
|
|
|
relSignalStrength: "r",
|
2012-09-27 18:18:34 +00:00
|
|
|
connected: "r"
|
2012-07-02 08:58:17 +00:00
|
|
|
};
|
|
|
|
|
2012-09-27 18:18:34 +00:00
|
|
|
for (let i in Network.api) {
|
|
|
|
ScanResult.api[i] = Network.api[i];
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function quote(s) {
|
|
|
|
return '"' + s + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
function dequote(s) {
|
|
|
|
if (s[0] != '"' || s[s.length - 1] != '"')
|
|
|
|
throw "Invalid argument, not a quoted string: " + s;
|
|
|
|
return s.substr(1, s.length - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isWepHexKey(s) {
|
|
|
|
if (s.length != 10 && s.length != 26 && s.length != 58)
|
|
|
|
return false;
|
|
|
|
return !/[^a-fA-F0-9]/.test(s);
|
|
|
|
}
|
|
|
|
|
2012-04-19 21:33:36 +00:00
|
|
|
|
|
|
|
let WifiNetworkInterface = {
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINetworkInterface]),
|
|
|
|
|
|
|
|
registered: false,
|
|
|
|
|
|
|
|
// nsINetworkInterface
|
|
|
|
|
|
|
|
NETWORK_STATE_UNKNOWN: Ci.nsINetworkInterface.NETWORK_STATE_UNKNOWN,
|
|
|
|
NETWORK_STATE_CONNECTING: Ci.nsINetworkInterface.CONNECTING,
|
|
|
|
NETWORK_STATE_CONNECTED: Ci.nsINetworkInterface.CONNECTED,
|
|
|
|
NETWORK_STATE_DISCONNECTING: Ci.nsINetworkInterface.DISCONNECTING,
|
|
|
|
NETWORK_STATE_DISCONNECTED: Ci.nsINetworkInterface.DISCONNECTED,
|
|
|
|
|
|
|
|
state: Ci.nsINetworkInterface.NETWORK_STATE_UNKNOWN,
|
|
|
|
|
2012-08-30 16:57:33 +00:00
|
|
|
NETWORK_TYPE_WIFI: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
|
|
|
NETWORK_TYPE_MOBILE: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
|
|
|
|
NETWORK_TYPE_MOBILE_MMS: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS,
|
|
|
|
NETWORK_TYPE_MOBILE_SUPL: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL,
|
2012-04-19 21:33:36 +00:00
|
|
|
|
|
|
|
type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
|
|
|
|
|
|
|
name: null,
|
|
|
|
|
2012-08-14 01:54:42 +00:00
|
|
|
ip: null,
|
|
|
|
|
|
|
|
netmask: null,
|
|
|
|
|
|
|
|
broadcast: null,
|
|
|
|
|
|
|
|
dns1: null,
|
|
|
|
|
|
|
|
dns2: null,
|
|
|
|
|
2012-08-01 14:55:43 +00:00
|
|
|
httpProxyHost: null,
|
|
|
|
|
|
|
|
httpProxyPort: null,
|
|
|
|
|
2012-04-19 21:33:36 +00:00
|
|
|
};
|
|
|
|
|
2012-10-02 00:40:19 +00:00
|
|
|
function WifiScanResult() {}
|
2012-04-19 21:33:36 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// TODO Make the difference between a DOM-based network object and our
|
|
|
|
// networks objects much clearer.
|
|
|
|
let netToDOM;
|
|
|
|
let netFromDOM;
|
|
|
|
|
|
|
|
function WifiWorker() {
|
|
|
|
var self = this;
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
this._mm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageListenerManager);
|
2012-09-27 18:18:34 +00:00
|
|
|
const messages = ["WifiManager:getNetworks", "WifiManager:getKnownNetworks",
|
2012-04-13 07:54:11 +00:00
|
|
|
"WifiManager:associate", "WifiManager:forget",
|
2012-08-22 19:37:11 +00:00
|
|
|
"WifiManager:wps", "WifiManager:getState",
|
2012-09-24 09:05:47 +00:00
|
|
|
"WifiManager:setPowerSavingMode",
|
2013-08-22 10:22:25 +00:00
|
|
|
"WifiManager:setHttpProxy",
|
2013-06-24 03:02:30 +00:00
|
|
|
"WifiManager:setStaticIpMode",
|
2012-10-02 21:24:53 +00:00
|
|
|
"child-process-shutdown"];
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
messages.forEach((function(msgName) {
|
|
|
|
this._mm.addMessageListener(msgName, this);
|
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-04 04:26:56 +00:00
|
|
|
Services.obs.addObserver(this, kMozSettingsChangedObserverTopic, false);
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
this.wantScanResults = [];
|
|
|
|
|
2012-12-21 21:57:19 +00:00
|
|
|
this._allowWpaEap = false;
|
2012-04-09 07:44:53 +00:00
|
|
|
this._needToEnableNetworks = false;
|
|
|
|
this._highestPriority = -1;
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// Networks is a map from SSID -> a scan result.
|
2012-04-09 07:44:53 +00:00
|
|
|
this.networks = Object.create(null);
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// ConfiguredNetworks is a map from SSID -> our view of a network. It only
|
2012-04-09 07:44:53 +00:00
|
|
|
// lists networks known to the wpa_supplicant. The SSID field (and other
|
|
|
|
// fields) are quoted for ease of use with WifiManager commands.
|
|
|
|
// Note that we don't have to worry about escaping embedded quotes since in
|
|
|
|
// all cases, the supplicant will take the last quotation that we pass it as
|
|
|
|
// the end of the string.
|
|
|
|
this.configuredNetworks = Object.create(null);
|
2012-12-17 23:39:30 +00:00
|
|
|
this._addingNetworks = Object.create(null);
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
this.currentNetwork = null;
|
2012-09-27 18:14:06 +00:00
|
|
|
this.ipAddress = "";
|
2013-07-26 02:05:50 +00:00
|
|
|
this.macAddress = null;
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
this._lastConnectionInfo = null;
|
|
|
|
this._connectionInfoTimer = null;
|
|
|
|
this._reconnectOnDisconnect = false;
|
|
|
|
|
2013-06-17 04:20:39 +00:00
|
|
|
// Users of instances of nsITimer should keep a reference to the timer until
|
2013-05-16 11:39:58 +00:00
|
|
|
// it is no longer needed in order to assure the timer is fired.
|
|
|
|
this._callbackTimer = null;
|
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
// XXX On some phones (Otoro and Unagi) the wifi driver doesn't play nicely
|
|
|
|
// with the automatic scans that wpa_supplicant does (it appears that the
|
|
|
|
// driver forgets that it's returned scan results and then refuses to try to
|
|
|
|
// rescan. In order to detect this case we start a timer when we enter the
|
|
|
|
// SCANNING state and reset it whenever we either get scan results or leave
|
|
|
|
// the SCANNING state. If the timer fires, we assume that we are stuck and
|
|
|
|
// forceably try to unstick the supplican, also turning on background
|
|
|
|
// scanning to avoid having to constantly poke the supplicant.
|
|
|
|
|
|
|
|
// How long we wait is controlled by the SCAN_STUCK_WAIT constant.
|
|
|
|
const SCAN_STUCK_WAIT = 12000;
|
|
|
|
this._scanStuckTimer = null;
|
|
|
|
this._turnOnBackgroundScan = false;
|
|
|
|
|
|
|
|
function startScanStuckTimer() {
|
2012-11-22 10:29:24 +00:00
|
|
|
if (WifiManager.schedScanRecovery) {
|
|
|
|
self._scanStuckTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
self._scanStuckTimer.initWithCallback(scanIsStuck, SCAN_STUCK_WAIT,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
2012-11-05 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function scanIsStuck() {
|
|
|
|
// Uh-oh, we've waited too long for scan results. Disconnect (which
|
|
|
|
// guarantees that we leave the SCANNING state and tells wpa_supplicant to
|
|
|
|
// wait for our next command) ensure that background scanning is on and
|
|
|
|
// then try again.
|
|
|
|
debug("Determined that scanning is stuck, turning on background scanning!");
|
|
|
|
WifiManager.disconnect(function(ok) {});
|
|
|
|
self._turnOnBackgroundScan = true;
|
|
|
|
}
|
|
|
|
|
2012-06-28 10:01:51 +00:00
|
|
|
// A list of requests to turn wifi on or off.
|
|
|
|
this._stateRequests = [];
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// Given a connection status network, takes a network from
|
|
|
|
// self.configuredNetworks and prepares it for the DOM.
|
|
|
|
netToDOM = function(net) {
|
2012-09-27 18:18:34 +00:00
|
|
|
var ssid = dequote(net.ssid);
|
2013-07-08 11:41:48 +00:00
|
|
|
var security = (net.key_mgmt === "NONE" && net.wep_key0) ? ["WEP"] :
|
|
|
|
(net.key_mgmt && net.key_mgmt !== "NONE") ? [net.key_mgmt] :
|
|
|
|
[];
|
2012-09-27 18:18:34 +00:00
|
|
|
var password;
|
|
|
|
if (("psk" in net && net.psk) ||
|
|
|
|
("password" in net && net.password) ||
|
|
|
|
("wep_key0" in net && net.wep_key0)) {
|
|
|
|
password = "*";
|
|
|
|
}
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
var pub = new Network(ssid, security, password);
|
2012-09-27 18:18:34 +00:00
|
|
|
if (net.identity)
|
|
|
|
pub.identity = dequote(net.identity);
|
2012-04-09 07:44:53 +00:00
|
|
|
if (net.netId)
|
|
|
|
pub.known = true;
|
2012-09-27 18:41:31 +00:00
|
|
|
if (net.scan_ssid === 1)
|
|
|
|
pub.hidden = true;
|
2012-04-09 07:44:53 +00:00
|
|
|
return pub;
|
|
|
|
};
|
|
|
|
|
|
|
|
netFromDOM = function(net, configured) {
|
|
|
|
// Takes a network from the DOM and makes it suitable for insertion into
|
|
|
|
// self.configuredNetworks (that is calling addNetwork will do the right
|
|
|
|
// thing).
|
|
|
|
// NB: Modifies net in place: safe since we don't share objects between
|
|
|
|
// the dom and the chrome code.
|
|
|
|
|
|
|
|
// Things that are useful for the UI but not to us.
|
|
|
|
delete net.bssid;
|
2012-06-01 21:09:59 +00:00
|
|
|
delete net.signalStrength;
|
|
|
|
delete net.relSignalStrength;
|
2013-07-08 11:41:48 +00:00
|
|
|
delete net.security;
|
2012-04-09 07:44:53 +00:00
|
|
|
delete net.capabilities;
|
|
|
|
|
|
|
|
if (!configured)
|
|
|
|
configured = {};
|
|
|
|
|
|
|
|
net.ssid = quote(net.ssid);
|
|
|
|
|
|
|
|
let wep = false;
|
|
|
|
if ("keyManagement" in net) {
|
|
|
|
if (net.keyManagement === "WEP") {
|
|
|
|
wep = true;
|
|
|
|
net.keyManagement = "NONE";
|
|
|
|
}
|
|
|
|
|
|
|
|
configured.key_mgmt = net.key_mgmt = net.keyManagement; // WPA2-PSK, WPA-PSK, etc.
|
|
|
|
delete net.keyManagement;
|
|
|
|
} else {
|
|
|
|
configured.key_mgmt = net.key_mgmt = "NONE";
|
|
|
|
}
|
|
|
|
|
2012-09-27 18:41:31 +00:00
|
|
|
if (net.hidden) {
|
|
|
|
configured.scan_ssid = net.scan_ssid = 1;
|
|
|
|
delete net.hidden;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
function checkAssign(name, checkStar) {
|
|
|
|
if (name in net) {
|
|
|
|
let value = net[name];
|
|
|
|
if (!value || (checkStar && value === '*')) {
|
|
|
|
if (name in configured)
|
|
|
|
net[name] = configured[name];
|
|
|
|
else
|
|
|
|
delete net[name];
|
|
|
|
} else {
|
|
|
|
configured[name] = net[name] = quote(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkAssign("psk", true);
|
|
|
|
checkAssign("identity", false);
|
|
|
|
checkAssign("password", true);
|
|
|
|
if (wep && net.wep && net.wep != '*') {
|
|
|
|
configured.wep_key0 = net.wep_key0 = isWepHexKey(net.wep) ? net.wep : quote(net.wep);
|
|
|
|
configured.auth_alg = net.auth_alg = "OPEN SHARED";
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:25:40 +00:00
|
|
|
if ("pin" in net) {
|
|
|
|
net.pin = quote(net.pin);
|
|
|
|
net.pcsc = quote("");
|
|
|
|
}
|
|
|
|
|
2013-05-30 03:17:07 +00:00
|
|
|
if ("phase1" in net)
|
|
|
|
net.phase1 = quote(net.phase1);
|
|
|
|
|
|
|
|
if ("phase2" in net)
|
|
|
|
net.phase2 = quote(net.phase2);
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
return net;
|
|
|
|
};
|
|
|
|
|
|
|
|
WifiManager.onsupplicantconnection = function() {
|
|
|
|
debug("Connected to supplicant");
|
2012-06-28 10:01:51 +00:00
|
|
|
WifiManager.enabled = true;
|
2012-04-09 07:44:53 +00:00
|
|
|
self._reloadConfiguredNetworks(function(ok) {
|
|
|
|
// Prime this.networks.
|
|
|
|
if (!ok)
|
|
|
|
return;
|
2012-11-21 08:19:08 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
self.waitForScan(function firstScan() {});
|
2012-11-21 08:19:08 +00:00
|
|
|
// The select network command we used in associate() disables others networks.
|
|
|
|
// Enable them here to make sure wpa_supplicant helps to connect to known
|
|
|
|
// network automatically.
|
|
|
|
self._enableAllNetworks();
|
|
|
|
WifiManager.saveConfig(function() {})
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
2012-06-28 10:01:51 +00:00
|
|
|
|
2012-12-21 21:57:19 +00:00
|
|
|
try {
|
|
|
|
self._allowWpaEap = Services.prefs.getBoolPref("b2g.wifi.allow_unsafe_wpa_eap");
|
|
|
|
} catch (e) {
|
|
|
|
self._allowWpaEap = false;
|
|
|
|
}
|
|
|
|
|
2012-06-28 10:01:51 +00:00
|
|
|
// Notify everybody, even if they didn't ask us to come up.
|
2013-03-15 16:58:41 +00:00
|
|
|
WifiManager.getMacAddress(function (mac) {
|
|
|
|
self.macAddress = mac;
|
|
|
|
debug("Got mac: " + mac);
|
|
|
|
self._fireEvent("wifiUp", { macAddress: mac });
|
|
|
|
});
|
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
if (WifiManager.state === "SCANNING")
|
|
|
|
startScanStuckTimer();
|
2012-09-04 04:26:56 +00:00
|
|
|
};
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
WifiManager.onsupplicantlost = function() {
|
2012-06-28 10:01:51 +00:00
|
|
|
WifiManager.enabled = WifiManager.supplicantStarted = false;
|
2012-08-15 00:00:43 +00:00
|
|
|
WifiManager.state = "UNINITIALIZED";
|
2012-04-09 07:44:53 +00:00
|
|
|
debug("Supplicant died!");
|
2012-06-28 10:01:51 +00:00
|
|
|
|
|
|
|
// Notify everybody, even if they didn't ask us to come up.
|
|
|
|
self._fireEvent("wifiDown", {});
|
2012-09-04 04:26:56 +00:00
|
|
|
};
|
|
|
|
|
2012-08-28 19:14:26 +00:00
|
|
|
WifiManager.onpasswordmaybeincorrect = function() {
|
|
|
|
WifiManager.authenticationFailuresCount++;
|
2012-09-04 04:26:56 +00:00
|
|
|
};
|
|
|
|
|
2012-08-28 19:14:26 +00:00
|
|
|
WifiManager.ondisconnected = function() {
|
2012-11-21 08:19:08 +00:00
|
|
|
// We may fail to establish the connection, re-enable the
|
|
|
|
// rest of our networks.
|
|
|
|
if (self._needToEnableNetworks) {
|
|
|
|
self._enableAllNetworks();
|
|
|
|
self._needToEnableNetworks = false;
|
|
|
|
}
|
|
|
|
|
2012-11-23 10:30:33 +00:00
|
|
|
WifiManager.getCurrentNetworkId(this.ssid, function(netId) {
|
|
|
|
// Trying to get netId from current network.
|
|
|
|
if (!netId &&
|
|
|
|
self.currentNetwork &&
|
|
|
|
typeof self.currentNetwork.netId !== "undefined") {
|
|
|
|
netId = self.currentNetwork.netId;
|
|
|
|
}
|
|
|
|
if (netId) {
|
|
|
|
WifiManager.disableNetwork(netId, function() {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self._fireEvent("onconnectingfailed", {network: self.currentNetwork});
|
|
|
|
}
|
2012-08-28 19:14:26 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
WifiManager.onstatechange = function() {
|
|
|
|
debug("State change: " + this.prevState + " -> " + this.state);
|
|
|
|
|
|
|
|
if (self._connectionInfoTimer &&
|
|
|
|
this.state !== "CONNECTED" &&
|
|
|
|
this.state !== "COMPLETED") {
|
|
|
|
self._stopConnectionInfoTimer();
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
if (this.state !== "SCANNING" &&
|
|
|
|
self._scanStuckTimer) {
|
|
|
|
self._scanStuckTimer.cancel();
|
|
|
|
self._scanStuckTimer = null;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
switch (this.state) {
|
|
|
|
case "DORMANT":
|
|
|
|
// The dormant state is a bad state to be in since we won't
|
|
|
|
// automatically connect. Try to knock us out of it. We only
|
|
|
|
// hit this state when we've failed to run DHCP, so trying
|
|
|
|
// again isn't the worst thing we can do. Eventually, we'll
|
|
|
|
// need to detect if we're looping in this state and bail out.
|
|
|
|
WifiManager.reconnect(function(){});
|
|
|
|
break;
|
|
|
|
case "ASSOCIATING":
|
|
|
|
// id has not yet been filled in, so we can only report the ssid and
|
|
|
|
// bssid.
|
|
|
|
self.currentNetwork =
|
|
|
|
{ bssid: WifiManager.connectionInfo.bssid,
|
|
|
|
ssid: quote(WifiManager.connectionInfo.ssid) };
|
|
|
|
self._fireEvent("onconnecting", { network: netToDOM(self.currentNetwork) });
|
|
|
|
break;
|
|
|
|
case "ASSOCIATED":
|
2012-04-09 04:13:28 +00:00
|
|
|
if (!self.currentNetwork) {
|
|
|
|
self.currentNetwork =
|
|
|
|
{ bssid: WifiManager.connectionInfo.bssid,
|
|
|
|
ssid: quote(WifiManager.connectionInfo.ssid) };
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
self.currentNetwork.netId = this.id;
|
|
|
|
WifiManager.getNetworkConfiguration(self.currentNetwork, function (){});
|
|
|
|
break;
|
|
|
|
case "COMPLETED":
|
|
|
|
// Now that we've successfully completed the connection, re-enable the
|
|
|
|
// rest of our networks.
|
|
|
|
// XXX Need to do this eventually if the user entered an incorrect
|
|
|
|
// password. For now, we require user interaction to break the loop and
|
|
|
|
// select a better network!
|
|
|
|
if (self._needToEnableNetworks) {
|
|
|
|
self._enableAllNetworks();
|
|
|
|
self._needToEnableNetworks = false;
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
// We get the ASSOCIATED event when we've associated but not connected, so
|
|
|
|
// wait until the handshake is complete.
|
2013-05-16 11:39:58 +00:00
|
|
|
if (this.fromStatus || !self.currentNetwork) {
|
2012-04-09 07:47:42 +00:00
|
|
|
// In this case, we connected to an already-connected wpa_supplicant,
|
|
|
|
// because of that we need to gather information about the current
|
|
|
|
// network here.
|
|
|
|
self.currentNetwork = { ssid: quote(WifiManager.connectionInfo.ssid),
|
2012-04-09 04:13:28 +00:00
|
|
|
netId: WifiManager.connectionInfo.id };
|
2012-04-09 07:47:42 +00:00
|
|
|
WifiManager.getNetworkConfiguration(self.currentNetwork, function(){});
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2013-08-22 10:22:25 +00:00
|
|
|
// Update http proxy when connected to network.
|
|
|
|
let netConnect = WifiManager.getHttpProxyNetwork(self.currentNetwork);
|
|
|
|
if (netConnect)
|
|
|
|
WifiManager.setHttpProxy(netConnect);
|
|
|
|
|
2012-08-28 19:14:26 +00:00
|
|
|
// The full authentication process is completed, reset the count.
|
|
|
|
WifiManager.authenticationFailuresCount = 0;
|
|
|
|
WifiManager.loopDetectionCount = 0;
|
2012-04-09 07:47:42 +00:00
|
|
|
self._startConnectionInfoTimer();
|
|
|
|
self._fireEvent("onassociate", { network: netToDOM(self.currentNetwork) });
|
|
|
|
break;
|
|
|
|
case "CONNECTED":
|
2012-11-12 02:34:49 +00:00
|
|
|
// BSSID is read after connected, update it.
|
|
|
|
self.currentNetwork.bssid = WifiManager.connectionInfo.bssid;
|
2012-04-09 07:47:42 +00:00
|
|
|
break;
|
|
|
|
case "DISCONNECTED":
|
2013-07-29 09:59:53 +00:00
|
|
|
// wpa_supplicant may give us a "DISCONNECTED" event even if
|
|
|
|
// we are already in "DISCONNECTED" state.
|
|
|
|
if (this.prevState === "INITIALIZING" ||
|
|
|
|
this.prevState === "DISCONNECTED" ||
|
|
|
|
this.prevState === "INTERFACE_DISABLED" ||
|
|
|
|
this.prevState === "INACTIVE" ||
|
|
|
|
this.prevState === "UNINITIALIZED") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
self._fireEvent("ondisconnect", {});
|
2013-08-22 10:22:25 +00:00
|
|
|
|
|
|
|
// When disconnected, clear the http proxy setting if it exists.
|
|
|
|
// Temporarily set http proxy to empty and restore user setting after setHttpProxy.
|
|
|
|
let netDisconnect = WifiManager.getHttpProxyNetwork(self.currentNetwork);
|
|
|
|
if (netDisconnect) {
|
|
|
|
let prehttpProxyHostSetting = netDisconnect.httpProxyHost;
|
|
|
|
let prehttpProxyPortSetting = netDisconnect.httpProxyPort;
|
|
|
|
|
|
|
|
netDisconnect.httpProxyHost = "";
|
|
|
|
netDisconnect.httpProxyPort = 0;
|
|
|
|
|
|
|
|
WifiManager.setHttpProxy(netDisconnect);
|
|
|
|
|
|
|
|
netDisconnect.httpProxyHost = prehttpProxyHostSetting;
|
|
|
|
netDisconnect.httpProxyPort = prehttpProxyPortSetting;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
self.currentNetwork = null;
|
2012-09-27 18:14:06 +00:00
|
|
|
self.ipAddress = "";
|
2012-04-09 07:47:42 +00:00
|
|
|
|
2012-11-05 22:29:07 +00:00
|
|
|
if (self._turnOnBackgroundScan) {
|
|
|
|
self._turnOnBackgroundScan = false;
|
|
|
|
WifiManager.setBackgroundScan("ON", function(did_something, ok) {
|
|
|
|
WifiManager.reassociate(function() {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 04:13:28 +00:00
|
|
|
WifiManager.connectionDropped(function() {
|
|
|
|
// We've disconnected from a network because of a call to forgetNetwork.
|
|
|
|
// Reconnect to the next available network (if any).
|
|
|
|
if (self._reconnectOnDisconnect) {
|
|
|
|
self._reconnectOnDisconnect = false;
|
|
|
|
WifiManager.reconnect(function(){});
|
|
|
|
}
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-04-19 21:33:36 +00:00
|
|
|
WifiNetworkInterface.state =
|
|
|
|
Ci.nsINetworkInterface.NETWORK_STATE_DISCONNECTED;
|
2012-08-14 01:54:42 +00:00
|
|
|
WifiNetworkInterface.ip = null;
|
|
|
|
WifiNetworkInterface.netmask = null;
|
|
|
|
WifiNetworkInterface.broadcast = null;
|
|
|
|
WifiNetworkInterface.gateway = null;
|
|
|
|
WifiNetworkInterface.dns1 = null;
|
|
|
|
WifiNetworkInterface.dns2 = null;
|
2012-04-19 21:33:36 +00:00
|
|
|
Services.obs.notifyObservers(WifiNetworkInterface,
|
|
|
|
kNetworkInterfaceStateChangedTopic,
|
|
|
|
null);
|
|
|
|
|
2012-04-09 07:47:42 +00:00
|
|
|
break;
|
2012-08-16 22:19:51 +00:00
|
|
|
case "WPS_TIMEOUT":
|
|
|
|
self._fireEvent("onwpstimeout", {});
|
|
|
|
break;
|
|
|
|
case "WPS_FAIL":
|
|
|
|
self._fireEvent("onwpsfail", {});
|
|
|
|
break;
|
|
|
|
case "WPS_OVERLAP_DETECTED":
|
|
|
|
self._fireEvent("onwpsoverlap", {});
|
|
|
|
break;
|
2012-11-05 22:29:07 +00:00
|
|
|
case "SCANNING":
|
|
|
|
// If we're already scanning in the background, we don't need to worry
|
|
|
|
// about getting stuck while scanning.
|
|
|
|
if (!WifiManager.backgroundScanEnabled && WifiManager.enabled)
|
|
|
|
startScanStuckTimer();
|
|
|
|
break;
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
WifiManager.onnetworkconnected = function() {
|
2012-04-19 21:33:36 +00:00
|
|
|
if (this.info) {
|
|
|
|
WifiNetworkInterface.state =
|
|
|
|
Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED;
|
2012-08-14 01:54:42 +00:00
|
|
|
WifiNetworkInterface.ip = this.info.ipaddr_str;
|
|
|
|
WifiNetworkInterface.netmask = this.info.mask_str;
|
|
|
|
WifiNetworkInterface.broadcast = this.info.broadcast_str;
|
|
|
|
WifiNetworkInterface.gateway = this.info.gateway_str;
|
|
|
|
WifiNetworkInterface.dns1 = this.info.dns1_str;
|
|
|
|
WifiNetworkInterface.dns2 = this.info.dns2_str;
|
2012-04-19 21:33:36 +00:00
|
|
|
Services.obs.notifyObservers(WifiNetworkInterface,
|
|
|
|
kNetworkInterfaceStateChangedTopic,
|
|
|
|
null);
|
|
|
|
|
2012-09-27 18:14:06 +00:00
|
|
|
self.ipAddress = this.info.ipaddr_str;
|
|
|
|
|
|
|
|
// We start the connection information timer when we associate, but
|
|
|
|
// don't have our IP address until here. Make sure that we fire a new
|
|
|
|
// connectionInformation event with the IP address the next time the
|
|
|
|
// timer fires.
|
|
|
|
self._lastConnectionInfo = null;
|
2012-04-09 07:44:53 +00:00
|
|
|
self._fireEvent("onconnect", { network: netToDOM(self.currentNetwork) });
|
2012-04-19 21:33:36 +00:00
|
|
|
} else {
|
2012-12-06 21:27:14 +00:00
|
|
|
// NB: We have to call disconnect first. Otherwise, we only reauth with
|
|
|
|
// the existing AP and don't retrigger DHCP.
|
|
|
|
WifiManager.disconnect(function() {
|
|
|
|
WifiManager.reassociate(function(){});
|
|
|
|
});
|
2012-04-19 21:33:36 +00:00
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WifiManager.onscanresultsavailable = function() {
|
2012-11-05 22:29:07 +00:00
|
|
|
if (self._scanStuckTimer) {
|
|
|
|
// We got scan results! We must not be stuck for now, try again.
|
|
|
|
self._scanStuckTimer.cancel();
|
|
|
|
self._scanStuckTimer.initWithCallback(scanIsStuck, SCAN_STUCK_WAIT,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
if (self.wantScanResults.length === 0) {
|
|
|
|
debug("Scan results available, but we don't need them");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Scan results are available! Asking for them.");
|
|
|
|
WifiManager.getScanResults(function(r) {
|
2012-04-06 15:56:15 +00:00
|
|
|
// Failure.
|
|
|
|
if (!r) {
|
|
|
|
self.wantScanResults.forEach(function(callback) { callback(null) });
|
|
|
|
self.wantScanResults = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// Now that we have scan results, there's no more need to continue
|
|
|
|
// scanning. Ignore any errors from this command.
|
|
|
|
WifiManager.setScanMode("inactive", function() {});
|
|
|
|
let lines = r.split("\n");
|
|
|
|
// NB: Skip the header line.
|
2012-11-08 05:34:12 +00:00
|
|
|
self.networksArray = [];
|
2012-04-09 07:44:53 +00:00
|
|
|
for (let i = 1; i < lines.length; ++i) {
|
|
|
|
// bssid / frequency / signal level / flags / ssid
|
2013-09-30 11:19:25 +00:00
|
|
|
var match = /([\S]+)\s+([\S]+)\s+([\S]+)\s+(\[[\S]+\])?\s(.*)/.exec(lines[i]);
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
if (match && match[5]) {
|
2012-11-08 05:34:12 +00:00
|
|
|
let ssid = match[5],
|
|
|
|
bssid = match[1],
|
|
|
|
signalLevel = match[3],
|
|
|
|
flags = match[4];
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-12-14 01:16:18 +00:00
|
|
|
// Skip ad-hoc networks which aren't supported (bug 811635).
|
|
|
|
if (flags.indexOf("[IBSS]") >= 0)
|
|
|
|
continue;
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// If this is the first time that we've seen this SSID in the scan
|
|
|
|
// results, add it to the list along with any other information.
|
|
|
|
// Also, we use the highest signal strength that we see.
|
2012-11-08 05:34:12 +00:00
|
|
|
let network = new ScanResult(ssid, bssid, flags, signalLevel);
|
|
|
|
|
|
|
|
let networkKey = getNetworkKey(network);
|
2012-12-21 21:57:05 +00:00
|
|
|
let eapIndex = -1;
|
2012-11-08 05:34:12 +00:00
|
|
|
if (networkKey in self.configuredNetworks) {
|
|
|
|
let known = self.configuredNetworks[networkKey];
|
|
|
|
network.known = true;
|
|
|
|
|
|
|
|
if ("identity" in known && known.identity)
|
|
|
|
network.identity = dequote(known.identity);
|
|
|
|
|
|
|
|
// Note: we don't hand out passwords here! The * marks that there
|
|
|
|
// is a password that we're hiding.
|
|
|
|
if (("psk" in known && known.psk) ||
|
|
|
|
("password" in known && known.password) ||
|
|
|
|
("wep_key0" in known && known.wep_key0)) {
|
|
|
|
network.password = "*";
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
2012-12-21 21:57:19 +00:00
|
|
|
} else if (!self._allowWpaEap &&
|
2013-07-08 11:41:48 +00:00
|
|
|
(eapIndex = network.security.indexOf("WPA-EAP")) >= 0) {
|
2012-12-21 21:57:05 +00:00
|
|
|
// Don't offer to connect to WPA-EAP networks unless one has been
|
|
|
|
// configured through other means (e.g. it was added directly to
|
|
|
|
// wpa_supplicant.conf). Here, we have an unknown WPA-EAP network,
|
|
|
|
// so we ignore it entirely if it only supports WPA-EAP, otherwise
|
|
|
|
// we take EAP out of the list and offer the rest of the
|
2013-07-08 11:41:48 +00:00
|
|
|
// security.
|
|
|
|
if (network.security.length === 1)
|
2012-12-21 21:57:05 +00:00
|
|
|
continue;
|
|
|
|
|
2013-07-08 11:41:48 +00:00
|
|
|
network.security.splice(eapIndex, 1);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 21:57:05 +00:00
|
|
|
self.networksArray.push(network);
|
2012-04-09 07:44:53 +00:00
|
|
|
if (network.bssid === WifiManager.connectionInfo.bssid)
|
|
|
|
network.connected = true;
|
|
|
|
|
|
|
|
let signal = calculateSignal(Number(match[3]));
|
2012-06-01 21:09:59 +00:00
|
|
|
if (signal > network.relSignalStrength)
|
|
|
|
network.relSignalStrength = signal;
|
2012-04-09 07:44:53 +00:00
|
|
|
} else if (!match) {
|
|
|
|
debug("Match didn't find anything for: " + lines[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:34:12 +00:00
|
|
|
self.wantScanResults.forEach(function(callback) { callback(self.networksArray) });
|
2012-04-09 07:44:53 +00:00
|
|
|
self.wantScanResults = [];
|
|
|
|
});
|
2012-09-04 04:26:56 +00:00
|
|
|
};
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-09-04 04:26:56 +00:00
|
|
|
// Read the 'wifi.enabled' setting in order to start with a known
|
|
|
|
// value at boot time. The handle() will be called after reading.
|
|
|
|
//
|
2013-06-24 03:02:30 +00:00
|
|
|
// nsISettingsServiceCallback implementation.
|
2012-09-04 04:26:56 +00:00
|
|
|
var initWifiEnabledCb = {
|
|
|
|
handle: function handle(aName, aResult) {
|
2013-05-16 11:39:58 +00:00
|
|
|
if (aName !== SETTINGS_WIFI_ENABLED)
|
2012-09-04 04:26:56 +00:00
|
|
|
return;
|
|
|
|
if (aResult === null)
|
|
|
|
aResult = true;
|
2013-05-16 11:39:58 +00:00
|
|
|
self.handleWifiEnabled(aResult);
|
2012-09-04 04:26:56 +00:00
|
|
|
},
|
|
|
|
handleError: function handleError(aErrorMessage) {
|
|
|
|
debug("Error reading the 'wifi.enabled' setting. Default to wifi on.");
|
2013-05-16 11:39:58 +00:00
|
|
|
self.handleWifiEnabled(true);
|
2012-10-29 19:26:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var initWifiDebuggingEnabledCb = {
|
|
|
|
handle: function handle(aName, aResult) {
|
2013-05-16 11:39:58 +00:00
|
|
|
if (aName !== SETTINGS_WIFI_DEBUG_ENABLED)
|
2012-10-29 19:26:55 +00:00
|
|
|
return;
|
|
|
|
if (aResult === null)
|
|
|
|
aResult = false;
|
|
|
|
DEBUG = aResult;
|
|
|
|
updateDebug();
|
2012-09-04 04:26:56 +00:00
|
|
|
},
|
2012-10-29 19:26:55 +00:00
|
|
|
handleError: function handleError(aErrorMessage) {
|
|
|
|
debug("Error reading the 'wifi.debugging.enabled' setting. Default to debugging off.");
|
|
|
|
DEBUG = false;
|
|
|
|
updateDebug();
|
|
|
|
}
|
2012-09-04 04:26:56 +00:00
|
|
|
};
|
2012-10-29 19:26:55 +00:00
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
this.initTetheringSettings();
|
|
|
|
|
2012-10-29 19:26:55 +00:00
|
|
|
let lock = gSettingsService.createLock();
|
2013-05-16 11:39:58 +00:00
|
|
|
lock.get(SETTINGS_WIFI_ENABLED, initWifiEnabledCb);
|
|
|
|
lock.get(SETTINGS_WIFI_DEBUG_ENABLED, initWifiDebuggingEnabledCb);
|
|
|
|
|
|
|
|
lock.get(SETTINGS_WIFI_SSID, this);
|
|
|
|
lock.get(SETTINGS_WIFI_SECURITY_TYPE, this);
|
|
|
|
lock.get(SETTINGS_WIFI_SECURITY_PASSWORD, this);
|
|
|
|
lock.get(SETTINGS_WIFI_IP, this);
|
|
|
|
lock.get(SETTINGS_WIFI_PREFIX, this);
|
|
|
|
lock.get(SETTINGS_WIFI_DHCPSERVER_STARTIP, this);
|
|
|
|
lock.get(SETTINGS_WIFI_DHCPSERVER_ENDIP, this);
|
|
|
|
lock.get(SETTINGS_WIFI_DNS1, this);
|
|
|
|
lock.get(SETTINGS_WIFI_DNS2, this);
|
|
|
|
lock.get(SETTINGS_WIFI_TETHERING_ENABLED, this);
|
|
|
|
|
2013-06-19 10:57:58 +00:00
|
|
|
lock.get(SETTINGS_USB_DHCPSERVER_STARTIP, this);
|
|
|
|
lock.get(SETTINGS_USB_DHCPSERVER_ENDIP, this);
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
this._wifiTetheringSettingsToRead = [SETTINGS_WIFI_SSID,
|
|
|
|
SETTINGS_WIFI_SECURITY_TYPE,
|
|
|
|
SETTINGS_WIFI_SECURITY_PASSWORD,
|
|
|
|
SETTINGS_WIFI_IP,
|
|
|
|
SETTINGS_WIFI_PREFIX,
|
|
|
|
SETTINGS_WIFI_DHCPSERVER_STARTIP,
|
|
|
|
SETTINGS_WIFI_DHCPSERVER_ENDIP,
|
|
|
|
SETTINGS_WIFI_DNS1,
|
|
|
|
SETTINGS_WIFI_DNS2,
|
2013-06-19 10:57:58 +00:00
|
|
|
SETTINGS_WIFI_TETHERING_ENABLED,
|
|
|
|
SETTINGS_USB_DHCPSERVER_STARTIP,
|
|
|
|
SETTINGS_USB_DHCPSERVER_ENDIP];
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 03:32:17 +00:00
|
|
|
function translateState(state) {
|
|
|
|
switch (state) {
|
|
|
|
case "INTERFACE_DISABLED":
|
|
|
|
case "INACTIVE":
|
|
|
|
case "SCANNING":
|
|
|
|
case "DISCONNECTED":
|
|
|
|
default:
|
|
|
|
return "disconnected";
|
|
|
|
|
|
|
|
case "AUTHENTICATING":
|
|
|
|
case "ASSOCIATING":
|
|
|
|
case "ASSOCIATED":
|
|
|
|
case "FOUR_WAY_HANDSHAKE":
|
|
|
|
case "GROUP_HANDSHAKE":
|
|
|
|
return "connecting";
|
|
|
|
|
|
|
|
case "COMPLETED":
|
|
|
|
return WifiManager.getDhcpInfo() ? "connected" : "associated";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
WifiWorker.prototype = {
|
|
|
|
classID: WIFIWORKER_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: WIFIWORKER_CID,
|
|
|
|
contractID: WIFIWORKER_CONTRACTID,
|
|
|
|
classDescription: "WifiWorker",
|
|
|
|
interfaces: [Ci.nsIWorkerHolder,
|
2012-09-04 04:26:56 +00:00
|
|
|
Ci.nsIWifi,
|
|
|
|
Ci.nsIObserver]}),
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWorkerHolder,
|
2013-05-16 11:39:58 +00:00
|
|
|
Ci.nsIWifi,
|
|
|
|
Ci.nsISettingsServiceCallback]),
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2013-04-02 07:30:32 +00:00
|
|
|
disconnectedByWifi: false,
|
|
|
|
|
|
|
|
disconnectedByWifiTethering: false,
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
_wifiTetheringSettingsToRead: [],
|
|
|
|
|
|
|
|
_oldWifiTetheringEnabledState: null,
|
|
|
|
|
|
|
|
tetheringSettings: {},
|
|
|
|
|
|
|
|
initTetheringSettings: function initTetheringSettings() {
|
2013-06-17 04:20:39 +00:00
|
|
|
this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] = null;
|
2013-05-16 11:39:58 +00:00
|
|
|
this.tetheringSettings[SETTINGS_WIFI_SSID] = DEFAULT_WIFI_SSID;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_SECURITY_TYPE] = DEFAULT_WIFI_SECURITY_TYPE;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_SECURITY_PASSWORD] = DEFAULT_WIFI_SECURITY_PASSWORD;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_IP] = DEFAULT_WIFI_IP;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_PREFIX] = DEFAULT_WIFI_PREFIX;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_STARTIP] = DEFAULT_WIFI_DHCPSERVER_STARTIP;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_ENDIP] = DEFAULT_WIFI_DHCPSERVER_ENDIP;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DNS1] = DEFAULT_DNS1;
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_DNS2] = DEFAULT_DNS2;
|
2013-06-19 10:57:58 +00:00
|
|
|
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DHCPSERVER_STARTIP] = DEFAULT_USB_DHCPSERVER_STARTIP;
|
|
|
|
this.tetheringSettings[SETTINGS_USB_DHCPSERVER_ENDIP] = DEFAULT_USB_DHCPSERVER_ENDIP;
|
2013-05-16 11:39:58 +00:00
|
|
|
},
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// Internal methods.
|
|
|
|
waitForScan: function(callback) {
|
|
|
|
this.wantScanResults.push(callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
// In order to select a specific network, we disable the rest of the
|
|
|
|
// networks known to us. However, in general, we want the supplicant to
|
|
|
|
// connect to which ever network it thinks is best, so when we select the
|
|
|
|
// proper network (or fail to), we need to re-enable the rest.
|
|
|
|
_enableAllNetworks: function() {
|
|
|
|
for each (let net in this.configuredNetworks) {
|
|
|
|
WifiManager.enableNetwork(net.netId, false, function(ok) {
|
|
|
|
net.disabled = ok ? 1 : 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_startConnectionInfoTimer: function() {
|
|
|
|
if (this._connectionInfoTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
function getConnectionInformation() {
|
2012-12-20 15:56:32 +00:00
|
|
|
WifiManager.getConnectionInfo(function(connInfo) {
|
2012-04-09 07:44:53 +00:00
|
|
|
// See comments in calculateSignal for information about this.
|
2012-12-20 15:56:32 +00:00
|
|
|
if (!connInfo) {
|
2012-07-31 21:27:46 +00:00
|
|
|
self._lastConnectionInfo = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-20 15:56:32 +00:00
|
|
|
let { rssi, linkspeed } = connInfo;
|
2012-04-09 07:44:53 +00:00
|
|
|
if (rssi > 0)
|
|
|
|
rssi -= 256;
|
|
|
|
if (rssi <= MIN_RSSI)
|
|
|
|
rssi = MIN_RSSI;
|
|
|
|
else if (rssi >= MAX_RSSI)
|
|
|
|
rssi = MAX_RSSI;
|
|
|
|
|
2012-07-13 01:56:39 +00:00
|
|
|
let info = { signalStrength: rssi,
|
|
|
|
relSignalStrength: calculateSignal(rssi),
|
2012-09-27 18:14:06 +00:00
|
|
|
linkSpeed: linkspeed,
|
|
|
|
ipAddress: self.ipAddress };
|
2012-07-13 01:56:39 +00:00
|
|
|
let last = self._lastConnectionInfo;
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-07-13 01:56:39 +00:00
|
|
|
// Only fire the event if the link speed changed or the signal
|
|
|
|
// strength changed by more than 10%.
|
|
|
|
function tensPlace(percent) ((percent / 10) | 0)
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-07-13 01:56:39 +00:00
|
|
|
if (last && last.linkSpeed === info.linkSpeed &&
|
|
|
|
tensPlace(last.relSignalStrength) === tensPlace(info.relSignalStrength)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2012-07-13 01:56:39 +00:00
|
|
|
self._lastConnectionInfo = info;
|
|
|
|
debug("Firing connectionInfoUpdate: " + uneval(info));
|
|
|
|
self._fireEvent("connectionInfoUpdate", info);
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prime our _lastConnectionInfo immediately and fire the event at the
|
|
|
|
// same time.
|
|
|
|
getConnectionInformation();
|
|
|
|
|
|
|
|
// Now, set up the timer for regular updates.
|
|
|
|
this._connectionInfoTimer =
|
|
|
|
Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this._connectionInfoTimer.init(getConnectionInformation, 5000,
|
|
|
|
Ci.nsITimer.TYPE_REPEATING_SLACK);
|
|
|
|
},
|
|
|
|
|
|
|
|
_stopConnectionInfoTimer: function() {
|
|
|
|
if (!this._connectionInfoTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._connectionInfoTimer.cancel();
|
|
|
|
this._connectionInfoTimer = null;
|
|
|
|
this._lastConnectionInfo = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_reloadConfiguredNetworks: function(callback) {
|
|
|
|
WifiManager.getConfiguredNetworks((function(networks) {
|
|
|
|
if (!networks) {
|
|
|
|
debug("Unable to get configured networks");
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._highestPriority = -1;
|
|
|
|
|
|
|
|
// Convert between netId-based and ssid-based indexing.
|
|
|
|
for (let net in networks) {
|
|
|
|
let network = networks[net];
|
|
|
|
if (!network.ssid) {
|
|
|
|
delete networks[net]; // TODO support these?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (network.priority && network.priority > this._highestPriority)
|
|
|
|
this._highestPriority = network.priority;
|
2012-11-08 05:34:12 +00:00
|
|
|
|
|
|
|
let networkKey = getNetworkKey(network);
|
|
|
|
networks[networkKey] = network;
|
2012-04-09 07:44:53 +00:00
|
|
|
delete networks[net];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.configuredNetworks = networks;
|
|
|
|
callback(true);
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Important side effect: calls WifiManager.saveConfig.
|
|
|
|
_reprioritizeNetworks: function(callback) {
|
|
|
|
// First, sort the networks in orer of their priority.
|
|
|
|
var ordered = Object.getOwnPropertyNames(this.configuredNetworks);
|
|
|
|
let self = this;
|
|
|
|
ordered.sort(function(a, b) {
|
|
|
|
var neta = self.configuredNetworks[a],
|
|
|
|
netb = self.configuredNetworks[b];
|
|
|
|
|
|
|
|
// Sort unsorted networks to the end of the list.
|
|
|
|
if (isNaN(neta.priority))
|
|
|
|
return isNaN(netb.priority) ? 0 : 1;
|
|
|
|
if (isNaN(netb.priority))
|
|
|
|
return -1;
|
|
|
|
return netb.priority - neta.priority;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Skip unsorted networks.
|
|
|
|
let newPriority = 0, i;
|
|
|
|
for (i = ordered.length - 1; i >= 0; --i) {
|
|
|
|
if (!isNaN(this.configuredNetworks[ordered[i]].priority))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No networks we care about?
|
|
|
|
if (i < 0) {
|
|
|
|
WifiManager.saveConfig(callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now assign priorities from 0 to length, starting with the smallest
|
|
|
|
// priority and heading towards the highest (note the dependency between
|
|
|
|
// total and i here).
|
|
|
|
let done = 0, errors = 0, total = i + 1;
|
|
|
|
for (; i >= 0; --i) {
|
|
|
|
let network = this.configuredNetworks[ordered[i]];
|
|
|
|
network.priority = newPriority++;
|
|
|
|
|
|
|
|
// Note: networkUpdated declared below since it happens logically after
|
|
|
|
// this loop.
|
|
|
|
WifiManager.updateNetwork(network, networkUpdated);
|
|
|
|
}
|
|
|
|
|
|
|
|
function networkUpdated(ok) {
|
|
|
|
if (!ok)
|
|
|
|
++errors;
|
|
|
|
if (++done === total) {
|
|
|
|
if (errors > 0) {
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiManager.saveConfig(function(ok) {
|
|
|
|
if (!ok) {
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._reloadConfiguredNetworks(function(ok) {
|
|
|
|
callback(ok);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIWifi
|
|
|
|
|
2012-08-22 19:37:11 +00:00
|
|
|
_domManagers: [],
|
2012-04-09 07:44:53 +00:00
|
|
|
_fireEvent: function(message, data) {
|
2012-10-02 21:24:53 +00:00
|
|
|
this._domManagers.forEach(function(manager) {
|
|
|
|
// Note: We should never have a dead message manager here because we
|
|
|
|
// observe our child message managers shutting down, below.
|
|
|
|
manager.sendAsyncMessage("WifiManager:" + message, data);
|
2012-08-22 19:37:11 +00:00
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
2012-08-22 18:34:42 +00:00
|
|
|
_sendMessage: function(message, success, data, msg) {
|
|
|
|
msg.manager.sendAsyncMessage(message + (success ? ":OK" : ":NO"),
|
|
|
|
{ data: data, rid: msg.rid, mid: msg.mid });
|
2012-04-09 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function MessageManager_receiveMessage(aMessage) {
|
2012-10-02 21:24:53 +00:00
|
|
|
let msg = aMessage.data || {};
|
|
|
|
msg.manager = aMessage.target;
|
|
|
|
|
|
|
|
// Note: By the time we receive child-process-shutdown, the child process
|
|
|
|
// has already forgotten its permissions so we do this before the
|
|
|
|
// permissions check.
|
|
|
|
if (aMessage.name === "child-process-shutdown") {
|
|
|
|
let i;
|
|
|
|
if ((i = this._domManagers.indexOf(msg.manager)) != -1) {
|
|
|
|
this._domManagers.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
2012-10-02 15:18:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-02 21:24:53 +00:00
|
|
|
if (!aMessage.target.assertPermission("wifi-manage")) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 18:34:42 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "WifiManager:getNetworks":
|
2012-08-22 18:34:42 +00:00
|
|
|
this.getNetworks(msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
break;
|
2012-09-27 18:18:34 +00:00
|
|
|
case "WifiManager:getKnownNetworks":
|
|
|
|
this.getKnownNetworks(msg);
|
|
|
|
break;
|
2012-04-09 07:44:53 +00:00
|
|
|
case "WifiManager:associate":
|
2012-08-22 18:34:42 +00:00
|
|
|
this.associate(msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
break;
|
|
|
|
case "WifiManager:forget":
|
2012-08-22 18:34:42 +00:00
|
|
|
this.forget(msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
break;
|
2012-08-16 22:19:51 +00:00
|
|
|
case "WifiManager:wps":
|
2012-08-22 18:34:42 +00:00
|
|
|
this.wps(msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
break;
|
2012-09-24 09:05:47 +00:00
|
|
|
case "WifiManager:setPowerSavingMode":
|
|
|
|
this.setPowerSavingMode(msg);
|
|
|
|
break;
|
2013-08-22 10:22:25 +00:00
|
|
|
case "WifiManager:setHttpProxy":
|
|
|
|
this.setHttpProxy(msg);
|
|
|
|
break;
|
2013-06-24 03:02:30 +00:00
|
|
|
case "WifiManager:setStaticIpMode":
|
|
|
|
this.setStaticIpMode(msg);
|
|
|
|
break;
|
2012-04-09 07:44:53 +00:00
|
|
|
case "WifiManager:getState": {
|
2012-08-22 19:37:11 +00:00
|
|
|
let i;
|
2012-10-02 21:24:53 +00:00
|
|
|
if ((i = this._domManagers.indexOf(msg.manager)) === -1) {
|
|
|
|
this._domManagers.push(msg.manager);
|
2012-08-22 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 21:24:53 +00:00
|
|
|
let net = this.currentNetwork ? netToDOM(this.currentNetwork) : null;
|
2012-04-09 07:44:53 +00:00
|
|
|
return { network: net,
|
|
|
|
connectionInfo: this._lastConnectionInfo,
|
2012-06-28 10:01:51 +00:00
|
|
|
enabled: WifiManager.enabled,
|
2012-09-27 18:14:06 +00:00
|
|
|
status: translateState(WifiManager.state),
|
|
|
|
macAddress: this.macAddress };
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-22 18:34:42 +00:00
|
|
|
getNetworks: function(msg) {
|
2012-04-06 15:56:15 +00:00
|
|
|
const message = "WifiManager:getNetworks:Return";
|
2012-08-27 02:29:46 +00:00
|
|
|
if (!WifiManager.enabled) {
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
2012-04-06 15:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:58:26 +00:00
|
|
|
let sent = false;
|
2012-08-30 18:05:02 +00:00
|
|
|
let callback = (function (networks) {
|
2013-01-08 08:58:26 +00:00
|
|
|
if (sent)
|
|
|
|
return;
|
|
|
|
sent = true;
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, networks !== null, networks, msg);
|
2012-08-30 18:05:02 +00:00
|
|
|
}).bind(this);
|
|
|
|
this.waitForScan(callback);
|
|
|
|
|
|
|
|
WifiManager.scan(true, (function(ok) {
|
|
|
|
// If the scan command succeeded, we're done.
|
|
|
|
if (ok)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Avoid sending multiple responses.
|
2013-01-08 08:58:26 +00:00
|
|
|
if (sent)
|
|
|
|
return;
|
2012-08-30 18:05:02 +00:00
|
|
|
|
|
|
|
// Otherwise, let the client know that it failed, it's responsible for
|
|
|
|
// trying again in a few seconds.
|
2013-01-08 08:58:26 +00:00
|
|
|
sent = true;
|
2012-08-30 18:05:02 +00:00
|
|
|
this._sendMessage(message, false, "ScanFailed", msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2012-10-02 00:40:19 +00:00
|
|
|
getWifiScanResults: function(callback) {
|
|
|
|
var count = 0;
|
|
|
|
var timer = null;
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.waitForScan(waitForScanCallback);
|
|
|
|
doScan();
|
|
|
|
function doScan() {
|
|
|
|
WifiManager.scan(true, function (ok) {
|
|
|
|
if (!ok) {
|
|
|
|
if (!timer) {
|
|
|
|
count = 0;
|
|
|
|
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count++ >= 3) {
|
|
|
|
timer = null;
|
|
|
|
this.wantScanResults.splice(this.wantScanResults.indexOf(waitForScanCallback), 1);
|
|
|
|
callback.onfailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else it's still running, continue waiting.
|
|
|
|
timer.initWithCallback(doScan, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForScanCallback(networks) {
|
|
|
|
if (networks === null) {
|
|
|
|
callback.onfailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var wifiScanResults = new Array();
|
|
|
|
var net;
|
|
|
|
for (let net in networks) {
|
|
|
|
let value = networks[net];
|
|
|
|
wifiScanResults.push(transformResult(value));
|
|
|
|
}
|
|
|
|
callback.onready(wifiScanResults.length, wifiScanResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
function transformResult(element) {
|
|
|
|
var result = new WifiScanResult();
|
|
|
|
result.connected = false;
|
|
|
|
for (let id in element) {
|
|
|
|
if (id === "__exposedProps__") {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-08 11:41:48 +00:00
|
|
|
if (id === "security") {
|
2012-10-02 00:40:19 +00:00
|
|
|
result[id] = 0;
|
2013-07-08 11:41:48 +00:00
|
|
|
var security = element[id];
|
|
|
|
for (let j = 0; j < security.length; j++) {
|
|
|
|
if (security[j] === "WPA-PSK") {
|
2012-10-02 00:40:19 +00:00
|
|
|
result[id] |= Ci.nsIWifiScanResult.WPA_PSK;
|
2013-07-08 11:41:48 +00:00
|
|
|
} else if (security[j] === "WPA-EAP") {
|
2012-10-02 00:40:19 +00:00
|
|
|
result[id] |= Ci.nsIWifiScanResult.WPA_EAP;
|
2013-07-08 11:41:48 +00:00
|
|
|
} else if (security[j] === "WEP") {
|
2012-10-02 00:40:19 +00:00
|
|
|
result[id] |= Ci.nsIWifiScanResult.WEP;
|
|
|
|
} else {
|
|
|
|
result[id] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result[id] = element[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-27 18:18:34 +00:00
|
|
|
getKnownNetworks: function(msg) {
|
|
|
|
const message = "WifiManager:getKnownNetworks:Return";
|
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._reloadConfiguredNetworks((function(ok) {
|
|
|
|
if (!ok) {
|
|
|
|
this._sendMessage(message, false, "Failed", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:34:12 +00:00
|
|
|
var networks = [];
|
|
|
|
for (let networkKey in this.configuredNetworks) {
|
|
|
|
networks.push(netToDOM(this.configuredNetworks[networkKey]));
|
2012-09-27 18:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._sendMessage(message, true, networks, msg);
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2012-06-28 10:01:51 +00:00
|
|
|
_setWifiEnabledCallback: function(status) {
|
|
|
|
// If we're enabling ourselves, then wait until we've connected to the
|
|
|
|
// supplicant to notify. If we're disabling, we take care of this in
|
|
|
|
// supplicantlost.
|
|
|
|
if (WifiManager.supplicantStarted)
|
|
|
|
WifiManager.start();
|
2013-09-09 15:25:16 +00:00
|
|
|
|
|
|
|
this.requestDone();
|
2012-06-28 10:01:51 +00:00
|
|
|
},
|
|
|
|
|
2013-09-09 15:25:16 +00:00
|
|
|
setWifiEnabled: function(enabled, callback) {
|
|
|
|
WifiManager.setWifiEnabled(enabled, callback);
|
2012-08-29 19:31:26 +00:00
|
|
|
},
|
|
|
|
|
2013-09-09 15:25:16 +00:00
|
|
|
// requestDone() must be called to before callback complete(or error)
|
|
|
|
// so next queue in the request quene can be executed.
|
2013-01-17 12:26:17 +00:00
|
|
|
queueRequest: function(enabled, callback) {
|
2013-09-09 15:25:16 +00:00
|
|
|
if (!callback) {
|
|
|
|
throw "Try to enqueue a request without callback";
|
|
|
|
}
|
|
|
|
|
|
|
|
this._stateRequests.push({
|
|
|
|
enabled: enabled,
|
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
|
|
|
|
this.nextRequest();
|
2012-08-29 19:31:26 +00:00
|
|
|
},
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
getWifiTetheringParameters: function getWifiTetheringParameters(enable) {
|
|
|
|
let ssid;
|
|
|
|
let securityType;
|
|
|
|
let securityId;
|
|
|
|
let interfaceIp;
|
|
|
|
let prefix;
|
2013-06-19 10:57:58 +00:00
|
|
|
let wifiDhcpStartIp;
|
|
|
|
let wifiDhcpEndIp;
|
|
|
|
let usbDhcpStartIp;
|
|
|
|
let usbDhcpEndIp;
|
2013-05-16 11:39:58 +00:00
|
|
|
let dns1;
|
|
|
|
let dns2;
|
|
|
|
|
|
|
|
ssid = this.tetheringSettings[SETTINGS_WIFI_SSID];
|
|
|
|
securityType = this.tetheringSettings[SETTINGS_WIFI_SECURITY_TYPE];
|
|
|
|
securityId = this.tetheringSettings[SETTINGS_WIFI_SECURITY_PASSWORD];
|
|
|
|
interfaceIp = this.tetheringSettings[SETTINGS_WIFI_IP];
|
|
|
|
prefix = this.tetheringSettings[SETTINGS_WIFI_PREFIX];
|
2013-06-19 10:57:58 +00:00
|
|
|
wifiDhcpStartIp = this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_STARTIP];
|
|
|
|
wifiDhcpEndIp = this.tetheringSettings[SETTINGS_WIFI_DHCPSERVER_ENDIP];
|
|
|
|
usbDhcpStartIp = this.tetheringSettings[SETTINGS_USB_DHCPSERVER_STARTIP];
|
|
|
|
usbDhcpEndIp = this.tetheringSettings[SETTINGS_USB_DHCPSERVER_ENDIP];
|
2013-05-16 11:39:58 +00:00
|
|
|
dns1 = this.tetheringSettings[SETTINGS_WIFI_DNS1];
|
|
|
|
dns2 = this.tetheringSettings[SETTINGS_WIFI_DNS2];
|
|
|
|
|
|
|
|
// Check the format to prevent netd from crash.
|
|
|
|
if (!ssid || ssid == "") {
|
|
|
|
debug("Invalid SSID value.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (securityType != WIFI_SECURITY_TYPE_NONE &&
|
|
|
|
securityType != WIFI_SECURITY_TYPE_WPA_PSK &&
|
|
|
|
securityType != WIFI_SECURITY_TYPE_WPA2_PSK) {
|
|
|
|
|
|
|
|
debug("Invalid security type.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (securityType != WIFI_SECURITY_TYPE_NONE && !securityId) {
|
|
|
|
debug("Invalid security password.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Using the default values here until application supports these settings.
|
|
|
|
if (interfaceIp == "" || prefix == "" ||
|
2013-06-19 10:57:58 +00:00
|
|
|
wifiDhcpStartIp == "" || wifiDhcpEndIp == "" ||
|
|
|
|
usbDhcpStartIp == "" || usbDhcpEndIp == "") {
|
2013-05-16 11:39:58 +00:00
|
|
|
debug("Invalid subnet information.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
ssid: ssid,
|
|
|
|
security: securityType,
|
|
|
|
key: securityId,
|
|
|
|
ip: interfaceIp,
|
|
|
|
prefix: prefix,
|
2013-06-19 10:57:58 +00:00
|
|
|
wifiStartIp: wifiDhcpStartIp,
|
|
|
|
wifiEndIp: wifiDhcpEndIp,
|
|
|
|
usbStartIp: usbDhcpStartIp,
|
|
|
|
usbEndIp: usbDhcpEndIp,
|
2013-05-16 11:39:58 +00:00
|
|
|
dns1: dns1,
|
|
|
|
dns2: dns2,
|
|
|
|
enable: enable,
|
|
|
|
mode: enable ? WIFI_FIRMWARE_AP : WIFI_FIRMWARE_STATION,
|
|
|
|
link: enable ? NETWORK_INTERFACE_UP : NETWORK_INTERFACE_DOWN
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-09-04 04:26:56 +00:00
|
|
|
setWifiApEnabled: function(enabled, callback) {
|
2013-05-16 11:39:58 +00:00
|
|
|
let configuration = this.getWifiTetheringParameters(enabled);
|
|
|
|
|
|
|
|
if (!configuration) {
|
2013-09-09 15:25:16 +00:00
|
|
|
this.requestDone();
|
2013-05-16 11:39:58 +00:00
|
|
|
debug("Invalid Wifi Tethering configuration.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiManager.setWifiApEnabled(enabled, configuration, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
2012-08-22 18:34:42 +00:00
|
|
|
associate: function(msg) {
|
2012-04-09 07:44:53 +00:00
|
|
|
const MAX_PRIORITY = 9999;
|
|
|
|
const message = "WifiManager:associate:Return";
|
2012-08-22 18:34:42 +00:00
|
|
|
let network = msg.data;
|
2012-08-27 02:29:46 +00:00
|
|
|
if (!WifiManager.enabled) {
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
2012-04-06 15:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
let privnet = network;
|
|
|
|
let self = this;
|
|
|
|
function networkReady() {
|
|
|
|
// saveConfig now before we disable most of the other networks.
|
|
|
|
function selectAndConnect() {
|
|
|
|
WifiManager.enableNetwork(privnet.netId, true, function (ok) {
|
|
|
|
if (ok)
|
|
|
|
self._needToEnableNetworks = true;
|
|
|
|
if (WifiManager.state === "DISCONNECTED" ||
|
|
|
|
WifiManager.state === "SCANNING") {
|
|
|
|
WifiManager.reconnect(function (ok) {
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, ok, ok, msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
} else {
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, ok, ok, msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self._highestPriority >= MAX_PRIORITY)
|
|
|
|
self._reprioritizeNetworks(selectAndConnect);
|
|
|
|
else
|
|
|
|
WifiManager.saveConfig(selectAndConnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ssid = privnet.ssid;
|
2012-11-08 05:34:12 +00:00
|
|
|
let networkKey = getNetworkKey(privnet);
|
2012-04-09 07:44:53 +00:00
|
|
|
let configured;
|
|
|
|
|
2012-12-17 23:39:30 +00:00
|
|
|
if (networkKey in this._addingNetworks) {
|
|
|
|
this._sendMessage(message, false, "Racing associates");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:34:12 +00:00
|
|
|
if (networkKey in this.configuredNetworks)
|
|
|
|
configured = this.configuredNetworks[networkKey];
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
netFromDOM(privnet, configured);
|
|
|
|
|
|
|
|
privnet.priority = ++this._highestPriority;
|
|
|
|
if (configured) {
|
|
|
|
privnet.netId = configured.netId;
|
|
|
|
WifiManager.updateNetwork(privnet, (function(ok) {
|
|
|
|
if (!ok) {
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, false, "Network is misconfigured", msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
networkReady();
|
|
|
|
}).bind(this));
|
|
|
|
} else {
|
|
|
|
// networkReady, above, calls saveConfig. We want to remember the new
|
|
|
|
// network as being enabled, which isn't the default, so we explicitly
|
|
|
|
// set it to being "enabled" before we add it and save the
|
|
|
|
// configuration.
|
|
|
|
privnet.disabled = 0;
|
2012-12-17 23:39:30 +00:00
|
|
|
this._addingNetworks[networkKey] = privnet;
|
2012-04-09 07:44:53 +00:00
|
|
|
WifiManager.addNetwork(privnet, (function(ok) {
|
2012-12-17 23:39:30 +00:00
|
|
|
delete this._addingNetworks[networkKey];
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!ok) {
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, false, "Network is misconfigured", msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:34:12 +00:00
|
|
|
this.configuredNetworks[networkKey] = privnet;
|
2012-04-09 07:44:53 +00:00
|
|
|
networkReady();
|
|
|
|
}).bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-22 18:34:42 +00:00
|
|
|
forget: function(msg) {
|
2012-04-09 07:44:53 +00:00
|
|
|
const message = "WifiManager:forget:Return";
|
2012-08-22 18:34:42 +00:00
|
|
|
let network = msg.data;
|
2012-08-27 02:29:46 +00:00
|
|
|
if (!WifiManager.enabled) {
|
2012-08-22 18:34:42 +00:00
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
2012-04-06 15:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 06:55:56 +00:00
|
|
|
this._reloadConfiguredNetworks((function(ok) {
|
|
|
|
// Give it a chance to remove the network even if reload is failed.
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!ok) {
|
2013-03-26 06:55:56 +00:00
|
|
|
debug("Warning !!! Failed to reload the configured networks");
|
|
|
|
}
|
|
|
|
|
|
|
|
let ssid = network.ssid;
|
|
|
|
let networkKey = getNetworkKey(network);
|
|
|
|
if (!(networkKey in this.configuredNetworks)) {
|
|
|
|
this._sendMessage(message, false, "Trying to forget an unknown network", msg);
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 06:55:56 +00:00
|
|
|
let self = this;
|
|
|
|
let configured = this.configuredNetworks[networkKey];
|
|
|
|
this._reconnectOnDisconnect = (this.currentNetwork &&
|
|
|
|
(this.currentNetwork.ssid === ssid));
|
|
|
|
WifiManager.removeNetwork(configured.netId, function(ok) {
|
|
|
|
if (!ok) {
|
|
|
|
self._sendMessage(message, false, "Unable to remove the network", msg);
|
|
|
|
self._reconnectOnDisconnect = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiManager.saveConfig(function() {
|
|
|
|
self._reloadConfiguredNetworks(function() {
|
|
|
|
self._sendMessage(message, true, true, msg);
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-03-26 06:55:56 +00:00
|
|
|
}).bind(this));
|
2012-04-09 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
2012-08-22 18:34:42 +00:00
|
|
|
wps: function(msg) {
|
2012-08-16 22:19:51 +00:00
|
|
|
const message = "WifiManager:wps:Return";
|
|
|
|
let self = this;
|
2012-08-22 18:34:42 +00:00
|
|
|
let detail = msg.data;
|
2012-08-16 22:19:51 +00:00
|
|
|
if (detail.method === "pbc") {
|
|
|
|
WifiManager.wpsPbc(function(ok) {
|
|
|
|
if (ok)
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, true, true, msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
else
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, false, "WPS PBC failed", msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
});
|
2012-08-22 19:41:31 +00:00
|
|
|
} else if (detail.method === "pin") {
|
2013-07-08 11:41:48 +00:00
|
|
|
WifiManager.wpsPin(detail, function(pin) {
|
2012-08-22 19:41:31 +00:00
|
|
|
if (pin)
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, true, pin, msg);
|
2012-08-22 19:41:31 +00:00
|
|
|
else
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, false, "WPS PIN failed", msg);
|
2012-08-22 19:41:31 +00:00
|
|
|
});
|
2012-08-16 22:19:51 +00:00
|
|
|
} else if (detail.method === "cancel") {
|
|
|
|
WifiManager.wpsCancel(function(ok) {
|
|
|
|
if (ok)
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, true, true, msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
else
|
2012-08-22 18:34:42 +00:00
|
|
|
self._sendMessage(message, false, "WPS Cancel failed", msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
});
|
|
|
|
} else {
|
2012-08-22 19:41:31 +00:00
|
|
|
self._sendMessage(message, false, "Invalid WPS method=" + detail.method,
|
2012-08-22 18:34:42 +00:00
|
|
|
msg);
|
2012-08-16 22:19:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-24 09:05:47 +00:00
|
|
|
setPowerSavingMode: function(msg) {
|
|
|
|
const message = "WifiManager:setPowerSavingMode:Return";
|
|
|
|
let self = this;
|
|
|
|
let enabled = msg.data;
|
|
|
|
let mode = enabled ? "AUTO" : "ACTIVE";
|
|
|
|
|
|
|
|
// Some wifi drivers may not implement this command. Set power mode
|
|
|
|
// even if suspend optimization command failed.
|
|
|
|
WifiManager.setSuspendOptimizations(enabled, function(ok) {
|
|
|
|
WifiManager.setPowerMode(mode, function(ok) {
|
|
|
|
if (ok) {
|
|
|
|
self._sendMessage(message, true, true, msg);
|
|
|
|
} else {
|
|
|
|
self._sendMessage(message, false, "Set power saving mode failed", msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-22 10:22:25 +00:00
|
|
|
setHttpProxy: function(msg) {
|
|
|
|
const message = "WifiManager:setHttpProxy:Return";
|
|
|
|
let self = this;
|
|
|
|
let network = msg.data.network;
|
|
|
|
let info = msg.data.info;
|
|
|
|
|
|
|
|
netFromDOM(network, null);
|
|
|
|
|
|
|
|
WifiManager.configureHttpProxy(network, info, function(ok) {
|
|
|
|
if (ok) {
|
|
|
|
// If configured network is current connected network
|
|
|
|
// need update http proxy immediately.
|
|
|
|
let setNetworkKey = getNetworkKey(network);
|
|
|
|
let curNetworkKey = self.currentNetwork ? getNetworkKey(self.currentNetwork) : null;
|
|
|
|
if (setNetworkKey === curNetworkKey)
|
|
|
|
WifiManager.setHttpProxy(network);
|
|
|
|
|
|
|
|
self._sendMessage(message, true, true, msg);
|
|
|
|
} else {
|
|
|
|
self._sendMessage(message, false, "Set http proxy failed", msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
setStaticIpMode: function(msg) {
|
|
|
|
const message = "WifiManager:setStaticMode:Return";
|
|
|
|
let self = this;
|
|
|
|
let network = msg.data.network;
|
|
|
|
let info = msg.data.info;
|
|
|
|
|
|
|
|
netFromDOM(network, null);
|
|
|
|
|
|
|
|
// To compatiable with DHCP returned info structure, do translation here
|
|
|
|
info.ipaddr_str = info.ipaddr;
|
|
|
|
info.proxy_str = info.proxy;
|
|
|
|
info.gateway_str = info.gateway;
|
|
|
|
info.dns1_str = info.dns1;
|
|
|
|
info.dns2_str = info.dns2;
|
|
|
|
|
|
|
|
WifiManager.setStaticIpMode(network, info, function(ok) {
|
|
|
|
if (ok) {
|
|
|
|
self._sendMessage(message, true, true, msg);
|
|
|
|
} else {
|
|
|
|
self._sendMessage(message, false, "Set static ip mode failed", msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
// This is a bit ugly, but works. In particular, this depends on the fact
|
|
|
|
// that RadioManager never actually tries to get the worker from us.
|
|
|
|
get worker() { throw "Not implemented"; },
|
|
|
|
|
|
|
|
shutdown: function() {
|
|
|
|
debug("shutting down ...");
|
2013-09-09 15:25:16 +00:00
|
|
|
this.queueRequest(false, function(data) {
|
|
|
|
this.setWifiEnabled(false, this._setWifiEnabledCallback.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
requestProcessing: false, // Hold while dequeue and execution a request.
|
|
|
|
// Released upon the request is fully executed,
|
|
|
|
// i.e, mostly after callback is done.
|
|
|
|
requestDone: function requestDone() {
|
|
|
|
this.requestProcessing = false;
|
|
|
|
this.nextRequest();
|
2012-08-29 19:31:26 +00:00
|
|
|
},
|
|
|
|
|
2013-01-17 12:26:17 +00:00
|
|
|
nextRequest: function nextRequest() {
|
2013-09-09 15:25:16 +00:00
|
|
|
// No request to process
|
|
|
|
if (this._stateRequests.length === 0) {
|
2012-09-13 07:19:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-09 15:25:16 +00:00
|
|
|
|
|
|
|
// Handling request, wait for it.
|
|
|
|
if (this.requestProcessing) {
|
|
|
|
return;
|
2012-09-13 07:19:47 +00:00
|
|
|
}
|
2013-09-09 15:25:16 +00:00
|
|
|
|
|
|
|
// Hold processing lock
|
|
|
|
this.requestProcessing = true;
|
|
|
|
|
|
|
|
// Find next valid request
|
|
|
|
let request = this._stateRequests.shift();
|
|
|
|
|
|
|
|
request.callback(request.enabled);
|
2012-09-13 07:19:47 +00:00
|
|
|
},
|
|
|
|
|
2013-04-02 07:30:32 +00:00
|
|
|
notifyTetheringOn: function notifyTetheringOn() {
|
|
|
|
// It's really sad that we don't have an API to notify the wifi
|
|
|
|
// hotspot status. Toggle settings to let gaia know that wifi hotspot
|
|
|
|
// is enabled.
|
2013-09-09 15:25:16 +00:00
|
|
|
let self = this;
|
2013-05-16 11:39:58 +00:00
|
|
|
this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] = true;
|
2013-07-22 09:11:57 +00:00
|
|
|
this._oldWifiTetheringEnabledState = true;
|
2013-04-02 07:30:32 +00:00
|
|
|
gSettingsService.createLock().set(
|
2013-09-09 15:25:16 +00:00
|
|
|
SETTINGS_WIFI_TETHERING_ENABLED,
|
|
|
|
true,
|
|
|
|
{
|
|
|
|
handle: function(aName, aResult) {
|
|
|
|
self.requestDone();
|
|
|
|
},
|
|
|
|
handleError: function(aErrorMessage) {
|
|
|
|
self.requestDone();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fromInternalSetting");
|
2013-04-02 07:30:32 +00:00
|
|
|
},
|
|
|
|
|
2013-01-17 12:26:17 +00:00
|
|
|
notifyTetheringOff: function notifyTetheringOff() {
|
|
|
|
// It's really sad that we don't have an API to notify the wifi
|
|
|
|
// hotspot status. Toggle settings to let gaia know that wifi hotspot
|
|
|
|
// is disabled.
|
2013-09-09 15:25:16 +00:00
|
|
|
let self = this;
|
2013-05-16 11:39:58 +00:00
|
|
|
this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] = false;
|
2013-07-22 09:11:57 +00:00
|
|
|
this._oldWifiTetheringEnabledState = false;
|
2013-01-17 12:26:17 +00:00
|
|
|
gSettingsService.createLock().set(
|
2013-09-09 15:25:16 +00:00
|
|
|
SETTINGS_WIFI_TETHERING_ENABLED,
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
handle: function(aName, aResult) {
|
|
|
|
self.requestDone();
|
|
|
|
},
|
|
|
|
handleError: function(aErrorMessage) {
|
|
|
|
self.requestDone();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fromInternalSetting");
|
2013-01-17 12:26:17 +00:00
|
|
|
},
|
|
|
|
|
2012-09-13 07:19:47 +00:00
|
|
|
handleWifiEnabled: function(enabled) {
|
|
|
|
if (WifiManager.enabled === enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-17 12:26:17 +00:00
|
|
|
// Make sure Wifi hotspot is idle before switching to Wifi mode.
|
2013-09-05 09:16:24 +00:00
|
|
|
if (enabled) {
|
2013-01-17 12:26:17 +00:00
|
|
|
this.queueRequest(false, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] ||
|
|
|
|
WifiManager.tetheringState != "UNINITIALIZED") {
|
2013-09-25 08:49:05 +00:00
|
|
|
this.disconnectedByWifi = true;
|
2013-09-05 09:16:24 +00:00
|
|
|
this.setWifiApEnabled(false, this.notifyTetheringOff.bind(this));
|
|
|
|
} else {
|
|
|
|
this.requestDone();
|
|
|
|
}
|
2012-09-13 07:19:47 +00:00
|
|
|
}.bind(this));
|
|
|
|
}
|
2013-09-09 15:25:16 +00:00
|
|
|
|
|
|
|
this.queueRequest(enabled, function(data) {
|
|
|
|
this.setWifiEnabled(enabled, this._setWifiEnabledCallback.bind(this));
|
|
|
|
}.bind(this));
|
2013-05-16 11:39:58 +00:00
|
|
|
|
2013-09-05 09:16:24 +00:00
|
|
|
if (!enabled) {
|
2013-04-02 07:30:32 +00:00
|
|
|
this.queueRequest(true, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (this.disconnectedByWifi) {
|
|
|
|
this.setWifiApEnabled(true, this.notifyTetheringOn.bind(this));
|
|
|
|
} else {
|
|
|
|
this.requestDone();
|
|
|
|
}
|
2013-04-02 07:30:32 +00:00
|
|
|
this.disconnectedByWifi = false;
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2012-09-13 07:19:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleWifiTetheringEnabled: function(enabled) {
|
2013-01-17 12:26:17 +00:00
|
|
|
// Make sure Wifi is idle before switching to Wifi hotspot mode.
|
2013-09-05 09:16:24 +00:00
|
|
|
if (enabled) {
|
2013-09-09 15:25:16 +00:00
|
|
|
this.queueRequest(false, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (WifiManager.enabled || WifiManager.state != "UNINITIALIZED") {
|
2013-09-25 08:49:05 +00:00
|
|
|
this.disconnectedByWifiTethering = true;
|
2013-09-05 09:16:24 +00:00
|
|
|
this.setWifiEnabled(false, this._setWifiEnabledCallback.bind(this));
|
|
|
|
} else {
|
|
|
|
this.requestDone();
|
|
|
|
}
|
2013-09-09 15:25:16 +00:00
|
|
|
}.bind(this));
|
2012-08-29 19:31:26 +00:00
|
|
|
}
|
2013-01-17 12:26:17 +00:00
|
|
|
|
|
|
|
this.queueRequest(enabled, function(data) {
|
2013-09-09 15:25:16 +00:00
|
|
|
this.setWifiApEnabled(data, this.requestDone.bind(this));
|
2013-01-17 12:26:17 +00:00
|
|
|
}.bind(this));
|
2013-04-02 07:30:32 +00:00
|
|
|
|
2013-09-05 09:16:24 +00:00
|
|
|
if (!enabled) {
|
2013-09-09 15:25:16 +00:00
|
|
|
this.queueRequest(true, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (this.disconnectedByWifiTethering) {
|
|
|
|
this.setWifiEnabled(true, this._setWifiEnabledCallback.bind(this));
|
|
|
|
} else {
|
|
|
|
this.requestDone();
|
|
|
|
}
|
2013-09-09 15:25:16 +00:00
|
|
|
this.disconnectedByWifiTethering = false;
|
|
|
|
}.bind(this));
|
2013-04-02 07:30:32 +00:00
|
|
|
}
|
2012-09-04 04:26:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver implementation
|
|
|
|
observe: function observe(subject, topic, data) {
|
|
|
|
// Note that this function gets called for any and all settings changes,
|
|
|
|
// so we need to carefully check if we have the one we're interested in.
|
|
|
|
// The string we're interested in will be a JSON string that looks like:
|
|
|
|
// {"key":"wifi.enabled","value":"true"}.
|
2012-09-13 07:19:47 +00:00
|
|
|
if (topic !== kMozSettingsChangedObserverTopic) {
|
2012-09-04 04:26:56 +00:00
|
|
|
return;
|
2012-09-13 07:19:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 04:26:56 +00:00
|
|
|
let setting = JSON.parse(data);
|
|
|
|
// To avoid WifiWorker setting the wifi again, don't need to deal with
|
|
|
|
// the "mozsettings-changed" event fired from internal setting.
|
2012-09-13 07:19:47 +00:00
|
|
|
if (setting.message && setting.message === "fromInternalSetting") {
|
2012-09-04 04:26:56 +00:00
|
|
|
return;
|
2012-09-13 07:19:47 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
this.handle(setting.key, setting.value);
|
|
|
|
},
|
|
|
|
|
|
|
|
handle: function handle(aName, aResult) {
|
|
|
|
switch(aName) {
|
|
|
|
case SETTINGS_WIFI_ENABLED:
|
|
|
|
this.handleWifiEnabled(aResult)
|
2012-09-13 07:19:47 +00:00
|
|
|
break;
|
2013-05-16 11:39:58 +00:00
|
|
|
case SETTINGS_WIFI_DEBUG_ENABLED:
|
|
|
|
if (aResult === null)
|
|
|
|
aResult = false;
|
|
|
|
DEBUG = aResult;
|
|
|
|
updateDebug();
|
2012-09-13 07:19:47 +00:00
|
|
|
break;
|
2013-05-16 11:39:58 +00:00
|
|
|
case SETTINGS_WIFI_TETHERING_ENABLED:
|
|
|
|
this._oldWifiTetheringEnabledState = this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED];
|
|
|
|
// Fall through!
|
|
|
|
case SETTINGS_WIFI_SSID:
|
|
|
|
case SETTINGS_WIFI_SECURITY_TYPE:
|
|
|
|
case SETTINGS_WIFI_SECURITY_PASSWORD:
|
|
|
|
case SETTINGS_WIFI_IP:
|
|
|
|
case SETTINGS_WIFI_PREFIX:
|
|
|
|
case SETTINGS_WIFI_DHCPSERVER_STARTIP:
|
|
|
|
case SETTINGS_WIFI_DHCPSERVER_ENDIP:
|
|
|
|
case SETTINGS_WIFI_DNS1:
|
|
|
|
case SETTINGS_WIFI_DNS2:
|
2013-06-19 10:57:58 +00:00
|
|
|
case SETTINGS_USB_DHCPSERVER_STARTIP:
|
|
|
|
case SETTINGS_USB_DHCPSERVER_ENDIP:
|
2013-05-16 11:39:58 +00:00
|
|
|
if (aResult !== null) {
|
|
|
|
this.tetheringSettings[aName] = aResult;
|
|
|
|
}
|
|
|
|
debug("'" + aName + "'" + " is now " + this.tetheringSettings[aName]);
|
|
|
|
let index = this._wifiTetheringSettingsToRead.indexOf(aName);
|
|
|
|
|
|
|
|
if (index != -1) {
|
|
|
|
this._wifiTetheringSettingsToRead.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._wifiTetheringSettingsToRead.length) {
|
|
|
|
debug("We haven't read completely the wifi Tethering data from settings db.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._oldWifiTetheringEnabledState === this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED]) {
|
|
|
|
debug("No changes for SETTINGS_WIFI_TETHERING_ENABLED flag. Nothing to do.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._oldWifiTetheringEnabledState === null &&
|
|
|
|
!this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED]) {
|
|
|
|
debug("Do nothing when initial settings for SETTINGS_WIFI_TETHERING_ENABLED flag is false.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-22 09:11:57 +00:00
|
|
|
this._oldWifiTetheringEnabledState = this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED];
|
2013-05-16 11:39:58 +00:00
|
|
|
this.handleWifiTetheringEnabled(aResult)
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError: function handleError(aErrorMessage) {
|
|
|
|
debug("There was an error while reading Tethering settings.");
|
|
|
|
this.tetheringSettings = {};
|
|
|
|
this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] = false;
|
|
|
|
},
|
2012-04-09 07:44:53 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiWorker]);
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
let debug;
|
2012-10-29 19:26:55 +00:00
|
|
|
function updateDebug() {
|
|
|
|
if (DEBUG) {
|
|
|
|
debug = function (s) {
|
|
|
|
dump("-*- WifiWorker component: " + s + "\n");
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debug = function (s) {};
|
|
|
|
}
|
2012-10-31 00:35:34 +00:00
|
|
|
WifiManager.syncDebug();
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
2012-10-29 19:26:55 +00:00
|
|
|
updateDebug();
|