forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rig.c
3271 lines (2763 loc) · 83.4 KB
/
rig.c
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Hamlib Interface - main file
* Copyright (c) 2000-2012 by Stephane Fillod
* Copyright (c) 2000-2003 by Frank Singleton
*
*
* 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
*
*/
/**
* \addtogroup rig
* @{
*/
/**
* \file src/rig.c
* \brief Ham Radio Control Libraries interface
* \author Stephane Fillod
* \author Frank Singleton
* \date 2000-2012
*
* Hamlib provides a user-callable API, a set of "front-end" routines that
* call rig-specific "back-end" routines which actually communicate with
* the physical rig.
*/
/*! \page rig Rig (radio) interface
*
* For us, a "rig" is an item of general remote controllable radio equipment.
* Generally, there are a VFO settings, gain controls, etc.
*/
/**
* \example ../tests/testrig.c
*/
#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"
#include "serial.h"
#include "parallel.h"
#include "usb_port.h"
#include "network.h"
#include "event.h"
#include "cm108.h"
#include "gpio.h"
/**
* \brief Hamlib release number
* The version number has the format x.y.z
*/
/*
* Careful: The hamlib 1.2 ABI implicitly specifies a size of 21 bytes for
* the hamlib_version string. Changing the size provokes a warning from the
* dynamic loader.
*/
const char hamlib_version[21] = "Hamlib " PACKAGE_VERSION;
/**
* \brief Hamlib copyright notice
*/
const char hamlib_copyright[231] = /* hamlib 1.2 ABI specifies 231 bytes */
"Copyright (C) 2000-2012 Stephane Fillod\n"
"Copyright (C) 2000-2003 Frank Singleton\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
#ifndef DOC_HIDDEN
#if defined(WIN32) && !defined(__CYGWIN__)
#define DEFAULT_SERIAL_PORT "\\\\.\\COM1"
#elif BSD
#define DEFAULT_SERIAL_PORT "/dev/cuaa0"
#elif MACOSX
#define DEFAULT_SERIAL_PORT "/dev/cu.usbserial"
#else
#define DEFAULT_SERIAL_PORT "/dev/ttyS0"
#endif
#if defined(WIN32)
#define DEFAULT_PARALLEL_PORT "\\\\.\\$VDMLPT1"
#elif defined(HAVE_DEV_PPBUS_PPI_H)
#define DEFAULT_PARALLEL_PORT "/dev/ppi0"
#else
#define DEFAULT_PARALLEL_PORT "/dev/parport0"
#endif
#if defined(WIN32) && !defined(__CYGWIN__)
#define DEFAULT_CM108_PORT "fixme"
#elif BSD
#define DEFAULT_CM108_PORT "fixme"
#else
#define DEFAULT_CM108_PORT "/dev/hidraw0"
#endif
#if defined(WIN32) && !defined(__CYGWIN__)
/* FIXME: Determine correct GPIO bit number for W32 using MinGW. */
#define DEFAULT_CM108_PTT_BITNUM 2
#elif BSD
/* FIXME: Determine correct GPIO bit number for *BSD. */
#define DEFAULT_CM108_PTT_BITNUM 2
#else
#define DEFAULT_CM108_PTT_BITNUM 2
#endif
#define DEFAULT_GPIO_PORT "0"
#define CHECK_RIG_ARG(r) (!(r) || !(r)->caps || !(r)->state.comm_state)
/*
* Data structure to track the opened rig (by rig_open)
*/
struct opened_rig_l {
RIG *rig;
struct opened_rig_l *next;
};
static struct opened_rig_l *opened_rig_list = { NULL };
/*
* Careful, the order must be the same as their RIG_E* counterpart!
* TODO: localise the messages..
*/
static const char *rigerror_table[] = {
"Command completed successfully",
"Invalid parameter",
"Invalid configuration",
"Memory shortage",
"Feature not implemented",
"Communication timed out",
"IO error",
"Internal Hamlib error",
"Protocol error",
"Command rejected by the rig",
"Command performed, but arg truncated, result not guaranteed",
"Feature not available",
"Target VFO unaccessible",
"Communication bus error",
"Communication bus collision",
"NULL RIG handle or invalid pointer parameter",
"Invalid VFO",
"Argument out of domain of func",
NULL,
};
#define ERROR_TBL_SZ (sizeof(rigerror_table)/sizeof(char *))
/*
* track which rig is opened (with rig_open)
* needed at least for transceive mode
*/
static int add_opened_rig(RIG *rig)
{
struct opened_rig_l *p;
p = (struct opened_rig_l *)malloc(sizeof(struct opened_rig_l));
if (!p)
return -RIG_ENOMEM;
p->rig = rig;
p->next = opened_rig_list;
opened_rig_list = p;
return RIG_OK;
}
static int remove_opened_rig(RIG *rig)
{
struct opened_rig_l *p,*q;
q = NULL;
for (p=opened_rig_list; p; p=p->next) {
if (p->rig == rig) {
if (q == NULL) {
opened_rig_list = opened_rig_list->next;
} else {
q->next = p->next;
}
free(p);
return RIG_OK;
}
q = p;
}
return -RIG_EINVAL; /* Not found in list ! */
}
/**
* \brief execs cfunc() on each opened rig
* \param cfunc The function to be executed on each rig
* \param data Data pointer to be passed to cfunc()
*
* Calls cfunc() function for each opened rig.
* The contents of the opened rig table
* is processed in random order according to a function
* pointed to by \a cfunc, whic is called with two arguments,
* the first pointing to the RIG handle, the second
* to a data pointer \a data.
* If \a data is not needed, then it can be set to NULL.
* The processing of the opened rig table is stopped
* when cfunc() returns 0.
* \internal
*
* \return always RIG_OK.
*/
int foreach_opened_rig(int (*cfunc)(RIG *, rig_ptr_t), rig_ptr_t data)
{
struct opened_rig_l *p;
for (p=opened_rig_list; p; p=p->next) {
if ((*cfunc)(p->rig,data) == 0)
return RIG_OK;
}
return RIG_OK;
}
#endif /* !DOC_HIDDEN */
/**
* \brief get string describing the error code
* \param errnum The error code
* \return the appropriate description string, otherwise a NULL pointer
* if the error code is unknown.
*
* Returns a string describing the error code passed in the argument \a errnum.
*
* \todo support gettext/localization
*/
const char * HAMLIB_API rigerror(int errnum)
{
errnum = abs(errnum);
if (errnum > ERROR_TBL_SZ)
return NULL;
return rigerror_table[errnum];
}
/**
* \brief allocate a new RIG handle
* \param rig_model The rig model for this new handle
*
* Allocates a new RIG handle and initializes the associated data
* for \a rig_model.
*
* \return a pointer to the #RIG handle otherwise NULL if memory allocation
* failed or \a rig_model is unknown (e.g. backend autoload failed).
*
* \sa rig_cleanup(), rig_open()
*/
RIG * HAMLIB_API rig_init(rig_model_t rig_model)
{
RIG *rig;
const struct rig_caps *caps;
struct rig_state *rs;
int i, retcode;
rig_debug(RIG_DEBUG_VERBOSE,"rig:rig_init called \n");
rig_check_backend(rig_model);
caps = rig_get_caps(rig_model);
if (!caps)
return NULL;
/*
* okay, we've found it. Allocate some memory and set it to zeros,
* and especially the callbacks
*/
rig = calloc(1, sizeof(RIG));
if (rig == NULL) {
/*
* FIXME: how can the caller know it's a memory shortage,
* and not "rig not found" ?
*/
return NULL;
}
/* caps is const, so we need to tell compiler
that we now what we are doing */
rig->caps = (struct rig_caps *) caps;
/*
* populate the rig->state
* TODO: read the Preferences here!
*/
rs = &rig->state;
rs->comm_state = 0;
rs->rigport.type.rig = caps->port_type; /* default from caps */
switch (caps->port_type) {
case RIG_PORT_SERIAL:
strncpy(rs->rigport.pathname, DEFAULT_SERIAL_PORT, FILPATHLEN - 1);
rs->rigport.parm.serial.rate = caps->serial_rate_max; /* fastest ! */
rs->rigport.parm.serial.data_bits = caps->serial_data_bits;
rs->rigport.parm.serial.stop_bits = caps->serial_stop_bits;
rs->rigport.parm.serial.parity = caps->serial_parity;
rs->rigport.parm.serial.handshake = caps->serial_handshake;
break;
case RIG_PORT_PARALLEL:
strncpy(rs->rigport.pathname, DEFAULT_PARALLEL_PORT, FILPATHLEN - 1);
break;
/* Adding support for CM108 GPIO. This is compatible with CM108 series
* USB audio chips from CMedia and SSS1623 series USB audio chips from 3S
*/
case RIG_PORT_CM108:
strncpy(rs->rigport.pathname, DEFAULT_CM108_PORT, FILPATHLEN);
rs->rigport.parm.cm108.ptt_bitnum = DEFAULT_CM108_PTT_BITNUM;
break;
case RIG_PORT_GPIO:
strncpy(rs->rigport.pathname, DEFAULT_GPIO_PORT, FILPATHLEN);
break;
case RIG_PORT_NETWORK:
case RIG_PORT_UDP_NETWORK:
strncpy(rs->rigport.pathname, "127.0.0.1:4532", FILPATHLEN - 1);
break;
default:
strncpy(rs->rigport.pathname, "", FILPATHLEN - 1);
}
rs->rigport.write_delay = caps->write_delay;
rs->rigport.post_write_delay = caps->post_write_delay;
rs->rigport.timeout = caps->timeout;
rs->rigport.retry = caps->retry;
rs->pttport.type.ptt = caps->ptt_type;
rs->dcdport.type.dcd = caps->dcd_type;
rs->vfo_comp = 0.0; /* override it with preferences */
rs->current_vfo = RIG_VFO_CURR; /* we don't know yet! */
rs->tx_vfo = RIG_VFO_CURR; /* we don't know yet! */
rs->transceive = RIG_TRN_OFF;
rs->poll_interval = 500;
/* should it be a parameter to rig_init ? --SF */
rs->itu_region = RIG_ITU_REGION2;
switch(rs->itu_region) {
case RIG_ITU_REGION1:
memcpy(rs->tx_range_list, caps->tx_range_list1,
sizeof(struct freq_range_list)*FRQRANGESIZ);
memcpy(rs->rx_range_list, caps->rx_range_list1,
sizeof(struct freq_range_list)*FRQRANGESIZ);
break;
case RIG_ITU_REGION2:
case RIG_ITU_REGION3:
default:
memcpy(rs->tx_range_list, caps->tx_range_list2,
sizeof(struct freq_range_list)*FRQRANGESIZ);
memcpy(rs->rx_range_list, caps->rx_range_list2,
sizeof(struct freq_range_list)*FRQRANGESIZ);
break;
}
rs->vfo_list = 0;
rs->mode_list = 0;
for (i=0; i<FRQRANGESIZ && !RIG_IS_FRNG_END(rs->rx_range_list[i]); i++) {
rs->vfo_list |= rs->rx_range_list[i].vfo;
rs->mode_list |= rs->rx_range_list[i].modes;
}
for (i=0; i<FRQRANGESIZ && !RIG_IS_FRNG_END(rs->tx_range_list[i]); i++) {
rs->vfo_list |= rs->tx_range_list[i].vfo;
rs->mode_list |= rs->tx_range_list[i].modes;
}
memcpy(rs->preamp, caps->preamp, sizeof(int)*MAXDBLSTSIZ);
memcpy(rs->attenuator, caps->attenuator, sizeof(int)*MAXDBLSTSIZ);
memcpy(rs->tuning_steps, caps->tuning_steps,
sizeof(struct tuning_step_list)*TSLSTSIZ);
memcpy(rs->filters, caps->filters,
sizeof(struct filter_list)*FLTLSTSIZ);
memcpy(&rs->str_cal, &caps->str_cal,
sizeof(cal_table_t));
memcpy(rs->chan_list, caps->chan_list, sizeof(chan_t)*CHANLSTSIZ);
rs->has_get_func = caps->has_get_func;
rs->has_set_func = caps->has_set_func;
rs->has_get_level = caps->has_get_level;
rs->has_set_level = caps->has_set_level;
rs->has_get_parm = caps->has_get_parm;
rs->has_set_parm = caps->has_set_parm;
/* emulation by frontend */
if ((caps->has_get_level & RIG_LEVEL_STRENGTH) == 0 &&
(caps->has_get_level & RIG_LEVEL_RAWSTR) == RIG_LEVEL_RAWSTR)
rs->has_get_level |= RIG_LEVEL_STRENGTH;
memcpy(rs->level_gran, caps->level_gran, sizeof(gran_t)*RIG_SETTING_MAX);
memcpy(rs->parm_gran, caps->parm_gran, sizeof(gran_t)*RIG_SETTING_MAX);
rs->max_rit = caps->max_rit;
rs->max_xit = caps->max_xit;
rs->max_ifshift = caps->max_ifshift;
rs->announces = caps->announces;
rs->rigport.fd = rs->pttport.fd = rs->dcdport.fd = -1;
/*
* let the backend a chance to setup his private data
* This must be done only once defaults are setup,
* so the backend init can override rig_state.
*/
if (caps->rig_init != NULL) {
retcode = caps->rig_init(rig);
if (retcode != RIG_OK) {
rig_debug(RIG_DEBUG_VERBOSE,"rig:backend_init failed!\n");
/* cleanup and exit */
free(rig);
return NULL;
}
}
return rig;
}
/**
* \brief open the communication to the rig
* \param rig The #RIG handle of the radio to be opened
*
* Opens communication to a radio which \a RIG handle has been passed
* by argument.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \retval RIG_EINVAL \a rig is NULL or unconsistent.
* \retval RIG_ENIMPL port type communication is not implemented yet.
*
* \sa rig_init(), rig_close()
*/
int HAMLIB_API rig_open(RIG *rig)
{
const struct rig_caps *caps;
struct rig_state *rs;
int status = RIG_OK;
rig_debug(RIG_DEBUG_VERBOSE,"rig:rig_open called \n");
if (!rig || !rig->caps)
return -RIG_EINVAL;
caps = rig->caps;
rs = &rig->state;
if (rs->comm_state)
return -RIG_EINVAL;
rs->rigport.fd = -1;
if (rs->rigport.type.rig == RIG_PORT_SERIAL)
{
if (rs->rigport.parm.serial.rts_state != RIG_SIGNAL_UNSET &&
rs->rigport.parm.serial.handshake == RIG_HANDSHAKE_HARDWARE)
{
rig_debug(RIG_DEBUG_ERR, "Cannot set RTS with hardware handshake \"%s\"\n",
rs->rigport.pathname);
return -RIG_ECONF;
}
if ('\0' == rs->pttport.pathname[0]
|| !strcmp(rs->pttport.pathname, rs->rigport.pathname))
{
/* check for control line conflicts */
if (rs->rigport.parm.serial.rts_state != RIG_SIGNAL_UNSET &&
rs->pttport.type.ptt == RIG_PTT_SERIAL_RTS)
{
rig_debug(RIG_DEBUG_ERR, "Cannot set RTS with PTT by RTS \"%s\"\n",
rs->rigport.pathname);
return -RIG_ECONF;
}
if (rs->rigport.parm.serial.dtr_state != RIG_SIGNAL_UNSET &&
rs->pttport.type.ptt == RIG_PTT_SERIAL_DTR)
{
rig_debug(RIG_DEBUG_ERR, "Cannot set DTR with PTT by DTR \"%s\"\n",
rs->rigport.pathname);
return -RIG_ECONF;
}
}
}
status = port_open(&rs->rigport);
if (status < 0)
return status;
switch(rs->pttport.type.ptt) {
case RIG_PTT_NONE:
case RIG_PTT_RIG:
case RIG_PTT_RIG_MICDATA:
break;
case RIG_PTT_SERIAL_RTS:
case RIG_PTT_SERIAL_DTR:
if (rs->pttport.pathname[0] == '\0' &&
rs->rigport.type.rig == RIG_PORT_SERIAL)
strcpy(rs->pttport.pathname, rs->rigport.pathname);
if (!strcmp(rs->pttport.pathname, rs->rigport.pathname))
{
rs->pttport.fd = rs->rigport.fd;
/* Needed on Linux because the serial port driver sets RTS/DTR
on open - only need to address the PTT line as we offer
config parameters to control the other (dtr_state &
rts_state) */
if (rs->pttport.type.ptt == RIG_PTT_SERIAL_DTR)
status = ser_set_dtr(&rs->pttport, 0);
if (rs->pttport.type.ptt == RIG_PTT_SERIAL_RTS)
status = ser_set_rts(&rs->pttport, 0);
}
else
{
rs->pttport.fd = ser_open(&rs->pttport);
if (rs->pttport.fd < 0)
{
rig_debug(RIG_DEBUG_ERR, "Cannot open PTT device \"%s\"\n",
rs->pttport.pathname);
status = -RIG_EIO;
}
if (RIG_OK == status
&& (rs->pttport.type.ptt == RIG_PTT_SERIAL_DTR
|| rs->pttport.type.ptt == RIG_PTT_SERIAL_RTS))
{
/* Needed on Linux because the serial port driver sets
RTS/DTR high on open - set both low since we offer no
control of the non-PTT line and low is better than
high */
status = ser_set_dtr(&rs->pttport, 0);
if (RIG_OK == status)
{
status = ser_set_rts(&rs->pttport, 0);
}
}
}
break;
case RIG_PTT_PARALLEL:
rs->pttport.fd = par_open(&rs->pttport);
if (rs->pttport.fd < 0)
{
rig_debug(RIG_DEBUG_ERR, "Cannot open PTT device \"%s\"\n",
rs->pttport.pathname);
status = -RIG_EIO;
}
else
par_ptt_set(&rs->pttport, RIG_PTT_OFF);
break;
case RIG_PTT_CM108:
rs->pttport.fd = cm108_open(&rs->pttport);
if (rs->pttport.fd < 0)
{
rig_debug(RIG_DEBUG_ERR, "Cannot open PTT device \"%s\"\n",
rs->pttport.pathname);
status = -RIG_EIO;
}
else
cm108_ptt_set(&rs->pttport, RIG_PTT_OFF);
break;
case RIG_PTT_GPIO:
rs->pttport.fd = gpio_open(&rs->pttport, 1);
if (rs->pttport.fd < 0) {
rig_debug(RIG_DEBUG_ERR,
"Cannot open PTT device \"%s\"\n",
rs->pttport.pathname);
status = -RIG_EIO;
} else
gpio_ptt_set(&rs->pttport, RIG_PTT_OFF);
case RIG_PTT_GPION:
rs->pttport.fd = gpio_open(&rs->pttport, 0);
if (rs->pttport.fd < 0) {
rig_debug(RIG_DEBUG_ERR,
"Cannot open PTT device \"%s\"\n",
rs->pttport.pathname);
status = -RIG_EIO;
} else
gpio_ptt_set(&rs->pttport, RIG_PTT_OFF);
default:
rig_debug(RIG_DEBUG_ERR, "Unsupported PTT type %d\n",
rs->pttport.type.ptt);
status = -RIG_ECONF;
}
switch(rs->dcdport.type.dcd) {
case RIG_DCD_NONE:
case RIG_DCD_RIG:
break;
case RIG_DCD_SERIAL_DSR:
case RIG_DCD_SERIAL_CTS:
case RIG_DCD_SERIAL_CAR:
if (rs->dcdport.pathname[0] == '\0' &&
rs->rigport.type.rig == RIG_PORT_SERIAL)
strcpy(rs->dcdport.pathname, rs->rigport.pathname);
rs->dcdport.fd = ser_open(&rs->dcdport);
if (rs->dcdport.fd < 0)
{
rig_debug(RIG_DEBUG_ERR, "Cannot open DCD device \"%s\"\n",
rs->dcdport.pathname);
status = -RIG_EIO;
}
break;
case RIG_DCD_PARALLEL:
rs->dcdport.fd = par_open(&rs->dcdport);
if (rs->dcdport.fd < 0)
{
rig_debug(RIG_DEBUG_ERR, "Cannot open DCD device \"%s\"\n",
rs->dcdport.pathname);
status = -RIG_EIO;
}
break;
default:
rig_debug(RIG_DEBUG_ERR, "Unsupported DCD type %d\n",
rs->dcdport.type.dcd);
status = -RIG_ECONF;
}
if (status < 0)
{
port_close (&rs->rigport, rs->rigport.type.rig);
return status;
}
add_opened_rig(rig);
rs->comm_state = 1;
/*
* Maybe the backend has something to initialize
* In case of failure, just close down and report error code.
*/
if (caps->rig_open != NULL) {
status = caps->rig_open(rig);
if (status != RIG_OK) {
return status;
}
}
/*
* trigger state->current_vfo first retrieval
*/
if (rig_get_vfo(rig, &rs->current_vfo) == RIG_OK)
rs->tx_vfo = rs->current_vfo;
#if 0
/*
* Check the current tranceive state of the rig
*/
if (rs->transceive == RIG_TRN_RIG) {
int retval, trn;
retval = rig_get_trn(rig, &trn);
if (retval == RIG_OK && trn == RIG_TRN_RIG)
add_trn_rig(rig);
}
#endif
return RIG_OK;
}
/**
* \brief close the communication to the rig
* \param rig The #RIG handle of the radio to be closed
*
* Closes communication to a radio which \a RIG handle has been passed
* by argument that was previously open with rig_open().
*
* \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_cleanup(), rig_open()
*/
int HAMLIB_API rig_close(RIG *rig)
{
const struct rig_caps *caps;
struct rig_state *rs;
rig_debug(RIG_DEBUG_VERBOSE,"rig:rig_close called \n");
if (!rig || !rig->caps)
return -RIG_EINVAL;
caps = rig->caps;
rs = &rig->state;
if (!rs->comm_state)
return -RIG_EINVAL;
if (rs->transceive != RIG_TRN_OFF) {
rig_set_trn(rig, RIG_TRN_OFF);
}
/*
* Let the backend say 73s to the rig.
* and ignore the return code.
*/
if (caps->rig_close)
caps->rig_close(rig);
/*
* FIXME: what happens if PTT and rig ports are the same?
* (eg. ptt_type = RIG_PTT_SERIAL)
*/
switch(rs->pttport.type.ptt) {
case RIG_PTT_NONE:
case RIG_PTT_RIG:
case RIG_PTT_RIG_MICDATA:
break;
case RIG_PTT_SERIAL_RTS:
ser_set_rts(&rs->pttport, 0);
if (rs->pttport.fd != rs->rigport.fd)
{
port_close(&rs->pttport, RIG_PORT_SERIAL);
}
break;
case RIG_PTT_SERIAL_DTR:
ser_set_dtr(&rs->pttport, 0);
if (rs->pttport.fd != rs->rigport.fd)
{
port_close(&rs->pttport, RIG_PORT_SERIAL);
}
break;
case RIG_PTT_PARALLEL:
par_ptt_set(&rs->pttport, RIG_PTT_OFF);
port_close(&rs->pttport, RIG_PORT_PARALLEL);
break;
case RIG_PTT_CM108:
cm108_ptt_set(&rs->pttport, RIG_PTT_OFF);
port_close(&rs->pttport, RIG_PORT_CM108);
break;
case RIG_PTT_GPIO:
case RIG_PTT_GPION:
gpio_ptt_set(&rs->pttport, RIG_PTT_OFF);
port_close(&rs->pttport, RIG_PORT_GPIO);
default:
rig_debug(RIG_DEBUG_ERR, "Unsupported PTT type %d\n",
rs->pttport.type.ptt);
}
switch(rs->dcdport.type.dcd) {
case RIG_DCD_NONE:
case RIG_DCD_RIG:
break;
case RIG_DCD_SERIAL_DSR:
case RIG_DCD_SERIAL_CTS:
case RIG_DCD_SERIAL_CAR:
port_close(&rs->dcdport, RIG_PORT_SERIAL);
break;
case RIG_DCD_PARALLEL:
port_close(&rs->dcdport, RIG_PORT_PARALLEL);
break;
default:
rig_debug(RIG_DEBUG_ERR, "Unsupported DCD type %d\n",
rs->dcdport.type.dcd);
}
rs->dcdport.fd = rs->pttport.fd = -1;
port_close(&rs->rigport, rs->rigport.type.rig);
remove_opened_rig(rig);
rs->comm_state = 0;
return RIG_OK;
}
/**
* \brief release a rig handle and free associated memory
* \param rig The #RIG handle of the radio to be closed
*
* Releases a rig struct which port has eventualy been closed already
* with rig_close().
*
* \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_init(), rig_close()
*/
int HAMLIB_API rig_cleanup(RIG *rig)
{
rig_debug(RIG_DEBUG_VERBOSE,"rig:rig_cleanup called \n");
if (!rig || !rig->caps)
return -RIG_EINVAL;
/*
* check if they forgot to close the rig
*/
if (rig->state.comm_state)
rig_close(rig);
/*
* basically free up the priv struct
*/
if (rig->caps->rig_cleanup)
rig->caps->rig_cleanup(rig);
free(rig);
return RIG_OK;
}
/**
* \brief set the frequency of the target VFO
* \param rig The rig handle
* \param vfo The target VFO
* \param freq The frequency to set to
*
* Sets the frequency of the target VFO.
*
* \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_freq()
*/
int HAMLIB_API rig_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
const struct rig_caps *caps;
int retcode, rc2;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (rig->state.vfo_comp != 0.0)
freq += (freq_t)((double)rig->state.vfo_comp * freq);
if (caps->set_freq == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_FREQ) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo) {
retcode = caps->set_freq(rig, vfo, freq);
} else {
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_freq(rig, vfo, freq);
rc2 = caps->set_vfo(rig, curr_vfo); /* try and revert even if we
had an error above */
if (RIG_OK == retcode)
{
retcode = rc2; /* return the first error code */
}
}
if (retcode == RIG_OK &&
(vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo))
rig->state.current_freq = freq;
return retcode;
}
/**
* \brief get the frequency of the target VFO
* \param rig The rig handle
* \param vfo The target VFO
* \param freq The location where to store the current frequency
*
* Retrieves the frequency of the target VFO.
* The value stored at \a freq location equals RIG_FREQ_NONE when the current
* frequency of the VFO is not defined (e.g. blank memory).
*
* \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_freq()
*/
int HAMLIB_API rig_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
const struct rig_caps *caps;
int retcode, rc2;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig) || !freq)
return -RIG_EINVAL;
caps = rig->caps;
if (caps->get_freq == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_FREQ) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo) {
retcode = caps->get_freq(rig, vfo, freq);
} else {
if (!caps->set_vfo)
return -RIG_ENAVAIL;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->get_freq(rig, vfo, freq);
rc2 = caps->set_vfo(rig, curr_vfo); /* try and revert even if we
had an error above */
if (RIG_OK == retcode)
{
retcode = rc2; /* return the first error code */
}
}
/* VFO compensation */
if (rig->state.vfo_comp != 0.0)
*freq += (freq_t)(rig->state.vfo_comp * (*freq));
if (retcode == RIG_OK &&
(vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo))
rig->state.current_freq = *freq;
return retcode;
}
/**
* \brief set the mode of the target VFO
* \param rig The rig handle
* \param vfo The target VFO
* \param mode The mode to set to
* \param width The passband width to set to
*
* Sets the mode and associated passband of the target VFO. The
* passband \a width must be supported by the backend of the rig or
* the special value RIG_PASSBAND_NOCHANGE which leaves the passband
* unchanged from the current value or default for the mode determined
* by the rig.
*
* \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_mode()
*/
int HAMLIB_API rig_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
{
const struct rig_caps *caps;
int retcode, rc2;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (caps->set_mode == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_MODE) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo) {
retcode = caps->set_mode(rig, vfo, mode, width);
} else {
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_mode(rig, vfo, mode, width);
rc2 = caps->set_vfo(rig, curr_vfo); /* try and revert even if we
had an error above */
if (RIG_OK == retcode)
{
retcode = rc2; /* return the first error code */