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.connman = settings.connman || {};
15 settings.connman = (function() {
17 var default_adapter = null;
19 /* Technology class */
20 var ConnmanTechnology = function(technology_path, technology_properties) {
21 this.id = technology_path;
22 this.prop = technology_properties;
25 ConnmanTechnology.prototype.setPowered = function(powered, success_cb, error_cb) {
26 if (wsAPI === undefined) return;
27 if (this.setPowerInProgress) {
28 console.log('Enabling/disabling technology in progress');
33 this.setPowerInProgress = true;
34 setTimeout(function() {
35 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'enable', [me.id, powered], function() {
36 me.setPowerInProgress = false;
39 if (e.indexOf('Already enabled') >= 0) {
40 console.log('connman ' + me.prop.Type + ' already enabled');
41 me.setPowerInProgress = false;
43 } else if (e.indexOf('Already disabled') >= 0) {
44 console.log('connman ' + me.prop.Type + ' already disabled');
45 me.setPowerInProgress = false;
48 me.setPowerInProgress = false;
55 ConnmanTechnology.prototype.getPowered = function(success_cb, error_cb) {
56 if (wsAPI === undefined) return;
58 wsAPI.sendRequest(this.prop.Type, 'is_enabled', null, function(isEnabled) {
59 if (isEnabled === undefined) {
60 console.log('Badly formed json message: ' + json_msg);
62 me.powered = isEnabled;
63 success_cb(me.powered);
67 ConnmanTechnology.prototype.setTethering = function(tethered, identifier, passphrase, success_cb, error_cb) {
68 if (wsAPI === undefined) return;
69 if (this.setTetheringInProgress) {
70 console.log('Activating/Deactiviating tethering in progress');
75 this.setTetheringInProgress = true;
77 var tethering_options = {
80 'password': passphrase,
84 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'tethering', tethering_options, function() {
85 me.setTetheringInProgress = false;
88 if (e.indexOf('Already enabled') >= 0) {
89 console.log('tethering ' + me.prop.Type + ' already enabled');
90 me.setTetheringInProgress = false;
92 } else if (e.indexOf('Already disabled') >= 0) {
93 console.log('tethering ' + me.prop.Type + ' already disabled');
94 me.setTetheringInProgress = false;
97 me.setTetheringInProgress = false;
104 ConnmanService = function(service_path, service_properties) {
105 this.id = service_path;
106 this.prop = service_properties;
109 ConnmanService.prototype.connect = function(passphrase, success_cb, error_cb) {
110 if (wsAPI === undefined) return;
114 'Name': this.prop.Name,
115 'Type': this.prop.EncryptionMode,
119 info[1].Passphrase = passphrase;
122 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'connect', info, success_cb, error_cb);
125 ConnmanService.prototype.disconnect = function(success_cb, error_cb) {
126 if (wsAPI === undefined) return;
127 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'disconnect', [this.id, null], success_cb, error_cb);
130 ConnmanService.prototype.setAutoConnect = function(autoconnect, success_cb, error_cb) {
131 if (wsAPI === undefined) return;
135 'enable': autoconnect
138 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'autoconnect', value, success_cb, error_cb);
141 function getTechnologies(success_cb, error_cb) {
142 if (wsAPI === undefined) return;
143 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_MANAGER, 'get_technologies', null, function(results) {
144 if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
145 error_cb('Cannot parse technologies');
149 var technologies_list = results[0];
152 var technologies = [];
153 for (var i = 0; i < technologies_list.length; i++) {
154 if (technologies_list[i][0] === undefined || technologies_list[i][1] === undefined) {
155 console.log('Badly form json message: ' + json_msg);
158 var technology = new settings.connman.ConnmanTechnology(technologies_list[i][0], technologies_list[i][1]);
159 technologies.push(technology);
164 success_cb(technologies);
168 function getServices(success_cb, error_cb) {
169 if (wsAPI === undefined) return;
170 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_MANAGER, 'get_services', null, function(results) {
171 if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
172 error_cb('Cannot parse get_services results');
176 var services_list = results[0];
180 for (var i = 0; i < services_list.length; i++) {
181 if (services_list[i][0] === undefined || services_list[i][1] === undefined) {
182 console.log('Badly form json message: ' + json_msg);
185 var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
186 results.push(service);
195 function scan(technology, success_cb, error_cb) {
196 if (wsAPI === undefined) return;
197 wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'scan', [technology, null], function(results) {
198 if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
199 error_cb('Cannot parse scan results');
203 var services_list = results[0];
207 for (var i = 0; i < services_list.length; i++) {
208 if (services_list[i][0] === undefined || services_list[i][1] === undefined) {
209 console.log('Badly form json message: ' + json_msg);
212 var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
213 services.push(service);
218 success_cb(services);
223 ConnmanTechnology: ConnmanTechnology,
224 ConnmanService: ConnmanService,
225 getTechnologies: getTechnologies,
226 getServices: getServices,