2 * Network Configuration Module
4 * Copyright (c) 2015 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.
21 * This file implements wifi direct dbus utility functions.
23 * @file wifi-direct-dbus.c
24 * @author Nishant Chaprana (n.chaprana@samsung.com)
29 #include "wifi-direct-iface.h"
30 #include "wifi-direct-dbus.h"
31 #include "wifi-direct-log.h"
32 #ifdef TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND
33 #include <wifi-direct.h>
34 #include "wifi-direct-ipc.h"
35 #include "wifi-direct-manager.h"
36 #endif/* TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND */
38 static GDBusConnection *g_connection = NULL;
39 static guint g_owner_id = 0; /* Name Owner ID */
41 #ifdef TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND
42 static int nameowner_changed_sub_id = 0;
43 static void wfd_manager_dbus_register_nameowner_signal(void);
44 #endif/* TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND */
46 static GDBusConnection *__dbus_get_gdbus_conn(void)
51 static void __on_bus_acquired(GDBusConnection *connection,
55 WDS_LOGD("on_bus_acquired: %s", name);
56 #ifdef TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND
57 wfd_manager_dbus_register_nameowner_signal();
58 #endif/* TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND */
59 wfd_manager_dbus_register();
62 static void __on_name_acquired(GDBusConnection *connection,
66 WDS_LOGD("on_name_acquired: %s", name);
69 static void __on_name_lost(GDBusConnection *connection,
73 WDS_LOGD("on_name_lost: %s", name);
76 #ifdef TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND
77 static void nameowner_signal_handler(GDBusConnection *connection,
78 const gchar *sender_name,
79 const gchar *object_path,
80 const gchar *interface_name,
81 const gchar *signal_name,
89 g_variant_get(parameters, "(&s&s&s)", &name, &old, &new);
90 wfd_manager_remove_active_client(name, old, new);
93 static void wfd_manager_dbus_register_nameowner_signal(void)
95 GDBusConnection *connection = __dbus_get_gdbus_conn();
97 if (connection == NULL) {
98 WDS_LOGE("Failed to get GDbus Connection");
102 nameowner_changed_sub_id = g_dbus_connection_signal_subscribe(
109 G_DBUS_SIGNAL_FLAGS_NONE,
110 nameowner_signal_handler,
114 WDS_LOGD("Subscribed successfully for NameOwnerChanged signals");
117 void wfd_manager_dbus_unregister_nameowner_signal(void)
119 GDBusConnection *connection = NULL;
121 wfd_manager_free_active_client_list();
123 connection = __dbus_get_gdbus_conn();
125 WDS_LOGE("Already unregistered. Nothing to be done");
129 g_dbus_connection_signal_unsubscribe(connection,
130 nameowner_changed_sub_id);
132 WDS_LOGD("Unsubscribed successfully for NameOwnerChanged signals");
134 #endif/* TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND */
136 guint wfd_manager_dbus_iface_register(const gchar* iface_name,
137 const gchar* iface_path,
138 GDBusNodeInfo *node_info,
139 const GDBusInterfaceVTable *interface_vtable)
141 GDBusInterfaceInfo *interface_info = NULL;
142 GError *Error = NULL;
144 GDBusConnection *connection = NULL;
146 connection = __dbus_get_gdbus_conn();
147 if (connection == NULL) {
148 WDS_LOGE("Dbus connection not yet initiated");
152 if (!iface_name || !iface_path || !node_info || !interface_vtable) {
153 WDS_LOGE("Invalid Parameters");
157 /* Register interface */
158 interface_info = g_dbus_node_info_lookup_interface(node_info, iface_name);
159 if (interface_info == NULL) {
160 WDS_LOGE("Failed to get interface info");
161 g_dbus_node_info_unref(node_info);
165 reg_id = g_dbus_connection_register_object(connection, iface_path,
166 interface_info, interface_vtable,
169 WDS_LOGE("Failed to register: %s", Error->message);
170 g_clear_error(&Error);
171 g_dbus_node_info_unref(node_info);
175 WDS_LOGD("Interface Registration ID [%d], Interface Name [%s]", reg_id, iface_name);
180 gboolean wfd_manager_dbus_iface_unregister(guint reg_id)
182 GDBusConnection *connection = NULL;
184 connection = __dbus_get_gdbus_conn();
185 if (connection == NULL) {
186 WDS_LOGE("Dbus connection not yet initiated");
191 if (g_dbus_connection_unregister_object(connection, reg_id) == FALSE)
192 WDS_LOGE("Failed to unregister iface object");
197 gboolean wfd_manager_dbus_init(void)
199 GError *Error = NULL;
201 if (g_connection != NULL) {
202 WDS_LOGE("Conenciton already present");
206 g_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &Error);
207 if (g_connection == NULL) {
208 WDS_LOGE("Failed to get connection, Error[%s]", Error->message);
214 g_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
216 G_BUS_NAME_OWNER_FLAGS_NONE,
222 if (g_owner_id == 0) {
223 WDS_LOGE("Failed to get bus name");
226 WDS_LOGD("DBus Owner id is [%d]", g_owner_id);
231 void wfd_manager_dbus_deinit(void)
233 if (g_connection == NULL || g_owner_id == 0)
236 g_object_unref(g_connection);
237 g_bus_unown_name(g_owner_id);
240 gboolean wfd_manager_dbus_emit_signal(const gchar *interface_name,
241 const gchar *signal_name,
242 GVariant *parameters)
245 GError *error = NULL;
246 GDBusConnection *connection;
248 connection = __dbus_get_gdbus_conn();
249 if (connection == NULL) {
250 WDS_LOGE("GDBusconnection is NULL");
254 DBUS_DEBUG_VARIANT(parameters);
256 rv = g_dbus_connection_emit_signal(connection,
264 WDS_LOGE("Failed to get node info, Error: %s", error->message);
267 WDS_LOGD("[%s] signal sent on [%s] interface", signal_name, interface_name);
273 GVariant* wfd_manager_dbus_pack_ay(const unsigned char *src, int size)
275 GVariantBuilder *builder = NULL;
276 GVariant *iter = NULL;
280 WDS_LOGE("Invalid parameter");
284 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
286 for (i = 0; i < size; i++)
287 g_variant_builder_add(builder, "y", src[i]);
289 iter = g_variant_new("ay", builder);
291 g_variant_builder_unref(builder);