Debian Codegraph @

gphoto_driver.cpp

raw
1 /*******************************************************************************
2 Copyright(c) 2009 Geoffrey Hausheer. All rights reserved.
3 Copyright(c) 2012-2016 Jasem Mutlaq. All rights reserved.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 The full GNU General Public License is included in this distribution in the
20 file called LICENSE.
21 *******************************************************************************/
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <cstdlib>
28
29 #include <config.h>
30 #include <indilogger.h>
31 #include <gphoto2/gphoto2-version.h>
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 // the older libraw uses auto_ptr
35 #include <libraw/libraw.h>
36 #pragma GCC diagnostic pop
37
38
39 #include "gphoto_driver.h"
40 #include "dsusbdriver.h"
41 #include <cmath>
42
43 #define EOS_CUSTOMFUNCEX "customfuncex"
44 #define EOS_MIRROR_LOCKUP_ENABLE "20,1,3,14,1,60f,1,1"
45 #define EOS_MIRROR_LOCKUP_DISABLE "20,1,3,14,1,60f,1,0"
46 // Anything below this threshold is camera control
47 // Above this, it is shutter release control
48 #define RELEASE_SHUTTER_THRESHOLD 30000000
49
50 static GPPortInfoList *portinfolist = nullptr;
51 static CameraAbilitiesList *abilities = nullptr;
52
53 static const char *fallbackShutterSpeeds[] =
54 {
55 "1/8000",
56 "1/6400",
57 "1/5000",
58 "1/4000",
59 "1/3200",
60 "1/2500",
61 "1/2000",
62 "1/1600",
63 "1/1250",
64 "1/1000",
65 "1/800",
66 "1/640",
67 "1/500",
68 "1/400",
69 "1/320",
70 "1/250",
71 "1/200",
72 "1/160",
73 "1/125",
74 "1/100",
75 "1/80",
76 "1/60",
77 "1/50",
78 "1/40",
79 "1/30",
80 "1/25",
81 "1/20",
82 "1/15",
83 "1/13",
84 "1/10",
85 "1/8",
86 "1/6",
87 "1/5",
88 "1/4",
89 "1/3",
90 "0.4",
91 "0.5",
92 "0.6",
93 "0.8",
94 "1",
95 "1.3",
96 "1.6",
97 "2",
98 "2.5",
99 "3.2",
100 "4",
101 "5",
102 "6",
103 "8",
104 "10",
105 "13",
106 "15",
107 "20",
108 "25",
109 "30",
110 "BULB"
111 };
112
113 struct _gphoto_widget_list
114 {
115 struct _gphoto_widget_list *next;
116 gphoto_widget *widget;
117 };
118
119 struct _gphoto_driver
120 {
121 Camera *camera;
122 GPContext *context;
123 CameraFile *camerafile;
124 CameraWidget *config;
125 CameraFilePath camerapath;
126 int command;
127 struct timeval bulb_end;
128
129 char filename[80];
130 int width;
131 int height;
132 gphoto_widget *format_widget;
133 gphoto_widget *iso_widget;
134 gphoto_widget *exposure_widget;
135 gphoto_widget *bulb_widget;
136 gphoto_widget *autoexposuremode_widget;
137 gphoto_widget *capturetarget_widget;
138 gphoto_widget *viewfinder_widget;
139 gphoto_widget *focus_widget;
140 gphoto_widget *customfuncex_widget;
141
142 char bulb_port[256];
143 int bulb_fd;
144
145 int exposure_cnt;
146 double *exposureList;
147 int bulb_exposure_index;
148 double max_exposure, min_exposure;
149 bool force_bulb;
150
151 int iso;
152 int format;
153 int upload_settings;
154 CameraImageHandling handle_sdcard_image;
155 bool is_aborted;
156
157 char *model;
158 char *manufacturer;
159
160 char **exposure_presets;
161 int exposure_presets_count;
162
163 bool supports_temperature;
164 float last_sensor_temp;
165 bool bulb_mode {false};
166
167 DSUSBDriver *dsusb;
168
169 gphoto_widget_list *widgets;
170 gphoto_widget_list *iter;
171
172 pthread_mutex_t mutex;
173 pthread_t thread;
174 pthread_cond_t signal;
175 };
176
177 enum
178 {
179 DSLR_CMD_BULB_CAPTURE = 0x01,
180 DSLR_CMD_CAPTURE = 0x02,
181 DSLR_CMD_ABORT = 0x04,
182 DSLR_CMD_DONE = 0x08,
183 DSLR_CMD_THREAD_EXIT = 0x10,
184 };
185
186 static char device[64];
187 static int RTS_flag = TIOCM_RTS;
188
189 void gphoto_set_debug(const char *name)
190 {
191 strncpy(device, name, 64);
192 }
193
194 static const char *widget_name(CameraWidget *widget)
195 {
196 const char *name = nullptr;
197 int ret;
198 if (!widget)
199 return nullptr;
200 ret = gp_widget_get_name(widget, &name);
201 if (ret < GP_OK)
202 {
203 ret = gp_widget_get_label(widget, &name);
204 if (ret < GP_OK)
205 return nullptr;
206 }
207 return name;
208 }
209 /*
210 * This function looks up a label or key entry of
211 * a configuration widget.
212 * The functions descend recursively, so you can just
213 * specify the last component.
214 */
215 static const char *lookup_widget(CameraWidget *config, const char *key, CameraWidget **widget)
216 {
217 int ret;
218 /*
219 * Grab name from widget instead of from 'key' because it will be invariant
220 * as long as the widget is valid
221 */
222 ret = gp_widget_get_child_by_name(config, key, widget);
223 if (ret < GP_OK)
224 gp_widget_get_child_by_label(config, key, widget);
225 return widget_name(*widget);
226 }
227
228 int gphoto_widget_changed(gphoto_widget *widget)
229 {
230 return gp_widget_changed(widget->widget);
231 }
232
233 int gphoto_read_widget(gphoto_widget *widget)
234 {
235 const char *ptr = nullptr;
236 int i;
237 int ret = GP_OK;
238
239 switch (widget->type)
240 {
241 case GP_WIDGET_TEXT:
242 ret = gp_widget_get_value(widget->widget, &widget->value.text);
243 break;
244 case GP_WIDGET_RANGE:
245 ret = gp_widget_get_value(widget->widget, &widget->value.num);
246 gp_widget_get_range(widget->widget, &widget->min, &widget->max, &widget->step);
247 break;
248 case GP_WIDGET_TOGGLE:
249 ret = gp_widget_get_value(widget->widget, &widget->value.toggle);
250 break;
251 case GP_WIDGET_RADIO:
252 case GP_WIDGET_MENU:
253 ret = gp_widget_get_value(widget->widget, &ptr);
254 if (ret != GP_OK)
255 return ret;
256 if (!widget->choices)
257 {
258 widget->choice_cnt = gp_widget_count_choices(widget->widget);
259 widget->choices = (char **)calloc(sizeof(char *), widget->choice_cnt + 1);
260 }
261
262 for (i = 0; i < widget->choice_cnt; i++)
263 {
264 const char *choice = nullptr;
265 ret = gp_widget_get_choice(widget->widget, i, &choice);
266 if (ret != GP_OK)
267 return ret;
268 if (ptr && choice)
269 {
270 if (strcmp(choice, ptr) == 0)
271 widget->value.index = i;
272 widget->choices[i] = (char *)choice;
273 }
274 else
275 return GP_ERROR;
276 }
277 break;
278 case GP_WIDGET_DATE:
279 ret = gp_widget_get_value(widget->widget, &widget->value.date);
280 break;
281 default:
282 DEBUGFDEVICE(device, INDI::Logger::DBG_WARNING, "WARNING: Widget type %d is unsupported", widget->type);
283 }
284 return ret;
285 }
286
287 static gphoto_widget *find_widget(gphoto_driver *gphoto, const char *name)
288 {
289 gphoto_widget *widget = (gphoto_widget *)calloc(sizeof(gphoto_widget), 1);
290 int ret;
291
292 widget->name = lookup_widget(gphoto->config, name, &widget->widget);
293 if (!widget->name)
294 {
295 /*fprintf (stderr, "lookup widget failed: %d", ret);*/
296 goto out;
297 }
298
299 ret = gp_widget_get_type(widget->widget, &widget->type);
300 if (ret < GP_OK)
301 {
302 fprintf(stderr, "widget get type failed: %d", ret);
303 goto out;
304 }
305 ret = gphoto_read_widget(widget);
306 if (ret == GP_OK)
307 return widget;
308 out:
309 free(widget);
310 return nullptr;
311 }
312
313 void show_widget(gphoto_widget *widget, const char *prefix)
314 {
315 int i;
316 struct tm *tm;
317 switch (widget->type)
318 {
319 case GP_WIDGET_TEXT:
320 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sValue: %s", prefix, widget->value.text);
321 break;
322 case GP_WIDGET_RANGE:
323 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sMin: %f", prefix, widget->min);
324 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sMax: %f", prefix, widget->max);
325 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sStep: %f", prefix, widget->step);
326 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sValue: %f", prefix, widget->value.num);
327 break;
328 case GP_WIDGET_TOGGLE:
329 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sValue: %s", prefix, widget->value.toggle ? "On" : "Off");
330 break;
331 case GP_WIDGET_RADIO:
332 case GP_WIDGET_MENU:
333 for (i = 0; i < widget->choice_cnt; i++)
334 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%s%s %3d: %s", prefix,
335 i == widget->value.index ? "*" : " ", i, widget->choices[i]);
336 break;
337 case GP_WIDGET_DATE:
338 tm = gmtime((time_t *)&widget->value.date);
339 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%sValue: %s", prefix, asctime(tm));
340 break;
341 default:
342 break;
343 }
344 }
345
346 int gphoto_set_config(Camera *camera, CameraWidget *config, GPContext *context)
347 {
348 int ret;
349 int retry;
350
351 for (retry = 0; retry < 5; retry++)
352 {
353 ret = gp_camera_set_config(camera, config, context);
354 switch (ret)
355 {
356 case GP_OK:
357 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting new configuration OK.");
358 return ret;
359 break;
360 case GP_ERROR_CAMERA_BUSY:
361 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG,
362 "Failed to set new configuration value (camera busy), retrying...");
363 usleep(500 * 1000);
364 break;
365 default:
366 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Failed to set new configuration value (GP result: %d)",
367 ret);
368 return ret;
369 break;
370 }
371 }
372 return ret;
373 }
374
375 int gphoto_set_widget_num(gphoto_driver *gphoto, gphoto_widget *widget, float value)
376 {
377 int ret;
378 int ival = value;
379 const char *ptr = 0;
380
381 if (!widget)
382 {
383 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Invalid widget specified to set_widget_num");
384 return GP_ERROR_NOT_SUPPORTED;
385 }
386
387 switch (widget->type)
388 {
389 case GP_WIDGET_TOGGLE:
390 ret = gp_widget_set_value(widget->widget, &ival);
391 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting toggle widget %s: %d", widget->name, ival);
392 break;
393 case GP_WIDGET_RADIO:
394 case GP_WIDGET_MENU:
395 ret = gp_widget_get_choice(widget->widget, ival, &ptr);
396 ret = gp_widget_set_value(widget->widget, ptr);
397 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting radio/menu widget %s: %d (%s)", widget->name, ival,
398 widget->choices[ival]);
399 break;
400 case GP_WIDGET_RANGE:
401 ret = gp_widget_set_value(widget->widget, &value);
402 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting range widget %s: %f", widget->name, value);
403 break;
404 default:
405 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Widget type: %d is unsupported", widget->type);
406 return GP_ERROR_NOT_SUPPORTED;
407 }
408
409 if (ret == GP_OK)
410 ret = gphoto_set_config(gphoto->camera, gphoto->config, gphoto->context);
411 else
412 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Failed to set widget %s configuration (%s)", widget->name,
413 gp_result_as_string(ret));
414
415 return ret;
416 }
417
418 int gphoto_set_widget_text(gphoto_driver *gphoto, gphoto_widget *widget, const char *str)
419 {
420 int ret;
421
422 if (!widget || widget->type != GP_WIDGET_TEXT)
423 {
424 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Invalid widget specified to set_widget_text");
425 return 1;
426 }
427
428 ret = gp_widget_set_value(widget->widget, str);
429 if (ret == GP_OK)
430 {
431 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting text widget %s: %s", widget->name, str);
432 ret = gphoto_set_config(gphoto->camera, gphoto->config, gphoto->context);
433 }
434
435 return ret;
436 }
437
438 static void widget_free(gphoto_widget *widget)
439 {
440 if (widget->choices)
441 free(widget->choices);
442 free(widget);
443 }
444
445 static double *parse_shutterspeed(gphoto_driver *gphoto, gphoto_widget *widget)
446 {
447 double *exposure, val;
448 int i, num, denom;
449 double max_exposure = gphoto->max_exposure;
450 double min_exposure = 1e6;
451 gphoto->bulb_exposure_index = -1;
452
453 if (widget->choice_cnt <= 0)
454 {
455 DEBUGFDEVICE(device, INDI::Logger::DBG_WARNING,
456 "Shutter speed widget does not have any valid data (count=%d). Using fallback speeds...",
457 widget->choice_cnt);
458
459 widget->choices = const_cast<char **>(fallbackShutterSpeeds);
460 widget->choice_cnt = 56;
461 widget->type = GP_WIDGET_TEXT;
462 }
463
464 if (widget->choice_cnt > 4)
465 {
466 gphoto->exposure_presets = widget->choices;
467 gphoto->exposure_presets_count = widget->choice_cnt;
468 }
469
470 exposure = (double *)calloc(sizeof(double), widget->choice_cnt);
471
472 for (i = 0; i < widget->choice_cnt; i++)
473 {
474 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Parsing shutter speed #%d: %s", i, widget->choices[i]);
475
476 if ((strncasecmp(widget->choices[i], "bulb", 4) == 0) || (strcmp(widget->choices[i], "65535/65535") == 0))
477 {
478 exposure[i] = -1;
479 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]= BULB", i);
480 gphoto->bulb_exposure_index = i;
481 }
482 else if (sscanf(widget->choices[i], "%d/%d", &num, &denom) == 2)
483 {
484 exposure[i] = 1.0 * num / (double)denom;
485 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]=%g seconds", i, exposure[i]);
486 }
487 else if ((val = strtod(widget->choices[i], nullptr)))
488 {
489 // Fuji returns long exposure values ( > 60s) with m postfix
490 if (widget->choices[i][strlen(widget->choices[i]) - 1] == 'm')
491 {
492 val = val * 60;
493 }
494 exposure[i] = val;
495 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]=%g seconds", i, exposure[i]);
496 }
497 else
498 {
499 // unknown
500 exposure[i] = -2;
501 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]= UNKNOWN", i);
502 }
503
504 if (exposure[i] > max_exposure)
505 max_exposure = exposure[i];
506 if (exposure[i] > 0 && exposure[i] < min_exposure)
507 min_exposure = exposure[i];
508 }
509
510 gphoto->max_exposure = max_exposure;
511 if (min_exposure != 1e6)
512 gphoto->min_exposure = min_exposure;
513
514 return exposure;
515 }
516
517 #if 0
518 static double *parse_shutterspeed(gphoto_driver *gphoto, char **choices, int count)
519 {
520 double *exposure, val;
521 int i, num, denom;
522 double max_exposure = gphoto->max_exposure;
523 double min_exposure = 1e6;
524 gphoto->bulb_exposure_index = -1;
525
526 if (count <= 0)
527 {
528 DEBUGFDEVICE(device, INDI::Logger::DBG_WARNING, "Shutter speed widget does not have any valid data (count=%d)",
529 count);
530 return nullptr;
531 }
532
533 if (count > 4)
534 {
535 gphoto->exposure_presets = choices;
536 gphoto->exposure_presets_count = count;
537 }
538
539 exposure = (double *)calloc(sizeof(double), count);
540
541 for (i = 0; i < count; i++)
542 {
543 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Parsing shutter speed #%d: %s", i, choices[i]);
544
545 if ((strncasecmp(choices[i], "bulb", 4) == 0) || (strcmp(choices[i], "65535/65535") == 0))
546 {
547 exposure[i] = -1;
548 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]= BULB", i);
549 gphoto->bulb_exposure_index = i;
550 }
551 else if (sscanf(choices[i], "%d/%d", &num, &denom) == 2)
552 {
553 exposure[i] = 1.0 * num / (double)denom;
554 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]=%g seconds", i, exposure[i]);
555 }
556 else if ((val = strtod(choices[i], nullptr)))
557 {
558 exposure[i] = val;
559 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]=%g seconds", i, exposure[i]);
560 }
561 else
562 {
563 // unknown
564 exposure[i] = -2;
565 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "exposure[%d]= UNKNOWN", i);
566 }
567
568 if (exposure[i] > max_exposure)
569 max_exposure = exposure[i];
570 if (exposure[i] > 0 && exposure[i] < min_exposure)
571 min_exposure = exposure[i];
572 }
573
574 gphoto->max_exposure = max_exposure;
575 if (min_exposure != 1e6)
576 gphoto->min_exposure = min_exposure;
577
578 return exposure;
579 }
580 #endif
581
582 static void *stop_bulb(void *arg)
583 {
584 int timeout_set = 0;
585 gphoto_driver *gphoto = (gphoto_driver *)arg;
586 //CameraEventType event;
587 long timeleft;
588 struct timespec timeout;
589 struct timeval curtime, diff;
590
591 pthread_mutex_lock(&gphoto->mutex);
592 pthread_cond_signal(&gphoto->signal);
593
594 while (1)
595 {
596 if (!timeout_set)
597 {
598 // 5 second timeout
599 gettimeofday(&curtime, nullptr);
600 timeout.tv_sec = curtime.tv_sec + 5;
601 timeout.tv_nsec = curtime.tv_usec * 1000;
602 }
603 timeout_set = 0;
604 // All camera opertions take place with the mutex held, so we are thread-safe
605 pthread_cond_timedwait(&gphoto->signal, &gphoto->mutex, &timeout);
606 //DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,"timeout expired");
607 if (!(gphoto->command & DSLR_CMD_DONE) && ( (gphoto->command & DSLR_CMD_BULB_CAPTURE)
608 || (gphoto->command & DSLR_CMD_ABORT)))
609 {
610 gphoto->is_aborted = (gphoto->command & DSLR_CMD_ABORT);
611 if (gphoto->command & DSLR_CMD_BULB_CAPTURE)
612 {
613 gettimeofday(&curtime, nullptr);
614 timersub(&gphoto->bulb_end, &curtime, &diff);
615 timeleft = diff.tv_sec * 1000 + diff.tv_usec / 1000;
616 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Time left: %ld ms", timeleft);
617 }
618 else
619 timeleft = 0;
620
621 if (timeleft <= 0)
622 {
623 if (gphoto->dsusb)
624 {
625 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Closing DSUSB shutter.");
626 gphoto->dsusb->closeShutter();
627 }
628 if (gphoto->bulb_widget)
629 {
630 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Closing internal shutter.");
631 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Using widget:%s", gphoto->bulb_widget->name);
632 if (strcmp(gphoto->bulb_widget->name, "eosremoterelease") == 0)
633 {
634 //600D eosremoterelease RELEASE FULL
635 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, EOS_RELEASE_FULL);
636 }
637 else
638 {
639 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, FALSE);
640 }
641 }
642 if (gphoto->bulb_port[0] && (gphoto->bulb_fd >= 0))
643 {
644 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Closing remote serial shutter.");
645
646 // Close Nikon Shutter
647 if (!strstr(device, "Nikon"))
648 {
649 uint8_t close_shutter[3] = {0xFF, 0x01, 0x00};
650 if (write(gphoto->bulb_fd, close_shutter, 3) != 3)
651 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "Closing Nikon remote serial shutter failed.");
652 }
653
654 ioctl(gphoto->bulb_fd, TIOCMBIC, &RTS_flag);
655 close(gphoto->bulb_fd);
656 }
657 gphoto->command |= DSLR_CMD_DONE;
658 pthread_cond_signal(&gphoto->signal);
659 }
660 else if (timeleft < 5000)
661 {
662 timeout_set = 1;
663 timeout.tv_sec = gphoto->bulb_end.tv_sec;
664 timeout.tv_nsec = gphoto->bulb_end.tv_usec * 1000;
665 }
666 }
667 if (!(gphoto->command & DSLR_CMD_DONE) && (gphoto->command & DSLR_CMD_CAPTURE))
668 {
669 //gp_camera_capture(gphoto->camera, GP_CAPTURE_IMAGE, &gphoto->camerapath, gphoto->context);
670 gp_camera_capture(gphoto->camera, GP_CAPTURE_IMAGE, &gphoto->camerapath, gphoto->context);
671 gphoto->command |= DSLR_CMD_DONE;
672 pthread_cond_signal(&gphoto->signal);
673 }
674 if (gphoto->command & DSLR_CMD_THREAD_EXIT)
675 {
676 break;
677 }
678 }
679 pthread_mutex_unlock(&gphoto->mutex);
680 return nullptr;
681 }
682
683 #if 0
684 static void reset_settings(gphoto_driver *gphoto)
685 {
686 if (gphoto->iso >= 0)
687 gphoto_set_widget_num(gphoto, gphoto->iso_widget, gphoto->iso_widget->value.index);
688
689 if (gphoto->format >= 0)
690 gphoto_set_widget_num(gphoto, gphoto->format_widget, gphoto->format_widget->value.index);
691
692 if (gphoto->exposure_widget)
693 gphoto_set_widget_num(gphoto, gphoto->exposure_widget, gphoto->exposure_widget->value.index);
694 }
695 #endif
696
697 int find_bulb_exposure(gphoto_driver *gphoto, gphoto_widget *widget)
698 {
699 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Looking for bulb exposure in %s exposure widget..", widget->name);
700 for (int i = 0; i < widget->choice_cnt; i++)
701 {
702 if (gphoto->exposureList[i] == -1)
703 {
704 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "bulb exposure found! index: %d", i);
705 return i;
706 }
707 }
708 return -1;
709 }
710
711 int find_exposure_setting(gphoto_driver *gphoto, gphoto_widget *widget, uint32_t exptime_usec, bool exact)
712 {
713 double exptime = exptime_usec / 1e6;
714 int best_idx = -1;
715 double delta = 0;
716 double best_match = 99999;
717
718 if (widget == nullptr)
719 {
720 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Cannot find optimal exposure setting due to missing exposure widget.");
721 return -1;
722 }
723
724 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Finding optimal exposure setting for %g seconds in %s (count=%d)...",
725 exptime, widget->name, widget->choice_cnt);
726
727 for (int i = 0; i < widget->choice_cnt; i++)
728 {
729 //DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,"Exposure[%d] = %g", i, gphoto->exposure[i]);
730 if (gphoto->exposureList[i] <= 0)
731 continue;
732
733 delta = exptime - gphoto->exposureList[i];
734 if (exact)
735 {
736 // Close "enough" to be exact
737 if (std::fabs(delta) < 1e-5)
738 {
739 best_match = delta;
740 best_idx = i;
741 break;
742 }
743 }
744 else
745 {
746 if (delta < 0)
747 delta = -delta;
748 // printf("%d: Comparing %f <> %f (delta: %f)", i, exptime, gphoto->exposure[i], delta);
749 if (delta < best_match)
750 {
751 best_match = delta;
752 best_idx = i;
753 }
754 }
755 }
756
757 if (best_idx >= 0)
758 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Closest match: %g seconds Index: %d", gphoto->exposureList[best_idx],
759 best_idx);
760 else
761 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "No optimal predefined exposure found.");
762
763
764 return best_idx;
765 }
766
767 void gphoto_set_upload_settings(gphoto_driver *gphoto, int setting)
768 {
769 gphoto->upload_settings = setting;
770 }
771
772 // A memory buffer based IO stream, so libtiff can read from memory instead of file
773 struct membuf : std::streambuf
774 {
775 membuf(char *begin, char *end) : begin(begin), end(end)
776 {
777 this->setg(begin, begin, end);
778 }
779
780 virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir,
781 std::ios_base::openmode /*which = std::ios_base::in*/) override
782 {
783 if(dir == std::ios_base::cur)
784 gbump(off);
785 else if(dir == std::ios_base::end)
786 setg(begin, end + off, end);
787 else if(dir == std::ios_base::beg)
788 setg(begin, begin + off, end);
789
790 return gptr() - eback();
791 }
792
793 virtual pos_type seekpos(std::streampos pos, std::ios_base::openmode mode) override
794 {
795 return seekoff(pos - pos_type(off_type(0)), std::ios_base::beg, mode);
796 }
797
798 char *begin, *end;
799 };
800
801 static int download_image(gphoto_driver *gphoto, CameraFilePath *fn, int fd)
802 {
803 int result = 0;
804 CameraFileInfo info;
805
806 if (gphoto->is_aborted)
807 {
808 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Deleting aborted image... Name: (%s) Folder: (%s)", fn->name, fn->folder);
809 }
810 else
811 {
812 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
813 "Downloading image... Name: (%s) Folder: (%s) Delete from SD card? (%s)", fn->name, fn->folder,
814 gphoto->handle_sdcard_image == DELETE_IMAGE ? "true" : "false");
815 }
816
817 strncpy(gphoto->filename, fn->name, sizeof(gphoto->filename));
818
819 if (fd < 0)
820 {
821 result = gp_file_new(&gphoto->camerafile);
822 if (result != GP_OK)
823 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "gp_file_new failed (%s)", gp_result_as_string(result));
824 }
825 else
826 {
827 result = gp_file_new_from_fd(&gphoto->camerafile, fd);
828 if (result != GP_OK)
829 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "gp_file_new_from_fd failed (%s)", gp_result_as_string(result));
830 }
831
832 result = gp_camera_file_get(gphoto->camera, fn->folder, fn->name, GP_FILE_TYPE_NORMAL, gphoto->camerafile,
833 gphoto->context);
834
835 //if (!(gphoto->command & DSLR_CMD_ABORT))
836 // DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Downloading image (%s) in folder (%s)", fn->name, fn->folder);
837
838 if (result != GP_OK)
839 {
840 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Error downloading image from camera: %s", gp_result_as_string(result));
841 gp_file_free(gphoto->camerafile);
842 gphoto->camerafile = nullptr;
843 return result;
844 }
845
846 result = gp_camera_file_get_info(gphoto->camera, fn->folder, fn->name, &info, gphoto->context);
847
848 if (result == GP_OK)
849 {
850 gphoto->width = info.file.width;
851 gphoto->height = info.file.height;
852
853 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, " Downloaded %dx%d (preview %dx%d)", info.file.width,
854 info.file.height, info.preview.width, info.preview.height);
855 }
856 else
857 {
858 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Could not determine image size (%s)", gp_result_as_string(result));
859 }
860
861 #if defined(LIBRAW_CAMERA_TEMPERATURE) && defined(LIBRAW_SENSOR_TEMPERATURE)
862 // Extract temperature(s) from gphoto image via libraw
863 const char *imgData;
864 unsigned long imgSize;
865 result = gp_file_get_data_and_size(gphoto->camerafile, &imgData, &imgSize);
866 if (result == GP_OK)
867 {
868 LibRaw lib_raw;
869 int rc = lib_raw.open_buffer((void *)imgData, imgSize);
870 if (rc != LIBRAW_SUCCESS)
871 {
872 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
873 "Cannot decode (%s)", libraw_strerror(rc));
874 }
875 else
876 {
877 if (lib_raw.imgdata.other.SensorTemperature > -273.15f)
878 gphoto->last_sensor_temp = lib_raw.imgdata.other.SensorTemperature;
879 else if (lib_raw.imgdata.other.CameraTemperature > -273.15f)
880 gphoto->last_sensor_temp = lib_raw.imgdata.other.CameraTemperature;
881 }
882 lib_raw.recycle();
883 if (fd >= 0)
884 {
885 // The gphoto documentation says I don't need to do this,
886 // but reading the source of gp_file_get_data_and_size says otherwise. :(
887 free((void *)imgData);
888 imgData = nullptr;
889 }
890 }
891 #elif defined(LIBRAW_CAMERA_TEMPERATURE2) && defined(LIBRAW_SENSOR_TEMPERATURE2)
892 // Extract temperature(s) from gphoto image via libraw
893 const char *imgData;
894 unsigned long imgSize;
895 result = gp_file_get_data_and_size(gphoto->camerafile, &imgData, &imgSize);
896 if (result == GP_OK)
897 {
898 LibRaw lib_raw;
899 int rc = lib_raw.open_buffer((void *)imgData, imgSize);
900 if (rc != LIBRAW_SUCCESS)
901 {
902 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
903 "Cannot decode (%s)", libraw_strerror(rc));
904 }
905 else
906 {
907 if (lib_raw.imgdata.makernotes.common.SensorTemperature > -273.15f)
908 gphoto->last_sensor_temp = lib_raw.imgdata.makernotes.common.SensorTemperature;
909 else if (lib_raw.imgdata.makernotes.common.CameraTemperature > -273.15f)
910 gphoto->last_sensor_temp = lib_raw.imgdata.makernotes.common.CameraTemperature;
911 }
912 lib_raw.recycle();
913 if (fd >= 0)
914 {
915 // The gphoto documentation says I don't need to do this,
916 // but reading the source of gp_file_get_data_and_size says otherwise. :(
917 free((void *)imgData);
918 imgData = nullptr;
919 }
920 }
921 #endif
922 /*
923 if(strstr(gphoto->manufacturer, "Canon"))
924 {
925 // Try to pull the camera temperature out of the EXIF data
926 const char *imgData;
927 unsigned long imgSize;
928 result = gp_file_get_data_and_size(gphoto->camerafile, &imgData, &imgSize);
929 if (result == GP_OK)
930 {
931 membuf sbuf((char *)imgData, (char *)imgData + imgSize);
932 std::istream is(&sbuf);
933 auto tiff = TIFFStreamOpen(fn->name, &is);
934 if (tiff)
935 {
936 toff_t exifoffset;
937 if (TIFFGetField (tiff, TIFFTAG_EXIFIFD, &exifoffset) && TIFFReadEXIFDirectory (tiff, exifoffset))
938 {
939 uint32_t count;
940 uint8_t* data;
941 int ret = TIFFGetField(tiff, EXIFTAG_MAKERNOTE, &count, &data);
942 if(ret != 0)
943 {
944 // Got the MakerNote EXIF data, now parse it out. It's been reverse-engineered and documented at
945 // https://sno.phy.queensu.ca/~phil/exiftool/TagNames/Canon.html
946 struct IFDEntry
947 {
948 uint16_t tag;
949 uint16_t type;
950 uint32_t count;
951 uint32_t offset;
952 };
953
954 // The TIFF library took care of handling byte-ordering for us until now. But now that we're parsing
955 // binary data directly, we need to remember to swap bytes when necessary.
956 uint16_t numEntries = *(uint16_t*)data;
957 if (TIFFIsByteSwapped(tiff)) TIFFSwabShort(&numEntries);
958 IFDEntry* entries = (IFDEntry*) (data + sizeof(uint16_t));
959 for (int i = 0; i < numEntries; i++)
960 {
961 IFDEntry* entry = &entries[i];
962 uint16_t tag = entry->tag;
963 if (TIFFIsByteSwapped(tiff)) TIFFSwabShort(&tag);
964 if (tag == 4)
965 {
966 // Found the ShotInfo tag. Extract the CameraTemperature field
967 uint32_t offset = entry->offset;
968 if (TIFFIsByteSwapped(tiff)) TIFFSwabLong(&offset);
969 uint16_t* shotInfo = (uint16_t*)(imgData + offset);
970 uint16_t temperature = shotInfo[12];
971 if (TIFFIsByteSwapped(tiff)) TIFFSwabShort(&temperature);
972
973 // The temperature is offset by 0x80, so correct that
974 gphoto->last_sensor_temp = (int)(temperature - 0x80);
975
976 break;
977 }
978 }
979 }
980 }
981 TIFFClose(tiff);
982 }
983 if (fd >= 0)
984 {
985 // The gphoto documentation says I don't need to do this,
986 // but reading the source of gp_file_get_data_and_size says otherwise. :(
987 free((void *)imgData);
988 imgData = nullptr;
989 }
990 }
991 }
992 */
993 // For some reason Canon 20D fails when deleting here
994 // so this hack is a workaround until a permement fix is found
995 // JM 2017-05-17
996 //if (gphoto->upload_settings == GP_UPLOAD_CLIENT && !strstr(gphoto->model, "20D"))
997 int captureTarget = -1;
998 gphoto_get_capture_target(gphoto, &captureTarget);
999 // If it was set to RAM or SD card image is set to be explicitly deleted
1000 if ( (gphoto->is_aborted || gphoto->handle_sdcard_image == DELETE_IMAGE || captureTarget == 0)
1001 && !strstr(gphoto->model, "20D"))
1002 {
1003 // 2018-04-16 JM: Delete all the folder to make sure there are no ghost images left somehow
1004 //result = gp_camera_folder_delete_all(gphoto->camera, fn->folder, gphoto->context);
1005
1006 // Delete individual file
1007 result = gp_camera_file_delete(gphoto->camera, fn->folder, fn->name, gphoto->context);
1008
1009 if (result != GP_OK)
1010 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Failed to delete file %s (%s)", fn->name, gp_result_as_string(result));
1011 }
1012
1013 if (fd >= 0)
1014 {
1015 // This will close the file descriptor
1016 result = gp_file_free(gphoto->camerafile);
1017 if (result != GP_OK)
1018 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Closing camera file descriptor failed (%s)", gp_result_as_string(result));
1019 gphoto->camerafile = nullptr;
1020 }
1021
1022 return GP_OK;
1023 }
1024
1025 bool gphoto_supports_temperature(gphoto_driver *gphoto)
1026 {
1027 return gphoto->supports_temperature;
1028 }
1029
1030 float gphoto_get_last_sensor_temperature(gphoto_driver *gphoto)
1031 {
1032 return gphoto->last_sensor_temp;
1033 }
1034
1035 int gphoto_mirrorlock(gphoto_driver *gphoto, int msec)
1036 {
1037 if (gphoto->bulb_widget && !strcmp(gphoto->bulb_widget->name, "eosremoterelease"))
1038 {
1039 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
1040 "eosremoterelease Mirror Lock for %g secs", msec / 1000.0);
1041
1042 // 2018-07-19: Disabling customfuncex since it seems to be causing problems for some
1043 // Canon model as reported in INDI forums. Follow up in PR #620
1044 #if 0
1045 gphoto_set_widget_text(gphoto, gphoto->customfuncex_widget,
1046 EOS_MIRROR_LOCKUP_ENABLE);
1047 #endif
1048
1049 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, EOS_PRESS_FULL);
1050 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, EOS_RELEASE_FULL);
1051
1052 usleep(msec * 1000);
1053
1054 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "End of mirror lock timer");
1055
1056 return 0;
1057 }
1058
1059 if (gphoto->bulb_port[0])
1060 {
1061 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Locking mirror by opening remote serial shutter port: %s ...",
1062 gphoto->bulb_port);
1063 gphoto->bulb_fd = open(gphoto->bulb_port, O_RDWR | O_NONBLOCK);
1064 if (gphoto->bulb_fd < 0)
1065 {
1066 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Failed to open serial port: %s", gphoto->bulb_port);
1067 //pthread_mutex_unlock(&gphoto->mutex);
1068 return -1;
1069 }
1070
1071 ioctl(gphoto->bulb_fd, TIOCMBIS, &RTS_flag);
1072
1073 //usleep(20000);
1074 // JM 2019-11-18: Changed to 150ms as per the discussion here
1075 // https://indilib.org/forum/ccds-dslrs/1564-mirror-lock-on-canon-with-serial-shutter.html
1076 usleep(150000);
1077 ioctl(gphoto->bulb_fd, TIOCMBIC, &RTS_flag);
1078 close(gphoto->bulb_fd);
1079 gphoto->bulb_fd = -1;
1080 //usleep(msec * 1000 - 20000);
1081 usleep(msec * 1000);
1082 return 0;
1083 }
1084
1085 // Otherwise fail gracefully
1086 DEBUGDEVICE(device, INDI::Logger::DBG_ERROR, "Mirror lock feature is not yet implemented for this camera model.");
1087 return -1;
1088 }
1089
1090 int gphoto_start_exposure(gphoto_driver *gphoto, uint32_t exptime_usec, int mirror_lock)
1091 {
1092 if (gphoto->exposure_widget == nullptr)
1093 {
1094 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "No exposure widget found. Can not expose!");
1095 return -1;
1096 }
1097
1098 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
1099 "Starting exposure (exptime: %g secs, mirror lock: %d, force bulb: %s, exposure index: %d)",
1100 exptime_usec / 1e6, mirror_lock, gphoto->force_bulb ? "true" : "false",
1101 gphoto->exposure_widget ? gphoto->exposure_widget->value.num : -1);
1102 pthread_mutex_lock(&gphoto->mutex);
1103 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Mutex locked");
1104
1105 // Set ISO Settings
1106 if (gphoto->iso >= 0)
1107 gphoto_set_widget_num(gphoto, gphoto->iso_widget, gphoto->iso);
1108
1109 // Set Format Settings
1110 if (gphoto->format >= 0)
1111 gphoto_set_widget_num(gphoto, gphoto->format_widget, gphoto->format);
1112
1113 // Find EXACT optimal exposure index in case we need to use it. If -1, we always use blob made if available
1114 int optimalExposureIndex = -1;
1115
1116 // JM 2018-09-23: In case force bulb is off, then we search for optimal exposure index
1117 if (gphoto->force_bulb == false &&
1118 // No external shutter port is specified OR
1119 ((!gphoto->bulb_port[0] && !gphoto->dsusb) ||
1120 // External shutter port is specified but exposure time < 30 secs
1121 ((gphoto->bulb_port[0] || gphoto->dsusb) && exptime_usec <= RELEASE_SHUTTER_THRESHOLD)))
1122 {
1123 optimalExposureIndex = find_exposure_setting(gphoto, gphoto->exposure_widget, exptime_usec, true);
1124 }
1125
1126 // Set Capture Target
1127 // JM: 2017-05-21: Disabled now since user can explicity set capture target via the driver interface
1128 #if 0
1129 if (gphoto->capturetarget_widget)
1130 {
1131 // Use RAM but NOT on Nikon as it can only work when saving to SD Card
1132 if (gphoto->upload_settings == GP_UPLOAD_CLIENT && (strstr(gphoto->manufacturer, "Nikon") == nullptr) )
1133 gphoto_set_widget_num(gphoto, gphoto->capturetarget_widget, 0);
1134 else
1135 // Store in SD Card in Camera
1136 gphoto_set_widget_num(gphoto, gphoto->capturetarget_widget, 1);
1137 }
1138 #endif
1139
1140 // ###########################################################################################
1141 // ################################### BULB Pathway ##########################################
1142 // ###########################################################################################
1143 // We take this route if any of the following conditions are met:
1144 // 1. Predefined Exposure List does not exist for the camera.
1145 // 2. An external shutter port or DSUSB is active (for > 1 second exposures or there is no optimal exposure)
1146 //// 3. Exposure Time > 1 second or there is no optimal exposure and we have a bulb widget to trigger the custom time
1147 // 3. There is no optimal exposure and we have a bulb widget to trigger the custom time
1148 if (gphoto->exposureList == nullptr ||
1149 ( (gphoto->bulb_port[0] || gphoto->dsusb) && optimalExposureIndex == -1) ||
1150 //(gphoto->bulb_widget != nullptr && (exptime_usec > 1e6 || optimalExposureIndex == -1)))
1151 (gphoto->bulb_widget != nullptr && optimalExposureIndex == -1))
1152 {
1153 // Check if we are in BULB or MANUAL or other mode. Always set it to BULB if needed
1154 if (gphoto->bulb_widget && gphoto->autoexposuremode_widget)
1155 {
1156 // If it settings is not either MANUAL or BULB then warn the user.
1157 if (gphoto->autoexposuremode_widget->value.index < 3 || gphoto->autoexposuremode_widget->value.index > 4)
1158 {
1159 DEBUGFDEVICE(device, INDI::Logger::DBG_WARNING,
1160 "Camera auto exposure mode is not set to either BULB or MANUAL modes (%s). Please set mode to BULB "
1161 "for long exposures.",
1162 gphoto->autoexposuremode_widget->choices[static_cast<uint8_t>(gphoto->autoexposuremode_widget->value.index)]);
1163 }
1164 }
1165
1166 // We set bulb setting for exposure widget if it is defined by the camera
1167 //if (gphoto->exposureList && gphoto->exposure_widget->type != GP_WIDGET_TEXT && gphoto->bulb_exposure_index != -1)
1168 if (!gphoto->bulb_mode && gphoto->bulb_exposure_index != -1)
1169 {
1170 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting exposure widget bulb index: %d", gphoto->bulb_exposure_index);
1171 if (gphoto_set_widget_num(gphoto, gphoto->exposure_widget, gphoto->bulb_exposure_index) == GP_OK)
1172 gphoto->bulb_mode = true;
1173 // If it's not already set to the bulb exposure index
1174 // if (gphoto->bulb_exposure_index != static_cast<uint8_t>(gphoto->exposure_widget->value.index))
1175 // {
1176 // DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Setting exposure widget bulb index: %d",
1177 // gphoto->bulb_exposure_index);
1178 // gphoto_set_widget_num(gphoto, gphoto->exposure_widget, gphoto->bulb_exposure_index);
1179 // }
1180 }
1181
1182 // If we have mirror lock enabled, let's lock mirror. Return on failure
1183 if (mirror_lock)
1184 {
1185 if (gphoto->dsusb)
1186 {
1187 DEBUGDEVICE(device, INDI::Logger::DBG_ERROR, "Using mirror lock with DSUSB is unsupported!");
1188 return -1;
1189 }
1190
1191 if (gphoto_mirrorlock(gphoto, mirror_lock * 1000))
1192 return -1;
1193 }
1194 // Disabled since it causes issues
1195 else if (gphoto->bulb_widget && !strcmp(gphoto->bulb_widget->name, "eosremoterelease"))
1196 {
1197 #if 0
1198 gphoto_set_widget_text(gphoto, gphoto->customfuncex_widget,
1199 EOS_MIRROR_LOCKUP_DISABLE);
1200 #endif
1201 }
1202
1203 // If DSUSB port is specified, let's open it
1204 if (gphoto->dsusb)
1205 {
1206 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Using DSUSB to open shutter...");
1207 gphoto->dsusb->openShutter();
1208 }
1209 // Otherwise open regular remote serial shutter port
1210 else if (gphoto->bulb_port[0])
1211 {
1212 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Opening remote serial shutter port: %s ...", gphoto->bulb_port);
1213 gphoto->bulb_fd = open(gphoto->bulb_port, O_RDWR | O_NONBLOCK);
1214 if (gphoto->bulb_fd < 0)
1215 {
1216 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Failed to open serial port: %s", gphoto->bulb_port);
1217 pthread_mutex_unlock(&gphoto->mutex);
1218 return -1;
1219 }
1220
1221 // Open Nikon Shutter
1222 if (!strstr(device, "Nikon"))
1223 {
1224 uint8_t open_shutter[3] = {0xFF, 0x01, 0x01};
1225 if (write(gphoto->bulb_fd, open_shutter, 3) != 3)
1226 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "Opening Nikon remote serial shutter failed.");
1227 }
1228
1229 ioctl(gphoto->bulb_fd, TIOCMBIS, &RTS_flag);
1230 }
1231 // Otherwise, let's fallback to the internal bulb widget
1232 else if (gphoto->bulb_widget)
1233 {
1234 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Using internal bulb widget:%s", gphoto->bulb_widget->name);
1235
1236 // Check if the internal bulb widget is eosremoterelease, and if yes, set it accordingly
1237 if (strcmp(gphoto->bulb_widget->name, "eosremoterelease") == 0)
1238 {
1239 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, EOS_PRESS_FULL); //600D eosremoterelease PRESS FULL
1240 }
1241 // Otherwise use the regular bulb widget
1242 else
1243 {
1244 gphoto_set_widget_num(gphoto, gphoto->bulb_widget, TRUE);
1245 }
1246 }
1247 // else we fail
1248 else
1249 {
1250 DEBUGDEVICE(device, INDI::Logger::DBG_ERROR, "No external or internal bulb widgets found. Cannot capture.");
1251 pthread_mutex_unlock(&gphoto->mutex);
1252 return -1;
1253 }
1254
1255 // Preparing exposure
1256 struct timeval duration, current_time;
1257 gettimeofday(&current_time, nullptr);
1258 duration.tv_sec = exptime_usec / 1000000;
1259 duration.tv_usec = exptime_usec % 1000000;
1260 timeradd(&current_time, &duration, &gphoto->bulb_end);
1261
1262 // Start actual exposure
1263 gphoto->command = DSLR_CMD_BULB_CAPTURE;
1264 pthread_cond_signal(&gphoto->signal);
1265 pthread_mutex_unlock(&gphoto->mutex);
1266 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Exposure started.");
1267 return 0;
1268 }
1269
1270 // ###########################################################################################
1271 // ############################ Predefined Exposure Pathway ##################################
1272 // ###########################################################################################
1273
1274 // If we're using fallback exposure_widget, then it's TEXT, not RADIO
1275 // libgphoto2 does not provide radio list for some cameras
1276 // See https://github.com/gphoto/libgphoto2/issues/276
1277 if (optimalExposureIndex == -1)
1278 optimalExposureIndex = find_exposure_setting(gphoto, gphoto->exposure_widget, exptime_usec, false);
1279
1280 if (optimalExposureIndex == -1)
1281 {
1282 DEBUGDEVICE(device, INDI::Logger::DBG_ERROR, "Failed to set non-bulb exposure time.");
1283 pthread_mutex_unlock(&gphoto->mutex);
1284 return -1;
1285 }
1286 else if (gphoto->exposure_widget && gphoto->exposure_widget->type == GP_WIDGET_TEXT)
1287 {
1288 gphoto_set_widget_text(gphoto, gphoto->exposure_widget, fallbackShutterSpeeds[optimalExposureIndex]);
1289 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Using predefined exposure time: %s seconds",
1290 fallbackShutterSpeeds[optimalExposureIndex]);
1291 }
1292 else if (gphoto->exposure_widget)
1293 {
1294 gphoto_set_widget_num(gphoto, gphoto->exposure_widget, optimalExposureIndex);
1295 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Using predefined exposure time: %g seconds",
1296 gphoto->exposureList[optimalExposureIndex]);
1297 }
1298
1299 // Lock the mirror if required.
1300 if (mirror_lock && gphoto_mirrorlock(gphoto, mirror_lock * 1000))
1301 {
1302 pthread_mutex_unlock(&gphoto->mutex);
1303 return -1;
1304 }
1305
1306 gphoto->bulb_mode = false;
1307
1308 // // If bulb port is specified, a serial shutter control is required to start the exposure. Treat this as a bulb exposure.
1309 // if (gphoto->bulb_port[0] && !gphoto->dsusb)
1310 // {
1311 // DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Opening remote serial shutter port: %s ...", gphoto->bulb_port);
1312 // gphoto->bulb_fd = open(gphoto->bulb_port, O_RDWR, O_NONBLOCK);
1313 // if (gphoto->bulb_fd < 0)
1314 // {
1315 // DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Failed to open serial port: %s", gphoto->bulb_port);
1316 // pthread_mutex_unlock(&gphoto->mutex);
1317 // return -1;
1318 // }
1319
1320 // ioctl(gphoto->bulb_fd, TIOCMBIS, &RTS_flag);
1321
1322 // // Preparing exposure: we let stop_bulb() close the serial port although this could be done here as well
1323 // // because the camera closes the shutter.
1324
1325 // struct timeval duration, current_time;
1326 // gettimeofday(&current_time, nullptr);
1327 // duration.tv_sec = exptime_usec / 1000000;
1328 // duration.tv_usec= exptime_usec % 1000000;
1329 // timeradd(&current_time, &duration, &gphoto->bulb_end);
1330
1331 // // Start actual exposure
1332 // gphoto->command = DSLR_CMD_BULB_CAPTURE;
1333 // pthread_cond_signal(&gphoto->signal);
1334 // pthread_mutex_unlock(&gphoto->mutex);
1335 // DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Exposure started");
1336 // return 0;
1337 // }
1338 gphoto->command = DSLR_CMD_CAPTURE;
1339 pthread_cond_signal(&gphoto->signal);
1340 pthread_mutex_unlock(&gphoto->mutex);
1341 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Exposure started.");
1342 return 0;
1343 }
1344
1345 int gphoto_read_exposure_fd(gphoto_driver *gphoto, int fd)
1346 {
1347 CameraFilePath *fn;
1348 CameraEventType event;
1349 void *data = nullptr;
1350 int result;
1351
1352 // Wait for exposure to complete
1353 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Reading exposure...");
1354 pthread_mutex_lock(&gphoto->mutex);
1355 if (gphoto->camerafile)
1356 {
1357 gp_file_free(gphoto->camerafile);
1358 gphoto->camerafile = nullptr;
1359 }
1360 if (!(gphoto->command & DSLR_CMD_DONE))
1361 pthread_cond_wait(&gphoto->signal, &gphoto->mutex);
1362
1363 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Exposure complete.");
1364
1365 if (gphoto->command & DSLR_CMD_CAPTURE)
1366 {
1367 if (gphoto->handle_sdcard_image == IGNORE_IMAGE)
1368 {
1369 gphoto->command = 0;
1370 pthread_mutex_unlock(&gphoto->mutex);
1371 return GP_OK;
1372 }
1373
1374 result = download_image(gphoto, &gphoto->camerapath, fd);
1375 gphoto->command = 0;
1376 //Set exposure back to original value
1377 // JM 2018-08-06: Why do we really need to reset values here?
1378 //reset_settings(gphoto);
1379 pthread_mutex_unlock(&gphoto->mutex);
1380 return result;
1381 }
1382
1383 //Bulb mode
1384 int timeoutCounter = 0;
1385 gphoto->command = 0;
1386 uint32_t waitMS = 1000;
1387 bool downloadComplete = false;
1388
1389 while (1)
1390 {
1391 // Wait for image to be ready to download
1392 result = gp_camera_wait_for_event(gphoto->camera, waitMS, &event, &data, gphoto->context);
1393
1394 if (result != GP_OK)
1395 {
1396 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "Could not wait for event.");
1397 timeoutCounter++;
1398 if (timeoutCounter >= 10)
1399 {
1400 pthread_mutex_unlock(&gphoto->mutex);
1401 return -1;
1402 }
1403
1404 continue;
1405 }
1406
1407 switch (event)
1408 {
1409 case GP_EVENT_CAPTURE_COMPLETE:
1410 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Capture event completed.");
1411 pthread_mutex_unlock(&gphoto->mutex);
1412 return GP_OK;
1413
1414 case GP_EVENT_FILE_ADDED:
1415 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "File added event completed.");
1416 fn = static_cast<CameraFilePath *>(data);
1417 if (gphoto->handle_sdcard_image != IGNORE_IMAGE)
1418 download_image(gphoto, fn, fd);
1419 waitMS = 100;
1420 downloadComplete = true;
1421 break;
1422 case GP_EVENT_UNKNOWN:
1423 //DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Unknown event.");
1424 break;
1425 case GP_EVENT_TIMEOUT:
1426 if (downloadComplete)
1427 {
1428 pthread_mutex_unlock(&gphoto->mutex);
1429 return GP_OK;
1430 }
1431 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Event timed out #%d, retrying...", ++timeoutCounter);
1432 // So retry for 5 seconds before giving up
1433 if (timeoutCounter >= 10)
1434 {
1435 pthread_mutex_unlock(&gphoto->mutex);
1436 return -1;
1437 }
1438 break;
1439
1440 default:
1441 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Got unexpected message: %d", event);
1442 }
1443 }
1444 return GP_OK;
1445 }
1446
1447 int gphoto_abort_exposure(gphoto_driver *gphoto)
1448 {
1449 gphoto->command = DSLR_CMD_ABORT;
1450 pthread_cond_signal(&gphoto->signal);
1451 pthread_mutex_unlock(&gphoto->mutex);
1452
1453 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Aborting exposure...");
1454
1455 // Wait until exposure is aborted
1456 pthread_mutex_lock(&gphoto->mutex);
1457 if (!(gphoto->command & DSLR_CMD_DONE))
1458 pthread_cond_wait(&gphoto->signal, &gphoto->mutex);
1459
1460 pthread_mutex_unlock(&gphoto->mutex);
1461
1462 gphoto_read_exposure(gphoto);
1463
1464 return GP_OK;
1465 }
1466
1467 int gphoto_read_exposure(gphoto_driver *gphoto)
1468 {
1469 return gphoto_read_exposure_fd(gphoto, -1);
1470 }
1471
1472 char **gphoto_get_formats(gphoto_driver *gphoto, int *cnt)
1473 {
1474 if (!gphoto->format_widget)
1475 {
1476 if (cnt)
1477 *cnt = 0;
1478 return nullptr;
1479 }
1480 if (cnt)
1481 *cnt = gphoto->format_widget->choice_cnt;
1482 return gphoto->format_widget->choices;
1483 }
1484
1485 char **gphoto_get_iso(gphoto_driver *gphoto, int *cnt)
1486 {
1487 if (!gphoto->iso_widget)
1488 {
1489 if (cnt)
1490 *cnt = 0;
1491 return nullptr;
1492 }
1493 if (cnt)
1494 *cnt = gphoto->iso_widget->choice_cnt;
1495 return gphoto->iso_widget->choices;
1496 }
1497
1498 void gphoto_set_iso(gphoto_driver *gphoto, int iso)
1499 {
1500 if (gphoto->iso_widget)
1501 gphoto->iso = iso;
1502 else
1503 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "WARNING: Could not set iso");
1504 }
1505
1506 char **gphoto_get_exposure_presets(gphoto_driver *gphoto, int *cnt)
1507 {
1508 if (!gphoto->exposure_presets)
1509 {
1510 if (cnt)
1511 *cnt = 0;
1512
1513 return nullptr;
1514 }
1515
1516 if (cnt)
1517 *cnt = gphoto->exposure_presets_count;
1518
1519 return gphoto->exposure_presets;
1520 }
1521
1522 void gphoto_get_minmax_exposure(gphoto_driver *gphoto, double *min, double *max)
1523 {
1524 *min = gphoto->min_exposure;
1525 *max = gphoto->max_exposure;
1526 }
1527
1528 void gphoto_set_format(gphoto_driver *gphoto, int format)
1529 {
1530 if (gphoto->format_widget)
1531 gphoto->format = format;
1532 else
1533 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "WARNING: Could not set format");
1534 }
1535
1536 int gphoto_get_format_current(gphoto_driver *gphoto)
1537 {
1538 if (!gphoto->format_widget)
1539 return 0;
1540 return gphoto->format_widget->value.index;
1541 }
1542
1543 int gphoto_get_iso_current(gphoto_driver *gphoto)
1544 {
1545 if (!gphoto->iso_widget)
1546 return 0;
1547 return gphoto->iso_widget->value.index;
1548 }
1549
1550 gphoto_driver *gphoto_open(Camera *camera, GPContext *context, const char *model, const char *port,
1551 const char *shutter_release_port)
1552 {
1553 gphoto_driver *gphoto;
1554 gphoto_widget *widget;
1555 CameraAbilities a;
1556 GPPortInfo pi;
1557 int result = 0, index = 0;
1558
1559 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "libgphoto2 info:");
1560 const char **libgphotoVerionInfo = gp_library_version(GP_VERSION_SHORT);
1561 for (const char **verionInfo = libgphotoVerionInfo; *verionInfo; verionInfo++)
1562 {
1563 const char *pInfo = *verionInfo;
1564 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%s", pInfo);
1565 }
1566
1567 //gp_log_add_func(GP_LOG_ERROR, errordumper, nullptr);
1568 gp_camera_new(&camera);
1569
1570 if (model == nullptr || port == nullptr)
1571 {
1572 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Camera init. Takes about 10 seconds.");
1573 result = gp_camera_init(camera, context);
1574 if (result != GP_OK)
1575 {
1576 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Camera open error (%d): %s", result,
1577 gp_result_as_string(result));
1578 return nullptr;
1579 }
1580 }
1581 else
1582 {
1583 if (!abilities)
1584 {
1585 /* Load all the camera drivers we have... */
1586 result = gp_abilities_list_new(&abilities);
1587 if (result < GP_OK)
1588 {
1589 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_abilities_list_new failed (%d): %s", result,
1590 gp_result_as_string(result));
1591 return nullptr;
1592 }
1593 result = gp_abilities_list_load(abilities, context);
1594 if (result < GP_OK)
1595 {
1596 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_abilities_list_load failed (%d): %s", result,
1597 gp_result_as_string(result));
1598 return nullptr;
1599 }
1600 }
1601
1602 /* First lookup the model / driver */
1603 index = gp_abilities_list_lookup_model(abilities, model);
1604 if (index < GP_OK)
1605 {
1606 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_abilities_list_lookup_model failed (%d): %s", index,
1607 gp_result_as_string(index));
1608 return nullptr;
1609 }
1610
1611 result = gp_abilities_list_get_abilities(abilities, index, &a);
1612 if (result < GP_OK)
1613 {
1614 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_abilities_list_get_abilities (%d): %s", result,
1615 gp_result_as_string(result));
1616 return nullptr;
1617 }
1618
1619 result = gp_camera_set_abilities(camera, a);
1620 if (result < GP_OK)
1621 {
1622 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_abilities_list_get_abilities (%d): %s", result,
1623 gp_result_as_string(result));
1624 return nullptr;
1625 }
1626
1627 if (!portinfolist)
1628 {
1629 /* Load all the port drivers we have... */
1630 result = gp_port_info_list_new(&portinfolist);
1631 if (result < GP_OK)
1632 {
1633 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_port_info_list_new (%d): %s", result,
1634 gp_result_as_string(result));
1635 return nullptr;
1636 }
1637 result = gp_port_info_list_load(portinfolist);
1638 if (result < 0)
1639 {
1640 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_port_info_list_load (%d): %s", result,
1641 gp_result_as_string(result));
1642 return nullptr;
1643 }
1644 result = gp_port_info_list_count(portinfolist);
1645 if (result < 0)
1646 {
1647 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_port_info_list_count (%d): %s", result,
1648 gp_result_as_string(result));
1649 return nullptr;
1650 }
1651 }
1652
1653 /* Then associate the camera with the specified port */
1654 index = gp_port_info_list_lookup_path(portinfolist, port);
1655 switch (index)
1656 {
1657 case GP_ERROR_UNKNOWN_PORT:
1658 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR,
1659 "The port you specified "
1660 "('%s') can not be found. Please "
1661 "specify one of the ports found by "
1662 "'gphoto2 --list-ports' and make "
1663 "sure the spelling is correct "
1664 "(i.e. with prefix 'serial:' or 'usb:').",
1665 port);
1666 break;
1667 default:
1668 break;
1669 }
1670
1671 if (index < GP_OK)
1672 {
1673 return nullptr;
1674 }
1675
1676 result = gp_port_info_list_get_info(portinfolist, index, &pi);
1677 if (result < GP_OK)
1678 {
1679 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_port_info_list_get_info (%d): %s", result,
1680 gp_result_as_string(result));
1681 return nullptr;
1682 }
1683
1684 result = gp_camera_set_port_info(camera, pi);
1685 if (result < GP_OK)
1686 {
1687 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "gp_port_info_list_get_info (%d): %s", result,
1688 gp_result_as_string(result));
1689 return nullptr;
1690 }
1691 }
1692
1693 gphoto = (gphoto_driver *)calloc(sizeof(gphoto_driver), 1);
1694 gphoto->camera = camera;
1695 gphoto->context = context;
1696 gphoto->exposure_presets = nullptr;
1697 gphoto->exposure_presets_count = 0;
1698 gphoto->max_exposure = 3600;
1699 gphoto->min_exposure = 0.001;
1700 gphoto->dsusb = nullptr;
1701 gphoto->force_bulb = true;
1702 gphoto->last_sensor_temp = -273.0; // 0 degrees Kelvin
1703
1704 result = gp_camera_get_config(gphoto->camera, &gphoto->config, gphoto->context);
1705 if (result < GP_OK)
1706 {
1707 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Camera_get_config failed (%d): %s", result,
1708 gp_result_as_string(result));
1709 free(gphoto);
1710 return nullptr;
1711 }
1712
1713 // Set 'capture=1' for Canon DSLRs. Won't harm other cameras
1714 if ((widget = find_widget(gphoto, "capture")))
1715 {
1716 gphoto_set_widget_num(gphoto, widget, TRUE);
1717 widget_free(widget);
1718 }
1719
1720 // Look for an exposure widget
1721 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Finding exposure widget...");
1722
1723 if ((gphoto->exposure_widget = find_widget(gphoto, "shutterspeed2")) ||
1724 (gphoto->exposure_widget = find_widget(gphoto, "shutterspeed")) ||
1725 (gphoto->exposure_widget = find_widget(gphoto, "eos-shutterspeed")))
1726 {
1727 gphoto->exposureList = parse_shutterspeed(gphoto, gphoto->exposure_widget);
1728 }
1729 else if ((gphoto->exposure_widget = find_widget(gphoto, "capturetarget")))
1730 {
1731 gphoto_widget tempWidget;
1732 const char *choices[2] = { "1/1", "bulb" };
1733 tempWidget.choice_cnt = 2;
1734 tempWidget.choices = const_cast<char **>(choices);
1735 gphoto->exposureList = parse_shutterspeed(gphoto, &tempWidget);
1736 }
1737 else
1738 {
1739 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING,
1740 "Warning: Didn't find an exposure widget! Are you sure the camera is set to Bulb mode?");
1741 }
1742
1743 if (gphoto->exposure_widget != nullptr)
1744 {
1745 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Exposure Widget: %s", gphoto->exposure_widget->name);
1746 int ret = gphoto_read_widget(gphoto->exposure_widget);
1747 if (ret == GP_OK)
1748 show_widget(gphoto->exposure_widget, "\t\t");
1749 }
1750
1751 gphoto->format_widget = find_widget(gphoto, "imageformat");
1752 // JM 2018-05-03: Nikon defines it as 'imagequality'
1753 if (gphoto->format_widget == nullptr)
1754 gphoto->format_widget = find_widget(gphoto, "imagequality");
1755 gphoto->format = -1;
1756 gphoto->manufacturer = nullptr;
1757 gphoto->model = nullptr;
1758 gphoto->upload_settings = GP_UPLOAD_CLIENT;
1759 gphoto->handle_sdcard_image = SAVE_IMAGE;
1760 gphoto->is_aborted = false;
1761
1762 if (gphoto->format_widget != nullptr)
1763 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Image Format Widget: %s", gphoto->format_widget->name);
1764
1765 gphoto->iso_widget = find_widget(gphoto, "iso");
1766 if (gphoto->iso_widget == nullptr)
1767 {
1768 gphoto->iso_widget = find_widget(gphoto, "eos-iso");
1769 }
1770 gphoto->iso = -1;
1771
1772 if (gphoto->iso_widget != nullptr)
1773 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "ISO Widget: %s", gphoto->iso_widget->name);
1774
1775 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Finding bulb widget...");
1776
1777 // Look for eosremoterelease widget first
1778 if ((gphoto->bulb_widget = find_widget(gphoto, "eosremoterelease")) == nullptr)
1779 {
1780 // Find Bulb widget if eosremoterelease is not found
1781 gphoto->bulb_widget = find_widget(gphoto, "bulb");
1782 }
1783
1784 if (gphoto->bulb_widget)
1785 {
1786 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Bulb Widget: %s", gphoto->bulb_widget->name);
1787 int ret = gphoto_read_widget(gphoto->bulb_widget);
1788 if (ret == GP_OK)
1789 show_widget(gphoto->bulb_widget, "\t\t");
1790 }
1791 else
1792 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "No bulb widget found.");
1793
1794 // Check for autoexposuremode widget for some cameras
1795 if ((gphoto->autoexposuremode_widget = find_widget(gphoto, "autoexposuremode")) != nullptr)
1796 {
1797 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Autoexposure Widget: %s", gphoto->autoexposuremode_widget->name);
1798 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Current Auto Exposure Mode: %s",
1799 gphoto->autoexposuremode_widget->choices[(uint8_t)gphoto->autoexposuremode_widget->value.index]);
1800 }
1801
1802 // Check for capture target widget, used to set where image is stored (RAM vs SD Card)
1803 if ((gphoto->capturetarget_widget = find_widget(gphoto, "capturetarget")) != nullptr)
1804 {
1805 if (gphoto->capturetarget_widget == gphoto->exposure_widget)
1806 gphoto->capturetarget_widget = nullptr;
1807 else
1808 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Capture Target Widget: %s",
1809 gphoto->capturetarget_widget->name);
1810 }
1811
1812 // Check viewfinder widget to force mirror down after live preview if needed
1813 if ((gphoto->viewfinder_widget = find_widget(gphoto, "viewfinder")) != nullptr)
1814 {
1815 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "ViewFinder Widget: %s", gphoto->viewfinder_widget->name);
1816 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Current ViewFinder Value: %s",
1817 (gphoto->viewfinder_widget->value.toggle == 0) ? "Off" : "On");
1818 }
1819
1820 if ((gphoto->focus_widget = find_widget(gphoto, "manualfocusdrive")) != nullptr ||
1821 (gphoto->focus_widget = find_widget(gphoto, "manualfocus")) != nullptr)
1822 {
1823 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Focus Widget: %s", gphoto->focus_widget->name);
1824 }
1825
1826 // Check customfuncex widget to enable/disable mirror lockup.
1827 if ((gphoto->customfuncex_widget = find_widget(gphoto, EOS_CUSTOMFUNCEX)) != nullptr)
1828 {
1829 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "CustomFuncex Widget: %s",
1830 gphoto->customfuncex_widget->name);
1831 }
1832
1833 // Find Manufacturer
1834 if ((widget = find_widget(gphoto, "manufacturer")) != nullptr)
1835 {
1836 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Manufacturer: %s", widget->value.text);
1837 gphoto->manufacturer = widget->value.text;
1838 }
1839
1840 // Find Model
1841 if ((widget = find_widget(gphoto, "cameramodel")) != nullptr || (widget = find_widget(gphoto, "model")) != nullptr)
1842 {
1843 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Model: %s", widget->value.text);
1844 gphoto->model = widget->value.text;
1845 }
1846 // Make sure manufacturer is set to something useful
1847 if (gphoto->manufacturer == nullptr)
1848 gphoto->manufacturer = gphoto->model;
1849
1850 if (strstr(gphoto->manufacturer, "Canon"))
1851 gphoto->supports_temperature = true;
1852
1853 // Check for user
1854 if (shutter_release_port)
1855 {
1856 strncpy(gphoto->bulb_port, shutter_release_port, sizeof(gphoto->bulb_port));
1857 /*if (strcmp(gphoto->bulb_port,"/dev/nullptr") != 0 )
1858 {
1859 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,"Using external shutter-release cable");
1860 } else {
1861 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,"Using main usb cable for mirror locking");
1862 }*/
1863
1864 if (!strcmp(gphoto->bulb_port, "DSUSB"))
1865 {
1866 gphoto->dsusb = new DSUSBDriver(device);
1867
1868 if (gphoto->dsusb->isConnected() == false)
1869 {
1870 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "Failed to detect DSUSB!");
1871 delete (gphoto->dsusb);
1872 gphoto->dsusb = nullptr;
1873 }
1874 else
1875 {
1876 DEBUGDEVICE(device, INDI::Logger::DBG_SESSION, "Connected to DSUSB");
1877 }
1878 }
1879 else
1880 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Using external shutter release port: %s", gphoto->bulb_port);
1881 }
1882
1883 gphoto->bulb_fd = -1;
1884
1885 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "GPhoto initialized.");
1886
1887 pthread_mutex_init(&gphoto->mutex, nullptr);
1888 pthread_cond_init(&gphoto->signal, nullptr);
1889
1890 pthread_mutex_lock(&gphoto->mutex);
1891 pthread_create(&gphoto->thread, nullptr, stop_bulb, gphoto);
1892 pthread_cond_wait(&gphoto->signal, &gphoto->mutex);
1893
1894 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Blub-stop thread enabled.");
1895
1896 pthread_mutex_unlock(&gphoto->mutex);
1897
1898 return gphoto;
1899 }
1900
1901 int gphoto_close(gphoto_driver *gphoto)
1902 {
1903 int result;
1904 if (!gphoto)
1905 return 0;
1906 if (gphoto->thread)
1907 {
1908 pthread_mutex_lock(&gphoto->mutex);
1909 gphoto->command |= DSLR_CMD_THREAD_EXIT;
1910 pthread_cond_signal(&gphoto->signal);
1911 pthread_mutex_unlock(&gphoto->mutex);
1912 pthread_join(gphoto->thread, nullptr);
1913 }
1914
1915 if (gphoto->exposureList)
1916 free(gphoto->exposureList);
1917 if (gphoto->exposure_widget && gphoto->exposure_widget->type == GP_WIDGET_RADIO)
1918 widget_free(gphoto->exposure_widget);
1919 if (gphoto->format_widget)
1920 widget_free(gphoto->format_widget);
1921 if (gphoto->iso_widget)
1922 widget_free(gphoto->iso_widget);
1923 if (gphoto->bulb_widget)
1924 widget_free(gphoto->bulb_widget);
1925 if (gphoto->autoexposuremode_widget)
1926 widget_free(gphoto->autoexposuremode_widget);
1927 if (gphoto->viewfinder_widget)
1928 widget_free(gphoto->viewfinder_widget);
1929 if (gphoto->focus_widget)
1930 widget_free(gphoto->focus_widget);
1931 if (gphoto->customfuncex_widget)
1932 widget_free(gphoto->customfuncex_widget);
1933
1934 while (gphoto->widgets)
1935 {
1936 gphoto_widget_list *next = gphoto->widgets->next;
1937 widget_free(gphoto->widgets->widget);
1938 free(gphoto->widgets);
1939 gphoto->widgets = next;
1940 }
1941
1942 result = gp_camera_exit(gphoto->camera, gphoto->context);
1943 if (result != GP_OK)
1944 {
1945 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "WARNING: Could not close camera connection.");
1946 }
1947 // We seem to leak the context here. libgphoto supplies no way to free it?
1948 free(gphoto);
1949 return 0;
1950 }
1951
1952 gphoto_widget *gphoto_get_widget_info(gphoto_driver *gphoto, gphoto_widget_list **iter)
1953 {
1954 INDI_UNUSED(gphoto);
1955 gphoto_widget *widget;
1956
1957 if (!*iter)
1958 return nullptr;
1959 widget = (*iter)->widget;
1960 int ret = gphoto_read_widget(widget);
1961 // Read next iterator regardless of return value.
1962 *iter = (*iter)->next;
1963 if (ret == GP_OK)
1964 {
1965 return widget;
1966 }
1967 else
1968 return nullptr;
1969 }
1970
1971 #define GPHOTO_MATCH_WIDGET(widget1, widget2) (widget2 && widget1 == widget2->widget)
1972 static void find_all_widgets(gphoto_driver *gphoto, CameraWidget *widget, char *prefix)
1973 {
1974 int ret, n, i;
1975 char *newprefix = nullptr;
1976 const char *uselabel;
1977 CameraWidgetType type;
1978
1979 uselabel = widget_name(widget);
1980 gp_widget_get_type(widget, &type);
1981
1982 n = gp_widget_count_children(widget);
1983
1984 if (prefix)
1985 {
1986 newprefix = (char *)malloc(strlen(prefix) + 1 + strlen(uselabel) + 1);
1987 if (!newprefix)
1988 return;
1989 sprintf(newprefix, "%s/%s", prefix, uselabel);
1990 }
1991
1992 if ((type != GP_WIDGET_WINDOW) && (type != GP_WIDGET_SECTION))
1993 {
1994 gphoto_widget_list *list;
1995 CameraWidget *parent = nullptr;
1996
1997 if (newprefix)
1998 {
1999 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "\t%s", newprefix);
2000 free(newprefix);
2001 }
2002 if (GPHOTO_MATCH_WIDGET(widget, gphoto->iso_widget) || GPHOTO_MATCH_WIDGET(widget, gphoto->format_widget) ||
2003 GPHOTO_MATCH_WIDGET(widget, gphoto->exposure_widget) || GPHOTO_MATCH_WIDGET(widget, gphoto->bulb_widget))
2004 {
2005 return;
2006 }
2007 list = (gphoto_widget_list *)calloc(1, sizeof(gphoto_widget_list));
2008 list->widget = (gphoto_widget *)calloc(sizeof(gphoto_widget), 1);
2009 list->widget->widget = widget;
2010 list->widget->type = type;
2011 list->widget->name = uselabel;
2012 gp_widget_get_parent(widget, &parent);
2013 if (parent)
2014 list->widget->parent = widget_name(parent);
2015 if (!gphoto->widgets)
2016 {
2017 gphoto->widgets = list;
2018 }
2019 else
2020 {
2021 gphoto->iter->next = list;
2022 }
2023 gp_widget_get_readonly(widget, &list->widget->readonly);
2024 gphoto->iter = list;
2025 return;
2026 }
2027
2028 for (i = 0; i < n; i++)
2029 {
2030 CameraWidget *child;
2031
2032 ret = gp_widget_get_child(widget, i, &child);
2033 if (ret != GP_OK)
2034 continue;
2035 find_all_widgets(gphoto, child, newprefix);
2036 }
2037 if (newprefix)
2038 free(newprefix);
2039 }
2040
2041 gphoto_widget_list *gphoto_find_all_widgets(gphoto_driver *gphoto)
2042 {
2043 find_all_widgets(gphoto, gphoto->config, nullptr);
2044 return gphoto->widgets;
2045 }
2046
2047 void gphoto_show_options(gphoto_driver *gphoto)
2048 {
2049 find_all_widgets(gphoto, gphoto->config, nullptr);
2050 if (gphoto->widgets)
2051 {
2052 gphoto_widget_list *list = gphoto->widgets;
2053 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Available options");
2054 while (list)
2055 {
2056 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "\t%s:", list->widget->name);
2057 int ret = gphoto_read_widget(list->widget);
2058 if (ret == GP_OK)
2059 show_widget(list->widget, "\t\t");
2060 list = list->next;
2061 }
2062 }
2063 if (gphoto->format_widget)
2064 {
2065 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Available image formats:");
2066 show_widget(gphoto->format_widget, "\t");
2067 }
2068 if (gphoto->iso_widget)
2069 {
2070 DEBUGDEVICE(device, INDI::Logger::DBG_DEBUG, "Available ISO:");
2071 show_widget(gphoto->iso_widget, "\t");
2072 }
2073 }
2074
2075 void gphoto_get_buffer(gphoto_driver *gphoto, const char **buffer, unsigned long *size)
2076 {
2077 gp_file_get_data_and_size(gphoto->camerafile, buffer, size);
2078 }
2079
2080 void gphoto_free_buffer(gphoto_driver *gphoto)
2081 {
2082 if (gphoto->camerafile)
2083 {
2084 gp_file_free(gphoto->camerafile);
2085 gphoto->camerafile = nullptr;
2086 }
2087 }
2088
2089 const char *gphoto_get_file_extension(gphoto_driver *gphoto)
2090 {
2091 if (gphoto->filename[0])
2092 return strchr(gphoto->filename, '.') + 1;
2093 else
2094 return "unknown";
2095 }
2096
2097 int gphoto_get_dimensions(gphoto_driver *gphoto, int *width, int *height)
2098 {
2099 *width = gphoto->width;
2100 *height = gphoto->height;
2101 return 0;
2102 }
2103
2104 /*
2105 * This function looks up a label or key entry of
2106 * a configuration widget.
2107 * The functions descend recursively, so you can just
2108 * specify the last component.
2109 */
2110
2111 static int _lookup_widget(CameraWidget *widget, const char *key, CameraWidget **child)
2112 {
2113 int ret;
2114 ret = gp_widget_get_child_by_name(widget, key, child);
2115 if (ret < GP_OK)
2116 ret = gp_widget_get_child_by_label(widget, key, child);
2117 return ret;
2118 }
2119
2120 /* calls the Nikon DSLR or Canon DSLR autofocus method. */
2121 int gphoto_auto_focus(gphoto_driver *gphoto, char *errMsg)
2122 {
2123 CameraWidget *widget = nullptr, *child = nullptr;
2124 CameraWidgetType type;
2125 int ret, val;
2126
2127 ret = gp_camera_get_config(gphoto->camera, &widget, gphoto->context);
2128 if (ret < GP_OK)
2129 {
2130 snprintf(errMsg, MAXRBUF, "camera_get_config failed: %d", ret);
2131 return ret;
2132 }
2133 ret = _lookup_widget(widget, "autofocusdrive", &child);
2134 if (ret < GP_OK)
2135 {
2136 snprintf(errMsg, MAXRBUF, "lookup 'autofocusdrive' failed: %d", ret);
2137 goto out;
2138 }
2139
2140 /* check that this is a toggle */
2141 ret = gp_widget_get_type(child, &type);
2142 if (ret < GP_OK)
2143 {
2144 snprintf(errMsg, MAXRBUF, "widget get type failed: %d", ret);
2145 goto out;
2146 }
2147 switch (type)
2148 {
2149 case GP_WIDGET_TOGGLE:
2150 break;
2151 default:
2152 snprintf(errMsg, MAXRBUF, "widget has bad type %d", type);
2153 ret = GP_ERROR_BAD_PARAMETERS;
2154 goto out;
2155 }
2156
2157 ret = gp_widget_get_value(child, &val);
2158 if (ret < GP_OK)
2159 {
2160 snprintf(errMsg, MAXRBUF, "could not get widget value: %d", ret);
2161 goto out;
2162 }
2163 val++;
2164 ret = gp_widget_set_value(child, &val);
2165 if (ret < GP_OK)
2166 {
2167 snprintf(errMsg, MAXRBUF, "could not set widget value to 1: %d", ret);
2168 goto out;
2169 }
2170
2171 ret = gp_camera_set_config(gphoto->camera, widget, gphoto->context);
2172 if (ret < GP_OK)
2173 {
2174 snprintf(errMsg, MAXRBUF, "could not set config tree to autofocus: %d", ret);
2175 goto out;
2176 }
2177 out:
2178 gp_widget_free(widget);
2179 return ret;
2180 }
2181
2182 int gphoto_capture_preview(gphoto_driver *gphoto, CameraFile *previewFile, char *errMsg)
2183 {
2184 int rc = gp_camera_capture_preview(gphoto->camera, previewFile, gphoto->context);
2185 if (rc != GP_OK)
2186 snprintf(errMsg, MAXRBUF, "Error capturing preview: %s", gp_result_as_string(rc));
2187
2188 return rc;
2189 }
2190
2191 int gphoto_start_preview(gphoto_driver *gphoto)
2192 {
2193 // Olympus, Sony, and Fuji cameras support streaming but without viewfinder_widget
2194 if (strcasestr(gphoto->manufacturer, "OLYMPUS") ||
2195 strcasestr(gphoto->manufacturer, "Sony Corporation") ||
2196 strcasestr(gphoto->manufacturer, "Fuji"))
2197 return GP_OK;
2198
2199 // If viewfinder not found, nothing to do
2200 if (gphoto->viewfinder_widget == nullptr)
2201 {
2202 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "View finder widget is not found. Cannot force camera mirror to go up!");
2203 return GP_ERROR_NOT_SUPPORTED;
2204 }
2205
2206 return gphoto_set_widget_num(gphoto, gphoto->viewfinder_widget, 1);
2207 }
2208
2209 int gphoto_stop_preview(gphoto_driver *gphoto)
2210 {
2211 // Olympus & Sony cameras support streaming but without viewfinder_widget
2212 if (strcasestr(gphoto->manufacturer, "OLYMPUS") || strcasestr(gphoto->manufacturer, "Sony Corporation"))
2213 return GP_OK;
2214
2215 // If viewfinder not found, nothing to do
2216 if (gphoto->viewfinder_widget == nullptr)
2217 {
2218 DEBUGDEVICE(device, INDI::Logger::DBG_WARNING, "View finder widget is not found. Cannot force camera mirror to go down!");
2219 return GP_ERROR_NOT_SUPPORTED;
2220 }
2221
2222 return gphoto_set_widget_num(gphoto, gphoto->viewfinder_widget, 0);
2223 }
2224
2225 void gphoto_set_view_finder(gphoto_driver *gphoto, bool enabled)
2226 {
2227 if (gphoto->viewfinder_widget)
2228 {
2229 gphoto_set_widget_num(gphoto, gphoto->viewfinder_widget, enabled ? 1 : 0);
2230 }
2231 }
2232
2233 bool gphoto_can_focus(gphoto_driver *gphoto)
2234 {
2235 return gphoto->focus_widget != nullptr;
2236 }
2237
2238 /* Manual focusing a camera...
2239 * xx is -3 / -2 / -1 / 0 / 1 / 2 / 3
2240 * Choice: 0 (-3) Near 1
2241 * Choice: 1 (-2) Near 2
2242 * Choice: 2 (-1) Near 3
2243 * Choice: 3 ( 0) None
2244 * Choice: 4 (+1) Far 1
2245 * Choice: 5 (+2) Far 2
2246 * Choice: 6 (+3) Far 3
2247 */
2248 int gphoto_manual_focus(gphoto_driver *gphoto, int speed, char *errMsg)
2249 {
2250 int rc = 0;
2251
2252 switch (gphoto->focus_widget->type)
2253 {
2254 case GP_WIDGET_RADIO:
2255 {
2256 // For Canon
2257 // Speed is either
2258 // 0: (None)
2259 // 1: Far 1
2260 // 2: Far 2
2261 // 3: Far 3
2262 // -1: Near 1
2263 // -2: Near 2
2264 // -3: Near 3
2265
2266 // Widget: Near1, Near2, Near3, None, Far1, Far2, Far3
2267 // Mapping target speed to widget choices
2268 uint8_t choice_index = (speed >= 0) ? speed + 3 : (speed * -1) - 1;
2269 if (choice_index > gphoto->focus_widget->choice_cnt)
2270 {
2271 snprintf(errMsg, MAXRBUF, "Speed %d choice index %d is out of bounds for focus widget count %d",
2272 speed, choice_index, gphoto->focus_widget->choice_cnt);
2273 return rc;
2274 }
2275
2276 // Set to None First before setting the actual value
2277 gp_widget_set_value(gphoto->focus_widget->widget, gphoto->focus_widget->choices[3]);
2278 gphoto_set_config(gphoto->camera, gphoto->config, gphoto->context);
2279
2280 usleep(100000);
2281
2282 rc = gp_widget_set_value(gphoto->focus_widget->widget, gphoto->focus_widget->choices[choice_index]);
2283 if (rc < GP_OK)
2284 {
2285 snprintf(errMsg, MAXRBUF, "Failed to set focus widget choice to %d: %s", choice_index, gp_result_as_string(rc));
2286 return rc;
2287 }
2288 break;
2289 }
2290 case GP_WIDGET_RANGE:
2291 {
2292 int rval = speed;
2293
2294 /* Range is on Nikon from -32768 <-> 32768 */
2295 if (strstr(device, "Nikon"))
2296 {
2297 switch (speed)
2298 {
2299 case -3:
2300 rval = -1024;
2301 break;
2302 case -2:
2303 rval = -512;
2304 break;
2305 case -1:
2306 rval = -128;
2307 break;
2308 case 1:
2309 rval = 128;
2310 break;
2311 case 2:
2312 rval = 512;
2313 break;
2314 case 3:
2315 rval = 1024;
2316 break;
2317 default:
2318 rval = 0;
2319 break;
2320 }
2321 }
2322
2323 rc = gp_widget_set_value(gphoto->focus_widget->widget, &rval);
2324 if (rc < GP_OK)
2325 {
2326 snprintf(errMsg, MAXRBUF, "could not set widget value to 1: %s", gp_result_as_string(rc));
2327 return rc;
2328 }
2329 break;
2330 }
2331 default:
2332 rc = -1;
2333 snprintf(errMsg, MAXRBUF, "Unsupported camera type: %d", gphoto->focus_widget->type);
2334 return rc;
2335 }
2336
2337
2338 for (int i = 0; i < 10; i++)
2339 {
2340 rc = gphoto_set_config(gphoto->camera, gphoto->config, gphoto->context);
2341 if (rc == GP_ERROR_CAMERA_BUSY)
2342 {
2343 usleep(500000);
2344 continue;
2345 }
2346 else
2347 break;
2348 }
2349
2350 if (rc < GP_OK)
2351 {
2352 snprintf(errMsg, MAXRBUF, "could not set config tree to manual focus: %s", gp_result_as_string(rc));
2353 return rc;
2354 }
2355
2356 return rc;
2357 }
2358
2359 const char *gphoto_get_manufacturer(gphoto_driver *gphoto)
2360 {
2361 return gphoto->manufacturer;
2362 }
2363
2364 const char *gphoto_get_model(gphoto_driver *gphoto)
2365 {
2366 return gphoto->model;
2367 }
2368
2369 int gphoto_get_capture_target(gphoto_driver *gphoto, int *capture_target)
2370 {
2371 if (gphoto->capturetarget_widget == nullptr)
2372 return GP_ERROR_NOT_SUPPORTED;
2373
2374 gphoto_read_widget(gphoto->capturetarget_widget);
2375
2376 *capture_target = gphoto->capturetarget_widget->value.toggle;
2377
2378 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Capture target is %s.",
2379 (*capture_target == 0) ? "INTERNAL RAM" : "SD Card");
2380
2381 return GP_OK;
2382 }
2383
2384 int gphoto_set_capture_target(gphoto_driver *gphoto, int capture_target)
2385 {
2386 if (gphoto->capturetarget_widget == nullptr)
2387 return GP_ERROR_NOT_SUPPORTED;
2388
2389 gphoto_set_widget_num(gphoto, gphoto->capturetarget_widget, capture_target);
2390
2391 return GP_OK;
2392 }
2393
2394 int gphoto_handle_sdcard_image(gphoto_driver *gphoto, CameraImageHandling handling)
2395 {
2396 gphoto->handle_sdcard_image = handling;
2397 return GP_OK;
2398 }
2399
2400
2401 void gphoto_force_bulb(gphoto_driver *gphoto, bool enabled)
2402 {
2403 gphoto->force_bulb = enabled;
2404 }
2405
2406 #ifdef GPHOTO_TEST
2407 #include <getopt.h>
2408
2409 void write_image(gphoto_driver *gphoto, const char *basename)
2410 {
2411 FILE *fh;
2412 const char *buffer;
2413 size_t size;
2414 const char *ext;
2415 char filename[256];
2416 gphoto_get_buffer(gphoto, &buffer, &size);
2417 ext = gphoto_get_file_extension(gphoto);
2418 sprintf(filename, "%s.%s", basename, ext);
2419 fh = fopen(filename, "w+");
2420 fwrite(buffer, size, 1, fh);
2421 fclose(fh);
2422 }
2423
2424 void show_help()
2425 {
2426 printf("gphoto_driver [options]");
2427 printf("\t\t-e/--exposure <exposure> specify exposure in msec (default: 100)");
2428 printf("\t\t-f/--file <filename> specify filename to write to");
2429 printf("\t\t-c/--count <count> specify how many sequential images to take");
2430 printf("\t\t-i/--iso <iso> choose iso (use --list to query values)");
2431 printf("\t\t-m/--format <format #> choose format (use --list to query values)");
2432 printf("\t\t-p/--port <path to serial port> choose a serial port to use for shutter release control");
2433 printf("\t\t-l/--list show available iso and format values");
2434 printf("\t\t-d/--debug enable debugging");
2435 printf("\t\t-h//-help show this message");
2436 exit(0);
2437 }
2438
2439 int main(int argc, char **argv)
2440 {
2441 gphoto_driver *gphoto;
2442 char image_name[80];
2443 int i;
2444 int count = 0;
2445 int list = 0;
2446 char *iso = nullptr;
2447 char *port = nullptr;
2448 int format = -1;
2449 unsigned int exposure = 100;
2450 unsigned int mlock = 0;
2451 char basename[256] = "image";
2452
2453 struct option long_options[] = { { "count", required_argument, nullptr, 'c' },
2454 { "debug", required_argument, nullptr, 'd' },
2455 { "exposure", required_argument, nullptr, 'e' },
2456 { "file", required_argument, nullptr, 'f' },
2457 { "help", no_argument, nullptr, 'h' },
2458 { "iso", required_argument, nullptr, 'i' },
2459 { "mlock", required_argument, nullptr, 'k' },
2460 { "list", no_argument, nullptr, 'l' },
2461 { "format", required_argument, nullptr, 'm' },
2462 { "port", required_argument, nullptr, 'p' },
2463 { 0, 0, 0, 0 }
2464 };
2465
2466 while (1)
2467 {
2468 char c;
2469 c = getopt_long(argc, argv, "c:de:f:hi:lm:p:", long_options, nullptr);
2470 if (c == EOF)
2471 break;
2472 switch (c)
2473 {
2474 case 'c':
2475 count = strtol(optarg, nullptr, 0);
2476 break;
2477 case 'd':
2478 debug = 1;
2479 break;
2480 case 'e':
2481 exposure = strtol(optarg, nullptr, 0);
2482 break;
2483 case 'f':
2484 strncpy(basename, optarg, 255);
2485 break;
2486 case 'h':
2487 show_help();
2488 break;
2489 case 'i':
2490 iso = optarg;
2491 break;
2492 case 'k':
2493 mlock = strtol(optarg, nullptr, 0);
2494 break;
2495 case 'l':
2496 list = 1;
2497 break;
2498 case 'm':
2499 format = strtol(optarg, nullptr, 0);
2500 break;
2501 case 'p':
2502 port = optarg;
2503 break;
2504 }
2505 }
2506
2507 if (!(gphoto = gphoto_open(port)))
2508 {
2509 printf("Could not open the DSLR device");
2510 return -1;
2511 }
2512 if (list)
2513 {
2514 gphoto_show_options(gphoto);
2515 gphoto_close(gphoto);
2516 return 0;
2517 }
2518
2519 if (iso)
2520 {
2521 int max;
2522 const char **values = gphoto_get_iso(gphoto, &max);
2523 for (i = 0; i < max; i++)
2524 {
2525 if (strcmp(values[i], iso) == 0)
2526 {
2527 gphoto_set_iso(gphoto, i);
2528 break;
2529 }
2530 }
2531 }
2532 if (format != -1)
2533 {
2534 gphoto_set_format(gphoto, format);
2535 }
2536 if (!gphoto->exposure_widget)
2537 {
2538 printf("No exposure widget. Aborting...");
2539 gphoto_close(gphoto);
2540 return 1;
2541 }
2542 printf("Exposing for %f sec", exposure / 1000.0);
2543 if (count == 0)
2544 {
2545 if (gphoto_start_exposure(gphoto, exposure, mlock))
2546 {
2547 printf("Exposure failed!");
2548 gphoto_close(gphoto);
2549 return 1;
2550 }
2551 usleep(exposure * 1000);
2552 gphoto_read_exposure(gphoto);
2553 write_image(gphoto, basename);
2554 }
2555 for (i = 0; i < count; i++)
2556 {
2557 sprintf(image_name, "%s%d", basename, i);
2558 if (gphoto_start_exposure(gphoto, exposure, mlock))
2559 {
2560 printf("Exposure failed!");
2561 gphoto_close(gphoto);
2562 return 1;
2563 }
2564 usleep(exposure * 1000);
2565 gphoto_read_exposure(gphoto);
2566 write_image(gphoto, image_name);
2567 }
2568 gphoto_close(gphoto);
2569 return 0;
2570 }
2571 #endif