5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #define CONNMAN_API_SUBJECT_TO_CHANGE
29 #include <connman/plugin.h>
30 #include <connman/log.h>
31 #include <connman/notifier.h>
32 #include <connman/dbus.h>
42 #define NM_SERVICE "org.freedesktop.NetworkManager"
43 #define NM_PATH "/org/freedesktop/NetworkManager"
44 #define NM_INTERFACE NM_SERVICE
46 #define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
48 static DBusConnection *connection = NULL;
49 static dbus_uint32_t state = NM_STATE_UNKNOWN;
52 static void nm_send_signal(const char *name, dbus_uint32_t state)
56 signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE, name);
60 dbus_message_append_args(signal, DBUS_TYPE_UINT32, &state,
63 g_dbus_send_message(connection, signal);
66 static void nm_send_prop_signal(dbus_uint32_t state)
68 const char *key = "State";
69 DBusMessageIter iter, dict, dict_entry, dict_val;
72 signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE,
77 dbus_message_iter_init_append(signal, &iter);
79 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
80 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
81 DBUS_TYPE_STRING_AS_STRING
82 DBUS_TYPE_VARIANT_AS_STRING
83 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
86 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
89 dbus_message_iter_append_basic(&dict_entry, DBUS_TYPE_STRING,
92 dbus_message_iter_open_container(&dict_entry,
94 DBUS_TYPE_UINT32_AS_STRING, &dict_val);
96 dbus_message_iter_append_basic(&dict_val, DBUS_TYPE_UINT32, &state);
98 dbus_message_iter_close_container(&dict_entry, &dict_val);
99 dbus_message_iter_close_container(&dict, &dict_entry);
100 dbus_message_iter_close_container(&iter, &dict);
102 g_dbus_send_message(connection, signal);
105 static void default_changed(struct connman_service *service)
108 state = NM_STATE_CONNECTED;
110 state = NM_STATE_DISCONNECTED;
112 DBG("%p %d", service, state);
114 /* older deprecated signal, in case applications still use this */
115 nm_send_signal("StateChange", state);
117 /* the preferred current signal */
118 nm_send_signal("StateChanged", state);
120 nm_send_prop_signal(state);
123 static struct connman_notifier notifier = {
125 .priority = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
126 .default_changed= default_changed,
129 static DBusMessage *nm_sleep(DBusConnection *conn,
130 DBusMessage *msg, void *data)
134 DBG("conn %p", conn);
136 reply = dbus_message_new_method_return(msg);
140 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
145 static DBusMessage *nm_wake(DBusConnection *conn,
146 DBusMessage *msg, void *data)
150 DBG("conn %p", conn);
152 reply = dbus_message_new_method_return(msg);
156 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
161 static DBusMessage *nm_state(DBusConnection *conn,
162 DBusMessage *msg, void *data)
166 DBG("conn %p", conn);
168 reply = dbus_message_new_method_return(msg);
172 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
178 static GDBusMethodTable nm_methods[] = {
179 { "sleep", "", "", nm_sleep },
180 { "wake", "", "", nm_wake },
181 { "state", "", "u", nm_state },
185 static DBusMessage *nm_prop_get(DBusConnection *conn,
186 DBusMessage *msg, void *data)
188 DBusMessageIter iter, value;
189 const char *interface, *key;
192 DBG("conn %p", conn);
194 reply = dbus_message_new_method_return(msg);
198 dbus_message_get_args(msg, NULL,
199 DBUS_TYPE_STRING, &interface,
200 DBUS_TYPE_STRING, &key,
203 if (g_strcmp0(key, "State") == 0) {
204 dbus_message_iter_init_append(reply, &iter);
206 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
207 DBUS_TYPE_UINT32_AS_STRING,
209 dbus_message_iter_append_basic(&value, DBUS_TYPE_UINT32,
211 dbus_message_iter_close_container(&iter, &value);
213 dbus_message_unref(reply);
214 return dbus_message_new_error(msg, DBUS_ERROR_FAILED,
215 "Unsupported property");
221 static GDBusMethodTable nm_prop_methods[] = {
222 { "Get", "ss", "v", nm_prop_get },
226 static int nmcompat_init(void)
232 connection = connman_dbus_get_connection();
233 if (connection == NULL)
236 if (g_dbus_request_name(connection, NM_SERVICE, NULL) == FALSE) {
237 connman_error("nmcompat: can't register nm service\n");
241 if (connman_notifier_register(¬ifier) < 0) {
242 connman_error("nmcompat: failed to register notifier");
246 ret = g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
247 nm_methods, NULL, NULL, NULL, NULL);
249 connman_error("nmcompat: can't register " NM_INTERFACE);
253 ret = g_dbus_register_interface(connection, NM_PATH,
254 DBUS_PROPERTIES_INTERFACE,
255 nm_prop_methods, NULL, NULL,
258 connman_error("nmcompat: can't register "
259 DBUS_PROPERTIES_INTERFACE);
266 static void nmcompat_exit(void)
270 connman_notifier_unregister(¬ifier);
272 if (connection == NULL)
275 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
277 dbus_connection_unref(connection);
280 CONNMAN_PLUGIN_DEFINE(nmcompat, "NetworkManager compatibility interfaces",
281 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
282 nmcompat_init, nmcompat_exit)