forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flrig.c
1910 lines (1572 loc) · 52.3 KB
/
flrig.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
/*
* Hamlib FLRig backend - main file
* Copyright (c) 2017 by Michael Black W9MDB
* Copyright (c) 2018 by Michael Black W9MDB
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <math.h>
#include <hamlib/rig.h>
#include <serial.h>
#include <misc.h>
#include <cal.h>
#include <token.h>
#include <register.h>
#include <network.h>
#include "flrig.h"
#define DEBUG 1
#define DEBUG_TRACE DEBUG_VERBOSE
#define MAXCMDLEN 8192
#define MAXXMLLEN 8192
#define MAXBANDWIDTHLEN 4096
#define DEFAULTPATH "127.0.0.1:12345"
#define FLRIG_VFOS (RIG_VFO_A|RIG_VFO_B)
#define FLRIG_MODES (RIG_MODE_AM | RIG_MODE_PKTAM | RIG_MODE_CW | RIG_MODE_CWR |\
RIG_MODE_RTTY | RIG_MODE_RTTYR |\
RIG_MODE_PKTLSB | RIG_MODE_PKTUSB |\
RIG_MODE_SSB | RIG_MODE_LSB | RIG_MODE_USB |\
RIG_MODE_FM | RIG_MODE_WFM | RIG_MODE_FMN |RIG_MODE_PKTFM )
#define streq(s1,s2) (strcmp(s1,s2)==0)
static int flrig_init(RIG *rig);
static int flrig_open(RIG *rig);
static int flrig_close(RIG *rig);
static int flrig_cleanup(RIG *rig);
static int flrig_set_freq(RIG *rig, vfo_t vfo, freq_t freq);
static int flrig_get_freq(RIG *rig, vfo_t vfo, freq_t *freq);
static int flrig_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt);
static int flrig_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width);
static int flrig_set_split_mode(RIG *rig, vfo_t vfo, rmode_t mode,
pbwidth_t width);
static int flrig_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width);
static int flrig_get_vfo(RIG *rig, vfo_t *vfo);
static int flrig_set_vfo(RIG *rig, vfo_t vfo);
static int flrig_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt);
static int flrig_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt);
static int flrig_set_split_freq(RIG *rig, vfo_t vfo, freq_t tx_freq);
static int flrig_get_split_freq(RIG *rig, vfo_t vfo, freq_t *tx_freq);
static int flrig_set_split_vfo(RIG *rig, vfo_t vfo, split_t split,
vfo_t tx_vfo);
static int flrig_get_split_vfo(RIG *rig, vfo_t vfo, split_t *split,
vfo_t *tx_vfo);
static int flrig_set_split_freq_mode(RIG *rig, vfo_t vfo, freq_t freq,
rmode_t mode, pbwidth_t width);
static int flrig_get_split_freq_mode(RIG *rig, vfo_t vfo, freq_t *freq,
rmode_t *mode, pbwidth_t *width);
static const char *flrig_get_info(RIG *rig);
struct flrig_priv_data
{
vfo_t curr_vfo;
char bandwidths[MAXBANDWIDTHLEN]; /* pipe delimited set returned from flrig */
int nbandwidths;
char info[8192];
ptt_t ptt;
split_t split;
rmode_t curr_modeA;
rmode_t curr_modeB;
freq_t curr_freqA;
freq_t curr_freqB;
pbwidth_t curr_widthA;
pbwidth_t curr_widthB;
int has_get_modeA; /* True if this function is available */
int has_get_bwA; /* True if this function is available */
};
const struct rig_caps flrig_caps =
{
RIG_MODEL(RIG_MODEL_FLRIG),
.model_name = "FLRig",
.mfg_name = "FLRig",
.version = BACKEND_VER ".0",
.copyright = "LGPL",
.status = RIG_STATUS_STABLE,
.rig_type = RIG_TYPE_TRANSCEIVER,
.targetable_vfo = RIG_TARGETABLE_FREQ | RIG_TARGETABLE_MODE,
.ptt_type = RIG_PTT_RIG,
.port_type = RIG_PORT_NETWORK,
.write_delay = 0,
.post_write_delay = 0,
.timeout = 2000,
.retry = 5,
.has_get_func = RIG_FUNC_NONE,
.has_set_func = RIG_FUNC_NONE,
.has_get_level = RIG_LEVEL_NONE,
.has_set_level = RIG_LEVEL_NONE,
.has_get_parm = RIG_PARM_NONE,
.has_set_parm = RIG_PARM_NONE,
.filters = {
RIG_FLT_END
},
.rx_range_list1 = {{
.startf = kHz(1), .endf = GHz(10), .modes = FLRIG_MODES,
.low_power = -1, .high_power = -1, FLRIG_VFOS, RIG_ANT_1
},
RIG_FRNG_END,
},
.tx_range_list1 = {RIG_FRNG_END,},
.rx_range_list2 = {{
.startf = kHz(1), .endf = GHz(10), .modes = FLRIG_MODES,
.low_power = -1, .high_power = -1, FLRIG_VFOS, RIG_ANT_1
},
RIG_FRNG_END,
},
.tx_range_list2 = {RIG_FRNG_END,},
.tuning_steps = { {FLRIG_MODES, 1}, {FLRIG_MODES, RIG_TS_ANY}, RIG_TS_END, },
.priv = NULL, /* priv */
.rig_init = flrig_init,
.rig_open = flrig_open,
.rig_close = flrig_close,
.rig_cleanup = flrig_cleanup,
.set_freq = flrig_set_freq,
.get_freq = flrig_get_freq,
.set_mode = flrig_set_mode,
.get_mode = flrig_get_mode,
.set_vfo = flrig_set_vfo,
.get_vfo = flrig_get_vfo,
.get_info = flrig_get_info,
.set_ptt = flrig_set_ptt,
.get_ptt = flrig_get_ptt,
.set_split_mode = flrig_set_split_mode,
.set_split_freq = flrig_set_split_freq,
.get_split_freq = flrig_get_split_freq,
.set_split_vfo = flrig_set_split_vfo,
.get_split_vfo = flrig_get_split_vfo,
.set_split_freq_mode = flrig_set_split_freq_mode,
.get_split_freq_mode = flrig_get_split_freq_mode
};
// Structure for mapping flrig dynmamic modes to hamlib modes
// flrig displays modes as the rig displays them
struct s_modeMap
{
rmode_t mode_hamlib;
char *mode_flrig;
};
// FLRig will provide us the modes for the selected rig
// We will then put them in this struct
static struct s_modeMap modeMap[] =
{
{RIG_MODE_USB, NULL},
{RIG_MODE_LSB, NULL},
{RIG_MODE_PKTUSB, NULL},
{RIG_MODE_PKTLSB, NULL},
{RIG_MODE_PKTUSB, NULL},
{RIG_MODE_PKTLSB, NULL},
{RIG_MODE_AM, NULL},
{RIG_MODE_FM, NULL},
{RIG_MODE_FMN, NULL},
{RIG_MODE_WFM, NULL},
{RIG_MODE_CW, NULL},
{RIG_MODE_CW, NULL},
{RIG_MODE_CWR, NULL},
{RIG_MODE_CWR, NULL},
{RIG_MODE_RTTY, NULL},
{RIG_MODE_RTTYR, NULL},
{0, NULL}
};
/*
* check_vfo
* No assumptions
*/
static int check_vfo(vfo_t vfo)
{
switch (vfo)
{
case RIG_VFO_A:
break;
case RIG_VFO_TX:
case RIG_VFO_B:
break;
case RIG_VFO_CURR:
break; // will default to A in which_vfo
default:
return FALSE;
}
return TRUE;
}
/* Rather than use some huge XML library we only need a few things
* So we'll hand craft them
* xml_build takes a value and return an xml string for FLRig
*/
static char *xml_build(char *cmd, char *value, char *xmlbuf, int xmlbuflen)
{
char xml[4096]; // we shouldn't need more the 4096 bytes for this
char tmp[32];
char *header;
int n;
// We want at least a 4K buf to play with
if (xmlbuflen < 4096)
{
rig_debug(RIG_DEBUG_ERR, "%s: xmllen < 4096\n", __func__);
return NULL;
}
header =
"POST /RPC2 HTTP/1.1\r\n" "User-Agent: XMLRPC++ 0.8\r\n"
"Host: 127.0.0.1:12345\r\n" "Content-type: text/xml\r\n";
n = snprintf(xmlbuf, xmlbuflen, "%s", header);
if (n != strlen(header))
{
rig_debug(RIG_DEBUG_ERR, "%s: snprintf of header failed, len=%d, got=%d\n",
__func__, (int)strlen(header), n);
}
n = snprintf(xml, sizeof(xml), "<?xml version=\"1.0\"?>\r\n");
if (n != strlen(xml))
{
rig_debug(RIG_DEBUG_ERR, "%s: snprintf of xml failed, len=%d, got=%d\n",
__func__, (int)strlen(header), n);
}
strncat(xml, "<methodCall><methodName>", sizeof(xml) - 1);
strncat(xml, cmd, sizeof(xml) - strlen(xml) - 1);
strncat(xml, "</methodName>\r\n", sizeof(xml) - strlen(xml) - 1);
if (value && strlen(value) > 0)
{
strncat(xml, value, sizeof(xml) - 1);
}
strncat(xml, "</methodCall>\r\n", sizeof(xml) - 1);
strncat(xmlbuf, "Content-length: ", xmlbuflen - 1);
snprintf(tmp, sizeof(tmp), "%d\r\n\r\n", (int)strlen(xml));
strncat(xmlbuf, tmp, xmlbuflen - 1);
strncat(xmlbuf, xml, xmlbuflen - 1);
return xmlbuf;
}
/* This is a very crude xml parse specific to what we need from FLRig
* This works for strings, doubles, I4-type values, and arrays
* Arrays are returned pipe delimited
*/
static char *xml_parse2(char *xml, char *value, int valueLen)
{
char *delims = "<>\r\n ";
char *xmltmp = strdup(xml);
//rig_debug(RIG_DEBUG_TRACE, "%s: xml='%s'\n", __func__,xml);
char *pr = xml;
char *p = strtok_r(xmltmp, delims, &pr);
value[0] = 0;
while (p)
{
if (streq(p, "value"))
{
p = strtok_r(NULL, delims, &pr);
if (streq(p, "array")) { continue; }
if (streq(p, "/value")) { continue; } // empty value
if (streq(p, "i4") || streq(p, "double"))
{
p = strtok_r(NULL, delims, &pr);
}
else if (streq(p, "array"))
{
strtok_r(NULL, delims, &pr);
p = strtok_r(NULL, delims, &pr);
}
if (strlen(value) + strlen(p) + 1 < valueLen)
{
if (value[0] != 0) { strcat(value, "|"); }
strcat(value, p);
}
else // we'll just stop adding stuff
{
rig_debug(RIG_DEBUG_ERR, "%s: max value length exceeded\n", __func__);
}
}
else
{
p = strtok_r(NULL, delims, &pr);
}
}
rig_debug(RIG_DEBUG_TRACE, "%s: value returned='%s'\n", __func__, value);
if (rig_need_debug(RIG_DEBUG_WARN) && value != NULL && strlen(value) == 0)
{
rig_debug(RIG_DEBUG_ERR, "%s: xml='%s'\n", __func__, xml);
}
free(xmltmp);
return value;
}
/*
* xml_parse
* Assumes xml!=NULL, value!=NULL, value_len big enough
* returns the string value contained in the xml string
*/
static char *xml_parse(char *xml, char *value, int value_len)
{
char *next;
char *pxml;
/* first off we should have an OK on the 1st line */
if (strstr(xml, " 200 OK") == NULL)
{
return NULL;
}
rig_debug(RIG_DEBUG_TRACE, "%s XML:\n%s\n", __func__, xml);
// find the xml skipping the other stuff above it
pxml = strstr(xml, "<?xml");
if (pxml == NULL)
{
return NULL;
}
next = strchr(pxml + 1, '<');
if (value != NULL)
{
xml_parse2(next, value, value_len);
}
if (strstr(value, "faultString"))
{
rig_debug(RIG_DEBUG_ERR, "%s error:\n%s\n", __func__, value);
value[0] = 0; /* truncate to give empty response */
}
return value;
}
/*
* read_transaction
* Assumes rig!=NULL, xml!=NULL, xml_len>=MAXXMLLEN
*/
static int read_transaction(RIG *rig, char *xml, int xml_len)
{
int retval;
int retry;
char *delims;
char *terminator = "</methodResponse>";
struct rig_state *rs = &rig->state;
rig_debug(RIG_DEBUG_TRACE, "%s\n", __func__);
rs->rigport.timeout = 1000; // 2 second read string timeout
retry = 5;
delims = "\n";
xml[0] = 0;
do
{
char tmp_buf[MAXXMLLEN]; // plenty big for expected flrig responses hopefully
int len = read_string(&rs->rigport, tmp_buf, sizeof(tmp_buf), delims,
strlen(delims));
rig_debug(RIG_DEBUG_TRACE, "%s: string='%s'", __func__, tmp_buf);
if (len > 0) { retry = 3; }
if (len <= 0)
{
rig_debug(RIG_DEBUG_ERR, "%s: read_string error=%d\n", __func__, len);
return -(100 + RIG_EPROTO);
}
if (strlen(xml) + strlen(tmp_buf) < xml_len - 1)
{
strncat(xml, tmp_buf, xml_len);
}
else
{
rig_debug(RIG_DEBUG_ERR,
"%s: xml buffer overflow!!\nTrying to add len=%d\n%sTo len=%d\n%s", __func__,
(int)strlen(tmp_buf), tmp_buf, (int)strlen(xml), xml);
return -RIG_EPROTO;
}
}
while (retry-- > 0 && strstr(xml, terminator) == NULL);
if (retry == 0)
{
rig_debug(RIG_DEBUG_WARN, "%s: retry timeout\n", __func__);
}
if (strstr(xml, terminator))
{
rig_debug(RIG_DEBUG_TRACE, "%s: got %s\n", __func__, terminator);
// Slow down just a bit -- not sure this is needed anymore but not a big deal here
hl_usleep(2 * 1000);
retval = RIG_OK;
}
else
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: did not get %s\n", __func__, terminator);
retval = -(101 + RIG_EPROTO);
}
return retval;
}
/*
* write_transaction
* Assumes rig!=NULL, xml!=NULL, xml_len=total size of xml for response
*/
static int write_transaction(RIG *rig, char *xml, int xml_len)
{
int try = rig->caps->retry;
int retval = -RIG_EPROTO;
struct rig_state *rs = &rig->state;
// This shouldn't ever happen...but just in case
// We need to avoid and empty write as rigctld replies with blank line
if (xml_len == 0)
{
rig_debug(RIG_DEBUG_ERR, "%s: len==0??\n", __func__);
return retval;
}
// appears we can lose sync if we don't clear things out
// shouldn't be anything for us now anyways
network_flush(&rig->state.rigport);
while (try-- >= 0 && retval != RIG_OK)
{
retval = write_block(&rs->rigport, xml, strlen(xml));
if (retval < 0)
{
return -RIG_EIO;
}
}
return retval;
}
/*
* flrig_init
* Assumes rig!=NULL
*/
static int flrig_init(RIG *rig)
{
struct flrig_priv_data *priv;
rig_debug(RIG_DEBUG_TRACE, "%s version %s\n", __func__, BACKEND_VER);
rig->state.priv = (struct flrig_priv_data *)malloc(sizeof(
struct flrig_priv_data));
if (!rig->state.priv)
{
return -RIG_ENOMEM;
}
priv = rig->state.priv;
memset(priv, 0, sizeof(struct flrig_priv_data));
/*
* set arbitrary initial status
*/
priv->curr_vfo = RIG_VFO_A;
priv->split = 0;
priv->ptt = 0;
priv->curr_modeA = -1;
priv->curr_modeB = -1;
priv->curr_widthA = -1;
priv->curr_widthB = -1;
if (!rig->caps)
{
return -RIG_EINVAL;
}
strncpy(rig->state.rigport.pathname, DEFAULTPATH,
sizeof(rig->state.rigport.pathname));
return RIG_OK;
}
/*
* modeMapGetFLRig
* Assumes mode!=NULL
* Return the string for FLRig for the given hamlib mode
*/
static const char *modeMapGetFLRig(rmode_t modeHamlib)
{
int i;
for (i = 0; modeMap[i].mode_hamlib != 0; ++i)
{
if (modeMap[i].mode_hamlib == modeHamlib && modeMap[i].mode_flrig != NULL)
{
return modeMap[i].mode_flrig;
}
}
rig_debug(RIG_DEBUG_ERR, "%s: Unknown mode requested: %s\n", __func__,
rig_strrmode(modeHamlib));
return "ERROR";
}
/*
* modeMapGetHamlib
* Assumes mode!=NULL
* Return the hamlib mode from the given FLRig string
*/
static rmode_t modeMapGetHamlib(const char *modeFLRig)
{
int i;
char modeFLRigCheck[64];
snprintf(modeFLRigCheck, sizeof(modeFLRigCheck), "|%s|", modeFLRig);
for (i = 0; modeMap[i].mode_hamlib != 0; ++i)
{
rig_debug(RIG_DEBUG_TRACE, "%s: find '%s' in '%s'\n", __func__,
modeFLRigCheck, modeMap[i].mode_flrig);
if (modeMap[i].mode_flrig
&& strcmp(modeMap[i].mode_flrig, modeFLRigCheck) == 0)
{
return modeMap[i].mode_hamlib;
}
}
rig_debug(RIG_DEBUG_TRACE, "%s: Unknown mode requested: %s\n", __func__,
modeFLRig);
return RIG_MODE_NONE;
}
/*
* modeMapAdd
* Assumes modes!=NULL
*/
static void modeMapAdd(rmode_t *modes, rmode_t mode_hamlib, char *mode_flrig)
{
int i;
int len1;
rig_debug(RIG_DEBUG_TRACE, "%s:mode_flrig=%s\n", __func__, mode_flrig);
// if we already have it just return
// We get ERROR if the mode is not known so non-ERROR is OK
if (modeMapGetHamlib(mode_flrig) != RIG_MODE_NONE) { return; }
len1 = strlen(mode_flrig) + 3; /* bytes needed for allocating */
for (i = 0; modeMap[i].mode_hamlib != 0; ++i)
{
if (modeMap[i].mode_hamlib == mode_hamlib)
{
int len2;
*modes |= modeMap[i].mode_hamlib;
/* we will pipe delimit all the entries for easier matching */
/* all entries will have pipe symbol on both sides */
if (modeMap[i].mode_flrig == NULL)
{
modeMap[i].mode_flrig = calloc(1, len1);
if (modeMap[i].mode_flrig == NULL)
{
rig_debug(RIG_DEBUG_ERR, "%s: error allocating memory for modeMap\n",
__func__);
return;
}
}
len2 = strlen(modeMap[i].mode_flrig); /* current len w/o null */
modeMap[i].mode_flrig = realloc(modeMap[i].mode_flrig,
strlen(modeMap[i].mode_flrig) + len1);
if (strlen(modeMap[i].mode_flrig) == 0) { modeMap[i].mode_flrig[0] = '|'; }
strncat(modeMap[i].mode_flrig, mode_flrig, len1 + len2);
strncat(modeMap[i].mode_flrig, "|", len1 + len2);
rig_debug(RIG_DEBUG_TRACE, "%s: Adding mode=%s, index=%d, result=%s\n",
__func__, mode_flrig, i, modeMap[i].mode_flrig);
return;
}
}
}
/*
* flrig_open
* Assumes rig!=NULL, rig->state.priv!=NULL
*/
static int flrig_open(RIG *rig)
{
int retval;
char xml[MAXXMLLEN];
char *pxml;
char value[MAXXMLLEN];
rmode_t modes;
char *p;
char *pr;
struct flrig_priv_data *priv = (struct flrig_priv_data *) rig->state.priv;
rig_debug(RIG_DEBUG_TRACE, "%s version %s\n", __func__, BACKEND_VER);
pxml = xml_build("rig.get_xcvr", NULL, xml, sizeof(xml));
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval < 0)
{
return retval;
}
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
strncpy(priv->info, value, sizeof(priv->info));
rig_debug(RIG_DEBUG_VERBOSE, "Transceiver=%s\n", value);
/* see if get_modeA is available */
pxml = xml_build("rig.get_modeA", NULL, xml, sizeof(xml));
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval != RIG_OK) { return retval; }
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
if (strlen(value) > 0) /* must have it since we got an answer */
{
priv->has_get_modeA = 1;
rig_debug(RIG_DEBUG_VERBOSE, "%s: getmodeA is available=%s\n", __func__,
value);
}
else
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: getmodeA is not available\n", __func__);
}
/* see if get_bwA is available */
pxml = xml_build("rig.get_bwA", NULL, xml, sizeof(xml));
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval != RIG_OK) { return retval; }
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
if (strlen(value) > 0) /* must have it since we got an answer */
{
priv->has_get_bwA = 1;
rig_debug(RIG_DEBUG_VERBOSE, "%s: get_bwA is available=%s\n", __func__,
value);
}
else
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: get_bwA is not available\n", __func__);
}
pxml = xml_build("rig.get_AB", value, xml, sizeof(xml));
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval != RIG_OK) { return retval; }
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
if (streq(value, "A"))
{
priv->curr_vfo = RIG_VFO_A;
}
else
{
priv->curr_vfo = RIG_VFO_B;
}
rig_debug(RIG_DEBUG_TRACE, "%s: currvfo=%s value=%s\n", __func__,
rig_strvfo(priv->curr_vfo), value);
//vfo_t vfo=RIG_VFO_A;
//vfo_t vfo_tx=RIG_VFO_B; // split is always VFOB
//flrig_get_split_vfo(rig, vfo, &priv->split, &vfo_tx);
/* find out available widths and modes */
pxml = xml_build("rig.get_modes", NULL, xml, sizeof(xml));
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval < 0)
{
return retval;
}
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
rig_debug(RIG_DEBUG_TRACE, "%s: modes=%s\n", __func__, value);
modes = 0;
pr = value;
/* The following modes in FLRig are not implemented yet
A1A
AM-2
AM6.0
AM-D1 -- doesn't appear to be read/set
AM-D2 -- doesn't appear to be read/set
AM-D3 -- doesn't appear to be read/set
AMW -- don't have mode in rig.h
CW2.4 -- could be CW
CW500 -- could be CWN but CWN not in rig.h
CW-N -- could be CWN but CWN not in rig.h
CWN -- dcould be CWN but CWN not in rig.h
CW-NR -- don't have mode in rig.h
DATA2-LSB
DV
DV-R
F1B
FM-D1 -- doesn't appear to be read/set
FM-D2 -- doesn't appear to be read/set
FM-D3 -- doesn't appear to be read/set
H3E
M11
USB-D -- doesn't appear to be read/set
USB-D1 -- doesn't appear to be read/set
USB-D2 -- doesn't appear to be read/set
USB-D3 -- doesn't appear to be read/set
USER-L -- doesn't appear to be read/set
USER-U -- doesn't appear to be read/set
*/
for (p = strtok_r(value, "|", &pr); p != NULL; p = strtok_r(NULL, "|", &pr))
{
if (streq(p, "AM-D")) { modeMapAdd(&modes, RIG_MODE_PKTAM, p); }
else if (streq(p, "AM")) { modeMapAdd(&modes, RIG_MODE_AM, p); }
else if (streq(p, "AM-N")) { modeMapAdd(&modes, RIG_MODE_AMN, p); }
else if (streq(p, "AMN")) { modeMapAdd(&modes, RIG_MODE_AMN, p); }
else if (streq(p, "CW")) { modeMapAdd(&modes, RIG_MODE_CW, p); }
else if (streq(p, "CW-L")) { modeMapAdd(&modes, RIG_MODE_CWR, p); }
else if (streq(p, "CW-LSB")) { modeMapAdd(&modes, RIG_MODE_CWR, p); }
else if (streq(p, "CW-R")) { modeMapAdd(&modes, RIG_MODE_CWR, p); }
else if (streq(p, "CW-U")) { modeMapAdd(&modes, RIG_MODE_CW, p); }
else if (streq(p, "CW-USB")) { modeMapAdd(&modes, RIG_MODE_CW, p); }
else if (streq(p, "CWL")) { modeMapAdd(&modes, RIG_MODE_CWR, p); }
else if (streq(p, "CWU")) { modeMapAdd(&modes, RIG_MODE_CW, p); }
else if (streq(p, "D-LSB")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "D-USB")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DATA")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DATA-FM")) { modeMapAdd(&modes, RIG_MODE_PKTFM, p); }
else if (streq(p, "DATA-LSB")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "DATA-USB")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DATA-U")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DIG")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DIGI")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "DATA-L")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "DATA-R")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "FM")) { modeMapAdd(&modes, RIG_MODE_FM, p); }
else if (streq(p, "FM-D")) { modeMapAdd(&modes, RIG_MODE_PKTFM, p); }
else if (streq(p, "FMN")) { modeMapAdd(&modes, RIG_MODE_FMN, p); }
else if (streq(p, "FM-N")) { modeMapAdd(&modes, RIG_MODE_FMN, p); }
else if (streq(p, "FMW")) { modeMapAdd(&modes, RIG_MODE_WFM, p); }
else if (streq(p, "FSK")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "FSK-R")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "LCW")) { modeMapAdd(&modes, RIG_MODE_CWR, p); }
else if (streq(p, "LSB")) { modeMapAdd(&modes, RIG_MODE_LSB, p); }
else if (streq(p, "LSB-D")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "LSB-D1")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "LSB-D2")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "LSB-D3")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "NFM")) { modeMapAdd(&modes, RIG_MODE_FMN, p); }
else if (streq(p, "PKT")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "PKT-FM")) { modeMapAdd(&modes, RIG_MODE_PKTFM, p); }
else if (streq(p, "PKT-L")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "PKT-U")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "PKT(L)")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "PKT(U)")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "PSK")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "PSK-L")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "PSK-R")) { modeMapAdd(&modes, RIG_MODE_PKTLSB, p); }
else if (streq(p, "PSK-U")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "RTTY")) { modeMapAdd(&modes, RIG_MODE_RTTY, p); }
else if (streq(p, "RTTY-L")) { modeMapAdd(&modes, RIG_MODE_RTTYR, p); }
else if (streq(p, "RTTY-R")) { modeMapAdd(&modes, RIG_MODE_RTTYR, p); }
else if (streq(p, "RTTY-U")) { modeMapAdd(&modes, RIG_MODE_RTTY, p); }
else if (streq(p, "RTTY(U)")) { modeMapAdd(&modes, RIG_MODE_RTTY, p); }
else if (streq(p, "RTTY(R")) { modeMapAdd(&modes, RIG_MODE_RTTYR, p); }
else if (streq(p, "SAH")) { modeMapAdd(&modes, RIG_MODE_SAH, p); }
else if (streq(p, "SAL")) { modeMapAdd(&modes, RIG_MODE_SAL, p); }
else if (streq(p, "SAM")) { modeMapAdd(&modes, RIG_MODE_SAM, p); }
else if (streq(p, "USB")) { modeMapAdd(&modes, RIG_MODE_USB, p); }
else if (streq(p, "USB-D")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "USB-D1")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "USB-D2")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "USB-D3")) { modeMapAdd(&modes, RIG_MODE_PKTUSB, p); }
else if (streq(p, "W-FM")) { modeMapAdd(&modes, RIG_MODE_WFM, p); }
else if (streq(p, "WFM")) { modeMapAdd(&modes, RIG_MODE_WFM, p); }
else if (streq(p, "UCW")) { modeMapAdd(&modes, RIG_MODE_CW, p); }
else { rig_debug(RIG_DEBUG_ERR, "%s: Unknown mode (new?) for this rig='%s'\n", __func__, p); }
}
rig->state.mode_list = modes;
retval = rig_strrmodes(modes, value, sizeof(value));
if (retval != RIG_OK) // we might get TRUNC but we can still print the debug
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: %s\n", __func__, rigerror(retval));
}
rig_debug(RIG_DEBUG_VERBOSE, "%s: hamlib modes=%s\n", __func__, value);
return retval;
}
/*
* flrig_close
* Assumes rig!=NULL
*/
static int flrig_close(RIG *rig)
{
rig_debug(RIG_DEBUG_TRACE, "%s\n", __func__);
return RIG_OK;
}
/*
* flrig_cleanup
* Assumes rig!=NULL, rig->state.priv!=NULL
*/
static int flrig_cleanup(RIG *rig)
{
int i;
if (!rig)
{
return -RIG_EINVAL;
}
free(rig->state.priv);
rig->state.priv = NULL;
for (i = 0; modeMap[i].mode_hamlib != 0; ++i)
{
if (modeMap[i].mode_flrig)
{
free(modeMap[i].mode_flrig);
modeMap[i].mode_flrig = NULL;
}
}
return RIG_OK;
}
/*
* flrig_get_freq
* Assumes rig!=NULL, rig->state.priv!=NULL, freq!=NULL
*/
static int flrig_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
int retries = 10;
char xml[MAXXMLLEN];
char value[MAXCMDLEN];
struct flrig_priv_data *priv = (struct flrig_priv_data *) rig->state.priv;
rig_debug(RIG_DEBUG_TRACE, "%s: vfo=%s\n", __func__,
rig_strvfo(vfo));
if (check_vfo(vfo) == FALSE)
{
rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n",
__func__, rig_strvfo(vfo));
return -RIG_EINVAL;
}
if (vfo == RIG_VFO_CURR)
{
vfo = priv->curr_vfo;
rig_debug(RIG_DEBUG_TRACE, "%s: get_freq2 vfo=%s\n",
__func__, rig_strvfo(vfo));
}
do
{
char *pxml;
int retval;
if (vfo == RIG_VFO_A)
{
pxml = xml_build("rig.get_vfoA", NULL, xml, sizeof(xml));
}
else
{
pxml = xml_build("rig.get_vfoB", NULL, xml, sizeof(xml));
}
retval = write_transaction(rig, pxml, strlen(pxml));
if (retval < 0)
{
return retval;
}
read_transaction(rig, xml, sizeof(xml));
xml_parse(xml, value, sizeof(value));
if (strlen(value) == 0)
{
rig_debug(RIG_DEBUG_ERR, "%s: retries=%d\n", __func__, retries);
//hl_usleep(10*1000);
}
}
while (--retries && strlen(value) == 0);
*freq = atof(value);
if (*freq == 0)
{
rig_debug(RIG_DEBUG_ERR, "%s: freq==0??\nvalue=%s\n", __func__,
value);
return -(102 + RIG_EPROTO);
}
else
{
rig_debug(RIG_DEBUG_TRACE, "%s: freq=%.0f\n", __func__, *freq);
}
if (vfo == RIG_VFO_A)
{
priv->curr_freqA = *freq;
}
else
{
priv->curr_freqB = *freq;
}
return RIG_OK;
}
/*
* flrig_set_freq
* assumes rig!=NULL, rig->state.priv!=NULL
*/
static int flrig_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
int retval;
char value[MAXXMLLEN];
char xml[MAXXMLLEN];
char *pxml;
struct flrig_priv_data *priv = (struct flrig_priv_data *) rig->state.priv;