-
Notifications
You must be signed in to change notification settings - Fork 329
/
config.c
11338 lines (10090 loc) · 397 KB
/
config.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
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* config.c
*
* Copyright (C) 2003-2021 Marcus Meissner <[email protected]>
*
* 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
*/
#define _DEFAULT_SOURCE
#include "config.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#ifdef WIN32
# include <winsock.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include <sys/types.h>
#include <gphoto2/gphoto2-library.h>
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-setting.h>
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) dgettext (GETTEXT_PACKAGE, String)
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
# define textdomain(String) (String)
# define gettext(String) (String)
# define dgettext(Domain,Message) (Message)
# define dcgettext(Domain,Message,Type) (Message)
# define bindtextdomain(Domain,Directory) (Domain)
# define _(String) (String)
# define N_(String) (String)
#endif
#include "ptp.h"
#include "ptp-bugs.h"
#include "ptp-private.h"
#include "ptp-pack.c"
#ifdef __GNUC__
# define __unused__ __attribute__((unused))
#else
# define __unused__
#endif
#define SET_CONTEXT(camera, ctx) ((PTPData *) camera->pl->params.data)->context = ctx
int
have_prop(Camera *camera, uint16_t vendor, uint16_t prop) {
unsigned int i;
/* prop 0 matches */
if (!prop && (camera->pl->params.deviceinfo.VendorExtensionID==vendor))
return 1;
if ( ((prop & 0x7000) == 0x5000) ||
(NIKON_1(&camera->pl->params) && ((prop & 0xf000) == 0xf000))
) { /* properties */
for (i=0; i<camera->pl->params.deviceinfo.DevicePropertiesSupported_len; i++) {
if (prop != camera->pl->params.deviceinfo.DevicePropertiesSupported[i])
continue;
if ((prop & 0xf000) == 0x5000) { /* generic property */
if (!vendor || (camera->pl->params.deviceinfo.VendorExtensionID==vendor))
return 1;
}
if (camera->pl->params.deviceinfo.VendorExtensionID==vendor)
return 1;
}
}
if ((prop & 0x7000) == 0x1000) { /* commands */
for (i=0; i<camera->pl->params.deviceinfo.OperationsSupported_len; i++) {
if (prop != camera->pl->params.deviceinfo.OperationsSupported[i])
continue;
if ((prop & 0xf000) == 0x1000) /* generic property */
return 1;
if (camera->pl->params.deviceinfo.VendorExtensionID==vendor)
return 1;
}
}
return 0;
}
static int
camera_prepare_chdk_capture(Camera *camera, GPContext *context) {
PTPParams *params = &camera->pl->params;
int scriptid = 0, major = 0,minor = 0;
unsigned int status;
int luastatus;
ptp_chdk_script_msg *msg = NULL;
char *lua =
PTP_CHDK_LUA_SERIALIZE
"if not get_mode() then\n\
switch_mode_usb(1)\n\
local i=0\n\
while not get_mode() and i < 300 do\n\
sleep(10)\n\
i=i+1\n\
end\n\
if not get_mode() then\n\
return false, 'switch failed'\n\
end\n\
return true\n\
end\n\
return false,'already in rec'\n\
";
C_PTP (ptp_chdk_get_version (params, &major, &minor));
GP_LOG_D ("CHDK %d.%d", major, minor);
GP_LOG_D ("calling lua script %s", lua);
C_PTP (ptp_chdk_exec_lua(params, lua, 0, &scriptid, &luastatus));
GP_LOG_D ("called script. script id %d, status %d", scriptid, luastatus);
while (1) {
C_PTP (ptp_chdk_get_script_status(params, &status));
GP_LOG_D ("script status %x", status);
if (status & PTP_CHDK_SCRIPT_STATUS_MSG) {
C_PTP (ptp_chdk_read_script_msg(params, &msg));
GP_LOG_D ("message script id %d, type %d, subtype %d", msg->script_id, msg->type, msg->subtype);
GP_LOG_D ("message script %s", msg->data);
free (msg);
}
if (!(status & PTP_CHDK_SCRIPT_STATUS_RUN))
break;
usleep(100000);
}
return GP_OK;
}
static int
camera_unprepare_chdk_capture(Camera *camera, GPContext *context) {
PTPParams *params = &camera->pl->params;
int scriptid = 0, status = 0;
ptp_chdk_script_msg *msg = NULL;
char *lua =
PTP_CHDK_LUA_SERIALIZE
"if get_mode() then\n\
switch_mode_usb(0)\n\
local i=0\n\
while get_mode() and i < 300 do\n\
sleep(10)\n\
i=i+1\n\
end\n\
if get_mode() then\n\
return false, 'switch failed'\n\
end\n\
return true\n\
end\n\
return false,'already in play'\n\
";
GP_LOG_D ("calling lua script %s", lua);
C_PTP (ptp_chdk_exec_lua(params, lua, 0, &scriptid, &status));
C_PTP (ptp_chdk_read_script_msg(params, &msg));
GP_LOG_D ("called script. script id %d, status %d", scriptid, status);
GP_LOG_D ("message script id %d, type %d, subtype %d", msg->script_id, msg->type, msg->subtype);
GP_LOG_D ("message script %s", msg->data);
free (msg);
if (!status) {
gp_context_error(context,_("CHDK did not leave recording mode."));
return GP_ERROR;
}
return GP_OK;
}
static int
camera_prepare_canon_powershot_capture(Camera *camera, GPContext *context) {
uint16_t ret;
PTPContainer event;
PTPPropertyValue propval;
PTPParams *params = &camera->pl->params;
int found, oldtimeout;
if (ptp_property_issupported(params, PTP_DPC_CANON_FlashMode)) {
GP_LOG_D ("Canon capture mode is already set up.");
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_EventEmulateMode, &propval, PTP_DTC_UINT16));
GP_LOG_D ("Event emulate mode 0x%04x", propval.u16);
params->canon_event_mode = propval.u16;
return GP_OK;
}
propval.u16 = 0;
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_EventEmulateMode, &propval, PTP_DTC_UINT16));
GP_LOG_D ("prop 0xd045 value is 0x%04x",propval.u16);
propval.u16=1;
C_PTP (ptp_setdevicepropvalue(params, PTP_DPC_CANON_EventEmulateMode, &propval, PTP_DTC_UINT16));
params->canon_event_mode = propval.u16;
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_SizeOfOutputDataFromCamera, &propval, PTP_DTC_UINT32));
GP_LOG_D ("prop PTP_DPC_CANON_SizeOfOutputDataFromCamera value is %d",propval.u32);
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_SizeOfInputDataToCamera, &propval, PTP_DTC_UINT32));
GP_LOG_D ("prop PTP_DPC_CANON_SizeOfInputDataToCamera value is %d",propval.u32);
C_PTP (ptp_getdeviceinfo (params, ¶ms->deviceinfo));
C_PTP (ptp_getdeviceinfo (params, ¶ms->deviceinfo));
CR (fixup_cached_deviceinfo (camera, ¶ms->deviceinfo));
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_SizeOfOutputDataFromCamera, &propval, PTP_DTC_UINT32));
GP_LOG_D ("prop PTP_DPC_CANON_SizeOfOutputDataFromCamera value is %d",propval.u32);
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_SizeOfInputDataToCamera, &propval, PTP_DTC_UINT32));
GP_LOG_D ("prop PTP_DPC_CANON_SizeOfInputDataToCamera value is %d",propval.u32);
C_PTP (ptp_getdeviceinfo (params, ¶ms->deviceinfo));
CR (fixup_cached_deviceinfo (camera, ¶ms->deviceinfo));
C_PTP (ptp_getdevicepropvalue(params, PTP_DPC_CANON_EventEmulateMode, &propval, PTP_DTC_UINT16));
params->canon_event_mode = propval.u16;
GP_LOG_D ("prop 0xd045 value is 0x%04x",propval.u16);
GP_LOG_D ("Magic code ends.");
GP_LOG_D ("Setting prop. EventEmulateMode to 7.");
/* interrupt 9013 get event
1 Yes No
2 Yes No
3 Yes Yes
4 Yes Yes
5 Yes Yes
6 No No
7 No Yes
*/
propval.u16 = 7;
C_PTP (ptp_setdevicepropvalue(params, PTP_DPC_CANON_EventEmulateMode, &propval, PTP_DTC_UINT16));
params->canon_event_mode = propval.u16;
ret = ptp_canon_startshootingmode (params);
if (ret == PTP_RC_CANON_A009) {
/* I think this means ... already in capture mode */
return GP_OK;
}
if (ret != PTP_RC_OK) {
GP_LOG_E ("'ptp_canon_startshootingmode (params)' failed: 0x%04x", ret);
C_PTP_REP (ret);
}
gp_port_get_timeout (camera->port, &oldtimeout);
gp_port_set_timeout (camera->port, 1000);
/* Catch the event telling us the mode was switched ... */
/* If we were prepared already, it will be 5*50*1000 wait, so 1/4 second ... hmm */
found = 0;
while (found++<10) {
ret = ptp_check_event (params);
if (ret != PTP_RC_OK)
break;
while (ptp_get_one_event (params, &event)) {
GP_LOG_D ("Event: 0x%x", event.Code);
if ((event.Code==0xc00c) ||
(event.Code==PTP_EC_StorageInfoChanged)) {
GP_LOG_D ("Event: Entered shooting mode.");
found = 1;
break;
}
}
usleep(50*1000);
}
#if 0
gp_port_set_timeout (camera->port, oldtimeout);
if (ptp_operation_issupported(params, PTP_OC_CANON_ViewfinderOn)) {
ret = ptp_canon_viewfinderon (params);
if (ret != PTP_RC_OK)
GP_LOG_E ("Canon enable viewfinder failed: %d", ret);
/* ignore errors here */
}
gp_port_set_timeout (camera->port, 1000);
#endif
/* Reget device info, they change on the Canons. */
C_PTP (ptp_getdeviceinfo(&camera->pl->params, &camera->pl->params.deviceinfo));
CR (fixup_cached_deviceinfo (camera, &camera->pl->params.deviceinfo));
gp_port_set_timeout (camera->port, oldtimeout);
return GP_OK;
}
int
camera_canon_eos_update_capture_target(Camera *camera, GPContext *context, int value) {
PTPParams *params = &camera->pl->params;
char buf[200];
PTPPropertyValue ct_val;
PTPDevicePropDesc dpd;
int cardval = -1;
memset(&dpd,0,sizeof(dpd));
if (!have_eos_prop(params, PTP_VENDOR_CANON, PTP_DPC_CANON_EOS_CaptureDestination) ) {
GP_LOG_D ("No CaptureDestination property?");
return GP_OK;
}
C_PTP (ptp_canon_eos_getdevicepropdesc (params,PTP_DPC_CANON_EOS_CaptureDestination, &dpd));
/* Look for the correct value of the card mode */
if (value != PTP_CANON_EOS_CAPTUREDEST_HD) {
if (dpd.FormFlag == PTP_DPFF_Enumeration) {
unsigned int i;
for (i=0;i<dpd.FORM.Enum.NumberOfValues;i++) {
if (dpd.FORM.Enum.SupportedValue[i].u32 != PTP_CANON_EOS_CAPTUREDEST_HD) {
cardval = dpd.FORM.Enum.SupportedValue[i].u32;
break;
}
}
GP_LOG_D ("Card value is %d",cardval);
}
if (cardval == -1) {
/* https://github.com/gphoto/gphoto2/issues/383 ... we dont get an enum, but the value is 2 ... */
if (dpd.CurrentValue.u32 != PTP_CANON_EOS_CAPTUREDEST_HD) {
cardval = dpd.CurrentValue.u32;
} else {
GP_LOG_D ("NO Card found - falling back to SDRAM!");
cardval = PTP_CANON_EOS_CAPTUREDEST_HD;
}
}
}
if (value == 1)
value = cardval;
/* -1 == use setting from config-file, 1 == card, 4 == ram*/
ct_val.u32 = (value == -1)
? (GP_OK == gp_setting_get("ptp2","capturetarget",buf)) && strcmp(buf,"sdram") ? cardval : PTP_CANON_EOS_CAPTUREDEST_HD
: value;
/* otherwise we get DeviceBusy for some reason */
if (ct_val.u32 != dpd.CurrentValue.u32) {
C_PTP_MSG (ptp_canon_eos_setdevicepropvalue (params, PTP_DPC_CANON_EOS_CaptureDestination, &ct_val, PTP_DTC_UINT32),
"setdevicepropvalue of capturetarget to 0x%x failed", ct_val.u32);
if (ct_val.u32 == PTP_CANON_EOS_CAPTUREDEST_HD) {
uint16_t ret;
/* if we want to download the image from the device, we need to tell the camera
* that we have enough space left. */
/* this might be a trigger value for "no space" -Marcus
ret = ptp_canon_eos_pchddcapacity(params, 0x7fffffff, 0x00001000, 0x00000001);
*/
LOG_ON_PTP_E (ptp_canon_eos_setuilock (params));
ret = ptp_canon_eos_pchddcapacity(params, 0x0fffffff, 0x00001000, 0x00000001);
LOG_ON_PTP_E (ptp_canon_eos_resetuilock (params));
/* not so bad if its just busy, would also fail later. */
if (ret == PTP_RC_DeviceBusy) ret = PTP_RC_OK;
C_PTP (ret);
/* Tricky ... eos1200d seems to take a while to register this change and the first capture
* when it is still switching might be going down the drain. */
while (1) {
C_PTP (ptp_check_eos_events (params));
C_PTP (ptp_canon_eos_getdevicepropdesc (params,PTP_DPC_CANON_EOS_AvailableShots, &dpd));
if (dpd.CurrentValue.u32 > 0)
break;
}
}
} else {
GP_LOG_D ("optimized ... setdevicepropvalue of capturetarget to 0x%x not done as it was set already.", ct_val.u32 );
}
ptp_free_devicepropdesc (&dpd);
return GP_OK;
}
static int
camera_prepare_canon_eos_capture(Camera *camera, GPContext *context) {
PTPParams *params = &camera->pl->params;
PTPStorageIDs sids;
int tries;
GP_LOG_D ("preparing EOS capture...");
if (is_canon_eos_m(params)) {
int mode = 0x15; /* default for EOS M and newer Powershot SX */
if (!strcmp(params->deviceinfo.Model,"Canon PowerShot SX540 HS")) mode = 0x11; /* testing for https://github.com/gphoto/libgphoto2/issues/360 */
if (!strcmp(params->deviceinfo.Model,"Canon PowerShot SX600 HS")) goto skip;
if (!strcmp(params->deviceinfo.Model,"Canon PowerShot G5 X")) mode = 0x11;
if (!strcmp(params->deviceinfo.Model,"Canon EOS M6 Mark II")) mode = 0x1;
C_PTP (ptp_canon_eos_setremotemode(params, mode));
} else {
C_PTP (ptp_canon_eos_setremotemode(params, 1));
}
skip:
C_PTP (ptp_canon_eos_seteventmode(params, 1));
params->eos_camerastatus = -1; /* aka unknown */
if (ptp_operation_issupported(params, PTP_OC_CANON_EOS_SetRequestOLCInfoGroup))
C_PTP (ptp_canon_eos_setrequestolcinfogroup(params, 0x00001fff));
/* Get the initial bulk set of event data */
C_PTP (ptp_check_eos_events (params));
if (ptp_operation_issupported(params, PTP_OC_CANON_EOS_RequestDevicePropValue)) {
/* request additional properties */
/* if one of this is not present GeneralError will be returned, so only check
* and log, but do not error return */
LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_Owner));
LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_Artist));
LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_Copyright));
LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_SerialNumber));
/* LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_DPOFVersion)); */
/* LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_MyMenuList)); */
/* LOG_ON_PTP_E (ptp_canon_eos_requestdevicepropvalue (params, PTP_DPC_CANON_EOS_LensAdjustParams)); */
}
if (ptp_operation_issupported(params, PTP_OC_CANON_EOS_GetDeviceInfoEx)) {
PTPCanonEOSDeviceInfo x;
unsigned int i;
C_PTP (ptp_canon_eos_getdeviceinfo (params, &x));
for (i=0;i<x.EventsSupported_len;i++)
GP_LOG_D ("event: %04x", x.EventsSupported[i]);
for (i=0;i<x.DevicePropertiesSupported_len;i++)
GP_LOG_D ("deviceprop: %04x", x.DevicePropertiesSupported[i]);
for (i=0;i<x.unk_len;i++)
GP_LOG_D ("unk: %04x", x.unk[i]);
ptp_free_EOS_DI (&x);
}
/* The new EOS occasionally returned an empty event set ... likely because we are too fast. try again some times. */
C_PTP (ptp_check_eos_events (params));
tries = 10;
while (--tries && !have_eos_prop(params,PTP_VENDOR_CANON,PTP_DPC_CANON_EOS_EVFOutputDevice)) {
GP_LOG_D("evfoutput device not found, retrying");
usleep(50*1000);
C_PTP (ptp_check_eos_events (params));
}
CR (camera_canon_eos_update_capture_target( camera, context, -1 ));
ptp_free_DI (¶ms->deviceinfo);
C_PTP (ptp_getdeviceinfo(params, ¶ms->deviceinfo));
CR (fixup_cached_deviceinfo (camera, ¶ms->deviceinfo));
C_PTP (ptp_canon_eos_getstorageids(params, &sids));
if (sids.n >= 1) {
unsigned char *sdata;
unsigned int slen;
C_PTP (ptp_canon_eos_getstorageinfo(params, sids.Storage[0], &sdata, &slen ));
free (sdata);
}
free (sids.Storage);
/* FIXME: 9114 call missing here! */
/* Get the second bulk set of 0x9116 property data */
C_PTP (ptp_check_eos_events (params));
params->eos_captureenabled = 1;
/* run this only on EOS M, not on PowerShot SX */
/* I lost track where it is needed.
* Need it:
* + EOS M10
* + PowerShot SX 720HS
* + PowerShot G9x mark II
*
* NOT NEEDED for EOS M6 Mark II ... this behaves like a regular EOS no-M and will disable tethering if we set output=8.
*/
if ( is_canon_eos_m (params) &&
(strcmp(params->deviceinfo.Model,"Canon EOS M6 Mark II") != 0)
) {
/* This code is needed on EOS m3 at least. might not be needed on others ... mess :/ */
PTPPropertyValue ct_val;
GP_LOG_D ("EOS M detected");
C_PTP (ptp_canon_eos_seteventmode(params, 2));
ct_val.u16 = 0x0008;
C_PTP (ptp_canon_eos_setdevicepropvalue (params, PTP_DPC_CANON_EOS_EVFOutputDevice, &ct_val, PTP_DTC_UINT16));
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
usleep(1000*1000); /* 1 second */
#endif
C_PTP (ptp_check_eos_events (params));
}
return GP_OK;
}
int
camera_prepare_capture (Camera *camera, GPContext *context)
{
PTPParams *params = &camera->pl->params;
GP_LOG_D ("prepare_capture");
switch (params->deviceinfo.VendorExtensionID) {
case PTP_VENDOR_FUJI:
{
PTPPropertyValue propval;
/* without the firmware update ... not an error... */
if (!have_prop (camera, PTP_VENDOR_FUJI, PTP_DPC_FUJI_PriorityMode))
return GP_OK;
/* timelapse does:
* d38c -> 1 (PC Mode)
* d207 -> 2 (USB control)
*/
propval.u16 = 0x0001;
LOG_ON_PTP_E (ptp_setdevicepropvalue (params, 0xd38c, &propval, PTP_DTC_UINT16));
propval.u16 = 0x0002;
LOG_ON_PTP_E (ptp_setdevicepropvalue (params, PTP_DPC_FUJI_PriorityMode, &propval, PTP_DTC_UINT16));
return GP_OK;
}
break;
case PTP_VENDOR_CANON:
if (ptp_operation_issupported(params, PTP_OC_CANON_InitiateReleaseControl))
return camera_prepare_canon_powershot_capture(camera,context);
if (ptp_operation_issupported(params, PTP_OC_CHDK))
return camera_prepare_chdk_capture(camera,context);
if (ptp_operation_issupported(params, PTP_OC_CANON_EOS_RemoteRelease) ||
ptp_operation_issupported(params, PTP_OC_CANON_EOS_RemoteReleaseOn)
)
return camera_prepare_canon_eos_capture(camera,context);
gp_context_error(context, _("Sorry, your Canon camera does not support Canon capture"));
return GP_ERROR_NOT_SUPPORTED;
case PTP_VENDOR_PANASONIC: {
char buf[1024];
if ((GP_OK != gp_setting_get("ptp2","capturetarget",buf)) || !strcmp(buf,"sdram"))
C_PTP (ptp_panasonic_setcapturetarget(params, 1));
else
C_PTP (ptp_panasonic_setcapturetarget(params, 0));
break;
}
default:
/* generic capture does not need preparation */
return GP_OK;
}
return GP_OK;
}
static int
camera_unprepare_canon_powershot_capture(Camera *camera, GPContext *context) {
PTPParams *params = &camera->pl->params;
C_PTP (ptp_canon_endshootingmode (params));
if (ptp_operation_issupported(params, PTP_OC_CANON_ViewfinderOff)) {
if (params->canon_viewfinder_on) {
params->canon_viewfinder_on = 0;
LOG_ON_PTP_E (ptp_canon_viewfinderoff (params));
/* ignore errors here */
}
}
/* Reget device info, they change on the Canons. */
C_PTP (ptp_getdeviceinfo(params, ¶ms->deviceinfo));
CR (fixup_cached_deviceinfo (camera, ¶ms->deviceinfo));
return GP_OK;
}
static int
camera_unprepare_canon_eos_capture(Camera *camera, GPContext *context) {
PTPParams *params = &camera->pl->params;
/* just in case we had autofocus running */
if (ptp_operation_issupported(params, PTP_OC_CANON_EOS_AfCancel))
CR (ptp_canon_eos_afcancel(params));
if (is_canon_eos_m (params)) {
PTPPropertyValue ct_val;
ct_val.u16 = 0x0000;
C_PTP (ptp_canon_eos_setdevicepropvalue (params, PTP_DPC_CANON_EOS_EVFOutputDevice, &ct_val, PTP_DTC_UINT16));
}
/* then emits 911b and 911c ... not done yet ... */
CR (camera_canon_eos_update_capture_target(camera, context, 1));
if (ptp_operation_issupported(&camera->pl->params, PTP_OC_CANON_EOS_ResetUILock))
LOG_ON_PTP_E (ptp_canon_eos_resetuilock (params));
/* Drain the rest set of the event data */
C_PTP (ptp_check_eos_events (params));
C_PTP (ptp_canon_eos_setremotemode(params, 0));
C_PTP (ptp_canon_eos_seteventmode(params, 0));
params->eos_captureenabled = 0;
return GP_OK;
}
int
camera_unprepare_capture (Camera *camera, GPContext *context)
{
GP_LOG_D ("Unprepare_capture");
switch (camera->pl->params.deviceinfo.VendorExtensionID) {
case PTP_VENDOR_CANON:
if (ptp_operation_issupported(&camera->pl->params, PTP_OC_CANON_TerminateReleaseControl))
return camera_unprepare_canon_powershot_capture (camera, context);
if (ptp_operation_issupported(&camera->pl->params, PTP_OC_CHDK))
return camera_unprepare_chdk_capture(camera,context);
if (ptp_operation_issupported(&camera->pl->params, PTP_OC_CANON_EOS_RemoteRelease) ||
ptp_operation_issupported(&camera->pl->params, PTP_OC_CANON_EOS_RemoteReleaseOn)
)
return camera_unprepare_canon_eos_capture (camera, context);
gp_context_error(context,
_("Sorry, your Canon camera does not support Canon capture"));
return GP_ERROR_NOT_SUPPORTED;
case PTP_VENDOR_FUJI:
{
PTPPropertyValue propval;
PTPParams *params = &camera->pl->params;
if (params->inliveview) {
C_PTP_REP (ptp_terminateopencapture (params, params->opencapture_transid));
params->inliveview = 0;
}
propval.u16 = 0x0001;
C_PTP (ptp_setdevicepropvalue (params, PTP_DPC_FUJI_PriorityMode, &propval, PTP_DTC_UINT16));
return GP_OK;
}
break;
default:
/* generic capture does not need unpreparation */
return GP_OK;
}
return GP_OK;
}
static uint16_t
nikon_wait_busy(PTPParams *params, int waitms, int timeout) {
uint16_t res;
int tries;
/* wait either 1 second, or 50 tries */
if (waitms)
tries=timeout/waitms;
else
tries=50;
do {
res = ptp_nikon_device_ready(params);
if ( (res != PTP_RC_DeviceBusy) &&
(res != PTP_RC_NIKON_Bulb_Release_Busy)
)
return res;
if (waitms) usleep(waitms*1000)/*wait a bit*/;
} while (tries--);
return res;
}
struct submenu;
#define CONFIG_GET_ARGS Camera *camera, CameraWidget **widget, struct submenu* menu, PTPDevicePropDesc *dpd
#define CONFIG_GET_NAMES camera, widget, menu, dpd
typedef int (*get_func)(CONFIG_GET_ARGS);
#define CONFIG_PUT_ARGS Camera *camera, CameraWidget *widget, PTPPropertyValue *propval, PTPDevicePropDesc *dpd
#define CONFIG_PUT_NAMES camera, widget, propval, dpd
typedef int (*put_func)(CONFIG_PUT_ARGS);
struct menu;
#define CONFIG_MENU_GET_ARGS Camera *camera, CameraWidget **widget, struct menu* menu
typedef int (*get_menu_func)(CONFIG_MENU_GET_ARGS);
#define CONFIG_MENU_PUT_ARGS Camera *camera, CameraWidget *widget
typedef int (*put_menu_func)(CONFIG_MENU_PUT_ARGS);
struct submenu {
char *label;
char *name;
uint16_t propid;
uint16_t vendorid;
uint32_t type; /* for 32bit alignment */
get_func getfunc;
put_func putfunc;
};
struct menu {
char *label;
char *name;
uint16_t usb_vendorid;
uint16_t usb_productid;
/* Either: Standard menu */
struct submenu *submenus;
/* Or: Non-standard menu with custom behaviour */
get_menu_func getfunc;
put_menu_func putfunc;
};
/* Generic helper function for:
*
* ENUM xINTxx propertiess, with potential vendor specific variables. \
*/
#define GENERIC_TABLE(bits,type,dpc) \
struct deviceproptable##bits { \
char *label; \
type value; \
uint16_t vendor_id; \
};\
\
static int \
_get_Generic##bits##Table(CONFIG_GET_ARGS, struct deviceproptable##bits * tbl, int tblsize) { \
int i, j; \
int isset = FALSE, isset2 = FALSE; \
\
if (!(dpd->FormFlag & (PTP_DPFF_Enumeration|PTP_DPFF_Range))) { \
GP_LOG_D ("no enumeration/range in %sbit table code... going on", #bits); \
} \
if (dpd->DataType != dpc) { \
GP_LOG_D ("no %s prop in %sbit table code", #bits, #bits); \
return GP_ERROR; \
} \
\
gp_widget_new (GP_WIDGET_RADIO, _(menu->label), widget); \
gp_widget_set_name (*widget, menu->name); \
if (dpd->FormFlag & PTP_DPFF_Enumeration) { \
if (!dpd->FORM.Enum.NumberOfValues) { \
/* fill in with all values we have in the table. */ \
for (j=0;j<tblsize;j++) { \
if ((tbl[j].vendor_id == 0) || \
(tbl[j].vendor_id == camera->pl->params.deviceinfo.VendorExtensionID) \
) { \
gp_widget_add_choice (*widget, _(tbl[j].label)); \
if (tbl[j].value == dpd->CurrentValue.bits) { \
gp_widget_set_value (*widget, _(tbl[j].label)); \
isset2 = TRUE; \
} \
} \
} \
/* fallthrough in case we do not have currentvalue in the table. isset2 = FALSE */ \
} \
for (i = 0; i<dpd->FORM.Enum.NumberOfValues; i++) { \
isset = FALSE; \
for (j=0;j<tblsize;j++) { \
if ((tbl[j].value == dpd->FORM.Enum.SupportedValue[i].bits) && \
((tbl[j].vendor_id == 0) || \
(tbl[j].vendor_id == camera->pl->params.deviceinfo.VendorExtensionID)) \
) { \
gp_widget_add_choice (*widget, _(tbl[j].label)); \
if (tbl[j].value == dpd->CurrentValue.bits) { \
isset2 = TRUE; \
gp_widget_set_value (*widget, _(tbl[j].label)); \
} \
isset = TRUE; \
break; \
} \
} \
if (!isset) { \
char buf[200]; \
sprintf(buf, _("Unknown value %04x"), dpd->FORM.Enum.SupportedValue[i].bits); \
gp_widget_add_choice (*widget, buf); \
if (dpd->FORM.Enum.SupportedValue[i].bits == dpd->CurrentValue.bits) { \
isset2 = TRUE; \
gp_widget_set_value (*widget, buf); \
} \
} \
} \
} \
if (dpd->FormFlag & PTP_DPFF_Range) { \
type r; \
for ( r = dpd->FORM.Range.MinimumValue.bits; \
r<=dpd->FORM.Range.MaximumValue.bits; \
r+= dpd->FORM.Range.StepSize.bits \
) { \
isset = FALSE; \
for (j=0;j<tblsize;j++) { \
if ((tbl[j].value == r) && \
((tbl[j].vendor_id == 0) || \
(tbl[j].vendor_id == camera->pl->params.deviceinfo.VendorExtensionID)) \
) { \
gp_widget_add_choice (*widget, _(tbl[j].label)); \
if (r == dpd->CurrentValue.bits) { \
isset2 = TRUE; \
gp_widget_set_value (*widget, _(tbl[j].label)); \
} \
isset = TRUE; \
break; \
} \
} \
if (!isset) { \
char buf[200]; \
sprintf(buf, _("Unknown value %04x"), r); \
gp_widget_add_choice (*widget, buf); \
if (r == dpd->CurrentValue.bits) { \
isset2 = TRUE; \
gp_widget_set_value (*widget, buf); \
} \
} \
\
/* device might report stepsize 0. but we do at least 1 round */ \
if (dpd->FORM.Range.StepSize.bits == 0) \
break; \
} \
} \
if (!isset2) { \
for (j=0;j<tblsize;j++) { \
if (((tbl[j].vendor_id == 0) || \
(tbl[j].vendor_id == camera->pl->params.deviceinfo.VendorExtensionID)) && \
(tbl[j].value == dpd->CurrentValue.bits) \
) { \
gp_widget_add_choice (*widget, _(tbl[j].label)); \
isset2 = TRUE; \
gp_widget_set_value (*widget, _(tbl[j].label)); \
} \
} \
if (!isset2) { \
char buf[200]; \
sprintf(buf, _("Unknown value %04x"), dpd->CurrentValue.bits); \
gp_widget_add_choice (*widget, buf); \
gp_widget_set_value (*widget, buf); \
} \
} \
return (GP_OK); \
} \
\
\
static int \
_put_Generic##bits##Table(CONFIG_PUT_ARGS, struct deviceproptable##bits * tbl, int tblsize) { \
char *value; \
int i, intval, j; \
int foundvalue = 0; \
type bits##val = 0; \
\
CR (gp_widget_get_value (widget, &value)); \
for (i=0;i<tblsize;i++) { \
if ((!strcmp(_(tbl[i].label),value) || !strcmp(tbl[i].label,value)) && \
((tbl[i].vendor_id == 0) || (tbl[i].vendor_id == camera->pl->params.deviceinfo.VendorExtensionID)) \
) { \
bits##val = tbl[i].value; \
foundvalue = 1; \
\
if (dpd->FormFlag & PTP_DPFF_Enumeration) { \
for (j = 0; j<dpd->FORM.Enum.NumberOfValues; j++) { \
if (bits##val == dpd->FORM.Enum.SupportedValue[j].bits) { \
GP_LOG_D ("FOUND right value for %s in the enumeration at val %d", value, bits##val); \
propval->bits = bits##val; \
return GP_OK; \
} \
} \
GP_LOG_D ("did not find the right value for %s in the enumeration at val %d... continuing", value, bits##val); \
/* continue looking, but with this value as fallback */ \
} else { \
GP_LOG_D ("not an enumeration ... return %s as %d", value, bits##val); \
propval->bits = bits##val; \
return GP_OK; \
} \
} \
} \
if (foundvalue) { \
GP_LOG_D ("Using fallback, not found in enum... return %s as %d", value, bits##val); \
propval->bits = bits##val; \
return GP_OK; \
} \
if (!sscanf(value, _("Unknown value %04x"), &intval)) { \
GP_LOG_E ("failed to find value %s in list", value); \
return GP_ERROR; \
} \
GP_LOG_D ("Using fallback, not found in enum... return %s as %d", value, bits##val); \
propval->bits = intval; \
return GP_OK; \
}
GENERIC_TABLE(u32,uint32_t,PTP_DTC_UINT32)
GENERIC_TABLE(u16,uint16_t,PTP_DTC_UINT16)
GENERIC_TABLE(i16,int16_t, PTP_DTC_INT16)
GENERIC_TABLE(u8, uint8_t, PTP_DTC_UINT8)
GENERIC_TABLE(i8, int8_t, PTP_DTC_INT8)
#define GENERIC16TABLE(name,tbl) \
static int \
_get_##name(CONFIG_GET_ARGS) { \
return _get_Genericu16Table(CONFIG_GET_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
} \
\
static int __unused__ \
_put_##name(CONFIG_PUT_ARGS) { \
return _put_Genericu16Table(CONFIG_PUT_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
}
#define GENERIC32TABLE(name,tbl) \
static int \
_get_##name(CONFIG_GET_ARGS) { \
return _get_Genericu32Table(CONFIG_GET_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
} \
\
static int __unused__ \
_put_##name(CONFIG_PUT_ARGS) { \
return _put_Genericu32Table(CONFIG_PUT_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
}
#define GENERICI16TABLE(name,tbl) \
static int \
_get_##name(CONFIG_GET_ARGS) { \
return _get_Generici16Table(CONFIG_GET_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
} \
\
static int __unused__ \
_put_##name(CONFIG_PUT_ARGS) { \
return _put_Generici16Table(CONFIG_PUT_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
}
#define GENERIC8TABLE(name,tbl) \
static int \
_get_##name(CONFIG_GET_ARGS) { \
return _get_Genericu8Table(CONFIG_GET_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
} \
\
static int __unused__ \
_put_##name(CONFIG_PUT_ARGS) { \
return _put_Genericu8Table(CONFIG_PUT_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
}
#define GENERICI8TABLE(name,tbl) \
static int \
_get_##name(CONFIG_GET_ARGS) { \
return _get_Generici8Table(CONFIG_GET_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
} \
\
static int __unused__ \
_put_##name(CONFIG_PUT_ARGS) { \
return _put_Generici8Table(CONFIG_PUT_NAMES, \
tbl,sizeof(tbl)/sizeof(tbl[0]) \
); \
}
static int
_get_AUINT8_as_CHAR_ARRAY(CONFIG_GET_ARGS) {
unsigned int j;
char value[128];
gp_widget_new (GP_WIDGET_TEXT, _(menu->label), widget);
gp_widget_set_name (*widget, menu->name);
if (dpd->DataType != PTP_DTC_AUINT8) {
sprintf (value,_("unexpected datatype %i"),dpd->DataType);
} else {
memset(value,0,sizeof(value));
for (j=0;j<dpd->CurrentValue.a.count;j++)
value[j] = dpd->CurrentValue.a.v[j].u8;
}
gp_widget_set_value (*widget,value);
return (GP_OK);
}
static int
_get_STR(CONFIG_GET_ARGS) {
char value[64];
gp_widget_new (GP_WIDGET_TEXT, _(menu->label), widget);
gp_widget_set_name (*widget, menu->name);
if (dpd->DataType != PTP_DTC_STR) {
sprintf (value,_("unexpected datatype %i"),dpd->DataType);
gp_widget_set_value (*widget,value);
} else {
gp_widget_set_value (*widget,dpd->CurrentValue.str);
}
return (GP_OK);
}
static int
_get_STR_ENUMList (CONFIG_GET_ARGS) {
int j;
if (!(dpd->FormFlag & PTP_DPFF_Enumeration))
return GP_ERROR;
if (dpd->DataType != PTP_DTC_STR)
return GP_ERROR;
gp_widget_new (GP_WIDGET_RADIO, _(menu->label), widget);
gp_widget_set_name (*widget, menu->name);
for (j=0;j<dpd->FORM.Enum.NumberOfValues; j++)
gp_widget_add_choice (*widget,dpd->FORM.Enum.SupportedValue[j].str);