Upgrade ofono to 1.11 and merge to 2.0alpha
[profile/ivi/ofono.git] / src / manager.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <glib.h>
28 #include <gdbus.h>
29
30 #include "ofono.h"
31
32 static void append_modem(struct ofono_modem *modem, void *userdata)
33 {
34         DBusMessageIter *array = userdata;
35         const char *path = ofono_modem_get_path(modem);
36         DBusMessageIter entry, dict;
37
38         if (ofono_modem_is_registered(modem) == FALSE)
39                 return;
40
41         dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
42                                                 NULL, &entry);
43         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
44                                         &path);
45         dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
46                                 OFONO_PROPERTIES_ARRAY_SIGNATURE,
47                                 &dict);
48
49         __ofono_modem_append_properties(modem, &dict);
50         dbus_message_iter_close_container(&entry, &dict);
51         dbus_message_iter_close_container(array, &entry);
52 }
53
54 static DBusMessage *manager_get_modems(DBusConnection *conn,
55                                         DBusMessage *msg, void *data)
56 {
57         DBusMessage *reply;
58         DBusMessageIter iter;
59         DBusMessageIter array;
60
61         reply = dbus_message_new_method_return(msg);
62         if (reply == NULL)
63                 return NULL;
64
65         dbus_message_iter_init_append(reply, &iter);
66
67         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
68                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
69                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
70                                         DBUS_TYPE_ARRAY_AS_STRING
71                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
72                                         DBUS_TYPE_STRING_AS_STRING
73                                         DBUS_TYPE_VARIANT_AS_STRING
74                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
75                                         DBUS_STRUCT_END_CHAR_AS_STRING,
76                                         &array);
77         __ofono_modem_foreach(append_modem, &array);
78         dbus_message_iter_close_container(&iter, &array);
79
80         return reply;
81 }
82
83 static const GDBusMethodTable manager_methods[] = {
84         { GDBUS_METHOD("GetModems",
85                                 NULL, GDBUS_ARGS({ "modems", "a(oa{sv})" }),
86                                 manager_get_modems) },
87         { }
88 };
89
90 static const GDBusSignalTable manager_signals[] = {
91         { GDBUS_SIGNAL("ModemAdded",
92                 GDBUS_ARGS({ "path", "o" }, { "properties", "a{sv}" })) },
93         { GDBUS_SIGNAL("ModemRemoved",
94                 GDBUS_ARGS({ "path", "o" })) },
95         { }
96 };
97
98 int __ofono_manager_init(void)
99 {
100         DBusConnection *conn = ofono_dbus_get_connection();
101         gboolean ret;
102
103         ret = g_dbus_register_interface(conn, OFONO_MANAGER_PATH,
104                                         OFONO_MANAGER_INTERFACE,
105                                         manager_methods, manager_signals,
106                                         NULL, NULL, NULL);
107
108         if (ret == FALSE)
109                 return -1;
110
111         return 0;
112 }
113
114 void __ofono_manager_cleanup(void)
115 {
116         DBusConnection *conn = ofono_dbus_get_connection();
117
118         g_dbus_unregister_interface(conn, OFONO_MANAGER_PATH,
119                                         OFONO_MANAGER_INTERFACE);
120 }