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 Low level usb cruft to talk gnubby. | 6 * @fileoverview Low level usb cruft to talk gnubby. |
7 */ | 7 */ |
8 | 8 |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... | |
31 /** | 31 /** |
32 * Sets usbGnubby's Gnubbies singleton. | 32 * Sets usbGnubby's Gnubbies singleton. |
33 * @param {Gnubbies} gnubbies Gnubbies singleton instance | 33 * @param {Gnubbies} gnubbies Gnubbies singleton instance |
34 */ | 34 */ |
35 usbGnubby.setGnubbies = function(gnubbies) { | 35 usbGnubby.setGnubbies = function(gnubbies) { |
36 /** @private {Gnubbies} */ | 36 /** @private {Gnubbies} */ |
37 usbGnubby.gnubbies_ = gnubbies; | 37 usbGnubby.gnubbies_ = gnubbies; |
38 }; | 38 }; |
39 | 39 |
40 /** | 40 /** |
41 * @param {function(number, Array.<llGnubbyDeviceId>)} cb Called back with the | 41 * @param {function(number, Array.<number>)} cb Called back with the |
juanlang (chromium.org)
2014/05/30 20:38:08
This looks like a mistake crept in. I'll have a lo
| |
42 * result of enumerating. | 42 * result of enumerating. |
43 */ | 43 */ |
44 usbGnubby.prototype.enumerate = function(cb) { | 44 usbGnubby.prototype.enumerate = function(cb) { |
45 if (!cb) cb = usbGnubby.defaultCallback; | 45 if (!cb) cb = usbGnubby.defaultCallback; |
46 if (this.closed) { | 46 if (this.closed) { |
47 cb(-llGnubby.GONE); | 47 cb(-llGnubby.NODEVICE); |
48 return; | 48 return; |
49 } | 49 } |
50 if (!usbGnubby.gnubbies_) { | 50 if (!usbGnubby.gnubbies_) { |
51 cb(-llGnubby.NODEVICE); | 51 cb(-llGnubby.NODEVICE); |
52 return; | 52 return; |
53 } | 53 } |
54 | 54 |
55 usbGnubby.gnubbies_.enumerate(cb); | 55 usbGnubby.gnubbies_.enumerate(cb); |
56 }; | 56 }; |
57 | 57 |
58 /** | 58 /** |
59 * Opens the gnubby with the given index, or the first found gnubby if no | 59 * Opens the gnubby with the given index, or the first found gnubby if no |
60 * index is specified. | 60 * index is specified. |
61 * @param {llGnubbyDeviceId|undefined} opt_which The device to open. | 61 * @param {llGnubbyDeviceId} which The device to open. If null, the first |
62 * gnubby found is opened. | |
62 * @param {function(number)|undefined} opt_cb Called with result of opening the | 63 * @param {function(number)|undefined} opt_cb Called with result of opening the |
63 * gnubby. | 64 * gnubby. |
64 */ | 65 */ |
65 usbGnubby.prototype.open = function(opt_which, opt_cb) { | 66 usbGnubby.prototype.open = function(which, opt_cb) { |
66 var cb = opt_cb ? opt_cb : usbGnubby.defaultCallback; | 67 var cb = opt_cb ? opt_cb : usbGnubby.defaultCallback; |
67 if (this.closed) { | 68 if (this.closed) { |
68 cb(-llGnubby.GONE); | 69 cb(-llGnubby.NODEVICE); |
69 return; | 70 return; |
70 } | 71 } |
71 this.closingWhenIdle = false; | 72 this.closingWhenIdle = false; |
72 | 73 |
73 if (document.location.href.indexOf('_generated_') == -1) { | 74 if (document.location.href.indexOf('_generated_') == -1) { |
74 // Not background page. | 75 // Not background page. |
75 // Pick more random cid to tell things apart on the usb bus. | 76 // Pick more random cid to tell things apart on the usb bus. |
76 var rnd = UTIL_getRandom(2); | 77 var rnd = UTIL_getRandom(2); |
77 this.cid ^= (rnd[0] << 16) | (rnd[1] << 8); | 78 this.cid ^= (rnd[0] << 16) | (rnd[1] << 8); |
78 } | 79 } |
79 | 80 |
80 var self = this; | 81 var self = this; |
81 function addSelfAsClient(which) { | 82 |
83 function setCid(which) { | |
82 self.cid &= 0x00ffffff; | 84 self.cid &= 0x00ffffff; |
83 self.cid |= ((which.device + 1) << 24); // For debugging. | 85 self.cid |= ((which.device + 1) << 24); // For debugging. |
86 } | |
84 | 87 |
88 var enumerateRetriesRemaining = 3; | |
89 function enumerated(rc, devs) { | |
90 if (!devs.length) | |
91 rc = -llGnubby.NODEVICE; | |
92 if (rc) { | |
93 cb(rc); | |
94 return; | |
95 } | |
96 which = devs[0]; | |
97 setCid(which); | |
85 usbGnubby.gnubbies_.addClient(which, self, function(rc, device) { | 98 usbGnubby.gnubbies_.addClient(which, self, function(rc, device) { |
99 if (rc == -llGnubby.NODEVICE && enumerateRetriesRemaining-- > 0) { | |
100 // We were trying to open the first device, but now it's not there? | |
101 // Do over. | |
102 usbGnubby.gnubbies_.enumerate(enumerated); | |
103 return; | |
104 } | |
86 self.dev = device; | 105 self.dev = device; |
87 cb(rc); | 106 cb(rc); |
88 }); | 107 }); |
89 } | 108 } |
90 | 109 |
91 if (!usbGnubby.gnubbies_) { | 110 if (which) { |
92 cb(-llGnubby.NODEVICE); | 111 setCid(which); |
93 return; | 112 usbGnubby.gnubbies_.addClient(which, self, function(rc, device) { |
94 } | 113 self.dev = device; |
95 if (opt_which) { | 114 cb(rc); |
96 addSelfAsClient(opt_which); | 115 }); |
97 } else { | 116 } else { |
98 usbGnubby.gnubbies_.enumerate(function(rc, devs) { | 117 usbGnubby.gnubbies_.enumerate(enumerated); |
99 if (rc || !devs.length) { | |
100 cb(-llGnubby.NODEVICE); | |
101 return; | |
102 } | |
103 addSelfAsClient(devs[0]); | |
104 }); | |
105 } | 118 } |
106 }; | 119 }; |
107 | 120 |
108 /** | 121 /** |
109 * @return {boolean} Whether this gnubby has any command outstanding. | 122 * @return {boolean} Whether this gnubby has any command outstanding. |
110 * @private | 123 * @private |
111 */ | 124 */ |
112 usbGnubby.prototype.inUse_ = function() { | 125 usbGnubby.prototype.inUse_ = function() { |
113 return this.commandPending; | 126 return this.commandPending; |
114 }; | 127 }; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 }; | 223 }; |
211 | 224 |
212 /** Poll from rxframes[]. | 225 /** Poll from rxframes[]. |
213 * @param {number} cmd Command | 226 * @param {number} cmd Command |
214 * @param {number} timeout timeout in seconds. | 227 * @param {number} timeout timeout in seconds. |
215 * @param {?function(...)} cb Callback | 228 * @param {?function(...)} cb Callback |
216 * @private | 229 * @private |
217 */ | 230 */ |
218 usbGnubby.prototype.read_ = function(cmd, timeout, cb) { | 231 usbGnubby.prototype.read_ = function(cmd, timeout, cb) { |
219 if (this.closed) { cb(-llGnubby.GONE); return; } | 232 if (this.closed) { cb(-llGnubby.GONE); return; } |
220 if (!this.dev) { cb(-llGnubby.NODEVICE); return; } | 233 if (!this.dev) { cb(-llGnubby.GONE); return; } |
221 | 234 |
222 var tid = null; // timeout timer id. | 235 var tid = null; // timeout timer id. |
223 var callback = cb; | 236 var callback = cb; |
224 var self = this; | 237 var self = this; |
225 | 238 |
226 var msg = null; | 239 var msg = null; |
227 var seqno = 0; | 240 var seqno = 0; |
228 var count = 0; | 241 var count = 0; |
229 | 242 |
230 /** | 243 /** |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
429 busyWait.clearTimeout(); | 442 busyWait.clearTimeout(); |
430 cb(rc, rc_data); | 443 cb(rc, rc_data); |
431 } | 444 } |
432 } | 445 } |
433 | 446 |
434 retryBusy(-llGnubby.BUSY, undefined); // Start work. | 447 retryBusy(-llGnubby.BUSY, undefined); // Start work. |
435 }; | 448 }; |
436 | 449 |
437 /** Default callback for commands. Simply logs to console. | 450 /** Default callback for commands. Simply logs to console. |
438 * @param {number} rc Result status code | 451 * @param {number} rc Result status code |
439 * @param {*} data Result data | 452 * @param {(ArrayBuffer|Uint8Array|Array.<number>|null)} data Result data |
440 */ | 453 */ |
441 usbGnubby.defaultCallback = function(rc, data) { | 454 usbGnubby.defaultCallback = function(rc, data) { |
442 var msg = 'defaultCallback(' + rc; | 455 var msg = 'defaultCallback(' + rc; |
443 if (data) { | 456 if (data) { |
444 if (typeof data == 'string') msg += ', ' + data; | 457 if (typeof data == 'string') msg += ', ' + data; |
445 else msg += ', ' + UTIL_BytesToHex(new Uint8Array(data)); | 458 else msg += ', ' + UTIL_BytesToHex(new Uint8Array(data)); |
446 } | 459 } |
447 msg += ')'; | 460 msg += ')'; |
448 console.log(UTIL_fmt(msg)); | 461 console.log(UTIL_fmt(msg)); |
449 }; | 462 }; |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 } | 685 } |
673 } | 686 } |
674 // Warn on errors other than waiting for touch, wrong data, and | 687 // Warn on errors other than waiting for touch, wrong data, and |
675 // unrecognized command. | 688 // unrecognized command. |
676 if (rc != 0x6985 && rc != 0x6a80 && rc != 0x6d00) { | 689 if (rc != 0x6985 && rc != 0x6a80 && rc != 0x6d00) { |
677 console.warn(UTIL_fmt('apduReply_ fail: ' + rc.toString(16))); | 690 console.warn(UTIL_fmt('apduReply_ fail: ' + rc.toString(16))); |
678 } | 691 } |
679 cb(rc); | 692 cb(rc); |
680 }); | 693 }); |
681 }; | 694 }; |
OLD | NEW |