OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Implements a low-level gnubby driver based on chrome.hid. | 6 * @fileoverview Implements a low-level gnubby driver based on chrome.hid. |
7 */ | 7 */ |
8 'use strict'; | 8 'use strict'; |
9 | 9 |
10 /** | 10 /** |
(...skipping 22 matching lines...) Expand all Loading... |
33 /** | 33 /** |
34 * Namespace for the llHidGnubby implementation. | 34 * Namespace for the llHidGnubby implementation. |
35 * @const | 35 * @const |
36 */ | 36 */ |
37 llHidGnubby.NAMESPACE = 'hid'; | 37 llHidGnubby.NAMESPACE = 'hid'; |
38 | 38 |
39 /** Destroys this low-level device instance. */ | 39 /** Destroys this low-level device instance. */ |
40 llHidGnubby.prototype.destroy = function() { | 40 llHidGnubby.prototype.destroy = function() { |
41 if (!this.dev) return; // Already dead. | 41 if (!this.dev) return; // Already dead. |
42 | 42 |
| 43 this.gnubbies_.removeOpenDevice( |
| 44 {namespace: llHidGnubby.NAMESPACE, device: this.id}); |
43 this.closing = true; | 45 this.closing = true; |
44 | 46 |
45 console.log(UTIL_fmt('llHidGnubby.destroy()')); | 47 console.log(UTIL_fmt('llHidGnubby.destroy()')); |
46 | 48 |
47 // Synthesize a close error frame to alert all clients, | 49 // Synthesize a close error frame to alert all clients, |
48 // some of which might be in read state. | 50 // some of which might be in read state. |
49 // | 51 // |
50 // Use magic CID 0 to address all. | 52 // Use magic CID 0 to address all. |
51 this.publishFrame_(new Uint8Array([ | 53 this.publishFrame_(new Uint8Array([ |
52 0, 0, 0, 0, // broadcast CID | 54 0, 0, 0, 0, // broadcast CID |
53 llGnubby.CMD_ERROR, | 55 llGnubby.CMD_ERROR, |
54 0, 1, // length | 56 0, 1, // length |
55 llGnubby.GONE]).buffer); | 57 llGnubby.GONE]).buffer); |
56 | 58 |
57 // Set all clients to closed status and remove them. | 59 // Set all clients to closed status and remove them. |
58 while (this.clients.length != 0) { | 60 while (this.clients.length != 0) { |
59 var client = this.clients.shift(); | 61 var client = this.clients.shift(); |
60 if (client) client.closed = true; | 62 if (client) client.closed = true; |
61 } | 63 } |
62 | 64 |
63 if (this.lockTID) { | 65 if (this.lockTID) { |
64 window.clearTimeout(this.lockTID); | 66 window.clearTimeout(this.lockTID); |
65 this.lockTID = null; | 67 this.lockTID = null; |
66 } | 68 } |
67 | 69 |
68 var dev = this.dev; | 70 var dev = this.dev; |
69 this.dev = null; | 71 this.dev = null; |
70 | 72 |
71 var self = this; | 73 chrome.hid.disconnect(dev.connectionId, function() { |
72 | |
73 function onClosed() { | |
74 console.log(UTIL_fmt('Device ' + dev.handle + ' closed')); | 74 console.log(UTIL_fmt('Device ' + dev.handle + ' closed')); |
75 self.gnubbies_.removeOpenDevice( | 75 }); |
76 {namespace: llHidGnubby.NAMESPACE, device: self.id}); | |
77 } | |
78 | |
79 chrome.hid.disconnect(dev.connectionId, onClosed); | |
80 }; | 76 }; |
81 | 77 |
82 /** | 78 /** |
83 * Push frame to all clients. | 79 * Push frame to all clients. |
84 * @param {ArrayBuffer} f Data to push | 80 * @param {ArrayBuffer} f Data to push |
85 * @private | 81 * @private |
86 */ | 82 */ |
87 llHidGnubby.prototype.publishFrame_ = function(f) { | 83 llHidGnubby.prototype.publishFrame_ = function(f) { |
88 var old = this.clients; | 84 var old = this.clients; |
89 | 85 |
90 var remaining = []; | 86 var remaining = []; |
91 var changes = false; | 87 var changes = false; |
92 for (var i = 0; i < old.length; ++i) { | 88 for (var i = 0; i < old.length; ++i) { |
93 var client = old[i]; | 89 var client = old[i]; |
94 if (client.receivedFrame(f)) { | 90 if (client.receivedFrame(f)) { |
95 // Client still alive; keep on list. | 91 // Client still alive; keep on list. |
96 remaining.push(client); | 92 remaining.push(client); |
97 } else { | 93 } else { |
98 changes = true; | 94 changes = true; |
99 console.log(UTIL_fmt( | 95 console.log(UTIL_fmt( |
100 '[' + client.cid.toString(16) + '] left?')); | 96 '[' + client.cid.toString(16) + '] left?')); |
101 } | 97 } |
102 } | 98 } |
103 if (changes) this.clients = remaining; | 99 if (changes) this.clients = remaining; |
104 }; | 100 }; |
105 | 101 |
106 /** | 102 /** |
107 * @return {boolean} whether this device is open and ready to use. | |
108 * @private | |
109 */ | |
110 llHidGnubby.prototype.readyToUse_ = function() { | |
111 if (this.closing) return false; | |
112 if (!this.dev) return false; | |
113 | |
114 return true; | |
115 }; | |
116 | |
117 /** | |
118 * Register a client for this gnubby. | 103 * Register a client for this gnubby. |
119 * @param {*} who The client. | 104 * @param {*} who The client. |
120 */ | 105 */ |
121 llHidGnubby.prototype.registerClient = function(who) { | 106 llHidGnubby.prototype.registerClient = function(who) { |
122 for (var i = 0; i < this.clients.length; ++i) { | 107 for (var i = 0; i < this.clients.length; ++i) { |
123 if (this.clients[i] === who) return; // Already registered. | 108 if (this.clients[i] === who) return; // Already registered. |
124 } | 109 } |
125 this.clients.push(who); | 110 this.clients.push(who); |
126 if (this.clients.length == 1) { | 111 if (this.clients.length == 1) { |
127 // First client? Kick off read loop. | 112 // First client? Kick off read loop. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 this.lockMillis); | 277 this.lockMillis); |
293 } | 278 } |
294 } | 279 } |
295 }; | 280 }; |
296 | 281 |
297 /** | 282 /** |
298 * Queue command to be sent. | 283 * Queue command to be sent. |
299 * If queue was empty, initiate the write. | 284 * If queue was empty, initiate the write. |
300 * @param {number} cid The client's channel ID. | 285 * @param {number} cid The client's channel ID. |
301 * @param {number} cmd The command to send. | 286 * @param {number} cmd The command to send. |
302 * @param {ArrayBuffer} data Command arguments | 287 * @param {ArrayBuffer|Uint8Array} data Command arguments |
303 */ | 288 */ |
304 llHidGnubby.prototype.queueCommand = function(cid, cmd, data) { | 289 llHidGnubby.prototype.queueCommand = function(cid, cmd, data) { |
305 if (!this.dev) return; | 290 if (!this.dev) return; |
306 if (!this.checkLock_(cid, cmd)) return; | 291 if (!this.checkLock_(cid, cmd)) return; |
307 | 292 |
308 var u8 = new Uint8Array(data); | 293 var u8 = new Uint8Array(data); |
309 var f = new Uint8Array(64); | 294 var f = new Uint8Array(64); |
310 | 295 |
311 llHidGnubby.setCid_(f, cid); | 296 llHidGnubby.setCid_(f, cid); |
312 f[4] = cmd; | 297 f[4] = cmd; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 * @param {Gnubbies} gnubbies Gnubbies registry | 434 * @param {Gnubbies} gnubbies Gnubbies registry |
450 */ | 435 */ |
451 llHidGnubby.register = function(gnubbies) { | 436 llHidGnubby.register = function(gnubbies) { |
452 var HID_GNUBBY_IMPL = { | 437 var HID_GNUBBY_IMPL = { |
453 enumerate: llHidGnubby.enumerate, | 438 enumerate: llHidGnubby.enumerate, |
454 deviceToDeviceId: llHidGnubby.deviceToDeviceId, | 439 deviceToDeviceId: llHidGnubby.deviceToDeviceId, |
455 open: llHidGnubby.open | 440 open: llHidGnubby.open |
456 }; | 441 }; |
457 gnubbies.registerNamespace(llHidGnubby.NAMESPACE, HID_GNUBBY_IMPL); | 442 gnubbies.registerNamespace(llHidGnubby.NAMESPACE, HID_GNUBBY_IMPL); |
458 }; | 443 }; |
OLD | NEW |