summaryrefslogtreecommitdiff
path: root/examples/simple-gps-dbus.c
blob: f7044e68c02990fbf277590492216d224ece3780 (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
/*
 * 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
 *
 * This example code is in the public domain.
 */

/*
 * simple-gps-dbus: A simple gps example using pure D-Bus.
 */

#include <glib.h>

#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus.h>

static DBusHandlerResult
parse_position_changed (DBusConnection *conn,
			DBusMessage    *message,
			void           *user_data)
{
	int timestamp, fields;
	double latitude, longitude, altitude;
	DBusError error;

	dbus_error_init (&error);
	if (!dbus_message_get_args (message, &error,
				    DBUS_TYPE_INT32, &fields,
				    DBUS_TYPE_INT32, &timestamp,
				    DBUS_TYPE_DOUBLE, &latitude,
				    DBUS_TYPE_DOUBLE, &longitude,
				    DBUS_TYPE_DOUBLE, &altitude,
				    DBUS_TYPE_INVALID)) {
		g_warning ("Could not get position: %s", error.message);

		dbus_error_free (&error);
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}

	g_print ("Latitude: %f\nLongitude: %f\nAltitude: %f\n",
		 latitude, longitude, altitude);

        return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult
parse_course_changed (DBusConnection *conn,
		      DBusMessage    *message,
		      void           *user_data)
{
	int timestamp, fields;
	double speed, direction, climb;
	DBusError error;

	dbus_error_init (&error);
	if (!dbus_message_get_args (message, &error,
				    DBUS_TYPE_INT32, &fields,
				    DBUS_TYPE_INT32, &timestamp,
				    DBUS_TYPE_DOUBLE, &speed,
				    DBUS_TYPE_DOUBLE, &direction,
				    DBUS_TYPE_DOUBLE, &climb,
				    DBUS_TYPE_INVALID)) {
		g_warning ("Could not get position: %s", error.message);

		dbus_error_free (&error);
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}

	g_print ("Speed: %f\nDirection: %f\nClimb: %f\n",
		 speed, direction, climb);

        return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult
signal_filter (DBusConnection *conn,
               DBusMessage    *message,
               void           *user_data)
{
        if (dbus_message_is_signal (message, "org.freedesktop.gypsy.Client",
                                    "PositionChanged")) {
                return parse_position_changed (conn, message, user_data);
        } 

        if (dbus_message_is_signal (message, "org.freedesktop.gypsy.Client",
                                    "CourseChanged")) {
                return parse_course_changed (conn, message, user_data);
        } 

        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusConnection *
get_connection (void)
{
        GError *error = NULL;
        DBusGConnection *conn;

        conn = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
        if (conn == NULL) {
                g_error ("Error getting bus: %s", error->message);
        }

        return dbus_g_connection_get_connection (conn);
}

#define POSITION_CHANGED_MATCH "type='signal',interface='org.freedesktop.gypsy.Client',member='PositionChanged'"
#define COURSE_CHANGED_MATCH "type='signal',interface='org.freedesktop.gypsy.Client',member='CourseChanged'"

int
main (int    argc,
      char **argv)
{
	DBusConnection *conn;
	DBusError error;
	GMainLoop *mainloop;

	g_type_init ();
	conn = get_connection ();

	dbus_error_init (&error);
	dbus_bus_add_match (conn, POSITION_CHANGED_MATCH, &error);
	if (dbus_error_is_set (&error)) {
		g_error ("Error adding match: %s", error.message);
	}

	dbus_bus_add_match (conn, COURSE_CHANGED_MATCH, &error);
	if (dbus_error_is_set (&error)) {
		g_error ("Error adding match: %s", error.message);
	}

	if (!dbus_connection_add_filter (conn, signal_filter,
					 NULL, NULL)) {
		g_error ("Error adding filter");
	}

	mainloop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (mainloop);

	return 0;
}