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, error_cb);
40 WiFiAdapter.prototype.getPowered = function(success_cb, error_cb) {
41 if (wsAPI === undefined) return;
43 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'isEnabled', null, function(json_msg) {
44 var result = JSON.parse(json_msg);
45 if (result.isEnabled === undefined) {
46 console.log('Badly form json message: ' + json_msg);
48 me.powered = result.isEnabled;
49 success_cb(me.powered);
53 WiFiAdapter.prototype.scan = function(success_cb, error_cb) {
54 if (wsAPI === undefined) return;
55 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'scan', null, function(json_msg) {
56 var network_list = JSON.parse(json_msg);
57 if (network_list.access_points === undefined) {
58 console.log('Badly form json message: ' + json_msg);
62 for (var i = 0; i < network_list.access_points.length; i++) {
63 var network = new settings.wifi.WiFiNetwork(network_list.access_points[i].ssid);
64 network.connected = network_list.access_points[i].connected;
65 results.push(network);
71 WiFiAdapter.prototype.connectNetwork = function(network, security, passcode, success_cb, error_cb) {
72 if (wsAPI === undefined) return;
73 var connectionInfo = {
78 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'connect', connectionInfo, success_cb, error_cb);
81 WiFiAdapter.prototype.disconnectNetwork = function(network, success_cb, error_cb) {
82 if (wsAPI === undefined) return;
83 wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'disconnect', network, success_cb, error_cb);
87 WiFiNetwork = function(ssid) {
89 this.security = WIFI_SECURITY_TYPE.NONE;
92 WiFiNetwork.prototype.getSecurity = function() {
96 function subscribeEvents(callback) {
97 wsAPI.subscribeEvents(callback);
100 function unsubscribeEvents(callback) {
101 wsAPI.unsubscribeEvents(callback);
104 function getDefaultAdapter() {
105 if (default_adapter === null) {
106 default_adapter = new WiFiAdapter(wsAPI);
109 return default_adapter;
113 WiFiAdapter: WiFiAdapter,
114 WiFiNetwork: WiFiNetwork,
115 subscribeEvents: subscribeEvents,
116 unsubscribeEvents: unsubscribeEvents,
117 getDefaultAdapter: getDefaultAdapter