2 * Copyright (c) 2013, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
11 var settings = settings || {};
12 settings.wifi = settings.wifi || {};
14 var WIFI_SECURITY_TYPE = {
24 settings.wifi = (function() {
26 var default_adapter = null;
29 var WiFiAdapter = function(adapter) {
30 this.wifi_adapter = adapter;
31 this.name = 'Connman-WiFi';
35 WiFiAdapter.prototype.setPowered = function(powered, success_cb, error_cb) {
36 if (wsAPI === undefined) return;
37 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'enable', powered, success_cb, function(e) {
38 if (e.indexOf('Already enabled') >= 0) {
39 console.log('wifi subsystem already enabled');
41 } else if (e.indexOf('Already disabled') >= 0) {
42 console.log('wifi subsystem already disabled');
50 WiFiAdapter.prototype.getPowered = function(success_cb, error_cb) {
51 if (wsAPI === undefined) return;
53 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'is_enabled', null, function(isEnabled) {
54 if (isEnabled === undefined) {
55 console.log('Badly formed json message: ' + json_msg);
57 me.powered = isEnabled;
58 success_cb(me.powered);
62 WiFiAdapter.prototype.scan = function(success_cb, error_cb) {
63 if (wsAPI === undefined) return;
64 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'scan', null, function(results) {
65 if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
66 error_cb('Cannot parse scan results');
70 var network_list = results[0];
74 for (var i = 0; i < network_list.length; i++) {
75 if (network_list[i][1] === undefined) {
76 console.log('Badly form json message: ' + json_msg);
78 if (network_list[i][1].Type === 'wifi') {
79 var network = new settings.wifi.WiFiNetwork(network_list[i][0]);
80 if (network_list[i][1].Name !== undefined) {
81 network.ssid = network_list[i][1].Name;
83 if (network_list[i][1].State === 'ready') {
84 network.connected = true;
85 } else if (network_list[i][1].State === 'idle') {
86 network.connected = false;
88 if (network_list[i][1].EncryptionMode !== undefined) {
89 network.encryption = network_list[i][1].EncryptionMode;
91 if (network_list[i][1].Strength !== undefined) {
92 network.strength = network_list[i][1].Strength;
94 if (network_list[i][1].IPv4.Address !== undefined) {
95 network.ipAddress = network_list[i][1].IPv4.Address;
97 if (network_list[i][1].IPv4.Gateway !== undefined) {
98 network.gateway = network_list[i][1].IPv4.Gateway;
100 if (network_list[i][1].IPv4.Netmask !== undefined) {
101 network.netmask = network_list[i][1].IPv4.Netmask;
103 results.push(network);
113 WiFiAdapter.prototype.connectNetwork = function(network_id, security, passcode, success_cb, error_cb) {
114 if (wsAPI === undefined) return;
116 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'connect', network_id, success_cb, error_cb);
119 WiFiAdapter.prototype.disconnectNetwork = function(network_id, success_cb, error_cb) {
120 if (wsAPI === undefined) return;
121 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'disconnect', network_id, success_cb, error_cb);
125 WiFiNetwork = function(object_path) {
126 this.id = object_path;
128 this.connected = false;
129 this.security = WIFI_SECURITY_TYPE.NONE;
130 this.encryption = 'Unknown';
131 this.ipAddress = 'Unknown';
132 this.gateway = 'Unknown';
133 this.netmask = 'Unknown';
137 WiFiNetwork.prototype.getSecurity = function() {
138 return this.security;
141 function subscribeEvents(callback) {
142 wsAPI.subscribeEvents(callback);
145 function unsubscribeEvents(callback) {
146 wsAPI.unsubscribeEvents(callback);
149 function getDefaultAdapter() {
150 if (default_adapter === null) {
151 default_adapter = new WiFiAdapter(wsAPI);
154 return default_adapter;
158 WiFiAdapter: WiFiAdapter,
159 WiFiNetwork: WiFiNetwork,
160 subscribeEvents: subscribeEvents,
161 unsubscribeEvents: unsubscribeEvents,
162 getDefaultAdapter: getDefaultAdapter