5 * Copyright (C) 2007-2012 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>
37 NM_STATE_DISCONNECTED = 20,
38 NM_STATE_DISCONNECTING = 30,
39 NM_STATE_CONNECTING = 40,
40 NM_STATE_CONNECTED_LOCAL = 50,
41 NM_STATE_CONNECTED_SITE = 60,
42 NM_STATE_CONNECTED_GLOBAL = 70
45 #define NM_SERVICE "org.freedesktop.NetworkManager"
46 #define NM_PATH "/org/freedesktop/NetworkManager"
47 #define NM_INTERFACE NM_SERVICE
49 #define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
51 static DBusConnection *connection = NULL;
52 static struct connman_service *current_service = NULL;
53 static dbus_uint32_t nm_state = NM_STATE_UNKNOWN;
55 static void state_changed(dbus_uint32_t state)
59 DBG("state %d", state);
61 signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE,
66 dbus_message_append_args(signal, DBUS_TYPE_UINT32, &state,
69 g_dbus_send_message(connection, signal);
72 static void properties_changed(dbus_uint32_t state)
74 const char *key = "State";
75 DBusMessageIter iter, dict, dict_entry, dict_val;
78 DBG("state %d", state);
80 signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE,
85 dbus_message_iter_init_append(signal, &iter);
87 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
88 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
89 DBUS_TYPE_STRING_AS_STRING
90 DBUS_TYPE_VARIANT_AS_STRING
91 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
94 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
97 dbus_message_iter_append_basic(&dict_entry, DBUS_TYPE_STRING, &key);
99 dbus_message_iter_open_container(&dict_entry, DBUS_TYPE_VARIANT,
100 DBUS_TYPE_UINT32_AS_STRING, &dict_val);
102 dbus_message_iter_append_basic(&dict_val, DBUS_TYPE_UINT32, &state);
104 dbus_message_iter_close_container(&dict_entry, &dict_val);
105 dbus_message_iter_close_container(&dict, &dict_entry);
106 dbus_message_iter_close_container(&iter, &dict);
108 g_dbus_send_message(connection, signal);
111 static void default_changed(struct connman_service *service)
113 DBG("service %p", service);
116 nm_state = NM_STATE_DISCONNECTED;
118 nm_state = NM_STATE_CONNECTED_LOCAL;
120 state_changed(nm_state);
121 properties_changed(nm_state);
123 current_service = service;
126 static void service_state_changed(struct connman_service *service,
127 enum connman_service_state state)
129 DBG("service %p state %d", service, state);
131 if (current_service == NULL || current_service != service)
135 case CONNMAN_SERVICE_STATE_UNKNOWN:
136 nm_state = NM_STATE_UNKNOWN;
138 case CONNMAN_SERVICE_STATE_FAILURE:
139 case CONNMAN_SERVICE_STATE_IDLE:
140 nm_state = NM_STATE_DISCONNECTED;
142 case CONNMAN_SERVICE_STATE_ASSOCIATION:
143 case CONNMAN_SERVICE_STATE_CONFIGURATION:
144 nm_state = NM_STATE_CONNECTING;
146 case CONNMAN_SERVICE_STATE_READY:
147 nm_state = NM_STATE_CONNECTED_LOCAL;
149 case CONNMAN_SERVICE_STATE_ONLINE:
150 nm_state = NM_STATE_CONNECTED_GLOBAL;
152 case CONNMAN_SERVICE_STATE_DISCONNECT:
153 nm_state = NM_STATE_DISCONNECTING;
157 state_changed(nm_state);
158 properties_changed(nm_state);
161 static void offline_mode(connman_bool_t enabled)
163 DBG("enabled %d", enabled);
166 nm_state = NM_STATE_ASLEEP;
168 nm_state = NM_STATE_DISCONNECTED;
170 state_changed(nm_state);
171 properties_changed(nm_state);
173 current_service = NULL;
176 static struct connman_notifier notifier = {
178 .priority = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
179 .default_changed = default_changed,
180 .service_state_changed = service_state_changed,
181 .offline_mode = offline_mode,
184 static DBusMessage *property_get(DBusConnection *conn,
185 DBusMessage *msg, void *data)
187 const char *interface, *key;
189 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
190 DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID);
192 DBG("interface %s property %s", interface, key);
194 if (g_str_equal(interface, NM_INTERFACE) == FALSE)
195 return dbus_message_new_error(msg, DBUS_ERROR_FAILED,
196 "Unsupported interface");
198 if (g_str_equal(key, "State") == TRUE) {
200 DBusMessageIter iter, value;
202 DBG("state %d", nm_state);
204 reply = dbus_message_new_method_return(msg);
208 dbus_message_iter_init_append(reply, &iter);
210 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
211 DBUS_TYPE_UINT32_AS_STRING, &value);
212 dbus_message_iter_append_basic(&value,
213 DBUS_TYPE_UINT32, &nm_state);
214 dbus_message_iter_close_container(&iter, &value);
219 return dbus_message_new_error(msg, DBUS_ERROR_FAILED,
220 "Unsupported property");
223 static GDBusMethodTable methods[] = {
224 { "Get", "ss", "v", property_get },
228 static GDBusSignalTable signals[] = {
229 { "PropertiesChanged", "a{sv}" },
230 { "StateChanged", "u" },
234 static int nmcompat_init(void)
238 connection = connman_dbus_get_connection();
239 if (connection == NULL)
242 if (g_dbus_request_name(connection, NM_SERVICE, NULL) == FALSE) {
243 connman_error("nmcompat: failed to register service");
247 if (connman_notifier_register(¬ifier) < 0) {
248 connman_error("nmcompat: failed to register notifier");
252 if (g_dbus_register_interface(connection, NM_PATH,
253 DBUS_PROPERTIES_INTERFACE,
254 methods, signals, NULL, NULL, NULL) == FALSE) {
255 connman_error("nmcompat: failed to register "
256 DBUS_PROPERTIES_INTERFACE);
263 static void nmcompat_exit(void)
267 connman_notifier_unregister(¬ifier);
269 if (connection == NULL)
272 g_dbus_unregister_interface(connection, NM_PATH,
273 DBUS_PROPERTIES_INTERFACE);
275 dbus_connection_unref(connection);
278 CONNMAN_PLUGIN_DEFINE(nmcompat, "NetworkManager compatibility interfaces",
279 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
280 nmcompat_init, nmcompat_exit)