forked from g0orx/linhpsdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrigctl.c
4092 lines (3986 loc) · 112 KB
/
rigctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* TS-2000 emulation via TCP
* Copyright (C) 2016 Steve Wilson <[email protected]>
* 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 2 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/>.
*/
/*
* PiHPSDR RigCtl by Steve KA6S Oct 16 2016
* With a kindly assist from Jae, K5JAE who has helped
* greatly with hamlib integration!
*/
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <math.h>
#include <wdsp.h>
#include "receiver.h"
//#include "band_menu.h"
#include "rigctl.h"
#include "discovered.h"
#include "adc.h"
#include "dac.h"
#include "transmitter.h"
#include "receiver.h"
#include "wideband.h"
#include "radio.h"
#include "channel.h"
#include "mode.h"
#include "filter.h"
#include "band.h"
#include "bandstack.h"
//#include "filter_menu.h"
#include "vfo.h"
//#include "sliders.h"
#include "transmitter.h"
#include "agc.h"
//#include "store.h"
#include "ext.h"
//#include "rigctl_menu.h"
//#include "noise_menu.h"
#include "protocol1.h"
#ifdef LOCALCW
#include "iambic.h" // declare keyer_update()
#endif
#include "main.h"
#include "subrx.h"
#define NEW_PARSER
int rigctl_port_base=19090;
int rigctl_enable=0;
#define RIGCTL_THROTTLE_NSEC 15000000L
#define NSEC_PER_SEC 1000000000L
// max number of bytes we can get at once
#define MAXDATASIZE 2000
int parse_cmd (void *data);
int connect_cnt = 0;
int rigctlGetFilterLow();
int rigctlGetFilterHigh();
int new_level;
int active_transmitter = 0;
int rigctl_busy = 0; // Used to tell rigctl_menu that launch has already occured
int cat_control;
extern int enable_tx_equalizer;
typedef struct _rigctl {
GMutex mutex;
gint listening_port;
gboolean socket_listening;
GThread *server_thread_id;
gint server_socket;
gint server_address_length;
struct sockaddr_in server_address;
socklen_t address_length;
struct sockaddr_in address;
int socket_fd;
gboolean socket_running;
char ser_port[64];
int serial_baudrate;
int serial_parity;
int serial_fd;
gboolean serial_running;
gboolean debug;
} RIGCTL;
FILE * out;
int output;
FILTER * band_filter;
static GThread *serial_server_thread_id = NULL;
static int server_socket=-1;
static int server_address_length;
static struct sockaddr_in server_address;
static int rigctl_timer = 0;
typedef struct _command {
RECEIVER *rx;
char *command;
int fd;
} COMMAND;
/*
static CLIENT client[MAX_CLIENTS];
*/
int squelch=-160; //local sim of squelch level
int fine = 0; // FINE status for TS-2000 decides whether rit_increment is 1Hz/10Hz.
int read_size;
int freq_flag; // Determines if we are in the middle of receiving frequency info
int digl_offset = 0;
int digl_pol = 0;
int digu_offset = 0;
int digu_pol = 0;
double new_vol = 0;
int lcl_cmd=0;
long long new_freqA = 0;
long long new_freqB = 0;
long long orig_freqA = 0;
long long orig_freqB = 0;
int lcl_split = 0;
int mox_state = 0;
// Radio functions -
// Memory channel stuff and things that aren't
// implemented - but here for a more complete emulation
int ctcss_tone; // Numbers 01-38 are legal values - set by CN command, read by CT command
int ctcss_mode; // Numbers 0/1 - on off.
static void rigctl_client(RECEIVER *rx);
static int step_size(RECEIVER *rx) {
int i=0;
for(i=0;i<=14;i++) {
if(steps[i]==rx->step) break;
}
if(i>14) i=0;
return i+1;
}
static int cat_band(RECEIVER *rx) {
int b;
switch(rx->band_a) {
case band2200:
b=888; //2200;
break;
case band630:
b=888; //630;
break;
case band160:
b=160;
break;
case band80:
b=80;
break;
case band60:
b=60;
break;
case band40:
b=40;
break;
case band30:
b=30;
break;
case band20:
b=20;
break;
case band17:
b=17;
break;
case band15:
b=15;
break;
case band12:
b=12;
break;
case band10:
b=10;
break;
case band6:
b=6;
break;
case bandGen:
b=888;
break;
case bandWWV:
b=999;
break;
default:
b=20;
break;
}
return b;
}
static double s_meter_level(RECEIVER *rx) {
double attenuation = radio->adc[rx->adc].attenuation;
if(radio->discovered->device==DEVICE_HERMES_LITE2) {
attenuation = attenuation * -1;
}
double level=rx->meter_db+attenuation;
return level;
}
void disable_rigctl(RECEIVER *rx) {
int i;
struct linger linger = { 0 };
linger.l_onoff = 1;
linger.l_linger = 0;
RIGCTL *rigctl=(RIGCTL *)rx->rigctl;
g_print("%s: server_socket=%d\n",__FUNCTION__,rigctl->server_socket);
rigctl->socket_running=FALSE;
if(setsockopt(rigctl->socket_fd,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for client");
}
g_print("closing client socket: %d\n",rigctl->socket_fd);
close(rigctl->socket_fd);
rigctl->socket_fd=-1;
if(rigctl->server_socket>=0) {
if(setsockopt(rigctl->server_socket,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for server");
}
close(rigctl->server_socket);
rigctl->server_socket=-1;
}
}
int vfo_sm=0; // VFO State Machine - this keeps track of
//#define RIGCTL_CW
#ifdef RIGCTL_CW
//
// CW sending stuff
//
static char cw_buf[30];
static int cw_busy=0;
static int cat_cw_seen=0;
static int dotlen;
static int dashlen;
static int dotsamples;
static int dashsamples;
//
// send_dash() send a "key-down" of a dashlen, followed by a "key-up" of a dotlen
// send_dot() send a "key-down" of a dotlen, followed by a "key-up" of a dotlen
// send_space(int len) send a "key_down" of zero, followed by a "key-up" of len*dotlen
//
// The "trick" to get proper timing is, that we really specify the number of samples
// for the next element (dash/dot/nothing) and the following pause. 30 wpm is no
// problem, and without too much "busy waiting". We just take a nap until 10 msec
// before we have to act, and then wait several times for 1 msec until we can shoot.
//
void send_dash() {
int TimeToGo;
for(;;) {
TimeToGo=cw_key_up+cw_key_down;
// TimeToGo is invalid if local CW keying has set in
if (cw_key_hit || cw_not_ready) return;
if (TimeToGo == 0) break;
// sleep until 10 msec before ignition
if (TimeToGo > 500) usleep((long)(TimeToGo-500)*20L);
// sleep 1 msec
usleep(1000L);
}
// If local CW keying has set in, do not interfere
if (cw_key_hit || cw_not_ready) return;
cw_key_down = dashsamples;
cw_key_up = dotsamples;
}
void send_dot() {
int TimeToGo;
for(;;) {
TimeToGo=cw_key_up+cw_key_down;
// TimeToGo is invalid if local CW keying has set in
if (cw_key_hit || cw_not_ready) return;
if (TimeToGo == 0) break;
// sleep until 10 msec before ignition
if (TimeToGo > 500) usleep((long)(TimeToGo-500)*20L);
// sleep 1 msec
usleep(1000L);
}
// If local CW keying has set in, do not interfere
if (cw_key_hit || cw_not_ready) return;
cw_key_down = dotsamples;
cw_key_up = dotsamples;
}
void send_space(int len) {
int TimeToGo;
for(;;) {
TimeToGo=cw_key_up+cw_key_down;
// TimeToGo is invalid if local CW keying has set in
if (cw_key_hit || cw_not_ready) return;
if (TimeToGo == 0) break;
// sleep until 10 msec before ignition
if (TimeToGo > 500) usleep((long)(TimeToGo-500)*20L);
// sleep 1 msec
usleep(1000L);
}
// If local CW keying has set in, do not interfere
if (cw_key_hit || cw_not_ready) return;
cw_key_up = len*dotsamples;
}
void rigctl_send_cw_char(char cw_char) {
char pattern[9],*ptr;
strcpy(pattern,"");
ptr = &pattern[0];
switch (cw_char) {
case 'a':
case 'A': strcpy(pattern,".-"); break;
case 'b':
case 'B': strcpy(pattern,"-..."); break;
case 'c':
case 'C': strcpy(pattern,"-.-."); break;
case 'd':
case 'D': strcpy(pattern,"-.."); break;
case 'e':
case 'E': strcpy(pattern,"."); break;
case 'f':
case 'F': strcpy(pattern,"..-."); break;
case 'g':
case 'G': strcpy(pattern,"--."); break;
case 'h':
case 'H': strcpy(pattern,"...."); break;
case 'i':
case 'I': strcpy(pattern,".."); break;
case 'j':
case 'J': strcpy(pattern,".---"); break;
case 'k':
case 'K': strcpy(pattern,"-.-"); break;
case 'l':
case 'L': strcpy(pattern,".-.."); break;
case 'm':
case 'M': strcpy(pattern,"--"); break;
case 'n':
case 'N': strcpy(pattern,"-."); break;
case 'o':
case 'O': strcpy(pattern,"---"); break;
case 'p':
case 'P': strcpy(pattern,".--."); break;
case 'q':
case 'Q': strcpy(pattern,"--.-"); break;
case 'r':
case 'R': strcpy(pattern,".-."); break;
case 's':
case 'S': strcpy(pattern,"..."); break;
case 't':
case 'T': strcpy(pattern,"-"); break;
case 'u':
case 'U': strcpy(pattern,"..-"); break;
case 'v':
case 'V': strcpy(pattern,"...-"); break;
case 'w':
case 'W': strcpy(pattern,".--"); break;
case 'x':
case 'X': strcpy(pattern,"-..-"); break;
case 'y':
case 'Y': strcpy(pattern,"-.--"); break;
case 'z':
case 'Z': strcpy(pattern,"--.."); break;
case '0': strcpy(pattern,"-----"); break;
case '1': strcpy(pattern,".----"); break;
case '2': strcpy(pattern,"..---"); break;
case '3': strcpy(pattern,"...--"); break;
case '4': strcpy(pattern,"....-"); break;
case '5': strcpy(pattern,".....");break;
case '6': strcpy(pattern,"-....");break;
case '7': strcpy(pattern,"--...");break;
case '8': strcpy(pattern,"---..");break;
case '9': strcpy(pattern,"----.");break;
//
// DL1YCF:
// There were some signs I considered wrong, other
// signs missing. Therefore I put the signs here
// from ITU Recommendation M.1677-1 (2009)
// in the order given there.
//
case '.': strcpy(pattern,".-.-.-"); break;
case ',': strcpy(pattern,"--..--"); break;
case ':': strcpy(pattern,"---.."); break;
case '?': strcpy(pattern,"..--.."); break;
case '\'': strcpy(pattern,".----."); break;
case '-': strcpy(pattern,"-....-"); break;
case '/': strcpy(pattern,"-..-."); break;
case '(': strcpy(pattern,"-.--."); break;
case ')': strcpy(pattern,"-.--.-"); break;
case '"': strcpy(pattern,".-..-."); break;
case '=': strcpy(pattern,"-...-"); break;
case '+': strcpy(pattern,".-.-."); break;
case '@': strcpy(pattern,".--.-."); break;
//
// Often used, but not ITU: Ampersand for "wait"
//
case '&': strcpy(pattern,".-...");break;
default: strcpy(pattern,"");
}
while(*ptr != '\0') {
if(*ptr == '-') {
send_dash();
}
if(*ptr == '.') {
send_dot();
}
ptr++;
}
// The last element (dash or dot) sent already has one dotlen space appended.
// If the current character is another "printable" sign, we need an additional
// pause of 2 dotlens to form the inter-character spacing of 3 dotlens.
// However if the current character is a "space" we must produce an inter-word
// spacing (7 dotlens) and therefore need 6 additional dotlens
// We need no longer take care of a sequence of spaces since adjacent spaces
// are now filtered out while filling the CW character (ring-) buffer.
if (cw_char == ' ') {
send_space(6); // produce inter-word space of 7 dotlens
} else {
send_space(2); // produce inter-character space of 3 dotlens
}
}
//
// This thread constantly looks whether CW data
// is available, and produces CW in this case.
//
// A ring buffer is maintained such that the contents
// of several KY commands can be buffered. This allows
// sending a large CW text word-by-word (each word in a
// separate KY command).
//
// If the contents of the last KY command do not fit into
// the ring buffer, cw_busy is NOT reset. Eventually, there
// is enough space in the ring buffer, then cw_busy is reset.
//
static gpointer rigctl_cw_thread(gpointer data)
{
int i;
char c;
char last_char=0;
char ring_buf[130];
char *write_buf=ring_buf;
char *read_buf =ring_buf;
char *p;
int num_buf=0;
while (server_running) {
// wait for CW data (periodically look every 100 msec)
if (!cw_busy && num_buf ==0) {
cw_key_hit=0;
usleep(100000L);
continue;
}
// if new data is available and fits into the buffer, copy-in.
// If there are several adjacent spaces, take only the first one.
// This also swallows the "tails" of the KY commands which
// (according to Kenwood) have to be padded with spaces up
// to the maximum length (24)
if (cw_busy && num_buf < 100) {
p=cw_buf;
while ((c=*p++)) {
if (last_char == ' ' && c == ' ') continue;
*write_buf++ = c;
last_char=c;
num_buf++;
if (write_buf - ring_buf == 128) write_buf=ring_buf; // wrap around
}
cw_busy=0; // mark one-line buffer free again
}
// This may happen if cw_buf was empty or contained only blanks
if (num_buf == 0) continue;
// these values may have changed, so recompute them here
// This means that we can change the speed (KS command) while
// the buffer is being sent
dotlen = 1200000L/(long)cw_keyer_speed;
dashlen = (dotlen * 3 * cw_keyer_weight) / 50L;
dotsamples = 57600 / cw_keyer_speed;
dashsamples = (3456 * cw_keyer_weight) / cw_keyer_speed;
CAT_cw_is_active=1;
if (!mox) {
// activate PTT
g_idle_add(ext_mox_update ,(gpointer)1);
// have to wait until it is really there
// Note that if out-of-band, we would wait
// forever here, so allow at most 200 msec
// We also have to wait for cw_not_ready becoming zero
i=200;
while ((!mox || cw_not_ready) && i-- > 0) usleep(1000L);
// still no MOX? --> silently discard CW character and give up
if (!mox) {
CAT_cw_is_active=0;
continue;
}
}
// At this point, mox==1 and CAT_cw_active == 1
if (cw_key_hit || cw_not_ready) {
//
// CW transmission has been aborted, either due to manually
// removing MOX, changing the mode to non-CW, or because a CW key has been hit.
// Do not remove PTT in the latter case
CAT_cw_is_active=0;
// If a CW key has been hit, we continue in TX mode.
// Otherwise, switch PTT off.
if (!cw_key_hit && mox) {
g_idle_add(ext_mox_update ,(gpointer)0);
}
// Let the CAT system swallow incoming CW commands by setting cw_busy to -1.
// Do so until no CAT CW message has arrived for 1 second
cw_busy=-1;
for (;;) {
cat_cw_seen=0;
usleep(1000000L);
if (cat_cw_seen) continue;
cw_busy=0;
break;
}
write_buf=read_buf=ring_buf;
num_buf=0;
} else {
rigctl_send_cw_char(*read_buf++);
if (read_buf - ring_buf == 128) read_buf=ring_buf; // wrap around
num_buf--;
//
// Character has been sent.
// If there are more to send, or the next message is pending, continue.
// Otherwise remove PTT and wait for next CAT CW command.
if (cw_busy || num_buf > 0) continue;
CAT_cw_is_active=0;
if (!cw_key_hit) {
g_idle_add(ext_mox_update ,(gpointer)0);
// wait up to 500 msec for MOX having gone
// otherwise there might be a race condition when sending
// the next character really soon
i=10;
while (mox && (i--) > 0) usleep(50000L);
}
}
// end of while (server_running)
}
// We arrive here if the rigctl server shuts down.
// This very rarely happens. But we should shut down the
// local CW system gracefully, in case we were in the mid
// of a transmission
rigctl_cw_thread_id = NULL;
cw_busy=0;
if (CAT_cw_is_active) {
CAT_cw_is_active=0;
g_idle_add(ext_mox_update ,(gpointer)0);
}
return NULL;
}
#endif
// Looks up entry INDEX_NUM in the command structure and
// returns the command string
//
void send_resp(COMMAND *cmd,char * msg) {
RECEIVER *rx=cmd->rx;
RIGCTL *rigctl=rx->rigctl;
if(rigctl->debug) g_print("%s: fd=%d RESP=%s\n",__FUNCTION__,cmd->fd,msg);
int length=strlen(msg);
int written=0;
while(written<length) {
written+=write(cmd->fd,&msg[written],length-written);
}
}
//
// only one connection via TCP/IP
//
static gpointer rigctl_server(gpointer data) {
RECEIVER *rx=(RECEIVER *)data;
RIGCTL *rigctl=(RIGCTL *)rx->rigctl;
int on=1;
int i;
g_print("%s: listening on port %d\n",__FUNCTION__,rigctl->listening_port);
rigctl->server_socket=socket(AF_INET,SOCK_STREAM,0);
if(rigctl->server_socket<0) {
perror("rigctl_server: listen socket failed");
return NULL;
}
setsockopt(rigctl->server_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(rigctl->server_socket, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
// bind to listening port
memset(&rigctl->server_address,0,sizeof(rigctl->server_address));
rigctl->server_address_length=sizeof(rigctl->server_address);
rigctl->server_address.sin_family=AF_INET;
rigctl->server_address.sin_addr.s_addr=INADDR_ANY;
rigctl->server_address.sin_port=htons(rigctl->listening_port);
if(bind(rigctl->server_socket,(struct sockaddr*)&rigctl->server_address,sizeof(rigctl->server_address))<0) {
perror("rigctl_server: listen socket bind failed");
close(rigctl->server_socket);
return NULL;
}
memset(&rigctl->address,0,sizeof(rigctl->address));
rigctl->address_length=sizeof(rigctl->address);
// must start the thread here in order NOT to inherit a lock
//if (!rigctl_cw_thread_id) rigctl_cw_thread_id = g_thread_new("RIGCTL cw", rigctl_cw_thread, NULL);
rigctl->socket_listening=TRUE;
while(rigctl->socket_listening) {
if(listen(rigctl->server_socket,1)<0) {
perror("rigctl_server: listen failed");
close(server_socket);
return NULL;
}
g_print("%s: accept connection\n",__FUNCTION__);
rigctl->socket_fd=accept(rigctl->server_socket,(struct sockaddr*)&rigctl->address,&rigctl->address_length);
if(rigctl->socket_fd<0) {
perror("rigctl_server: client accept failed");
continue;
}
#ifdef __APPLE__
if(setsockopt(rigctl->socket_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on))<0) {
#else
if(setsockopt(rigctl->socket_fd, SOL_TCP, TCP_NODELAY, (void *)&on, sizeof(on))<0) {
#endif
perror("TCP_NODELAY");
}
// no longer a separate thread as only one client per receiver
rigctl_client(rx);
g_print("%s: setting SO_LINGER to 0 for client_socket: %d\n",__FUNCTION__,rigctl->socket_fd);
struct linger linger = { 0 };
linger.l_onoff = 1;
linger.l_linger = 0;
if(setsockopt(rigctl->socket_fd,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for client");
}
close(rigctl->socket_fd);
}
close(server_socket);
return NULL;
}
static void rigctl_client(RECEIVER *rx) {
RIGCTL *rigctl=(RIGCTL *)rx->rigctl;
int i;
int numbytes;
char cmd_input[MAXDATASIZE] ;
char *command=g_new(char,MAXDATASIZE);
int command_index=0;
g_print("%s: starting rigctl_client: socket=%d\n",__FUNCTION__,rigctl->socket_fd);
rigctl->socket_running=TRUE;
while(rigctl->socket_running && (numbytes=recv(rigctl->socket_fd , cmd_input , MAXDATASIZE-2 , 0)) > 0 ) {
for(i=0;i<numbytes;i++) {
command[command_index]=cmd_input[i];
command_index++;
if(cmd_input[i]==';') {
command[command_index]='\0';
COMMAND *cmd=g_new(COMMAND,1);
cmd->rx=rx;
cmd->command=command;
cmd->fd=rigctl->socket_fd;
g_mutex_lock(&rigctl->mutex);
g_idle_add(parse_cmd,cmd);
g_mutex_unlock(&rigctl->mutex);
command=g_new(char,MAXDATASIZE);
command_index=0;
}
}
}
perror("recv");
g_print("%s: running=%d numbytes=%d\n",__FUNCTION__,rigctl->socket_running,numbytes);
}
static int ts2000_mode(int m) {
int mode=1;
switch(m) {
case LSB:
mode=1;
break;
case USB:
mode=2;
break;
case CWL:
mode=7;
break;
case CWU:
mode=3;
break;
case FMN:
mode=4;
break;
case AM:
case SAM:
mode=5;
break;
case DIGL:
mode=6;
break;
case DIGU:
mode=9;
break;
default:
break;
}
return mode;
}
gboolean parse_extended_cmd(COMMAND *cmd) {
RECEIVER *rx=cmd->rx;
RIGCTL *rigctl=rx->rigctl;
char *command=cmd->command;
gboolean implemented=TRUE;
char reply[256];
reply[0]='\0';
switch(command[2]) {
case 'A': //ZZAx
switch(command[3]) {
case 'A': //ZZAA
implemented=FALSE;
break;
case 'B': //ZZAB
implemented=FALSE;
break;
case 'C': //ZZAC
// sets or reads the Step Size
if(command[4]==';') {
sprintf(reply,"ZZAC%02d;",step_size(rx));
send_resp(cmd,reply) ;
} else if(command[6]==';') {
// set the step size
int i=atoi(&command[4]) ;
if(i>0 && i<=14) {
rx->step=steps[i+1];
update_vfo(rx);
}
} else {
}
break;
case 'D': //ZZAD
// move VFO A down by selected step
if(command[6]==';') {
int step_index=atoi(&command[4]);
long long hz=0;
if(step_index>0 && step_index<=14) {
hz=(long long)steps[step_index-1];
}
if(hz!=0LL) {
receiver_move(rx,-hz,FALSE);
}
} else {
}
break;
case 'E': //ZZAE
// move VFO A down nn tune steps
if(command[6]==';') {
int steps=-atoi(&command[4]);
receiver_move(rx,rx->step*steps,TRUE);
}
break;
case 'F': //ZZAF
// move VFO A up nn tune steps
if(command[6]==';') {
int steps=atoi(&command[4]);
receiver_move(rx,rx->step*steps,TRUE);
}
break;
case 'G': //ZZAG
// read/set audio gain
if(command[4]==';') {
// send reply back
sprintf(reply,"ZZAG%03d;",(int)(rx->volume*100.0));
send_resp(cmd,reply) ;
} else {
int gain=atoi(&command[4]);
rx->volume=(double)gain/100.0;
receiver_set_volume(rx);
update_vfo(rx);
}
break;
case 'I': //ZZAI
implemented=FALSE;
break;
case 'P': //ZZAP
implemented=FALSE;
break;
case 'R': //ZZAR
// read/set RX0 AGC Threshold
if(command[4]==';') {
// send reply back
sprintf(reply,"ZZAR%+04d;",(int)(rx->agc_gain));
send_resp(cmd,reply) ;
} else {
rx->agc_gain=(double)atoi(&command[4]);
receiver_set_agc_gain(rx);
}
break;
case 'S': //ZZAS
// read/set RX1 AGC Threshold
implemented=FALSE;
break;
case 'T': //ZZAT
implemented=FALSE;
break;
case 'U': //ZZAU
// move VFO A up by selected step
if(command[6]==';') {
int step_index=atoi(&command[4]);
long long hz=0;
if(step_index>0 && step_index<=14) {
hz=(long long)steps[step_index-1];
}
if(hz!=0LL) {
receiver_move(rx,hz,TRUE);
}
} else {
}
break;
default:
implemented=FALSE;
break;
}
break;
case 'B': //ZZBx
switch(command[3]) {
case 'A': //ZZBA
// move RX2 up down one band
implemented=FALSE;
break;
case 'B': //ZZBB
// move RX2 up one band
implemented=FALSE;
break;
case 'D': //ZZBD
// move RX1 down one band
if(command[4]==';') {
int b=previous_band(rx->band_a);
set_band(rx,b,-1);
}
break;
case 'E': //ZZBE
// move VFO B down nn tune steps
if(command[6]==';') {
int steps=-atoi(&command[4]);
receiver_move(rx,rx->step*steps,TRUE);
}
break;
case 'F': //ZZBF
// move VFO B up nn tune steps
if(command[6]==';') {
int steps=atoi(&command[4]);
receiver_move(rx,rx->step*steps,TRUE);
}
break;
case 'G': //ZZBG
if(command[4]==';') {
sprintf(reply,"ZZBG%d;",0);
send_resp(cmd,reply) ;
} else {
implemented=FALSE;
}
break;
case 'I': //ZZBI
if(command[4]==';') {
sprintf(reply,"ZZBI%d;",0);
send_resp(cmd,reply) ;
} else {
implemented=FALSE;
}
break;
case 'M': //ZZBM
// move VFO B down by selected step
if(command[6]==';') {
int step_index=atoi(&command[4]);
long long hz=0;
if(step_index>0 && step_index<=14) {
hz=(long long)steps[step_index-1];
}
if(hz!=0LL) {
receiver_move_b(rx,-hz,FALSE,FALSE);
}
} else {
}
break;
case 'P': //ZZBP
// move VFO B up by selected step
if(command[6]==';') {
int step_index=atoi(&command[4]);
long long hz=0;
if(step_index>0 && step_index<=14) {
hz=(long long)steps[step_index-1];
}
if(hz!=0LL) {
receiver_move_b(rx,hz,FALSE,FALSE);
}
} else {
}
break;
case 'R': //ZZBR
if(command[4]==';') {
sprintf(reply,"ZZBR%d;",0);
send_resp(cmd,reply) ;
} else {
implemented=FALSE;
}
break;
case 'S': //ZZBS
// set/read RX1 band switch
if(command[4]==';') {
sprintf(reply,"ZZBS%03d;",cat_band(rx));
send_resp(cmd,reply) ;
} else {
implemented=FALSE;
}
break;
case 'T': //ZZBT
// set or reads RX2 Band Switch
implemented=FALSE;
break;
case 'U': //ZZBU
// moves RX1 band swithc up one band
implemented=FALSE;
break;
case 'Y': //ZZBY
// closes console
implemented=FALSE;
break;
default:
implemented=FALSE;
break;
}
break;
case 'C': //ZZCx
switch(command[3]) {
case 'B': //ZZCB
// sets/reads break in enable
implemented=FALSE;
break;
case 'D': //ZZCD
// sets/reads break delay
implemented=FALSE;
break;
case 'F': //ZZCF