File: hamlib.c

package info (click to toggle)
gmfsk 0.6%2B0.7pre1-2.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,020 kB
  • ctags: 3,859
  • sloc: ansic: 23,271; cpp: 6,129; sh: 3,363; xml: 2,080; makefile: 295
file content (597 lines) | stat: -rw-r--r-- 12,085 bytes parent folder | download | duplicates (4)
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
/*
 *    hamlib.c  --  Hamlib (rig control) interface
 *
 *    Copyright (C) 2001, 2002, 2003, 2004
 *      Tomi Manninen (oh2bns@sral.fi)
 *
 *    This file is part of gMFSK.
 *
 *    gMFSK is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    gMFSK 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with gMFSK; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#if WANT_HAMLIB

#include <gnome.h>
#include <pthread.h>
#include <hamlib/rig.h>

#include "support.h"

#include "hamlib.h"
#include "main.h"
#include "conf.h"
#include "qsodata.h"
#include "trx.h"

static GSList *riglist = NULL;
static GPtrArray *rignames = NULL;

static gint riglist_compare_func(gconstpointer a, gconstpointer b);
static gint riglist_make_list(const struct rig_caps *caps, gpointer data);

/* ---------------------------------------------------------------------- */

GSList *riglist_get_list(void)
{
	if (riglist == NULL) {
		rig_set_debug(RIG_DEBUG_NONE);
		rig_load_all_backends();
		rig_set_debug(RIG_DEBUG_ERR);

		rig_list_foreach(riglist_make_list, NULL);
	}

	return riglist;
}

gint riglist_get_names(GPtrArray **list)
{
	gint i, len;

	if (riglist == NULL)
		riglist_get_list();

	if (rignames != NULL) {
		*list = rignames;
		return g_slist_length(riglist);
	}

	len = g_slist_length(riglist);

	rignames = g_ptr_array_sized_new(len);

	for (i = 0; i < len; i++) {
		struct rig_caps *rig = g_slist_nth_data(riglist, i);
		gchar *name;

		name = g_strdup_printf("%s %s",
				       rig->mfg_name,
				       rig->model_name);

		g_ptr_array_add(rignames, (gpointer) name);
	}

	*list = rignames;
	return len;
}

gint riglist_get_id_from_index(gint idx)
{
	struct rig_caps *rig;

	g_return_val_if_fail(riglist != NULL, 0);

	if ((rig = g_slist_nth_data(riglist, idx)) == NULL)
		return 0;

	return rig->rig_model;
}

gint riglist_get_index_from_id(gint id)
{
	gint i, len;

	g_return_val_if_fail(riglist != NULL, 0);

	len = g_slist_length(riglist);

	for (i = 0; i < len; i++) {
		struct rig_caps *rig = g_slist_nth_data(riglist, i);

		if (rig->rig_model == id)
			return i;
	}

	return 0;
}

/* ---------------------------------------------------------------------- */

static const gint speedlist[] = {
	300,	600,	1200,	2400,
	4800,	9600,	19200,	38400,
	57600,	115200,	230400,	460800,
	-1
};

gint riglist_get_speeds(gint rigid, gint **speeds)
{
	struct rig_caps *rig;
	gint i, n, len, *list;

	if (riglist == NULL)
		riglist_get_list();

	rig = g_slist_nth_data(riglist, riglist_get_index_from_id(rigid));

	for (len = i = 0; speedlist[i] > 0; i++) {
		if (speedlist[i] > rig->serial_rate_max)
			break;
		if (speedlist[i] >= rig->serial_rate_min)
			len++;
	}

	list = g_new(gint, len);

	for (n = i = 0; speedlist[i] > 0; i++) {
		if (speedlist[i] > rig->serial_rate_max)
			break;
		if (speedlist[i] >= rig->serial_rate_min)
			list[n++] = speedlist[i];
	}

	*speeds = list;
	return len;
}

gint riglist_get_index_from_speed(gint rigid, gint speed)
{
	gint i, len, *speeds;

	if ((len = riglist_get_speeds(rigid, &speeds)) == 0)
		return 0;

	for (i = 0; i < len; i++)
		if (speeds[i] == speed)
			break;

	/* default to the largest speed */
	if (i == len)
		i = len - 1;

	g_free(speeds);
	return i;
}

gint riglist_get_speed_from_index(gint rigid, gint idx)
{
	gint i, len, *speeds;

	if ((len = riglist_get_speeds(rigid, &speeds)) == 0)
		return 0;

	if (idx >= 0 && idx < len)
		i = speeds[idx];
	else
		i = 0;

	g_free(speeds);
	return i;
}

/* ---------------------------------------------------------------------- */

static gint riglist_make_list(const struct rig_caps *caps, gpointer data)
{
	riglist = g_slist_insert_sorted(riglist,
					(gpointer) caps,
					riglist_compare_func);
	return 1;
}

static gint riglist_compare_func(gconstpointer a, gconstpointer b)
{
	const struct rig_caps *rig1 = a;
	const struct rig_caps *rig2 = b;
	gint ret;

	if ((ret = strcmp(rig1->mfg_name, rig2->mfg_name)) != 0)
		return ret;

	if ((ret = strcmp(rig1->model_name, rig2->model_name)) != 0)
		return ret;

	return rig1->rig_model - rig2->rig_model;
}

/* ---------------------------------------------------------------------- */

G_LOCK_DEFINE_STATIC(hamlib_mutex);

static RIG *rig = NULL;
static pthread_t hamlib_thread;
static gboolean hamlib_exit = FALSE;

static gboolean hamlib_waterfall;
static gboolean hamlib_qsodata;
static gboolean hamlib_ptt;
static gboolean hamlib_qsy;
static gint hamlib_res;

static gboolean need_freq;
static gboolean need_mode;

static void *hamlib_loop(void *args);

void hamlib_set_conf(gboolean wf, gboolean qd, gboolean ptt, gint res)
{
	hamlib_waterfall = wf;
	hamlib_qsodata = qd;
	hamlib_ptt = ptt;
	hamlib_res = res;

	need_freq = (wf == TRUE || qd == TRUE) ? TRUE : FALSE;
	need_mode = need_freq;

	if (!hamlib_waterfall) {
		waterfall_set_carrier_frequency(waterfall, 0.0);
		waterfall_set_lsb(waterfall, FALSE);
	}

	if (!hamlib_qsodata)
		qsodata_set_band_mode(QSODATA_BAND_COMBO);
}

static gboolean hamlib_set_param(const gchar *par, const gchar *val)
{
	token_t t;
	gint ret;

	g_return_val_if_fail(rig != NULL, FALSE);

	if ((t = rig_token_lookup(rig, par)) == RIG_CONF_END) {
		errmsg(_("Hamlib init: Bad rig config parameter: '%s'"), par);
		return FALSE;
	}

	if ((ret = rig_set_conf(rig, t, val)) != RIG_OK) {
		errmsg(_("Hamlib init: rig_set_conf failed (%s=%s): %s"), par, val, rigerror(ret));
		return FALSE;
	}

	return TRUE;
}

void hamlib_init(void)
{
	rig_model_t model;
	struct timespec sleep;
	freq_t freq;
	rmode_t mode;
	pbwidth_t width;
	gboolean enable;
	gchar *port, *conf, *spd;
	gint ret, speed;

	if (rig != NULL)
		return;

	enable = conf_get_bool("hamlib/enable");
	model = conf_get_int("hamlib/rig");
	port = conf_get_filename("hamlib/port");
	speed = conf_get_int("hamlib/speed");
	conf = conf_get_string("hamlib/conf");

	if (!enable || !model || port[0] == 0)
		return;

	rig_set_debug(RIG_DEBUG_ERR);

	rig = rig_init(model);

	if (rig == NULL) {
		errmsg(_("Hamlib init: rig_init failed (model=%d)"), model);
		return;
	}

	g_strstrip(conf);
	if (conf[0]) {
		gchar **v, **p, *q;

		v = g_strsplit(conf, ",", 0);

		for (p = v; *p; p++) {
			if ((q = strchr(*p, '=')) == NULL) {
				errmsg(_("Hamlib init: Bad param=value pair: '%s'"), *p);
				break;
			}
			*q++ = 0;

			g_strstrip(*p);
			g_strstrip(q);

			if (hamlib_set_param(*p, q) == FALSE)
				break;
		}

		g_strfreev(v);
	}
	g_free(conf);

	hamlib_set_param("rig_pathname", port);
	g_free(port);

	spd = g_strdup_printf("%d", speed);
	hamlib_set_param("serial_speed", spd);
	g_free(spd);

	ret = rig_open(rig);

	if (ret != RIG_OK) {
		errmsg(_("Hamlib init: rig_open failed: %s"), rigerror(ret));
		rig_cleanup(rig);
		rig = NULL;
		return;
	}

	/* Polling the rig sometimes fails right after opening it */
	sleep.tv_sec = 0;
	sleep.tv_nsec = 100000000L;	/* 100ms */
	nanosleep(&sleep, NULL);

	if (need_freq == TRUE && \
	    (ret = rig_get_freq(rig, RIG_VFO_CURR, &freq)) != RIG_OK) {
		errmsg(_("Hamlib init: rig_get_freq failed: %s"), rigerror(ret));

		hamlib_waterfall = FALSE;
		hamlib_qsodata = FALSE;

		need_freq = FALSE;
		need_mode = FALSE;
	}

	if (need_mode == TRUE &&
	    (ret = rig_get_mode(rig, RIG_VFO_CURR, &mode, &width)) != RIG_OK) {
		errmsg(_("Hamlib init: rig_get_mode failed: %s.\nAssuming USB mode."), rigerror(ret));

		need_mode = FALSE;
	}

	if (hamlib_ptt == TRUE && 
	    (ret = rig_set_ptt(rig, RIG_VFO_CURR, RIG_PTT_OFF)) != RIG_OK) {
		errmsg(_("Hamlib init: rig_set_ptt failed: %s.\nHamlib PTT disabled"), rigerror(ret));

		hamlib_ptt = FALSE;
	}

	/* Don't create the thread if frequency data is not needed */
	if (need_freq == FALSE) {
		/* If PTT isn't needed either then close everything */
		if (hamlib_ptt == FALSE) {
			rig_close(rig);
			rig_cleanup(rig);
			rig = NULL;
		}
		return;
	}

	if (pthread_create(&hamlib_thread, NULL, hamlib_loop, NULL) < 0) {
		errmsg(_("Hamlib init: pthread_create: %m"));
		rig_close(rig);
		rig_cleanup(rig);
		rig = NULL;
	}
}

void hamlib_close(void)
{
	if (rig == NULL)
		return;

	/* tell the hamlib thread to kill it self */
	hamlib_exit = TRUE;

	/* and the wait for it to die */
	pthread_join(hamlib_thread, NULL);

	hamlib_exit = FALSE;

	G_LOCK(hamlib_mutex);
	rig_close(rig);
	rig_cleanup(rig);
	rig = NULL;
	G_UNLOCK(hamlib_mutex);
}

gboolean hamlib_active(void)
{
	return (rig != NULL);
}

void hamlib_set_ptt(gint ptt)
{
	gint ret;

	if (!rig || !hamlib_ptt)
		return;

	G_LOCK(hamlib_mutex);

	ret = rig_set_ptt(rig, RIG_VFO_CURR, ptt ? RIG_PTT_ON : RIG_PTT_OFF);

	if (ret != RIG_OK) {
		errmsg(_("rig_set_ptt failed: %s.\nHamlib PTT disabled.\n"), rigerror(ret));
		hamlib_ptt = FALSE;
	}

	G_UNLOCK(hamlib_mutex);
}

void hamlib_set_qsy(void)
{
	if (rig == NULL)
		return;

	hamlib_qsy = TRUE;
}

static void *hamlib_loop(void *args)
{
	struct timespec sleep;
	gdouble freq = 0.0;
	rmode_t mode = RIG_MODE_USB;
	gint ret;
	gchar *str;

	sleep.tv_sec = 0;
	sleep.tv_nsec = 100000000L;	/* 100ms */

	for (;;) {
		nanosleep(&sleep, NULL);

//		need_freq = (hamlib_waterfall || hamlib_qsodata || hamlib_qsy);
//		need_mode = need_freq;

		/* see if we are being canceled */
		if (hamlib_exit)
			break;

		if (need_freq) {
			freq_t f;

			G_LOCK(hamlib_mutex);
			ret = rig_get_freq(rig, RIG_VFO_CURR, &f);
			G_UNLOCK(hamlib_mutex);

			if (ret != RIG_OK) {
				str = g_strdup_printf(_("rig_get_freq failed: %s"),
						      rigerror(ret));
				statusbar_set_main(str);
				g_free(str);
				continue;
			}

			freq = (gdouble) f;
		}

		/* see if we are being canceled */
		if (hamlib_exit)
			break;

		if (need_mode) {
			pbwidth_t width;

			G_LOCK(hamlib_mutex);
			ret = rig_get_mode(rig, RIG_VFO_CURR, &mode, &width);
			G_UNLOCK(hamlib_mutex);

			if (ret != RIG_OK) {
				str = g_strdup_printf(_("rig_get_mode failed: %s"),
						      rigerror(ret));
				statusbar_set_main(str);
				g_free(str);
				continue;
			}
		}

		if (hamlib_qsy) {
			gfloat f = conf_get_float("hamlib/cfreq");

			hamlib_qsy = FALSE;

			G_LOCK(hamlib_mutex);
			ret = rig_set_freq(rig, RIG_VFO_CURR, ((gdouble) freq) + trx_get_freq() - f);
			G_UNLOCK(hamlib_mutex);

			if (ret != RIG_OK) {
				str = g_strdup_printf(_("rig_set_freq failed: %s"),
						      rigerror(ret));
				statusbar_set_main(str);
				g_free(str);
				continue;
			}

			waterfall_set_frequency(waterfall, f);
		}

		if (hamlib_waterfall) {
			waterfall_set_carrier_frequency(waterfall, freq);

			if (mode == RIG_MODE_LSB)
				waterfall_set_lsb(waterfall, TRUE);
			else
				waterfall_set_lsb(waterfall, FALSE);
		}

		if (hamlib_qsodata) {
			gchar *str, *fmt;

			if (mode == RIG_MODE_LSB)
				freq -= trx_get_freq();
			else
				freq += trx_get_freq();

			switch (hamlib_res) {
			case 2:
				fmt = "%.6f";
				break;
			case 1:
				fmt = "%.3f";
				break;
			case 0:
			default:
				fmt = "%.0f";
				break;
			}

			str = g_strdup_printf(fmt, freq / 1000000.0);

			gdk_threads_enter();
			qsodata_set_band_mode(QSODATA_BAND_ENTRY);
			qsodata_set_freq(str);
			gdk_threads_leave();

			g_free(str);
		}

		/* see if we are being canceled */
		if (hamlib_exit)
			break;
	}

	if (hamlib_waterfall) {
		waterfall_set_carrier_frequency(waterfall, 0.0);
		waterfall_set_lsb(waterfall, FALSE);
	}

	if (hamlib_qsodata) {
		gdk_threads_enter();
		qsodata_set_band_mode(QSODATA_BAND_COMBO);
		qsodata_set_freq("");
		gdk_threads_leave();
	}

	/* this will exit the hamlib thread */
	return NULL;
}

/* ---------------------------------------------------------------------- */

#endif