| 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.usb. | 6 * @fileoverview Implements a low-level gnubby driver based on chrome.usb. |
| 7 */ | 7 */ |
| 8 'use strict'; | 8 'use strict'; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Low level gnubby 'driver'. One per physical USB device. | 11 * Low level gnubby 'driver'. One per physical USB device. |
| 12 * @param {Gnubbies} gnubbies The gnubbies instances this device is enumerated | 12 * @param {Gnubbies} gnubbies The gnubbies instances this device is enumerated |
| 13 * in. | 13 * in. |
| 14 * @param {!chrome.usb.ConnectionHandle} dev The device. | 14 * @param {!chrome.usb.ConnectionHandle} dev The device. |
| 15 * @param {number} id The device's id. | 15 * @param {number} id The device's id. |
| 16 * @param {number} inEndpoint The device's in endpoint. | 16 * @param {number} inEndpoint The device's in endpoint. |
| 17 * @param {number} outEndpoint The device's out endpoint. | 17 * @param {number} outEndpoint The device's out endpoint. |
| 18 * @constructor | 18 * @constructor |
| 19 * @implements {GnubbyDevice} | 19 * @implements {GnubbyDevice} |
| 20 */ | 20 */ |
| 21 function UsbGnubbyDevice(gnubbies, dev, id, inEndpoint, outEndpoint) { | 21 function UsbGnubbyDevice(gnubbies, dev, id, inEndpoint, outEndpoint) { |
| 22 /** @private {Gnubbies} */ | 22 /** @private {Gnubbies} */ |
| 23 this.gnubbies_ = gnubbies; | 23 this.gnubbies_ = gnubbies; |
| 24 this.dev = dev; | 24 this.dev = dev; |
| 25 this.id = id; | 25 this.id = id; |
| 26 this.inEndpoint = inEndpoint; | 26 this.inEndpoint = inEndpoint; |
| 27 this.outEndpoint = outEndpoint; | 27 this.outEndpoint = outEndpoint; |
| 28 this.txqueue = []; | 28 this.txqueue = []; |
| 29 this.clients = []; | 29 this.clients = []; |
| 30 this.lockCID = 0; // channel ID of client holding a lock, if != 0. | 30 this.lockCID = 0; // channel ID of client holding a lock, if != 0. |
| 31 this.lockMillis = 0; // current lock period. | 31 this.lockMillis = 0; // current lock period. |
| 32 this.lockTID = null; // timer id of lock timeout. | 32 this.lockTID = null; // timer id of lock timeout. |
| 33 this.closing = false; // device to be closed by receive loop. | 33 this.closing = false; // device to be closed by receive loop. |
| 34 this.updating = false; // device firmware is in final stage of updating. | 34 this.updating = false; // device firmware is in final stage of updating. |
| 35 this.inTransferPending = false; | 35 this.inTransferPending = false; |
| 36 this.outTransferPending = false; | 36 this.outTransferPending = false; |
| 37 } | 37 } |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Namespace for the UsbGnubbyDevice implementation. | 40 * Namespace for the UsbGnubbyDevice implementation. |
| 41 * @const | 41 * @const |
| 42 */ | 42 */ |
| 43 UsbGnubbyDevice.NAMESPACE = 'usb'; | 43 UsbGnubbyDevice.NAMESPACE = 'usb'; |
| 44 | 44 |
| 45 /** Destroys this low-level device instance. */ | 45 /** Destroys this low-level device instance. */ |
| 46 UsbGnubbyDevice.prototype.destroy = function() { | 46 UsbGnubbyDevice.prototype.destroy = function() { |
| 47 function closeLowLevelDevice(dev) { | 47 function closeLowLevelDevice(dev) { |
| 48 chrome.usb.releaseInterface(dev, 0, function() { | 48 chrome.usb.releaseInterface(dev, 0, function() { |
| 49 if (chrome.runtime.lastError) { | 49 if (chrome.runtime.lastError) { |
| 50 console.warn(UTIL_fmt('Device ' + dev.handle + | 50 console.warn( |
| 51 ' couldn\'t be released:')); | 51 UTIL_fmt('Device ' + dev.handle + ' couldn\'t be released:')); |
| 52 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); | 52 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 console.log(UTIL_fmt('Device ' + dev.handle + ' released')); | 55 console.log(UTIL_fmt('Device ' + dev.handle + ' released')); |
| 56 chrome.usb.closeDevice(dev, function() { | 56 chrome.usb.closeDevice(dev, function() { |
| 57 if (chrome.runtime.lastError) { | 57 if (chrome.runtime.lastError) { |
| 58 console.warn(UTIL_fmt('Device ' + dev.handle + | 58 console.warn( |
| 59 ' couldn\'t be closed:')); | 59 UTIL_fmt('Device ' + dev.handle + ' couldn\'t be closed:')); |
| 60 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); | 60 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 console.log(UTIL_fmt('Device ' + dev.handle + ' closed')); | 63 console.log(UTIL_fmt('Device ' + dev.handle + ' closed')); |
| 64 }); | 64 }); |
| 65 }); | 65 }); |
| 66 } | 66 } |
| 67 | 67 |
| 68 if (!this.dev) return; // Already dead. | 68 if (!this.dev) |
| 69 return; // Already dead. |
| 69 | 70 |
| 70 this.gnubbies_.removeOpenDevice( | 71 this.gnubbies_.removeOpenDevice( |
| 71 {namespace: UsbGnubbyDevice.NAMESPACE, device: this.id}); | 72 {namespace: UsbGnubbyDevice.NAMESPACE, device: this.id}); |
| 72 this.closing = true; | 73 this.closing = true; |
| 73 | 74 |
| 74 console.log(UTIL_fmt('UsbGnubbyDevice.destroy()')); | 75 console.log(UTIL_fmt('UsbGnubbyDevice.destroy()')); |
| 75 | 76 |
| 76 // Synthesize a close error frame to alert all clients, | 77 // Synthesize a close error frame to alert all clients, |
| 77 // some of which might be in read state. | 78 // some of which might be in read state. |
| 78 // | 79 // |
| 79 // Use magic CID 0 to address all. | 80 // Use magic CID 0 to address all. |
| 80 this.publishFrame_(new Uint8Array([ | 81 this.publishFrame_(new Uint8Array([ |
| 81 0, 0, 0, 0, // broadcast CID | 82 0, 0, 0, 0, // broadcast CID |
| 82 GnubbyDevice.CMD_ERROR, | 83 GnubbyDevice.CMD_ERROR, 0, 1, // length |
| 83 0, 1, // length | 84 GnubbyDevice.GONE |
| 84 GnubbyDevice.GONE]).buffer); | 85 ]).buffer); |
| 85 | 86 |
| 86 // Set all clients to closed status and remove them. | 87 // Set all clients to closed status and remove them. |
| 87 while (this.clients.length != 0) { | 88 while (this.clients.length != 0) { |
| 88 var client = this.clients.shift(); | 89 var client = this.clients.shift(); |
| 89 if (client) client.closed = true; | 90 if (client) |
| 91 client.closed = true; |
| 90 } | 92 } |
| 91 | 93 |
| 92 if (this.lockTID) { | 94 if (this.lockTID) { |
| 93 window.clearTimeout(this.lockTID); | 95 window.clearTimeout(this.lockTID); |
| 94 this.lockTID = null; | 96 this.lockTID = null; |
| 95 } | 97 } |
| 96 | 98 |
| 97 var dev = this.dev; | 99 var dev = this.dev; |
| 98 this.dev = null; | 100 this.dev = null; |
| 99 var reallyCloseDevice = closeLowLevelDevice.bind(null, dev); | 101 var reallyCloseDevice = closeLowLevelDevice.bind(null, dev); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 129 | 131 |
| 130 var remaining = []; | 132 var remaining = []; |
| 131 var changes = false; | 133 var changes = false; |
| 132 for (var i = 0; i < old.length; ++i) { | 134 for (var i = 0; i < old.length; ++i) { |
| 133 var client = old[i]; | 135 var client = old[i]; |
| 134 if (client.receivedFrame(f)) { | 136 if (client.receivedFrame(f)) { |
| 135 // Client still alive; keep on list. | 137 // Client still alive; keep on list. |
| 136 remaining.push(client); | 138 remaining.push(client); |
| 137 } else { | 139 } else { |
| 138 changes = true; | 140 changes = true; |
| 139 console.log(UTIL_fmt( | 141 console.log(UTIL_fmt('[' + Gnubby.hexCid(client.cid) + '] left?')); |
| 140 '[' + Gnubby.hexCid(client.cid) + '] left?')); | |
| 141 } | 142 } |
| 142 } | 143 } |
| 143 if (changes) this.clients = remaining; | 144 if (changes) |
| 145 this.clients = remaining; |
| 144 }; | 146 }; |
| 145 | 147 |
| 146 /** | 148 /** |
| 147 * @return {boolean} whether this device is open and ready to use. | 149 * @return {boolean} whether this device is open and ready to use. |
| 148 * @private | 150 * @private |
| 149 */ | 151 */ |
| 150 UsbGnubbyDevice.prototype.readyToUse_ = function() { | 152 UsbGnubbyDevice.prototype.readyToUse_ = function() { |
| 151 if (this.closing) return false; | 153 if (this.closing) |
| 152 if (!this.dev) return false; | 154 return false; |
| 155 if (!this.dev) |
| 156 return false; |
| 153 | 157 |
| 154 return true; | 158 return true; |
| 155 }; | 159 }; |
| 156 | 160 |
| 157 /** | 161 /** |
| 158 * Reads one reply from the low-level device. | 162 * Reads one reply from the low-level device. |
| 159 * @private | 163 * @private |
| 160 */ | 164 */ |
| 161 UsbGnubbyDevice.prototype.readOneReply_ = function() { | 165 UsbGnubbyDevice.prototype.readOneReply_ = function() { |
| 162 if (!this.readyToUse_()) return; // No point in continuing. | 166 if (!this.readyToUse_()) |
| 163 if (this.updating) return; // Do not bother waiting for final update reply. | 167 return; // No point in continuing. |
| 168 if (this.updating) |
| 169 return; // Do not bother waiting for final update reply. |
| 164 | 170 |
| 165 var self = this; | 171 var self = this; |
| 166 | 172 |
| 167 function inTransferComplete(x) { | 173 function inTransferComplete(x) { |
| 168 self.inTransferPending = false; | 174 self.inTransferPending = false; |
| 169 | 175 |
| 170 if (!self.readyToUse_()) return; // No point in continuing. | 176 if (!self.readyToUse_()) |
| 177 return; // No point in continuing. |
| 171 | 178 |
| 172 if (chrome.runtime.lastError) { | 179 if (chrome.runtime.lastError) { |
| 173 console.warn(UTIL_fmt('in bulkTransfer got lastError: ')); | 180 console.warn(UTIL_fmt('in bulkTransfer got lastError: ')); |
| 174 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); | 181 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); |
| 175 window.setTimeout(function() { self.destroy(); }, 0); | 182 window.setTimeout(function() { |
| 183 self.destroy(); |
| 184 }, 0); |
| 176 return; | 185 return; |
| 177 } | 186 } |
| 178 | 187 |
| 179 if (x.data) { | 188 if (x.data) { |
| 180 var u8 = new Uint8Array(x.data); | 189 var u8 = new Uint8Array(x.data); |
| 181 console.log(UTIL_fmt('<' + UTIL_BytesToHex(u8))); | 190 console.log(UTIL_fmt('<' + UTIL_BytesToHex(u8))); |
| 182 | 191 |
| 183 self.publishFrame_(x.data); | 192 self.publishFrame_(x.data); |
| 184 | 193 |
| 185 // Write another pending request, if any. | 194 // Write another pending request, if any. |
| 186 window.setTimeout( | 195 window.setTimeout(function() { |
| 187 function() { | 196 self.txqueue.shift(); // Drop sent frame from queue. |
| 188 self.txqueue.shift(); // Drop sent frame from queue. | 197 self.writeOneRequest_(); |
| 189 self.writeOneRequest_(); | 198 }, 0); |
| 190 }, | |
| 191 0); | |
| 192 } else { | 199 } else { |
| 193 console.log(UTIL_fmt('no x.data!')); | 200 console.log(UTIL_fmt('no x.data!')); |
| 194 console.log(x); | 201 console.log(x); |
| 195 window.setTimeout(function() { self.destroy(); }, 0); | 202 window.setTimeout(function() { |
| 203 self.destroy(); |
| 204 }, 0); |
| 196 } | 205 } |
| 197 } | 206 } |
| 198 | 207 |
| 199 if (this.inTransferPending == false) { | 208 if (this.inTransferPending == false) { |
| 200 this.inTransferPending = true; | 209 this.inTransferPending = true; |
| 201 chrome.usb.bulkTransfer( | 210 chrome.usb.bulkTransfer( |
| 202 /** @type {!chrome.usb.ConnectionHandle} */(this.dev), | 211 /** @type {!chrome.usb.ConnectionHandle} */ (this.dev), |
| 203 { direction: 'in', endpoint: this.inEndpoint, length: 2048 }, | 212 {direction: 'in', endpoint: this.inEndpoint, length: 2048}, |
| 204 inTransferComplete); | 213 inTransferComplete); |
| 205 } else { | 214 } else { |
| 206 throw 'inTransferPending!'; | 215 throw 'inTransferPending!'; |
| 207 } | 216 } |
| 208 }; | 217 }; |
| 209 | 218 |
| 210 /** | 219 /** |
| 211 * Register a client for this gnubby. | 220 * Register a client for this gnubby. |
| 212 * @param {*} who The client. | 221 * @param {*} who The client. |
| 213 */ | 222 */ |
| 214 UsbGnubbyDevice.prototype.registerClient = function(who) { | 223 UsbGnubbyDevice.prototype.registerClient = function(who) { |
| 215 for (var i = 0; i < this.clients.length; ++i) { | 224 for (var i = 0; i < this.clients.length; ++i) { |
| 216 if (this.clients[i] === who) return; // Already registered. | 225 if (this.clients[i] === who) |
| 226 return; // Already registered. |
| 217 } | 227 } |
| 218 this.clients.push(who); | 228 this.clients.push(who); |
| 219 }; | 229 }; |
| 220 | 230 |
| 221 /** | 231 /** |
| 222 * De-register a client. | 232 * De-register a client. |
| 223 * @param {*} who The client. | 233 * @param {*} who The client. |
| 224 * @return {number} The number of remaining listeners for this device, or -1 | 234 * @return {number} The number of remaining listeners for this device, or -1 |
| 225 * Returns number of remaining listeners for this device. | 235 * Returns number of remaining listeners for this device. |
| 226 * if this had no clients to start with. | 236 * if this had no clients to start with. |
| 227 */ | 237 */ |
| 228 UsbGnubbyDevice.prototype.deregisterClient = function(who) { | 238 UsbGnubbyDevice.prototype.deregisterClient = function(who) { |
| 229 var current = this.clients; | 239 var current = this.clients; |
| 230 if (current.length == 0) return -1; | 240 if (current.length == 0) |
| 241 return -1; |
| 231 this.clients = []; | 242 this.clients = []; |
| 232 for (var i = 0; i < current.length; ++i) { | 243 for (var i = 0; i < current.length; ++i) { |
| 233 var client = current[i]; | 244 var client = current[i]; |
| 234 if (client !== who) this.clients.push(client); | 245 if (client !== who) |
| 246 this.clients.push(client); |
| 235 } | 247 } |
| 236 return this.clients.length; | 248 return this.clients.length; |
| 237 }; | 249 }; |
| 238 | 250 |
| 239 /** | 251 /** |
| 240 * @param {*} who The client. | 252 * @param {*} who The client. |
| 241 * @return {boolean} Whether this device has who as a client. | 253 * @return {boolean} Whether this device has who as a client. |
| 242 */ | 254 */ |
| 243 UsbGnubbyDevice.prototype.hasClient = function(who) { | 255 UsbGnubbyDevice.prototype.hasClient = function(who) { |
| 244 if (this.clients.length == 0) return false; | 256 if (this.clients.length == 0) |
| 257 return false; |
| 245 for (var i = 0; i < this.clients.length; ++i) { | 258 for (var i = 0; i < this.clients.length; ++i) { |
| 246 if (who === this.clients[i]) | 259 if (who === this.clients[i]) |
| 247 return true; | 260 return true; |
| 248 } | 261 } |
| 249 return false; | 262 return false; |
| 250 }; | 263 }; |
| 251 | 264 |
| 252 /** | 265 /** |
| 253 * Stuff queued frames from txqueue[] to device, one by one. | 266 * Stuff queued frames from txqueue[] to device, one by one. |
| 254 * @private | 267 * @private |
| 255 */ | 268 */ |
| 256 UsbGnubbyDevice.prototype.writeOneRequest_ = function() { | 269 UsbGnubbyDevice.prototype.writeOneRequest_ = function() { |
| 257 if (!this.readyToUse_()) return; // No point in continuing. | 270 if (!this.readyToUse_()) |
| 271 return; // No point in continuing. |
| 258 | 272 |
| 259 if (this.txqueue.length == 0) return; // Nothing to send. | 273 if (this.txqueue.length == 0) |
| 274 return; // Nothing to send. |
| 260 | 275 |
| 261 var frame = this.txqueue[0]; | 276 var frame = this.txqueue[0]; |
| 262 | 277 |
| 263 var self = this; | 278 var self = this; |
| 264 function OutTransferComplete(x) { | 279 function OutTransferComplete(x) { |
| 265 self.outTransferPending = false; | 280 self.outTransferPending = false; |
| 266 | 281 |
| 267 if (!self.readyToUse_()) return; // No point in continuing. | 282 if (!self.readyToUse_()) |
| 283 return; // No point in continuing. |
| 268 | 284 |
| 269 if (chrome.runtime.lastError) { | 285 if (chrome.runtime.lastError) { |
| 270 console.warn(UTIL_fmt('out bulkTransfer lastError: ')); | 286 console.warn(UTIL_fmt('out bulkTransfer lastError: ')); |
| 271 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); | 287 console.warn(UTIL_fmt(chrome.runtime.lastError.message)); |
| 272 window.setTimeout(function() { self.destroy(); }, 0); | 288 window.setTimeout(function() { |
| 289 self.destroy(); |
| 290 }, 0); |
| 273 return; | 291 return; |
| 274 } | 292 } |
| 275 | 293 |
| 276 window.setTimeout(function() { self.readOneReply_(); }, 0); | 294 window.setTimeout(function() { |
| 295 self.readOneReply_(); |
| 296 }, 0); |
| 277 }; | 297 }; |
| 278 | 298 |
| 279 var u8 = new Uint8Array(frame); | 299 var u8 = new Uint8Array(frame); |
| 280 | 300 |
| 281 // See whether this requires scrubbing before logging. | 301 // See whether this requires scrubbing before logging. |
| 282 var alternateLog = Gnubby.hasOwnProperty('redactRequestLog') && | 302 var alternateLog = Gnubby.hasOwnProperty('redactRequestLog') && |
| 283 Gnubby['redactRequestLog'](u8); | 303 Gnubby['redactRequestLog'](u8); |
| 284 if (alternateLog) { | 304 if (alternateLog) { |
| 285 console.log(UTIL_fmt('>' + alternateLog)); | 305 console.log(UTIL_fmt('>' + alternateLog)); |
| 286 } else { | 306 } else { |
| 287 console.log(UTIL_fmt('>' + UTIL_BytesToHex(u8))); | 307 console.log(UTIL_fmt('>' + UTIL_BytesToHex(u8))); |
| 288 } | 308 } |
| 289 | 309 |
| 290 if (this.outTransferPending == false) { | 310 if (this.outTransferPending == false) { |
| 291 this.outTransferPending = true; | 311 this.outTransferPending = true; |
| 292 chrome.usb.bulkTransfer( | 312 chrome.usb.bulkTransfer( |
| 293 /** @type {!chrome.usb.ConnectionHandle} */(this.dev), | 313 /** @type {!chrome.usb.ConnectionHandle} */ (this.dev), |
| 294 { direction: 'out', endpoint: this.outEndpoint, data: frame }, | 314 {direction: 'out', endpoint: this.outEndpoint, data: frame}, |
| 295 OutTransferComplete); | 315 OutTransferComplete); |
| 296 } else { | 316 } else { |
| 297 throw 'outTransferPending!'; | 317 throw 'outTransferPending!'; |
| 298 } | 318 } |
| 299 }; | 319 }; |
| 300 | 320 |
| 301 /** | 321 /** |
| 302 * Check whether channel is locked for this request or not. | 322 * Check whether channel is locked for this request or not. |
| 303 * @param {number} cid Channel id | 323 * @param {number} cid Channel id |
| 304 * @param {number} cmd Command to be sent | 324 * @param {number} cmd Command to be sent |
| 305 * @return {boolean} true if not locked for this request. | 325 * @return {boolean} true if not locked for this request. |
| 306 * @private | 326 * @private |
| 307 */ | 327 */ |
| 308 UsbGnubbyDevice.prototype.checkLock_ = function(cid, cmd) { | 328 UsbGnubbyDevice.prototype.checkLock_ = function(cid, cmd) { |
| 309 if (this.lockCID) { | 329 if (this.lockCID) { |
| 310 // We have an active lock. | 330 // We have an active lock. |
| 311 if (this.lockCID != cid) { | 331 if (this.lockCID != cid) { |
| 312 // Some other channel has active lock. | 332 // Some other channel has active lock. |
| 313 | 333 |
| 314 if (cmd != GnubbyDevice.CMD_SYNC && | 334 if (cmd != GnubbyDevice.CMD_SYNC && cmd != GnubbyDevice.CMD_INIT) { |
| 315 cmd != GnubbyDevice.CMD_INIT) { | |
| 316 // Anything but SYNC|INIT gets an immediate busy. | 335 // Anything but SYNC|INIT gets an immediate busy. |
| 317 var busy = new Uint8Array( | 336 var busy = new Uint8Array([ |
| 318 [(cid >> 24) & 255, | 337 (cid >> 24) & 255, (cid >> 16) & 255, (cid >> 8) & 255, cid & 255, |
| 319 (cid >> 16) & 255, | 338 GnubbyDevice.CMD_ERROR, 0, 1, // length |
| 320 (cid >> 8) & 255, | 339 GnubbyDevice.BUSY |
| 321 cid & 255, | 340 ]); |
| 322 GnubbyDevice.CMD_ERROR, | |
| 323 0, 1, // length | |
| 324 GnubbyDevice.BUSY]); | |
| 325 // Log the synthetic busy too. | 341 // Log the synthetic busy too. |
| 326 console.log(UTIL_fmt('<' + UTIL_BytesToHex(busy))); | 342 console.log(UTIL_fmt('<' + UTIL_BytesToHex(busy))); |
| 327 this.publishFrame_(busy.buffer); | 343 this.publishFrame_(busy.buffer); |
| 328 return false; | 344 return false; |
| 329 } | 345 } |
| 330 | 346 |
| 331 // SYNC|INIT get to go to the device to flush OS tx/rx queues. | 347 // SYNC|INIT get to go to the device to flush OS tx/rx queues. |
| 332 // The usb firmware is to always respond to SYNC|INIT, | 348 // The usb firmware is to always respond to SYNC|INIT, |
| 333 // regardless of lock status. | 349 // regardless of lock status. |
| 334 } | 350 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 359 this.lockMillis = nseconds * 1000 + 100; | 375 this.lockMillis = nseconds * 1000 + 100; |
| 360 } else { | 376 } else { |
| 361 // Releasing lock voluntarily. | 377 // Releasing lock voluntarily. |
| 362 this.lockCID = 0; | 378 this.lockCID = 0; |
| 363 } | 379 } |
| 364 } | 380 } |
| 365 | 381 |
| 366 // (re)set the lock timeout if we still hold it. | 382 // (re)set the lock timeout if we still hold it. |
| 367 if (this.lockCID) { | 383 if (this.lockCID) { |
| 368 var self = this; | 384 var self = this; |
| 369 this.lockTID = window.setTimeout( | 385 this.lockTID = window.setTimeout(function() { |
| 370 function() { | 386 console.warn( |
| 371 console.warn(UTIL_fmt( | 387 UTIL_fmt('lock for CID ' + Gnubby.hexCid(cid) + ' expired!')); |
| 372 'lock for CID ' + Gnubby.hexCid(cid) + ' expired!')); | 388 self.lockTID = null; |
| 373 self.lockTID = null; | 389 self.lockCID = 0; |
| 374 self.lockCID = 0; | 390 }, this.lockMillis); |
| 375 }, | |
| 376 this.lockMillis); | |
| 377 } | 391 } |
| 378 } | 392 } |
| 379 }; | 393 }; |
| 380 | 394 |
| 381 /** | 395 /** |
| 382 * Queue command to be sent. | 396 * Queue command to be sent. |
| 383 * If queue was empty, initiate the write. | 397 * If queue was empty, initiate the write. |
| 384 * @param {number} cid The client's channel ID. | 398 * @param {number} cid The client's channel ID. |
| 385 * @param {number} cmd The command to send. | 399 * @param {number} cmd The command to send. |
| 386 * @param {ArrayBuffer|Uint8Array} data Command argument data | 400 * @param {ArrayBuffer|Uint8Array} data Command argument data |
| 387 */ | 401 */ |
| 388 UsbGnubbyDevice.prototype.queueCommand = function(cid, cmd, data) { | 402 UsbGnubbyDevice.prototype.queueCommand = function(cid, cmd, data) { |
| 389 if (!this.dev) return; | 403 if (!this.dev) |
| 390 if (!this.checkLock_(cid, cmd)) return; | 404 return; |
| 405 if (!this.checkLock_(cid, cmd)) |
| 406 return; |
| 391 | 407 |
| 392 var u8 = new Uint8Array(data); | 408 var u8 = new Uint8Array(data); |
| 393 var frame = new Uint8Array(u8.length + 7); | 409 var frame = new Uint8Array(u8.length + 7); |
| 394 | 410 |
| 395 frame[0] = cid >>> 24; | 411 frame[0] = cid >>> 24; |
| 396 frame[1] = cid >>> 16; | 412 frame[1] = cid >>> 16; |
| 397 frame[2] = cid >>> 8; | 413 frame[2] = cid >>> 8; |
| 398 frame[3] = cid; | 414 frame[3] = cid; |
| 399 frame[4] = cmd; | 415 frame[4] = cmd; |
| 400 frame[5] = (u8.length >> 8); | 416 frame[5] = (u8.length >> 8); |
| 401 frame[6] = (u8.length & 255); | 417 frame[6] = (u8.length & 255); |
| 402 | 418 |
| 403 frame.set(u8, 7); | 419 frame.set(u8, 7); |
| 404 | 420 |
| 405 var lockArg = (u8.length > 0) ? u8[0] : 0; | 421 var lockArg = (u8.length > 0) ? u8[0] : 0; |
| 406 this.updateLock_(cid, cmd, lockArg); | 422 this.updateLock_(cid, cmd, lockArg); |
| 407 | 423 |
| 408 var wasEmpty = (this.txqueue.length == 0); | 424 var wasEmpty = (this.txqueue.length == 0); |
| 409 this.txqueue.push(frame.buffer); | 425 this.txqueue.push(frame.buffer); |
| 410 if (wasEmpty) this.writeOneRequest_(); | 426 if (wasEmpty) |
| 427 this.writeOneRequest_(); |
| 411 }; | 428 }; |
| 412 | 429 |
| 413 /** | 430 /** |
| 414 * @const | 431 * @const |
| 415 */ | 432 */ |
| 416 UsbGnubbyDevice.WINUSB_VID_PIDS = [ | 433 UsbGnubbyDevice.WINUSB_VID_PIDS = [ |
| 417 {'vendorId': 4176, 'productId': 529} // Yubico WinUSB | 434 {'vendorId': 4176, 'productId': 529} // Yubico WinUSB |
| 418 ]; | 435 ]; |
| 419 | 436 |
| 420 /** | 437 /** |
| 421 * @param {function(Array)} cb Enumerate callback | 438 * @param {function(Array)} cb Enumerate callback |
| 422 * @param {GnubbyEnumerationTypes=} opt_type Which type of enumeration to do. | 439 * @param {GnubbyEnumerationTypes=} opt_type Which type of enumeration to do. |
| 423 */ | 440 */ |
| 424 UsbGnubbyDevice.enumerate = function(cb, opt_type) { | 441 UsbGnubbyDevice.enumerate = function(cb, opt_type) { |
| 425 // UsbGnubbyDevices are all non-FIDO devices, so return an empty list if | 442 // UsbGnubbyDevices are all non-FIDO devices, so return an empty list if |
| 426 // FIDO is what's wanted. | 443 // FIDO is what's wanted. |
| 427 if (opt_type == GnubbyEnumerationTypes.FIDO_U2F) { | 444 if (opt_type == GnubbyEnumerationTypes.FIDO_U2F) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 console.warn(UTIL_fmt('failed to claim interface. busy?')); | 548 console.warn(UTIL_fmt('failed to claim interface. busy?')); |
| 532 // Claim failed? Let the callers know and bail out. | 549 // Claim failed? Let the callers know and bail out. |
| 533 chrome.usb.closeDevice(nonNullHandle); | 550 chrome.usb.closeDevice(nonNullHandle); |
| 534 cb(-GnubbyDevice.BUSY); | 551 cb(-GnubbyDevice.BUSY); |
| 535 return; | 552 return; |
| 536 } | 553 } |
| 537 // Restore the enumeratedBy value, if we had it. | 554 // Restore the enumeratedBy value, if we had it. |
| 538 if (enumeratedBy) { | 555 if (enumeratedBy) { |
| 539 dev.enumeratedBy = enumeratedBy; | 556 dev.enumeratedBy = enumeratedBy; |
| 540 } | 557 } |
| 541 var gnubby = new UsbGnubbyDevice(gnubbies, nonNullHandle, which, | 558 var gnubby = new UsbGnubbyDevice( |
| 542 inEndpoint, outEndpoint); | 559 gnubbies, nonNullHandle, which, inEndpoint, outEndpoint); |
| 543 cb(-GnubbyDevice.OK, gnubby); | 560 cb(-GnubbyDevice.OK, gnubby); |
| 544 }); | 561 }); |
| 545 }); | 562 }); |
| 546 } | 563 } |
| 547 | 564 |
| 548 var enumeratedBy = dev.enumeratedBy; | 565 var enumeratedBy = dev.enumeratedBy; |
| 549 | 566 |
| 550 if (UsbGnubbyDevice.runningOnCrOS === undefined) { | 567 if (UsbGnubbyDevice.runningOnCrOS === undefined) { |
| 551 UsbGnubbyDevice.runningOnCrOS = | 568 UsbGnubbyDevice.runningOnCrOS = |
| 552 (window.navigator.appVersion.indexOf('; CrOS ') != -1); | 569 (window.navigator.appVersion.indexOf('; CrOS ') != -1); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 */ | 606 */ |
| 590 UsbGnubbyDevice.register = function(gnubbies) { | 607 UsbGnubbyDevice.register = function(gnubbies) { |
| 591 var USB_GNUBBY_IMPL = { | 608 var USB_GNUBBY_IMPL = { |
| 592 isSharedAccess: false, | 609 isSharedAccess: false, |
| 593 enumerate: UsbGnubbyDevice.enumerate, | 610 enumerate: UsbGnubbyDevice.enumerate, |
| 594 deviceToDeviceId: UsbGnubbyDevice.deviceToDeviceId, | 611 deviceToDeviceId: UsbGnubbyDevice.deviceToDeviceId, |
| 595 open: UsbGnubbyDevice.open | 612 open: UsbGnubbyDevice.open |
| 596 }; | 613 }; |
| 597 gnubbies.registerNamespace(UsbGnubbyDevice.NAMESPACE, USB_GNUBBY_IMPL); | 614 gnubbies.registerNamespace(UsbGnubbyDevice.NAMESPACE, USB_GNUBBY_IMPL); |
| 598 }; | 615 }; |
| OLD | NEW |