summaryrefslogtreecommitdiffstats
path: root/src/gnssagent.c
blob: 56d00f9e095e5d76072a61d0b06a7c46ca5e9bf0 (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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
 *  Copyright (C) 2011  ST-Ericsson AB.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

#define _GNU_SOURCE
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <glib.h>
#include <gdbus.h>

#include "ofono.h"
#include "gnssagent.h"

struct gnss_agent {
	char *path;
	char *bus;
	guint disconnect_watch;
	ofono_destroy_func removed_cb;
	void *removed_data;
};

static void gnss_agent_send_noreply(struct gnss_agent *agent,
					const char *method, int type, ...)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *message;
	va_list args;

	message = dbus_message_new_method_call(agent->bus, agent->path,
					OFONO_GNSS_POSR_AGENT_INTERFACE,
					method);

	va_start(args, type);
	dbus_message_append_args_valist(message, type, args);
	va_end(args);

	dbus_message_set_no_reply(message, TRUE);

	g_dbus_send_message(conn, message);
}

static inline void gnss_agent_send_release(struct gnss_agent *agent)
{
	gnss_agent_send_noreply(agent, "Release", DBUS_TYPE_INVALID);
}

void gnss_agent_receive_request(struct gnss_agent *agent, const char *xml)
{
	gnss_agent_send_noreply(agent, "Request", DBUS_TYPE_STRING, &xml,
				DBUS_TYPE_INVALID);
}

void gnss_agent_receive_reset(struct gnss_agent *agent)
{
	gnss_agent_send_noreply(agent, "ResetAssistanceData",
				DBUS_TYPE_INVALID);
}

ofono_bool_t gnss_agent_matches(struct gnss_agent *agent,
				const char *path, const char *sender)
{
	return g_str_equal(agent->path, path) &&
			g_str_equal(agent->bus, sender);
}

ofono_bool_t gnss_agent_sender_matches(struct gnss_agent *agent,
					const char *sender)
{
	return g_str_equal(agent->bus, sender);
}

void gnss_agent_set_removed_notify(struct gnss_agent *agent,
					ofono_destroy_func destroy,
					void *user_data)
{
	agent->removed_cb = destroy;
	agent->removed_data = user_data;
}

void gnss_agent_free(struct gnss_agent *agent)
{
	DBusConnection *conn = ofono_dbus_get_connection();

	if (agent->disconnect_watch) {
		gnss_agent_send_release(agent);
		g_dbus_remove_watch(conn, agent->disconnect_watch);
		agent->disconnect_watch = 0;
	}

	if (agent->removed_cb)
		agent->removed_cb(agent->removed_data);

	g_free(agent->path);
	g_free(agent->bus);
	g_free(agent);
}

static void gnss_agent_disconnect_cb(DBusConnection *conn, void *user_data)
{
	struct gnss_agent *agent = user_data;

	agent->disconnect_watch = 0;

	gnss_agent_free(agent);
}

struct gnss_agent *gnss_agent_new(const char *path, const char *sender)
{
	struct gnss_agent *agent = g_try_new0(struct gnss_agent, 1);
	DBusConnection *conn = ofono_dbus_get_connection();

	if (agent == NULL)
		return NULL;

	agent->path = g_strdup(path);
	agent->bus = g_strdup(sender);

	agent->disconnect_watch = g_dbus_add_disconnect_watch(conn, sender,
						gnss_agent_disconnect_cb,
						agent, NULL);

	return agent;
}