2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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 GDBusObjectManagerServer *manager_server_mptcp = NULL;
46 static guint owner_id = 0;
47 static got_name_cb g_callback = NULL;
49 struct gdbus_conn_data {
50 GDBusConnection *connection;
52 GCancellable *cancellable;
55 static struct gdbus_conn_data gconn_data = {NULL, 0, NULL};
57 GDBusObjectManagerServer *netdbus_get_wifi_manager(void)
59 return manager_server_wifi;
62 GDBusObjectManagerServer *netdbus_get_state_manager(void)
64 return manager_server_state;
67 GDBusObjectManagerServer *netdbus_get_statistics_manager(void)
69 return manager_server_statistics;
72 GDBusObjectManagerServer *netdbus_get_vpn_manager(void)
74 return manager_server_vpn;
77 GDBusObjectManagerServer *netdbus_get_mptcp_manager(void)
79 return manager_server_mptcp;
82 GDBusConnection *netdbus_get_connection(void)
84 return gconn_data.connection;
87 GCancellable *netdbus_get_cancellable(void)
89 return gconn_data.cancellable;
92 void netconfig_gdbus_pending_call_ref(void)
94 g_object_ref(gconn_data.connection);
96 __sync_fetch_and_add(&gconn_data.conn_ref_count, 1);
99 void netconfig_gdbus_pending_call_unref(void)
101 if (gconn_data.conn_ref_count < 1)
104 g_object_unref(gconn_data.connection);
106 if (__sync_sub_and_fetch(&gconn_data.conn_ref_count, 1) < 1) {
108 * gconn_data.connection = NULL;
113 int _create_gdbus_call(GDBusConnection *conn)
115 if (gconn_data.connection != NULL) {
116 ERR("Connection already set");
120 gconn_data.connection = conn;
121 if (gconn_data.connection == NULL) {
122 ERR("Failed to connect to the D-BUS daemon");
126 gconn_data.cancellable = g_cancellable_new();
131 gboolean netconfig_is_cellular_internet_profile(const char *profile)
133 const char internet_suffix[] = "_1";
139 if (g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX)
141 suffix = strrchr(profile, '_');
142 if (g_strcmp0(suffix, internet_suffix) == 0)
149 gboolean netconfig_is_cellular_profile(const char *profile)
154 return g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
157 gboolean netconfig_is_wifi_profile(const char *profile)
162 return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
165 gboolean netconfig_is_ethernet_profile(const char *profile)
170 return g_str_has_prefix(profile, CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
173 gboolean netconfig_is_bluetooth_profile(const char *profile)
178 return g_str_has_prefix(profile, CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
181 gboolean netconfig_invoke_dbus_method_nonblock(const char *dest, const char *path,
182 const char *interface_name, const char *method, GVariant *params,
183 GAsyncReadyCallback notify_func)
185 GDBusConnection *connection = NULL;
187 DBG("[GDBUS Async] %s %s %s", interface_name, method, path);
189 connection = netdbus_get_connection();
190 if (connection == NULL) {
191 ERR("Failed to get gdbus connection");
195 g_dbus_connection_call(connection,
202 G_DBUS_CALL_FLAGS_NONE,
203 NETCONFIG_DBUS_REPLY_TIMEOUT,
204 netdbus_get_cancellable(),
205 (GAsyncReadyCallback) notify_func,
208 netconfig_gdbus_pending_call_ref();
213 GVariant *netconfig_invoke_dbus_method(const char *dest, const char *path,
214 const char *interface_name, const char *method, GVariant *params)
217 GError *error = NULL;
218 GVariant *reply = NULL;
219 GDBusConnection *connection;
221 connection = netdbus_get_connection();
222 if (connection == NULL) {
223 ERR("Failed to get GDBusconnection");
227 reply = g_dbus_connection_call_sync(
235 G_DBUS_CALL_FLAGS_NONE,
236 NETCONFIG_DBUS_REPLY_TIMEOUT,
237 netdbus_get_cancellable(),
242 ERR("g_dbus_connection_call_sync() failed"
243 "error [%d: %s]", error->code, error->message);
246 ERR("g_dbus_connection_call_sync() failed");
255 gboolean netconfig_dbus_emit_signal(const gchar *destination_bus_name,
256 const gchar *object_path,
257 const gchar *interface_name,
258 const gchar *signal_name,
262 GError *Error = NULL;
263 GDBusConnection *connection;
265 connection = netdbus_get_connection();
266 if (connection == NULL) {
267 ERR("[NET_DBUS] GDBusconnection is NULL");
271 rv = g_dbus_connection_emit_signal(connection, destination_bus_name,
272 object_path, interface_name, signal_name, params, &Error);
274 ERR("[NET_DBUS] Failed to emit signal, Error: %s", Error->message);
275 g_clear_error(&Error);
279 INFO("Sent signal (%s), Interface (%s)", signal_name, interface_name);
284 static void _got_bus_cb(GDBusConnection *conn, const gchar *name,
287 _create_gdbus_call(conn);
290 static void _got_name_cb(GDBusConnection *conn, const gchar *name,
293 INFO("Got gdbus name: [%s] and gdbus connection: [%p]", name, conn);
295 if (g_callback != NULL)
299 static void _lost_name_cb(GDBusConnection *conn, const gchar *name,
302 /* May service name is already in use */
303 ERR("_lost_name_cb [%s]", name);
305 /* The result of DBus name request is only permitted,
306 * such as DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER.
311 int setup_gdbus(got_name_cb cb)
315 manager_server_wifi = g_dbus_object_manager_server_new(NETCONFIG_WIFI_PATH);
316 if (manager_server_wifi == NULL) {
317 ERR("Manager server for WIFI_PATH not created.");
321 manager_server_state = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATE_PATH);
322 if (manager_server_state == NULL) {
323 ERR("Manager server for STATE_PATH not created.");
327 manager_server_statistics = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATISTICS_PATH);
328 if (manager_server_statistics == NULL) {
329 ERR("Manager server for STATISTICS_PATH not created.");
333 manager_server_vpn = g_dbus_object_manager_server_new(NETCONFIG_VPNSVC_PATH);
334 if (manager_server_vpn == NULL) {
335 ERR("Manager server for VPNSVC_PATH not created.");
339 manager_server_mptcp = g_dbus_object_manager_server_new(NETCONFIG_MPTCP_PATH);
340 if (manager_server_mptcp == NULL) {
341 ERR("Manager server for MPTCP_PATH not created.");
345 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, NETCONFIG_SERVICE,
346 G_BUS_NAME_OWNER_FLAGS_NONE,
347 _got_bus_cb, _got_name_cb, _lost_name_cb,
350 ERR("Could not get system bus!");
354 INFO("Got system bus!");
358 void cleanup_gdbus(void)
360 g_bus_unown_name(owner_id);
361 g_object_unref(manager_server_wifi);
362 g_object_unref(manager_server_state);
363 g_object_unref(manager_server_statistics);