summaryrefslogtreecommitdiff
path: root/camlibs/sonydscf55/sony.c
blob: c8c53b3ec708a5d21138f453c8cc1a1275e93093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* Sony DSC-F55 & MSAC-SR1 - gPhoto2 camera library
 * Copyright (C) 2001 Raymond Penners <raymond@dotsphinx.com>
 * Copyright (C) 2000 Mark Davies <mdavies@dial.pipex.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gphoto2.h>
#include "sony.h"

/**
 * Constants
 */
#define SONY_CONVERSE_RETRY 5

#define SONY_INVALID_CHECKSUM	0x40
#define SONY_INVALID_SEQUENCE	0x41
#define SONY_RESET_SEQUENCE	0x42
#define SONY_RESEND_PACKET	0x43

#define SONY_ESCAPE_CHAR		0x7d
#define SONY_START_CHAR		0xc0
#define SONY_END_CHAR		0xc1

static unsigned char START_PACKET = 192;
static unsigned char END_PACKET = 193;

static char ESC_START_STRING[] = { 0x7d, 0xe0 };
static char ESC_END_STRING[] = { 0x7d, 0xe1 };
static char ESC_ESC_STRING[] = { 0x7d, 0x5d };

static unsigned char PacketCodes[2] = { 192, 193 };

Packet CameraInvalid = { 0, 2, {131, 125}, 93 };
Packet ResendPacket = { 0, 4, {129, 2, '1', 0}, 'L' };


#if defined(USE_ALL_TYPES)
static unsigned char EmailImage[] =
    { 0, 2, 2, 0, 16, '/', 'M', 'S', 'S', 'O', 'N', 'Y', '/', 'I', 'M',
'C',
	'I', 'F', '1', '0', '0'
};
#endif


static unsigned char IdentString[] =
    { 0, 1, 1, 'S', 'O', 'N', 'Y', ' ', ' ', ' ', ' ', ' ' };
static unsigned char EmptyPacket[] = { 0 };	/* null packet */
static unsigned char SetTransferRate[] = { 0, 1, 3, 0 };
static unsigned char SendImageCount[] = { 0, 2, 1 };
static unsigned char StillImage[] =
    { 0, 2, 2, 0, 14, '/', 'D', 'C', 'I', 'M', '/', '1', '0', '0', 'M',
'S',
	'D', 'C', 'F'
};
static unsigned char MpegImage[] =
    { 0, 2, 2, 0, 16, '/', 'M', 'S', 'S', 'O', 'N', 'Y', '/', 'M', 'O',
'M',
	'L', '0', '0', '0', '1'
};
static unsigned char SelectImage[] = { 0, 2, 48, 0, 0, 0, 0 };
static unsigned char SendImage[] = { 0, 2, '1', 0, 1, 0, 0 };
static unsigned char SendNextImagePacket[] = { 0, 2, '1', 0 };
static unsigned char SendThumbnail[] = { 0, 2, '0', 0 };

#if 0
static unsigned char SelectCamera[] = { 0, 1, 2 };
static unsigned char DownloadComplete[] = { 0, 2, '0', 255 };
static unsigned char X10Camera[] = { 0, 1, 5 };

static unsigned char X5Camera[] = { 0, 2, 1 };
static unsigned char X13Camera[] = { 0, 2, 18 };

#endif


/**
 * This array contains the expected packet sequence code to to be applied/
 * checked for.
 */
static const unsigned char sony_sequence[] =
    { 14, 0, 32, 34, 66, 68, 100, 102, 134, 136, 168, 170, 202, 204, 236,
238,
	255
};


#ifdef __linux__
static const int baud_rate = 115200;
#else
# ifdef __sun__
static const int baud_rate = 38400;
# else
static const int baud_rate = 9600;
# endif
#endif





/**
 * Returns transfer rate ID
 */
static int sony_baud_to_id(long baud)
{
	int r;

	switch (baud) {
	case 115200:
		r = 4;
		break;
	case 57600:
		r = 3;
		break;		/* FIXME ??? */
	case 38400:
		r = 2;
		break;		/* works on sun */
	case 19200:
		r = 1;
		break;		/* works on sun */
	default:
	case 9600:
		r = 0;
		break;		/* works on sun */
	}
	return r;
}


/**
 * Reads a byte
 */
static int sony_read_byte(Camera * camera, unsigned char *b)
{
	int n = gp_port_read(camera->port, b, 1);
	if (n != 1)
		return GP_ERROR;
	else
		return GP_OK;
}



/**
 * Returns the checksum for a packet
 */
static unsigned char sony_packet_checksum(Packet * p)
{
	unsigned short int o = 0;
	unsigned long int sum = 0;

	sum = 0;

	while (o < p->length)
		sum += p->buffer[o++];

	return 256 - (sum & 255);
}

/**
 * Returns TRUE iff the packet is valid
 */
static int sony_packet_validate(Camera * camera, Packet * p)
{
	unsigned char c = sony_packet_checksum(p);

	if (c != p->checksum) {
		gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
				"sony_packet_validate: invalid checksum");
		return SONY_INVALID_CHECKSUM;
	}

	if (129 == p->buffer[0]) {
		gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
				"sony_packet_validate: resend packet");
		return SONY_RESEND_PACKET;
	}

	if (sony_sequence[sony_data(camera)->sequence_id] != p->buffer[0]) {
		gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
				"sony_packet_validate: invalid sequence");
		return SONY_INVALID_SEQUENCE;
	}

	return GP_OK;
}

/**
 * Constructs a packet.
 */
static int
sony_packet_make(Camera * camera, Packet * p, unsigned char *buffer,
		 unsigned short int length)
{
	SonyData *data = sony_data(camera);
	p->length = 0;

	while (length--)
		p->buffer[p->length++] = *(buffer++);

	if (255 == sony_sequence[++(data->sequence_id)])
		data->sequence_id = 0;

	p->buffer[0] = sony_sequence[data->sequence_id++];

	if (255 == sony_sequence[data->sequence_id])
		data->sequence_id = 0;

	p->checksum = sony_packet_checksum(p);

	return TRUE;
}

/**
 * Reads a packet.
 */
static int sony_packet_read(Camera * camera, Packet * pack)
{
	unsigned int n;
	unsigned char byte = 0;
	static Packet p;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
			"sony_packet_read()");
	p.length = 0;

	for (n = 0; n < 2; n++) {
		for (byte = 0; byte != (unsigned char) PacketCodes[n];) {
			if (sony_read_byte(camera, &byte) == GP_ERROR) {
				return FALSE;
			}

			if (n > 0) {
				if (SONY_ESCAPE_CHAR == byte) {
					unsigned char extra;

					sony_read_byte(camera, &extra);

					switch (extra) {
					case 1:
					case 7:
					case 0xe1:
					case 0xe0:
						extra &= 0xcf;
						p.buffer[p.length++] =
						    extra;
						continue;
					case 0x5d:
						p.buffer[p.length++] =
						    byte;
						continue;
					default:
						p.buffer[p.length++] =
						    byte;
						p.buffer[p.length++] =
						    extra;
						continue;
					}
				} else
					p.buffer[p.length++] = byte;
			}
		}
	}

	p.length -= 2;
	p.checksum = p.buffer[p.length];

	memcpy(pack, &p, sizeof(Packet));
	return TRUE;
}

/**
 * Sends a packet
 */
static int sony_packet_write(Camera * camera, Packet * p)
{
	unsigned short int count;
	SonyData *data;
	int rc;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
			"sony_packet_write()");
	data = sony_data(camera);

	rc = gp_port_write(camera->port, &START_PACKET, 1);

	p->buffer[p->length] = p->checksum;

	for (count = 0; count < p->length + 1 && rc != GP_ERROR; count++) {
		switch ((unsigned char) p->buffer[count]) {
		case SONY_ESCAPE_CHAR:
			rc = gp_port_write(camera->port, ESC_ESC_STRING, 2);
			break;

		case SONY_START_CHAR:
			rc = gp_port_write(camera->port, ESC_START_STRING, 2);
			break;

		case SONY_END_CHAR:
			rc = gp_port_write(camera->port, ESC_END_STRING, 2);
			break;

		default:
			rc = gp_port_write(camera->port,
					   (char *) &p->buffer[count], 1);
			break;
		}
	}
	if (rc != GP_ERROR)
		rc = gp_port_write(camera->port, (char *) &END_PACKET, 1);
	return rc;
}

/**
 * Communicates packets
 */
static int
sony_converse(Camera * camera, Packet * out, unsigned char *str, int len)
{
	Packet ps;
	char old_sequence = 33;
	int sequence_count = 0;
	int invalid_sequence = 0;
	int count;
	SonyData *data = sony_data(camera);
	int rc;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID, "sony_converse()");
	sony_packet_make(camera, &ps, str, len);

	for (count = 0; count < SONY_CONVERSE_RETRY; count++) {
		rc = sony_packet_write(camera, &ps);

		if (rc == GP_OK) {
			if (sony_packet_read(camera, out)) {
				switch (sony_packet_validate(camera, out)) {
				case SONY_INVALID_CHECKSUM:
					if (invalid_sequence) {
						sony_packet_make(camera,
								 &ps, str,
								 len);
						break;
					}

					gp_debug_printf(GP_DEBUG_LOW,
							SONY_CAMERA_ID,
							"Checksum invalid");
					ps.buffer[0] = 129;
					ps.checksum =
					    sony_packet_checksum(&ps);
					break;

				case SONY_INVALID_SEQUENCE:
					if (sony_data(camera)->msac_sr1) {
						invalid_sequence = 1;
						sony_packet_make(camera,
								 &ps, str,
								 len);
						break;
					}

					if (old_sequence == out->buffer[0])
						sequence_count++;
					else if (0 == sequence_count)
						old_sequence =
						    out->buffer[0];

					if (sequence_count == 4) {
						gp_debug_printf
						    (GP_DEBUG_LOW,
						     SONY_CAMERA_ID,
						     "Attempting to reset sequence id - image may be corrupt.");
						data->sequence_id = 0;

						while (sony_sequence
						       [data->
							sequence_id] !=
						       old_sequence)
							data->
							    sequence_id++;

						return GP_OK;
					}

					gp_debug_printf(GP_DEBUG_LOW,
							SONY_CAMERA_ID,
							"Invalid Sequence");
					ps.buffer[0] = 129;
					ps.checksum =
					    sony_packet_checksum(&ps);
					break;

				case SONY_RESET_SEQUENCE:
					data->sequence_id = 0;
					return GP_OK;

				case SONY_RESEND_PACKET:
					gp_debug_printf(GP_DEBUG_LOW,
							SONY_CAMERA_ID,
							"Resending Packet");
					break;

				case GP_OK:
					return GP_OK;

				default:
					gp_debug_printf(GP_DEBUG_LOW,
							SONY_CAMERA_ID,
							"Unknown Error");
					break;
				}
			} else {
				/*                                printf("Incomplete packet\n"); */
				ps.buffer[0] = 129;
				ps.checksum = sony_packet_checksum(&ps);
			}
		}
	}

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
			"Failed to read packet during transfer.");

	return GP_ERROR;
}


/**
 * Sets baud rate
 */
static int sony_baud_port_set(Camera * camera, long baud)
{
	gp_port_settings settings;

	gp_port_settings_get(camera->port, &settings);
	settings.serial.speed = baud;
	gp_port_settings_set(camera->port, settings);

	usleep(70000);

	return GP_OK;
}

/**
 * Sets baud rate
 */
static int sony_baud_set(Camera * camera, long baud)
{
	Packet dp;
	int rc;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID, "sony_baud_set(%ld)",
			baud);
	// FIXME
	SetTransferRate[3] = sony_baud_to_id(baud);

	rc = sony_converse(camera, &dp, SetTransferRate, 4);
	if (rc == GP_OK) {
		sony_baud_port_set(camera, baud);
		rc = sony_converse(camera, &dp, EmptyPacket, 1);
		usleep(100000);	/* 50000 was good too, jw */
	}
	return rc;
}

/**
 * Port initialisation
 */
static int sony_init_port (Camera *camera)
{
	gp_port_settings settings;
	int rc;
	
	rc = gp_port_timeout_set (camera->port, 5000);
	if (rc == GP_OK) { 
		strcpy(settings.serial.port, camera->port_info->path);

		settings.serial.speed = 9600;
		settings.serial.bits = 8;
		settings.serial.parity = 0;
		settings.serial.stopbits = 1;

		rc = gp_port_settings_set(camera->port, settings);
		if (rc == GP_OK) {
			rc = gp_port_flush(camera->port, 0);
		}
	}
	return rc;
}

/**
 * Establish first contact (remember the prime directive? :)
 */
static int sony_init_first_contact (Camera *camera)
{
	int count = 0;
	Packet dp;
	SonyData *data = sony_data (camera);
	int rc = GP_ERROR;
	
	for (count = 0; count < 3; count++) {
		data->sequence_id = 0;

		rc = sony_converse(camera, &dp, IdentString, 12);
		if (rc == GP_OK) {
			gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
					"Init OK");
			break;
		}
		usleep(2000);
		gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID,
				"Init - Fail %u", count + 1);
	}
	return rc;
}

/**
 * Initialises camera
 */
int sony_init (Camera * camera, int msac)
{
	int rc;

	rc = sony_init_port (camera);
	if (rc == GP_OK) {
		SonyData *data;
		data = (SonyData *) malloc(sizeof(SonyData));
		camera->camlib_data = data;
		if (data) {
			rc = sony_init_first_contact (camera);
			if (rc != GP_OK) {
				free (data);
				camera->camlib_data = NULL;
			}
		}
		else {
			rc = GP_ERROR;
		}
	}
	return rc;
}

/**
 * Reset the camera sequence count and baud rate.
 */
int sony_exit(Camera * camera)
{
	Packet dp;
	int rc = GP_ERROR;

	if (camera->camlib_data) {
		SonyData *data = sony_data(camera);
		rc = sony_baud_set(camera, 9600);
		while (rc == GP_OK && data->sequence_id > 0) {
			rc = sony_converse(camera, &dp, EmptyPacket, 1);
		}
		free (camera->camlib_data);
		camera->camlib_data = NULL;
	}
	return rc;
}

/**
 * Return count of images taken.
 */
static int
sony_item_count(Camera * camera, unsigned char *from, int from_len)
{
	Packet dp;
	int rc;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID, "sony_item_count()");
	rc = sony_converse(camera, &dp, SetTransferRate, 4);
	if (rc == GP_OK) {
		rc = sony_converse(camera, &dp, from, from_len);
		if (rc == GP_OK) {
			rc = sony_converse(camera, &dp, SendImageCount, 3);
			if (rc == GP_OK) {
				int nr = (int) dp.buffer[5];
				gp_debug_printf(GP_DEBUG_LOW,
						SONY_CAMERA_ID,
						"count = %d", nr);
				return nr;
			}
		}
	}
	return GP_ERROR;
}


/**
 * Returns number of still images
 */
int sony_image_count(Camera * camera)
{
	return sony_item_count(camera, StillImage, sizeof(StillImage));
}


/**
 * Returns number of still images
 */
int sony_mpeg_count(Camera * camera)
{
	return sony_item_count(camera, MpegImage, sizeof(MpegImage));
}


/**
 * Fetches an image.
 */
static int
sony_file_get(Camera * camera, int imageid, int thumbnail,
	      CameraFile * file)
{
	int sc;			/* count of bytes to skip at start of packet */
	Packet dp;
	int rc;

	gp_debug_printf(GP_DEBUG_LOW, SONY_CAMERA_ID, "sony_file_get()");
	rc = gp_file_clean(file);
	if (rc == GP_OK) {
		gp_file_set_mime_type (file, GP_MIME_JPEG);
		// FIXME: There appears to be a file name at dp.buffer+5
		// after a SelectImage. We could use that for the real
		// file name instead of our own mockup ("dsc%d.jpg").
		sprintf(file->name, SONY_FILE_NAME_FMT, imageid);

		sony_baud_set(camera, baud_rate);

		rc = sony_converse(camera, &dp, StillImage, 19);
		if (rc == GP_OK) {
			if (thumbnail) {
				sc = 0x247;

				// FIXME
				SelectImage[4] = imageid;
				sony_converse(camera, &dp, SelectImage, 7);

				gp_debug_printf(GP_DEBUG_LOW,
						SONY_CAMERA_ID,
						"XYZ %11.11s",
						dp.buffer + 5);

				if (sony_data(camera)->msac_sr1) {
					gp_file_append(file,
						       "\xff\xd8\xff", 3);
				}

				for (;;) {
					sony_converse(camera, &dp,
						      SendThumbnail, 4);

					gp_file_append(file,
						       (char *) dp.buffer +
						       sc, dp.length - sc);
					sc = 7;

					if (3 == dp.buffer[4])
						break;
				}
			} else {
				sc = 11;

				// FIXME
				SendImage[4] = imageid;
				sony_converse(camera, &dp, SendImage, 7);

				for (;;) {
					gp_file_append(file,
						       (char *) dp.buffer +
						       sc, dp.length - sc);
					sc = 7;

					if (3 == dp.buffer[4])
						break;

					sony_converse(camera, &dp,
						      SendNextImagePacket,
						      4);
				}
			}
		}
		sony_baud_set(camera, 9600);

		if (rc != GP_OK) {
			gp_file_clean(file);
		}
	}
	return rc;
}

/**
 * Fetches an image.
 */
int sony_thumbnail_get(Camera * camera, int imageid, CameraFile * file)
{
	return sony_file_get(camera, imageid, TRUE, file);
}

/**
 * Fetches an image.
 */
int sony_image_get(Camera * camera, int imageid, CameraFile * file)
{
	return sony_file_get(camera, imageid, FALSE, file);
}


/**
 * Fetches image details.
 */
int sony_image_info(Camera * camera, int imageid, CameraFileInfo * info)
{
	unsigned int l = 0;
	int rc;
	Packet dp;

	// FIXME
	SelectImage[4] = imageid;
	rc = sony_converse(camera, &dp, SelectImage, 7);
	if (rc == GP_OK) {
		l = (l << 8) | dp.buffer[16];
		l = (l << 8) | dp.buffer[17];
		l = (l << 8) | dp.buffer[18];
		l = (l << 8) | dp.buffer[19];

		info->file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_TYPE;
		info->file.size = l;
		strcpy (info->file.type, GP_MIME_JPEG);

		info->preview.fields = 0;
	}
	return rc;
}



/*
 * Local Variables:
 * c-file-style:"linux"
 * indent-tabs-mode:t
 * End:
 */