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
10 /* Dummy backend for faking websocket daemon */
11 var dummyBackend = false;
13 var WS_REQUEST_TYPE = {
15 BLUETOOTH: "bluetooth",
24 BLUETOOTH: "bluetooth",
31 /* web socket module to connect to the settings daemon */
32 var wsAPI = (function() {
33 /* number of connection retries to attempt if the socket closes */
35 this.connected = false;
36 this.event_callbacks = $.Callbacks();
38 /* default values for WebSocket */
39 this.socketUrl = 'ws://localhost:16000/';
40 this.socketProtocol = 'http-only';
43 this.methodCalls = [];
44 for (var i = 0; i < 100; i++) {
45 this.methodCalls[i] = null;
48 this.MethodCall = function(id, name, success_cb, error_cb) {
50 this.successCB = success_cb;
51 this.errorCB = error_cb;
52 this.transactionid = id;
55 this.start = function() {
56 me.timeout = setTimeout(function() {
57 if (me.errorCB !== undefined) {
58 me.errorCB('\"' + me.name + '\" method timed out after ' + self.timeouttime + 'ms');
63 this.finish = function() {
64 if (me.timeout !== undefined) {
65 clearTimeout(me.timeout);
71 this.EventObject = function(type, name, value) {
78 function connect(url, protocol, sucess_cb, error_cb) {
79 self.socketUrl = typeof url !== 'undefined' ? url : self.socketUrl;
80 self.socketProtocol = typeof protocol !== 'undefined' ? protocol : self.socketProtocol;
81 self.successCB = sucess_cb;
82 self.errorCB = error_cb;
84 if ('WebSocket' in window) {
85 if (self.socketProtocol.length > 0) {
86 self.socket = new WebSocket(self.socketUrl, self.socketProtocol);
88 self.socket = new WebSocket(self);
90 console.log('Connecting to websocket: ' + self.socketUrl);
92 self.socket.onopen = function() {
93 self.connected = true;
94 console.log('websocket opened');
98 self.socket.onclose = function() {
99 self.connected = false;
100 console.log('websocket closed');
103 /* fake the connection for dummy backend */
104 self.connected = true;
109 self.errorCB('Disconnected from settings daemon');
112 self.socket.onerror = function(e) {
113 console.log('web socket error: ', e.data);
116 /* ignore websocket error */
121 self.errorCB('websocket error: ' + e.data);
123 self.errorCB('websocket error: unknown');
127 self.socket.onmessage = function(e) {
128 self.receive(e.data);
131 console.log('Websockets not supported');
135 function send(msg, success_cb, error_cb) {
136 if (!this.connected) {
137 if (errorCB !== undefined) {
138 errorCB('Settings daemon is not connected');
142 var i = this.methodIdx;
143 this.methodIdx = (this.methodIdx + 1) % 100;
144 this.methodCalls[i] = new this.MethodCall(msg.transactionid, msg.name, success_cb, error_cb);
145 this.methodCalls[i].start();
147 var jsonMsg = JSON.stringify(msg);
148 console.log('Sending json msg: ' + jsonMsg);
150 /* fake with dummy data */
151 dummyBackendSend(msg);
153 this.socket.send(jsonMsg);
157 function fireEvent(type, name, value) {
158 var event = new this.EventObject(type, name, value);
159 console.log('Fire event: [' + type + ', ' + name + ', ' + value + ']');
160 event_callbacks.fire(event);
163 function receive(msg) {
167 console.log("Received json msg: " + msg);
168 event = JSON.parse(msg);
170 self.iErrorCB('GARBAGE MESSAGE: ' + msg);
174 if ((event === undefined) || (event.type === undefined) || (event.name === undefined)) {
175 self.iErrorCB('BADLY FORMED MESSAGE: ' + msg);
178 if (event.type === 'methodReply') {
179 var calls = this.methodCalls;
180 for (var i = 0; i < calls.length; i++) {
182 if (call && (!call.done) && (call.transactionid === event.transactionid)) {
184 if (event.error !== undefined) {
185 call.errorCB(event.error);
187 if (event.data !== undefined && call.successCB !== undefined) {
188 call.successCB(event.data);
193 } else if (event.type === 'event') {
194 self.fireSevent(event.name, event.data);
199 function generateTransactionId() {
201 for (i = 0; i < 8; i++) {
202 var num = Math.floor((Math.random() + 1) * 65536);
203 val[i] = num.toString(16).substring(1);
205 var uuid = val[0] + val[1] + '-' + val[2] + '-' + val[3] + '-' + val[4] + '-' + val[5] + val[6] + val[7];
209 function sendRequest(request_type, request_name, request_args, success_cb, error_cb) {
211 'type': request_type,
212 'name': request_name,
213 'transactionid': generateTransactionId(),
217 send(msg, success_cb, error_cb);
220 function subscribeEvents(callback) {
221 event_callbacks.add(callback);
224 function unsubscribeEvents(callback) {
225 event_callbacks.remove(callback);
228 /* this is dummy data for testing purposes */
229 function dummyBackendSend(msg) {
231 console.log('Sending to dummy server');
233 var calls = this.methodCalls;
236 for (var i = 0; i < calls.length; i++) {
238 if (call && (!call.done) && (call.transactionid === msg.transactionid)) {
240 if (msg.error !== undefined) {
241 call.errorCB(msg.error);
243 if (msg.data !== undefined && call.successCB && call.errorCB !== undefined) {
247 case WS_REQUEST_TYPE.WIFI:
248 if (msg.name === 'isEnabled') {
249 /* default to enabled */
253 replyMsg = JSON.stringify(results);
254 } else if (msg.name === 'enable') {
255 if (msg.data === true) {
256 fireEvent(WS_EVENT_TYPE.WIFI, 'enabled', true);
258 fireEvent(WS_EVENT_TYPE.WIFI, 'enabled', false);
260 } else if (msg.name === 'scan') {
263 'ssid': 'Access Point 1',
266 'ssid': 'Access Point 2',
269 'ssid': 'Access Point 3',
272 'ssid': 'Access Point 4',
276 replyMsg = JSON.stringify(results);
277 /* simulate scan behavior */
279 } else if (msg.name === 'connect') {
281 fireEvent(WS_EVENT_TYPE.WIFI, 'connecting', replyMsg);
282 setTimeout(function() {
283 fireEvent(WS_EVENT_TYPE.WIFI, 'connected', replyMsg);
285 } else if (msg.name === 'disconnect') {
287 fireEvent(WS_EVENT_TYPE.WIFI, 'disconnecting', replyMsg);
288 setTimeout(function() {
289 fireEvent(WS_EVENT_TYPE.WIFI, 'disconnected', replyMsg);
292 call.errorCB('Unsupported request name: ' + msg.name);
296 case WS_REQUEST_TYPE.BLUETOOTH:
297 if (msg.name === 'enable') {
298 fireEvent(WS_EVENT_TYPE.BLUETOOTH, 'enabled', true);
299 } else if (msg.name === 'disable') {
300 fireEvent(WS_EVENT_TYPE.BLUETOOTH, 'enabled', false);
302 call.errorCB('Unsupported request name: ' + msg.name);
306 case WS_REQUEST_TYPE.DATETIME:
308 case WS_REQUEST_TYPE.DISPLAY:
309 case WS_REQUEST_TYPE.SOUND:
310 case WS_REQUEST_TYPE.LOCALE:
311 call.errorCB('Request not implemented');
314 call.errorCB('Invalid request type: ' + msg.type);
317 setTimeout(function() {
318 call.successCB(replyMsg);
329 sendRequest: sendRequest,
330 subscribeEvents: subscribeEvents