summaryrefslogtreecommitdiff
path: root/gypsy/gypsy-control.c
blob: 2b481a6f352d89d72b3ac05a658eb22a5dcadaf7 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Gypsy
 *
 * A simple to use and understand GPSD replacement
 * that uses D-Bus, GLib and memory allocations
 *
 * Author: Iain Holmes <iain@gnome.org>
 * Copyright (C) 2007
 */

/**
 * SECTION:gypsy-control
 * @short_description: Control object for gypsy-daemon
 *
 * #GypsyControl is the object that controls the gypsy-daemon process. 
 * It is a singleton object, meaning that there will only be one instance of it
 * per application. Once the object has been created (or referenced if it 
 * had already been created for the application) with 
 * gypsy_control_get_default(), it can be used to tell gypsy-daemon what GPS
 * device to connect to with gypsy_control_create(). This call returns the
 * D-Bus object path to the GPS object in gypsy-daemon. This object path is then used to 
 * create other objects, such as #GypsyPosition, or #GypsyCourse.
 *
 * As gypsy-daemon is able to connect to multiple GPS devices, the one
 * #GypsyControl can be used to create them, returning a different path for each
 * GPS device. Gypsy-daemon can connect to both serial port devices which have
 * an entry in <filename>/dev</filename> and Bluetooth devices natively without
 * the need to use rfcomm to set up a <filename>/dev</filename> entry. To do
 * this you pass either the device path or the Bluetooth address of the device
 * to gypsy_control_create().
 *
 * Once the program has finished with the GPS, the #GypsyControl object should
 * by unreferenced with g_object_unref().
 *
 * <informalexample>
 * <programlisting>
 * . . .
 *
 * #GypsyControl *control;
 * char *path_bt, *path_dev;
 * GError *error = NULL;
 *
 * . . .
 *
 * control = gypsy_control_get_default ();
 * / * Use a Bluetooth device * /
 * path_bt = gypsy_control_create (control, "aa:bb:cc:dd:ee", &error);
 * if (path_bt == NULL) {
 * &nbsp;&nbsp;g_warning ("There was an error creating aa:bb:cc:dd:ee - %s", error->message);
 * &nbsp;&nbsp;g_error_free (error);
 * &nbsp;&nbsp;error = NULL;
 * }
 *
 * / * Use a serial port device * /
 * path_dev = gypsy_control_create (control, "/dev/gps", &error);
 * if (path_dev == NULL) {
 * &nbsp;&nbsp;g_warning ("There was an error creating /dev/gps - %s", error->message);
 * &nbsp;&nbsp;g_error_free (error);
 * &nbsp;&nbsp;error = NULL;
 * }
 *
 * . . .
 *
 * / * Use the paths here to create listener objects * /
 *
 * . . . 
 *
 * g_free (path_bt);
 * g_free (path_dev);
 *
 * . . .
 *
 * / * The program has finished with Gypsy now, unref the object. * /
 * g_object_unref (G_OBJECT (control));
 * </programlisting>
 * </informalexample>
 */

#include <glib-object.h>

#include <gypsy/gypsy-control.h>
#include <gypsy/gypsy-marshal.h>

#include "gypsy-server-bindings.h"

typedef struct _GypsyControlPrivate {
	DBusGProxy *proxy;
	char *device_name;
} GypsyControlPrivate;

#define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GYPSY_TYPE_CONTROL, GypsyControlPrivate))

G_DEFINE_TYPE (GypsyControl, gypsy_control, G_TYPE_OBJECT);

GQuark 
gypsy_control_error_quark (void)
{
	static GQuark quark = 0;
	if (G_UNLIKELY (quark == 0)) {
		quark = g_quark_from_static_string ("gypsy-control-error-quark");
	}

	return quark;
}

static void
finalize (GObject *object)
{
	GypsyControlPrivate *priv;

	priv = GET_PRIVATE (object);

	if (priv->device_name) {
		GError *error = NULL;

		/* Shoutdown the server object when this control object
		   is unreffed */
		if (!org_freedesktop_Gypsy_Server_shutdown (priv->proxy, 
							    priv->device_name,
							    &error)) {
			g_error_free (error);
		}

		g_free (priv->device_name);
	}
		
	G_OBJECT_CLASS (gypsy_control_parent_class)->finalize (object);
}

static void
dispose (GObject *object)
{
	GypsyControlPrivate *priv;

	priv = GET_PRIVATE (object);
	
	if (priv->proxy) {
		g_object_unref (priv->proxy);
		priv->proxy = NULL;
	}

	G_OBJECT_CLASS (gypsy_control_parent_class)->dispose (object);
}

static void
gypsy_control_class_init (GypsyControlClass *klass)
{
	GObjectClass *o_class = (GObjectClass *) klass;

	o_class->finalize = finalize;
	o_class->dispose = dispose;

	g_type_class_add_private (klass, sizeof (GypsyControlPrivate));
}

static void
gypsy_control_init (GypsyControl *control)
{
	GypsyControlPrivate *priv;
	DBusGConnection *connection;
	GError *error;

	priv = GET_PRIVATE (control);

	error = NULL;
	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (!connection) {
		g_warning ("Unable to get connection to system bus\n%s",
			   error->message);
		g_error_free (error);
		return;
	}

	priv->proxy = dbus_g_proxy_new_for_name (connection,
						 GYPSY_CONTROL_DBUS_SERVICE,
						 GYPSY_CONTROL_DBUS_PATH,
						 GYPSY_CONTROL_DBUS_INTERFACE);
	dbus_g_object_register_marshaller (gypsy_marshal_VOID__INT_INT_DOUBLE_DOUBLE_DOUBLE,
					   G_TYPE_NONE,
					   G_TYPE_INT,
					   G_TYPE_INT,
					   G_TYPE_DOUBLE,
					   G_TYPE_DOUBLE,
					   G_TYPE_DOUBLE,
					   G_TYPE_INVALID);
	dbus_g_object_register_marshaller (gypsy_marshal_VOID__INT_DOUBLE_DOUBLE_DOUBLE,
					   G_TYPE_NONE,
					   G_TYPE_INT,
					   G_TYPE_DOUBLE,
					   G_TYPE_DOUBLE,
					   G_TYPE_DOUBLE,
					   G_TYPE_INVALID);
}

/**
 * gypsy_control_get_default:
 * 
 * Retrieves the default #GypsyControl object.
 *
 * Return value: A singleton #GypsyControl.  Once the program has finished using
 * the #GypsyControl, it should be unreferenced with g_object_unref().
 */
GypsyControl *
gypsy_control_get_default (void)
{
	static GypsyControl *default_control = NULL;

	if (G_UNLIKELY (default_control == NULL)) {
		default_control = g_object_new (GYPSY_TYPE_CONTROL, NULL);
		g_object_add_weak_pointer (G_OBJECT (default_control),
					   (gpointer) &default_control);
		return default_control;
	}

	return g_object_ref (default_control);
}

/**
 * gypsy_control_create:
 * @control: The #GypsyControl device
 * @device_name: The path to the device file, or Bluetooth address
 * @error: A #GError to return errors in, or %NULL
 *
 * Creates a object on the server that refers to the GPS device at @device_name.
 * When this object is finalized, the remote object on the server will be
 * shutdown after which any calls to the object at the returned path
 * are not guaranteed to work.
 *
 * Return value: The path to the created object.
 */
char *
gypsy_control_create (GypsyControl *control,
		      const char   *device_name,
		      GError      **error)
{
	GypsyControlPrivate *priv;
	char *path;

	g_return_val_if_fail (GYPSY_IS_CONTROL (control), NULL);
	g_return_val_if_fail (device_name != NULL, NULL);

	priv = GET_PRIVATE (control);

	if (!org_freedesktop_Gypsy_Server_create (priv->proxy, device_name,
						  &path, error)) {
		return NULL;
	}

	priv->device_name = g_strdup (device_name);
	return path;
}