File: rigclass.cpp

package info (click to toggle)
wsjtx 1.1.r3496-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,988 kB
  • ctags: 1,377
  • sloc: cpp: 6,647; f90: 5,319; ansic: 1,266; fortran: 108; makefile: 17; sh: 10
file content (324 lines) | stat: -rw-r--r-- 7,962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 * \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.
 */

/**
 *
 *  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>
#include "rigclass.h"
#include <QDebug>
#include <QHostAddress>

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);
}

Rig::Rig()
{
  rig_set_debug_level( RIG_DEBUG_WARN);
}

Rig::~Rig() {
  theRig->state.obj = NULL;
  rig_cleanup(theRig);
  caps = NULL;
}

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;
}

int Rig::open(int n) {
  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;
    bool bConnect=false;
    bConnect = HRDInterfaceConnect(L"localhost",7809);
    if(bConnect) {
      const wchar_t* context=HRDInterfaceSendMessage(L"Get Context");
      m_context="[" + QString::fromWCharArray (context,-1) + "] ";
      HRDInterfaceFreeString(context);
      return 0;
    } else {
      m_hrd=false;
      return -1;
    }
  }
  if(n==9998) {
    if(socket->state()==QAbstractSocket::ConnectedState) {
      socket->abort();
    }

    if(socket->state()==QAbstractSocket::UnconnectedState) {
      socket->connectToHost(QHostAddress::LocalHost, 52002);
      if(!socket->waitForConnected(1000)) {
        return -1;
      }
    }
    QString t;
    t="<command:10>CmdGetFreq<parameters:0>";
    QByteArray ba = t.toLocal8Bit();
    const char* buf=ba.data();
    socket->write(buf);
    socket->waitForReadyRead(1000);
    QByteArray reply=socket->read(128);
    if(reply.indexOf("<CmdFreq:")==0) {
      m_cmndr=true;
      return 0;
    }
  }
#endif
  return -1;
}

int Rig::close(void) {
#ifdef WIN32	// Ham Radio Deluxe only on Windows
  if(m_hrd) {
    HRDInterfaceDisconnect();
    return 0;
  } else if(m_cmndr) {
    socket->close();
    return 0;
  } else
#endif
    {
    return rig_close(theRig);
  }
}

int Rig::setConf(const char *name, const char *val)
{
  return rig_set_conf(theRig, tokenLookup(name), val);
}

int Rig::setFreq(freq_t freq, vfo_t vfo) {
#ifdef WIN32	// Ham Radio Deluxe (only on Windows)
  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);
    HRDInterfaceFreeString(result);
    if(t2=="OK") {
      return 0;
    } else {
      return -1;
    }
  } else if(m_cmndr) {
    QString t;
    double f=0.001*freq;
    t.sprintf("<command:10>CmdSetFreq<parameters:23><xcvrfreq:10>%10.3f",f);
    QByteArray ba = t.toLocal8Bit();
    const char* buf=ba.data();
    socket->write(buf);
    socket->waitForBytesWritten(1000);
    return 0;
  } else
#endif
  {
    return rig_set_freq(theRig, vfo, freq);
  }
}

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;
    double f=0.001*tx_freq;
    t.sprintf("<command:12>CmdSetTxFreq<parameters:23><xcvrfreq:10>%10.3f",f);
    QByteArray ba = t.toLocal8Bit();
    const char* buf=ba.data();
    socket->write(buf);
    socket->waitForBytesWritten(1000);
    return 0;
  } else
#endif
  {
    return rig_set_split_freq(theRig, vfo, tx_freq);
  }
}

freq_t Rig::getFreq(vfo_t vfo)
{
  freq_t freq;
#ifdef WIN32	// Ham Radio Deluxe (only on Windows)
  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;
  } else if(m_cmndr) {
    QString t;
    t="<command:10>CmdGetFreq<parameters:0>";
    QByteArray ba = t.toLocal8Bit();
    const char* buf=ba.data();
    socket->write(buf);
    socket->waitForReadyRead(1000);
    QByteArray reply=socket->read(128);
    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;
    }
  } else
#endif
  {
    int iret=rig_get_freq(theRig, vfo, &freq);
// iret should be 0.  Negative values mean rig_get_freq() failed.
    if(iret<0) freq=-1.0;
    return freq;
  }
}

int Rig::setMode(rmode_t mode, pbwidth_t width, vfo_t vfo) {
  return rig_set_mode(theRig, vfo, mode, width);
}

rmode_t Rig::getMode(pbwidth_t& width, vfo_t vfo) {
  rmode_t mode;
  rig_get_mode(theRig, vfo, &mode, &width);
  return mode;
}

int Rig::setPTT(ptt_t ptt, vfo_t vfo)
{
#ifdef WIN32	// Ham Radio Deluxe only on Windows
  if(m_hrd) {
    wchar_t* cmnd;

    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();
    }
    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;
    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();
    socket->write(buf);
    socket->waitForBytesWritten(1000);
    return 0;
  } else
#endif
    {
    return rig_set_ptt(theRig, vfo, ptt);
  }
}

ptt_t Rig::getPTT(vfo_t vfo)
{
  ptt_t ptt;
  rig_get_ptt(theRig, vfo, &ptt);
  return ptt;
}

token_t Rig::tokenLookup(const char *name)
{
  return rig_token_lookup(theRig, name);
}