Debian Codegraph @

camlibs/kodak/dc240/library.c

raw
1 /* library.c
2 *
3 * Copyright (C) 2001,2002 Hubert Figuiere <[email protected]>
4 * Copyright (C) 2000,2001,2002 Scott Fritzinger
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21
22 /*
23 Kodak DC 240/280/3400/5000 driver.
24 Maintainer:
25 Hubert Figuiere <[email protected]>
26 */
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include <gphoto2/gphoto2.h>
36 #include <gphoto2/gphoto2-port.h>
37 #include "libgphoto2/gphoto2-endian.h"
38
39 #include "libgphoto2/i18n.h"
40
41 #include "dc240.h"
42 #include "library.h"
43
44
45 #define GP_MODULE "dc240"
46
47 /* do not sleep during fuzzing */
48 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
49 # define usleep(x)
50 #endif
51
52 /* legacy from dc240.h */
53 /*
54 #define COMM1 0x00
55 #define READY 0x10
56 #define ACK 0xd1
57 #define PACK1 0xd2
58 */
59 /* nice. errors all have upper nibble of 'e' */
60 /*
61 #define NAK 0xe1
62 #define COMM0 0xe2
63 #define PACK0 0xe3
64 #define CANCL 0xe4
65 */
66
67 /* system codes */
68 enum {
69 DC240_SC_COMPLETE = 0x0,
70 DC240_SC_ACK = 0xd1,
71 DC240_SC_CORRECT = 0xd2,
72 DC240_SC_FIRST_ERROR = 0xe0, /* not really an real code */
73 DC240_SC_NAK = 0xe1,
74 DC240_SC_ERROR = 0xe2,
75 DC240_SC_ILLEGAL = 0xe3,
76 DC240_SC_CANCEL = 0xe4,
77 DC240_SC_BUSY = 0xf0
78 };
79
80 static unsigned char *dc240_packet_new (int command_byte);
81 static int dc240_packet_write (Camera *camera, unsigned char *packet, int size,
82 int read_response);
83 static int dc240_packet_read (Camera *camera, unsigned char *packet, int size);
84
85
86
87 static unsigned char *
88 dc240_packet_new (int command_byte) {
89
90 unsigned char *p = malloc(8);
91 memset (p, 0, 8);
92 p[0] = command_byte;
93 p[7] = 0x1a;
94 return p;
95 }
96
97 static unsigned char *
98 dc240_packet_new_path (const char *folder, const char *filename) {
99 unsigned char *p;
100 char buf[1024];
101 size_t x;
102 unsigned char cs = 0;
103
104 p = malloc(sizeof(char)*60);
105 if (!p)
106 return NULL;
107
108 strcpy(buf, folder);
109
110 if (buf[strlen(buf)-1] != '/')
111 strcat(buf, "/");
112
113 if (filename)
114 strcat(buf, filename);
115 else
116 strcat(buf, "*.*");
117
118 for (x=0; x<strlen(buf); x++) {
119 buf[x] = (buf[x] == '/'? '\\' : buf[x]);
120 cs = cs ^ (unsigned char)buf[x];
121 }
122
123 memset(p, 0, sizeof(char)*60);
124 p[0] = 0x80;
125 memcpy(&p[1], buf, sizeof(char)*strlen(buf));
126 p[59] = cs;
127 return (p);
128 }
129
130
131 static int dc240_packet_write (Camera *camera, unsigned char *packet, int size, int read_response) {
132
133 /* Writes the packet and returns the result */
134
135 int x=0;
136 char in[2];
137
138 write_again:
139 /* If retry, give camera some recup time */
140 if (x > 0) {
141 usleep(SLEEP_TIMEOUT * 1000);
142 }
143
144 /* Return error if too many retries */
145 x++;
146 if (x > RETRIES) {
147 return (GP_ERROR_TIMEOUT);
148 }
149
150 if (gp_port_write(camera->port, (char*)packet, size) < GP_OK) {
151 goto write_again;
152 }
153
154 /* Read in the response from the camera if requested */
155 while (read_response) {
156 int ret;
157 if ((ret=gp_port_read(camera->port, in, 1)) >= GP_OK) {
158 /* On error, read again */
159 read_response = 0;
160 break;
161 }
162 if (ret == GP_ERROR_IO_READ) return ret; /* e.g. device detached? */
163 }
164
165 return GP_OK;
166 }
167
168 static int
169 dc240_packet_read (Camera *camera, unsigned char *packet, int size)
170 {
171
172 int retval = gp_port_read(camera->port, (char*)packet, size);
173
174 /*
175 * If we try to read data about a non-picture file, we get back
176 * a single DC240_SC_ERROR byte. So, if we're trying to read a packet with
177 * size greater than one, but we only get one byte, and it is a
178 * DC240_SC_ERROR error, return failure
179 */
180 if ((size > 1) && (retval == 1) && (packet[0] == DC240_SC_ERROR))
181 return GP_ERROR_NOT_SUPPORTED;
182
183 if (retval < GP_OK)
184 return retval;
185 else
186 return GP_OK;
187 }
188
189 static int dc240_packet_write_ack (Camera *camera)
190 {
191 int retval;
192 unsigned char c;
193
194 c = DC240_SC_CORRECT;
195 retval = gp_port_write(camera->port, (char*)&c, 1);
196 if (retval < GP_OK)
197 return retval;
198 return GP_OK;
199 }
200
201 static int dc240_packet_write_nak (Camera *camera)
202 {
203 int retval;
204 unsigned char c;
205
206 c = DC240_SC_ILLEGAL;
207 retval = gp_port_write(camera->port, (char*)&c, 1);
208 if (retval < GP_OK)
209 return retval;
210 return GP_OK;
211 }
212
213 static int dc240_wait_for_completion (Camera *camera) {
214 unsigned char p[8];
215 int retval = GP_OK;
216 int x=0, done=0;
217
218 /* Wait for command completion */
219 while ((x++ < 25)&&(!done)) {
220 retval = dc240_packet_read(camera, p, 1);
221 switch (retval) {
222 case GP_ERROR:
223 GP_DEBUG ("GP_ERROR\n");
224 return (GP_ERROR);
225 break;
226 case GP_ERROR_TIMEOUT:
227 GP_DEBUG ("GP_ERROR_TIMEOUT\n");
228 /* in busy state, GP_ERROR_IO_READ can happened */
229 break;
230 default:
231 done = 1;
232 }
233 }
234
235 if (x==25) {
236 return (GP_ERROR_TIMEOUT);
237 }
238
239 return GP_OK;
240 }
241
242 /*
243 Implement the wait state aka busy case
244 See 2.7.2 in the DC240 Host interface Specification
245 */
246 static int dc240_wait_for_busy_completion (Camera *camera)
247 {
248 enum {
249 BUSY_RETRIES = 100
250 };
251 unsigned char p[8];
252 int retval = 0;
253 int x=0, done=0;
254
255 /* Wait for command completion */
256 while ((x++ < BUSY_RETRIES)&&(!done)) {
257 retval = dc240_packet_read(camera, p, 1);
258 switch (retval) {
259 case GP_ERROR:
260 return retval;
261 break;
262 case GP_ERROR_IO_READ:
263 case GP_ERROR_TIMEOUT:
264 /* in busy state, GP_ERROR_IO_READ can happened */
265 break;
266 default:
267 if (*p != DC240_SC_BUSY) {
268 done = 1;
269 }
270 }
271 }
272 if (x == BUSY_RETRIES)
273 return (GP_ERROR);
274 return retval;
275 }
276
277
278 static int dc240_packet_exchange (Camera *camera, CameraFile *file,
279 unsigned char *cmd_packet, unsigned char *path_packet,
280 int *size, int block_size, GPContext *context)
281 {
282 /* Reads in multi-packet data, appending it to the "file". */
283 unsigned char check_sum;
284 int i;
285 int num_packets=1, num_bytes, retval = GP_OK;
286 int x=0, retries=0;
287 float t;
288 unsigned char packet[HPBS+2];
289 unsigned int id;
290
291 if (*size > 0) {
292 /* Known size to begin with */
293 t = (float)*size / (float)(block_size);
294 num_packets = (int)t;
295 if (t - (float)num_packets > 0) {
296 num_packets++;
297 }
298 } else {
299 /* Calculate size in 1st packet */
300 num_packets = 2;
301 }
302
303 read_data_write_again:
304 /* Write command/path packets */
305 if (cmd_packet) {
306 retval = dc240_packet_write(camera, cmd_packet, 8, 1);
307 if (retval < GP_OK)
308 return retval;
309 }
310
311 if (path_packet) {
312 retval = dc240_packet_write(camera, path_packet, 60, 1);
313 if (retval < GP_OK)
314 return retval;
315 }
316
317 id = gp_context_progress_start (context, num_packets, _("Getting data..."));
318 while (x < num_packets) {
319 read_data_read_again:
320 gp_context_progress_update (context, id, x);
321
322 /* Read the response/data */
323 retval = dc240_packet_read(camera, packet, block_size+2);
324
325 if (retval == GP_ERROR_NOT_SUPPORTED)
326 return retval;
327
328 if ((retval == GP_ERROR) || (retval == GP_ERROR_TIMEOUT)) {
329 /* ERROR reading response/data */
330 if (retries++ > RETRIES) {
331 gp_context_progress_stop (context, id);
332 return (GP_ERROR_TIMEOUT);
333 }
334
335 if (x==0) {
336 /* If first packet didn't come, write command again */
337 goto read_data_write_again;
338 } else {
339 /* Didn't get data packet. Send NAK for retry */
340 dc240_packet_write_nak(camera);
341 goto read_data_read_again;
342 }
343 }
344
345 /* Validate checksum */
346 check_sum = 0;
347 for (i = 1; i < block_size + 1; i++) {
348 check_sum ^= packet [i];
349 }
350 if ( block_size > 1 && check_sum != packet [i]) {
351 dc240_packet_write_nak (camera);
352 goto read_data_read_again;
353 }
354
355 /* Check for error in command/path */
356 if (packet[0] > DC240_SC_FIRST_ERROR) {
357 gp_context_progress_stop (context, id);
358 return GP_ERROR;
359 }
360
361 /* Check for end of data */
362 if (packet[0] == DC240_SC_COMPLETE) {
363 gp_context_progress_stop (context, id);
364 return GP_OK;
365 }
366
367 /* Write packet was OK */
368 if (dc240_packet_write_ack(camera) < GP_OK)
369 goto read_data_read_again;
370
371 /* Set size for folder/file list command from 1st packet */
372 if (cmd_packet && (cmd_packet[0]==0x99) && (x==0)) {
373 *size = (packet[1] * 256 +
374 packet[2])* 20 + 2;
375 t = (float)*size / (float)(block_size);
376 num_packets = (int)t;
377 if (t - (float)num_packets > 0)
378 num_packets++;
379 }
380
381 /* Copy in data */
382 if (x == num_packets)
383 num_bytes = *size - ((num_packets-1) * block_size);
384 else
385 num_bytes = block_size;
386 gp_file_append(file, (char*)&packet[1], num_bytes);
387
388 /* Increment packet count, reset retries */
389 x++;
390 retries = 0;
391 }
392 gp_context_progress_stop (context, id);
393 /* Read in command completed */
394 return dc240_wait_for_completion(camera);
395 }
396
397
398 /*
399 See 5.1.37
400 */
401 int dc240_open (Camera *camera)
402 {
403 int retval = GP_OK;
404 unsigned char *p = dc240_packet_new(0x96);
405
406 GP_DEBUG ("dc240_open\n");
407
408 retval = dc240_packet_write(camera, p, 8, 1);
409 if (retval != GP_OK) {
410 GP_DEBUG ("dc240_open: write returned %d\n", retval);
411 goto fail;
412 }
413
414 retval = dc240_wait_for_completion(camera);
415 if (retval < GP_OK) {
416 GP_DEBUG ("dc240_open: wait returned %d\n", retval);
417 goto fail;
418 }
419
420 fail:
421 free(p);
422 return retval;
423 }
424
425 int dc240_close (Camera *camera, GPContext *context)
426 {
427 unsigned char *p = dc240_packet_new(0x97);
428 int retval, size = -1;
429
430 retval = dc240_packet_exchange(camera, NULL, p, NULL, &size, -1, context);
431
432 free(p);
433 return retval;
434 }
435
436 static int dc240_get_file_size (Camera *camera, const char *folder, const char *filename, int thumb, GPContext *context) {
437
438 CameraFile *f;
439 unsigned char *p1, *p2;
440 int size = 256, offset;
441 unsigned long int fsize;
442 const unsigned char *fdata;
443
444 offset = (thumb? 92:104);
445
446 gp_file_new(&f);
447 p1 = dc240_packet_new(0x91);
448 p2 = dc240_packet_new_path(folder, filename);
449 if (dc240_packet_exchange(camera, f, p1, p2, &size, 256, context) < 0)
450 size = 0;
451 else {
452 int ret;
453 ret = gp_file_get_data_and_size (f, (const char**)&fdata, &fsize);
454 if (ret < GP_OK) return ret;
455 if (!fdata || (fsize < 4)) return GP_ERROR;
456 size = (fdata[offset] << 24) |
457 (fdata[offset+1] << 16) |
458 (fdata[offset+2] << 8 ) |
459 (fdata[offset+3]);
460 }
461
462 gp_file_free(f);
463 free(p1);
464 free(p2);
465
466 return (size);
467 }
468
469 int dc240_set_speed (Camera *camera, int speed)
470 {
471 int retval;
472 unsigned char *p = dc240_packet_new(0x41);
473 gp_port_settings settings;
474
475 GP_DEBUG ("dc240_set_speed\n");
476
477 gp_port_get_settings(camera->port, &settings);
478
479 switch (speed) {
480 case 9600:
481 p[2] = 0x96;
482 p[3] = 0x00;
483 settings.serial.speed = 9600;
484 break;
485 case 19200:
486 p[2] = 0x19;
487 p[3] = 0x20;
488 settings.serial.speed = 19200;
489 break;
490 case 38400:
491 p[2] = 0x38;
492 p[3] = 0x40;
493 settings.serial.speed = 38400;
494 break;
495 case 57600:
496 p[2] = 0x57;
497 p[3] = 0x60;
498 settings.serial.speed = 57600;
499 break;
500 case 0: /* Default */
501 case 115200:
502 p[2] = 0x11;
503 p[3] = 0x52;
504 settings.serial.speed = 115200;
505 break;
506 /* how well supported is 230.4?
507 case 230400:
508 p[2] = 0x23;
509 p[3] = 0x04;
510 settings.serial.speed = 230400;
511 break;
512 */
513 default:
514 retval = GP_ERROR;
515 goto fail;
516 }
517
518 retval = dc240_packet_write(camera, p, 8, 1);
519 if (retval != GP_OK)
520 goto fail;
521
522 retval = gp_port_set_settings (camera->port, settings);
523 if (retval != GP_OK)
524 goto fail;
525
526 usleep(300 * 1000);
527 retval = dc240_wait_for_completion(camera);
528 if (retval != GP_OK)
529 goto fail;
530 /* Speed change went OK. */
531 fail:
532 free (p);
533 return retval;
534 }
535
536 static struct _type_to_camera {
537 uint16_t status_type;
538 char * name;
539 } type_to_camera [] = {
540 { 4, "DC210" },
541 { 5, "DC240" },
542 { 6, "DC280" },
543 { 7, "DC5000" },
544 { 8, "DC3400" },
545 { 0, "Unknown" } /* unknown type. MUST be last */
546 };
547
548
549
550 const char *dc240_convert_type_to_camera (uint16_t type)
551 {
552 int i = 0;
553
554 while (type_to_camera[i].status_type != 0) {
555 if (type_to_camera[i].status_type == type)
556 return type_to_camera[i].name;
557 i++;
558 }
559 /* not found */
560 return type_to_camera[i].name;
561 }
562
563 const char *dc240_get_battery_status_str (uint8_t status)
564 {
565 switch (status) {
566 case 0:
567 return _("OK");
568 break;
569 case 1:
570 return _("Weak");
571 break;
572 case 2:
573 return _("Empty");
574 break;
575 default:
576 break;
577 }
578 return _("Invalid");
579 }
580
581 const char *dc240_get_ac_status_str (uint8_t status)
582 {
583 switch (status) {
584 case 0:
585 return _("Not used");
586 break;
587 case 1:
588 return _("In use");
589 break;
590 default:
591 break;
592 }
593 return _("Invalid");
594 }
595
596
597 const char * dc240_get_memcard_status_str(uint8_t status)
598 {
599 if (status & 0x80) {
600 if ((status & 0x10) == 0) {
601 if (status & 0x08)
602 return _("Card is open");
603 return _("Card is not open");
604 }
605 return _("Card is not formatted");
606 }
607 return _("No card");
608 }
609
610
611 /*
612 Feed manually the structure from data.
613 */
614 static int dc240_load_status_data_to_table (const unsigned char *fdata, DC240StatusTable *table)
615 {
616 if (fdata [0] != 0x01)
617 return GP_ERROR;
618 GP_DEBUG ("Camera Type = %d, %s\n", fdata[1], dc240_convert_type_to_camera (fdata[1]));
619 table->cameraType = fdata[1];
620 table->fwVersInt = fdata[2];
621 table->fwVersDec = fdata[3];
622 GP_DEBUG ("Firmware version = %d, %d\n", fdata[2], fdata[3]);
623 table->romVers32Int = fdata[4];
624 table->romVers32Dec = fdata[5];
625 table->romVers8Int = fdata[6];
626 table->romVers8Dec = fdata[7];
627 table->battStatus = fdata[8];
628 table->acAdapter = fdata[9];
629 table->strobeStatus = fdata[10];
630 table->memCardStatus = fdata[11];
631 table->videoFormat = fdata[12];
632 table->quickViewMode = fdata[13]; /* DC280 */
633 table->numPict = be16atoh(&fdata[14]);
634 strncpy (table->volumeID, (char*)&fdata[16], 11);
635 table->powerSave = fdata[27]; /* DC280 */
636 strncpy (table->cameraID, (char*)&fdata[28], 32);
637 table->remPictLow = be16atoh(&fdata[60]);
638 table->remPictMed = be16atoh(&fdata[62]);
639 table->remPictHigh = be16atoh(&fdata[64]);
640 table->totalPictTaken = be16atoh(&fdata[66]);
641 table->totalStrobeFired = be16atoh(&fdata[68]);
642 table->langType = fdata[70]; /* DC 280 */
643 table->beep = fdata[71];
644 table->fileType = fdata[78];
645 table->pictSize = fdata[79];
646 table->imgQuality = fdata[80];
647 table->ipChainDisable = fdata[81]; /* reserved on DC280. WTF is it ? */
648 table->imageIncomplete = fdata[82];
649 table->timerMode = fdata[83];
650 table->year = be16atoh(&fdata[88]);
651 table->month = fdata[90];
652 table->day = fdata[91];
653 table->hour = fdata[92];
654 table->minute = fdata[93];
655 table->second = fdata[94];
656 table->tenmSec = fdata[95];
657 table->strobeMode = fdata[97];
658 table->exposureComp = (fdata[98] * 100) + (fdata[99]);
659 table->aeMode = fdata[100];
660 table->focusMode = fdata[101];
661 table->afMode = fdata[102];
662 table->awbMode = fdata[103];
663 /* table->zoomMag = ????? */
664 table->exposureMode = fdata[129];
665 table->sharpControl = fdata[131];
666 /* table->expTime = */
667 table->fValue = (fdata[136] * 100) + (fdata[137]);
668 table->imageEffect = fdata[138];
669 table->dateTimeStamp = fdata[139];
670 strncpy (table->borderFileName, (char*)&fdata[140], 11);
671 table->exposureLock = fdata[152];
672 table->isoMode = fdata[153]; /* DC280 */
673
674 return GP_OK;
675 }
676
677 /*
678 Return the camera status table.
679 See 5.1.29
680 */
681 int dc240_get_status (Camera *camera, DC240StatusTable *table, GPContext *context)
682 {
683 CameraFile *file;
684 unsigned char *p = dc240_packet_new(0x7F);
685 int retval;
686 const char *fdata;
687 unsigned long int fsize;
688 int size = 256;
689
690 gp_file_new (&file);
691 GP_DEBUG ("enter dc240_get_status() \n");
692 retval = dc240_packet_exchange(camera, file, p, NULL, &size, 256, context);
693
694 if (retval == GP_OK) {
695 retval = gp_file_get_data_and_size (file, &fdata, &fsize);
696 if (retval != GP_OK) goto exit;
697 if (fsize != 256) {
698 GP_DEBUG ("wrong status packet size ! Size is %ld", fsize);
699 retval = GP_ERROR;
700 goto exit;
701 }
702 if (fdata [0] != 0x01) { /* see 2.6 for why 0x01 */
703 GP_DEBUG ("not a status table. Is %d", fdata [0]);
704 retval = GP_ERROR;
705 goto exit;
706 }
707 dc240_load_status_data_to_table ((uint8_t *)fdata, table);
708 }
709 exit:
710 gp_file_free(file);
711 free (p);
712
713 return (retval);
714 }
715
716 int dc240_get_directory_list (Camera *camera, CameraList *list, const char *folder,
717 unsigned char attrib, GPContext *context) {
718
719 CameraFile *file;
720 unsigned int x, y=0, z;
721 int size=256;
722 char buf[64];
723 unsigned char *p1 = dc240_packet_new(0x99);
724 unsigned char *p2 = dc240_packet_new_path(folder, NULL);
725 const char *fdata;
726 unsigned long int fsize;
727 int ret;
728 int num_of_entries = 0; /* number of entries in the listing */
729 unsigned int total_size = 0; /* total useful size of the listing */
730
731 gp_file_new(&file);
732 ret = dc240_packet_exchange(camera, file, p1, p2, &size, 256, context);
733 if (ret < 0) {
734 gp_file_free (file);
735 return ret;
736 }
737 free(p1);
738 free(p2);
739
740 /* Don't expect to have a fully useful buffer. */
741 ret = gp_file_get_data_and_size (file, &fdata, &fsize);
742 if (ret < 0) {
743 gp_file_free (file);
744 return ret;
745 }
746 if ((size < 1) || !fdata) {
747 gp_file_free (file);
748 return GP_ERROR;
749 }
750
751 /* numbers in DC 240 are Big-Endian. */
752 /* Conversion below should be endian neutral. */
753 num_of_entries = be16atoh(&fdata [0]) + 1;
754 total_size = 2 + (num_of_entries * 20);
755 GP_DEBUG ("number of file entries : %d, size = %ld", num_of_entries, fsize);
756 if (total_size > fsize) {
757 GP_DEBUG ("total_size %d > fsize %ld", total_size, fsize);
758 gp_file_free (file);
759 return GP_ERROR;
760 }
761 for (x = 2; x < total_size; x += 20) {
762 if ((fdata[x] != '.') && (attrib == (unsigned char)fdata[x+11])) {
763 /* Files have attrib 0x00, Folders have attrib 0x10 */
764 if (attrib == 0x00) {
765 strncpy(buf, &fdata[x], 8); /* Copy over filename */
766 buf[8] = 0; /* NULL terminate */
767 strcat(buf, "."); /* Append dot */
768 strcat(buf, &fdata[x+8]); /* Append extension */
769 GP_DEBUG ("found file: %s", buf);
770 } else {
771 strncpy(buf, &fdata[x], 8); /* Copy over folder name */
772 z=0;
773 while ((buf[z] != 0x20)&&(z<8)) /* Chop trailing spaces */
774 z++;
775 buf[z] = 0; /* NULL terminate */
776 GP_DEBUG ("found folder: %s", buf);
777 }
778 gp_list_append(list, buf, NULL);
779 y++;
780 }
781 }
782 gp_file_free(file);
783 return (GP_OK);
784 }
785
786 int dc240_file_action (Camera *camera, int action, CameraFile *file,
787 const char *folder, const char *filename, GPContext *context) {
788
789 int size=0, thumb=0, retval=GP_OK;
790 unsigned char *cmd_packet, *path_packet;
791
792 cmd_packet = dc240_packet_new(action);
793 path_packet = dc240_packet_new_path(folder, filename);
794
795 switch (action) {
796 case DC240_ACTION_PREVIEW:
797 cmd_packet[4] = 0x02;
798 thumb = 1;
799 /* fallthrough */
800 case DC240_ACTION_IMAGE:
801 if ((size = dc240_get_file_size(camera, folder, filename, thumb, context)) < GP_OK) {
802 retval = GP_ERROR;
803 break;
804 }
805 retval = dc240_packet_exchange(camera, file, cmd_packet, path_packet, &size, HPBS, context);
806 break;
807 case DC240_ACTION_DELETE:
808 size = -1;
809 retval = dc240_packet_exchange(camera, file, cmd_packet, path_packet, &size, -1, context);
810 break;
811 default:
812 free(cmd_packet);
813 free(path_packet);
814 return (GP_ERROR);
815 }
816
817 free(cmd_packet);
818 free(path_packet);
819
820 if (file)
821 gp_file_set_mime_type (file, GP_MIME_JPEG);
822 return (retval);
823 }
824
825 /*
826 See 5.1.27 in the DC240 Host Spec.
827 Capture a picture on the flash card and retrieve its full
828 name.
829 See also 5.1.19
830 */
831 int dc240_capture (Camera *camera, CameraFilePath *path, GPContext *context)
832 {
833 CameraFile *file;
834 int size = 256;
835 int ret = GP_OK;
836 unsigned char *p = dc240_packet_new(0x7C);
837 const char *fdata;
838 unsigned long int fsize;
839
840 /* Take the picture to Flash memory */
841 ret = dc240_packet_write(camera, p, 8, 1);
842 free (p);
843 if (ret != GP_OK) {
844 return ret;
845 }
846
847 gp_context_status (context, "Waiting for completion...");
848 ret = dc240_wait_for_completion(camera);
849 if (ret != GP_OK) {
850 return ret;
851 }
852
853 ret = dc240_wait_for_busy_completion(camera);
854 if (ret != GP_OK) {
855 return ret;
856 }
857
858 /*
859 get the last picture taken location (good firmware developers)
860 */
861 gp_file_new(&file);
862 p = dc240_packet_new(0x4C); /* 5.1.19 Send last taken image name */
863 ret = dc240_packet_exchange(camera, file, p, NULL, &size, 256, context);
864 free(p);
865 if (ret != GP_OK) {
866 path->name[0] = 0;
867 path->folder[0] = 0;
868 gp_file_unref (file);
869 return ret;
870 }
871
872 /* this part assumes that the response is of fixed size */
873 /* according to the specs, this is the case since only the */
874 /* numbering changes */
875 gp_file_get_data_and_size (file, &fdata, &fsize);
876 strncpy (path->folder, fdata, 14);
877 path->folder [14] = 0;
878 path->folder [0] = '/';
879 path->folder [5] = '/';
880 strncpy (path->name, &(fdata[15]), 13);
881 path->name[13] = 0;
882 gp_file_unref (file);
883
884 return GP_OK;
885 }
886
887 int dc240_packet_set_size (Camera *camera, short int size) {
888
889 unsigned char *p = dc240_packet_new(0x2A);
890
891 p[2] = (size >> 8) & 0xFF;
892 p[3] = (size ) & 0xFF;
893
894 if (dc240_packet_write(camera, p, 8, 1) == GP_ERROR)
895 return (GP_ERROR);
896
897 if (dc240_wait_for_completion(camera)==GP_ERROR)
898 return (GP_ERROR);
899 free(p);
900 return (GP_OK);
901 }