Upgrade ofono to 1.11 and merge to 2.0alpha
[profile/ivi/ofono.git] / src / gnssagent.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  ST-Ericsson AB.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #define _GNU_SOURCE
28 #include <stdint.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include <glib.h>
33 #include <gdbus.h>
34
35 #include "ofono.h"
36 #include "gnssagent.h"
37
38 struct gnss_agent {
39         char *path;
40         char *bus;
41         guint disconnect_watch;
42         ofono_destroy_func removed_cb;
43         void *removed_data;
44 };
45
46 static void gnss_agent_send_noreply(struct gnss_agent *agent,
47                                         const char *method, int type, ...)
48 {
49         DBusConnection *conn = ofono_dbus_get_connection();
50         DBusMessage *message;
51         va_list args;
52
53         message = dbus_message_new_method_call(agent->bus, agent->path,
54                                         OFONO_GNSS_POSR_AGENT_INTERFACE,
55                                         method);
56
57         va_start(args, type);
58         dbus_message_append_args_valist(message, type, args);
59         va_end(args);
60
61         dbus_message_set_no_reply(message, TRUE);
62
63         g_dbus_send_message(conn, message);
64 }
65
66 static inline void gnss_agent_send_release(struct gnss_agent *agent)
67 {
68         gnss_agent_send_noreply(agent, "Release", DBUS_TYPE_INVALID);
69 }
70
71 void gnss_agent_receive_request(struct gnss_agent *agent, const char *xml)
72 {
73         gnss_agent_send_noreply(agent, "Request", DBUS_TYPE_STRING, &xml,
74                                 DBUS_TYPE_INVALID);
75 }
76
77 void gnss_agent_receive_reset(struct gnss_agent *agent)
78 {
79         gnss_agent_send_noreply(agent, "ResetAssistanceData",
80                                 DBUS_TYPE_INVALID);
81 }
82
83 ofono_bool_t gnss_agent_matches(struct gnss_agent *agent,
84                                 const char *path, const char *sender)
85 {
86         return g_str_equal(agent->path, path) &&
87                         g_str_equal(agent->bus, sender);
88 }
89
90 ofono_bool_t gnss_agent_sender_matches(struct gnss_agent *agent,
91                                         const char *sender)
92 {
93         return g_str_equal(agent->bus, sender);
94 }
95
96 void gnss_agent_set_removed_notify(struct gnss_agent *agent,
97                                         ofono_destroy_func destroy,
98                                         void *user_data)
99 {
100         agent->removed_cb = destroy;
101         agent->removed_data = user_data;
102 }
103
104 void gnss_agent_free(struct gnss_agent *agent)
105 {
106         DBusConnection *conn = ofono_dbus_get_connection();
107
108         if (agent->disconnect_watch) {
109                 gnss_agent_send_release(agent);
110                 g_dbus_remove_watch(conn, agent->disconnect_watch);
111                 agent->disconnect_watch = 0;
112         }
113
114         if (agent->removed_cb)
115                 agent->removed_cb(agent->removed_data);
116
117         g_free(agent->path);
118         g_free(agent->bus);
119         g_free(agent);
120 }
121
122 static void gnss_agent_disconnect_cb(DBusConnection *conn, void *user_data)
123 {
124         struct gnss_agent *agent = user_data;
125
126         agent->disconnect_watch = 0;
127
128         gnss_agent_free(agent);
129 }
130
131 struct gnss_agent *gnss_agent_new(const char *path, const char *sender)
132 {
133         struct gnss_agent *agent = g_try_new0(struct gnss_agent, 1);
134         DBusConnection *conn = ofono_dbus_get_connection();
135
136         if (agent == NULL)
137                 return NULL;
138
139         agent->path = g_strdup(path);
140         agent->bus = g_strdup(sender);
141
142         agent->disconnect_watch = g_dbus_add_disconnect_watch(conn, sender,
143                                                 gnss_agent_disconnect_cb,
144                                                 agent, NULL);
145
146         return agent;
147 }