summaryrefslogtreecommitdiff
path: root/camlibs/casio/casio-qv-commands.c
blob: 2feff9aafe66d150cce87ec92ec6a0f21f451007 (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
/* casio-qv-commands.c
 *
 * Copyright 2001 Lutz Mueller
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */
#include "config.h"
#include "casio-qv-commands.h"

#include <stdlib.h>

#define STX 0x02
#define ETX 0x03
#define ENQ 0x05
#define ACK 0x06
#define DC2 0x12
#define NAK 0x15
#define ETB 0x17
#define UNKNOWN1 0xfe
#define UNKNOWN2 0xe0

#define CASIO_QV_RETRIES 5

#define CR(result) {int r = (result); if (r < 0) return (r);}

int
QVping (Camera *camera)
{
	unsigned char c;
	int result = GP_OK, i = 0;

	/* Send ENQ and wait for ACK */
	while (1) {
		c = ENQ;
		CR (gp_port_write (camera->port, (char *)&c, 1));
		result = gp_port_read (camera->port, (char *)&c, 1);

		/* If we got ACK, everything is fine. */
		if (result >= 0) {
			switch (c) {
			case ACK:
			case ENQ:

				/*
				 * According to gphoto, we need to wait
				 * for ACK. But the camera of
				 * David Wolfskill <david@catwhisker.org>
				 * seems to return ENQ after some NAK.
				 */
				return (GP_OK);

			case NAK:

				/* The camera is not yet ready. */
				break;

			case UNKNOWN1:
			case UNKNOWN2:

				/*
				 * David Wolfskill <david@catwhisker.org>
				 * has seen those two bytes if one sends
				 * only ENQs to the camera. The camera first
				 * answers with some NAKs, then with some
				 * ACKs, and finally with UNKNOWN1 and
				 * UNKNOWN2.
				 */
				while (gp_port_read (camera->port, (char *)&c, 1) >= 0);
				break;

			default:
				while (gp_port_read (camera->port, (char *)&c, 1) >= 0);
				break;
			}
		}

		if (++i < CASIO_QV_RETRIES)
			continue;

		/* If we got an error from libgphoto2_port, pass it along */
		CR (result);

		/* Return some error code */
		return (GP_ERROR_CORRUPTED_DATA);
	}
}

static int
QVsend (Camera *camera, unsigned char *cmd, int cmd_len,
			       unsigned char *buf, int buf_len)
{
	unsigned char c;
	int checksum;
	const unsigned char *cmd_end;

	/* The camera does not insist on a ping each command, but */
	/* sometimes it hangs up without one.                     */
	CR (QVping (camera));

	/* Write the request and calculate the checksum */
	CR (gp_port_write (camera->port, (char *)cmd, cmd_len));
	for (cmd_end = cmd+cmd_len, checksum = 0; cmd < cmd_end; ++cmd)
		checksum += *cmd;

	/* Read the checksum */
	CR (gp_port_read (camera->port, (char *)&c, 1));
	if (c != (unsigned char)(~checksum))
		return (GP_ERROR_CORRUPTED_DATA);

	/* Send ACK */
	c = ACK;
	CR (gp_port_write (camera->port, (char *)&c, 1));

	/* Receive the answer */
	if (buf_len)
		CR (gp_port_read (camera->port, (char *)buf, buf_len));

	return (GP_OK);
}

static int
QVblockrecv (Camera *camera, unsigned char **buf, unsigned long int *buf_len)
{
	/* XXX - does the caller know to free *buf in case of an error? */
	unsigned char c;
	int retries, pos;

	retries = 0;
	*buf = NULL;
	*buf_len = 0;
	pos = 0;

	/* Send DC2 */
	c = DC2;
	CR (gp_port_write (camera->port, (char *)&c, 1));

	while (1) {
		unsigned char buffer[2];
		int size, i;
		int sum;
		unsigned char *new;

		/* Read STX */
		CR (gp_port_read (camera->port, (char *)&c, 1));
		if (c != STX) {
			retries++;
			c = NAK;
			CR (gp_port_write (camera->port, (char *)&c, 1));
			if (retries > CASIO_QV_RETRIES)
				return (GP_ERROR_CORRUPTED_DATA);
			else
				continue;
		}

		/* Read sector size */
		CR (gp_port_read (camera->port, (char *)buffer, 2));
		size = (buffer[0] << 8) | buffer[1];
		sum = buffer[0] + buffer[1];

		/* Allocate the memory */
		new = (unsigned char*)realloc (*buf, sizeof (char) * (*buf_len + size));
		if (new == (unsigned char*)0) {
			if (*buf != (unsigned char*)0) free(*buf);
			return (GP_ERROR_NO_MEMORY);
		}
		*buf = new;
		*buf_len += size;

		/* Get the sector */
		CR (gp_port_read (camera->port, (char *)*buf + pos, size));
		for (i = 0; i < size; i++)
			sum += (*buf)[i + pos];

		/* Get EOT or ETX and the checksum */
		CR (gp_port_read (camera->port, (char *)buffer, 2));
		sum += buffer[0];

		/* Verify the checksum */
		if ((unsigned char)(~sum) != buffer[1]) {
			retries++;
			c = NAK;
			CR (gp_port_write (camera->port, (char *)&c, 1));
			if (retries > CASIO_QV_RETRIES)
				return (GP_ERROR_CORRUPTED_DATA);
			else
				continue;
		}

		/* Acknowledge and prepare for next packet */
		c = ACK;
		CR (gp_port_write (camera->port, (char *)&c, 1));
		pos += size;

		/* Are we done? */
		if (buffer[0] == ETX)
			break;		/* Yes */
		else if (buffer[0] == ETB)
			continue;	/* No  */
		else
			return (GP_ERROR_CORRUPTED_DATA);
	}

	return (GP_OK);
}

int
QVbattery (Camera *camera, float *battery)
{
	unsigned char cmd[6];
	unsigned char b;

	cmd[0] = 'R';
	cmd[1] = 'B';
	cmd[2] = ENQ;
	cmd[3] = 0xff;
	cmd[4] = 0xfe;
	cmd[5] = 0xe6;
	CR (QVsend (camera, cmd, 6, &b, 1));
	*battery = b / 16.;

	return (GP_OK);
}

int
QVrevision (Camera *camera, long int *revision)
{
	unsigned char cmd[2];
	unsigned char buf[4];

	cmd[0] = 'S';
	cmd[1] = 'U';
	CR (QVsend (camera, cmd, 2, buf, 4));
	*revision = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];

	return (GP_OK);
}

int
QVnumpic (Camera *camera)
{
	unsigned char cmd[2];
	unsigned char b;

	cmd[0] = 'M';
	cmd[1] = 'P';
	CR (QVsend (camera, cmd, 2, &b, 1));

	return (b);
}

int
QVpicattr (Camera *camera, int n, unsigned char *picattr)
{
	unsigned char cmd[4];
	unsigned char b;

	cmd[0] = 'D';
	cmd[1] = 'Y';
        cmd[2] = STX;
        cmd[3] = n+1;
	CR (QVsend (camera, cmd, 4, &b, 1));
	*picattr = b;

	return (GP_OK);
}

int
QVshowpic (Camera *camera, int n)
{
	unsigned char cmd[3];

	cmd[0] = 'D';
	cmd[1] = 'A';
	cmd[2] = n+1;
	CR (QVsend (camera, cmd, 3, NULL, 0));

	return (GP_OK);
}

int
QVsetpic (Camera *camera)
{
	unsigned char cmd[2];

	cmd[0] = 'D';
	cmd[1] = 'L';
	CR (QVsend (camera, cmd, 2, NULL, 0));

	return (GP_OK);
}

int
QVgetCAMpic (Camera *camera, unsigned char **data, unsigned long int *size, int fine)
{
	unsigned char cmd[2];

	cmd[0] = 'M';
	cmd[1] = fine ? 'g' : 'G';
	CR (QVsend (camera, cmd, 2, NULL, 0));
	CR (QVblockrecv (camera, data, size));

	return (GP_OK);
}

int
QVgetYCCpic (Camera *camera, unsigned char **data, unsigned long int *size)
{
	unsigned char cmd[2];

	cmd[0] = 'M';
	cmd[1] = 'K';
	CR (QVsend (camera, cmd, 2, NULL, 0));
	CR (QVblockrecv (camera, data, size));

	return (GP_OK);
}

int
QVdelete (Camera *camera, int n)
{
	unsigned char cmd[4];

	cmd[0] = 'D';
	cmd[1] = 'F';
	cmd[2] = n+1;
	cmd[3] = 0xff;
	CR (QVsend (camera, cmd, 4, NULL, 0));

	return (GP_OK);
}

int
QVprotect (Camera *camera, int n, int on)
{
	unsigned char cmd[4];

	cmd[0] = 'D';
	cmd[1] = 'Y';
	cmd[2] = on ? 1 : 0;
	cmd[3] = n+1;
	CR (QVsend (camera, cmd, 4, NULL, 0));

	return (GP_OK);
}

int
QVsize (Camera *camera, long int *size)
{
	unsigned char cmd[2];
	unsigned char buf[4];

	cmd[0] = 'E';
	cmd[1] = 'M';
	CR (QVsend (camera, cmd, 2, buf, 4));
	*size = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];

	return (GP_OK);
}

int
QVcapture (Camera *camera)
{
	unsigned char cmd[2];
	unsigned char b;

	cmd[0] = 'D';
	cmd[1] = 'R';
	CR (QVsend (camera, cmd, 2, &b, 1));

	return (GP_OK);
}

int
QVstatus (Camera *camera, char *status)
{
        unsigned char cmd[3];

        cmd[0] = 'D';
        cmd[1] = 'S';
        cmd[2] = STX;
        CR (QVsend (camera, cmd, 3, (unsigned char *)status, 2));

        return (GP_OK);
}

int
QVreset (Camera *camera)
{
        unsigned char cmd[2];

	cmd[0] = 'Q';
	cmd[1] = 'R';
	CR (QVsend (camera, cmd, 2, NULL, 0));

        return (GP_OK);
}

int
QVsetspeed (Camera *camera, int speed)
{
	unsigned char cmd[3];
        gp_port_settings settings;

	cmd[0] = 'C';
	cmd[1] = 'B';
	switch (speed) {
	case   9600: cmd[2] = 46; break;
	case  19200: cmd[2] = 22; break;
	case  38400: cmd[2] = 11; break;
	case  57600: cmd[2] =  7; break;
	case 115200: cmd[2] =  3; break;
	default: return (GP_ERROR_NOT_SUPPORTED);
	}
	CR (QVsend (camera, cmd, 3, NULL, 0));

        CR (gp_port_get_settings (camera->port, &settings));
        settings.serial.speed = speed;
        CR (gp_port_set_settings (camera->port, settings));

	CR (QVping (camera));

	return (GP_OK);
}