Add the vpn service
[platform/core/connectivity/net-config.git] / src / dbus / netdbus.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "log.h"
25 #include "netdbus.h"
26
27 #define DBUS_PARAM_TYPE_STRING          "string"
28 #define DBUS_PARAM_TYPE_INT16           "int16"
29 #define DBUS_PARAM_TYPE_UINT16          "uint16"
30 #define DBUS_PARAM_TYPE_INT32           "int32"
31 #define DBUS_PARAM_TYPE_UINT32          "uint32"
32 #define DBUS_PARAM_TYPE_INT64           "int64"
33 #define DBUS_PARAM_TYPE_UINT64          "uint64"
34 #define DBUS_PARAM_TYPE_DOUBLE          "double"
35 #define DBUS_PARAM_TYPE_BYTE            "byte"
36 #define DBUS_PARAM_TYPE_BOOLEAN         "boolean"
37 #define DBUS_PARAM_TYPE_OBJECT_PATH     "objpath"
38 #define DBUS_PARAM_TYPE_VARIANT         "variant"
39 #define DBUS_PARAM_TYPE_ARRAY           "array"
40
41 static GDBusObjectManagerServer *manager_server_wifi = NULL;
42 static GDBusObjectManagerServer *manager_server_state = NULL;
43 static GDBusObjectManagerServer *manager_server_statistics = NULL;
44 static GDBusObjectManagerServer *manager_server_vpn = NULL;
45 static guint owner_id = 0;
46 static got_name_cb g_callback = NULL;
47
48 struct gdbus_conn_data {
49         GDBusConnection *connection;
50         int conn_ref_count;
51         GCancellable *cancellable;
52 };
53
54 static struct gdbus_conn_data gconn_data = {NULL, 0, NULL};
55
56 GDBusObjectManagerServer *netdbus_get_wifi_manager(void)
57 {
58         return manager_server_wifi;
59 }
60
61 GDBusObjectManagerServer *netdbus_get_state_manager(void)
62 {
63         return manager_server_state;
64 }
65
66 GDBusObjectManagerServer *netdbus_get_statistics_manager(void)
67 {
68         return manager_server_statistics;
69 }
70
71 GDBusObjectManagerServer *netdbus_get_vpn_manager(void)
72 {
73         return manager_server_vpn;
74 }
75
76 GDBusConnection *netdbus_get_connection(void)
77 {
78         return gconn_data.connection;
79 }
80
81 GCancellable *netdbus_get_cancellable(void)
82 {
83         return gconn_data.cancellable;
84 }
85
86 void netconfig_gdbus_pending_call_ref(void)
87 {
88         g_object_ref(gconn_data.connection);
89
90         __sync_fetch_and_add(&gconn_data.conn_ref_count, 1);
91 }
92
93 void netconfig_gdbus_pending_call_unref(void)
94 {
95         if (gconn_data.conn_ref_count < 1)
96                 return;
97
98         g_object_unref(gconn_data.connection);
99
100         if (__sync_sub_and_fetch(&gconn_data.conn_ref_count, 1) < 1) {
101                 /* TODO: Check this
102                  * gconn_data.connection = NULL;
103                  */
104         }
105 }
106
107 int _create_gdbus_call(GDBusConnection *conn)
108 {
109         if (gconn_data.connection != NULL) {
110                 ERR("Connection already set");
111                 return -1;
112         }
113
114         gconn_data.connection = conn;
115         if (gconn_data.connection == NULL) {
116                 ERR("Failed to connect to the D-BUS daemon");
117                 return -1;
118         }
119
120         gconn_data.cancellable = g_cancellable_new();
121
122         return 0;
123 }
124
125 gboolean netconfig_is_cellular_internet_profile(const char *profile)
126 {
127         const char internet_suffix[] = "_1";
128         char *suffix = NULL;
129
130         if (profile == NULL)
131                 return FALSE;
132
133         if (g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX)
134                         == TRUE) {
135                 suffix = strrchr(profile, '_');
136                 if (g_strcmp0(suffix, internet_suffix) == 0)
137                         return TRUE;
138         }
139
140         return FALSE;
141 }
142
143 gboolean netconfig_is_cellular_profile(const char *profile)
144 {
145         if (profile == NULL)
146                 return FALSE;
147
148         return g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
149 }
150
151 gboolean netconfig_is_wifi_profile(const char *profile)
152 {
153         if (profile == NULL)
154                 return FALSE;
155
156         return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
157 }
158
159 gboolean netconfig_is_ethernet_profile(const char *profile)
160 {
161         if (profile == NULL)
162                 return FALSE;
163
164         return g_str_has_prefix(profile, CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
165 }
166
167 gboolean netconfig_is_bluetooth_profile(const char *profile)
168 {
169         if (profile == NULL)
170                 return FALSE;
171
172         return g_str_has_prefix(profile, CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
173 }
174
175 gboolean netconfig_invoke_dbus_method_nonblock(const char *dest, const char *path,
176                 const char *interface_name, const char *method, GVariant *params,
177                 GAsyncReadyCallback notify_func)
178 {
179         GDBusConnection *connection = NULL;
180
181         DBG("[GDBUS Async] %s %s %s", interface_name, method, path);
182
183         connection = netdbus_get_connection();
184         if (connection == NULL) {
185                 ERR("Failed to get gdbus connection");
186                 return FALSE;
187         }
188
189         g_dbus_connection_call(connection,
190                         dest,
191                         path,
192                         interface_name,
193                         method,
194                         params,
195                         NULL,
196                         G_DBUS_CALL_FLAGS_NONE,
197                         NETCONFIG_DBUS_REPLY_TIMEOUT,
198                         netdbus_get_cancellable(),
199                         (GAsyncReadyCallback) notify_func,
200                         NULL);
201         if (notify_func)
202                 netconfig_gdbus_pending_call_ref();
203
204         return TRUE;
205 }
206
207 GVariant *netconfig_invoke_dbus_method(const char *dest, const char *path,
208                 const char *interface_name, const char *method, GVariant *params)
209 {
210
211         GError *error = NULL;
212         GVariant *reply = NULL;
213         GDBusConnection *connection;
214
215         connection = netdbus_get_connection();
216         if (connection == NULL) {
217                 ERR("Failed to get GDBusconnection");
218                 return reply;
219         }
220
221         reply = g_dbus_connection_call_sync(
222                         connection,
223                         dest,
224                         path,
225                         interface_name,
226                         method,
227                         params,
228                         NULL,
229                         G_DBUS_CALL_FLAGS_NONE,
230                         NETCONFIG_DBUS_REPLY_TIMEOUT,
231                         netdbus_get_cancellable(),
232                         &error);
233
234         if (reply == NULL) {
235                 if (error != NULL) {
236                         ERR("g_dbus_connection_call_sync() failed"
237                                                 "error [%d: %s]", error->code, error->message);
238                         g_error_free(error);
239                 } else {
240                         ERR("g_dbus_connection_call_sync() failed");
241                 }
242
243                 return NULL;
244         }
245
246         return reply;
247 }
248
249 gboolean netconfig_dbus_emit_signal(const gchar *destination_bus_name,
250                                                                         const gchar *object_path,
251                                                                         const gchar *interface_name,
252                                                                         const gchar *signal_name,
253                                                                         GVariant *params)
254 {
255         gboolean rv = FALSE;
256         GError *Error = NULL;
257         GDBusConnection *connection;
258
259         connection = netdbus_get_connection();
260         if (connection == NULL) {
261                 ERR("[NET_DBUS] GDBusconnection is NULL");
262                 return 0;
263         }
264
265         rv = g_dbus_connection_emit_signal(connection, destination_bus_name,
266                 object_path, interface_name, signal_name, params, &Error);
267         if (rv != TRUE) {
268                 ERR("[NET_DBUS] Failed to emit signal, Error: %s", Error->message);
269                 g_clear_error(&Error);
270                 return rv;
271         }
272
273         INFO("Sent signal (%s), Interface (%s)", signal_name, interface_name);
274
275         return rv;
276 }
277
278 static void _got_bus_cb(GDBusConnection *conn, const gchar *name,
279                 gpointer user_data)
280 {
281         _create_gdbus_call(conn);
282 }
283
284 static void _got_name_cb(GDBusConnection *conn, const gchar *name,
285                 gpointer user_data)
286 {
287         INFO("Got gdbus name: [%s] and gdbus connection: [%p]", name, conn);
288
289         if (g_callback != NULL)
290                 g_callback();
291 }
292
293 static void _lost_name_cb(GDBusConnection *conn, const gchar *name,
294                 gpointer user_data)
295 {
296         /* May service name is already in use */
297         ERR("_lost_name_cb [%s]", name);
298
299         /* The result of DBus name request is only permitted,
300          *  such as DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER.
301          */
302         exit(2);
303 }
304
305 int setup_gdbus(got_name_cb cb)
306 {
307         g_callback = cb;
308
309         manager_server_wifi = g_dbus_object_manager_server_new(NETCONFIG_WIFI_PATH);
310         if (manager_server_wifi == NULL) {
311                 ERR("Manager server for WIFI_PATH not created.");
312                 exit(1);
313         }
314
315         manager_server_state = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATE_PATH);
316         if (manager_server_state == NULL) {
317                 ERR("Manager server for STATE_PATH not created.");
318                 exit(1);
319         }
320
321         manager_server_statistics = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATISTICS_PATH);
322         if (manager_server_statistics == NULL) {
323                 ERR("Manager server for STATISTICS_PATH not created.");
324                 exit(1);
325         }
326
327         manager_server_vpn= g_dbus_object_manager_server_new(NETCONFIG_VPNSVC_PATH);
328         if (manager_server_vpn == NULL) {
329                 ERR("Manager server for VPNSVC_PATH not created.");
330                 exit(1);
331         }
332
333         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, NETCONFIG_SERVICE,
334                                                           G_BUS_NAME_OWNER_FLAGS_NONE,
335                                                           _got_bus_cb, _got_name_cb, _lost_name_cb,
336                                                           NULL, NULL);
337         if (!owner_id) {
338                 ERR("Could not get system bus!");
339                 return -EIO;
340         }
341
342         INFO("Got system bus!");
343         return 0;
344 }
345
346 void cleanup_gdbus(void)
347 {
348         g_bus_unown_name(owner_id);
349         g_object_unref(manager_server_wifi);
350         g_object_unref(manager_server_state);
351         g_object_unref(manager_server_statistics);
352 }