[go: up one dir, main page]

Menu

[c11636]: / src / mem.c  Maximize  Restore  History

Download this file

526 lines (432 with data), 13.9 kB

  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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* \file src/mem.c
* \ingroup rig
* \brief Memory and channel interface
* \author Stephane Fillod
* \date 2000-2003
*
* Hamlib interface is a frontend implementing wrapper functions.
*/
/*
* Hamlib Interface - mem/channel calls
* Copyright (c) 2000-2003 by Stephane Fillod
*
* $Id: mem.c,v 1.2 2003-04-06 18:40:35 fillods Exp $
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <hamlib/rig.h>
#ifndef DOC_HIDDEN
#define CHECK_RIG_ARG(r) (!(r) || !(r)->caps || !(r)->state.comm_state)
#endif /* !DOC_HIDDEN */
/**
* \brief set the current memory channel number
* \param rig The rig handle
* \param vfo The target VFO
* \param ch The memory channel number
*
* Sets the current memory channel number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem()
*/
int rig_set_mem(RIG *rig, vfo_t vfo, int ch)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (caps->set_mem == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_ALL) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->set_mem(rig, vfo, ch);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->set_mem(rig, vfo, ch);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
/**
* \brief get the current memory channel number
* \param rig The rig handle
* \param vfo The target VFO
* \param ch The location where to store the current memory channel number
*
* Retrieves the current memory channel number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_mem()
*/
int rig_get_mem(RIG *rig, vfo_t vfo, int *ch)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig) || !ch)
return -RIG_EINVAL;
caps = rig->caps;
if (caps->get_mem == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_ALL) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->get_mem(rig, vfo, ch);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->get_mem(rig, vfo, ch);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
/**
* \brief set the current memory bank
* \param rig The rig handle
* \param vfo The target VFO
* \param bank The memory bank
*
* Sets the current memory bank number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_mem()
*/
int rig_set_bank(RIG *rig, vfo_t vfo, int bank)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (caps->set_bank == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_ALL) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->set_bank(rig, vfo, bank);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->set_bank(rig, vfo, bank);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
#ifndef DOC_HIDDEN
/*
* call on every ext_levels of a rig
*/
static int generic_retr_extl(RIG *rig, const struct confparams *cfp, rig_ptr_t ptr)
{
channel_t *chan = (channel_t *)ptr;
struct ext_list *p;
unsigned el_size = 0;
if (chan->ext_levels == NULL)
p = chan->ext_levels = malloc(2*sizeof(struct ext_list));
else {
for (p = chan->ext_levels; !RIG_IS_EXT_END(*p); p++)
el_size += sizeof(struct ext_list);
chan->ext_levels = realloc(chan->ext_levels,
el_size+sizeof(struct ext_list));
}
p->token = cfp->token;
rig_get_ext_level(rig, RIG_VFO_CURR, p->token, &p->val);
p++;
p->token = 0; /* RIG_EXT_END */
return 1; /* process them all */
}
/*
* stores current VFO state into chan by emulating rig_get_channel
*/
int generic_save_channel(RIG *rig, channel_t *chan)
{
int i;
int chan_num;
vfo_t vfo;
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
chan_num = chan->channel_num;
vfo = chan->vfo;
memset(chan, 0, sizeof(channel_t));
chan->channel_num = chan_num;
chan->vfo = vfo;
rig_get_vfo(rig, &chan->vfo);
rig_get_freq(rig, RIG_VFO_CURR, &chan->freq);
rig_get_mode(rig, RIG_VFO_CURR, &chan->mode, &chan->width);
chan->split = RIG_SPLIT_OFF;
rig_get_split_vfo(rig, RIG_VFO_CURR, &chan->split, &chan->tx_vfo);
if (chan->split != RIG_SPLIT_OFF) {
rig_get_split_freq(rig, RIG_VFO_CURR, &chan->tx_freq);
rig_get_split_mode(rig, RIG_VFO_CURR, &chan->tx_mode, &chan->tx_width);
} else {
chan->tx_freq = chan->freq;
chan->tx_mode = chan->mode;
chan->tx_width = chan->width;
}
rig_get_rptr_shift(rig, RIG_VFO_CURR, &chan->rptr_shift);
rig_get_rptr_offs(rig, RIG_VFO_CURR, &chan->rptr_offs);
rig_get_ant(rig, RIG_VFO_CURR, &chan->ant);
rig_get_ts(rig, RIG_VFO_CURR, &chan->tuning_step);
rig_get_rit(rig, RIG_VFO_CURR, &chan->rit);
rig_get_xit(rig, RIG_VFO_CURR, &chan->xit);
for (i=0; i<RIG_SETTING_MAX; i++)
if (RIG_LEVEL_SET(rig_idx2setting(i)))
rig_get_level(rig, RIG_VFO_CURR, rig_idx2setting(i), &chan->levels[i]);
chan->funcs = 0;
for (i=0; i<RIG_SETTING_MAX; i++) {
int fstatus;
if (rig_get_func(rig, RIG_VFO_CURR, rig_idx2setting(i), &fstatus) == RIG_OK)
chan->funcs |= fstatus ? rig_idx2setting(i) : 0;
}
rig_get_ctcss_tone(rig, RIG_VFO_CURR, &chan->ctcss_tone);
rig_get_ctcss_sql(rig, RIG_VFO_CURR, &chan->ctcss_sql);
rig_get_dcs_code(rig, RIG_VFO_CURR, &chan->dcs_code);
rig_get_dcs_sql(rig, RIG_VFO_CURR, &chan->dcs_sql);
/* rig_get_mem_name(rig, RIG_VFO_CURR, chan->channel_desc); */
rig_ext_level_foreach(rig, generic_retr_extl, (rig_ptr_t)chan);
return RIG_OK;
}
/*
* Restores chan into current VFO state by emulating rig_set_channel
*/
int generic_restore_channel(RIG *rig, const channel_t *chan)
{
int i;
struct ext_list *p;
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
rig_set_vfo(rig, chan->vfo);
rig_set_freq(rig, RIG_VFO_CURR, chan->freq);
rig_set_mode(rig, RIG_VFO_CURR, chan->mode, chan->width);
rig_set_split_vfo(rig, RIG_VFO_CURR, chan->split, chan->tx_vfo);
if (chan->split != RIG_SPLIT_OFF) {
rig_set_split_freq(rig, RIG_VFO_CURR, chan->tx_freq);
rig_set_split_mode(rig, RIG_VFO_CURR, chan->tx_mode, chan->tx_width);
}
rig_set_rptr_shift(rig, RIG_VFO_CURR, chan->rptr_shift);
rig_set_rptr_offs(rig, RIG_VFO_CURR, chan->rptr_offs);
for (i=0; i<RIG_SETTING_MAX; i++)
rig_set_level(rig, RIG_VFO_CURR, rig_idx2setting(i), chan->levels[i]);
rig_set_ant(rig, RIG_VFO_CURR, chan->ant);
rig_set_ts(rig, RIG_VFO_CURR, chan->tuning_step);
rig_set_rit(rig, RIG_VFO_CURR, chan->rit);
rig_set_xit(rig, RIG_VFO_CURR, chan->xit);
for (i=0; i<RIG_SETTING_MAX; i++)
rig_set_func(rig, RIG_VFO_CURR, rig_idx2setting(i),
chan->funcs & rig_idx2setting(i));
rig_set_ctcss_tone(rig, RIG_VFO_CURR, chan->ctcss_tone);
rig_set_ctcss_sql(rig, RIG_VFO_CURR, chan->ctcss_sql);
rig_set_dcs_code(rig, RIG_VFO_CURR, chan->dcs_code);
rig_set_dcs_sql(rig, RIG_VFO_CURR, chan->dcs_sql);
/* rig_set_mem_name(rig, RIG_VFO_CURR, chan->channel_desc); */
for (p = chan->ext_levels; !RIG_IS_EXT_END(*p); p++)
rig_set_ext_level(rig, RIG_VFO_CURR, p->token, p->val);
return RIG_OK;
}
#endif /* !DOC_HIDDEN */
/**
* \brief set channel data
* \param rig The rig handle
* \param chan The location of data to set for this channel
*
* Sets the data associated with a channel. This channel can either
* be the state of a VFO specified by \a chan->vfo, or a memory channel
* specified with \a chan->vfo = RIG_VFO_MEM and \a chan->channel_num.
* See #channel_t for more information.
*
* The rig_set_channel is supposed to have no impact on the current VFO
* and memory number selected. Depending on backend and rig capabilities,
* the chan struct may not be set completely.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_channel()
*/
int rig_set_channel(RIG *rig, const channel_t *chan)
{
struct rig_caps *rc;
int curr_chan_num, get_mem_status = RIG_OK;
vfo_t curr_vfo;
vfo_t vfo; /* requested vfo */
int retcode;
#ifdef PARANOID_CHANNEL_HANDLING
channel_t curr_chan;
#endif
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
/*
* TODO: check chan->channel_num is valid
*/
rc = rig->caps;
if (rc->set_channel)
return rc->set_channel(rig, chan);
/*
* if not available, emulate it
* Optional: get_vfo, set_vfo,
* TODO: check return codes
*/
vfo = chan->vfo;
if (vfo == RIG_VFO_MEM && !rc->set_mem)
return -RIG_ENAVAIL;
if (vfo == RIG_VFO_CURR)
return generic_restore_channel(rig, chan);
if (!rc->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
/* may be needed if the restore_channel has some side effects */
#ifdef PARANOID_CHANNEL_HANDLING
generic_save_channel(rig, &curr_chan);
#endif
if (vfo == RIG_VFO_MEM)
get_mem_status = rig_get_mem(rig, RIG_VFO_CURR, &curr_chan_num);
retcode = rig_set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
if (vfo == RIG_VFO_MEM)
rig_set_mem(rig, RIG_VFO_CURR, chan->channel_num);
retcode = generic_restore_channel(rig, chan);
/* restore current memory number */
if (vfo == RIG_VFO_MEM && get_mem_status == RIG_OK)
rig_set_mem(rig, RIG_VFO_CURR, curr_chan_num);
rig_set_vfo(rig, curr_vfo);
#ifdef PARANOID_CHANNEL_HANDLING
generic_restore_channel(rig, &curr_chan);
#endif
return retcode;
}
/**
* \brief get channel data
* \param rig The rig handle
* \param chan The location where to store the channel data
*
* Retrieves the data associated with a channel. This channel can either
* be the state of a VFO specified by \a chan->vfo, or a memory channel
* specified with \a chan->vfo = RIG_VFO_MEM and \a chan->channel_num.
* See #channel_t for more information.
*
* Example:
\code
channel_t chan;
int err;
chan->vfo = RIG_VFO_MEM;
chan->channel_num = 10;
err = rig_get_channel(rig, &chan);
if (err != RIG_OK)
error("get_channel failed: %s", rigerror(err));
\endcode
*
* The rig_get_channel is supposed to have no impact on the current VFO
* and memory number selected. Depending on backend and rig capabilities,
* the chan struct may not be filled in completely.
*
* Note: chan->ext_levels is a pointer to a newly mallocated memory.
* This is the responsability of the caller to manage and eventually
* free it.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_channel()
*/
int rig_get_channel(RIG *rig, channel_t *chan)
{
struct rig_caps *rc;
int curr_chan_num, get_mem_status = RIG_OK;
vfo_t curr_vfo;
vfo_t vfo; /* requested vfo */
int retcode;
#ifdef PARANOID_CHANNEL_HANDLING
channel_t curr_chan;
#endif
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
/*
* TODO: check chan->channel_num is valid
*/
rc = rig->caps;
if (rc->get_channel)
return rc->get_channel(rig, chan);
/*
* if not available, emulate it
* Optional: get_vfo, set_vfo
* TODO: check return codes
*/
vfo = chan->vfo;
if (vfo == RIG_VFO_MEM && !rc->set_mem)
return -RIG_ENAVAIL;
if (vfo == RIG_VFO_CURR)
return generic_save_channel(rig, chan);
if (!rc->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
/* may be needed if the restore_channel has some side effects */
#ifdef PARANOID_CHANNEL_HANDLING
generic_save_channel(rig, &curr_chan);
#endif
if (vfo == RIG_VFO_MEM)
get_mem_status = rig_get_mem(rig, RIG_VFO_CURR, &curr_chan_num);
retcode = rig_set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
if (vfo == RIG_VFO_MEM)
rig_set_mem(rig, RIG_VFO_CURR, chan->channel_num);
retcode = generic_save_channel(rig, chan);
/* restore current memory number */
if (vfo == RIG_VFO_MEM && get_mem_status == RIG_OK)
rig_set_mem(rig, RIG_VFO_CURR, curr_chan_num);
rig_set_vfo(rig, curr_vfo);
#ifdef PARANOID_CHANNEL_HANDLING
generic_restore_channel(rig, &curr_chan);
#endif
return retcode;
}