forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kenwood.c
6195 lines (4992 loc) · 154 KB
/
kenwood.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 Kenwood backend - main file
* Copyright (c) 2000-2011 by Stephane Fillod
* Copyright (C) 2009,2010 Alessandro Zummo <a.zummo@towertech.it>
* Copyright (C) 2009,2010,2011,2012,2013 by Nate Bargmann, n0nb@n0nb.us
*
*
* 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
*
*/
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <ctype.h>
#include "hamlib/rig.h"
#include "serial.h"
#include "register.h"
#include "cal.h"
#include "cache.h"
#include "misc.h"
#include "kenwood.h"
#include "ts990s.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
struct kenwood_id
{
rig_model_t model;
int id;
};
struct kenwood_id_string
{
rig_model_t model;
const char *id;
};
#define UNKNOWN_ID -1
/*
* Identification number as returned by "ID;"
* Please, if the model number of your rig is listed as UNKNOWN_ID,
* send the value to <fillods@users.sourceforge.net> for inclusion. Thanks --SF
*
* TODO: sort this list with most frequent rigs first.
*/
static const struct kenwood_id kenwood_id_list[] =
{
{ RIG_MODEL_TS940, 1 },
{ RIG_MODEL_TS811, 2 },
{ RIG_MODEL_TS711, 3 },
{ RIG_MODEL_TS440, 4 },
{ RIG_MODEL_R5000, 5 },
{ RIG_MODEL_TS140S, 6 },
// { RIG_MODEL_TS680S, 6 }, // The TS680S is supposed #6 too but it will return as TS140S since it matches it
{ RIG_MODEL_TS790, 7 },
{ RIG_MODEL_TS950S, 8 },
{ RIG_MODEL_TS850, 9 },
{ RIG_MODEL_TS450S, 10 },
{ RIG_MODEL_TS690S, 11 },
{ RIG_MODEL_TS950SDX, 12 },
{ RIG_MODEL_TS50, 13 },
{ RIG_MODEL_TS870S, 15 },
{ RIG_MODEL_TRC80, 16 },
{ RIG_MODEL_TS570D, 17 }, /* Elecraft K2|K3 also returns 17 */
{ RIG_MODEL_TS570S, 18 },
{ RIG_MODEL_TS2000, 19 },
{ RIG_MODEL_TS480, 20 },
{ RIG_MODEL_TS590S, 21 },
{ RIG_MODEL_TS990S, 22 },
{ RIG_MODEL_TS590SG, 23 },
{ RIG_MODEL_TS890S, 24 },
{ RIG_MODEL_NONE, UNKNOWN_ID }, /* end marker */
};
/* XXX numeric ids have been tested only with the TS-450 */
static const struct kenwood_id_string kenwood_id_string_list[] =
{
{ RIG_MODEL_TS940, "001" },
{ RIG_MODEL_TS811, "002" },
{ RIG_MODEL_TS711, "003" },
{ RIG_MODEL_TS440, "004" },
{ RIG_MODEL_R5000, "005" },
{ RIG_MODEL_TS140S, "006" },
{ RIG_MODEL_TS790, "007" },
{ RIG_MODEL_TS950S, "008" },
{ RIG_MODEL_TS850, "009" },
{ RIG_MODEL_TS450S, "010" },
{ RIG_MODEL_TS690S, "011" },
{ RIG_MODEL_TS950SDX, "012" },
{ RIG_MODEL_TS50, "013" },
{ RIG_MODEL_TS870S, "015" },
{ RIG_MODEL_TS570D, "017" }, /* Elecraft K2|K3|KX3 also returns 17 */
{ RIG_MODEL_TS570S, "018" },
{ RIG_MODEL_TS2000, "019" },
{ RIG_MODEL_TS480, "020" },
{ RIG_MODEL_PT8000A, "020" }, // TS480 ID but behaves differently
{ RIG_MODEL_SDRUNO, "020" }, // TS480 ID but behaves differently
{ RIG_MODEL_TS590S, "021" },
{ RIG_MODEL_TS990S, "022" },
{ RIG_MODEL_TS590SG, "023" },
{ RIG_MODEL_TS890S, "024" },
{ RIG_MODEL_THD7A, "TH-D7" },
{ RIG_MODEL_THD7AG, "TH-D7G" },
{ RIG_MODEL_TMD700, "TM-D700" },
{ RIG_MODEL_TMD710, "TM-D710" },
{ RIG_MODEL_THD72A, "TH-D72" },
{ RIG_MODEL_THD74, "TH-D74" },
{ RIG_MODEL_TMV7, "TM-V7" },
{ RIG_MODEL_TMV71, "TM-V71" },
{ RIG_MODEL_THF6A, "TH-F6" },
{ RIG_MODEL_THF7E, "TH-F7" },
{ RIG_MODEL_THG71, "TH-G71" },
{ RIG_MODEL_MALACHITE, "020" },
{ RIG_MODEL_NONE, NULL }, /* end marker */
};
rmode_t kenwood_mode_table[KENWOOD_MODE_TABLE_MAX] =
{
[0] = RIG_MODE_NONE,
[1] = RIG_MODE_LSB,
[2] = RIG_MODE_USB,
[3] = RIG_MODE_CW,
[4] = RIG_MODE_FM,
[5] = RIG_MODE_AM,
[6] = RIG_MODE_RTTY, // FSK Mode
[7] = RIG_MODE_CWR,
[8] = RIG_MODE_NONE, /* TUNE mode or PKTUSB for SDRUNO */
[9] = RIG_MODE_RTTYR, // FSKR Mode
[10] = RIG_MODE_PSK,
[11] = RIG_MODE_PSKR,
[12] = RIG_MODE_PKTLSB,
[13] = RIG_MODE_PKTUSB,
[14] = RIG_MODE_PKTFM,
[15] = RIG_MODE_PKTAM
};
/*
* 38 CTCSS sub-audible tones
*/
tone_t kenwood38_ctcss_list[] =
{
670, 719, 744, 770, 797, 825, 854, 885, 915, 948,
974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, 1318,
1365, 1413, 1462, 1514, 1567, 1622, 1679, 1738, 1799, 1862,
1928, 2035, 2107, 2181, 2257, 2336, 2418, 2503,
0,
};
/*
* 42 CTCSS sub-audible tones
*/
tone_t kenwood42_ctcss_list[] =
{
670, 693, 719, 744, 770, 797, 825, 854, 885, 915, 948,
974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, 1318,
1365, 1413, 1462, 1514, 1567, 1622, 1679, 1738, 1799, 1862,
1928, 2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541,
0,
};
/* Token definitions for .cfgparams in rig_caps
*
* See enum rig_conf_e and struct confparams in rig.h
*/
struct confparams kenwood_cfg_params[] =
{
{
TOK_FINE, "fine", "Fine", "Fine step mode",
NULL, RIG_CONF_CHECKBUTTON, { }
},
{
TOK_VOICE, "voice", "Voice", "Voice recall",
NULL, RIG_CONF_BUTTON, { }
},
{
TOK_XIT, "xit", "XIT", "XIT",
NULL, RIG_CONF_CHECKBUTTON, { }
},
{
TOK_RIT, "rit", "RIT", "RIT",
NULL, RIG_CONF_CHECKBUTTON, { }
},
{
TOK_NO_ID, "no_id", "No ID", "If true do not send ID; with set commands",
NULL, RIG_CONF_CHECKBUTTON, { }
},
{ RIG_CONF_END, NULL, }
};
/**
* kenwood_transaction
* Assumes rig!=NULL rig->state!=NULL rig->caps!=NULL
*
* Parameters:
* cmdstr: Command to be sent to the rig. cmdstr can also be NULL,
* indicating that only a reply is needed (nothing will be sent).
* data: Buffer for reply string. Can be NULL, indicating that no reply
* is needed and will return with RIG_OK after command was sent.
* datasize: Size of buffer. It is the caller's responsibily to provide
* a large enough buffer for all possible replies for a command.
*
* returns:
* RIG_OK - if no error occurred.
* RIG_EIO - if an I/O error occurred while sending/receiving data.
* RIG_ETIMEOUT - if timeout expires without any characters received.
* RIG_REJECTED - if a negative acknowledge was received or command not
* recognized by rig.
*/
int kenwood_transaction(RIG *rig, const char *cmdstr, char *data,
size_t datasize)
{
char buffer[KENWOOD_MAX_BUF_LEN]; /* use our own buffer since
verification may need a longer
buffer than the user supplied one */
char cmdtrm_str[2]; /* Default Command/Reply termination char */
int retval = -RIG_EINTERNAL;
char *cmd;
int len;
int retry_read = 0;
struct kenwood_priv_data *priv = rig->state.priv;
struct kenwood_priv_caps *caps = kenwood_caps(rig);
struct rig_state *rs;
if (datasize > 0 && datasize < (cmdstr ? strlen(cmdstr) : 0))
{
rig_debug(RIG_DEBUG_WARN,
"%s called cmd=%s datasize=%d, datasize < cmd length?\n", __func__,
cmdstr ? cmdstr : "(NULL)",
(int)datasize);
}
else
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called cmd=%s\n", __func__,
cmdstr ? cmdstr : "(NULL)");
}
if ((!cmdstr && !datasize) || (datasize && !data))
{
RETURNFUNC2(-RIG_EINVAL);
}
rs = &rig->state;
rs->transaction_active = 1;
/* Emulators don't need any post_write_delay */
if (priv->is_emulation) { rs->rigport.post_write_delay = 0; }
// if this is an IF cmdstr and not the first time through check cache
if (cmdstr && strcmp(cmdstr, "IF") == 0 && priv->cache_start.tv_sec != 0)
{
int cache_age_ms;
cache_age_ms = elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_GET);
if (cache_age_ms < 500) // 500ms cache time
{
rig_debug(RIG_DEBUG_TRACE, "%s(%d): cache hit, age=%dms\n", __func__, __LINE__,
cache_age_ms);
if (data) { strncpy(data, priv->last_if_response, datasize); }
RETURNFUNC2(RIG_OK);
}
// else we drop through and do the real IF command
}
if (cmdstr && (strlen(cmdstr) > 2 || strcmp(cmdstr, "RX") == 0
|| strncmp(cmdstr, "TX", 2) == 0 || strncmp(cmdstr, "ZZTX", 4)) == 0)
{
// then we must be setting something so we'll invalidate the cache
rig_debug(RIG_DEBUG_TRACE, "%s: cache invalidated\n", __func__);
priv->cache_start.tv_sec = 0;
}
cmdtrm_str[0] = caps->cmdtrm;
cmdtrm_str[1] = '\0';
transaction_write:
if (cmdstr)
{
rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, cmdstr);
len = strlen(cmdstr);
cmd = calloc(1, len + 2);
if (cmd == NULL)
{
retval = -RIG_ENOMEM;
goto transaction_quit;
}
memcpy(cmd, cmdstr, len);
/* XXX the if is temporary, until all invocations are fixed */
if (cmdstr[len - 1] != ';' && cmdstr[len - 1] != '\r')
{
cmd[len] = caps->cmdtrm;
len++;
}
/* flush anything in the read buffer before command is sent */
rig_flush(&rs->rigport);
retval = write_block(&rs->rigport, (unsigned char *) cmd, len);
free(cmd);
if (retval != RIG_OK)
{
goto transaction_quit;
}
}
// we're not going to do the verify on RX cmd
// Seems some rigs (like TS-480) return "?" when RX is done while PTT=OFF
// So we'll skip the checks just on this one command for now
// The TS-480 PC Control says RX; should return RX0; but it doesn't
// We may eventually want to verify PTT with rig_get_ptt instead
// The TS-2000 doesn't like doing an ID right after RU or RD
if (retval == RIG_OK)
{
int skip = strncmp(cmdstr, "RX", 2) == 0;
skip |= strncmp(cmdstr, "RU", 2) == 0;
skip |= strncmp(cmdstr, "RD", 2) == 0;
skip |= strncmp(cmdstr, "KYW", 3) == 0;
skip |= strncmp(cmdstr, "KY ", 3) == 0;
skip |= strncmp(cmdstr, "PS1", 3) == 0;
skip |= strncmp(cmdstr, "PS0", 3) == 0;
skip |= strncmp(cmdstr, "K22", 3) == 0;
if (skip)
{
// most command we give them a little time -- but not KY
if (strncmp(cmdstr, "KY ", 3) != 0)
{
hl_usleep(200 * 1000); // give little settle time for these commands
}
goto transaction_quit;
}
}
// Malachite SDR cannot send ID after FA
if (!datasize && priv->no_id) { RETURNFUNC2(RIG_OK); }
if (!datasize && strncmp(cmdstr, "KY", 2) != 0)
{
rig->state.transaction_active = 0;
// there are some commands that have problems with immediate follow-up
// so we'll just ignore them
/* no reply expected so we need to write a command that always
gives a reply so we can read any error replies from the actual
command being sent without blocking */
if (RIG_OK != (retval = write_block(&rs->rigport,
(unsigned char *) priv->verify_cmd, strlen(priv->verify_cmd))))
{
goto transaction_quit;
}
}
transaction_read:
/* allow room for most any response */
// this len/expected stuff is confusing -- logic in some places includes the semicolon
// so we add 1 to our read_string length to cover these cases
// eventually we should be able to get rid of this but requires testing all Kenwood rigs
len = min(datasize ? datasize + 1 : strlen(priv->verify_cmd) + 48,
KENWOOD_MAX_BUF_LEN);
retval = read_string(&rs->rigport, (unsigned char *) buffer, len,
cmdtrm_str, strlen(cmdtrm_str), 0, 1);
rig_debug(RIG_DEBUG_TRACE, "%s: read_string len=%d '%s'\n", __func__,
(int)strlen(buffer), buffer);
if (retval < 0)
{
rig_debug(RIG_DEBUG_WARN,
"%s: read_string retval < 0, retval = %d, retry_read=%d, rs->rigport.retry=%d\n",
__func__,
retval, retry_read, rs->rigport.retry);
// only retry if we expect a response from the command
if (retry_read++ < rs->rigport.retry)
{
goto transaction_write;
// we use to not re-do the write
// but now we use ID; to verify commands are working
// so in order to retry commands need to re-write them
// https://github.com/Hamlib/Hamlib/issues/983
#if 0
if (datasize)
{
goto transaction_write;
}
else if (-RIG_ETIMEOUT == retval)
{
goto transaction_read;
}
#endif
}
goto transaction_quit;
}
/* Check that command termination is correct */
if (strchr(cmdtrm_str, buffer[strlen(buffer) - 1]) == NULL)
{
rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n",
__func__, buffer);
if (retry_read++ < rs->rigport.retry)
{
goto transaction_write;
}
retval = -RIG_EPROTO;
goto transaction_quit;
}
if (strlen(buffer) == 2)
{
switch (buffer[0])
{
case 'N':
/* Command recognised by rig but invalid data entered. */
if (cmdstr)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: NegAck for '%s'\n", __func__, cmdstr);
}
retval = -RIG_ENAVAIL;
goto transaction_quit;
case 'O':
/* Too many characters sent without a carriage return */
if (cmdstr)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: Overflow for '%s'\n", __func__, cmdstr);
}
if (retry_read++ < rs->rigport.retry)
{
goto transaction_write;
}
retval = -RIG_EPROTO;
goto transaction_quit;
case 'E':
/* Communication error */
if (cmdstr)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: Communication error for '%s'\n", __func__,
cmdstr);
}
if (retry_read++ < rs->rigport.retry)
{
goto transaction_write;
}
retval = -RIG_EIO;
goto transaction_quit;
case '?':
/* The ? response is an ambiguous response, but for get commands it seems to
* indicate that the rig rejected the command because the state of the rig is not valid for the command
* or that the command parameter is invalid. Retrying the command does not fix the issue,
* as the error is caused by the an invalid combination of rig state.
*
* For example, the following cases have been observed:
* - NL (NB level) and RL (NR level) commands fail if NB / NR are not enabled on TS-590SG
* - SH and SL (filter width) fail in CW mode on TS-590SG
* - GT (AGC) fails in FM mode on TS-590SG
*
* There are more cases like these and they vary by rig model.
*/
if (priv->question_mark_response_means_rejected)
{
rig_debug(RIG_DEBUG_ERR, "%s: Command rejected by the rig (get): '%s'\n",
__func__, cmdstr);
RETURNFUNC(-RIG_ERJCTED);
}
/* Command not understood by rig or rig busy */
if (cmdstr)
{
rig_debug(RIG_DEBUG_ERR, "%s: Unknown command or rig busy '%s'\n", __func__,
cmdstr);
// sometimes IF; command after TX; will return ? but still return IF response
if (retry_read++ <= 1)
{
hl_usleep(100 * 1000);
goto transaction_read;
}
}
if (retry_read++ < rs->rigport.retry)
{
rig_debug(RIG_DEBUG_ERR, "%s: Retrying shortly %d of %d\n", __func__,
retry_read, rs->rigport.retry);
hl_usleep(rig->caps->timeout * 1000);
goto transaction_write;
}
retval = -RIG_ERJCTED;
goto transaction_quit;
}
}
/*
* Check that we received the correct reply. The first two characters
* should be the same as command. Because the Elecraft XG3 uses
* single character commands we only check the first character in
* that case.
*/
if (datasize)
{
// we ignore the special PS command
if (cmdstr && strcmp(cmdstr, "PS") != 0 && (buffer[0] != cmdstr[0]
|| (cmdstr[1] && buffer[1] != cmdstr[1])))
{
/*
* TODO: When RIG_TRN is enabled, we can pass the string to
* the decoder for callback. That way we don't ignore any
* commands.
*/
rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command %c%c\n",
__func__, buffer[0], buffer[1], cmdstr[0], cmdstr[1]);
rig_debug(RIG_DEBUG_ERR, "%s: retry_read=%d, rs->rigport.retry=%d\n", __func__,
retry_read, rs->rigport.retry);
if (retry_read++ < rs->rigport.retry)
{
if (strlen(buffer) == 0)
{
goto transaction_write; // didn't get an answer so send again
}
else
{
// should be able to handle transceive mode here
goto transaction_read; // might be an async or corrupt reply so we'll read until timeout
}
}
retval = -RIG_EPROTO;
goto transaction_quit;
}
if (retval > 0)
{
/* move the result excluding the command terminator into the
caller buffer */
len = min(datasize, retval) - 1;
strncpy(data, buffer, len);
data[len] = '\0';
}
}
else
{
rig_debug(RIG_DEBUG_TRACE, "%s: No data expected, checking %s in %s\n",
__func__,
priv->verify_cmd, buffer);
// seems some rigs will send back an IF response to RX/TX when it changes the status
// normally RX/TX returns nothing when it's a null effect
// TS-950SDX is known to behave this way
if (strncmp(cmdstr, "RX", 2) == 0 || strncmp(cmdstr, "TX", 2) == 0)
{
if (strncmp(priv->verify_cmd, "IF", 2) == 0)
{
rig_debug(RIG_DEBUG_TRACE, "%s: RX/TX got IF response so we're good\n",
__func__);
goto transaction_quit;
}
}
if (priv->verify_cmd[0] != buffer[0]
|| (priv->verify_cmd[1] && priv->verify_cmd[1] != buffer[1]))
{
/*
* TODO: When RIG_TRN is enabled, we can pass the string to
* the decoder for callback. That way we don't ignore any
* commands.
*/
// if we got FA or FB unexpectedly then perhaps RIG_TRN is enabled and we just need to handle it
if (strncmp(buffer, "FA", 2) == 0)
{
freq_t freq;
sscanf(buffer, "FA%lg", &freq);
rig_set_cache_freq(rig, RIG_VFO_A, freq);
goto transaction_read;
}
else if (strncmp(buffer, "FB", 2) == 0)
{
freq_t freq;
sscanf(buffer, "FB%lg", &freq);
rig_set_cache_freq(rig, RIG_VFO_B, freq);
goto transaction_read;
}
rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command verification %c%c\n",
__func__, buffer[0], buffer[1]
, priv->verify_cmd[0], priv->verify_cmd[1]);
if (retry_read++ < rs->rigport.retry)
{
goto transaction_write;
}
retval = -RIG_EPROTO;
goto transaction_quit;
}
}
retval = RIG_OK;
rig_debug(RIG_DEBUG_TRACE, "%s: returning RIG_OK, retval=%d\n", __func__,
retval);
transaction_quit:
// update the cache
if (retval == RIG_OK && cmdstr && strcmp(cmdstr, "IF") == 0)
{
elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_SET);
strncpy(priv->last_if_response, buffer, caps->if_len);
}
rs->transaction_active = 0;
RETURNFUNC2(retval);
}
/**
* kenwood_safe_transaction
* A wrapper for kenwood_transaction to check returned data against
* expected length,
*
* Parameters:
* cmd Same as kenwood_transaction() cmdstr
* buf Same as kenwwod_transaction() data
* buf_size Same as kenwood_transaction() datasize
* expected Value of expected string length
*
* Returns:
* RIG_OK - if no error occurred.
* RIG_EPROTO if returned string and expected are not equal
* Error from kenwood_transaction() if any
*
*/
int kenwood_safe_transaction(RIG *rig, const char *cmd, char *buf,
size_t buf_size, size_t expected)
{
int err;
int retry = 0;
rig_debug(RIG_DEBUG_VERBOSE, "%s called, cmd=%s, expected=%d\n", __func__, cmd,
(int)expected);
if (!cmd)
{
RETURNFUNC2(-RIG_EINVAL);
}
memset(buf, 0, buf_size);
if (expected == 0)
{
buf_size = 0;
}
do
{
size_t length;
// some PowerSDR commands have variable len
int checklen = !RIG_IS_POWERSDR && !RIG_IS_THETIS;
err = kenwood_transaction(rig, cmd, buf, buf_size);
if (err != RIG_OK) /* return immediately on error as any
retries handled at lower level */
{
RETURNFUNC2(err);
}
length = strlen(buf);
if (checklen && length != expected) /* worth retrying as some rigs
occasionally send short results */
{
// QRPLABS can't seem top decide if they give 37 or 38 bytes for IF command
if (strncmp(cmd, "IF", 2) == 0 && rig->caps->rig_model == RIG_MODEL_QRPLABS) { break; }
struct kenwood_priv_data *priv = rig->state.priv;
rig_debug(RIG_DEBUG_ERR,
"%s: wrong answer; len for cmd %s: expected = %d, got %d\n",
__func__, cmd, (int)expected, (int)length);
err = -RIG_EPROTO;
elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_INVALIDATE);
hl_usleep(50 * 1000); // let's do a short wait
}
}
while (err != RIG_OK && ++retry < rig->state.rigport.retry);
RETURNFUNC2(err);
}
rmode_t kenwood2rmode(unsigned char mode, const rmode_t mode_table[])
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
if (mode >= KENWOOD_MODE_TABLE_MAX)
{
return (RIG_MODE_NONE);
}
return (mode_table[mode]);
}
char rmode2kenwood(rmode_t mode, const rmode_t mode_table[])
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called, mode=%s\n", __func__,
rig_strrmode(mode));
if (mode != RIG_MODE_NONE)
{
int i;
for (i = 0; i < KENWOOD_MODE_TABLE_MAX; i++)
{
if (mode_table[i] == mode)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: returning %d\n", __func__, i);
return (i);
}
}
}
return (-1);
}
int kenwood_init(RIG *rig)
{
struct kenwood_priv_data *priv;
struct kenwood_priv_caps *caps = kenwood_caps(rig);
rig_debug(RIG_DEBUG_VERBOSE, "%s called, version %s/%s\n", __func__,
BACKEND_VER, rig->caps->version);
rig->state.priv = calloc(1, sizeof(struct kenwood_priv_data));
if (rig->state.priv == NULL)
{
RETURNFUNC2(-RIG_ENOMEM);
}
priv = rig->state.priv;
memset(priv, 0x00, sizeof(struct kenwood_priv_data));
if (RIG_IS_XG3)
{
priv->verify_cmd[0] = caps->cmdtrm;
priv->verify_cmd[1] = '\0';
}
else
{
priv->verify_cmd[0] = 'I';
priv->verify_cmd[1] = 'D';
priv->verify_cmd[2] = caps->cmdtrm;
priv->verify_cmd[3] = '\0';
}
priv->split = RIG_SPLIT_OFF;
priv->trn_state = -1;
priv->curr_mode = 0;
priv->micgain_min = -1;
priv->micgain_max = -1;
priv->has_ps = 1; // until proven otherwise
if (rig->caps->rig_model == RIG_MODEL_TS450S
|| rig->caps->rig_model == RIG_MODEL_TS50
|| rig->caps->rig_model == RIG_MODEL_TS140S
|| rig->caps->rig_model == RIG_MODEL_TS2000
|| rig->caps->rig_model == RIG_MODEL_TS440)
{
priv->has_ps = 0;
}
/* default mode_table */
if (caps->mode_table == NULL)
{
caps->mode_table = kenwood_mode_table;
}
/* default if_len */
if (caps->if_len == 0)
{
caps->if_len = 37;
}
priv->ag_format = -1; // force determination of AG format
rig_debug(RIG_DEBUG_TRACE, "%s: if_len = %d\n", __func__, caps->if_len);
// SDRUno uses mode 8 for DIG
if (rig->caps->rig_model == RIG_MODEL_SDRUNO)
{
kenwood_mode_table[8] = RIG_MODE_PKTUSB;
}
RETURNFUNC2(RIG_OK);
}
int kenwood_cleanup(RIG *rig)
{
ENTERFUNC;
free(rig->state.priv);
rig->state.priv = NULL;
RETURNFUNC(RIG_OK);
}
int kenwood_open(RIG *rig)
{
struct kenwood_priv_data *priv = rig->state.priv;
struct kenwood_priv_caps *caps = kenwood_caps(rig);
int err, i;
char *idptr;
char id[KENWOOD_MAX_BUF_LEN];
int retry_save = rig->state.rigport.retry;
ENTERFUNC;
id[0] = 0;
rig->state.rigport.retry = 0;
priv->question_mark_response_means_rejected = 0;
if (rig->state.auto_power_on)
{
// Ensure rig is on
rig_set_powerstat(rig, 1);
sleep(1);
}
err = kenwood_get_id(rig, id);
if (err != RIG_OK)
{
// TS450S is flaky on the 1st ID call so we'll try again
hl_usleep(200 * 1000);
err = kenwood_get_id(rig, id);
}
if (err == RIG_OK && priv->has_ps) // some rigs give ID while in standby
{
powerstat_t powerstat = 0;
rig_debug(RIG_DEBUG_TRACE, "%s: got ID so try PS\n", __func__);
err = rig_get_powerstat(rig, &powerstat);
if (err == RIG_OK && powerstat == 0 && priv->poweron == 0
&& rig->state.auto_power_on)
{
priv->has_ps = 1;
rig_debug(RIG_DEBUG_TRACE, "%s: got PS0 so powerup\n", __func__);
rig_set_powerstat(rig, 1);
}
else if (err == -RIG_ETIMEOUT) // Some rigs like TS-450 don't have PS cmd
{
priv->has_ps = 0;
}
priv->poweron = 1;
err = RIG_OK; // reset our err back to OK for later checks
}
if (RIG_OK != err)
{
rig_debug(RIG_DEBUG_ERR,
"%s: no response to get_id from rig...continuing anyway\n", __func__);
}
if (RIG_IS_TS2000
|| RIG_IS_TS480
|| RIG_IS_TS590S
|| RIG_IS_TS590SG
|| RIG_IS_TS890S
|| RIG_IS_TS990S)
{
// rig has Set 2 RIT/XIT function
rig_debug(RIG_DEBUG_TRACE, "%s: rig has_rit2\n", __func__);
priv->has_rit2 = 1;
}
if (RIG_IS_TS590S)
{
/* we need the firmware version for these rigs to deal with f/w defects */
static char fw_version[7];
err = kenwood_transaction(rig, "FV", fw_version, sizeof(fw_version));
if (RIG_OK != err)
{
rig_debug(RIG_DEBUG_ERR, "%s: cannot get f/w version, defaulting to 1.0\n",
__func__);
rig->state.rigport.retry = retry_save;
priv->fw_rev_uint = 100;
}
else
{
char *dot_pos;
/* store the data after the "FV" which should be a f/w version
string of the form n.n e.g. 1.07 */
priv->fw_rev = &fw_version[2];
dot_pos = strchr(fw_version, '.');
if (dot_pos)
{
priv->fw_rev_uint = atoi(&fw_version[2]) * 100 + atoi(dot_pos + 1);
}
else
{
rig_debug(RIG_DEBUG_ERR, "%s: cannot get f/w version\n", __func__);
rig->state.rigport.retry = retry_save;
RETURNFUNC(-RIG_EPROTO);
}
}
rig_debug(RIG_DEBUG_TRACE, "%s: found f/w version %.1f\n", __func__,
priv->fw_rev_uint / 100.0);
}
if (!RIG_IS_XG3 && -RIG_ETIMEOUT == err)
{
char buffer[KENWOOD_MAX_BUF_LEN];
/* Some Kenwood emulations have no ID command response :(
* Try an FA command to see if anyone is listening */
err = kenwood_transaction(rig, "FA", buffer, sizeof(buffer));
if (RIG_OK != err)
{
rig_debug(RIG_DEBUG_ERR, "%s: no response from rig\n", __func__);
rig->state.rigport.retry = retry_save;
RETURNFUNC(err);
}
/* here we know there is something that responds to FA but not
to ID so use FA as the command verification command */
priv->verify_cmd[0] = 'F';
priv->verify_cmd[1] = 'A';
priv->verify_cmd[2] = caps->cmdtrm;
priv->verify_cmd[3] = '\0';
strcpy(id, "ID019"); /* fake a TS-2000 */
}
else
{
if (err != RIG_OK)