/
casio-qv-commands.c
364 lines (294 loc) · 6.88 KB
/
casio-qv-commands.c
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
/* casio-qv-commands.c
*
* Copyright © 2001 Lutz Müller
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, 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, &c, 1));
result = gp_port_read (camera->port, &c, 1);
/* If we got ACK, everything is fine. */
if (result >= 0) {
switch © {
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, &c, 1) >= 0);
break;
default:
while (gp_port_read (camera->port, &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, checksum;
int i;
/* Write the request and calculate the checksum */
CR (gp_port_write (camera->port, cmd, cmd_len));
for (i = 0, checksum = 0; i < cmd_len; i++)
checksum += cmd[i];
/* Read the checksum */
CR (gp_port_read (camera->port, &c, 1));
if (c != ~checksum)
return (GP_ERROR_CORRUPTED_DATA);
/* Send ACK */
c = ACK;
CR (gp_port_write (camera->port, &c, 1));
/* Receive the answer */
if (buf_len)
CR (gp_port_read (camera->port, buf, buf_len));
return (GP_OK);
}
static int
QVblockrecv (Camera *camera, unsigned char **buf, unsigned long int *buf_len)
{
unsigned char c;
unsigned char buffer[2];
int pos = 0, size, retries = 0, i;
unsigned char sum;
/* Send DC2 */
CR (gp_port_write (camera->port, &c, 1));
*buf = NULL;
*buf_len = 0;
while (1) {
/* Read STX */
CR (gp_port_read (camera->port, &c, 1));
if (c != STX) {
retries++;
c = NAK;
CR (gp_port_write (camera->port, &c, 1));
if (retries > CASIO_QV_RETRIES)
return (GP_ERROR_CORRUPTED_DATA);
else
continue;
}
/* Read sector size */
CR (gp_port_read (camera->port, buffer, 2));
size = (buffer[0] << 8) | buffer[1];
sum = buffer[0] + buffer[1];
/* Allocate the memory */
if (!*buf)
buf = malloc (sizeof (char) * size);
else
buf = realloc (buf, sizeof (char) * (*buf_len + size));
if (!buf)
return (GP_ERROR_NO_MEMORY);
*buf_len += size;
/* Get the sector */
CR (gp_port_read (camera->port, *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, buffer, 2));
sum += buffer[0];
/* Verify the checksum */
if (sum != ~buffer[1]) {
c = NAK;
CR (gp_port_write (camera->port, &c, 1));
continue;
}
/* Acknowledge and prepare for next packet */
c = ACK;
CR (gp_port_write (camera->port, &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
QVsetspeed (Camera *camera, QVSpeed speed)
{
unsigned char cmd[3];
cmd[0] = 'C';
cmd[1] = 'B';
cmd[3] = (unsigned char) speed;
CR (QVsend (camera, cmd, 3, NULL, 0));
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[2];
unsigned char b;
cmd[0] = 'D';
cmd[1] = 'Y';
CR (QVsend (camera, cmd, 2, &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;
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
QVgetpic (Camera *camera, unsigned char **data, unsigned long int *size)
{
unsigned char cmd[2];
cmd[0] = 'M';
cmd[1] = 'G';
CR (QVsend (camera, cmd, 2, NULL, 0));
CR (QVblockrecv (camera, data, size));
return (GP_OK);
}
int
QVgetthumb (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;
cmd[3] = 0xff;
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];
cmd[0] = 'D';
cmd[1] = 'R';
CR (QVsend (camera, cmd, 2, NULL, 0));
return (GP_OK);
}