2014-06-25 05:12:07 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2012-04-09 07:44:53 +00:00
|
|
|
/* 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");
|
2014-04-30 11:58:40 +00:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2013-09-30 03:05:05 +00:00
|
|
|
Cu.import("resource://gre/modules/WifiCommand.jsm");
|
|
|
|
Cu.import("resource://gre/modules/WifiNetUtil.jsm");
|
2014-01-14 10:23:42 +00:00
|
|
|
Cu.import("resource://gre/modules/WifiP2pManager.jsm");
|
|
|
|
Cu.import("resource://gre/modules/WifiP2pWorkerObserver.jsm");
|
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-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-10-18 08:46:30 +00:00
|
|
|
const MAX_RETRIES_ON_DHCP_FAILURE = 2;
|
2012-08-28 19:14:26 +00:00
|
|
|
|
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");
|
|
|
|
|
2013-11-15 09:35:33 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gNetworkService",
|
|
|
|
"@mozilla.org/network/service;1",
|
|
|
|
"nsINetworkService");
|
|
|
|
|
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() {
|
2013-10-14 08:33:59 +00:00
|
|
|
var manager = {};
|
|
|
|
|
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"),
|
2014-01-14 10:23:42 +00:00
|
|
|
p2pSupported: libcutils.property_get("ro.moz.wifi.p2p_supported") === "1",
|
2014-03-20 06:39:21 +00:00
|
|
|
eapSimSupported: libcutils.property_get("ro.moz.wifi.eapsim_supported") === "1",
|
2013-07-29 09:59:53 +00:00
|
|
|
ifname: libcutils.property_get("wifi.interface")
|
2012-11-22 10:29:24 +00:00
|
|
|
};
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 06:39:21 +00:00
|
|
|
let {sdkVersion, unloadDriverEnabled, schedScanRecovery,
|
|
|
|
driverDelay, p2pSupported, eapSimSupported, ifname} = getStartupPrefs();
|
|
|
|
|
|
|
|
let capabilities = {
|
2014-05-19 02:29:21 +00:00
|
|
|
security: ["OPEN", "WEP", "WPA-PSK", "WPA-EAP"],
|
|
|
|
eapMethod: ["PEAP", "TTLS"],
|
|
|
|
eapPhase2: ["MSCHAPV2"],
|
|
|
|
certificate: ["SERVER"]
|
2014-03-20 06:39:21 +00:00
|
|
|
};
|
2014-05-19 02:29:21 +00:00
|
|
|
if (eapSimSupported) {
|
|
|
|
capabilities.eapMethod.unshift("SIM");
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
|
2013-08-27 20:54:51 +00:00
|
|
|
let wifiListener = {
|
2013-10-14 08:33:59 +00:00
|
|
|
onWaitEvent: function(event, iface) {
|
|
|
|
if (manager.ifname === iface && handleEvent(event)) {
|
|
|
|
waitForEvent(iface);
|
2014-01-14 10:23:42 +00:00
|
|
|
} else if (p2pSupported) {
|
|
|
|
if (WifiP2pManager.INTERFACE_NAME === iface) {
|
|
|
|
// If the connection is closed, wifi.c::wifi_wait_for_event()
|
|
|
|
// will still return 'CTRL-EVENT-TERMINATING - connection closed'
|
|
|
|
// rather than blocking. So when we see this special event string,
|
|
|
|
// just return immediately.
|
|
|
|
const TERMINATED_EVENT = 'CTRL-EVENT-TERMINATING - connection closed';
|
|
|
|
if (-1 !== event.indexOf(TERMINATED_EVENT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p2pManager.handleEvent(event);
|
|
|
|
waitForEvent(iface);
|
|
|
|
}
|
2013-08-27 20:54:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-14 08:33:59 +00:00
|
|
|
onCommand: function(event, iface) {
|
|
|
|
onmessageresult(event, iface);
|
2013-08-27 20:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
manager.ifname = ifname;
|
2014-04-10 05:52:38 +00:00
|
|
|
manager.connectToSupplicant = false;
|
2013-07-29 09:59:53 +00:00
|
|
|
// 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
|
|
|
|
2014-01-14 10:23:42 +00:00
|
|
|
// Regular Wifi stuff.
|
|
|
|
var netUtil = WifiNetUtil(controlMessage);
|
2014-05-06 07:04:16 +00:00
|
|
|
var wifiCommand = WifiCommand(controlMessage, manager.ifname, sdkVersion);
|
2014-01-14 10:23:42 +00:00
|
|
|
|
|
|
|
// Wifi P2P stuff
|
|
|
|
var p2pManager;
|
|
|
|
if (p2pSupported) {
|
2014-05-06 07:04:16 +00:00
|
|
|
let p2pCommand = WifiCommand(controlMessage, WifiP2pManager.INTERFACE_NAME, sdkVersion);
|
2014-01-14 10:23:42 +00:00
|
|
|
p2pManager = WifiP2pManager(p2pCommand, netUtil);
|
|
|
|
}
|
|
|
|
|
2013-10-14 08:33:59 +00:00
|
|
|
let wifiService = Cc["@mozilla.org/wifi/service;1"];
|
|
|
|
if (wifiService) {
|
|
|
|
wifiService = wifiService.getService(Ci.nsIWifiProxyService);
|
|
|
|
let interfaces = [manager.ifname];
|
2014-01-14 10:23:42 +00:00
|
|
|
if (p2pSupported) {
|
|
|
|
interfaces.push(WifiP2pManager.INTERFACE_NAME);
|
|
|
|
}
|
2013-10-14 08:33:59 +00:00
|
|
|
wifiService.start(wifiListener, interfaces, interfaces.length);
|
|
|
|
} else {
|
|
|
|
debug("No wifi service component available!");
|
|
|
|
}
|
|
|
|
|
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;
|
2013-10-14 08:33:59 +00:00
|
|
|
if (callback) {
|
2012-04-09 07:44:53 +00:00
|
|
|
controlCallbacks[id] = callback;
|
2013-10-14 08:33:59 +00:00
|
|
|
}
|
|
|
|
wifiService.sendCommand(obj, obj.iface);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 08:33:59 +00:00
|
|
|
let onmessageresult = function(data, iface) {
|
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;
|
|
|
|
|
2013-10-14 08:33:59 +00:00
|
|
|
function waitForEvent(iface) {
|
|
|
|
wifiService.waitForEvent(iface);
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.loadDriver(function (status) {
|
2012-07-23 21:41:29 +00:00
|
|
|
driverLoaded = (status >= 0);
|
|
|
|
callback(status)
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 02:39:13 +00:00
|
|
|
function unloadDriver(type, 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.
|
2014-01-10 02:39:13 +00:00
|
|
|
if (type === WIFI_FIRMWARE_STATION) {
|
|
|
|
notify("supplicantlost", { success: true });
|
|
|
|
}
|
2013-11-22 09:49:56 +00:00
|
|
|
callback(0);
|
2013-04-11 03:52:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-23 21:41:29 +00:00
|
|
|
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.unloadDriver(function(status) {
|
2012-07-23 21:41:29 +00:00
|
|
|
driverLoaded = (status < 0);
|
2014-01-10 02:39:13 +00:00
|
|
|
if (type === WIFI_FIRMWARE_STATION) {
|
|
|
|
notify("supplicantlost", { success: true });
|
|
|
|
}
|
2013-11-22 09:49:56 +00:00
|
|
|
callback(status);
|
2012-07-23 21:41:29 +00:00
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.setBackgroundScan(manager.backgroundScanEnabled, callback);
|
2012-10-25 23:27:10 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
var scanModeActive = false;
|
|
|
|
|
2013-09-30 03:05:05 +00:00
|
|
|
function scan(forceActive, callback) {
|
2012-04-09 07:44:53 +00:00
|
|
|
if (forceActive && !scanModeActive) {
|
2012-07-12 22:09:18 +00:00
|
|
|
// Note: we ignore errors from doSetScanMode.
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.doSetScanMode(true, function(ignore) {
|
2012-10-25 23:27:10 +00:00
|
|
|
setBackgroundScan("OFF", function(turned, ignore) {
|
|
|
|
reEnableBackgroundScan = turned;
|
2014-01-14 10:23:42 +00:00
|
|
|
manager.handlePreWifiScan();
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.scan(function(ok) {
|
|
|
|
wifiCommand.doSetScanMode(false, function(ignore) {
|
2012-10-25 23:27:10 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2014-01-14 10:23:42 +00:00
|
|
|
manager.handlePreWifiScan();
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.scan(callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-31 00:35:34 +00:00
|
|
|
var debugEnabled = false;
|
|
|
|
|
|
|
|
function syncDebug() {
|
|
|
|
if (debugEnabled !== DEBUG) {
|
|
|
|
let wanted = DEBUG;
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.setLogLevel(wanted ? "DEBUG" : "INFO", function(ok) {
|
2012-10-31 00:35:34 +00:00
|
|
|
if (ok)
|
|
|
|
debugEnabled = wanted;
|
|
|
|
});
|
2014-02-25 08:43:36 +00:00
|
|
|
if (p2pSupported && p2pManager) {
|
|
|
|
p2pManager.setDebug(DEBUG);
|
|
|
|
}
|
2012-10-31 00:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDebugEnabled(callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.getLogLevel(function(level) {
|
2012-10-31 00:35:34 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:05:05 +00:00
|
|
|
function setScanMode(setActive, callback) {
|
2012-04-09 07:44:53 +00:00
|
|
|
scanModeActive = setActive;
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.doSetScanMode(setActive, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-11-15 09:35:33 +00:00
|
|
|
gNetworkService.setNetworkProxy(network);
|
2013-08-22 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-08-13 07:30:01 +00:00
|
|
|
info.ipaddr = netHelpers.stringToIP(info.ipaddr_str);
|
|
|
|
info.gateway = netHelpers.stringToIP(info.gateway_str);
|
|
|
|
info.mask_str = netHelpers.ipToString(netHelpers.makeMask(info.maskLength));
|
2013-06-24 03:02:30 +00:00
|
|
|
|
|
|
|
// Optional
|
2014-08-13 07:30:01 +00:00
|
|
|
info.dns1 = netHelpers.stringToIP(info.dns1_str);
|
|
|
|
info.dns2 = netHelpers.stringToIP(info.dns2_str);
|
|
|
|
info.proxy = netHelpers.stringToIP(info.proxy_str);
|
2013-06-24 03:02:30 +00:00
|
|
|
|
|
|
|
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.
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.disableInterface(manager.ifname, function (ok) {
|
|
|
|
netUtil.enableInterface(manager.ifname, function (ok) {
|
2013-06-24 03:02:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
var dhcpInfo = null;
|
2013-06-24 03:02:30 +00:00
|
|
|
|
|
|
|
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) {
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.stopDhcp(manager.ifname, function() {});
|
2013-06-24 03:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set ip, mask length, gateway, dns to network interface
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.configureInterface( { ifname: ifname,
|
|
|
|
ipaddr: staticIpInfo.ipaddr,
|
|
|
|
mask: staticIpInfo.maskLength,
|
|
|
|
gateway: staticIpInfo.gateway,
|
|
|
|
dns1: staticIpInfo.dns1,
|
|
|
|
dns2: staticIpInfo.dns2 }, function (data) {
|
|
|
|
netUtil.runIpConfig(ifname, staticIpInfo, function(data) {
|
|
|
|
dhcpInfo = data.info;
|
|
|
|
notify("networkconnected", data);
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-22 09:49:56 +00:00
|
|
|
fields.prevState = manager.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);
|
2013-11-22 09:49:56 +00:00
|
|
|
|
|
|
|
// Don't update state when and after disabling.
|
|
|
|
if (manager.state === "DISABLING" ||
|
|
|
|
manager.state === "UNINITIALIZED") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.state = fields.state;
|
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;
|
2014-04-22 12:22:03 +00:00
|
|
|
recvErrors = 0;
|
2014-04-10 05:52:38 +00:00
|
|
|
manager.connectToSupplicant = true;
|
2012-08-30 14:08:52 +00:00
|
|
|
didConnectSupplicant(function(){});
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-07 11:29:55 +00:00
|
|
|
if (connectTries++ < 5) {
|
|
|
|
// Try again in 1 seconds.
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!retryTimer)
|
|
|
|
retryTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
retryTimer.initWithCallback(function(timer) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.connectToSupplicant(connectCallback);
|
2014-03-07 11:29:55 +00:00
|
|
|
}, 1000, 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
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.configureInterface( { ifname: manager.ifname,
|
|
|
|
ipaddr: 0,
|
|
|
|
mask: 0,
|
|
|
|
gateway: 0,
|
|
|
|
dns1: 0,
|
|
|
|
dns2: 0 }, function (data) {
|
2013-05-02 10:02:33 +00:00
|
|
|
});
|
|
|
|
|
2012-04-09 04:13:28 +00:00
|
|
|
// If we got disconnected, kill the DHCP client in preparation for
|
|
|
|
// reconnection.
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.resetConnections(manager.ifname, function() {
|
|
|
|
netUtil.stopDhcp(manager.ifname, function() {
|
2012-04-09 04:13:28 +00:00
|
|
|
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);
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.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;
|
|
|
|
}
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.runDhcp(manager.ifname, function(data) {
|
|
|
|
dhcpInfo = data.info;
|
2013-10-18 08:46:30 +00:00
|
|
|
if (!dhcpInfo) {
|
|
|
|
if (++manager.dhcpFailuresCount >= MAX_RETRIES_ON_DHCP_FAILURE) {
|
|
|
|
manager.dhcpFailuresCount = 0;
|
2014-06-20 02:37:43 +00:00
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
2013-10-18 08:46:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// NB: We have to call disconnect first. Otherwise, we only reauth with
|
|
|
|
// the existing AP and don't retrigger DHCP.
|
|
|
|
manager.disconnect(function() {
|
|
|
|
manager.reassociate(function(){});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.dhcpFailuresCount = 0;
|
2013-09-30 03:05:05 +00:00
|
|
|
notify("networkconnected", 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" };
|
|
|
|
|
2014-06-20 02:37:43 +00:00
|
|
|
manager.getNetworkId = function (ssid, callback) {
|
2012-11-23 10:30:33 +00:00
|
|
|
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
|
2014-06-20 02:37:43 +00:00
|
|
|
// 1. network matching SSID if SSID is provided.
|
|
|
|
// 2. current network if no SSID is provided, it's not guaranteed that
|
|
|
|
// current network matches requested SSID.
|
|
|
|
if ((!ssid && network.status === "CURRENT") ||
|
2014-08-07 10:00:23 +00:00
|
|
|
(ssid && network.ssid && ssid === dequote(network.ssid))) {
|
2012-11-23 10:30:33 +00:00
|
|
|
return callback(net);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-04-25 10:41:26 +00:00
|
|
|
function handleWpaEapEvents(event) {
|
|
|
|
if (event.indexOf("CTRL-EVENT-EAP-FAILURE") !== -1) {
|
|
|
|
if (event.indexOf("EAP authentication failed") !== -1) {
|
|
|
|
notify("passwordmaybeincorrect");
|
2014-06-20 02:37:43 +00:00
|
|
|
if (manager.authenticationFailuresCount > MAX_RETRIES_ON_AUTHENTICATION_FAILURE) {
|
|
|
|
manager.authenticationFailuresCount = 0;
|
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
|
|
|
}
|
2014-04-25 10:41:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (event.indexOf("CTRL-EVENT-EAP-TLS-CERT-ERROR") !== -1) {
|
|
|
|
// Cert Error
|
2014-06-20 02:37:43 +00:00
|
|
|
notify("passwordmaybeincorrect");
|
|
|
|
if (manager.authenticationFailuresCount > MAX_RETRIES_ON_AUTHENTICATION_FAILURE) {
|
|
|
|
manager.authenticationFailuresCount = 0;
|
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
|
|
|
}
|
2014-04-25 10:41:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (event.indexOf("CTRL-EVENT-EAP-STARTED") !== -1) {
|
|
|
|
notifyStateChange({ state: "AUTHENTICATING" });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
2014-06-20 02:37:43 +00:00
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
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) {
|
2013-11-08 08:16:16 +00:00
|
|
|
// As long the monitor socket is not closed and we haven't seen too many
|
|
|
|
// recv errors yet, we will keep going for a bit longer.
|
2014-04-22 12:22:03 +00:00
|
|
|
if (event.indexOf("connection closed") === -1 &&
|
|
|
|
event.indexOf("recv error") !== -1 && ++recvErrors < 10)
|
2012-04-09 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
|
2012-05-11 20:05:51 +00:00
|
|
|
notifyStateChange({ state: "DISCONNECTED", BSSID: null, id: -1 });
|
2013-11-08 08:16:16 +00:00
|
|
|
|
|
|
|
// If the supplicant is terminated as commanded, the supplicant lost
|
|
|
|
// notification will be sent after driver unloaded. In such case, the
|
|
|
|
// manager state will be "DISABLING" or "UNINITIALIZED".
|
|
|
|
// So if supplicant terminated with incorrect manager state, implying
|
|
|
|
// unexpected condition, we should notify supplicant lost here.
|
|
|
|
if (manager.state !== "DISABLING" && manager.state !== "UNINITIALIZED") {
|
|
|
|
notify("supplicantlost", { success: true });
|
|
|
|
}
|
2014-05-06 05:40:07 +00:00
|
|
|
|
|
|
|
if (manager.stopSupplicantCallback) {
|
|
|
|
cancelWaitForTerminateEventTimer();
|
|
|
|
// It's possible that the terminating event triggered by timer comes
|
|
|
|
// earlier than the event from wpa_supplicant. Since
|
|
|
|
// stopSupplicantCallback contains async. callbacks, swap it to local
|
|
|
|
// to prevent calling the callback twice.
|
|
|
|
let stopSupplicantCallback = manager.stopSupplicantCallback.bind(manager);
|
|
|
|
manager.stopSupplicantCallback = null;
|
|
|
|
stopSupplicantCallback();
|
|
|
|
stopSupplicantCallback = null;
|
|
|
|
}
|
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;
|
2014-06-20 02:37:43 +00:00
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
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;
|
|
|
|
}
|
|
|
|
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() {});
|
|
|
|
}
|
2014-01-14 10:23:42 +00:00
|
|
|
manager.handlePostWifiScan();
|
2012-04-09 07:44:53 +00:00
|
|
|
notify("scanresultsavailable");
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-25 10:41:26 +00:00
|
|
|
if (eventData.indexOf("CTRL-EVENT-EAP") === 0) {
|
|
|
|
return handleWpaEapEvents(event);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-30 14:08:52 +00:00
|
|
|
function didConnectSupplicant(callback) {
|
2013-10-14 08:33:59 +00:00
|
|
|
waitForEvent(manager.ifname);
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
// Load up the supplicant state.
|
2012-10-31 00:35:34 +00:00
|
|
|
getDebugEnabled(function(ok) {
|
|
|
|
syncDebug();
|
|
|
|
});
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.status(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();
|
|
|
|
});
|
2014-01-14 10:23:42 +00:00
|
|
|
|
|
|
|
if (p2pSupported) {
|
|
|
|
manager.enableP2p(function(success) {});
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.killSupplicant(function() {
|
|
|
|
netUtil.disableInterface(manager.ifname, function (ok) {
|
2012-08-30 14:08:52 +00:00
|
|
|
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.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;
|
2013-10-18 08:46:30 +00:00
|
|
|
manager.dhcpFailuresCount = 0;
|
2014-05-06 05:40:07 +00:00
|
|
|
manager.stopSupplicantCallback = null;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
manager.__defineGetter__("enabled", function() {
|
|
|
|
switch (manager.state) {
|
|
|
|
case "UNINITIALIZED":
|
|
|
|
case "INITIALIZING":
|
|
|
|
case "DISABLING":
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-06 05:40:07 +00:00
|
|
|
var waitForTerminateEventTimer = null;
|
|
|
|
function cancelWaitForTerminateEventTimer() {
|
|
|
|
if (waitForTerminateEventTimer) {
|
|
|
|
waitForTerminateEventTimer.cancel();
|
|
|
|
waitForTerminateEventTimer = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function createWaitForTerminateEventTimer(onTimeout) {
|
|
|
|
if (waitForTerminateEventTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
waitForTerminateEventTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
waitForTerminateEventTimer.initWithCallback(onTimeout,
|
|
|
|
4000,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
};
|
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.
|
2014-04-15 03:43:51 +00:00
|
|
|
manager.setWifiEnabled = function(enabled, callback) {
|
|
|
|
if (enabled === manager.isWifiEnabled(manager.state)) {
|
2012-06-28 10:01:51 +00:00
|
|
|
callback("no change");
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-15 03:43:51 +00:00
|
|
|
if (enabled) {
|
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;
|
2014-03-29 07:18:24 +00:00
|
|
|
WifiNetworkInterface.ips = [];
|
|
|
|
WifiNetworkInterface.prefixLengths = [];
|
|
|
|
WifiNetworkInterface.gateways = [];
|
|
|
|
WifiNetworkInterface.dnses = [];
|
2014-07-29 08:44:27 +00:00
|
|
|
gNetworkManager.updateNetworkInterface(WifiNetworkInterface);
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
prepareForStartup(function() {
|
|
|
|
loadDriver(function (status) {
|
|
|
|
if (status < 0) {
|
|
|
|
callback(status);
|
|
|
|
manager.state = "UNINITIALIZED";
|
|
|
|
return;
|
|
|
|
}
|
2013-12-19 06:22:10 +00:00
|
|
|
// This command is mandatory for Nexus 4. But some devices like
|
|
|
|
// Galaxy S2 don't support it. Continue to start wpa_supplicant
|
|
|
|
// even if we fail to set wifi operation mode to station.
|
2013-11-15 09:35:33 +00:00
|
|
|
gNetworkService.setWifiOperationMode(manager.ifname,
|
2013-07-29 09:59:53 +00:00
|
|
|
WIFI_FIRMWARE_STATION,
|
|
|
|
function (status) {
|
2014-04-10 05:52:38 +00:00
|
|
|
|
|
|
|
function startSupplicantInternal() {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.startSupplicant(function (status) {
|
2013-07-29 09:59:53 +00:00
|
|
|
if (status < 0) {
|
2014-01-10 02:39:13 +00:00
|
|
|
unloadDriver(WIFI_FIRMWARE_STATION, function() {
|
2013-07-29 09:59:53 +00:00
|
|
|
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;
|
2013-09-30 03:05:05 +00:00
|
|
|
netUtil.enableInterface(manager.ifname, function (ok) {
|
2013-07-29 09:59:53 +00:00
|
|
|
callback(ok ? 0 : -1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-04-10 05:52:38 +00:00
|
|
|
|
|
|
|
function doStartSupplicant() {
|
|
|
|
cancelWaitForDriverReadyTimer();
|
|
|
|
|
|
|
|
if (!manager.connectToSupplicant) {
|
|
|
|
startSupplicantInternal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wifiCommand.closeSupplicantConnection(function () {
|
|
|
|
manager.connectToSupplicant = false;
|
2014-04-15 03:43:51 +00:00
|
|
|
// closeSupplicantConnection() will trigger onsupplicantlost
|
|
|
|
// and set manager.state to "UNINITIALIZED", we have to
|
|
|
|
// restore it here.
|
|
|
|
manager.state = "INITIALIZING";
|
2014-04-10 05:52:38 +00:00
|
|
|
startSupplicantInternal();
|
|
|
|
});
|
|
|
|
}
|
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 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 {
|
2014-03-27 05:58:04 +00:00
|
|
|
manager.state = "DISABLING";
|
2012-04-09 07:44:53 +00:00
|
|
|
// 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.
|
2014-01-14 10:23:42 +00:00
|
|
|
let doDisableWifi = function() {
|
2014-05-06 05:40:07 +00:00
|
|
|
manager.stopSupplicantCallback = (function () {
|
|
|
|
wifiCommand.stopSupplicant(function (status) {
|
|
|
|
wifiCommand.closeSupplicantConnection(function() {
|
2014-07-16 08:10:21 +00:00
|
|
|
manager.connectToSupplicant = false;
|
2014-03-27 05:58:04 +00:00
|
|
|
manager.state = "UNINITIALIZED";
|
|
|
|
netUtil.disableInterface(manager.ifname, function (ok) {
|
|
|
|
unloadDriver(WIFI_FIRMWARE_STATION, callback);
|
2012-04-09 04:13:28 +00:00
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-06 05:40:07 +00:00
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
let terminateEventCallback = (function() {
|
|
|
|
handleEvent("CTRL-EVENT-TERMINATING");
|
|
|
|
}).bind(this);
|
|
|
|
createWaitForTerminateEventTimer(terminateEventCallback);
|
|
|
|
|
2014-07-16 08:10:21 +00:00
|
|
|
// We are going to terminate the connection between wpa_supplicant.
|
|
|
|
// Stop the polling timer immediately to prevent connection info update
|
|
|
|
// command blocking in control thread until socket timeout.
|
|
|
|
notify("stopconnectioninfotimer");
|
|
|
|
|
2014-05-06 05:40:07 +00:00
|
|
|
wifiCommand.terminateSupplicant(function (ok) {
|
|
|
|
manager.connectionDropped(function () {
|
|
|
|
});
|
2012-04-09 07:44:53 +00:00
|
|
|
});
|
2014-01-14 10:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p2pSupported) {
|
|
|
|
p2pManager.setEnabled(false, { onDisabled: doDisableWifi });
|
|
|
|
} else {
|
|
|
|
doDisableWifi();
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 08:37:40 +00:00
|
|
|
var wifiHotspotStatusTimer = null;
|
|
|
|
function cancelWifiHotspotStatusTimer() {
|
|
|
|
if (wifiHotspotStatusTimer) {
|
|
|
|
wifiHotspotStatusTimer.cancel();
|
|
|
|
wifiHotspotStatusTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWifiHotspotStatusTimer(onTimeout) {
|
|
|
|
wifiHotspotStatusTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
wifiHotspotStatusTimer.init(onTimeout, 5000, Ci.nsITimer.TYPE_REPEATING_SLACK);
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-04-15 03:43:51 +00:00
|
|
|
if (enabled === manager.isWifiTetheringEnabled(manager.tetheringState)) {
|
|
|
|
callback("no change");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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";
|
2014-01-22 08:37:40 +00:00
|
|
|
if (wifiHotspotStatusTimer) {
|
|
|
|
cancelWifiHotspotStatusTimer();
|
|
|
|
wifiCommand.closeHostapdConnection(function(result) {
|
|
|
|
});
|
|
|
|
}
|
2012-08-29 19:31:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-26 09:56:44 +00:00
|
|
|
|
2014-01-22 08:37:40 +00:00
|
|
|
function getWifiHotspotStatus() {
|
|
|
|
wifiCommand.hostapdGetStations(function(result) {
|
2014-07-04 06:37:55 +00:00
|
|
|
notify("stationinfoupdate", {station: result});
|
2014-01-22 08:37:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:59:53 +00:00
|
|
|
function doStartWifiTethering() {
|
|
|
|
cancelWaitForDriverReadyTimer();
|
2014-08-28 10:34:38 +00:00
|
|
|
WifiNetworkInterface.name = libcutils.property_get("wifi.tethering.interface", manager.ifname);
|
2013-07-29 09:59:53 +00:00
|
|
|
gNetworkManager.setWifiTethering(enabled, WifiNetworkInterface,
|
|
|
|
configuration, function(result) {
|
|
|
|
if (result) {
|
|
|
|
manager.tetheringState = "UNINITIALIZED";
|
|
|
|
} else {
|
|
|
|
manager.tetheringState = "COMPLETED";
|
2014-01-22 08:37:40 +00:00
|
|
|
wifiCommand.connectToHostapd(function(result) {
|
|
|
|
if (result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Create a timer to track the connection status.
|
|
|
|
createWifiHotspotStatusTimer(getWifiHotspotStatus);
|
|
|
|
});
|
2013-07-29 09:59:53 +00:00
|
|
|
}
|
|
|
|
// 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 {
|
2014-07-11 03:45:55 +00:00
|
|
|
cancelWifiHotspotStatusTimer();
|
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.
|
2014-01-10 02:39:13 +00:00
|
|
|
unloadDriver(WIFI_FIRMWARE_AP, function(status) {
|
2012-09-13 07:19:47 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:05:05 +00:00
|
|
|
manager.disconnect = wifiCommand.disconnect;
|
|
|
|
manager.reconnect = wifiCommand.reconnect;
|
|
|
|
manager.reassociate = wifiCommand.reassociate;
|
2012-04-09 07:44:53 +00:00
|
|
|
|
|
|
|
var networkConfigurationFields = [
|
2014-08-29 03:18:02 +00:00
|
|
|
{name: "ssid", type: "string"},
|
|
|
|
{name: "bssid", type: "string"},
|
|
|
|
{name: "psk", type: "string"},
|
|
|
|
{name: "wep_key0", type: "string"},
|
|
|
|
{name: "wep_key1", type: "string"},
|
|
|
|
{name: "wep_key2", type: "string"},
|
|
|
|
{name: "wep_key3", type: "string"},
|
|
|
|
{name: "wep_tx_keyidx", type: "integer"},
|
|
|
|
{name: "priority", type: "integer"},
|
|
|
|
{name: "key_mgmt", type: "string"},
|
|
|
|
{name: "scan_ssid", type: "string"},
|
|
|
|
{name: "disabled", type: "integer"},
|
|
|
|
{name: "identity", type: "string"},
|
|
|
|
{name: "password", type: "string"},
|
|
|
|
{name: "auth_alg", type: "string"},
|
|
|
|
{name: "phase1", type: "string"},
|
|
|
|
{name: "phase2", type: "string"},
|
|
|
|
{name: "eap", type: "string"},
|
|
|
|
{name: "pin", type: "string"},
|
|
|
|
{name: "pcsc", type: "string"},
|
|
|
|
{name: "ca_cert", type: "string"},
|
|
|
|
{name: "subject_match", type: "string"}
|
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) {
|
2014-08-29 03:18:02 +00:00
|
|
|
let fieldName = networkConfigurationFields[n].name;
|
|
|
|
let fieldType = networkConfigurationFields[n].type;
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.getNetworkVariable(netId, fieldName, function(value) {
|
2014-08-29 03:18:02 +00:00
|
|
|
if (value !== null) {
|
|
|
|
if (fieldType === "integer") {
|
|
|
|
config[fieldName] = parseInt(value, 10);
|
|
|
|
} else {
|
|
|
|
// value is string type by default.
|
|
|
|
config[fieldName] = value;
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
if (++done == networkConfigurationFields.length)
|
|
|
|
callback(config);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
manager.setNetworkConfiguration = function(config, callback) {
|
|
|
|
var netId = config.netId;
|
|
|
|
var done = 0;
|
|
|
|
var errors = 0;
|
2014-04-30 11:58:54 +00:00
|
|
|
|
|
|
|
function hasValidProperty(name) {
|
|
|
|
return ((name in config) &&
|
|
|
|
config[name] != null &&
|
2014-07-16 10:06:13 +00:00
|
|
|
(["password", "wep_key0", "psk"].indexOf(name) === -1 ||
|
2014-04-30 11:58:54 +00:00
|
|
|
config[name] !== '*'));
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
for (var n = 0; n < networkConfigurationFields.length; ++n) {
|
2014-08-29 03:18:02 +00:00
|
|
|
let fieldName = networkConfigurationFields[n].name;
|
2014-04-30 11:58:54 +00:00
|
|
|
if (!hasValidProperty(fieldName)) {
|
2012-04-09 07:44:53 +00:00
|
|
|
++done;
|
|
|
|
} else {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.setNetworkVariable(netId, fieldName, config[fieldName], function(ok) {
|
2012-04-09 07:44:53 +00:00
|
|
|
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) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.listNetworks(function (reply) {
|
2012-04-09 07:44:53 +00:00
|
|
|
var networks = Object.create(null);
|
2014-02-13 16:51:12 +00:00
|
|
|
var lines = reply ? reply.split("\n") : 0;
|
|
|
|
if (lines.length <= 1) {
|
2012-04-09 07:44:53 +00:00
|
|
|
// 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");
|
2014-06-20 02:37:43 +00:00
|
|
|
var netId = parseInt(result[0], 10);
|
2012-04-09 07:44:53 +00:00
|
|
|
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.
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.removeNetwork(netId, function() {
|
2012-04-09 07:44:53 +00:00
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(networks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
manager.addNetwork = function(config, callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.addNetwork(function (netId) {
|
2012-04-09 07:44:53 +00:00
|
|
|
config.netId = netId;
|
|
|
|
manager.setNetworkConfiguration(config, function (ok) {
|
|
|
|
if (!ok) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.removeNetwork(netId, function() { callback(false); });
|
2012-04-09 07:44:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(ok);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
manager.updateNetwork = function(config, callback) {
|
|
|
|
manager.setNetworkConfiguration(config, callback);
|
|
|
|
}
|
|
|
|
manager.removeNetwork = function(netId, callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.removeNetwork(netId, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
manager.saveConfig = function(callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.saveConfig(callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
manager.enableNetwork = function(netId, disableOthers, callback) {
|
2014-01-14 10:23:42 +00:00
|
|
|
if (p2pSupported) {
|
|
|
|
// We have to stop wifi direct scan before associating to an AP.
|
|
|
|
// Otherwise we will get a "REJECT" wpa supplicant event.
|
|
|
|
p2pManager.setScanEnabled(false, function(success) {});
|
|
|
|
}
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.enableNetwork(netId, disableOthers, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
manager.disableNetwork = function(netId, callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
wifiCommand.disableNetwork(netId, callback);
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
2013-09-30 03:05:05 +00:00
|
|
|
manager.getMacAddress = wifiCommand.getMacAddress;
|
|
|
|
manager.getScanResults = wifiCommand.scanResults;
|
2012-04-09 07:44:53 +00:00
|
|
|
manager.setScanMode = function(mode, callback) {
|
2013-09-30 03:05:05 +00:00
|
|
|
setScanMode(mode === "active", callback); // Use our own version.
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
2013-09-30 03:05:05 +00:00
|
|
|
manager.setBackgroundScan = setBackgroundScan; // Use our own version.
|
|
|
|
manager.scan = scan; // Use our own version.
|
|
|
|
manager.wpsPbc = wifiCommand.wpsPbc;
|
|
|
|
manager.wpsPin = wifiCommand.wpsPin;
|
|
|
|
manager.wpsCancel = wifiCommand.wpsCancel;
|
2013-07-09 20:27:58 +00:00
|
|
|
manager.setPowerMode = (sdkVersion >= 16)
|
2013-09-30 03:05:05 +00:00
|
|
|
? wifiCommand.setPowerModeJB
|
|
|
|
: wifiCommand.setPowerModeICS;
|
2013-08-22 10:22:25 +00:00
|
|
|
manager.getHttpProxyNetwork = getHttpProxyNetwork;
|
|
|
|
manager.setHttpProxy = setHttpProxy;
|
|
|
|
manager.configureHttpProxy = configureHttpProxy;
|
2014-01-10 04:12:38 +00:00
|
|
|
manager.setSuspendOptimizations = (sdkVersion >= 16)
|
|
|
|
? wifiCommand.setSuspendOptimizationsJB
|
|
|
|
: wifiCommand.setSuspendOptimizationsICS;
|
2013-06-24 03:02:30 +00:00
|
|
|
manager.setStaticIpMode = setStaticIpMode;
|
2013-09-30 03:05:05 +00:00
|
|
|
manager.getRssiApprox = wifiCommand.getRssiApprox;
|
|
|
|
manager.getLinkSpeed = wifiCommand.getLinkSpeed;
|
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)
|
2013-09-30 03:05:05 +00:00
|
|
|
? wifiCommand.getConnectionInfoICS
|
|
|
|
: wifiCommand.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;
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 03:43:51 +00:00
|
|
|
|
|
|
|
manager.isWifiEnabled = function(state) {
|
|
|
|
switch (state) {
|
|
|
|
case "UNINITIALIZED":
|
|
|
|
case "DISABLING":
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.isWifiTetheringEnabled = function(state) {
|
|
|
|
switch (state) {
|
|
|
|
case "UNINITIALIZED":
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-06-20 02:37:43 +00:00
|
|
|
notify("disconnected", {connectionInfo: manager.connectionInfo});
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 10:23:42 +00:00
|
|
|
manager.handlePreWifiScan = function() {
|
|
|
|
if (p2pSupported) {
|
|
|
|
// Before doing regular wifi scan, we have to disable wifi direct
|
|
|
|
// scan first. Otherwise we will never get the scan result.
|
|
|
|
p2pManager.blockScan();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
manager.handlePostWifiScan = function() {
|
|
|
|
if (p2pSupported) {
|
|
|
|
// After regular wifi scanning, we should restore the restricted
|
|
|
|
// wifi direct scan.
|
|
|
|
p2pManager.unblockScan();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Public APIs for P2P.
|
|
|
|
//
|
|
|
|
|
|
|
|
manager.p2pSupported = function() {
|
|
|
|
return p2pSupported;
|
|
|
|
};
|
|
|
|
|
|
|
|
manager.getP2pManager = function() {
|
|
|
|
return p2pManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
manager.enableP2p = function(callback) {
|
|
|
|
p2pManager.setEnabled(true, {
|
|
|
|
onSupplicantConnected: function() {
|
2014-03-27 05:58:04 +00:00
|
|
|
waitForEvent(WifiP2pManager.INTERFACE_NAME);
|
2014-01-14 10:23:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onEnabled: function(success) {
|
|
|
|
callback(success);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-03-20 06:39:21 +00:00
|
|
|
manager.getCapabilities = function() {
|
|
|
|
return capabilities;
|
|
|
|
}
|
|
|
|
|
2014-04-30 11:58:40 +00:00
|
|
|
// Cert Services
|
|
|
|
let wifiCertService = Cc["@mozilla.org/wifi/certservice;1"];
|
|
|
|
if (wifiCertService) {
|
|
|
|
wifiCertService = wifiCertService.getService(Ci.nsIWifiCertService);
|
|
|
|
wifiCertService.start(wifiListener);
|
|
|
|
} else {
|
|
|
|
debug("No wifi CA service component available");
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.importCert = function(caInfo, callback) {
|
|
|
|
var id = idgen++;
|
|
|
|
if (callback) {
|
|
|
|
controlCallbacks[id] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
wifiCertService.importCert(id, caInfo.certBlob, caInfo.certPassword,
|
|
|
|
caInfo.certNickname);
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:50:11 +00:00
|
|
|
manager.deleteCert = function(caInfo, callback) {
|
|
|
|
var id = idgen++;
|
|
|
|
if (callback) {
|
|
|
|
controlCallbacks[id] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
wifiCertService.deleteCert(id, caInfo.certNickname);
|
|
|
|
}
|
|
|
|
|
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";
|
2014-06-13 04:00:10 +00:00
|
|
|
} else if (key_mgmt.indexOf("WPA-EAP") != -1) {
|
2012-11-08 05:34:12 +00:00
|
|
|
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",
|
2014-04-30 11:58:54 +00:00
|
|
|
phase2: "rw",
|
|
|
|
serverCertificate: "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,
|
|
|
|
|
2014-03-29 07:18:24 +00:00
|
|
|
ips: [],
|
2012-08-14 01:54:42 +00:00
|
|
|
|
2014-03-29 07:18:24 +00:00
|
|
|
prefixLengths: [],
|
2012-08-14 01:54:42 +00:00
|
|
|
|
2014-03-29 07:18:24 +00:00
|
|
|
dnses: [],
|
2012-08-14 01:54:42 +00:00
|
|
|
|
2014-03-29 07:18:24 +00:00
|
|
|
gateways: [],
|
2012-08-14 01:54:42 +00:00
|
|
|
|
2012-08-01 14:55:43 +00:00
|
|
|
httpProxyHost: null,
|
|
|
|
|
|
|
|
httpProxyPort: null,
|
|
|
|
|
2014-03-29 07:18:24 +00:00
|
|
|
getAddresses: function (ips, prefixLengths) {
|
|
|
|
ips.value = this.ips.slice();
|
|
|
|
prefixLengths.value = this.prefixLengths.slice();
|
|
|
|
|
|
|
|
return this.ips.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
getGateways: function (count) {
|
|
|
|
if (count) {
|
|
|
|
count.value = this.gateways.length;
|
|
|
|
}
|
|
|
|
return this.gateways.slice();
|
|
|
|
},
|
|
|
|
|
|
|
|
getDnses: function (count) {
|
|
|
|
if (count) {
|
|
|
|
count.value = this.dnses.length;
|
|
|
|
}
|
|
|
|
return this.dnses.slice();
|
|
|
|
}
|
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",
|
2014-04-30 11:58:40 +00:00
|
|
|
"WifiManager:importCert",
|
2014-04-30 11:58:45 +00:00
|
|
|
"WifiManager:getImportedCerts",
|
2014-04-28 07:50:11 +00:00
|
|
|
"WifiManager:deleteCert",
|
2014-08-07 09:19:31 +00:00
|
|
|
"WifiManager:setWifiEnabled",
|
2014-08-11 09:13:25 +00:00
|
|
|
"WifiManager:setWifiTethering",
|
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);
|
2014-07-31 02:31:18 +00:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2012-09-04 04:26:56 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
this.wantScanResults = [];
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-01-14 10:23:42 +00:00
|
|
|
// Create p2pObserver and assign to p2pManager.
|
|
|
|
if (WifiManager.p2pSupported()) {
|
|
|
|
this._p2pObserver = WifiP2pWorkerObserver(WifiManager.getP2pManager());
|
|
|
|
WifiManager.getP2pManager().setObserver(this._p2pObserver);
|
|
|
|
|
|
|
|
// Add DOM message observerd by p2pObserver to the message listener as well.
|
|
|
|
this._p2pObserver.getObservedDOMMessages().forEach((function(msgName) {
|
|
|
|
this._mm.addMessageListener(msgName, this);
|
|
|
|
}).bind(this));
|
|
|
|
}
|
|
|
|
|
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!");
|
2014-01-14 10:23:42 +00:00
|
|
|
WifiManager.handlePostWifiScan();
|
2012-11-05 22:29:07 +00:00
|
|
|
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"] :
|
2014-06-13 04:00:10 +00:00
|
|
|
(net.key_mgmt && net.key_mgmt !== "NONE") ? [net.key_mgmt.split(" ")[0]] :
|
2013-07-08 11:41:48 +00:00
|
|
|
[];
|
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;
|
2014-04-30 11:58:54 +00:00
|
|
|
if ("ca_cert" in net && net.ca_cert &&
|
|
|
|
net.ca_cert.indexOf("keystore://WIFI_SERVERCERT_" === 0)) {
|
|
|
|
pub.serverCertificate = net.ca_cert.substr(27);
|
|
|
|
}
|
2014-05-19 06:41:08 +00:00
|
|
|
if(net.subject_match) {
|
|
|
|
pub.subjectMatch = net.subject_match;
|
|
|
|
}
|
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";
|
2014-06-13 04:00:10 +00:00
|
|
|
} else if (net.keyManagement === "WPA-EAP") {
|
|
|
|
net.keyManagement += " IEEE8021X";
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2014-04-30 11:58:54 +00:00
|
|
|
function hasValidProperty(name) {
|
|
|
|
return ((name in net) && net[name] != null);
|
2013-08-05 20:25:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 11:58:54 +00:00
|
|
|
if (hasValidProperty("eap")) {
|
|
|
|
if (hasValidProperty("pin")) {
|
|
|
|
net.pin = quote(net.pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasValidProperty("phase1"))
|
|
|
|
net.phase1 = quote(net.phase1);
|
2013-05-30 03:17:07 +00:00
|
|
|
|
2014-04-30 11:58:54 +00:00
|
|
|
if (hasValidProperty("phase2")) {
|
2014-06-24 09:41:38 +00:00
|
|
|
if (net.phase2 === "TLS") {
|
2014-04-30 11:58:54 +00:00
|
|
|
net.phase2 = quote("autheap=" + net.phase2);
|
2014-06-24 09:41:38 +00:00
|
|
|
} else { // PAP, MSCHAPV2, etc.
|
|
|
|
net.phase2 = quote("auth=" + net.phase2);
|
2014-04-30 11:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasValidProperty("serverCertificate"))
|
|
|
|
net.ca_cert = quote("keystore://WIFI_SERVERCERT_" + net.serverCertificate);
|
2014-05-19 06:41:08 +00:00
|
|
|
|
|
|
|
if (hasValidProperty("subjectMatch"))
|
|
|
|
net.subject_match = quote(net.subjectMatch);
|
2014-04-30 11:58:54 +00:00
|
|
|
}
|
2013-05-30 03:17:07 +00:00
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
return net;
|
|
|
|
};
|
|
|
|
|
|
|
|
WifiManager.onsupplicantconnection = function() {
|
|
|
|
debug("Connected to supplicant");
|
2014-06-11 08:37:28 +00:00
|
|
|
// Give it a state other than UNINITIALIZED, INITIALIZING or DISABLING
|
|
|
|
// defined in getter function of WifiManager.enabled. It implies that
|
|
|
|
// we are ready to accept dom request from applications.
|
|
|
|
WifiManager.state = "DISCONNECTED";
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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 });
|
2014-04-15 03:43:51 +00:00
|
|
|
self.requestDone();
|
2013-03-15 16:58:41 +00:00
|
|
|
});
|
|
|
|
|
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() {
|
2014-06-11 08:37:28 +00:00
|
|
|
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", {});
|
2014-04-15 03:43:51 +00:00
|
|
|
self.requestDone();
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-20 02:37:43 +00:00
|
|
|
let connectionInfo = this.connectionInfo;
|
|
|
|
WifiManager.getNetworkId(connectionInfo.ssid, function(netId) {
|
2012-11-23 10:30:33 +00:00
|
|
|
// Trying to get netId from current network.
|
|
|
|
if (!netId &&
|
2014-08-07 10:00:23 +00:00
|
|
|
self.currentNetwork && self.currentNetwork.ssid &&
|
|
|
|
dequote(self.currentNetwork.ssid) == connectionInfo.ssid &&
|
2012-11-23 10:30:33 +00:00
|
|
|
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.
|
2014-04-10 03:48:50 +00:00
|
|
|
if ((WifiNetworkInterface.state ===
|
|
|
|
Ci.nsINetworkInterface.NETWORK_STATE_DISCONNECTED) &&
|
|
|
|
(this.prevState === "INITIALIZING" ||
|
|
|
|
this.prevState === "DISCONNECTED" ||
|
|
|
|
this.prevState === "INTERFACE_DISABLED" ||
|
|
|
|
this.prevState === "INACTIVE" ||
|
|
|
|
this.prevState === "UNINITIALIZED")) {
|
2013-07-29 09:59:53 +00:00
|
|
|
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;
|
2014-03-29 07:18:24 +00:00
|
|
|
WifiNetworkInterface.ips = [];
|
|
|
|
WifiNetworkInterface.prefixLengths = [];
|
|
|
|
WifiNetworkInterface.gateways = [];
|
|
|
|
WifiNetworkInterface.dnses = [];
|
2014-07-29 08:44:27 +00:00
|
|
|
gNetworkManager.updateNetworkInterface(WifiNetworkInterface);
|
2012-04-19 21:33:36 +00:00
|
|
|
|
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;
|
2014-04-25 10:41:26 +00:00
|
|
|
case "AUTHENTICATING":
|
|
|
|
self._fireEvent("onauthenticating", {network: netToDOM(self.currentNetwork)});
|
|
|
|
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() {
|
2014-03-29 07:18:24 +00:00
|
|
|
if (!this.info || !this.info.ipaddr_str) {
|
2013-10-18 08:46:30 +00:00
|
|
|
debug("Network information is invalid.");
|
|
|
|
return;
|
2012-04-19 21:33:36 +00:00
|
|
|
}
|
2013-10-18 08:46:30 +00:00
|
|
|
|
2014-02-26 10:08:46 +00:00
|
|
|
let maskLength =
|
|
|
|
netHelpers.getMaskLength(netHelpers.stringToIP(this.info.mask_str));
|
2014-03-29 07:18:24 +00:00
|
|
|
if (!maskLength) {
|
|
|
|
maskLength = 32; // max prefix for IPv4.
|
|
|
|
}
|
2013-10-18 08:46:30 +00:00
|
|
|
WifiNetworkInterface.state =
|
|
|
|
Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED;
|
2014-03-29 07:18:24 +00:00
|
|
|
WifiNetworkInterface.ips = [this.info.ipaddr_str];
|
|
|
|
WifiNetworkInterface.prefixLengths = [maskLength];
|
|
|
|
WifiNetworkInterface.gateways = [this.info.gateway_str];
|
|
|
|
if (typeof this.info.dns1_str == "string" &&
|
|
|
|
this.info.dns1_str.length) {
|
|
|
|
WifiNetworkInterface.dnses.push(this.info.dns1_str);
|
|
|
|
}
|
|
|
|
if (typeof this.info.dns2_str == "string" &&
|
|
|
|
this.info.dns2_str.length) {
|
|
|
|
WifiNetworkInterface.dnses.push(this.info.dns2_str);
|
|
|
|
}
|
2014-07-29 08:44:27 +00:00
|
|
|
gNetworkManager.updateNetworkInterface(WifiNetworkInterface);
|
2013-10-18 08:46:30 +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;
|
|
|
|
self._fireEvent("onconnect", { network: netToDOM(self.currentNetwork) });
|
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).
|
2014-03-21 09:00:38 +00:00
|
|
|
if (flags && flags.indexOf("[IBSS]") >= 0)
|
2012-12-14 01:16:18 +00:00
|
|
|
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: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
|
|
|
|
2014-07-04 06:37:55 +00:00
|
|
|
WifiManager.onstationinfoupdate = function() {
|
|
|
|
self._fireEvent("stationinfoupdate", { station: this.station });
|
2014-01-22 08:37:40 +00:00
|
|
|
};
|
|
|
|
|
2014-07-16 08:10:21 +00:00
|
|
|
WifiManager.onstopconnectioninfotimer = function() {
|
|
|
|
self._stopConnectionInfoTimer();
|
|
|
|
};
|
|
|
|
|
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,
|
2014-07-31 02:31:18 +00:00
|
|
|
Ci.nsIObserver,
|
2013-05-16 11:39:58 +00:00
|
|
|
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 &&
|
2014-06-27 09:26:31 +00:00
|
|
|
last.ipAddress === info.ipAddress &&
|
2012-07-13 01:56:39 +00:00
|
|
|
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;
|
2014-07-04 06:37:55 +00:00
|
|
|
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];
|
2014-03-10 08:48:13 +00:00
|
|
|
delete networks[net];
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
if (!network.ssid) {
|
2014-03-10 08:48:13 +00:00
|
|
|
WifiManager.removeNetwork(network.netId, function() {});
|
2012-04-09 07:44:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:18:02 +00:00
|
|
|
if (network.hasOwnProperty("priority") &&
|
|
|
|
network.priority > this._highestPriority) {
|
2012-04-09 07:44:53 +00:00
|
|
|
this._highestPriority = network.priority;
|
2014-08-29 03:18:02 +00:00
|
|
|
}
|
2012-11-08 05:34:12 +00:00
|
|
|
|
|
|
|
let networkKey = getNetworkKey(network);
|
2014-03-10 08:48:13 +00:00
|
|
|
// Accept latest config of same network(same SSID and same security).
|
|
|
|
if (networks[networkKey]) {
|
|
|
|
WifiManager.removeNetwork(networks[networkKey].netId, function() {});
|
|
|
|
}
|
2012-11-08 05:34:12 +00:00
|
|
|
networks[networkKey] = network;
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-04-03 10:10:16 +00:00
|
|
|
try {
|
|
|
|
msg.manager.sendAsyncMessage(message + (success ? ":OK" : ":NO"),
|
|
|
|
{ data: data, rid: msg.rid, mid: msg.mid });
|
|
|
|
} catch (e) {
|
|
|
|
debug("sendAsyncMessage error : " + e);
|
|
|
|
}
|
2014-03-12 10:25:32 +00:00
|
|
|
this._splicePendingRequest(msg);
|
|
|
|
},
|
|
|
|
|
|
|
|
_domRequest: [],
|
|
|
|
|
|
|
|
_splicePendingRequest: function(msg) {
|
|
|
|
for (let i = 0; i < this._domRequest.length; i++) {
|
|
|
|
if (this._domRequest[i].msg === msg) {
|
|
|
|
this._domRequest.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_clearPendingRequest: function() {
|
|
|
|
if (this._domRequest.length === 0) return;
|
2014-03-21 08:43:54 +00:00
|
|
|
this._domRequest.forEach((function(req) {
|
2014-03-12 10:25:32 +00:00
|
|
|
this._sendMessage(req.name + ":Return", false, "Wifi is disabled", req.msg);
|
2014-03-21 08:43:54 +00:00
|
|
|
}).bind(this));
|
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;
|
|
|
|
|
2014-01-14 10:23:42 +00:00
|
|
|
if (WifiManager.p2pSupported()) {
|
|
|
|
// If p2pObserver returns something truthy, return it!
|
|
|
|
// Otherwise, continue to do the rest of tasks.
|
|
|
|
var p2pRet = this._p2pObserver.onDOMMessage(aMessage);
|
|
|
|
if (p2pRet) {
|
|
|
|
return p2pRet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 21:24:53 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2014-04-03 10:10:16 +00:00
|
|
|
for (i = this._domRequest.length - 1; i >= 0; i--) {
|
|
|
|
if (this._domRequest[i].msg.manager === msg.manager) {
|
|
|
|
this._domRequest.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
|
|
|
|
2014-03-12 10:25:32 +00:00
|
|
|
// We are interested in DOMRequests only.
|
|
|
|
if (aMessage.name != "WifiManager:getState") {
|
|
|
|
this._domRequest.push({name: aMessage.name, msg:msg});
|
|
|
|
}
|
|
|
|
|
2012-04-09 07:44:53 +00:00
|
|
|
switch (aMessage.name) {
|
2014-08-07 09:19:31 +00:00
|
|
|
case "WifiManager:setWifiEnabled":
|
|
|
|
this.setWifiEnabled(msg);
|
|
|
|
break;
|
2012-04-09 07:44:53 +00:00
|
|
|
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;
|
2014-04-30 11:58:40 +00:00
|
|
|
case "WifiManager:importCert":
|
|
|
|
this.importCert(msg);
|
|
|
|
break;
|
2014-04-30 11:58:45 +00:00
|
|
|
case "WifiManager:getImportedCerts":
|
|
|
|
this.getImportedCerts(msg);
|
|
|
|
break;
|
2014-04-28 07:50:11 +00:00
|
|
|
case "WifiManager:deleteCert":
|
|
|
|
this.deleteCert(msg);
|
|
|
|
break;
|
2014-08-11 09:13:25 +00:00
|
|
|
case "WifiManager:setWifiTethering":
|
|
|
|
this.setWifiTethering(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),
|
2014-05-19 02:29:21 +00:00
|
|
|
macAddress: this.macAddress,
|
|
|
|
capabilities: WifiManager.getCapabilities()};
|
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));
|
|
|
|
},
|
|
|
|
|
2014-02-25 08:33:13 +00:00
|
|
|
getWifiScanResults: function(callback) {
|
|
|
|
var count = 0;
|
|
|
|
var timer = null;
|
|
|
|
var self = this;
|
|
|
|
|
2014-06-04 10:08:51 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
callback.onfailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-25 08:33:13 +00:00
|
|
|
self.waitForScan(waitForScanCallback);
|
|
|
|
doScan();
|
|
|
|
function doScan() {
|
2014-04-21 03:59:16 +00:00
|
|
|
WifiManager.scan(true, (function (ok) {
|
2014-02-25 08:33:13 +00:00
|
|
|
if (!ok) {
|
|
|
|
if (!timer) {
|
|
|
|
count = 0;
|
|
|
|
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count++ >= 3) {
|
|
|
|
timer = null;
|
2014-03-31 02:30:44 +00:00
|
|
|
self.wantScanResults.splice(self.wantScanResults.indexOf(waitForScanCallback), 1);
|
2014-02-25 08:33:13 +00:00
|
|
|
callback.onfailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else it's still running, continue waiting.
|
|
|
|
timer.initWithCallback(doScan, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-21 03:59:16 +00:00
|
|
|
}).bind(this));
|
2014-02-25 08:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (id === "security") {
|
|
|
|
result[id] = 0;
|
|
|
|
var security = element[id];
|
|
|
|
for (let j = 0; j < security.length; j++) {
|
|
|
|
if (security[j] === "WPA-PSK") {
|
|
|
|
result[id] |= Ci.nsIWifiScanResult.WPA_PSK;
|
|
|
|
} else if (security[j] === "WPA-EAP") {
|
|
|
|
result[id] |= Ci.nsIWifiScanResult.WPA_EAP;
|
|
|
|
} else if (security[j] === "WEP") {
|
|
|
|
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) {
|
2014-04-15 03:43:51 +00:00
|
|
|
if (status !== 0) {
|
|
|
|
this.requestDone();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-28 10:01:51 +00:00
|
|
|
// 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();
|
|
|
|
},
|
|
|
|
|
2014-08-07 09:19:31 +00:00
|
|
|
/**
|
|
|
|
* Compatibility flags for detecting if Gaia is controlling wifi by settings
|
|
|
|
* or API, once API is called, gecko will no longer accept wifi enable
|
|
|
|
* control from settings.
|
|
|
|
* This is used to deal with compatibility issue while Gaia adopted to use
|
|
|
|
* API but gecko doesn't remove the settings code in time.
|
|
|
|
* TODO: Remove this flag in Bug 1050147
|
|
|
|
*/
|
|
|
|
ignoreWifiEnabledFromSettings: false,
|
|
|
|
setWifiEnabled: function(msg) {
|
|
|
|
const message = "WifiManager:setWifiEnabled:Return";
|
|
|
|
let self = this;
|
|
|
|
let enabled = msg.data;
|
|
|
|
|
|
|
|
self.ignoreWifiEnabledFromSettings = true;
|
|
|
|
// No change.
|
|
|
|
if (enabled === WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, true, true, msg);
|
2014-08-18 07:10:24 +00:00
|
|
|
return;
|
2014-08-07 09:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can't enable wifi while hotspot mode is enabled.
|
|
|
|
if (enabled && (this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] ||
|
|
|
|
WifiManager.isWifiTetheringEnabled(WifiManager.tetheringState))) {
|
|
|
|
self._sendMessage(message, false, "Can't enable Wifi while hotspot mode is enabled", msg);
|
2014-08-18 07:10:24 +00:00
|
|
|
return;
|
2014-08-07 09:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WifiManager.setWifiEnabled(enabled, function(ok) {
|
|
|
|
if (ok === 0 || ok === "no change") {
|
|
|
|
self._sendMessage(message, true, true, msg);
|
2014-08-18 07:10:24 +00:00
|
|
|
|
|
|
|
// Reply error to pending requests.
|
|
|
|
if (!enabled) {
|
|
|
|
self._clearPendingRequest();
|
|
|
|
} else {
|
|
|
|
WifiManager.start();
|
|
|
|
}
|
2014-08-07 09:19:31 +00:00
|
|
|
} else {
|
2014-08-18 07:10:24 +00:00
|
|
|
self._sendMessage(message, false, "Set wifi enabled failed", msg);
|
2014-08-07 09:19:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_setWifiEnabled: function(enabled, callback) {
|
2014-03-12 10:25:32 +00:00
|
|
|
// Reply error to pending requests.
|
|
|
|
if (!enabled) {
|
|
|
|
this._clearPendingRequest();
|
|
|
|
}
|
|
|
|
|
2013-09-09 15:25:16 +00:00
|
|
|
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.
|
2014-08-07 09:19:31 +00:00
|
|
|
// TODO: Remove command queue in Bug 1050147
|
2014-04-21 12:06:27 +00:00
|
|
|
queueRequest: function(data, callback) {
|
2013-09-09 15:25:16 +00:00
|
|
|
if (!callback) {
|
|
|
|
throw "Try to enqueue a request without callback";
|
|
|
|
}
|
|
|
|
|
2014-04-21 12:06:27 +00:00
|
|
|
let optimizeCommandList = ["setWifiEnabled", "setWifiApEnabled"];
|
|
|
|
if (optimizeCommandList.indexOf(data.command) != -1) {
|
|
|
|
this._stateRequests = this._stateRequests.filter(function(element) {
|
|
|
|
return element.data.command !== data.command;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-09-09 15:25:16 +00:00
|
|
|
this._stateRequests.push({
|
2014-04-21 12:06:27 +00:00
|
|
|
data: data,
|
2013-09-09 15:25:16 +00:00
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
|
|
|
|
this.nextRequest();
|
2012-08-29 19:31:26 +00:00
|
|
|
},
|
|
|
|
|
2013-05-16 11:39:58 +00:00
|
|
|
getWifiTetheringParameters: function getWifiTetheringParameters(enable) {
|
2014-08-11 09:13:25 +00:00
|
|
|
if (this.useTetheringAPI) {
|
|
|
|
return this.getWifiTetheringConfiguration(enable);
|
|
|
|
} else {
|
|
|
|
return this.getWifiTetheringParametersBySetting(enable);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getWifiTetheringConfiguration: function getWifiTetheringConfiguration(enable) {
|
|
|
|
let config = {};
|
|
|
|
let params = this.tetheringConfig;
|
|
|
|
|
|
|
|
let check = function(field, _default) {
|
|
|
|
config[field] = field in params ? params[field] : _default;
|
|
|
|
};
|
|
|
|
|
|
|
|
check("ssid", DEFAULT_WIFI_SSID);
|
|
|
|
check("security", DEFAULT_WIFI_SECURITY_TYPE);
|
|
|
|
check("key", DEFAULT_WIFI_SECURITY_PASSWORD);
|
|
|
|
check("ip", DEFAULT_WIFI_IP);
|
|
|
|
check("prefix", DEFAULT_WIFI_PREFIX);
|
|
|
|
check("wifiStartIp", DEFAULT_WIFI_DHCPSERVER_STARTIP);
|
|
|
|
check("wifiEndIp", DEFAULT_WIFI_DHCPSERVER_ENDIP);
|
|
|
|
check("usbStartIp", DEFAULT_USB_DHCPSERVER_STARTIP);
|
|
|
|
check("usbEndIp", DEFAULT_USB_DHCPSERVER_ENDIP);
|
|
|
|
check("dns1", DEFAULT_DNS1);
|
|
|
|
check("dns2", DEFAULT_DNS2);
|
|
|
|
|
|
|
|
config.enable = enable;
|
|
|
|
config.mode = enable ? WIFI_FIRMWARE_AP : WIFI_FIRMWARE_STATION;
|
|
|
|
config.link = enable ? NETWORK_INTERFACE_UP : NETWORK_INTERFACE_DOWN;
|
|
|
|
|
|
|
|
// Check the format to prevent netd from crash.
|
|
|
|
if (enable && (!config.ssid || config.ssid == "")) {
|
|
|
|
debug("Invalid SSID value.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable && (config.security != WIFI_SECURITY_TYPE_NONE && !config.key)) {
|
|
|
|
debug("Invalid security password.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the default values here until application supports these settings.
|
|
|
|
if (config.ip == "" || config.prefix == "" ||
|
|
|
|
config.wifiStartIp == "" || config.wifiEndIp == "" ||
|
|
|
|
config.usbStartIp == "" || config.usbEndIp == "") {
|
|
|
|
debug("Invalid subnet information.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
|
|
|
getWifiTetheringParametersBySetting: function getWifiTetheringParametersBySetting(enable) {
|
2013-05-16 11:39:58 +00:00
|
|
|
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;
|
2014-02-10 17:05:13 +00:00
|
|
|
|
|
|
|
let privnet = network;
|
|
|
|
let dontConnect = privnet.dontConnect;
|
|
|
|
delete privnet.dontConnect;
|
|
|
|
|
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 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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-10 17:05:13 +00:00
|
|
|
var selectAndConnectOrReturn = dontConnect ?
|
|
|
|
function() {
|
|
|
|
self._sendMessage(message, true, "Wifi has been recorded", msg);
|
|
|
|
} : selectAndConnect;
|
|
|
|
if (self._highestPriority >= MAX_PRIORITY) {
|
|
|
|
self._reprioritizeNetworks(selectAndConnectOrReturn);
|
|
|
|
} else {
|
|
|
|
WifiManager.saveConfig(selectAndConnectOrReturn);
|
|
|
|
}
|
2012-04-09 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-08-29 03:18:02 +00:00
|
|
|
// Sync priority back to configured so if priority reaches MAX_PRIORITY,
|
|
|
|
// it can be sorted correctly in _reprioritizeNetworks() because the
|
|
|
|
// function sort network based on priority in configure list.
|
|
|
|
configured.priority = privnet.priority;
|
2012-04-09 07:44:53 +00:00
|
|
|
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) {
|
2014-06-20 02:37:43 +00:00
|
|
|
if (self._needToEnableNetworks) {
|
|
|
|
self._enableAllNetworks();
|
|
|
|
self._needToEnableNetworks = false;
|
|
|
|
}
|
|
|
|
|
2013-03-26 06:55:56 +00:00
|
|
|
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;
|
2014-06-11 08:37:28 +00:00
|
|
|
|
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-16 22:19:51 +00:00
|
|
|
if (detail.method === "pbc") {
|
2014-05-06 07:04:16 +00:00
|
|
|
WifiManager.wpsPbc(function(ok) {
|
2012-08-16 22:19:51 +00:00
|
|
|
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";
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-24 09:05:47 +00:00
|
|
|
// 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;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-22 10:22:25 +00:00
|
|
|
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;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 03:02:30 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-30 11:58:40 +00:00
|
|
|
importCert: function importCert(msg) {
|
|
|
|
const message = "WifiManager:importCert:Return";
|
|
|
|
let self = this;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-30 11:58:40 +00:00
|
|
|
WifiManager.importCert(msg.data, function(data) {
|
|
|
|
if (data.status === 0) {
|
|
|
|
let usageString = ["ServerCert"];
|
|
|
|
let usageArray = [];
|
|
|
|
for (let i = 0; i < usageString.length; i++) {
|
|
|
|
if (data.usageFlag & (0x01 << i)) {
|
|
|
|
usageArray.push(usageString[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self._sendMessage(message, true, {
|
|
|
|
nickname: data.nickname,
|
|
|
|
usage: usageArray
|
|
|
|
}, msg);
|
|
|
|
} else {
|
|
|
|
self._sendMessage(message, false, "Import Cert failed", msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-30 11:58:45 +00:00
|
|
|
getImportedCerts: function getImportedCerts(msg) {
|
|
|
|
const message = "WifiManager:getImportedCerts:Return";
|
|
|
|
let self = this;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:09:24 +00:00
|
|
|
let certDB = Cc["@mozilla.org/security/x509certdb;1"]
|
|
|
|
.getService(Ci.nsIX509CertDB);
|
|
|
|
if (!certDB) {
|
2014-04-30 11:58:45 +00:00
|
|
|
self._sendMessage(message, false, "Failed to query NSS DB service", msg);
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:09:24 +00:00
|
|
|
let certList = certDB.getCerts();
|
2014-04-30 11:58:45 +00:00
|
|
|
if (!certList) {
|
|
|
|
self._sendMessage(message, false, "Failed to get certificate List", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
let certListEnum = certList.getEnumerator();
|
|
|
|
if (!certListEnum) {
|
|
|
|
self._sendMessage(message, false, "Failed to get certificate List enumerator", msg);
|
|
|
|
}
|
|
|
|
let importedCerts = {
|
|
|
|
ServerCert: [],
|
|
|
|
};
|
|
|
|
let UsageMapping = {
|
|
|
|
SERVERCERT: "ServerCert",
|
|
|
|
};
|
|
|
|
|
|
|
|
while (certListEnum.hasMoreElements()) {
|
2014-07-04 05:09:24 +00:00
|
|
|
let certInfo = certListEnum.getNext().QueryInterface(Ci.nsIX509Cert);
|
2014-04-30 11:58:45 +00:00
|
|
|
let certNicknameInfo = /WIFI\_([A-Z]*)\_(.*)/.exec(certInfo.nickname);
|
|
|
|
if (!certNicknameInfo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
importedCerts[UsageMapping[certNicknameInfo[1]]].push(certNicknameInfo[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
self._sendMessage(message, true, importedCerts, msg);
|
|
|
|
},
|
|
|
|
|
2014-04-28 07:50:11 +00:00
|
|
|
deleteCert: function deleteCert(msg) {
|
|
|
|
const message = "WifiManager:deleteCert:Return";
|
|
|
|
let self = this;
|
|
|
|
|
2014-06-11 08:37:28 +00:00
|
|
|
if (!WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is disabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:50:11 +00:00
|
|
|
WifiManager.deleteCert(msg.data, function(data) {
|
|
|
|
self._sendMessage(message, data.status === 0, "Delete Cert failed", msg);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-08-11 09:13:25 +00:00
|
|
|
// TODO : These two variables should be removed once GAIA uses tethering API.
|
|
|
|
useTetheringAPI : false,
|
|
|
|
tetheringConfig : {},
|
|
|
|
|
|
|
|
setWifiTethering: function setWifiTethering(msg) {
|
|
|
|
const message = "WifiManager:setWifiTethering:Return";
|
|
|
|
let self = this;
|
|
|
|
let enabled = msg.data.enabled;
|
|
|
|
|
|
|
|
this.useTetheringAPI = true;
|
|
|
|
this.tetheringConfig = msg.data.config;
|
|
|
|
|
|
|
|
if (WifiManager.enabled) {
|
|
|
|
this._sendMessage(message, false, "Wifi is enabled", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setWifiApEnabled(enabled, function() {
|
|
|
|
if ((enabled && WifiManager.tetheringState == "COMPLETED") ||
|
|
|
|
(!enabled && WifiManager.tetheringState == "UNINITIALIZED")) {
|
|
|
|
self._sendMessage(message, true, msg.data, msg);
|
|
|
|
} else {
|
|
|
|
msg.data.reason = enabled ?
|
|
|
|
"Enable WIFI tethering faild" : "Disable WIFI tethering faild";
|
|
|
|
self._sendMessage(message, false, msg.data, 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 ...");
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiEnabled", value: false}, function(data) {
|
2014-08-07 09:19:31 +00:00
|
|
|
this._setWifiEnabled(false, this._setWifiEnabledCallback.bind(this));
|
2013-09-09 15:25:16 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-08-07 09:19:31 +00:00
|
|
|
// TODO: Remove command queue in Bug 1050147.
|
2013-09-09 15:25:16 +00:00
|
|
|
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();
|
|
|
|
|
2014-04-21 12:06:27 +00:00
|
|
|
request.callback(request.data);
|
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();
|
|
|
|
}
|
2014-08-28 04:01:31 +00:00
|
|
|
});
|
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();
|
|
|
|
}
|
2014-08-28 04:01:31 +00:00
|
|
|
});
|
2013-01-17 12:26:17 +00:00
|
|
|
},
|
|
|
|
|
2012-09-13 07:19:47 +00:00
|
|
|
handleWifiEnabled: function(enabled) {
|
2014-08-07 09:19:31 +00:00
|
|
|
if (this.ignoreWifiEnabledFromSettings) {
|
|
|
|
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) {
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiApEnabled", value: false}, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (this.tetheringSettings[SETTINGS_WIFI_TETHERING_ENABLED] ||
|
2014-04-15 03:43:51 +00:00
|
|
|
WifiManager.isWifiTetheringEnabled(WifiManager.tetheringState)) {
|
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
|
|
|
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiEnabled", value: enabled}, function(data) {
|
2014-08-07 09:19:31 +00:00
|
|
|
this._setWifiEnabled(enabled, this._setWifiEnabledCallback.bind(this));
|
2013-09-09 15:25:16 +00:00
|
|
|
}.bind(this));
|
2013-05-16 11:39:58 +00:00
|
|
|
|
2013-09-05 09:16:24 +00:00
|
|
|
if (!enabled) {
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiApEnabled", value: 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) {
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiEnabled", value: false}, function(data) {
|
2014-04-15 03:43:51 +00:00
|
|
|
if (WifiManager.isWifiEnabled(WifiManager.state)) {
|
2013-09-25 08:49:05 +00:00
|
|
|
this.disconnectedByWifiTethering = true;
|
2014-08-07 09:19:31 +00:00
|
|
|
this._setWifiEnabled(false, this._setWifiEnabledCallback.bind(this));
|
2013-09-05 09:16:24 +00:00
|
|
|
} 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
|
|
|
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiApEnabled", value: enabled}, function(data) {
|
2014-04-15 03:43:51 +00:00
|
|
|
this.setWifiApEnabled(enabled, 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) {
|
2014-04-21 12:06:27 +00:00
|
|
|
this.queueRequest({command: "setWifiEnabled", value: true}, function(data) {
|
2013-09-05 09:16:24 +00:00
|
|
|
if (this.disconnectedByWifiTethering) {
|
2014-08-07 09:19:31 +00:00
|
|
|
this._setWifiEnabled(true, this._setWifiEnabledCallback.bind(this));
|
2013-09-05 09:16:24 +00:00
|
|
|
} 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) {
|
2014-07-31 02:31:18 +00:00
|
|
|
switch (topic) {
|
|
|
|
case kMozSettingsChangedObserverTopic:
|
2014-09-16 20:54:25 +00:00
|
|
|
// The string we're interested in will be a JSON string that looks like:
|
|
|
|
// {"key":"wifi.enabled","value":"true"}.
|
|
|
|
|
|
|
|
let setting = JSON.parse(data);
|
2014-07-31 02:31:18 +00:00
|
|
|
// To avoid WifiWorker setting the wifi again, don't need to deal with
|
|
|
|
// the "mozsettings-changed" event fired from internal setting.
|
2014-09-16 20:54:25 +00:00
|
|
|
if (setting.isInternalChange) {
|
2014-07-31 02:31:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-13 07:19:47 +00:00
|
|
|
|
2014-09-16 20:54:25 +00:00
|
|
|
this.handle(setting.key, setting.value);
|
2014-07-31 02:31:18 +00:00
|
|
|
break;
|
2012-09-13 07:19:47 +00:00
|
|
|
|
2014-07-31 02:31:18 +00:00
|
|
|
case "xpcom-shutdown":
|
|
|
|
let wifiService = Cc["@mozilla.org/wifi/service;1"].getService(Ci.nsIWifiProxyService);
|
|
|
|
wifiService.shutdown();
|
|
|
|
let wifiCertService = Cc["@mozilla.org/wifi/certservice;1"].getService(Ci.nsIWifiCertService);
|
|
|
|
wifiCertService.shutdown();
|
|
|
|
break;
|
|
|
|
}
|
2013-05-16 11:39:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handle: function handle(aName, aResult) {
|
|
|
|
switch(aName) {
|
2014-08-07 09:19:31 +00:00
|
|
|
// TODO: Remove function call in Bug 1050147.
|
2013-05-16 11:39:58 +00:00
|
|
|
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:
|
2014-08-11 09:13:25 +00:00
|
|
|
// TODO: code related to wifi-tethering setting should be removed after GAIA
|
|
|
|
// use tethering API
|
|
|
|
if (this.useTetheringAPI) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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();
|