nmcompat: Add plugin
[platform/upstream/connman.git] / plugins / nmcompat.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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,
37         NM_STATE_CONNECTING,
38         NM_STATE_CONNECTED,
39         NM_STATE_DISCONNECTED
40 };
41
42 #define NM_SERVICE    "org.freedesktop.NetworkManager"
43 #define NM_PATH       "/org/freedesktop/NetworkManager"
44 #define NM_INTERFACE  NM_SERVICE
45
46 static DBusConnection *connection = NULL;
47 static dbus_uint32_t state = NM_STATE_UNKNOWN;
48
49
50 static void nm_send_signal(const char *name, dbus_uint32_t state)
51 {
52         DBusMessage *signal;
53
54         signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE, name);
55         if (signal == NULL)
56                 return;
57
58         dbus_message_append_args(signal, DBUS_TYPE_UINT32, &state,
59                                 DBUS_TYPE_INVALID);
60
61         g_dbus_send_message(connection, signal);
62 }
63
64 static void default_changed(struct connman_service *service)
65 {
66         if (service != NULL)
67                 state = NM_STATE_CONNECTED;
68         else
69                 state = NM_STATE_DISCONNECTED;
70
71         DBG("%p %d", service, state);
72
73         /* older deprecated signal, in case applications still use this */
74         nm_send_signal("StateChange", state);
75
76         /* the preferred current signal */
77         nm_send_signal("StateChanged", state);
78 }
79
80 static struct connman_notifier notifier = {
81         .name           = "nmcompat",
82         .priority       = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
83         .default_changed= default_changed,
84 };
85
86 static DBusMessage *nm_sleep(DBusConnection *conn,
87                                         DBusMessage *msg, void *data)
88 {
89         DBusMessage *reply;
90
91         DBG("conn %p", conn);
92
93         reply = dbus_message_new_method_return(msg);
94         if (reply == NULL)
95                 return NULL;
96
97         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
98
99         return reply;
100 }
101
102 static DBusMessage *nm_wake(DBusConnection *conn,
103                                         DBusMessage *msg, void *data)
104 {
105         DBusMessage *reply;
106
107         DBG("conn %p", conn);
108
109         reply = dbus_message_new_method_return(msg);
110         if (reply == NULL)
111                 return NULL;
112
113         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
114
115         return reply;
116 }
117
118 static DBusMessage *nm_state(DBusConnection *conn,
119                                         DBusMessage *msg, void *data)
120 {
121         DBusMessage *reply;
122
123         DBG("conn %p", conn);
124
125         reply = dbus_message_new_method_return(msg);
126         if (reply == NULL)
127                 return NULL;
128
129         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
130                                                         DBUS_TYPE_INVALID);
131
132         return reply;
133 }
134
135 static GDBusMethodTable nm_methods[] = {
136         { "sleep", "",  "",   nm_sleep        },
137         { "wake",  "",  "",   nm_wake         },
138         { "state", "",  "u",  nm_state        },
139         { },
140 };
141
142 static int nmcompat_init(void)
143 {
144         gboolean ret;
145
146         DBG("");
147
148         connection = connman_dbus_get_connection();
149         if (connection == NULL)
150                 return -1;
151
152         if (g_dbus_request_name(connection, NM_SERVICE, NULL) == FALSE) {
153                 connman_error("nmcompat: can't register nm service\n");
154                 return -1;
155         }
156
157         if (connman_notifier_register(&notifier) < 0) {
158                 connman_error("nmcompat: failed to register notifier");
159                 return -1;
160         }
161
162         ret = g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
163                                         nm_methods, NULL, NULL, NULL, NULL);
164         if (ret == FALSE) {
165                 connman_error("nmcompat: can't register " NM_INTERFACE);
166                 return -1;
167         }
168
169         return 0;
170 }
171
172 static void nmcompat_exit(void)
173 {
174         DBG("");
175
176         connman_notifier_unregister(&notifier);
177
178         if (connection == NULL)
179                 return;
180
181         g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
182
183         dbus_connection_unref(connection);
184 }
185
186 CONNMAN_PLUGIN_DEFINE(nmcompat, "NetworkManager compatibility interfaces",
187                         VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
188                         nmcompat_init, nmcompat_exit)