Debian Codegraph @

camlibs/panasonic/coolshot/library.c

raw
1 /****************************************************************/
2 /* library.c - Gphoto2 library for accessing the Panasonic */
3 /* Coolshot KXL-600A & KXL-601A digital cameras. */
4 /* */
5 /* Copyright 2001 Chris Pinkham */
6 /* */
7 /* Author: Chris Pinkham <[email protected]> */
8 /* */
9 /* This library is free software; you can redistribute it */
10 /* and/or modify it under the terms of the GNU Library General */
11 /* Public License as published by the Free Software Foundation; */
12 /* either version 2 of the License, or (at your option) any */
13 /* later version. */
14 /* */
15 /* This library is distributed in the hope that it will be */
16 /* useful, but WITHOUT ANY WARRANTY; without even the implied */
17 /* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
18 /* PURPOSE. See the GNU Library General Public License for */
19 /* more details. */
20 /* */
21 /* You should have received a copy of the GNU Library General */
22 /* Public License along with this library; if not, write to the */
23 /* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,*/
24 /* Boston, MA 02110-1301 USA */
25 /****************************************************************/
26
27 #define _POSIX_C_SOURCE 199309L
28 #define _DEFAULT_SOURCE
29
30 #include "config.h"
31
32 /* Must be included before including unistd.h to define _XOPEN_SOURCE
33 before unistd.h on FreeBSD. */
34 #include <gphoto2/gphoto2-port-portability.h>
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <gphoto2/gphoto2.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "libgphoto2/i18n.h"
43
44 #include "library.h"
45
46
47 #define GP_MODULE "coolshot"
48
49 #define COOL_SLEEP 10000
50
51 #define RETRIES 10
52
53 #define COOLSHOT_DONE 0x00
54 #define COOLSHOT_PKT 0x01
55 #define COOLSHOT_ENQ 0x05
56 #define COOLSHOT_ACK 0x06
57 #define COOLSHOT_NAK 0x15
58
59
60 static int coolshot_ack (Camera *camera);
61 #if 0
62 static int coolshot_nak (Camera *camera);
63 #endif
64 static int coolshot_sp (Camera *camera);
65 static int coolshot_fs( Camera *camera, int number );
66 static int coolshot_write_packet (Camera *camera, char *packet);
67 static int coolshot_read_packet (Camera *camera, char *packet);
68 static int coolshot_check_checksum( char *packet, int length );
69 static int coolshot_download_image( Camera *camera, CameraFile *file,
70 char *buf, int *len, int thumbnail, GPContext *context );
71
72 static int packet_size = 500;
73
74 /* ??set mode?? */
75 int coolshot_sm( Camera *camera ) {
76 char buf[16];
77
78 GP_DEBUG ("* coolshot_sm");
79
80 memset( buf, 0, sizeof( buf ));
81
82 buf[0] = COOLSHOT_PKT;
83 buf[2] = 'S';
84 buf[3] = 'M';
85 buf[4] = 0x01;
86 buf[15] = 0x02;
87
88 coolshot_write_packet( camera, buf );
89
90 /* read ACK */
91 coolshot_read_packet( camera, buf );
92
93 /* read data */
94 coolshot_read_packet( camera, buf );
95
96 /* send ACK */
97 coolshot_ack( camera );
98
99 packet_size = 128;
100
101 return( GP_OK );
102 }
103
104 /* ?set baud? */
105 int coolshot_sb( Camera *camera, int speed ) {
106 char buf[16];
107 gp_port_settings settings;
108
109 GP_DEBUG ("* coolshot_sb");
110 GP_DEBUG ("*** speed: %i", speed);
111
112 memset( buf, 0, sizeof( buf ));
113
114 buf[0] = COOLSHOT_PKT;
115 buf[2] = 'S';
116 buf[3] = 'B';
117 buf[4] = 0x01;
118 buf[15] = 0x02;
119
120 gp_port_get_settings (camera->port, &settings);
121
122 switch (speed) {
123 case 9600:
124 buf[4] = '1';
125 settings.serial.speed = 9600;
126 break;
127 case -1:
128 case 19200:
129 buf[4] = '2';
130 settings.serial.speed = 19200;
131 break;
132 case 28800:
133 buf[4] = '3';
134 settings.serial.speed = 28800;
135 break;
136 case 38400:
137 buf[4] = '4';
138 settings.serial.speed = 38400;
139 break;
140 case 57600:
141 buf[4] = '5';
142 settings.serial.speed = 57600;
143 break;
144 case 0: /* Default speed */
145 case 115200:
146 buf[4] = '6';
147 settings.serial.speed = 115200;
148 break;
149 default:
150 return (GP_ERROR_IO_SERIAL_SPEED);
151 }
152
153 coolshot_enq( camera );
154
155 /* set speed */
156 coolshot_write_packet( camera, buf );
157
158 /* read ack */
159 coolshot_read_packet( camera, buf );
160
161 /* read OK */
162 coolshot_read_packet( camera, buf );
163
164 /* ack the OK */
165 coolshot_ack( camera );
166
167 CHECK (gp_port_set_settings (camera->port, settings));
168
169 usleep(10 * 1000);
170 return (GP_OK);
171 }
172
173 static
174 int coolshot_fs( Camera *camera, int number ) {
175 char buf[16];
176
177 GP_DEBUG ("* coolshot_fs");
178
179 memset( buf, 0, sizeof( buf ));
180
181 buf[0] = COOLSHOT_PKT;
182 buf[2] = 'F';
183 buf[3] = 'S';
184 buf[7] = number;
185 buf[15] = 0x02;
186
187 coolshot_enq( camera );
188
189 coolshot_write_packet( camera, buf );
190
191 /* read ACK */
192 coolshot_read_packet( camera, buf );
193
194 /* read data */
195 coolshot_read_packet( camera, buf );
196
197 /* send ACK */
198 coolshot_ack( camera );
199 return( GP_OK );
200 }
201
202 static
203 int coolshot_sp( Camera *camera ) {
204 char buf[16];
205
206 GP_DEBUG ("* coolshot_sp");
207
208 memset( buf, 0, sizeof( buf ));
209
210 buf[0] = COOLSHOT_PKT;
211 buf[2] = 'S';
212 buf[3] = 'P';
213 buf[4] = 0x02;
214 buf[15] = 0x02;
215
216 coolshot_enq( camera );
217
218 coolshot_write_packet( camera, buf );
219
220 /* read ACK */
221 coolshot_read_packet( camera, buf );
222
223 packet_size = 500;
224
225 return( GP_OK );
226 }
227
228 int coolshot_file_count (Camera *camera) {
229 char buf[16];
230 int count = 0;
231
232 GP_DEBUG ("* coolshot_file_count");
233
234 memset( buf, 0, sizeof( buf ));
235
236 buf[0] = COOLSHOT_PKT;
237 buf[2] = 'R';
238 buf[3] = 'N';
239 buf[5] = 0x01;
240 buf[15] = 0x02;
241
242 coolshot_enq( camera );
243
244 /* request count */
245 coolshot_write_packet( camera, buf );
246
247 /* read ack */
248 coolshot_read_packet( camera, buf );
249
250 /* read data packet */
251 coolshot_read_packet( camera, buf );
252
253 count = buf[7];
254
255 usleep( COOL_SLEEP );
256 coolshot_ack( camera );
257
258 return( count );
259 }
260
261 int coolshot_request_image( Camera *camera, CameraFile *file,
262 char *buf, int *len, int number, GPContext *context ) {
263 char packet[16];
264
265 GP_DEBUG ("* coolshot_request_image");
266
267 memset( packet, 0, sizeof( packet ));
268
269 packet[0] = COOLSHOT_PKT;
270 packet[2] = 'R';
271 packet[3] = 'D';
272 packet[7] = number;
273 packet[15] = 0x02;
274
275 /* fixme */
276 coolshot_fs( camera, number );
277 coolshot_sp( camera );
278
279 coolshot_enq( camera );
280
281 /* request image */
282 coolshot_write_packet( camera, packet );
283
284 /* read ack */
285 coolshot_read_packet( camera, packet );
286
287 /* read OK */
288 coolshot_read_packet( camera, packet );
289
290 /* read data */
291 coolshot_download_image( camera, file, buf, len, 0, context );
292
293 return( GP_OK );
294 }
295
296 int coolshot_request_thumbnail( Camera *camera, CameraFile *file,
297 char *buf, int *len, int number, GPContext *context ) {
298 char packet[16];
299
300 GP_DEBUG ("* coolshot_request_thumbnail");
301
302 memset( packet, 0, sizeof( packet ));
303
304 packet[0] = COOLSHOT_PKT;
305 packet[2] = 'R';
306 packet[3] = 'M';
307 packet[7] = number;
308 packet[15] = 0x02;
309
310 /* fixme */
311 coolshot_fs( camera, number );
312 /*
313 coolshot_sp( camera );
314 */
315
316 coolshot_enq( camera );
317
318 /* request image */
319 coolshot_write_packet( camera, packet );
320
321 /* read ack */
322 coolshot_read_packet( camera, packet );
323
324 /* read OK */
325 coolshot_read_packet( camera, packet );
326
327 /* read data */
328 coolshot_download_image( camera, file, buf, len, 1, context );
329
330 return( GP_OK );
331 }
332
333 static
334 int coolshot_check_checksum( char *packet, int length ) {
335 int checksum = 0;
336 int p_csum = 0;
337 int x;
338 unsigned char *ptr;
339
340 ptr = (unsigned char *)packet + 2;
341 for( x = 2; x < (length - 4 ); x++ ) {
342 checksum += *(ptr++);
343 /* checksum += (unsigned char)packet[x]; */
344 }
345 checksum &= 0xffff; /* 16 bit checksum */
346
347 p_csum = (unsigned char)packet[length - 4];
348 p_csum = p_csum << 8;
349 p_csum += (unsigned char)packet[length - 3];
350
351 if ( checksum == p_csum ) {
352 return( GP_OK );
353 } else {
354 return( GP_ERROR );
355 }
356 }
357
358 int coolshot_download_image( Camera *camera, CameraFile *file,
359 char *buf, int *len, int thumbnail, GPContext *context ) {
360 char packet[1024];
361 int data_len;
362 int bytes_read = 0;
363 int last_good = 0;
364 unsigned int id;
365
366 GP_DEBUG ("* coolshot_download_image");
367
368 memset( packet, 0, sizeof( packet ));
369
370 packet[2] = '0';
371 packet[3] = '0';
372
373 coolshot_ack( camera );
374
375 coolshot_read_packet( camera, packet );
376
377 data_len = (unsigned char)packet[6] * 256;
378 data_len += (unsigned char)packet[7];
379
380 /* fixme, get rid of hardcoded length */
381 if ( coolshot_check_checksum( packet, 8 + packet_size + 4 ) == GP_OK ) {
382 coolshot_ack( camera );
383 last_good = 1;
384 } else {
385 /*
386 coolshot_nak( camera );
387 */
388 last_good = 0;
389 }
390
391 id = gp_context_progress_start (context, thumbnail ? 1800 : 80000,
392 _("Downloading image..."));
393 while( strncmp( packet + 2, "DT", 2 ) == 0 ) {
394 /* process packet */
395 if ( last_good ) {
396 data_len = (unsigned char)packet[6] * 256;
397 data_len += (unsigned char)packet[7];
398
399 memcpy( buf + bytes_read, packet + 8, data_len );
400
401 bytes_read += data_len;
402 }
403
404 gp_context_progress_update (context, id, bytes_read);
405 /* fixme, add ability to cancel download */
406
407 coolshot_read_packet( camera, packet );
408
409 data_len = (unsigned char)packet[6] * 256;
410 data_len += (unsigned char)packet[7];
411
412 /* fixme, get rid of hardcoded length */
413 if ( coolshot_check_checksum( packet, 8 + packet_size + 4 ) == GP_OK) {
414 coolshot_ack( camera );
415 last_good = 1;
416 } else {
417 /*
418 coolshot_nak( camera );
419 */
420 last_good = 0;
421 }
422 }
423 gp_context_progress_stop (context, id);
424
425 coolshot_ack( camera );
426
427 *len = bytes_read;
428
429 return( GP_OK );
430 }
431
432 static
433 int coolshot_write_packet (Camera *camera, char *packet) {
434 int x, ret, r, checksum=0, length;
435
436 GP_DEBUG ("* coolshot_write_packet");
437
438 if ( packet[0] == COOLSHOT_PKT ) {
439 /* fixme */
440 length = 16;
441
442 for ( x = 2 ; x < 12; x++ ) {
443 checksum += (unsigned char)packet[x];
444 }
445
446 packet[length - 4] = (checksum >> 8 ) & 0xff;
447 packet[length - 3] = checksum & 0xff;
448
449 } else if (( packet[0] == COOLSHOT_ENQ ) ||
450 ( packet[0] == COOLSHOT_ACK ) ||
451 ( packet[0] == COOLSHOT_NAK )) {
452 length = 1;
453 } else {
454 length = 0;
455 /* fixme */
456 return( -1 );
457 }
458
459 for (r = 0; r < RETRIES; r++) {
460 ret = gp_port_write (camera->port, packet, length);
461 if (ret == GP_ERROR_TIMEOUT)
462 continue;
463
464 return (ret);
465 }
466
467 return (GP_ERROR_TIMEOUT);
468 }
469
470 static
471 int coolshot_read_packet (Camera *camera, char *packet) {
472 int r = 0, x = 0, ret, done, length=0;
473 int blocksize, bytes_read;
474
475 GP_DEBUG ("* coolshot_read_packet");
476
477 read_packet_again:
478 packet[0] = 0;
479
480 if (r > 0)
481 GP_DEBUG ("* reading again...");
482
483 done = 0;
484
485 blocksize = 1;
486
487 for (r = 0; r < RETRIES; r++) {
488
489 bytes_read = gp_port_read (camera->port, packet, blocksize);
490 if (bytes_read == GP_ERROR_TIMEOUT)
491 continue;
492 if (bytes_read < 0)
493 return (bytes_read);
494
495 if ( packet[0] == COOLSHOT_ENQ ) {
496 usleep( COOL_SLEEP );
497 coolshot_ack( camera );
498 coolshot_read_packet( camera, packet );
499 return( GP_OK );
500 }
501
502 if (( packet[0] == COOLSHOT_ACK ) ||
503 ( packet[0] == COOLSHOT_DONE )) {
504 return( GP_OK );
505 }
506
507 if ( packet[0] != COOLSHOT_PKT ) {
508 return( GP_ERROR );
509 }
510
511 bytes_read = gp_port_read (camera->port, packet + 1, 3 );
512 if (bytes_read == GP_ERROR_TIMEOUT)
513 continue;
514 if (bytes_read < 0)
515 return (bytes_read);
516
517 /* Determine the packet type */
518 if (( strncmp( packet + 2, "OK", 2 ) == 0 ) ||
519 ( strncmp( packet + 2, "DE", 2 ) == 0 ) ||
520 ( strncmp( packet + 2, "SB", 2 ) == 0 )) {
521 /* normal 16-byte packet, so read the other 12 bytes */
522 ret = gp_port_read (camera->port, packet + 4, 12 );
523 if (ret == GP_ERROR_TIMEOUT) {
524 /* fixme */
525 /*
526 coolshot_nak (camera);
527 */
528 goto read_packet_again;
529 }
530
531 if (ret < 0)
532 return (ret);
533
534 return( GP_OK );
535 } else if ( strncmp( packet + 2, "DT", 2 ) == 0 ) {
536 /* read in packet number and length */
537 ret = gp_port_read (camera->port, packet + 4, 4 );
538 /* fixme, error detection */
539
540 length = (unsigned char)packet[6] * 256;
541 length += (unsigned char)packet[7];
542
543 if (( packet_size == 128 ) ||
544 ( length == 128 )) {
545 length = 128;
546 } else {
547 length = 500;
548 }
549 length += 4;
550
551 ret = gp_port_read (camera->port, packet + 8, length );
552 /* fixme, error detection */
553
554 x = 0;
555 while(( ret == GP_ERROR_TIMEOUT ) &&
556 ( x < RETRIES )) {
557 x++;
558 ret = gp_port_read (camera->port, packet + 8, length );
559 }
560
561 return( GP_OK );
562 }
563
564 if (done) break;
565 }
566
567 return (GP_ERROR_TIMEOUT);
568 }
569
570 static
571 int coolshot_ack (Camera *camera)
572 {
573 int ret, r = 0;
574 char buf[16];
575
576 GP_DEBUG ("* coolshot_ack");
577
578 buf[0] = COOLSHOT_ACK;
579
580 for (r = 0; r < RETRIES; r++) {
581
582 ret = coolshot_write_packet (camera, buf);
583 if (ret == GP_ERROR_TIMEOUT)
584 continue;
585 if (ret == GP_OK)
586 return (ret);
587 }
588 return (GP_ERROR_TIMEOUT);
589 }
590
591 #if 0
592 static
593 int coolshot_nak (Camera *camera)
594 {
595 int ret, r = 0;
596 char buf[16];
597
598 GP_DEBUG ("* coolshot_nak");
599
600 buf[0] = COOLSHOT_NAK;
601
602 for (r = 0; r < RETRIES; r++) {
603
604 ret = coolshot_write_packet (camera, buf);
605 if (ret == GP_ERROR_TIMEOUT)
606 continue;
607 if (ret == GP_OK)
608 return (ret);
609 }
610 return (GP_ERROR_TIMEOUT);
611 }
612 #endif
613
614 int coolshot_enq (Camera *camera)
615 {
616 int ret, r = 0;
617 char buf[16];
618
619 GP_DEBUG ("* coolshot_enq");
620
621 buf[0] = COOLSHOT_ENQ;
622
623 for (r = 0; r < RETRIES; r++) {
624
625 ret = coolshot_write_packet (camera, buf);
626 if (ret == GP_ERROR_TIMEOUT)
627 continue;
628 if (ret != GP_OK)
629 return (ret);
630
631 ret = coolshot_read_packet (camera, buf);
632 if (ret == GP_ERROR_TIMEOUT)
633 continue;
634 if (ret != GP_OK)
635 return (ret);
636
637 if (buf[0] == COOLSHOT_ACK)
638 return (GP_OK);
639 else
640 return (GP_ERROR_CORRUPTED_DATA);
641
642 }
643 return (GP_ERROR_TIMEOUT);
644 }
645
646
647 #define WIDTH 40
648 #define HEIGHT 30
649
650 #define Y_ADJ 25
651
652 #define RIND(x, y, w) (((w)*(HEIGHT))+((y/2)*(WIDTH/2))+(x/2))
653 #define BIND(x, y, w) (((w)*(HEIGHT))+((WIDTH/4)*(HEIGHT))+((y/2)*(WIDTH/2))+(x/2))
654
655 int coolshot_build_thumbnail (char *data, int *size)
656 {
657 char thumbnail[32768];
658 char *ptr;
659 unsigned char *udata = (unsigned char *)data;
660 char *src;
661 int length;
662 int x, y;
663 int loop;
664 int Y, U, V;
665
666
667 ptr = thumbnail;
668
669 src = data;
670 x = y = 0;
671
672 for( loop = 0; loop < *size; loop++ ) {
673 if ( x == WIDTH ) {
674 x = 0;
675 y++;
676 }
677
678 if ( y < HEIGHT ) {
679 /*
680 from imagemagick
681 Y = 0.299000*R+0.587000*G+0.114000*B
682 Cb= -0.168736*R-0.331264*G+0.500000*B
683 Cr= 0.500000*R-0.418688*G-0.081316*B
684
685 R = Y +1.402000*Cr
686 G = Y-0.344136*Cb-0.714136*Cr
687 B = Y+1.772000*Cb
688 */
689
690 Y = *src + Y_ADJ;
691 U = udata[RIND(x,y,WIDTH)] - 128;
692 V = udata[BIND(x,y,WIDTH)] - 128;
693
694 ptr[0] = Y + ( 1.402000 * V );
695 ptr[1] = Y - ( 0.344136 * U ) - ( 0.714136 * V );
696 ptr[2] = Y + ( 1.772000 * U );
697 ptr += 3;
698
699 x++;
700 src++;
701 }
702 }
703
704 /* copy the header */
705 sprintf( data,
706 "P6\n"
707 "# CREATOR: gphoto2, panasonic coolshot library\n"
708 "%d %d\n"
709 "255\n", 80, 60 );
710
711 length = strlen( data );
712
713 /* copy the image doubling both height and width */
714 ptr = data + length;
715 for( y = 0; y < HEIGHT; y++ ) {
716 src = thumbnail + (y * WIDTH * 3);
717 for( x = 0; x < WIDTH ; x++, src += 3 ) {
718 ptr[0] = src[0]; ptr[1] = src[1]; ptr[2] = src[2]; ptr += 3;
719 ptr[0] = src[0]; ptr[1] = src[1]; ptr[2] = src[2]; ptr += 3;
720 }
721 src = thumbnail + (y * WIDTH * 3);
722 for( x = 0; x < WIDTH ; x++, src += 3 ) {
723 ptr[0] = src[0]; ptr[1] = src[1]; ptr[2] = src[2]; ptr += 3;
724 ptr[0] = src[0]; ptr[1] = src[1]; ptr[2] = src[2]; ptr += 3;
725 }
726 }
727
728 length += WIDTH * HEIGHT * 3 * 4;
729
730 *size = length;
731
732 return( GP_OK );
733 }