Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/common/extensions/docs/server2/templates/articles/app_hardware.html

Issue 10832042: Extensions Docs Server: Doc conversion script (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: everything but svn stuff Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <h1>Accessing Hardware Devices</h1>
2
3
4 <p>
5 This doc shows you how packaged apps can connect to USB devices
6 and read from and write to a user's serial ports.
7 See also the reference docs for the
8 <a href="experimental.usb.html">USB API</a>
9 and the
10 <a href="experimental.serial.html">Serial API</a>.
11 The <a href="experimental.bluetooth.html">Bluetooth API</a> has just landed and
12 we'll write more on this soon.
13 We've included a link to an early sample below.
14 </p>
15
16 <p class="note">
17 <b>API Samples: </b>
18 Want to play with the code?
19 Check out the
20 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/serial"> serial</a>,
21 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo">s ervo</a>,
22 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">usb </a>,
23 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/blue tooth-demo">bluetooth-demo</a> samples.
24 </p>
25
26 <h2 id="usb">Accessing USB devices</h2>
27
28 <p>
29 You can use the USB API to send messages to any connected device.
30 </p>
31
32 <h3>Manifest requirement</h3>
33
34 <p>
35 You must add the "usb" permission
36 to the manifest file:
37 </p>
38
39 <pre>
40 "permissions": [
41 "app.window",
42 "experimental",
43 "usb"
44 ]
45 </pre>
46
47 <h3>Finding a device</h3>
48
49 <p>
50 Every device in a USB bus is identified
51 by its vendor and product IDs.
52 To find a device,
53 use the <code>findDevice()</code> method
54 which has four parameters:
55 </p>
56
57 <pre>
58 chrome.experimental.usb.findDevice(vendorId, productId, eventCallbacks, onDevice FoundCallback)
59 </pre>
60
61 <br>
62
63 <table border="0">
64 <tr>
65 <th scope="col"> Parameter (type) </th>
66 <th scope="col"> Description </th>
67 </tr>
68 <tr>
69 <td>vendorId (long)</td>
70 <td>The vendor ID for your USB device (in Linux, use lsusb to find it).</td>
71 </tr>
72 <tr>
73 <td>productId (long)</td>
74 <td>The product ID for your USB device (in Linux, use lsusb to find it).</td >
75 </tr>
76 <tr>
77 <td>eventCallbacks(object)</td>
78 <td>An object with a single key, "onEvent",
79 to be called whenever an event occurs on the corresponding device.
80 This will be the primary method of receiving information from the device.
81 As the host-initiated USB protocol is complex, read on to learn more.
82 </td>
83 </tr>
84 <tr>
85 <td>onDevideFoundCallback()</td>
86 <td>Called when the device is found.
87 The callback's parameter is an object
88 with three properties: <code>handle</code>,
89 <code>vendorId</code>,
90 <code>productId</code>;
91 or <code>NULL</code>,
92 if no device is found.
93 Save this object
94 as it's required to send messages to the device.</td>
95 </tr>
96 </table>
97
98 <p>
99 Example:
100 </p>
101
102 <pre>
103 var onDeviceFound = function(device) {
104 deviceObj=device;
105 if (device) {
106 console.log(“Device found: ”+device.handle);
107 } else {
108 console.log(“Device could not be found”);
109 }
110 };
111
112 var onUsbEvent = function(event) {
113 console.log(“USB event!”);
114 };
115
116 chrome.experimental.usb.findDevice(vendorId, productId, {"onEvent": onUsbEvent}, onDeviceFound);
117 </pre>
118
119 <h3>USB transfers and receiving data from a device</h3>
120
121 <p>
122 USB protocol defines four types of transfers:
123 control, bulk, isochronous and interrupt.
124 Theoretically they can all occur in both directions:<br>
125 device-to-host and host-to-device.
126 Host-to-device is initiated by your code and is described in the next sections.
127 </p>
128
129 <p>
130 Device-to-host messages are handled by Chrome and delivered
131 to the <code>onEvent()</code> callback
132 defined in the <code>findDevice()</code> method.
133 For each message from the device,
134 the <code>onEvent</code> callback will receive
135 an event object with the following properties:
136 </p>
137
138 <br>
139
140 <table border="0">
141 <tr>
142 <th scope="col"> Property </th>
143 <th scope="col"> Description </th>
144 </tr>
145 <tr>
146 <td>type (string)</td>
147 <td>Currently always contains the string "transferResult".</td>
148 </tr>
149 <tr>
150 <td>resultCode (integer)</td>
151 <td>0 is success; other values indicate failure.</td>
152 </tr>
153 <tr>
154 <td>data (integer array)</td>
155 <td>Contains the data sent by the device.
156 </td>
157 </tr>
158 </table>
159
160 <p>
161 Example:
162 </p>
163
164 <pre>
165 var onUsbEvent = function(event) {
166 if (event &amp;&amp; event.resultCode===0 &amp;&amp; event.data) {
167 console.log(“got ”+event.data.length+" bytes");
168 }
169 };
170
171 chrome.experimental.usb.findDevice( vendorId, productId, {"onEvent": onUsbEvent} , onDeviceFound);
172 </pre>
173
174 <h3>Sending data - control transfers</h3>
175
176 <p>
177 Control transfers are generally used to send configuration
178 or command parameters to a USB device.
179 The method is simple and receives three parameters:
180 </p>
181
182 <pre>
183 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, transferCallbac k)
184 </pre>
185
186 <br>
187
188 <table border="0">
189 <tr>
190 <th scope="col"> Parameter (types)</th>
191 <th scope="col"> Description </th>
192 </tr>
193 <tr>
194 <td>deviceObj</td>
195 <td>Object received from <code>findDevice()</code> callback.</td>
196 </tr>
197 <tr>
198 <td>transferInfo</td>
199 <td>Parameter object with values from the table below.
200 Check your USB device protocol specification
201 to know which values are supported.</td>
202 </tr>
203 <tr>
204 <td>transferCallback()</td>
205 <td>Invoked when the transfer has completed.
206 Please note this only indicates that
207 the transfer has been processed.
208 The device's response, if any, will always be sent through
209 the <code>onEvent()</code> callback set on <code>findDevice()</code>.
210 </td>
211 </tr>
212 </table>
213
214 <p>
215 Values for <code>transferInfo</code> object:
216 </p>
217
218 <table border="0">
219 <tr>
220 <th scope="col"> Value </th>
221 <th scope="col"> Description </th>
222 </tr>
223 <tr>
224 <td>requestType (string)</td>
225 <td>"vendor", "standard", "class" or "reserved".</td>
226 </tr>
227 <tr>
228 <td>recipient (string)</td>
229 <td>"device", "interface", "endpoint" or "other".</td>
230 </tr>
231 <tr>
232 <td>direction (string)</td>
233 <td>in" or "out".
234 IN direction is used to notify the device
235 that it should send information to the host.
236 All communication in a USB bus is host-initiated,
237 so use an 'in' transfer to allow a device
238 to send information back.</td>
239 </tr>
240 <tr>
241 <td>request (integer)</td>
242 <td>Defined by your device's protocol.</td>
243 </tr>
244 <tr>
245 <td>value (integer)</td>
246 <td>Defined by your device's protocol.</td>
247 </tr>
248 <tr>
249 <td>index (integer)</td>
250 <td>Defined by your device's protocol.</td>
251 </tr>
252 <tr>
253 <td>length (integer)</td>
254 <td>Only used when direction is "in".
255 Notifies the device that this is the amount
256 of data the host is expecting in response.</td>
257 </tr>
258 <tr>
259 <td>data (integer array)</td>
260 <td>Defined by your device's protocol,
261 required when direction is "out".
262 <b>WARNING: </b>in the near future,
263 this parameter will likely change
264 to <code>ArrayBuffer</code>.</td>
265 </tr>
266 </table>
267
268 <p>
269 Example:
270 </p>
271
272 <pre>
273 var transferInfo = {
274 "requestType": "vendor",
275 "recipient": "device",
276 "direction": "out",
277 "request": 0x31,
278 "value": 120,
279 "index": 0,
280 "data": [4, 8, 15, 16, 23, 42]
281 };
282 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, optionalCallbac k);
283 </pre>
284
285 <h3>Sending data - isochronous transfers</h3>
286
287 <p>
288 Isochronous transfers are commonly used for streams of data.
289 Video and sound, for example, are good candidates for this transfer type.
290 To send an isochronous transfer, use:
291 </p>
292
293 <pre>
294 chrome.experimental.usb.isochronousTransfer(deviceObj, isochronousTransferInfo, transferCallback)
295 </pre>
296
297 <br>
298
299 <table border="0">
300 <tr>
301 <th scope="col"> Parameter </th>
302 <th scope="col"> Description </th>
303 </tr>
304 <tr>
305 <td>deviceObj</td>
306 <td>Object sent on <code>findDevice()</code> callback.</td>
307 </tr>
308 <tr>
309 <td>isochronousTransferInfo</td>
310 <td>Parameter object with the values in the table below.</td>
311 </tr>
312 <tr>
313 <td>transferCallback()</td>
314 <td>Invoked when the transfer has completed.
315 Notice that this callback doesn't represent any response from the device.
316 It's just to notify your code that the asynchronous transfer request
317 has been processed and you can go ahead.
318 The device's response, if any, will always be sent through
319 the <code>onEvent()</code> callback set on <code>findDevice()</code>.
320 </td>
321 </tr>
322 </table>
323
324 <p>
325 Values for <code>isochronousTransferInfo</code> object:
326 </p>
327
328 <table border="0">
329 <tr>
330 <th scope="col"> Value </th>
331 <th scope="col"> Description </th>
332 </tr>
333 <tr>
334 <td>transferInfo (object)</td>
335 <td>A parameter object with the following parameters:<br>
336 <b>direction (string): </b>"in" or "out".<br>
337 <b>endpoint (integer): </b>defined by your device's protocol.<br>
338 <b>length (integer): </b>only used when direction is "in".
339 Notifies the device that this is the amount
340 of data the host is expecting in response<br>
341 <b>data (integer array): </b>defined by your device's protocol;
342 only used when direction is "out".
343 </td>
344 </tr>
345 <tr>
346 <td>packets (integer)</td>
347 <td>Total number of packets expected in this transfer.</td>
348 </tr>
349 <tr>
350 <td>packetLength (integer)</td>
351 <td>Expected length of each packet in this transfer.</td>
352 </tr>
353 </table>
354
355 <p>
356 Example:
357 </p>
358
359 <pre>
360 var transferInfo = {
361 "direction": "in",
362 "endpoint": 1,
363 "length": 2560
364 };
365 var isoTransferInfo = {
366 "transferInfo": transferInfo,
367 "packets": 20,
368 "packetLength": 128
369 };
370 chrome.experimental.usb.isochronousTransfer(deviceObj, isoTransferInfo, optional Callback);
371 </pre>
372
373 <h3>Sending data - bulk transfers</h3>
374
375 <p>
376 Bulk transfer is a USB transfer type commonly used
377 to transfer a large amount of data reliably.
378 The method has three parameters:
379 </p>
380
381 <pre>
382 chrome.experimental.usb.bulkTransfer(deviceObj, transferInfo, transferCallback)
383 </pre>
384
385 <br>
386
387 <table border="0">
388 <tr>
389 <th scope="col"> Parameter </th>
390 <th scope="col"> Description </th>
391 </tr>
392 <tr>
393 <td>deviceObj</td>
394 <td>Object sent on <code>findDevice()</code> callback.</td>
395 </tr>
396 <tr>
397 <td>transferInfo</td>
398 <td>Parameter object with the values in the table below.</td>
399 </tr>
400 <tr>
401 <td>transferCallback()</td>
402 <td>Invoked when the transfer has completed.
403 Notice that this callback doesn't represent any response from the device.
404 It's just to notify your code that the asynchronous transfer request
405 has been processed and you can go ahead.
406 The device's response, if any, will always be sent through
407 the <code>onEvent()</code> callback set on <code>findDevice()</code>.
408 </td>
409 </tr>
410 </table>
411
412 <p>
413 Values for <code>transferInfo</code> object:
414 </p>
415
416 <table border="0">
417 <tr>
418 <th scope="col"> Value </th>
419 <th scope="col"> Description </th>
420 </tr>
421 <tr>
422 <td>direction (string)</td>
423 <td>"in" or "out".</td>
424 </tr>
425 <tr>
426 <td>endpoint (integer)</td>
427 <td>Defined by your device's protocol.</td>
428 </tr>
429 <tr>
430 <td>length (integer)</td>
431 <td>Only used when direction is "in".
432 Notifies the device that this is the amount
433 of data the host is expecting in response.</td>
434 </tr>
435 <tr>
436 <td>data (integer array)</td>
437 <td>Defined by your device's protocol;
438 only used when direction is "out".</td>
439 </tr>
440 </table>
441
442 <p>
443 Example:
444 </p>
445
446 <pre>
447 var transferInfo = {
448 "direction": "out",
449 "endpoint": 1,
450 "data": [4, 8, 15, 16, 23, 42]
451 };
452 </pre>
453
454 <h3>Sending data - interrupt transfers</h3>
455
456 <p>
457 Interrupt transfers are used to send important notifications.
458 Since all USB communication is initiated by the host,
459 host code usually polls the device periodically,
460 sending interrupt IN transfers that will make the device send data back
461 if there is anything on the interrupt queue.
462 The method has three parameters:
463 </p>
464
465 <pre>
466 chrome.experimental.usb.interruptTransfer(deviceObj, transferInfo, transferCallb ack)
467 </pre>
468
469 <br>
470
471 <table border="0">
472 <tr>
473 <th scope="col"> Parameter </th>
474 <th scope="col"> Description </th>
475 </tr>
476 <tr>
477 <td>deviceObj</td>
478 <td>Object sent on <code>findDevice()</code> callback.</td>
479 </tr>
480 <tr>
481 <td>transferInfo</td>
482 <td>Parameter object with the values in the table below.</td>
483 </tr>
484 <tr>
485 <td>transferCallback()</td>
486 <td>Invoked when the transfer has completed.
487 Notice that this callback doesn't represent any response from the device.
488 It's just to notify your code that the asynchronous transfer request
489 has been processed and you can go ahead.
490 The device's response, if any, will always be sent through
491 the <code>onEvent()</code> callback set on <code>findDevice()</code>.
492 </td>
493 </tr>
494 </table>
495
496 <p>
497 Values for <code>transferInfo</code> object:
498 </p>
499
500 <table border="0">
501 <tr>
502 <th scope="col"> Value </th>
503 <th scope="col"> Description </th>
504 </tr>
505 <tr>
506 <td>direction (string)</td>
507 <td>"in" or "out".</td>
508 </tr>
509 <tr>
510 <td>endpoint (integer)</td>
511 <td>Defined by your device's protocol.</td>
512 </tr>
513 <tr>
514 <td>length (integer)</td>
515 <td>Only used when direction is "in".
516 Notifies the device that this is the amount
517 of data the host is expecting in response.</td>
518 </tr>
519 <tr>
520 <td>data (integer array)</td>
521 <td>Defined by your device's protocol;
522 only used when direction is "out".</td>
523 </tr>
524
525 <p>
526 Example:
527 </p>
528
529 <pre>
530 var transferInfo = {
531 "direction": "in",
532 "endpoint": 1,
533 "length": 2
534 };
535 chrome.experimental.usb.interruptTransfer(deviceObj, transferInfo, optionalCallb ack);
536 </pre>
537
538 <h3>Caveats</h3>
539
540 <p>
541 On Linux,
542 you must have specific permission to access USB devices.
543 Create a file: <code>/etc/udev/rules.d/50-yourdevicename.rules</code>.
544 Add the following content:
545 </p>
546
547 <pre>
548 SUBSYSTEM=="usb", ATTR{idVendor}=="yourdevicevendor", MODE="0664", GROUP="plugde v"
549 </pre>
550
551 <p>
552 On MacOSX,
553 devices which expose a HID profile cannot be managed
554 using this low level API due to system restrictions.
555 Currently there is no workaround.
556 </p>
557
558 <h2 id="serial">Accessing serial devices</h2>
559
560 <p>
561 You can use the serial API to read
562 and write from a serial device.
563 </p>
564
565 <h3>Manifest requirement</h3>
566
567 <p>
568 The "serial" permission is not yet required;
569 "experimental" is enough for now:
570 </p>
571
572 <pre>
573 "permissions": ["experimental"]
574 </pre>
575
576 <h3>Listing available serial ports</h3>
577
578 <p>
579 To get a list of available serial ports,
580 use the <code>getPorts()</code> method:
581 </p>
582
583 <pre>
584 var onGetPorts = function(ports) {
585 for (var i=0; i&lt;ports.length; i++) {
586 console.log(ports[i]);
587 }
588 }
589 chrome.experimental.serial.getPorts(onGetPorts);
590 </pre>
591
592 <h3>Opening a serial device</h3>
593
594 <p>
595 Here's how to open a serial device:
596 </p>
597
598 <pre>
599 var onOpen = function(connectionInfo) {
600 // The serial device has been opened. Save its id to use later.
601 var conId = connectionInfo.connectionId;
602 // Do whatever you need to do with the opened device.
603 }
604 // Open the serial device /dev/ttyS01
605 chrome.experimental.serial.open("/dev/ttyS01", onOpen);
606 </pre>
607
608 <h3>Closing a serial device</h3>
609
610 <p>
611 Here's how to close a serial device:
612 </p>
613
614 <pre>
615 var onClose = function(result) {
616 console.log(“Serial port closed”);
617 }
618 chrome.experimental.serial.close(conId, onClose);
619 </pre>
620
621 <h3>Reading from a serial device</h3>
622
623 <p>
624 The serial API reads from the serial port and
625 delivers the read bytes as an ArrayBuffer.
626 There is no guarantee that all the available bytes will be read in one chunk
627 (they are currently read one byte per time, but this might change in the future) .
628 The following procedure can accumulate read bytes until a new line is read,
629 and then call a listener with the <code>ArrayBuffer</code> bytes converted to a String:
630 </p>
631
632 <pre>
633 var dataRead='';
634
635 var onCharRead=function(readInfo) {
636 if (!connectionInfo) {
637 return;
638 }
639 if (readInfo &amp;&amp; readInfo.bytesRead>0 &amp;&amp; readInfo.data) {
640 var str=ab2str(readInfo.data);
641 if (str[str.length-1]==='\n') {
642 dataRead+=str.substring(0, str.length-1);
643 onLineRead(dataRead);
644 dataRead="";
645 } else {
646 dataRead+=str;
647 }
648 }
649 chrome.experimental.serial.read(connectionInfo.connectionId, onCharRead);
650 }
651
652 /* Convert an ArrayBuffer to a String, using UTF-8 as the encoding scheme.
653 This is consistent with how Arduino sends characters by default */
654 var ab2str=function(buf) {
655 return String.fromCharCode.apply(null, new Uint8Array(buf));
656 };
657 </pre>
658
659 <h3>Writing to a serial device</h3>
660
661 <p>
662 The writing routine is simpler than the reading,
663 since the writing can occur all at once.
664 The only catch is that if your data protocol is String based,
665 you have to convert your output string to an <code>ArrayBuffer</code>
666 to compy with write's method signature.
667 See the code below:
668 </p>
669
670 <pre>
671 var writeSerial=function(str) {
672 chrome.experimental.serial.write(connectionInfo.connectionId, str2ab(str), onW rite);
673 }
674 var str2ab=function(str) {
675 var buf=new ArrayBuffer(str.length);
676 var bufView=new Uint8Array(buf);
677 for (var i=0; i&lt;str.length; i++) {
678 bufView[i]=str.charCodeAt(i);
679 }
680 return buf;
681 }
682 </pre>
683
684 <h3>Flushing a serial device buffer</h3>
685
686 <p>
687 You can flush your serial device buffer by issuing the flush command on the API:
688 </p>
689
690 <pre>
691 var flushSerial=function(str) {
692 chrome.experimental.serial.flush(connectionInfo.connectionId, onFlush);
693 }
694 </pre>
695
696 <p class="backtotop"><a href="#top">Back to top</a></p>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698