Hamlib/skanti/skanti.c

351 wiersze
7.3 KiB
C
Czysty Zwykły widok Historia

/*
* Hamlib Skanti backend - main file
* Copyright (c) 2004-2005 by Stephane Fillod
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <math.h>
#include <hamlib/rig.h>
#include <serial.h>
#include <misc.h>
#include <cal.h>
#include <register.h>
#include "skanti.h"
/* Line Feed */
#define EOM "\x0d"
#define LF "\x0a"
#define PROMPT ">"
#define BUFSZ 32
/*
* modes
*/
2019-11-30 16:19:08 +00:00
#define MD_LSB "L"
#define MD_USB "J"
#define MD_CW "A1"
#define MD_MCW "A2"
#define MD_AM "H"
#define MD_RTTY "F"
2019-11-30 16:19:08 +00:00
#define MD_R3E "R"
/*
* skanti_transaction
* We assume that rig!=NULL, rig->state!= NULL, data!=NULL, data_len!=NULL
* Otherwise, you'll get a nice seg fault. You've been warned!
* TODO: error case handling
*/
2019-11-30 16:19:08 +00:00
static int skanti_transaction(RIG *rig, const char *cmd, int cmd_len,
char *data, int *data_len)
{
2019-11-30 16:19:08 +00:00
int retval;
struct rig_state *rs;
rs = &rig->state;
serial_flush(&rs->rigport);
retval = write_block(&rs->rigport, cmd, cmd_len);
if (retval != RIG_OK)
{
return retval;
}
/* no data expected, check for OK returned */
if (!data || !data_len)
{
/*
* Transceiver sends back ">"
*/
2019-12-08 22:01:13 +00:00
char retbuf[BUFSZ + 1];
2019-11-30 16:19:08 +00:00
retval = read_string(&rs->rigport, retbuf, BUFSZ, PROMPT, strlen(PROMPT));
if (retval < 0)
{
return retval;
}
retbuf[retval] = '\0';
if (strstr(retbuf, PROMPT))
{
return RIG_OK;
}
else
{
return -RIG_ERJCTED;
}
}
retval = read_string(&rs->rigport, data, BUFSZ, LF, strlen(LF));
if (retval == -RIG_ETIMEOUT)
{
retval = 0;
}
if (retval < 0)
{
return retval;
}
*data_len = retval;
/* strip CR/LF from string
*/
*data_len -= 2;
data[*data_len] = 0;
return RIG_OK;
}
/*
* skanti_reset
* Assumes rig!=NULL
*/
int skanti_reset(RIG *rig, reset_t reset)
{
2019-11-30 16:19:08 +00:00
int retval;
2019-11-30 16:19:08 +00:00
/*
* master reset
*
* returned data: *x1A345SF
* whatever this means? unit serial #?
*/
2019-11-30 16:19:08 +00:00
retval = skanti_transaction(rig, "0" EOM, strlen("0" EOM), NULL, NULL);
2019-11-30 16:19:08 +00:00
if (retval != RIG_OK)
{
return retval;
}
return RIG_OK;
}
/*
* skanti_set_freq
* Assumes rig!=NULL
*/
int skanti_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
2019-11-30 16:19:08 +00:00
char freqbuf[BUFSZ];
int freq_len;
2019-11-30 16:19:08 +00:00
/* 6 digits */
freq_len = sprintf(freqbuf, "Z%06ld" EOM, (long)(freq / 100));
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, freqbuf, freq_len, NULL, NULL);
}
/*
* skanti_set_mode
* Assumes rig!=NULL
*/
int skanti_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
{
2019-11-30 16:19:08 +00:00
int retval;
char *sk_mode, *sk_filter;
pbwidth_t passband_normal;
switch (mode)
{
/* TODO: MCW, R3E */
case RIG_MODE_CW: sk_mode = MD_CW EOM; break;
case RIG_MODE_USB: sk_mode = MD_USB EOM; break;
case RIG_MODE_LSB: sk_mode = MD_LSB EOM; break;
case RIG_MODE_RTTY: sk_mode = MD_RTTY EOM; break;
case RIG_MODE_AM: sk_mode = MD_AM EOM; break;
default:
rig_debug(RIG_DEBUG_ERR, "%s: unsupported mode %s\n",
__func__, rig_strrmode(mode));
return -RIG_EINVAL;
}
retval = skanti_transaction(rig, sk_mode, strlen(sk_mode), NULL, NULL);
if (retval != RIG_OK)
{
return retval;
}
if (RIG_PASSBAND_NOCHANGE == width) { return retval; }
/*
* TODO: please sk8000 owners, check this, I'm not sure
* which passband is default!
*/
passband_normal = rig_passband_normal(rig, mode);
if (width == RIG_PASSBAND_NORMAL ||
width == passband_normal)
{
sk_filter = "I" EOM;
}
else if (width < passband_normal)
{
sk_filter = width < 1000 ? "V" EOM : "N" EOM;
}
else
{
sk_filter = "W" EOM;
}
retval = skanti_transaction(rig, sk_filter, strlen(sk_filter), NULL, NULL);
return retval;
}
/*
* skanti_set_split_freq
* Assumes rig!=NULL
*/
int skanti_set_split_freq(RIG *rig, vfo_t vfo, freq_t tx_freq)
{
2019-11-30 16:19:08 +00:00
char freqbuf[BUFSZ];
int freq_len;
2019-11-30 16:19:08 +00:00
/* 6 digits */
freq_len = sprintf(freqbuf, "T%06ld" EOM, (long)(tx_freq / 100));
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, freqbuf, freq_len, NULL, NULL);
}
/*
* skanti_set_level
* Assumes rig!=NULL
* FIXME: cannot support PREAMP and ATT both at same time (make sense though)
*/
int skanti_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val)
{
2019-11-30 16:19:08 +00:00
int cmd_len;
char cmdbuf[BUFSZ], *agc;
/* Optimize:
* sort the switch cases with the most frequent first
*/
switch (level)
{
case RIG_LEVEL_PREAMP:
cmd_len = sprintf(cmdbuf, "R%c" EOM, val.i ? 'F' : 'O');
return skanti_transaction(rig, cmdbuf, cmd_len, NULL, NULL);
case RIG_LEVEL_ATT:
cmd_len = sprintf(cmdbuf, "A%c" EOM, val.i ? 'T' : 'O');
return skanti_transaction(rig, cmdbuf, cmd_len, NULL, NULL);
2019-11-30 16:19:08 +00:00
case RIG_LEVEL_RFPOWER:
cmd_len = sprintf(cmdbuf, "M%cO" EOM,
val.f < 0.33 ? 'L' : (val.f < 0.66 ? 'M' : 'F'));
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, cmdbuf, cmd_len, NULL, NULL);
2019-11-30 16:19:08 +00:00
case RIG_LEVEL_AGC:
switch (val.i)
{
case RIG_AGC_SLOW: agc = "AS"EOM; break;
2019-11-30 16:19:08 +00:00
case RIG_AGC_FAST: agc = "AA"EOM; break;
2019-11-30 16:19:08 +00:00
case RIG_AGC_OFF: agc = "AF"EOM; break;
2019-11-30 16:19:08 +00:00
default: return -RIG_EINVAL;
}
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, agc, strlen(agc), NULL, NULL);
2019-11-30 16:19:08 +00:00
default:
rig_debug(RIG_DEBUG_ERR, "%s: unsupported set_level %s\n", __func__,
rig_strlevel(level));
return -RIG_EINVAL;
}
2019-11-30 16:19:08 +00:00
return RIG_OK;
}
/*
* skanti_set_ptt
* Assumes rig!=NULL, ptt!=NULL
*/
int skanti_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
{
2019-11-30 16:19:08 +00:00
char cmdbuf[BUFSZ];
int cmd_len;
2019-11-30 16:19:08 +00:00
cmd_len = sprintf(cmdbuf, "X%c" EOM, ptt == RIG_PTT_ON ? 'N' : 'F');
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, cmdbuf, cmd_len, NULL, NULL);
}
int skanti_vfo_op(RIG *rig, vfo_t vfo, vfo_op_t op)
{
2019-11-30 16:19:08 +00:00
if (op != RIG_OP_TUNE)
{
return -RIG_EINVAL;
}
2019-11-30 16:19:08 +00:00
return skanti_transaction(rig, "XT"EOM, strlen("XT"EOM), NULL, NULL);
}
/*
* initrigs_skanti is called by rig_backend_load
*/
DECLARE_INITRIG_BACKEND(skanti)
{
2019-11-30 16:19:08 +00:00
rig_debug(RIG_DEBUG_VERBOSE, "%s: _init called\n", __func__);
2019-11-30 16:19:08 +00:00
rig_register(&trp8000_caps);
rig_register(&trp8255_caps);
2019-11-30 16:19:08 +00:00
return RIG_OK;
}