c62979af21f4f1c5049c830abd0b9d27b611c48b
[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 #include <wifi-direct.h>
33 #include "wifi-direct-ipc.h"
34 #include "wifi-direct-manager.h"
35
36 /* introspection xml to register interfaces */
37 const gchar wfd_manager_introspection_xml[] = {
38         "<node name='/net/wifidirect'>"
39                 "<interface name='net.wifidirect'>"
40                         "<method name='Activate'>"
41                                 "<arg type='i' name='error_code' direction='out'/>"
42                         "</method>"
43                         "<method name='AddClient'>"
44                                 "<arg type='i' name='result' direction='out'/>"
45                         "</method>"
46                         "<method name='Deactivate'>"
47                                 "<arg type='i' name='error_code' direction='out'/>"
48                         "</method>"
49                         "<method name='StartDiscovery'>"
50                                 "<arg type='a{sv}' name='parameters' direction='in'/>"
51                                 "<arg type='i' name='error_code' direction='out'/>"
52                         "</method>"
53                         "<method name='StopDiscovery'>"
54                                 "<arg type='i' name='error_code' direction='out'/>"
55                         "</method>"
56                         "<method name='GetDiscoveredPeers'>"
57                                 "<arg type='i' name='error_code' direction='out'/>"
58                                 "<arg type='aa{sv}' name='peer_details_list' direction='out'/>"
59                         "</method>"
60                         "<method name='Connect'>"
61                                 "<arg type='s' name='mac_address' direction='in'/>"
62                                 "<arg type='i' name='error_code' direction='out'/>"
63                         "</method>"
64                         "<method name='Disconnect'>"
65                                 "<arg type='s' name='mac_address' direction='in'/>"
66                                 "<arg type='i' name='error_code' direction='out'/>"
67                         "</method>"
68                         "<method name='CancelConnection'>"
69                                 "<arg type='s' name='mac_address' direction='in'/>"
70                                 "<arg type='i' name='error_code' direction='out'/>"
71                         "</method>"
72                         "<method name='AcceptConnection'>"
73                                 "<arg type='s' name='mac_address' direction='in'/>"
74                                 "<arg type='i' name='error_code' direction='out'/>"
75                         "</method>"
76                         "<method name='RejectConnection'>"
77                                 "<arg type='s' name='mac_address' direction='in'/>"
78                                 "<arg type='i' name='error_code' direction='out'/>"
79                         "</method>"
80                         "<method name='DisconnectAll'>"
81                                 "<arg type='i' name='error_code' direction='out'/>"
82                         "</method>"
83                         "<method name='GetConnectedPeers'>"
84                                 "<arg type='i' name='error_code' direction='out'/>"
85                                 "<arg type='aa{sv}' name='peer_details_list' direction='out'/>"
86                         "</method>"
87                         "<method name='GetConnectingPeer'>"
88                                 "<arg type='i' name='error_code' direction='out'/>"
89                                 "<arg type='a{sv}' name='peer_details' direction='out'/>"
90                         "</method>"
91                         "<method name='IsDiscoverable'>"
92                                 "<arg type='b' name='result' direction='out'/>"
93                         "</method>"
94                         "<method name='IsListeningOnly'>"
95                                 "<arg type='b' name='result' direction='out'/>"
96                         "</method>"
97                         "<method name='GetPeerInfo'>"
98                                 "<arg type='s' name='mac_address' direction='in'/>"
99                                 "<arg type='i' name='error_code' direction='out'/>"
100                                 "<arg type='a{sv}' name='peer_details' direction='out'/>"
101                         "</method>"
102                         "<method name='GetPeerVsie'>"
103                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
104                                 "<arg type='i' name='error_code' direction='out'/>"
105                                 "<arg type='s' name='vsie' direction='out'/>"
106                         "</method>"
107                         "<method name='GetState'>"
108                                 "<arg type='i' name='error_code' direction='out'/>"
109                                 "<arg type='i' name='state' direction='out'/>"
110                         "</method>"
111                         "<signal name='Activation'>"
112                                 "<arg type='i' name='error_code'/>"
113                         "</signal>"
114                         "<signal name='Deactivation'>"
115                                 "<arg type='i' name='error_code'/>"
116                         "</signal>"
117                         "<signal name='Connection'>"
118                                 "<arg type='i' name='error_code'/>"
119                                 "<arg type='i' name='connection_state'/>"
120                                 "<arg type='s' name='peer_mac_address'/>"
121                         "</signal>"
122                         "<signal name='Disconnection'>"
123                                 "<arg type='i' name='error_code'/>"
124                                 "<arg type='i' name='connection_state'/>"
125                                 "<arg type='s' name='peer_mac_address'/>"
126                         "</signal>"
127                         "<signal name='ListenStarted'>"
128                         "</signal>"
129                         "<signal name='DiscoveryStarted'>"
130                         "</signal>"
131                         "<signal name='DiscoveryFinished'>"
132                         "</signal>"
133                         "<signal name='PeerFound'>"
134                                 "<arg type='s' name='peer_mac_address'/>"
135                         "</signal>"
136                         "<signal name='PeerLost'>"
137                                 "<arg type='s' name='peer_mac_address'/>"
138                         "</signal>"
139                         "<signal name='PeerIPAssigned'>"
140                                 "<arg type='s' name='peer_mac_address'/>"
141                                 "<arg type='s' name='assigned_ip_address'/>"
142                         "</signal>"
143                 "</interface>"
144                 "<interface name='net.wifidirect.group'>"
145                         "<method name='CreateGroup'>"
146                                 "<arg type='a{sv}' name='parameters' direction='in'/>"
147                                 "<arg type='i' name='error_code' direction='out'/>"
148                         "</method>"
149                         "<method name='DestroyGroup'>"
150                                 "<arg type='i' name='error_code' direction='out'/>"
151                         "</method>"
152                         "<method name='IsGroupOwner'>"
153                                 "<arg type='b' name='result' direction='out'/>"
154                         "</method>"
155                         "<method name='IsAutoGroup'>"
156                                 "<arg type='b' name='result' direction='out'/>"
157                         "</method>"
158                         "<method name='ActivatePushButton'>"
159                                 "<arg type='i' name='error_code' direction='out'/>"
160                         "</method>"
161                         "<method name='GetPersistentGroups'>"
162                                 "<arg type='i' name='error_code' direction='out'/>"
163                                 "<arg type='aa{sv}' name='result' direction='out'/>"
164                         "</method>"
165                         "<method name='RemovePersistentGroup'>"
166                                 "<arg type='s' name='mac_address' direction='in'/>"
167                                 "<arg type='s' name='ssid' direction='in'/>"
168                                 "<arg type='i' name='error_code' direction='out'/>"
169                         "</method>"
170                         "<method name='SetPassphrase'>"
171                                 "<arg type='s' name='passphrase' direction='in'/>"
172                                 "<arg type='i' name='error_code' direction='out'/>"
173                         "</method>"
174                         "<method name='GetPassphrase'>"
175                                 "<arg type='i' name='error_code' direction='out'/>"
176                                 "<arg type='s' name='passphrase' direction='out'/>"
177                         "</method>"
178                         "<method name='SetPersistentGroupEnabled'>"
179                                 "<arg type='b' name='enable' direction='in'/>"
180                                 "<arg type='i' name='error_code' direction='out'/>"
181                         "</method>"
182                         "<method name='IsPersistentGroupEnabled'>"
183                                 "<arg type='b' name='result' direction='out'/>"
184                         "</method>"
185                         "<method name='RemovePersistentDevice'>"
186                                 "<arg type='s' name='mac_address' direction='in'/>"
187                                 "<arg type='i' name='error_code' direction='out'/>"
188                         "</method>"
189                         "<method name='RemoveAllPersistentDevice'>"
190                                 "<arg type='i' name='error_code' direction='out'/>"
191                         "</method>"
192                         "<signal name='Created'>"
193                         "</signal>"
194                         "<signal name='Destroyed'>"
195                         "</signal>"
196                 "</interface>"
197                 "<interface name='net.wifidirect.config'>"
198                         "<method name='GetDeviceName'>"
199                                 "<arg type='i' name='error_code' direction='out'/>"
200                                 "<arg type='s' name='device_name' direction='out'/>"
201                         "</method>"
202                         "<method name='SetDeviceName'>"
203                                 "<arg type='s' name='device_name' direction='in'/>"
204                                 "<arg type='i' name='error_code' direction='out'/>"
205                         "</method>"
206                         "<method name='SetWpsPin'>"
207                                 "<arg type='s' name='wps_pin' direction='in'/>"
208                                 "<arg type='i' name='error_code' direction='out'/>"
209                         "</method>"
210                         "<method name='GetWpsPin'>"
211                                 "<arg type='i' name='error_code' direction='out'/>"
212                                 "<arg type='s' name='wps_pin' direction='out'/>"
213                         "</method>"
214                         "<method name='GenerateWpsPin'>"
215                                 "<arg type='i' name='error_code' direction='out'/>"
216                                 "<arg type='s' name='wps_pin' direction='out'/>"
217                         "</method>"
218                         "<method name='GetSupportedWpsMode'>"
219                                 "<arg type='i' name='error_code' direction='out'/>"
220                                 "<arg type='i' name='config_methods' direction='out'/>"
221                         "</method>"
222                         "<method name='GetReqWpsMode'>"
223                                 "<arg type='i' name='error_code' direction='out'/>"
224                                 "<arg type='i' name='req_wps_mode' direction='out'/>"
225                         "</method>"
226                         "<method name='SetReqWpsMode'>"
227                                 "<arg type='i' name='req_wps_mode' direction='in'/>"
228                                 "<arg type='i' name='error_code' direction='out'/>"
229                         "</method>"
230                         "<method name='GetLocalWpsMode'>"
231                                 "<arg type='i' name='error_code' direction='out'/>"
232                                 "<arg type='i' name='local_wps_mode' direction='out'/>"
233                         "</method>"
234                         "<method name='GetIPAddress'>"
235                                 "<arg type='i' name='error_code' direction='out'/>"
236                                 "<arg type='s' name='local_ip_address' direction='out'/>"
237                         "</method>"
238                         "<method name='GetMacAddress'>"
239                                 "<arg type='i' name='error_code' direction='out'/>"
240                                 "<arg type='s' name='local_mac_address' direction='out'/>"
241                         "</method>"
242                         "<method name='GetGoIntent'>"
243                                 "<arg type='i' name='error_code' direction='out'/>"
244                                 "<arg type='i' name='go_intent' direction='out'/>"
245                         "</method>"
246                         "<method name='SetGoIntent'>"
247                                 "<arg type='i' name='go_intent' direction='in'/>"
248                                 "<arg type='i' name='error_code' direction='out'/>"
249                         "</method>"
250                         "<method name='SetGoIntentPerType'>"
251                                 "<arg type='i' name='type' direction='in'/>"
252                                 "<arg type='i' name='go_intent' direction='in'/>"
253                                 "<arg type='i' name='error_code' direction='out'/>"
254                         "</method>"
255                         "<method name='GetGoIntentPerType'>"
256                             "<arg type='i' name='type' direction='in'/>"
257                                 "<arg type='i' name='error_code' direction='out'/>"
258                                 "<arg type='i' name='go_intent' direction='out'/>"
259                         "</method>"
260                         "<method name='GetMaxClient'>"
261                                 "<arg type='i' name='error_code' direction='out'/>"
262                                 "<arg type='i' name='max_client' direction='out'/>"
263                         "</method>"
264                         "<method name='SetMaxClient'>"
265                                 "<arg type='i' name='max_client' direction='in'/>"
266                                 "<arg type='i' name='error_code' direction='out'/>"
267                         "</method>"
268                         "<method name='SetAutoConnectionMode'>"
269                                 "<arg type='b' name='auto_connection_mode' direction='in'/>"
270                                 "<arg type='i' name='error_code' direction='out'/>"
271                         "</method>"
272                         "<method name='IsAutoConnectionMode'>"
273                                 "<arg type='i' name='error_code' direction='out'/>"
274                                 "<arg type='b' name='result' direction='out'/>"
275                         "</method>"
276                         "<method name='GetOperatingChannel'>"
277                                 "<arg type='i' name='error_code' direction='out'/>"
278                                 "<arg type='i' name='operating_channel' direction='out'/>"
279                         "</method>"
280                         "<method name='SetAutoConnectionPeer'>"
281                                 "<arg type='s' name='peer_mac_address' direction='in'/>"
282                                 "<arg type='i' name='error_code' direction='out'/>"
283                         "</method>"
284                         "<method name='GetConnectingPeer'>"
285                                 "<arg type='i' name='error_code' direction='out'/>"
286                                 "<arg type='s' name='local_mac_address' direction='out'/>"
287                         "</method>"
288                         "<method name='GetInterfaceName'>"
289                                 "<arg type='i' name='error_code' direction='out'/>"
290                                 "<arg type='s' name='ifname' direction='out'/>"
291                         "</method>"
292                         "<method name='GetSubnetMask'>"
293                                 "<arg type='i' name='error_code' direction='out'/>"
294                                 "<arg type='s' name='subnet_mask' direction='out'/>"
295                         "</method>"
296                         "<method name='GetGateway'>"
297                                 "<arg type='i' name='error_code' direction='out'/>"
298                                 "<arg type='s' name='gateway_address' direction='out'/>"
299                         "</method>"
300                         "<method name='GetSessionTimer'>"
301                                 "<arg type='i' name='error_code' direction='out'/>"
302                                 "<arg type='i' name='session_timer' direction='out'/>"
303                         "</method>"
304                         "<method name='SetSessionTimer'>"
305                                 "<arg type='i' name='session_timer' direction='in'/>"
306                                 "<arg type='i' name='error_code' direction='out'/>"
307                         "</method>"
308                         "<method name='SetAutoGroupRemoval'>"
309                                 "<arg type='b' name='enable' direction='in'/>"
310                                 "<arg type='i' name='error_code' direction='out'/>"
311                         "</method>"
312                         "<method name='GetPrimaryDevType'>"
313                                 "<arg type='i' name='pri_dev_type' direction='out'/>"
314                                 "<arg type='i' name='error_code' direction='out'/>"
315                         "</method>"
316                         "<method name='GetSecondaryDevType'>"
317                                 "<arg type='i' name='sec_dev_type' direction='out'/>"
318                                 "<arg type='i' name='error_code' direction='out'/>"
319                         "</method>"
320                         "<method name='GetPeerRssi'>"
321                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
322                                 "<arg type='i' name='error_code' direction='out'/>"
323                                 "<arg type='i' name='rssi' direction='out'/>"
324                         "</method>"
325                         "<method name='AddVsie'>"
326                                 "<arg type='i' name='frame_id' direction='in'/>"
327                                 "<arg type='s' name='vsie' direction='in'/>"
328                                 "<arg type='i' name='error_code' direction='out'/>"
329                         "</method>"
330                         "<method name='GetVsie'>"
331                                 "<arg type='i' name='frame_id' direction='in'/>"
332                                 "<arg type='i' name='error_code' direction='out'/>"
333                                 "<arg type='s' name='vsie' direction='out'/>"
334                         "</method>"
335                         "<method name='RemoveVsie'>"
336                                 "<arg type='i' name='frame_id' direction='in'/>"
337                                 "<arg type='s' name='vsie' direction='in'/>"
338                                 "<arg type='i' name='error_code' direction='out'/>"
339                         "</method>"
340                         "<method name='GetWpsConfigMethod'>"
341                                 "<arg type='i' name='error_code' direction='out'/>"
342                                 "<arg type='i' name='req_wps_mode' direction='out'/>"
343                         "</method>"
344                         "<method name='SetWpsConfigMethod'>"
345                                 "<arg type='i' name='req_wps_mode' direction='in'/>"
346                                 "<arg type='i' name='error_code' direction='out'/>"
347                         "</method>"
348                 "</interface>"
349                 "<interface name='net.wifidirect.service'>"
350                         "<method name='StartDiscovery'>"
351                                 "<arg type='i' name='service_type' direction='in'/>"
352                                 "<arg type='s' name='mac_addr' direction='in'/>"
353                                 "<arg type='i' name='error_code' direction='out'/>"
354                         "</method>"
355                         "<method name='StopDiscovery'>"
356                                 "<arg type='i' name='service_type' direction='in'/>"
357                                 "<arg type='s' name='mac_addr' direction='in'/>"
358                                 "<arg type='i' name='error_code' direction='out'/>"
359                         "</method>"
360                         "<method name='Register'>"
361                                 "<arg type='i' name='service_type' direction='in'/>"
362                                 "<arg type='s' name='info_string' direction='in'/>"
363                                 "<arg type='i' name='error_code' direction='out'/>"
364                                 "<arg type='i' name='service_id' direction='out'/>"
365                         "</method>"
366                         "<method name='Deregister'>"
367                                 "<arg type='i' name='service_id' direction='in'/>"
368                                 "<arg type='i' name='error_code' direction='out'/>"
369                         "</method>"
370                         "<signal name='DiscoveryStarted'>"
371                         "</signal>"
372                         "<signal name='DiscoveryFound'>"
373                                 "<arg type='i' name='service_type'/>"
374                                 "<arg type='s' name='response_data'/>"
375                                 "<arg type='s' name='peer_mac_address'/>"
376                         "</signal>"
377                         "<signal name='DiscoveryFinished'>"
378                         "</signal>"
379                 "</interface>"
380                 "<interface name='net.wifidirect.display'>"
381                         "<method name='Init'>"
382                                 "<arg type='i' name='error_code' direction='out'/>"
383                         "</method>"
384                         "<method name='Deinit'>"
385                                 "<arg type='i' name='error_code' direction='out'/>"
386                         "</method>"
387                         "<method name='SetConfig'>"
388                                 "<arg type='i' name='type' direction='in'/>"
389                                 "<arg type='i' name='port' direction='in'/>"
390                                 "<arg type='i' name='hdcp' direction='in'/>"
391                                 "<arg type='i' name='error_code' direction='out'/>"
392                         "</method>"
393                         "<method name='SetAvailiability'>"
394                                 "<arg type='i' name='availability' direction='in'/>"
395                                 "<arg type='i' name='error_code' direction='out'/>"
396                         "</method>"
397                         "<method name='GetConfig'>"
398                                 "<arg type='i' name='error_code' direction='out'/>"
399                                 "<arg type='i' name='type' direction='out'/>"
400                                 "<arg type='i' name='port' direction='out'/>"
401                                 "<arg type='i' name='hdcp' direction='out'/>"
402                         "</method>"
403                         "<method name='GetAvailiability'>"
404                                 "<arg type='i' name='error_code' direction='out'/>"
405                                 "<arg type='i' name='availability' direction='out'/>"
406                         "</method>"
407                         "<method name='GetPeerType'>"
408                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
409                                 "<arg type='i' name='error_code' direction='out'/>"
410                                 "<arg type='i' name='result' direction='out'/>"
411                         "</method>"
412                         "<method name='GetPeerAvailability'>"
413                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
414                                 "<arg type='i' name='error_code' direction='out'/>"
415                                 "<arg type='i' name='result' direction='out'/>"
416                         "</method>"
417                         "<method name='GetPeerHdcp'>"
418                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
419                                 "<arg type='i' name='error_code' direction='out'/>"
420                                 "<arg type='i' name='result' direction='out'/>"
421                         "</method>"
422                         "<method name='GetPeerPort'>"
423                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
424                                 "<arg type='i' name='error_code' direction='out'/>"
425                                 "<arg type='i' name='result' direction='out'/>"
426                         "</method>"
427                         "<method name='GetPeerThroughput'>"
428                                 "<arg type='s' name='peer_mac_addr' direction='in'/>"
429                                 "<arg type='i' name='error_code' direction='out'/>"
430                                 "<arg type='i' name='result' direction='out'/>"
431                         "</method>"
432                 "</interface>"
433                 "<interface name='net.wifidirect.asp'>"
434                         "<method name='AdvertiseService'>"
435                                 "<arg type='a{sv}' name='parameters' direction='in'/>"
436                                 "<arg type='i' name='result' direction='out'/>"
437                         "</method>"
438                         "<method name='CancelAdvertiseService'>"
439                                 "<arg type='u' name='adv_id' direction='in'/>"
440                                 "<arg type='i' name='result' direction='out'/>"
441                         "</method>"
442                         "<method name='SeekService'>"
443                                 "<arg type='a{sv}' name='parameters' direction='in'/>"
444                                 "<arg type='i' name='result' direction='out'/>"
445                         "</method>"
446                         "<method name='CancelSeekService'>"
447                                 "<arg type='t' name='search_id' direction='in'/>"
448                                 "<arg type='i' name='result' direction='out'/>"
449                         "</method>"
450                 "<method name='ConnectSession'>"
451                         "<arg type='a{sv}' name='parameters' direction='in'/>"
452                         "<arg type='i' name='result' direction='out'/>"
453                 "</method>"
454                 "<method name='ConfirmSession'>"
455                         "<arg type='a{sv}' name='parameters' direction='in'/>"
456                         "<arg type='i' name='result' direction='out'/>"
457                 "</method>"
458                         "<signal name='SearchResult'>"
459                                 "<arg type='a{sv}' name='parameters'/>"
460                         "</signal>"
461                 "<signal name='SessionRequest'>"
462                         "<arg type='a{sv}' name='parameters'/>"
463                 "</signal>"
464                 "<signal name='SessionConfigRequest'>"
465                         "<arg type='a{sv}' name='parameters'/>"
466                 "</signal>"
467                 "<signal name='ConnectStatus'>"
468                         "<arg type='a{sv}' name='parameters'/>"
469                 "</signal>"
470                 "<signal name='SessionStatus'>"
471                         "<arg type='a{sv}' name='parameters'/>"
472                 "</signal>"
473                 "<signal name='SessionPeerIPAssigned'>"
474                 "<arg type='a{sv}' name='parameters'/>"
475                 "</signal>"
476                 "</interface>"
477         "</node>"
478 };
479
480 static GDBusConnection *g_connection = NULL;
481 static guint g_owner_id = 0;  /* Name Owner ID */
482
483 static int nameowner_changed_sub_id = 0;
484
485 static void wfd_manager_dbus_register_nameowner_signal(void);
486
487 static GDBusConnection *__dbus_get_gdbus_conn(void)
488 {
489         return g_connection;
490 }
491 //LCOV_EXCL_START
492 static void wfd_manger_dbus_manage_iface_handler(
493                 GDBusConnection *connection,
494                 const gchar *sender,
495                 const gchar *object_path,
496                 const gchar *interface_name,
497                 const gchar *method_name,
498                 GVariant *parameters,
499                 GDBusMethodInvocation *invocation,
500                 gpointer user_data)
501 {
502         if (!g_strcmp0(method_name, "AddClient"))
503                 wfd_manager_add_active_client(sender);
504
505         /* Method Call */
506         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
507         DBUS_DEBUG_VARIANT(parameters);
508
509         wfd_manager_iface_manage_handler(method_name, parameters, invocation);
510
511         return;
512 }
513
514 static void wfd_manger_dbus_group_iface_handler(
515                 GDBusConnection *connection,
516                 const gchar *sender,
517                 const gchar *object_path,
518                 const gchar *interface_name,
519                 const gchar *method_name,
520                 GVariant *parameters,
521                 GDBusMethodInvocation *invocation,
522                 gpointer user_data)
523 {
524         /* Method Call */
525         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
526         DBUS_DEBUG_VARIANT(parameters);
527
528         wfd_manager_iface_group_handler(method_name, parameters, invocation);
529         return;
530 }
531
532 static void wfd_manger_dbus_config_iface_handler(
533                 GDBusConnection *connection,
534                 const gchar *sender,
535                 const gchar *object_path,
536                 const gchar *interface_name,
537                 const gchar *method_name,
538                 GVariant *parameters,
539                 GDBusMethodInvocation *invocation,
540                 gpointer user_data)
541 {
542         /* Method Call */
543         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
544         DBUS_DEBUG_VARIANT(parameters);
545
546         wfd_manager_iface_config_handler(method_name, parameters, invocation);
547         return;
548 }
549
550 static void wfd_manger_dbus_service_iface_handler(
551                 GDBusConnection *connection,
552                 const gchar *sender,
553                 const gchar *object_path,
554                 const gchar *interface_name,
555                 const gchar *method_name,
556                 GVariant *parameters,
557                 GDBusMethodInvocation *invocation,
558                 gpointer user_data)
559 {
560         /* Method Call */
561         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
562         DBUS_DEBUG_VARIANT(parameters);
563
564         wfd_manager_iface_service_handler(method_name, parameters, invocation);
565         return;
566 }
567
568 static void wfd_manger_dbus_display_iface_handler(
569                 GDBusConnection *connection,
570                 const gchar *sender,
571                 const gchar *object_path,
572                 const gchar *interface_name,
573                 const gchar *method_name,
574                 GVariant *parameters,
575                 GDBusMethodInvocation *invocation,
576                 gpointer user_data)
577 {
578         /* Method Call */
579         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
580         DBUS_DEBUG_VARIANT(parameters);
581
582         wfd_manager_iface_display_handler(method_name, parameters, invocation);
583         return;
584 }
585
586 static void wfd_manger_dbus_asp_iface_handler(
587                 GDBusConnection *connection,
588                 const gchar *sender,
589                 const gchar *object_path,
590                 const gchar *interface_name,
591                 const gchar *method_name,
592                 GVariant *parameters,
593                 GDBusMethodInvocation *invocation,
594                 gpointer user_data)
595 {
596         /* Method Call */
597         WDS_LOGD("interface : [%s], method : [%s]", interface_name, method_name);
598         DBUS_DEBUG_VARIANT(parameters);
599
600         wfd_manager_iface_asp_handler(method_name, parameters, invocation);
601         return;
602 }
603
604 /* GDBus method handler */
605
606 static const GDBusInterfaceVTable wfd_manager_dbus_manage_iface_vtable = {
607                 wfd_manger_dbus_manage_iface_handler, NULL, NULL};
608
609 static const GDBusInterfaceVTable wfd_manager_dbus_group_iface_vtable = {
610                 wfd_manger_dbus_group_iface_handler, NULL, NULL};
611
612 static const GDBusInterfaceVTable wfd_manager_dbus_config_iface_vtable = {
613                 wfd_manger_dbus_config_iface_handler, NULL, NULL};
614
615 static const GDBusInterfaceVTable wfd_manager_dbus_service_iface_vtable = {
616                 wfd_manger_dbus_service_iface_handler, NULL, NULL};
617
618 static const GDBusInterfaceVTable wfd_manager_dbus_display_iface_vtable = {
619                 wfd_manger_dbus_display_iface_handler, NULL, NULL};
620
621 static const GDBusInterfaceVTable wfd_manager_dbus_asp_iface_vtable = {
622                 wfd_manger_dbus_asp_iface_handler, NULL, NULL};
623
624
625 static struct {
626         guint reg_id;
627         const gchar *iface_name;
628         const GDBusInterfaceVTable *vtbl;
629 } wfd_manager_iface_map[] = {
630         {
631                 0,
632                 WFD_MANAGER_MANAGE_INTERFACE,
633                 &wfd_manager_dbus_manage_iface_vtable
634         },
635         {
636                 0,
637                 WFD_MANAGER_GROUP_INTERFACE,
638                 &wfd_manager_dbus_group_iface_vtable
639         },
640         {
641                 0,
642                 WFD_MANAGER_CONFIG_INTERFACE,
643                 &wfd_manager_dbus_config_iface_vtable
644         },
645         {
646                 0,
647                 WFD_MANAGER_SERVICE_INTERFACE,
648                 &wfd_manager_dbus_service_iface_vtable
649         },
650         {
651                 0,
652                 WFD_MANAGER_DISPLAY_INTERFACE,
653                 &wfd_manager_dbus_display_iface_vtable
654         },
655         {
656                 0,
657                 WFD_MANAGER_ASP_INTERFACE,
658                 &wfd_manager_dbus_asp_iface_vtable
659         },
660         {
661                 0,
662                 NULL,
663                 NULL
664         }
665 };
666
667 static guint wfd_manager_dbus_iface_register(const gchar* iface_name,
668                                       const gchar* iface_path,
669                                       GDBusNodeInfo *node_info,
670                                       const GDBusInterfaceVTable *interface_vtable)
671 {
672         GDBusInterfaceInfo *interface_info = NULL;
673         GError *Error = NULL;
674         guint reg_id = 0;
675         GDBusConnection *connection = NULL;
676
677         connection = __dbus_get_gdbus_conn();
678         if (connection == NULL) {
679                 WDS_LOGE("Dbus connection not yet initiated");
680                 return 0;
681         }
682
683         if (!iface_name || !iface_path || !node_info || !interface_vtable) {
684                 WDS_LOGE("Invalid Parameters");
685                 return 0;
686         }
687
688         /* Register interface */
689         interface_info = g_dbus_node_info_lookup_interface(node_info, iface_name);
690         if (interface_info == NULL) {
691                 WDS_LOGE("Failed to get interface info");
692                 g_dbus_node_info_unref(node_info);
693                 return 0;
694         }
695
696         reg_id = g_dbus_connection_register_object(connection, iface_path,
697                         interface_info, interface_vtable,
698                         NULL, NULL, &Error);
699         if (reg_id == 0) {
700                 WDS_LOGE("Failed to register: %s", Error->message);
701                 g_clear_error(&Error);
702                 g_dbus_node_info_unref(node_info);
703                 return 0;
704         }
705
706         WDS_LOGD("Interface Registration ID [%d], Interface Name [%s]", reg_id, iface_name);
707
708         return reg_id;
709 }
710
711 static gboolean wfd_manager_dbus_register(void)
712 {
713         GDBusNodeInfo *node_info = NULL;
714         GError *Error = NULL;
715         int count = 0;
716
717         wfd_error_register();
718
719         node_info = g_dbus_node_info_new_for_xml(wfd_manager_introspection_xml, &Error);
720         if (node_info == NULL) {
721                 WDS_LOGE("Failed to get node info, Error: %s", Error->message);
722                 g_clear_error(&Error);
723                 return FALSE;
724         }
725
726         while (wfd_manager_iface_map[count].iface_name != NULL) {
727                 wfd_manager_iface_map[count].reg_id =
728                         wfd_manager_dbus_iface_register(wfd_manager_iface_map[count].iface_name,
729                                                         WFD_MANAGER_PATH,
730                                                         node_info,
731                                                         wfd_manager_iface_map[count].vtbl);
732
733                 WDS_LOGD("Registered Interface [%d, %s]",
734                                         wfd_manager_iface_map[count].reg_id,
735                                         wfd_manager_iface_map[count].iface_name);
736
737                 count++;
738         }
739
740         g_dbus_node_info_unref(node_info);
741         return TRUE;
742 }
743
744 static void nameowner_signal_handler(GDBusConnection *connection,
745                                       const gchar *sender_name,
746                                       const gchar *object_path,
747                                       const gchar *interface_name,
748                                       const gchar *signal_name,
749                                       GVariant *parameters,
750                                       gpointer user_data)
751 {
752         gchar *name = NULL;
753         gchar *old = NULL;
754         gchar *new = NULL;
755
756         g_variant_get(parameters, "(&s&s&s)", &name, &old, &new);
757         wfd_manager_remove_active_client(name, old, new);
758 }
759
760 static void wfd_manager_dbus_register_nameowner_signal(void)
761 {
762         GDBusConnection *connection = __dbus_get_gdbus_conn();
763
764         if (connection == NULL) {
765                 WDS_LOGE("Failed to get GDbus Connection");
766                 return;
767         }
768
769         nameowner_changed_sub_id = g_dbus_connection_signal_subscribe(
770                               connection,
771                               DBUS_SERVICE,
772                               DBUS_INTERFACE,
773                               "NameOwnerChanged",
774                               DBUS_PATH,
775                               NULL,
776                               G_DBUS_SIGNAL_FLAGS_NONE,
777                               nameowner_signal_handler,
778                               NULL,
779                               NULL);
780
781         WDS_LOGD("Subscribed successfully for NameOwnerChanged signals");
782 }
783
784 static void __on_bus_acquired(GDBusConnection *connection,
785                               const gchar *name,
786                               gpointer user_data)
787 {
788         WDS_LOGD("on_bus_acquired: %s", name);
789         wfd_manager_dbus_register_nameowner_signal();
790         wfd_manager_dbus_register();
791 }
792
793 static void __on_name_acquired(GDBusConnection *connection,
794                                const gchar *name,
795                                gpointer user_data)
796 {
797         WDS_LOGD("on_name_acquired: %s", name);
798 }
799
800 static void __on_name_lost(GDBusConnection *connection,
801                            const gchar *name,
802                            gpointer user_data)
803 {
804         WDS_LOGD("on_name_lost: %s", name);
805 }
806
807 void wfd_manager_dbus_unregister_nameowner_signal(void)
808 {
809         GDBusConnection *connection = NULL;
810
811         wfd_manager_free_active_client_list();
812
813         connection = __dbus_get_gdbus_conn();
814         if (!connection) {
815                 WDS_LOGE("Already unregistered. Nothing to be done");
816                 return;
817         }
818
819         g_dbus_connection_signal_unsubscribe(connection,
820                                 nameowner_changed_sub_id);
821
822         WDS_LOGD("Unsubscribed successfully for NameOwnerChanged signals");
823 }
824
825 gboolean wfd_manager_dbus_iface_unregister(guint reg_id)
826 {
827         GDBusConnection *connection = NULL;
828
829         connection = __dbus_get_gdbus_conn();
830         if (connection == NULL) {
831                 WDS_LOGE("Dbus connection not yet initiated");
832                 return FALSE;
833         }
834
835         if (reg_id > 0) {
836                 if (g_dbus_connection_unregister_object(connection, reg_id) == FALSE)
837                         WDS_LOGE("Failed to unregister iface object");
838         }
839         return TRUE;
840 }
841
842 void wfd_manager_dbus_unregister(void)
843 {
844         int count = 0;
845
846         wfd_error_deregister();
847
848         while (wfd_manager_iface_map[count].iface_name != NULL) {
849                 wfd_manager_dbus_iface_unregister(wfd_manager_iface_map[count].reg_id);
850                 count++;
851         }
852 }
853
854 gboolean wfd_manager_dbus_init(void)
855 {
856         GError *Error = NULL;
857
858         if (g_connection != NULL) {
859                 WDS_LOGE("Conenciton already present");
860                 return TRUE;
861         }
862
863         g_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &Error);
864         if (g_connection == NULL) {
865                 WDS_LOGE("Failed to get connection, Error[%s]", Error->message);
866                 g_error_free(Error);
867                 return FALSE;
868         }
869
870         g_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
871                                     WFD_MANAGER_SERVICE,
872                                     G_BUS_NAME_OWNER_FLAGS_NONE,
873                                     __on_bus_acquired,
874                                     __on_name_acquired,
875                                     __on_name_lost,
876                                     NULL,
877                                     NULL);
878         if (g_owner_id == 0) {
879                 WDS_LOGE("Failed to get bus name");
880                 return FALSE;
881         }
882         WDS_LOGD("DBus Owner id is [%d]", g_owner_id);
883
884         return TRUE;
885 }
886
887 void wfd_manager_dbus_deinit(void)
888 {
889         if (g_connection == NULL || g_owner_id == 0)
890                 return;
891
892         g_object_unref(g_connection);
893         g_bus_unown_name(g_owner_id);
894 }
895
896 gboolean wfd_manager_dbus_emit_signal(const gchar *interface_name,
897                                       const gchar *signal_name,
898                                       GVariant *parameters)
899 {
900         gboolean rv = FALSE;
901         GError *error = NULL;
902         GDBusConnection *connection;
903
904         connection = __dbus_get_gdbus_conn();
905         if (connection == NULL) {
906                 WDS_LOGE("GDBusconnection is NULL");
907                 return 0;
908         }
909
910         DBUS_DEBUG_VARIANT(parameters);
911
912         rv = g_dbus_connection_emit_signal(connection,
913                                            NULL,
914                                            WFD_MANAGER_PATH,
915                                            interface_name,
916                                            signal_name,
917                                            parameters,
918                                            &error);
919         if (rv != TRUE) {
920                 WDS_LOGE("Failed to get node info, Error: %s", error->message);
921                 g_error_free(error);
922         } else {
923                 WDS_LOGD("[%s] signal sent on [%s] interface", signal_name, interface_name);
924         }
925
926         return rv;
927 }
928
929 void wfd_manager_dbus_return_err(int ret, GDBusMethodInvocation *invocation)
930 {
931         GError *err = NULL;
932         gchar* dbus_error_name = NULL;
933
934         if (!invocation)
935                 return;
936
937         wfd_error_set_gerror(ret, &err);
938         dbus_error_name = g_dbus_error_encode_gerror(err);
939         WDS_LOGD("g_dbus_method_invocation_return_gerror with [%s]", dbus_error_name);
940         g_free(dbus_error_name);
941         g_dbus_method_invocation_return_gerror(invocation, err);
942         g_clear_error(&err);
943         return;
944 }
945
946 void wfd_manager_dbus_reply_error_none(GDBusMethodInvocation *invocation)
947 {
948         if (!invocation)
949                 return;
950
951         g_dbus_method_invocation_return_value(invocation,
952                         g_variant_new("(i)", WIFI_DIRECT_ERROR_NONE));
953
954         return;
955 }
956
957 void wfd_manager_dbus_reply_params(GDBusMethodInvocation *invocation, GVariant *parameters)
958 {
959         if (!invocation)
960                 return;
961
962         g_dbus_method_invocation_return_value(invocation, parameters);
963
964         return;
965 }
966 //LCOV_EXCL_STOP
967 GVariant* wfd_manager_dbus_pack_ay(const unsigned char *src, int size)
968 {
969         GVariantBuilder *builder = NULL;
970         GVariant *iter = NULL;
971         int i = 0;
972
973         if (!src) {
974                 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
975                 return NULL;
976         }
977
978         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
979
980         for (i = 0; i < size; i++)
981                 g_variant_builder_add(builder, "y", src[i]);
982
983         iter = g_variant_new("ay", builder);
984
985         g_variant_builder_unref(builder);
986         return iter;
987 }