Fixed: Coding rule issues.
[platform/core/connectivity/wifi-direct-manager.git] / src / wifi-direct-dbus.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2015 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 /**
21  * This file implements wifi direct dbus utility functions.
22  *
23  * @file        wifi-direct-dbus.c
24  * @author      Nishant Chaprana (n.chaprana@samsung.com)
25  * @version     0.1
26  */
27
28 #include <glib.h>
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 */
37
38 static GDBusConnection *g_connection = NULL;
39 static guint g_owner_id = 0;  /* Name Owner ID */
40
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 */
45
46 static GDBusConnection *__dbus_get_gdbus_conn(void)
47 {
48         return g_connection;
49 }
50
51 static void __on_bus_acquired(GDBusConnection *connection,
52                               const gchar *name,
53                               gpointer user_data)
54 {
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();
60 }
61
62 static void __on_name_acquired(GDBusConnection *connection,
63                                const gchar *name,
64                                gpointer user_data)
65 {
66         WDS_LOGD("on_name_acquired: %s", name);
67 }
68
69 static void __on_name_lost(GDBusConnection *connection,
70                            const gchar *name,
71                            gpointer user_data)
72 {
73         WDS_LOGD("on_name_lost: %s", name);
74 }
75
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,
82                                       GVariant *parameters,
83                                       gpointer user_data)
84 {
85         gchar *name = NULL;
86         gchar *old = NULL;
87         gchar *new = NULL;
88
89         g_variant_get(parameters, "(&s&s&s)", &name, &old, &new);
90         wfd_manager_remove_active_client(name, old, new);
91 }
92
93 static void wfd_manager_dbus_register_nameowner_signal(void)
94 {
95         GDBusConnection *connection = __dbus_get_gdbus_conn();
96
97         if (connection == NULL) {
98                 WDS_LOGE("Failed to get GDbus Connection");
99                 return;
100         }
101
102         nameowner_changed_sub_id = g_dbus_connection_signal_subscribe(
103                               connection,
104                               DBUS_SERVICE,
105                               DBUS_INTERFACE,
106                               "NameOwnerChanged",
107                               DBUS_PATH,
108                               NULL,
109                               G_DBUS_SIGNAL_FLAGS_NONE,
110                               nameowner_signal_handler,
111                               NULL,
112                               NULL);
113
114         WDS_LOGD("Subscribed successfully for NameOwnerChanged signals");
115 }
116
117 void wfd_manager_dbus_unregister_nameowner_signal(void)
118 {
119         GDBusConnection *connection = NULL;
120
121         wfd_manager_free_active_client_list();
122
123         connection = __dbus_get_gdbus_conn();
124         if (!connection) {
125                 WDS_LOGE("Already unregistered. Nothing to be done");
126                 return;
127         }
128
129         g_dbus_connection_signal_unsubscribe(connection,
130                                 nameowner_changed_sub_id);
131
132         WDS_LOGD("Unsubscribed successfully for NameOwnerChanged signals");
133 }
134 #endif/* TIZEN_FEATURE_WIFI_DIRECT_ON_DEMAND */
135
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)
140 {
141         GDBusInterfaceInfo *interface_info = NULL;
142         GError *Error = NULL;
143         guint reg_id = 0;
144         GDBusConnection *connection = NULL;
145
146         connection = __dbus_get_gdbus_conn();
147         if (connection == NULL) {
148                 WDS_LOGE("Dbus connection not yet initiated");
149                 return 0;
150         }
151
152         if (!iface_name || !iface_path || !node_info || !interface_vtable) {
153                 WDS_LOGE("Invalid Parameters");
154                 return 0;
155         }
156
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);
162                 return 0;
163         }
164
165         reg_id = g_dbus_connection_register_object(connection, iface_path,
166                         interface_info, interface_vtable,
167                         NULL, NULL, &Error);
168         if (reg_id == 0) {
169                 WDS_LOGE("Failed to register: %s", Error->message);
170                 g_clear_error(&Error);
171                 g_dbus_node_info_unref(node_info);
172                 return 0;
173         }
174
175         WDS_LOGD("Interface Registration ID [%d], Interface Name [%s]", reg_id, iface_name);
176
177         return reg_id;
178 }
179
180 gboolean wfd_manager_dbus_iface_unregister(guint reg_id)
181 {
182         GDBusConnection *connection = NULL;
183
184         connection = __dbus_get_gdbus_conn();
185         if (connection == NULL) {
186                 WDS_LOGE("Dbus connection not yet initiated");
187                 return FALSE;
188         }
189
190         if (reg_id > 0) {
191                 if (g_dbus_connection_unregister_object(connection, reg_id) == FALSE)
192                         WDS_LOGE("Failed to unregister iface object");
193         }
194         return TRUE;
195 }
196
197 gboolean wfd_manager_dbus_init(void)
198 {
199         GError *Error = NULL;
200
201         if (g_connection != NULL) {
202                 WDS_LOGE("Conenciton already present");
203                 return TRUE;
204         }
205
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);
209                 g_error_free(Error);
210                 return FALSE;
211         }
212
213
214         g_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
215                                     WFD_MANAGER_SERVICE,
216                                     G_BUS_NAME_OWNER_FLAGS_NONE,
217                                     __on_bus_acquired,
218                                     __on_name_acquired,
219                                     __on_name_lost,
220                                     NULL,
221                                     NULL);
222         if (g_owner_id == 0) {
223                 WDS_LOGE("Failed to get bus name");
224                 return FALSE;
225         }
226         WDS_LOGD("DBus Owner id is [%d]", g_owner_id);
227
228         return TRUE;
229 }
230
231 void wfd_manager_dbus_deinit(void)
232 {
233         if (g_connection == NULL || g_owner_id == 0)
234                 return;
235
236         g_object_unref(g_connection);
237         g_bus_unown_name(g_owner_id);
238 }
239
240 gboolean wfd_manager_dbus_emit_signal(const gchar *interface_name,
241                                       const gchar *signal_name,
242                                       GVariant *parameters)
243 {
244         gboolean rv = FALSE;
245         GError *error = NULL;
246         GDBusConnection *connection;
247
248         connection = __dbus_get_gdbus_conn();
249         if (connection == NULL) {
250                 WDS_LOGE("GDBusconnection is NULL");
251                 return 0;
252         }
253
254         DBUS_DEBUG_VARIANT(parameters);
255
256         rv = g_dbus_connection_emit_signal(connection,
257                                            NULL,
258                                            WFD_MANAGER_PATH,
259                                            interface_name,
260                                            signal_name,
261                                            parameters,
262                                            &error);
263         if (rv != TRUE) {
264                 WDS_LOGE("Failed to get node info, Error: %s", error->message);
265                 g_error_free(error);
266         } else {
267                 WDS_LOGD("[%s] signal sent on [%s] interface", signal_name, interface_name);
268         }
269
270         return rv;
271 }
272
273 GVariant* wfd_manager_dbus_pack_ay(const unsigned char *src, int size)
274 {
275         GVariantBuilder *builder = NULL;
276         GVariant *iter = NULL;
277         int i = 0;
278
279         if (!src) {
280                 WDS_LOGE("Invalid parameter");
281                 return NULL;
282         }
283
284         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
285
286         for (i = 0; i < size; i++)
287                 g_variant_builder_add(builder, "y", src[i]);
288
289         iter = g_variant_new("ay", builder);
290
291         g_variant_builder_unref(builder);
292         return iter;
293 }