summaryrefslogtreecommitdiff
path: root/radio.c
blob: 07c6ff80ebc0c5df487241639561607fcbc800a9 (plain)
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
/*
 *  fmrxd - a daemon to enable and multiplex access to the N950/N9 radio tuner
 *  Copyright (C) 2011 Javier S. Pedro <maemo@javispedro.com>
 *
 *  This program 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.

 *  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 General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <glib.h>
#include "fmrxd.h"

static guint start_source_id = 0;
static guint stop_source_id = 0;
static bool active = false;

bool radio_active()
{
	return active;
}

void radio_start()
{
	g_return_if_fail(!active);
	g_debug("Starting radio");

	bool ok;

	ok = configure_bt_muxer(true);
	if (!ok) goto fail_start;

	configure_mixer(true);
	if (!ok) goto fail_start;

	configure_tuner(true);
	if (!ok) goto fail_start;

	configure_capture(true);
	if (!ok) goto fail_start;

	// Not essential.
	configure_signal(true);
	configure_rds(true);

	active = true;
	return; // Success

fail_start:
	g_critical("Radio failed to start!");

	// Try to force a stop
	active = true;
	radio_stop();
}

void radio_stop()
{
	g_return_if_fail(active);
	g_debug("Stopping radio");

	configure_rds(false);
	configure_signal(false);
	configure_capture(false);
	configure_tuner(false);
	configure_mixer(false);
	configure_bt_muxer(false);

	server_notify_stopped();

	active = false;
}

static gboolean radio_start_func(gpointer data)
{
	radio_start();
	start_source_id = 0;
	return FALSE;
}

static gboolean radio_stop_func(gpointer data)
{
	radio_stop();
	stop_source_id = 0;
	return FALSE;
}

void radio_queue_start()
{
	if (stop_source_id) {
		/* If there was a queued stop pending, remove it. */
		g_source_remove(stop_source_id);
	}
	if (!radio_active() && !start_source_id) {
		start_source_id = g_idle_add(radio_start_func, NULL);
	}
}

void radio_queue_stop()
{
	if (start_source_id) {
		/* If there is a queued start, remove it. */
		g_source_remove(start_source_id);
	}
	if (radio_active() && !stop_source_id) {
		stop_source_id = g_timeout_add_seconds(RADIO_LINGER_TIME,
			radio_stop_func, NULL);
	}
}