Fixed typo in error message.
[gsmd2.git] / libfreesmartphone / freesmartphone.c
blobf3deb2e1a7d56224bab890cd852918c39a73e198
1 /*
2 * freesmartphone.c
4 * Copyright(C) 2008 Ixonos Plc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Boston, MA 02111.
22 * Written by
23 * Heikki Paajanen
24 * Vesa Pikki
27 /**
28 * @mainpage Libfreesmartphone
30 * \section intro Introduction
32 * This is the documentation of libfreesmartphone, a library to access
33 * GSMD2's dbus methods conveniently.
38 #include <dbus/dbus-glib.h>
39 #include <glib.h>
40 #include "freesmartphone.h"
41 #include "freesmartphone-internal.h"
42 #include "freesmartphone-call.h"
43 #include "freesmartphone-sms.h"
45 static InterfaceDetails
46 details[]
47 = {
48 {FSO_CALL,
49 "/org/freesmartphone/GSM/Call",
50 "org.freesmartphone.GSM.Call",
51 fso_gsm_call_init
54 {FSO_DEVICE,
55 "/org/freesmartphone/GSM/Device",
56 "org.freesmartphone.GSM.Device",
57 fso_gsm_device_init},
59 {FSO_NETWORK,
60 "/org/freesmartphone/GSM/Network",
61 "org.freesmartphone.GSM.Network",
62 fso_gsm_network_init},
64 {FSO_PDP,
65 "/org/freesmartphone/GSM/PDP",
66 "org.freesmartphone.GSM.PDP",
67 fso_gsm_pdp_init},
69 {FSO_SIM,
70 "/org/freesmartphone/GSM/SIM",
71 "org.freesmartphone.GSM.SIM",
72 fso_gsm_sim_init},
74 {FSO_SMS,
75 "/org/freesmartphone/GSM/SMS",
76 "org.freesmartphone.GSM.SMS",
77 fso_gsm_sms_init}
81 /**
82 * Initialize libfreesmartphone library
85 * @return Handle for the library or NULL if initialization fails
87 FreeSmartphone* fso_init()
89 g_debug("%s", __func__);
90 g_type_init();
92 FreeSmartphone *fs = g_try_new0(FreeSmartphone, 1);
93 if ( !fs ) {
94 g_warning("%s : Out of memory", __func__);
95 return NULL;
97 GError *error = NULL;
98 fs->conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
99 if ( !fs->conn || error) {
100 g_warning("%s : Failed connect to system bus: %s",
101 __func__, error->message);
102 g_error_free( error );
103 error = NULL;
104 fso_free(fs);
105 return NULL;
108 fs->proxies = g_hash_table_new_full(g_int_hash,
109 g_int_equal,
110 NULL,
111 g_object_unref);
114 dbus_g_connection_ref (fs->conn);
116 return fs;
121 * @param fs
123 void fso_reply_data_free(ReplyData *rd)
125 g_free( rd );
128 void fso_free_signal_handler(FreeSmartphone *fs,FSOSignal signal)
130 GList *list = fso_get_signal_handler(fs,signal);
132 while ( list ) {
133 fso_reply_data_free((ReplyData*)list->data);
134 list = g_list_next(list);
137 list = fso_get_signal_handler(fs,signal);
139 if (list)
140 g_list_free(list);
143 void fso_free(FreeSmartphone *fs)
145 if ( !fs ) {
146 return;
148 if ( fs->conn ) {
149 dbus_g_connection_unref (fs->conn);
152 g_hash_table_destroy(fs->proxies);
153 fs->conn = NULL;
155 FSOSignal signal = FSO_SIGNAL_CALL_STATUS;
157 for(;signal<FSO_SIGNAL_LAST;signal++) {
158 fso_free_signal_handler(fs,signal);
161 g_free(fs);
167 ReplyData *fso_reply_data_new(GCallback cb,
168 gpointer data)
170 ReplyData *rd = g_try_new0(ReplyData, 1);
171 if ( rd ) {
172 rd->cb = cb;
173 rd->data = data;
175 return rd;
180 gboolean fso_signal_remove(FreeSmartphone *fs,
181 FSOInterface interface,
182 const gchar *signal_name,
183 FSOSignal signal,
184 GCallback signal_handler,
185 gpointer signal_handler_data,
186 GCallback usercb,
187 gpointer userdata)
189 GList *list = fso_get_signal_handler(fs,signal);
190 DBusGProxy *proxy = fso_get_proxy(fs,interface);
192 ReplyData *match = NULL;
193 while ( list ) {
194 ReplyData *rd = (ReplyData*)list->data;
195 if ( rd->cb == G_CALLBACK(usercb) && rd->data == userdata ) {
196 match= rd;
197 break;
199 list = g_list_next(list);
201 if ( match ) {
202 fs->signal_handlers[signal] = g_list_remove_all(fs->signal_handlers[signal], match);
203 if ( fs->signal_handlers[signal] == NULL) {
204 g_debug("%s : '%s'", __func__, signal_name);
205 dbus_g_proxy_disconnect_signal(proxy, signal_name,
206 G_CALLBACK(signal_handler),
207 signal_handler_data);
209 fso_reply_data_free( match );
211 return TRUE;
215 * @brief Returns and ensures that specific interface's proxy exists,
216 * creates it if necessary
218 * @param fs pointer to freesmartphone strucht
219 * @param interface interface whose proxy to ensure
220 * @return true if proxy exists, false if it couldn't be created
222 DBusGProxy *fso_get_proxy(FreeSmartphone *fs,FSOInterface interface)
224 g_assert(fs);
225 g_assert(fs->proxies);
227 //Try to find the proxy
228 DBusGProxy* proxy = g_hash_table_lookup(fs->proxies,
229 &details[interface].key);
231 if (!proxy) {
232 //If no proxy found, create
233 proxy = dbus_g_proxy_new_for_name(fs->conn,
234 FSO_GSM_SERVICE_NAME,
235 details[interface].path,
236 details[interface].interface);
238 if (!proxy)
239 return NULL;
241 //and add it
242 g_hash_table_insert(fs->proxies,
243 &details[interface].key,
244 proxy);
246 if (details[interface].init)
247 details[interface].init(fs);
250 return proxy;
254 * @brief Appends replydata to signal handler
256 * @param fs pointer to freesmartphone struct
257 * @param signal signal handler to append replydata to
258 * @param replydata replydata to append
259 * @return true if signal handler needs to be initialized
261 gboolean fso_append_data_to_signal_handler(FreeSmartphone *fs,
262 FSOSignal signal,
263 ReplyData *replydata)
265 g_return_val_if_fail(fs, FALSE);
266 gboolean result = FALSE;
268 if (!fs->signal_handlers[signal])
269 result = TRUE;
271 fs->signal_handlers[signal] = g_list_append(fs->signal_handlers[signal],
272 replydata);
273 return result;
276 GList *fso_get_signal_handler(FreeSmartphone *fs, FSOSignal signal)
278 g_return_val_if_fail(fs, NULL);
279 return fs->signal_handlers[signal];
286 * @brief Prepares replydata for a method call
287 * Ensures that a proper proxy is present.
289 * @param fs pointer to freesmartphone data
290 * @param cb Callback function
291 * @param userdata user data
292 * @param interface interface to which the method (we are preparing for) belongs
293 * @return new replydata (that has to be freed) or null if failed
295 ReplyData *fso_prepare_method(FreeSmartphone *fs,
296 GCallback cb,
297 gpointer userdata,
298 FSOInterface interface)
301 if ( !cb ) {
302 g_warning("%s : No callback function provided!", __func__);
303 return NULL;
306 ReplyData *replydata = fso_reply_data_new(cb, userdata);
307 if ( !replydata ) {
308 g_warning("%s : Out of memory", __func__);
309 return NULL;
313 DBusGProxy *proxy = fso_get_proxy(fs,interface);
315 if ( !proxy ) {
316 g_free( replydata );
317 return NULL;
320 return replydata;