1 #LyX 1.1 created this file. For more info see http://www.lyx.org/
14 \paperorientation portrait
17 \paragraph_separation indent
19 \quotes_language english
23 \paperpagestyle default
26 \added_space_top vfill \added_space_bottom vfill
27 gPhoto2 Camera Library Developer's Guide
38 \begin_inset LatexCommand \tableofcontents{}
45 Reverse Engineering the Camera Protocol
48 The most difficult part for most developers is obtaining the transfer protocol.
49 If the OEM's are lucky enough, they will simply provide us with the protocol
50 specifications for their cameras and the drivers will be written at no
52 Most OEM's refuse to do so though, citing trade secrets or company policy;
53 this is truly unfortunate in that they have effectively told their own
54 customers who use operating systems other than Windows and the Mac that
55 they don't want their future business and that they aren't valued customer
59 When OEM's do not cooperate, the developer is left to determine the protocol
60 him/herself through reverse engineering.
67 What follows are the most common setups for sniffing camera protocol traffic.
68 In all setups, a host computer runs the native camera drivers.; typically,
69 the Windows serial port drivers are used for reverse engineering.
70 The drivers are run through a series of functions that include getting
71 a picture index, downloading thumbnails, download full images, deleting
72 images, camera configuration options, in addition to any other features
74 During these operations, one or more of the following methods are used
75 to capture the communication between the host computer and the camera.
81 A serial repeater consists of the host computer, a computer used as a repeater,
83 The setup is shown in figure .
86 The repeater runs special software which reads data from one serial port,
87 logs the communication, and then outputs the data to the other serial port.
88 Data that is from the host computer to the camera and from the camera to
89 the host computer is logged sequentially in a single log file.
90 Information logged includes hexadecimal data values, direction of the communica
91 tion, as well as time stamps for synchronization.
92 An example sniffer to use for this configuration is
93 \begin_inset Quotes eld
97 \begin_inset Quotes erd
101 \layout Subsubsection
104 \begin_inset Quotes eld
108 \begin_inset Quotes erd
114 To avoid using two computers, a Y serial cable can be used.
116 \begin_inset Quotes eld
120 \begin_inset Quotes erd
123 end of the serial cable attaches to the camera's serial transfer cable,
125 \begin_inset Quotes eld
129 \begin_inset Quotes erd
132 plug in to two serial ports on the host computer.
133 Figure shows this setup.
136 The camera drivers use one of the serial ports on the host computer, while
137 the other port is opened with a hexadecimal monitor application that dumps
138 all communications on the port to a file.
139 The downside to this approach is the developer would have to determine
140 which sets of data was generated by the camera or the host computer.
141 Also, a Y cable would have to be either built or purchased from an electronics
143 \layout Subsubsection
145 Virtual Device Driver Hooks
148 The Windows platform allows virtual device drivers to
149 \begin_inset Quotes eld
153 \begin_inset Quotes erd
156 into other drivers to provide additional functionality or feature enhancements.
157 A combination GUI and device driver named PortMon by Systems Internals
158 is a communications debugging utility that hooks into the existing Windows
159 serial device driver (vcomm.vxd) and logs communications.
160 Figure shows this equipment arrangement.
163 This setup allows the developer to not use any extra hardware by simply
165 This is perhaps the easiest method for capturing camera data.
168 Making Sense Out of the Protocol
171 What follows are some pointers on decoding camera protocols.
172 It uses a protocol that isn't really any camera protocol in particular,
173 but should demonstrate some commonalities between most camera protocols.
176 Cameras like to ping.
177 This is the in the form of an "ACK"" command that is different for different
179 Basically, it is usually a short packet (probably 1 byte) that is sent
180 both ways in order for the camera to know the computer is there or vice
182 It is also sometimes used to wake up a camera that has gone into power-save
184 It usually starts out the communications, as well as confirms each packet
185 in any sort of "mass" transfer.
186 The opposite, a "NAK", is sent to basically say the last packet was not
187 received, or an error has occurred.
188 Again, this is usually just a single byte as well.
200 The Camera sent an ACK ("01") and the Computer responded with an ACK as
204 Transfers are usually in "reverse network order", meaning least significant
205 bytes come before most significant bytes.
207 \begin_inset Quotes eld
211 \begin_inset Quotes erd
214 should actually be reassembled as
215 \begin_inset Quotes eld
219 \begin_inset Quotes erd
226 Most protocols use starting and stopping bytes.
233 Computer: 03 50 00 0f e0 04
235 Camera : 03 03 00 3f 03 04
241 For this example, notice the packets begin with "03" and end with "04" (don't
242 pay attention to what is between them).
243 Also notice the Computer sent an "ACK" to confirm it got the packet.
246 Packets usually have a "command" byte, which tells either the computer or
247 the camera what to do.
248 Let's say you told the software to retrieve the number of pictures, which
249 at the time happened to be "8", and you got the following:
253 Computer: 03 01 00 00 00 04
255 Camera : 03 01 00 00 08 04
261 In this example, you notice the "03" and "04" specifying the start and stop
263 Also, you notice the second byte in the Computer packet is "01".
264 The camera responds with the above packet, and low and behold, you see
265 the number 8 in the same packet.
266 It would appear, initially, that the second byte is used as a command byte,
267 and that "01" specifies the camera to return the number of pictures.
268 This may very well be right, but don't jump into it yet.
269 Make sure you look at a bunch of similar situations to confirm this.
270 (Again, notice the "ACK" sent by the computer).
273 Most protocols have a "data size" byte(s) in data packets.
274 Let's say that you told the camera to retrieve thumbnail 8 and you get
279 Computer: 03 02 00 00 08 04
281 Camera : 03 02 00 0F (15 bytes) 04
287 OK, here's a brief breakdown of this transaction:
291 -Looks like the command to retrieve a thumbnail is "02" (2nd byte in the
292 computer packet), and that the byte that is "08" specifies which thumbnail
295 -The camera responds with a "02" in the command field, specifying it is
296 returning a thumbnail, and then sends "0F", and 15 bytes of data.
299 -It looks like the byte "0F" specifies how many bytes are after it in the
301 This is a data size byte.
304 (Note: this is a simplistic example.
305 No thumbnail will only be 15 bytes :) this leads up to the next thing to
309 Most protocols have an "order" or "counter" byte.
310 This is used so that, in large data transfers where the picture may be
311 split up into several different packets, the computer knows how to reassemble
313 The entire thumbnail more than likely will not be contained in a single
314 packet for logistical reasons, so they break up the data into many different
315 packets and give each packet a unique number (or
316 \begin_inset Quotes eld
320 \begin_inset Quotes erd
324 Let's say you told your camera to return thumbnail 8 (which is, as mentioned,
325 pretty big), and you get the following:
329 Computer: 02 03 00 00 08 03
331 Camera : 02 03 00 0F (15 bytes) 03
335 Camera : 02 03 01 0F (15 bytes) 03
339 Camera : 02 03 02 0F (15 bytes) 03
344 5 more packets and ACKs ...
346 Camera : 02 03 08 09 (9 bytes) 03
352 You notice that the 3rd byte of each of the camera packets increments with
353 each packet sent from the camera.
354 This looks like it is an order (counter) byte.
355 the computer can then reassemble the data from all the packets in order
356 to reproduce the image.
359 Most protocols have some sort of error detection byte(s) at the end of the
361 This is usually a simple checksum (summation of bytes), or a CRC (a somewhat
362 complex algorithm that reduces the probability of mis-diagnosing a packet
363 with errors by magnitudes).
364 These bytes can take into account only the data, or maybe the entire packet
365 excluding those error detection bytes.
366 If this isn't a known scheme, this winds up being the hardest part of reimpleme
368 Lets take the above example again, this time we'll add a couple bytes on
369 the end for error detection:
373 Computer: 02 03 00 00 08 03
375 Camera : 02 03 00 0F (15 bytes) 0f 02 03
379 Camera : 02 03 01 0F (15 bytes) 0e 00 03
383 Camera : 02 03 01 0F (15 bytes) fa d0 03
387 Camera : 02 03 02 0F (15 bytes) fa d0 03
392 5 more packets and ACKs
394 Camera : 02 03 08 09 (9 bytes) d7 38 03
400 Notice how the error detection bytes are usually different for each packet.
401 These may be checksums, or CRC's, or something else.
402 Only way to find out really is to try each one, on different combinations
403 of packet parts (data, order byte, command byte, etc...) and see if you get
405 Try this on the shorter packets to make life easier.
409 Look at one more thing that sticks out in this transaction: for packet with
411 \begin_inset Quotes eld
415 \begin_inset Quotes erd
418 , the Computer responded with a "02
419 \begin_inset Quotes erd
423 and the Camera then resent the same packet it just did.
424 This shows that the NAK byte is "02", and this could happen because maybe
425 the error detection bytes didn't match with the data, or maybe something
427 either way, the camera resent the last packet, and now you know how the
428 camera can recover from transfer errors.
429 If you didn't get the packet you were expecting, send the camera a NAK
430 and it will resend the same packet again.
433 Understanding the gPhoto2 Design
436 The gPhoto2 design is the same three-tiered structure that has worked extremely
437 well in the past with other software packages.
438 Here is a listing of the 3 tiers:
451 \begin_inset Quotes eld
455 \begin_inset Quotes erd
461 Role of the Camera Library
464 The camera library is in charge of talking directly with the camera.
465 The library uses the gPhoto2 Camera API in order to provide a common access-met
466 hod for the library itself.
467 Being dynamically linked, the libraries are loaded at run-time depending
468 on the camera model the end-user would like to access.
472 In order to provide flexilibity with variations in camera design, there
474 \begin_inset Quotes eld
478 \begin_inset Quotes erd
481 which list, well, the abilities of each camera model.
482 Some camera may support serial port connections only, while others may
483 be able to use USB and a serial port.
484 We've run into cameras that don't support thumbnailing on the camera so
486 \begin_inset Quotes eld
490 \begin_inset Quotes erd
493 field to specify whether or not the camera supports thumbnailing.
495 \begin_inset Quotes eld
499 \begin_inset Quotes erd
502 also list other things such as supported serial transfer speeds, file deletion,
503 and other functionality.
506 The camera libraries only make functions calls to the I/O library and to
510 There is more information on the specifics of the camera library in section
514 Role of the I/O Library
517 The gPhoto2 I/O library is a platform-independent communications library
518 that support serial, parallel, USB, firewire, and network connections.
519 It is a work-in-progress with a constantly expanding list of supported
521 This library uses the gPhoto2 I/O library API for accessing communications
523 It enumerates the devices available on a system, and provides read/write
527 The camera libraries all use the I/O library for communications with the
529 By doing having all communications go through a single library, the camera
530 libraries become as portable as the I/O library.
531 Porting gPhoto2 to other platforms become extremely easy.
534 There is more information on the specifics of the I/O library in section
538 Role of the Front-end
541 The front-end is the application that the user interacts with.
542 It is usually a command-line program, or a graphical point-and-click interface.
543 The front-end talks only with the gPhoto2 core in order to retrieve pictures
544 and perform other functions with the camera.
547 Role of the gPhoto2 Core
551 \begin_inset Quotes eld
555 \begin_inset Quotes erd
558 is the heart of gPhoto2.
559 It provides services to both the camera libraries and the front-ends.
560 Most of the services deal with error-checking and enumeration of devices
561 (cameras, I/O devices, etc...).
562 The core performs validity checking on data passed to/from the front-end
563 or the camera library.
566 You could consider the core a translator/interpreter/spell-checker/army-general
568 \begin_inset Quotes eld
572 \begin_inset Quotes erd
576 It does the grunt-work and performs the coordination of the other parts.
579 Implementing the Library
582 gPhoto2 camera libraries use the gPhoto2 Camera API (CAPI) for implementation.
583 Here is a listing of the CAPI functions:
609 camera_file_get_preview
638 Section 3.1 details the purpose of each of these functions, while Section
639 3.2 discusses how to use the I/O library.
645 The CAPI provides the full set of functions for doing various tasks with
647 All CAPI functions return either GP_OK for succesful execution , or GP_ERROR
648 for a failure of execution
651 What follows is a listing of the functions, including prototypes and data
653 \layout Subsubsection
662 Retrieve the unique id for the camera library.
669 int camera_id (CameraText *id);
677 CameraText *id : unique string to represent the camera library
682 In order to guarantee that only once instance of the camera library is loaded
683 for each instance of the core, the camera library must copy a unique string
685 \begin_inset Quotes eld
689 \begin_inset Quotes erd
693 Please consult the gPhoto developers to determine which string you should
704 int camera_id(CameraText *id) {
708 \begin_inset Quotes eld
712 \begin_inset Quotes erd
722 \layout Subsubsection
731 Retrieve the list of supported cameras and the abilities for each camera
738 int camera_abilities (CameraAbilitiesList *list);
748 CameraAbilities *abilities : the list of abilities for the supported cameras
751 int *count : the number of
752 \layout Subsubsection
761 Initialize the camera
768 int camera_init (Camera *camera, CameraInit *init);
776 \layout Subsubsection
792 int camera_exit (Camera *camera);
800 \layout Subsubsection
809 List the files in a particular folder on the camera
816 int camera_file_list(Camera *camera, CameraList *list, char *folder);
824 \layout Subsubsection
833 List the subfolders in a particular folder on the camera
840 int camera_folder_list(Camera *camera, CameraList *list, char *folder);
849 \layout Subsubsection
858 Retrieve a file from the camera
865 int camera_file_get (Camera *camera, CameraFile *file, char *folder, char
874 \layout Subsubsection
876 camera_file_get_preview
883 Retrieve a file's preview from the camera
890 int camera_file_get_preview (Camera *camera, CameraFile *file, char *folder,
899 \layout Subsubsection
908 Place (upload) a file to the camera
915 int camera_file_put (Camera *camera, CameraFile *file, char *folder);
923 \layout Subsubsection
932 Delete a file from the camera
939 int camera_file_delete (Camera *camera, char *folder, char *filename);
947 \layout Subsubsection
956 Retrieve the configuration window.
963 int camera_config_get (Camera *camera, CameraWidget *window);
971 \layout Subsubsection
980 Set camera configuration
987 int camera_config_set (Camera *camera, CameraSetting *setting, int count);
996 \layout Subsubsection
1005 Retrieve live data from the camera
1012 int camera_capture (Camera *camera, CameraFile *file, CameraCaptureInfo
1021 \layout Subsubsection
1030 Retrieve the camera summary information
1037 int camera_summary (Camera *camera, CameraText *summary);
1045 \layout Subsubsection
1054 Retrieve the camera user's guide (manual)
1061 int camera_manual (Camera *camera, CameraText *manual);
1069 \layout Subsubsection
1078 Retrieve information about the camera library
1085 int camera_about (Camera *camera, CameraText *about);
1095 The gPhoto2 I/O Library