1 /*
2 * Hamlib Kenwood backend - main file
3 * Copyright (c) 2000-2011 by Stephane Fillod
4 * Copyright (C) 2009,2010 Alessandro Zummo <a.zummo@towertech.it>
5 * Copyright (C) 2009,2010,2011,2012,2013 by Nate Bargmann, n0nb@n0nb.us
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h> /* String function definitions */
30 #include <unistd.h> /* UNIX standard function definitions */
31 #include <ctype.h>
32
33 #include "hamlib/rig.h"
34 #include "network.h"
35 #include "serial.h"
36 #include "register.h"
37 #include "cal.h"
38
39 #include "kenwood.h"
40 #include "ts990s.h"
41
42 #ifndef max
43 #define max(a,b) (((a) > (b)) ? (a) : (b))
44 #define min(a,b) (((a) < (b)) ? (a) : (b))
45 #endif
46
47 struct kenwood_id
48 {
49 rig_model_t model;
50 int id;
51 };
52
53 struct kenwood_id_string
54 {
55 rig_model_t model;
56 const char *id;
57 };
58
59 #define UNKNOWN_ID -1
60
61 /*
62 * Identification number as returned by "ID;"
63 * Please, if the model number of your rig is listed as UNKNOWN_ID,
64 * send the value to <fillods@users.sourceforge.net> for inclusion. Thanks --SF
65 *
66 * TODO: sort this list with most frequent rigs first.
67 */
68 static const struct kenwood_id kenwood_id_list[] =
69 {
70 { RIG_MODEL_TS940, 1 },
71 { RIG_MODEL_TS811, 2 },
72 { RIG_MODEL_TS711, 3 },
73 { RIG_MODEL_TS440, 4 },
74 { RIG_MODEL_R5000, 5 },
75 { RIG_MODEL_TS140S, 6 },
76 // { RIG_MODEL_TS680S, 6 }, // The TS680S is supposed #6 too but it will return as TS140S since it matches it
77 { RIG_MODEL_TS790, 7 },
78 { RIG_MODEL_TS950S, 8 },
79 { RIG_MODEL_TS850, 9 },
80 { RIG_MODEL_TS450S, 10 },
81 { RIG_MODEL_TS690S, 11 },
82 { RIG_MODEL_TS950SDX, 12 },
83 { RIG_MODEL_TS50, 13 },
84 { RIG_MODEL_TS870S, 15 },
85 { RIG_MODEL_TRC80, 16 },
86 { RIG_MODEL_TS570D, 17 }, /* Elecraft K2|K3 also returns 17 */
87 { RIG_MODEL_TS570S, 18 },
88 { RIG_MODEL_TS2000, 19 },
89 { RIG_MODEL_TS480, 20 },
90 { RIG_MODEL_TS590S, 21 },
91 { RIG_MODEL_TS990S, 22 },
92 { RIG_MODEL_TS590SG, 23 },
93 { RIG_MODEL_TS890S, 24 },
94 { RIG_MODEL_NONE, UNKNOWN_ID }, /* end marker */
95 };
96
97 /* XXX numeric ids have been tested only with the TS-450 */
98 static const struct kenwood_id_string kenwood_id_string_list[] =
99 {
100 { RIG_MODEL_TS940, "001" },
101 { RIG_MODEL_TS811, "002" },
102 { RIG_MODEL_TS711, "003" },
103 { RIG_MODEL_TS440, "004" },
104 { RIG_MODEL_R5000, "005" },
105 { RIG_MODEL_TS140S, "006" },
106 { RIG_MODEL_TS790, "007" },
107 { RIG_MODEL_TS950S, "008" },
108 { RIG_MODEL_TS850, "009" },
109 { RIG_MODEL_TS450S, "010" },
110 { RIG_MODEL_TS690S, "011" },
111 { RIG_MODEL_TS950SDX, "012" },
112 { RIG_MODEL_TS50, "013" },
113 { RIG_MODEL_TS870S, "015" },
114 { RIG_MODEL_TS570D, "017" }, /* Elecraft K2|K3|KX3 also returns 17 */
115 { RIG_MODEL_TS570S, "018" },
116 { RIG_MODEL_TS2000, "019" },
117 { RIG_MODEL_TS480, "020" },
118 { RIG_MODEL_PT8000A, "020" }, // TS480 ID but behaves differently
119 { RIG_MODEL_TS590S, "021" },
120 { RIG_MODEL_TS990S, "022" },
121 { RIG_MODEL_TS590SG, "023" },
122 { RIG_MODEL_TS890S, "024" },
123 { RIG_MODEL_THD7A, "TH-D7" },
124 { RIG_MODEL_THD7AG, "TH-D7G" },
125 { RIG_MODEL_TMD700, "TM-D700" },
126 { RIG_MODEL_TMD710, "TM-D710" },
127 { RIG_MODEL_THD72A, "TH-D72" },
128 { RIG_MODEL_THD74, "TH-D74" },
129 { RIG_MODEL_TMV7, "TM-V7" },
130 { RIG_MODEL_TMV71, "TM-V71" },
131 { RIG_MODEL_THF6A, "TH-F6" },
132 { RIG_MODEL_THF7E, "TH-F7" },
133 { RIG_MODEL_THG71, "TH-G71" },
134 { RIG_MODEL_MALACHITE, "020" },
135 { RIG_MODEL_NONE, NULL }, /* end marker */
136 };
137
138 rmode_t kenwood_mode_table[KENWOOD_MODE_TABLE_MAX] =
139 {
140 [0] = RIG_MODE_NONE,
141 [1] = RIG_MODE_LSB,
142 [2] = RIG_MODE_USB,
143 [3] = RIG_MODE_CW,
144 [4] = RIG_MODE_FM,
145 [5] = RIG_MODE_AM,
146 [6] = RIG_MODE_RTTY, // FSK Mode
147 [7] = RIG_MODE_CWR,
148 [8] = RIG_MODE_NONE, /* TUNE mode */
149 [9] = RIG_MODE_RTTYR, // FSKR Mode
150 [10] = RIG_MODE_PSK,
151 [11] = RIG_MODE_PSKR,
152 [12] = RIG_MODE_PKTLSB,
153 [13] = RIG_MODE_PKTUSB,
154 [14] = RIG_MODE_PKTFM,
155 [15] = RIG_MODE_PKTAM
156 };
157
158 /*
159 * 38 CTCSS sub-audible tones
160 */
161 tone_t kenwood38_ctcss_list[] =
162 {
163 670, 719, 744, 770, 797, 825, 854, 885, 915, 948,
164 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, 1318,
165 1365, 1413, 1462, 1514, 1567, 1622, 1679, 1738, 1799, 1862,
166 1928, 2035, 2107, 2181, 2257, 2336, 2418, 2503,
167 0,
168 };
169
170
171 /*
172 * 42 CTCSS sub-audible tones
173 */
174 tone_t kenwood42_ctcss_list[] =
175 {
176 670, 693, 719, 744, 770, 797, 825, 854, 885, 915, 948,
177 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, 1318,
178 1365, 1413, 1462, 1514, 1567, 1622, 1679, 1738, 1799, 1862,
179 1928, 2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541,
180 0,
181 };
182
183
184 /* Token definitions for .cfgparams in rig_caps
185 *
186 * See enum rig_conf_e and struct confparams in rig.h
187 */
188 struct confparams kenwood_cfg_params[] =
189 {
190 {
191 TOK_FINE, "fine", "Fine", "Fine step mode",
192 NULL, RIG_CONF_CHECKBUTTON, { }
193 },
194 {
195 TOK_VOICE, "voice", "Voice", "Voice recall",
196 NULL, RIG_CONF_BUTTON, { }
197 },
198 {
199 TOK_XIT, "xit", "XIT", "XIT",
200 NULL, RIG_CONF_CHECKBUTTON, { }
201 },
202 {
203 TOK_RIT, "rit", "RIT", "RIT",
204 NULL, RIG_CONF_CHECKBUTTON, { }
205 },
206 {
207 TOK_NO_ID, "no_id", "No ID", "If true do not send ID; with set commands",
208 NULL, RIG_CONF_CHECKBUTTON, { }
209 },
210 { RIG_CONF_END, NULL, }
211 };
212
213
214 /**
215 * kenwood_transaction
216 * Assumes rig!=NULL rig->state!=NULL rig->caps!=NULL
217 *
218 * Parameters:
219 * cmdstr: Command to be sent to the rig. cmdstr can also be NULL,
220 * indicating that only a reply is needed (nothing will be sent).
221 * data: Buffer for reply string. Can be NULL, indicating that no reply
222 * is needed and will return with RIG_OK after command was sent.
223 * datasize: Size of buffer. It is the caller's responsibily to provide
224 * a large enough buffer for all possible replies for a command.
225 *
226 * returns:
227 * RIG_OK - if no error occurred.
228 * RIG_EIO - if an I/O error occurred while sending/receiving data.
229 * RIG_ETIMEOUT - if timeout expires without any characters received.
230 * RIG_REJECTED - if a negative acknowledge was received or command not
231 * recognized by rig.
232 */
kenwood_transaction(RIG * rig,const char * cmdstr,char * data,size_t datasize)233 int kenwood_transaction(RIG *rig, const char *cmdstr, char *data,
234 size_t datasize)
235 {
236 char buffer[KENWOOD_MAX_BUF_LEN]; /* use our own buffer since
237 verification may need a longer
238 buffer than the user supplied one */
239 char cmdtrm_str[2]; /* Default Command/Reply termination char */
240 int retval = -RIG_EINTERNAL;
241 char *cmd;
242 int len;
243 int retry_read = 0;
244 struct kenwood_priv_data *priv = rig->state.priv;
245 struct kenwood_priv_caps *caps = kenwood_caps(rig);
246 struct rig_state *rs;
247
248 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
249
250 if ((!cmdstr && !datasize) || (datasize && !data))
251 {
252 RETURNFUNC(-RIG_EINVAL);
253 }
254
255 rs = &rig->state;
256
257 rs->hold_decode = 1;
258
259 /* Emulators don't need any post_write_delay */
260 if (priv->is_emulation) { rs->rigport.post_write_delay = 0; }
261
262 // if this is an IF cmdstr and not the first time through check cache
263 if (strcmp(cmdstr, "IF") == 0 && priv->cache_start.tv_sec != 0)
264 {
265 int cache_age_ms;
266
267 cache_age_ms = elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_GET);
268
269 if (cache_age_ms < 500) // 500ms cache time
270 {
271 rig_debug(RIG_DEBUG_TRACE, "%s: cache hit, age=%dms\n", __func__, cache_age_ms);
272
273 if (data) { strncpy(data, priv->last_if_response, datasize); }
274
275 RETURNFUNC(RIG_OK);
276 }
277
278 // else we drop through and do the real IF command
279 }
280
281 if (strlen(cmdstr) > 2 || strcmp(cmdstr, "RX") == 0
282 || strncmp(cmdstr, "TX", 2) == 0 || strncmp(cmdstr, "ZZTX", 4) == 0)
283 {
284 // then we must be setting something so we'll invalidate the cache
285 rig_debug(RIG_DEBUG_TRACE, "%s: cache invalidated\n", __func__);
286 priv->cache_start.tv_sec = 0;
287 }
288
289 cmdtrm_str[0] = caps->cmdtrm;
290 cmdtrm_str[1] = '\0';
291
292 transaction_write:
293
294 if (cmdstr)
295 {
296 rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, cmdstr);
297
298 len = strlen(cmdstr);
299
300 cmd = malloc(len + 2);
301
302 if (cmd == NULL)
303 {
304 retval = -RIG_ENOMEM;
305 goto transaction_quit;
306 }
307
308 memcpy(cmd, cmdstr, len);
309
310 /* XXX the if is temporary, until all invocations are fixed */
311 if (cmdstr[len - 1] != ';' && cmdstr[len - 1] != '\r')
312 {
313 cmd[len] = caps->cmdtrm;
314 len++;
315 }
316
317 /* flush anything in the read buffer before command is sent */
318 rig_flush(&rs->rigport);
319
320 retval = write_block(&rs->rigport, cmd, len);
321
322 free(cmd);
323
324 if (retval != RIG_OK)
325 {
326 goto transaction_quit;
327 }
328 }
329
330 // we're not going to do the verify on RX cmd
331 // Seems some rigs (like TS-480) return "?" when RX is done while PTT=OFF
332 // So we'll skip the checks just on this one command for now
333 // The TS-480 PC Control says RX; should return RX0; but it doesn't
334 // We may eventually want to verify PTT with rig_get_ptt instead
335 // The TS-2000 doesn't like doing and ID right after RU or RD
336 if (retval == RIG_OK)
337 {
338 int skip = strncmp(cmdstr, "RX", 2) == 0;
339 skip |= strncmp(cmdstr, "RU", 2) == 0;
340 skip |= strncmp(cmdstr, "RD", 2) == 0;
341
342 if (skip)
343 {
344 goto transaction_quit;
345 }
346 }
347
348 // Malachite SDR cannot send ID after FA
349 if (!datasize && priv->no_id) { RETURNFUNC(RIG_OK); }
350
351 if (!datasize)
352 {
353 rig->state.hold_decode = 0;
354
355 // there are some commands that have problems with immediate follow-up
356 // so we'll just ignore them
357
358 /* no reply expected so we need to write a command that always
359 gives a reply so we can read any error replies from the actual
360 command being sent without blocking */
361 if (RIG_OK != (retval = write_block(&rs->rigport, priv->verify_cmd
362 , strlen(priv->verify_cmd))))
363 {
364 goto transaction_quit;
365 }
366 }
367
368 transaction_read:
369 /* allow room for most any response */
370 len = min(datasize ? datasize + 1 : strlen(priv->verify_cmd) + 32,
371 KENWOOD_MAX_BUF_LEN);
372 retval = read_string(&rs->rigport, buffer, len, cmdtrm_str, strlen(cmdtrm_str));
373 rig_debug(RIG_DEBUG_TRACE, "%s: read_string(len=%d)='%s'\n", __func__,
374 (int)strlen(buffer), buffer);
375
376 if (retval < 0)
377 {
378 rig_debug(RIG_DEBUG_WARN,
379 "%s: read_string retval < 0, retval = %d, retry_read=%d, rs->rigport.retry=%d\n",
380 __func__,
381 retval, retry_read, rs->rigport.retry);
382
383 // only retry if we expect a response from the command
384 if (retry_read++ < rs->rigport.retry)
385 {
386 if (datasize)
387 {
388 goto transaction_write;
389 }
390 else if (-RIG_ETIMEOUT == retval)
391 {
392 goto transaction_read;
393 }
394 }
395
396 goto transaction_quit;
397 }
398
399 /* Check that command termination is correct */
400 if (strchr(cmdtrm_str, buffer[strlen(buffer) - 1]) == NULL)
401 {
402 rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n",
403 __func__, buffer);
404
405 if (retry_read++ < rs->rigport.retry)
406 {
407 goto transaction_write;
408 }
409
410 retval = -RIG_EPROTO;
411 goto transaction_quit;
412 }
413
414 if (strlen(buffer) == 2)
415 {
416 switch (buffer[0])
417 {
418 case 'N':
419
420 /* Command recognised by rig but invalid data entered. */
421 if (cmdstr)
422 {
423 rig_debug(RIG_DEBUG_VERBOSE, "%s: NegAck for '%s'\n", __func__, cmdstr);
424 }
425
426 retval = -RIG_ENAVAIL;
427 goto transaction_quit;
428
429 case 'O':
430
431 /* Too many characters sent without a carriage return */
432 if (cmdstr)
433 {
434 rig_debug(RIG_DEBUG_VERBOSE, "%s: Overflow for '%s'\n", __func__, cmdstr);
435 }
436
437 if (retry_read++ < rs->rigport.retry)
438 {
439 goto transaction_write;
440 }
441
442 retval = -RIG_EPROTO;
443 goto transaction_quit;
444
445 case 'E':
446
447 /* Communication error */
448 if (cmdstr)
449 {
450 rig_debug(RIG_DEBUG_VERBOSE, "%s: Communication error for '%s'\n", __func__,
451 cmdstr);
452 }
453
454 if (retry_read++ < rs->rigport.retry)
455 {
456 goto transaction_write;
457 }
458
459 retval = -RIG_EIO;
460 goto transaction_quit;
461
462 case '?':
463
464 /* Command not understood by rig or rig busy */
465 if (cmdstr)
466 {
467 rig_debug(RIG_DEBUG_ERR, "%s: Unknown command or rig busy '%s'\n", __func__,
468 cmdstr);
469 }
470
471 if (retry_read++ < rs->rigport.retry)
472 {
473 rig_debug(RIG_DEBUG_ERR, "%s: Retrying shortly\n", __func__);
474 hl_usleep(rig->caps->timeout * 1000);
475 goto transaction_write;
476 }
477
478 retval = -RIG_ERJCTED;
479 goto transaction_quit;
480 }
481 }
482
483 /*
484 * Check that we received the correct reply. The first two characters
485 * should be the same as command. Because the Elecraft XG3 uses
486 * single character commands we only check the first character in
487 * that case.
488 */
489 if (datasize)
490 {
491 if (cmdstr && (buffer[0] != cmdstr[0] || (cmdstr[1] && buffer[1] != cmdstr[1])))
492 {
493 /*
494 * TODO: When RIG_TRN is enabled, we can pass the string to
495 * the decoder for callback. That way we don't ignore any
496 * commands.
497 */
498 rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command %c%c\n",
499 __func__, buffer[0], buffer[1], cmdstr[0], cmdstr[1]);
500
501 rig_debug(RIG_DEBUG_ERR, "%s: retry_read=%d, rs->rigport.retry=%d\n", __func__,
502 retry_read, rs->rigport.retry);
503
504 if (retry_read++ < rs->rigport.retry)
505 {
506 goto transaction_write;
507 }
508
509 retval = -RIG_EPROTO;
510 goto transaction_quit;
511 }
512
513 if (retval > 0)
514 {
515 /* move the result excluding the command terminator into the
516 caller buffer */
517 len = min(datasize, retval) - 1;
518 strncpy(data, buffer, len);
519 data[len] = '\0';
520 }
521 }
522 else
523 {
524 rig_debug(RIG_DEBUG_TRACE, "%s: No data expected, checking %s in %s\n",
525 __func__,
526 priv->verify_cmd, buffer);
527
528 // seems some rigs will send back an IF response to RX/TX when it changes the status
529 // normally RX/TX returns nothing when it's a null effect
530 // TS-950SDX is known to behave this way
531 if (strncmp(cmdstr, "RX", 2) == 0 || strncmp(cmdstr, "TX", 2) == 0)
532 {
533 if (strncmp(priv->verify_cmd, "IF", 2) == 0)
534 {
535 rig_debug(RIG_DEBUG_TRACE, "%s: RX/TX got IF response so we're good\n",
536 __func__);
537 goto transaction_quit;
538 }
539 }
540
541 if (priv->verify_cmd[0] != buffer[0]
542 || (priv->verify_cmd[1] && priv->verify_cmd[1] != buffer[1]))
543 {
544 /*
545 * TODO: When RIG_TRN is enabled, we can pass the string to
546 * the decoder for callback. That way we don't ignore any
547 * commands.
548 */
549 rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command verification %c%c\n",
550 __func__, buffer[0], buffer[1]
551 , priv->verify_cmd[0], priv->verify_cmd[1]);
552
553 if (retry_read++ < rs->rigport.retry)
554 {
555 goto transaction_write;
556 }
557
558 retval = -RIG_EPROTO;
559 goto transaction_quit;
560 }
561 }
562
563 retval = RIG_OK;
564 rig_debug(RIG_DEBUG_TRACE, "%s: returning RIG_OK, retval=%d\n", __func__,
565 retval);
566
567 transaction_quit:
568
569 // update the cache
570 if (retval == RIG_OK && strcmp(cmdstr, "IF") == 0)
571 {
572 elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_SET);
573 strncpy(priv->last_if_response, buffer, caps->if_len);
574 }
575
576 rs->hold_decode = 0;
577 RETURNFUNC(retval);
578 }
579
580
581 /**
582 * kenwood_safe_transaction
583 * A wrapper for kenwood_transaction to check returned data against
584 * expected length,
585 *
586 * Parameters:
587 * cmd Same as kenwood_transaction() cmdstr
588 * buf Same as kenwwod_transaction() data
589 * buf_size Same as kenwood_transaction() datasize
590 * expected Value of expected string length
591 *
592 * Returns:
593 * RIG_OK - if no error occurred.
594 * RIG_EPROTO if returned string and expected are not equal
595 * Error from kenwood_transaction() if any
596 *
597 */
kenwood_safe_transaction(RIG * rig,const char * cmd,char * buf,size_t buf_size,size_t expected)598 int kenwood_safe_transaction(RIG *rig, const char *cmd, char *buf,
599 size_t buf_size, size_t expected)
600 {
601 int err;
602 int retry = 0;
603
604 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
605
606 if (!cmd)
607 {
608 RETURNFUNC(-RIG_EINVAL);
609 }
610
611 memset(buf, 0, buf_size);
612
613 if (expected == 0)
614 {
615 buf_size = 0;
616 }
617
618 do
619 {
620 size_t length;
621 // some PowerSDR commands have variable len
622 int checklen = !RIG_IS_POWERSDR;
623 err = kenwood_transaction(rig, cmd, buf, buf_size);
624
625 if (err != RIG_OK) /* return immediately on error as any
626 retries handled at lower level */
627 {
628 RETURNFUNC(err);
629 }
630
631 length = strlen(buf);
632
633 if (checklen && length != expected) /* worth retrying as some rigs
634 occasionally send short results */
635 {
636 struct kenwood_priv_data *priv = rig->state.priv;
637 rig_debug(RIG_DEBUG_ERR,
638 "%s: wrong answer; len for cmd %s: expected = %d, got %d\n",
639 __func__, cmd, (int)expected, (int)length);
640 err = -RIG_EPROTO;
641 elapsed_ms(&priv->cache_start, HAMLIB_ELAPSED_INVALIDATE);
642 hl_usleep(50 * 1000); // let's do a short wait
643 }
644 }
645 while (err != RIG_OK && ++retry < rig->state.rigport.retry);
646
647 RETURNFUNC(err);
648 }
649
kenwood2rmode(unsigned char mode,const rmode_t mode_table[])650 rmode_t kenwood2rmode(unsigned char mode, const rmode_t mode_table[])
651 {
652 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
653
654 if (mode >= KENWOOD_MODE_TABLE_MAX)
655 {
656 RETURNFUNC(RIG_MODE_NONE);
657 }
658
659 RETURNFUNC(mode_table[mode]);
660 }
661
rmode2kenwood(rmode_t mode,const rmode_t mode_table[])662 char rmode2kenwood(rmode_t mode, const rmode_t mode_table[])
663 {
664 rig_debug(RIG_DEBUG_VERBOSE, "%s called, mode=%s\n", __func__,
665 rig_strrmode(mode));
666
667 if (mode != RIG_MODE_NONE)
668 {
669 int i;
670
671 for (i = 0; i < KENWOOD_MODE_TABLE_MAX; i++)
672 {
673 if (mode_table[i] == mode)
674 {
675 rig_debug(RIG_DEBUG_VERBOSE, "%s: returning %d\n", __func__, i);
676 RETURNFUNC(i);
677 }
678 }
679 }
680
681 RETURNFUNC(-1);
682 }
683
kenwood_init(RIG * rig)684 int kenwood_init(RIG *rig)
685 {
686 struct kenwood_priv_data *priv;
687 struct kenwood_priv_caps *caps = kenwood_caps(rig);
688
689 rig_debug(RIG_DEBUG_VERBOSE, "%s called, version %s/%s\n", __func__,
690 BACKEND_VER, rig->caps->version);
691
692 rig->state.priv = malloc(sizeof(struct kenwood_priv_data));
693
694 if (rig->state.priv == NULL)
695 {
696 RETURNFUNC(-RIG_ENOMEM);
697 }
698
699 priv = rig->state.priv;
700
701 memset(priv, 0x00, sizeof(struct kenwood_priv_data));
702
703 if (RIG_IS_XG3)
704 {
705 priv->verify_cmd[0] = caps->cmdtrm;
706 priv->verify_cmd[1] = '\0';
707 }
708 else
709 {
710 priv->verify_cmd[0] = 'I';
711 priv->verify_cmd[1] = 'D';
712 priv->verify_cmd[2] = caps->cmdtrm;
713 priv->verify_cmd[3] = '\0';
714 }
715
716 priv->split = RIG_SPLIT_OFF;
717 priv->trn_state = -1;
718 priv->curr_mode = 0;
719 priv->micgain_min = -1;
720 priv->micgain_max = -1;
721
722 /* default mode_table */
723 if (caps->mode_table == NULL)
724 {
725 caps->mode_table = kenwood_mode_table;
726 }
727
728 /* default if_len */
729 if (caps->if_len == 0)
730 {
731 caps->if_len = 37;
732 }
733
734 priv->ag_format = -1; // force determination of AG format
735
736 rig_debug(RIG_DEBUG_TRACE, "%s: if_len = %d\n", __func__, caps->if_len);
737
738 RETURNFUNC(RIG_OK);
739 }
740
kenwood_cleanup(RIG * rig)741 int kenwood_cleanup(RIG *rig)
742 {
743 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
744
745 free(rig->state.priv);
746 rig->state.priv = NULL;
747
748 RETURNFUNC(RIG_OK);
749 }
750
kenwood_open(RIG * rig)751 int kenwood_open(RIG *rig)
752 {
753 struct kenwood_priv_data *priv = rig->state.priv;
754 struct kenwood_priv_caps *caps = kenwood_caps(rig);
755 int err, i;
756 char *idptr;
757 char id[KENWOOD_MAX_BUF_LEN];
758 int retry_save = rig->state.rigport.retry;
759
760 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
761
762 id[0] = 0;
763 rig->state.rigport.retry = 0;
764 err = kenwood_get_id(rig, id);
765
766 if (err != RIG_OK)
767 {
768 // TS450S is flaky on the 1st ID call so we'll try again
769 hl_usleep(200 * 1000);
770 err = kenwood_get_id(rig, id);
771 }
772
773 if (err == RIG_OK) // some rigs give ID while in standby
774 {
775 powerstat_t powerstat = 0;
776 rig_debug(RIG_DEBUG_TRACE, "%s: got ID so try PS\n", __func__);
777 err = rig_get_powerstat(rig, &powerstat);
778
779 if (err == RIG_OK && powerstat == 0 && priv->poweron == 0
780 && rig->state.auto_power_on)
781 {
782 rig_debug(RIG_DEBUG_TRACE, "%s: got PS0 so powerup\n", __func__);
783 rig_set_powerstat(rig, 1);
784 }
785
786 priv->poweron = 1;
787
788 err = RIG_OK; // reset our err back to OK for later checks
789 }
790
791 if (err == -RIG_ETIMEOUT && rig->state.auto_power_on)
792 {
793 // Ensure rig is on
794 rig_set_powerstat(rig, 1);
795 /* Try get id again */
796 err = kenwood_get_id(rig, id);
797 }
798
799 if (RIG_OK != err)
800 {
801 rig_debug(RIG_DEBUG_ERR,
802 "%s: no response to get_id from rig...continuing anyway\n", __func__);
803 }
804
805 if (RIG_IS_TS2000
806 || RIG_IS_TS480
807 || RIG_IS_TS590S
808 || RIG_IS_TS590SG
809 || RIG_IS_TS890S
810 || RIG_IS_TS990S)
811 {
812 // rig has Set 2 RIT/XIT function
813 rig_debug(RIG_DEBUG_TRACE, "%s: rig has_rit2\n", __func__);
814 priv->has_rit2 = 1;
815 }
816
817 if (RIG_IS_TS590S)
818 {
819 /* we need the firmware version for these rigs to deal with f/w defects */
820 static char fw_version[7];
821 char *dot_pos;
822
823 err = kenwood_transaction(rig, "FV", fw_version, sizeof(fw_version));
824
825 if (RIG_OK != err)
826 {
827 rig_debug(RIG_DEBUG_ERR, "%s: cannot get f/w version\n", __func__);
828 rig->state.rigport.retry = retry_save;
829 RETURNFUNC(err);
830 }
831
832 /* store the data after the "FV" which should be a f/w version
833 string of the form n.n e.g. 1.07 */
834 priv->fw_rev = &fw_version[2];
835 dot_pos = strchr(fw_version, '.');
836
837 if (dot_pos)
838 {
839 priv->fw_rev_uint = atoi(&fw_version[2]) * 100 + atoi(dot_pos + 1);
840 }
841 else
842 {
843 rig_debug(RIG_DEBUG_ERR, "%s: cannot get f/w version\n", __func__);
844 rig->state.rigport.retry = retry_save;
845 RETURNFUNC(-RIG_EPROTO);
846 }
847
848 rig_debug(RIG_DEBUG_TRACE, "%s: found f/w version %s\n", __func__,
849 priv->fw_rev);
850 }
851
852 if (!RIG_IS_XG3 && -RIG_ETIMEOUT == err)
853 {
854 char buffer[KENWOOD_MAX_BUF_LEN];
855 /* Some Kenwood emulations have no ID command response :(
856 * Try an FA command to see if anyone is listening */
857 err = kenwood_transaction(rig, "FA", buffer, sizeof(buffer));
858
859 if (RIG_OK != err)
860 {
861 rig_debug(RIG_DEBUG_ERR, "%s: no response from rig\n", __func__);
862 rig->state.rigport.retry = retry_save;
863 RETURNFUNC(err);
864 }
865
866 /* here we know there is something that responds to FA but not
867 to ID so use FA as the command verification command */
868 priv->verify_cmd[0] = 'F';
869 priv->verify_cmd[1] = 'A';
870 priv->verify_cmd[2] = caps->cmdtrm;
871 priv->verify_cmd[3] = '\0';
872 strcpy(id, "ID019"); /* fake a TS-2000 */
873 }
874 else
875 {
876 if (err != RIG_OK)
877 {
878 rig_debug(RIG_DEBUG_ERR, "%s: cannot get identification\n", __func__);
879 rig->state.rigport.retry = retry_save;
880 RETURNFUNC(err);
881 }
882 }
883
884 /* id is something like 'IDXXX' or 'ID XXX' */
885 if (strlen(id) < 5)
886 {
887 rig_debug(RIG_DEBUG_ERR, "%s: unknown id type (%s)...continuing\n", __func__,
888 id);
889
890 // Malachite SDR gives no reponse to ID and is supposed to be TS480 compatible
891 if (RIG_IS_MALACHITE) { strcpy(id, "ID020"); }
892
893 }
894
895 if (!strcmp("IDID900", id) /* DDUtil in TS-2000 mode */
896 || !strcmp("ID900", id) /* PowerSDR after ZZID; command */
897 || !strcmp("ID904", id) /* SmartSDR Flex-6700 */
898 || !strcmp("ID905", id) /* PowerSDR Flex-6500 */
899 || !strcmp("ID906", id) /* PowerSDR Flex-6700R */
900 || !strcmp("ID907", id) /* PowerSDR Flex-6300 */
901 || !strcmp("ID908", id) /* PowerSDR Flex-6400 */
902 || !strcmp("ID909", id) /* PowerSDR Flex-6600 */
903 )
904 {
905 priv->is_emulation = 1; /* Emulations don't have SAT mode */
906 strcpy(id, "ID019"); /* fake it */
907 }
908
909 /* check for a white space and skip it */
910 idptr = &id[2];
911
912 if (*idptr == ' ')
913 {
914 idptr++;
915 }
916
917 /* compare id string */
918 for (i = 0; kenwood_id_string_list[i].model != RIG_MODEL_NONE; i++)
919 {
920 //rig_debug(RIG_DEBUG_ERR, "%s: comparing '%s'=='%s'\n", __func__, kenwood_id_string_list[i].id, idptr);
921 if (strcmp(kenwood_id_string_list[i].id, idptr) != 0)
922 {
923 continue;
924 }
925
926 /* found matching id, verify driver */
927 rig_debug(RIG_DEBUG_TRACE, "%s: found match %s\n",
928 __func__, kenwood_id_string_list[i].id);
929
930 if (kenwood_id_string_list[i].model == rig->caps->rig_model)
931 {
932 int retval;
933 split_t split;
934 vfo_t tx_vfo;
935 rig_debug(RIG_DEBUG_VERBOSE, "%s: found the right driver for %s(%d)\n",
936 __func__, rig->caps->model_name, rig->caps->rig_model);
937 /* get current AI state so it can be restored */
938 kenwood_get_trn(rig, &priv->trn_state); /* ignore errors */
939
940 /* Currently we cannot cope with AI mode so turn it off in
941 case last client left it on */
942 if (priv->trn_state != RIG_TRN_OFF)
943 {
944 kenwood_set_trn(rig, RIG_TRN_OFF); /* ignore status in case
945 it's not supported */
946 }
947
948 if (!RIG_IS_THD74 && !RIG_IS_THD7A)
949 {
950 // call get_split to fill in current split and tx_vfo status
951 retval = kenwood_get_split_vfo_if(rig, RIG_VFO_A, &split, &tx_vfo);
952
953 if (retval != RIG_OK)
954 {
955 rig_debug(RIG_DEBUG_ERR, "%s: %s\n", __func__, rigerror(retval));
956 }
957
958 priv->tx_vfo = tx_vfo;
959 rig_debug(RIG_DEBUG_VERBOSE, "%s: priv->tx_vfo=%s\n", __func__,
960 rig_strvfo(priv->tx_vfo));
961 }
962
963 rig->state.rigport.retry = retry_save;
964
965 // Default to 1st VFO and split off -- but only 1 time
966 if (rig->caps->set_vfo && priv->opened == 0)
967 {
968 rig_set_vfo(rig, vfo_fixup(rig, RIG_VFO_A, 0));
969 priv->opened = 1;
970 }
971
972 RETURNFUNC(RIG_OK);
973 }
974
975 /* driver mismatch */
976 rig_debug(RIG_DEBUG_ERR,
977 "%s: not the right driver apparently (found %u, asked for %d, checked %s)\n",
978 __func__, rig->caps->rig_model,
979 kenwood_id_string_list[i].model,
980 rig->caps->model_name);
981
982 // we continue to search for other matching IDs/models
983 }
984
985 rig_debug(RIG_DEBUG_ERR,
986 "%s: your rig (%s) did not match but we will continue anyways\n",
987 __func__, id);
988
989 // we're making this non fatal
990 // mismatched IDs can still be tested
991 rig->state.rigport.retry = retry_save;
992
993 RETURNFUNC(RIG_OK);
994 }
995
996
kenwood_close(RIG * rig)997 int kenwood_close(RIG *rig)
998 {
999 struct kenwood_priv_data *priv = rig->state.priv;
1000
1001 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1002
1003 if (!no_restore_ai && priv->trn_state >= 0)
1004 {
1005 /* restore AI state */
1006 kenwood_set_trn(rig, priv->trn_state); /* ignore status in case
1007 it's not supported */
1008 }
1009
1010 if (priv->poweron != 0 && rig->state.auto_power_off)
1011 {
1012 rig_debug(RIG_DEBUG_TRACE, "%s: got PS1 so powerdown\n", __func__);
1013 rig_set_powerstat(rig, 0);
1014 }
1015
1016 RETURNFUNC(RIG_OK);
1017 }
1018
1019
1020 /* ID
1021 * Reads transceiver ID number
1022 *
1023 * caller must give a buffer of KENWOOD_MAX_BUF_LEN size
1024 *
1025 */
kenwood_get_id(RIG * rig,char * buf)1026 int kenwood_get_id(RIG *rig, char *buf)
1027 {
1028 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1029
1030 RETURNFUNC(kenwood_transaction(rig, "ID", buf, KENWOOD_MAX_BUF_LEN));
1031 }
1032
1033
1034 /* IF
1035 * Retrieves the transceiver status
1036 *
1037 */
kenwood_get_if(RIG * rig)1038 int kenwood_get_if(RIG *rig)
1039 {
1040 struct kenwood_priv_data *priv = rig->state.priv;
1041 struct kenwood_priv_caps *caps = kenwood_caps(rig);
1042
1043 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1044
1045 return (kenwood_safe_transaction(rig, "IF", priv->info,
1046 KENWOOD_MAX_BUF_LEN, caps->if_len));
1047 }
1048
1049
1050 /* FN FR FT
1051 * Sets the RX/TX VFO or M.CH mode of the transceiver, does not set split
1052 * VFO, but leaves it unchanged if in split VFO mode.
1053 *
1054 */
kenwood_set_vfo(RIG * rig,vfo_t vfo)1055 int kenwood_set_vfo(RIG *rig, vfo_t vfo)
1056 {
1057 char cmdbuf[12];
1058 int retval;
1059 char vfo_function;
1060 struct kenwood_priv_data *priv = rig->state.priv;
1061
1062 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1063
1064
1065 /* Emulations do not need to set VFO since VFOB is a copy of VFOA
1066 * except for frequency. And we can change freq without changing VFOS
1067 * This prevents a 1.8 second delay in PowerSDR when switching VFOs
1068 * We'll do this once if curr_mode has not been set yet
1069 */
1070 if (priv->is_emulation && priv->curr_mode > 0) { RETURNFUNC(RIG_OK); }
1071
1072 switch (vfo)
1073 {
1074 case RIG_VFO_A:
1075 vfo_function = '0';
1076 break;
1077
1078 case RIG_VFO_B:
1079 vfo_function = '1';
1080 break;
1081
1082 case RIG_VFO_MEM:
1083 vfo_function = '2';
1084 break;
1085
1086 case RIG_VFO_CURR:
1087 RETURNFUNC(RIG_OK);
1088
1089 default:
1090 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
1091 RETURNFUNC(-RIG_EINVAL);
1092 }
1093
1094 //if rig=ts2000 then check Satellite mode status
1095 if (RIG_IS_TS2000 && !priv->is_emulation)
1096 {
1097 char retbuf[20];
1098 rig_debug(RIG_DEBUG_VERBOSE, "%s: checking satellite mode status\n", __func__);
1099 snprintf(cmdbuf, sizeof(cmdbuf), "SA");
1100
1101 retval = kenwood_transaction(rig, cmdbuf, retbuf, 20);
1102
1103 if (retval != RIG_OK)
1104 {
1105 RETURNFUNC(retval);
1106 }
1107
1108 rig_debug(RIG_DEBUG_VERBOSE, "%s: satellite mode status %s\n", __func__,
1109 retbuf);
1110
1111 //Satellite mode ON
1112 if (retbuf[2] == '1')
1113 {
1114 //SAT mode doesn't allow FR command (cannot select VFO)
1115 //selecting VFO is useless in SAT MODE
1116 RETURNFUNC(RIG_OK);
1117 }
1118 }
1119
1120 snprintf(cmdbuf, sizeof(cmdbuf), "FR%c", vfo_function);
1121
1122 // as we change VFO we will change split to the other VFO
1123 // some rigs turn split off with FR command
1124 if (priv->split)
1125 {
1126 if (vfo_function == '0')
1127 {
1128 strcat(cmdbuf, ";FT1");
1129 }
1130 else
1131 {
1132 strcat(cmdbuf, ";FT0");
1133 }
1134 }
1135
1136 if (RIG_IS_TS50 || RIG_IS_TS940)
1137 {
1138 cmdbuf[1] = 'N';
1139 }
1140
1141 /* set RX VFO */
1142 retval = kenwood_transaction(rig, cmdbuf, NULL, 0);
1143
1144 if (retval != RIG_OK)
1145 {
1146 RETURNFUNC(retval);
1147 }
1148
1149 /* if FN command then there's no FT or FR */
1150 /* If split mode on, the don't change TxVFO */
1151 if ('N' == cmdbuf[1] || priv->split != RIG_SPLIT_OFF)
1152 {
1153 RETURNFUNC(RIG_OK);
1154 }
1155
1156 // some rigs need split turned on after VFOA is set
1157 if (vfo == RIG_VFO_A && priv->split == RIG_SPLIT_ON)
1158 {
1159 rig_set_split_vfo(rig, RIG_VFO_CURR, 1, priv->tx_vfo);
1160 }
1161
1162 /* set TX VFO */
1163 cmdbuf[1] = 'T';
1164 RETURNFUNC(kenwood_transaction(rig, cmdbuf, NULL, 0));
1165 }
1166
1167
1168 /* CB
1169 * Sets the operating VFO, does not set split
1170 * VFO, but leaves it unchanged if in split VFO mode.
1171 *
1172 */
kenwood_set_vfo_main_sub(RIG * rig,vfo_t vfo)1173 int kenwood_set_vfo_main_sub(RIG *rig, vfo_t vfo)
1174 {
1175 char cmdbuf[6];
1176 char vfo_function;
1177
1178 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1179
1180 switch (vfo)
1181 {
1182 case RIG_VFO_MAIN:
1183 vfo_function = '0';
1184 break;
1185
1186 case RIG_VFO_SUB:
1187 vfo_function = '1';
1188 break;
1189
1190 case RIG_VFO_CURR:
1191 RETURNFUNC(RIG_OK);
1192
1193 default:
1194 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
1195 RETURNFUNC(-RIG_EINVAL);
1196 }
1197
1198 snprintf(cmdbuf, sizeof(cmdbuf), "CB%c", vfo_function);
1199 RETURNFUNC(kenwood_transaction(rig, cmdbuf, NULL, 0));
1200 }
1201
1202
1203 /* CB
1204 * Gets the operating VFO
1205 *
1206 */
kenwood_get_vfo_main_sub(RIG * rig,vfo_t * vfo)1207 int kenwood_get_vfo_main_sub(RIG *rig, vfo_t *vfo)
1208 {
1209 char buf[4];
1210 int rc;
1211
1212 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1213
1214 if (!vfo)
1215 {
1216 RETURNFUNC(-RIG_EINVAL);
1217 }
1218
1219 if (RIG_OK == (rc = kenwood_safe_transaction(rig, "CB", buf, sizeof(buf), 3)))
1220 {
1221 *vfo = buf[2] == '1' ? RIG_VFO_SUB : RIG_VFO_MAIN;
1222 }
1223
1224 RETURNFUNC(rc);
1225 }
1226
1227
1228 /* FR FT TB
1229 * Sets the split RX/TX VFO or M.CH mode of the transceiver.
1230 *
1231 */
kenwood_set_split_vfo(RIG * rig,vfo_t vfo,split_t split,vfo_t txvfo)1232 int kenwood_set_split_vfo(RIG *rig, vfo_t vfo, split_t split, vfo_t txvfo)
1233 {
1234 struct kenwood_priv_data *priv = rig->state.priv;
1235 char cmdbuf[12];
1236 int retval;
1237 unsigned char vfo_function;
1238
1239 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1240
1241 if (RIG_IS_TS990S)
1242 {
1243 if (split)
1244 {
1245 // Rx MAIN/Tx SUB is the only split method
1246 retval = kenwood_set_vfo_main_sub(rig, RIG_VFO_MAIN);
1247
1248 if (retval != RIG_OK) { RETURNFUNC(retval); }
1249 }
1250
1251 snprintf(cmdbuf, sizeof(cmdbuf), "TB%c", RIG_SPLIT_ON == split ? '1' : '0');
1252 RETURNFUNC(kenwood_transaction(rig, cmdbuf, NULL, 0));
1253 }
1254
1255 if (vfo != RIG_VFO_CURR)
1256 {
1257 switch (vfo)
1258 {
1259 case RIG_VFO_A: vfo_function = '0'; break;
1260
1261 case RIG_VFO_B: vfo_function = '1'; break;
1262
1263 case RIG_VFO_MEM: vfo_function = '2'; break;
1264
1265 default:
1266 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
1267 RETURNFUNC(-RIG_EINVAL);
1268 }
1269
1270 /* set RX VFO */
1271 snprintf(cmdbuf, sizeof(cmdbuf), "FR%c", vfo_function);
1272
1273 // FR can turn off split on some Kenwood rigs
1274 // So we'll turn it back on just in case
1275 if (split && vfo_function == '0') { strcat(cmdbuf, ";FT1"); }
1276
1277 if (priv->split)
1278 {
1279 if (vfo_function == '0')
1280 {
1281 strcat(cmdbuf, ";FT1");
1282 }
1283 else
1284 {
1285 strcat(cmdbuf, ";FT0");
1286 }
1287 }
1288
1289 retval = kenwood_transaction(rig, cmdbuf, NULL, 0);
1290
1291 if (retval != RIG_OK)
1292 {
1293 RETURNFUNC(retval);
1294 }
1295 }
1296
1297 /* Split off means Rx and Tx are the same */
1298 if (split == RIG_SPLIT_OFF)
1299 {
1300 txvfo = vfo;
1301
1302 if (txvfo == RIG_VFO_CURR)
1303 {
1304 retval = rig_get_vfo(rig, &txvfo);
1305
1306 if (retval != RIG_OK)
1307 {
1308 RETURNFUNC(retval);
1309 }
1310 }
1311 }
1312
1313 switch (txvfo)
1314 {
1315 case RIG_VFO_VFO:
1316 case RIG_VFO_A: vfo_function = '0'; break;
1317
1318 case RIG_VFO_B: vfo_function = '1'; break;
1319
1320 case RIG_VFO_MEM: vfo_function = '2'; break;
1321
1322 default:
1323 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__,
1324 rig_strvfo(txvfo));
1325 RETURNFUNC(-RIG_EINVAL);
1326 }
1327
1328 priv->tx_vfo = txvfo;
1329
1330 /* do not attempt redundant split change commands on Elecraft as
1331 they impact output power when transmitting
1332 and all other rigs don't need to set it if it's already set correctly
1333 */
1334 if (RIG_OK == (retval = kenwood_safe_transaction(rig, "FT", cmdbuf,
1335 sizeof(cmdbuf), 3)))
1336 {
1337 if (cmdbuf[2] == vfo_function) { RETURNFUNC(RIG_OK); }
1338 }
1339
1340 /* set TX VFO */
1341 snprintf(cmdbuf, sizeof(cmdbuf), "FT%c", vfo_function);
1342 retval = kenwood_transaction(rig, cmdbuf, NULL, 0);
1343
1344 if (retval != RIG_OK)
1345 {
1346 RETURNFUNC(retval);
1347 }
1348
1349 /* Remember whether split is on, for kenwood_set_vfo */
1350 priv->split = split;
1351
1352 RETURNFUNC(RIG_OK);
1353 }
1354
1355
1356 /* SP
1357 * Sets the split mode of the transceivers that have the FN command.
1358 *
1359 */
kenwood_set_split(RIG * rig,vfo_t vfo,split_t split,vfo_t txvfo)1360 int kenwood_set_split(RIG *rig, vfo_t vfo, split_t split, vfo_t txvfo)
1361 {
1362 struct kenwood_priv_data *priv = rig->state.priv;
1363 char cmdbuf[6];
1364 int retval;
1365
1366 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1367
1368 snprintf(cmdbuf, sizeof(cmdbuf), "SP%c", RIG_SPLIT_ON == split ? '1' : '0');
1369
1370 retval = kenwood_transaction(rig, cmdbuf, NULL, 0);
1371
1372 if (retval != RIG_OK)
1373 {
1374 RETURNFUNC(retval);
1375 }
1376
1377 /* Remember whether split is on, for kenwood_set_vfo */
1378 priv->split = split;
1379 priv->tx_vfo = txvfo;
1380 rig_debug(RIG_DEBUG_VERBOSE, "%s: priv->tx_vfo=%s\n", __func__,
1381 rig_strvfo(priv->tx_vfo));
1382
1383 RETURNFUNC(RIG_OK);
1384 }
1385
1386
1387 /* IF TB
1388 * Gets split VFO status from kenwood_get_if()
1389 *
1390 */
kenwood_get_split_vfo_if(RIG * rig,vfo_t rxvfo,split_t * split,vfo_t * txvfo)1391 int kenwood_get_split_vfo_if(RIG *rig, vfo_t rxvfo, split_t *split,
1392 vfo_t *txvfo)
1393 {
1394 int transmitting;
1395 int retval;
1396 struct kenwood_priv_data *priv = rig->state.priv;
1397
1398
1399 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1400
1401 if (!split || !txvfo)
1402 {
1403 RETURNFUNC(-RIG_EINVAL);
1404 }
1405
1406 if (RIG_IS_TS990S)
1407 {
1408 char buf[4];
1409
1410 if (RIG_OK == (retval = kenwood_safe_transaction(rig, "TB", buf, sizeof(buf),
1411 3)))
1412 {
1413 if ('1' == buf[2])
1414 {
1415 *split = RIG_SPLIT_ON;
1416 *txvfo = RIG_VFO_SUB;
1417 priv->tx_vfo = *txvfo;
1418 }
1419 else
1420 {
1421 *split = RIG_SPLIT_OFF;
1422 *txvfo = RIG_VFO_MAIN;
1423 priv->tx_vfo = *txvfo;
1424 }
1425 }
1426
1427 RETURNFUNC(retval);
1428 }
1429
1430 retval = kenwood_get_if(rig);
1431
1432 if (retval != RIG_OK)
1433 {
1434 RETURNFUNC(retval);
1435 }
1436
1437 switch (priv->info[32])
1438 {
1439 case '0':
1440 *split = RIG_SPLIT_OFF;
1441 break;
1442
1443 case '1':
1444 *split = RIG_SPLIT_ON;
1445 break;
1446
1447 default:
1448 rig_debug(RIG_DEBUG_ERR, "%s: unsupported split %c\n",
1449 __func__, priv->info[32]);
1450 RETURNFUNC(-RIG_EPROTO);
1451 }
1452
1453 /* Remember whether split is on, for kenwood_set_vfo */
1454 priv->split = *split;
1455
1456 /* find where is the txvfo.. */
1457 /* Elecraft info[30] does not track split VFO when transmitting */
1458 transmitting = '1' == priv->info[28] && !RIG_IS_K2 && !RIG_IS_K3;
1459
1460 switch (priv->info[30])
1461 {
1462 case '0':
1463 *txvfo = priv->tx_vfo = (*split && !transmitting) ? RIG_VFO_B : RIG_VFO_A;
1464 break;
1465
1466 case '1':
1467 *txvfo = priv->tx_vfo = (*split && !transmitting) ? RIG_VFO_A : RIG_VFO_B;
1468 break;
1469
1470 case '2':
1471 *txvfo = priv->tx_vfo =
1472 RIG_VFO_MEM; /* SPLIT MEM operation doesn't involve VFO A or VFO B */
1473 break;
1474
1475 default:
1476 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %c\n",
1477 __func__, priv->info[30]);
1478 RETURNFUNC(-RIG_EPROTO);
1479 }
1480
1481 priv->tx_vfo = *txvfo;
1482 rig_debug(RIG_DEBUG_VERBOSE, "%s: priv->tx_vfo=%s\n", __func__,
1483 rig_strvfo(priv->tx_vfo));
1484 RETURNFUNC(RIG_OK);
1485 }
1486
1487
1488 /*
1489 * kenwood_get_vfo_if using byte 31 of the IF information field
1490 *
1491 * Specifically this needs to return the RX VFO, the IF command tells
1492 * us the TX VFO in split TX mode when transmitting so we need to swap
1493 * results sometimes.
1494 */
kenwood_get_vfo_if(RIG * rig,vfo_t * vfo)1495 int kenwood_get_vfo_if(RIG *rig, vfo_t *vfo)
1496 {
1497 int retval;
1498 int split_and_transmitting;
1499 struct kenwood_priv_data *priv = rig->state.priv;
1500
1501 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1502
1503 if (!vfo)
1504 {
1505 RETURNFUNC(-RIG_EINVAL);
1506 }
1507
1508 retval = kenwood_get_if(rig);
1509
1510 if (retval != RIG_OK)
1511 {
1512 RETURNFUNC(retval);
1513 }
1514
1515 /* Elecraft info[30] does not track split VFO when transmitting */
1516 split_and_transmitting =
1517 '1' == priv->info[28] /* transmitting */
1518 && '1' == priv->info[32] /* split */
1519 && !RIG_IS_K2
1520 && !RIG_IS_K3;
1521
1522 switch (priv->info[30])
1523 {
1524 case '0':
1525 *vfo = priv->tx_vfo = split_and_transmitting ? RIG_VFO_B : RIG_VFO_A;
1526
1527 if (priv->info[32] == '1') { priv->tx_vfo = RIG_VFO_B; }
1528
1529 break;
1530
1531 case '1':
1532 *vfo = split_and_transmitting ? RIG_VFO_A : RIG_VFO_B;
1533 priv->tx_vfo = RIG_VFO_B;
1534 break;
1535
1536 case '2':
1537 *vfo = priv->tx_vfo = RIG_VFO_MEM;
1538 break;
1539
1540 default:
1541 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %c\n",
1542 __func__, priv->info[30]);
1543 RETURNFUNC(-RIG_EPROTO);
1544 }
1545
1546 rig_debug(RIG_DEBUG_VERBOSE, "%s: priv->tx_vfo=%s\n", __func__,
1547 rig_strvfo(priv->tx_vfo));
1548 RETURNFUNC(RIG_OK);
1549 }
1550
1551
1552 /*
1553 * kenwood_set_freq
1554 */
kenwood_set_freq(RIG * rig,vfo_t vfo,freq_t freq)1555 int kenwood_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
1556 {
1557 char freqbuf[16];
1558 unsigned char vfo_letter = '\0';
1559 vfo_t tvfo;
1560 int err;
1561 struct kenwood_priv_data *priv = rig->state.priv;
1562
1563 rig_debug(RIG_DEBUG_VERBOSE, "%s called vfo=%s freq=%.0f\n", __func__,
1564 rig_strvfo(vfo), freq);
1565
1566 tvfo = (vfo == RIG_VFO_CURR
1567 || vfo == RIG_VFO_VFO) ? rig->state.current_vfo : vfo;
1568
1569 rig_debug(RIG_DEBUG_TRACE, "%s: tvfo=%s\n", __func__, rig_strvfo(tvfo));
1570
1571 if (tvfo == RIG_VFO_CURR || tvfo == RIG_VFO_NONE)
1572 {
1573 /* fetch from rig */
1574 err = rig_get_vfo(rig, &tvfo);
1575
1576 if (RIG_OK != err) { RETURNFUNC(err); }
1577 }
1578
1579 switch (tvfo)
1580 {
1581 case RIG_VFO_A:
1582 case RIG_VFO_MAIN:
1583 vfo_letter = 'A';
1584 break;
1585
1586 case RIG_VFO_B:
1587 case RIG_VFO_SUB:
1588 vfo_letter = 'B';
1589 break;
1590
1591 case RIG_VFO_C:
1592 vfo_letter = 'C';
1593 break;
1594
1595 case RIG_VFO_TX:
1596 if (priv->tx_vfo == RIG_VFO_A) { vfo_letter = 'A'; }
1597 else if (priv->tx_vfo == RIG_VFO_B) { vfo_letter = 'B'; }
1598 else
1599 {
1600 rig_debug(RIG_DEBUG_ERR, "%s: unsupported tx_vfo, tx_vfo=%s\n", __func__,
1601 rig_strvfo(priv->tx_vfo));
1602 }
1603
1604 break;
1605
1606 default:
1607 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
1608 RETURNFUNC(-RIG_EINVAL);
1609 }
1610
1611 // cppcheck-suppress *
1612 snprintf(freqbuf, sizeof(freqbuf), "F%c%011"PRIll, vfo_letter, (int64_t)freq);
1613
1614 err = kenwood_transaction(rig, freqbuf, NULL, 0);
1615
1616 if (RIG_OK == err && RIG_IS_TS590S
1617 && priv->fw_rev_uint <= 107 && ('A' == vfo_letter || 'B' == vfo_letter))
1618 {
1619 /* TS590s f/w rev 1.07 or earlier has a defect that means
1620 frequency set on TX VFO in split mode may not be set
1621 correctly.
1622
1623 The symptom of the defect is either TX on the wrong
1624 frequency (i.e. TX on a frequency different from that
1625 showing on the TX VFO) or no output.
1626
1627 We use an IF command to find out if we have just set
1628 the "back" VFO when the rig is in split mode. If we
1629 have; we then read the other VFO and set it to what we
1630 read - a null transaction that fixes the defect. */
1631
1632 err = kenwood_get_if(rig);
1633
1634 if (RIG_OK != err)
1635 {
1636 RETURNFUNC(err);
1637 }
1638
1639 if ('1' == priv->info[32] && priv->info[30] != ('A' == vfo_letter ? '0' : '1'))
1640 {
1641 /* split mode and setting "back" VFO */
1642
1643 /* set other VFO to whatever it is at currently */
1644 err = kenwood_safe_transaction(rig, 'A' == vfo_letter ? "FB" : "FA", freqbuf,
1645 16, 13);
1646
1647 if (RIG_OK != err)
1648 {
1649 RETURNFUNC(err);
1650 }
1651
1652 err = kenwood_transaction(rig, freqbuf, NULL, 0);
1653 }
1654 }
1655
1656 RETURNFUNC(err);
1657 }
1658
kenwood_get_freq_if(RIG * rig,vfo_t vfo,freq_t * freq)1659 int kenwood_get_freq_if(RIG *rig, vfo_t vfo, freq_t *freq)
1660 {
1661 struct kenwood_priv_data *priv = rig->state.priv;
1662 char freqbuf[50];
1663 int retval;
1664
1665 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1666
1667 if (!freq)
1668 {
1669 RETURNFUNC(-RIG_EINVAL);
1670 }
1671
1672 retval = kenwood_get_if(rig);
1673
1674 if (retval != RIG_OK)
1675 {
1676 RETURNFUNC(retval);
1677 }
1678
1679 memcpy(freqbuf, priv->info, 15);
1680 freqbuf[14] = '\0';
1681 sscanf(freqbuf + 2, "%"SCNfreq, freq);
1682
1683 RETURNFUNC(RIG_OK);
1684 }
1685
1686 /*
1687 * kenwood_get_freq
1688 */
kenwood_get_freq(RIG * rig,vfo_t vfo,freq_t * freq)1689 int kenwood_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
1690 {
1691 char freqbuf[50];
1692 char cmdbuf[4];
1693 int retval;
1694 unsigned char vfo_letter = '\0';
1695 vfo_t tvfo;
1696 struct kenwood_priv_data *priv = rig->state.priv;
1697
1698 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1699
1700 if (!freq)
1701 {
1702 RETURNFUNC(-RIG_EINVAL);
1703 }
1704
1705 tvfo = (vfo == RIG_VFO_CURR
1706 || vfo == RIG_VFO_VFO) ? rig->state.current_vfo : vfo;
1707
1708 if (RIG_VFO_CURR == tvfo)
1709 {
1710 /* fetch from rig */
1711 retval = rig_get_vfo(rig, &tvfo);
1712
1713 if (RIG_OK != retval) { RETURNFUNC(retval); }
1714 }
1715
1716 /* memory frequency cannot be read with an Fx command, use IF */
1717 if (tvfo == RIG_VFO_MEM)
1718 {
1719
1720 RETURNFUNC(kenwood_get_freq_if(rig, vfo, freq));
1721 }
1722
1723 switch (tvfo)
1724 {
1725 case RIG_VFO_A:
1726 case RIG_VFO_MAIN:
1727 vfo_letter = 'A';
1728 break;
1729
1730 case RIG_VFO_B:
1731 case RIG_VFO_SUB:
1732 vfo_letter = 'B';
1733 break;
1734
1735 case RIG_VFO_C:
1736 vfo_letter = 'C';
1737 break;
1738
1739 case RIG_VFO_TX:
1740 if (priv->split) { vfo_letter = 'B'; } // always assume B is the TX VFO
1741 else { vfo_letter = 'A'; }
1742
1743 break;
1744
1745 default:
1746 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
1747 RETURNFUNC(-RIG_EINVAL);
1748 }
1749
1750 snprintf(cmdbuf, sizeof(cmdbuf), "F%c", vfo_letter);
1751
1752 retval = kenwood_safe_transaction(rig, cmdbuf, freqbuf, 50, 13);
1753
1754 if (retval != RIG_OK)
1755 {
1756 RETURNFUNC(retval);
1757 }
1758
1759 sscanf(freqbuf + 2, "%"SCNfreq, freq);
1760
1761 RETURNFUNC(RIG_OK);
1762 }
1763
kenwood_get_rit(RIG * rig,vfo_t vfo,shortfreq_t * rit)1764 int kenwood_get_rit(RIG *rig, vfo_t vfo, shortfreq_t *rit)
1765 {
1766 int retval;
1767 char buf[7];
1768 struct kenwood_priv_data *priv = rig->state.priv;
1769
1770 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1771
1772 if (!rit)
1773 {
1774 RETURNFUNC(-RIG_EINVAL);
1775 }
1776
1777 retval = kenwood_get_if(rig);
1778
1779 if (retval != RIG_OK)
1780 {
1781 RETURNFUNC(retval);
1782 }
1783
1784 // TODO: Fix for different rigs
1785 memcpy(buf, &priv->info[17], 6);
1786
1787 buf[6] = '\0';
1788 *rit = atoi(buf);
1789
1790 RETURNFUNC(RIG_OK);
1791 }
1792
1793 /*
1794 * rit can only move up/down by 10 Hz, so we use a loop...
1795 */
kenwood_set_rit(RIG * rig,vfo_t vfo,shortfreq_t rit)1796 int kenwood_set_rit(RIG *rig, vfo_t vfo, shortfreq_t rit)
1797 {
1798 char buf[32];
1799 int retval, i;
1800 int diff;
1801 int rit_enabled;
1802 int xit_enabled;
1803 shortfreq_t curr_rit;
1804 struct kenwood_priv_data *priv = rig->state.priv;
1805
1806 rig_debug(RIG_DEBUG_VERBOSE, "%s called: vfo=%s, rit=%ld\n",
1807 __func__,
1808 rig_strvfo(vfo), rit);
1809
1810 // RC clear command cannot be executed if RIT/XIT is not enabled
1811 retval = kenwood_get_func(rig, vfo, RIG_FUNC_RIT, &rit_enabled);
1812
1813 if (retval != RIG_OK)
1814 {
1815 RETURNFUNC(retval);
1816 }
1817
1818 if (!rit_enabled)
1819 {
1820 retval = kenwood_get_func(rig, vfo, RIG_FUNC_XIT, &xit_enabled);
1821
1822 if (retval != RIG_OK)
1823 {
1824 RETURNFUNC(retval);
1825 }
1826 }
1827
1828 if (!rit_enabled && !xit_enabled)
1829 {
1830 retval = kenwood_set_func(rig, vfo, RIG_FUNC_RIT, 1);
1831
1832 if (retval != RIG_OK)
1833 {
1834 RETURNFUNC(retval);
1835 }
1836 }
1837
1838 // by getting current rit we can determine how to handle change
1839 // we just use curr_rit - rit to determine how far we need to move
1840 // No need to zero out rit
1841 retval = kenwood_get_rit(rig, RIG_VFO_CURR, &curr_rit);
1842
1843 if (retval != RIG_OK)
1844 {
1845 RETURNFUNC(retval);
1846 }
1847
1848 #if 0 // no longer needed if diff can be done
1849 retval = kenwood_transaction(rig, "RC", NULL, 0);
1850
1851 if (retval != RIG_OK)
1852 {
1853 RETURNFUNC(retval);
1854 }
1855
1856 #endif
1857
1858 if (rit == 0 && curr_rit == 0)
1859 {
1860 RETURNFUNC(RIG_OK);
1861 }
1862
1863 if (priv->has_rit2)
1864 {
1865 diff = rit - curr_rit;
1866 rig_debug(RIG_DEBUG_TRACE, "%s: rit=%ld, curr_rit=%ld, diff=%d\n", __func__,
1867 rit, curr_rit, diff);
1868 snprintf(buf, sizeof(buf), "R%c%05d", (diff > 0) ? 'U' : 'D', abs((int) diff));
1869 retval = kenwood_transaction(rig, buf, NULL, 0);
1870 }
1871 else
1872 {
1873 snprintf(buf, sizeof(buf), "R%c", (rit > 0) ? 'U' : 'D');
1874 diff = labs(((curr_rit - rit) + (curr_rit - rit) >= 0 ? 5 : -5) /
1875 10); // round to nearest 10Hz
1876 rig_debug(RIG_DEBUG_TRACE, "%s: rit=%ld, curr_rit=%ld, diff=%d\n", __func__,
1877 rit, curr_rit, diff);
1878 rig_debug(RIG_DEBUG_TRACE, "%s: rit change loop=%d\n", __func__, diff);
1879
1880 for (i = 0; i < diff; i++)
1881 {
1882 retval = kenwood_transaction(rig, buf, NULL, 0);
1883 }
1884 }
1885
1886 RETURNFUNC(retval);
1887 }
1888
1889 /*
1890 * rit and xit are the same
1891 */
kenwood_get_xit(RIG * rig,vfo_t vfo,shortfreq_t * rit)1892 int kenwood_get_xit(RIG *rig, vfo_t vfo, shortfreq_t *rit)
1893 {
1894 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1895
1896 RETURNFUNC(kenwood_get_rit(rig, vfo, rit));
1897 }
1898
kenwood_set_xit(RIG * rig,vfo_t vfo,shortfreq_t rit)1899 int kenwood_set_xit(RIG *rig, vfo_t vfo, shortfreq_t rit)
1900 {
1901 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1902
1903 RETURNFUNC(kenwood_set_rit(rig, vfo, rit));
1904 }
1905
kenwood_scan(RIG * rig,vfo_t vfo,scan_t scan,int ch)1906 int kenwood_scan(RIG *rig, vfo_t vfo, scan_t scan, int ch)
1907 {
1908 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1909
1910 if (RIG_IS_TS990S)
1911 {
1912 RETURNFUNC(kenwood_transaction(rig, scan == RIG_SCAN_STOP ? "SC00" : "SC01",
1913 NULL, 0));
1914 }
1915 else
1916 {
1917 RETURNFUNC(kenwood_transaction(rig, scan == RIG_SCAN_STOP ? "SC0" : "SC1", NULL,
1918 0));
1919 }
1920 }
1921
1922 /*
1923 * 000 No select
1924 * 002 FM Wide
1925 * 003 FM Narrow
1926 * 005 AM
1927 * 007 SSB
1928 * 009 CW
1929 * 010 CW NARROW
1930 */
1931
1932 /* XXX revise */
kenwood_set_filter(RIG * rig,pbwidth_t width)1933 static int kenwood_set_filter(RIG *rig, pbwidth_t width)
1934 {
1935 char *cmd;
1936
1937 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
1938
1939 if (width <= Hz(250))
1940 {
1941 cmd = "FL010009";
1942 }
1943 else if (width <= Hz(500))
1944 {
1945 cmd = "FL009009";
1946 }
1947 else if (width <= kHz(2.7))
1948 {
1949 cmd = "FL007007";
1950 }
1951 else if (width <= kHz(6))
1952 {
1953 cmd = "FL005005";
1954 }
1955 else
1956 {
1957 cmd = "FL002002";
1958 }
1959
1960 RETURNFUNC(kenwood_transaction(rig, cmd, NULL, 0));
1961 }
1962
kenwood_set_filter_width(RIG * rig,rmode_t mode,pbwidth_t width)1963 static int kenwood_set_filter_width(RIG *rig, rmode_t mode, pbwidth_t width)
1964 {
1965 struct kenwood_priv_caps *caps = kenwood_caps(rig);
1966 struct kenwood_filter_width *selected_filter_width = NULL;
1967 char cmd[20];
1968 int i;
1969
1970 rig_debug(RIG_DEBUG_VERBOSE, "%s called, width=%ld\n", __func__, width);
1971
1972 if (caps->filter_width == NULL)
1973 {
1974 RETURNFUNC(-RIG_ENAVAIL);
1975 }
1976
1977 for (i = 0; caps->filter_width[i].value >= 0; i++)
1978 {
1979 if (caps->filter_width[i].modes & mode)
1980 {
1981 selected_filter_width = &caps->filter_width[i];
1982
1983 if (caps->filter_width[i].width_hz >= width)
1984 {
1985 break;
1986 }
1987 }
1988 }
1989
1990 if (selected_filter_width == NULL)
1991 {
1992 RETURNFUNC(-RIG_EINVAL);
1993 }
1994
1995 snprintf(cmd, sizeof(cmd), "FW%04d", selected_filter_width->value);
1996
1997 RETURNFUNC(kenwood_transaction(rig, cmd, NULL, 0));
1998 }
1999
2000 /*
2001 * kenwood_set_mode
2002 */
kenwood_set_mode(RIG * rig,vfo_t vfo,rmode_t mode,pbwidth_t width)2003 int kenwood_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
2004 {
2005 char c;
2006 char kmode;
2007 char buf[6];
2008 char data_mode = '0';
2009 int err;
2010 struct kenwood_priv_data *priv = rig->state.priv;
2011 struct kenwood_priv_caps *caps = kenwood_caps(rig);
2012
2013
2014 rig_debug(RIG_DEBUG_VERBOSE, "%s called, vfo=%s, mode=%s, width=%d\n", __func__,
2015 rig_strvfo(vfo), rig_strrmode(mode), (int)width);
2016
2017 if (RIG_IS_TS590S || RIG_IS_TS590SG || RIG_IS_TS950S || RIG_IS_TS950SDX)
2018 {
2019 /* supports DATA sub modes */
2020 switch (mode)
2021 {
2022 case RIG_MODE_PKTUSB:
2023 data_mode = '1';
2024 mode = RIG_MODE_USB;
2025 break;
2026
2027 case RIG_MODE_PKTLSB:
2028 data_mode = '1';
2029 mode = RIG_MODE_LSB;
2030 break;
2031
2032 case RIG_MODE_PKTFM:
2033 data_mode = '1';
2034 mode = RIG_MODE_FM;
2035 break;
2036
2037 default: break;
2038 }
2039 }
2040
2041 if (priv->is_emulation || RIG_IS_HPSDR)
2042 {
2043 /* emulations like PowerSDR and SmartSDR normally hijack the
2044 RTTY modes for SSB-DATA AFSK modes */
2045 if (RIG_MODE_PKTLSB == mode) { mode = RIG_MODE_RTTY; }
2046
2047 if (RIG_MODE_PKTUSB == mode) { mode = RIG_MODE_RTTYR; }
2048 }
2049
2050 kmode = rmode2kenwood(mode, caps->mode_table);
2051
2052 if (kmode < 0)
2053 {
2054 rig_debug(RIG_DEBUG_WARN, "%s: unsupported mode '%s'\n",
2055 __func__, rig_strrmode(mode));
2056 RETURNFUNC(-RIG_EINVAL);
2057 }
2058
2059 if (kmode <= 9)
2060 {
2061 c = '0' + kmode;
2062 }
2063 else
2064 {
2065 c = 'A' + kmode - 10;
2066 }
2067
2068 rig_debug(RIG_DEBUG_VERBOSE, "%s: kmode=%d, cmode=%c\n", __func__, kmode, c);
2069
2070 if (RIG_IS_TS990S)
2071 {
2072 /* The TS990s has targetable read mode but can only set the mode
2073 of the current VFO :( So we need to toggle the operating VFO
2074 to set the "back" VFO mode. This is done here rather than not
2075 setting caps.targetable_vfo to not include
2076 RIG_TARGETABLE_MODE since the toggle is not required for
2077 reading the mode. */
2078 vfo_t curr_vfo;
2079 err = kenwood_get_vfo_main_sub(rig, &curr_vfo);
2080
2081 if (err != RIG_OK) { RETURNFUNC(err); }
2082
2083 if (vfo != RIG_VFO_CURR && vfo != curr_vfo)
2084 {
2085 err = kenwood_set_vfo_main_sub(rig, vfo);
2086
2087 if (err != RIG_OK) { RETURNFUNC(err); }
2088 }
2089
2090 snprintf(buf, sizeof(buf), "OM0%c", c); /* target vfo is ignored */
2091 err = kenwood_transaction(rig, buf, NULL, 0);
2092
2093 if (err == RIG_OK && vfo != RIG_VFO_CURR && vfo != curr_vfo)
2094 {
2095 int err2 = kenwood_set_vfo_main_sub(rig, curr_vfo);
2096
2097 if (err2 != RIG_OK) { RETURNFUNC(err2); }
2098 }
2099 }
2100 else
2101 {
2102 pbwidth_t twidth;
2103 err = rig_get_mode(rig, vfo, &priv->curr_mode, &twidth);
2104
2105 // only change mode if needed
2106 if (priv->curr_mode != mode)
2107 {
2108 snprintf(buf, sizeof(buf), "MD%c", c);
2109 err = kenwood_transaction(rig, buf, NULL, 0);
2110 }
2111 }
2112
2113 if (err != RIG_OK) { RETURNFUNC(err); }
2114
2115 if (RIG_IS_TS590S || RIG_IS_TS590SG || RIG_IS_TS950S || RIG_IS_TS950SDX)
2116 {
2117 if (!(RIG_MODE_CW == mode
2118 || RIG_MODE_CWR == mode
2119 || RIG_MODE_AM == mode
2120 || RIG_MODE_RTTY == mode
2121 || RIG_MODE_RTTYR == mode))
2122 {
2123 char *data_cmd = "DA";
2124
2125 if (RIG_IS_TS950S || RIG_IS_TS950SDX)
2126 {
2127 data_cmd = "DT";
2128 }
2129
2130 /* supports DATA sub modes - see above */
2131 snprintf(buf, sizeof(buf), "%s%c", data_cmd, data_mode);
2132 err = kenwood_transaction(rig, buf, NULL, 0);
2133
2134 if (err != RIG_OK) { RETURNFUNC(err); }
2135 }
2136 }
2137
2138 if (RIG_PASSBAND_NOCHANGE == width) { RETURNFUNC(RIG_OK); }
2139
2140 if (RIG_IS_TS450S || RIG_IS_TS690S || RIG_IS_TS850 || RIG_IS_TS950S
2141 || RIG_IS_TS950SDX)
2142 {
2143
2144 if (RIG_PASSBAND_NORMAL == width)
2145 {
2146 width = rig_passband_normal(rig, mode);
2147 }
2148
2149 kenwood_set_filter(rig, width);
2150 /* non fatal */
2151 }
2152 else if (RIG_IS_TS480)
2153 {
2154 err = kenwood_set_filter_width(rig, mode, width);
2155
2156 if (err != RIG_OK)
2157 {
2158 // Ignore errors as non-fatal
2159 rig_debug(RIG_DEBUG_ERR, "%s: error setting filter width, error: %d\n",
2160 __func__, err);
2161 }
2162 }
2163
2164 RETURNFUNC(RIG_OK);
2165 }
2166
kenwood_get_filter(RIG * rig,pbwidth_t * width)2167 static int kenwood_get_filter(RIG *rig, pbwidth_t *width)
2168 {
2169 int err, f, f1, f2;
2170 char buf[10];
2171
2172 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
2173
2174 if (!width)
2175 {
2176 RETURNFUNC(-RIG_EINVAL);
2177 }
2178
2179 err = kenwood_safe_transaction(rig, "FL", buf, sizeof(buf), 8);
2180
2181 if (err != RIG_OK)
2182 {
2183 RETURNFUNC(err);
2184 }
2185
2186 f2 = atoi(&buf[5]);
2187
2188 buf[5] = '\0';
2189 f1 = atoi(&buf[2]);
2190
2191 if (f2 > f1)
2192 {
2193 f = f2;
2194 }
2195 else
2196 {
2197 f = f1;
2198 }
2199
2200 switch (f)
2201 {
2202 case 2:
2203 *width = kHz(12);
2204 break;
2205
2206 case 3:
2207 case 5:
2208 *width = kHz(6);
2209 break;
2210
2211 case 7:
2212 *width = kHz(2.7);
2213 break;
2214
2215 case 9:
2216 *width = Hz(500);
2217 break;
2218
2219 case 10:
2220 *width = Hz(250);
2221 break;
2222 }
2223
2224 RETURNFUNC(RIG_OK);
2225 }
2226
kenwood_get_filter_width(RIG * rig,rmode_t mode,pbwidth_t * width)2227 static int kenwood_get_filter_width(RIG *rig, rmode_t mode, pbwidth_t *width)
2228 {
2229 struct kenwood_priv_caps *caps = kenwood_caps(rig);
2230 char ackbuf[20];
2231 int i;
2232 int retval;
2233 int filter_value;
2234
2235 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
2236
2237 if (caps->filter_width == NULL)
2238 {
2239 RETURNFUNC(-RIG_ENAVAIL);
2240 }
2241
2242 retval = kenwood_safe_transaction(rig, "FW", ackbuf, sizeof(ackbuf), 6);
2243
2244 if (retval != RIG_OK)
2245 {
2246 RETURNFUNC(retval);
2247 }
2248
2249 sscanf(ackbuf, "FW%d", &filter_value);
2250
2251 for (i = 0; caps->filter_width[i].value >= 0; i++)
2252 {
2253 if (caps->filter_width[i].modes & mode)
2254 {
2255 if (caps->filter_width[i].value == filter_value)
2256 {
2257 *width = caps->filter_width[i].width_hz;
2258 RETURNFUNC(RIG_OK);
2259 }
2260 }
2261 }
2262
2263 RETURNFUNC(-RIG_EINVAL);
2264 }
2265
2266 /*
2267 * kenwood_get_mode
2268 */
kenwood_get_mode(RIG * rig,vfo_t vfo,rmode_t * mode,pbwidth_t * width)2269 int kenwood_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
2270 {
2271 char cmd[4];
2272 char modebuf[10];
2273 int offs;
2274 int retval;
2275 int kmode;
2276
2277 struct kenwood_priv_data *priv = rig->state.priv;
2278 struct kenwood_priv_caps *caps = kenwood_caps(rig);
2279
2280 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
2281
2282 if (!mode || !width)
2283 {
2284 RETURNFUNC(-RIG_EINVAL);
2285 }
2286
2287 /* for emulation do not read mode from VFOB as it is copy of VFOA */
2288 /* we avoid the VFO swapping most of the time this way */
2289 /* only need to get it if it has to be initialized */
2290 if (priv->curr_mode > 0 && priv->is_emulation && vfo == RIG_VFO_B)
2291 {
2292 RETURNFUNC(priv->curr_mode);
2293 }
2294
2295 if (RIG_IS_TS990S)
2296 {
2297 char c;
2298
2299 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
2300 {
2301 if (RIG_OK != (retval = kenwood_get_vfo_main_sub(rig, &vfo)))
2302 {
2303 RETURNFUNC(retval);
2304 }
2305 }
2306
2307 switch (vfo)
2308 {
2309 case RIG_VFO_MAIN: c = '0'; break;
2310
2311 case RIG_VFO_SUB: c = '1'; break;
2312
2313 default:
2314 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
2315 RETURNFUNC(-RIG_EINVAL);
2316 }
2317
2318 snprintf(cmd, sizeof(cmd), "OM%c", c);
2319 offs = 3;
2320 }
2321 else
2322 {
2323 snprintf(cmd, sizeof(cmd), "MD");
2324 offs = 2;
2325 }
2326
2327 retval = kenwood_safe_transaction(rig, cmd, modebuf, 6, offs + 1);
2328
2329 if (retval != RIG_OK)
2330 {
2331 RETURNFUNC(retval);
2332 }
2333
2334 if (modebuf[offs] <= '9')
2335 {
2336 kmode = modebuf[offs] - '0';
2337 }
2338 else
2339 {
2340 kmode = modebuf[offs] - 'A' + 10;
2341 }
2342
2343 *mode = kenwood2rmode(kmode, caps->mode_table);
2344
2345 if (priv->is_emulation || RIG_IS_HPSDR)
2346 {
2347 /* emulations like PowerSDR and SmartSDR normally hijack the
2348 RTTY modes for SSB-DATA AFSK modes */
2349 if (RIG_MODE_RTTY == *mode) { *mode = RIG_MODE_PKTLSB; }
2350
2351 if (RIG_MODE_RTTYR == *mode) { *mode = RIG_MODE_PKTUSB; }
2352 }
2353
2354 if (RIG_IS_TS590S || RIG_IS_TS590SG || RIG_IS_TS950S || RIG_IS_TS950SDX)
2355 {
2356 /* supports DATA sub-modes */
2357 retval = kenwood_safe_transaction(rig, "DA", modebuf, 6, 3);
2358
2359 if (retval != RIG_OK)
2360 {
2361 RETURNFUNC(retval);
2362 }
2363
2364 if ('1' == modebuf[2])
2365 {
2366 switch (*mode)
2367 {
2368 case RIG_MODE_USB: *mode = RIG_MODE_PKTUSB; break;
2369
2370 case RIG_MODE_LSB: *mode = RIG_MODE_PKTLSB; break;
2371
2372 case RIG_MODE_FM: *mode = RIG_MODE_PKTFM; break;
2373
2374 default: break;
2375 }
2376 }
2377 }
2378
2379 if (RIG_IS_TS480)
2380 {
2381 retval = kenwood_get_filter_width(rig, *mode, width);
2382
2383 if (retval != RIG_OK)
2384 {
2385 // Ignore errors as non-fatal
2386 rig_debug(RIG_DEBUG_ERR, "%s: error getting filter width, error: %d\n",
2387 __func__, retval);
2388 *width = rig_passband_normal(rig, *mode);
2389 }
2390 }
2391 else
2392 {
2393 *width = rig_passband_normal(rig, *mode);
2394 }
2395
2396 RETURNFUNC(RIG_OK);
2397 }
2398
2399 /* This is used when the radio does not support MD; for mode reading */
kenwood_get_mode_if(RIG * rig,vfo_t vfo,rmode_t * mode,pbwidth_t * width)2400 int kenwood_get_mode_if(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
2401 {
2402 int err;
2403 struct kenwood_priv_caps *caps = kenwood_caps(rig);
2404 struct kenwood_priv_data *priv = rig->state.priv;
2405
2406 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
2407
2408 if (!mode || !width)
2409 {
2410 RETURNFUNC(-RIG_EINVAL);
2411 }
2412
2413 err = kenwood_get_if(rig);
2414
2415 if (err != RIG_OK)
2416 {
2417 RETURNFUNC(err);
2418 }
2419
2420 *mode = kenwood2rmode(priv->info[29] - '0', caps->mode_table);
2421
2422 *width = rig_passband_normal(rig, *mode);
2423
2424 if (RIG_IS_TS450S || RIG_IS_TS690S || RIG_IS_TS850 || RIG_IS_TS950S
2425 || RIG_IS_TS950SDX)
2426 {
2427
2428 kenwood_get_filter(rig, width);
2429 /* non fatal */
2430 }
2431
2432 RETURNFUNC(RIG_OK);
2433 }
2434
2435 /* kenwood_get_micgain_minmax
2436 * Kenwood rigs have different micgain levels
2437 * This routine relies on the idea that setting the micgain
2438 * to 0 and 255 will result in the minimum and maximum values being set
2439 * If a rig doesn't behave this way then customize inside that rig's backend
2440 */
kenwood_get_micgain_minmax(RIG * rig,int * micgain_now,int * micgain_min,int * micgain_max,int restore)2441 static int kenwood_get_micgain_minmax(RIG *rig, int *micgain_now,
2442 int *micgain_min,
2443 int *micgain_max,
2444 int restore)
2445 {
2446 int retval;
2447 char levelbuf[19];
2448 // read micgain_now, set 0, read micgain_min, set 255, read_micgain_max; set 0
2449 // we set back to 0 for safety and if restore is true we restore micgain_min
2450 // otherwise we expect calling routine to be setting new micgain level
2451 // we batch these commands together for speed
2452 char *cmd = "MG;MG000;MG;MG255;MG;MG000;";
2453 int n;
2454 struct rig_state *rs = &rig->state;
2455
2456 rig_debug(RIG_DEBUG_TRACE, "%s: called\n", __func__);
2457 retval = write_block(&rs->rigport, cmd, strlen(cmd));
2458
2459 if (retval != RIG_OK) { RETURNFUNC(retval); }
2460
2461 retval = read_string(&rs->rigport, levelbuf, sizeof(levelbuf), NULL, 0);
2462
2463 rig_debug(RIG_DEBUG_TRACE, "%s: retval=%d\n", __func__, retval);
2464
2465 if (retval != 18)
2466 {
2467 rig_debug(RIG_DEBUG_ERR, "%s: expected 19, got %d in '%s'\n", __func__, retval,
2468 levelbuf);
2469 RETURNFUNC(-RIG_EPROTO);
2470 }
2471
2472 n = sscanf(levelbuf, "MG%d;MG%d;MG%d", micgain_now, micgain_min, micgain_max);
2473
2474 if (n != 3)
2475 {
2476 rig_debug(RIG_DEBUG_ERR, "%s: count not parse 3 values from '%s'\n", __func__,
2477 levelbuf);
2478 RETURNFUNC(-RIG_EPROTO);
2479 }
2480
2481 if (restore)
2482 {
2483 snprintf(levelbuf, sizeof(levelbuf), "MG%03d;", *micgain_now);
2484 retval = kenwood_transaction(rig, levelbuf, NULL, 0);
2485 RETURNFUNC(retval);
2486 }
2487
2488 rig_debug(RIG_DEBUG_TRACE, "%s: returning now=%d, min=%d, max=%d\n", __func__,
2489 *micgain_now, *micgain_min, *micgain_max);
2490 RETURNFUNC(RIG_OK);
2491 }
2492
2493 /* kenwood_get_power_minmax
2494 * Kenwood rigs have different power levels by mode and by rig
2495 * This routine relies on the idea that setting the power
2496 * to 0 and 255 will result in the minimum and maximum values being set
2497 * If a rig doesn't behave this way then customize inside that rig's backend
2498 */
kenwood_get_power_minmax(RIG * rig,int * power_now,int * power_min,int * power_max,int restore)2499 static int kenwood_get_power_minmax(RIG *rig, int *power_now, int *power_min,
2500 int *power_max,
2501 int restore)
2502 {
2503 int retval, expval;
2504 char levelbuf[19];
2505 // read power_now, set 0, read power_min, set 255, read_power_max; set 0
2506 // we set back to 0 for safety and if restore is true we restore power_min
2507 // otherwise we expect calling routine to be setting new power level
2508 // we batch these commands together for speed
2509 char *cmd;
2510 int n;
2511 struct rig_state *rs = &rig->state;
2512
2513 rig_debug(RIG_DEBUG_TRACE, "%s: called\n", __func__);
2514
2515 switch (rig->caps->rig_model)
2516 {
2517 // TS480 can't handle the long command string
2518 // We can treat it like the TS890S
2519 case RIG_MODEL_TS480:
2520
2521 // TS890S can't take power levels outside 5-100 and 5-25
2522 // So all we'll do is read power_now
2523 case RIG_MODEL_TS890S:
2524 rig->state.power_min = *power_min = 5;
2525 rig->state.power_max = *power_max = 100;
2526
2527 if (rig->state.current_mode == RIG_MODE_AM) { *power_max = 50; }
2528
2529 if (rig->state.current_freq >= 70)
2530 {
2531 rig->state.power_max = 50;
2532
2533 if (rig->state.current_mode == RIG_MODE_AM) { *power_max = 13; }
2534 }
2535
2536
2537 cmd = "PC;";
2538 break;
2539
2540 default:
2541 cmd = "PC;PC000;PC;PC255;PC;PC000;";
2542 }
2543
2544 // Don't do this if PTT is on...don't want to max out power!!
2545 if (rig->state.cache.ptt == RIG_PTT_ON)
2546 {
2547 rig_debug(RIG_DEBUG_TRACE, "%s: ptt on so not checking min/max power levels\n",
2548 __func__);
2549 // return the last values we got
2550 *power_now = rig->state.power_now;
2551 *power_min = rig->state.power_min;
2552 *power_max = rig->state.power_max;
2553 RETURNFUNC(RIG_OK);
2554 }
2555
2556 retval = write_block(&rs->rigport, cmd, strlen(cmd));
2557
2558 if (retval != RIG_OK) { RETURNFUNC(retval); }
2559
2560 retval = read_string(&rs->rigport, levelbuf, sizeof(levelbuf), NULL, 0);
2561
2562 rig_debug(RIG_DEBUG_TRACE, "%s: retval=%d\n", __func__, retval);
2563
2564 if (RIG_IS_TS890S || RIG_IS_TS480)
2565 {
2566 expval = 6;
2567 }
2568 else
2569 {
2570 expval = 18;
2571 }
2572
2573 if (retval != expval)
2574 {
2575 rig_debug(RIG_DEBUG_ERR, "%s: expected %d, got %d in '%s'\n", __func__, expval,
2576 retval,
2577 levelbuf);
2578 RETURNFUNC(-RIG_EPROTO);
2579 }
2580
2581 if (RIG_IS_TS890S || RIG_IS_TS480)
2582 {
2583 n = sscanf(levelbuf, "PC%d;", power_now);
2584
2585 if (n != 1)
2586 {
2587 rig_debug(RIG_DEBUG_ERR, "%s: count not parse 1 value from '%s'\n", __func__,
2588 levelbuf);
2589 RETURNFUNC(-RIG_EPROTO);
2590 }
2591 }
2592 else
2593 {
2594 n = sscanf(levelbuf, "PC%d;PC%d;PC%d", power_now, power_min, power_max);
2595
2596 if (n != 3)
2597 {
2598 rig_debug(RIG_DEBUG_ERR, "%s: count not parse 3 values from '%s'\n", __func__,
2599 levelbuf);
2600 RETURNFUNC(-RIG_EPROTO);
2601 }
2602
2603 if (restore) // only need to restore if 3-value cmd is done
2604 {
2605 snprintf(levelbuf, sizeof(levelbuf), "PC%03d;", *power_now);
2606 retval = kenwood_transaction(rig, levelbuf, NULL, 0);
2607 RETURNFUNC(retval);
2608 }
2609 }
2610
2611 rig_debug(RIG_DEBUG_TRACE, "%s: returning now=%d, min=%d, max=%d\n", __func__,
2612 *power_now, *power_min, *power_max);
2613
2614 rig->state.power_now = *power_now;
2615 rig->state.power_min = *power_min;
2616 rig->state.power_max = *power_max;
2617 RETURNFUNC(RIG_OK);
2618 }
2619
kenwood_find_slope_filter_for_frequency(RIG * rig,vfo_t vfo,struct kenwood_slope_filter * filter,int frequency_hz,int * value)2620 static int kenwood_find_slope_filter_for_frequency(RIG *rig, vfo_t vfo,
2621 struct kenwood_slope_filter *filter, int frequency_hz, int *value)
2622 {
2623 int retval;
2624 int i;
2625 struct kenwood_slope_filter *last_filter = NULL;
2626 freq_t freq;
2627 int cache_ms_freq;
2628 rmode_t mode;
2629 int cache_ms_mode;
2630 pbwidth_t width;
2631 int cache_ms_width;
2632 int data_mode_filter_active;
2633
2634 if (filter == NULL)
2635 {
2636 return -RIG_ENAVAIL;
2637 }
2638
2639 retval = rig_get_cache(rig, vfo, &freq, &cache_ms_freq, &mode, &cache_ms_mode,
2640 &width, &cache_ms_width);
2641
2642 if (retval != RIG_OK)
2643 {
2644 return -RIG_EINVAL;
2645 }
2646
2647 retval = rig_get_ext_func(rig, vfo, TOK_FUNC_FILTER_WIDTH_DATA,
2648 &data_mode_filter_active);
2649
2650 if (retval != RIG_OK)
2651 {
2652 // Ignore errors, e.g. if the command is not supported
2653 data_mode_filter_active = 0;
2654 }
2655
2656 for (i = 0; filter[i].value >= 0; i++)
2657 {
2658 if (filter[i].modes & mode
2659 && filter[i].data_mode_filter == data_mode_filter_active)
2660 {
2661 if (filter[i].frequency_hz >= frequency_hz)
2662 {
2663 *value = filter[i].value;
2664 return RIG_OK;
2665 }
2666
2667 last_filter = &filter[i];
2668 }
2669 }
2670
2671 if (last_filter != NULL)
2672 {
2673 *value = last_filter->value;
2674 return RIG_OK;
2675 }
2676
2677 return -RIG_EINVAL;
2678 }
2679
kenwood_find_slope_filter_for_value(RIG * rig,vfo_t vfo,struct kenwood_slope_filter * filter,int value,int * frequency_hz)2680 static int kenwood_find_slope_filter_for_value(RIG *rig, vfo_t vfo,
2681 struct kenwood_slope_filter *filter, int value, int *frequency_hz)
2682 {
2683 int retval;
2684 int i;
2685 freq_t freq;
2686 int cache_ms_freq;
2687 rmode_t mode;
2688 int cache_ms_mode;
2689 pbwidth_t width;
2690 int cache_ms_width;
2691 int data_mode_filter_active;
2692
2693 if (filter == NULL)
2694 {
2695 return -RIG_ENAVAIL;
2696 }
2697
2698 retval = rig_get_cache(rig, vfo, &freq, &cache_ms_freq, &mode, &cache_ms_mode,
2699 &width, &cache_ms_width);
2700
2701 if (retval != RIG_OK)
2702 {
2703 return -RIG_EINVAL;
2704 }
2705
2706 retval = rig_get_ext_func(rig, vfo, TOK_FUNC_FILTER_WIDTH_DATA,
2707 &data_mode_filter_active);
2708
2709 if (retval != RIG_OK)
2710 {
2711 // Ignore errors, e.g. if the command is not supported
2712 data_mode_filter_active = 0;
2713 }
2714
2715 for (i = 0; filter[i].value >= 0; i++)
2716 {
2717 if (filter[i].modes & mode
2718 && filter[i].data_mode_filter == data_mode_filter_active)
2719 {
2720 if (filter[i].value == value)
2721 {
2722 *frequency_hz = filter[i].frequency_hz;
2723 return RIG_OK;
2724 }
2725 }
2726 }
2727
2728 return -RIG_EINVAL;
2729 }
2730
kenwood_set_level(RIG * rig,vfo_t vfo,setting_t level,value_t val)2731 int kenwood_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val)
2732 {
2733 char levelbuf[16];
2734 int i, kenwood_val;
2735 struct kenwood_priv_data *priv = rig->state.priv;
2736 struct kenwood_priv_caps *caps = kenwood_caps(rig);
2737
2738 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
2739
2740 if (RIG_LEVEL_IS_FLOAT(level))
2741 {
2742 if (val.f > 1.0) { RETURNFUNC(-RIG_EINVAL); }
2743
2744 kenwood_val = val.f * 255;
2745 }
2746 else
2747 {
2748 kenwood_val = val.i;
2749 }
2750
2751 switch (level)
2752 {
2753 int retval;
2754
2755 case RIG_LEVEL_RFPOWER:
2756 {
2757 int power_now, power_min, power_max;
2758 // Power min/max can vary so we query to find them out every time
2759 retval = kenwood_get_power_minmax(rig, &power_now, &power_min, &power_max, 0);
2760
2761 if (retval != RIG_OK) { RETURNFUNC(retval); }
2762
2763 // https://github.com/Hamlib/Hamlib/issues/465
2764 kenwood_val = val.f * power_max;
2765
2766 if (kenwood_val < power_min) { kenwood_val = power_min; }
2767
2768 if (kenwood_val > power_max) { kenwood_val = power_max; }
2769
2770 snprintf(levelbuf, sizeof(levelbuf), "PC%03d", kenwood_val);
2771 break;
2772 }
2773
2774
2775 case RIG_LEVEL_AF:
2776 {
2777 int vfo_set = vfo == RIG_VFO_A || vfo == RIG_VFO_MAIN ? 0 : 1;
2778
2779 // some rigs only recognize 0 for vfo_set
2780 // https://github.com/Hamlib/Hamlib/issues/304
2781 // This is now fixed for all rigs
2782 // https://github.com/Hamlib/Hamlib/issues/380
2783 // ag_format is determined in kenwood_get_level
2784 switch (priv->ag_format)
2785 {
2786 case 1:
2787 snprintf(levelbuf, sizeof(levelbuf), "AG%03d", kenwood_val);
2788 break;
2789
2790 case 2:
2791 snprintf(levelbuf, sizeof(levelbuf), "AG0%03d", kenwood_val);
2792 break;
2793
2794 case 3:
2795 snprintf(levelbuf, sizeof(levelbuf), "AG%d%03d", vfo_set, kenwood_val);
2796 break;
2797
2798 default:
2799 rig_debug(RIG_DEBUG_ERR, "%s: Unknown ag_format=%d\n", __func__,
2800 priv->ag_format);
2801 }
2802 }
2803 break;
2804
2805 case RIG_LEVEL_MICGAIN:
2806 {
2807 int micgain_now;
2808
2809 if (priv->micgain_min == -1) // then we need to know our min/max
2810 {
2811 retval = kenwood_get_micgain_minmax(rig, &micgain_now, &priv->micgain_min,
2812 &priv->micgain_max, 0);
2813
2814 if (retval != RIG_OK) { RETURNFUNC(retval); }
2815 }
2816
2817 if (val.f > 1.0 || val.f < 0) { RETURNFUNC(-RIG_EINVAL); }
2818
2819 // is micgain_min ever > 0 ??
2820 kenwood_val = val.f * (priv->micgain_max - priv->micgain_min) +
2821 priv->micgain_min;
2822 snprintf(levelbuf, sizeof(levelbuf), "MG%03d", kenwood_val);
2823 break;
2824 }
2825
2826 case RIG_LEVEL_RF:
2827
2828 /* XXX check level range */
2829 // KX2 and KX3 have range -190 to 250
2830 if (val.f > 1.0 || val.f < 0) { RETURNFUNC(-RIG_EINVAL); }
2831
2832 kenwood_val = val.f * 255.0;
2833 snprintf(levelbuf, sizeof(levelbuf), "RG%03d", kenwood_val);
2834 break;
2835
2836 case RIG_LEVEL_SQL:
2837 /* Default to RX#0 */
2838 snprintf(levelbuf, sizeof(levelbuf), "SQ0%03d", kenwood_val);
2839 break;
2840
2841 case RIG_LEVEL_AGC:
2842 if (kenwood_val > 3)
2843 {
2844 kenwood_val = 3; /* 0.. 255 */
2845 }
2846
2847 snprintf(levelbuf, sizeof(levelbuf), "GT%03d", 84 * kenwood_val);
2848 break;
2849
2850 case RIG_LEVEL_ATT:
2851
2852 /* set the attenuator if a correct value is entered */
2853 if (val.i == 0)
2854 {
2855 snprintf(levelbuf, sizeof(levelbuf), "RA00");
2856 }
2857 else
2858 {
2859 int foundit = 0;
2860
2861 for (i = 0; i < HAMLIB_MAXDBLSTSIZ && rig->state.attenuator[i]; i++)
2862 {
2863 if (val.i == rig->state.attenuator[i])
2864 {
2865 snprintf(levelbuf, sizeof(levelbuf), "RA%02d", i + 1);
2866 foundit = 1;
2867 break;
2868 }
2869 }
2870
2871 if (!foundit)
2872 {
2873 RETURNFUNC(-RIG_EINVAL);
2874 }
2875 }
2876
2877 break;
2878
2879 case RIG_LEVEL_PREAMP:
2880
2881 /* set the preamp if a correct value is entered */
2882 if (val.i == 0)
2883 {
2884 snprintf(levelbuf, sizeof(levelbuf), "PA0");
2885 }
2886 else
2887 {
2888 int foundit = 0;
2889
2890 for (i = 0; i < HAMLIB_MAXDBLSTSIZ && rig->state.preamp[i]; i++)
2891 {
2892 if (val.i == rig->state.preamp[i])
2893 {
2894 snprintf(levelbuf, sizeof(levelbuf), "PA%01d", i + 1);
2895 foundit = 1;
2896 break;
2897 }
2898 }
2899
2900 if (!foundit)
2901 {
2902 RETURNFUNC(-RIG_EINVAL);
2903 }
2904 }
2905
2906 break;
2907
2908 case RIG_LEVEL_SLOPE_HIGH:
2909 retval = kenwood_find_slope_filter_for_frequency(rig, vfo,
2910 caps->slope_filter_high, val.i, &kenwood_val);
2911
2912 if (retval != RIG_OK)
2913 {
2914 // Fall back to using raw values
2915 if (val.i > 20 || val.i < 0)
2916 {
2917 RETURNFUNC(-RIG_EINVAL);
2918 }
2919
2920 kenwood_val = val.i;
2921 }
2922
2923 snprintf(levelbuf, sizeof(levelbuf), "SH%02d", kenwood_val);
2924 break;
2925
2926 case RIG_LEVEL_SLOPE_LOW:
2927 retval = kenwood_find_slope_filter_for_frequency(rig, vfo,
2928 caps->slope_filter_low, val.i, &kenwood_val);
2929
2930 if (retval != RIG_OK)
2931 {
2932 // Fall back to using raw values
2933 if (val.i > 20 || val.i < 0)
2934 {
2935 RETURNFUNC(-RIG_EINVAL);
2936 }
2937
2938 kenwood_val = val.i;
2939 }
2940
2941 snprintf(levelbuf, sizeof(levelbuf), "SL%02d", kenwood_val);
2942 break;
2943
2944 case RIG_LEVEL_CWPITCH:
2945 if (val.i > 1000 || val.i < 400)
2946 {
2947 RETURNFUNC(-RIG_EINVAL);
2948 }
2949
2950 snprintf(levelbuf, sizeof(levelbuf), "PT%02d", (val.i / 50) - 8);
2951 break;
2952
2953 case RIG_LEVEL_KEYSPD:
2954 if (val.i > 60 || val.i < 5)
2955 {
2956 RETURNFUNC(-RIG_EINVAL);
2957 }
2958
2959 snprintf(levelbuf, sizeof(levelbuf), "KS%03d", val.i);
2960 break;
2961
2962 case RIG_LEVEL_COMP:
2963 if (RIG_IS_TS990S)
2964 {
2965 kenwood_val = val.f * 255.0f;
2966 }
2967 else
2968 {
2969 kenwood_val = val.f * 100.0f;
2970 }
2971
2972 snprintf(levelbuf, sizeof(levelbuf), "PL%03d%03d", kenwood_val, kenwood_val);
2973 break;
2974
2975 case RIG_LEVEL_VOXDELAY:
2976 if (val.i > 30 || val.i < 0)
2977 {
2978 RETURNFUNC(-RIG_EINVAL);
2979 }
2980
2981 // Raw value is in milliseconds
2982 snprintf(levelbuf, sizeof(levelbuf), "VD%04d", val.i * 100);
2983 break;
2984
2985 case RIG_LEVEL_VOXGAIN:
2986 kenwood_val = val.f * 9.0f;
2987 snprintf(levelbuf, sizeof(levelbuf), "VG%03d", kenwood_val);
2988 break;
2989
2990 case RIG_LEVEL_BKIN_DLYMS:
2991 if (val.i > 1000 || val.i < 0)
2992 {
2993 RETURNFUNC(-RIG_EINVAL);
2994 }
2995
2996 snprintf(levelbuf, sizeof(levelbuf), "SD%04d", val.i);
2997 break;
2998
2999 default:
3000 rig_debug(RIG_DEBUG_ERR, "%s: unsupported set_level %s", __func__,
3001 rig_strlevel(level));
3002 RETURNFUNC(-RIG_EINVAL);
3003 }
3004
3005 RETURNFUNC(kenwood_transaction(rig, levelbuf, NULL, 0));
3006 }
3007
get_kenwood_level(RIG * rig,const char * cmd,float * fval,int * ival)3008 int get_kenwood_level(RIG *rig, const char *cmd, float *fval, int *ival)
3009 {
3010 char lvlbuf[10];
3011 int retval;
3012 int lvl;
3013 int len = strlen(cmd);
3014
3015 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3016
3017 if (!fval && !ival)
3018 {
3019 RETURNFUNC(-RIG_EINVAL);
3020 }
3021
3022 retval = kenwood_safe_transaction(rig, cmd, lvlbuf, 10, len + 3);
3023
3024 if (retval != RIG_OK)
3025 {
3026 RETURNFUNC(retval);
3027 }
3028
3029 /* 000..255 */
3030 sscanf(lvlbuf + len, "%d", &lvl);
3031
3032 if (ival) { *ival = lvl; } // raw value
3033
3034 if (fval) { *fval = lvl / 255.0; } // our default scaling of 0-255
3035
3036 RETURNFUNC(RIG_OK);
3037 };
3038
3039
3040 /*
3041 * kenwood_get_level
3042 */
kenwood_get_level(RIG * rig,vfo_t vfo,setting_t level,value_t * val)3043 int kenwood_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val)
3044 {
3045 char lvlbuf[KENWOOD_MAX_BUF_LEN];
3046 char *cmd;
3047 int retval;
3048 int lvl;
3049 int i, ret, agclevel, len, value;
3050 struct kenwood_priv_data *priv = rig->state.priv;
3051 struct kenwood_priv_caps *caps = kenwood_caps(rig);
3052
3053 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3054
3055 if (!val)
3056 {
3057 RETURNFUNC(-RIG_EINVAL);
3058 }
3059
3060 switch (level)
3061 {
3062 int power_now, power_min, power_max;
3063
3064 case RIG_LEVEL_RAWSTR:
3065 if (RIG_IS_TS590S || RIG_IS_TS590SG)
3066 {
3067 cmd = "SM0";
3068 len = 3;
3069 }
3070 else
3071 {
3072 cmd = "SM";
3073 len = 2;
3074 }
3075
3076 retval = kenwood_safe_transaction(rig, cmd, lvlbuf, 10, len + 4);
3077
3078 if (retval != RIG_OK)
3079 {
3080 RETURNFUNC(retval);
3081 }
3082
3083 /* XXX atoi ? */
3084 sscanf(lvlbuf + len, "%d", &val->i); /* rawstr */
3085 break;
3086
3087 case RIG_LEVEL_STRENGTH:
3088 if (RIG_IS_TS590S || RIG_IS_TS590SG || RIG_IS_TS480)
3089 {
3090 cmd = "SM0";
3091 len = 3;
3092 }
3093 else
3094 {
3095 cmd = "SM";
3096 len = 2;
3097 }
3098
3099 retval = kenwood_safe_transaction(rig, cmd, lvlbuf, 10, len + 4);
3100
3101 if (retval != RIG_OK)
3102 {
3103 RETURNFUNC(retval);
3104 }
3105
3106 sscanf(lvlbuf + len, "%d", &val->i); /* rawstr */
3107
3108 if (rig->caps->str_cal.size)
3109 {
3110 val->i = (int) rig_raw2val(val->i, &rig->caps->str_cal);
3111 }
3112 else
3113 {
3114 val->i = (val->i * 4) - 54;
3115 }
3116
3117 break;
3118
3119 case RIG_LEVEL_ATT:
3120 retval = kenwood_safe_transaction(rig, "RA", lvlbuf, 50, 6);
3121
3122 if (retval != RIG_OK)
3123 {
3124 RETURNFUNC(retval);
3125 }
3126
3127 sscanf(lvlbuf + 2, "%d", &lvl);
3128
3129 if (lvl == 0)
3130 {
3131 val->i = 0;
3132 }
3133 else
3134 {
3135 for (i = 0; i < lvl && i < HAMLIB_MAXDBLSTSIZ; i++)
3136 {
3137 if (rig->state.attenuator[i] == 0)
3138 {
3139 rig_debug(RIG_DEBUG_ERR, "%s: "
3140 "unexpected att level %d\n",
3141 __func__, lvl);
3142 RETURNFUNC(-RIG_EPROTO);
3143 }
3144 }
3145
3146 if (i != lvl)
3147 {
3148 RETURNFUNC(-RIG_EINTERNAL);
3149 }
3150
3151 val->i = rig->state.attenuator[i - 1];
3152 }
3153
3154 break;
3155
3156 case RIG_LEVEL_PREAMP:
3157 retval = kenwood_safe_transaction(rig, "PA", lvlbuf, 50, 3);
3158
3159 if (retval != RIG_OK)
3160 {
3161 RETURNFUNC(retval);
3162 }
3163
3164 if (lvlbuf[2] == '0')
3165 {
3166 val->i = 0;
3167 }
3168 else if (isdigit((int)lvlbuf[2]))
3169 {
3170 lvl = lvlbuf[2] - '0';
3171
3172 for (i = 0; i < lvl && i < HAMLIB_MAXDBLSTSIZ; i++)
3173 {
3174 if (rig->state.preamp[i] == 0)
3175 {
3176 rig_debug(RIG_DEBUG_ERR, "%s: "
3177 "unexpected preamp level %d\n",
3178 __func__, lvl);
3179 RETURNFUNC(-RIG_EPROTO);
3180 }
3181 }
3182
3183 if (i != lvl)
3184 {
3185 RETURNFUNC(-RIG_EINTERNAL);
3186 }
3187
3188 val->i = rig->state.preamp[i - 1];
3189 }
3190 else
3191 {
3192 rig_debug(RIG_DEBUG_ERR, "%s: "
3193 "unexpected preamp char '%c'\n",
3194 __func__, lvlbuf[2]);
3195 RETURNFUNC(-RIG_EPROTO);
3196 }
3197
3198 break;
3199
3200 case RIG_LEVEL_RFPOWER:
3201 // Power min/max can vary so we query to find them out every time
3202 retval = kenwood_get_power_minmax(rig, &power_now, &power_min, &power_max, 1);
3203
3204 if (retval != RIG_OK) { RETURNFUNC(retval); }
3205
3206 power_min = 0; // our return scale is 0-max to match the input scale
3207 val->f = (power_now - power_min) / (float)(power_max - power_min);
3208 RETURNFUNC(RIG_OK);
3209
3210 case RIG_LEVEL_AF:
3211
3212 // first time through we'll determine the AG format
3213 // Can be "AG" "AG0" or "AG0/1"
3214 // This could be done by rig but easy enough to make it automagic
3215 if (priv->ag_format < 0)
3216 {
3217 int retry_save = rig->state.rigport.retry;
3218 rig->state.rigport.retry = 0; // speed up this check so no retries
3219 rig_debug(RIG_DEBUG_TRACE, "%s: AF format check determination...\n", __func__);
3220 // Determine AG format
3221 // =-1 == Undetermine
3222 // 0 == Unknown
3223 // 1 == AG
3224 // 2 == AG0 (fixed VFO)
3225 // 3 == AG0/1 (with VFO arg)
3226 char buffer[KENWOOD_MAX_BUF_LEN];
3227 ret = kenwood_transaction(rig, "AG", buffer, sizeof(buffer));
3228
3229 if (ret == RIG_OK)
3230 {
3231 priv->ag_format = 1;
3232 }
3233 else
3234 {
3235 ret = kenwood_transaction(rig, "AG1", buffer, sizeof(buffer));
3236
3237 if (ret == RIG_OK)
3238 {
3239 priv->ag_format = 3;
3240 }
3241 else
3242 {
3243 ret = kenwood_transaction(rig, "AG0", buffer, sizeof(buffer));
3244
3245 if (ret == RIG_OK)
3246 {
3247 priv->ag_format = 2;
3248 }
3249 else
3250 {
3251 priv->ag_format = 0; // rats....can't figure it out
3252 }
3253 }
3254 }
3255
3256 rig->state.rigport.retry = retry_save;
3257 }
3258
3259 rig_debug(RIG_DEBUG_TRACE, "%s: ag_format=%d\n", __func__, priv->ag_format);
3260
3261 if (priv->ag_format == 0)
3262 {
3263 priv->ag_format = -1; // we'll keep trying next time
3264 rig_debug(RIG_DEBUG_WARN, "%s: Unable to set AG format?\n", __func__);
3265 RETURNFUNC(RIG_OK); // this is non-fatal for no))w
3266 }
3267
3268 switch (priv->ag_format)
3269 {
3270 case 0:
3271 priv->ag_format = -1; // reset to try again
3272 RETURNFUNC(RIG_OK);
3273 break;
3274
3275 case 1:
3276 RETURNFUNC(get_kenwood_level(rig, "AG", &val->f, NULL));
3277 break;
3278
3279 case 2:
3280 RETURNFUNC(get_kenwood_level(rig, "AG0", &val->f, NULL));
3281 break;
3282
3283 case 3:
3284 RETURNFUNC(get_kenwood_level(rig, vfo == RIG_VFO_MAIN ? "AG0" : "AG1", &val->f,
3285 NULL));
3286 break;
3287
3288 default:
3289 rig_debug(RIG_DEBUG_WARN, "%s: Invalid ag_format=%d?\n", __func__,
3290 priv->ag_format);
3291 RETURNFUNC(-RIG_EPROTO);
3292 }
3293
3294 case RIG_LEVEL_RF:
3295 RETURNFUNC(get_kenwood_level(rig, "RG", &val->f, NULL));
3296
3297
3298
3299 case RIG_LEVEL_MICGAIN:
3300 {
3301 int micgain_now;
3302
3303 if (priv->micgain_min == -1) // then we need to know our min/max
3304 {
3305 retval = kenwood_get_micgain_minmax(rig, &micgain_now, &priv->micgain_min,
3306 &priv->micgain_max, 1);
3307
3308 if (retval != RIG_OK) { RETURNFUNC(retval); }
3309 }
3310
3311 rig_debug(RIG_DEBUG_TRACE, "%s: micgain_min=%d, micgain_max=%d\n", __func__,
3312 priv->micgain_min, priv->micgain_max);
3313
3314 ret = get_kenwood_level(rig, "MG", NULL, &val->i);
3315
3316 if (ret != RIG_OK)
3317 {
3318 rig_debug(RIG_DEBUG_ERR, "%s: Error getting MICGAIN\n", __func__);
3319 RETURNFUNC(ret);
3320 }
3321
3322 val->f = (val->i - priv->micgain_min) / (float)(priv->micgain_max -
3323 priv->micgain_min);
3324 RETURNFUNC(RIG_OK);
3325 }
3326
3327 case RIG_LEVEL_AGC:
3328 ret = get_kenwood_level(rig, "GT", NULL, &val->i);
3329 agclevel = val->i;
3330
3331 if (agclevel == 0) { val->i = 0; }
3332 else if (agclevel < 85) { val->i = 1; }
3333 else if (agclevel < 170) { val->i = 2; }
3334 else if (agclevel <= 255) { val->i = 3; }
3335
3336 RETURNFUNC(ret);
3337
3338 case RIG_LEVEL_SLOPE_LOW:
3339 retval = kenwood_transaction(rig, "SL", lvlbuf, sizeof(lvlbuf));
3340
3341 if (retval != RIG_OK)
3342 {
3343 RETURNFUNC(retval);
3344 }
3345
3346 value = atoi(&lvlbuf[2]);
3347
3348 retval = kenwood_find_slope_filter_for_value(rig, vfo, caps->slope_filter_low,
3349 value, &val->i);
3350
3351 if (retval != RIG_OK)
3352 {
3353 if (retval == -RIG_ENAVAIL)
3354 {
3355 // Fall back to using raw values
3356 val->i = value;
3357 }
3358 else
3359 {
3360 RETURNFUNC(retval);
3361 }
3362 }
3363
3364 break;
3365
3366 case RIG_LEVEL_SLOPE_HIGH:
3367 retval = kenwood_transaction(rig, "SH", lvlbuf, sizeof(lvlbuf));
3368
3369 if (retval != RIG_OK)
3370 {
3371 RETURNFUNC(retval);
3372 }
3373
3374 value = atoi(&lvlbuf[2]);
3375
3376 retval = kenwood_find_slope_filter_for_value(rig, vfo, caps->slope_filter_high,
3377 value, &val->i);
3378
3379 if (retval != RIG_OK)
3380 {
3381 if (retval == -RIG_ENAVAIL)
3382 {
3383 // Fall back to using raw values
3384 val->i = value;
3385 }
3386 else
3387 {
3388 RETURNFUNC(retval);
3389 }
3390 }
3391
3392 break;
3393
3394 case RIG_LEVEL_CWPITCH:
3395 retval = kenwood_safe_transaction(rig, "PT", lvlbuf, 50, 4);
3396
3397 if (retval != RIG_OK)
3398 {
3399 RETURNFUNC(retval);
3400 }
3401
3402 sscanf(lvlbuf + 2, "%d", &val->i);
3403 val->i = (val->i * 1000) + 1000; /* 00 - 08 */
3404 break;
3405
3406 case RIG_LEVEL_KEYSPD:
3407 retval = kenwood_safe_transaction(rig, "KS", lvlbuf, 50, 5);
3408
3409 if (retval != RIG_OK)
3410 {
3411 RETURNFUNC(retval);
3412 }
3413
3414 sscanf(lvlbuf + 2, "%d", &val->i);
3415 break;
3416
3417 case RIG_LEVEL_COMP:
3418 {
3419 int raw_value;
3420 retval = kenwood_safe_transaction(rig, "PL", lvlbuf, 50, 8);
3421
3422 if (retval != RIG_OK)
3423 {
3424 RETURNFUNC(retval);
3425 }
3426
3427 sscanf(lvlbuf + 2, "%3d", &raw_value);
3428
3429 if (RIG_IS_TS990S)
3430 {
3431 val->f = (float) raw_value / 255.0f;
3432 }
3433 else
3434 {
3435 val->f = (float) raw_value / 100.0f;
3436 }
3437
3438 break;
3439 }
3440
3441 case RIG_LEVEL_VOXDELAY:
3442 {
3443 int raw_value;
3444 retval = kenwood_safe_transaction(rig, "VD", lvlbuf, 50, 6);
3445
3446 if (retval != RIG_OK)
3447 {
3448 RETURNFUNC(retval);
3449 }
3450
3451 sscanf(lvlbuf + 2, "%d", &raw_value);
3452
3453 // Value is in milliseconds
3454 val->i = raw_value / 100;
3455 break;
3456 }
3457
3458 case RIG_LEVEL_VOXGAIN:
3459 {
3460 int raw_value;
3461 retval = kenwood_safe_transaction(rig, "VG", lvlbuf, 50, 5);
3462
3463 if (retval != RIG_OK)
3464 {
3465 RETURNFUNC(retval);
3466 }
3467
3468 sscanf(lvlbuf + 2, "%d", &raw_value);
3469
3470 val->f = (float) raw_value / 9.0f;
3471 break;
3472 }
3473
3474 case RIG_LEVEL_BKIN_DLYMS:
3475 {
3476 int raw_value;
3477 retval = kenwood_safe_transaction(rig, "SD", lvlbuf, 50, 6);
3478
3479 if (retval != RIG_OK)
3480 {
3481 RETURNFUNC(retval);
3482 }
3483
3484 sscanf(lvlbuf + 2, "%d", &raw_value);
3485
3486 val->i = raw_value;
3487 break;
3488 }
3489
3490 case RIG_LEVEL_IF:
3491 case RIG_LEVEL_APF:
3492 case RIG_LEVEL_NR:
3493 case RIG_LEVEL_PBT_IN:
3494 case RIG_LEVEL_PBT_OUT:
3495 case RIG_LEVEL_NOTCHF:
3496 case RIG_LEVEL_BKINDL:
3497 case RIG_LEVEL_BALANCE:
3498 RETURNFUNC(-RIG_ENIMPL);
3499
3500 default:
3501 rig_debug(RIG_DEBUG_ERR, "%s: unsupported get_level %s", __func__,
3502 rig_strlevel(level));
3503 RETURNFUNC(-RIG_EINVAL);
3504 }
3505
3506 RETURNFUNC(RIG_OK);
3507 }
3508
kenwood_set_func(RIG * rig,vfo_t vfo,setting_t func,int status)3509 int kenwood_set_func(RIG *rig, vfo_t vfo, setting_t func, int status)
3510 {
3511 char buf[10]; /* longest cmd is GTxxx */
3512 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3513
3514 switch (func)
3515 {
3516 case RIG_FUNC_NB:
3517 case RIG_FUNC_NB2:
3518
3519 /* newer Kenwoods have a second noise blanker */
3520 if (RIG_IS_TS890S)
3521 {
3522 switch (func)
3523 {
3524 case RIG_FUNC_NB:
3525 snprintf(buf, sizeof(buf), "NB1%c", (status == 0) ? '0' : '1');
3526 break;
3527
3528 case RIG_FUNC_NB2:
3529 snprintf(buf, sizeof(buf), "NB2%c", (status == 0) ? '0' : '1');
3530 break;
3531
3532 default:
3533 rig_debug(RIG_DEBUG_ERR, "%s: expected 0,1, or 2 and got %d\n", __func__,
3534 status);
3535 RETURNFUNC(-RIG_EINVAL);
3536 }
3537 }
3538 else
3539 {
3540 snprintf(buf, sizeof(buf), "NB%c", (status == 0) ? '0' : '1');
3541 }
3542
3543 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3544
3545 case RIG_FUNC_ABM:
3546 snprintf(buf, sizeof(buf), "AM%c", (status == 0) ? '0' : '1');
3547 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3548
3549 case RIG_FUNC_COMP:
3550 if (RIG_IS_TS890S)
3551 {
3552 snprintf(buf, sizeof(buf), "PR0%c", (status == 0) ? '0' : '1');
3553 }
3554 else
3555 {
3556 snprintf(buf, sizeof(buf), "PR%c", (status == 0) ? '0' : '1');
3557 }
3558
3559 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3560
3561 case RIG_FUNC_TONE:
3562 snprintf(buf, sizeof(buf), "TO%c", (status == 0) ? '0' : '1');
3563 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3564
3565 case RIG_FUNC_TSQL:
3566 snprintf(buf, sizeof(buf), "CT%c", (status == 0) ? '0' : '1');
3567 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3568
3569 case RIG_FUNC_VOX:
3570 snprintf(buf, sizeof(buf), "VX%c", (status == 0) ? '0' : '1');
3571 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3572
3573 case RIG_FUNC_FAGC:
3574 snprintf(buf, sizeof(buf), "GT00%c", (status == 0) ? '4' : '2');
3575 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3576
3577 case RIG_FUNC_NR:
3578 if (RIG_IS_TS890S)
3579 {
3580 char c = '1';
3581
3582 if (status == 2) { c = '2'; }
3583
3584 snprintf(buf, sizeof(buf), "NR%c", (status == 0) ? '0' : c);
3585 }
3586 else
3587 {
3588 snprintf(buf, sizeof(buf), "NR%c", (status == 0) ? '0' : '1');
3589 }
3590
3591 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3592
3593 case RIG_FUNC_BC:
3594 snprintf(buf, sizeof(buf), "BC%c", (status == 0) ? '0' : '1');
3595 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3596
3597 case RIG_FUNC_BC2:
3598 snprintf(buf, sizeof(buf), "BC%c", (status == 0) ? '0' : '2');
3599 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3600
3601 case RIG_FUNC_ANF:
3602 snprintf(buf, sizeof(buf), "NT%c", (status == 0) ? '0' : '1');
3603 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3604
3605 case RIG_FUNC_LOCK:
3606 snprintf(buf, sizeof(buf), "LK%c", (status == 0) ? '0' : '1');
3607 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3608
3609 case RIG_FUNC_AIP:
3610 snprintf(buf, sizeof(buf), "MX%c", (status == 0) ? '0' : '1');
3611 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3612
3613 case RIG_FUNC_RIT:
3614 snprintf(buf, sizeof(buf), "RT%c", (status == 0) ? '0' : '1');
3615 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3616
3617 case RIG_FUNC_XIT:
3618 snprintf(buf, sizeof(buf), "XT%c", (status == 0) ? '0' : '1');
3619 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3620
3621 case RIG_FUNC_TUNER:
3622 snprintf(buf, sizeof(buf), "AC1%c0", (status == 0) ? '0' : '1');
3623 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3624
3625 case RIG_FUNC_FBKIN:
3626 snprintf(buf, sizeof(buf), "SD%04d", (status == 1) ? 0 : 50);
3627 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3628
3629 default:
3630 rig_debug(RIG_DEBUG_ERR, "Unsupported set_func %s", rig_strfunc(func));
3631 RETURNFUNC(-RIG_EINVAL);
3632 }
3633
3634 RETURNFUNC(-RIG_EINVAL);
3635 }
3636
3637 /*
3638 * works for any 'format 1' command or newer command like the TS890 has
3639 * as long as the return is a number 0-9
3640 * answer is always 4 bytes or 5 bytes: two or three byte command id, status and terminator
3641 */
get_kenwood_func(RIG * rig,const char * cmd,int * status)3642 int get_kenwood_func(RIG *rig, const char *cmd, int *status)
3643 {
3644 int retval;
3645 char buf[10];
3646 int offset = 2;
3647
3648 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3649
3650 if (!cmd || !status)
3651 {
3652 RETURNFUNC(-RIG_EINVAL);
3653 }
3654
3655 if (strlen(cmd) == 3) { offset = 3; } // some commands are 3 letters
3656
3657 retval = kenwood_safe_transaction(rig, cmd, buf, sizeof(buf), offset + 1);
3658
3659 if (retval != RIG_OK)
3660 {
3661 RETURNFUNC(retval);
3662 }
3663
3664 *status = buf[offset] - '0'; // just return whatever the rig returns
3665
3666 RETURNFUNC(RIG_OK);
3667 };
3668
3669 /*
3670 * kenwood_get_func
3671 */
kenwood_get_func(RIG * rig,vfo_t vfo,setting_t func,int * status)3672 int kenwood_get_func(RIG *rig, vfo_t vfo, setting_t func, int *status)
3673 {
3674 char *cmd;
3675 char respbuf[20];
3676 int retval;
3677 int raw_value;
3678
3679 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3680
3681 if (!status)
3682 {
3683 RETURNFUNC(-RIG_EINVAL);
3684 }
3685
3686 switch (func)
3687 {
3688 case RIG_FUNC_FAGC:
3689 retval = kenwood_safe_transaction(rig, "GT", respbuf, 20, 5);
3690
3691 if (retval != RIG_OK)
3692 {
3693 RETURNFUNC(retval);
3694 }
3695
3696 *status = respbuf[4] != '4' ? 1 : 0;
3697 RETURNFUNC(RIG_OK);
3698
3699 case RIG_FUNC_NB:
3700 cmd = "NB";
3701
3702 if (RIG_IS_TS890S)
3703 {
3704 cmd = "NB1";
3705 }
3706
3707 RETURNFUNC(get_kenwood_func(rig, cmd, status));
3708
3709 case RIG_FUNC_NB2:
3710 RETURNFUNC(get_kenwood_func(rig, "NB2", status));
3711
3712 case RIG_FUNC_ABM:
3713 RETURNFUNC(get_kenwood_func(rig, "AM", status));
3714
3715 case RIG_FUNC_COMP:
3716 RETURNFUNC(get_kenwood_func(rig, "PR", status));
3717
3718 case RIG_FUNC_TONE:
3719 RETURNFUNC(get_kenwood_func(rig, "TO", status));
3720
3721 case RIG_FUNC_TSQL:
3722 RETURNFUNC(get_kenwood_func(rig, "CT", status));
3723
3724 case RIG_FUNC_VOX:
3725 RETURNFUNC(get_kenwood_func(rig, "VX", status));
3726
3727 case RIG_FUNC_NR:
3728 RETURNFUNC(get_kenwood_func(rig, "NR", status));
3729
3730 /* FIXME on TS2000 */
3731 // Check for BC #1
3732 case RIG_FUNC_BC: // Most will return BC1 or BC0, if BC2 then BC1 is off
3733 retval = get_kenwood_func(rig, "BC", &raw_value);
3734
3735 if (retval == RIG_OK)
3736 {
3737 *status = raw_value == 1 ? 1 : 0;
3738 }
3739
3740 RETURNFUNC(retval);
3741
3742 case RIG_FUNC_BC2: // TS-890 check Beat Cancel 2 we return boolean true/false
3743 retval = get_kenwood_func(rig, "BC", &raw_value);
3744
3745 if (retval == RIG_OK)
3746 {
3747 *status = raw_value == 2 ? 1 : 0;
3748 }
3749
3750 RETURNFUNC(retval);
3751
3752 case RIG_FUNC_ANF:
3753 RETURNFUNC(get_kenwood_func(rig, "NT", status));
3754
3755 case RIG_FUNC_LOCK:
3756 RETURNFUNC(get_kenwood_func(rig, "LK", status));
3757
3758 case RIG_FUNC_AIP:
3759 RETURNFUNC(get_kenwood_func(rig, "MX", status));
3760
3761 case RIG_FUNC_RIT:
3762 RETURNFUNC(get_kenwood_func(rig, "RT", status));
3763
3764 case RIG_FUNC_XIT:
3765 RETURNFUNC(get_kenwood_func(rig, "XT", status));
3766
3767 case RIG_FUNC_TUNER:
3768 retval = kenwood_safe_transaction(rig, "AC", respbuf, 20, 5);
3769
3770 if (retval != RIG_OK)
3771 {
3772 RETURNFUNC(retval);
3773 }
3774
3775 *status = respbuf[3] != '0' ? 1 : 0;
3776 RETURNFUNC(RIG_OK);
3777
3778 case RIG_FUNC_FBKIN:
3779 {
3780 int raw_value;
3781
3782 retval = kenwood_safe_transaction(rig, "SD", respbuf, 20, 6);
3783
3784 if (retval != RIG_OK)
3785 {
3786 RETURNFUNC(retval);
3787 }
3788
3789 sscanf(respbuf + 2, "%d", &raw_value);
3790 *status = (raw_value == 0) ? 1 : 0;
3791 RETURNFUNC(RIG_OK);
3792 }
3793
3794 default:
3795 rig_debug(RIG_DEBUG_ERR, "Unsupported get_func %s", rig_strfunc(func));
3796 RETURNFUNC(-RIG_EINVAL);
3797 }
3798
3799 RETURNFUNC(-RIG_EINVAL);
3800 }
3801
3802 /*
3803 * kenwood_set_ctcss_tone
3804 * Assumes rig->caps->ctcss_list != NULL
3805 *
3806 * Warning! This is untested stuff! May work at least on TS-870S
3807 * Please owners report to me <fillods@users.sourceforge.net>, thanks. --SF
3808 */
kenwood_set_ctcss_tone(RIG * rig,vfo_t vfo,tone_t tone)3809 int kenwood_set_ctcss_tone(RIG *rig, vfo_t vfo, tone_t tone)
3810 {
3811 const struct rig_caps *caps;
3812 char tonebuf[16];
3813 int i;
3814
3815 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3816
3817 caps = rig->caps;
3818
3819 for (i = 0; caps->ctcss_list[i] != 0; i++)
3820 {
3821 if (caps->ctcss_list[i] == tone)
3822 {
3823 break;
3824 }
3825 }
3826
3827 if (caps->ctcss_list[i] != tone)
3828 {
3829 RETURNFUNC(-RIG_EINVAL);
3830 }
3831
3832 /* TODO: replace menu no 57 by a define */
3833 snprintf(tonebuf, sizeof(tonebuf), "EX%03d%04d", 57, i + 1);
3834
3835 RETURNFUNC(kenwood_transaction(rig, tonebuf, NULL, 0));
3836 }
3837
kenwood_set_ctcss_tone_tn(RIG * rig,vfo_t vfo,tone_t tone)3838 int kenwood_set_ctcss_tone_tn(RIG *rig, vfo_t vfo, tone_t tone)
3839 {
3840 const struct rig_caps *caps = rig->caps;
3841 char buf[16];
3842 int i;
3843
3844 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3845
3846 for (i = 0; caps->ctcss_list[i] != 0; i++)
3847 {
3848 if (tone == caps->ctcss_list[i])
3849 {
3850 break;
3851 }
3852 }
3853
3854 if (tone != caps->ctcss_list[i])
3855 {
3856 RETURNFUNC(-RIG_EINVAL);
3857 }
3858
3859 if (RIG_IS_TS990S)
3860 {
3861 char c;
3862
3863 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
3864 {
3865 int err;
3866
3867 if (RIG_OK != (err = kenwood_get_vfo_main_sub(rig, &vfo)))
3868 {
3869 RETURNFUNC(err);
3870 }
3871 }
3872
3873 switch (vfo)
3874 {
3875 case RIG_VFO_MAIN: c = '0'; break;
3876
3877 case RIG_VFO_SUB: c = '1'; break;
3878
3879 default:
3880 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
3881 RETURNFUNC(-RIG_EINVAL);
3882 }
3883
3884 snprintf(buf, sizeof(buf), "TN%c%02d", c, i + 1);
3885 }
3886 else
3887 {
3888 snprintf(buf, sizeof(buf), "TN%02d", i + 1);
3889 }
3890
3891 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
3892 }
3893
3894 /*
3895 * kenwood_get_ctcss_tone
3896 * Assumes rig->state.priv != NULL
3897 */
kenwood_get_ctcss_tone(RIG * rig,vfo_t vfo,tone_t * tone)3898 int kenwood_get_ctcss_tone(RIG *rig, vfo_t vfo, tone_t *tone)
3899 {
3900 struct kenwood_priv_data *priv = rig->state.priv;
3901 const struct rig_caps *caps;
3902 char tonebuf[3];
3903 int i, retval;
3904 unsigned int tone_idx;
3905
3906 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3907
3908 if (!tone)
3909 {
3910 RETURNFUNC(-RIG_EINVAL);
3911 }
3912
3913 caps = rig->caps;
3914
3915 if (RIG_IS_TS990S)
3916 {
3917 char cmd[4];
3918 char buf[6];
3919 char c;
3920
3921 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
3922 {
3923 if (RIG_OK != (retval = kenwood_get_vfo_main_sub(rig, &vfo)))
3924 {
3925 RETURNFUNC(retval);
3926 }
3927 }
3928
3929 switch (vfo)
3930 {
3931 case RIG_VFO_MAIN: c = '0'; break;
3932
3933 case RIG_VFO_SUB: c = '1'; break;
3934
3935 default:
3936 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
3937 RETURNFUNC(-RIG_EINVAL);
3938 }
3939
3940 snprintf(cmd, sizeof(cmd), "TN%c", c);
3941 retval = kenwood_safe_transaction(rig, cmd, buf, sizeof(buf), 5);
3942 memcpy(tonebuf, &buf[3], 2);
3943 }
3944 else
3945 {
3946 retval = kenwood_get_if(rig);
3947 memcpy(tonebuf, &priv->info[34], 2);
3948 }
3949
3950 if (retval != RIG_OK)
3951 {
3952 RETURNFUNC(retval);
3953 }
3954
3955 tonebuf[2] = '\0';
3956 tone_idx = atoi(tonebuf);
3957
3958 if (tone_idx == 0)
3959 {
3960 rig_debug(RIG_DEBUG_ERR, "%s: CTCSS tone is zero (%s)\n",
3961 __func__, tonebuf);
3962 RETURNFUNC(-RIG_EPROTO);
3963 }
3964
3965 /* check this tone exists. That's better than nothing. */
3966 for (i = 0; i < tone_idx; i++)
3967 {
3968 if (caps->ctcss_list[i] == 0)
3969 {
3970 rig_debug(RIG_DEBUG_ERR, "%s: CTCSS NG (%04u)\n",
3971 __func__, tone_idx);
3972 RETURNFUNC(-RIG_EPROTO);
3973 }
3974 }
3975
3976 *tone = caps->ctcss_list[tone_idx - 1];
3977
3978 RETURNFUNC(RIG_OK);
3979 }
3980
kenwood_set_ctcss_sql(RIG * rig,vfo_t vfo,tone_t tone)3981 int kenwood_set_ctcss_sql(RIG *rig, vfo_t vfo, tone_t tone)
3982 {
3983 const struct rig_caps *caps = rig->caps;
3984 char buf[16];
3985 int i;
3986
3987 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
3988
3989 for (i = 0; caps->ctcss_list[i] != 0; i++)
3990 {
3991 if (tone == caps->ctcss_list[i])
3992 {
3993 break;
3994 }
3995 }
3996
3997 if (tone != caps->ctcss_list[i])
3998 {
3999 RETURNFUNC(-RIG_EINVAL);
4000 }
4001
4002 if (RIG_IS_TS990S)
4003 {
4004 char c;
4005
4006 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
4007 {
4008 int err;
4009
4010 if (RIG_OK != (err = kenwood_get_vfo_main_sub(rig, &vfo)))
4011 {
4012 RETURNFUNC(err);
4013 }
4014 }
4015
4016 switch (vfo)
4017 {
4018 case RIG_VFO_MAIN: c = '0'; break;
4019
4020 case RIG_VFO_SUB: c = '1'; break;
4021
4022 default:
4023 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
4024 RETURNFUNC(-RIG_EINVAL);
4025 }
4026
4027 snprintf(buf, sizeof(buf), "CN%c%02d", c, i + 1);
4028 }
4029 else
4030 {
4031 snprintf(buf, sizeof(buf), "CN%02d", i + 1);
4032 }
4033
4034 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
4035 }
4036
kenwood_get_ctcss_sql(RIG * rig,vfo_t vfo,tone_t * tone)4037 int kenwood_get_ctcss_sql(RIG *rig, vfo_t vfo, tone_t *tone)
4038 {
4039 const struct rig_caps *caps;
4040 char cmd[4];
4041 char tonebuf[6];
4042 int offs;
4043 int i, retval;
4044 unsigned int tone_idx;
4045
4046 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4047
4048 if (!tone)
4049 {
4050 RETURNFUNC(-RIG_EINVAL);
4051 }
4052
4053 caps = rig->caps;
4054
4055 if (RIG_IS_TS990S)
4056 {
4057 char c;
4058
4059 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
4060 {
4061 if (RIG_OK != (retval = kenwood_get_vfo_main_sub(rig, &vfo)))
4062 {
4063 RETURNFUNC(retval);
4064 }
4065 }
4066
4067 switch (vfo)
4068 {
4069 case RIG_VFO_MAIN: c = '0'; break;
4070
4071 case RIG_VFO_SUB: c = '1'; break;
4072
4073 default:
4074 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
4075 RETURNFUNC(-RIG_EINVAL);
4076 }
4077
4078 snprintf(cmd, sizeof(cmd), "CN%c", c);
4079 offs = 3;
4080 }
4081 else
4082 {
4083 snprintf(cmd, sizeof(cmd), "CT");
4084 offs = 2;
4085 }
4086
4087 retval = kenwood_safe_transaction(rig, cmd, tonebuf, 6, offs + 2);
4088
4089 if (retval != RIG_OK)
4090 {
4091 RETURNFUNC(retval);
4092 }
4093
4094 tone_idx = atoi(tonebuf + offs);
4095
4096 if (tone_idx == 0)
4097 {
4098 rig_debug(RIG_DEBUG_ERR, "%s: CTCSS is zero (%s)\n",
4099 __func__, tonebuf);
4100 RETURNFUNC(-RIG_EPROTO);
4101 }
4102
4103 /* check this tone exists. That's better than nothing. */
4104 for (i = 0; i < tone_idx; i++)
4105 {
4106 if (caps->ctcss_list[i] == 0)
4107 {
4108 rig_debug(RIG_DEBUG_ERR, "%s: CTCSS NG (%04u)\n",
4109 __func__, tone_idx);
4110 RETURNFUNC(-RIG_EPROTO);
4111 }
4112 }
4113
4114 *tone = caps->ctcss_list[tone_idx - 1];
4115
4116 RETURNFUNC(RIG_OK);
4117 }
4118
4119
4120 /*
4121 * set the aerial/antenna to use
4122 */
kenwood_set_ant(RIG * rig,vfo_t vfo,ant_t ant,value_t option)4123 int kenwood_set_ant(RIG *rig, vfo_t vfo, ant_t ant, value_t option)
4124 {
4125 char cmd[8];
4126 char a;
4127
4128 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4129
4130 switch (ant)
4131 {
4132 case RIG_ANT_1: a = '1'; break;
4133
4134 case RIG_ANT_2: a = '2'; break;
4135
4136 case RIG_ANT_3: a = '3'; break;
4137
4138 case RIG_ANT_4: a = '4'; break;
4139
4140 default:
4141 RETURNFUNC(-RIG_EINVAL);
4142 }
4143
4144 if (RIG_IS_TS990S)
4145 {
4146 char c;
4147
4148 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
4149 {
4150 int err;
4151
4152 if (RIG_OK != (err = kenwood_get_vfo_main_sub(rig, &vfo)))
4153 {
4154 RETURNFUNC(err);
4155 }
4156 }
4157
4158 switch (vfo)
4159 {
4160 case RIG_VFO_MAIN: c = '0'; break;
4161
4162 case RIG_VFO_SUB: c = '1'; break;
4163
4164 default:
4165 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
4166 RETURNFUNC(-RIG_EINVAL);
4167 }
4168
4169 snprintf(cmd, sizeof(cmd), "AN0%c%c99", c, a);
4170 }
4171 else
4172 {
4173 snprintf(cmd, sizeof(cmd), "AN%c", a);
4174 }
4175
4176 RETURNFUNC(kenwood_transaction(rig, cmd, NULL, 0));
4177 }
4178
kenwood_set_ant_no_ack(RIG * rig,vfo_t vfo,ant_t ant,value_t option)4179 int kenwood_set_ant_no_ack(RIG *rig, vfo_t vfo, ant_t ant, value_t option)
4180 {
4181 const char *cmd;
4182
4183 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4184
4185 switch (ant)
4186 {
4187 case RIG_ANT_1:
4188 cmd = "AN1";
4189 break;
4190
4191 case RIG_ANT_2:
4192 cmd = "AN2";
4193 break;
4194
4195 case RIG_ANT_3:
4196 cmd = "AN3";
4197 break;
4198
4199 case RIG_ANT_4:
4200 cmd = "AN4";
4201 break;
4202
4203 default:
4204 RETURNFUNC(-RIG_EINVAL);
4205 }
4206
4207 RETURNFUNC(kenwood_transaction(rig, cmd, NULL, 0));
4208 }
4209
4210 /*
4211 * get the aerial/antenna in use
4212 */
kenwood_get_ant(RIG * rig,vfo_t vfo,ant_t dummy,value_t * option,ant_t * ant_curr,ant_t * ant_tx,ant_t * ant_rx)4213 int kenwood_get_ant(RIG *rig, vfo_t vfo, ant_t dummy, value_t *option,
4214 ant_t *ant_curr, ant_t *ant_tx, ant_t *ant_rx)
4215 {
4216 char ackbuf[8];
4217 int offs;
4218 int retval;
4219
4220 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4221
4222 if (!ant_curr)
4223 {
4224 RETURNFUNC(-RIG_EINVAL);
4225 }
4226
4227 if (RIG_IS_TS990S)
4228 {
4229 retval = kenwood_safe_transaction(rig, "AN0", ackbuf, sizeof(ackbuf), 7);
4230 offs = 4;
4231 }
4232 else
4233 {
4234 retval = kenwood_safe_transaction(rig, "AN", ackbuf, sizeof(ackbuf), 3);
4235 offs = 2;
4236 }
4237
4238 if (retval != RIG_OK)
4239 {
4240 RETURNFUNC(retval);
4241 }
4242
4243 if (ackbuf[offs] < '1' || ackbuf[offs] > '9')
4244 {
4245 RETURNFUNC(-RIG_EPROTO);
4246 }
4247
4248 *ant_curr = RIG_ANT_N(ackbuf[offs] - '1');
4249
4250 /* XXX check that the returned antenna is valid for the current rig */
4251
4252 RETURNFUNC(RIG_OK);
4253 }
4254
4255 /*
4256 * kenwood_get_ptt
4257 */
kenwood_get_ptt(RIG * rig,vfo_t vfo,ptt_t * ptt)4258 int kenwood_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
4259 {
4260 struct kenwood_priv_data *priv = rig->state.priv;
4261 int retval;
4262
4263 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4264
4265 if (!ptt)
4266 {
4267 RETURNFUNC(-RIG_EINVAL);
4268 }
4269
4270 retval = kenwood_get_if(rig);
4271
4272 if (retval != RIG_OK)
4273 {
4274 RETURNFUNC(retval);
4275 }
4276
4277 *ptt = priv->info[28] == '0' ? RIG_PTT_OFF : RIG_PTT_ON;
4278
4279 RETURNFUNC(RIG_OK);
4280 }
4281
kenwood_set_ptt(RIG * rig,vfo_t vfo,ptt_t ptt)4282 int kenwood_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
4283 {
4284 const char *ptt_cmd;
4285
4286 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4287
4288 switch (ptt)
4289 {
4290 case RIG_PTT_ON: ptt_cmd = "TX"; break;
4291
4292 case RIG_PTT_ON_MIC: ptt_cmd = "TX0"; break;
4293
4294 case RIG_PTT_ON_DATA: ptt_cmd = "TX1"; break;
4295
4296 case RIG_PTT_OFF: ptt_cmd = "RX"; break;
4297
4298 default: RETURNFUNC(-RIG_EINVAL);
4299 }
4300
4301 int retval = kenwood_transaction(rig, ptt_cmd, NULL, 0);
4302
4303 if (ptt == RIG_PTT_OFF) { hl_usleep(100 * 1000); } // a little time for PTT to turn off
4304
4305 RETURNFUNC(retval);
4306 }
4307
kenwood_set_ptt_safe(RIG * rig,vfo_t vfo,ptt_t ptt)4308 int kenwood_set_ptt_safe(RIG *rig, vfo_t vfo, ptt_t ptt)
4309 {
4310 int err;
4311 ptt_t current_ptt;
4312
4313 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4314
4315 err = kenwood_get_ptt(rig, vfo, ¤t_ptt);
4316
4317 if (err != RIG_OK)
4318 {
4319 RETURNFUNC(err);
4320 }
4321
4322 if (current_ptt == ptt)
4323 {
4324 RETURNFUNC(RIG_OK);
4325 }
4326
4327 RETURNFUNC(kenwood_transaction(rig,
4328 (ptt == RIG_PTT_ON) ? "TX" : "RX", NULL, 0));
4329 }
4330
4331
4332 /*
4333 * kenwood_get_dcd
4334 */
kenwood_get_dcd(RIG * rig,vfo_t vfo,dcd_t * dcd)4335 int kenwood_get_dcd(RIG *rig, vfo_t vfo, dcd_t *dcd)
4336 {
4337 char busybuf[10];
4338 int retval;
4339 int expected;
4340 int offs;
4341
4342 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4343
4344 if (!dcd)
4345 {
4346 RETURNFUNC(-RIG_EINVAL);
4347 }
4348
4349 if (RIG_IS_TS480 || RIG_IS_TS590S || RIG_IS_TS590SG || RIG_IS_TS990S
4350 || RIG_IS_TS2000)
4351 {
4352 expected = 4;
4353 }
4354 else
4355 {
4356 expected = 3;
4357 }
4358
4359 retval = kenwood_safe_transaction(rig, "BY", busybuf, 10, expected);
4360
4361 if (retval != RIG_OK)
4362 {
4363 RETURNFUNC(retval);
4364 }
4365
4366 if ((RIG_IS_TS990S && RIG_VFO_SUB == vfo) || (RIG_IS_TS2000
4367 && RIG_VFO_SUB == vfo))
4368 {
4369 offs = 3;
4370 }
4371 else
4372 {
4373 offs = 2;
4374 }
4375
4376 *dcd = (busybuf[offs] == '1') ? RIG_DCD_ON : RIG_DCD_OFF;
4377
4378 RETURNFUNC(RIG_OK);
4379 }
4380
4381 /*
4382 * kenwood_set_trn
4383 */
kenwood_set_trn(RIG * rig,int trn)4384 int kenwood_set_trn(RIG *rig, int trn)
4385 {
4386 char buf[5];
4387 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4388
4389 switch (rig->caps->rig_model)
4390 {
4391 case RIG_MODEL_POWERSDR: // powersdr doesn't have AI command
4392 RETURNFUNC(-RIG_ENAVAIL);
4393
4394 case RIG_MODEL_TS990S:
4395 RETURNFUNC(kenwood_transaction(rig, (trn == RIG_TRN_RIG) ? "AI2" : "AI0", NULL,
4396 0));
4397 break;
4398
4399 case RIG_MODEL_THD7A:
4400 case RIG_MODEL_THD74:
4401 RETURNFUNC(kenwood_transaction(rig, (trn == RIG_TRN_RIG) ? "AI 1" : "AI 0", buf,
4402 sizeof buf));
4403 break;
4404
4405 default:
4406 RETURNFUNC(kenwood_transaction(rig, (trn == RIG_TRN_RIG) ? "AI1" : "AI0", NULL,
4407 0));
4408 break;
4409 }
4410 }
4411
4412 /*
4413 * kenwood_get_trn
4414 */
kenwood_get_trn(RIG * rig,int * trn)4415 int kenwood_get_trn(RIG *rig, int *trn)
4416 {
4417 char trnbuf[6];
4418 int retval;
4419
4420 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4421
4422 if (!trn)
4423 {
4424 RETURNFUNC(-RIG_EINVAL);
4425 }
4426
4427 /* these rigs only have AI[0|1] set commands and no AI query */
4428 if (RIG_IS_TS450S || RIG_IS_TS690S || RIG_IS_TS790 || RIG_IS_TS850
4429 || RIG_IS_TS950S || RIG_IS_TS950SDX || RIG_IS_POWERSDR)
4430 {
4431 RETURNFUNC(-RIG_ENAVAIL);
4432 }
4433
4434 if (RIG_IS_THD74 || RIG_IS_THD7A)
4435 {
4436 retval = kenwood_safe_transaction(rig, "AI", trnbuf, 6, 4);
4437 }
4438 else
4439 {
4440 retval = kenwood_safe_transaction(rig, "AI", trnbuf, 6, 3);
4441 }
4442
4443 if (retval != RIG_OK)
4444 {
4445 RETURNFUNC(retval);
4446 }
4447
4448 if (RIG_IS_THD74 || RIG_IS_THD7A)
4449 {
4450 *trn = trnbuf[3] != '0' ? RIG_TRN_RIG : RIG_TRN_OFF;
4451 }
4452 else
4453 {
4454 *trn = trnbuf[2] != '0' ? RIG_TRN_RIG : RIG_TRN_OFF;
4455 }
4456
4457 RETURNFUNC(RIG_OK);
4458 }
4459
4460 /*
4461 * kenwood_set_powerstat
4462 */
kenwood_set_powerstat(RIG * rig,powerstat_t status)4463 int kenwood_set_powerstat(RIG *rig, powerstat_t status)
4464 {
4465 int retval = kenwood_transaction(rig,
4466 (status == RIG_POWER_ON) ? ";;;;PS1;" : "PS0",
4467 NULL, 0);
4468 int i = 0;
4469 int retry_save = rig->state.rigport.retry;
4470
4471 rig_debug(RIG_DEBUG_VERBOSE, "%s called status=%d\n", __func__, status);
4472
4473 rig->state.rigport.retry = 0;
4474
4475 if (status == RIG_POWER_ON) // wait for wakeup only
4476 {
4477 for (i = 0; i < 8; ++i) // up to ~10 seconds including the timeouts
4478 {
4479 freq_t freq;
4480 sleep(1);
4481 retval = rig_get_freq(rig, RIG_VFO_A, &freq);
4482
4483 if (retval == RIG_OK)
4484 {
4485 rig->state.rigport.retry = retry_save;
4486 RETURNFUNC(retval);
4487 }
4488
4489 rig_debug(RIG_DEBUG_TRACE, "%s: Wait #%d for power up\n", __func__, i + 1);
4490 }
4491 }
4492
4493 rig->state.rigport.retry = retry_save;
4494
4495 if (i == 9)
4496 {
4497 rig_debug(RIG_DEBUG_TRACE, "%s: timeout waiting for powerup, try %d\n",
4498 __func__,
4499 i + 1);
4500 retval = -RIG_ETIMEOUT;
4501 }
4502
4503 RETURNFUNC(retval);
4504 }
4505
4506 /*
4507 * kenwood_get_powerstat
4508 */
kenwood_get_powerstat(RIG * rig,powerstat_t * status)4509 int kenwood_get_powerstat(RIG *rig, powerstat_t *status)
4510 {
4511 char pwrbuf[6];
4512 int retval;
4513
4514 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4515
4516 if (!status)
4517 {
4518 RETURNFUNC(-RIG_EINVAL);
4519 }
4520
4521 retval = kenwood_safe_transaction(rig, "PS", pwrbuf, 6, 3);
4522
4523 if (retval != RIG_OK)
4524 {
4525 RETURNFUNC(retval);
4526 }
4527
4528 *status = pwrbuf[2] == '0' ? RIG_POWER_OFF : RIG_POWER_ON;
4529
4530 RETURNFUNC(RIG_OK);
4531 }
4532
4533 /*
4534 * kenwood_reset
4535 */
kenwood_reset(RIG * rig,reset_t reset)4536 int kenwood_reset(RIG *rig, reset_t reset)
4537 {
4538 char rstbuf[6];
4539 char rst;
4540
4541 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4542
4543 if (RIG_IS_TS990S)
4544 {
4545 switch (reset)
4546 {
4547 case RIG_RESET_SOFT: rst = '4'; break;
4548
4549 case RIG_RESET_VFO: rst = '3'; break;
4550
4551 case RIG_RESET_MCALL: rst = '2'; break;
4552
4553 case RIG_RESET_MASTER: rst = '5'; break;
4554
4555 default:
4556 rig_debug(RIG_DEBUG_ERR, "%s: unsupported reset %d\n",
4557 __func__, reset);
4558 RETURNFUNC(-RIG_EINVAL);
4559 }
4560 }
4561 else
4562 {
4563 switch (reset)
4564 {
4565 case RIG_RESET_VFO: rst = '1'; break;
4566
4567 case RIG_RESET_MASTER: rst = '2'; break;
4568
4569 default:
4570 rig_debug(RIG_DEBUG_ERR, "%s: unsupported reset %d\n",
4571 __func__, reset);
4572 RETURNFUNC(-RIG_EINVAL);
4573 }
4574 }
4575
4576 snprintf(rstbuf, sizeof(rstbuf), "SR%c", rst);
4577
4578 /* this command has no answer */
4579 RETURNFUNC(kenwood_transaction(rig, rstbuf, NULL, 0));
4580 }
4581
4582 /*
4583 * kenwood_send_morse
4584 */
kenwood_send_morse(RIG * rig,vfo_t vfo,const char * msg)4585 int kenwood_send_morse(RIG *rig, vfo_t vfo, const char *msg)
4586 {
4587 char morsebuf[40], m2[30];
4588 int msg_len, retval, i;
4589 const char *p;
4590
4591 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4592
4593 if (!msg)
4594 {
4595 RETURNFUNC(-RIG_EINVAL);
4596 }
4597
4598 p = msg;
4599 msg_len = strlen(msg);
4600
4601 while (msg_len > 0)
4602 {
4603 int buff_len;
4604
4605 /*
4606 * Check with "KY" if char buffer is available.
4607 * if not, sleep.
4608 */
4609 for (;;)
4610 {
4611 retval = kenwood_transaction(rig, "KY;", m2, 4);
4612
4613 if (retval != RIG_OK)
4614 {
4615 RETURNFUNC(retval);
4616 }
4617
4618 /*
4619 * If answer is "KY0;", there is space in buffer and we can proceed.
4620 * If answer is "KY1;", we have to wait a while
4621 * If answer is something else, return with error to prevent infinite loops
4622 */
4623 if (!strncmp(m2, "KY0", 3)) { break; }
4624
4625 if (!strncmp(m2, "KY1", 3)) { hl_usleep(500000); }
4626 else { RETURNFUNC(-RIG_EINVAL); }
4627 }
4628
4629 buff_len = msg_len > 24 ? 24 : msg_len;
4630
4631 strncpy(m2, p, 24);
4632 m2[24] = '\0';
4633
4634 /*
4635 * Make the total message segments 28 characters
4636 * in length because some Kenwoods demand it.
4637 * 0x20 fills in the message end.
4638 * Some rigs don't need the fill
4639 */
4640 switch (rig->caps->rig_model)
4641 {
4642 case RIG_MODEL_K3: // probably a lot more rigs need to go here
4643 case RIG_MODEL_K3S:
4644 case RIG_MODEL_KX2:
4645 case RIG_MODEL_KX3:
4646 snprintf(morsebuf, sizeof(morsebuf), "KY %s", m2);
4647 break;
4648
4649 default:
4650 /* the command must consist of 28 bytes 0x20 padded */
4651 snprintf(morsebuf, sizeof(morsebuf), "KY %-24s", m2);
4652
4653 for (i = strlen(morsebuf) - 1; i > 0 && morsebuf[i] == ' '; --i)
4654 {
4655 morsebuf[i] = 0x20;
4656 }
4657 }
4658
4659 retval = kenwood_transaction(rig, morsebuf, NULL, 0);
4660
4661 if (retval != RIG_OK)
4662 {
4663 RETURNFUNC(retval);
4664 }
4665
4666 msg_len -= buff_len;
4667 p += buff_len;
4668 }
4669
4670 RETURNFUNC(RIG_OK);
4671 }
4672
4673 /*
4674 * kenwood_vfo_op
4675 */
kenwood_vfo_op(RIG * rig,vfo_t vfo,vfo_op_t op)4676 int kenwood_vfo_op(RIG *rig, vfo_t vfo, vfo_op_t op)
4677 {
4678 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4679
4680 switch (op)
4681 {
4682 case RIG_OP_UP:
4683 RETURNFUNC(kenwood_transaction(rig, "UP", NULL, 0));
4684
4685 case RIG_OP_DOWN:
4686 RETURNFUNC(kenwood_transaction(rig, "DN", NULL, 0));
4687
4688 case RIG_OP_BAND_UP:
4689 RETURNFUNC(kenwood_transaction(rig, "BU", NULL, 0));
4690
4691 case RIG_OP_BAND_DOWN:
4692 RETURNFUNC(kenwood_transaction(rig, "BD", NULL, 0));
4693
4694 case RIG_OP_TUNE:
4695 RETURNFUNC(kenwood_transaction(rig, "AC111", NULL, 0));
4696
4697 case RIG_OP_CPY:
4698 RETURNFUNC(kenwood_transaction(rig, "VV", NULL, 0));
4699
4700 default:
4701 rig_debug(RIG_DEBUG_ERR, "%s: unsupported op %#x\n",
4702 __func__, op);
4703 RETURNFUNC(-RIG_EINVAL);
4704 }
4705 }
4706
4707 /*
4708 * kenwood_set_mem
4709 */
kenwood_set_mem(RIG * rig,vfo_t vfo,int ch)4710 int kenwood_set_mem(RIG *rig, vfo_t vfo, int ch)
4711 {
4712 char buf[7];
4713
4714 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4715
4716 if (RIG_IS_TS990S)
4717 {
4718 char c;
4719
4720 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
4721 {
4722 int err;
4723
4724 if (RIG_OK != (err = kenwood_get_vfo_main_sub(rig, &vfo)))
4725 {
4726 RETURNFUNC(err);
4727 }
4728 }
4729
4730 switch (vfo)
4731 {
4732 case RIG_VFO_MAIN: c = '0'; break;
4733
4734 case RIG_VFO_SUB: c = '1'; break;
4735
4736 default:
4737 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
4738 RETURNFUNC(-RIG_EINVAL);
4739 }
4740
4741 snprintf(buf, sizeof(buf), "MN%c%03d", c, ch);
4742 }
4743 else
4744 {
4745 /*
4746 * "MCbmm;"
4747 * where b is the bank number, mm the memory number.
4748 * b can be a space
4749 */
4750 snprintf(buf, sizeof(buf), "MC %02d", ch);
4751 }
4752
4753 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
4754 }
4755
4756 /*
4757 * kenwood_get_mem
4758 */
kenwood_get_mem(RIG * rig,vfo_t vfo,int * ch)4759 int kenwood_get_mem(RIG *rig, vfo_t vfo, int *ch)
4760 {
4761 char cmd[4];
4762 char membuf[10];
4763 int offs;
4764 int retval;
4765
4766 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4767
4768 if (!ch)
4769 {
4770 RETURNFUNC(-RIG_EINVAL);
4771 }
4772
4773 if (RIG_IS_TS990S)
4774 {
4775 char c;
4776
4777 if (RIG_VFO_CURR == vfo || RIG_VFO_VFO == vfo)
4778 {
4779 if (RIG_OK != (retval = kenwood_get_vfo_main_sub(rig, &vfo)))
4780 {
4781 RETURNFUNC(retval);
4782 }
4783 }
4784
4785 switch (vfo)
4786 {
4787 case RIG_VFO_MAIN: c = '0'; break;
4788
4789 case RIG_VFO_SUB: c = '1'; break;
4790
4791 default:
4792 rig_debug(RIG_DEBUG_ERR, "%s: unsupported VFO %s\n", __func__, rig_strvfo(vfo));
4793 RETURNFUNC(-RIG_EINVAL);
4794 }
4795
4796 snprintf(cmd, sizeof(cmd), "MN%c", c);
4797 offs = 3;
4798 }
4799 else
4800 {
4801 /*
4802 * "MCbmm;"
4803 * where b is the bank number, mm the memory number.
4804 * b can be a space
4805 */
4806 snprintf(cmd, sizeof(cmd), "MC");
4807 offs = 2;
4808 }
4809
4810 retval = kenwood_safe_transaction(rig, cmd, membuf, sizeof(membuf), 3 + offs);
4811
4812 if (retval != RIG_OK)
4813 {
4814 RETURNFUNC(retval);
4815 }
4816
4817 *ch = atoi(membuf + offs);
4818
4819 RETURNFUNC(RIG_OK);
4820 }
4821
kenwood_get_mem_if(RIG * rig,vfo_t vfo,int * ch)4822 int kenwood_get_mem_if(RIG *rig, vfo_t vfo, int *ch)
4823 {
4824 int err;
4825 char buf[4];
4826 struct kenwood_priv_data *priv = rig->state.priv;
4827
4828 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4829
4830 if (!ch)
4831 {
4832 RETURNFUNC(-RIG_EINVAL);
4833 }
4834
4835 err = kenwood_get_if(rig);
4836
4837 if (err != RIG_OK)
4838 {
4839 RETURNFUNC(err);
4840 }
4841
4842 memcpy(buf, &priv->info[26], 2);
4843 buf[2] = '\0';
4844
4845 *ch = atoi(buf);
4846
4847 RETURNFUNC(RIG_OK);
4848 }
4849
kenwood_get_channel(RIG * rig,vfo_t vfo,channel_t * chan,int read_only)4850 int kenwood_get_channel(RIG *rig, vfo_t vfo, channel_t *chan, int read_only)
4851 {
4852 int err;
4853 char buf[26];
4854 char cmd[8];
4855 char bank = ' ';
4856 struct kenwood_priv_caps *caps = kenwood_caps(rig);
4857
4858 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4859
4860 if (!chan)
4861 {
4862 RETURNFUNC(-RIG_EINVAL);
4863 }
4864
4865 /* put channel num in the command string */
4866
4867 if (RIG_IS_TS940)
4868 {
4869 bank = '0' + chan->bank_num;
4870 }
4871
4872 snprintf(cmd, sizeof(cmd), "MR0%c%02d", bank, chan->channel_num);
4873
4874 err = kenwood_safe_transaction(rig, cmd, buf, 26, 23);
4875
4876 if (err != RIG_OK)
4877 {
4878 RETURNFUNC(err);
4879 }
4880
4881 memset(chan, 0x00, sizeof(channel_t));
4882
4883 chan->vfo = RIG_VFO_VFO;
4884
4885 /* MR0 1700005890000510 ;
4886 * MRsbccfffffffffffMLTtt ;
4887 */
4888
4889 /* parse from right to left */
4890
4891 /* XXX based on the available documentation, there is no command
4892 * to read out the filters of a given memory channel. The rig, however,
4893 * stores this information.
4894 */
4895
4896 if (buf[19] == '0' || buf[19] == ' ')
4897 {
4898 chan->ctcss_tone = 0;
4899 }
4900 else
4901 {
4902 buf[22] = '\0';
4903
4904 if (rig->caps->ctcss_list)
4905 {
4906 chan->ctcss_tone = rig->caps->ctcss_list[atoi(&buf[20])];
4907 }
4908 }
4909
4910 /* memory lockout */
4911 if (buf[18] == '1')
4912 {
4913 chan->flags |= RIG_CHFLAG_SKIP;
4914 }
4915
4916 chan->mode = kenwood2rmode(buf[17] - '0', caps->mode_table);
4917
4918 buf[17] = '\0';
4919 chan->freq = atoi(&buf[6]);
4920
4921 if (chan->freq == RIG_FREQ_NONE)
4922 {
4923 RETURNFUNC(-RIG_ENAVAIL);
4924 }
4925
4926 buf[6] = '\0';
4927 chan->channel_num = atoi(&buf[4]);
4928
4929 if (buf[3] >= '0' && buf[3] <= '9')
4930 {
4931 chan->bank_num = buf[3] - '0';
4932 }
4933
4934 /* split freq */
4935 cmd[2] = '1';
4936 err = kenwood_safe_transaction(rig, cmd, buf, 26, 23);
4937
4938 if (err != RIG_OK)
4939 {
4940 RETURNFUNC(err);
4941 }
4942
4943 chan->tx_mode = kenwood2rmode(buf[17] - '0', caps->mode_table);
4944
4945 buf[17] = '\0';
4946 chan->tx_freq = atoi(&buf[6]);
4947
4948 if (chan->freq == chan->tx_freq)
4949 {
4950 chan->tx_freq = RIG_FREQ_NONE;
4951 chan->tx_mode = RIG_MODE_NONE;
4952 chan->split = RIG_SPLIT_OFF;
4953 }
4954 else
4955 {
4956 chan->split = RIG_SPLIT_ON;
4957 }
4958
4959 if (!read_only)
4960 {
4961 // Set rig to channel values
4962 rig_debug(RIG_DEBUG_ERR,
4963 "%s: please contact hamlib mailing list to implement this\n", __func__);
4964 rig_debug(RIG_DEBUG_ERR,
4965 "%s: need to know if rig updates when channel read or not\n", __func__);
4966 RETURNFUNC(-RIG_ENIMPL);
4967 }
4968
4969 RETURNFUNC(RIG_OK);
4970 }
4971
kenwood_set_channel(RIG * rig,vfo_t vfo,const channel_t * chan)4972 int kenwood_set_channel(RIG *rig, vfo_t vfo, const channel_t *chan)
4973 {
4974 char buf[128];
4975 char mode, tx_mode = 0;
4976 char bank = ' ';
4977 int err;
4978 int tone = 0;
4979 struct kenwood_priv_caps *caps = kenwood_caps(rig);
4980
4981 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
4982
4983 if (!chan)
4984 {
4985 RETURNFUNC(-RIG_EINVAL);
4986 }
4987
4988 mode = rmode2kenwood(chan->mode, caps->mode_table);
4989
4990 if (mode < 0)
4991 {
4992 rig_debug(RIG_DEBUG_ERR, "%s: unsupported mode '%s'\n",
4993 __func__, rig_strrmode(chan->mode));
4994 RETURNFUNC(-RIG_EINVAL);
4995 }
4996
4997 if (chan->split == RIG_SPLIT_ON)
4998 {
4999 tx_mode = rmode2kenwood(chan->tx_mode, caps->mode_table);
5000
5001 if (tx_mode < 0)
5002 {
5003 rig_debug(RIG_DEBUG_ERR, "%s: unsupported mode '%s'\n",
5004 __func__, rig_strrmode(chan->tx_mode));
5005 RETURNFUNC(-RIG_EINVAL);
5006 }
5007
5008 }
5009
5010 /* find tone */
5011 if (chan->ctcss_tone)
5012 {
5013
5014 for (tone = 0; rig->caps->ctcss_list[tone] != 0; tone++)
5015 {
5016 if (chan->ctcss_tone == rig->caps->ctcss_list[tone])
5017 {
5018 break;
5019 }
5020 }
5021
5022 if (chan->ctcss_tone != rig->caps->ctcss_list[tone])
5023 {
5024 tone = 0;
5025 }
5026 }
5027
5028 if (RIG_IS_TS940)
5029 {
5030 bank = '0' + chan->bank_num;
5031 }
5032
5033 snprintf(buf, sizeof(buf),
5034 "MW0%c%02d%011"PRIll"%c%c%c%02d ", /* note the space at
5035 the end */
5036 bank,
5037 chan->channel_num,
5038 (int64_t)chan->freq,
5039 '0' + mode,
5040 (chan->flags & RIG_CHFLAG_SKIP) ? '1' : '0',
5041 chan->ctcss_tone ? '1' : '0',
5042 chan->ctcss_tone ? (tone + 1) : 0);
5043
5044 err = kenwood_transaction(rig, buf, NULL, 0);
5045
5046 if (err != RIG_OK)
5047 {
5048 RETURNFUNC(err);
5049 }
5050
5051 snprintf(buf, sizeof(buf), "MW1%c%02d%011"PRIll"%c%c%c%02d ",
5052 bank,
5053 chan->channel_num,
5054 (int64_t)(chan->split == RIG_SPLIT_ON ? chan->tx_freq : 0),
5055 (chan->split == RIG_SPLIT_ON) ? ('0' + tx_mode) : '0',
5056 (chan->flags & RIG_CHFLAG_SKIP) ? '1' : '0',
5057 chan->ctcss_tone ? '1' : '0',
5058 chan->ctcss_tone ? (tone + 1) : 0);
5059
5060 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
5061 }
5062
kenwood_set_ext_parm(RIG * rig,token_t token,value_t val)5063 int kenwood_set_ext_parm(RIG *rig, token_t token, value_t val)
5064 {
5065 struct kenwood_priv_data *priv = rig->state.priv;
5066 char buf[4];
5067
5068 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
5069
5070 switch (token)
5071 {
5072 case TOK_VOICE:
5073 RETURNFUNC(kenwood_transaction(rig, "VR", NULL, 0));
5074
5075 case TOK_FINE:
5076 snprintf(buf, sizeof(buf), "FS%c", (val.i == 0) ? '0' : '1');
5077 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
5078
5079 case TOK_XIT:
5080 snprintf(buf, sizeof(buf), "XT%c", (val.i == 0) ? '0' : '1');
5081 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
5082
5083 case TOK_RIT:
5084 snprintf(buf, sizeof(buf), "RT%c", (val.i == 0) ? '0' : '1');
5085 RETURNFUNC(kenwood_transaction(rig, buf, NULL, 0));
5086
5087 case TOK_NO_ID:
5088 priv->no_id = val.i;
5089 RETURNFUNC(RIG_OK);
5090 }
5091
5092 RETURNFUNC(-RIG_EINVAL);
5093 }
5094
kenwood_get_ext_parm(RIG * rig,token_t token,value_t * val)5095 int kenwood_get_ext_parm(RIG *rig, token_t token, value_t *val)
5096 {
5097 int err;
5098 struct kenwood_priv_data *priv = rig->state.priv;
5099
5100 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
5101
5102 if (!val)
5103 {
5104 RETURNFUNC(-RIG_EINVAL);
5105 }
5106
5107 switch (token)
5108 {
5109 case TOK_FINE:
5110 RETURNFUNC(get_kenwood_func(rig, "FS", &val->i));
5111
5112 case TOK_XIT:
5113 err = kenwood_get_if(rig);
5114
5115 if (err != RIG_OK)
5116 {
5117 RETURNFUNC(err);
5118 }
5119
5120 val->i = (priv->info[24] == '1') ? 1 : 0;
5121 RETURNFUNC(RIG_OK);
5122
5123 case TOK_RIT:
5124 err = kenwood_get_if(rig);
5125
5126 if (err != RIG_OK)
5127 {
5128 RETURNFUNC(err);
5129 }
5130
5131 val->i = (priv->info[23] == '1') ? 1 : 0;
5132 RETURNFUNC(RIG_OK);
5133 }
5134
5135 RETURNFUNC(-RIG_ENIMPL);
5136 }
5137
5138 /*
5139 * kenwood_get_info
5140 * supposed to work only for TS2000...
5141 */
kenwood_get_info(RIG * rig)5142 const char *kenwood_get_info(RIG *rig)
5143 {
5144 char firmbuf[10];
5145 int retval;
5146
5147 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
5148
5149 if (!rig)
5150 {
5151 return ("*rig == NULL");
5152 }
5153
5154 retval = kenwood_safe_transaction(rig, "TY", firmbuf, 10, 5);
5155
5156 if (retval != RIG_OK)
5157 {
5158 return (NULL);
5159 }
5160
5161 switch (firmbuf[4])
5162 {
5163 case '0': return ("Firmware: Overseas type");
5164
5165 case '1': return ("Firmware: Japanese 100W type");
5166
5167 case '2': return ("Firmware: Japanese 20W type");
5168
5169 default: return ("Firmware: unknown");
5170 }
5171 }
5172
5173 #define IDBUFSZ 16
5174
5175 /*
5176 * proberigs_kenwood
5177 *
5178 * Notes:
5179 * There's only one rig possible per port.
5180 *
5181 * rig_model_t probeallrigs_kenwood(port_t *port, rig_probe_func_t cfunc, rig_ptr_t data)
5182 */
DECLARE_PROBERIG_BACKEND(kenwood)5183 DECLARE_PROBERIG_BACKEND(kenwood)
5184 {
5185 char idbuf[IDBUFSZ];
5186 int id_len = -1, i, k_id;
5187 int retval = -1;
5188 int rates[] = { 115200, 57600, 38400, 19200, 9600, 4800, 1200, 0 }; /* possible baud rates */
5189 int rates_idx;
5190 int write_delay = port->write_delay;
5191 int retry = port->retry;
5192
5193 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
5194
5195 if (!port)
5196 {
5197 RETURNFUNC(RIG_MODEL_NONE);
5198 }
5199
5200 if (port->type.rig != RIG_PORT_SERIAL)
5201 {
5202 RETURNFUNC(RIG_MODEL_NONE);
5203 }
5204
5205 port->write_delay = port->post_write_delay = 0;
5206 port->parm.serial.stop_bits = 2;
5207 port->retry = 0;
5208
5209 /*
5210 * try for all different baud rates
5211 */
5212 for (rates_idx = 0; rates[rates_idx]; rates_idx++)
5213 {
5214 port->parm.serial.rate = rates[rates_idx];
5215 port->timeout = 2 * 1000 / rates[rates_idx] + 50;
5216
5217 retval = serial_open(port);
5218
5219 if (retval != RIG_OK)
5220 {
5221 port->write_delay = write_delay;
5222 port->retry = retry;
5223 RETURNFUNC(RIG_MODEL_NONE);
5224 }
5225
5226 retval = write_block(port, "ID;", 3);
5227 id_len = read_string(port, idbuf, IDBUFSZ, ";\r", 2);
5228 close(port->fd);
5229
5230 if (retval != RIG_OK || id_len < 0)
5231 {
5232 continue;
5233 }
5234 }
5235
5236 if (retval != RIG_OK || id_len < 0 || !strcmp(idbuf, "ID;"))
5237 {
5238 port->write_delay = write_delay;
5239 port->retry = retry;
5240 RETURNFUNC(RIG_MODEL_NONE);
5241 }
5242
5243 /*
5244 * reply should be something like 'IDxxx;'
5245 */
5246 if (id_len != 5 && id_len != 6)
5247 {
5248 idbuf[7] = '\0';
5249 rig_debug(RIG_DEBUG_VERBOSE, "probe_kenwood: protocol error, "
5250 " expected %d, received %d: %s\n",
5251 6, id_len, idbuf);
5252 port->write_delay = write_delay;
5253 port->retry = retry;
5254 RETURNFUNC(RIG_MODEL_NONE);
5255 }
5256
5257
5258 /* first, try ID string */
5259 for (i = 0; kenwood_id_string_list[i].model != RIG_MODEL_NONE; i++)
5260 {
5261 if (!strncmp(kenwood_id_string_list[i].id, idbuf + 2, 16))
5262 {
5263 rig_debug(RIG_DEBUG_VERBOSE, "probe_kenwood: "
5264 "found %s\n", idbuf + 2);
5265
5266 if (cfunc)
5267 {
5268 (*cfunc)(port, kenwood_id_string_list[i].model, data);
5269 }
5270
5271 port->write_delay = write_delay;
5272 port->retry = retry;
5273 RETURNFUNC(kenwood_id_string_list[i].model);
5274 }
5275 }
5276
5277 /* then, try ID numbers */
5278
5279 k_id = atoi(idbuf + 2);
5280
5281 /*
5282 * Elecraft K2 returns same ID as TS570
5283 */
5284 if (k_id == 17)
5285 {
5286 retval = serial_open(port);
5287
5288 if (retval != RIG_OK)
5289 {
5290 RETURNFUNC(RIG_MODEL_NONE);
5291 }
5292
5293 retval = write_block(port, "K2;", 3);
5294 id_len = read_string(port, idbuf, IDBUFSZ, ";\r", 2);
5295 close(port->fd);
5296
5297 if (retval != RIG_OK)
5298 {
5299 RETURNFUNC(RIG_MODEL_NONE);
5300 }
5301
5302 /*
5303 * reply should be something like 'K2n;'
5304 */
5305 if (id_len == 4 || !strcmp(idbuf, "K2"))
5306 {
5307 rig_debug(RIG_DEBUG_VERBOSE, "%s: found K2\n", __func__);
5308
5309 if (cfunc)
5310 {
5311 (*cfunc)(port, RIG_MODEL_K2, data);
5312 }
5313
5314 RETURNFUNC(RIG_MODEL_K2);
5315 }
5316 }
5317
5318 for (i = 0; kenwood_id_list[i].model != RIG_MODEL_NONE; i++)
5319 {
5320 if (kenwood_id_list[i].id == k_id)
5321 {
5322 rig_debug(RIG_DEBUG_VERBOSE, "probe_kenwood: "
5323 "found %03d\n", k_id);
5324
5325 if (cfunc)
5326 {
5327 (*cfunc)(port, kenwood_id_list[i].model, data);
5328 }
5329
5330 RETURNFUNC(kenwood_id_list[i].model);
5331 }
5332 }
5333
5334 /*
5335 * not found in known table....
5336 * update kenwood_id_list[]!
5337 */
5338 rig_debug(RIG_DEBUG_WARN, "probe_kenwood: found unknown device "
5339 "with ID %03d, please report to Hamlib "
5340 "developers.\n", k_id);
5341
5342 rig_debug(RIG_DEBUG_TRACE, "%s: post_write_delay=%d\n", __func__,
5343 port->post_write_delay);
5344 RETURNFUNC(RIG_MODEL_NONE);
5345 }
5346
5347
5348 /*
5349 * initrigs_kenwood is called by rig_backend_load
5350 */
DECLARE_INITRIG_BACKEND(kenwood)5351 DECLARE_INITRIG_BACKEND(kenwood)
5352 {
5353 rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
5354
5355 rig_register(&ts950s_caps);
5356 rig_register(&ts950sdx_caps);
5357 rig_register(&ts50s_caps);
5358 rig_register(&ts140_caps);
5359 rig_register(&ts450s_caps);
5360 rig_register(&ts570d_caps);
5361 rig_register(&ts570s_caps);
5362 rig_register(&ts680s_caps);
5363 rig_register(&ts690s_caps);
5364 rig_register(&ts790_caps);
5365 rig_register(&ts850_caps);
5366 rig_register(&ts870s_caps);
5367 rig_register(&ts930_caps);
5368 rig_register(&ts2000_caps);
5369 rig_register(&trc80_caps);
5370 rig_register(&k2_caps);
5371 rig_register(&k3_caps);
5372 rig_register(&k3s_caps);
5373 rig_register(&kx2_caps);
5374 rig_register(&kx3_caps);
5375 rig_register(&k4_caps);
5376 rig_register(&xg3_caps);
5377
5378 rig_register(&ts440_caps);
5379 rig_register(&ts940_caps);
5380 rig_register(&ts711_caps);
5381 rig_register(&ts811_caps);
5382 rig_register(&r5000_caps);
5383
5384 rig_register(&tmd700_caps);
5385 rig_register(&thd7a_caps);
5386 rig_register(&thd72a_caps);
5387 rig_register(&thd74_caps);
5388 rig_register(&thf7e_caps);
5389 rig_register(&thg71_caps);
5390 rig_register(&tmv7_caps);
5391 rig_register(&tmd710_caps);
5392
5393 rig_register(&ts590_caps);
5394 rig_register(&ts990s_caps);
5395 rig_register(&ts590sg_caps);
5396 rig_register(&ts480_caps);
5397 rig_register(&thf6a_caps);
5398
5399 rig_register(&transfox_caps);
5400
5401 rig_register(&f6k_caps);
5402 rig_register(&powersdr_caps);
5403 rig_register(&pihpsdr_caps);
5404 rig_register(&ts890s_caps);
5405 rig_register(&pt8000a_caps);
5406 rig_register(&malachite_caps);
5407
5408 RETURNFUNC(RIG_OK);
5409 }
5410