Updated connman and bluetooth settings to the new connman protocols in settings-daemon
[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     /* Service class */
68     ConnmanService = function(service_path, service_properties) {
69         this.id = service_path;
70         this.prop = service_properties;
71     };
72
73     ConnmanService.prototype.connect = function(passphrase, success_cb, error_cb) {
74         if (wsAPI === undefined) return;
75
76         var info = [this.id,
77         {
78             'Name': this.prop.Name,
79             'Type': this.prop.EncryptionMode,
80             'Passphrase': passphrase
81         }]
82         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'connect', info, success_cb, error_cb);
83     };
84
85     ConnmanService.prototype.disconnect = function(success_cb, error_cb) {
86         if (wsAPI === undefined) return;
87         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_SERVICE, 'disconnect', [this.id, null], success_cb, error_cb);
88     };
89
90     function getTechnologies(success_cb, error_cb) {
91         if (wsAPI === undefined) return;
92         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_MANAGER, 'get_technologies', null, function(results) {
93             if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
94                 error_cb('Cannot parse technologies');
95                 return;
96             }
97
98             var technologies_list = results[0];
99
100             try {
101                 var technologies = [];
102                 for (var i = 0; i < technologies_list.length; i++) {
103                     if (technologies_list[i][0] === undefined || technologies_list[i][1] === undefined) {
104                         console.log('Badly form json message: ' + json_msg);
105                     }
106
107                     var technology = new settings.connman.ConnmanTechnology(technologies_list[i][0], technologies_list[i][1]);
108                     technologies.push(technology);
109                 }
110             } catch (e) {
111                 error_cb(e);
112             }
113             success_cb(technologies);
114         }, error_cb);
115     }
116
117     function getServices(success_cb, error_cb) {
118         if (wsAPI === undefined) return;
119         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_MANAGER, 'get_services', null, function(results) {
120             if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
121                 error_cb('Cannot parse get_services results');
122                 return;
123             }
124
125             var services_list = results[0];
126
127             try {
128                 var results = [];
129                 for (var i = 0; i < services_list.length; i++) {
130                     if (services_list[i][0] === undefined || services_list[i][1] === undefined) {
131                         console.log('Badly form json message: ' + json_msg);
132                     }
133
134                     var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
135                     results.push(service);
136                 }
137             } catch (e) {
138                 error_cb(e);
139             }
140             success_cb(results);
141         }, error_cb);
142     }
143
144     function scan(technology, success_cb, error_cb) {
145         if (wsAPI === undefined) return;
146         wsAPI.sendRequest(WS_REQUEST_TYPE.CONNMAN_TECHNOLOGY, 'scan', [technology, null], function(results) {
147             if (results.length >= 0 && results[0].length >= 0 && results[0][0][0] === undefined) {
148                 error_cb('Cannot parse scan results');
149                 return;
150             }
151
152             var services_list = results[0];
153
154             try {
155                 var services = [];
156                 for (var i = 0; i < services_list.length; i++) {
157                     if (services_list[i][0] === undefined || services_list[i][1] === undefined) {
158                         console.log('Badly form json message: ' + json_msg);
159                     }
160
161                     var service = new settings.connman.ConnmanService(services_list[i][0], services_list[i][1]);
162                     services.push(service);
163                 }
164             } catch (e) {
165                 error_cb(e);
166             }
167             success_cb(services);
168         }, error_cb);
169     };
170
171     return {
172         ConnmanTechnology: ConnmanTechnology,
173         ConnmanService: ConnmanService,
174         getTechnologies: getTechnologies,
175         getServices: getServices,
176         scan: scan
177     };
178 })();