Debian Codegraph @

camlibs/kodak/dc210/library.c

raw
1 /* library.c
2 *
3 * Copyright (C) 2002 Michel Koltan <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <gphoto2/gphoto2.h>
29
30 #include "libgphoto2/i18n.h"
31
32 #include "dc210.h"
33 #include "library.h"
34
35
36 static int dc210_cmd_error = 0;
37 static const char ppmheader[] = "P6\n96 72\n255\n";
38
39 #ifdef DEBUG
40 static int firststatus = 1;
41 static char oldstatus[DC210_STATUS_SIZE];
42 #endif
43
44 /***************************************************************************
45
46 Global in- and output procedures
47
48 ****************************************************************************/
49
50 static void cfa2ppm (CameraFile * file) {
51
52 /* this is a very quick and dirty hack to convert cfa into ppm */
53 unsigned char buf[THUMBHEIGHT][THUMBWIDTH];
54 unsigned char rgb[THUMBHEIGHT][THUMBWIDTH][3];
55 unsigned char val;
56 unsigned int addval;
57 const char * data;
58 unsigned long datasize;
59 int i, x, y;
60
61 /* get data */
62
63 DC210_DEBUG("Converting CFA to PPM\n");
64
65 gp_file_get_data_and_size(file, &data, &datasize);
66
67 /* split 8 bit values into to 4 bit values and blow up again to 8 bit*/
68
69 i = 0;
70 for(y = 0; y < THUMBHEIGHT; y++ )
71 for(x = 0; x < THUMBWIDTH; x+=2 ){
72 val = (unsigned char) data[i] >> 4;
73 buf[y][x] = (val << 4) | val;
74 val = (unsigned char) data[i++] & 0x0F;
75 buf[y][x+1] = (val << 4) | val;
76 };
77
78 /* fill in the exact values */
79 for( y = 0; y < THUMBHEIGHT; y += 2 )
80 for( x = 0; x < THUMBWIDTH; x += 2 )
81 {
82 rgb[y][x][GREEN] = buf[y][x];
83 rgb[y][x+1][GREEN] = buf[y][x];
84 rgb[y+1][x][GREEN] = buf[y+1][x+1];
85 rgb[y+1][x+1][GREEN] = buf[y+1][x+1];
86 rgb[y][x][RED] = buf[y][x+1];
87 rgb[y][x+1][RED] = buf[y][x+1];
88 rgb[y+1][x][RED] = buf[y][x+1];
89 rgb[y+1][x+1][RED] = buf[y][x+1];
90 rgb[y][x][BLUE] = buf[y+1][x];
91 rgb[y][x+1][BLUE] = buf[y+1][x];
92 rgb[y+1][x][BLUE] = buf[y+1][x];
93 rgb[y+1][x+1][BLUE] = buf[y+1][x];
94 };
95
96 /* fill in the interpolated values, don't care about borders */
97 for ( y = 1; y < THUMBHEIGHT - 2; y+= 2)
98 for ( x = 0; x < THUMBWIDTH - 2; x +=2){
99 /* 2 missing green values */
100 addval = rgb[y][x][GREEN] + rgb[y][x+2][GREEN]
101 + rgb[y-1][x+1][GREEN] + rgb[y+1][x+1][GREEN];
102 rgb[y][x+1][GREEN] = addval / 4;
103
104 addval = rgb[y+1][x-1][GREEN] + rgb[y+1][x+1][GREEN]
105 + rgb[y][x][GREEN] + rgb[y+2][x][GREEN];
106 rgb[y+1][x][GREEN] = addval / 4;
107
108 /* 3 missing red values */
109 addval = rgb[y-1][x][RED] + rgb[y+1][x][RED];
110 rgb[y][x][RED] = addval / 2;
111
112 addval = rgb[y-1][x][RED] + rgb[y-1][x+2][RED]
113 + rgb[y+1][x][RED] + rgb[y+1][x+2][RED];
114 rgb[y][x+1][RED] = addval / 4;
115
116 addval = rgb[y+1][x][RED] + rgb[y+1][x+2][RED];
117 rgb[y+1][x+1][RED] = addval / 2;
118
119 /* 3 missing blue values */
120 addval = rgb[y][x-1][BLUE] + rgb[y][x+1][BLUE];
121 rgb[y][x][BLUE] = addval / 2;
122
123 addval = rgb[y][x-1][BLUE] + rgb[y][x+1][BLUE]
124 + rgb[y+2][x-1][BLUE] + rgb[y+2][x+1][BLUE];
125 rgb[y+1][x][BLUE] = addval / 4;
126
127 addval = rgb[y][x+1][BLUE] + rgb[y+2][x+1][BLUE];
128 rgb[y+1][x+1][BLUE] = addval / 2;
129 };
130
131 gp_file_clean(file);
132 gp_file_append(file, ppmheader, sizeof(ppmheader) - 1);
133 gp_file_append(file, (char *) rgb, THUMBWIDTH * THUMBHEIGHT * 3);
134 gp_file_set_mime_type(file, GP_MIME_PPM);
135
136 };
137
138 /* Utility procedures for command execution */
139
140 static void dc210_cmd_init (char * cmd, unsigned char command_byte) {
141
142 /* utility procedure to initialize a command string */
143
144 memset (cmd, 0, 8);
145
146 cmd[0] = command_byte;
147 cmd[7] = 0x1a;
148
149 };
150
151 static void dc210_cmd_packet_init (char * cmd_packet, const char * filename) {
152
153 /* prepare command packet */
154 memset (cmd_packet, 0x00, DC210_CMD_DATA_SIZE);
155 memset (cmd_packet + 48, 0xFF, 8);
156 strcpy(cmd_packet, "\\PCCARD\\DCIMAGES\\");
157 strcpy(cmd_packet + 17, filename);
158
159 DC210_DEBUG("Complete filename is %s\n", cmd_packet);
160
161 };
162
163 static int dc210_write_single_char (Camera *camera, unsigned char response) {
164
165 /* utility procedure to write a single character */
166
167 int i;
168
169 for (i=0; i< RETRIES; i++){
170 if (gp_port_write(camera->port, (char *)&response, 1) >= 0)
171 return (GP_OK);
172 };
173
174 return GP_ERROR;
175
176 };
177
178 static int dc210_read_single_char (Camera *camera, unsigned char * response) {
179
180 /* utility procedure to read a single character */
181
182 int i;
183 signed char error;
184
185 for (i = 0; i < RETRIES; i++){
186
187 error = gp_port_read(camera->port, (char *)response, 1);
188
189 if (error < 0){
190 if (error == GP_ERROR_TIMEOUT)
191 continue;
192 else{
193 DC210_DEBUG("Real bad error reading single character. Errornumber: %d\n", error);
194 return GP_ERROR;
195 };
196 };
197
198 return GP_OK;
199
200 };
201
202 return GP_ERROR_TIMEOUT;
203
204 };
205
206 /* Command execution */
207
208 static int dc210_execute_command (Camera *camera, char *cmd) {
209
210 /* This procedure sends a command to the camera and
211 waits for acknowledgement. Calling procedures have
212 to check the returncode; if the command succeeded
213 there are three possible follow ups:
214 - Read a DC210_COMMAND_COMPLETE in case of
215 simple commands
216 - Send an additional command package in case of
217 sophisticated commands
218 - Read data from the camera
219 But all this depends on the command and should not
220 be mixed in this procedure. */
221
222 int i,k ;
223 unsigned char response;
224 signed char error;
225
226 dc210_cmd_error = DC210_CMD_OKAY;
227
228 for (i = 0; i < RETRIES; i++){
229
230 /* write command */
231 if (gp_port_write(camera->port, cmd, 8) < 0){
232 DC210_DEBUG("Could not write to port.\n");
233 dc210_cmd_error = DC210_WRITE_ERROR;
234 continue; /* try again */
235 };
236
237 for (k = 0; k < RETRIES; k++){
238 error = gp_port_read(camera->port, (char *)&response, 1);
239 if (error != 1){
240 if (error == GP_ERROR_TIMEOUT){
241 dc210_cmd_error = DC210_TIMEOUT_ERROR;
242 continue; /* wait a little bit longer */
243 }
244 else{
245 DC210_DEBUG("Real bad error reading answer. Errornumber: %d\n", error);
246 dc210_cmd_error = DC210_READ_ERROR;
247 return error;
248 };
249 };
250
251 switch (response){
252 case DC210_COMMAND_ACK:
253 DC210_DEBUG("Command 0x%02X acknowledged.\n", (unsigned char) cmd[0]);
254 return GP_OK; break; /* that's fine, just what we expected */
255 case DC210_COMMAND_NAK:
256 DC210_DEBUG("Sorry, but the camera seems not to understand the command 0x%02X\n", (unsigned char) cmd[0]);
257 dc210_cmd_error = DC210_NAK_ERROR;
258 break; /* anyway, send the command again */
259 default:
260 DC210_DEBUG("Strange. Unexpected response for command 0x%02X: 0x%02X\n", (unsigned char) cmd[0], response);
261 dc210_cmd_error = DC210_GARBAGE_ERROR;
262 return GP_ERROR;
263 break;
264 };
265 break;
266 };
267
268 };
269
270 /* command does definitely not work */
271
272 DC210_DEBUG("Command definitely didn't work.\n");
273
274 return GP_ERROR;
275
276 };
277
278 /* appropriate actions to a command */
279
280 static int dc210_write_command_packet (Camera * camera, char * data) {
281
282 unsigned char checksum;
283 int i, error;
284 unsigned char answer;
285
286 /* calculating checksum */
287 checksum = 0;
288 for (i = 0; i < DC210_CMD_DATA_SIZE; i ++){
289 checksum ^= data[i];
290 };
291
292 for (i = 0; i < RETRIES; i++){
293
294 /* write the startbyte */
295 dc210_write_single_char(camera, DC210_CMD_PACKET_FOLLOWING);
296
297 /* write packet */
298 gp_port_write(camera->port, data, DC210_CMD_DATA_SIZE);
299
300 /* write checksum */
301 dc210_write_single_char(camera, checksum);
302
303 /* read answer */
304
305 error = gp_port_read(camera->port, (char *)&answer, 1);
306
307 if (error < 0) return GP_ERROR;
308
309 if (answer == DC210_CORRECT_PACKET) return GP_OK;
310
311 if (answer != DC210_ILLEGAL_PACKET) {
312 DC210_DEBUG("Strange answer to command packet: 0x%02X.\n", answer);
313 return GP_ERROR;
314 };
315
316 };
317
318 DC210_DEBUG("Could not send command packet.\n");
319
320 return GP_ERROR;
321
322 };
323
324 static int dc210_wait_for_response (Camera *camera, int expect_busy, GPContext *context) {
325
326 /* Waits for a command to finish.
327 Expects either a timeout, a DC210_BUSY,
328 a DC210_COMMAND_COMPLETE or a
329 DC210_PACKET_FOLLOWING; all other answers
330 are considered errors */
331
332 unsigned char response;
333 int error = 0;
334 int counter = 0;
335 int progress_id = 0;
336
337 if (context)
338 progress_id = gp_context_progress_start (context, expect_busy, _("Waiting..."));
339
340 while (1) {
341
342 error = dc210_read_single_char(camera, &response);
343 if (error < 0) {
344 if (context)
345 gp_context_progress_stop (context, progress_id);
346 return error;
347 };
348
349 switch (response) {
350 case DC210_BUSY:
351 /* wait a little bit longer */
352 if (context && counter <= expect_busy)
353 gp_context_progress_update (context, progress_id, counter++);
354 break;
355 case DC210_COMMAND_COMPLETE:
356 case DC210_PACKET_FOLLOWING:
357 if (context)
358 gp_context_progress_stop (context, progress_id);
359 return response;
360 break;
361 default:
362 if (context)
363 gp_context_progress_stop (context, progress_id);
364 DC210_DEBUG("Command terminated with errorcode 0x%02X.\n", response);
365 return GP_ERROR;
366 };
367 };
368
369 };
370
371 static int dc210_read_single_block (Camera *camera, unsigned char * b, int blocksize) {
372
373 int i, k, error;
374 char cs_read, cs_computed;
375
376 for (i = 0; i < RETRIES; i++) {
377
378 if (dc210_wait_for_response(camera, 0, NULL) != DC210_PACKET_FOLLOWING)
379 return GP_ERROR;
380
381 error = 1;
382 for (k = 0; k < RETRIES; k++) {
383 if (gp_port_read(camera->port, (char *)b, blocksize) < 0) {
384 continue;
385 };
386 error = 0;
387 break;
388 };
389
390 if (error) return GP_ERROR;
391
392 if (dc210_read_single_char(camera, (unsigned char *)&cs_read) < 0)
393 return GP_ERROR;
394
395 cs_computed = 0;
396 for (k = 0; k < blocksize; k++)
397 cs_computed ^= b[k];
398 if (cs_computed == cs_read) {
399 dc210_write_single_char(camera, DC210_CORRECT_PACKET);
400 return GP_OK;
401 };
402
403 dc210_write_single_char(camera, DC210_ILLEGAL_PACKET);
404
405 };
406
407 return GP_ERROR;
408
409 };
410
411 static int dc210_read_to_file (Camera *camera, CameraFile * f, int blocksize, long int expectsize, GPContext *context) {
412
413 int packets, k, l, fatal_error, packet_following;
414 unsigned int progress_id = 0;
415 unsigned char cs_read, cs_computed;
416 unsigned char * b;
417
418 int blocks = expectsize / blocksize;
419 int remaining = expectsize % blocksize;
420
421 if (NULL == (b = malloc(blocksize))) return GP_ERROR;
422
423 if (remaining) blocks++;
424
425 if (context)
426 progress_id = gp_context_progress_start (context, blocks, _("Getting data..."));
427
428 fatal_error = 0;
429 packets = 0;
430
431 packet_following = dc210_wait_for_response(camera, 0, NULL);
432 while (DC210_PACKET_FOLLOWING == packet_following) {
433 fatal_error = 1;
434 for (k = 0; k < RETRIES; k++) {
435 /* read packet */
436 if (gp_port_read(camera->port, (char *)b, blocksize) < 0) {
437 dc210_write_single_char(camera, DC210_ILLEGAL_PACKET);
438 packet_following = dc210_wait_for_response(camera, 0, NULL);
439 continue;
440 };
441 /* read checksum */
442 if (dc210_read_single_char(camera, &cs_read) == GP_ERROR) {
443 free(b);
444 return GP_ERROR;
445 };
446 /* test checksum */
447 cs_computed = 0;
448 for (l = 0; l < blocksize; l++)
449 cs_computed ^= b[l];
450 if (cs_computed != cs_read) {
451 dc210_write_single_char(camera, DC210_ILLEGAL_PACKET);
452 packet_following = dc210_wait_for_response(camera, 0, NULL);
453 continue;
454 };
455 /* append to file */
456 if (packets == blocks - 1 && remaining)
457 gp_file_append(f, (char *)b, remaining);
458 else
459 gp_file_append(f, (char *)b, blocksize);
460 /* request next packet */
461 dc210_write_single_char(camera, DC210_CORRECT_PACKET);
462 packet_following = dc210_wait_for_response(camera, 0, NULL);
463 fatal_error = 0;
464 if (context)
465 gp_context_progress_update (context, progress_id, packets);
466 packets++;
467 break;
468 };
469 if (fatal_error) break;
470 };
471
472 if (packet_following < 0)
473 fatal_error = 1;
474
475 if (context)
476 gp_context_progress_stop (context, progress_id);
477
478 free(b);
479
480 if (fatal_error) return GP_ERROR;
481
482 return GP_OK;
483
484 };
485
486 /***************************************************************************
487
488 Working procedures
489
490 ****************************************************************************/
491
492 /***** Set procedures *****/
493
494 static int dc210_set_option (Camera * camera, char command, unsigned int value, int valuesize) {
495
496 char cmd[8];
497 dc210_cmd_init(cmd, command);
498
499 switch(valuesize){
500 case 0:
501 break;
502 case 1:
503 cmd[2] = value & 0xFF;
504 break;
505 case 2:
506 cmd[3] = value & 0xFF;
507 cmd[2] = (value >> 8) & 0xFF;
508 break;
509 case 4:
510 cmd[5] = value & 0xFF;
511 cmd[4] = (value >> 8) & 0xFF;
512 cmd[3] = (value >> 16) & 0xFF;
513 cmd[2] = (value >> 24) & 0xFF;
514 break;
515 default:
516 return GP_ERROR;
517 };
518
519 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
520
521 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
522
523 return GP_OK;
524
525 };
526
527 int dc210_set_compression (Camera * camera, dc210_compression_type compression) {
528
529 return dc210_set_option(camera, DC210_SET_QUALITY, compression, 1);
530
531 };
532
533 int dc210_set_flash (Camera * camera, dc210_flash_type flash, char preflash) {
534
535 if (flash != DC210_FLASH_NONE && preflash) {
536 return dc210_set_option(camera, DC210_SET_FLASH, flash + 3, 1);
537 }
538 else
539 return dc210_set_option(camera, DC210_SET_FLASH, flash, 1);
540
541 };
542
543 int dc210_set_zoom (Camera * camera, dc210_zoom_type zoom) {
544
545 return dc210_set_option(camera, DC210_SET_ZOOM, zoom, 1);
546
547 };
548
549 int dc210_set_file_type (Camera * camera, dc210_file_type_type file_type) {
550
551 return dc210_set_option(camera, DC210_SET_FILE_TYPE, file_type, 1);
552
553 };
554
555 int dc210_set_resolution (Camera * camera, dc210_resolution_type res) {
556
557 return dc210_set_option(camera, DC210_SET_RESOLUTION, res, 1);
558
559 };
560
561 int dc210_set_delay (Camera * camera) {
562
563 /* This function works, but I do no have the slightest idea,
564 what the effect is / should be.
565 There is *no* delay, when you execute this function before
566 capturing. */
567
568 return dc210_set_option(camera, DC210_SET_DELAY, 10, 4);
569
570 };
571
572 int dc210_set_exp_compensation (Camera * camera, signed int compensation) {
573
574 unsigned char param = abs(compensation);
575
576 if (compensation < 0) param |= 0x80;
577
578 return dc210_set_option(camera, DC210_SET_EXPOSURE, param, 1);
579
580 };
581
582 int dc210_system_time_callback (Camera * camera, CameraWidget * widget, GPContext * context) {
583
584 return dc210_set_option(camera, DC210_SET_TIME, (time(NULL) - CAMERA_SET_EPOC) << 1, 4);
585
586 };
587
588 static int dc210_format_card (Camera * camera, char * album_name, GPContext * context) {
589
590 char data[DC210_CMD_DATA_SIZE];
591 char cmd[8];
592 char * subst;
593 unsigned char answer[16];
594 unsigned char checksum_read, checksum;
595 int i;
596
597 memset (data, 0, DC210_CMD_DATA_SIZE);
598
599 /* Make the album name acceptable - we need at least 8 characters
600 which are not blank and there are no blanks allowed before the
601 last character.
602 */
603 if (album_name != NULL && strlen(album_name) > 0) {
604 strncpy(data, album_name, 11);
605 /* substitute blanks in the first 8 characters*/
606 while (NULL != (subst = strchr(data, ' '))) *subst = '_';
607 if (strlen(data) < 8) strncat(data, "________", 8 - strlen(data));
608 };
609
610 DC210_DEBUG("Album name is '%s'\n", data);
611
612 dc210_cmd_init(cmd, DC210_CARD_FORMAT);
613
614 dc210_execute_command(camera, cmd);
615 dc210_write_command_packet(camera, data);
616 if (dc210_wait_for_response(camera, 3, context) != DC210_PACKET_FOLLOWING) return GP_ERROR;
617
618 gp_port_read(camera->port, (char *)answer, 16);
619 gp_port_read(camera->port, (char *)&checksum_read, 1);
620 checksum = 0;
621
622 for (i = 0; i < 16; i++) checksum ^= answer[i];
623 if (checksum_read != checksum) return GP_ERROR;
624
625 DC210_DEBUG("Flash card formatted.\n");
626
627 if (dc210_write_single_char(camera, DC210_CORRECT_PACKET) == GP_ERROR) return GP_ERROR;
628 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
629
630 gp_filesystem_reset(camera->fs);
631
632 return GP_OK;
633
634 };
635
636 static int dc210_get_card_status (Camera * camera, dc210_card_status * card_status) {
637
638 char cmd[8];
639 unsigned char answer[16];
640 unsigned char checksum_read, checksum;
641 int i;
642
643 card_status->open = 0;
644 card_status->program = 0;
645 card_status->space = 0;
646
647 dc210_cmd_init(cmd, DC210_CARD_STATUS);
648 dc210_execute_command(camera, cmd);
649 if (dc210_wait_for_response(camera, 0, NULL) != DC210_PACKET_FOLLOWING) return GP_ERROR;
650
651 gp_port_read(camera->port, (char *)answer, 16);
652 gp_port_read(camera->port, (char *)&checksum_read, 1);
653
654 checksum = 0;
655 for (i = 0; i < 16; i++) checksum ^= answer[i];
656
657 if (checksum_read != checksum)
658 DC210_DEBUG("Error reading card status.\n");
659 else
660 DC210_DEBUG("Card status correctly read.\n");
661
662 if (answer[0] & 0x08) card_status->open = 1;
663 DC210_DEBUG("Card status open is %d\n", card_status->open);
664 card_status->program = answer[1] * 0x100 + answer[2];
665 /* set the space in kb */
666 card_status->space = (answer[3] * 0x1000000 + answer[4] * 0x10000 + answer[5] * 0x100 + answer[6]) / 1024;
667
668 if (dc210_write_single_char(camera, DC210_CORRECT_PACKET) == GP_ERROR) return GP_ERROR;
669 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
670
671 return GP_OK;
672
673 };
674
675 int dc210_format_callback(Camera * camera, CameraWidget * widget, GPContext * context) {
676
677 CameraWidget * window;
678 char * album_name;
679
680 gp_widget_get_root(widget, &window);
681
682 gp_widget_get_child_by_label (window, _("Album name"), &widget);
683 gp_widget_get_value (widget, &album_name);
684
685 return dc210_format_card(camera, album_name, context);
686
687 };
688
689 static int dc210_check_battery (Camera *camera) {
690
691 char cmd[8];
692 dc210_cmd_init(cmd, DC210_CHECK_BATTERY);
693
694 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
695 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
696
697 return GP_OK;
698
699 };
700
701 int dc210_init_port (Camera *camera) {
702
703 /* sending a break command resets the
704 speed to 9600 */
705 int camera_speeds[] = {115200, 19200, 38400, 57600};
706 int i, desired_speed;
707 GPPortSettings settings;
708
709 gp_port_get_settings (camera->port, &settings);
710 gp_port_set_timeout (camera->port, TIMEOUT);
711 settings.serial.bits = 8;
712 settings.serial.parity = 0;
713 settings.serial.stopbits = 1;
714
715 desired_speed = settings.serial.speed;
716 if (! desired_speed) desired_speed = 115200;
717
718 DC210_DEBUG("Desired port speed is %d.\n", desired_speed);
719
720 /* only set the speed if nothing was specified on the command line */
721 if (! settings.serial.speed) settings.serial.speed = 9600;
722 gp_port_set_settings (camera->port, settings);
723
724 /* ok, run a dummy command; if the camera was off,
725 this will activate it and set the speed to 9600;
726 but the command will return an error;
727 It makes no sense to shorten the timeout time,
728 because the camera may need up to 10 seconds (information
729 of the manual) to startup */
730 if (dc210_check_battery(camera) == GP_OK) return GP_OK;
731
732 /* Fine, the last command didn't work, but we are now
733 sure, the camera is working, if it is connected
734 and has enough power.
735 This is the time to send a break to reset the speed
736 to 9600 */
737
738 gp_camera_set_port_speed(camera, 9600);
739 gp_port_send_break(camera->port, 300);
740 usleep(300 * 1000);
741
742 /* Excellent. Now our dummy command should work */
743
744 if (dc210_check_battery(camera) == GP_OK){
745 return dc210_set_speed(camera, desired_speed);
746 };
747
748 /* My, my, this is not nice. We change the timeout
749 and try other speeds as last resort */
750
751 gp_port_set_timeout (camera->port, 100);
752
753 for (i = 0; i < 4; i++){
754 settings.serial.speed = camera_speeds[i];
755 gp_port_set_settings (camera->port, settings);
756
757 if (dc210_check_battery(camera) == GP_OK){
758 gp_port_set_timeout (camera->port, TIMEOUT);
759 return dc210_set_speed(camera, desired_speed);
760 };
761
762 DC210_DEBUG("What a pity. Speed %d does not work.\n", camera_speeds[i]);
763 };
764
765 /* reset the timeout */
766 gp_port_set_timeout (camera->port, TIMEOUT);
767
768 return GP_ERROR;
769
770 };
771
772 int dc210_set_speed (Camera *camera, int speed) {
773
774 unsigned char cmd[8];
775 GPPortSettings settings;
776
777 dc210_cmd_init((char*)cmd, DC210_SET_SPEED);
778
779 switch (speed) {
780 case 9600:
781 cmd[2] = 0x96; cmd[3] = 0x00; break;
782 case 19200:
783 cmd[2] = 0x19; cmd[3] = 0x20; break;
784 case 38400:
785 cmd[2] = 0x38; cmd[3] = 0x40; break;
786 case 57600:
787 cmd[2] = 0x57; cmd[3] = 0x60; break;
788 case 115200:
789 cmd[2] = 0x11; cmd[3] = 0x52; break;
790 default:
791 return (GP_ERROR);
792 };
793
794 if (dc210_execute_command(camera, (char*)cmd) == GP_ERROR) return GP_ERROR;
795
796 gp_port_get_settings (camera->port, &settings);
797 settings.serial.speed = speed;
798 gp_port_set_settings (camera->port, settings);
799
800 DC210_DEBUG("Port speed set to %d.\n", speed);
801
802 return GP_OK;
803
804 };
805
806 /****** Picture actions *****/
807
808 int dc210_take_picture (Camera * camera, GPContext * context)
809 {
810 char cmd[8];
811 dc210_cmd_init(cmd, DC210_TAKE_PICTURE);
812
813 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
814 switch (dc210_wait_for_response(camera, 5, context)) {
815 /* we allow a timeout error here and check the result via status */
816 case DC210_COMMAND_COMPLETE:
817 case GP_ERROR_TIMEOUT: return GP_OK; break;
818 default: return GP_ERROR; break;
819 };
820
821 return GP_ERROR;
822
823 };
824
825 int dc210_delete_picture (Camera * camera, unsigned int picno) {
826
827 char cmd[8];
828 unsigned int pic_offset = picno - 1;
829
830 dc210_cmd_init(cmd, DC210_DELETE_PICTURE);
831 cmd[3] = pic_offset & 0xFF;
832 cmd[2] = (pic_offset >> 8) & 0xFF;
833
834 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
835 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
836
837 return GP_OK;
838
839 };
840
841 int dc210_delete_last_picture (Camera * camera) {
842
843 dc210_status status;
844
845 if (dc210_get_status(camera, &status) == GP_ERROR)
846 return GP_ERROR;
847
848 if (0 == status.numPicturesInCamera)
849 return GP_ERROR;
850
851 return dc210_delete_picture(camera, status.numPicturesInCamera);
852
853 };
854
855 int dc210_delete_picture_by_name (Camera * camera, const char * filename ) {
856
857 char cmd[8];
858 char cmd_packet[DC210_CMD_DATA_SIZE];
859
860 dc210_cmd_init(cmd, DC210_CARD_FILE_DEL);
861 dc210_cmd_packet_init(cmd_packet, filename);
862
863 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
864 if (dc210_write_command_packet(camera, cmd_packet) == GP_ERROR) return GP_ERROR;
865 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
866
867 return GP_OK;
868 };
869
870 int dc210_download_last_picture (Camera * camera, CameraFile *file, dc210_picture_type type, GPContext *context) {
871
872 dc210_status status;
873 dc210_picture_info picinfo;
874
875 if (dc210_get_status(camera, &status) == GP_ERROR) return GP_ERROR;
876 if (status.numPicturesInCamera == 0) return GP_ERROR;
877
878 if (dc210_get_picture_info(camera, &picinfo, status.numPicturesInCamera) == GP_ERROR)
879 return GP_ERROR;
880
881 if (dc210_get_picture_info(camera, &picinfo, status.numPicturesInCamera) == GP_ERROR)
882 return GP_ERROR;
883
884 return dc210_download_picture_by_name(camera, file, picinfo.image_name, type, context);
885
886 };
887
888 int dc210_open_card (Camera * camera) {
889
890 char cmd[8];
891 dc210_card_status card_status;
892
893 /* check if card is already opened */
894 dc210_get_card_status(camera, &card_status);
895 if (card_status.open) return GP_OK;
896
897 dc210_cmd_init(cmd, DC210_OPEN_CARD);
898
899 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
900 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
901
902 return GP_OK;
903 };
904
905 #if 0 /* This function is not used by anybody */
906 static int dc210_close_card
907 (Camera * camera)
908 {
909
910 char cmd[8];
911 dc210_cmd_init(cmd, DC210_CLOSE_CARD);
912
913 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
914 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
915
916 return GP_OK;
917 };
918 #endif
919
920 int dc210_download_picture_by_name (Camera * camera, CameraFile *file, const char *filename, dc210_picture_type type, GPContext *context) {
921
922 char cmd[8];
923 char cmd_packet[DC210_CMD_DATA_SIZE];
924 dc210_picture_info picinfo;
925
926 /* prepare command */
927 if (type == DC210_FULL_PICTURE){
928 /* we need the file size for the progress bar */
929 if (dc210_get_picture_info_by_name(camera, &picinfo, filename) == GP_ERROR)
930 return GP_ERROR;
931 DC210_DEBUG("Picture size is %d\n", picinfo.picture_size);
932 dc210_cmd_init(cmd, DC210_CARD_READ_FILE);
933 }
934 else
935 dc210_cmd_init(cmd, DC210_CARD_READ_THUMB);
936
937 if (type == DC210_RGB_THUMB) cmd[4] = 1;
938
939 dc210_cmd_packet_init(cmd_packet, filename);
940
941 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
942 if (dc210_write_command_packet(camera, cmd_packet) == GP_ERROR) return GP_ERROR;
943
944 switch (type){
945 case DC210_FULL_PICTURE:
946 if (picinfo.file_type == DC210_FILE_TYPE_JPEG)
947 gp_file_set_mime_type(file, GP_MIME_JPEG);
948 if (dc210_read_to_file(camera, file,
949 DC210_CARD_BLOCK_SIZE,
950 picinfo.picture_size, context) == GP_ERROR)
951 return GP_ERROR;
952 break;
953 case DC210_CFA_THUMB:
954 /* this thumb type is much faster to download, because it has just
955 a 6th of the size of rgb data */
956 if (dc210_read_to_file(camera, file,
957 DC210_DOWNLOAD_BLOCKSIZE, 48 * 72, NULL) == GP_ERROR)
958 return GP_ERROR;
959 /* convert cfa data to rgb: */
960 cfa2ppm(file);
961 break;
962 break;
963 case DC210_RGB_THUMB:
964 /* if you really need nice thumbs, you can use this, but I don't think it's
965 worth the 6 times longer download time */
966 gp_file_set_mime_type(file, GP_MIME_PPM);
967 gp_file_append(file, ppmheader, sizeof(ppmheader) - 1);
968 if (dc210_read_to_file(camera, file,
969 DC210_DOWNLOAD_BLOCKSIZE, 96 * 72 * 3, NULL) == GP_ERROR)
970 return GP_ERROR;
971 break;
972 };
973
974 return GP_OK;
975 };
976
977 int dc210_capture (Camera *camera, CameraFilePath *path, GPContext *context)
978 {
979
980 dc210_status status;
981 dc210_picture_info picinfo;
982 int pictures_in_camera;
983
984 /* dc210_take_picture no longer returns an error on timeout; check
985 the working of the command via status */
986
987 if (dc210_get_status(camera, &status) == GP_ERROR) return GP_ERROR;
988 pictures_in_camera = status.numPicturesInCamera;
989
990 if (dc210_take_picture(camera, context) == GP_ERROR) return GP_ERROR;
991
992 if (dc210_get_status(camera, &status) == GP_ERROR) return GP_ERROR;
993 if (pictures_in_camera == status.numPicturesInCamera) return GP_ERROR;
994
995 if (dc210_get_picture_info(camera, &picinfo, status.numPicturesInCamera) == GP_ERROR) return GP_ERROR;
996
997 strcpy(path->folder, "/");
998 strcpy(path->name, picinfo.image_name);
999
1000 return GP_OK;
1001
1002 };
1003
1004 /***** Retrieve information *****/
1005
1006 int dc210_get_picture_number (Camera *camera, const char * filename){
1007
1008 /* This function is terrible slow if you have a lot of pictures
1009 in the camera; therefor it should not be used */
1010
1011 dc210_status status;
1012 dc210_picture_info picinfo;
1013 int i;
1014
1015 if (dc210_get_status(camera, &status) == GP_ERROR) return -1;
1016
1017 for (i = 1; i <= status.numPicturesInCamera; i++){
1018
1019 if (dc210_get_picture_info(camera, &picinfo, i) == GP_ERROR) return -1;
1020
1021 if (strcmp(picinfo.image_name, filename) == 0){
1022 return i;
1023 };
1024
1025 };
1026
1027 return -1;
1028
1029 };
1030
1031 int dc210_get_filenames (Camera *camera, CameraList *list, GPContext *context)
1032 {
1033 char cmd[8];
1034 CameraFile *file;
1035 char filename[13];
1036 const char * data;
1037 unsigned long datasize;
1038 int pictures, i;
1039
1040 gp_file_new(&file);
1041
1042 dc210_cmd_init(cmd, DC210_GET_ALBUM_FILENAMES);
1043
1044 dc210_execute_command(camera, cmd);
1045
1046 dc210_read_to_file(camera, file, DC210_DIRLIST_SIZE, 0, NULL);
1047
1048 gp_file_get_data_and_size(file, &data, &datasize);
1049
1050 pictures = (unsigned char) data[0] * 0x100 + (unsigned char) data[1];
1051 DC210_DEBUG("There are %d pictures in the camera\n", pictures);
1052
1053 filename[8] = '.';
1054 filename[12] = '\0';
1055 for (i = 0; i < pictures; i++){
1056 strncpy(filename, data + 2 + i*20, 8);
1057 strncpy(filename + 9, data + 10 + i*20, 3);
1058 DC210_DEBUG("Adding filename %s to list\n", filename);
1059 gp_list_append(list, filename, NULL);
1060 };
1061
1062 gp_file_free(file);
1063
1064 return GP_OK;
1065
1066 };
1067
1068 static void dc210_picinfo_from_block (dc210_picture_info * picinfo, unsigned char * data){
1069
1070 picinfo->camera_type = data[1];
1071 picinfo->file_type = data[2];
1072 picinfo->resolution = data[3];
1073 picinfo->compression = data[4];
1074 picinfo->picture_number = data[6] * 0x100 + data[7];
1075 picinfo->picture_size = data[8] * 0x1000000 + data[9] * 0x10000 + data[10] * 0x100 + data[11];
1076 picinfo->preview_size = 96 * 72 * 3 + sizeof(ppmheader);
1077 picinfo->picture_time = CAMERA_GET_EPOC + ((data[12] * 0x1000000 +
1078 data[13] * 0x10000 +
1079 data[14] * 0x100 +
1080 data[15]) >> 1);
1081 picinfo->flash_used = data[16];
1082 picinfo->flash = data[17];
1083 picinfo->preflash = (data[17] > 2);
1084 if (picinfo->preflash) picinfo->flash -= 3;
1085 picinfo->zoom = data[21];
1086 picinfo->f_number = data[26];
1087 picinfo->battery = data[27];
1088 picinfo->exposure_time = data[28] * 0x1000000 + data[29] * 0x10000 + data[30] * 0x100 + data[31];
1089 strncpy(picinfo->image_name, (char *)&data[32], 12);
1090 picinfo->image_name[12] = 0;
1091
1092 }
1093
1094 int dc210_get_picture_info (Camera *camera, dc210_picture_info *picinfo, unsigned int picno) {
1095
1096 char cmd[8];
1097 unsigned char data[DC210_PICINFO_SIZE];
1098 unsigned int pic_offset = picno - 1;
1099
1100 dc210_cmd_init(cmd, DC210_GET_PICINFO);
1101 cmd[3] = pic_offset & 0xFF;
1102 cmd[2] = (pic_offset >> 8) & 0xFF;
1103
1104 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
1105 if (dc210_read_single_block(camera, data, DC210_PICINFO_SIZE) == GP_ERROR) return GP_ERROR;
1106 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
1107
1108 dc210_picinfo_from_block(picinfo, data);
1109
1110 return GP_OK;
1111
1112 };
1113
1114 int dc210_get_picture_info_by_name (Camera *camera, dc210_picture_info *picinfo, const char * filename) {
1115
1116 char cmd[8];
1117 char cmd_packet[DC210_CMD_DATA_SIZE];
1118 unsigned char data[DC210_CARD_BLOCK_SIZE];
1119
1120 dc210_cmd_init(cmd, DC210_CARD_GET_PICINFO);
1121 dc210_cmd_packet_init(cmd_packet, filename);
1122
1123 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
1124 if (dc210_write_command_packet(camera, cmd_packet) == GP_ERROR) return GP_ERROR;
1125 if (dc210_read_single_block(camera, data, DC210_CARD_BLOCK_SIZE) == GP_ERROR) return GP_ERROR;
1126 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
1127
1128 dc210_picinfo_from_block(picinfo, data);
1129
1130 return GP_OK;
1131
1132 };
1133
1134 int dc210_get_status (Camera *camera, dc210_status *status) {
1135
1136 #ifdef DEBUG
1137 int i;
1138 #endif
1139
1140 char data[DC210_STATUS_SIZE];
1141 char cmd[8];
1142
1143 /* you have to check the battery status explicitly
1144 before reading the status table*/
1145 dc210_check_battery(camera);
1146
1147 /* check the card */
1148 dc210_get_card_status(camera, &(status->card_status));
1149
1150 dc210_cmd_init(cmd, DC210_GET_STATUS);
1151
1152 if (dc210_execute_command(camera, cmd) == GP_ERROR) return GP_ERROR;
1153 if (dc210_read_single_block(camera, (unsigned char *)data, DC210_STATUS_SIZE) == GP_ERROR) return GP_ERROR;
1154 if (dc210_wait_for_response(camera, 0, NULL) != DC210_COMMAND_COMPLETE) return GP_ERROR;
1155
1156 #ifdef DEBUG
1157 if (firststatus){
1158 firststatus = 0;
1159 }
1160 else{
1161 for (i = 0; i < DC210_STATUS_SIZE; i++){
1162 if (oldstatus[i] != data[i] && (i < 12 || i > 15)){
1163 DC210_DEBUG("Statusdata differs at offset %03d (old: 0x%02X, new: 0x%02X)\n",
1164 i, (unsigned char) oldstatus[i], (unsigned char) data[i]);
1165 };
1166 };
1167 };
1168 memcpy(oldstatus, data, DC210_STATUS_SIZE);
1169 #endif
1170
1171 status->firmwareMajor = data[2];
1172 status->firmwareMinor = data[3];
1173 status->battery = data[8];
1174 status->acstatus = data[9];
1175
1176 /* seconds since unix epoc */
1177 status->time = CAMERA_GET_EPOC + ((unsigned char) data[12] * 0x1000000 +
1178 (unsigned char) data[13] * 0x10000 +
1179 (unsigned char) data[14] * 0x100 +
1180 (unsigned char) data[15]) / 2;
1181
1182 status->zoom = data[16];
1183 status->compression_type = data[19];
1184 status->flash = data[20];
1185 status->exp_compensation = data[21] & 0x7F;
1186 if (data[21] & 0x80) status->exp_compensation *= -1;
1187 status->preflash = (data[20] > 2);
1188 if (status->preflash) status->flash -= 3;
1189 status->resolution = data[22];
1190 status->file_type = data[23];
1191 status->totalPicturesTaken = (unsigned char) data[25] * 0x100 + (unsigned char) data[26];
1192 status->totalFlashesFired = (unsigned char) data[27] * 0x100 + (unsigned char) data[28];
1193 status->numPicturesInCamera = (unsigned char) data[56] * 0x100 + (unsigned char) data[57];
1194 status->remainingLow = (unsigned char) data[68] * 0x100 + (unsigned char) data[69];
1195 status->remainingMedium = (unsigned char) data[70] * 0x100 + (unsigned char) data[71];
1196 status->remainingHigh = (unsigned char) data[72] * 0x100 + (unsigned char) data[73];
1197 strncpy(status->album_name, &data[77], 11);
1198 status->album_name[11] = 0;
1199
1200 return GP_OK;
1201
1202 }
1203
1204 /* Debugging stuff, not really needed in the driver */
1205
1206 #ifdef DEBUG
1207
1208 static int dc210_read_dummy_packet(Camera * camera){
1209
1210 int i;
1211 signed char error;
1212 unsigned char answer, lastanswer, checksum;
1213
1214 checksum = 0;
1215 lastanswer = 0;
1216 for ( i = 0; i < 2048; i++){
1217
1218 error = gp_port_read(camera->port, &answer, 1);
1219
1220 if (error < 0){
1221 DC210_DEBUG("Packet read exhausted. Packet size: %d\n", i-1);
1222 if (checksum == lastanswer)
1223 DC210_DEBUG("Checksum is okay. Sending okay.\n");
1224 else
1225 DC210_DEBUG("Checksum is not okay. Sending correct packet anyway.\n");
1226 answer = DC210_CORRECT_PACKET;
1227 gp_port_write(camera->port, &answer, 1);
1228 return GP_OK;
1229 };
1230 DC210_DEBUG(" Byte %03d: 0x%02X", i, answer);
1231 if (answer > 32)
1232 DC210_DEBUG(" (%c)", answer);
1233 DC210_DEBUG("\n");
1234 checksum ^= lastanswer;
1235 lastanswer = answer;
1236 };
1237
1238 DC210_DEBUG("Packet too long. Sending cancel.\n");
1239 answer = DC210_CANCEL;
1240 gp_port_write(camera->port, &answer, 1);
1241
1242 return GP_OK;
1243
1244 };
1245
1246 static int dc210_test_command
1247 (Camera * camera, unsigned char cmdbyte, unsigned char *databytes){
1248
1249 /* this is a debug command to test the behaviour
1250 of certain camera commands */
1251 unsigned char cmd[8];
1252 unsigned char answer;
1253 signed char error;
1254 int packetcount = 0;
1255 char dummy_cmd_packet[DC210_CMD_DATA_SIZE];
1256
1257 memset (dummy_cmd_packet, 0, DC210_CMD_DATA_SIZE);
1258
1259 dc210_cmd_init(cmd, cmdbyte);
1260
1261 memcpy(cmd + 2, databytes, 4);
1262
1263 /* okay, write the command and wait for acknowledge */
1264
1265 gp_port_write(camera->port, cmd, 8);
1266
1267 error = gp_port_read(camera->port, &answer, 1);
1268
1269 if (error < 0){
1270 DC210_DEBUG("Command 0x%02X produced port read error %d",
1271 cmdbyte, error);
1272 return GP_OK;
1273 };
1274
1275 switch (answer){
1276 case DC210_COMMAND_ACK:
1277 DC210_DEBUG("Okay, command 0x%02X acknowledged.\n", cmdbyte);
1278 break;
1279 case DC210_COMMAND_NAK:
1280 DC210_DEBUG("Sorry, command 0x%02X not acknowledged.\n", cmdbyte);
1281 return GP_OK;
1282 default:
1283 DC210_DEBUG("Unexpected response 0x%02X to command 0x%02X.\n",
1284 answer, cmdbyte);
1285 return GP_OK;
1286 };
1287
1288 wait_finish:
1289 error = gp_port_read(camera->port, &answer, 1);
1290
1291 if (error < 0){
1292 DC210_DEBUG("Command 0x%02X produced port read error %d\n",
1293 cmdbyte, error);
1294 DC210_DEBUG("Probably the command expects a command package.\n");
1295 DC210_DEBUG("Sending one...\n");
1296 dc210_write_command_packet(camera, dummy_cmd_packet);
1297 goto wait_finish;
1298 };
1299
1300 switch(answer){
1301 case DC210_COMMAND_COMPLETE:
1302 DC210_DEBUG("Okay. Command has been accepted and executed.\n");
1303 return GP_OK;
1304 case DC210_EXECUTION_ERROR:
1305 DC210_DEBUG("Sorry. An error occurred while executing command.\n");
1306 return GP_OK;
1307 case DC210_CORRECT_PACKET:
1308 DC210_DEBUG("Okay. Packet successfully received.\n");
1309 goto wait_finish;
1310 case DC210_PACKET_FOLLOWING:
1311 DC210_DEBUG("My, my, you got more than you bargained for. ");
1312 DC210_DEBUG("Awaiting packet...\n");
1313 dc210_read_dummy_packet(camera);
1314 DC210_DEBUG("Read %d packets.\n", ++packetcount);
1315 goto wait_finish;
1316 case DC210_BUSY:
1317 DC210_DEBUG("Camera is busy.\n");
1318 goto wait_finish;
1319 default:
1320 DC210_DEBUG("Unexpected response 0x%02X to command 0x%02X.\n",
1321 answer, cmdbyte);
1322 return GP_OK;
1323 };
1324 };
1325
1326 int dc210_debug_callback(Camera * camera, CameraWidget * widget, GPContext * context){
1327
1328 /*CameraFile *file;*/
1329 CameraWidget * window;
1330 char * s_param1, *s_param2, *s_param3;
1331 int param1, param2, param3;
1332 int value;
1333 unsigned char params[4];
1334 dc210_status status;
1335
1336 memset(params, 0, 4);
1337
1338 gp_widget_get_root(widget, &window);
1339
1340 gp_widget_get_child_by_label (window, _("Parameter 1"), &widget);
1341 gp_widget_get_value (widget, &s_param1);
1342 param1 = atoi(s_param1);
1343
1344 gp_widget_get_child_by_label (window, _("Parameter 2"), &widget);
1345 gp_widget_get_value (widget, &s_param2);
1346 param2 = atoi(s_param2);
1347
1348 gp_widget_get_child_by_label (window, _("Parameter 3"), &widget);
1349 gp_widget_get_value (widget, &s_param3);
1350 param3 = atoi(s_param3);
1351
1352 /* Put the stuff you want to test here */
1353 dc210_get_status(camera, &status);
1354 dc210_test_command(camera, 0x85, params);
1355 dc210_get_status(camera, &status);
1356
1357 return GP_OK;
1358
1359 };
1360
1361 #endif