Update Modello web samples from upstream; version up
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / SettingsApp / project / js / api-wifi.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.wifi = settings.wifi || {};
13
14 var WIFI_SECURITY_TYPE = {
15     NONE: 0,
16     WEP: 1,
17     WPA: 2,
18     WPA2: 3,
19     WPA_ENTERPRISE: 4,
20     WPA2_ENTERPRISE: 5
21 };
22
23 /* Module */
24 settings.wifi = (function() {
25
26     var default_adapter = null;
27
28     /* Adapter class */
29     var WiFiAdapter = function(adapter) {
30             this.wifi_adapter = adapter;
31             this.name = 'Connman-WiFi';
32             this.powered = false;
33         };
34
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');
40                 success_cb();
41             } else if (e.indexOf('Already disabled') >= 0) {
42                 console.log('wifi subsystem already disabled');
43                 success_cb();
44             } else {
45                 error_cb(e);
46             }
47         });
48     };
49
50     WiFiAdapter.prototype.getPowered = function(success_cb, error_cb) {
51         if (wsAPI === undefined) return;
52         var me = this;
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);
56             }
57             me.powered = isEnabled;
58             success_cb(me.powered);
59         }, error_cb);
60     };
61
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');
67                 return;
68             }
69
70             var network_list = results[0];
71
72             try {
73                 var results = [];
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);
77                     }
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;
82                         }
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;
87                         }
88                         if (network_list[i][1].EncryptionMode !== undefined) {
89                             network.encryption = network_list[i][1].EncryptionMode;
90                         }
91                         if (network_list[i][1].Strength !== undefined) {
92                             network.strength = network_list[i][1].Strength;
93                         }
94                         if (network_list[i][1].IPv4.Address !== undefined) {
95                             network.ipAddress = network_list[i][1].IPv4.Address;
96                         }
97                         if (network_list[i][1].IPv4.Gateway !== undefined) {
98                             network.gateway = network_list[i][1].IPv4.Gateway;
99                         }
100                         if (network_list[i][1].IPv4.Netmask !== undefined) {
101                             network.netmask = network_list[i][1].IPv4.Netmask;
102                         }
103                         results.push(network);
104                     }
105                 }
106             } catch (e) {
107                 error_cb(e);
108             }
109             success_cb(results);
110         }, error_cb);
111     };
112
113     WiFiAdapter.prototype.connectNetwork = function(network_id, security, passcode, success_cb, error_cb) {
114         if (wsAPI === undefined) return;
115
116         wsAPI.sendRequest(WS_REQUEST_TYPE.WIFI, 'connect', network_id, success_cb, error_cb);
117     };
118
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);
122     };
123
124     /* Network class */
125     WiFiNetwork = function(object_path) {
126         this.id = object_path;
127         this.ssid = '';
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';
134         this.strength = -1;
135     };
136
137     WiFiNetwork.prototype.getSecurity = function() {
138         return this.security;
139     }
140
141     function subscribeEvents(callback) {
142         wsAPI.subscribeEvents(callback);
143     }
144
145     function unsubscribeEvents(callback) {
146         wsAPI.unsubscribeEvents(callback);
147     }
148
149     function getDefaultAdapter() {
150         if (default_adapter === null) {
151             default_adapter = new WiFiAdapter(wsAPI);
152         }
153
154         return default_adapter;
155     };
156
157     return {
158         WiFiAdapter: WiFiAdapter,
159         WiFiNetwork: WiFiNetwork,
160         subscribeEvents: subscribeEvents,
161         unsubscribeEvents: unsubscribeEvents,
162         getDefaultAdapter: getDefaultAdapter
163     };
164 })();