2013-04-22 20:52:51 -04:00
|
|
|
/**
|
|
|
|
* \file src/rigclass.cc
|
|
|
|
* \brief Ham Radio Control Libraries C++ interface
|
|
|
|
* \author Stephane Fillod
|
|
|
|
* \date 2001-2003
|
|
|
|
*
|
|
|
|
* Hamlib C++ interface is a frontend implementing wrapper functions.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 09:17:22 -04:00
|
|
|
/**
|
2013-04-28 10:47:45 -04:00
|
|
|
*
|
2013-04-22 20:52:51 -04:00
|
|
|
* Hamlib C++ bindings - main file
|
|
|
|
* Copyright (c) 2001-2003 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 <hamlib/rig.h>
|
2013-04-28 10:47:45 -04:00
|
|
|
#include "rigclass.h"
|
2013-05-21 15:49:41 -04:00
|
|
|
#include <QDebug>
|
2013-07-08 09:17:22 -04:00
|
|
|
#include <QHostAddress>
|
2013-04-30 14:54:11 -04:00
|
|
|
|
2013-07-23 13:32:59 -04:00
|
|
|
#define NUMTRIES 5
|
|
|
|
|
2013-04-22 20:52:51 -04:00
|
|
|
static int hamlibpp_freq_event(RIG *rig, vfo_t vfo, freq_t freq, rig_ptr_t arg);
|
|
|
|
|
|
|
|
static int hamlibpp_freq_event(RIG *rig, vfo_t vfo, freq_t freq, rig_ptr_t arg)
|
|
|
|
{
|
|
|
|
if (!rig || !rig->state.obj)
|
|
|
|
return -RIG_EINVAL;
|
|
|
|
|
|
|
|
/* assert rig == ((Rig*)rig->state.obj).theRig */
|
|
|
|
return ((Rig*)rig->state.obj)->FreqEvent(vfo, freq, arg);
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:21:19 -04:00
|
|
|
Rig::Rig()
|
|
|
|
{
|
2013-04-26 18:06:22 -04:00
|
|
|
rig_set_debug_level( RIG_DEBUG_WARN);
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rig::~Rig() {
|
|
|
|
theRig->state.obj = NULL;
|
2013-04-30 15:55:13 -04:00
|
|
|
rig_cleanup(theRig);
|
2013-04-22 20:52:51 -04:00
|
|
|
caps = NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:21:19 -04:00
|
|
|
int Rig::init(rig_model_t rig_model)
|
|
|
|
{
|
|
|
|
int initOk;
|
|
|
|
|
|
|
|
theRig = rig_init(rig_model);
|
|
|
|
if (!theRig)
|
|
|
|
initOk = false;
|
|
|
|
else
|
|
|
|
initOk = true;
|
|
|
|
|
|
|
|
caps = theRig->caps;
|
|
|
|
theRig->callbacks.freq_event = &hamlibpp_freq_event;
|
|
|
|
theRig->state.obj = (rig_ptr_t)this;
|
|
|
|
|
|
|
|
return initOk;
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:49:41 -04:00
|
|
|
int Rig::open(int n) {
|
2013-07-08 09:17:22 -04:00
|
|
|
m_hrd=false;
|
|
|
|
m_cmndr=false;
|
|
|
|
if(n<9900) {
|
|
|
|
if(n==-99999) return -1; //Silence compiler warning
|
|
|
|
return rig_open(theRig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN32 // Ham radio Deluxe or Commander (Windows only)
|
|
|
|
if(n==9999) {
|
|
|
|
m_hrd=true;
|
2013-05-21 15:49:41 -04:00
|
|
|
bool bConnect=false;
|
|
|
|
bConnect = HRDInterfaceConnect(L"localhost",7809);
|
|
|
|
if(bConnect) {
|
|
|
|
const wchar_t* context=HRDInterfaceSendMessage(L"Get Context");
|
|
|
|
m_context="[" + QString::fromWCharArray (context,-1) + "] ";
|
2013-05-21 17:31:11 -04:00
|
|
|
HRDInterfaceFreeString(context);
|
2013-05-21 15:49:41 -04:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
m_hrd=false;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 09:17:22 -04:00
|
|
|
if(n==9998) {
|
2013-07-23 13:32:59 -04:00
|
|
|
if(commanderSocket->state()==QAbstractSocket::ConnectedState) {
|
|
|
|
commanderSocket->abort();
|
2013-07-09 18:41:15 -04:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:32:59 -04:00
|
|
|
if(commanderSocket->state()==QAbstractSocket::UnconnectedState) {
|
|
|
|
commanderSocket->connectToHost(QHostAddress::LocalHost, 52002);
|
|
|
|
if(!commanderSocket->waitForConnected(1000)) {
|
2013-07-09 18:41:15 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-08 09:17:22 -04:00
|
|
|
}
|
|
|
|
QString t;
|
|
|
|
t="<command:10>CmdGetFreq<parameters:0>";
|
|
|
|
QByteArray ba = t.toLocal8Bit();
|
|
|
|
const char* buf=ba.data();
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->write(buf);
|
|
|
|
commanderSocket->waitForReadyRead(1000);
|
|
|
|
QByteArray reply=commanderSocket->read(128);
|
2013-07-09 18:41:15 -04:00
|
|
|
if(reply.indexOf("<CmdFreq:")==0) {
|
2013-07-08 09:17:22 -04:00
|
|
|
m_cmndr=true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|
|
|
|
|
2013-04-30 14:54:11 -04:00
|
|
|
int Rig::close(void) {
|
2013-05-27 16:27:36 -04:00
|
|
|
#ifdef WIN32 // Ham Radio Deluxe only on Windows
|
2013-05-21 15:49:41 -04:00
|
|
|
if(m_hrd) {
|
|
|
|
HRDInterfaceDisconnect();
|
2013-05-23 11:05:16 -04:00
|
|
|
return 0;
|
2013-07-08 09:17:22 -04:00
|
|
|
} else if(m_cmndr) {
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->close();
|
2013-07-08 09:17:22 -04:00
|
|
|
return 0;
|
2013-05-27 16:27:36 -04:00
|
|
|
} else
|
2013-05-21 17:36:34 -04:00
|
|
|
#endif
|
2013-05-27 16:27:36 -04:00
|
|
|
{
|
2013-05-21 15:49:41 -04:00
|
|
|
return rig_close(theRig);
|
|
|
|
}
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|
|
|
|
|
2013-04-30 14:54:11 -04:00
|
|
|
int Rig::setConf(const char *name, const char *val)
|
2013-04-22 20:52:51 -04:00
|
|
|
{
|
2013-04-30 15:55:13 -04:00
|
|
|
return rig_set_conf(theRig, tokenLookup(name), val);
|
2013-04-30 14:54:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Rig::setFreq(freq_t freq, vfo_t vfo) {
|
2013-07-10 09:57:13 -04:00
|
|
|
#ifdef WIN32 // Ham Radio Deluxe (only on Windows)
|
2013-05-21 15:49:41 -04:00
|
|
|
if(m_hrd) {
|
|
|
|
QString t;
|
|
|
|
int nhz=(int)freq;
|
|
|
|
t=m_context + "Set Frequency-Hz " + QString::number(nhz);
|
|
|
|
const wchar_t* cmnd = (const wchar_t*) t.utf16();
|
|
|
|
const wchar_t* result=HRDInterfaceSendMessage(cmnd);
|
|
|
|
QString t2=QString::fromWCharArray (result,-1);
|
2013-05-21 17:31:11 -04:00
|
|
|
HRDInterfaceFreeString(result);
|
2013-05-21 15:49:41 -04:00
|
|
|
if(t2=="OK") {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-08 09:17:22 -04:00
|
|
|
} else if(m_cmndr) {
|
|
|
|
QString t;
|
2013-07-18 07:55:29 -04:00
|
|
|
double f=0.001*freq;
|
|
|
|
t.sprintf("<command:10>CmdSetFreq<parameters:23><xcvrfreq:10>%10.3f",f);
|
2013-07-08 09:17:22 -04:00
|
|
|
QByteArray ba = t.toLocal8Bit();
|
|
|
|
const char* buf=ba.data();
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->write(buf);
|
|
|
|
commanderSocket->waitForBytesWritten(1000);
|
2013-07-08 09:17:22 -04:00
|
|
|
return 0;
|
2013-05-27 16:27:36 -04:00
|
|
|
} else
|
2013-05-21 17:36:34 -04:00
|
|
|
#endif
|
2013-07-08 09:17:22 -04:00
|
|
|
{
|
2013-05-21 15:49:41 -04:00
|
|
|
return rig_set_freq(theRig, vfo, freq);
|
|
|
|
}
|
2013-04-30 14:54:11 -04:00
|
|
|
}
|
|
|
|
|
2013-07-08 09:17:22 -04:00
|
|
|
int Rig::setXit(shortfreq_t xit, vfo_t vfo)
|
|
|
|
{
|
|
|
|
return rig_set_xit(theRig, vfo, xit);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Rig::setVFO(vfo_t vfo)
|
|
|
|
{
|
|
|
|
return rig_set_vfo(theRig, vfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfo_t Rig::getVFO()
|
|
|
|
{
|
|
|
|
vfo_t vfo;
|
|
|
|
rig_get_vfo(theRig, &vfo);
|
|
|
|
return vfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Rig::setSplitFreq(freq_t tx_freq, vfo_t vfo) {
|
|
|
|
#ifdef WIN32 // Ham Radio Deluxe only on Windows
|
|
|
|
if(m_hrd) {
|
|
|
|
QString t;
|
|
|
|
int nhz=(int)tx_freq;
|
|
|
|
t=m_context + "Set Frequency-Hz " + QString::number(nhz);
|
|
|
|
const wchar_t* cmnd = (const wchar_t*) t.utf16();
|
|
|
|
const wchar_t* result=HRDInterfaceSendMessage(cmnd);
|
|
|
|
QString t2=QString::fromWCharArray (result,-1);
|
|
|
|
HRDInterfaceFreeString(result);
|
|
|
|
if(t2=="OK") {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(m_cmndr) {
|
|
|
|
QString t;
|
2013-07-18 07:55:29 -04:00
|
|
|
double f=0.001*tx_freq;
|
|
|
|
t.sprintf("<command:12>CmdSetTxFreq<parameters:23><xcvrfreq:10>%10.3f",f);
|
2013-07-08 09:17:22 -04:00
|
|
|
QByteArray ba = t.toLocal8Bit();
|
|
|
|
const char* buf=ba.data();
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->write(buf);
|
|
|
|
commanderSocket->waitForBytesWritten(1000);
|
2013-07-08 09:17:22 -04:00
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return rig_set_split_freq(theRig, vfo, tx_freq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 14:54:11 -04:00
|
|
|
freq_t Rig::getFreq(vfo_t vfo)
|
|
|
|
{
|
|
|
|
freq_t freq;
|
2013-07-10 09:57:13 -04:00
|
|
|
#ifdef WIN32 // Ham Radio Deluxe (only on Windows)
|
2013-05-21 15:49:41 -04:00
|
|
|
if(m_hrd) {
|
|
|
|
const wchar_t* cmnd = (const wchar_t*) (m_context+"Get Frequency").utf16();
|
|
|
|
const wchar_t* freqString=HRDInterfaceSendMessage(cmnd);
|
|
|
|
QString t2=QString::fromWCharArray (freqString,-1);
|
|
|
|
HRDInterfaceFreeString(freqString);
|
|
|
|
freq=t2.toDouble();
|
|
|
|
return freq;
|
2013-07-08 09:17:22 -04:00
|
|
|
} else if(m_cmndr) {
|
|
|
|
QString t;
|
|
|
|
t="<command:10>CmdGetFreq<parameters:0>";
|
|
|
|
QByteArray ba = t.toLocal8Bit();
|
|
|
|
const char* buf=ba.data();
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->write(buf);
|
|
|
|
commanderSocket->waitForReadyRead(1000);
|
|
|
|
QByteArray reply=commanderSocket->read(128);
|
2013-07-08 09:17:22 -04:00
|
|
|
QString t2(reply);
|
|
|
|
if(t2.indexOf("<CmdFreq:")==0) {
|
|
|
|
int i1=t2.indexOf(">");
|
|
|
|
t2=t2.mid(i1+1).replace(",","");
|
|
|
|
freq=1000.0*t2.toDouble();
|
|
|
|
return freq;
|
|
|
|
} else {
|
|
|
|
return -1.0;
|
|
|
|
}
|
2013-05-27 16:27:36 -04:00
|
|
|
} else
|
2013-05-21 17:36:34 -04:00
|
|
|
#endif
|
2013-07-08 09:17:22 -04:00
|
|
|
{
|
2013-07-23 13:32:59 -04:00
|
|
|
freq=-1.0;
|
|
|
|
for(int i=0; i<NUMTRIES; i++) {
|
|
|
|
int iret=rig_get_freq(theRig, vfo, &freq);
|
|
|
|
if(iret==RIG_OK) break;
|
|
|
|
}
|
2013-05-21 15:49:41 -04:00
|
|
|
return freq;
|
|
|
|
}
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:55:13 -04:00
|
|
|
int Rig::setMode(rmode_t mode, pbwidth_t width, vfo_t vfo) {
|
|
|
|
return rig_set_mode(theRig, vfo, mode, width);
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rmode_t Rig::getMode(pbwidth_t& width, vfo_t vfo) {
|
|
|
|
rmode_t mode;
|
2013-04-30 15:55:13 -04:00
|
|
|
rig_get_mode(theRig, vfo, &mode, &width);
|
2013-04-22 20:52:51 -04:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:55:13 -04:00
|
|
|
int Rig::setPTT(ptt_t ptt, vfo_t vfo)
|
|
|
|
{
|
2013-05-27 16:27:36 -04:00
|
|
|
#ifdef WIN32 // Ham Radio Deluxe only on Windows
|
2013-05-23 10:52:51 -04:00
|
|
|
if(m_hrd) {
|
2013-05-23 18:00:12 -04:00
|
|
|
wchar_t* cmnd;
|
2013-05-27 16:27:36 -04:00
|
|
|
|
2013-05-23 18:00:12 -04:00
|
|
|
if(ptt==0) {
|
|
|
|
cmnd = (wchar_t*) (m_context +
|
|
|
|
"Set Button-Select TX 0").utf16();
|
|
|
|
} else {
|
|
|
|
cmnd = (wchar_t*) (m_context +
|
|
|
|
"Set Button-Select TX 1").utf16();
|
|
|
|
}
|
2013-05-23 10:52:51 -04:00
|
|
|
const wchar_t* result=HRDInterfaceSendMessage(cmnd);
|
|
|
|
QString t2=QString::fromWCharArray (result,-1);
|
|
|
|
HRDInterfaceFreeString(result);
|
|
|
|
if(t2=="OK") {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-08 09:17:22 -04:00
|
|
|
} else if(m_cmndr) {
|
|
|
|
QString t;
|
|
|
|
if(ptt==0) t="<command:5>CmdRX<parameters:0>";
|
|
|
|
if(ptt>0) t="<command:5>CmdTX<parameters:0>";
|
|
|
|
QByteArray ba = t.toLocal8Bit();
|
|
|
|
const char* buf=ba.data();
|
2013-07-23 13:32:59 -04:00
|
|
|
commanderSocket->write(buf);
|
|
|
|
commanderSocket->waitForBytesWritten(1000);
|
2013-07-08 09:17:22 -04:00
|
|
|
return 0;
|
2013-05-27 16:27:36 -04:00
|
|
|
} else
|
2013-05-23 10:52:51 -04:00
|
|
|
#endif
|
2013-05-27 16:27:36 -04:00
|
|
|
{
|
2013-05-23 10:52:51 -04:00
|
|
|
return rig_set_ptt(theRig, vfo, ptt);
|
|
|
|
}
|
2013-04-30 15:55:13 -04:00
|
|
|
}
|
2013-04-22 20:52:51 -04:00
|
|
|
|
|
|
|
ptt_t Rig::getPTT(vfo_t vfo)
|
|
|
|
{
|
|
|
|
ptt_t ptt;
|
2013-04-30 15:55:13 -04:00
|
|
|
rig_get_ptt(theRig, vfo, &ptt);
|
2013-04-22 20:52:51 -04:00
|
|
|
return ptt;
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:55:13 -04:00
|
|
|
token_t Rig::tokenLookup(const char *name)
|
2013-04-22 20:52:51 -04:00
|
|
|
{
|
2013-04-30 15:55:13 -04:00
|
|
|
return rig_token_lookup(theRig, name);
|
2013-04-22 20:52:51 -04:00
|
|
|
}
|