Remove generated files
[framework/connectivity/libgphoto2.git] / doc / camlib_devel.sgml
1 <!doctype linuxdoc system>
2
3 <!-- LyX 1.1 created this file. For more info see http://www.lyx.org/ -->
4 <article>
5 <title>
6 gPhoto2 Camera Library Developer's Guide
7 </title>
8 <author>
9 Scott Fritzinger
10 2000-07-26
11 Revision 1
12 </author>
13 <toc>
14 <sect>
15 Reverse Engineering the Camera Protocol
16 <p>
17 The most difficult part for most developers is obtaining the transfer protocol.
18  If the OEM's are lucky enough, they will simply provide us with the protocol
19  specifications for their cameras and the drivers will be written at no cost
20  to them. Most OEM's refuse to do so though, citing trade secrets or company
21  policy; this is truly unfortunate in that they have effectively told their
22  own customers who use operating systems other than Windows and the Mac that
23  they don't want their future business and that they aren't valued customer
24  to begin with.
25 </p>
26 <p>
27 When OEM's do not cooperate, the developer is left to determine the protocol
28  him/herself through reverse engineering. 
29 </p>
30 <sect1>
31 Sniffing the Protocol
32 <p>
33 What follows are the most common setups for sniffing camera protocol traffic.
34  In all setups, a host computer runs the native camera drivers.; typically,
35  the Windows serial port drivers are used for reverse engineering. The drivers
36  are run through a series of functions that include getting a picture index,
37  downloading thumbnails, download full images, deleting images, camera configuration
38  options, in addition to any other features a camera might have. During these
39  operations, one or more of the following methods are used to capture the communication
40  between the host computer and the camera.
41 </p>
42 <sect2>
43 Serial Repeater
44 <p>
45 A serial repeater consists of the host computer, a computer used as a repeater,
46  and the camera. The setup is shown in figure .
47 </p>
48 <p>
49 The repeater runs special software which reads data from one serial port,
50  logs the communication, and then outputs the data to the other serial port.
51  Data that is from the host computer to the camera and from the camera to the
52  host computer is logged sequentially in a single log file. Information logged
53  includes hexadecimal data values, direction of the communication, as well as
54  time stamps for synchronization. An example sniffer to use for this configuration
55  is "sersniff".
56 </p>
57 <sect2>
58 "Y" Serial Cable
59 <p>
60 To avoid using two computers, a Y serial cable can be used. The "trunk" end
61  of the serial cable attaches to the camera's serial transfer cable, while the
62  two "branches" plug in to two serial ports on the host computer. Figure shows
63  this setup.
64 </p>
65 <p>
66 The camera drivers use one of the serial ports on the host computer, while
67  the other port is opened with a hexadecimal monitor application that dumps
68  all communications on the port to a file. The downside to this approach is
69  the developer would have to determine which sets of data was generated by the
70  camera or the host computer. Also, a Y cable would have to be either built
71  or purchased from an electronics supply store.
72 </p>
73 <sect2>
74 Virtual Device Driver Hooks
75 <p>
76 The Windows platform allows virtual device drivers to "hook" into other drivers
77  to provide additional functionality or feature enhancements. A combination
78  GUI and device driver named PortMon by Systems Internals is a communications
79  debugging utility that hooks into the existing Windows serial device driver
80  (vcomm.vxd) and logs communications. Figure shows this equipment arrangement.
81 </p>
82 <p>
83 This setup allows the developer to not use any extra hardware by simply
84  relying on software. This is perhaps the easiest method for capturing camera
85  data.
86 </p>
87 <sect1>
88 Making Sense Out of the Protocol
89 <p>
90 What follows are some pointers on decoding camera protocols. It uses a
91  protocol that isn't really any camera protocol in particular, but should demonstrate
92  some commonalities between most camera protocols.
93 </p>
94 <p>
95 <enum>
96  <item>
97 Cameras like to ping. This is the in the form of an &quot;ACK&quot;&quot;
98  command that is different for different cameras. Basically, it is usually a
99  short packet (probably 1 byte) that is sent both ways in order for the camera
100  to know the computer is there or vice versa. It is also sometimes used to wake
101  up a camera that has gone into power-save mode. It usually starts out the communications,
102  as well as confirms each packet in any sort of &quot;mass&quot; transfer. The
103  opposite, a &quot;NAK&quot;, is sent to basically say the last packet was not
104  received, or an error has occured. Again, this is usually just a single byte
105  as well.
106
107 Example:
108 Computer: 01
109 Camera : 01
110
111 The Camera sent an ACK (&quot;01&quot;)
112  and the Computer responded with an ACK as well.
113  <item>
114 Transfers are usually in &quot;reverse network order&quot;, meaning least
115  significant bytes come before most significant bytes. For example, "00 08" should
116  actually be reassembled as "08 00". 
117  <item>
118 Most protocols use starting and stopping bytes. 
119
120 Example:
121 Computer: 03
122  50 00 0f e0 04 
123 Camera : 03 03 00 3f 03 04 
124 Computer: 01 
125
126 For this example,
127  notice the packets begin with &quot;03&quot; and end with &quot;04&quot; (don't
128  pay attention to what is between them). Also notice the Computer sent an &quot;ACK&quot;
129  to confirm it got the packet.
130  <item>
131 Packets usually have a &quot;command&quot; byte, which tells either the
132  computer or the camera what to do. Let's say you told the software to retrieve
133  the number of pictures, which at the time happened to be &quot;8&quot;, and
134  you got the following:
135
136 Computer: 03 01 00 00 00 04 
137 Camera : 03 01 00 00 08
138  04 
139 Computer: 01
140
141 In this example, you notice the &quot;03&quot; and &quot;04&quot;
142  specifying the start and stop of the packet. Also, you notice the second byte
143  in the Computer packet is &quot;01&quot;. The camera responds with the above
144  packet, and low and behold, you see the number 8 in the same packet. It would
145  appear, initially, that the second byte is used as a command byte, and that
146  &quot;01&quot; specifies the camera to return the number of pictures. This
147  may very well be right, but don't jump into it yet. Make sure you look at a
148  bunch of similar situations to confirm this. (Again, notice the &quot;ACK&quot;
149  sent by the computer).
150  <item>
151 Most protocols have a &quot;data size&quot; byte(s) in data packets. Let's
152  say that you told the camera to retrieve thumbnail 8 and you get the following:
153
154 Computer:
155  03 02 00 00 08 04
156 Camera : 03 02 00 0F (15 bytes) 04
157 Computer: 01 
158
159 OK, here's
160  a brief breakdown of this transaction:
161
162 -Looks like the command to retrieve
163  a thumbnail is &quot;02&quot; (2nd byte in the computer packet), and that the
164  byte that is &quot;08&quot; specifies which thumbnail to return.
165 -The camera
166  responds with a &quot;02&quot; in the command field, specifying it is returning
167  a thumbnail, and then sends &quot;0F&quot;, and 15 bytes of data. 
168 -It looks
169  like the byte &quot;0F&quot; specifies how many bytes are after it in the same
170  packet. This is a data size byte. 
171 (Note: this is a simplistic example. No
172  thumbnail will only be 15 bytes :) this leads up to the next thing to consider)
173  <item>
174 Most protocols have an &quot;order&quot; or &quot;counter&quot; byte. This
175  is used so that, in large data transfers where the picture may be split up
176  into several different packets, the computer knows how to reassemble all the
177  data. The entire thumbnail more than likely will not be contained in a single
178  packet for logistical reasons, so they break up the data into many different
179  packets and give each packet a unique number (or "order" byte). Let's say you
180  told your camera to return thumbnail 8 (which is, as mentioned, pretty big),
181  and you get the following:
182
183 Computer: 02 03 00 00 08 03 
184 Camera : 02 03 00
185  0F (15 bytes) 03 
186 Computer: 01
187 Camera : 02 03 01 0F (15 bytes) 03 
188 Computer:
189  01
190 Camera : 02 03 02 0F (15 bytes) 03
191 Computer: 01
192 ... 5 more packets and ACKs
193  ...
194 Camera : 02 03 08 09 (9 bytes) 03
195 Computer: 01
196
197 You notice that the 3rd
198  byte of each of the camera packets increments with each packet sent from the
199  camera. This looks like it is an order (counter) byte. the computer can then
200  reassemble the data from all the packets in order to reproduce the image.
201  <item>
202 Most protocols have some sort of error detection byte(s) at the end of
203  the packet. This is usually a simple checksum (summation of bytes), or a CRC
204  (a somewhat complex algorithm that reduces the probability of mis-diagnosing
205  a packet with errors by magnitudes). These bytes can take into account only
206  the data, or maybe the entire packet excluding those error detection bytes.
207  If this isn't a known scheme, this winds up being the hardest part of reimplementing
208  the protocol. Lets take the above example again, this time we'll add a couple
209  bytes on the end for error detection:
210
211 Computer: 02 03 00 00 08 03
212 Camera :
213  02 03 00 0F (15 bytes) 0f 02 03
214 Computer: 01
215 Camera : 02 03 01 0F (15 bytes)
216  0e 00 03
217 Computer: 02
218 Camera : 02 03 01 0F (15 bytes) fa d0 03
219 Computer: 01
220 Camera
221  : 02 03 02 0F (15 bytes) fa d0 03
222 Computer: 01
223 ... 5 more packets and ACKs
224 Camera
225  : 02 03 08 09 (9 bytes) d7 38 03
226 Computer: 01
227
228 Notice how the error detection
229  bytes are usually different for each packet. These may be checksums, or CRC's,
230  or something else. Only way to find out really is to try each one, on different
231  combinations of packet parts (data, order byte, command byte, etc...) and see
232  if you get the same thing. Try this on the shorter packets to make life easier.
233
234 Look
235  at one more thing that sticks out in this transaction: for packet with order
236  byte "01", the Computer responded with a &quot;02". and the Camera then resent
237  the same packet it just did. This shows that the NAK byte is &quot;02&quot;,
238  and this could happen because maybe the error detection bytes didn't match
239  with the data, or maybe something else happened. either way, the camera resent
240  the last packet, and now you know how the camera can recover from transfer
241  errors. If you didn't get the packet you were expecting, send the camera a
242  NAK and it will resend the same packet again.
243 </enum>
244 </p><sect>
245 Understanding the gPhoto2 Design
246 <p>
247 The gPhoto2 design is the same three-tiered structure that has worked extremely
248  well in the past with other software packages. Here is a listing of the 3 tiers:
249 </p>
250 <p>
251 <itemize>
252  <item>
253 the camera library
254  <item>
255 the I/O library
256  <item>
257 the front-end
258  <item>
259 the "core"
260 </itemize>
261 </p><sect1>
262 Role of the Camera Library
263 <p>
264 The camera library is in charge of talking directly with the camera. The
265  library uses the gPhoto2 Camera API in order to provide a common access-method
266  for the library itself. Being dynamically linked, the libraries are loaded
267  at run-time depending on the camera model the end-user would like to access.
268  
269 </p>
270 <p>
271 In order to provide flexilibity with variations in camera design, there
272  are camera "abilities" which list, well, the abilities of each camera model.
273  Some camera may support serial port connections only, while others may be able
274  to use USB and a serial port. We've run into cameras that don't support thumbnailing
275  on the camera so there is an "abilities" field to specify whether or not the
276  camera supports thumbnailing. The "abilities" also list other things such as
277  supported serial transfer speeds, file deletion, and other functionality.
278 </p>
279 <p>
280 The camera libraries only make functions calls to the I/O library and to
281  the gPhoto2 core.
282 </p>
283 <p>
284 There is more information on the specifics of the camera library in section
285  3 of this document.
286 </p>
287 <sect1>
288 Role of the I/O Library
289 <p>
290 The gPhoto2 I/O library is a platform-independent communications library
291  that support serial, parallel, USB, firewire, and network connections. It is
292  a work-in-progress with a constantly expanding list of supported platforms.
293  This library uses the gPhoto2 I/O library API for accessing communications
294  devices. It enumerates the devices available on a system, and provides read/write
295  access.
296 </p>
297 <p>
298 The camera libraries all use the I/O library for communications with the
299  cameras. By doing having all communications go through a single library, the
300  camera libraries become as portable as the I/O library. Porting gPhoto2 to
301  other platforms become extremely easy.
302 </p>
303 <p>
304 There is more information on the specifics of the I/O library in section
305  3 of this document.
306 </p>
307 <sect1>
308 Role of the Front-end
309 <p>
310 The front-end is the application that the user interacts with. It is usually
311  a command-line program, or a graphical point-and-click interface. The front-end
312  talks only with the gPhoto2 core in order to retrieve pictures and perform
313  other functions with the camera.
314 </p>
315 <sect1>
316 Role of the gPhoto2 Core
317 <p>
318 The gPhoto2 "core" is the heart of gPhoto2. It provides services to both
319  the camera libraries and the front-ends. Most of the services deal with error-checking
320  and enumeration of devices (cameras, I/O devices, etc...). The core performs
321  validity checking on data passed to/from the front-end or the camera library.
322 </p>
323 <p>
324 You could consider the core a translator/interpreter/spell-checker/army-general
325  in the "big picture" of gPhoto2. It does the grunt-work and performs the coordination
326  of the other parts.
327 </p>
328 <sect>
329 Implementing the Library
330 <p>
331 gPhoto2 camera libraries use the gPhoto2 Camera API (CAPI) for implementation.
332  Here is a listing of the CAPI functions: 
333 </p>
334 <p>
335 camera_id 
336 </p>
337 <p>
338 camera_abilities 
339 </p>
340 <p>
341 camera_init 
342 </p>
343 <p>
344 camera_exit
345 </p>
346 <p>
347 camera_folder_list
348 </p>
349 <p>
350 camera_file_list 
351 </p>
352 <p>
353 camera_file_get
354 </p>
355 <p>
356 camera_file_get_preview
357 </p>
358 <p>
359 camera_file_put 
360 </p>
361 <p>
362 camera_file_delete 
363 </p>
364 <p>
365 camera_config_get
366 </p>
367 <p>
368 camera_config_set
369 </p>
370 <p>
371 camera_capture
372 </p>
373 <p>
374 camera_summary
375 </p>
376 <p>
377 camera_manual
378 </p>
379 <p>
380 camera_about
381
382 </p>
383 <p>
384 Section 3.1 details the purpose of each of these functions, while Section
385  3.2 discusses how to use the I/O library.
386 </p>
387 <sect1>
388 Camera API
389 <p>
390 The CAPI provides the full set of functions for doing various tasks with
391  the camera. All CAPI functions return either GP_OK for succesful execution
392  , or GP_ERROR for a failure of execution
393 </p>
394 <p>
395 What follows is a listing of the functions, including prototypes and data
396  exchange:
397 </p>
398 <sect2>
399 camera_id
400 <p>
401 <bf>Purpose: </bf>Retrieve the unique id for the camera library.
402 </p>
403 <p>
404 <bf>Prototype: </bf>int camera_id (CameraText *id); 
405 </p>
406 <p>
407 <bf>Arguments: </bf>
408 </p>
409 <p>
410 CameraText *id : unique string to represent the camera library
411
412 </p>
413 <p>
414 In order to guarantee that only once instance of the camera library is
415  loaded for each instance of the core, the camera library must copy a unique
416  string into the "id". Please consult the gPhoto developers to determine which
417  string you should use.
418
419 </p>
420 <p>
421 <bf>Example:</bf>
422 </p>
423 <p>
424 int camera_id(CameraText *id) &lcub;
425 </p>
426 <p>
427 strcpy(id-&gt;text, "my-unique-string");
428 </p>
429 <p>
430 return (GP_OK);
431 </p>
432 <p>
433 &rcub;
434 </p>
435 <sect2>
436 camera_abilities
437 <p>
438 <bf>Purpose: </bf>Retrieve the list of supported cameras and the abilities for each
439  camera
440 </p>
441 <p>
442 <bf>Prototype: </bf>int camera_abilities (CameraAbilitiesList *list); 
443 </p>
444 <p>
445 <bf>Arguments: </bf>d
446 </p>
447 <p>
448 CameraAbilities *abilities : the list of abilities for the supported cameras
449 </p>
450 <p>
451 int *count : the number of 
452 </p>
453 <sect2>
454 camera_init
455 <p>
456 <bf>Purpose: </bf>Initialize the camera
457 </p>
458 <p>
459 <bf>Prototype: </bf>int camera_init (Camera *camera, CameraInit *init); 
460 </p>
461 <p>
462 <bf>Arguments: </bf>d
463 </p>
464 <sect2>
465 camera_exit 
466 <p>
467 <bf>Purpose: </bf>Close the camera
468 </p>
469 <p>
470 <bf>Prototype: </bf>int camera_exit (Camera *camera); 
471 </p>
472 <p>
473 <bf>Arguments: </bf>d
474 </p>
475 <sect2>
476 camera_file_list
477 <p>
478 <bf>Purpose: </bf>List the files in a particular folder on the camera
479 </p>
480 <p>
481 <bf>Prototype: </bf>int camera_file_list(Camera *camera, CameraList *list, char
482  *folder); 
483 </p>
484 <p>
485 <bf>Arguments: </bf>d
486 </p>
487 <sect2>
488 camera_folder_list
489 <p>
490 <bf>Purpose: </bf>List the subfolders in a particular folder on the camera
491 </p>
492 <p>
493 <bf>Prototype: </bf>int camera_folder_list(Camera *camera, CameraList *list, char
494  *folder); 
495 </p>
496 <p>
497 <bf>Arguments: </bf>d
498 </p>
499 <sect2>
500 camera_file_get
501 <p>
502 <bf>Purpose: </bf>Retrieve a file from the camera
503 </p>
504 <p>
505 <bf>Prototype: </bf>int camera_file_get (Camera *camera, CameraFile *file, char
506  *folder, char *filename); 
507 </p>
508 <p>
509 <bf>Arguments: </bf>d
510 </p>
511 <sect2>
512 camera_file_get_preview
513 <p>
514 <bf>Purpose: </bf>Retrieve a file's preview from the camera
515 </p>
516 <p>
517 <bf>Prototype: </bf>int camera_file_get_preview (Camera *camera, CameraFile *file,
518  char *folder, char *filename); 
519 </p>
520 <p>
521 <bf>Arguments: </bf>d
522 </p>
523 <sect2>
524 camera_file_put
525 <p>
526 <bf>Purpose: </bf>Place (upload) a file to the camera
527 </p>
528 <p>
529 <bf>Prototype: </bf>int camera_file_put (Camera *camera, CameraFile *file, char
530  *folder); 
531 </p>
532 <p>
533 <bf>Arguments: </bf>d
534 </p>
535 <sect2>
536 camera_file_delete
537 <p>
538 <bf>Purpose: </bf>Delete a file from the camera
539 </p>
540 <p>
541 <bf>Prototype: </bf>int camera_file_delete (Camera *camera, char *folder, char *filename);
542  
543 </p>
544 <p>
545 <bf>Arguments: </bf>d
546 </p>
547 <sect2>
548 camera_config_get
549 <p>
550 <bf>Purpose: </bf>Retrieve the configuration window.
551 </p>
552 <p>
553 <bf>Prototype: </bf>int camera_config_get (Camera *camera, CameraWidget *window);
554  
555 </p>
556 <p>
557 <bf>Arguments: </bf>d
558 </p>
559 <sect2>
560 camera_config_set
561 <p>
562 <bf>Purpose: </bf>Set camera configuration
563 </p>
564 <p>
565 <bf>Prototype: </bf>int camera_config_set (Camera *camera, CameraSetting *setting,
566  int count); 
567 </p>
568 <p>
569 <bf>Arguments: </bf>d
570 </p>
571 <sect2>
572 camera_capture
573 <p>
574 <bf>Purpose: </bf>Retrieve live data from the camera
575 </p>
576 <p>
577 <bf>Prototype: </bf>int camera_capture (Camera *camera, CameraFile *file, CameraCaptureInfo
578  *info); 
579 </p>
580 <p>
581 <bf>Arguments: </bf>d
582 </p>
583 <sect2>
584 camera_summary
585 <p>
586 <bf>Purpose: </bf>Retrieve the camera summary information
587 </p>
588 <p>
589 <bf>Prototype: </bf>int camera_summary (Camera *camera, CameraText *summary); 
590 </p>
591 <p>
592 <bf>Arguments: </bf>d
593 </p>
594 <sect2>
595 camera_manual
596 <p>
597 <bf>Purpose: </bf>Retrieve the camera user's guide (manual)
598 </p>
599 <p>
600 <bf>Prototype: </bf>int camera_manual (Camera *camera, CameraText *manual); 
601 </p>
602 <p>
603 <bf>Arguments: </bf>d
604 </p>
605 <sect2>
606 camera_about
607 <p>
608 <bf>Purpose: </bf>Retrieve information about the camera library
609 </p>
610 <p>
611 <bf>Prototype: </bf>int camera_about (Camera *camera, CameraText *about);
612 </p>
613 <p>
614 <bf>Arguments: </bf>d
615 </p>
616 <sect1>
617 The gPhoto2 I/O Library
618
619
620 </article>