-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
1325 lines (1293 loc) · 48.7 KB
/
server.c
File metadata and controls
1325 lines (1293 loc) · 48.7 KB
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
/*
* This file is part of the CCD_Capture project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdatomic.h>
#include <netdb.h>
#include <pthread.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <usefull_macros.h>
#include "ccdfunc.h"
#include "cmdlnopts.h"
#include "server.h"
#include "socket.h"
static int parsestring(int fd, cc_handleritem *handlers, char *str);
static atomic_int camdevno = 0, wheeldevno = 0, focdevno = 0; // current devices numbers
static _Atomic cc_camera_state camstate = CAMERA_IDLE;
#define FLAG_STARTCAPTURE (1<<0)
#define FLAG_CANCEL (1<<1)
#define FLAG_RESTARTSERVER (1<<2)
static atomic_int camflags = 0, camfanspd = 0, confio = 0, nflushes, infty = 0;
static char *outfile = NULL, *lastfile = NULL; // current output file name/prefix; last name of saved file
static cc_frameformat frmformatmax = {0}, curformat = {0}; // maximal format
static float focmaxpos = 0., focminpos = 0.; // focuser extremal positions
static int wmaxpos = 0.; // wheel max pos
static float tremain = 0.; // time when capture done
// IPC key for shared memory
static key_t shmkey = IPC_PRIVATE;
typedef struct{
const char *key;
const char *help;
}strpair;
// cat | awk '{print "{ " $3 ", \"\" }," }' | sort
strpair allcommands[] = {
{ CC_CMD_8BIT, "run in 8 bit mode instead of 16 bit" },
{ CC_CMD_AUTHOR, "FITS 'AUTHOR' field" },
{ CC_CMD_BRIGHTNESS, "camera brightness" },
{ CC_CMD_CAMDEVNO, "camera device number" },
{ CC_CMD_CAMLIST, "list all connected cameras" },
{ CC_CMD_CAMFANSPD, "fan speed of camera" },
{ CC_CMD_CONFIO, "camera IO configuration" },
{ CC_CMD_DARK, "don't open shutter @ exposure" },
{ CC_CMD_EXPSTATE, "get exposition state (0: cancel, 1: capturing, 2: frame ready, 3: error) "
"or set (0: cancel, 1: start exp)\n also get camflags (bits: 0-start capture, 1-cancel, 2-restart server" },
{ CC_CMD_EXPOSITION, "exposition time" },
{ CC_CMD_FASTSPD, "fast readout speed" },
{ CC_CMD_FILENAME, "save file with this name, like file.fits" },
{ CC_CMD_FILENAMEPREFIX,"prefix of files, like ex (will be saved as exXXXX.fits)" },
{ CC_CMD_FDEVNO, "focuser device number" },
{ CC_CMD_FOCLIST, "list all connected focusers" },
{ CC_CMD_FGOTO, "focuser position" },
{ CC_CMD_FRAMEFORMAT, "camera frame format (X0,Y0,X1,Y1)" },
{ CC_CMD_GAIN, "camera gain" },
{ CC_CMD_GETHEADERS, "get last file FITS headers" },
{ CC_CMD_HBIN, "horizontal binning" },
{ CC_CMD_HEADERFILES, "add FITS records from these files (comma-separated list)" },
{ CC_CMD_HELP, "show this help" },
{ CC_CMD_IMHEIGHT, "last image height" },
{ CC_CMD_IMWIDTH, "last image width" },
{ CC_CMD_INFO, "connected devices state" },
{ CC_CMD_INFTY, "an infinity loop taking images until there's connected clients" },
{ CC_CMD_INSTRUMENT, "FITS 'INSTRUME' field" },
{ CC_CMD_IO, "get/set camera IO" },
{ CC_CMD_LASTFNAME, "path to last saved file"},
{ CC_CMD_FRAMEMAX, "camera maximal available format" },
{ CC_CMD_NFLUSHES, "camera number of preflushes" },
{ CC_CMD_OBJECT, "FITS 'OBJECT' field" },
{ CC_CMD_OBJTYPE, "FITS 'IMAGETYP' field" },
{ CC_CMD_OBSERVER, "FITS 'OBSERVER' field" },
{ CC_CMD_PLUGINCMD, "custom camera plugin command" },
{ CC_CMD_PROGRAM, "FITS 'PROG-ID' field" },
{ CC_CMD_RESTART, "restart server" },
{ CC_CMD_REWRITE, "rewrite file (if give `filename`, not `filenameprefix`)" },
{ CC_CMD_SHMEMKEY, "get shared memory key" },
{ CC_CMD_SHUTTER, "camera shutter's operations" },
{ CC_CMD_CAMTEMPER, "camera chip temperature" },
{ CC_CMD_TREMAIN, "time (in seconds) of exposition remained" },
{ CC_CMD_VBIN, "vertical binning" },
{ CC_CMD_WDEVNO, "wheel device number" },
{ CC_CMD_WLIST, "list all connected wheels" },
{ CC_CMD_WPOS, "wheel position" },
{NULL, NULL},
};
static pthread_mutex_t locmutex = PTHREAD_MUTEX_INITIALIZER; // mutex for wheel/camera/focuser functions
// return TRUE if `locmutex` can be locked
static int lock(){
if(pthread_mutex_trylock(&locmutex)){
//DBG("\n\nAlready locked");
return FALSE;
}
return TRUE;
}
static void unlock(){
if(pthread_mutex_unlock(&locmutex)) ERR("Can't unlock mutex");
}
static cc_IMG *ima = NULL;
static void fixima(){
FNAME();
if(!camera) return;
int raw_width = curformat.w / GP->hbin, raw_height = curformat.h / GP->vbin;
// allocate memory for largest possible image
if(!ima) ima = cc_getshm(GP->shmkey, camera->array.h * camera->array.w * 2);
if(!ima) ERRX("Can't allocate memory for image");
shmkey = GP->shmkey;
//if(raw_width == ima->w && raw_height == ima->h) return; // all OK
DBG("curformat: %dx%d", curformat.w, curformat.h);
ima->h = raw_height;
ima->w = raw_width;
if(!camera->getbitpix || !camera->getbitpix(&ima->bitpix)) ima->bitpix = 16;
if(ima->bitpix < 8 || ima->bitpix > 16) ima->bitpix = 16; // use maximum in any strange cases
DBG("bitpix=%d", ima->bitpix);
GP->_8bit = (ima->bitpix < 9) ? 1 : 0;
DBG("GP->_8bit=%d", GP->_8bit);
ima->bytelen = raw_height * raw_width * cc_getNbytes(ima);
DBG("new image: %dx%d", raw_width, raw_height);
}
// functions for processCAM finite state machine
static inline void cameraidlestate(){ // idle - wait for capture commands
if(camflags & FLAG_STARTCAPTURE){ // start capturing
TIMESTAMP("Start exposition");
camflags &= ~(FLAG_STARTCAPTURE | FLAG_CANCEL);
camstate = CAMERA_CAPTURE;
fixima();
if(!camera->startexposition){
LOGERR(_("Camera plugin have no function `start exposition`"));
WARNX(_("Camera plugin have no function `start exposition`"));
camstate = CAMERA_ERROR;
return;
}
if(!camera->startexposition()){
LOGERR(_("Can't start exposition"));
WARNX(_("Can't start exposition"));
camstate = CAMERA_ERROR;
return;
}
}
}
static inline void cameracapturestate(){ // capturing - wait for exposition ends
cc_capture_status cs;
if(camera->pollcapture && camera->pollcapture(&cs, &tremain)){
if(cs != CAPTURE_PROCESS){
TIMESTAMP("Capture ready");
tremain = 0.;
// now save frame
if(!ima->data) LOGERR("Can't save image: not initialized");
else{
TIMESTAMP("start capture");
if(!camera->capture){
WARNX(_("Camera plugin have no function `capture`"));
LOGERR(_("Camera plugin have no function `capture`"));
camstate = CAMERA_ERROR;
return;
}
if(!camera->capture(ima)){
LOGERR("Can't capture image");
camstate = CAMERA_ERROR;
return;
}else{
ima->gotstat = 0; // fresh image without statistics - recalculate when save
ima->timestamp = sl_dtime(); // set timestamp
++ima->imnumber; // increment counter
if(saveFITS(ima, &lastfile)){
DBG("LAST file name changed");
}
TIMESTAMP("Image saved");
}
}
camstate = CAMERA_FRAMERDY;
}
}
}
// base camera thread
static void* processCAM(_U_ void *d){
if(!camera){
LOGERR("No camera device");
ERRX(_("No camera device"));
}
double logt = 0;
while(1){
if(camflags & FLAG_RESTARTSERVER){
LOGERR("User asks to restart");
signals(1);
}
usleep(100);
if(tremain < 0.5 && tremain > 0.) usleep(tremain*1e6);
if(lock()){
// log
if(sl_dtime() - logt > TLOG_PAUSE){
logt = sl_dtime();
float t;
if(camera->getTcold && camera->getTcold(&t)){
LOGMSG("CCDTEMP=%.1f", t);
}
if(camera->getThot && camera->getThot(&t)){
LOGMSG("HOTTEMP=%.1f", t);
}
if(camera->getTbody && camera->getTbody(&t)){
LOGMSG("BODYTEMP=%.1f", t);
}
}
if(camflags & FLAG_CANCEL){ // cancel all expositions
DBG("Cancel exposition");
LOGMSG("User canceled exposition");
camflags &= ~(FLAG_STARTCAPTURE | FLAG_CANCEL);
if(camera->cancel) camera->cancel();
camstate = CAMERA_IDLE;
infty = 0; // also cancel infinity loop
unlock();
continue;
}
cc_camera_state curstate = camstate;
switch(curstate){
case CAMERA_IDLE:
cameraidlestate();
break;
case CAMERA_CAPTURE:
cameracapturestate();
break;
case CAMERA_FRAMERDY:
// do nothing: when `server` got this state it sends "expstate=2" to all clients and changes state to IDLE
break;
case CAMERA_ERROR:
// do nothing: when `server` got this state it sends "expstate=3" to all clients and changes state to IDLE
break;
}
unlock();
}
}
return NULL;
}
// functions running @ each devno change
static int camdevini(int n){
FNAME();
if(!camera) return FALSE;
DBG("Devno: %d", n);
if(camera->setDevNo && !camera->setDevNo(n)){
LOGERR("Can't set active camera number");
return FALSE;
}
camdevno = n;
LOGMSG("Set camera device number to %d", camdevno);
cc_frameformat step;
if(camera->getgeomlimits) camera->getgeomlimits(&frmformatmax, &step);
curformat = frmformatmax;
DBG("\n\nGeometry format max (offx/offy) w/h: (%d/%d) %d/%d", curformat.xoff, curformat.yoff,
curformat.w, curformat.h);
// curformat.w -= curformat.xoff;
// curformat.h -= curformat.yoff;
curformat.xoff = 0;
curformat.yoff = 0;
if(GP->hbin < 1) GP->hbin = 1;
if(GP->vbin < 1) GP->vbin = 1;
fixima();
if(!camera->setbin || !camera->setbin(GP->hbin, GP->vbin)) WARNX(_("Can't set binning %dx%d"), GP->hbin, GP->vbin);
if(!camera->setgeometry || !camera->setgeometry(&curformat)) WARNX(_("Can't set given geometry"));
return TRUE;
}
static int focdevini(int n){
if(!focuser) return FALSE;
if(!focuser->setDevNo(n)){
LOGERR("Can't set active focuser number");
return FALSE;
}
focdevno = n;
LOGMSG("Set focuser device number to %d", focdevno);
focuser->getMaxPos(&focmaxpos);
focuser->getMinPos(&focminpos);
return TRUE;
}
static int wheeldevini(int n){
if(!wheel) return FALSE;
if(!wheel->setDevNo(n)){
LOGERR("Can't set active wheel number");
return FALSE;
}
wheeldevno = n;
LOGMSG("Set wheel device number to %d", wheeldevno);
wheel->getMaxPos(&wmaxpos);
return TRUE;
}
/*******************************************************************************
*************************** Service handlers **********************************
******************************************************************************/
static cc_hresult restarthandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
camflags |= FLAG_RESTARTSERVER;
return CC_RESULT_OK;
}
/*******************************************************************************
*************************** CCD/CMOS handlers *********************************
******************************************************************************/
// image size
static cc_hresult imsizehandler(int fd, const char *key, _U_ const char *val){
char buf[64];
// send image width/height in pixels
if(0 == strcmp(key, CC_CMD_IMHEIGHT)) snprintf(buf, 63, CC_CMD_IMHEIGHT "=%d", ima->h);
else snprintf(buf, 63, CC_CMD_IMWIDTH "=%d", ima->w);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult camlisthandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[BUFSIZ], modname[256];
if(!camera->getModelName) return CC_RESULT_FAIL;
for(int i = 0; i < camera->Ndevices; ++i){
if(camera->setDevNo && !camera->setDevNo(i)) continue;
camera->getModelName(modname, 255);
snprintf(buf, BUFSIZ-1, CC_CMD_CAMLIST "='%s'", modname);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(camdevno > -1 && camera->setDevNo) camera->setDevNo(camdevno);
return CC_RESULT_SILENCE;
}
static cc_hresult camsetNhandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(val){
int num = atoi(val);
if(num > camera->Ndevices - 1 || num < 0){
return CC_RESULT_BADVAL;
}
if(!camdevini(num)) return CC_RESULT_FAIL;
}
snprintf(buf, 63, CC_CMD_CAMDEVNO "=%d", camdevno);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// exposition time setter/getter
static cc_hresult exphandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(val){
DBG("setexp to %s", val);
double v = atof(val);
if(v < DBL_EPSILON) return CC_RESULT_BADVAL;
if(!camera->setexp) return CC_RESULT_FAIL;
if(camera->setexp(v)){
GP->exptime = v;
}else LOGWARN("Can't set exptime to %g", v);
}
DBG("expt: %g", GP->exptime);
snprintf(buf, 63, CC_CMD_EXPOSITION "=%g", GP->exptime);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// show last filename of saved FITS
static cc_hresult lastfnamehandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[PATH_MAX+32];
if(lastfile && *lastfile) snprintf(buf, PATH_MAX+31, CC_CMD_LASTFNAME "=%s", lastfile);
else snprintf(buf, PATH_MAX+31, CC_CMD_LASTFNAME "=");
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// filename setter/getter
static cc_hresult namehandler(int fd, _U_ const char *key, const char *val){
char buf[PATH_MAX+32];
DBG("filename=%s", val);
if(val && *val){
DBG("Make abs path");
char *path = makeabspath(val, FALSE);
if(!path){
LOGERR("Can't create file '%s'", val);
return CC_RESULT_BADVAL;
}
FREE(outfile);
outfile = strdup(path);
GP->outfile = outfile;
GP->outfileprefix = NULL;
}else{ // clear names
DBG("Clear names");
GP->outfileprefix = NULL;
GP->outfile = NULL;
return CC_RESULT_OK;
}
if(!GP->outfile) return CC_RESULT_FAIL;
snprintf(buf, PATH_MAX+31, CC_CMD_FILENAME "=%s", GP->outfile);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// filename prefix
static cc_hresult nameprefixhandler(_U_ int fd, _U_ const char *key, const char *val){
char buf[PATH_MAX+32];
DBG("filename prefix=%s", val);
if(val){
char *path = makeabspath(val, FALSE);
if(!path){
LOGERR("Can't create file '%s'", val);
return CC_RESULT_BADVAL;
}
FREE(outfile);
outfile = strdup(path);
GP->outfileprefix = outfile;
GP->outfile = NULL;
}else{ // clear names
GP->outfileprefix = NULL;
GP->outfile = NULL;
return CC_RESULT_OK;
}
if(!GP->outfileprefix) return CC_RESULT_FAIL;
snprintf(buf, PATH_MAX+31, CC_CMD_FILENAMEPREFIX "=%s", GP->outfileprefix);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// rewrite
static cc_hresult rewritefilehandler(_U_ int fd, _U_ const char *key, const char *val){
char buf[64];
if(val){
int n = atoi(val);
if(n < 0 || n > 1) return CC_RESULT_BADVAL;
GP->rewrite = n;
}
snprintf(buf, 63, CC_CMD_REWRITE "=%d", GP->rewrite);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult binhandler(_U_ int fd, const char *key, const char *val){
char buf[64];
if(val){
int b = atoi(val);
if(b < 1) return CC_RESULT_BADVAL;
if(0 == strcmp(key, CC_CMD_HBIN)) GP->hbin = b;
else GP->vbin = b;
if(!camera->setbin) return CC_RESULT_FAIL;
if(!camera->setbin(GP->hbin, GP->vbin)){
return CC_RESULT_BADVAL;
}
}
if(!camera->getbin) return CC_RESULT_SILENCE;
int r = camera->getbin(&GP->hbin, &GP->vbin);
if(r){
if(0 == strcmp(key, CC_CMD_HBIN)) snprintf(buf, 63, "%s=%d", key, GP->hbin);
else snprintf(buf, 63, "%s=%d", key, GP->vbin);
if(val) fixima();
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
return CC_RESULT_FAIL;
}
static cc_hresult temphandler(int fd, _U_ const char *key, const char *val){
float f;
char buf[64];
int r;
if(!camera->setT) return CC_RESULT_FAIL;
if(val){
f = atof(val);
r = camera->setT((float)f);
if(!r){
LOGWARN("Can't set camera T to %.1f", f);
return CC_RESULT_FAIL;
}
LOGMSG("Set camera T to %.1f", f);
}
if(!camera->getTcold) return CC_RESULT_SILENCE;
r = camera->getTcold(&f);
if(r){
snprintf(buf, 63, CC_CMD_CAMTEMPER "=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
if(camera->getTbody){
r = camera->getTbody(&f);
if(r){
snprintf(buf, 63, "tbody=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
}
if(camera->getThot){
r = camera->getThot(&f);
if(r){
snprintf(buf, 63, "thot=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
}
return CC_RESULT_SILENCE;
}else return CC_RESULT_FAIL;
}
static cc_hresult camfanhandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(!camera->setfanspeed) return CC_RESULT_FAIL;
if(val){
int spd = atoi(val);
if(spd < 0) return CC_RESULT_BADVAL;
if(spd > FAN_HIGH) spd = FAN_HIGH;
int r = camera->setfanspeed((cc_fan_speed)spd);
if(!r) return CC_RESULT_FAIL;
camfanspd = spd;
}
snprintf(buf, 63, CC_CMD_CAMFANSPD "=%d", camfanspd);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
const char *shutterstr[] = {"open", "close", "expose @high", "expose @low"};
static cc_hresult shutterhandler(_U_ int fd, _U_ const char *key, const char *val){
if(!camera->shuttercmd) return CC_RESULT_FAIL;
if(val){
int x = atoi(val);
if(x < 0 || x >= SHUTTER_AMOUNT) return CC_RESULT_BADVAL;
int r = camera->shuttercmd((cc_shutter_op)x);
if(r){
LOGMSG("Shutter command '%s'", shutterstr[x]);
}else{
LOGWARN("Can't run shutter command '%s'", shutterstr[x]);
return CC_RESULT_FAIL;
}
}
return CC_RESULT_OK;
}
static cc_hresult confiohandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(!camera->confio) return CC_RESULT_FAIL;
if(val){
int io = atoi(val);
int r = camera->confio(io);
if(!r) return CC_RESULT_FAIL;
confio = io;
}
snprintf(buf, 63, CC_CMD_CONFIO "=%d", confio);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult iohandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
int io;
if(!camera->setio) return CC_RESULT_FAIL;
if(val){
io = atoi(val);
int r = camera->setio(io);
if(!r) return CC_RESULT_FAIL;
}
int r = camera->getio(&io);
if(!r) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_IO "=%d", io);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult gainhandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
float f;
if(!camera->setgain) return CC_RESULT_FAIL;
if(val){
f = atof(val);
int r = camera->setgain(f);
if(!r) return CC_RESULT_FAIL;
}
if(!camera->getgain) return CC_RESULT_SILENCE;
int r = camera->getgain(&f);
if(!r) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_GAIN "=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult brightnesshandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
float b;
if(!camera->setbrightness) return CC_RESULT_FAIL;
if(val){
b = atof(val);
int r = camera->setbrightness(b);
if(!r) return CC_RESULT_FAIL;
}
if(!camera->getbrightness) return CC_RESULT_SILENCE;
int r = camera->getbrightness(&b);
if(!r) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_BRIGHTNESS "=%.1f", b);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// set format: `format=X0,X1,Y0,Y1`
// get geomlimits: `maxformat=X0,X1,Y0,Y1`
static cc_hresult formathandler(int fd, const char *key, const char *val){
char buf[64];
cc_frameformat fmt;
DBG("key=%s, val=%s", key, val);
if(val){
if(!camera->setgeometry) return CC_RESULT_FAIL;
if(0 == strcmp(key, CC_CMD_FRAMEMAX)){
DBG("CANT SET MAXFORMAT");
return CC_RESULT_BADKEY; // can't set maxformat
}
if(4 != sscanf(val, "%d,%d,%d,%d", &fmt.xoff, &fmt.yoff, &fmt.w, &fmt.h)){
DBG("Wrong format %s", val);
return CC_RESULT_BADVAL;
}
fmt.w -= fmt.xoff; fmt.h -= fmt.yoff;
int r = camera->setgeometry(&fmt);
if(!r) return CC_RESULT_FAIL;
curformat = fmt;
DBG("curformat: w=%d, h=%d", curformat.w, curformat.h);
fixima();
}
if(0 == strcmp(key, CC_CMD_FRAMEMAX)) snprintf(buf, 63, CC_CMD_FRAMEMAX "=%d,%d,%d,%d",
frmformatmax.xoff, frmformatmax.yoff, frmformatmax.xoff+frmformatmax.w, frmformatmax.yoff+frmformatmax.h);
else snprintf(buf, 63, CC_CMD_FRAMEFORMAT "=%d,%d,%d,%d",
camera->geometry.xoff, camera->geometry.yoff, camera->geometry.xoff+camera->geometry.w, camera->geometry.yoff+camera->geometry.h);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult nflusheshandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(!camera->setnflushes) return CC_RESULT_FAIL;
if(val){
int n = atoi(val);
if(n < 1) return CC_RESULT_BADVAL;
if(!camera->setnflushes(n)){
return CC_RESULT_FAIL;
}
nflushes = n;
}
snprintf(buf, 63, CC_CMD_NFLUSHES "=%d", nflushes);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult expstatehandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(val){
int n = atoi(val);
if(n == CAMERA_IDLE){ // cancel expositions
camflags |= FLAG_CANCEL;
return CC_RESULT_OK;
}
else if(n == CAMERA_CAPTURE){ // start exposition
if(GP->exptime < 1e-9){
snprintf(buf, 63, CC_CMD_EXPOSITION "=%g", GP->exptime);
cc_sendstrmessage(fd, buf);
return CC_RESULT_FAIL;
}
TIMESTAMP("Get FLAG_STARTCAPTURE");
TIMEINIT();
camflags |= FLAG_STARTCAPTURE;
return CC_RESULT_OK;
}
else return CC_RESULT_BADVAL;
}
snprintf(buf, 63, CC_CMD_EXPSTATE "=%d", camstate);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
snprintf(buf, 63, "camflags=%d", camflags);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult tremainhandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
snprintf(buf, 63, CC_CMD_TREMAIN "=%g", tremain);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult _8bithandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(!camera->setbitdepth) return CC_RESULT_FAIL;
if(val){
int s = atoi(val);
if(s != 0 && s != 1) return CC_RESULT_BADVAL;
if(!camera->setbitdepth(!s)) return CC_RESULT_FAIL;
fixima();
GP->_8bit = s;
}
snprintf(buf, 63, CC_CMD_8BIT "=%d", GP->_8bit);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult fastspdhandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(!camera->setfastspeed) return CC_RESULT_FAIL;
if(val){
int b = atoi(val);
if(b != 0 && b != 1) return CC_RESULT_BADVAL;
GP->fast = b;
if(!camera->setfastspeed(b)) return CC_RESULT_FAIL;
}
snprintf(buf, 63, CC_CMD_FASTSPD "=%d", GP->fast);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult darkhandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(!camera->setframetype) return CC_RESULT_FAIL;
if(val){
int d = atoi(val);
if(d != 0 && d != 1) return CC_RESULT_BADVAL;
GP->dark = d;
d = !d;
if(!camera->setframetype(d)) return CC_RESULT_FAIL;
}
snprintf(buf, 63, CC_CMD_DARK "=%d", GP->dark);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult FITSparhandler(int fd, const char *key, const char *val){
char buf[256], **fitskey = NULL;
if(0 == strcmp(key, CC_CMD_AUTHOR)){
fitskey = &GP->author;
}else if(0 == strcmp(key, CC_CMD_INSTRUMENT)){
fitskey = &GP->instrument;
}else if(0 == strcmp(key, CC_CMD_OBSERVER)){
fitskey = &GP->observers;
}else if(0 == strcmp(key, CC_CMD_OBJECT)){
fitskey = &GP->objname;
}else if(0 == strcmp(key, CC_CMD_PROGRAM)){
fitskey = &GP->prog_id;
}else if(0 == strcmp(key, CC_CMD_OBJTYPE)){
fitskey = &GP->objtype;
}else return CC_RESULT_BADKEY;
if(val){
FREE(*fitskey);
*fitskey = strdup(val);
}
snprintf(buf, 255, "%s=%s", key, *fitskey);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult FITSheaderhandler(int fd, _U_ const char *key, const char *val){
char buf[BUFSIZ], **sptr;
static char *curhdr = NULL;
static int firstrun = 1;
if(val){
int sz = 10, amount = 0;
// first we should check `val`
char b2[BUFSIZ], *bptr = buf;
snprintf(b2, BUFSIZ-1, "%s", val);
char **list = MALLOC(char*, sz), **lptr = list;
int L = BUFSIZ;
for(char *s = b2; ; s = NULL){
char *tok = strtok(s, ",;");
if(!tok){
*lptr = NULL;
break;
}
// check path
char *newpath = makeabspath(tok, TRUE);
DBG("next token: %s, path: %s", tok, newpath);
if(!newpath){ // error! Free list and return err
DBG("No such file");
sptr = list;
while(*sptr){
FREE(*sptr++);
}
FREE(list);
return CC_RESULT_BADVAL;
}
*lptr++ = strdup(newpath);
if(++amount == sz){
DBG("Realloc");
sz += 10;
list = realloc(list, sz*sizeof(char*));
bzero(&list[sz-10], 10*sizeof(char*));
}
int N = snprintf(bptr, L-1, "%s,", newpath);
bptr += N; L -= N;
}
// free old list and change its value
if(GP->addhdr){
DBG("Free old list");
sptr = GP->addhdr;
while(*sptr){
DBG("Free %s", *sptr);
free(*(sptr++));
}
}
GP->addhdr = list;
FREE(curhdr);
if(*val && *val != ',') curhdr = strdup(buf); // command with empty arg will clear curhdr
DBG("curhdr now: %s", curhdr);
}
if(!curhdr && firstrun){
firstrun = 0;
if(GP->addhdr && *GP->addhdr){
char *ptr = buf;
int L = BUFSIZ;
sptr = GP->addhdr;
while(*sptr){
DBG("Add to curhdr: %s", *sptr);
int N = snprintf(ptr, L-1, "%s,", *sptr++);
L -= N; ptr += N;
}
curhdr = strdup(buf);
}
}
snprintf(buf, BUFSIZ-1, CC_CMD_HEADERFILES "=%s", curhdr);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
/*
static cc_hresult handler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
return CC_RESULT_SILENCE;
}
*/
/*******************************************************************************
***************************** cc_Wheel handlers **********************************
******************************************************************************/
static cc_hresult wlisthandler(int fd, _U_ const char *key, _U_ const char *val){
if(wheel->Ndevices < 1) return CC_RESULT_FAIL;
for(int i = 0; i < wheel->Ndevices; ++i){
if(!wheel->setDevNo(i)) continue;
char modname[256], buf[BUFSIZ];
wheel->getModelName(modname, 255);
snprintf(buf, BUFSIZ-1, CC_CMD_WLIST "='%s'", modname);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(wheeldevno > -1) wheel->setDevNo(wheeldevno);
return CC_RESULT_SILENCE;
}
static cc_hresult wsetNhandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(val){
int num = atoi(val);
if(num > wheel->Ndevices - 1 || num < 0){
return CC_RESULT_BADVAL;
}
if(!wheeldevini(num)) return CC_RESULT_FAIL;
}
snprintf(buf, 63, CC_CMD_WDEVNO "=%d", wheeldevno);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult wgotohandler(_U_ int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
int pos;
if(val){
pos = atoi(val);
DBG("USER wants to %d", pos);
int r = wheel->setPos(pos);
DBG("wheel->setPos(%d)", pos);
if(!r) return CC_RESULT_BADVAL;
}
int r = wheel->getPos(&pos);
if(!r) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_WPOS "=%d", pos);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
/*******************************************************************************
**************************** Focuser handlers *********************************
******************************************************************************/
static cc_hresult foclisthandler(int fd, _U_ const char *key, _U_ const char *val){
if(focuser->Ndevices < 1) return CC_RESULT_FAIL;
for(int i = 0; i < focuser->Ndevices; ++i){
char modname[256], buf[BUFSIZ];
if(!focuser->setDevNo(i)) continue;
focuser->getModelName(modname, 255);
snprintf(buf, BUFSIZ-1, CC_CMD_FOCLIST "='%s'", modname);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(focdevno > -1) focuser->setDevNo(focdevno);
return CC_RESULT_SILENCE;
}
static cc_hresult fsetNhandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(val){
int num = atoi(val);
if(num > focuser->Ndevices - 1 || num < 0){
return CC_RESULT_BADVAL;
}
if(!focdevini(num)) return CC_RESULT_FAIL;
}
snprintf(buf, 63, CC_CMD_FDEVNO "=%d", focdevno);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
static cc_hresult fgotohandler(int fd, _U_ const char *key, const char *val){
char buf[64];
float f;
int r;
if(val){
f = atof(val);
if(f < focminpos || f > focmaxpos) return CC_RESULT_BADVAL;
if(f - focminpos < __FLT_EPSILON__){
r = focuser->home(1);
}else{
r = focuser->setAbsPos(1, f);
}
if(!r) return CC_RESULT_FAIL;
}
r = focuser->getPos(&f);
if(!r) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_FGOTO "=%g", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
/*******************************************************************************
**************************** Common handlers **********************************
******************************************************************************/
// information about everything
static cc_hresult infohandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[BUFSIZ], buf1[256];
float f;
int i;
if(camera){
if(camera->getModelName && camera->getModelName(buf1, 255)){
snprintf(buf, BUFSIZ-1, CC_CMD_CAMLIST "='%s'", buf1);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
#define RUN(f, arg) do{if(CC_RESULT_DISCONNECTED == f(fd, arg, NULL)) return CC_RESULT_DISCONNECTED;}while(0)
RUN(namehandler, CC_CMD_FILENAME);
RUN(binhandler, CC_CMD_HBIN);
RUN(binhandler, CC_CMD_VBIN);
RUN(temphandler, CC_CMD_CAMTEMPER);
RUN(exphandler, CC_CMD_EXPOSITION);
RUN(lastfnamehandler, CC_CMD_LASTFNAME);
RUN(expstatehandler, CC_CMD_EXPSTATE);
#undef RUN
}
if(wheel){
DBG("chk wheel");
if(wheel->getModelName(buf1, 255)){
snprintf(buf, BUFSIZ-1, CC_CMD_WLIST "='%s'", buf1);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(wheel->getTbody(&f)){
snprintf(buf, BUFSIZ-1, "wtemp=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(wheel->getPos(&i)){
snprintf(buf, BUFSIZ-1, CC_CMD_WPOS "=%d", i);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
snprintf(buf, BUFSIZ-1, "wmaxpos=%d", wmaxpos);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(focuser){
DBG("Chk focuser");
if(focuser->getModelName(buf1, 255)){
snprintf(buf, BUFSIZ-1, CC_CMD_FOCLIST "='%s'", buf1);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
if(focuser->getTbody(&f)){
snprintf(buf, BUFSIZ-1, "foctemp=%.1f", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
snprintf(buf, BUFSIZ-1, "focminpos=%g", focminpos);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
snprintf(buf, BUFSIZ-1, "focmaxpos=%g", focmaxpos);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
if(focuser->getPos(&f)){
snprintf(buf, BUFSIZ-1, CC_CMD_FGOTO "=%g", f);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
}
}
DBG("EOF");
return CC_RESULT_SILENCE;
}
// show help
static cc_hresult helphandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[256];
strpair *ptr = allcommands;
while(ptr->key){
snprintf(buf, 255, "%s - %s", ptr->key, ptr->help);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
++ptr;
}
return CC_RESULT_SILENCE;
}
// shared memory key
static cc_hresult shmemkeyhandler(int fd, _U_ const char *key, _U_ const char *val){
char buf[64];
if(shmkey == IPC_PRIVATE) return CC_RESULT_FAIL;
snprintf(buf, 63, CC_CMD_SHMEMKEY "=%d", shmkey);
if(!cc_sendstrmessage(fd, buf)) return CC_RESULT_DISCONNECTED;
return CC_RESULT_SILENCE;
}
// infinity loop
static cc_hresult inftyhandler(int fd, _U_ const char *key, const char *val){
char buf[64];
if(val){
int i = atoi(val);
infty = (i) ? 1 : 0;
if(!infty) camflags |= FLAG_CANCEL;
}