Fixes TIVI-2784 - Wifi password is not empty
[profile/ivi/SettingsApp.git] / js / api-connman.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
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
7  *
8  */
9
10 /* Namespace */
11 var settings = settings || {};
12 settings.connman = settings.connman || {};
13
14 /* Module */
15 settings.connman = (function() {
16
17     var default_adapter = null;
18
19     /* Technology class */
20     var ConnmanTechnology = function(technology_path, technology_properties) {
21             this.id = technology_path;
22             this.prop = technology_properties;
23         };
24
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');
29             return;
30         }
31
32         var me = this;
33         this.setPowerInProgress = true;
34         setTimeout(function() {
35             wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'enable', [me.id, powered], function() {
36                 me.setPowerInProgress = false;
37                 success_cb();
38             }, function(e) {
39                 if (e.indexOf('Already enabled') >= 0) {
40                     console.log('connman ' + me.prop.Type + ' already enabled');
41                     me.setPowerInProgress = false;
42                     success_cb();
43                 } else if (e.indexOf('Already disabled') >= 0) {
44                     console.log('connman ' + me.prop.Type + ' already disabled');
45                     me.setPowerInProgress = false;
46                     success_cb();
47                 } else {
48                     me.setPowerInProgress = false;
49                     error_cb(e);
50                 }
51             })
52         }, 2000);
53     };
54
55     ConnmanTechnology.prototype.getPowered = function(success_cb, error_cb) {
56         if (wsAPI === undefined) return;
57         var me = this;
58         wsAPI.sendRequest(this.prop.Type, 'is_enabled', null, function(isEnabled) {
59             if (isEnabled === undefined) {
60                 console.log('Badly formed json message: ' + json_msg);
61             }
62             me.powered = isEnabled;
63             success_cb(me.powered);
64         }, error_cb);
65     };
66
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');
71             return;
72         }
73
74         var me = this;
75         this.setTetheringInProgress = true;
76
77         var tethering_options = {
78             'path': me.id,
79             'ssid': identifier,
80             'password': passphrase,
81             'enabled': tethered
82         };
83
84         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'tethering', tethering_options, function() {
85             me.setTetheringInProgress = false;
86             success_cb();
87         }, function(e) {
88             if (e.indexOf('Already enabled') >= 0) {
89                 console.log('tethering ' + me.prop.Type + ' already enabled');
90                 me.setTetheringInProgress = false;
91                 success_cb();
92             } else if (e.indexOf('Already disabled') >= 0) {
93                 console.log('tethering ' + me.prop.Type + ' already disabled');
94                 me.setTetheringInProgress = false;
95                 success_cb();
96             } else {
97                 me.setTetheringInProgress = false;
98                 error_cb(e);
99             }
100         })
101     };
102
103     /* Service class */
104     ConnmanService = function(service_path, service_properties) {
105         this.id = service_path;
106         this.prop = service_properties;
107     };
108
109     ConnmanService.prototype.connect = function(passphrase, success_cb, error_cb) {
110         if (wsAPI === undefined) return;
111
112         var info = [this.id,
113         {
114             'Name': this.prop.Name,
115             'Type': this.prop.EncryptionMode,
116         }]
117
118         if (passphrase) {
119             info[1].Passphrase = passphrase;
120         }
121
122         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'connect', info, success_cb, error_cb);
123     };
124
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);
128     };
129
130     ConnmanService.prototype.setAutoConnect = function(autoconnect, success_cb, error_cb) {
131         if (wsAPI === undefined) return;
132
133         var value = {
134             'path': this.id,
135             'enable': autoconnect
136         };
137
138         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'autoconnect', value, success_cb, error_cb);
139     };
140
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');
146                 return;
147             }
148
149             var technologies_list = results[0];
150
151             try {
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);
156                     }
157
158                     var technology = new settings.connman.ConnmanTechnology(technologies_list[i][0], technologies_list[i][1]);
159                     technologies.push(technology);
160                 }
161             } catch (e) {
162                 error_cb(e);
163             }
164             success_cb(technologies);
165         }, error_cb);
166     }
167
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');
173                 return;
174             }
175
176             var services_list = results[0];
177
178             try {
179                 var results = [];
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);
183                     }
184
185                     var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
186                     results.push(service);
187                 }
188             } catch (e) {
189                 error_cb(e);
190             }
191             success_cb(results);
192         }, error_cb);
193     }
194
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');
200                 return;
201             }
202
203             var services_list = results[0];
204
205             try {
206                 var services = [];
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);
210                     }
211
212                     var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
213                     services.push(service);
214                 }
215             } catch (e) {
216                 error_cb(e);
217             }
218             success_cb(services);
219         }, error_cb);
220     };
221
222     return {
223         ConnmanTechnology: ConnmanTechnology,
224         ConnmanService: ConnmanService,
225         getTechnologies: getTechnologies,
226         getServices: getServices,
227         scan: scan
228     };
229 })();