Debian Codegraph @

camlibs/kodak/dc120/library.c

raw
1 /* dc120.c
2 *
3 * Copyright (C) Scott Fritzinger
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 #define _DEFAULT_SOURCE
22 #define _POSIX_C_SOURCE 199309L
23 #include "config.h"
24
25 /* Must be included before including unistd.h to define _XOPEN_SOURCE
26 before unistd.h on FreeBSD. */
27 #include <gphoto2/gphoto2-port-portability.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34
35 #include <gphoto2/gphoto2.h>
36
37 #include "libgphoto2/i18n.h"
38
39 #include "dc120.h"
40 #include "library.h"
41
42
43 static char *
44 dc120_packet_new (int command_byte) {
45
46 char *p = (char *)malloc(sizeof(char) * 8);
47
48 memset (p, 0, 8);
49
50 p[0] = command_byte;
51 p[7] = 0x1a;
52
53 return p;
54 }
55
56 /** Checks if a response is ok.
57 * Checks if the response is ok (completed, ready, received) or not ok.
58 * @returns GP_OK if command is ok, GP_ERROR otherwise.
59 */
60 static int
61 dc120_response_ok (unsigned char response) {
62 /* This is stupid. how do you get a constant of type signed char?? (Ralf) */
63 if (response == 0x00 || /* Command completed */
64 response == 0x10 || /* Command ready */
65 response == 0xd1 || /* Command received (ACK) */
66 response == 0xd2 ) /* Correct packet */
67 return (GP_OK);
68 else
69 return (GP_ERROR);
70 }
71
72 /** Writes a packet to the port.
73 * This function writes a packet to a port determined by camera->port.
74 * When read_response != FALSE it also reads the response.
75 */
76 static int
77 dc120_packet_write (Camera *camera, char *packet, int size, int read_response) {
78
79 /* Writes the packet and returns the result */
80
81 int x=0;
82 unsigned char in[2];
83
84 write_again:
85 /* If retry, give camera some recup time */
86 if (x > 0)
87 usleep(SLEEP_TIMEOUT * 1000);
88
89 /* Return error if too many retries */
90 if (x++ > RETRIES)
91 return (GP_ERROR);
92
93 if (gp_port_write(camera->port, packet, size) < GP_OK)
94 goto write_again;
95
96
97 /* Read in the response from the camera if requested */
98 if (read_response) {
99 if (gp_port_read(camera->port, (char *)in, 1) < GP_OK)
100 /* On error, write again */
101 goto write_again;
102
103 if ( dc120_response_ok(in[0]) != (GP_OK) ) {
104 /* If a single byte response, don't rewrite */
105 if (size == 1)
106 return (GP_ERROR);
107 /* Got error response. Go again. */
108 goto write_again;
109 }
110 }
111
112 return (GP_OK);
113 }
114
115 static int
116 dc120_packet_read (Camera *camera, char *packet, int size) {
117
118 return (gp_port_read(camera->port, packet, size));
119 }
120
121 static int dc120_packet_read_data (Camera *camera, CameraFile *file, char *cmd_packet, int *size, int block_size, GPContext *context) {
122
123 /* Reads in multi-packet data, appending it to the "file". */
124
125 int num_packets, retval;
126 int packets_received;
127 int bytes_received;
128 int retries=0;
129 char packet[2048], p[8];
130 unsigned int id;
131
132 /* Determine number of packets needed on-the-fly */
133 if (*size > 0) {
134 num_packets = (*size + block_size-1) / block_size;
135 } else {
136 num_packets = 5;
137 }
138
139 id = gp_context_progress_start (context, num_packets, _("Getting data..."));
140 read_data_write_again:
141 if (dc120_packet_write(camera, cmd_packet, 8, 1) < GP_OK)
142 return (GP_ERROR);
143
144 packets_received = 0;
145 while( packets_received < num_packets ) {
146 gp_context_progress_update (context, id, packets_received);
147 retval = dc120_packet_read(camera, packet, block_size+1);
148 switch (retval) {
149 case GP_ERROR:
150 case GP_ERROR_TIMEOUT:
151 /* Write that packet was bad and resend */
152 if (retries++ > RETRIES)
153 return (GP_ERROR);
154 if (packets_received==0) {
155 /* If first packet didn't come, write command again */
156 goto read_data_write_again;
157 } else {
158 /* Didn't get data packet. Send retry */
159 p[0] = PACK0;
160 if (dc120_packet_write(camera, p, 1, 0)== GP_ERROR)
161 return (GP_ERROR);
162 }
163 break;
164 default:
165 packets_received++;
166
167 /* Default is packet was OK */
168 p[0] = PACK1;
169
170 /* Do some command-specific stuff */
171 switch (cmd_packet[0]) {
172 case 0x4A:
173 /* Set size and num_packets for camera filenames*/
174 if (packets_received==1)
175 *size = ((unsigned char)packet[0] * 256 +
176 (unsigned char)packet[1])* 20 + 2;
177 num_packets = (*size + block_size-1) / block_size;
178 break;
179 case 0x64:
180 case 0x54:
181 /* If thumbnail, cancel rest of image transfer */
182 if ((num_packets == 16)&&(packets_received==16))
183 p[0] = CANCL;
184 /* No break on purpose */
185 break;
186 default:
187 /* Nada */
188 break;
189 }
190
191 /* Write response */
192 if (dc120_packet_write(camera, p, 1, FALSE)== GP_ERROR)
193 return (GP_ERROR);
194
195 /* Copy in data */
196 if (packets_received == num_packets)
197 bytes_received = *size - ((num_packets-1) * block_size);
198 else
199 bytes_received = block_size;
200 gp_file_append(file, packet, bytes_received);
201
202 }
203 }
204 gp_context_progress_stop (context, id);
205
206 if ((unsigned char)p[0] != CANCL)
207 /* Read in command completed.
208 (Shouldn't we check for command completed (Ralf)) */
209 dc120_packet_read(camera, p, 1);
210
211 return (GP_OK);
212
213 }
214
215 int dc120_set_speed (Camera *camera, int speed)
216 {
217 int error = GP_OK;
218 GPPortSettings settings;
219 char *p = dc120_packet_new(0x41);
220
221 if(p == NULL) {
222 /* unlikely to happen, FFS */
223 return GP_ERROR;
224 }
225 gp_port_get_settings(camera->port, &settings);
226
227 switch (speed) {
228 case 9600:
229 p[2] = (unsigned char)0x96;
230 p[3] = (unsigned char)0x00;
231 settings.serial.speed = 9600;
232 break;
233 case 19200:
234 p[2] = (unsigned char)0x19;
235 p[3] = (unsigned char)0x20;
236 settings.serial.speed = 19200;
237 break;
238 case 38400:
239 p[2] = (unsigned char)0x38;
240 p[3] = (unsigned char)0x40;
241 settings.serial.speed = 38400;
242 break;
243 case 57600:
244 p[2] = (unsigned char)0x57;
245 p[3] = (unsigned char)0x60;
246 settings.serial.speed = 57600;
247 break;
248 case 0: /* Default */
249 case 115200:
250 p[2] = (unsigned char)0x11;
251 p[3] = (unsigned char)0x52;
252 settings.serial.speed = 115200;
253 break;
254 /* how well supported is 230.4?
255 case 230400:
256 p[2] = 0x23;
257 p[3] = 0x04;
258 settings.serial.speed = 230400;
259 break;
260 */
261 default:
262 error = GP_ERROR;
263 goto fail;
264 break;
265 }
266
267 if (dc120_packet_write(camera, p, 8, 1) == GP_ERROR)
268 {
269 error = GP_ERROR;
270 goto fail;
271 }
272
273 gp_port_set_settings (camera->port, settings);
274
275
276 usleep(300 * 1000);
277
278 /* Speed change went OK. */
279 error = GP_OK;
280
281 fail:
282 free (p);
283 return error;
284 }
285
286 int dc120_get_status (Camera *camera, Kodak_dc120_status *status, GPContext *context) {
287
288 CameraFile *file;
289 char *p = dc120_packet_new(0x7F);
290 int i;
291 int retval;
292 int size;
293
294 gp_file_new (&file);
295 size = 256;
296 retval = dc120_packet_read_data(camera, file, p, &size, 256, context);
297
298 if (retval == (GP_OK) && status != NULL)
299 {
300 const char *data;
301 long unsigned int lsize;
302
303 gp_file_get_data_and_size(file, &data, &lsize);
304 if (lsize<122) {
305 gp_file_free (file);
306 free (p);
307 return (GP_ERROR);
308 }
309
310 memset((char*)status, 0, sizeof(*status));
311
312 status->camera_type_id = data[1];
313 status->firmware_major = data[2];
314 status->firmware_minor = data[3];
315 status->batteryStatusId = data[8];
316 status->acStatusId = data[9];
317
318 /* seconds since unix epoc */
319 status->time = CAMERA_EPOC + (
320 data[12] * 0x1000000 +
321 data[13] * 0x10000 +
322 data[14] * 0x100 +
323 data[15]) / 2;
324
325 status->af_mode = data[16] & 0x0f;
326 status->zoom_mode = (data[16] & 0x30)>>4;
327 status->flash_charged = data[18];
328 status->compression_mode_id = data[19];
329 status->flash_mode = data[20];
330 status->exposure_compensation = (data[2]&0x40 ? -1 : 1) * (data[21] & 0x3f);
331 status->light_value = data[22];
332 status->manual_exposure = data[23];
333 status->exposure_time = (data[24] * 0x1000000 +
334 data[25] * 0x10000 +
335 data[26] * 0x100 +
336 data[27]) / 2;
337 status->shutter_delay = data[29];
338 status->memory_card = data[30];
339 status->front_cover = data[31];
340 status->date_format = data[32];
341 status->time_format = data[33];
342 status->distance_format = data[34];
343 status->taken_pict_mem = data[36] * 0x100 + data[37];
344 for (i=0; i<4; i++) {
345 status->remaining_pic_mem[i] =
346 data[46+i*2] * 0x100 +
347 data[47+i*2];
348 }
349 status->taken_pict_card = data[56] * 0x100 + data[57];
350 for (i=0; i<4; i++) {
351 status->remaining_pic_card[i] =
352 data[66+i*2] * 0x100 +
353 data[67+i*2];
354 }
355 strncpy(status->card_id, data + 77, 32);
356 strncpy(status->camera_id, data + 90, 32);
357 }
358
359
360 gp_file_free(file);
361 free (p);
362
363 return (retval);
364 }
365
366 int dc120_get_albums (Camera *camera, int from_card, CameraList *list, GPContext *context) {
367
368 CameraFile *file;
369 int x;
370 int size;
371 char *f;
372 char *p = dc120_packet_new(0x44);
373 const char *file_data;
374 long unsigned int file_size;
375
376 if (from_card)
377 p[1] = 0x01;
378
379 gp_file_new(&file);
380
381 size = 256;
382 if (dc120_packet_read_data(camera, file, p, &size, 256, context)==GP_ERROR) {
383 gp_file_free(file);
384 free (p);
385 return GP_ERROR;
386 }
387 gp_file_get_data_and_size (file, &file_data, &file_size);
388
389 for (x=0; x<8; x++) {
390 f = (char*)&file_data[x*15];
391 if (strlen(f)>0)
392 gp_list_append(list, f, NULL);
393 }
394
395 gp_file_free(file);
396 free (p);
397
398 return (GP_OK);
399 }
400
401 int dc120_get_filenames (Camera *camera, int from_card, int album_number, CameraList *list, GPContext *context)
402 {
403 CameraFile *file;
404 int x;
405 int size;
406 char *p;
407 char buf[16];
408 const char *file_data;
409 long unsigned int file_size;
410
411 /* --- now read the files --- */
412
413 p = dc120_packet_new(0x4A);
414
415 if (from_card)
416 p[1] = 0x01;
417
418 /* Set the album number */
419 p[4] = album_number;
420
421 gp_file_new(&file);
422 size = 256; /* packet_read_data has special handling for size */
423 if (dc120_packet_read_data(camera, file, p, &size, size, context)==GP_ERROR) {
424 gp_file_free(file);
425 free (p);
426 return (GP_ERROR);
427 }
428
429
430 /* extract the filenames from the packet data */
431 gp_file_get_data_and_size (file, &file_data, &file_size);
432 x=2;
433 while (x < size) {
434 if (file_data[x] != '\0') {
435 strncpy(buf, (char*)&file_data[x], 11);
436 buf[7] = '.';
437 buf[11] = '\0';
438 gp_list_append(list, buf, NULL);
439 }
440 x += 20;
441 }
442
443 /* be a good boy and free up our stuff */
444 gp_file_free(file);
445 free (p);
446
447 return (GP_OK);
448 }
449
450 static int dc120_get_file_preview (Camera *camera, CameraFile *file, int file_number, char *cmd_packet, int *size, GPContext *context) {
451
452 CameraFile *f;
453 int x;
454 char buf[16];
455 const char *f_data;
456 long unsigned int f_size;
457
458 *size = 15680;
459
460 /* Create a file for KDC data */
461 gp_file_new(&f);
462 if (dc120_packet_read_data(camera, f, cmd_packet, size, 1024, context)==GP_ERROR) {
463 gp_file_free(f);
464 return (GP_ERROR);
465 }
466 /* Convert to PPM file for now */
467 gp_file_append(file, "P3\n80 60\n255\n", 13);
468
469 gp_file_get_data_and_size (f, &f_data, &f_size);
470
471 for (x=0; x<14400; x+=3) {
472 sprintf(buf, "%i %i %i\n",
473 (unsigned char)f_data[x+1280],
474 (unsigned char)f_data[x+1281],
475 (unsigned char)f_data[x+1282]);
476 gp_file_append(file, buf, strlen(buf));
477 }
478
479 gp_file_free(f);
480 /* sleep(1); */ /* FIXME: why? ... Marcus: removed it for now.*/
481 return (GP_OK);
482 }
483
484 static int dc120_get_file (Camera *camera, CameraFile *file, int file_number, char *cmd_packet, int *size, GPContext *context)
485 {
486 CameraFile *size_file; /* file used to determine the filesize */
487 char *p;
488 const char *file_data;
489 long unsigned int file_size;
490 unsigned int offset;
491
492 /* --- first read the file size --- */
493 p = dc120_packet_new(0x4A);
494
495 /* Copy over location and album number */
496 p[1] = cmd_packet[1];
497 p[4] = cmd_packet[4];
498
499 gp_file_new(&size_file);
500 *size = 256; /* packet_read_data has special handling for size */
501 if (dc120_packet_read_data(camera, size_file, p, size, *size, context)==GP_ERROR) {
502 gp_file_free(size_file);
503 free (p);
504 return (GP_ERROR);
505 }
506
507 /* extract the filesize from the packet data */
508 gp_file_get_data_and_size (size_file, &file_data, &file_size);
509
510 offset = 2 + (file_number-1) * 20;
511 if (file_size < (offset+19))
512 {
513 gp_file_free(size_file);
514 free (p);
515 return (GP_ERROR);
516 }
517
518 *size = (unsigned char)file_data[offset + 16] * 256*256*256 +
519 (unsigned char)file_data[offset + 17] * 256*256 +
520 (unsigned char)file_data[offset + 18] * 256 +
521 (unsigned char)file_data[offset + 19];
522
523 /* be a good boy and free up our stuff */
524 gp_file_free(size_file);
525 free (p);
526
527 if (dc120_packet_read_data(camera, file, cmd_packet, size, 1024, context)==GP_ERROR)
528 return (GP_ERROR);
529
530
531 return (GP_OK);
532 }
533
534 static int dc120_wait_for_completion (Camera *camera, GPContext *context) {
535
536 char p[8];
537 int retval;
538 int x=0, done=0;
539 unsigned int id;
540
541 /* Wait for command completion */
542 id = gp_context_progress_start (context, 25, _("Waiting for completion..."));
543 while ((x++ < 25)&&(!done)) {
544
545 retval = dc120_packet_read(camera, p, 1);
546 switch (retval) {
547 case GP_ERROR:
548 return (GP_ERROR);
549 break;
550 case GP_ERROR_TIMEOUT:
551 break;
552 default:
553 done = 1;
554 }
555 gp_context_progress_update (context, id, x);
556 }
557 gp_context_progress_stop (context, id);
558
559 if (x==25)
560 return (GP_ERROR);
561 return (GP_OK);
562
563 }
564
565 static int dc120_delete_file (Camera *camera, char *cmd_packet, GPContext *context) {
566
567 char p[8];
568
569 if (dc120_packet_write(camera, cmd_packet, 8, 1) == GP_ERROR)
570 return (GP_ERROR);
571
572 if (dc120_packet_read(camera, p, 1)==GP_ERROR)
573 return (GP_ERROR);
574
575 if (dc120_wait_for_completion(camera, context)==GP_ERROR)
576 return (GP_ERROR);
577
578 return (GP_OK);
579 }
580
581 int dc120_file_action (Camera *camera, int action, int from_card, int album_number,
582 int file_number, CameraFile *file, GPContext *context) {
583
584 int retval;
585 int size=0;
586 char *p = dc120_packet_new(0x00);
587
588 if (from_card)
589 p[1] = 0x01;
590
591 /* Set the picture number */
592 p[2] = (file_number >> 8) & 0xFF;
593 p[3] = file_number & 0xFF;
594
595 /* Set the album number */
596 p[4] = album_number;
597
598 switch (action) {
599 case DC120_ACTION_PREVIEW:
600 p[0] = (from_card ? 0x64 : 0x54);
601 retval = dc120_get_file_preview(camera, file, file_number, p, &size, context);
602 break;
603 case DC120_ACTION_IMAGE:
604 p[0] = (from_card ? 0x64 : 0x54);
605 retval = dc120_get_file(camera, file, file_number, p, &size, context);
606 break;
607 case DC120_ACTION_DELETE:
608 p[0] = (from_card ? 0x7B : 0x7A);
609 retval = dc120_delete_file(camera, p, context);
610 break;
611 default:
612 retval = GP_ERROR;
613 }
614 free(p);
615 return (retval);
616 }
617
618 int dc120_capture (Camera *camera, CameraFilePath *path, GPContext *context)
619 {
620 int retval;
621 char *p = dc120_packet_new(0x77);
622
623 /* Take the picture to Flash memory */
624 if (dc120_packet_write(camera, p, 8, 1) == GP_ERROR) {
625 retval = (GP_ERROR);
626 } else if (dc120_wait_for_completion(camera, context) == GP_ERROR) {
627 retval = (GP_ERROR);
628 } else {
629 retval = (GP_OK);
630 }
631
632 free(p);
633 return retval;
634 }