Add preparation for Network Manager compatibility
[connman] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusMessage *list_interfaces(DBusConnection *conn,
31                                         DBusMessage *msg, void *data)
32 {
33         DBusMessage *reply;
34         DBusMessageIter array, iter;
35
36         DBG("conn %p", conn);
37
38         reply = dbus_message_new_method_return(msg);
39         if (reply == NULL)
40                 return NULL;
41
42         dbus_message_iter_init_append(reply, &array);
43
44         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
45                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
46
47         __connman_iface_list(&iter);
48
49         dbus_message_iter_close_container(&array, &iter);
50
51         return reply;
52 }
53
54 static GDBusMethodTable manager_methods[] = {
55         { "ListInterfaces", "", "ao", list_interfaces },
56         { },
57 };
58
59 static GDBusSignalTable manager_signals[] = {
60         { "InterfaceAdded",   "o" },
61         { "InterfaceRemoved", "o" },
62         { },
63 };
64
65 static DBusMessage *activate_device(DBusConnection *conn,
66                                         DBusMessage *msg, void *data)
67 {
68         DBusMessage *reply;
69         const char *path;
70
71         DBG("conn %p", conn);
72
73         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
74                                                         DBUS_TYPE_INVALID);
75
76         DBG("device %s", path);
77
78         reply = dbus_message_new_method_return(msg);
79         if (reply == NULL)
80                 return NULL;
81
82         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
83
84         return reply;
85 }
86
87 static DBusMessage *set_wireless(DBusConnection *conn,
88                                         DBusMessage *msg, void *data)
89 {
90         DBusMessage *reply;
91         dbus_bool_t enabled;
92
93         DBG("conn %p", conn);
94
95         dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &enabled,
96                                                         DBUS_TYPE_INVALID);
97
98         reply = dbus_message_new_method_return(msg);
99         if (reply == NULL)
100                 return NULL;
101
102         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
103
104         return reply;
105 }
106
107 static DBusMessage *get_wireless(DBusConnection *conn,
108                                         DBusMessage *msg, void *data)
109 {
110         DBusMessage *reply;
111         dbus_bool_t enabled = TRUE;
112
113         DBG("conn %p", conn);
114
115         reply = dbus_message_new_method_return(msg);
116         if (reply == NULL)
117                 return NULL;
118
119         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &enabled,
120                                                         DBUS_TYPE_INVALID);
121
122         return reply;
123 }
124
125 static DBusMessage *do_sleep(DBusConnection *conn,
126                                         DBusMessage *msg, void *data)
127 {
128         DBusMessage *reply;
129
130         DBG("conn %p", conn);
131
132         reply = dbus_message_new_method_return(msg);
133         if (reply == NULL)
134                 return NULL;
135
136         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
137
138         return reply;
139 }
140
141 static DBusMessage *do_wake(DBusConnection *conn,
142                                         DBusMessage *msg, void *data)
143 {
144         DBusMessage *reply;
145
146         DBG("conn %p", conn);
147
148         reply = dbus_message_new_method_return(msg);
149         if (reply == NULL)
150                 return NULL;
151
152         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
153
154         return reply;
155 }
156
157 enum {
158         NM_STATE_UNKNOWN = 0,
159         NM_STATE_ASLEEP,
160         NM_STATE_CONNECTING,
161         NM_STATE_CONNECTED,
162         NM_STATE_DISCONNECTED
163 };
164
165 static DBusMessage *get_state(DBusConnection *conn,
166                                         DBusMessage *msg, void *data)
167 {
168         DBusMessage *reply;
169         dbus_uint32_t state = NM_STATE_DISCONNECTED;
170
171         DBG("conn %p", conn);
172
173         reply = dbus_message_new_method_return(msg);
174         if (reply == NULL)
175                 return NULL;
176
177         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
178                                                         DBUS_TYPE_INVALID);
179
180         return reply;
181 }
182
183 static GDBusMethodTable nm_methods[] = {
184         { "getDevices",            "",  "ao", list_interfaces },
185         { "setActiveDevice",       "o", "",   activate_device },
186         { "setWirelessEnabled",    "b", "",   set_wireless    },
187         { "getWirelessEnabled",    "",  "b",  get_wireless    },
188         { "sleep",                 "",  "",   do_sleep        },
189         { "wake",                  "",  "",   do_wake         },
190         { "state",                 "",  "u",  get_state       },
191         { },
192 };
193
194 static DBusConnection *connection = NULL;
195 static int nm_compat = 0;
196
197 int __connman_manager_init(DBusConnection *conn, int compat)
198 {
199         DBG("conn %p", conn);
200
201         connection = dbus_connection_ref(conn);
202         if (connection == NULL)
203                 return -1;
204
205         g_dbus_register_object(connection, CONNMAN_MANAGER_PATH, NULL, NULL);
206
207         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
208                                                 CONNMAN_MANAGER_INTERFACE,
209                                                 manager_methods,
210                                                 manager_signals, NULL);
211
212         if (compat) {
213                 g_dbus_register_object(connection, NM_PATH, NULL, NULL);
214
215                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
216                                                 nm_methods, NULL, NULL);
217
218                 nm_compat = 1;
219         }
220
221         return 0;
222 }
223
224 void __connman_manager_cleanup(void)
225 {
226         DBG("conn %p", connection);
227
228         if (nm_compat) {
229                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
230
231                 g_dbus_unregister_object(connection, NM_PATH);
232         }
233
234         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
235                                                 CONNMAN_MANAGER_INTERFACE);
236
237         g_dbus_unregister_object(connection, CONNMAN_MANAGER_PATH);
238
239         dbus_connection_unref(connection);
240 }