Revert "Hamlib Exception Handling"

This reverts commit 1b39ddca1f4f346df3725d77f26b9f0241b3f4ec.
pull/2/head
Stelios Bounanos 2009-10-13 23:06:56 +01:00
rodzic a4ecf8e1b6
commit ecc297b0ae
3 zmienionych plików z 287 dodań i 221 usunięć

Wyświetl plik

@ -6,39 +6,66 @@
#ifndef _RIGCLASS_H
#define _RIGCLASS_H
#include <exception>
#include <string>
#include <hamlib/rig.h>
class RigException : public std::exception
{
public:
RigException(int e = 0)
: err(e), msg(rigerror(e)) { }
RigException(const char* m)
: err(0), msg(m) { }
RigException(const char* prefix, int e)
: err(e), msg(std::string(prefix).append(": ").append(rigerror(e))) { }
virtual ~RigException() throw() { }
const char* what(void) const throw() { return msg.c_str(); }
int error(void) const { return err; }
protected:
int err;
std::string msg;
};
class Rig {
protected:
RIG *rig;
int err;
RIG *rig; // Global ref. to the rig
public:
Rig();
Rig(rig_model_t rig_model);
virtual ~Rig();
const char *error() { return rigerror(err); }
void init(rig_model_t rig_model);
bool init(rig_model_t rig_model);
bool isOnLine() { return rig; }
bool open(void);
bool isOnLine() { return rig; }
// This method open the communication port to the rig
void open(void);
// This method close the communication port to the rig
void close(void);
bool setFreq(freq_t freq, vfo_t vfo = RIG_VFO_CURR);
freq_t getFreq(bool &b, vfo_t vfo = RIG_VFO_CURR );
void setFreq(freq_t freq, vfo_t vfo = RIG_VFO_CURR);
freq_t getFreq(vfo_t vfo = RIG_VFO_CURR);
bool setMode(rmode_t, pbwidth_t width = RIG_PASSBAND_NORMAL, vfo_t vfo = RIG_VFO_CURR);
rmode_t getMode(bool &b, pbwidth_t&, vfo_t vfo = RIG_VFO_CURR);
void setMode(rmode_t, pbwidth_t width = RIG_PASSBAND_NORMAL, vfo_t vfo = RIG_VFO_CURR);
rmode_t getMode(pbwidth_t&, vfo_t vfo = RIG_VFO_CURR);
bool setPTT (ptt_t ptt, vfo_t vfo = RIG_VFO_CURR);
ptt_t getPTT ( bool &b, vfo_t vfo = RIG_VFO_CURR);
void setPTT (ptt_t ptt, vfo_t vfo = RIG_VFO_CURR);
ptt_t getPTT (vfo_t vfo = RIG_VFO_CURR);
bool setConf(token_t token, const char *val);
bool setConf(const char *name, const char *val);
bool getConf(token_t token, char *val);
bool getConf(const char *name, char *val);
void setVFO(vfo_t);
vfo_t getVFO();
void setConf(token_t token, const char *val);
void setConf(const char *name, const char *val);
void getConf(token_t token, char *val);
void getConf(const char *name, char *val);
const char *getName();
const struct rig_caps* getCaps(void);

Wyświetl plik

@ -7,8 +7,6 @@
#include <vector>
#include <algorithm>
#include <hamlib/rig.h>
#include "trx.h"
#include "configuration.h"
#include "confdialog.h"
@ -47,6 +45,7 @@ static bool need_freq = false;
static bool need_mode = false;
static bool hamlib_bypass = false;
static bool hamlib_closed = false;
static int hamlib_passes = 20;
static long int hamlib_freq;
static rmode_t hamlib_rmode = RIG_MODE_USB;
@ -59,6 +58,15 @@ enum { SIDEBAND_RIG, SIDEBAND_LSB, SIDEBAND_USB };
static void *hamlib_loop(void *args);
void show_error(const char* msg1, const char* msg2 = 0)
{
string error = msg1;
if (msg2)
error.append(": ").append(msg2);
put_status(error.c_str(), 10.0);
LOG_ERROR("%s", error.c_str());
}
void hamlib_get_defaults()
{
char szParam[40];
@ -141,105 +149,122 @@ bool hamlib_init(bool bPtt)
return false;
}
char szParam[20];
try {
char szParam[20];
xcvr->init(progdefaults.HamRigModel);
xcvr->init(progdefaults.HamRigModel);
#ifdef __CYGWIN__
xcvr->setConf("rig_pathname", port.c_str());
xcvr->setConf("rig_pathname", port.c_str());
#else
xcvr->setConf("rig_pathname", progdefaults.HamRigDevice.c_str());
xcvr->setConf("rig_pathname", progdefaults.HamRigDevice.c_str());
#endif
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibWait);
xcvr->setConf("post_write_delay", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibWait);
xcvr->setConf("post_write_delay", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibWriteDelay);
xcvr->setConf("write_delay", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibWriteDelay);
xcvr->setConf("write_delay", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibTimeout);
xcvr->setConf("timeout", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibTimeout);
xcvr->setConf("timeout", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibRetries);
xcvr->setConf("retry", szParam);
snprintf(szParam, sizeof(szParam), "%d", progdefaults.HamlibRetries);
xcvr->setConf("retry", szParam);
if (xcvr->getCaps()->port_type == RIG_PORT_SERIAL) {
xcvr->setConf("serial_speed", progdefaults.strBaudRate());
if (xcvr->getCaps()->port_type == RIG_PORT_SERIAL) {
xcvr->setConf("serial_speed", progdefaults.strBaudRate());
if (progdefaults.HamlibRTSplus)
xcvr->setConf("rts_state", "ON");
else
xcvr->setConf("rts_state", "OFF");
if (progdefaults.HamlibRTSplus)
xcvr->setConf("rts_state", "ON");
else
xcvr->setConf("rts_state", "OFF");
if (progdefaults.HamlibDTRplus)
xcvr->setConf("dtr_state", "ON");
else
xcvr->setConf("dtr_state", "OFF");
if (progdefaults.HamlibDTRplus)
xcvr->setConf("dtr_state", "ON");
else
xcvr->setConf("dtr_state", "OFF");
if (progdefaults.HamlibRTSCTSflow)
xcvr->setConf("serial_handshake", "Hardware");
else if (progdefaults.HamlibXONXOFFflow)
xcvr->setConf("serial_handshake", "XONXOFF");
else
xcvr->setConf("serial_handshake", "None");
xcvr->setConf("stop_bits", progdefaults.HamRigStopbits == 1 ? "1" : "2");
}
string::size_type c = progdefaults.HamConfig.find('#');
if (c != string::npos)
progdefaults.HamConfig.erase(c);
if (!progdefaults.HamConfig.empty()) {
re_t re("([^, =]+) *= *([^, =]+)", REG_EXTENDED);
const char* conf = progdefaults.HamConfig.c_str();
int end;
while (re.match(conf)) {
xcvr->setConf(re.submatch(1).c_str(), re.submatch(2).c_str());
re.suboff(0, NULL, &end);
conf += end;
if (progdefaults.HamlibRTSCTSflow)
xcvr->setConf("serial_handshake", "Hardware");
else if (progdefaults.HamlibXONXOFFflow)
xcvr->setConf("serial_handshake", "XONXOFF");
else
xcvr->setConf("serial_handshake", "None");
xcvr->setConf("stop_bits", progdefaults.HamRigStopbits == 1 ? "1" : "2");
}
}
if (!xcvr->open()) {
LOG_ERROR("open failed");
string::size_type c = progdefaults.HamConfig.find('#');
if (c != string::npos)
progdefaults.HamConfig.erase(c);
if (!progdefaults.HamConfig.empty()) {
re_t re("([^, =]+) *= *([^, =]+)", REG_EXTENDED);
const char* conf = progdefaults.HamConfig.c_str();
int end;
while (re.match(conf)) {
xcvr->setConf(re.submatch(1).c_str(), re.submatch(2).c_str());
re.suboff(0, NULL, &end);
conf += end;
}
}
xcvr->open();
}
catch (const RigException& Ex) {
show_error(__func__, Ex.what());
xcvr->close();
return false;
}
MilliSleep(500);
need_freq = true;
freq = xcvr->getFreq(need_freq);
if (freq == 0 || !need_freq) {
xcvr->close();
LOG_ERROR("Rig not responding ");
return false;
}
need_mode = true;
mode = xcvr->getMode(need_mode, width);
if (!need_mode) {
LOG_ERROR("Get Mode ");
}
if (hamlib_ptt)
LOG_INFO("trying PTT");
if (!xcvr->setPTT(RIG_PTT_OFF)) {
LOG_ERROR("Set Ptt ");
hamlib_ptt = false;
LOG_INFO("trying frequency request");
try {
need_freq = true;
freq = xcvr->getFreq();
if (freq == 0) {
xcvr->close();
show_error(__func__, "Rig not responding");
return false;
}
}
catch (const RigException& Ex) {
show_error("Get Freq", Ex.what());
need_freq = false;
}
if (!need_freq && !need_mode && !hamlib_ptt) {
try {
need_mode = true;
mode = xcvr->getMode(width);
}
catch (const RigException& Ex) {
show_error("Get Mode", Ex.what());
need_mode = false;
}
try {
if (hamlib_ptt == true) {
LOG_INFO("trying PTT");
xcvr->setPTT(RIG_PTT_OFF);
}
}
catch (const RigException& Ex) {
show_error("Set Ptt", Ex.what());
hamlib_ptt = false;
}
if (need_freq == false && need_mode == false && hamlib_ptt == false ) {
xcvr->close();
LOG_INFO("Failed freq/mode/ptt");
return false;
}
hamlib_freq = 0;
hamlib_rmode = RIG_MODE_NONE;
hamlib_rmode = RIG_MODE_NONE;//RIG_MODE_USB;
hamlib_exit = false;
hamlib_bypass = false;
if (pthread_create(&hamlib_thread, NULL, hamlib_loop, NULL) < 0) {
LOG_ERROR("pthread_create failed");
show_error(__func__, "pthread_create failed");
xcvr->close();
return false;
}
@ -257,16 +282,12 @@ void hamlib_close(void)
if (hamlib_closed || !xcvr->isOnLine())
return;
hamlib_exit = true;
int count = 20;
pthread_mutex_lock(&hamlib_mutex);
hamlib_exit = true;
pthread_mutex_unlock(&hamlib_mutex);
while (!hamlib_closed) {
MilliSleep(50);
if (!count--) {
LOG_ERROR("Hamlib thread not responding, forcing shutdown");
show_error(__func__, "Hamlib thread not responding, forcing shutdown");
xcvr->close();
diediedie();
}
@ -276,6 +297,7 @@ void hamlib_close(void)
if (rigcontrol)
rigcontrol->hide();
wf->USB(true);
// wf->setQSY(0);
}
bool hamlib_active(void)
@ -285,105 +307,100 @@ bool hamlib_active(void)
void hamlib_set_ptt(int ptt)
{
if (!xcvr->isOnLine())
if (xcvr->isOnLine() == false)
return;
if (!hamlib_ptt)
return;
pthread_mutex_lock(&hamlib_mutex);
hamlib_ptt = xcvr->setPTT(ptt ? RIG_PTT_ON : RIG_PTT_OFF);
try {
xcvr->setPTT(ptt ? RIG_PTT_ON : RIG_PTT_OFF);
hamlib_bypass = ptt ? true : false;
}
catch (const RigException& Ex) {
show_error("Rig PTT", Ex.what());
hamlib_ptt = false;
}
pthread_mutex_unlock(&hamlib_mutex);
if (!hamlib_ptt)
LOG_ERROR("%s", xcvr->error());
}
void hamlib_set_qsy(long long f, long long fmid)
{
if (!xcvr->isOnLine())
if (xcvr->isOnLine() == false)
return;
pthread_mutex_lock(&hamlib_mutex);
double fdbl = f;
hamlib_qsy = false;
bool qsyerr = false;
pthread_mutex_lock(&hamlib_mutex);
qsyerr = xcvr->setFreq(fdbl);
try {
xcvr->setFreq(fdbl);
if (active_modem->freqlocked() == true) {
active_modem->set_freqlock(false);
active_modem->set_freq((int)fmid);
active_modem->set_freqlock(true);
} else
active_modem->set_freq((int)fmid);
wf->rfcarrier(f);
wf->movetocenter();
}
catch (const RigException& Ex) {
show_error("QSY", Ex.what());
hamlib_passes = 0;
}
pthread_mutex_unlock(&hamlib_mutex);
if (qsyerr)
LOG_ERROR("%s", xcvr->error());
if (active_modem->freqlocked()) {
active_modem->set_freqlock(false);
active_modem->set_freq((int)fmid);
active_modem->set_freqlock(true);
} else
active_modem->set_freq((int)fmid);
wf->rfcarrier(f);
wf->movetocenter();
}
int hamlib_setfreq(long f)
{
if (!xcvr->isOnLine())
if (xcvr->isOnLine() == false)
return -1;
int retval;
pthread_mutex_lock(&hamlib_mutex);
retval = xcvr->setFreq(f);
try {
LOG_DEBUG("%ld", f);
xcvr->setFreq(f);
// wf->rfcarrier(f);//(hamlib_freq);
}
catch (const RigException& Ex) {
show_error("SetFreq", Ex.what());
hamlib_passes = 0;
}
pthread_mutex_unlock(&hamlib_mutex);
if (!retval) {
LOG_ERROR("%s", xcvr->error());
return -1;
}
return 1;
}
int hamlib_setmode(rmode_t m)
{
if (!need_mode)
if (need_mode == false)
return -1;
if (!xcvr->isOnLine())
if (xcvr->isOnLine() == false)
return -1;
bool setok = true;
pthread_mutex_lock(&hamlib_mutex);
hamlib_rmode = xcvr->getMode(setok, hamlib_pbwidth);
if (setok) setok = xcvr->setMode(m, hamlib_pbwidth);
hamlib_rmode = m;
try {
hamlib_rmode = xcvr->getMode(hamlib_pbwidth);
xcvr->setMode(m, hamlib_pbwidth);
hamlib_rmode = m;
}
catch (const RigException& Ex) {
show_error("Set Mode", Ex.what());
hamlib_passes = 0;
}
pthread_mutex_unlock(&hamlib_mutex);
if (!setok) {
LOG_ERROR("%s", xcvr->error());
return -1;
}
return 1;
}
int hamlib_setwidth(pbwidth_t w)
{
if (!xcvr->isOnLine())
if (xcvr->isOnLine() == false)
return -1;
bool setok = true;
pthread_mutex_lock(&hamlib_mutex);
hamlib_rmode = xcvr->getMode(setok, hamlib_pbwidth);
if (setok) setok = xcvr->setMode(hamlib_rmode, w);
hamlib_pbwidth = w;
try {
hamlib_rmode = xcvr->getMode(hamlib_pbwidth);
xcvr->setMode(hamlib_rmode, w);
hamlib_pbwidth = w;
}
catch (const RigException& Ex) {
show_error("Set Width", Ex.what());
hamlib_passes = 0;
}
pthread_mutex_unlock(&hamlib_mutex);
if (!setok) {
LOG_ERROR("%s", xcvr->error());
return -1;
}
return 1;
}
@ -414,21 +431,35 @@ static void *hamlib_loop(void *args)
// hamlib locked while accessing hamlib serial i/o
pthread_mutex_lock(&hamlib_mutex);
if (need_freq) {
freq_t f = xcvr->getFreq(freqok);
if (freqok) {
freq = (long int) f;
freqok = (freq > 0);
} else
LOG_ERROR("%s", xcvr->error());
if (need_freq) {
freq_t f;
try {
f = xcvr->getFreq();
freq = (long int) f;
freqok = true;
if (freq == 0) {
pthread_mutex_unlock(&hamlib_mutex);
continue;
}
}
if (need_mode && hamlib_rmode == numode) {
numode = xcvr->getMode(modeok, hamlib_pbwidth);
if (!modeok)
LOG_ERROR("%s", xcvr->error());
catch (const RigException& Ex) {
show_error(__func__, "Rig not responding: freq");
freqok = false;
}
}
if (hamlib_exit)
break;
if (need_mode && hamlib_rmode == numode) {
try {
numode = xcvr->getMode(hamlib_pbwidth);
modeok = true;
}
catch (const RigException& Ex) {
show_error(__func__, "Rig not responding: mode");
modeok = false;
}
}
pthread_mutex_unlock(&hamlib_mutex);
if (hamlib_exit)
@ -436,7 +467,7 @@ static void *hamlib_loop(void *args)
if (hamlib_bypass)
continue;
if (freqok && (freq != hamlib_freq)) {
if (freqok && freq && (freq != hamlib_freq)) {
hamlib_freq = freq;
show_frequency(hamlib_freq);
wf->rfcarrier(hamlib_freq);
@ -454,6 +485,9 @@ static void *hamlib_loop(void *args)
hamlib_rmode == RIG_MODE_ECSSLSB ||
hamlib_rmode == RIG_MODE_RTTY));
}
if (hamlib_exit)
break;
}
hamlib_closed = true;

Wyświetl plik

@ -21,6 +21,8 @@ Rig::Rig() : rig(0) { }
Rig::Rig(rig_model_t rig_model)
{
rig = rig_init(rig_model);
if (!rig)
throw RigException ("Could not initialize rig");
}
Rig::~Rig()
@ -28,16 +30,12 @@ Rig::~Rig()
close();
}
bool Rig::init(rig_model_t rig_model)
void Rig::init(rig_model_t rig_model)
{
close();
rig = rig_init(rig_model);
if (rig) {
LOG_INFO("Initialised rig model %d: %s", rig_model, getName());
return true;
}
LOG_ERROR("Could not initialize rig");
return false;
if ((rig = rig_init(rig_model)) == NULL)
throw RigException ("Could not initialize rig");
LOG_INFO("Initialised rig model %d: %s", rig_model, getName());
}
const char *Rig::getName()
@ -50,9 +48,11 @@ const struct rig_caps* Rig::getCaps(void)
return rig ? rig->caps : 0;
}
bool Rig::open(void)
void Rig::open(void)
{
return (rig_open(rig) == RIG_OK);
int err = rig_open(rig);
if (err != RIG_OK)
throw RigException(err);
}
void Rig::close(void)
@ -64,90 +64,95 @@ void Rig::close(void)
}
}
bool Rig::setFreq(freq_t freq, vfo_t vfo)
void Rig::setFreq(freq_t freq, vfo_t vfo)
{
err = 0;
int err;
for (int i = 0; i < NUMTRIES; i++) {
if ((err = rig_set_freq(rig, vfo, freq)) == RIG_OK);
return true;
err = rig_set_freq(rig, vfo, freq);
if (err == RIG_OK)
return;
}
return false;
}
throw RigException(err);
}
freq_t Rig::getFreq(bool & b, vfo_t vfo)
freq_t Rig::getFreq(vfo_t vfo)
{
err = 0;
freq_t freq = 0;
for (int i = 0; i < NUMTRIES; i++)
if ((err = rig_get_freq(rig, vfo, &freq)) == RIG_OK)
int i;
for (i = 0; i < NUMTRIES; i++)
if (rig_get_freq(rig, vfo, &freq) == RIG_OK)
break;
b = (err == RIG_OK);
return freq;
}
bool Rig::setMode(rmode_t mode, pbwidth_t width, vfo_t vfo)
void Rig::setMode(rmode_t mode, pbwidth_t width, vfo_t vfo)
{
err = 0;
int err;
for (int i = 0; i < NUMTRIES; i++) {
if ((err = rig_set_mode(rig, vfo, mode, width)) == RIG_OK)
return true;
return;
}
return false;
throw RigException(err);
}
rmode_t Rig::getMode( bool & b, pbwidth_t& width, vfo_t vfo)
rmode_t Rig::getMode(pbwidth_t& width, vfo_t vfo)
{
int err;
rmode_t mode;
for (int i = 0; i < NUMTRIES; i++)
for (int i = 0; i < NUMTRIES; i++) {
if ((err = rig_get_mode(rig, vfo, &mode, &width)) == RIG_OK)
break;
b = (err == RIG_OK);
return mode;
return mode;
}
throw RigException(err);
}
bool Rig::setPTT(ptt_t ptt, vfo_t vfo)
void Rig::setPTT(ptt_t ptt, vfo_t vfo)
{
err = 0;
for (int i = 0; i < NUMTRIES; i++)
int err;
for (int i = 0; i < NUMTRIES; i++) {
if ((err = rig_set_ptt(rig, vfo, ptt)) == RIG_OK)
return true;
return false;
return;
}
throw RigException(err);
}
ptt_t Rig::getPTT(bool & b, vfo_t vfo)
ptt_t Rig::getPTT(vfo_t vfo)
{
err = 0;
int err;
ptt_t ptt;
for (int i = 0; i < NUMTRIES; i++)
for (int i = 0; i < NUMTRIES; i++) {
if ((err = rig_get_ptt(rig, vfo, &ptt)) == RIG_OK)
break;
b = (err == RIG_OK);
return ptt;
return ptt;
}
throw RigException(err);
}
bool Rig::setConf(token_t token, const char *val)
void Rig::setConf(token_t token, const char *val)
{
err = rig_set_conf(rig, token, val);
return (err == RIG_OK);
int err = rig_set_conf(rig, token, val);
if (err != RIG_OK)
throw RigException(err);
}
void Rig::setConf(const char *name, const char *val)
{
LOG_INFO("setting \"%s\" to \"%s\"", name, val);
int err = rig_set_conf(rig, tokenLookup(name), val);
if (err != RIG_OK)
throw RigException(name, err);
}
bool Rig::setConf(const char *name, const char *val)
void Rig::getConf(token_t token, char *val)
{
err = rig_set_conf(rig, tokenLookup(name), val);
return (err == RIG_OK);
int err = rig_get_conf(rig, token, val);
if (err != RIG_OK)
throw RigException(err);
}
bool Rig::getConf(token_t token, char *val)
void Rig::getConf(const char *name, char *val)
{
err = rig_get_conf(rig, token, val);
return (err == RIG_OK);
}
bool Rig::getConf(const char *name, char *val)
{
err = rig_get_conf(rig, tokenLookup(name), val);
return (err == RIG_OK);
int err = rig_get_conf(rig, tokenLookup(name), val);
if (err != RIG_OK)
throw RigException(name, err);
}
token_t Rig::tokenLookup(const char *name)