Debian Codegraph @

libgphoto2_port/vusb/vcamera.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* camera.c
3 *
4 * Copyright (c) 2015-2017 Marcus Meissner <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21 #define _DARWIN_C_SOURCE
22 #include "config.h"
23 #include <gphoto2/gphoto2-port-library.h>
24 #include <gphoto2/gphoto2-port-portability.h>
25
26 #ifdef HAVE_LIBEXIF
27 # include <libexif/exif-data.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <sys/time.h>
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
37 #endif
38 #include <string.h>
39 #include <stdint.h>
40
41 #include "vcamera.h"
42
43 #include <gphoto2/gphoto2-port.h>
44 #include <gphoto2/gphoto2-port-result.h>
45 #include <gphoto2/gphoto2-port-log.h>
46
47 #include "libgphoto2_port/i18n.h"
48
49
50 #define CHECK(result) {int r=(result); if (r<0) return (r);}
51
52 static int ptp_inject_interrupt(vcamera*cam, int when, uint16_t code, int nparams, uint32_t param1, uint32_t transid);
53
54 static uint32_t get_32bit_le(unsigned char *data) {
55 return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
56 }
57
58 static uint16_t get_16bit_le(unsigned char *data) {
59 return data[0] | (data[1] << 8);
60 }
61
62 static uint8_t get_8bit_le(unsigned char *data) {
63 return data[0];
64 }
65
66 static int8_t get_i8bit_le(unsigned char *data) {
67 return data[0];
68 }
69
70
71 static int put_64bit_le(unsigned char *data, uint64_t x) {
72 data[0] = x & 0xff;
73 data[1] = (x>>8) & 0xff;
74 data[2] = (x>>16) & 0xff;
75 data[3] = (x>>24) & 0xff;
76 data[4] = (x>>32) & 0xff;
77 data[5] = (x>>40) & 0xff;
78 data[6] = (x>>48) & 0xff;
79 data[7] = (x>>56) & 0xff;
80 return 8;
81 }
82
83 static int put_32bit_le(unsigned char *data, uint32_t x) {
84 data[0] = x & 0xff;
85 data[1] = (x>>8) & 0xff;
86 data[2] = (x>>16) & 0xff;
87 data[3] = (x>>24) & 0xff;
88 return 4;
89 }
90
91 static int put_16bit_le(unsigned char *data, uint16_t x) {
92 data[0] = x & 0xff;
93 data[1] = (x>>8) & 0xff;
94 return 2;
95 }
96
97 static int put_8bit_le(unsigned char *data, uint8_t x) {
98 data[0] = x;
99 return 1;
100 }
101
102 static int put_string(unsigned char *data, char *str) {
103 int i;
104
105 if (!str) { /* empty string, just has length 0 */
106 data[0] = 0;
107 return 1;
108 }
109 if (strlen(str)+1>255)
110 gp_log (GP_LOG_ERROR, "put_string", "string length is longer than 255 characters");
111
112 data[0] = strlen (str)+1;
113 for (i=0;i<data[0];i++)
114 put_16bit_le(data+1+2*i,str[i]);
115
116 return 1+(strlen(str)+1)*2;
117 }
118
119 static char * get_string(unsigned char *data) {
120 int i, len;
121 char *x;
122
123 len = data[0];
124 x = malloc(len+1);
125 x[len] = 0;
126
127 for (i=0;i<len;i++)
128 x[i] = get_16bit_le(data+1+2*i);
129
130 return x;
131 }
132
133
134 static int put_16bit_le_array(unsigned char *data, uint16_t *arr, int cnt) {
135 int x = 0, i;
136
137 x += put_32bit_le (data,cnt);
138 for (i=0;i<cnt;i++)
139 x += put_16bit_le (data+x, arr[i]);
140 return x;
141 }
142
143 static int put_32bit_le_array(unsigned char *data, uint32_t *arr, int cnt) {
144 int x = 0, i;
145
146 x += put_32bit_le (data,cnt);
147 for (i=0;i<cnt;i++)
148 x += put_32bit_le (data+x, arr[i]);
149 return x;
150 }
151
152 static void
153 ptp_senddata(vcamera *cam, uint16_t code, unsigned char *data, int bytes) {
154 unsigned char *offset;
155 int size = bytes + 12;
156
157 if (!cam->inbulk) {
158 cam->inbulk = malloc(size);
159 } else {
160 cam->inbulk = realloc(cam->inbulk,cam->nrinbulk+size);
161 }
162 offset = cam->inbulk + cam->nrinbulk;
163 cam->nrinbulk += size;
164
165 put_32bit_le(offset,size);
166 put_16bit_le(offset+4,0x2);
167 put_16bit_le(offset+6,code);
168 put_32bit_le(offset+8,cam->seqnr);
169 memcpy(offset+12,data,bytes);
170 }
171
172 static void
173 ptp_response(vcamera *cam, uint16_t code, int nparams, ...) {
174 unsigned char *offset;
175 int i, x = 0;
176 va_list args;
177
178 if (!cam->inbulk) {
179 cam->inbulk = malloc(12 + nparams*4);
180 } else {
181 cam->inbulk = realloc(cam->inbulk,cam->nrinbulk+12+nparams*4);
182 }
183 offset = cam->inbulk + cam->nrinbulk;
184 cam->nrinbulk += 12+nparams*4;
185 x += put_32bit_le(offset+x,12+nparams*4);
186 x += put_16bit_le(offset+x,0x3);
187 x += put_16bit_le(offset+x,code);
188 x += put_32bit_le(offset+x,cam->seqnr);
189
190 va_start(args, nparams);
191 for (i=0;i<nparams;i++)
192 x += put_32bit_le (offset+x, va_arg(args, uint32_t));
193 va_end(args);
194
195 cam->seqnr++;
196 }
197
198 #define PTP_RC_OK 0x2001
199 #define PTP_RC_GeneralError 0x2002
200 #define PTP_RC_SessionNotOpen 0x2003
201 #define PTP_RC_OperationNotSupported 0x2005
202 #define PTP_RC_InvalidStorageId 0x2008
203 #define PTP_RC_InvalidObjectHandle 0x2009
204 #define PTP_RC_DevicePropNotSupported 0x200A
205 #define PTP_RC_InvalidObjectFormatCode 0x200B
206 #define PTP_RC_StoreFull 0x200C
207 #define PTP_RC_ObjectWriteProtected 0x200D
208 #define PTP_RC_StoreReadOnly 0x200E
209 #define PTP_RC_AccessDenied 0x200F
210 #define PTP_RC_NoThumbnailPresent 0x2010
211 #define PTP_RC_StoreNotAvailable 0x2013
212 #define PTP_RC_SpecificationByFormatUnsupported 0x2014
213 #define PTP_RC_InvalidParentObject 0x201A
214 #define PTP_RC_InvalidDevicePropFormat 0x201B
215 #define PTP_RC_InvalidParameter 0x201D
216 #define PTP_RC_SessionAlreadyOpened 0x201E
217
218 #define CHECK_PARAM_COUNT(x) \
219 if (ptp->nparams != x) { \
220 gp_log (GP_LOG_ERROR, __FUNCTION__,"params should be %d, but is %d", x, ptp->nparams); \
221 ptp_response (cam, PTP_RC_GeneralError, 0); \
222 return 1; \
223 }
224
225 #define CHECK_SEQUENCE_NUMBER() \
226 if (ptp->seqnr != cam->seqnr) { \
227 /* not clear if normal cameras react like this */ \
228 gp_log (GP_LOG_ERROR, __FUNCTION__, "seqnr %d was sent, expected was %d", ptp->seqnr, cam->seqnr);\
229 ptp_response (cam,PTP_RC_GeneralError,0); \
230 return 1; \
231 }
232
233 #define CHECK_SESSION() \
234 if (!cam->session) { \
235 gp_log (GP_LOG_ERROR,__FUNCTION__,"session is not open"); \
236 ptp_response(cam, PTP_RC_SessionNotOpen, 0); \
237 return 1; \
238 }
239
240 static int ptp_opensession_write(vcamera *cam, ptpcontainer *ptp);
241 static int ptp_closesession_write(vcamera *cam, ptpcontainer *ptp);
242 static int ptp_deviceinfo_write(vcamera *cam, ptpcontainer *ptp);
243 static int ptp_getnumobjects_write(vcamera *cam, ptpcontainer *ptp);
244 static int ptp_getobjecthandles_write(vcamera *cam, ptpcontainer *ptp);
245 static int ptp_getstorageids_write(vcamera *cam, ptpcontainer *ptp);
246 static int ptp_getstorageinfo_write(vcamera *cam, ptpcontainer *ptp);
247 static int ptp_getobjectinfo_write(vcamera *cam, ptpcontainer *ptp);
248 static int ptp_getobject_write(vcamera *cam, ptpcontainer *ptp);
249 static int ptp_getthumb_write(vcamera *cam, ptpcontainer *ptp);
250 static int ptp_deleteobject_write(vcamera *cam, ptpcontainer *ptp);
251 static int ptp_getdevicepropdesc_write(vcamera *cam, ptpcontainer *ptp);
252 static int ptp_getdevicepropvalue_write(vcamera *cam, ptpcontainer *ptp);
253 static int ptp_setdevicepropvalue_write(vcamera *cam, ptpcontainer *ptp);
254 static int ptp_setdevicepropvalue_write_data(vcamera *cam, ptpcontainer *ptp, unsigned char*data, unsigned int len);
255 static int ptp_initiatecapture_write(vcamera *cam, ptpcontainer *ptp);
256 static int ptp_vusb_write(vcamera *cam, ptpcontainer *ptp);
257 static int ptp_nikon_setcontrolmode_write(vcamera *cam, ptpcontainer *ptp);
258
259 static struct ptp_function {
260 int code;
261 int (*write)(vcamera *cam, ptpcontainer *ptp);
262 int (*write_data)(vcamera *cam, ptpcontainer *ptp, unsigned char *data, unsigned int size);
263 } ptp_functions_generic[] = {
264 {0x1001, ptp_deviceinfo_write, NULL },
265 {0x1002, ptp_opensession_write, NULL },
266 {0x1003, ptp_closesession_write, NULL },
267 {0x1004, ptp_getstorageids_write, NULL },
268 {0x1005, ptp_getstorageinfo_write, NULL },
269 {0x1006, ptp_getnumobjects_write, NULL },
270 {0x1007, ptp_getobjecthandles_write, NULL },
271 {0x1008, ptp_getobjectinfo_write, NULL },
272 {0x1009, ptp_getobject_write, NULL },
273 {0x100A, ptp_getthumb_write, NULL },
274 {0x100B, ptp_deleteobject_write, NULL },
275 {0x100E, ptp_initiatecapture_write, NULL },
276 {0x1014, ptp_getdevicepropdesc_write, NULL },
277 {0x1015, ptp_getdevicepropvalue_write, NULL },
278 {0x1016, ptp_setdevicepropvalue_write, ptp_setdevicepropvalue_write_data },
279 {0x9999, ptp_vusb_write, NULL },
280 };
281
282 static struct ptp_function ptp_functions_nikon_dslr[] = {
283 {0x90c2, ptp_nikon_setcontrolmode_write, NULL },
284 };
285
286 static struct ptp_map_functions {
287 vcameratype type;
288 struct ptp_function *functions;
289 unsigned int nroffunctions;
290 } ptp_functions[] = {
291 {GENERIC_PTP, ptp_functions_generic, sizeof(ptp_functions_generic)/sizeof(ptp_functions_generic[0])},
292 {NIKON_D750, ptp_functions_nikon_dslr, sizeof(ptp_functions_nikon_dslr)/sizeof(ptp_functions_nikon_dslr[0])},
293 };
294
295 typedef union _PTPPropValue {
296 char *str; /* common string, malloced */
297 uint8_t u8;
298 int8_t i8;
299 uint16_t u16;
300 int16_t i16;
301 uint32_t u32;
302 int32_t i32;
303 uint64_t u64;
304 int64_t i64;
305 /* XXXX: 128 bit signed and unsigned missing */
306 struct array {
307 uint32_t count;
308 union _PTPPropValue *v; /* malloced, count elements */
309 } a;
310 } PTPPropValue;
311
312 struct _PTPPropDescRangeForm {
313 PTPPropValue MinValue;
314 PTPPropValue MaxValue;
315 PTPPropValue StepSize;
316 };
317 typedef struct _PTPPropDescRangeForm PTPPropDescRangeForm;
318
319 /* Property Describing Dataset, Enum Form */
320
321 struct _PTPPropDescEnumForm {
322 uint16_t NumberOfValues;
323 PTPPropValue *SupportedValue; /* malloced */
324 };
325 typedef struct _PTPPropDescEnumForm PTPPropDescEnumForm;
326
327 /* Device Property Describing Dataset (DevicePropDesc) */
328
329 struct _PTPDevicePropDesc {
330 uint16_t DevicePropCode;
331 uint16_t DataType;
332 uint8_t GetSet;
333 PTPPropValue DefaultValue;
334 PTPPropValue CurrentValue;
335 uint8_t FormFlag;
336 union {
337 PTPPropDescEnumForm Enum;
338 PTPPropDescRangeForm Range;
339 } FORM;
340 };
341 typedef struct _PTPDevicePropDesc PTPDevicePropDesc;
342
343 // We need these (modified) helpers from ptp.c.
344 // Perhaps vcamera.c should be moved to camlibs/ptp2 for easier sharing
345 // in the future.
346 static void
347 ptp_free_propvalue(uint16_t dt, PTPPropValue* dpd)
348 {
349 if (dt == /* PTP_DTC_STR */ 0xFFFF) {
350 free(dpd->str);
351 } else if (dt & /* PTP_DTC_ARRAY_MASK */ 0x4000) {
352 free(dpd->a.v);
353 }
354 }
355
356 static void
357 ptp_free_devicepropdesc(PTPDevicePropDesc* dpd)
358 {
359 uint16_t i;
360
361 ptp_free_propvalue (dpd->DataType, &dpd->DefaultValue);
362 ptp_free_propvalue (dpd->DataType, &dpd->CurrentValue);
363 switch (dpd->FormFlag) {
364 case /* PTP_DPFF_Range */ 0x01:
365 ptp_free_propvalue (dpd->DataType, &dpd->FORM.Range.MinValue);
366 ptp_free_propvalue (dpd->DataType, &dpd->FORM.Range.MaxValue);
367 ptp_free_propvalue (dpd->DataType, &dpd->FORM.Range.StepSize);
368 break;
369 case /* PTP_DPFF_Enumeration */ 0x02:
370 if (dpd->FORM.Enum.SupportedValue) {
371 for (i=0;i<dpd->FORM.Enum.NumberOfValues;i++)
372 ptp_free_propvalue (dpd->DataType, dpd->FORM.Enum.SupportedValue+i);
373 free (dpd->FORM.Enum.SupportedValue);
374 }
375 }
376 }
377
378 static int ptp_battery_getdesc(vcamera*,PTPDevicePropDesc*);
379 static int ptp_battery_getvalue(vcamera*,PTPPropValue*);
380 static int ptp_imagesize_getdesc(vcamera*,PTPDevicePropDesc*);
381 static int ptp_imagesize_getvalue(vcamera*,PTPPropValue*);
382 static int ptp_datetime_getdesc(vcamera*,PTPDevicePropDesc*);
383 static int ptp_datetime_getvalue(vcamera*,PTPPropValue*);
384 static int ptp_datetime_setvalue(vcamera*,PTPPropValue*);
385 static int ptp_shutterspeed_getdesc(vcamera*,PTPDevicePropDesc*);
386 static int ptp_shutterspeed_getvalue(vcamera*,PTPPropValue*);
387 static int ptp_shutterspeed_setvalue(vcamera*,PTPPropValue*);
388 static int ptp_fnumber_getdesc(vcamera*,PTPDevicePropDesc*);
389 static int ptp_fnumber_getvalue(vcamera*,PTPPropValue*);
390 static int ptp_fnumber_setvalue(vcamera*,PTPPropValue*);
391 static int ptp_exposurebias_getdesc(vcamera*,PTPDevicePropDesc*);
392 static int ptp_exposurebias_getvalue(vcamera*,PTPPropValue*);
393 static int ptp_exposurebias_setvalue(vcamera*,PTPPropValue*);
394
395 static struct ptp_property {
396 int code;
397 int (*getdesc )(vcamera *cam, PTPDevicePropDesc*);
398 int (*getvalue)(vcamera *cam, PTPPropValue*);
399 int (*setvalue)(vcamera *cam, PTPPropValue*);
400 } ptp_properties[] = {
401 {0x5001, ptp_battery_getdesc, ptp_battery_getvalue, NULL },
402 {0x5003, ptp_imagesize_getdesc, ptp_imagesize_getvalue, NULL },
403 {0x5007, ptp_fnumber_getdesc, ptp_fnumber_getvalue, ptp_fnumber_setvalue },
404 {0x5010, ptp_exposurebias_getdesc, ptp_exposurebias_getvalue, ptp_exposurebias_setvalue },
405 {0x500d, ptp_shutterspeed_getdesc, ptp_shutterspeed_getvalue, ptp_shutterspeed_setvalue },
406 {0x5011, ptp_datetime_getdesc, ptp_datetime_getvalue, ptp_datetime_setvalue },
407 };
408
409 struct ptp_dirent {
410 uint32_t id;
411 char *name;
412 char *fsname;
413 struct stat stbuf;
414 struct ptp_dirent *parent;
415 struct ptp_dirent *next;
416 };
417
418 static struct ptp_dirent *first_dirent = NULL;
419 static uint32_t ptp_objectid = 0;
420
421 static void *read_file(struct ptp_dirent *cur) {
422 FILE *file = fopen(cur->fsname, "rb");
423 if (!file) {
424 gp_log (GP_LOG_ERROR,__FUNCTION__, "could not open %s", cur->fsname);
425 return NULL;
426 }
427 void *data = malloc(cur->stbuf.st_size);
428 if (!data) {
429 gp_log (GP_LOG_ERROR,__FUNCTION__, "could not allocate data for %s", cur->fsname);
430 return NULL;
431 }
432 if (!fread(data, cur->stbuf.st_size, 1, file)) {
433 gp_log (GP_LOG_ERROR,__FUNCTION__, "could not read data of %s", cur->fsname);
434 free(data);
435 data = NULL;
436 }
437 fclose(file);
438 return data;
439 }
440
441 static void
442 read_directories(const char *path, struct ptp_dirent *parent) {
443 struct ptp_dirent *cur;
444 gp_system_dir dir;
445 gp_system_dirent de;
446
447 dir = gp_system_opendir(path);
448 if (!dir) return;
449 while ((de=gp_system_readdir(dir))) {
450 if (!strcmp(gp_system_filename(de),".")) continue;
451 if (!strcmp(gp_system_filename(de),"..")) continue;
452
453 cur = malloc(sizeof(struct ptp_dirent));
454 if (!cur) break;
455 cur->name = strdup(gp_system_filename(de));
456 cur->fsname = aprintf("%s/%s", path, gp_system_filename(de));
457 cur->id = ptp_objectid++;
458 cur->next = first_dirent;
459 cur->parent = parent;
460 first_dirent = cur;
461 if (-1 == stat(cur->fsname, &cur->stbuf))
462 continue;
463 if (S_ISDIR(cur->stbuf.st_mode))
464 read_directories(cur->fsname, cur); /* recurse! */
465 }
466 gp_system_closedir(dir);
467 }
468
469 static void
470 free_dirent(struct ptp_dirent *ent) {
471 free (ent->name);
472 free (ent->fsname);
473 free (ent);
474 }
475
476 static void
477 read_tree(const char *path) {
478 struct ptp_dirent *root = NULL, *dir, *dcim = NULL;
479
480 if (first_dirent)
481 return;
482
483 first_dirent = malloc(sizeof(struct ptp_dirent));
484 first_dirent->name = strdup("");
485 first_dirent->fsname = strdup(path);
486 first_dirent->id = ptp_objectid++;
487 first_dirent->next = NULL;
488 stat(first_dirent->fsname, &first_dirent->stbuf); /* assuming it works */
489 root = first_dirent;
490 read_directories(path,first_dirent);
491
492 /* See if we have a DCIM directory, if not, create one. */
493 dir = first_dirent;
494 while (dir) {
495 if (!strcmp(dir->name,"DCIM") && dir->parent && !dir->parent->id)
496 dcim = dir;
497 dir = dir->next;
498 }
499 if (!dcim) {
500 dcim = malloc(sizeof(struct ptp_dirent));
501 dcim->name = strdup("DCIM");
502 dcim->fsname = strdup(path);
503 dcim->id = ptp_objectid++;
504 dcim->next = first_dirent;
505 dcim->parent = root;
506 stat(dcim->fsname, &dcim->stbuf); /* assuming it works */
507 first_dirent = dcim;
508 }
509 }
510
511 static int
512 ptp_nikon_setcontrolmode_write(vcamera *cam, ptpcontainer *ptp) {
513 CHECK_PARAM_COUNT(1);
514
515 if ((ptp->params[0] != 0) && (ptp->params[0] != 1)) {
516 gp_log (GP_LOG_ERROR,__FUNCTION__,"controlmode must not be 0 or 1, is %d", ptp->params[0]);
517 ptp_response (cam, PTP_RC_InvalidParameter, 0);
518 return 1;
519 }
520 ptp_response (cam,PTP_RC_OK,0);
521 return 1;
522 }
523
524 static int
525 ptp_opensession_write(vcamera *cam, ptpcontainer *ptp) {
526 CHECK_PARAM_COUNT(1);
527
528 if (ptp->params[0] == 0) {
529 gp_log (GP_LOG_ERROR,__FUNCTION__,"session must not be 0, is %d", ptp->params[0]);
530 ptp_response (cam, PTP_RC_InvalidParameter, 0);
531 return 1;
532 }
533 if (cam->session) {
534 gp_log (GP_LOG_ERROR,__FUNCTION__,"session is already open");
535 ptp_response (cam, PTP_RC_SessionAlreadyOpened, 0);
536 return 1;
537 }
538 cam->session = ptp->params[0];
539 ptp_response (cam,PTP_RC_OK,0);
540 return 1;
541 }
542
543 static int
544 ptp_closesession_write(vcamera *cam, ptpcontainer *ptp) {
545 CHECK_PARAM_COUNT(0);
546 CHECK_SEQUENCE_NUMBER();
547
548 if (!cam->session) {
549 gp_log (GP_LOG_ERROR,__FUNCTION__,"session is not open");
550 ptp_response(cam, PTP_RC_SessionAlreadyOpened, 0);
551 return 1;
552 }
553 cam->session = 0;
554 ptp_response(cam,PTP_RC_OK,0);
555 return 1;
556 }
557
558 static int
559 ptp_deviceinfo_write(vcamera *cam, ptpcontainer *ptp) {
560 unsigned char *data;
561 int x = 0, i, cnt, vendor;
562 uint16_t *opcodes, *devprops;
563 uint16_t imageformats[1];
564 uint16_t events[5];
565
566 CHECK_PARAM_COUNT(0);
567
568 /* Session does not need to be open for GetDeviceInfo */
569
570 /* Getdeviceinfo is special. it can be called with transid 0 outside of the session. */
571 if ((ptp->seqnr != 0) && (ptp->seqnr != cam->seqnr)) {
572 /* not clear if normal cameras react like this */ \
573 gp_log (GP_LOG_ERROR, __FUNCTION__, "seqnr %d was sent, expected was %d", ptp->seqnr, cam->seqnr);\
574 ptp_response(cam,PTP_RC_GeneralError,0); \
575 return 1;
576 }
577 data = malloc(2000);
578
579 x += put_16bit_le(data+x,100); /* StandardVersion */
580 switch (cam->type) {
581 case NIKON_D750:
582 x += put_32bit_le(data+x,0xa); /* VendorExtensionID */
583 x += put_16bit_le(data+x,100); /* VendorExtensionVersion */
584 break;
585 default:
586 x += put_32bit_le(data+x,0); /* VendorExtensionID */
587 x += put_16bit_le(data+x,0); /* VendorExtensionVersion */
588 break;
589 }
590 x += put_string(data+x,"G-V: 1.0;"); /* VendorExtensionDesc */
591 x += put_16bit_le(data+x,0); /* FunctionalMode */
592
593 cnt = 0;
594 for (i = 0;i<sizeof(ptp_functions)/sizeof(ptp_functions[0]);i++) {
595 if (ptp_functions[i].type == GENERIC_PTP) {
596 cnt += ptp_functions[i].nroffunctions;
597 continue;
598 }
599 if (ptp_functions[i].type == cam->type) {
600 vendor = i;
601 cnt += ptp_functions[i].nroffunctions;
602 }
603 }
604 opcodes = malloc(cnt*sizeof(uint16_t));
605
606 for (i=0;i<ptp_functions[0].nroffunctions;i++)
607 opcodes[i] = ptp_functions[0].functions[i].code;
608
609 if (cam->type != GENERIC_PTP) {
610 for (i=0;i<ptp_functions[vendor].nroffunctions;i++)
611 opcodes[i+ptp_functions[0].nroffunctions] = ptp_functions[vendor].functions[i].code;
612 }
613
614 x += put_16bit_le_array(data+x,opcodes,cnt); /* Operations */
615 free (opcodes);
616
617 events[0] = 0x4002;
618 events[1] = 0x4003;
619 events[2] = 0x4006;
620 events[3] = 0x400a;
621 events[4] = 0x400d;
622 x += put_16bit_le_array(data+x,events,sizeof(events)/sizeof(events[0])); /* Events */
623
624 devprops = malloc(sizeof(ptp_properties)/sizeof(ptp_properties[0])*sizeof(uint16_t));
625 for (i=0;i<sizeof(ptp_properties)/sizeof(ptp_properties[0]);i++)
626 devprops[i] = ptp_properties[i].code;
627 x += put_16bit_le_array(data+x,devprops,sizeof(ptp_properties)/sizeof(ptp_properties[0]));/* DeviceProps */
628 free (devprops);
629
630 imageformats[0] = 0x3801;
631 x += put_16bit_le_array(data+x,imageformats,1); /* CaptureFormats */
632
633 imageformats[0] = 0x3801;
634 x += put_16bit_le_array(data+x,imageformats,1); /* ImageFormats */
635
636 x += put_string(data+x,"GP"); /* Manufacturer */
637 x += put_string(data+x,"VC"); /* Model */
638 x += put_string(data+x,"2.5.11");/* DeviceVersion */
639 x += put_string(data+x,"0.1"); /* DeviceVersion */
640 x += put_string(data+x,"1"); /* SerialNumber */
641
642 ptp_senddata(cam,0x1001,data,x);
643 free (data);
644 ptp_response(cam,PTP_RC_OK,0);
645 return 1;
646 }
647
648 static int
649 ptp_getnumobjects_write(vcamera *cam, ptpcontainer *ptp) {
650 int cnt;
651 struct ptp_dirent *cur;
652 uint32_t mode = 0;
653
654 CHECK_SEQUENCE_NUMBER();
655 CHECK_SESSION();
656
657 if (ptp->nparams < 1) {
658 gp_log (GP_LOG_ERROR,__FUNCTION__, "parameter count %d", ptp->nparams);
659 ptp_response (cam, PTP_RC_InvalidParameter, 0);
660 return 1;
661 }
662 if ((ptp->params[0] != 0xffffffff) && (ptp->params[0] != 0x00010001)) {
663 gp_log (GP_LOG_ERROR,__FUNCTION__, "storage id 0x%08x unknown", ptp->params[0]);
664 ptp_response (cam, PTP_RC_InvalidStorageId, 0);
665 return 1;
666 }
667 if (ptp->nparams >= 2) {
668 if (ptp->params[1] != 0) {
669 gp_log (GP_LOG_ERROR,__FUNCTION__, "currently can not handle OFC selection (0x%04x)", ptp->params[1]);
670 ptp_response (cam, PTP_RC_SpecificationByFormatUnsupported, 0);
671 return 1;
672 }
673 }
674 if (ptp->nparams >= 3) {
675 mode = ptp->params[2];
676 if ((mode != 0) && (mode != 0xffffffff)) {
677 cur = first_dirent;
678 while (cur) {
679 if (cur->id == mode) break;
680 cur = cur->next;
681 }
682 if (!cur) {
683 gp_log (GP_LOG_ERROR,__FUNCTION__, "requested subtree of (0x%08x), but no such handle", mode);
684 ptp_response (cam, PTP_RC_InvalidObjectHandle, 0);
685 return 1;
686 }
687 if (!S_ISDIR(cur->stbuf.st_mode)) {
688 gp_log (GP_LOG_ERROR,__FUNCTION__, "requested subtree of (0x%08x), but this is no asssocation", mode);
689 ptp_response (cam, PTP_RC_InvalidParentObject, 0);
690 return 1;
691 }
692 }
693 }
694
695 cnt = 0; cur = first_dirent;
696 while (cur) {
697 if (cur->id) { /* do not include 0 entry */
698 switch (mode) {
699 case 0: /* all objects recursive on device */
700 cnt++;
701 break;
702 case 0xffffffff: /* only root dir */
703 if (cur->parent->id == 0)
704 cnt++;
705 break;
706 default: /* single level directory below this handle */
707 if (cur->parent->id == mode)
708 cnt++;
709 break;
710 }
711 }
712 cur = cur->next;
713 }
714
715 ptp_response (cam, PTP_RC_OK, 1, cnt);
716 return 1;
717 }
718
719 static int
720 ptp_getobjecthandles_write(vcamera *cam, ptpcontainer *ptp) {
721 unsigned char *data;
722 int x = 0, cnt;
723 struct ptp_dirent *cur;
724 uint32_t mode = 0;
725
726 CHECK_SEQUENCE_NUMBER();
727 CHECK_SESSION();
728
729 if (ptp->nparams < 1) {
730 gp_log (GP_LOG_ERROR,__FUNCTION__, "parameter count %d", ptp->nparams);
731 ptp_response (cam, PTP_RC_InvalidParameter, 0);
732 return 1;
733 }
734 if ((ptp->params[0] != 0xffffffff) && (ptp->params[0] != 0x00010001)) {
735 gp_log (GP_LOG_ERROR,__FUNCTION__, "storage id 0x%08x unknown", ptp->params[0]);
736 ptp_response (cam, PTP_RC_InvalidStorageId, 0);
737 return 1;
738 }
739 if (ptp->nparams >= 2) {
740 if (ptp->params[1] != 0) {
741 gp_log (GP_LOG_ERROR,__FUNCTION__, "currently can not handle OFC selection (0x%04x)", ptp->params[1]);
742 ptp_response (cam, PTP_RC_SpecificationByFormatUnsupported, 0);
743 return 1;
744 }
745 }
746 if (ptp->nparams >= 3) {
747 mode = ptp->params[2];
748 if ((mode != 0) && (mode != 0xffffffff)) {
749 cur = first_dirent;
750 while (cur) {
751 if (cur->id == mode) break;
752 cur = cur->next;
753 }
754 if (!cur) {
755 gp_log (GP_LOG_ERROR,__FUNCTION__, "requested subtree of (0x%08x), but no such handle", mode);
756 ptp_response (cam, PTP_RC_InvalidObjectHandle, 0);
757 return 1;
758 }
759 if (!S_ISDIR(cur->stbuf.st_mode)) {
760 gp_log (GP_LOG_ERROR,__FUNCTION__, "requested subtree of (0x%08x), but this is no asssocation", mode);
761 ptp_response (cam, PTP_RC_InvalidParentObject, 0);
762 return 1;
763 }
764 }
765 }
766
767 cnt = 0; cur = first_dirent;
768 while (cur) {
769 if (cur->id) { /* do not include 0 entry */
770 switch (mode) {
771 case 0: /* all objects recursive on device */
772 cnt++;
773 break;
774 case 0xffffffff: /* only root dir */
775 if (cur->parent->id == 0)
776 cnt++;
777 break;
778 default: /* single level directory below this handle */
779 if (cur->parent->id == mode)
780 cnt++;
781 break;
782 }
783 }
784 cur = cur->next;
785 }
786
787 data = malloc(4+4*cnt);
788 x = put_32bit_le(data + x,cnt);
789 cur = first_dirent;
790 while (cur) {
791 if (cur->id) { /* do not include 0 entry */
792 switch (mode) {
793 case 0: /* all objects recursive on device */
794 x += put_32bit_le(data+x, cur->id);
795 break;
796 case 0xffffffff: /* only root dir */
797 if (cur->parent->id == 0)
798 x += put_32bit_le(data+x, cur->id);
799 break;
800 default: /* single level directory below this handle */
801 if (cur->parent->id == mode)
802 x += put_32bit_le(data+x, cur->id);
803 break;
804 }
805 }
806 cur = cur->next;
807 }
808 ptp_senddata(cam,0x1007,data,x);
809 free (data);
810 ptp_response(cam,PTP_RC_OK,0);
811 return 1;
812 }
813
814 static int
815 ptp_getstorageids_write(vcamera *cam, ptpcontainer *ptp) {
816 unsigned char *data;
817 int x = 0;
818 uint32_t sids[1];
819
820 CHECK_SEQUENCE_NUMBER();
821 CHECK_SESSION();
822 CHECK_PARAM_COUNT(0);
823
824 data = malloc(200);
825
826 sids[0] = 0x00010001;
827 x = put_32bit_le_array (data, sids, 1);
828
829 ptp_senddata (cam, 0x1004, data, x);
830 free (data);
831 ptp_response(cam,PTP_RC_OK,0);
832 return 1;
833 }
834
835 static int
836 ptp_getstorageinfo_write(vcamera *cam, ptpcontainer *ptp) {
837 unsigned char *data;
838 int x = 0;
839
840 CHECK_SEQUENCE_NUMBER();
841 CHECK_SESSION();
842 CHECK_PARAM_COUNT(1);
843
844 if (ptp->params[0] != 0x00010001) {
845 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid storage id 0x%08x", ptp->params[0]);
846 ptp_response(cam,PTP_RC_InvalidStorageId,0);
847 return 1;
848 }
849
850 data = malloc(200);
851 x += put_16bit_le (data+x, 3); /* StorageType: Fixed RAM */
852 x += put_16bit_le (data+x, 3); /* FileSystemType: Generic Hierarchical */
853 x += put_16bit_le (data+x, 2); /* AccessCapability: R/O with object deletion */
854 x += put_64bit_le (data+x, 0x42424242); /* MaxCapacity */
855 x += put_64bit_le (data+x, 0x21212121); /* FreeSpaceInBytes */
856 x += put_32bit_le (data+x, 150); /* FreeSpaceInImages ... around 150 */
857 x += put_string (data+x, "GPVC Storage"); /* StorageDescription */
858 x += put_string (data+x, "GPVCS Label"); /* VolumeLabel */
859
860 ptp_senddata (cam, 0x1005, data, x);
861 free (data);
862 ptp_response(cam, PTP_RC_OK, 0);
863 return 1;
864 }
865
866 static int
867 ptp_getobjectinfo_write(vcamera *cam, ptpcontainer *ptp) {
868 struct ptp_dirent *cur;
869 unsigned char *data;
870 int x = 0;
871 uint16_t ofc, thumbofc = 0;
872 int thumbwidth = 0, thumbheight = 0, thumbsize = 0;
873 int imagewidth = 0, imageheight = 0, imagebitdepth = 0;
874 struct tm *tm;
875 time_t xtime;
876 char xdate[40];
877
878 CHECK_SEQUENCE_NUMBER();
879 CHECK_SESSION();
880 CHECK_PARAM_COUNT(1);
881
882 cur = first_dirent;
883 while (cur) {
884 if (cur->id == ptp->params[0])
885 break;
886 cur = cur->next;
887 }
888 if (!cur) {
889 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid object handle 0x%08x", ptp->params[0]);
890 ptp_response(cam,PTP_RC_InvalidObjectHandle,0);
891 return 1;
892 }
893 data = malloc(2000);
894 x += put_32bit_le (data+x, 0x00010001); /* StorageID */
895 /* ObjectFormatCode */
896 ofc = 0x3000;
897 if (S_ISDIR(cur->stbuf.st_mode)) {
898 ofc = 0x3001;
899 } else {
900 if (strstr(cur->name,".JPG") || strstr(cur->name,".jpg"))
901 ofc = 0x3801;
902 if (strstr(cur->name,".GIF") || strstr(cur->name,".gif"))
903 ofc = 0x3807;
904 if (strstr(cur->name,".PNG") || strstr(cur->name,".png"))
905 ofc = 0x380B;
906 if (strstr(cur->name,".DNG") || strstr(cur->name,".dng"))
907 ofc = 0x3811;
908 if (strstr(cur->name,".TXT") || strstr(cur->name,".txt"))
909 ofc = 0x3004;
910 if (strstr(cur->name,".HTML") || strstr(cur->name,".html"))
911 ofc = 0x3005;
912 if (strstr(cur->name,".MP3") || strstr(cur->name,".mp3"))
913 ofc = 0x3009;
914 if (strstr(cur->name,".AVI") || strstr(cur->name,".avi"))
915 ofc = 0x300A;
916 if ( strstr(cur->name,".MPG") || strstr(cur->name,".mpg") ||
917 strstr(cur->name,".MPEG") || strstr(cur->name,".mpeg")
918 )
919 ofc = 0x300B;
920 }
921
922 #ifdef HAVE_LIBEXIF
923 if (ofc == 0x3801) { /* We are jpeg ... look into the exif data */
924 ExifData *ed;
925 ExifEntry *e;
926 unsigned char *filedata;
927
928 filedata = read_file(cur);
929 if (!filedata) {
930 free (data);
931 ptp_response(cam,PTP_RC_GeneralError,0);
932 return 1;
933 }
934
935 ed = exif_data_new_from_data ((unsigned char*)filedata, cur->stbuf.st_size);
936 if (ed) {
937 if (ed->data) {
938 thumbofc = 0x3808;
939 thumbsize = ed->size;
940 }
941 e = exif_data_get_entry (ed, EXIF_TAG_PIXEL_X_DIMENSION);
942 if (e) {
943 gp_log (GP_LOG_DEBUG, __FUNCTION__ , "pixel x dim format is %d", e->format);
944 if (e->format == EXIF_FORMAT_SHORT) {
945 imagewidth = exif_get_short (e->data, exif_data_get_byte_order (ed));
946 }
947 }
948 e = exif_data_get_entry (ed, EXIF_TAG_PIXEL_Y_DIMENSION);
949 if (e) {
950 gp_log (GP_LOG_DEBUG, __FUNCTION__ , "pixel y dim format is %d", e->format);
951 if (e->format == EXIF_FORMAT_SHORT) {
952 imageheight = exif_get_short (e->data, exif_data_get_byte_order (ed));
953 }
954 }
955 /* FIXME: potentially could find out more about thumbnail too */
956 }
957 exif_data_unref (ed);
958 free (filedata);
959 }
960 #endif
961 x += put_16bit_le (data+x, ofc);
962 x += put_16bit_le (data+x, 0); /* ProtectionStatus, no protection */
963 x += put_32bit_le (data+x, cur->stbuf.st_size); /* ObjectSize */
964 x += put_16bit_le (data+x, thumbofc); /* ThumbFormat */
965 x += put_32bit_le (data+x, thumbsize); /* ThumbSize */
966 x += put_32bit_le (data+x, thumbwidth); /* ThumbPixWidth */
967 x += put_32bit_le (data+x, thumbheight); /* ThumbPixHeight */
968 x += put_32bit_le (data+x, imagewidth); /* ImagePixWidth */
969 x += put_32bit_le (data+x, imageheight); /* ImagePixHeight */
970 x += put_32bit_le (data+x, imagebitdepth); /* ImageBitDepth */
971 x += put_32bit_le (data+x, cur->parent->id); /* ParentObject */
972 /* AssociationType */
973 if (S_ISDIR(cur->stbuf.st_mode)) {
974 x += put_16bit_le (data+x, 1); /* GenericFolder */
975 x += put_32bit_le (data+x, 0); /* unused */
976 } else {
977 x += put_16bit_le (data+x, 0); /* Undefined */
978 x += put_32bit_le (data+x, 0); /* Undefined */
979 }
980 x += put_32bit_le (data+x, 0); /* SequenceNumber */
981 x += put_string (data+x, cur->name); /* Filename */
982
983 xtime = cur->stbuf.st_ctime;
984 tm = gmtime(&xtime);
985 sprintf(xdate,"%04d%02d%02dT%02d%02d%02d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
986 x += put_string (data+x, xdate); /* CreationDate */
987 xtime = cur->stbuf.st_mtime;
988 tm = gmtime(&xtime);
989 sprintf(xdate,"%04d%02d%02dT%02d%02d%02d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
990 x += put_string (data+x, xdate); /* ModificatioDate */
991
992 x += put_string (data+x, "keyword"); /* Keywords */
993
994 ptp_senddata (cam, 0x1008, data, x);
995 free (data);
996 ptp_response(cam, PTP_RC_OK, 0);
997 return 1;
998 }
999
1000 static int
1001 ptp_getobject_write(vcamera *cam, ptpcontainer *ptp) {
1002 unsigned char *data;
1003 struct ptp_dirent *cur;
1004
1005 CHECK_SEQUENCE_NUMBER();
1006 CHECK_SESSION();
1007 CHECK_PARAM_COUNT(1);
1008
1009 cur = first_dirent;
1010 while (cur) {
1011 if (cur->id == ptp->params[0]) break;
1012 cur = cur->next;
1013 }
1014 if (!cur) {
1015 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid object handle 0x%08x", ptp->params[0]);
1016 ptp_response(cam,PTP_RC_InvalidObjectHandle,0);
1017 return 1;
1018 }
1019 data = read_file(cur);
1020 if (!data) {
1021 ptp_response(cam,PTP_RC_GeneralError,0);
1022 return 1;
1023 }
1024
1025 ptp_senddata (cam, 0x1009, data, cur->stbuf.st_size);
1026 free (data);
1027 ptp_response (cam, PTP_RC_OK, 0);
1028 return 1;
1029 }
1030
1031 static int
1032 ptp_getthumb_write(vcamera *cam, ptpcontainer *ptp) {
1033 unsigned char *data;
1034 struct ptp_dirent *cur;
1035 #ifdef HAVE_LIBEXIF
1036 ExifData *ed;
1037 #endif
1038
1039 CHECK_SEQUENCE_NUMBER();
1040 CHECK_SESSION();
1041 CHECK_PARAM_COUNT(1);
1042
1043 cur = first_dirent;
1044 while (cur) {
1045 if (cur->id == ptp->params[0]) break;
1046 cur = cur->next;
1047 }
1048 if (!cur) {
1049 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid object handle 0x%08x", ptp->params[0]);
1050 ptp_response(cam,PTP_RC_InvalidObjectHandle,0);
1051 return 1;
1052 }
1053 data = read_file(cur);
1054 if (!data) {
1055 ptp_response(cam,PTP_RC_GeneralError,0);
1056 return 1;
1057 }
1058
1059 #ifdef HAVE_LIBEXIF
1060 ed = exif_data_new_from_data ((unsigned char*)data, cur->stbuf.st_size);
1061 if (!ed) {
1062 gp_log (GP_LOG_ERROR, __FUNCTION__, "Could not parse EXIF data");
1063 free (data);
1064 ptp_response(cam,PTP_RC_NoThumbnailPresent,0);
1065 return 1;
1066 }
1067 if (!ed->data) {
1068 gp_log (GP_LOG_ERROR, __FUNCTION__, "EXIF data does not contain a thumbnail");
1069 free (data);
1070 ptp_response(cam,PTP_RC_NoThumbnailPresent,0);
1071 exif_data_unref (ed);
1072 return 1;
1073 }
1074 /*
1075 * We found a thumbnail in EXIF data! Those
1076 * thumbnails are always JPEG. Set up the file.
1077 */
1078 ptp_senddata (cam, 0x100A, ed->data, ed->size);
1079 exif_data_unref (ed);
1080
1081 ptp_response (cam, PTP_RC_OK, 0);
1082 #else
1083 gp_log (GP_LOG_ERROR, __FUNCTION__, "Cannot get thumbnail without libexif, lying about missing thumbnail");
1084 ptp_response(cam,PTP_RC_NoThumbnailPresent,0);
1085 #endif
1086 free (data);
1087 return 1;
1088 }
1089
1090 static int
1091 ptp_initiatecapture_write(vcamera *cam, ptpcontainer *ptp) {
1092 struct ptp_dirent *cur, *newcur, *dir, *dcim = NULL;
1093 static int capcnt = 98;
1094 char buf[10];
1095
1096 CHECK_SEQUENCE_NUMBER();
1097 CHECK_SESSION();
1098 CHECK_PARAM_COUNT(2);
1099
1100 if ((ptp->params[0] != 0) && (ptp->params[0] != 0x00010001)) {
1101 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid storage id 0x%08x", ptp->params[0]);
1102 ptp_response (cam, PTP_RC_StoreNotAvailable, 0);
1103 return 1;
1104 }
1105 if ((ptp->params[1] != 0) && (ptp->params[1] != 0x3801)) {
1106 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid objectformat code id 0x%04x", ptp->params[1]);
1107 ptp_response (cam, PTP_RC_InvalidObjectFormatCode, 0);
1108 return 1;
1109 }
1110 if (capcnt > 150) {
1111 gp_log (GP_LOG_ERROR,__FUNCTION__, "Declaring store full at picture 151");
1112 ptp_response (cam, PTP_RC_StoreFull, 0);
1113 return 1;
1114 }
1115
1116 /* just search for first jpeg we find in the tree, we will use this to send back as the captured image */
1117 cur = first_dirent;
1118 while (cur) {
1119 if (strstr (cur->name, ".jpg") || strstr (cur->name, ".JPG"))
1120 break;
1121 cur = cur->next;
1122 }
1123 if (!cur) {
1124 gp_log (GP_LOG_ERROR,__FUNCTION__, "I do not have a JPG file in the store, can not proceed");
1125 ptp_response (cam, PTP_RC_StoreNotAvailable, 0);
1126 return 1;
1127 }
1128 /* identify the DCIM dir, so we can attach a virtual xxxGPHOT directory to virtually store the new picture in */
1129 dir = first_dirent;
1130 while (dir) {
1131 if (!strcmp(dir->name,"DCIM") && dir->parent && !dir->parent->id)
1132 dcim = dir;
1133 dir = dir->next;
1134 }
1135 /* find the nnnGPHOT directories, where nnn is 100-999. (See DCIM standard.) */
1136 sprintf(buf, "%03dGPHOT", 100 + ((capcnt / 100) % 900));
1137 dir = first_dirent;
1138 while (dir) {
1139 if (!strcmp (dir->name, buf) && (dir->parent == dcim))
1140 break;
1141 dir = dir->next;
1142 }
1143 /* if not yet found, create the virtual /DCIM/xxxGPHOT/ directory. */
1144 if (!dir) {
1145 dir = malloc (sizeof(struct ptp_dirent));
1146 dir->id = ++ptp_objectid;
1147 dir->fsname = "virtual";
1148 dir->stbuf = dcim->stbuf; /* only the S_ISDIR flag is used */
1149 dir->parent = dcim;
1150 dir->next = first_dirent;
1151 dir->name = strdup (buf);
1152 first_dirent = dir;
1153 /* Emit ObjectAdded event for the created folder */
1154 ptp_inject_interrupt (cam, 80, 0x4002, 1, ptp_objectid, cam->seqnr); /* objectadded */
1155 }
1156 if (capcnt++ == 150) {
1157 /* The start of the operation succeeds, but the memory runs full during it. */
1158 ptp_inject_interrupt (cam, 100, 0x400A, 1, ptp_objectid, cam->seqnr); /* storefull */
1159 ptp_response (cam, PTP_RC_OK, 0);
1160 return 1;
1161 }
1162
1163 newcur = malloc (sizeof(struct ptp_dirent));
1164 newcur->id = ++ptp_objectid;
1165 newcur->fsname = strdup(cur->fsname);
1166 newcur->stbuf = cur->stbuf;
1167 newcur->parent = dir;
1168 newcur->next = first_dirent;
1169 newcur->name = malloc(8+3+1+1);
1170 sprintf(newcur->name,"GPH_%04d.JPG", capcnt++);
1171 first_dirent = newcur;
1172
1173 ptp_inject_interrupt (cam, 100, 0x4002, 1, ptp_objectid, cam->seqnr); /* objectadded */
1174 ptp_inject_interrupt (cam, 120, 0x400d, 0, 0, cam->seqnr); /* capturecomplete */
1175 ptp_response (cam, PTP_RC_OK, 0);
1176 return 1;
1177 }
1178
1179 static int
1180 ptp_deleteobject_write(vcamera *cam, ptpcontainer *ptp) {
1181 struct ptp_dirent *cur, *xcur;
1182
1183 CHECK_SEQUENCE_NUMBER();
1184 CHECK_SESSION();
1185
1186 if (ptp->nparams < 1) {
1187 gp_log (GP_LOG_ERROR, __FUNCTION__, "parameter count %d", ptp->nparams);
1188 ptp_response (cam, PTP_RC_InvalidParameter, 0);
1189 return 1;
1190 }
1191 if (ptp->params[0] == 0xffffffff) { /* delete all mode */
1192 gp_log (GP_LOG_DEBUG, __FUNCTION__, "delete all");
1193 cur = first_dirent;
1194
1195 while (cur) {
1196 xcur = cur->next;
1197 free_dirent(cur);
1198 cur = xcur;
1199 }
1200 first_dirent = NULL;
1201 ptp_response (cam, PTP_RC_OK, 0);
1202 return 1;
1203 }
1204
1205 if ((ptp->nparams == 2) && (ptp->params[1] != 0)) {
1206 gp_log (GP_LOG_ERROR, __FUNCTION__, "single object delete, but ofc is 0x%08x", ptp->params[1]);
1207 ptp_response (cam, PTP_RC_InvalidParameter, 0);
1208 return 1;
1209 }
1210 /* for associations this even means recursive deletion */
1211
1212 cur = first_dirent;
1213 while (cur) {
1214 if (cur->id == ptp->params[0]) break;
1215 cur = cur->next;
1216 }
1217 if (!cur) {
1218 gp_log (GP_LOG_ERROR,__FUNCTION__, "invalid object handle 0x%08x", ptp->params[0]);
1219 ptp_response(cam,PTP_RC_InvalidObjectHandle,0);
1220 return 1;
1221 }
1222 if (S_ISDIR(cur->stbuf.st_mode)) {
1223 gp_log (GP_LOG_ERROR,__FUNCTION__, "FIXME: not yet deleting directories");
1224 ptp_response(cam,PTP_RC_ObjectWriteProtected,0);
1225 return 1;
1226 }
1227 if (cur == first_dirent) {
1228 first_dirent = cur->next;
1229 free_dirent (cur);
1230 } else {
1231 xcur = first_dirent;
1232 while (xcur) {
1233 if (xcur->next == cur) {
1234 xcur->next = xcur->next->next;
1235 free_dirent (cur);
1236 break;
1237 }
1238 xcur = xcur->next;
1239 }
1240 }
1241 ptp_response (cam, PTP_RC_OK, 0);
1242 return 1;
1243 }
1244
1245
1246 static int
1247 put_propval (unsigned char *data, uint16_t type, PTPPropValue *val) {
1248 switch (type) {
1249 case 0x1: return put_8bit_le (data, val->i8);
1250 case 0x2: return put_8bit_le (data, val->u8);
1251 case 0x3: return put_16bit_le (data, val->i16);
1252 case 0x4: return put_16bit_le (data, val->u16);
1253 case 0x6: return put_32bit_le (data, val->u32);
1254 case 0xffff: return put_string (data, val->str);
1255 default: gp_log (GP_LOG_ERROR, __FUNCTION__, "unhandled datatype %d", type);
1256 return 0;
1257 }
1258 return 0;
1259 }
1260
1261 static int
1262 get_propval (unsigned char *data, unsigned int len, uint16_t type, PTPPropValue *val) {
1263 #define CHECK_SIZE(x) if (len < x) return 0;
1264 switch (type) {
1265 case 0x1: CHECK_SIZE(1);
1266 val->i8 = get_i8bit_le (data);
1267 return 1;
1268 case 0x2: CHECK_SIZE(1);
1269 val->u8 = get_8bit_le (data);
1270 return 1;
1271 case 0x3: CHECK_SIZE(2);
1272 val->i16 = get_16bit_le (data);
1273 return 1;
1274 case 0x4: CHECK_SIZE(2);
1275 val->u16 = get_16bit_le (data);
1276 return 1;
1277 case 0x6: CHECK_SIZE(4);
1278 val->u32 = get_32bit_le (data);
1279 return 1;
1280 case 0xffff: {
1281 int slen;
1282 CHECK_SIZE(1);
1283 slen = get_8bit_le (data);
1284 CHECK_SIZE(1+slen*2);
1285 val->str = get_string (data);
1286 return 1+slen*2;
1287 }
1288 default: gp_log (GP_LOG_ERROR, __FUNCTION__, "unhandled datatype %d", type);
1289 return 0;
1290 }
1291 return 0;
1292 #undef CHECK_SIZE
1293 }
1294
1295 static int
1296 ptp_getdevicepropdesc_write(vcamera *cam, ptpcontainer *ptp) {
1297 int i, x = 0;
1298 unsigned char *data;
1299 PTPDevicePropDesc desc;
1300
1301 CHECK_SEQUENCE_NUMBER();
1302 CHECK_SESSION();
1303 CHECK_PARAM_COUNT(1);
1304
1305 for (i=0;i<sizeof (ptp_properties)/sizeof (ptp_properties[0]);i++) {
1306 if (ptp_properties[i].code == ptp->params[0])
1307 break;
1308 }
1309 if (i == sizeof (ptp_properties)/sizeof (ptp_properties[0])) {
1310 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x not found", ptp->params[0]);
1311 ptp_response (cam, PTP_RC_DevicePropNotSupported, 0);
1312 return 1;
1313 }
1314 data = malloc(2000);
1315 ptp_properties[i].getdesc (cam, &desc);
1316
1317 x += put_16bit_le (data+x, desc.DevicePropCode);
1318 x += put_16bit_le (data+x, desc.DataType);
1319 x += put_8bit_le (data+x, desc.GetSet);
1320 x += put_propval (data+x, desc.DataType, &desc.DefaultValue);
1321 x += put_propval (data+x, desc.DataType, &desc.CurrentValue);
1322 x += put_8bit_le (data+x, desc.FormFlag);
1323 switch (desc.FormFlag) {
1324 case 0: break;
1325 case 1: /* range */
1326 x += put_propval (data+x, desc.DataType, &desc.FORM.Range.MinValue);
1327 x += put_propval (data+x, desc.DataType, &desc.FORM.Range.MaxValue);
1328 x += put_propval (data+x, desc.DataType, &desc.FORM.Range.StepSize);
1329 break;
1330 case 2: /* ENUM */
1331 x += put_16bit_le (data+x, desc.FORM.Enum.NumberOfValues);
1332 for (i=0;i<desc.FORM.Enum.NumberOfValues;i++)
1333 x += put_propval (data+x, desc.DataType, &desc.FORM.Enum.SupportedValue[i]);
1334 break;
1335 }
1336
1337 ptp_free_devicepropdesc(&desc);
1338
1339 ptp_senddata (cam, 0x1014, data, x);
1340 free (data);
1341 ptp_response (cam, PTP_RC_OK, 0);
1342 return 1;
1343 }
1344
1345 static int
1346 ptp_getdevicepropvalue_write(vcamera *cam, ptpcontainer *ptp) {
1347 unsigned char* data;
1348 int i, x = 0;
1349 PTPPropValue val;
1350 PTPDevicePropDesc desc;
1351
1352 CHECK_SEQUENCE_NUMBER();
1353 CHECK_SESSION();
1354 CHECK_PARAM_COUNT(1);
1355
1356 for (i=0;i<sizeof(ptp_properties)/sizeof(ptp_properties[0]);i++) {
1357 if (ptp_properties[i].code == ptp->params[0])
1358 break;
1359 }
1360 if (i == sizeof (ptp_properties)/sizeof (ptp_properties[0])) {
1361 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x not found", ptp->params[0]);
1362 ptp_response (cam, PTP_RC_DevicePropNotSupported, 0);
1363 return 1;
1364 }
1365 data = malloc(2000);
1366 ptp_properties[i].getdesc (cam, &desc);
1367 ptp_properties[i].getvalue (cam, &val);
1368 x = put_propval (data+x, desc.DataType, &val);
1369
1370 ptp_senddata (cam, 0x1015, data, x);
1371 free (data);
1372
1373 ptp_response (cam, PTP_RC_OK, 0);
1374 return 1;
1375 }
1376
1377 static int
1378 ptp_setdevicepropvalue_write(vcamera *cam, ptpcontainer *ptp) {
1379 int i;
1380
1381 CHECK_SEQUENCE_NUMBER();
1382 CHECK_SESSION();
1383 CHECK_PARAM_COUNT(1);
1384
1385 for (i=0;i<sizeof(ptp_properties)/sizeof(ptp_properties[0]);i++) {
1386 if (ptp_properties[i].code == ptp->params[0])
1387 break;
1388 }
1389 if (i == sizeof (ptp_properties)/sizeof (ptp_properties[0])) {
1390 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x not found", ptp->params[0]);
1391 ptp_response (cam, PTP_RC_DevicePropNotSupported, 0);
1392 return 1;
1393 }
1394 /* so ... we wait for the data phase */
1395 return 1;
1396 }
1397
1398 /* magic opcode for our driver, to inject commands */
1399 static int
1400 ptp_vusb_write(vcamera *cam, ptpcontainer *ptp) {
1401 static int capcnt = 98;
1402 static int timeout = 1;
1403
1404 CHECK_SEQUENCE_NUMBER();
1405 CHECK_SESSION();
1406 if (ptp->nparams < 1) {
1407 gp_log (GP_LOG_ERROR, __FUNCTION__, "parameter count %d", ptp->nparams);
1408 ptp_response (cam, PTP_RC_InvalidParameter, 0);
1409 return 1;
1410 }
1411 if (ptp->nparams >= 2) {
1412 timeout = ptp->params[1];
1413 gp_log (GP_LOG_DEBUG, __FUNCTION__, "new timeout %d", timeout);
1414 } else
1415 timeout++;
1416
1417 switch (ptp->params[0]) {
1418 case 0: {/* add a new image after 1 second */
1419 struct ptp_dirent *cur, *newcur, *dir, *dcim = NULL;
1420 char buf[10];
1421
1422 cur = first_dirent;
1423 while (cur) {
1424 if (strstr (cur->name, ".jpg") || strstr (cur->name, ".JPG"))
1425 break;
1426 cur = cur->next;
1427 }
1428 if (!cur) {
1429 gp_log (GP_LOG_ERROR,__FUNCTION__, "I do not have a JPG file in the store, can not proceed");
1430 ptp_response (cam, PTP_RC_GeneralError, 0);
1431 return 1;
1432 }
1433 dir = first_dirent;
1434 while (dir) {
1435 if (!strcmp(dir->name,"DCIM") && dir->parent && !dir->parent->id)
1436 dcim = dir;
1437 dir = dir->next;
1438 }
1439
1440 cur = first_dirent;
1441 while (cur) {
1442 if (strstr (cur->name, ".jpg") || strstr (cur->name, ".JPG"))
1443 break;
1444 cur = cur->next;
1445 }
1446 if (!cur) {
1447 gp_log (GP_LOG_ERROR,__FUNCTION__, "I do not have a JPG file in the store, can not proceed");
1448 ptp_response (cam, PTP_RC_GeneralError, 0);
1449 return 1;
1450 }
1451 dir = first_dirent;
1452 while (dir) {
1453 if (!strcmp(dir->name,"DCIM") && dir->parent && !dir->parent->id)
1454 dcim = dir;
1455 dir = dir->next;
1456 }
1457 /* nnnGPHOT directories, where nnn is 100-999. (See DCIM standard.) */
1458 sprintf(buf, "%03dGPHOT", 100 + ((capcnt / 100) % 900));
1459 dir = first_dirent;
1460 while (dir) {
1461 if (!strcmp (dir->name, buf) && (dir->parent == dcim))
1462 break;
1463 dir = dir->next;
1464 }
1465 if (!dir) {
1466 dir = malloc (sizeof(struct ptp_dirent));
1467 dir->id = ++ptp_objectid;
1468 dir->fsname = "virtual";
1469 dir->stbuf = dcim->stbuf; /* only the S_ISDIR flag is used */
1470 dir->parent = dcim;
1471 dir->next = first_dirent;
1472 dir->name = strdup (buf);
1473 first_dirent = dir;
1474 /* Emit ObjectAdded event for the created folder */
1475 ptp_inject_interrupt (cam, 80, 0x4002, 1, ptp_objectid, cam->seqnr); /* objectadded */
1476 }
1477
1478 newcur = malloc (sizeof(struct ptp_dirent));
1479 newcur->id = ++ptp_objectid;
1480 newcur->fsname = strdup(cur->fsname);
1481 newcur->stbuf = cur->stbuf;
1482 newcur->parent = dir;
1483 newcur->next = first_dirent;
1484 newcur->name = malloc(8+3+1+1);
1485 sprintf(newcur->name,"GPH_%04d.JPG", capcnt++);
1486 first_dirent = newcur;
1487
1488 ptp_inject_interrupt (cam, timeout, 0x4002, 1, ptp_objectid, cam->seqnr); /* objectadded */
1489 ptp_response (cam, PTP_RC_OK, 0);
1490 break;
1491 }
1492 case 1: {/* remove 1 image from directory */
1493 struct ptp_dirent **pcur, *cur;
1494
1495 pcur = &first_dirent;
1496 while (*pcur) {
1497 if (strstr ((*pcur)->name, ".jpg") || strstr ((*pcur)->name, ".JPG"))
1498 break;
1499 pcur = &((*pcur)->next);
1500 }
1501 if (!*pcur) {
1502 gp_log (GP_LOG_ERROR,__FUNCTION__, "I do not have a JPG file in the store, can not proceed");
1503 ptp_response (cam, PTP_RC_GeneralError, 0);
1504 return 1;
1505 }
1506 ptp_inject_interrupt (cam, timeout, 0x4003, 1, (*pcur)->id, cam->seqnr); /* objectremoved */
1507 cur = *pcur;
1508 *pcur = (*pcur)->next;
1509 free (cur->name);
1510 free (cur->fsname);
1511 free (cur);
1512 ptp_response (cam, PTP_RC_OK, 0);
1513 break;
1514 }
1515 case 2: /* capture complete */
1516 ptp_inject_interrupt (cam, timeout, 0x400d, 0, 0, cam->seqnr); /* capturecomplete */
1517 ptp_response (cam, PTP_RC_OK, 0);
1518 break;
1519 default:
1520 gp_log (GP_LOG_ERROR, __FUNCTION__, "unknown action %d", ptp->params[0]);
1521 ptp_response (cam, PTP_RC_OK, 0);
1522 break;
1523 }
1524 return 1;
1525 }
1526
1527 static int
1528 ptp_setdevicepropvalue_write_data(vcamera *cam, ptpcontainer *ptp, unsigned char *data, unsigned int len) {
1529 int i;
1530 PTPPropValue val;
1531 PTPDevicePropDesc desc;
1532
1533 CHECK_SEQUENCE_NUMBER();
1534 CHECK_SESSION();
1535 CHECK_PARAM_COUNT(1);
1536
1537 for (i=0;i<sizeof(ptp_properties)/sizeof(ptp_properties[0]);i++) {
1538 if (ptp_properties[i].code == ptp->params[0])
1539 break;
1540 }
1541 if (i == sizeof (ptp_properties)/sizeof (ptp_properties[0])) {
1542 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x not found", ptp->params[0]);
1543 /* we emitted the response already in _write */
1544 return 1;
1545 }
1546 if (!ptp_properties[i].setvalue) {
1547 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x is not settable", ptp->params[0]);
1548 ptp_response (cam, PTP_RC_AccessDenied, 0);
1549 return 1;
1550 }
1551 ptp_properties[i].getdesc(cam, &desc);
1552 if (!get_propval (data, len, desc.DataType, &val)) {
1553 gp_log (GP_LOG_ERROR,__FUNCTION__, "deviceprop 0x%04x is not retrievable", ptp->params[0]);
1554 ptp_response (cam, PTP_RC_InvalidDevicePropFormat, 0);
1555 return 1;
1556 }
1557 ptp_properties[i].setvalue (cam, &val);
1558 ptp_response (cam, PTP_RC_OK, 0);
1559 return 1;
1560 }
1561
1562
1563 /************************** Properties *****************************************************/
1564 static int
1565 ptp_battery_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1566 desc->DevicePropCode = 0x5001;
1567 desc->DataType = 2; /* uint8 */
1568 desc->GetSet = 0; /* Get only */
1569 desc->DefaultValue.u8 = 50;
1570 desc->CurrentValue.u8 = 50;
1571 desc->FormFlag = 0x01; /* range */
1572 desc->FORM.Range.MinValue.u8 = 0;
1573 desc->FORM.Range.MaxValue.u8 = 100;
1574 desc->FORM.Range.StepSize.u8 = 1;
1575 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5001, 0xffffffff);
1576 return 1;
1577 }
1578
1579 static int
1580 ptp_battery_getvalue (vcamera* cam, PTPPropValue *val) {
1581 val->u8 = 50;
1582 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5001, 0xffffffff);
1583 return 1;
1584 }
1585
1586 static int
1587 ptp_imagesize_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1588 desc->DevicePropCode = 0x5003;
1589 desc->DataType = 0xffff; /* STR */
1590 desc->GetSet = 0; /* Get only */
1591 desc->DefaultValue.str = strdup("640x480");
1592 desc->CurrentValue.str = strdup("640x480");
1593 desc->FormFlag = 0x02; /* enum */
1594 desc->FORM.Enum.NumberOfValues = 3;
1595 desc->FORM.Enum.SupportedValue = malloc(3*sizeof(desc->FORM.Enum.SupportedValue[0]));
1596 desc->FORM.Enum.SupportedValue[0].str = strdup("640x480");
1597 desc->FORM.Enum.SupportedValue[1].str = strdup("1024x768");
1598 desc->FORM.Enum.SupportedValue[2].str = strdup("2048x1536");
1599
1600 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5003, 0xffffffff);
1601 return 1;
1602 }
1603
1604 static int
1605 ptp_imagesize_getvalue (vcamera* cam, PTPPropValue *val) {
1606 val->str = strdup("640x480");
1607 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5003, 0xffffffff);
1608 return 1;
1609 }
1610
1611 static int
1612 ptp_shutterspeed_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1613 desc->DevicePropCode = 0x500D;
1614 desc->DataType = 0x0006; /* UINT32 */
1615 desc->GetSet = 1; /* Get/Set */
1616 if (!cam->shutterspeed) cam->shutterspeed = 100; /* 1/100 * 10000 */
1617 desc->DefaultValue.u32 = cam->shutterspeed;
1618 desc->CurrentValue.u32 = cam->shutterspeed;
1619 desc->FormFlag = 0x02; /* enum */
1620 desc->FORM.Enum.NumberOfValues = 9;
1621 desc->FORM.Enum.SupportedValue = malloc(desc->FORM.Enum.NumberOfValues*sizeof(desc->FORM.Enum.SupportedValue[0]));
1622 desc->FORM.Enum.SupportedValue[0].u32 = 10000;
1623 desc->FORM.Enum.SupportedValue[1].u32 = 1000;
1624 desc->FORM.Enum.SupportedValue[2].u32 = 500;
1625 desc->FORM.Enum.SupportedValue[3].u32 = 200;
1626 desc->FORM.Enum.SupportedValue[4].u32 = 100;
1627 desc->FORM.Enum.SupportedValue[5].u32 = 50;
1628 desc->FORM.Enum.SupportedValue[6].u32 = 25;
1629 desc->FORM.Enum.SupportedValue[7].u32 = 12;
1630 desc->FORM.Enum.SupportedValue[8].u32 = 1;
1631
1632 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x500D, 0xffffffff);
1633 return 1;
1634 }
1635
1636 static int
1637 ptp_shutterspeed_getvalue (vcamera* cam, PTPPropValue *val) {
1638 val->u32 = cam->shutterspeed;
1639 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x500d, 0xffffffff);
1640 return 1;
1641 }
1642
1643 static int
1644 ptp_shutterspeed_setvalue (vcamera* cam, PTPPropValue *val) {
1645 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x500d, 0xffffffff);
1646 gp_log (GP_LOG_DEBUG, __FUNCTION__, "got %d as value", val->u32);
1647 cam->shutterspeed = val->u32;
1648 return 1;
1649 }
1650
1651 static int
1652 ptp_fnumber_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1653 desc->DevicePropCode = 0x5007;
1654 desc->DataType = 0x0004; /* UINT16 */
1655 desc->GetSet = 1; /* Get/Set */
1656 if (!cam->fnumber) cam->fnumber = 280; /* 2.8 * 100 */
1657 desc->DefaultValue.u16 = cam->fnumber;
1658 desc->CurrentValue.u16 = cam->fnumber;
1659 desc->FormFlag = 0x02; /* enum */
1660 desc->FORM.Enum.NumberOfValues = 18;
1661 desc->FORM.Enum.SupportedValue = malloc(desc->FORM.Enum.NumberOfValues*sizeof(desc->FORM.Enum.SupportedValue[0]));
1662 desc->FORM.Enum.SupportedValue[0].u16 = 280;
1663 desc->FORM.Enum.SupportedValue[1].u16 = 350;
1664 desc->FORM.Enum.SupportedValue[2].u16 = 400;
1665 desc->FORM.Enum.SupportedValue[3].u16 = 450;
1666 desc->FORM.Enum.SupportedValue[4].u16 = 500;
1667 desc->FORM.Enum.SupportedValue[5].u16 = 560;
1668 desc->FORM.Enum.SupportedValue[6].u16 = 630;
1669 desc->FORM.Enum.SupportedValue[7].u16 = 710;
1670 desc->FORM.Enum.SupportedValue[8].u16 = 800;
1671 desc->FORM.Enum.SupportedValue[9].u16 = 900;
1672 desc->FORM.Enum.SupportedValue[10].u16 = 1000;
1673 desc->FORM.Enum.SupportedValue[11].u16 = 1100;
1674 desc->FORM.Enum.SupportedValue[12].u16 = 1300;
1675 desc->FORM.Enum.SupportedValue[13].u16 = 1400;
1676 desc->FORM.Enum.SupportedValue[14].u16 = 1600;
1677 desc->FORM.Enum.SupportedValue[15].u16 = 1800;
1678 desc->FORM.Enum.SupportedValue[16].u16 = 2000;
1679 desc->FORM.Enum.SupportedValue[17].u16 = 2200;
1680
1681 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5007, 0xffffffff);
1682 return 1;
1683 }
1684
1685 static int
1686 ptp_fnumber_getvalue (vcamera* cam, PTPPropValue *val) {
1687 val->u16 = cam->fnumber;
1688 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5007, 0xffffffff);
1689 return 1;
1690 }
1691
1692 static int
1693 ptp_fnumber_setvalue (vcamera* cam, PTPPropValue *val) {
1694 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5007, 0xffffffff);
1695 gp_log (GP_LOG_DEBUG, __FUNCTION__, "got %d as value", val->u16);
1696 cam->fnumber = val->u16;
1697 return 1;
1698 }
1699
1700 static int
1701 ptp_exposurebias_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1702 desc->DevicePropCode = 0x5010;
1703 desc->DataType = 0x0003; /* INT16 */
1704 desc->GetSet = 1; /* Get/Set */
1705 if (!cam->exposurebias) cam->exposurebias = 0; /* 0.0 */
1706 desc->DefaultValue.i16 = cam->exposurebias;
1707 desc->CurrentValue.i16 = cam->exposurebias;
1708 desc->FormFlag = 0x02; /* enum */
1709 desc->FORM.Enum.NumberOfValues = 13;
1710 desc->FORM.Enum.SupportedValue = malloc(desc->FORM.Enum.NumberOfValues*sizeof(desc->FORM.Enum.SupportedValue[0]));
1711 desc->FORM.Enum.SupportedValue[0].i16 = -3000;
1712 desc->FORM.Enum.SupportedValue[1].i16 = -2500;
1713 desc->FORM.Enum.SupportedValue[2].i16 = -2000;
1714 desc->FORM.Enum.SupportedValue[3].i16 = -1500;
1715 desc->FORM.Enum.SupportedValue[4].i16 = -1000;
1716 desc->FORM.Enum.SupportedValue[5].i16 = -500;
1717 desc->FORM.Enum.SupportedValue[6].i16 = 0;
1718 desc->FORM.Enum.SupportedValue[7].i16 = 500;
1719 desc->FORM.Enum.SupportedValue[8].i16 = 1000;
1720 desc->FORM.Enum.SupportedValue[9].i16 = 1500;
1721 desc->FORM.Enum.SupportedValue[10].i16 = 2000;
1722 desc->FORM.Enum.SupportedValue[11].i16 = 2500;
1723 desc->FORM.Enum.SupportedValue[12].i16 = 3000;
1724
1725 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5010, 0xffffffff);
1726 return 1;
1727 }
1728
1729 static int
1730 ptp_exposurebias_getvalue (vcamera* cam, PTPPropValue *val) {
1731 val->i16 = cam->exposurebias;
1732 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5010, 0xffffffff);
1733 return 1;
1734 }
1735
1736 static int
1737 ptp_exposurebias_setvalue (vcamera* cam, PTPPropValue *val) {
1738 ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5010, 0xffffffff);
1739 gp_log (GP_LOG_DEBUG, __FUNCTION__, "got %d as value", val->i16);
1740 cam->exposurebias = val->i16;
1741 return 1;
1742 }
1743
1744
1745 static int
1746 ptp_datetime_getdesc (vcamera* cam, PTPDevicePropDesc *desc) {
1747 struct tm *tm;
1748 time_t xtime;
1749
1750 desc->DevicePropCode = 0x5011;
1751 desc->DataType = 0xffff; /* string */
1752 desc->GetSet = 1; /* get only */
1753 time(&xtime);
1754 tm = gmtime(&xtime);
1755 desc->DefaultValue.str = aprintf("%04d%02d%02dT%02d%02d%02d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
1756 desc->CurrentValue.str = strdup (desc->DefaultValue.str);
1757 desc->FormFlag = 0; /* no form */
1758 /*ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5011, 0xffffffff);*/
1759 return 1;
1760 }
1761
1762 static int
1763 ptp_datetime_getvalue (vcamera* cam, PTPPropValue *val) {
1764 struct tm *tm;
1765 time_t xtime;
1766
1767 time(&xtime);
1768 tm = gmtime(&xtime);
1769 val->str = aprintf("%04d%02d%02dT%02d%02d%02d",tm->tm_year + 1900,tm->tm_mon + 1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
1770 /*ptp_inject_interrupt (cam, 1000, 0x4006, 1, 0x5011, 0xffffffff);*/
1771 return 1;
1772 }
1773
1774
1775 static int
1776 ptp_datetime_setvalue (vcamera* cam, PTPPropValue *val) {
1777 gp_log (GP_LOG_DEBUG, __FUNCTION__, "got %s as value", val->str);
1778 return 1;
1779 }
1780
1781
1782 /********************************************************************************************/
1783
1784 static int vcam_init(vcamera* cam) {
1785 return GP_OK;
1786 }
1787
1788 static int vcam_exit(vcamera* cam) {
1789 return GP_OK;
1790 }
1791
1792 static int vcam_open(vcamera* cam, const char *port) {
1793 #ifdef FUZZING
1794 char *s = strchr(port,':');
1795
1796 if (s) {
1797 if (s[1] == '>') { /* record mode */
1798 cam->fuzzf = fopen(s+2,"wb");
1799 cam->fuzzmode = FUZZMODE_PROTOCOL;
1800 } else {
1801 cam->fuzzf = fopen(s+1,"rb");
1802 #ifndef FUZZ_PTP
1803 /* first 4 byte are vendor and product USB id */
1804 if (cam->fuzzf)
1805 fseek (cam->fuzzf, 4, SEEK_SET);
1806 #endif
1807 cam->fuzzpending = 0;
1808 cam->fuzzmode = FUZZMODE_NORMAL;
1809 }
1810 /* in virtual camera mode we have no fuzzing
1811 if (cam->fuzzf == NULL) {
1812 perror(s+1);
1813 }
1814 */
1815 }
1816 #endif
1817 return GP_OK;
1818 }
1819
1820 static int vcam_close(vcamera* cam) {
1821 #ifdef FUZZING
1822 if (cam->fuzzf) {
1823 fclose (cam->fuzzf);
1824 cam->fuzzf = NULL;
1825 cam->fuzzpending = 0;
1826 }
1827 #endif
1828 free (cam->inbulk);
1829 free (cam->outbulk);
1830 return GP_OK;
1831 }
1832
1833 static void
1834 vcam_process_output(vcamera *cam) {
1835 ptpcontainer ptp;
1836 int i, j;
1837
1838 if (cam->nroutbulk < 4)
1839 return; /* wait for more data */
1840
1841 ptp.size = get_32bit_le (cam->outbulk);
1842 if (ptp.size > cam->nroutbulk)
1843 return; /* wait for more data */
1844
1845 if (ptp.size < 12) { /* No ptp command can be less than 12 bytes */
1846 /* not clear if normal cameras react like this */
1847 gp_log (GP_LOG_ERROR, __FUNCTION__, "input size was %d, minimum is 12", ptp.size);
1848 ptp_response (cam, PTP_RC_GeneralError, 0);
1849 memmove (cam->outbulk, cam->outbulk+ptp.size, cam->nroutbulk-ptp.size);
1850 cam->nroutbulk -= ptp.size;
1851 return;
1852 }
1853 /* ptp: 4 byte size, 2 byte opcode, 2 byte type, 4 byte serial number */
1854 ptp.type = get_16bit_le (cam->outbulk+4);
1855 ptp.code = get_16bit_le (cam->outbulk+6);
1856 ptp.seqnr = get_32bit_le (cam->outbulk+8);
1857
1858 if ((ptp.type != 1) && (ptp.type != 2)) { /* We want either CMD or DATA phase. */
1859 /* not clear if normal cameras react like this */
1860 gp_log (GP_LOG_ERROR, __FUNCTION__, "expected CMD or DATA, but type was %d", ptp.type);
1861 ptp_response (cam, PTP_RC_GeneralError, 0);
1862 memmove (cam->outbulk, cam->outbulk+ptp.size, cam->nroutbulk-ptp.size);
1863 cam->nroutbulk -= ptp.size;
1864 return;
1865 }
1866 if ((ptp.code & 0x7000) != 0x1000) {
1867 /* not clear if normal cameras react like this */
1868 gp_log (GP_LOG_ERROR, __FUNCTION__, "OPCODE 0x%04x does not start with 0x1 or 0x9", ptp.code);
1869 ptp_response (cam, PTP_RC_GeneralError, 0);
1870 memmove (cam->outbulk, cam->outbulk+ptp.size, cam->nroutbulk-ptp.size);
1871 cam->nroutbulk -= ptp.size;
1872 return;
1873 }
1874 if (ptp.type == 1) {
1875 if ((ptp.size - 12) % 4) {
1876 /* not clear if normal cameras react like this */
1877 gp_log (GP_LOG_ERROR, __FUNCTION__, "SIZE-12 is not divisible by 4, but is %d", ptp.size-12);
1878 ptp_response (cam, PTP_RC_GeneralError, 0);
1879 memmove (cam->outbulk, cam->outbulk+ptp.size, cam->nroutbulk-ptp.size);
1880 cam->nroutbulk -= ptp.size;
1881 return;
1882 }
1883 if ((ptp.size - 12) / 4 >= 6) {
1884 /* not clear if normal cameras react like this */
1885 gp_log (GP_LOG_ERROR, __FUNCTION__, "(SIZE-12)/4 is %d, exceeds maximum arguments", (ptp.size-12)/4);
1886 ptp_response (cam, PTP_RC_GeneralError, 0);
1887 memmove (cam->outbulk, cam->outbulk+ptp.size, cam->nroutbulk-ptp.size);
1888 cam->nroutbulk -= ptp.size;
1889 return;
1890 }
1891 ptp.nparams = (ptp.size - 12)/4;
1892 for (i=0;i<ptp.nparams;i++)
1893 ptp.params[i] = get_32bit_le (cam->outbulk+12+i*4);
1894 }
1895
1896 cam->nroutbulk -= ptp.size;
1897
1898 /* call the opcode handler */
1899 for (j=0;j<sizeof(ptp_functions)/sizeof(ptp_functions[0]);j++) {
1900 struct ptp_function *funcs = ptp_functions[j].functions;
1901
1902 if ((ptp_functions[j].type != GENERIC_PTP) && (ptp_functions[j].type != cam->type))
1903 continue;
1904
1905 for (i=0;i<ptp_functions[j].nroffunctions;i++) {
1906 if (funcs[i].code == ptp.code) {
1907 if (ptp.type == 1) {
1908 funcs[i].write (cam, &ptp);
1909 memcpy(&cam->ptpcmd, &ptp, sizeof(ptp));
1910 } else {
1911 if (funcs[i].write_data == NULL) {
1912 gp_log (GP_LOG_ERROR, __FUNCTION__, "opcode 0x%04x received with dataphase, but no dataphase expected", ptp.code);
1913 ptp_response (cam, PTP_RC_GeneralError, 0);
1914 } else {
1915 funcs[i].write_data (cam, &cam->ptpcmd, cam->outbulk+12, ptp.size-12);
1916 }
1917 }
1918 return;
1919 }
1920 }
1921 }
1922 gp_log (GP_LOG_ERROR,__FUNCTION__,"received an unsupported opcode 0x%04x", ptp.code);
1923 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1924 /* in fuzzing mode, be less strict with unknown opcodes */
1925 ptp_response (cam, PTP_RC_OperationNotSupported, 0);
1926 #else
1927 ptp_response (cam, PTP_RC_OK, 0);
1928 #endif
1929 }
1930
1931 static int
1932 vcam_read(vcamera*cam, int ep, unsigned char *data, int bytes) {
1933 unsigned int toread = bytes;
1934
1935 #ifdef FUZZING
1936 /* here we are not using the virtual ptp camera, but just read from a file, or write to it. */
1937 if (cam->fuzzf) {
1938 unsigned int hasread;
1939
1940 memset(data,0,toread);
1941 if (cam->fuzzmode == FUZZMODE_PROTOCOL) {
1942 fwrite(cam->inbulk, 1, toread, cam->fuzzf);
1943 /* fallthrough */
1944 } else {
1945 #ifdef FUZZ_PTP
1946 /* for reading fuzzer data */
1947 if (cam->fuzzpending) {
1948 toread = cam->fuzzpending;
1949 if (toread > bytes) toread = bytes;
1950 cam->fuzzpending -= toread;
1951 hasread = fread (data, 1, toread, cam->fuzzf);
1952 } else {
1953 hasread = fread (data, 1, 4, cam->fuzzf);
1954 if (hasread != 4)
1955 return GP_ERROR_IO_READ;
1956
1957 toread = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
1958
1959 if (toread > bytes) {
1960 cam->fuzzpending = toread - bytes;
1961 toread = bytes;
1962 }
1963 if (toread <= 4)
1964 return toread;
1965
1966 toread -= 4;
1967
1968 hasread = fread(data + 4, 1, toread, cam->fuzzf);
1969
1970 hasread += 4; /* readd size */
1971 }
1972 #else
1973 /* just return a blob of data in generic fuzzing */
1974 hasread = fread (data, 1, bytes, cam->fuzzf);
1975 if (!hasread && feof(cam->fuzzf))
1976 return GP_ERROR_IO_READ;
1977 #endif
1978 #if 0
1979 for (i=0;i<toread;i++)
1980 data[i] ^= cam->inbulk[i];
1981 #endif
1982 toread = hasread;
1983
1984 return toread;
1985 }
1986 }
1987 #endif /* FUZZING */
1988
1989 /* Emulated PTP camera stuff */
1990
1991 if (toread > cam->nrinbulk)
1992 toread = cam->nrinbulk;
1993
1994 memcpy (data, cam->inbulk, toread);
1995 memmove (cam->inbulk, cam->inbulk + toread, (cam->nrinbulk - toread));
1996 cam->nrinbulk -= toread;
1997 return toread;
1998 }
1999
2000 static int vcam_write(vcamera*cam, int ep, const unsigned char *data, int bytes) {
2001 /*gp_log_data("vusb", data, bytes, "data, vcam_write");*/
2002 if (!cam->outbulk) {
2003 cam->outbulk = malloc(bytes);
2004 } else {
2005 cam->outbulk = realloc(cam->outbulk,cam->nroutbulk + bytes);
2006 }
2007 memcpy(cam->outbulk + cam->nroutbulk, data, bytes);
2008 cam->nroutbulk += bytes;
2009
2010 vcam_process_output(cam);
2011
2012 return bytes;
2013 }
2014
2015 struct ptp_interrupt {
2016 unsigned char *data;
2017 int size;
2018 struct timeval triggertime;
2019 struct ptp_interrupt *next;
2020 };
2021
2022 static struct ptp_interrupt *first_interrupt;
2023
2024 static int
2025 ptp_inject_interrupt(vcamera*cam, int when, uint16_t code, int nparams, uint32_t param1, uint32_t transid) {
2026 struct ptp_interrupt *interrupt, **pint;
2027 struct timeval now;
2028 unsigned char *data;
2029 int x = 0;
2030
2031 gp_log (GP_LOG_DEBUG, __FUNCTION__, "generate interrupt 0x%04x, %d params, param1 0x%08x, timeout=%d", code, nparams, param1, when);
2032
2033 gettimeofday (&now, NULL);
2034 now.tv_usec += (when % 1000)*1000;
2035 now.tv_sec += when / 1000;
2036 if (now.tv_usec > 1000000) {
2037 now.tv_usec -= 1000000;
2038 now.tv_sec++;
2039 }
2040
2041 data = malloc (0x10);
2042 x += put_32bit_le (data+x, 0x10);
2043 x += put_16bit_le (data+x, 4);
2044 x += put_16bit_le (data+x, code);
2045 x += put_32bit_le (data+x, transid);
2046 x += put_32bit_le (data+x, param1);
2047
2048 interrupt = malloc (sizeof(struct ptp_interrupt));
2049 interrupt->data = data;
2050 interrupt->size = x;
2051 interrupt->triggertime = now;
2052 interrupt->next = NULL;
2053
2054 /* Insert into list, sorted by trigger time, next triggering one first */
2055 pint = &first_interrupt;
2056 while (*pint) {
2057 if (now.tv_sec > (*pint)->triggertime.tv_sec) {
2058 pint = &((*pint)->next);
2059 continue;
2060 }
2061 if ( (now.tv_sec == (*pint)->triggertime.tv_sec) &&
2062 (now.tv_usec > (*pint)->triggertime.tv_usec)) {
2063 pint = &((*pint)->next);
2064 continue;
2065 }
2066 interrupt->next = *pint;
2067 *pint = interrupt;
2068 break;
2069 }
2070 if (!*pint) /* single entry */
2071 *pint = interrupt;
2072 return 1;
2073 }
2074
2075 static int
2076 vcam_readint(vcamera*cam, unsigned char *data, int bytes, int timeout) {
2077 struct timeval now, end;
2078 int newtimeout, tocopy;
2079 struct ptp_interrupt *pint;
2080
2081 if (!first_interrupt) {
2082 #ifdef FUZZING
2083 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2084 usleep (timeout*1000);
2085 #endif
2086 /* this emulates plugged out devices during fuzzing */
2087 if (cam->fuzzf && feof(cam->fuzzf))
2088 return GP_ERROR_IO;
2089 #endif /* FUZZING */
2090 return GP_ERROR_TIMEOUT;
2091 }
2092 gettimeofday (&now, NULL);
2093 end = now;
2094 end.tv_usec += (timeout % 1000)*1000;
2095 end.tv_sec += timeout / 1000;
2096 if (end.tv_usec > 1000000) {
2097 end.tv_usec -= 1000000;
2098 end.tv_sec++;
2099 }
2100 if (first_interrupt->triggertime.tv_sec > end.tv_sec) {
2101 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2102 usleep (1000*timeout);
2103 #endif
2104 return GP_ERROR_TIMEOUT;
2105 }
2106 if ( (first_interrupt->triggertime.tv_sec == end.tv_sec) &&
2107 (first_interrupt->triggertime.tv_usec > end.tv_usec)
2108 ) {
2109 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2110 usleep (1000*timeout);
2111 #endif
2112 return GP_ERROR_TIMEOUT;
2113 }
2114 newtimeout = (first_interrupt->triggertime.tv_sec - now.tv_sec)*1000 + (first_interrupt->triggertime.tv_usec - now.tv_usec)/1000;
2115 if (newtimeout > timeout)
2116 gp_log (GP_LOG_ERROR, __FUNCTION__, "miscalculated? %d vs %d", timeout, newtimeout);
2117 tocopy = first_interrupt->size;
2118 if (tocopy > bytes)
2119 tocopy = bytes;
2120 memcpy (data, first_interrupt->data, tocopy);
2121 pint = first_interrupt;
2122 first_interrupt = first_interrupt->next;
2123 free (pint->data);
2124 free (pint);
2125 return tocopy;
2126 }
2127
2128 vcamera*
2129 vcamera_new(vcameratype type) {
2130 vcamera *cam;
2131
2132 cam = calloc(1,sizeof(vcamera));
2133 if (!cam) return NULL;
2134
2135 const char *vcameradir_env = getenv("VCAMERADIR");
2136 read_tree(vcameradir_env != NULL ? vcameradir_env : VCAMERADIR);
2137
2138 cam->init = vcam_init;
2139 cam->exit = vcam_exit;
2140 cam->open = vcam_open;
2141 cam->close = vcam_close;
2142
2143 cam->read = vcam_read;
2144 cam->readint = vcam_readint;
2145 cam->write = vcam_write;
2146
2147 cam->type = type;
2148 cam->seqnr = 0;
2149
2150 return cam;
2151 }