Support for MultiThreaded Applications
[platform/core/api/wifi-direct.git] / src / wifi-direct-dbus.c
1 /*
2  * Wi-Fi Direct
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.2
26  */
27
28 #include "wifi-direct-dbus.h"
29 #include "wifi-direct-log.h"
30 #include "wifi-direct-client-proxy.h"
31
32 typedef struct {
33         GDBusConnection *connection;
34         guint signal_subscribe_id;
35 } gdbus_connection_data;
36
37 static __thread gdbus_connection_data gdbus_conn = {NULL, 0};
38
39 typedef struct {
40         int sub_id;
41         const char *interface;
42         const char *member;
43         void (*function) (GDBusConnection *connection,
44                           const gchar *sender,
45                           const gchar *object_path,
46                           const gchar *interface,
47                           const gchar *signal,
48                           GVariant *parameters,
49                           gpointer user_data);
50 } wifi_direct_dbus_signal_map_s;
51
52 static __thread wifi_direct_dbus_signal_map_s wifi_direct_dbus_signal_map[] = {
53         {
54                 0,
55                 WFD_MANAGER_MANAGE_INTERFACE,
56                 "Activation",
57                 wifi_direct_process_manage_activation
58         },
59         {
60                 0,
61                 WFD_MANAGER_MANAGE_INTERFACE,
62                 "Deactivation",
63                 wifi_direct_process_manage_deactivation
64         },
65         {
66                 0,
67                 WFD_MANAGER_MANAGE_INTERFACE,
68                 "Connection",
69                 wifi_direct_process_manage_connection
70         },
71         {
72                 0,
73                 WFD_MANAGER_MANAGE_INTERFACE,
74                 "Disconnection",
75                 wifi_direct_process_manage_disconnection
76         },
77         {
78                 0,
79                 WFD_MANAGER_MANAGE_INTERFACE,
80                 "DisconnectionInd",
81                 wifi_direct_process_manage_disconnection_ind
82         },
83         {
84                 0,
85                 WFD_MANAGER_MANAGE_INTERFACE,
86                 "PeerIPAssigned",
87                 wifi_direct_process_manage_peer_ip_assigned
88         },
89         {
90                 0,
91                 WFD_MANAGER_MANAGE_INTERFACE,
92                 "DiscoveryStarted",
93                 wifi_direct_process_manage_discovery_started
94         },
95         {
96                 0,
97                 WFD_MANAGER_MANAGE_INTERFACE,
98                 "ListenStarted",
99                 wifi_direct_process_manage_listen_started
100         },
101         {
102                 0,
103                 WFD_MANAGER_MANAGE_INTERFACE,
104                 "DiscoveryFinished",
105                 wifi_direct_process_manage_discovery_finished
106         },
107         {
108                 0,
109                 WFD_MANAGER_MANAGE_INTERFACE,
110                 "PeerFound",
111                 wifi_direct_process_manage_peer_found
112         },
113         {
114                 0,
115                 WFD_MANAGER_MANAGE_INTERFACE,
116                 "PeerLost",
117                 wifi_direct_process_manage_peer_lost
118         },
119         {
120                 0,
121                 WFD_MANAGER_GROUP_INTERFACE,
122                 "Created",
123                 wifi_direct_process_group_created
124         },
125         {
126                 0,
127                 WFD_MANAGER_GROUP_INTERFACE,
128                 "Destroyed",
129                 wifi_direct_process_group_destroyed
130         },
131         {
132                 0,
133                 WFD_MANAGER_SERVICE_INTERFACE,
134                 "DiscoveryStarted",
135                 wifi_direct_process_service_discovery_started
136         },
137         {
138                 0,
139                 WFD_MANAGER_SERVICE_INTERFACE,
140                 "DiscoveryFound",
141                 wifi_direct_process_service_discovery_found
142         },
143         {
144                 0,
145                 WFD_MANAGER_SERVICE_INTERFACE,
146                 "DiscoveryFinished",
147                 wifi_direct_process_service_discovery_finished
148         },
149         {
150                 0,
151                 NULL,
152                 NULL,
153                 NULL
154         }
155 };
156
157 GVariant *wifi_direct_dbus_method_call_sync_debug(const char* interface_name,
158                                                   const char* method,
159                                                   GVariant *params,
160                                                   GError **error,
161                                                   const char *calling_func)
162 {
163         GVariant *reply = NULL;
164
165         if (gdbus_conn.connection == NULL) {
166                 WDC_LOGE("GDBusconnection is NULL"); //LCOV_EXCL_LINE
167                 return reply; //LCOV_EXCL_LINE
168         }
169
170         WDC_LOGD("[%s][%s.%s]", calling_func, interface_name, method);
171         DBUS_DEBUG_VARIANT(params);
172
173         reply = g_dbus_connection_call_sync(gdbus_conn.connection,
174                                             WFD_MANAGER_SERVICE, /* bus name */
175                                             WFD_MANAGER_PATH, /* object path */
176                                             interface_name, /* interface name */
177                                             method, /* method name */
178                                             params, /* GVariant *params */
179                                             NULL, /* reply_type */
180                                             G_DBUS_CALL_FLAGS_NONE, /* flags */
181                                             WIFI_DIRECT_DBUS_REPLY_TIMEOUT_SYNC,
182                                             /* timeout */
183                                             NULL, /* cancellable */
184                                             error); /* error */
185         DBUS_DEBUG_VARIANT(reply);
186         return reply;
187 }
188
189 gboolean wifi_direct_dbus_init(void)
190 {
191         GError *Error = NULL;
192         int i = 0;
193
194         if (gdbus_conn.connection != NULL) {
195                 WDC_LOGI("GDbusConnection already initialized");
196                 return TRUE;
197         }
198
199         gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &Error);
200         if (gdbus_conn.connection == NULL) {
201                 WDC_LOGE("Failed to get connection, Error[%s]", Error->message);
202                 g_error_free(Error); //LCOV_EXCL_LINE
203                 return FALSE; //LCOV_EXCL_LINE
204         }
205
206         /* subscribe signal handler */
207         for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
208                 wifi_direct_dbus_signal_map[i].sub_id =
209                         g_dbus_connection_signal_subscribe(gdbus_conn.connection,
210                                                            WFD_MANAGER_SERVICE, /* bus name */
211                                                            wifi_direct_dbus_signal_map[i].interface, /* interface */
212                                                            wifi_direct_dbus_signal_map[i].member, /* member */
213                                                            WFD_MANAGER_PATH, /* object_path */
214                                                            NULL, /* arg0 */
215                                                            G_DBUS_SIGNAL_FLAGS_NONE,
216                                                            wifi_direct_dbus_signal_map[i].function,
217                                                            NULL,
218                                                            NULL);
219                 WDC_LOGD("Subscribed dbus signals [%d]", wifi_direct_dbus_signal_map[i].sub_id);
220         }
221
222         return TRUE;
223 }
224
225 void wifi_direct_dbus_deinit(void)
226 {
227         int i = 0;
228
229         if (gdbus_conn.connection == NULL)
230                 return;
231
232         /* unsubscribe signal handler */
233         for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
234                 g_dbus_connection_signal_unsubscribe(gdbus_conn.connection,
235                                                      wifi_direct_dbus_signal_map[i].sub_id);
236                 wifi_direct_dbus_signal_map[i].sub_id = 0;
237         }
238
239         /* unref gdbus connection */
240         g_object_unref(gdbus_conn.connection);
241         gdbus_conn.connection = NULL;
242 }
243
244 //LCOV_EXCL_START
245 int wifi_direct_dbus_unpack_ay(unsigned char *dst, GVariant *src, int size)
246 {
247         GVariantIter *iter = NULL;
248         int length = 0;
249         int rv = 1;
250
251         if (!dst || !src || size == 0) {
252                 WDC_LOGE("Invalid parameter");
253                 return -1;
254         }
255         g_variant_get(src, "ay", &iter);
256         if (iter == NULL) {
257                 WDC_LOGE("failed to get iterator");
258                 return -1;
259         }
260
261         while (g_variant_iter_loop(iter, "y", &dst[length])) {
262                 length++;
263                 if (length >= size)
264                         break;
265         }
266         g_variant_iter_free(iter);
267
268         if (length < size) {
269                 WDC_LOGE("array is shorter than size");
270                 rv = -1;
271         }
272
273         return rv;
274 }
275 //LCOV_EXCL_STOP