forked from g0orx/linhpsdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrigctl.c
More file actions
4214 lines (4045 loc) · 243 KB
/
rigctl.c
File metadata and controls
4214 lines (4045 loc) · 243 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
/* TS-2000 emulation via TCP
* Copyright (C) 2016 Steve Wilson <wevets@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 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!
*
* Extended into LinHPSDR - tear out the radio functionality
* and call it within ext.c as an API. All radio functions should
* be accesed within ext.c
*/
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
//#include <semaphore.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// IP stuff below
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include "receiver.h"
#ifdef PIHPSDR
#include "toolbar.h"
#include "band_menu.h"
#include "sliders.h"
#include "rigctl.h"
#include "radio.h"
#include "channel.h"
#include "filter.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 <wdsp.h>
#include "store.h"
#include "ext.h"
#include "rigctl_menu.h"
#endif // PIHPSDR
#ifndef PIHPSDR
#include <wdsp.h>
#include "alex.h"
#include "button_text.h"
#include "discovered.h"
#include "mode.h"
#include "receiver.h"
#include "transmitter.h"
#include "receiver.h"
#include "wideband.h"
#include "adc.h"
#include "radio.h"
#include "protocol1.h"
#include "protocol2.h"
#include "main.h"
#include "radio_dialog.h"
#include "audio.h"
#include "property.h"
#include "ext.h"
#endif // PIHPSDR
#include <math.h>
#undef RIGCTL_DEBUG
//#define RIGCTL_DEBUG
#ifndef PIHPSDR
RADIO * local_radio;
#endif
int rigctl_port_base=19090;
int rigctl_enable=0;
// the port client will be connecting to
// 2-26-17 K5JAE - Changed the defines to const ints to allow use via pointers.
//static const int TelnetPortA = 19090;
//static const int TelnetPortB = 19091;
//static const int TelnetPortC = 19092;
#define RIGCTL_TIMER_DELAY 15000
// max number of bytes we can get at once
#define MAXDATASIZE 2000
//MOX mox_struct;
int mox = 0;
void parse_cmd ();
int cw_busy = 0; // Used to signal that the system is in a busy state for sending CW - assert busy back to the user
int cw_reset = 0; // Signals reset the transceiver
int connect_cnt = 0;
int rigctlGetFilterLow();
int rigctlGetFilterHigh();
int rigctlSetFilterLow(int val);
int rigctlSetFilterHigh(int val);
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;
//extern int serial_baud_rate;
//extern int serial_parity;
//typedef struct {GMutex m; } GT_MUTEX;
//GT_MUTEX * mutex_a;
//GT_MUTEX * mutex_b;
//GT_MUTEX * mutex_c;
//GT_MUTEX * mutex_busy;
//
////int mutex_b_exists = 0;
#ifndef PIHPSDR
char ser_port[64] = "/dev/ttyUSB0";
int serial_baud_rate = 4800;
#endif
FILE * out;
int output;
#ifdef PIHPSDR
FILTER * band_filter;
#endif // PIHPSDR
#define MAX_CLIENTS 3
//static GThread *rigctl_server_thread_id = NULL;
//static GThread *rigctl_set_timer_thread_id = NULL;
//static int server_running;
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 _client {
int socket;
int address_length;
struct sockaddr_in address;
GThread *thread_id;
} CLIENT;
int fd; // Serial port file descriptor
//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;
// 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 gpointer rigctl_client (gpointer data);
void close_rigctl_ports(RECEIVER *rx) {
int i;
struct linger linger = { 0 };
linger.l_onoff = 1;
linger.l_linger = 0;
fprintf(stderr,"close_rigctl_ports: server_socket=%d\n",rx->rigctl_server_socket);
rx->rigctl_running=FALSE;
fprintf(stderr,"setting SO_LINGER to 0 for client_socket: %d\n",rx->rigctl_client_socket);
if(setsockopt(rx->rigctl_client_socket,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for client");
}
fprintf(stderr,"closing client socket: %d\n",rx->rigctl_client_socket);
close(rx->rigctl_client_socket);
rx->rigctl_client_socket=-1;
if(rx->rigctl_server_socket>=0) {
fprintf(stderr,"setting SO_LINGER to 0 for server_socket: %d\n",rx->rigctl_server_socket);
if(setsockopt(rx->rigctl_server_socket,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for server");
}
fprintf(stderr,"closing server_socket: %d\n",rx->rigctl_server_socket);
close(rx->rigctl_server_socket);
rx->rigctl_server_socket=-1;
}
}
// RigCtl Timer - to throttle passes through the parser...
// Sets rigctl_timer while waiting - clears and exits thread.
static gpointer set_rigctl_timer (gpointer data) {
rigctl_timer = 1;
// Wait throttle time
usleep(RIGCTL_TIMER_DELAY);
rigctl_timer = 0;
}
//
// Used to convert transmitter->ctcss_frequency into 1-39 value for TS2000.
// This COULD be done with a simple table lookup - but I've already written the code
// so at this point...
//
#ifdef PIHPSDR
int convert_ctcss() {
int local_tone = 1;
if(transmitter->ctcss_frequency == (double) 67.0) {
local_tone = 1;
} else if (transmitter->ctcss_frequency == (double) 71.9) {
local_tone = 2;
} else if (transmitter->ctcss_frequency == (double) 74.4) {
local_tone = 3;
} else if (transmitter->ctcss_frequency == (double) 77.0) {
local_tone = 4;
} else if (transmitter->ctcss_frequency == (double) 79.7) {
local_tone = 5;
} else if (transmitter->ctcss_frequency == (double) 82.5) {
local_tone = 6;
} else if (transmitter->ctcss_frequency == (double) 85.4) {
local_tone = 7;
} else if (transmitter->ctcss_frequency == (double) 88.5) {
local_tone = 8;
} else if (transmitter->ctcss_frequency == (double) 91.5) {
local_tone = 9;
} else if (transmitter->ctcss_frequency == (double) 94.8) {
local_tone = 10;
} else if (transmitter->ctcss_frequency == (double) 97.4) {
local_tone = 11;
} else if (transmitter->ctcss_frequency == (double) 100.0) {
local_tone = 12;
} else if (transmitter->ctcss_frequency == (double) 103.5) {
local_tone = 13;
} else if (transmitter->ctcss_frequency == (double) 107.2) {
local_tone = 14;
} else if (transmitter->ctcss_frequency == (double) 110.9) {
local_tone = 15;
} else if (transmitter->ctcss_frequency == (double) 114.8) {
local_tone = 16;
} else if (transmitter->ctcss_frequency == (double) 118.8) {
local_tone = 17;
} else if (transmitter->ctcss_frequency == (double) 123.0) {
local_tone = 18;
} else if (transmitter->ctcss_frequency == (double) 127.3) {
local_tone = 19;
} else if (transmitter->ctcss_frequency == (double) 131.8) {
local_tone = 20;
} else if (transmitter->ctcss_frequency == (double) 136.5) {
local_tone = 21;
} else if (transmitter->ctcss_frequency == (double) 141.3) {
local_tone = 22;
} else if (transmitter->ctcss_frequency == (double) 146.2) {
local_tone = 23;
} else if (transmitter->ctcss_frequency == (double) 151.4) {
local_tone = 24;
} else if (transmitter->ctcss_frequency == (double) 156.7) {
local_tone = 25;
} else if (transmitter->ctcss_frequency == (double) 162.2) {
local_tone = 26;
} else if (transmitter->ctcss_frequency == (double) 167.9) {
local_tone = 27;
} else if (transmitter->ctcss_frequency == (double) 173.8) {
local_tone = 28;
} else if (transmitter->ctcss_frequency == (double) 179.9) {
local_tone = 29;
} else if (transmitter->ctcss_frequency == (double) 186.2) {
local_tone = 30;
} else if (transmitter->ctcss_frequency == (double) 192.8) {
local_tone = 31;
} else if (transmitter->ctcss_frequency == (double) 203.5) {
local_tone = 32;
} else if (transmitter->ctcss_frequency == (double) 210.7) {
local_tone = 33;
} else if (transmitter->ctcss_frequency == (double) 218.1) {
local_tone = 34;
} else if (transmitter->ctcss_frequency == (double) 225.7) {
local_tone = 35;
} else if (transmitter->ctcss_frequency == (double) 233.6) {
local_tone = 36;
} else if (transmitter->ctcss_frequency == (double) 241.8) {
local_tone = 37;
} else if (transmitter->ctcss_frequency == (double) 250.3) {
local_tone = 38;
} else if (transmitter->ctcss_frequency == (double) 1750.0) {
local_tone = 39;
}
return(local_tone);
}
#endif // PIHPSDR
int vfo_sm=0; // VFO State Machine - this keeps track of
#ifdef PIHPSDR
// Now my stuff
//
void send_dash() {
//long delay = (1200000L * ((long)cw_keyer_weight/10L))/(long)cw_keyer_speed ;
int dot_delay = 1200/cw_keyer_speed;
int delay = (dot_delay * 3 * cw_keyer_weight)/50;
g_idle_add(ext_cw_key, (gpointer)(long)1);
usleep((long)delay*3000L);
g_idle_add(ext_cw_key, (gpointer)(long)0);
usleep((long)delay * 1000L);
//fprintf(stderr,"_%d",mox);
}
void send_dot() {
int dot_delay = 1200/cw_keyer_speed;
g_idle_add(ext_cw_key, (gpointer)(long)1);
usleep((long)dot_delay * 1000L);
g_idle_add(ext_cw_key, (gpointer)(long)0);
usleep((long)dot_delay* 1000L);
//fprintf(stderr,".%d",mox);
}
void send_space() {
int dot_delay = 1200/cw_keyer_speed;
usleep((long)dot_delay* 7000L);
//fprintf(stderr," %d",mox);
}
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 '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;
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;
default: strcpy(pattern," ");
}
//fprintf(stderr,"Sending %c:",cw_char);
g_idle_add(ext_cw_key, (gpointer)(long)0);
while(*ptr != '\0') {
if(*ptr == '-') {
send_dash();
}
if(*ptr == '.') {
send_dot();
}
if(*ptr == ' ') {
send_space();
}
ptr++;
}
// Need a delay HERE between characters
long delay = (1200000L * ((long)cw_keyer_weight/10L))/(long)cw_keyer_speed ;
usleep(delay*3L);
//fprintf(stderr,"\n");
}
void gui_vfo_move_to(gpointer data) {
long long freq = *(long long *) data;
fprintf(stderr,"GUI: %11lld\n",freq);
return;
//vfo_move_to(freq);
}
// This looks up the frequency of the Active receiver with
// protection for 1 versus 2 receivers
long long rigctl_getFrequency() {
if(receivers == 1) {
return vfo[VFO_A].frequency;
} else {
return vfo[active_receiver->id].frequency;
}
}
#endif // PIHPSDR
// Looks up entry INDEX_NUM in the command structure and
// returns the command string
//
void send_resp (int client_sock,char * msg) {
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: RESP=%s\n",msg);
#endif
if(client_sock == -1) { // Serial port
int bytes=write(fd,msg,strlen(msg));
} else { // TCP/IP port
int bytes=write(client_sock, msg, strlen(msg));
}
}
//
// 2-25-17 - K5JAE - removed duplicate rigctl
//
static gpointer rigctl_server(gpointer data) {
RECEIVER *rx=(RECEIVER *)data;
int port=rigctl_port_base+rx->channel;
int rc;
int on=1;
int i;
GThread *client_thread_id;
fprintf(stderr,"rigctl_server: starting server on port %d\n",port);
rx->rigctl_server_socket=socket(AF_INET,SOCK_STREAM,0);
if(rx->rigctl_server_socket<0) {
perror("rigctl_server: listen socket failed");
return NULL;
}
setsockopt(rx->rigctl_server_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(rx->rigctl_server_socket, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
// bind to listening port
memset(&rx->rigctl_server_address,0,sizeof(rx->rigctl_server_address));
rx->rigctl_server_address.sin_family=AF_INET;
rx->rigctl_server_address.sin_addr.s_addr=INADDR_ANY;
rx->rigctl_server_address.sin_port=htons(port);
if(bind(rx->rigctl_server_socket,(struct sockaddr*)&rx->rigctl_server_address,sizeof(rx->rigctl_server_address))<0) {
perror("rigctl_server: listen socket bind failed");
close(rx->rigctl_server_socket);
return NULL;
}
rx->rigctl_running=TRUE;
while(rx->rigctl_running) {
// listen with a max queue of 3
if(listen(rx->rigctl_server_socket,1)<0) {
perror("rigctl_server: listen failed");
close(rx->rigctl_server_socket);
return NULL;
}
rx->rigctl_client_socket=accept(rx->rigctl_server_socket,(struct sockaddr*)&rx->rigctl_client_address,&rx->rigctl_client_address_length);
if(rx->rigctl_client_socket<0) {
perror("rigctl_server: client accept failed");
continue;
}
client_thread_id = g_thread_new("rigctl client", rigctl_client, (gpointer)rx);
if(client_thread_id==NULL) {
fprintf(stderr,"g_thread_new failed for rigctl_client\n");
fprintf(stderr,"setting SO_LINGER to 0 for client_socket: %d\n",rx->rigctl_client_socket);
struct linger linger = { 0 };
linger.l_onoff = 1;
linger.l_linger = 0;
if(setsockopt(rx->rigctl_client_socket,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for client");
}
close(rx->rigctl_client_socket);
}
}
close(rx->rigctl_server_socket);
return NULL;
}
static gpointer rigctl_client (gpointer data) {
int len;
int c;
RECEIVER *rx=(RECEIVER *)data;
fprintf(stderr,"rigctl_client: starting rigctl_client: socket=%d\n",rx->rigctl_client_socket);
g_mutex_lock(&rx->rigctl_mutex);
rx->cat_control++;
//#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: CTLA INC cat_contro=%d\n",rx->cat_control);
//#endif
g_mutex_unlock(&rx->rigctl_mutex);
#ifdef PIHPSDR
g_idle_add(ext_vfo_update,NULL);
#else
g_idle_add(ext_vfo_update,rx);
#endif // PIHPSDR
int save_flag = 0; // Used to concatenate two cmd lines together
int semi_number = 0;
int i;
char * work_ptr;
char work_buf[MAXDATASIZE];
int numbytes;
char cmd_input[MAXDATASIZE] ;
char cmd_save[80];
char cw_check_buf[4];
while(rx->rigctl_running && (numbytes=recv(rx->rigctl_client_socket , cmd_input , MAXDATASIZE-2 , 0)) > 0 ) {
for(i=0;i<numbytes;i++) { work_buf[i] = cmd_input[i]; }
work_buf[i+1] = '\0';
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: RCVD=%s<-\n",work_buf);
#endif
// Need to handle two cases
// 1. Command is short, i.e. no semicolon - that will set save_flag=1 and
// read another line..
// 2. 1 to N commands per line. Turns out N1MM sends multiple commands per line
if(save_flag == 0) { // Count the number of semicolons if we aren't already in mode 1.
semi_number = 0;
for(i=0;i<numbytes;i++) {
if(cmd_input[i] == ';') { semi_number++;};
}
}
if((save_flag == 0) && (semi_number == 0)) {
cmd_input[numbytes] = '\0'; // Turn it into a C string
strcpy(cmd_save,cmd_input); // And save a copy of it till next time through
save_flag = 1;
} else if(save_flag == 1) {
save_flag = 0;
cmd_input[numbytes] = '\0'; // Turn it into a C string
strcat(cmd_save,cmd_input);
strcpy(cmd_input,cmd_save); // Cat them together and replace cmd_input
numbytes = strlen(cmd_input);
}
if(save_flag != 1) {
work_ptr = strtok(cmd_input,";");
while(work_ptr != NULL) {
if(cw_busy == 1) {
if(strlen(work_ptr)>2) {
cw_check_buf[0] = work_ptr[0];
cw_check_buf[1] = work_ptr[1];
cw_check_buf[2] = '\0';
if(strcmp("ZZ",cw_check_buf)==0) {
if(strlen(work_ptr)>4) {
cw_check_buf[0] = work_ptr[2];
cw_check_buf[1] = work_ptr[3];
cw_check_buf[2] = '\0';
} else {
send_resp(rx->rigctl_client_socket,"?;");
}
}
} else {
// Illegal Command
send_resp(rx->rigctl_client_socket,"?;");
}
// Got here because we have a legal command in cw_check_buf
// Look for RESET and BUSY which re respond to else - send ?;
if(strcmp("BY",cw_check_buf)==0) {
send_resp(rx->rigctl_client_socket,"BY11;"); // Indicate that we are BUSY
} else if (strcmp("SR",cw_check_buf) == 0) {
// Reset the transceiver
g_mutex_lock(&rx->rigctl_mutex);
cw_reset = 1;
g_mutex_unlock(&rx->rigctl_mutex);
// Wait till BUSY clears
while(cw_busy);
g_mutex_lock(&rx->rigctl_mutex);
cw_reset = 0;
g_mutex_unlock(&rx->rigctl_mutex);
}
}
// Lock so only one user goes into this at a time
g_mutex_lock(&rx->rigctl_mutex);
parse_cmd(work_ptr,strlen(work_ptr),rx->rigctl_client_socket,rx);
g_mutex_unlock(&rx->rigctl_mutex);
work_ptr = strtok(NULL,";");
}
for(i=0;i<MAXDATASIZE;i++){
cmd_input[i] = '\0';
work_buf[i] = '\0'; // Clear the input buffer
}
}
// Got here because socket closed
}
fprintf(stderr,"RIGCTL: Leaving rigctl_client thread");
if(rx->rigctl_client_socket!=-1) {
fprintf(stderr,"setting SO_LINGER to 0 for client_socket: %d\n",rx->rigctl_client_socket);
struct linger linger = { 0 };
linger.l_onoff = 1;
linger.l_linger = 0;
if(setsockopt(rx->rigctl_client_socket,SOL_SOCKET,SO_LINGER,(const char *)&linger,sizeof(linger))==-1) {
perror("setsockopt(...,SO_LINGER,...) failed for client");
}
close(rx->rigctl_client_socket);
rx->rigctl_client_socket=-1;
// Decrement CAT_CONTROL
g_mutex_lock(&rx->rigctl_mutex);
rx->cat_control--;
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: CTLA DEC - cat_control=%d\n",cat_control);
#endif
g_mutex_unlock(&rx->rigctl_mutex);
#ifdef PIHPSDR
g_idle_add(ext_vfo_update,NULL);
#else
g_idle_add(ext_vfo_update,rx);
#endif // PIHPSDR
}
return NULL;
}
//
// FT command intepret vfo_sm state - used by IF command
//
int ft_read() {
return(active_transmitter);
}
//
// Determines RIT state - used by IF command
//
int rit_on () {
#ifdef PIHPSDR
if(receivers == 1) { // Worry about 1 versus 2 radios
if(vfo[VFO_A].rit != 0) {
return 1;
} else {
return 0;
}
} else { // Well - we have two so use active_reciever->id
if(vfo[active_receiver->id].rit != 0) {
return 1 ;
} else {
return 0;
}
}
#endif // PIHPSDR
}
//
// TS-2000 parser
// -- Now extended with zzid_flag to indicate PSDR extended command set
//
void parse_cmd ( char * cmd_input,int len,int client_sock,RECEIVER *rx) {
int work_int;
int new_low, new_high;
int zzid_flag;
double meter;
double forward;
double reverse;
double vswr;
char msg[200];
char buf[200];
int i;
#ifdef PIHPSDR
BANDSTACK_ENTRY *entry;
#endif // PIHPSDR
// Parse the cmd_input
//int space = command.indexOf(' ');
//char cmd_char = com_head->cmd_string[0]; // Assume the command is first thing!
char cmd_str[3];
/*
// Put in throtle check here - we have an issue with issuing to many
// GUI commands - the idea is to create a separate thread that maintains a 200ms clock
// and use the Mutex mechanism to wait here till we process the next command
while(rigctl_timer != 0) { // Wait here till the timer expires
usleep(1000);
}
// Start a new timer...
rigctl_set_timer_thread_id = g_thread_new( "Rigctl Timer", set_rigctl_timer, NULL);
while(rigctl_timer != 1) { // Wait here till the timer sets!
usleep(1000);
}
usleep(1000);
// Clean up the thread
g_thread_unref(rigctl_set_timer_thread_id);
*/
// On with the rest of the show..
cmd_str[0] = cmd_input[0];
cmd_str[1] = cmd_input[1];
cmd_str[2] = '\0';
// Added support for PSDR extended command set - they all start
// with ZZ so:
//
// Check to see if first part of string is ZZ - if so
// strip the ZZ out and set the zzid_flag = 1;
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: CMD=%s\n",cmd_input);
#endif
zzid_flag = 0; // Set to indicate we haven't seen ZZ
char * zzid_ptr;
char temp[80];
int cnt;
if(strcmp(cmd_str,"ZZ")==0) {
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: Init=%s\n",cmd_str);
#endif
// Adjust the Parse input like there hadn't been a ZZ in front - but
// set the ZZ flag to indicate we saw it.
zzid_ptr = &temp[0];
// It is 4AM and this was the only safe way for me to get a strcpy to work
// so - there ya go...
for(cnt=2; cnt<=len;cnt++) {
//fprintf(stderr,"[%d]=%c:",cnt,cmd_input[cnt]);
*zzid_ptr++= cmd_input[cnt];
}
temp[len+1] = '\0';
strcpy(cmd_input,temp);
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: Cmd_input=%s\n",cmd_input);
#endif
//
cmd_str[0] = cmd_input[0];
cmd_str[1] = cmd_input[1];
cmd_str[2] = '\0';
zzid_flag = 1;
len = strlen (temp);
}
if(strcmp(cmd_str,"AC")==0) {
// TS-2000 - AC - Responds with 000??
// PiHPSDR - ZZAC - Step Command
if(zzid_flag ==1) { // Set or Read the Step Size (replaces ZZST)
if(len <= 2) {
switch(rx->step) {
case 1: work_int = 0; break;
case 10: work_int = 1; break;
case 25: work_int = 2; break;
case 50: work_int = 3; break;
case 100: work_int = 4; break;
case 250: work_int = 5; break;
case 500: work_int = 6; break;
case 1000: work_int = 7; break;
case 2000: work_int = 8; break;
case 2500: work_int = 9; break;
case 5000: work_int = 10; break;
case 6250: work_int = 11; break;
case 9000: work_int = 12; break;
case 10000: work_int = 13; break;
case 12500: work_int = 14; break;
case 15000: work_int = 15; break;
case 20000: work_int = 16; break;
case 25000: work_int = 17; break;
case 30000: work_int = 18; break;
case 50000: work_int = 19; break;
case 100000: work_int = 20; break;
case 500000: work_int = 21; break;
case 1000000: work_int = 22; break;
case 10000000: work_int = 23; break;
default:
work_int = 0;
fprintf(stderr,"RIGCTL: ERROR step out of range\n");
send_resp(client_sock,"?;");
break;
}
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RESP: ZZAC%02d;",work_int);
#endif
sprintf(msg,"ZZAC%02d;",work_int);
send_resp(client_sock,msg);
} else { // Legal vals between 00 and 22.
switch(atoi(&cmd_input[2])) {
case 0: rx->step = 1; break;
case 1: rx->step = 10; break;
case 2: rx->step = 25; break;
case 3: rx->step = 50; break;
case 4: rx->step = 100; break;
case 5: rx->step = 250; break;
case 6: rx->step = 500; break;
case 7: rx->step = 1000; break;
case 8: rx->step = 2000; break;
case 9: rx->step = 2500; break;
case 10: rx->step = 5000; break;
case 11: rx->step = 6250; break;
case 12: rx->step = 9000; break;
case 13: rx->step = 10000; break;
case 14: rx->step = 12500; break;
case 15: rx->step = 15000; break;
case 16: rx->step = 20000; break;
case 17: rx->step = 25000; break;
case 18: rx->step = 30000; break;
case 19: rx->step = 50000; break;
case 20: rx->step = 100000; break;
case 21: rx->step = 500000; break;
case 22: rx->step = 1000000; break;
case 23: rx->step = 10000000; break;
default:
fprintf(stderr,"RIGCTL: ERROR - ZZAC out of range\n");
send_resp(client_sock,"?;");
}
}
} else { // Sets or reads the internal antenna tuner status
// P1 0: RX-AT Thru, 1: RX-AT IN
// P2 0: TX-AT Thru 1: TX-AT In
//
if(len <= 2) {
send_resp(client_sock,"AC000;");
}
}
}
else if((strcmp(cmd_str,"AD")==0) && (zzid_flag==1)) {
// PiHPSDR - ZZAD - Move VFO A Down by step
#ifdef PIHPSDR
//vfo_step(-1);
int lcl_step = -1;
g_idle_add(ext_vfo_step,(gpointer)(long)lcl_step);
g_idle_add(ext_vfo_update,NULL);
#endif // PIHPSDR
}
else if(strcmp(cmd_str,"AG")==0) {
#ifdef PIHPSDR
if(zzid_flag == 1) {
// PiHPSDR - ZZAG - Set/read Audio Gain
// Values are 000-100
if(len<=2) { // Send ZZAGxxx; - active_receiver->volume 0.00-1.00
sprintf(msg,"ZZAG%03d;",(int) (roundf(g_idle_add(ext_get_act_audio_gain,NULL)*100.0)));
send_resp(client_sock,msg);
} else { // Set Audio Gain
work_int = atoi(&cmd_input[2]);
g_idle_add(ext_set_act_audio_gain,((double) work_int)/100.0);
g_idle_add(ext_update_af_gain,NULL);
}
} else {
// TS-2000 - AG - Set Audio Gain
// AG123; Value of 0-
// AG1 = Sub receiver - what is the value set
// Response - AG<0/1>123; Where 123 is 0-260
int lcl_receiver;
if(receivers == 1) {
lcl_receiver = 0;
} else {
lcl_receiver = (int) g_idle_add(ext_get_act_id,NULL);
}
if(len>4) { // Set Audio Gain
if((atoi(&cmd_input[3]) >=0) && (atoi(&cmd_input[3])<=260)) {
g_idle_add(ext_set_act_audio_gain,((double) atoi(&cmd_input[3]))/260);
g_idle_add(ext_update_af_gain,NULL);
} else {
send_resp(client_sock,"?;");
}
} else { // Read Audio Gain
//sprintf(msg,"AG%1d%03d;",lcl_receiver,(int) (260 * active_receiver->volume));
sprintf(msg,"AG%1d%03d;",lcl_receiver,(int) (260 * g_idle_add(ext_get_act_audio_gain,NULL)));
send_resp(client_sock,msg);
}
}
#endif // PIHPSDR
}
else if((strcmp(cmd_str,"AI")==0) && (zzid_flag == 0)) {
// TS-2000 - AI- Allow rebroadcast of set frequency after set - not supported
if(len <=2) {
//send_resp(client_sock,"AI0;");
send_resp(client_sock,"?;");
}
}
else if((strcmp(cmd_str,"AL")==0) && (zzid_flag == 0)) {
// TS-2000 - AL - Set/Reads the auto Notch level - not supported
if(len <=2) {
//send_resp(client_sock,"AL000;");
send_resp(client_sock,"?;");
}
}
else if((strcmp(cmd_str,"AM")==0) && (zzid_flag == 0)) {
// TS-2000 - AM - Sets or reads the Auto Mode - not supported
if(len <=2) {
//send_resp(client_sock,"AM0;");
send_resp(client_sock,"?;");
}
}
else if((strcmp(cmd_str,"AN")==0) && (zzid_flag == 0)) {
// TS-2000 - AN - Selects the antenna connector (ANT1/2) - not supported
if(len <=2) {
//send_resp(client_sock,"AN0;");
send_resp(client_sock,"?;");
}
}
else if((strcmp(cmd_str,"AP")==0) && (zzid_flag == 1)) {
#ifdef PIHPSDR
// PiHPSDR - ZZAP - Set/Read Power Amp Settings.
// format - P1P1P1 P2P2.P2
// P1 - Band P2 - 3.1 float WITH decimal point required!
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL: ZZAP len=%d\n",len);
#endif
if(len<=5) { // Read Command
work_int = lookup_band(atoi(&cmd_input[2]));
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL ZZAP work_int=%d\n",work_int);
#endif
BAND *band = band_get_band(work_int);
sprintf(msg,"ZZAP%03d%3.1f;",atoi(&cmd_input[2]),band->pa_calibration);
send_resp(client_sock,msg);
} else {
/* isolate the command band from the value */
char lcl_char = cmd_input[5];;
cmd_input[5]='\0'; // Make a temp string
work_int = atoi(&cmd_input[2]);
cmd_input[5] = lcl_char; // Restore the orig string..
double lcl_float = atof(&cmd_input[5]);
#ifdef RIGCTL_DEBUG
fprintf(stderr,"RIGCTL ZZAP - band=%d setting=%3.1f\n",work_int, lcl_float);
#endif
work_int = lookup_band(work_int);
// Sequence below lifted from pa_menu