plugins: Update copyright information
[profile/ivi/connman.git] / plugins / nmcompat.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #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>
33
34 enum {
35         NM_STATE_UNKNOWN          = 0,
36         NM_STATE_ASLEEP           = 10,
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
43 };
44
45 #define NM_SERVICE    "org.freedesktop.NetworkManager"
46 #define NM_PATH       "/org/freedesktop/NetworkManager"
47 #define NM_INTERFACE  NM_SERVICE
48
49 #define DBUS_PROPERTIES_INTERFACE       "org.freedesktop.DBus.Properties"
50
51 static DBusConnection *connection = NULL;
52 static struct connman_service *current_service = NULL;
53 static dbus_uint32_t nm_state = NM_STATE_UNKNOWN;
54
55 static void state_changed(dbus_uint32_t state)
56 {
57         DBusMessage *signal;
58
59         DBG("state %d", state);
60
61         signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE,
62                                                 "StateChanged");
63         if (signal == NULL)
64                 return;
65
66         dbus_message_append_args(signal, DBUS_TYPE_UINT32, &state,
67                                                 DBUS_TYPE_INVALID);
68
69         g_dbus_send_message(connection, signal);
70 }
71
72 static void properties_changed(dbus_uint32_t state)
73 {
74         const char *key = "State";
75         DBusMessageIter iter, dict, dict_entry, dict_val;
76         DBusMessage *signal;
77
78         DBG("state %d", state);
79
80         signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE,
81                                                 "PropertiesChanged");
82         if (signal == NULL)
83                 return;
84
85         dbus_message_iter_init_append(signal, &iter);
86
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,
92                                         &dict);
93
94         dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
95                                                         NULL, &dict_entry);
96
97         dbus_message_iter_append_basic(&dict_entry, DBUS_TYPE_STRING, &key);
98
99         dbus_message_iter_open_container(&dict_entry, DBUS_TYPE_VARIANT,
100                                         DBUS_TYPE_UINT32_AS_STRING, &dict_val);
101
102         dbus_message_iter_append_basic(&dict_val, DBUS_TYPE_UINT32, &state);
103
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);
107
108         g_dbus_send_message(connection, signal);
109 }
110
111 static void default_changed(struct connman_service *service)
112 {
113         DBG("service %p", service);
114
115         if (service == NULL)
116                 nm_state = NM_STATE_DISCONNECTED;
117         else
118                 nm_state = NM_STATE_CONNECTED_LOCAL;
119
120         state_changed(nm_state);
121         properties_changed(nm_state);
122
123         current_service = service;
124 }
125
126 static void service_state_changed(struct connman_service *service,
127                                         enum connman_service_state state)
128 {
129         DBG("service %p state %d", service, state);
130
131         if (current_service == NULL || current_service != service)
132                 return;
133
134         switch (state) {
135         case CONNMAN_SERVICE_STATE_UNKNOWN:
136                 nm_state = NM_STATE_UNKNOWN;
137                 break;
138         case CONNMAN_SERVICE_STATE_FAILURE:
139         case CONNMAN_SERVICE_STATE_IDLE:
140                 nm_state = NM_STATE_DISCONNECTED;
141                 break;
142         case CONNMAN_SERVICE_STATE_ASSOCIATION:
143         case CONNMAN_SERVICE_STATE_CONFIGURATION:
144                 nm_state = NM_STATE_CONNECTING;
145                 break;
146         case CONNMAN_SERVICE_STATE_READY:
147                 nm_state = NM_STATE_CONNECTED_LOCAL;
148                 break;
149         case CONNMAN_SERVICE_STATE_ONLINE:
150                 nm_state = NM_STATE_CONNECTED_GLOBAL;
151                 break;
152         case CONNMAN_SERVICE_STATE_DISCONNECT:
153                 nm_state = NM_STATE_DISCONNECTING;
154                 break;
155         }
156
157         state_changed(nm_state);
158         properties_changed(nm_state);
159 }
160
161 static void offline_mode(connman_bool_t enabled)
162 {
163         DBG("enabled %d", enabled);
164
165         if (enabled == TRUE)
166                 nm_state = NM_STATE_ASLEEP;
167         else
168                 nm_state = NM_STATE_DISCONNECTED;
169
170         state_changed(nm_state);
171         properties_changed(nm_state);
172
173         current_service = NULL;
174 }
175
176 static struct connman_notifier notifier = {
177         .name                   = "nmcompat",
178         .priority               = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
179         .default_changed        = default_changed,
180         .service_state_changed  = service_state_changed,
181         .offline_mode           = offline_mode,
182 };
183
184 static DBusMessage *property_get(DBusConnection *conn,
185                                         DBusMessage *msg, void *data)
186 {
187         const char *interface, *key;
188
189         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
190                                 DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID);
191
192         DBG("interface %s property %s", interface, key);
193
194         if (g_str_equal(interface, NM_INTERFACE) == FALSE)
195                 return dbus_message_new_error(msg, DBUS_ERROR_FAILED,
196                                                 "Unsupported interface");
197
198         if (g_str_equal(key, "State") == TRUE) {
199                 DBusMessage *reply;
200                 DBusMessageIter iter, value;
201
202                 DBG("state %d", nm_state);
203
204                 reply = dbus_message_new_method_return(msg);
205                 if (reply == NULL)
206                         return NULL;
207
208                 dbus_message_iter_init_append(reply, &iter);
209
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);
215
216                 return reply;
217         }
218
219         return dbus_message_new_error(msg, DBUS_ERROR_FAILED,
220                                                 "Unsupported property");
221 }
222
223 static GDBusMethodTable methods[] = {
224         { "Get", "ss",  "v",   property_get     },
225         { },
226 };
227
228 static GDBusSignalTable signals[] = {
229         { "PropertiesChanged",  "a{sv}" },
230         { "StateChanged",       "u"     },
231         { },
232 };
233
234 static int nmcompat_init(void)
235 {
236         DBG("");
237
238         connection = connman_dbus_get_connection();
239         if (connection == NULL)
240                 return -1;
241
242         if (g_dbus_request_name(connection, NM_SERVICE, NULL) == FALSE) {
243                 connman_error("nmcompat: failed to register service");
244                 return -1;
245         }
246
247         if (connman_notifier_register(&notifier) < 0) {
248                 connman_error("nmcompat: failed to register notifier");
249                 return -1;
250         }
251
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);
257                 return -1;
258         }
259
260         return 0;
261 }
262
263 static void nmcompat_exit(void)
264 {
265         DBG("");
266
267         connman_notifier_unregister(&notifier);
268
269         if (connection == NULL)
270                 return;
271
272         g_dbus_unregister_interface(connection, NM_PATH,
273                                         DBUS_PROPERTIES_INTERFACE);
274
275         dbus_connection_unref(connection);
276 }
277
278 CONNMAN_PLUGIN_DEFINE(nmcompat, "NetworkManager compatibility interfaces",
279                         VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
280                         nmcompat_init, nmcompat_exit)