OLD | NEW |
---|---|
1 <h1>Accessing Hardware Devices</h1> | 1 <h1>Accessing Hardware Devices</h1> |
2 | 2 |
3 <p> | 3 <p> |
4 This doc shows you how packaged apps can connect to USB devices | 4 This doc shows you how packaged apps can connect to USB devices and read from |
5 and read from and write to a user's serial ports. | 5 and write to a user's serial ports. See also the reference docs for the <a |
6 See also the reference docs for the | 6 href="usb.html">USB API</a> and the <a href="serial.html">Serial API</a>. |
7 <a href="usb.html">USB API</a> | 7 The <a href="bluetooth.html">Bluetooth API</a> is also available; we've |
8 and the | 8 included a link to a Bluetooth sample below. |
9 <a href="serial.html">Serial API</a>. | |
10 The <a href="bluetooth.html">Bluetooth API</a> is also available; | |
11 we've include a link to a Bluetooth sample below. | |
12 </p> | 9 </p> |
13 | 10 |
14 <p class="note"> | 11 <p class="note"> |
15 <b>API Samples: </b> | 12 <b>API Samples: </b> Want to play with the code? Check out the <a |
16 Want to play with the code? | 13 href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/serial" >serial</a>, |
17 Check out the | 14 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo" >servo</a>, |
18 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/serial"> serial</a>, | 15 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">u sb</a>, |
19 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo">s ervo</a>, | 16 and <a |
20 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">usb </a>, | 17 href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/zephyr_ hxm">zephyr_hxm |
21 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/zeph yr_hxm">zephyr_hxm Bluetooth</a> samples. | 18 Bluetooth</a> samples. |
22 </p> | 19 </p> |
23 | 20 |
24 <h2 id="usb">Accessing USB devices</h2> | 21 <h2 id="usb">Accessing USB devices</h2> |
25 | 22 |
26 <p> | 23 <p> |
27 You can use the USB API to send messages to connected devices using only JavaScr ipt code. Some devices are not accessible through this API - see the <a href="#c aveats">Caveats section</a> below for more details. | 24 You can use the USB API to communicate with USB devices using only JavaScript |
Andy
2013/09/05 22:51:36
Replace the first sentence with:
"""
The <a href=
Bei Zhang
2013/09/06 07:37:15
Done.
| |
25 code. Some devices are not accessible through this API - see the <a | |
Andy
2013/09/05 22:51:36
Replace the part after the dash with:
"""
See <a
Bei Zhang
2013/09/06 07:37:15
Done.
| |
26 href="#caveats">Caveats section</a> below for more details. | |
27 </p> | |
28 | |
29 <p> | |
Andy
2013/09/05 22:51:36
Delete this paragraph -- it does not fit into the
Bei Zhang
2013/09/06 07:37:15
Done.
| |
30 Each USB device is assigned with a vendor id/ product id (VID/PID) pair. You | |
31 can use $ref:chrome.usb.getDevices to | |
32 enumerate devices by its VID/PID pair. <br/> | |
33 </p> | |
34 | |
35 <p> | |
Andy
2013/09/05 22:51:36
The anchor text "here" is not helpful, especially
Bei Zhang
2013/09/06 07:37:15
Done.
| |
36 For official USB specifications can be found <a href="http://www.usb.org/home" > | |
37 here</a>. <br/> <a href="http://www.beyondlogic.org/usbnutshell/usb1.shtml">He re</a> | |
38 is a reasonable crash course about it (you may be able to finish it within 24 | |
39 hours). | |
28 </p> | 40 </p> |
29 | 41 |
30 <h3 id="manifest">Manifest requirement</h3> | 42 <h3 id="manifest">Manifest requirement</h3> |
31 | 43 |
32 <p> | 44 <p>The USB API requires a special permission "usb" in the manifest file:</p> |
Andy
2013/09/05 22:51:36
'a special permission "usb"' -> 'the "usb" permiss
Bei Zhang
2013/09/06 07:37:15
Done.
| |
33 The USB API requires a special permission "usb" in the manifest file: | |
34 </p> | |
35 | 45 |
36 <pre> | 46 <pre> |
37 "permissions": [ | 47 "permissions": [ |
38 "usb" | 48 "usb" |
39 ] | 49 ] |
40 </pre> | 50 </pre> |
41 | 51 |
52 <p>In order to prevent finger-printing, you must declare all the device type | |
Andy
2013/09/05 22:51:36
1) What is fingerprinting? It may be worth adding
Bei Zhang
2013/09/06 07:37:15
PID is the id of the device type.
| |
53 you want to access in the manifest file, other wi. For each product id/vendor | |
Andy
2013/09/05 22:51:36
Let's add the information about VID/PID before the
Bei Zhang
2013/09/06 07:37:15
The VIDs are approved by http://www.usb.org/develo
| |
54 id pair of the device, you need to declare it in manifest file under | |
Andy
2013/09/05 22:51:36
Replace the sentence that starts with "For" with:
Bei Zhang
2013/09/06 07:37:15
Done.
| |
55 "usbDevices" permission:</p> | |
56 | |
57 <pre> | |
58 "permissions": [ | |
59 "usbDevices": [ | |
60 { | |
61 "vendorId": 123, | |
62 "productId": 456 | |
63 } | |
64 ] | |
65 ] | |
66 </pre> | |
67 | |
68 <i>Note that only decimal numbers are allowed in JSON format. You cannot pass | |
Andy
2013/09/05 22:51:36
1) Replace the <i> tag with <p class="note">. It's
Bei Zhang
2013/09/06 07:37:15
Done.
| |
69 hexadecimal numbers to these fields.</i> | |
70 | |
42 <h3 id="finding_device">Finding a device</h3> | 71 <h3 id="finding_device">Finding a device</h3> |
43 | 72 |
44 <p> | 73 <p> |
45 Every device in a USB bus is identified | 74 To find a device, use the $ref:chrome.usb.getDevices |
Andy
2013/09/05 22:51:36
"Find" implies that you can search for and discove
Bei Zhang
2013/09/06 07:37:15
Done.
| |
46 by its vendor and product IDs. | 75 method which has two parameters: |
Andy
2013/09/05 22:51:36
Delete "which has two parameters:".
Bei Zhang
2013/09/06 07:37:15
Done.
| |
47 To find a device, | |
48 use the <code>findDevices()</code> method | |
49 which has two parameters: | |
50 </p> | 76 </p> |
51 | 77 |
52 <pre> | 78 <pre> |
53 chrome.usb.findDevices(FindDeviceOptions, callback) | 79 chrome.usb.getDevices(EnumerateDevicesOptions options, GetDevicesCallback callba ck); |
54 </pre> | 80 </pre> |
55 | 81 |
56 <br> | 82 <br/> |
57 | 83 |
58 <table class="simple"> | 84 <table class="simple"> |
59 <tr> | 85 <tr> |
60 <th scope="col"> Parameter (type) </th> | 86 <th scope="col">Parameter (type)</th> |
61 <th scope="col"> Description </th> | 87 <th scope="col">Description</th> |
62 </tr> | 88 </tr> |
63 <tr> | 89 <tr> |
64 <td>FindDeviceOptions (object)</td> | 90 <td>EnumerateDevicesOptions (object)</td> |
65 <td>An object specifying both a <code>vendorId</code> (long) and | 91 <td>An object specifying both a <code>vendorId</code> (long) and <code>produ ctId</code> |
Andy
2013/09/05 22:51:36
Can you only specify one object (device) at a time
Bei Zhang
2013/09/06 07:37:15
As explained above, VID/PID refers to a type of de
| |
66 <code>productId</code> (long) used to find the correct type of device on | 92 (long) used to find the correct type of device on the bus. Your manifest |
67 the bus. Your manifest must declare the<code>usbDevices</code> permission | 93 must declare the<code>usbDevices</code> permission section listing all the |
68 section listing all the <code>vendorId</code> and | 94 <code>vendorId</code> and <code>deviceId</code> pairs your app wants to |
69 <code>deviceId</code> pairs your app wants to access. | 95 access. |
70 </td> | 96 </td> |
71 </tr> | 97 </tr> |
72 <tr> | 98 <tr> |
73 <td>callback (function)</td> | 99 <td>callback (function)</td> |
74 <td>Called when the device scan is finished. | 100 <td>Called when the device scan is finished. The callback will be |
Andy
2013/09/05 22:51:36
I would replace "scan" with "search" or maybe "ide
Bei Zhang
2013/09/06 07:37:15
Done. I understand why it was called "scan". It's
| |
75 The callback will be executed with one parameter, an array of device objec ts | 101 executed with one parameter, an array of <code>Device</code> objects with |
76 with three properties: <code>handle</code>, | 102 three properties: <code>device</code>, <code>vendorId</code>, <code>produc tId</code>. |
Andy
2013/09/05 22:51:36
How is the "device" property used?
Bei Zhang
2013/09/06 07:37:15
Done.
| |
77 <code>vendorId</code>, | 103 If no devices could be found, the array will be empty. |
Andy
2013/09/05 22:51:36
"could be found" -> "are found"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
78 <code>productId</code>. If the optional permissions for this USB device | 104 </td> |
79 were not declared in the extension manifest or the user did not consent | |
80 to the permissions requested, the parameter will be <code>null</code>. | |
81 If no devices could be found, the array will be empty.</td> | |
82 </tr> | 105 </tr> |
83 </table> | 106 </table> |
84 | 107 |
85 <p> | 108 <p>Example:</p> |
86 Example: | |
87 </p> | |
88 | 109 |
89 <pre> | 110 <pre> |
90 var onDeviceFound = function(devices) { | 111 function onDeviceFound(devices) { |
91 _this.devices=devices; | 112 this.devices=devices; |
92 if (devices) { | 113 if (devices) { |
93 if (devices.length > 0) { | 114 if (devices.length > 0) { |
94 console.log("Device(s) found: "+devices.length); | 115 console.log("Device(s) found: "+devices.length); |
95 } else { | 116 } else { |
96 console.log("Device could not be found"); | 117 console.log("Device could not be found"); |
97 } | 118 } |
98 } else { | 119 } else { |
99 console.log("Did not request correct permissions"); | 120 console.log("Did not request correct permissions"); |
100 } | 121 } |
101 }; | 122 }; |
102 | 123 |
103 chrome.usb.findDevices({"vendorId": vendorId, "productId": productId}, onDeviceF ound); | 124 chrome.usb.getDevices({"vendorId": vendorId, "productId": productId}, onDeviceFo und); |
104 </pre> | 125 </pre> |
105 | 126 |
127 <h3 id="usb_open">Opening the device</h3> | |
Andy
2013/09/05 22:51:36
"the" -> "a"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
128 Once the <code>Device</code> objects are returned you can open the device using | |
Andy
2013/09/05 22:51:36
1) Wrap your text in a <p> tag.
2) Add a comma af
Bei Zhang
2013/09/06 07:37:15
Done.
| |
129 $ref:chrome.usb.openDevice to obtain a connection handle. You can only | |
130 communicate with the device using this handle. | |
Andy
2013/09/05 22:51:36
"the device" -> "devices"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
131 | |
132 Not every device can be opened successfully. Devices that are claimed by the OS cannot be opened in Chrome in general. | |
Bei Zhang
2013/09/03 21:25:55
Zel@, This is the key part to explain interfaceId.
Andy
2013/09/05 22:51:36
Start a new paragraph with "Not". I would start t
Bei Zhang
2013/09/06 07:37:15
Done.
| |
133 On Linux other than Chrome OS, once an interface of a device is claimed by the O S, the whole device is locked down, even | |
134 if the other interfaces of the device can be used. On Chrome OS, it is possible to request access to these devices using | |
Andy
2013/09/05 22:51:36
It's not clear what "these devices" refers to. Ma
Bei Zhang
2013/09/06 07:37:15
Done.
| |
135 $ref:chrome.usb.requestAccess method. | |
136 | |
137 To simplify the opending process, you can call $ref:chrome.usb.findDevices | |
Andy
2013/09/05 22:51:36
1) I would start a new paragraph with "To simplify
Bei Zhang
2013/09/06 07:37:15
Done.
| |
138 method which enumerates, requests access and opens the devices in one call. | |
Andy
2013/09/05 22:51:36
1) Add a comma after "method"
2) Add a comma afte
Bei Zhang
2013/09/06 07:37:15
Done.
| |
139 | |
106 <h3 id="usb_transfers">USB transfers and receiving data from a device</h3> | 140 <h3 id="usb_transfers">USB transfers and receiving data from a device</h3> |
107 | 141 |
108 <p> | 142 <p> |
109 USB protocol defines four types of transfers: | 143 USB protocol defines four types of transfers: <a href="#control_transfers">con trol</a>, |
Andy
2013/09/05 22:51:36
"USB" -> "The USB"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
110 <a href="#control_transfers">control</a>, <a href="#bulk_transfers">bulk</a>, <a href="#isochronous_transfers">isochronous</a> and <a href="#interrupt_transfers ">interrupt</a>. | 144 <a href="#bulk_transfers">bulk</a>, <a href="#isochronous_transfers">isochrono us</a> |
111 Theoretically they can occur in both directions:<br> | 145 and <a href="#interrupt_transfers">interrupt</a>. Theoretically they can occur |
Andy
2013/09/05 22:51:36
Delete the sentence that starts with "Theorectical
Bei Zhang
2013/09/06 07:37:15
Done.
| |
112 device-to-host (inbound) and host-to-device (outbound). | 146 in both directions:<br> device-to-host (inbound) and host-to-device |
147 (outbound). | |
113 </p> | 148 </p> |
114 | 149 |
115 <p> | 150 <p> |
116 However, due to the nature of the USB protocol, both inbound and outbound messag es must be initiated by the host (your computer). For inbound (device-to-host) m essages, the host, your JavaScript code, sends a message flagged as "inbound" to the device. The exact contents of the message depends on the device, but usuall y will have some identification of what you are requesting from it. The device t hen responds with the requested data. The device's response is handled by Chrome and delivered asynchronously to the callback you specified in the transfer meth od. | 151 However, due to the nature of the USB protocol, both inbound and outbound messag es must be initiated by the host (your computer). For inbound (device-to-host) m essages, the host, your JavaScript code, sends a message flagged as "inbound" to the device. The exact contents of the message depends on the device, but usuall y will have some identification of what you are requesting from it. The device t hen responds with the requested data. The device's response is handled by Chrome and delivered asynchronously to the callback you specified in the transfer meth od. |
Andy
2013/09/05 22:51:36
1) Start this paragraph with:
"""
Transfers can o
Bei Zhang
2013/09/06 07:37:15
Done.
| |
117 An outbound (host-to-device) message is similar, but the response doesn't contai n data returned from the device.</p> | 152 An outbound (host-to-device) message is similar, but the response doesn't contai n data returned from the device.</p> |
118 | 153 |
119 <p>For each message from the device, | 154 <p>For each message from the device, |
120 the specified callback will receive | 155 the specified callback will receive |
121 an event object with the following properties: | 156 an event object with the following properties: |
122 </p> | 157 </p> |
123 | 158 |
124 <br> | 159 <br> |
125 | 160 |
126 <table class="simple"> | 161 <table class="simple"> |
127 <tr> | 162 <tr> |
128 <th scope="col"> Property </th> | 163 <th scope="col">Property</th> |
129 <th scope="col"> Description </th> | 164 <th scope="col">Description</th> |
130 </tr> | 165 </tr> |
131 <tr> | 166 <tr> |
132 <td>resultCode (integer)</td> | 167 <td>resultCode (integer)</td> |
133 <td>0 is success; other values indicate failure. An error string can be read from | 168 <td>0 is success; other values indicate failure. An error string can be |
134 <code>chrome.extension.lastError</code> when a failure is indicated.</td> | 169 read from <code>chrome.extension.lastError</code> when a failure is |
170 indicated. | |
171 </td> | |
135 </tr> | 172 </tr> |
136 <tr> | 173 <tr> |
137 <td>data (arraybuffer)</td> | 174 <td>data (arraybuffer)</td> |
138 <td>Contains the data sent by the device if transfer was inbound. | 175 <td>Contains the data sent by the device if transfer was inbound.</td> |
Andy
2013/09/05 22:51:36
"transfer" -> "the transfer"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
139 </td> | |
140 </tr> | 176 </tr> |
141 </table> | 177 </table> |
142 | 178 |
143 <p> | 179 <p>Example:</p> |
144 Example: | |
145 </p> | |
146 | 180 |
147 <pre> | 181 <pre> |
148 var onTransferCallback = function(event) { | 182 var onTransferCallback = function(event) { |
149 if (event && event.resultCode === 0 && event.data) { | 183 if (event && event.resultCode === 0 && event.data) { |
150 console.log("got "+event.data.byteLength+" bytes"); | 184 console.log("got "+event.data.byteLength+" bytes"); |
151 } | 185 } |
152 }; | 186 }; |
153 | 187 |
154 chrome.usb.bulkTransfer(device, transferInfo, onTransferCallback); | 188 chrome.usb.bulkTransfer(device, transferInfo, onTransferCallback); |
Andy
2013/09/05 22:51:36
"device" -> "connectionHandle" ?
Bei Zhang
2013/09/06 07:37:15
Done.
| |
155 </pre> | 189 </pre> |
156 | 190 |
157 <h3 id="control_transfers">CONTROL transfers</h3> | 191 <h3 id="control_transfers">CONTROL transfers</h3> |
158 | 192 |
159 <p> | 193 <p>Control transfers are generally used to send or receive configuration or |
160 Control transfers are generally used to send or receive configuration | 194 command parameters to a USB device. It always sends to endpoint 0 and no |
Andy
2013/09/05 22:51:36
1) It's not clear what "It" refers to. Maybe: "The
Bei Zhang
2013/09/06 07:37:15
Done.
| |
161 or command parameters to a USB device. | 195 claimInterface is required. The method is simple and receives three |
162 The method is simple and receives three parameters: | 196 parameters:</p> |
163 </p> | |
164 | 197 |
165 <pre> | 198 <pre> |
166 chrome.usb.controlTransfer(deviceObj, transferInfo, transferCallback) | 199 chrome.usb.controlTransfer(connectionHandle, transferInfo, transferCallback) |
167 </pre> | 200 </pre> |
168 | 201 |
169 <br> | 202 <br> |
170 | 203 |
171 <table class="simple"> | 204 <table class="simple"> |
172 <tr> | 205 <tr> |
173 <th scope="col"> Parameter (types)</th> | 206 <th scope="col">Parameter (types)</th> |
174 <th scope="col"> Description </th> | 207 <th scope="col">Description</th> |
175 </tr> | 208 </tr> |
176 <tr> | 209 <tr> |
177 <td>deviceObj</td> | 210 <td>connectionHandle</td> |
178 <td>Object sent in <code>findDevice()</code> callback.</td> | 211 <td>Object sent in $ref:chrome.usb.openDevice callback. |
Andy
2013/09/05 22:51:36
"sent" -> "received" ?
Bei Zhang
2013/09/06 07:37:15
Done.
| |
212 </td> | |
179 </tr> | 213 </tr> |
180 <tr> | 214 <tr> |
181 <td>transferInfo</td> | 215 <td>transferInfo</td> |
182 <td>Parameter object with values from the table below. | 216 <td>Parameter object with values from the table below. Check your USB |
183 Check your USB device protocol specification for specifics.</td> | 217 device protocol specification for specifics. |
Andy
2013/09/05 22:51:36
"specifics" -> "details"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
218 </td> | |
184 </tr> | 219 </tr> |
185 <tr> | 220 <tr> |
186 <td>transferCallback()</td> | 221 <td>transferCallback()</td> |
187 <td>Invoked when the transfer has completed. | 222 <td>Invoked when the transfer has completed.</td> |
223 </tr> | |
224 </table> | |
225 | |
226 <p> | |
227 Values for | |
228 <code>transferInfo</code> | |
229 object: | |
230 </p> | |
231 | |
232 <table class="simple"> | |
233 <tr> | |
234 <th scope="col">Value</th> | |
235 <th scope="col">Description</th> | |
236 </tr> | |
237 <tr> | |
238 <td>requestType (string)</td> | |
239 <td>"vendor", "standard", "class" or "reserved".</td> | |
240 </tr> | |
241 <tr> | |
242 <td>recipient (string)</td> | |
243 <td>"device", "interface", "endpoint" or "other".</td> | |
244 </tr> | |
245 <tr> | |
246 <td>direction (string)</td> | |
247 <td>"in" or "out". "in" direction is used to notify the device that it | |
Andy
2013/09/05 22:51:36
Start the second sentence with:
"""
The "in" dire
Bei Zhang
2013/09/06 07:37:15
Done.
| |
248 should send information to the host. All communication in a USB bus is | |
Andy
2013/09/05 22:51:36
"in" -> "on"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
249 host-initiated, so use an 'in' transfer to allow a device to send | |
250 information back. | |
251 </td> | |
252 </tr> | |
253 <tr> | |
254 <td>request (integer)</td> | |
255 <td>Defined by your device's protocol.</td> | |
256 </tr> | |
257 <tr> | |
258 <td>value (integer)</td> | |
259 <td>Defined by your device's protocol.</td> | |
260 </tr> | |
261 <tr> | |
262 <td>index (integer)</td> | |
263 <td>Defined by your device's protocol.</td> | |
264 </tr> | |
265 <tr> | |
266 <td>length (integer)</td> | |
267 <td>Only used when direction is "in". Notifies the device that this is | |
268 the amount of data the host is expecting in response. | |
269 </td> | |
270 </tr> | |
271 <tr> | |
272 <td>data (arraybuffer)</td> | |
273 <td>Defined by your device's protocol, required when direction is | |
274 "out". | |
188 </td> | 275 </td> |
189 </tr> | 276 </tr> |
190 </table> | 277 </table> |
191 | 278 |
192 <p> | 279 <p>Example:</p> |
193 Values for <code>transferInfo</code> object: | |
194 </p> | |
195 | |
196 <table class="simple"> | |
197 <tr> | |
198 <th scope="col"> Value </th> | |
199 <th scope="col"> Description </th> | |
200 </tr> | |
201 <tr> | |
202 <td>requestType (string)</td> | |
203 <td>"vendor", "standard", "class" or "reserved".</td> | |
204 </tr> | |
205 <tr> | |
206 <td>recipient (string)</td> | |
207 <td>"device", "interface", "endpoint" or "other".</td> | |
208 </tr> | |
209 <tr> | |
210 <td>direction (string)</td> | |
211 <td>"in" or "out". | |
212 "in" direction is used to notify the device | |
213 that it should send information to the host. | |
214 All communication in a USB bus is host-initiated, | |
215 so use an 'in' transfer to allow a device | |
216 to send information back.</td> | |
217 </tr> | |
218 <tr> | |
219 <td>request (integer)</td> | |
220 <td>Defined by your device's protocol.</td> | |
221 </tr> | |
222 <tr> | |
223 <td>value (integer)</td> | |
224 <td>Defined by your device's protocol.</td> | |
225 </tr> | |
226 <tr> | |
227 <td>index (integer)</td> | |
228 <td>Defined by your device's protocol.</td> | |
229 </tr> | |
230 <tr> | |
231 <td>length (integer)</td> | |
232 <td>Only used when direction is "in". | |
233 Notifies the device that this is the amount | |
234 of data the host is expecting in response.</td> | |
235 </tr> | |
236 <tr> | |
237 <td>data (arraybuffer)</td> | |
238 <td>Defined by your device's protocol, | |
239 required when direction is "out".</td> | |
240 </tr> | |
241 </table> | |
242 | |
243 <p> | |
244 Example: | |
245 </p> | |
246 | 280 |
247 <pre> | 281 <pre> |
248 var transferInfo = { | 282 var transferInfo = { |
249 "requestType": "vendor", | 283 "requestType": "vendor", |
250 "recipient": "device", | 284 "recipient": "device", |
251 "direction": "out", | 285 "direction": "out", |
252 "request": 0x31, | 286 "request": 0x31, |
253 "value": 120, | 287 "value": 120, |
254 "index": 0, | 288 "index": 0, |
255 "data": new Uint8Array([4, 8, 15, 16, 23, 42]).buffer | 289 "data": new Uint8Array([4, 8, 15, 16, 23, 42]).buffer // Note that the ArrayB uffer, not the TypedArray it self is used. |
Andy
2013/09/05 22:51:36
1) "it self" -> "itself"
2) I don't understand th
Bei Zhang
2013/09/06 07:37:15
Means you cannot pass `new Uint8Array(...)` to it.
| |
256 }; | 290 }; |
257 chrome.usb.controlTransfer(deviceObj, transferInfo, optionalCallback); | 291 chrome.usb.controlTransfer(connectionHandle, transferInfo, optionalCallback); |
258 </pre> | 292 </pre> |
259 | 293 |
260 <h3 id="isochronous_transfers">ISOCHRONOUS transfers</h3> | 294 <h3 id="isochronous_transfers">ISOCHRONOUS transfers</h3> |
261 | 295 |
262 <p> | 296 <p>Isochronous transfers is the most complex type of USB transfers. They are |
Andy
2013/09/05 22:51:36
"is" -> "are"
Bei Zhang
2013/09/06 07:37:15
Done. Not sure about this though.
| |
263 Isochronous transfers is the most complex type of USB transfers. They are common ly used for streams of data, like video and sound. To initiate an isochronous tr ansfer (either inbound or outbound), you must use: | 297 commonly used for streams of data, like video and sound. To initiate an |
264 </p> | 298 isochronous transfer (either inbound or outbound), you must use:</p> |
Andy
2013/09/05 22:51:36
"you must use" -> "you must use the isochronousTra
Bei Zhang
2013/09/06 07:37:15
Done.
| |
265 | 299 |
266 <pre> | 300 <pre> |
267 chrome.usb.isochronousTransfer(deviceObj, isochronousTransferInfo, transferCallb ack) | 301 chrome.usb.isochronousTransfer(connectionHandle, isochronousTransferInfo, transf erCallback) |
268 </pre> | 302 </pre> |
269 | 303 |
270 <br> | 304 <br> |
271 | 305 |
272 <table class="simple"> | 306 <table class="simple"> |
273 <tr> | 307 <tr> |
274 <th scope="col"> Parameter </th> | 308 <th scope="col">Parameter</th> |
275 <th scope="col"> Description </th> | 309 <th scope="col">Description</th> |
276 </tr> | 310 </tr> |
277 <tr> | 311 <tr> |
278 <td>deviceObj</td> | 312 <td>connectionHandle</td> |
279 <td>Object sent on <code>findDevice()</code> callback.</td> | 313 <td>Object sent in $ref:chrome.usb.openDevice callback. |
Andy
2013/09/05 22:51:36
"sent" -> "received" ?
Bei Zhang
2013/09/06 07:37:15
Done.
| |
314 </td> | |
280 </tr> | 315 </tr> |
281 <tr> | 316 <tr> |
282 <td>isochronousTransferInfo</td> | 317 <td>isochronousTransferInfo</td> |
283 <td>Parameter object with the values in the table below.</td> | 318 <td>Parameter object with the values in the table below.</td> |
284 </tr> | 319 </tr> |
285 <tr> | 320 <tr> |
286 <td>transferCallback()</td> | 321 <td>transferCallback()</td> |
287 <td>Invoked when the transfer has completed. | 322 <td>Invoked when the transfer has completed.</td> |
288 </td> | |
289 </tr> | 323 </tr> |
290 </table> | 324 </table> |
291 | 325 |
292 <p> | 326 <p> |
293 Values for <code>isochronousTransferInfo</code> object: | 327 Values for |
328 <code>isochronousTransferInfo</code> | |
329 object: | |
294 </p> | 330 </p> |
295 | 331 |
296 <table class="simple"> | 332 <table class="simple"> |
297 <tr> | 333 <tr> |
298 <th scope="col"> Value </th> | 334 <th scope="col">Value</th> |
299 <th scope="col"> Description </th> | 335 <th scope="col">Description</th> |
300 </tr> | 336 </tr> |
301 <tr> | 337 <tr> |
302 <td>transferInfo (object)</td> | 338 <td>transferInfo (object)</td> |
303 <td>An object with the following attributes:<br> | 339 <td>An object with the following attributes:<br> <b>direction |
304 <b>direction (string): </b>"in" or "out".<br> | 340 (string): </b>"in" or "out".<br> <b>endpoint (integer): </b>defined by |
305 <b>endpoint (integer): </b>defined by your device. Usually can be found by looking at an USB instrospection tool, like <code>lsusb -v</code><br> | 341 your device. Usually can be found by looking at an USB instrospection |
306 <b>length (integer): </b>only used when direction is "in". | 342 tool, like <code>lsusb -v</code><br> <b>length (integer): </b>only |
307 Notifies the device that this is the amount | 343 used when direction is "in". Notifies the device that this is the amount |
308 of data the host is expecting in response. Should be AT LEAST <code>packet s * packetLength</code><br> | 344 of data the host is expecting in response. Should be AT LEAST <code>packet s |
309 <b>data (arraybuffer): </b>defined by your device's protocol; | 345 * packetLength</code><br> <b>data (arraybuffer): </b>defined by your |
310 only used when direction is "out". | 346 device's protocol; only used when direction is "out". |
311 </td> | 347 </td> |
312 </tr> | 348 </tr> |
313 <tr> | 349 <tr> |
314 <td>packets (integer)</td> | 350 <td>packets (integer)</td> |
315 <td>Total number of packets expected in this transfer.</td> | 351 <td>Total number of packets expected in this transfer.</td> |
316 </tr> | 352 </tr> |
317 <tr> | 353 <tr> |
318 <td>packetLength (integer)</td> | 354 <td>packetLength (integer)</td> |
319 <td>Expected length of each packet in this transfer.</td> | 355 <td>Expected length of each packet in this transfer.</td> |
320 </tr> | 356 </tr> |
321 </table> | 357 </table> |
322 | 358 |
323 <p> | 359 <p>Example:</p> |
324 Example: | |
325 </p> | |
326 | 360 |
327 <pre> | 361 <pre> |
328 var transferInfo = { | 362 var transferInfo = { |
329 "direction": "in", | 363 "direction": "in", |
330 "endpoint": 1, | 364 "endpoint": 1, |
331 "length": 2560 | 365 "length": 2560 |
332 }; | 366 }; |
333 var isoTransferInfo = { | 367 var isoTransferInfo = { |
334 "transferInfo": transferInfo, | 368 "transferInfo": transferInfo, |
335 "packets": 20, | 369 "packets": 20, |
336 "packetLength": 128 | 370 "packetLength": 128 |
337 }; | 371 }; |
338 chrome.usb.isochronousTransfer(deviceObj, isoTransferInfo, optionalCallback); | 372 chrome.usb.isochronousTransfer(connectionHandle, isoTransferInfo, optionalCallba ck); |
339 </pre> | 373 </pre> |
340 | 374 |
341 <p> | 375 <p> |
342 <b>Notes:</b> One isochronous transfer will contain <code>isoTransferInfo.pack ets</code> packets of <code>isoTransferInfo.packetLength</code> bytes. If it is an inbound transfer (your code requested data from the device), the <code>data</ code> field in the onUsbEvent will be an ArrayBuffer of size <code>transferInfo. length</code>. It is your duty to walk through this ArrayBuffer and extract the different packets, each starting at a multiple of <code>isoTransferInfo.packetLe ngth</code> bytes. If you are expecting a stream of data from the device, rememb er that you will have to send one "inbound" transfer for each transfer you expec t back. USB devices don't send transfers to the bus unless the host explicitly r equests them through "inbound" transfers. | 376 <b>Notes:</b> One isochronous transfer will contain |
377 <code>isoTransferInfo.packets</code> | |
378 packets of | |
379 <code>isoTransferInfo.packetLength</code> | |
380 bytes. If it is an inbound transfer (your code requested data from the | |
381 device), the | |
382 <code>data</code> | |
383 field in the onUsbEvent will be an ArrayBuffer of size | |
384 <code>transferInfo.length</code> | |
385 . It is your duty to walk through this ArrayBuffer and extract the different | |
386 packets, each starting at a multiple of | |
387 <code>isoTransferInfo.packetLength</code> | |
388 bytes. If you are expecting a stream of data from the device, remember that | |
389 you will have to send one "inbound" transfer for each transfer you expect | |
390 back. USB devices don't send transfers to the bus unless the host explicitly | |
Andy
2013/09/05 22:51:36
"the bus" -> "the USB bus"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
391 requests them through "inbound" transfers. | |
343 </p> | 392 </p> |
344 | 393 |
345 <h3 id="bulk_transfers">BULK transfers</h3> | 394 <h3 id="bulk_transfers">BULK transfers</h3> |
346 | 395 |
347 <p> | 396 <p>Bulk transfer is an USB transfer type commonly used to transfer a large |
Andy
2013/09/05 22:51:36
Start this paragraph with:
"""
Bulk transfers are
Bei Zhang
2013/09/06 07:37:15
Done.
| |
348 Bulk transfer is an USB transfer type commonly used | 397 amount of data in a reliable way. The method has three parameters:</p> |
Andy
2013/09/05 22:51:36
"The method" -> "The bulkTransfer() method"
When
Bei Zhang
2013/09/06 07:37:15
Done.
| |
349 to transfer a large amount of data in a reliable way. | |
350 The method has three parameters: | |
351 </p> | |
352 | 398 |
353 <pre> | 399 <pre> |
354 chrome.usb.bulkTransfer(deviceObj, transferInfo, transferCallback) | 400 chrome.usb.bulkTransfer(connectionHandle, transferInfo, transferCallback) |
355 </pre> | 401 </pre> |
356 | 402 |
357 <br> | 403 <br> |
358 | 404 |
359 <table class="simple"> | 405 <table class="simple"> |
360 <tr> | 406 <tr> |
361 <th scope="col"> Parameter </th> | 407 <th scope="col">Parameter</th> |
362 <th scope="col"> Description </th> | 408 <th scope="col">Description</th> |
363 </tr> | 409 </tr> |
364 <tr> | 410 <tr> |
365 <td>deviceObj</td> | 411 <td>connectionHandle</td> |
366 <td>Object sent on <code>findDevice()</code> callback.</td> | 412 <td>Object sent in $ref:chrome.usb.openDevice callback. |
Andy
2013/09/05 22:51:36
"sent" -> "received" ?
Bei Zhang
2013/09/06 07:37:15
Done.
| |
413 </td> | |
367 </tr> | 414 </tr> |
368 <tr> | 415 <tr> |
369 <td>transferInfo</td> | 416 <td>transferInfo</td> |
370 <td>Parameter object with the values in the table below.</td> | 417 <td>Parameter object with the values in the table below.</td> |
371 </tr> | 418 </tr> |
372 <tr> | 419 <tr> |
373 <td>transferCallback</td> | 420 <td>transferCallback</td> |
374 <td>Invoked when the transfer has completed. | 421 <td>Invoked when the transfer has completed.</td> |
375 </td> | |
376 </tr> | 422 </tr> |
377 </table> | 423 </table> |
378 | 424 |
379 <p> | 425 <p> |
380 Values for <code>transferInfo</code> object: | 426 Values for |
427 <code>transferInfo</code> | |
428 object: | |
381 </p> | 429 </p> |
382 | 430 |
383 <table class="simple"> | 431 <table class="simple"> |
384 <tr> | 432 <tr> |
385 <th scope="col"> Value </th> | 433 <th scope="col">Value</th> |
386 <th scope="col"> Description </th> | 434 <th scope="col">Description</th> |
387 </tr> | 435 </tr> |
388 <tr> | 436 <tr> |
389 <td>direction (string)</td> | 437 <td>direction (string)</td> |
390 <td>"in" or "out".</td> | 438 <td>"in" or "out".</td> |
391 </tr> | 439 </tr> |
392 <tr> | 440 <tr> |
393 <td>endpoint (integer)</td> | 441 <td>endpoint (integer)</td> |
394 <td>Defined by your device's protocol.</td> | 442 <td>Defined by your device's protocol.</td> |
395 </tr> | 443 </tr> |
396 <tr> | 444 <tr> |
397 <td>length (integer)</td> | 445 <td>length (integer)</td> |
398 <td>Only used when direction is "in". | 446 <td>Only used when direction is "in". Notifies the device that this is |
399 Notifies the device that this is the amount | 447 the amount of data the host is expecting in response. |
400 of data the host is expecting in response.</td> | 448 </td> |
401 </tr> | 449 </tr> |
402 <tr> | 450 <tr> |
403 <td>data (ArrayBuffer)</td> | 451 <td>data (ArrayBuffer)</td> |
404 <td>Defined by your device's protocol; | 452 <td>Defined by your device's protocol; only used when direction is |
405 only used when direction is "out".</td> | 453 "out". |
454 </td> | |
406 </tr> | 455 </tr> |
407 </table> | 456 </table> |
408 | 457 |
409 <p> | 458 <p>Example:</p> |
410 Example: | |
411 </p> | |
412 | 459 |
413 <pre> | 460 <pre> |
414 var transferInfo = { | 461 var transferInfo = { |
415 "direction": "out", | 462 "direction": "out", |
416 "endpoint": 1, | 463 "endpoint": 1, |
417 "data": new Uint8Array([4, 8, 15, 16, 23, 42]).buffer | 464 "data": new Uint8Array([4, 8, 15, 16, 23, 42]).buffer |
418 }; | 465 }; |
419 </pre> | 466 </pre> |
420 | 467 |
421 <h3 id="interrupt_transfers">INTERRUPT transfers</h3> | 468 <h3 id="interrupt_transfers">INTERRUPT transfers</h3> |
422 | 469 |
423 <p> | 470 <p>Interrupt transfers are used to send important notifications. Since all |
Andy
2013/09/05 22:51:36
An example of an "important notification" would be
| |
424 Interrupt transfers are used to send important notifications. | 471 USB communication is initiated by the host, host code usually polls the device |
425 Since all USB communication is initiated by the host, | 472 periodically, sending interrupt IN transfers that will make the device send |
426 host code usually polls the device periodically, | 473 data back if there is anything in the interrupt queue. The method has three |
Andy
2013/09/05 22:51:36
"The method" -> "The interruptTransfer() method"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
427 sending interrupt IN transfers that will make the device send data back | 474 parameters:</p> |
428 if there is anything in the interrupt queue. | |
429 The method has three parameters: | |
430 </p> | |
431 | 475 |
432 <pre> | 476 <pre> |
433 chrome.usb.interruptTransfer(deviceObj, transferInfo, transferCallback) | 477 chrome.usb.interruptTransfer(connectionHandle, transferInfo, transferCallback) |
434 </pre> | 478 </pre> |
435 | 479 |
436 <br> | 480 <br> |
437 | 481 |
438 <table class="simple"> | 482 <table class="simple"> |
439 <tr> | 483 <tr> |
440 <th scope="col"> Parameter </th> | 484 <th scope="col">Parameter</th> |
441 <th scope="col"> Description </th> | 485 <th scope="col">Description</th> |
442 </tr> | 486 </tr> |
443 <tr> | 487 <tr> |
444 <td>deviceObj</td> | 488 <td>connectionHandle</td> |
445 <td>Object sent on <code>findDevice()</code> callback.</td> | 489 <td>Object sent in $ref:chrome.usb.openDevice callback. |
490 </td> | |
446 </tr> | 491 </tr> |
447 <tr> | 492 <tr> |
448 <td>transferInfo</td> | 493 <td>transferInfo</td> |
449 <td>Parameter object with the values in the table below.</td> | 494 <td>Parameter object with the values in the table below.</td> |
450 </tr> | 495 </tr> |
451 <tr> | 496 <tr> |
452 <td>transferCallback</td> | 497 <td>transferCallback</td> |
453 <td>Invoked when the transfer has completed. | 498 <td>Invoked when the transfer has completed. Notice that this callback |
454 Notice that this callback doesn't contain the device's response. | 499 doesn't contain the device's response. It's just to notify your code that |
Andy
2013/09/05 22:51:36
"It's just" -> "The purpose of the callback is sim
Bei Zhang
2013/09/06 07:37:15
Done.
| |
455 It's just to notify your code that the asynchronous transfer request | 500 the asynchronous transfer request has been processed and you can go ahead. |
Andy
2013/09/05 22:51:36
Go ahead with what? Maybe end the sentence after
Bei Zhang
2013/09/06 07:37:15
Done.
| |
456 has been processed and you can go ahead. | |
457 The device's response, if any, will always be sent through | |
458 the <code>onEvent()</code> callback set on <code>findDevice()</code>. | |
459 </td> | 501 </td> |
460 </tr> | 502 </tr> |
461 </table> | 503 </table> |
462 | 504 |
463 <p> | 505 <p> |
464 Values for <code>transferInfo</code> object: | 506 Values for |
507 <code>transferInfo</code> | |
508 object: | |
465 </p> | 509 </p> |
466 | 510 |
467 <table class="simple"> | 511 <table class="simple"> |
468 <tr> | 512 <tr> |
469 <th scope="col"> Value </th> | 513 <th scope="col">Value</th> |
470 <th scope="col"> Description </th> | 514 <th scope="col">Description</th> |
471 </tr> | 515 </tr> |
472 <tr> | 516 <tr> |
473 <td>direction (string)</td> | 517 <td>direction (string)</td> |
474 <td>"in" or "out".</td> | 518 <td>"in" or "out".</td> |
475 </tr> | 519 </tr> |
476 <tr> | 520 <tr> |
477 <td>endpoint (integer)</td> | 521 <td>endpoint (integer)</td> |
478 <td>Defined by your device's protocol.</td> | 522 <td>Defined by your device's protocol.</td> |
479 </tr> | 523 </tr> |
480 <tr> | 524 <tr> |
481 <td>length (integer)</td> | 525 <td>length (integer)</td> |
482 <td>Only used when direction is "in". | 526 <td>Only used when direction is "in". Notifies the device that this is |
483 Notifies the device that this is the amount | 527 the amount of data the host is expecting in response. |
484 of data the host is expecting in response.</td> | 528 </td> |
485 </tr> | 529 </tr> |
486 <tr> | 530 <tr> |
487 <td>data (ArrayBuffer)</td> | 531 <td>data (ArrayBuffer)</td> |
488 <td>Defined by your device's protocol; | 532 <td>Defined by your device's protocol; only used when direction is |
489 only used when direction is "out".</td> | 533 "out". |
490 </tr> | 534 </td> |
535 </tr> | |
536 </table> | |
491 | 537 |
492 <p> | 538 <p>Example:</p> |
493 Example: | |
494 </p> | |
495 | 539 |
496 <pre> | 540 <pre> |
497 var transferInfo = { | 541 var transferInfo = { |
498 "direction": "in", | 542 "direction": "in", |
499 "endpoint": 1, | 543 "endpoint": 1, |
500 "length": 2 | 544 "length": 2 |
501 }; | 545 }; |
502 chrome.usb.interruptTransfer(deviceObj, transferInfo, optionalCallback); | 546 chrome.usb.interruptTransfer(connectionHandle, transferInfo, optionalCallback); |
503 </pre> | 547 </pre> |
504 | 548 |
505 <h3 id="caveats">Caveats</h3> | 549 <h3 id="caveats">Caveats</h3> |
506 | 550 |
507 <p> | 551 <p> |
508 On most Linux systems, USB devices are mapped with read-only permissions by defa ult. To access it through this API, your user will need to have write access too . A simple solution is to set a udev rule. Create a file <code>/etc/udev/rules.d /50-yourdevicename.rules</code> | 552 On most Linux systems, USB devices are mapped with read-only permissions by |
509 with the following content: | 553 default. To access it through this API, your user will need to have write |
Andy
2013/09/05 22:51:36
What does "it" refer to? Try: "To access devices
Bei Zhang
2013/09/06 07:37:15
Done.
| |
554 access too. A simple solution is to set a udev rule. Create a file | |
Andy
2013/09/05 22:51:36
Do users need to set this rule? Or can an app do
Bei Zhang
2013/09/06 07:37:15
Done.
| |
555 <code>/etc/udev/rules.d/50-yourdevicename.rules</code> | |
556 with the following content: | |
510 </p> | 557 </p> |
511 | 558 |
512 <pre> | 559 <pre> |
513 SUBSYSTEM=="usb", ATTR{idVendor}=="[yourdevicevendor]", MODE="0664", GROUP="plug dev" | 560 SUBSYSTEM=="usb", ATTR{idVendor}=="[yourdevicevendor]", MODE="0664", GROUP="plug dev" |
514 </pre> | 561 </pre> |
515 | 562 |
516 <p> | 563 <p> |
517 Then, just restart the udev daemon: <code>service udev restart</code>. You can check if the permissions are correctly set by: | 564 Then, just restart the udev daemon: <code>service udev restart</code>. You can |
Andy
2013/09/05 22:51:36
Replace the last sentence with:
"""
You can check
Bei Zhang
2013/09/06 07:37:15
Done.
| |
518 <ul> | 565 check if the permissions are correctly set by: |
519 <li>Find the bus and device numbers in <code>lsusb</code></li> | |
520 <li><code>ls -al /dev/bus/usb/[bus]/[device]</code>. This file should be own ed by group "plugdev" and have group write permissions.</li> | |
521 </ul> | |
522 </p> | 566 </p> |
523 | 567 |
524 <p> | 568 <ul> |
525 Not all devices can be accessed through this API. In general, devices are not accessible either because the Operating System's kernel or a native driver holds them off from user space code. Some examples are devices with HID profiles on O SX systems and USB pen drives. | 569 <li>Find the bus and device numbers in <code>lsusb</code></li> |
Andy
2013/09/05 22:51:36
Replace the first step with:
"""
Run <code>lsusb<
Bei Zhang
2013/09/06 07:37:15
Done.
| |
526 </p> | 570 <li><code>ls -al /dev/bus/usb/[bus]/[device]</code>. This file should be owned |
Andy
2013/09/05 22:51:36
Start the second step with:
"""
Run <code>ls...
"
Bei Zhang
2013/09/06 07:37:15
Done.
| |
571 by group "plugdev" and have group write permissions. | |
572 </li> | |
573 </ul> | |
574 | |
575 <p>On Chrome OS, simply call $ref:chrome.usb.requestAccess.</p> | |
576 | |
577 <p>Not all devices can be accessed through this API. In general, devices | |
578 are not accessible either because the Operating System's kernel or a native | |
579 driver holds them off from user space code. Some examples are devices with | |
580 HID profiles on OSX systems and USB pen drives.</p> | |
527 | 581 |
528 <h2 id="serial">Accessing serial devices</h2> | 582 <h2 id="serial">Accessing serial devices</h2> |
529 | 583 |
530 <p> | 584 <p> |
531 You can use the serial API to read | 585 You can use the serial API to read |
532 and write from a serial device. | 586 and write from a serial device. |
533 </p> | 587 </p> |
534 | 588 |
535 <h3 id="requirement">Manifest requirement</h3> | 589 <h3 id="requirement">Manifest requirement</h3> |
536 | 590 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
680 | 734 |
681 <p> | 735 <p> |
682 You can flush your serial port buffer by issuing the flush command: | 736 You can flush your serial port buffer by issuing the flush command: |
683 </p> | 737 </p> |
684 | 738 |
685 <pre> | 739 <pre> |
686 chrome.serial.flush(connectionId, onFlush); | 740 chrome.serial.flush(connectionId, onFlush); |
687 </pre> | 741 </pre> |
688 | 742 |
689 <p class="backtotop"><a href="#top">Back to top</a></p> | 743 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |