| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 if (this.lockTID) { | 65 if (this.lockTID) { |
| 66 window.clearTimeout(this.lockTID); | 66 window.clearTimeout(this.lockTID); |
| 67 this.lockTID = null; | 67 this.lockTID = null; |
| 68 } | 68 } |
| 69 | 69 |
| 70 var dev = this.dev; | 70 var dev = this.dev; |
| 71 this.dev = null; | 71 this.dev = null; |
| 72 | 72 |
| 73 chrome.hid.disconnect(dev.connectionId, function() { | 73 chrome.hid.disconnect(dev.connectionId, function() { |
| 74 if (chrome.runtime.lastError) { |
| 75 console.warn(UTIL_fmt('Device ' + dev.connectionId + |
| 76 ' couldn\'t be disconnected:')); |
| 77 console.warn(chrome.runtime.lastError); |
| 78 return; |
| 79 } |
| 74 console.log(UTIL_fmt('Device ' + dev.connectionId + ' closed')); | 80 console.log(UTIL_fmt('Device ' + dev.connectionId + ' closed')); |
| 75 }); | 81 }); |
| 76 }; | 82 }; |
| 77 | 83 |
| 78 /** | 84 /** |
| 79 * Push frame to all clients. | 85 * Push frame to all clients. |
| 80 * @param {ArrayBuffer} f Data to push | 86 * @param {ArrayBuffer} f Data to push |
| 81 * @private | 87 * @private |
| 82 */ | 88 */ |
| 83 HidGnubbyDevice.prototype.publishFrame_ = function(f) { | 89 HidGnubbyDevice.prototype.publishFrame_ = function(f) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 * @param {number} cmd Request command | 212 * @param {number} cmd Request command |
| 207 * @return {boolean} true if not locked for this request. | 213 * @return {boolean} true if not locked for this request. |
| 208 * @private | 214 * @private |
| 209 */ | 215 */ |
| 210 HidGnubbyDevice.prototype.checkLock_ = function(cid, cmd) { | 216 HidGnubbyDevice.prototype.checkLock_ = function(cid, cmd) { |
| 211 if (this.lockCID) { | 217 if (this.lockCID) { |
| 212 // We have an active lock. | 218 // We have an active lock. |
| 213 if (this.lockCID != cid) { | 219 if (this.lockCID != cid) { |
| 214 // Some other channel has active lock. | 220 // Some other channel has active lock. |
| 215 | 221 |
| 216 if (cmd != GnubbyDevice.CMD_SYNC) { | 222 if (cmd != GnubbyDevice.CMD_SYNC && |
| 217 // Anything but SYNC gets an immediate busy. | 223 cmd != GnubbyDevice.CMD_INIT) { |
| 224 // Anything but SYNC|INIT gets an immediate busy. |
| 218 var busy = new Uint8Array( | 225 var busy = new Uint8Array( |
| 219 [(cid >> 24) & 255, | 226 [(cid >> 24) & 255, |
| 220 (cid >> 16) & 255, | 227 (cid >> 16) & 255, |
| 221 (cid >> 8) & 255, | 228 (cid >> 8) & 255, |
| 222 cid & 255, | 229 cid & 255, |
| 223 GnubbyDevice.CMD_ERROR, | 230 GnubbyDevice.CMD_ERROR, |
| 224 0, 1, // length | 231 0, 1, // length |
| 225 GnubbyDevice.BUSY]); | 232 GnubbyDevice.BUSY]); |
| 226 // Log the synthetic busy too. | 233 // Log the synthetic busy too. |
| 227 console.log(UTIL_fmt('<' + UTIL_BytesToHex(busy))); | 234 console.log(UTIL_fmt('<' + UTIL_BytesToHex(busy))); |
| 228 this.publishFrame_(busy.buffer); | 235 this.publishFrame_(busy.buffer); |
| 229 return false; | 236 return false; |
| 230 } | 237 } |
| 231 | 238 |
| 232 // SYNC gets to go to the device to flush OS tx/rx queues. | 239 // SYNC|INIT gets to go to the device to flush OS tx/rx queues. |
| 233 // The usb firmware always responds to SYNC, regardless of lock status. | 240 // The usb firmware is to alway respond to SYNC/INIT, |
| 241 // regardless of lock status. |
| 234 } | 242 } |
| 235 } | 243 } |
| 236 return true; | 244 return true; |
| 237 }; | 245 }; |
| 238 | 246 |
| 239 /** | 247 /** |
| 240 * Update or grab lock. | 248 * Update or grab lock. |
| 241 * @param {number} cid Channel ID | 249 * @param {number} cid Channel ID |
| 242 * @param {number} cmd Command | 250 * @param {number} cmd Command |
| 243 * @param {number} arg Command argument | 251 * @param {number} arg Command argument |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 var numEnumerated = 0; | 412 var numEnumerated = 0; |
| 405 var allDevs = []; | 413 var allDevs = []; |
| 406 | 414 |
| 407 function enumerated(devs) { | 415 function enumerated(devs) { |
| 408 allDevs = allDevs.concat(devs); | 416 allDevs = allDevs.concat(devs); |
| 409 if (++numEnumerated == permittedDevs.length) { | 417 if (++numEnumerated == permittedDevs.length) { |
| 410 cb(allDevs); | 418 cb(allDevs); |
| 411 } | 419 } |
| 412 } | 420 } |
| 413 | 421 |
| 414 GnubbyDevice.getPermittedUsbDevices(function(devs) { | 422 try { |
| 415 permittedDevs = devs; | 423 chrome.hid.getDevices({filters: [{usagePage: 0xf1d0}]}, cb); |
| 416 for (var i = 0; i < devs.length; i++) { | 424 } catch (e) { |
| 417 chrome.hid.getDevices(devs[i], enumerated); | 425 console.log(e); |
| 418 } | 426 console.log(UTIL_fmt('falling back to vid/pid enumeration')); |
| 419 }); | 427 GnubbyDevice.getPermittedUsbDevices(function(devs) { |
| 428 permittedDevs = devs; |
| 429 for (var i = 0; i < devs.length; i++) { |
| 430 chrome.hid.getDevices(devs[i], enumerated); |
| 431 } |
| 432 }); |
| 433 } |
| 420 }; | 434 }; |
| 421 | 435 |
| 422 /** | 436 /** |
| 423 * @param {Gnubbies} gnubbies The gnubbies instances this device is enumerated | 437 * @param {Gnubbies} gnubbies The gnubbies instances this device is enumerated |
| 424 * in. | 438 * in. |
| 425 * @param {number} which The index of the device to open. | 439 * @param {number} which The index of the device to open. |
| 426 * @param {!chrome.hid.HidDeviceInfo} dev The device to open. | 440 * @param {!chrome.hid.HidDeviceInfo} dev The device to open. |
| 427 * @param {function(number, GnubbyDevice=)} cb Called back with the | 441 * @param {function(number, GnubbyDevice=)} cb Called back with the |
| 428 * result of opening the device. | 442 * result of opening the device. |
| 429 */ | 443 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 */ | 476 */ |
| 463 HidGnubbyDevice.register = function(gnubbies) { | 477 HidGnubbyDevice.register = function(gnubbies) { |
| 464 var HID_GNUBBY_IMPL = { | 478 var HID_GNUBBY_IMPL = { |
| 465 isSharedAccess: true, | 479 isSharedAccess: true, |
| 466 enumerate: HidGnubbyDevice.enumerate, | 480 enumerate: HidGnubbyDevice.enumerate, |
| 467 deviceToDeviceId: HidGnubbyDevice.deviceToDeviceId, | 481 deviceToDeviceId: HidGnubbyDevice.deviceToDeviceId, |
| 468 open: HidGnubbyDevice.open | 482 open: HidGnubbyDevice.open |
| 469 }; | 483 }; |
| 470 gnubbies.registerNamespace(HidGnubbyDevice.NAMESPACE, HID_GNUBBY_IMPL); | 484 gnubbies.registerNamespace(HidGnubbyDevice.NAMESPACE, HID_GNUBBY_IMPL); |
| 471 }; | 485 }; |
| OLD | NEW |