[Support Legacy Connection] Identify legacy peer connection.
[platform/core/connectivity/wifi-direct-manager.git] / plugin / wpasupplicant / ctrl_iface_dbus / wfd-plugin-wpasupplicant.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012 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 wpasupplicant dbus plugin functions.
22  *
23  * @file                wfd-plugin-dbus-wpasupplicant.c
24  * @author      Jiung Yu (jiung.yu@samsung.com)
25  * @version     0.7
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #define _GNU_SOURCE
38 #include <poll.h>
39 #include <errno.h>
40 #include <sys/ioctl.h>
41 #include <net/if.h>
42
43 #include <glib.h>
44 #include <gio/gio.h>
45
46 #include <tzplatform_config.h>
47
48 #include "wifi-direct-oem.h"
49 #include "wfd-plugin-log.h"
50 #include "wfd-plugin-wpasupplicant.h"
51 #include "dbus/wfd-plugin-supplicant-dbus.h"
52
53 #if defined(TIZEN_FEATURE_ASP)
54 #define GLIST_ITER_START(arg_list, elem)\
55         GList *temp = NULL;\
56         temp = g_list_first(arg_list);\
57         while (temp) {\
58                 elem = temp->data;\
59                 temp = g_list_next(temp);\
60
61 #define GLIST_ITER_END() }
62 #endif
63
64 #define NETCONFIG_SERVICE "net.netconfig"
65 #define NETCONFIG_WIFI_INTERFACE "net.netconfig.wifi"
66 #define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
67
68 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
69
70 #if defined TIZEN_MOBILE
71 #define DEFAULT_MAC_FILE_PATH tzplatform_mkpath(TZ_SYS_ETC, ".mac.info")
72 #endif
73
74 #if defined TIZEN_WIFI_MODULE_BUNDLE
75 #define DEFAULT_MAC_FILE_PATH "/sys/class/net/wlan0/address"
76 #endif
77
78 #ifndef DEFAULT_MAC_FILE_PATH
79 #define DEFAULT_MAC_FILE_PATH "/sys/class/net/p2p0/address"
80 #endif
81
82 static wfd_oem_ops_s supplicant_ops = {
83         .init = ws_init,
84         .deinit = ws_deinit,
85         .activate = ws_activate,
86         .deactivate = ws_deactivate,
87
88         .start_scan = ws_start_scan,
89         .stop_scan = ws_stop_scan,
90         .get_visibility = ws_get_visibility,
91         .set_visibility = ws_set_visibility,
92         .get_scan_result = ws_get_scan_result,
93         .get_peer_info = ws_get_peer_info,
94
95         .prov_disc_req = ws_prov_disc_req,
96
97         .connect = ws_connect,
98         .disconnect = ws_disconnect,
99         .reject_connection = ws_reject_connection,
100         .cancel_connection = ws_cancel_connection,
101
102         .get_connected_peers = ws_get_connected_peers,
103         .get_pin = ws_get_pin,
104         .set_pin = ws_set_pin,
105         .generate_pin = ws_generate_pin,
106         .get_supported_wps_mode = ws_get_supported_wps_mode,
107
108         .create_group = ws_create_group,
109         .destroy_group = ws_destroy_group,
110         .invite = ws_invite,
111         .wps_start = ws_wps_start,
112         .enrollee_start = ws_enrollee_start,
113         .wps_cancel = ws_wps_cancel,
114
115         .get_dev_name = ws_get_dev_name,
116         .set_dev_name = ws_set_dev_name,
117         .get_dev_mac = ws_get_dev_mac,
118         .get_dev_type = ws_get_dev_type,
119         .set_dev_type = ws_set_dev_type,
120         .get_go_intent = ws_get_go_intent,
121         .set_go_intent = ws_set_go_intent,
122         .set_country = ws_set_country,
123         .get_persistent_groups = ws_get_persistent_groups,
124         .remove_persistent_group = ws_remove_persistent_group,
125         .set_persistent_reconnect = ws_set_persistent_reconnect,
126
127 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
128         .start_service_discovery = ws_start_service_discovery,
129         .cancel_service_discovery = ws_cancel_service_discovery,
130
131         .serv_add = ws_serv_add,
132         .serv_del = ws_serv_del,
133 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
134
135 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
136         .miracast_init = ws_miracast_init,
137         .set_display = ws_set_display,
138 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
139
140         .refresh = ws_refresh,
141         .save_config =  ws_save_config,
142         .set_operating_channel = ws_set_operating_channel,
143         .remove_all_network = ws_remove_all_network,
144         .get_wpa_status = ws_get_wpa_status,
145
146 #if defined(TIZEN_FEATURE_ASP)
147         .advertise_service = ws_advertise_service,
148         .cancel_advertise_service = ws_cancel_advertise_service,
149         .seek_service = ws_seek_service,
150         .cancel_seek_service = ws_cancel_seek_service,
151         .asp_prov_disc_req = ws_asp_prov_disc_req,
152 #endif /* TIZEN_FEATURE_ASP */
153         };
154
155 static ws_dbus_plugin_data_s *g_pd;
156
157 static int is_peer_joined_notified = 0;
158 static int is_peer_disconnected_notified = 0;
159
160 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
161 static GList *service_list;
162 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
163
164 static GList *seek_list;
165
166 static void _supplicant_signal_cb(GDBusConnection *connection,
167                 const gchar *sender, const gchar *object_path, const gchar *interface,
168                 const gchar *signal, GVariant *parameters, gpointer user_data);
169
170 static void _p2pdevice_signal_cb(GDBusConnection *connection,
171                 const gchar *sender, const gchar *object_path, const gchar *interface,
172                 const gchar *signal, GVariant *parameters, gpointer user_data);
173
174 static void _group_signal_cb(GDBusConnection *connection,
175                 const gchar *sender, const gchar *object_path, const gchar *interface,
176                 const gchar *signal, GVariant *parameters, gpointer user_data);
177
178 static void _interface_signal_cb(GDBusConnection *connection,
179                 const gchar *sender, const gchar *object_path, const gchar *interface,
180                 const gchar *signal, GVariant *parameters, gpointer user_data);
181
182 static int __ws_txt_to_mac(unsigned char *txt, unsigned char *mac)
183 {
184         int i = 0;
185
186         if (!txt || !mac) {
187                 WDP_LOGE("Invalid parameter");
188                 return -1;
189         }
190
191         for (;;) {
192                 mac[i++] = (char) strtoul((char *)txt, (char **)&txt, 16);
193                 if (!*txt++ || i == 6)
194                         break;
195         }
196
197         if (i != WS_MACADDR_LEN)
198                 return -1;
199
200         WDP_LOGD("Converted MAC address [" MACSECSTR "]", MAC2SECSTR(mac));
201         return 0;
202 }
203
204 static int __ws_mac_compact_to_normal(char *compact, unsigned char *mac)
205 {
206         g_snprintf((char *)mac, OEM_MACSTR_LEN, "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
207                         compact[0], compact[1], compact[2], compact[3],
208                         compact[4], compact[5], compact[6], compact[7],
209                         compact[8], compact[9], compact[10], compact[11]);
210         return 0;
211 }
212
213 static char *__ws_wps_to_txt(int wps_mode)
214 {
215         switch (wps_mode) {
216         case WFD_OEM_WPS_MODE_PBC:
217                 return WS_DBUS_STR_PBC;
218                 break;
219         case WFD_OEM_WPS_MODE_DISPLAY:
220                 return WS_DBUS_STR_DISPLAY;
221                 break;
222         case WFD_OEM_WPS_MODE_KEYPAD:
223                 return WS_DBUS_STR_KEYPAD;
224                 break;
225 #if defined(TIZEN_FEATURE_ASP)
226         case WFD_OEM_WPS_MODE_NONE:
227         case WFD_OEM_WPS_MODE_P2PS:
228                 return WS_DBUS_STR_P2PS;
229 #endif /* TIZEN_FEATURE_ASP */
230         default:
231                 return "";
232                 break;
233         }
234 }
235 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
236 static int __ws_byte_to_hex(char *buf, int buf_size, unsigned char *data, int data_len)
237 {
238         int i;
239         char *pos = buf;
240         char *end = buf + buf_size;
241         int ret;
242         if (buf_size == 0)
243                 return 0;
244         for (i = 0; i < data_len; i++) {
245                 ret = snprintf(pos, end - pos, "%02x", data[i]);
246                 if (ret < 0 || ret >= end - pos) {
247                         end[-1] = '\0';
248                         return pos - buf;
249                 }
250                 pos += ret;
251         }
252         end[-1] = '\0';
253         return pos - buf;
254 }
255
256 static int __ws_hex_to_num(char *src, int len)
257 {
258         char *temp = NULL;
259         int num = 0;
260
261         if (!src || len < 0) {
262                 WDP_LOGE("Invalid parameter");
263                 return -1;
264         }
265
266         temp = (char*) g_try_malloc0(len+1);
267         if (!temp) {
268                 WDP_LOGE("Failed to allocate memory");
269                 return -1;
270         }
271
272         memcpy(temp, src, len);
273         num = strtoul(temp, NULL, 16);
274         g_free(temp);
275
276         return num;
277 }
278
279 static int __ws_segment_to_service(char *segment, wfd_oem_new_service_s **service)
280 {
281         wfd_oem_new_service_s *serv_tmp = NULL;
282         char *ptr = NULL;
283         char *temp = NULL;
284         int len = 0;
285         int i = 0;
286
287         if (!segment || !service) {
288                 WDP_LOGE("Invalid parameter");
289                 return -1;
290         }
291
292         ptr = segment;
293         WDP_LOGD("Segment: %s", segment);
294
295         serv_tmp = (wfd_oem_new_service_s*) g_try_malloc0(sizeof(wfd_oem_new_service_s));
296         if (!serv_tmp) {
297                 WDP_LOGE("Failed to allocate memory for service");
298                 return -1;
299         }
300
301         serv_tmp->protocol = __ws_hex_to_num(ptr, 2);
302         serv_tmp->trans_id = __ws_hex_to_num(ptr+2, 2);
303         serv_tmp->status = __ws_hex_to_num(ptr+4, 2);
304         ptr += 6;
305         WDP_LOGD("Protocol[%d], Transaction ID[%d], Status[%d]", serv_tmp->protocol, serv_tmp->trans_id, serv_tmp->status);
306
307         if (serv_tmp->status != 0) {
308                 WDP_LOGE("Service status is not success");
309                 free(serv_tmp);
310                 return -1;
311         }
312
313         if (serv_tmp->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
314                 WDP_LOGD("===== Bonjour service =====");
315                 char compr[5] = {0, };
316                 char query[256] = {0, };
317                 char rdata[256] = {0, };
318                 int dns_type = 0;
319
320                 while (*ptr != 0 && strncmp(ptr, "c0", 2)) {
321                         len = __ws_hex_to_num(ptr, 2);
322                         ptr += 2;
323                         if (len && len <= 0xff) {
324                                 temp = (char*) calloc(1, len+2);
325                                 if (temp) {
326                                         temp[0] = '.';
327                                         for (i = 0; i < len; i++) {
328                                                 temp[i+1] = (char) __ws_hex_to_num(ptr, 2);
329                                                 ptr += 2;
330                                         }
331                                         strncat(query, temp, len+1);
332                                         g_free(temp);
333                                         temp = NULL;
334                                 }
335                         }
336                 }
337
338                 if (!strncmp(ptr, "c0", 2)) {
339                         memcpy(compr, ptr, 4);
340                         ptr += 2;
341
342                         if (!strncmp(ptr, "27", 2)) {
343                                 WDP_LOGD("Segment ended");
344                                 ptr += 2;
345                         } else {
346                                 ptr += 2;
347                                 dns_type = __ws_hex_to_num(ptr, 4);
348                                 ptr += 6;
349                                 if (dns_type == 12) {
350                                         if (!strncmp(compr, "c011", 4))
351                                                 strncat(query, ".local.", 7);
352                                         else if (!strncmp(compr, "c00c", 4))
353                                                 strncat(query, "._tcp.local.", 12);
354                                         else if (!strncmp(compr, "c01c", 4))
355                                                 strncat(query, "._udp.local.", 12);
356                                 }
357                         }
358                 }
359                 serv_tmp->data.bonjour.query = strdup(query + 1);
360                 while (*ptr != 0 && strncmp(ptr, "c0", 2)) {
361                         len = __ws_hex_to_num(ptr, 2);
362                         ptr += 2;
363                         if (len && len <= 0xff) {
364                                 temp = (char*) g_try_malloc0(len+2);
365                                 if (temp) {
366                                         temp[0] = '.';
367                                         for (i = 0; i < len; i++) {
368                                                 temp[i+1] = (char) __ws_hex_to_num(ptr, 2);
369                                                 ptr += 2;
370                                         }
371                                         strncat(rdata, temp, len+1);
372                                         g_free(temp);
373                                         temp = NULL;
374                                 }
375                         }
376                 }
377                 serv_tmp->data.bonjour.rdata = strdup(rdata + 1);
378
379                 WDP_LOGD("Query: %s", serv_tmp->data.bonjour.query);
380                 WDP_LOGD("RData: %s", serv_tmp->data.bonjour.rdata);
381         } else {
382                 WDP_LOGE("Not supported yet. Only bonjour service supproted [%d]",
383                                         serv_tmp->protocol);
384                 g_free(serv_tmp);
385                 return -1;
386         }
387
388         *service = serv_tmp;
389
390         return 0;
391 }
392 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
393
394 static void __ws_path_to_addr(char *peer_path,
395                 unsigned char *dev_addr, GVariant *parameter)
396 {
397         __WDP_LOG_FUNC_ENTER__;
398
399         static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
400         const char *path = NULL;
401         char *loc = NULL;
402
403         g_variant_get(parameter, "(&o)", &path);
404         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
405         WDP_LOGD("Retrive Added path [%s]", peer_path);
406
407         loc = strrchr(peer_path,'/');
408         if(loc != NULL)
409                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
410
411         __ws_txt_to_mac(peer_dev, dev_addr);
412         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(dev_addr));
413
414         __WDP_LOG_FUNC_EXIT__;
415         return;
416 }
417
418 static int __ws_unpack_ay(unsigned char *dst, GVariant *src, int size)
419 {
420         GVariantIter *iter = NULL;
421         int length = 0;
422         int res = 1;
423
424         if (!dst || !src || size == 0) {
425                 WDP_LOGE("Invalid parameter");
426                 return -1;
427         }
428         g_variant_get(src, "ay", &iter);
429         if (iter == NULL) {
430                 WDP_LOGE("failed to get iterator");
431                 return -1;
432         }
433
434         while (g_variant_iter_loop (iter, "y", &dst[length])) {
435                 length++;
436                 if(length >= size)
437                         break;
438         }
439         g_variant_iter_free(iter);
440
441         if (length < size) {
442                 WDP_LOGE("array is shorter than size");
443                 res = -1;
444         }
445
446         return res;
447 }
448
449 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
450 static int __parsing_wfd_info(unsigned char *wfd_dev_info,
451                 wfd_oem_display_s *display )
452 {
453         __WDP_LOG_FUNC_ENTER__;
454
455         int wfd_info = 0;
456         if (!wfd_dev_info || !display) {
457                 WDP_LOGE("Invalid parameter");
458                 __WDP_LOG_FUNC_EXIT__;
459                 return -1;
460         }
461
462         wfd_info = (wfd_dev_info[3]<<8 | wfd_dev_info[4]);
463
464         if (wfd_info & WS_WFD_INFO_PRIMARY_SINK)
465                 display->type |= WS_WFD_INFO_PRIMARY_SINK;
466         if (wfd_info & WS_WFD_INFO_SECONDARY_SINK)
467                 display->type |= WS_WFD_INFO_SECONDARY_SINK;
468
469         display->availability = (wfd_info & WS_WFD_INFO_AVAILABILITY) >> 4;
470         display->hdcp_support = (wfd_info & WS_WFD_INFO_HDCP_SUPPORT) >> 8;
471
472         display->port = (wfd_dev_info[5]<<8 | wfd_dev_info[6]);
473         display->max_tput = (wfd_dev_info[7]<<8 | wfd_dev_info[8]);
474
475         WDP_LOGD("type [%d],availability [%d],hdcp_support [%d],ctrl_port [%d] "
476                         "max_tput[%d]", display->type, display->availability,
477                         display->hdcp_support, display->port,display->max_tput);
478
479         __WDP_LOG_FUNC_EXIT__;
480         return 0;
481 }
482 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
483
484 static int _ws_get_local_dev_mac(unsigned char *dev_mac)
485 {
486         __WDP_LOG_FUNC_ENTER__;
487         FILE *fd = NULL;
488         const char *file_path = DEFAULT_MAC_FILE_PATH;
489         char local_mac[OEM_MACSTR_LEN] = {0, };
490         char *ptr = NULL;
491         int res = 0;
492
493         errno = 0;
494         fd = fopen(file_path, "r");
495         if (!fd) {
496                 WDP_LOGE("Failed to open MAC info file [%s] (%s)",file_path, strerror(errno));
497                 __WDP_LOG_FUNC_EXIT__;
498                 return -1;
499         }
500
501         errno = 0;
502         ptr = fgets((char *)local_mac, WS_MACSTR_LEN, fd);
503         if (!ptr) {
504                 WDP_LOGE("Failed to read file or no data read(%s)", strerror(errno));
505                 fclose(fd);
506                 __WDP_LOG_FUNC_EXIT__;
507                 return -1;
508         }
509         WDP_SECLOGD("Local MAC address [%s]", ptr);
510         WDP_SECLOGD("Local MAC address [%s]", local_mac);
511
512         res = __ws_txt_to_mac((unsigned char *)local_mac, dev_mac);
513         if (res < 0) {
514                 WDP_LOGE("Failed to convert text to MAC address");
515                 fclose(fd);
516                 __WDP_LOG_FUNC_EXIT__;
517                 return -1;
518         }
519
520         WDP_LOGD("Local Device MAC address [" MACSECSTR "]", MAC2SECSTR(dev_mac));
521
522         fclose(fd);
523         __WDP_LOG_FUNC_EXIT__;
524         return 0;
525 }
526
527 static void _supplicant_signal_cb(GDBusConnection *connection,
528                 const gchar *sender, const gchar *object_path, const gchar *interface,
529                 const gchar *signal, GVariant *parameters, gpointer user_data)
530 {
531 #if defined (TIZEN_DEBUG_DBUS_VALUE)
532         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
533 #endif /* TIZEN_DEBUG_DBUS_VALUE */
534
535         if (!g_pd) {
536                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
537                 return;
538         }
539
540         if (!g_strcmp0(signal,"InterfaceAdded")) {
541                 WDP_LOGD("InterfaceAdded");
542
543         } else if (!g_strcmp0(signal,"InterfaceRemoved")) {
544                 WDP_LOGD("InterfaceRemoved");
545                 static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
546                 const char *path = NULL;
547
548                 g_variant_get(parameters, "(&o)", &path);
549                 g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
550
551                 WDP_LOGD("Retrive removed path [%s]", interface_path);
552
553                 if (!g_strcmp0(g_pd->group_iface_path, interface_path)) {
554
555                         WDP_LOGD("p2p group interface removed");
556                         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
557                 }
558 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE) && defined(TIZEN_MOBILE)
559                 else if (!g_strcmp0(g_pd->iface_path, interface_path)) {
560
561                         WDP_LOGD("p2p interface removed");
562                         wfd_oem_event_s event;
563
564                         ws_deactivate(1);
565
566                         memset(&event, 0x0, sizeof(wfd_oem_event_s));
567                         event.event_id = WFD_OEM_EVENT_DEACTIVATED;
568                         g_pd->callback(g_pd->user_data, &event);
569
570                         memset(g_pd->iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
571                 }
572 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE && TIZEN_MOBILE */
573         } else if(!g_strcmp0(signal,"PropertiesChanged")){
574                 WDP_LOGD("PropertiesChanged");
575         }
576 }
577
578 static void __ws_get_peer_property(const char *key, GVariant *value, void *user_data)
579 {
580         __WDP_LOG_FUNC_ENTER__;
581
582         wfd_oem_device_s *peer = (wfd_oem_device_s *)user_data;
583         if(!peer) {
584                 __WDP_LOG_FUNC_EXIT__;
585                 return;
586         }
587 #if defined (TIZEN_DEBUG_DBUS_VALUE)
588         CHECK_KEY_VALUE(key, value);
589 #endif /* TIZEN_DEBUG_DBUS_VALUE */
590
591         if (g_strcmp0(key, "DeviceName") == 0) {
592                 const char *name = NULL;
593
594                 g_variant_get(value, "&s", &name);
595                 g_strlcpy(peer->dev_name, name, WS_SSID_LEN);
596                 WDP_LOGD("Device name [%s]", peer->dev_name);
597
598         } else if (g_strcmp0(key, "config_method") == 0) {
599                 int config_methods = 0;
600                 g_variant_get(value, "q", &config_methods);
601
602                 if (config_methods & WS_CONFIG_METHOD_DISPLAY)
603                         peer->config_methods |= WFD_OEM_WPS_MODE_DISPLAY;
604                 if (config_methods & WS_CONFIG_METHOD_PUSHBUTTON)
605                         peer->config_methods |= WFD_OEM_WPS_MODE_PBC;
606                 if (config_methods & WS_CONFIG_METHOD_KEYPAD)
607                         peer->config_methods |= WFD_OEM_WPS_MODE_KEYPAD;
608                 WDP_LOGD("Config method [0x%x]", peer->config_methods);
609
610         } else if (g_strcmp0(key, "level") == 0) {
611
612         } else if (g_strcmp0(key, "devicecapability") == 0) {
613                 unsigned char devicecapability = 0;
614
615                 g_variant_get(value, "y", &devicecapability);
616                 peer->dev_flags = (int)devicecapability;
617                 WDP_LOGD("Device Capa [0x%02x]", peer->dev_flags);
618
619         } else if (g_strcmp0(key, "groupcapability") == 0) {
620                 unsigned char groupcapability = 0;
621
622                 g_variant_get(value, "y", &groupcapability);
623                 WDP_LOGD("Group Capa [0x%02x]", groupcapability);
624                 if (groupcapability & WS_GROUP_CAP_GROUP_OWNER) {
625                         peer->group_flags = WFD_OEM_GROUP_FLAG_GROUP_OWNER;
626                         peer->dev_role = WFD_OEM_DEV_ROLE_GO;
627                 }
628                 if (groupcapability & WS_GROUP_CAP_PERSISTENT_GROUP)
629                         peer->group_flags = WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
630
631         } else if (g_strcmp0(key, "PrimaryDeviceType") == 0) {
632                 unsigned char primarydevicetype[WS_DEVTYPE_LEN] = {0,};
633
634                 if(__ws_unpack_ay(primarydevicetype, value, WS_DEVTYPE_LEN)) {
635                         peer->pri_dev_type = primarydevicetype[1];
636                         peer->sec_dev_type = primarydevicetype[WS_DEVTYPE_LEN -1];
637                 }
638         } else if (g_strcmp0(key, "SecondaryDeviceTypes") == 0) {
639         } else if (g_strcmp0(key, "VendorExtension") == 0) {
640 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
641         } else if (g_strcmp0(key, "IEs") == 0) {
642                 unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
643
644                 if(__ws_unpack_ay(ies, value, WFD_SUBELEM_LEN_DEV_INFO + 3))
645                         __parsing_wfd_info(ies, &(peer->display));
646 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
647         } else if (g_strcmp0(key, "DeviceAddress") == 0) {
648
649                 if (__ws_unpack_ay(peer->dev_addr, value, WS_MACADDR_LEN))
650                         WDP_LOGD("Device address [" MACSTR "]", MAC2STR(peer->dev_addr));
651
652         } else if (g_strcmp0(key, "InterfaceAddress") == 0) {
653
654                 if (__ws_unpack_ay(peer->intf_addr, value, WS_MACADDR_LEN))
655                         WDP_LOGD("Interface address [" MACSTR "]", MAC2STR(peer->intf_addr));
656
657         } else if (g_strcmp0(key, "GODeviceAddress") == 0) {
658
659                 if (__ws_unpack_ay(peer->go_dev_addr, value, WS_MACADDR_LEN))
660                         WDP_LOGD("GODevice address [" MACSTR "]", MAC2STR(peer->go_dev_addr));
661
662                 if(!ISZEROMACADDR(peer->go_dev_addr))
663                         peer->dev_role = WFD_OEM_DEV_ROLE_GC;
664         } else {
665                 WDP_LOGD("Unknown value");
666         }
667         __WDP_LOG_FUNC_EXIT__;
668         return;
669 }
670
671 static void __ws_peer_property(const char *key, GVariant *value, void *user_data)
672 {
673         __WDP_LOG_FUNC_ENTER__;
674         if(!user_data) {
675                 __WDP_LOG_FUNC_EXIT__;
676                 return;
677         }
678
679         wfd_oem_dev_data_s *peer = (wfd_oem_dev_data_s *)user_data;
680 #if defined (TIZEN_DEBUG_DBUS_VALUE)
681         CHECK_KEY_VALUE(key, value);
682 #endif /* TIZEN_DEBUG_DBUS_VALUE */
683         if (g_strcmp0(key, "DeviceName") == 0) {
684                 const char *name = NULL;
685
686                 g_variant_get(value, "&s", &name);
687                 g_strlcpy(peer->name, name, WS_SSID_LEN);
688                 WDP_LOGD("Device Name [%s]", peer->name);
689
690         } else if (g_strcmp0(key, "config_method") == 0) {
691                 int config_methods = 0;
692
693                 g_variant_get(value, "q", &config_methods);
694
695                 if (config_methods & WS_CONFIG_METHOD_DISPLAY)
696                         peer->config_methods |= WFD_OEM_WPS_MODE_DISPLAY;
697                 if (config_methods & WS_CONFIG_METHOD_PUSHBUTTON)
698                         peer->config_methods |= WFD_OEM_WPS_MODE_PBC;
699                 if (config_methods & WS_CONFIG_METHOD_KEYPAD)
700                         peer->config_methods |= WFD_OEM_WPS_MODE_KEYPAD;
701                 WDP_LOGD("Config method [0x%x]", peer->config_methods);
702
703         } else if (g_strcmp0(key, "level") == 0) {
704
705         } else if (g_strcmp0(key, "devicecapability") == 0) {
706                 unsigned char devicecapability = 0;
707
708                 g_variant_get(value, "y", &devicecapability);
709                 peer->dev_flags = (int)devicecapability;
710                 WDP_LOGD("Device Capa [0x%02x]", peer->dev_flags);
711
712         } else if (g_strcmp0(key, "groupcapability") == 0) {
713                 unsigned char groupcapability = 0;
714
715                 g_variant_get(value, "y", &groupcapability);
716                 WDP_LOGD("Group Capa [0x%02x]", groupcapability);
717                 if (groupcapability & WS_GROUP_CAP_GROUP_OWNER) {
718                         peer->group_flags = WFD_OEM_GROUP_FLAG_GROUP_OWNER;
719                         peer->dev_role = WFD_OEM_DEV_ROLE_GO;
720                 }
721                 if (groupcapability & WS_GROUP_CAP_PERSISTENT_GROUP)
722                         peer->group_flags = WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
723
724         } else if (g_strcmp0(key, "PrimaryDeviceType") == 0) {
725                 unsigned char primarydevicetype[WS_DEVTYPE_LEN] = {0,};
726
727                 if (__ws_unpack_ay(primarydevicetype, value, WS_DEVTYPE_LEN)) {
728                         peer->pri_dev_type = primarydevicetype[1];
729                         peer->sec_dev_type = primarydevicetype[WS_DEVTYPE_LEN -1];
730                 }
731         } else if (g_strcmp0(key, "SecondaryDeviceTypes") == 0) {
732         } else if (g_strcmp0(key, "VendorExtension") == 0) {
733 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
734         } else if (g_strcmp0(key, "IEs") == 0) {
735                 unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
736
737                 if(__ws_unpack_ay(ies, value, WFD_SUBELEM_LEN_DEV_INFO + 3))
738                         __parsing_wfd_info(ies, &(peer->display));
739 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
740         } else if (g_strcmp0(key, "DeviceAddress") == 0) {
741
742                 if (__ws_unpack_ay(peer->p2p_dev_addr, value, WS_MACADDR_LEN))
743                         WDP_LOGD("Device address [" MACSTR "]", MAC2STR(peer->p2p_dev_addr));
744
745         } else if (g_strcmp0(key, "InterfaceAddress") == 0) {
746
747                 if (__ws_unpack_ay(peer->p2p_intf_addr, value, WS_MACADDR_LEN))
748                         WDP_LOGD("Interface Address [" MACSTR "]", MAC2STR(peer->p2p_intf_addr));
749
750         } else if (g_strcmp0(key, "GODeviceAddress") == 0) {
751
752                 unsigned char go_dev_addr[OEM_MACADDR_LEN] = {0,};
753                 if (__ws_unpack_ay(go_dev_addr, value, WS_MACADDR_LEN))
754                         WDP_LOGD("[" MACSTR "]", MAC2STR(go_dev_addr));
755
756                 if(!ISZEROMACADDR(go_dev_addr))
757                         peer->dev_role = WFD_OEM_DEV_ROLE_GC;
758 #if defined(TIZEN_FEATURE_ASP)
759         } else if (g_strcmp0(key, "AdvertiseService") == 0) {
760                 WDP_LOGD("size [%d]", g_variant_get_size(value));
761                 if (g_variant_get_size(value) != 0) {
762                         peer->has_asp_services = 1;
763                 } else {
764                         peer->has_asp_services = 0;
765                 }
766 #endif /* TIZEN_FEATURE_ASP */
767         } else {
768                 WDP_LOGD("Unknown value");
769         }
770         __WDP_LOG_FUNC_EXIT__;
771         return;
772 }
773
774 void __ws_interface_property(const char *key, GVariant *value, void *user_data)
775 {
776         __WDP_LOG_FUNC_ENTER__;
777         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
778         if(!event)
779                 return;
780 #if defined (TIZEN_DEBUG_DBUS_VALUE)
781         CHECK_KEY_VALUE(key, value);
782 #endif /* TIZEN_DEBUG_DBUS_VALUE */
783         if (g_strcmp0(key, "Ifname") == 0) {
784                 const char *ifname = NULL;
785
786                 g_variant_get(value, "&s", &ifname);
787                 g_strlcpy(event->ifname, ifname, OEM_IFACE_NAME_LEN+1);
788                 WDP_LOGD("Ifname [%s]", event->ifname);
789
790         }
791         __WDP_LOG_FUNC_EXIT__;
792         return;
793 }
794
795 void __ws_group_property(const char *key, GVariant *value, void *user_data)
796 {
797         __WDP_LOG_FUNC_ENTER__;
798         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
799         if(!event || !event->edata)
800                 return;
801
802         wfd_oem_group_data_s *group = (wfd_oem_group_data_s *)event->edata;
803 #if defined (TIZEN_DEBUG_DBUS_VALUE)
804         CHECK_KEY_VALUE(key, value);
805 #endif /* TIZEN_DEBUG_DBUS_VALUE */
806         if (g_strcmp0(key, "Role") == 0) {
807                 const char *role = NULL;
808
809                 g_variant_get(value, "&s", &role);
810                 WDP_LOGD("Role [%s]", role);
811
812                 if (!strncmp(role, "GO", 2))
813                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
814                 else if (!strncmp(role, "client", 6))
815                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
816
817         } else if (g_strcmp0(key, "Frequency") == 0) {
818                 int frequency = 0;
819
820                 g_variant_get(value, "q", &frequency);
821                 group->freq = (int)frequency;
822
823         } else if (g_strcmp0(key, "Passphrase") == 0) {
824                 const char *passphrase = NULL;
825
826                 g_variant_get(value, "&s", &passphrase);
827                 g_strlcpy(group->pass, passphrase, OEM_PASS_PHRASE_LEN+1);
828                 WDP_LOGD("passphrase [%s]", group->pass);
829
830         } else if (g_strcmp0(key, "Group") == 0) {
831
832         } else if (g_strcmp0(key, "SSID") == 0) {
833                 unsigned char ssid[WS_SSID_LEN +1] = {0,};
834
835                 __ws_unpack_ay(ssid, value, WS_SSID_LEN);
836                 memcpy(group->ssid, ssid, WS_SSID_LEN+1);
837                 WDP_LOGD("ssid [%s]", group->ssid);
838
839         } else if (g_strcmp0(key, "BSSID") == 0) {
840
841                 if (__ws_unpack_ay(group->go_dev_addr, value, WS_MACADDR_LEN))
842                         WDP_LOGD("[" MACSTR "]", MAC2STR(group->go_dev_addr));
843
844         } else {
845                 WDP_LOGD("Unknown value");
846         }
847         __WDP_LOG_FUNC_EXIT__;
848         return;
849 }
850
851 void __ws_extract_invitation_details(const char *key, GVariant *value, void *user_data)
852 {
853         __WDP_LOG_FUNC_ENTER__;
854         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
855         if(!event || !event->edata)
856                 return;
857
858         wfd_oem_invite_data_s *invitation = (wfd_oem_invite_data_s *)event->edata;
859 #if defined (TIZEN_DEBUG_DBUS_VALUE)
860         CHECK_KEY_VALUE(key, value);
861 #endif /* TIZEN_DEBUG_DBUS_VALUE */
862         if (g_strcmp0(key, "sa") == 0) {
863                 if (__ws_unpack_ay(invitation->sa, value, WS_MACADDR_LEN))
864                         WDP_LOGD("SA [" MACSTR "]", MAC2STR(invitation->sa));
865
866         } else if (g_strcmp0(key, "go_dev_addr") == 0) {
867                 if (__ws_unpack_ay(invitation->go_dev_addr, value, WS_MACADDR_LEN))
868                                         WDP_LOGD("GO device address [" MACSTR "]", MAC2STR(invitation->go_dev_addr));
869
870         } else if (g_strcmp0(key, "bssid") == 0) {
871                 if (__ws_unpack_ay(invitation->bssid, value, WS_MACADDR_LEN))
872                                         WDP_LOGD("BSSID [" MACSTR "]", MAC2STR(invitation->bssid));
873
874         } else if (g_strcmp0(key, "persistent_id") == 0) {
875                 g_variant_get(value, "i", &(invitation->persistent_id));
876                 WDP_LOGD("persistent id [%d]", invitation->persistent_id);
877
878         } else if (g_strcmp0(key, "op_freq") == 0) {
879                 g_variant_get(value, "i", &(invitation->oper_freq));
880                 WDP_LOGD("op freq [%d]", invitation->oper_freq);
881         } else {
882                 WDP_LOGD("Unknown value");
883         }
884         __WDP_LOG_FUNC_EXIT__;
885         return;
886 }
887
888 void __ws_extract_group_details(const char *key, GVariant *value, void *user_data)
889 {
890         __WDP_LOG_FUNC_ENTER__;
891         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
892         if(!event || !event->edata)
893                 return;
894
895         if (!g_pd) {
896                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
897                 return;
898         }
899
900 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
901         wfd_oem_group_data_s *group = (wfd_oem_group_data_s *)event->edata;
902 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
903 #if defined (TIZEN_DEBUG_DBUS_VALUE)
904         CHECK_KEY_VALUE(key, value);
905 #endif /* TIZEN_DEBUG_DBUS_VALUE */
906         if (g_strcmp0(key, "interface_object") == 0) {
907                 static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
908                 const char *i_path = NULL;
909
910                 g_variant_get(value, "&o", &i_path);
911                 g_strlcpy(interface_path, i_path, DBUS_OBJECT_PATH_MAX);
912                 WDP_LOGD("Retrive Added path [%s]", interface_path);
913                 g_strlcpy(g_pd->group_iface_path, interface_path, DBUS_OBJECT_PATH_MAX);
914                 dbus_property_get_all(interface_path, g_pd->g_dbus,
915                                 SUPPLICANT_IFACE, __ws_interface_property, event);
916
917         } else if (g_strcmp0(key, "role") == 0) {
918                 const char *role = NULL;
919
920                 g_variant_get(value, "&s", &role);
921                 WDP_LOGD("Role [%s]", role);
922
923                 if (!strncmp(role, "GO", 2))
924                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
925                 else if (!strncmp(role, "client", 6))
926                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
927 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
928         } else if (g_strcmp0(key, "IpAddr") == 0) {
929
930                 if (__ws_unpack_ay(group->ip_addr, value, OEM_IPADDR_LEN))
931                         WDP_LOGD("IP address [" IPSTR "]", IP2STR(group->ip_addr));
932
933         } else if (g_strcmp0(key, "IpAddrMask") == 0) {
934
935                 if (__ws_unpack_ay(group->ip_addr_mask, value, OEM_IPADDR_LEN))
936                         WDP_LOGD("IP mask [" IPSTR "]", IP2STR(group->ip_addr_mask));
937
938         } else if (g_strcmp0(key, "IpAddrGo") == 0) {
939
940                 if (__ws_unpack_ay(group->ip_addr_go, value, OEM_IPADDR_LEN))
941                         WDP_LOGD("GO IP address [" IPSTR "]", IP2STR(group->ip_addr_go));
942 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
943         } else if (g_strcmp0(key, "group_object") == 0) {
944                 static char group_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
945                 const char *g_path;
946
947                 g_variant_get(value, "&o", &g_path);
948                 g_strlcpy(group_path, g_path, DBUS_OBJECT_PATH_MAX);
949                 WDP_LOGD("Retrive group path [%s]", group_path);
950                 dbus_property_get_all(group_path, g_pd->g_dbus, SUPPLICANT_P2P_GROUP,
951                                 __ws_group_property, event);
952
953                 g_pd->group_sub_id =
954                         g_dbus_connection_signal_subscribe(
955                                 g_pd->g_dbus,
956                                 SUPPLICANT_SERVICE, /* bus name */
957                                 SUPPLICANT_P2P_GROUP, /* interface */
958                                 NULL, /* member */
959                                 group_path, /* object path */
960                                 NULL, /* arg0 */
961                                 G_DBUS_SIGNAL_FLAGS_NONE,
962                                 _group_signal_cb,
963                                 NULL, NULL);
964         }
965         __WDP_LOG_FUNC_EXIT__;
966         return;
967 }
968
969 void __ws_extract_gonegfailaure_details(const char *key, GVariant *value, void *user_data)
970 {
971         __WDP_LOG_FUNC_ENTER__;
972         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
973         if(!event || !event->edata)
974                 return;
975
976         wfd_oem_conn_data_s *conn = (wfd_oem_conn_data_s *)event->edata;
977 #if defined (TIZEN_DEBUG_DBUS_VALUE)
978         CHECK_KEY_VALUE(key, value);
979 #endif /* TIZEN_DEBUG_DBUS_VALUE */
980         if (g_strcmp0(key, "peer_object") == 0) {
981                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
982                 const char *path;
983
984                 g_variant_get(value, "&o", &path);
985                 g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
986                 WDP_LOGD("Retrive peer path [%s]", peer_path);
987
988         } else if (g_strcmp0(key, "status") == 0) {
989                 int status = 0;
990
991                 g_variant_get(value, "i", &status);
992                 WDP_LOGD("Retrive status [%d]", status);
993                 conn->status = status;
994         }
995         __WDP_LOG_FUNC_EXIT__;
996         return;
997 }
998
999 void __ws_extract_gonegsuccess_details(const char *key, GVariant *value, void *user_data)
1000 {
1001         __WDP_LOG_FUNC_ENTER__;
1002         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1003         if(!event || !event->edata)
1004                 return;
1005
1006         wfd_oem_conn_data_s *edata = (wfd_oem_conn_data_s *)event->edata;
1007 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1008         CHECK_KEY_VALUE(key, value);
1009 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1010         if (g_strcmp0(key, "peer_object") == 0) {
1011
1012         } else if (g_strcmp0(key, "status") == 0) {
1013
1014         } else if (g_strcmp0(key, "passphrase") == 0) {
1015
1016         } else if (g_strcmp0(key, "role_go") == 0) {
1017                 //local device role
1018                 const char *role = NULL;
1019
1020                 g_variant_get(value, "&s", &role);
1021                 if (!strncmp(role, "GO", 2))
1022                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
1023                 else if (!strncmp(role, "client", 6))
1024                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
1025
1026         } else if (g_strcmp0(key, "ssid") == 0) {
1027                 unsigned char ssid[WS_SSID_LEN +1] = {0,};
1028
1029                 __ws_unpack_ay(ssid, value, WS_SSID_LEN);
1030                 memcpy(edata->ssid, ssid, WS_SSID_LEN+1);
1031                 WDP_LOGD("ssid [%s]", edata->ssid);
1032
1033         } else if (g_strcmp0(key, "peer_device_addr") == 0) {
1034
1035                 if(__ws_unpack_ay(edata->peer_device_addr, value, WS_MACADDR_LEN))
1036                         WDP_LOGD("Device address[" MACSTR "]", MAC2STR(edata->peer_device_addr));
1037
1038         } else if(g_strcmp0(key, "peer_interface_addr") == 0) {
1039
1040                 if(__ws_unpack_ay(edata->peer_intf_addr, value, WS_MACADDR_LEN))
1041                         WDP_LOGD("Interface address [" MACSTR "]", MAC2STR(edata->peer_intf_addr));
1042
1043         } else if (g_strcmp0(key, "wps_method") == 0) {
1044
1045         } else if (g_strcmp0(key, "frequency_list") == 0) {
1046
1047         } else if (g_strcmp0(key, "persistent_group") == 0) {
1048
1049                 g_variant_get(value, "i", &(edata->persistent_group));
1050                 WDP_LOGD("persistent_group [%d]", edata->persistent_group);
1051
1052         } else if (g_strcmp0(key, "peer_config_timeout") == 0) {
1053
1054         }
1055         __WDP_LOG_FUNC_EXIT__;
1056         return;
1057 }
1058
1059 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1060 void __ws_extract_peer_service(wfd_oem_event_s *data, unsigned char *service_hex, int tlvs_len)
1061 {
1062         GList *services = NULL;
1063         wfd_oem_new_service_s *new_service = NULL;
1064         char *segment = NULL;
1065         int count = 0;
1066         int ptr = 0;
1067         int length = 0;
1068         int res = 0;
1069
1070         while(ptr + 2 < WS_MAX_SERVICE_LEN &&
1071                         (length = (service_hex[ptr+1]*256) + service_hex[ptr]) > 0) {
1072                 segment = (char*) g_try_malloc0(length*2+1);
1073                 if(segment) {
1074                         __ws_byte_to_hex(segment, length * 2 + 1, &service_hex[ptr + 3], length);
1075                         res = __ws_segment_to_service(segment, &new_service);
1076                         if (res < 0) {
1077                                 WDP_LOGE("Failed to convert segment as service instance");
1078                                 g_free(segment);
1079                                 segment = NULL;
1080                                 continue;
1081                         }
1082                         services = g_list_append(services, new_service);
1083                         count++;
1084                         ptr += length + 4;
1085                         g_free(segment);
1086                         segment = NULL;
1087                 }
1088                 data->edata_type = WFD_OEM_EDATA_TYPE_NEW_SERVICE;
1089                 data->dev_role = count;
1090                 data->edata = (void*) services;
1091         }
1092 }
1093
1094 void __ws_extract_servicediscoveryresponse_details(const char *key, GVariant *value, void *user_data)
1095 {
1096         __WDP_LOG_FUNC_ENTER__;
1097         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1098
1099         if(!event)
1100                 return;
1101 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1102         CHECK_KEY_VALUE(key, value);
1103 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1104         if (g_strcmp0(key, "peer_object") == 0) {
1105                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1106                 const char *path = NULL;
1107                 char *loc = NULL;
1108
1109                 g_variant_get(value, "&o", &path);
1110                 if(path == NULL)
1111                         return;
1112
1113                 WDP_LOGD("Retrive Added path [%s]", path);
1114                 loc = strrchr(path,'/');
1115                 if(loc != NULL)
1116                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1117                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1118
1119         } else if (g_strcmp0(key, "update_indicator")) {
1120
1121         } else if (g_strcmp0(key, "tlvs")) {
1122                 GVariantIter *iter = NULL;
1123                 unsigned char service_hex[WS_MAX_SERVICE_LEN];
1124                 int byte_length = 0;
1125
1126                 g_variant_get(value, "ay", &iter);
1127                 if (iter == NULL) {
1128                         WDP_LOGE("failed to get iterator");
1129                         return;
1130                 }
1131
1132                 memset(service_hex, 0x0, WS_MAX_SERVICE_LEN);
1133                 while (g_variant_iter_loop (iter, "y", &service_hex[byte_length]))
1134                         byte_length++;
1135                 g_variant_iter_free(iter);
1136
1137                 __ws_extract_peer_service(event, service_hex, byte_length);
1138         }
1139
1140         __WDP_LOG_FUNC_EXIT__;
1141 }
1142 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1143
1144 #if defined(TIZEN_FEATURE_ASP)
1145 static void __ws_extract_serviceaspresponse_details(const char *key, GVariant *value, void *user_data)
1146 {
1147         __WDP_LOG_FUNC_ENTER__;
1148         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1149         if(!event || !event->edata)
1150                 return;
1151
1152         wfd_oem_asp_service_s *service = (wfd_oem_asp_service_s *)event->edata;
1153
1154         if (g_strcmp0(key, "peer_object") == 0) {
1155                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1156                 const char *path = NULL;
1157                 char *loc = NULL;
1158
1159                 g_variant_get(value, "o", &path);
1160                 if(path == NULL)
1161                         return;
1162
1163                 WDP_LOGD("Retrive Added path [%s]", path);
1164                 loc = strrchr(path,'/');
1165                 if(loc != NULL)
1166                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1167                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1168
1169         } else if (g_strcmp0(key, "srv_trans_id") == 0) {
1170                 unsigned int srv_trans_id = 0;
1171                 g_variant_get(value, "u", &srv_trans_id);
1172                 service->tran_id = srv_trans_id;
1173                 WDP_LOGD("Retrive srv_trans_id [%x]", service->tran_id);
1174
1175         } else if (g_strcmp0(key, "adv_id") == 0) {
1176                 unsigned int adv_id = 0;
1177                 g_variant_get(value, "u", &adv_id);
1178                 service->adv_id = adv_id;
1179                 WDP_LOGD("Retrive adv_id [%x]", service->adv_id);
1180
1181         } else if (g_strcmp0(key, "svc_status") == 0) {
1182                 unsigned char svc_status = 0;
1183                 g_variant_get(value, "u", &svc_status);
1184                 service->status = svc_status;
1185                 WDP_LOGD("Retrive svc_status [%x]", service->status);
1186
1187         } else if (g_strcmp0(key, "config_methods") == 0) {
1188                 unsigned int config_methods = 0;
1189                 g_variant_get(value, "q", &config_methods);
1190                 service->config_method = config_methods;
1191                 WDP_LOGD("Retrive config_methods [%x]", service->config_method);
1192
1193         } else if (g_strcmp0(key, "svc_str") == 0) {
1194                 const char *svc_str = NULL;
1195                 g_variant_get(value, "s", &svc_str);
1196                 if(svc_str != NULL)
1197                         service->service_type = g_strdup(svc_str);
1198                 WDP_LOGD("Retrive srv_name [%s]", service->service_type);
1199
1200         } else if (g_strcmp0(key, "info") == 0) {
1201                 const char *info = NULL;
1202                 g_variant_get(value, "s", &info);
1203                 if(info != NULL)
1204                         service->service_info = g_strdup(info);
1205                 WDP_LOGD("Retrive srv_info [%s]", service->service_info);
1206         }
1207         __WDP_LOG_FUNC_EXIT__;
1208 }
1209
1210 static void __ws_extract_asp_provision_start_details(const char *key, GVariant *value, void *user_data)
1211 {
1212         __WDP_LOG_FUNC_ENTER__;
1213         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1214         wfd_oem_asp_prov_s *asp_params = NULL;
1215         if (!event || !event->edata) {
1216                 __WDP_LOG_FUNC_EXIT__;
1217                 return;
1218         }
1219
1220         asp_params = (wfd_oem_asp_prov_s *)event->edata;
1221
1222         if (g_strcmp0(key, "peer_object") == 0) {
1223                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1224                 const char *path = NULL;
1225                 char *loc = NULL;
1226
1227                 g_variant_get(value, "&o", &path);
1228                 if (path == NULL) {
1229                         __WDP_LOG_FUNC_EXIT__;
1230                         return;
1231                 }
1232
1233                 WDP_LOGD("Retrive Added path [%s]", path);
1234                 loc = strrchr(path,'/');
1235                 if (loc != NULL)
1236                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1237                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1238
1239         } else if (g_strcmp0(key, "adv_id") == 0) {
1240                 g_variant_get(value, "u", &asp_params->adv_id);
1241                 WDP_LOGD("Retrive adv_id [%u]", asp_params->adv_id);
1242
1243         } else if (g_strcmp0(key, "ses_id") == 0) {
1244                 g_variant_get(value, "u", &asp_params->session_id);
1245                 WDP_LOGD("Retrive session id [%u]", asp_params->session_id);
1246
1247         } else if (g_strcmp0(key, "dev_passwd_id") == 0) {
1248                 g_variant_get(value, "i", &event->wps_mode);
1249                 WDP_LOGD("Retrive dev_passwd_id [%d]", event->wps_mode);
1250
1251         } else if (g_strcmp0(key, "conncap") == 0) {
1252                 g_variant_get(value, "u", &asp_params->network_role);
1253                 WDP_LOGD("Retrive conncap [%x]", asp_params->network_role);
1254
1255         } else if (g_strcmp0(key, "adv_mac") == 0) {
1256                 if(__ws_unpack_ay(asp_params->service_mac, value, WS_MACADDR_LEN))
1257                         WDP_LOGD("Adv address[" MACSTR "]", MAC2STR(asp_params->service_mac));
1258
1259         } else if (g_strcmp0(key, "ses_mac") == 0) {
1260                 if(__ws_unpack_ay(asp_params->session_mac, value, WS_MACADDR_LEN))
1261                         WDP_LOGD("session address[" MACSTR "]", MAC2STR(asp_params->session_mac));
1262
1263         } else if (g_strcmp0(key, "session_info") == 0) {
1264                 const char *session_info = NULL;
1265                 g_variant_get(value, "&s", &session_info);
1266                 if (session_info != NULL)
1267                         asp_params->session_information = g_strdup(session_info);
1268                 WDP_LOGD("Retrive session_info [%s]", asp_params->session_information);
1269         }
1270         __WDP_LOG_FUNC_EXIT__;
1271 }
1272
1273 static void __ws_extract_asp_provision_done_details(const char *key, GVariant *value, void *user_data)
1274 {
1275         __WDP_LOG_FUNC_ENTER__;
1276         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1277         wfd_oem_asp_prov_s *asp_params = NULL;
1278         if (!event || !event->edata) {
1279                 __WDP_LOG_FUNC_EXIT__;
1280                 return;
1281         }
1282
1283         asp_params = (wfd_oem_asp_prov_s *)event->edata;
1284
1285
1286         if (g_strcmp0(key, "peer_object") == 0) {
1287                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1288                 const char *path = NULL;
1289                 char *loc = NULL;
1290
1291                 g_variant_get(value, "&o", &path);
1292                 if (path == NULL) {
1293                         __WDP_LOG_FUNC_EXIT__;
1294                         return;
1295                 }
1296
1297                 WDP_LOGD("Retrive Added path [%s]", path);
1298                 loc = strrchr(path,'/');
1299                 if (loc != NULL)
1300                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1301                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1302
1303                 WDP_LOGD("peer address[" MACSTR "]", MAC2STR(event->dev_addr));
1304
1305         } else if (g_strcmp0(key, "adv_id") == 0) {
1306                 g_variant_get(value, "u", &asp_params->adv_id);
1307                 WDP_LOGD("Retrive adv_id [%u]", asp_params->adv_id);
1308
1309         } else if (g_strcmp0(key, "ses_id") == 0) {
1310                 g_variant_get(value, "u", &asp_params->session_id);
1311                 WDP_LOGD("Retrive session id [%u]", asp_params->session_id);
1312
1313         } else if (g_strcmp0(key, "dev_passwd_id") == 0) {
1314                 g_variant_get(value, "i", &event->wps_mode);
1315                 WDP_LOGD("Retrive dev_passwd_id [%d]", event->wps_mode);
1316
1317         } else if (g_strcmp0(key, "conncap") == 0) {
1318                 g_variant_get(value, "u", &asp_params->network_role);
1319                 WDP_LOGD("Retrive network role [%x]", asp_params->network_role);
1320
1321         } else if (g_strcmp0(key, "status") == 0) {
1322                 g_variant_get(value, "u", &asp_params->status);
1323                 WDP_LOGD("Retrive status [%x]", asp_params->status);
1324
1325         } else if (g_strcmp0(key, "persist") == 0) {
1326                 g_variant_get(value, "u", &asp_params->persistent_group_id);
1327                 asp_params->persist = 1;
1328                 WDP_LOGD("Retrive persist [%u]", asp_params->persistent_group_id);
1329
1330         }   else if (g_strcmp0(key, "adv_mac") == 0) {
1331                 if(__ws_unpack_ay(asp_params->service_mac, value, WS_MACADDR_LEN))
1332                         WDP_LOGD("Adv address[" MACSTR "]", MAC2STR(asp_params->service_mac));
1333
1334         } else if (g_strcmp0(key, "ses_mac") == 0) {
1335                 if(__ws_unpack_ay(asp_params->session_mac, value, WS_MACADDR_LEN))
1336                         WDP_LOGD("session address[" MACSTR "]", MAC2STR(asp_params->session_mac));
1337
1338         } else if (g_strcmp0(key, "group_mac") == 0) {
1339                 if(__ws_unpack_ay(asp_params->group_mac, value, WS_MACADDR_LEN))
1340                         WDP_LOGD("group address[" MACSTR "]", MAC2STR(asp_params->group_mac));
1341         }
1342         __WDP_LOG_FUNC_EXIT__;
1343 }
1344
1345 static void __ws_extract_provision_fail_details(const char *key, GVariant *value, void *user_data)
1346 {
1347         __WDP_LOG_FUNC_ENTER__;
1348         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1349         wfd_oem_asp_prov_s *asp_params = NULL;
1350         if(!event || !event->edata) {
1351                 __WDP_LOG_FUNC_EXIT__;
1352                 return;
1353         }
1354
1355         asp_params = (wfd_oem_asp_prov_s *)event->edata;
1356
1357         if (g_strcmp0(key, "peer_object") == 0) {
1358                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1359                 const char *path = NULL;
1360                 char *loc = NULL;
1361
1362                 g_variant_get(value, "&o", &path);
1363                 if (path == NULL) {
1364                         __WDP_LOG_FUNC_EXIT__;
1365                         return;
1366                 }
1367
1368                 WDP_LOGD("Retrive Added path [%s]", path);
1369                 loc = strrchr(path,'/');
1370                 if (loc != NULL)
1371                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1372                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1373
1374         } else if (g_strcmp0(key, "adv_id") == 0) {
1375                 g_variant_get(value, "u", &asp_params->adv_id);
1376                 WDP_LOGD("Retrive adv_id [%d]", asp_params->adv_id);
1377
1378         }  else if (g_strcmp0(key, "status") == 0) {
1379                 g_variant_get(value, "i", &asp_params->status);
1380                 WDP_LOGD("Retrive status [%d]", asp_params->status);
1381
1382         } else if (g_strcmp0(key, "deferred_session_resp") == 0) {
1383                 const char *session_info = NULL;
1384                 g_variant_get(value, "&s", &session_info);
1385                 if(session_info != NULL)
1386                         asp_params->session_information = g_strdup(session_info);
1387                 WDP_LOGD("Retrive deferred_session_resp [%s]", asp_params->session_information);
1388         }
1389         __WDP_LOG_FUNC_EXIT__;
1390 }
1391 #endif /* TIZEN_FEATURE_ASP */
1392
1393 static int _ws_flush()
1394 {
1395         __WDP_LOG_FUNC_ENTER__;
1396         GDBusConnection *g_dbus = NULL;
1397         dbus_method_param_s params;
1398         int res = 0;
1399
1400         if (!g_pd) {
1401                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1402                 __WDP_LOG_FUNC_EXIT__;
1403                 return -1;
1404         }
1405
1406         g_dbus = g_pd->g_dbus;
1407         if (!g_dbus) {
1408                 WDP_LOGE("DBus connection is NULL");
1409                 __WDP_LOG_FUNC_EXIT__;
1410                 return -1;
1411         }
1412         memset(&params, 0x0, sizeof(dbus_method_param_s));
1413
1414         dbus_set_method_param(&params, "Flush", g_pd->iface_path, g_dbus);
1415         params.params = NULL;
1416
1417         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
1418         if (res < 0)
1419                 WDP_LOGE("Failed to send command to wpa_supplicant");
1420         else
1421                 WDP_LOGD("Succeeded to flush");
1422
1423         __WDP_LOG_FUNC_EXIT__;
1424         return 0;
1425 }
1426
1427 static int _ws_cancel()
1428 {
1429         __WDP_LOG_FUNC_ENTER__;
1430         GDBusConnection *g_dbus = NULL;
1431         dbus_method_param_s params;
1432         int res = 0;
1433
1434         if (!g_pd) {
1435                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1436                 return -1;
1437         }
1438
1439
1440         g_dbus = g_pd->g_dbus;
1441         if (!g_dbus) {
1442                 WDP_LOGE("DBus connection is NULL");
1443                 return -1;
1444         }
1445         memset(&params, 0x0, sizeof(dbus_method_param_s));
1446
1447         dbus_set_method_param(&params, "Cancel", g_pd->iface_path , g_dbus);
1448         params.params = NULL;
1449
1450         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
1451         if (res < 0)
1452                 WDP_LOGE("Failed to send command to wpa_supplicant");
1453         else
1454                 WDP_LOGD("Succeeded to cancel");
1455
1456         __WDP_LOG_FUNC_EXIT__;
1457         return 0;
1458 }
1459
1460 #if defined(TIZEN_FEATURE_ASP)
1461 int ws_get_advertise_service(const char *peer_path, GList **asp_services)
1462 {
1463         __WDP_LOG_FUNC_ENTER__;
1464         GDBusConnection *g_dbus = NULL;
1465         GVariant *param = NULL;
1466         GVariant *reply = NULL;
1467         GVariant *temp = NULL;
1468         GError *error = NULL;
1469         GVariantIter *iter = NULL;
1470         wfd_oem_advertise_service_s *service;
1471         wfd_oem_asp_service_s *seek = NULL;
1472         unsigned char desc[7];
1473         unsigned int adv_id;
1474         unsigned int config_method;
1475         unsigned char length;
1476         char *value;
1477         int cnt;
1478         int res = 0;
1479
1480         if (!g_pd) {
1481                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1482                 return -1;
1483         }
1484
1485         g_dbus = g_pd->g_dbus;
1486         if (!g_dbus) {
1487                 WDP_LOGE("DBus connection is NULL");
1488                 return -1;
1489         }
1490
1491         param = g_variant_new("(ss)", SUPPLICANT_P2P_PEER, "AdvertiseService");
1492 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1493         DEBUG_PARAMS(param);
1494 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1495
1496         reply = g_dbus_connection_call_sync (
1497                         g_dbus,
1498                         SUPPLICANT_SERVICE, /* bus name */
1499                         peer_path, /* object path */
1500                         DBUS_PROPERTIES_INTERFACE, /* interface name */
1501                         DBUS_PROPERTIES_METHOD_GET, /* method name */
1502                         param, /* GVariant *params */
1503                         NULL, /* reply_type */
1504                         G_DBUS_CALL_FLAGS_NONE, /* flags */
1505                         SUPPLICANT_TIMEOUT , /* timeout */
1506                         NULL, /* cancellable */
1507                         &error); /* error */
1508
1509         if(error != NULL) {
1510                 WDP_LOGE("Error! Failed to get peer advertise service: [%s]",
1511                                 error->message);
1512                 g_error_free(error);
1513                 if(reply)
1514                         g_variant_unref(reply);
1515                 __WDP_LOG_FUNC_EXIT__;
1516                 return -1;
1517         }
1518
1519         if (reply != NULL) {
1520
1521 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1522                 DEBUG_PARAMS(reply);
1523 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1524
1525                 /* replay will have the format <(<ay>,)>
1526                  * So, you need to remove tuple out side of variant and
1527                  * variant out side of byte array
1528                  * */
1529                 temp = g_variant_get_child_value(reply, 0);
1530                 temp = g_variant_get_child_value(temp, 0);
1531                 g_variant_get(temp, "ay", &iter);
1532                 if (iter == NULL) {
1533                         g_variant_unref(reply);
1534                         WDP_LOGE("Failed to get iterator");
1535                         return -1;
1536                 }
1537
1538                 while (1) {
1539                         /* 4byte advertisement ID, 2 byte config method, 1byte length */
1540                         cnt = 0;
1541                         memset(desc, 0x0, 7);
1542                         while (cnt < 7 && g_variant_iter_loop (iter, "y", &desc[cnt])) {
1543                                 cnt++;
1544                         }
1545
1546                         if (cnt != 7 || desc[6] == 0) {
1547                                 WDP_LOGE("Invalid descriptor header length cnt [%d]", cnt);
1548                                 g_variant_unref(reply);
1549                                 return res;
1550                         }
1551
1552                         adv_id = desc[3] << 24 | desc[2] << 16 | desc[1] << 8 | desc[0];
1553                         config_method = desc[4] << 8 | desc[4];
1554                         length = desc[6];
1555                         value = NULL;
1556                         value = g_try_malloc0(length + 1);
1557                         if (value == NULL) {
1558                                 WDP_LOGE("g_try_malloc0 failed");
1559                                 g_variant_unref(reply);
1560                                 return res;
1561                         }
1562                         WDP_LOGD("adv_id[%u] config_method[%u]  length[%hhu]", adv_id, config_method, length);
1563
1564                         cnt = 0;
1565                         while (cnt < length && g_variant_iter_loop (iter, "y", &value[cnt])) {
1566                                 cnt++;
1567                         }
1568
1569                         if (cnt != length) {
1570                                 WDP_LOGE("Length doesn't matched with header value cnt [%d]", cnt);
1571                                 g_variant_unref(reply);
1572                                 g_free(value);
1573                                 return res;
1574                         }
1575
1576                         service = NULL;
1577                         service = (wfd_oem_advertise_service_s *)
1578                                         g_try_malloc0(sizeof(wfd_oem_advertise_service_s));
1579                         if (service == NULL) {
1580                                 WDP_LOGE("g_try_malloc0 failed");
1581                                 g_variant_unref(reply);
1582                                 g_free(value);
1583                                 return res;
1584                         }
1585                         service->adv_id = adv_id;
1586                         service->config_method = config_method;
1587                         service->service_type_length = length;
1588                         service->service_type = value;
1589
1590 GLIST_ITER_START(seek_list, seek)
1591                         if (g_strcmp0(seek->service_type, service->service_type) == 0) {
1592                                 WDP_LOGD("service type matched [%s] search_id [%llu]",
1593                                                 service->service_type, seek->search_id);
1594                         } else {
1595                                 seek = NULL;
1596                         }
1597 GLIST_ITER_END()
1598                         if(seek != NULL && seek->service_info != NULL) {
1599                                 WDP_LOGD("service info exists, service discovery will be performed");
1600                         } else {
1601                                 WDP_LOGD("service info doesn't exists. Add service to list");
1602                                 service->search_id = seek->search_id;
1603                                 *asp_services = g_list_append(*asp_services, service);
1604                         }
1605                 }
1606                 g_variant_unref(reply);
1607         }
1608         __WDP_LOG_FUNC_EXIT__;
1609         return res;
1610 }
1611 #endif /* TIZEN_FEATURE_ASP */
1612
1613 static void _ws_process_device_found(GDBusConnection *connection,
1614                 const gchar *object_path, GVariant *parameters)
1615 {
1616         __WDP_LOG_FUNC_ENTER__;
1617         wfd_oem_event_s event;
1618         wfd_oem_dev_data_s *edata = NULL;
1619         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1620
1621         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1622         if (!edata) {
1623                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1624                                 strerror(errno));
1625                 __WDP_LOG_FUNC_EXIT__;
1626                 return;
1627         }
1628         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1629
1630         event.edata = (void*) edata;
1631         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1632         event.event_id = WFD_OEM_EVENT_PEER_FOUND;
1633
1634         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1635
1636         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1637                         __ws_peer_property, event.edata);
1638
1639 #if defined(TIZEN_FEATURE_ASP)
1640         if(edata->has_asp_services)
1641                 ws_get_advertise_service(peer_path, (GList **)&(event.asp_services));
1642 #endif /* TIZEN_FEATURE_ASP */
1643
1644         g_pd->callback(g_pd->user_data, &event);
1645 #if defined(TIZEN_FEATURE_ASP)
1646         if(event.asp_services != NULL) {
1647                 GList *l;
1648                 wfd_oem_advertise_service_s *service;
1649                 for(l = (GList *)event.asp_services; l != NULL; l = l->next) {
1650                         service = (wfd_oem_advertise_service_s *)l->data;
1651                         g_free(service->service_type);
1652                         g_free(service);
1653                         event.asp_services = g_list_remove(l, service);
1654                 }
1655                 g_list_free(l);
1656         }
1657 #endif /* TIZEN_FEATURE_ASP */
1658         g_free(event.edata);
1659
1660         __WDP_LOG_FUNC_EXIT__;
1661 }
1662
1663 static void _ws_process_device_lost(GDBusConnection *connection,
1664                 const gchar *object_path, GVariant *parameters)
1665 {
1666         __WDP_LOG_FUNC_ENTER__;
1667         wfd_oem_event_s event;
1668         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1669
1670         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1671
1672         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1673         event.event_id = WFD_OEM_EVENT_PEER_DISAPPEARED;
1674
1675         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1676
1677         g_pd->callback(g_pd->user_data, &event);
1678
1679         __WDP_LOG_FUNC_EXIT__;
1680 }
1681
1682 static void _ws_process_find_stoppped(GDBusConnection *connection,
1683                 const gchar *object_path, GVariant *parameters)
1684 {
1685         __WDP_LOG_FUNC_ENTER__;
1686         wfd_oem_event_s event;
1687
1688         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1689
1690         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1691         event.event_id = WFD_OEM_EVENT_DISCOVERY_FINISHED;
1692
1693         g_pd->callback(g_pd->user_data, &event);
1694
1695         __WDP_LOG_FUNC_EXIT__;
1696 }
1697
1698 static void _ws_process_prov_disc_req_display_pin(GDBusConnection *connection,
1699                 const gchar *object_path, GVariant *parameters)
1700 {
1701         __WDP_LOG_FUNC_ENTER__;
1702
1703         wfd_oem_event_s event;
1704         wfd_oem_dev_data_s *edata = NULL;
1705
1706         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1707         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1708         const char *path = NULL;
1709         const char *pin = NULL;
1710         char *loc = NULL;
1711
1712         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1713         if (!edata) {
1714                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1715                                 strerror(errno));
1716                 __WDP_LOG_FUNC_EXIT__;
1717                 return;
1718         }
1719         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1720
1721         event.edata = (void*) edata;
1722         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1723         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1724         event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
1725
1726         g_variant_get(parameters, "(&o&s)", &path, &pin);
1727         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1728         WDP_LOGD("Retrive Added path [%s]", peer_path);
1729
1730         loc = strrchr(peer_path,'/');
1731         if(loc != NULL)
1732                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1733         __ws_txt_to_mac(peer_dev, event.dev_addr);
1734         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1735
1736         g_strlcpy(event.wps_pin, pin, WS_PINSTR_LEN + 1);
1737         WDP_LOGD("Retrive pin [%s]", event.wps_pin);
1738
1739         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1740                         __ws_peer_property, event.edata);
1741
1742         g_pd->callback(g_pd->user_data, &event);
1743         g_free(event.edata);
1744
1745         __WDP_LOG_FUNC_EXIT__;
1746 }
1747
1748 static void _ws_process_prov_disc_resp_display_pin(GDBusConnection *connection,
1749                 const gchar *object_path, GVariant *parameters)
1750 {
1751         __WDP_LOG_FUNC_ENTER__;
1752
1753         wfd_oem_event_s event;
1754         wfd_oem_dev_data_s *edata = NULL;
1755
1756         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1757         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1758         const char *path = NULL;
1759         const char *pin = NULL;
1760         char *loc = NULL;
1761
1762         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1763         if (!edata) {
1764                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1765                                 strerror(errno));
1766                 __WDP_LOG_FUNC_EXIT__;
1767                 return;
1768         }
1769         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1770
1771         event.edata = (void*) edata;
1772         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1773         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1774         event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
1775
1776         g_variant_get(parameters, "(&o&s)", &path, &pin);
1777         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1778         WDP_LOGD("Retrive Added path [%s]", peer_path);
1779
1780         loc = strrchr(peer_path,'/');
1781         if(loc != NULL)
1782                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1783         __ws_txt_to_mac(peer_dev, event.dev_addr);
1784         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1785
1786         g_strlcpy(event.wps_pin, pin, WS_PINSTR_LEN + 1);
1787         WDP_LOGD("Retrive pin [%s]", event.wps_pin);
1788
1789         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1790                         __ws_peer_property, event.edata);
1791
1792         g_pd->callback(g_pd->user_data, &event);
1793         g_free(event.edata);
1794
1795         __WDP_LOG_FUNC_EXIT__;
1796 }
1797
1798 static void _ws_process_prov_disc_req_enter_pin(GDBusConnection *connection,
1799                 const gchar *object_path, GVariant *parameters)
1800 {
1801         __WDP_LOG_FUNC_ENTER__;
1802         wfd_oem_event_s event;
1803         wfd_oem_dev_data_s *edata = NULL;
1804         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1805
1806         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1807         if (!edata) {
1808                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1809                                 strerror(errno));
1810                 __WDP_LOG_FUNC_EXIT__;
1811                 return;
1812         }
1813         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1814
1815         event.edata = (void*) edata;
1816         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1817         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1818         event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
1819
1820         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1821
1822         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1823                         __ws_peer_property, event.edata);
1824
1825         g_pd->callback(g_pd->user_data, &event);
1826         g_free(event.edata);
1827
1828         __WDP_LOG_FUNC_EXIT__;
1829 }
1830
1831 static void _ws_process_prov_disc_resp_enter_pin(GDBusConnection *connection,
1832                 const gchar *object_path, GVariant *parameters)
1833 {
1834         __WDP_LOG_FUNC_ENTER__;
1835         wfd_oem_event_s event;
1836         wfd_oem_dev_data_s *edata = NULL;
1837         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1838
1839         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1840         if (!edata) {
1841                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1842                                 strerror(errno));
1843                 __WDP_LOG_FUNC_EXIT__;
1844                 return;
1845         }
1846         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1847
1848         event.edata = (void*) edata;
1849         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1850         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1851         event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
1852
1853         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1854
1855         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1856                         __ws_peer_property, event.edata);
1857
1858         g_pd->callback(g_pd->user_data, &event);
1859         g_free(event.edata);
1860
1861         __WDP_LOG_FUNC_EXIT__;
1862 }
1863
1864 static void _ws_process_prov_disc_pbc_req(GDBusConnection *connection,
1865                 const gchar *object_path, GVariant *parameters)
1866 {
1867         __WDP_LOG_FUNC_ENTER__;
1868         wfd_oem_event_s event;
1869         wfd_oem_dev_data_s *edata = NULL;
1870         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1871
1872         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1873         if (!edata) {
1874                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1875                                 strerror(errno));
1876                 __WDP_LOG_FUNC_EXIT__;
1877                 return;
1878         }
1879         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1880
1881         event.edata = (void*) edata;
1882         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1883         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1884         event.wps_mode = WFD_OEM_WPS_MODE_PBC;
1885
1886         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1887
1888         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1889                         __ws_peer_property, event.edata);
1890
1891         g_pd->callback(g_pd->user_data, &event);
1892         g_free(event.edata);
1893
1894         __WDP_LOG_FUNC_EXIT__;
1895 }
1896
1897 static void _ws_process_prov_disc_pbc_resp(GDBusConnection *connection,
1898                 const gchar *object_path, GVariant *parameters)
1899 {
1900         __WDP_LOG_FUNC_ENTER__;
1901         wfd_oem_event_s event;
1902         wfd_oem_dev_data_s *edata = NULL;
1903         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1904
1905         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1906         if (!edata) {
1907                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1908                                 strerror(errno));
1909                 __WDP_LOG_FUNC_EXIT__;
1910                 return;
1911         }
1912         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1913
1914         event.edata = (void*) edata;
1915         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1916         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1917         event.wps_mode = WFD_OEM_WPS_MODE_PBC;
1918
1919         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1920
1921         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1922                         __ws_peer_property, event.edata);
1923
1924         g_pd->callback(g_pd->user_data, &event);
1925         g_free(event.edata);
1926
1927         __WDP_LOG_FUNC_EXIT__;
1928 }
1929
1930 #if defined(TIZEN_FEATURE_ASP)
1931 static void _ws_process_prov_disc_failure(GDBusConnection *connection,
1932                 const gchar *object_path, GVariant *parameters)
1933 {
1934         __WDP_LOG_FUNC_ENTER__;
1935         GVariantIter *iter = NULL;
1936         wfd_oem_event_s event;
1937         wfd_oem_asp_prov_s *edata;
1938
1939         edata = (wfd_oem_asp_prov_s *) g_try_malloc0(sizeof(wfd_oem_asp_prov_s));
1940         if (!edata) {
1941                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1942                                 strerror(errno));
1943                 __WDP_LOG_FUNC_EXIT__;
1944                 return;
1945         }
1946         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1947
1948         event.edata = (void*) edata;
1949         event.event_id = WFD_OEM_EVENT_PROV_DISC_FAIL;
1950
1951         if (parameters != NULL) {
1952                 g_variant_get(parameters, "(a{sv})", &iter);
1953                 if (iter != NULL) {
1954                         dbus_property_foreach(iter, __ws_extract_provision_fail_details, &event);
1955                         event.edata_type = WFD_OEM_EDATA_TYPE_ASP_PROV;
1956                         g_variant_iter_free(iter);
1957                 }
1958         } else {
1959                 WDP_LOGE("No Properties");
1960         }
1961
1962         g_pd->callback(g_pd->user_data, &event);
1963
1964         if (event.edata_type == WFD_OEM_EDATA_TYPE_ASP_PROV)
1965                 g_free(edata->session_information);
1966         g_free(edata);
1967
1968         __WDP_LOG_FUNC_EXIT__;
1969 }
1970 #else
1971 static void _ws_process_prov_disc_failure(GDBusConnection *connection,
1972                 const gchar *object_path, GVariant *parameters)
1973 {
1974         __WDP_LOG_FUNC_ENTER__;
1975         wfd_oem_event_s event;
1976         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1977         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1978         const char *path = NULL;
1979         int prov_status = 0;
1980         char *loc = NULL;
1981
1982         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1983
1984         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1985         event.event_id = WFD_OEM_EVENT_PROV_DISC_FAIL;
1986
1987         g_variant_get(parameters, "(&oi)", &path, &prov_status);
1988         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1989         WDP_LOGD("Retrive Added path [%s]", peer_path);
1990         WDP_LOGD("Retrive Failure stateus [%d]", prov_status);
1991
1992         loc = strrchr(peer_path,'/');
1993         if(loc != NULL)
1994                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1995         __ws_txt_to_mac(peer_dev, event.dev_addr);
1996         WDP_LOGE("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1997
1998         g_pd->callback(g_pd->user_data, &event);
1999
2000         __WDP_LOG_FUNC_EXIT__;
2001 }
2002 #endif /* TIZEN_FEATURE_ASP */
2003
2004
2005 static void _ws_process_group_started(GDBusConnection *connection,
2006                 const gchar *object_path, GVariant *parameters)
2007 {
2008         __WDP_LOG_FUNC_ENTER__;
2009         GVariantIter *iter = NULL;
2010         wfd_oem_event_s event;
2011         wfd_oem_group_data_s *edata = NULL;
2012
2013         edata = (wfd_oem_group_data_s*)calloc(1, sizeof(wfd_oem_group_data_s));
2014         if (!edata) {
2015                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2016                                 strerror(errno));
2017                 __WDP_LOG_FUNC_EXIT__;
2018                 return;
2019         }
2020         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2021
2022         event.edata = (void*) edata;
2023         event.edata_type = WFD_OEM_EDATA_TYPE_GROUP;
2024         event.event_id = WFD_OEM_EVENT_GROUP_CREATED;
2025
2026         if(parameters != NULL){
2027                 g_variant_get(parameters, "(a{sv})", &iter);
2028
2029                 if (iter != NULL) {
2030                         dbus_property_foreach(iter, __ws_extract_group_details, &event);
2031                         g_variant_iter_free(iter);
2032                 }
2033         } else {
2034                 WDP_LOGE("No properties");
2035         }
2036
2037         g_pd->callback(g_pd->user_data, &event);
2038         g_free(event.edata);
2039
2040         __WDP_LOG_FUNC_EXIT__;
2041 }
2042
2043 static void _ws_process_go_neg_success(GDBusConnection *connection,
2044                 const gchar *object_path, GVariant *parameters)
2045 {
2046         __WDP_LOG_FUNC_ENTER__;
2047         GVariantIter *iter = NULL;
2048         wfd_oem_event_s event;
2049         wfd_oem_conn_data_s *edata = NULL;
2050
2051         edata = (wfd_oem_conn_data_s*)calloc(1, sizeof(wfd_oem_conn_data_s));
2052         if (!edata) {
2053                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2054                                 strerror(errno));
2055                 __WDP_LOG_FUNC_EXIT__;
2056                 return;
2057         }
2058         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2059
2060         event.edata = edata;
2061         event.edata_type = WFD_OEM_EDATA_TYPE_CONN;
2062         event.event_id = WFD_OEM_EVENT_GO_NEG_DONE;
2063
2064         if (parameters != NULL){
2065                 g_variant_get(parameters, "(a{sv})", &iter);
2066
2067                 if (iter != NULL) {
2068                         dbus_property_foreach(iter, __ws_extract_gonegsuccess_details, &event);
2069                         g_variant_iter_free(iter);
2070                 }
2071         } else {
2072                 WDP_LOGE("No properties");
2073         }
2074
2075         g_pd->callback(g_pd->user_data, &event);
2076         g_free(edata);
2077
2078         __WDP_LOG_FUNC_EXIT__;
2079 }
2080
2081 static void _ws_process_go_neg_failure(GDBusConnection *connection,
2082                 const gchar *object_path, GVariant *parameters)
2083 {
2084         __WDP_LOG_FUNC_ENTER__;
2085         GVariantIter *iter = NULL;
2086         wfd_oem_event_s event;
2087         wfd_oem_conn_data_s *edata = NULL;
2088
2089         edata = (wfd_oem_conn_data_s *) g_try_malloc0(sizeof(wfd_oem_conn_data_s));
2090         if (!edata) {
2091                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2092                                 strerror(errno));
2093                 __WDP_LOG_FUNC_EXIT__;
2094                 return;
2095         }
2096         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2097
2098         event.edata = (void*) edata;
2099         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
2100         event.event_id = WFD_OEM_EVENT_GO_NEG_FAIL;
2101
2102         if (parameters != NULL) {
2103                 g_variant_get(parameters, "(a{sv})", &iter);
2104
2105                 if (iter != NULL) {
2106                         dbus_property_foreach(iter, __ws_extract_gonegfailaure_details, &event);
2107                         g_variant_iter_free(iter);
2108                 }
2109         } else {
2110                 WDP_LOGE("No properties");
2111         }
2112
2113         g_pd->callback(g_pd->user_data, &event);
2114         g_free(event.edata);
2115
2116         __WDP_LOG_FUNC_EXIT__;
2117 }
2118
2119 static void _ws_process_go_neg_request(GDBusConnection *connection,
2120                 const gchar *object_path, GVariant *parameters)
2121 {
2122         __WDP_LOG_FUNC_ENTER__;
2123         wfd_oem_event_s event;
2124         wfd_oem_dev_data_s *edata = NULL;
2125         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2126         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
2127         const char *path = NULL;
2128         char * loc = NULL;
2129
2130         int dev_passwd_id = 0;
2131         int device_go_intent = 0;
2132
2133         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
2134         if (!edata) {
2135                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2136                                 strerror(errno));
2137                 __WDP_LOG_FUNC_EXIT__;
2138                 return;
2139         }
2140         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2141
2142         event.edata = (void*) edata;
2143         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
2144         event.event_id = WFD_OEM_EVENT_GO_NEG_REQ;
2145
2146         g_variant_get(parameters, "(&oqy)", &path, &dev_passwd_id, &device_go_intent);
2147         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
2148
2149         WDP_LOGD("Retrive peer path [%s]", peer_path);
2150         WDP_LOGD("Retrive dev_passwd_id [%d]", dev_passwd_id);
2151         WDP_LOGD("Retrive device_go_intent [%d]", device_go_intent);
2152
2153         if (dev_passwd_id == WS_DEV_PASSWD_ID_PUSH_BUTTON)
2154                 event.wps_mode = WFD_OEM_WPS_MODE_PBC;
2155         else if (dev_passwd_id == WS_DEV_PASSWD_ID_REGISTRAR_SPECIFIED)
2156                 event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
2157         else if (dev_passwd_id == WS_DEV_PASSWD_ID_USER_SPECIFIED)
2158                 event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
2159         else
2160                 event.wps_mode = WFD_OEM_WPS_MODE_NONE;
2161         edata->device_go_intent = device_go_intent;
2162
2163         loc = strrchr(peer_path,'/');
2164         if(loc != NULL)
2165                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
2166         __ws_txt_to_mac(peer_dev, event.dev_addr);
2167         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
2168
2169         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
2170                         __ws_peer_property, event.edata);
2171
2172         g_pd->callback(g_pd->user_data, &event);
2173         g_free(event.edata);
2174
2175         __WDP_LOG_FUNC_EXIT__;
2176 }
2177 static void _ws_process_invitation_received(GDBusConnection *connection,
2178                 const gchar *object_path, GVariant *parameters)
2179 {
2180         __WDP_LOG_FUNC_ENTER__;
2181         GVariantIter *iter = NULL;
2182         wfd_oem_event_s event;
2183         wfd_oem_invite_data_s *edata = NULL;
2184
2185         edata = (wfd_oem_invite_data_s *) g_try_malloc0(sizeof(wfd_oem_invite_data_s));
2186         if (!edata) {
2187                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2188                                 strerror(errno));
2189                 __WDP_LOG_FUNC_EXIT__;
2190                 return;
2191         }
2192         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2193
2194         event.edata = (void*) edata;
2195         event.edata_type = WFD_OEM_EDATA_TYPE_INVITE;
2196         event.event_id = WFD_OEM_EVENT_INVITATION_REQ;
2197
2198         if (parameters != NULL) {
2199                 g_variant_get(parameters, "(a{sv})", &iter);
2200
2201                 if (iter != NULL) {
2202                         dbus_property_foreach(iter, __ws_extract_invitation_details, &event);
2203                         g_variant_iter_free(iter);
2204                 }
2205         } else {
2206                 WDP_LOGE("No properties");
2207         }
2208         memcpy(&(event.dev_addr), edata->sa, OEM_MACADDR_LEN);
2209
2210         g_pd->callback(g_pd->user_data, &event);
2211         g_free(event.edata);
2212
2213         __WDP_LOG_FUNC_EXIT__;
2214 }
2215
2216 static void _ws_process_invitation_result(GDBusConnection *connection,
2217                 const gchar *object_path, GVariant *parameters)
2218 {
2219         __WDP_LOG_FUNC_ENTER__;
2220         wfd_oem_event_s event;
2221         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2222
2223 //      g_pd->callback(g_pd->user_data, event);
2224
2225         __WDP_LOG_FUNC_EXIT__;
2226 }
2227
2228 static void _ws_process_group_finished(GDBusConnection *connection,
2229                 const gchar *object_path, GVariant *parameters)
2230 {
2231         __WDP_LOG_FUNC_ENTER__;
2232         wfd_oem_event_s event;
2233
2234         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2235
2236         event.event_id = WFD_OEM_EVENT_GROUP_DESTROYED;
2237         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2238
2239         g_dbus_connection_signal_unsubscribe(g_pd->g_dbus, g_pd->group_sub_id);
2240         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
2241         _ws_flush();
2242
2243         g_pd->callback(g_pd->user_data, &event);
2244
2245         __WDP_LOG_FUNC_EXIT__;
2246 }
2247
2248 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
2249 static void _ws_process_service_discovery_response(GDBusConnection *connection,
2250                 const gchar *object_path, GVariant *parameters)
2251 {
2252         __WDP_LOG_FUNC_ENTER__;
2253         GVariantIter *iter = NULL;
2254         wfd_oem_event_s event;
2255
2256         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2257
2258         event.event_id = WFD_OEM_EVENT_SERV_DISC_RESP;
2259
2260         if(parameters != NULL) {
2261                 g_variant_get(parameters, "(a{sv})", &iter);
2262                 if(iter != NULL) {
2263                         dbus_property_foreach(iter, __ws_extract_servicediscoveryresponse_details, &event);
2264                         event.edata_type = WFD_OEM_EDATA_TYPE_NEW_SERVICE;
2265                         g_variant_iter_free(iter);
2266                 }
2267         } else {
2268                 WDP_LOGE("No Properties");
2269         }
2270
2271         g_pd->callback(g_pd->user_data, &event);
2272
2273         if (event.edata_type == WFD_OEM_EDATA_TYPE_NEW_SERVICE)
2274                 g_list_free((GList*) event.edata);
2275
2276         __WDP_LOG_FUNC_EXIT__;
2277 }
2278 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
2279
2280 #if defined(TIZEN_FEATURE_ASP)
2281 static void _ws_process_service_asp_response(GDBusConnection *connection,
2282                 const gchar *object_path, GVariant *parameters)
2283 {
2284         __WDP_LOG_FUNC_ENTER__;
2285         GVariantIter *iter = NULL;
2286         wfd_oem_event_s event;
2287         wfd_oem_asp_service_s *service, *tmp;
2288
2289         service = (wfd_oem_asp_service_s *) g_try_malloc0(sizeof(wfd_oem_asp_service_s));
2290         if (!service) {
2291                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2292                                 strerror(errno));
2293                 __WDP_LOG_FUNC_EXIT__;
2294                 return;
2295         }
2296         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2297
2298         event.edata = (void*) service;
2299         event.edata_type = WFD_OEM_EDATA_TYPE_ASP_SERVICE;
2300         event.event_id = WFD_OEM_EVENT_ASP_SERV_RESP;
2301
2302         if(parameters != NULL) {
2303                 g_variant_get(parameters, "(a{sv})", &iter);
2304                 if(iter != NULL) {
2305                         dbus_property_foreach(iter, __ws_extract_serviceaspresponse_details, &event);
2306                         g_variant_iter_free(iter);
2307                 }
2308         } else {
2309                 WDP_LOGE("No Properties");
2310         }
2311 GLIST_ITER_START(seek_list, tmp)
2312         if(tmp->tran_id == service->tran_id) {
2313                 WDP_LOGD("srv_trans_id matched [%d] search_id [%llu]"
2314                                 ,tmp->tran_id, tmp->search_id);
2315                 service->search_id = tmp->search_id;
2316                 break;
2317         } else {
2318                 tmp = NULL;
2319         }
2320 GLIST_ITER_END()
2321
2322         if(tmp->service_info != NULL)
2323                 g_pd->callback(g_pd->user_data, &event);
2324         else
2325                 WDP_LOGD("service info is not required, don't notify to user");
2326
2327         g_free(service->service_type);
2328         g_free(service->service_info);
2329         g_free(service);
2330
2331         __WDP_LOG_FUNC_EXIT__;
2332 }
2333 #endif /* TIZEN_FEATURE_ASP */
2334
2335 static void _ws_process_persistent_group_added(GDBusConnection *connection,
2336                 const gchar *object_path, GVariant *parameters)
2337 {
2338         __WDP_LOG_FUNC_ENTER__;
2339         wfd_oem_event_s event;
2340         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2341
2342 //      g_pd->callback(g_pd->user_data, &event);
2343
2344         __WDP_LOG_FUNC_EXIT__;
2345 }
2346
2347 static void _ws_process_persistent_group_removed(GDBusConnection *connection,
2348                 const gchar *object_path, GVariant *parameters)
2349 {
2350         __WDP_LOG_FUNC_ENTER__;
2351         wfd_oem_event_s event;
2352         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2353
2354 //      g_pd->callback(g_pd->user_data, &event);
2355
2356         __WDP_LOG_FUNC_EXIT__;
2357 }
2358
2359 static void _ws_process_wps_failed(GDBusConnection *connection,
2360                 const gchar *object_path, GVariant *parameters)
2361 {
2362         __WDP_LOG_FUNC_ENTER__;
2363         GVariantIter *iter = NULL;
2364         wfd_oem_event_s event;
2365         const char *name = NULL;
2366
2367         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2368
2369         event.event_id = WFD_OEM_EVENT_WPS_FAIL;
2370         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2371
2372         g_variant_get(parameters, "(&sa{sv})", &name, &iter);
2373
2374         WDP_LOGD("code [%s]", name);
2375
2376         if (iter != NULL) {
2377
2378                 gchar *key = NULL;
2379                 GVariant *value = NULL;
2380
2381                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
2382 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2383                         CHECK_KEY_VALUE(key, value);
2384 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2385                 }
2386                 g_variant_iter_free(iter);
2387         }
2388
2389         g_pd->callback(g_pd->user_data, &event);
2390
2391         __WDP_LOG_FUNC_EXIT__;
2392 }
2393
2394 static void _ws_process_group_formation_failure(GDBusConnection *connection,
2395                 const gchar *object_path, GVariant *parameters)
2396 {
2397         __WDP_LOG_FUNC_ENTER__;
2398         wfd_oem_event_s event;
2399
2400         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2401
2402         event.event_id = WFD_OEM_EVENT_GROUP_FORMATION_FAILURE;
2403         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2404
2405         g_pd->callback(g_pd->user_data, &event);
2406
2407         __WDP_LOG_FUNC_EXIT__;
2408 }
2409
2410 static void _ws_process_invitation_accepted(GDBusConnection *connection,
2411                 const gchar *object_path, GVariant *parameters)
2412 {
2413         __WDP_LOG_FUNC_ENTER__;
2414         GVariantIter *iter = NULL;
2415         wfd_oem_event_s event;
2416
2417         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2418
2419         event.event_id = WFD_OEM_EVENT_INVITATION_ACCEPTED;
2420         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2421
2422         if (parameters != NULL) {
2423                 g_variant_get(parameters, "(a{sv})", &iter);
2424
2425                 if (iter != NULL) {
2426                         gchar *key = NULL;
2427                         GVariant *value = NULL;
2428
2429                         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
2430                                 CHECK_KEY_VALUE(key, value);
2431                                 if(g_strcmp0(key, "sa") == 0)
2432                                         if (__ws_unpack_ay(event.dev_addr, value, WS_MACADDR_LEN))
2433                                                 WDP_LOGI("[" MACSTR "]", MAC2STR(event.dev_addr));
2434                         }
2435                         g_variant_iter_free(iter);
2436                 }
2437         }
2438
2439         g_pd->callback(g_pd->user_data, &event);
2440         __WDP_LOG_FUNC_EXIT__;
2441 }
2442
2443 #if defined(TIZEN_FEATURE_ASP)
2444 static void _ws_process_asp_provision_start(GDBusConnection *connection,
2445                 const gchar *object_path, GVariant *parameters)
2446 {
2447         __WDP_LOG_FUNC_ENTER__;
2448         GVariantIter *iter = NULL;
2449         wfd_oem_event_s event;
2450         wfd_oem_asp_prov_s *edata;
2451
2452         edata = (wfd_oem_asp_prov_s *) g_try_malloc0(sizeof(wfd_oem_asp_prov_s));
2453         if (!edata) {
2454                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2455                                 strerror(errno));
2456                 __WDP_LOG_FUNC_EXIT__;
2457                 return;
2458         }
2459         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2460
2461         event.edata = (void*) edata;
2462         event.event_id = WFD_OEM_EVENT_ASP_PROV_START;
2463
2464         if (parameters != NULL) {
2465                 g_variant_get(parameters, "(a{sv})", &iter);
2466                 if (iter != NULL) {
2467                         dbus_property_foreach(iter, __ws_extract_asp_provision_start_details, &event);
2468                         event.edata_type = WFD_OEM_EDATA_TYPE_ASP_PROV;
2469                         g_variant_iter_free(iter);
2470                 }
2471         } else {
2472                 WDP_LOGE("No Properties");
2473         }
2474
2475         g_pd->callback(g_pd->user_data, &event);
2476
2477         if (event.edata_type == WFD_OEM_EDATA_TYPE_ASP_PROV)
2478                 g_free(edata->session_information);
2479         g_free(edata);
2480
2481         __WDP_LOG_FUNC_EXIT__;
2482 }
2483
2484 static void _ws_process_asp_provision_done(GDBusConnection *connection,
2485                 const gchar *object_path, GVariant *parameters)
2486 {
2487         __WDP_LOG_FUNC_ENTER__;
2488         GVariantIter *iter = NULL;
2489         wfd_oem_event_s event;
2490         wfd_oem_asp_prov_s *edata;
2491
2492         edata = (wfd_oem_asp_prov_s *) g_try_malloc0(sizeof(wfd_oem_asp_prov_s));
2493         if (!edata) {
2494                 WDP_LOGF("Failed to allocate memory for event. [%s]",
2495                                 strerror(errno));
2496                 __WDP_LOG_FUNC_EXIT__;
2497                 return;
2498         }
2499         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2500
2501         event.edata = (void*) edata;
2502         event.event_id = WFD_OEM_EVENT_ASP_PROV_DONE;
2503
2504         if (parameters != NULL) {
2505                 g_variant_get(parameters, "(a{sv})", &iter);
2506                 if (iter != NULL) {
2507                         dbus_property_foreach(iter, __ws_extract_asp_provision_done_details, &event);
2508                         event.edata_type = WFD_OEM_EDATA_TYPE_ASP_PROV;
2509                         g_variant_iter_free(iter);
2510                 }
2511         } else {
2512                 WDP_LOGE("No Properties");
2513         }
2514
2515         g_pd->callback(g_pd->user_data, &event);
2516         g_free(edata);
2517
2518         __WDP_LOG_FUNC_EXIT__;
2519 }
2520 #endif /* TIZEN_FEATURE_ASP */
2521
2522 static struct {
2523         const char *interface;
2524         const char *member;
2525         void (*function) (GDBusConnection *connection,const gchar *object_path,
2526                         GVariant *parameters);
2527 } ws_p2pdevice_signal_map[] = {
2528         {
2529                 SUPPLICANT_P2PDEVICE,
2530                 "DeviceFound",
2531                 _ws_process_device_found
2532         },
2533         {
2534                 SUPPLICANT_P2PDEVICE,
2535                 "DeviceLost",
2536                 _ws_process_device_lost
2537         },
2538         {
2539                 SUPPLICANT_P2PDEVICE,
2540                 "FindStopped",
2541                 _ws_process_find_stoppped
2542         },
2543         {
2544                 SUPPLICANT_P2PDEVICE,
2545                 "ProvisionDiscoveryRequestDisplayPin",
2546                 _ws_process_prov_disc_req_display_pin
2547         },
2548         {
2549                 SUPPLICANT_P2PDEVICE,
2550                 "ProvisionDiscoveryResponseDisplayPin",
2551                 _ws_process_prov_disc_resp_display_pin
2552         },
2553         {
2554                 SUPPLICANT_P2PDEVICE,
2555                 "ProvisionDiscoveryRequestEnterPin",
2556                 _ws_process_prov_disc_req_enter_pin
2557         },
2558         {
2559                 SUPPLICANT_P2PDEVICE,
2560                 "ProvisionDiscoveryResponseEnterPin",
2561                 _ws_process_prov_disc_resp_enter_pin
2562         },
2563         {
2564                 SUPPLICANT_P2PDEVICE,
2565                 "ProvisionDiscoveryPBCRequest",
2566                 _ws_process_prov_disc_pbc_req
2567         },
2568         {
2569                 SUPPLICANT_P2PDEVICE,
2570                 "ProvisionDiscoveryPBCResponse",
2571                 _ws_process_prov_disc_pbc_resp
2572         },
2573         {
2574                 SUPPLICANT_P2PDEVICE,
2575                 "ProvisionDiscoveryFailure",
2576                 _ws_process_prov_disc_failure
2577         },
2578         {
2579                 SUPPLICANT_P2PDEVICE,
2580                 "GroupStarted",
2581                 _ws_process_group_started
2582         },
2583         {
2584                 SUPPLICANT_P2PDEVICE,
2585                 "GONegotiationSuccess",
2586                 _ws_process_go_neg_success
2587         },
2588         {
2589                 SUPPLICANT_P2PDEVICE,
2590                 "GONegotiationFailure",
2591                 _ws_process_go_neg_failure
2592         },
2593         {
2594                 SUPPLICANT_P2PDEVICE,
2595                 "GONegotiationRequest",
2596                 _ws_process_go_neg_request
2597         },
2598         {
2599                 SUPPLICANT_P2PDEVICE,
2600                 "InvitationReceived",
2601                 _ws_process_invitation_received
2602         },
2603         {
2604                 SUPPLICANT_P2PDEVICE,
2605                 "InvitationResult",
2606                 _ws_process_invitation_result
2607         },
2608         {
2609                 SUPPLICANT_P2PDEVICE,
2610                 "GroupFinished",
2611                 _ws_process_group_finished
2612         },
2613 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
2614         {
2615                 SUPPLICANT_P2PDEVICE,
2616                 "ServiceDiscoveryResponse",
2617                 _ws_process_service_discovery_response
2618         },
2619 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
2620 #if defined(TIZEN_FEATURE_ASP)
2621         {
2622                 SUPPLICANT_P2PDEVICE,
2623                 "ServiceASPResponse",
2624                 _ws_process_service_asp_response
2625         },
2626         {
2627                 SUPPLICANT_P2PDEVICE,
2628                 "ASPProvisionStart",
2629                 _ws_process_asp_provision_start
2630         },
2631         {
2632                 SUPPLICANT_P2PDEVICE,
2633                 "ASPProvisionDone",
2634                 _ws_process_asp_provision_done
2635         },
2636 #endif /* TIZEN_FEATURE_ASP */
2637         {
2638                 SUPPLICANT_P2PDEVICE,
2639                 "PersistentGroupAdded",
2640                 _ws_process_persistent_group_added
2641         },
2642         {
2643                 SUPPLICANT_P2PDEVICE,
2644                 "PersistentGroupRemoved",
2645                 _ws_process_persistent_group_removed
2646         },
2647         {
2648                 SUPPLICANT_P2PDEVICE,
2649                 "WpsFailed",
2650                 _ws_process_wps_failed
2651         },
2652         {
2653                 SUPPLICANT_P2PDEVICE,
2654                 "GroupFormationFailure",
2655                 _ws_process_group_formation_failure
2656         },
2657         {
2658                 SUPPLICANT_P2PDEVICE,
2659                 "InvitationAccepted",
2660                 _ws_process_invitation_accepted
2661         },
2662         {
2663                 NULL,
2664                 NULL,
2665                 NULL
2666         }
2667 };
2668
2669 static void _p2pdevice_signal_cb(GDBusConnection *connection,
2670                 const gchar *sender, const gchar *object_path, const gchar *interface,
2671                 const gchar *signal, GVariant *parameters, gpointer user_data)
2672 {
2673         int i = 0;
2674 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2675         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
2676 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2677
2678         if (!g_pd) {
2679                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2680                 return;
2681         }
2682
2683         for (i = 0; ws_p2pdevice_signal_map[i].member != NULL; i++) {
2684                 if (!g_strcmp0(signal, ws_p2pdevice_signal_map[i].member) &&
2685                                 ws_p2pdevice_signal_map[i].function != NULL)
2686                         ws_p2pdevice_signal_map[i].function(connection, object_path, parameters);
2687         }
2688 }
2689
2690
2691 static void _ws_process_sta_authorized(GDBusConnection *connection,
2692                 const gchar *object_path, GVariant *parameters)
2693 {
2694         __WDP_LOG_FUNC_ENTER__;
2695         wfd_oem_event_s event;
2696         const gchar* mac_str = NULL;
2697
2698         if (is_peer_joined_notified) {
2699                 is_peer_joined_notified = 0;
2700                 __WDP_LOG_FUNC_EXIT__;
2701                 return;
2702         }
2703
2704         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2705         g_variant_get(parameters, "(&s)", &mac_str);
2706         __ws_txt_to_mac((unsigned char *)mac_str, event.intf_addr);
2707
2708         event.event_id = WFD_OEM_EVENT_STA_CONNECTED;
2709         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2710
2711         g_pd->callback(g_pd->user_data, &event);
2712         __WDP_LOG_FUNC_EXIT__;
2713 }
2714
2715 static void _ws_process_sta_deauthorized(GDBusConnection *connection,
2716                 const gchar *object_path, GVariant *parameters)
2717 {
2718         __WDP_LOG_FUNC_ENTER__;
2719         wfd_oem_event_s event;
2720         const gchar* mac_str = NULL;
2721
2722         if (is_peer_disconnected_notified) {
2723                 is_peer_disconnected_notified = 0;
2724                 __WDP_LOG_FUNC_EXIT__;
2725                 return;
2726         }
2727
2728         memset(&event, 0x0, sizeof(wfd_oem_event_s));
2729         g_variant_get(parameters, "(&s)", &mac_str);
2730         __ws_txt_to_mac((unsigned char *)mac_str, event.intf_addr);
2731
2732         event.event_id = WFD_OEM_EVENT_STA_DISCONNECTED;
2733         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2734
2735         g_pd->callback(g_pd->user_data, &event);
2736         __WDP_LOG_FUNC_EXIT__;
2737 }
2738
2739 static struct {
2740         const char *interface;
2741         const char *member;
2742         void (*function) (GDBusConnection *connection,const gchar *object_path,
2743                         GVariant *parameters);
2744 } ws_interface_signal_map[] = {
2745         {
2746                 SUPPLICANT_INTERFACE,
2747                 "StaAuthorized",
2748                 _ws_process_sta_authorized
2749         },
2750         {
2751                 SUPPLICANT_INTERFACE,
2752                 "StaDeauthorized",
2753                 _ws_process_sta_deauthorized
2754         },
2755         {
2756                 NULL,
2757                 NULL,
2758                 NULL
2759         }
2760 };
2761
2762 static void _interface_signal_cb(GDBusConnection *connection,
2763                 const gchar *sender, const gchar *object_path, const gchar *interface,
2764                 const gchar *signal, GVariant *parameters, gpointer user_data)
2765 {
2766         int i = 0;
2767 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2768         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
2769 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2770
2771         if (!g_pd) {
2772                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2773                 __WDP_LOG_FUNC_EXIT__;
2774                 return;
2775         }
2776
2777         for (i = 0; ws_interface_signal_map[i].member != NULL; i++) {
2778                 if (!g_strcmp0(signal, ws_interface_signal_map[i].member) &&
2779                                 ws_interface_signal_map[i].function != NULL)
2780                         ws_interface_signal_map[i].function(connection, object_path, parameters);
2781         }
2782 }
2783
2784
2785 static void __ws_parse_peer_joined(char *peer_path,
2786                 unsigned char *dev_addr, unsigned char *ip_addr, GVariant *parameter)
2787 {
2788         __WDP_LOG_FUNC_ENTER__;
2789
2790         GVariantIter *iter;
2791         static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
2792         const char *path = NULL;
2793         char *loc = NULL;
2794 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2795         int i = 0;
2796 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2797
2798         g_variant_get(parameter, "(&oay)", &path, &iter);
2799         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
2800         WDP_LOGD("Retrive Added path [%s]", peer_path);
2801
2802         loc = strrchr(peer_path,'/');
2803         if(loc != NULL)
2804                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
2805         __ws_txt_to_mac(peer_dev, dev_addr);
2806         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(dev_addr));
2807 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2808         for(i = 0; i < OEM_IPADDR_LEN; i++)
2809                 g_variant_iter_loop (iter, "y", &ip_addr[i]);
2810         g_variant_iter_free(iter);
2811
2812         WDP_LOGD("peer ip [" IPSTR "]", IP2STR(ip_addr));
2813 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2814
2815         __WDP_LOG_FUNC_EXIT__;
2816         return;
2817 }
2818
2819
2820 static void _group_signal_cb(GDBusConnection *connection,
2821                 const gchar *sender, const gchar *object_path, const gchar *interface,
2822                 const gchar *signal, GVariant *parameters, gpointer user_data)
2823 {
2824 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2825         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
2826 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2827
2828         if (!g_pd) {
2829                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2830                 return;
2831         }
2832
2833         if (!g_strcmp0(signal,"PeerJoined")) {
2834
2835                 wfd_oem_event_s event;
2836                 wfd_oem_dev_data_s *edata = NULL;
2837
2838                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2839
2840                 edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
2841                 if (!edata) {
2842                         WDP_LOGF("Failed to allocate memory for event. [%s]",
2843                                         strerror(errno));
2844                         __WDP_LOG_FUNC_EXIT__;
2845                         return;
2846                 }
2847                 memset(&event, 0x0, sizeof(wfd_oem_event_s));
2848
2849                 event.edata = (void*) edata;
2850                 event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
2851                 event.event_id = WFD_OEM_EVENT_STA_CONNECTED;
2852
2853                 __ws_parse_peer_joined(peer_path, event.dev_addr, event.ip_addr_peer, parameters);
2854
2855                 dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
2856                                 __ws_peer_property, event.edata);
2857
2858                 g_pd->callback(g_pd->user_data, &event);
2859                 is_peer_joined_notified = 1;
2860
2861                 g_free(edata);
2862
2863         } else if (!g_strcmp0(signal,"PeerDisconnected")) {
2864
2865                 wfd_oem_event_s event;
2866
2867                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2868
2869                 memset(&event, 0x0, sizeof(wfd_oem_event_s));
2870
2871                 event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2872                 event.event_id = WFD_OEM_EVENT_STA_DISCONNECTED;
2873
2874                 __ws_path_to_addr(peer_path, event.dev_addr, parameters);
2875
2876                 g_pd->callback(g_pd->user_data, &event);
2877                 is_peer_disconnected_notified = 1;
2878         }
2879 }
2880
2881 static void __register_p2pdevice_signal(GVariant *value, void *user_data)
2882 {
2883         __WDP_LOG_FUNC_ENTER__;
2884         ws_dbus_plugin_data_s * pd_data;
2885         static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2886         const char *path = NULL;
2887
2888         if (!g_pd) {
2889                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2890                 return;
2891         }
2892
2893         pd_data = (ws_dbus_plugin_data_s *)g_pd;
2894
2895         g_variant_get(value, "(&o)", &path);
2896         g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
2897         g_strlcpy(pd_data->iface_path, path, DBUS_OBJECT_PATH_MAX);
2898
2899         WDP_LOGD("interface object path [%s]", interface_path);
2900
2901         /* subscribe Interface iface signal */
2902         WDP_LOGD("Register Interface iface signal");
2903         pd_data->p2pdevice_sub_id = g_dbus_connection_signal_subscribe(
2904                 pd_data->g_dbus,
2905                 SUPPLICANT_SERVICE, /* bus name */
2906                 SUPPLICANT_IFACE, /* interface */
2907                 NULL, /* member */
2908                 NULL, /* object path */
2909                 NULL, /* arg0 */
2910                 G_DBUS_SIGNAL_FLAGS_NONE,
2911                 _interface_signal_cb,
2912                 NULL, NULL);
2913
2914         /* subscribe P2PDevice iface signal */
2915         WDP_LOGD("register P2PDevice iface signal");
2916         pd_data->p2pdevice_sub_id = g_dbus_connection_signal_subscribe(
2917                 pd_data->g_dbus,
2918                 SUPPLICANT_SERVICE, /* bus name */
2919                 SUPPLICANT_P2PDEVICE, /* interface */
2920                 NULL, /* member */
2921                 NULL, /* object path */
2922                 NULL, /* arg0 */
2923                 G_DBUS_SIGNAL_FLAGS_NONE,
2924                 _p2pdevice_signal_cb,
2925                 NULL, NULL);
2926         __WDP_LOG_FUNC_EXIT__;
2927 }
2928
2929 static int _ws_create_interface(const char *iface_name, handle_reply function, void *user_data)
2930 {
2931         __WDP_LOG_FUNC_ENTER__;
2932         GDBusConnection *g_dbus = NULL;
2933         GVariantBuilder *builder = NULL;
2934         dbus_method_param_s params;
2935
2936         int res = 0;
2937
2938         if (!g_pd) {
2939                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2940                 return -1;
2941         }
2942
2943         g_dbus = g_pd->g_dbus;
2944         if (!g_dbus) {
2945                 WDP_LOGE("DBus connection is NULL");
2946                 return -1;
2947         }
2948         memset(&params, 0x0, sizeof(dbus_method_param_s));
2949
2950         dbus_set_method_param(&params, "CreateInterface", SUPPLICANT_PATH, g_dbus);
2951
2952         builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
2953         g_variant_builder_add(builder, "{sv}", "Ifname", g_variant_new_string(iface_name));
2954         g_variant_builder_add(builder, "{sv}", "ConfigFile", g_variant_new_string(CONF_FILE_PATH));
2955         params.params = g_variant_new("(a{sv})", builder);
2956         g_variant_builder_unref(builder);
2957         res = dbus_method_call(&params, SUPPLICANT_INTERFACE, function, user_data);
2958         if (res < 0)
2959                 WDP_LOGE("Failed to send command to wpa_supplicant");
2960         else
2961                 WDP_LOGD("Succeeded to CreateInterface");
2962
2963         __WDP_LOG_FUNC_EXIT__;
2964         return 0;
2965 }
2966
2967 static int _ws_get_interface(const char *iface_name, handle_reply function, void *user_data)
2968 {
2969         __WDP_LOG_FUNC_ENTER__;
2970         GDBusConnection *g_dbus = NULL;
2971         dbus_method_param_s params;
2972         int res = 0;
2973
2974         if (!g_pd) {
2975                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2976                 return -1;
2977         }
2978
2979         g_dbus = g_pd->g_dbus;
2980         if (!g_dbus) {
2981                 WDP_LOGE("DBus connection is NULL");
2982                 return -1;
2983         }
2984
2985         dbus_set_method_param(&params, SUPPLICANT_METHOD_GETINTERFACE,
2986                         SUPPLICANT_PATH, g_pd->g_dbus);
2987
2988         params.params = g_variant_new("(s)", iface_name);
2989 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2990         DEBUG_PARAMS(params.params);
2991 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2992
2993         res = dbus_method_call(&params, SUPPLICANT_INTERFACE,
2994                         function, user_data);
2995
2996         if (res < 0)
2997                 WDP_LOGE("Failed to send command to wpa_supplicant");
2998         else
2999                 WDP_LOGD("Succeeded to get interface");
3000
3001         __WDP_LOG_FUNC_EXIT__;
3002         return res;
3003 }
3004
3005 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
3006 static void __ws_remove_interface(GVariant *value, void *user_data)
3007 {
3008         __WDP_LOG_FUNC_ENTER__;
3009         GDBusConnection *g_dbus = NULL;
3010         dbus_method_param_s params;
3011         const char *path = NULL;
3012         static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3013         int res = 0;
3014
3015         if (!g_pd) {
3016                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3017                 return;
3018         }
3019
3020         g_dbus = g_pd->g_dbus;
3021         if (!g_dbus) {
3022                 WDP_LOGE("DBus connection is NULL");
3023                 return;
3024         }
3025
3026         g_variant_get(value, "(&o)", &path);
3027         g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
3028         WDP_LOGD("interface object path [%s]", interface_path);
3029
3030         memset(&params, 0x0, sizeof(dbus_method_param_s));
3031
3032         dbus_set_method_param(&params, "RemoveInterface", SUPPLICANT_PATH, g_dbus);
3033         params.params = g_variant_new("(o)", interface_path);
3034
3035         res = dbus_method_call(&params, SUPPLICANT_INTERFACE, NULL, NULL);
3036         if (res < 0)
3037                 WDP_LOGE("Failed to send command to wpa_supplicant");
3038         else
3039                 WDP_LOGD("Succeeded to RemoveInterface");
3040
3041         __WDP_LOG_FUNC_EXIT__;
3042         return;
3043 }
3044 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
3045
3046 static int _ws_init_dbus_connection(void)
3047 {
3048         __WDP_LOG_FUNC_ENTER__;
3049         GDBusConnection *conn = NULL;
3050         GError *error = NULL;
3051         int res = 0;
3052
3053         if (!g_pd) {
3054                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3055                 return -1;
3056         }
3057
3058         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3059
3060         if (conn == NULL) {
3061                 if(error != NULL){
3062                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
3063                                         error->message);
3064                         g_error_free(error);
3065                 }
3066                 __WDP_LOG_FUNC_EXIT__;
3067                 return -1;
3068         }
3069
3070         g_pd->g_dbus = conn;
3071
3072         WDP_LOGD("register supplicant signal");
3073         /* subscribe supplicant signal */
3074         g_pd->supp_sub_id = g_dbus_connection_signal_subscribe(
3075                 g_pd->g_dbus,
3076                 SUPPLICANT_SERVICE, /* bus name */
3077                 SUPPLICANT_INTERFACE, /* interface */
3078                 NULL, /* member */
3079                 SUPPLICANT_PATH, /* object path */
3080                 NULL, /* arg0 */
3081                 G_DBUS_SIGNAL_FLAGS_NONE,
3082                 _supplicant_signal_cb,
3083                 NULL, NULL);
3084 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
3085         if(_ws_get_interface(COMMON_IFACE_NAME, NULL, NULL) < 0)
3086                 _ws_create_interface(COMMON_IFACE_NAME, NULL, NULL);
3087         if(_ws_get_interface(P2P_IFACE_NAME, __register_p2pdevice_signal, NULL) < 0)
3088                 res = _ws_create_interface(P2P_IFACE_NAME, __register_p2pdevice_signal, NULL);
3089 #else /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
3090         if(_ws_get_interface(COMMON_IFACE_NAME, __register_p2pdevice_signal, NULL) < 0)
3091                 res = _ws_create_interface(COMMON_IFACE_NAME, __register_p2pdevice_signal, NULL);
3092 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
3093
3094         if (res < 0)
3095                 WDP_LOGE("Failed to subscribe interface signal");
3096         else
3097                 WDP_LOGI("Successfully register signal filters");
3098
3099         __WDP_LOG_FUNC_EXIT__;
3100         return res;
3101 }
3102
3103 static int _ws_deinit_dbus_connection(void)
3104 {
3105         GDBusConnection *g_dbus = NULL;
3106
3107         if (!g_pd) {
3108                 WDP_LOGE("Invalid parameter");
3109                 __WDP_LOG_FUNC_EXIT__;
3110                 return -1;
3111         }
3112
3113         g_dbus = g_pd->g_dbus;
3114         if (!g_dbus) {
3115                 WDP_LOGE("DBus connection is NULL");
3116                 return -1;
3117         }
3118
3119         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->supp_sub_id);
3120         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->p2pdevice_sub_id);
3121         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->group_sub_id);
3122
3123         g_pd->group_iface_sub_id = 0;
3124         g_pd->p2pdevice_sub_id = 0;
3125         g_pd->group_sub_id = 0;
3126         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
3127         memset(g_pd->iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
3128
3129         g_object_unref(g_dbus);
3130         return 0;
3131 }
3132
3133 int wfd_plugin_load(wfd_oem_ops_s **ops)
3134 {
3135         if (!ops) {
3136                 WDP_LOGE("Invalid parameter");
3137                 return -1;
3138         }
3139
3140         *ops = &supplicant_ops;
3141
3142         return 0;
3143 }
3144
3145 static int _ws_reset_plugin(ws_dbus_plugin_data_s *f_pd)
3146 {
3147         __WDP_LOG_FUNC_ENTER__;
3148
3149         if (!f_pd) {
3150                 WDP_LOGE("Invalid parameter");
3151                 __WDP_LOG_FUNC_EXIT__;
3152                 return -1;
3153         }
3154
3155         _ws_deinit_dbus_connection();
3156
3157         if (f_pd->activated)
3158                 ws_deactivate(f_pd->concurrent);
3159
3160         g_free(f_pd);
3161
3162         __WDP_LOG_FUNC_EXIT__;
3163         return 0;
3164 }
3165
3166 #ifndef TIZEN_WIFI_MODULE_BUNDLE
3167 static int __ws_check_net_interface(char* if_name)
3168 {
3169         struct ifreq ifr;
3170         int fd;
3171
3172         if (if_name == NULL) {
3173                 WDP_LOGE("Invalid param");
3174                 return -1;
3175         }
3176
3177         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
3178         if (fd < 0) {
3179                 WDP_LOGE("socket create error: %d", fd);
3180                 return -2;
3181         }
3182
3183         memset(&ifr, 0, sizeof(ifr));
3184         strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
3185         ifr.ifr_name[IFNAMSIZ-1] = '\0';
3186
3187         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
3188                 close(fd);
3189                 WDP_LOGE("ioctl error: SIOCGIFFLAGS: %s [ %s ]", strerror(errno), if_name); /* interface is not found. */
3190                 return -3;
3191         }
3192
3193         close(fd);
3194
3195         if (ifr.ifr_flags & IFF_UP) {
3196                 WDP_LOGD("%s interface is up", if_name);
3197                 return 1;
3198         } else if (!(ifr.ifr_flags & IFF_UP)) {
3199                 WDP_LOGD("%s interface is down", if_name);
3200                 return 0;
3201         }
3202         return 0;
3203 }
3204 #endif
3205
3206 int ws_init(wfd_oem_event_cb callback, void *user_data)
3207 {
3208         __WDP_LOG_FUNC_ENTER__;
3209
3210         if (g_pd)
3211                 _ws_reset_plugin(g_pd);
3212
3213         errno = 0;
3214         g_pd = (ws_dbus_plugin_data_s*) g_try_malloc0 (sizeof(ws_dbus_plugin_data_s));
3215         if (!g_pd) {
3216                 WDP_LOGE("Failed to allocate memory for plugin data. [%s]", strerror(errno));
3217                 return -1;
3218         }
3219
3220         g_pd->callback = callback;
3221         g_pd->user_data = user_data;
3222         g_pd->initialized = TRUE;
3223
3224         __WDP_LOG_FUNC_EXIT__;
3225         return 0;
3226 }
3227
3228 int ws_deinit()
3229 {
3230         __WDP_LOG_FUNC_ENTER__;
3231
3232         if (g_pd) {
3233                 _ws_reset_plugin(g_pd);
3234                 g_pd = NULL;
3235         }
3236
3237         __WDP_LOG_FUNC_EXIT__;
3238         return 0;
3239 }
3240
3241 gboolean _ws_util_execute_file(const char *file_path,
3242         char *const args[], char *const envs[])
3243 {
3244         pid_t pid = 0;
3245         int rv = 0;
3246         errno = 0;
3247         register unsigned int index = 0;
3248
3249         while (args[index] != NULL) {
3250                 WDP_LOGD("[%s]", args[index]);
3251                 index++;
3252         }
3253
3254         if (!(pid = fork())) {
3255                 WDP_LOGD("pid(%d), ppid(%d)", getpid(), getppid());
3256                 WDP_LOGD("Inside child, exec (%s) command", file_path);
3257
3258                 errno = 0;
3259                 if (execve(file_path, args, envs) == -1) {
3260                         WDP_LOGE("Fail to execute command (%s)", strerror(errno));
3261                         exit(1);
3262                 }
3263         } else if (pid > 0) {
3264                 if (waitpid(pid, &rv, 0) == -1)
3265                         WDP_LOGD("wait pid (%u) rv (%d)", pid, rv);
3266                 if (WIFEXITED(rv)) {
3267                         WDP_LOGD("exited, rv=%d", WEXITSTATUS(rv));
3268                 } else if (WIFSIGNALED(rv)) {
3269                         WDP_LOGD("killed by signal %d", WTERMSIG(rv));
3270                 } else if (WIFSTOPPED(rv)) {
3271                         WDP_LOGD("stopped by signal %d", WSTOPSIG(rv));
3272                 } else if (WIFCONTINUED(rv)) {
3273                         WDP_LOGD("continued");
3274                 }
3275
3276                 return TRUE;
3277         }
3278
3279         WDP_LOGE("failed to fork (%s)", strerror(errno));
3280         return FALSE;
3281 }
3282
3283 #ifndef TIZEN_WIFI_MODULE_BUNDLE
3284 static int __ws_p2p_firmware_start(void)
3285 {
3286         GError *error = NULL;
3287         GVariant *reply = NULL;
3288         GVariant *param = NULL;
3289         GDBusConnection *connection = NULL;
3290         const char *device = "p2p";
3291
3292         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3293         if (connection == NULL) {
3294                 if(error != NULL){
3295                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
3296                                         error->message);
3297                         g_error_free(error);
3298                 }
3299                 __WDP_LOG_FUNC_EXIT__;
3300                 return -1;
3301         }
3302         param = g_variant_new("(s)", device);
3303
3304         reply = g_dbus_connection_call_sync (connection,
3305                         NETCONFIG_SERVICE, /* bus name */
3306                         NETCONFIG_WIFI_PATH, /* object path */
3307                         NETCONFIG_WIFI_INTERFACE ".Firmware", /* interface name */
3308                         "Start", /* method name */
3309                         param, /* GVariant *params */
3310                         NULL, /* reply_type */
3311                         G_DBUS_CALL_FLAGS_NONE, /* flags */
3312                         NETCONFIG_DBUS_REPLY_TIMEOUT , /* timeout */
3313                         NULL, /* cancellable */
3314                         &error); /* error */
3315
3316         if(error != NULL){
3317                 if(strstr(error->message, ".AlreadyExists") != NULL) {
3318                         WDP_LOGD("p2p already enabled");
3319                         g_error_free(error);
3320
3321                 } else {
3322                         WDP_LOGE("Error! Failed to call net-config method: [%s]",
3323                                         error->message);
3324                         g_error_free(error);
3325                         if(reply)
3326                                  g_variant_unref(reply);
3327                         g_object_unref(connection);
3328                         __WDP_LOG_FUNC_EXIT__;
3329                         return -1;
3330                 }
3331         }
3332         if(reply)
3333                  g_variant_unref(reply);
3334         g_object_unref(connection);
3335         return 0;
3336 }
3337
3338 static int __ws_p2p_firmware_stop(void)
3339 {
3340         GError *error = NULL;
3341         GVariant *reply = NULL;
3342         GVariant *param = NULL;
3343         GDBusConnection *connection = NULL;
3344         const char *device = "p2p";
3345
3346         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3347         if (connection == NULL) {
3348                 if(error != NULL){
3349                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
3350                                         error->message);
3351                         g_error_free(error);
3352                 }
3353                 __WDP_LOG_FUNC_EXIT__;
3354                 return -1;
3355         }
3356         param = g_variant_new("(s)", device);
3357
3358         reply = g_dbus_connection_call_sync (connection,
3359                         NETCONFIG_SERVICE, /* bus name */
3360                         NETCONFIG_WIFI_PATH, /* object path */
3361                         NETCONFIG_WIFI_INTERFACE ".Firmware", /* interface name */
3362                         "Stop", /* method name */
3363                         param, /* GVariant *params */
3364                         NULL, /* reply_type */
3365                         G_DBUS_CALL_FLAGS_NONE, /* flags */
3366                         NETCONFIG_DBUS_REPLY_TIMEOUT , /* timeout */
3367                         NULL, /* cancellable */
3368                         &error); /* error */
3369
3370         if(error != NULL){
3371                 if(strstr(error->message, ".AlreadyExists") != NULL) {
3372                         WDP_LOGD("p2p already disabled");
3373                         g_error_free(error);
3374
3375                 } else {
3376                         WDP_LOGE("Error! Failed to call net-config method: [%s]",
3377                                         error->message);
3378                         g_error_free(error);
3379                         if(reply)
3380                                  g_variant_unref(reply);
3381                         g_object_unref(connection);
3382                         __WDP_LOG_FUNC_EXIT__;
3383                         return -1;
3384                 }
3385         }
3386         if(reply)
3387                  g_variant_unref(reply);
3388         g_object_unref(connection);
3389         return 0;
3390 }
3391 #endif
3392
3393 static int __ws_p2p_supplicant_start(void)
3394 {
3395         gboolean rv = FALSE;
3396         const char *path = "/usr/sbin/p2p_supp.sh";
3397         char *const args[] = { "/usr/sbin/p2p_supp.sh", "start_dbus", NULL };
3398         char *const envs[] = { NULL };
3399
3400         rv = _ws_util_execute_file(path, args, envs);
3401
3402         if (rv != TRUE) {
3403                 WDP_LOGE("Failed to start p2p_supp.sh");
3404                 return -1;
3405         }
3406
3407         WDP_LOGI("Successfully started p2p_supp.sh");
3408         return 0;
3409 }
3410
3411
3412 static int __ws_p2p_supplicant_stop(void)
3413 {
3414         gboolean rv = FALSE;
3415         const char *path = "/usr/sbin/p2p_supp.sh";
3416         char *const args[] = { "/usr/sbin/p2p_supp.sh", "stop", NULL };
3417         char *const envs[] = { NULL };
3418
3419         rv = _ws_util_execute_file(path, args, envs);
3420
3421         if (rv != TRUE) {
3422                 WDP_LOGE("Failed to stop p2p_supp.sh");
3423                 return -1;
3424         }
3425
3426         WDP_LOGI("Successfully stopped p2p_supp.sh");
3427         return 0;
3428 }
3429 #if 0
3430 static int __ws_p2p_on(void)
3431 {
3432         DBusError error;
3433         DBusMessage *reply = NULL;
3434         DBusMessage *message = NULL;
3435         DBusConnection *connection = NULL;
3436
3437         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3438         if (connection == NULL) {
3439                 WDP_LOGE("Failed to get system bus");
3440                 return -EIO;
3441         }
3442
3443         message = dbus_message_new_method_call(NETCONFIG_SERVICE,
3444                         NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE, "LoadP2pDriver");
3445         if (message == NULL) {
3446                 WDP_LOGE("Failed DBus method call");
3447                 dbus_connection_unref(connection);
3448                 return -EIO;
3449         }
3450
3451         dbus_error_init(&error);
3452
3453         reply = dbus_connection_send_with_reply_and_block(connection, message,
3454                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
3455         if (dbus_error_is_set(&error) == TRUE) {
3456                 if (NULL != strstr(error.message, ".AlreadyExists")) {
3457                         // p2p already enabled
3458                 } else {
3459                         WDP_LOGE("dbus_connection_send_with_reply_and_block() failed. "
3460                                         "DBus error [%s: %s]", error.name, error.message);
3461
3462                         dbus_error_free(&error);
3463                 }
3464
3465                 dbus_error_free(&error);
3466         }
3467
3468         if (reply != NULL)
3469                 dbus_message_unref(reply);
3470
3471         dbus_message_unref(message);
3472         dbus_connection_unref(connection);
3473
3474         return 0;
3475 }
3476
3477 static int __ws_p2p_off(void)
3478 {
3479         DBusError error;
3480         DBusMessage *reply = NULL;
3481         DBusMessage *message = NULL;
3482         DBusConnection *connection = NULL;
3483
3484         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3485         if (connection == NULL) {
3486                 WDP_LOGE("Failed to get system bus");
3487                 return -EIO;
3488         }
3489
3490         message = dbus_message_new_method_call(NETCONFIG_SERVICE,
3491                         NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE, "RemoveP2pDriver");
3492         if (message == NULL) {
3493                 WDP_LOGE("Failed DBus method call");
3494                 dbus_connection_unref(connection);
3495                 return -EIO;
3496         }
3497
3498         dbus_error_init(&error);
3499
3500         reply = dbus_connection_send_with_reply_and_block(connection, message,
3501                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
3502         if (dbus_error_is_set(&error) == TRUE) {
3503                 if (NULL != strstr(error.message, ".AlreadyExists")) {
3504                         // p2p already disabled
3505                 } else {
3506                         WDP_LOGE("dbus_connection_send_with_reply_and_block() failed. "
3507                                         "DBus error [%s: %s]", error.name, error.message);
3508
3509                         dbus_error_free(&error);
3510                 }
3511
3512                 dbus_error_free(&error);
3513         }
3514
3515         if (reply != NULL)
3516                 dbus_message_unref(reply);
3517
3518         dbus_message_unref(message);
3519         dbus_connection_unref(connection);
3520
3521         return 0;
3522 }
3523 #endif
3524
3525 int __ws_init_p2pdevice()
3526 {
3527         __WDP_LOG_FUNC_ENTER__;
3528         GDBusConnection *g_dbus = NULL;
3529
3530         GVariant *value = NULL;
3531         GVariant *param = NULL;
3532         GVariantBuilder *builder = NULL;
3533         GVariantBuilder *type_builder = NULL;
3534         dbus_method_param_s params;
3535
3536         const char *primary_device_type = PRIMARY_DEVICE_TYPE;
3537
3538 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
3539         const char *ip_addr_go = DEFAULT_IP_GO;
3540         const char *ip_addr_mask = DEFAULT_IP_MASK;
3541         const char *ip_addr_start = DEFAULT_IP_START;
3542         const char *ip_addr_end = DEFAULT_IP_END;
3543 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
3544         int i = 0;
3545         int res = 0;
3546
3547         if (!g_pd) {
3548                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3549                 return -1;
3550         }
3551
3552         for(i = 0; i < WS_DEVTYPE_LEN; i++)
3553                 WDP_LOGD("device type[%02x]", primary_device_type[i]);
3554
3555         g_dbus = g_pd->g_dbus;
3556         if (!g_dbus) {
3557                 WDP_LOGE("DBus connection is NULL");
3558                 return -1;
3559         }
3560         memset(&params, 0x0, sizeof(dbus_method_param_s));
3561
3562         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
3563                          g_dbus);
3564
3565         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
3566         g_variant_builder_add (builder, "{sv}", "DeviceName",
3567                                         g_variant_new_string(DEFAULT_DEVICE_NAME));
3568
3569         g_variant_builder_add (builder, "{sv}", "GOIntent",
3570                                         g_variant_new_uint32(DEFAULT_GO_INTENT));
3571
3572         g_variant_builder_add (builder, "{sv}", "PersistentReconnect",
3573                                         g_variant_new_boolean(DEFAULT_PERSISTENT_RECONNECT));
3574
3575         g_variant_builder_add (builder, "{sv}", "ListenRegClass",
3576                                         g_variant_new_uint32(DEFAULT_LISTEN_REG_CLASS));
3577
3578         g_variant_builder_add (builder, "{sv}", "ListenChannel",
3579                                         g_variant_new_uint32(DEFAULT_LISTEN_CHANNEL));
3580
3581         g_variant_builder_add (builder, "{sv}", "OperRegClass",
3582                                         g_variant_new_uint32(DEFAULT_OPER_REG_CLASS));
3583
3584         g_variant_builder_add (builder, "{sv}", "OperChannel",
3585                                         g_variant_new_uint32(DEFAULT_OPER_CHANNEL));
3586
3587         g_variant_builder_add (builder, "{sv}", "SsidPostfix",
3588                                         g_variant_new_string(DEFAULT_DEVICE_NAME));
3589
3590         g_variant_builder_add (builder, "{sv}", "NoGroupIface",
3591                                         g_variant_new_boolean(DEFAULT_NO_GROUP_IFACE));
3592
3593         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3594         for(i = 0; i < WS_DEVTYPE_LEN; i++)
3595                 g_variant_builder_add(type_builder, "y", primary_device_type[i]);
3596         g_variant_builder_add (builder, "{sv}", "PrimaryDeviceType",
3597                         g_variant_new ("ay", type_builder));
3598         g_variant_builder_unref (type_builder);
3599 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
3600         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3601         for(i = 0; i < OEM_IPADDR_LEN; i++)
3602                 g_variant_builder_add(type_builder, "y", ip_addr_go[i]);
3603         g_variant_builder_add (builder, "{sv}", "IpAddrGO",
3604                         g_variant_new ("ay", type_builder));
3605         g_variant_builder_unref (type_builder);
3606
3607         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3608         for(i = 0; i < OEM_IPADDR_LEN; i++)
3609                 g_variant_builder_add(type_builder, "y", ip_addr_mask[i]);
3610         g_variant_builder_add (builder, "{sv}", "IpAddrMask",
3611                         g_variant_new ("ay", type_builder));
3612         g_variant_builder_unref (type_builder);
3613
3614         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3615         for(i = 0; i < OEM_IPADDR_LEN; i++)
3616                 g_variant_builder_add(type_builder, "y", ip_addr_start[i]);
3617         g_variant_builder_add (builder, "{sv}", "IpAddrStart",
3618                         g_variant_new ("ay", type_builder));
3619         g_variant_builder_unref (type_builder);
3620
3621         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3622         for(i = 0; i < OEM_IPADDR_LEN; i++)
3623                 g_variant_builder_add(type_builder, "y", ip_addr_end[i]);
3624         g_variant_builder_add (builder, "{sv}", "IpAddrEnd",
3625                         g_variant_new ("ay", type_builder));
3626         g_variant_builder_unref (type_builder);
3627 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
3628         value = g_variant_new ("a{sv}", builder);
3629         g_variant_builder_unref (builder);
3630
3631         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
3632
3633         params.params = param;
3634 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3635         DEBUG_PARAMS(params.params);
3636 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3637
3638         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
3639         if (res < 0)
3640                 WDP_LOGE("Failed to send command to wpa_supplicant");
3641         else
3642                 WDP_LOGD("Succeeded to initialize p2pdevice");
3643         __WDP_LOG_FUNC_EXIT__;
3644         return res;
3645 }
3646
3647 int __ws_set_config_methods()
3648 {
3649         __WDP_LOG_FUNC_ENTER__;
3650         GDBusConnection *g_dbus = NULL;
3651
3652         GVariant *value = NULL;
3653         GVariant *param = NULL;
3654
3655         dbus_method_param_s params;
3656         int res = 0;
3657
3658         if (!g_pd) {
3659                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3660                 return -1;
3661         }
3662
3663         g_dbus = g_pd->g_dbus;
3664         if (!g_dbus) {
3665                 WDP_LOGE("DBus connection is NULL");
3666                 return -1;
3667         }
3668         memset(&params, 0x0, sizeof(dbus_method_param_s));
3669
3670         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
3671                          g_dbus);
3672
3673         value = g_variant_new_string(DEFAULT_CONFIG_METHOD);
3674
3675         param = g_variant_new("(ssv)", SUPPLICANT_WPS, "ConfigMethods", value);
3676         params.params = param;
3677
3678         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
3679         if (res < 0)
3680                 WDP_LOGE("Failed to send command to wpa_supplicant");
3681         else
3682                 WDP_LOGD("Succeeded to set config method(%s)", DEFAULT_CONFIG_METHOD);
3683
3684         __WDP_LOG_FUNC_EXIT__;
3685         return res;
3686 }
3687
3688 int ws_activate(int concurrent)
3689 {
3690         __WDP_LOG_FUNC_ENTER__;
3691         int res = 0;
3692         int retry_count = 0;
3693
3694         if (!g_pd) {
3695                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3696                 return -1;
3697         }
3698
3699         res = __ws_p2p_supplicant_start();
3700         if (res < 0) {
3701                 res = __ws_p2p_supplicant_stop();
3702                 WDP_LOGI("P2P supplicant stopped with error %d", res);
3703                 __WDP_LOG_FUNC_EXIT__;
3704                 return -1;
3705         }
3706 #ifndef TIZEN_WIFI_MODULE_BUNDLE
3707         while (retry_count < WS_CONN_RETRY_COUNT) {
3708                 /* load wlan driver */
3709                 if(concurrent == 0)
3710                         res = __ws_p2p_firmware_start();
3711                 if (res < 0) {
3712                         WDP_LOGE("Failed to load driver [ret=%d]", res);
3713                         return -1;
3714                 }
3715                 WDP_LOGI("P2P firmware started with error %d", res);
3716
3717                 if (__ws_check_net_interface(COMMON_IFACE_NAME) < 0) {
3718                         usleep(150000); // wait for 150ms
3719                         concurrent = 0;
3720                         retry_count++;
3721                         WDP_LOGE("interface is not up: retry, %d", retry_count);
3722                 } else {
3723                         break;
3724                 }
3725         }
3726 #endif
3727         if (retry_count >= WS_CONN_RETRY_COUNT) {
3728                 WDP_LOGE("Driver loading is failed", res);
3729                 __WDP_LOG_FUNC_EXIT__;
3730                 return -1;
3731         }
3732         if (retry_count > 0) {
3733                 // Give driver marginal time to config net
3734                 WDP_LOGE("Driver loading is done. Wait marginal time for driver");
3735                 sleep(1); // 1s
3736         }
3737
3738         g_pd->concurrent = concurrent;
3739
3740         res = _ws_init_dbus_connection();
3741         if (res < 0) {
3742                 res = __ws_p2p_supplicant_stop();
3743                 WDP_LOGI("[/usr/sbin/p2p_supp.sh stop] returns %d", res);
3744 #ifndef TIZEN_WIFI_MODULE_BUNDLE
3745                 res = __ws_p2p_firmware_stop();
3746                 WDP_LOGI("P2P firmware stopped with error %d", res);
3747 #endif
3748                 __WDP_LOG_FUNC_EXIT__;
3749                 return -1;
3750         }
3751
3752         g_pd->activated = TRUE;
3753         __ws_init_p2pdevice();
3754         __ws_set_config_methods();
3755         seek_list = NULL;
3756
3757         __WDP_LOG_FUNC_EXIT__;
3758         return 0;
3759 }
3760
3761 int ws_deactivate(int concurrent)
3762 {
3763         __WDP_LOG_FUNC_ENTER__;
3764 #if defined(TIZEN_FEATURE_ASP)
3765         wfd_oem_asp_service_s *data = NULL;
3766 #endif /* TIZEN_FEATURE_ASP */
3767         int res = 0;
3768
3769         if (!g_pd) {
3770                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3771                 return -1;
3772         }
3773
3774         if (!g_pd->activated) {
3775                 WDP_LOGE("Wi-Fi Direct is not activated");
3776                 return -1;
3777         }
3778
3779         ws_stop_scan();
3780
3781         g_pd->concurrent = concurrent;
3782 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
3783         _ws_get_interface(P2P_IFACE_NAME, __ws_remove_interface, NULL);
3784         if(concurrent == 0)
3785                 _ws_get_interface(COMMON_IFACE_NAME, __ws_remove_interface, NULL);
3786 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
3787
3788         _ws_deinit_dbus_connection();
3789
3790         if(concurrent == 0) {
3791                 res = __ws_p2p_supplicant_stop();
3792                 WDP_LOGI("[/usr/sbin/p2p_supp.sh stop] returns %d", res);
3793 #ifndef TIZEN_WIFI_MODULE_BUNDLE
3794                 res = __ws_p2p_firmware_stop();
3795                 WDP_LOGI("P2P firmware stopped with error %d", res);
3796 #endif
3797         }
3798         g_pd->activated = FALSE;
3799
3800 #if defined(TIZEN_FEATURE_ASP)
3801         GLIST_ITER_START(seek_list, data)
3802
3803         if (data) {
3804                 temp = g_list_next(seek_list);
3805                 seek_list = g_list_remove(seek_list, data);
3806                 g_free(data->service_type);
3807                 g_free(data->service_info);
3808                 g_free(data);
3809         }
3810
3811         GLIST_ITER_END()
3812 #endif /* TIZEN_FEATURE_ASP */
3813         __WDP_LOG_FUNC_EXIT__;
3814         return 0;
3815 }
3816
3817 #if 0
3818 static gboolean _retry_start_scan(gpointer data)
3819 {
3820         __WDP_LOG_FUNC_ENTER__;
3821
3822         WDP_LOGD("Succeeded to start scan");
3823
3824         __WDP_LOG_FUNC_EXIT__;
3825         return 0;
3826 }
3827 #endif
3828
3829 #if defined(TIZEN_FEATURE_ASP)
3830 static void __ws_add_seek_params(GVariantBuilder *builder)
3831 {
3832         GVariantBuilder *outter = NULL;
3833         GVariantBuilder *inner = NULL;
3834         wfd_oem_asp_service_s *data = NULL;
3835         int len = 0;
3836         int i = 0;
3837
3838         if(seek_list == NULL || g_list_length(seek_list) == 0) {
3839                 WDP_LOGD("seek list is NULL");
3840                 return;
3841         }
3842         WDP_LOGD("seek list length [%d]", g_list_length(seek_list));
3843
3844         outter = g_variant_builder_new(G_VARIANT_TYPE("aay"));
3845
3846 GLIST_ITER_START(seek_list, data)
3847         if (data && data->service_type) {
3848                 len = strlen(data->service_type) + 1;
3849                 WDP_LOGD("data [%s] len [%d]", data->service_type, len);
3850                 inner = g_variant_builder_new(G_VARIANT_TYPE("ay"));
3851                 for(i = 0; i < len; i++)
3852                         g_variant_builder_add(inner, "y", data->service_type[i]);
3853                 g_variant_builder_add(outter, "ay", inner);
3854                 g_variant_builder_unref(inner);
3855         }
3856 GLIST_ITER_END()
3857         g_variant_builder_add (builder, "{sv}", "Seek", g_variant_new ("aay", outter));
3858         g_variant_builder_unref(outter);
3859
3860         return;
3861 }
3862 #endif /* TIZEN_FEATURE_ASP */
3863
3864
3865 int ws_start_scan(wfd_oem_scan_param_s *param)
3866 {
3867         __WDP_LOG_FUNC_ENTER__;
3868         GDBusConnection *g_dbus = NULL;
3869         GVariantBuilder *builder = NULL;
3870         GVariant *value = NULL;
3871         dbus_method_param_s params;
3872         int res = 0;
3873
3874         if (!param) {
3875                 WDP_LOGE("Invalid parameter");
3876                 return -1;
3877         }
3878
3879         if (!g_pd) {
3880                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3881                 return -1;
3882         }
3883
3884         g_dbus = g_pd->g_dbus;
3885         if (!g_dbus) {
3886                 WDP_LOGE("DBus connection is NULL");
3887                 return -1;
3888         }
3889         memset(&params, 0x0, sizeof(dbus_method_param_s));
3890
3891         if (param->scan_mode == WFD_OEM_SCAN_MODE_ACTIVE) {
3892
3893                 dbus_set_method_param(&params, "Find",  g_pd->iface_path, g_dbus);
3894
3895                 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3896
3897                         if (param->scan_time)
3898                                 g_variant_builder_add (builder, "{sv}", "Timeout",
3899                                                         g_variant_new_int32(param->scan_time));
3900                         if (param->scan_type == WFD_OEM_SCAN_TYPE_SOCIAL)
3901                                 g_variant_builder_add (builder, "{sv}", "DiscoveryType",
3902                                                         g_variant_new_string("social"));
3903 #if defined(TIZEN_FEATURE_ASP)
3904                         if(seek_list != NULL)
3905                                 __ws_add_seek_params(builder);
3906 #endif /* TIZEN_FEATURE_ASP */
3907
3908                         value = g_variant_new ("(a{sv})", builder);
3909                         g_variant_builder_unref (builder);
3910         } else {
3911
3912                 dbus_set_method_param(&params, "Listen", g_pd->iface_path, g_dbus);
3913                 value = g_variant_new ("(i)", param->scan_time);
3914         }
3915
3916         params.params = value;
3917 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3918         DEBUG_PARAMS(params.params);
3919 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3920
3921         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3922         if (res < 0)
3923                 WDP_LOGE("Failed to send command to wpa_supplicant");
3924         else
3925                 WDP_LOGD("Succeeded to start scan");
3926
3927         __WDP_LOG_FUNC_EXIT__;
3928         return res;
3929 }
3930
3931 int ws_restart_scan(int freq)
3932 {
3933         __WDP_LOG_FUNC_ENTER__;
3934         GDBusConnection *g_dbus = NULL;
3935         GVariantBuilder *builder = NULL;
3936         GVariant *value = NULL;
3937         dbus_method_param_s params;
3938         int res = 0;
3939
3940         if (!g_pd) {
3941                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3942                 return -1;
3943         }
3944
3945         g_dbus = g_pd->g_dbus;
3946         if (!g_dbus) {
3947                 WDP_LOGE("DBus connection is NULL");
3948                 return -1;
3949         }
3950         memset(&params, 0x0, sizeof(dbus_method_param_s));
3951
3952         dbus_set_method_param(&params, "Find", g_pd->iface_path, g_dbus);
3953
3954         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3955         g_variant_builder_add (builder, "{sv}", "Timeout", g_variant_new_int32(2));
3956         g_variant_builder_add (builder, "{sv}", "DiscoveryType",
3957                                 g_variant_new_string("social"));
3958         value = g_variant_new ("(a{sv})", builder);
3959         g_variant_builder_unref (builder);
3960
3961         params.params = value;
3962 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3963         DEBUG_PARAMS(params.params);
3964 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3965
3966         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3967         if (res < 0)
3968                 WDP_LOGE("Failed to send command to wpa_supplicant");
3969         else
3970                 WDP_LOGD("Succeeded to start scan");
3971
3972         __WDP_LOG_FUNC_EXIT__;
3973         return res;
3974 }
3975
3976 int ws_stop_scan()
3977 {
3978         __WDP_LOG_FUNC_ENTER__;
3979         GDBusConnection *g_dbus = NULL;
3980         dbus_method_param_s params;
3981         int res = 0;
3982
3983         if (!g_pd) {
3984                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3985                 return -1;
3986         }
3987
3988         g_dbus = g_pd->g_dbus;
3989         if (!g_dbus) {
3990                 WDP_LOGE("DBus connection is NULL");
3991                 return -1;
3992         }
3993         memset(&params, 0x0, sizeof(dbus_method_param_s));
3994
3995         dbus_set_method_param(&params, "StopFind", g_pd->iface_path, g_dbus);
3996         params.params = NULL;
3997
3998         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3999         if (res < 0)
4000                         WDP_LOGE("Failed to send command to wpa_supplicant");
4001         else
4002                 WDP_LOGD("Succeeded to stop scan");
4003
4004         __WDP_LOG_FUNC_EXIT__;
4005         return res;
4006 }
4007
4008 int ws_get_visibility(int *visibility)
4009 {
4010         __WDP_LOG_FUNC_ENTER__;
4011
4012         __WDP_LOG_FUNC_EXIT__;
4013         return 0;
4014 }
4015
4016 int ws_set_visibility(int visibility)
4017 {
4018         __WDP_LOG_FUNC_ENTER__;
4019
4020         __WDP_LOG_FUNC_EXIT__;
4021         return 0;
4022 }
4023
4024 int ws_get_scan_result(GList **peers, int *peer_count)
4025 {
4026         __WDP_LOG_FUNC_ENTER__;
4027
4028         __WDP_LOG_FUNC_EXIT__;
4029         return 0;
4030 }
4031
4032 int ws_get_peer_info(unsigned char *peer_addr, wfd_oem_device_s **peer)
4033 {
4034         __WDP_LOG_FUNC_ENTER__;
4035         GDBusConnection *g_dbus = NULL;
4036         wfd_oem_device_s *ws_dev = NULL;
4037         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4038         int res = 0;
4039
4040         if (!peer_addr || !peer) {
4041                 WDP_LOGE("Invalid parameter");
4042                 return -1;
4043         }
4044
4045         if (!g_pd) {
4046                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4047                 return -1;
4048         }
4049
4050         g_dbus = g_pd->g_dbus;
4051         if (!g_dbus) {
4052                 WDP_LOGE("DBus connection is NULL");
4053                 return -1;
4054         }
4055
4056         ws_dev = (wfd_oem_device_s *) g_try_malloc0(sizeof(wfd_oem_device_s));
4057         if (!ws_dev) {
4058                 WDP_LOGF("Failed to allocate memory device. [%s]",
4059                                 strerror(errno));
4060                 __WDP_LOG_FUNC_EXIT__;
4061                 return -1;
4062         }
4063
4064         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4065                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4066
4067         WDP_LOGD("get peer path [%s]", peer_path);
4068
4069         res = dbus_property_get_all(peer_path, g_dbus, SUPPLICANT_P2P_PEER,
4070                                 __ws_get_peer_property, ws_dev);
4071
4072         if (res < 0) {
4073                         WDP_LOGE("Failed to send command to wpa_supplicant");
4074                         g_free(ws_dev);
4075                         __WDP_LOG_FUNC_EXIT__;
4076                         return -1;
4077         } else {
4078                 WDP_LOGD("succeeded to get peer info");
4079                 *peer = ws_dev;
4080         }
4081         __WDP_LOG_FUNC_EXIT__;
4082         return 0;
4083 }
4084
4085 int ws_prov_disc_req(unsigned char *peer_addr, wfd_oem_wps_mode_e wps_mode, int join)
4086 {
4087         __WDP_LOG_FUNC_ENTER__;
4088         GDBusConnection *g_dbus = NULL;
4089         GVariant *value = NULL;
4090         dbus_method_param_s params;
4091         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4092         int res = 0;
4093
4094         if (!peer_addr) {
4095                 WDP_LOGE("Invalid parameter");
4096                 __WDP_LOG_FUNC_EXIT__;
4097                 return -1;
4098         }
4099
4100         if (!g_pd) {
4101                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4102                 __WDP_LOG_FUNC_EXIT__;
4103                 return -1;
4104         }
4105
4106         g_dbus = g_pd->g_dbus;
4107         if (!g_dbus) {
4108                 WDP_LOGE("DBus connection is NULL");
4109                 __WDP_LOG_FUNC_EXIT__;
4110                 return -1;
4111         }
4112         memset(&params, 0x0, sizeof(dbus_method_param_s));
4113
4114         dbus_set_method_param(&params, "ProvisionDiscoveryRequest", g_pd->iface_path, g_dbus);
4115
4116         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4117                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4118         WDP_LOGD("get peer path [%s]", peer_path);
4119
4120         value = g_variant_new ("(os)", peer_path, __ws_wps_to_txt(wps_mode));
4121
4122         params.params = value;
4123 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4124         DEBUG_PARAMS(params.params);
4125 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4126
4127         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4128         if (res < 0)
4129                 WDP_LOGE("Failed to send command to wpa_supplicant");
4130         else
4131                 WDP_LOGD("Succeeded to send prov disc to peer[" MACSTR "]", MAC2STR(peer_addr));
4132
4133         __WDP_LOG_FUNC_EXIT__;
4134         return res;
4135 }
4136
4137 int ws_connect(unsigned char *peer_addr, wfd_oem_conn_param_s *param)
4138 {
4139         __WDP_LOG_FUNC_ENTER__;
4140         GDBusConnection *g_dbus = NULL;
4141         GVariantBuilder *builder = NULL;
4142         GVariant *value = NULL;
4143         dbus_method_param_s params;
4144         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4145         int res = 0;
4146
4147         if (!peer_addr || !param) {
4148                 WDP_LOGE("Invalid parameter");
4149                 __WDP_LOG_FUNC_EXIT__;
4150                 return -1;
4151         }
4152
4153         if (!g_pd) {
4154                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4155                 __WDP_LOG_FUNC_EXIT__;
4156                 return -1;
4157         }
4158
4159         g_dbus = g_pd->g_dbus;
4160         if (!g_dbus) {
4161                 WDP_LOGE("DBus connection is NULL");
4162                 __WDP_LOG_FUNC_EXIT__;
4163                 return -1;
4164         }
4165         memset(&params, 0x0, sizeof(dbus_method_param_s));
4166
4167         dbus_set_method_param(&params, "Connect", g_pd->iface_path, g_dbus);
4168
4169         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4170                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4171         WDP_LOGD("get peer path [%s]", peer_path);
4172
4173         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4174         g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
4175         if (param->conn_flags & WFD_OEM_CONN_TYPE_PERSISTENT)
4176                 g_variant_builder_add (builder, "{sv}", "persistent", g_variant_new_boolean(TRUE));
4177
4178         if (param->conn_flags & WFD_OEM_CONN_TYPE_JOIN)
4179                 g_variant_builder_add (builder, "{sv}", "join", g_variant_new_boolean(TRUE));
4180
4181         if (param->conn_flags& WFD_OEM_CONN_TYPE_AUTH)
4182                 g_variant_builder_add (builder, "{sv}", "autorize_only", g_variant_new_boolean(TRUE));
4183
4184         if (param->wps_pin[0] != '\0')
4185                 g_variant_builder_add (builder, "{sv}", "pin", g_variant_new_string(param->wps_pin));
4186
4187         g_variant_builder_add (builder, "{sv}", "wps_method",
4188                                 g_variant_new_string(__ws_wps_to_txt(param->wps_mode)));
4189
4190         value = g_variant_new ("(a{sv})", builder);
4191         g_variant_builder_unref (builder);
4192
4193         params.params = value;
4194 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4195         DEBUG_PARAMS(params.params);
4196 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4197
4198         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4199         if (res < 0)
4200                 WDP_LOGE("Failed to send command to wpa_supplicant");
4201         else
4202                 WDP_LOGD("Succeeded to send connection command to peer[" MACSTR "]", MAC2STR(peer_addr));
4203
4204         __WDP_LOG_FUNC_EXIT__;
4205         return res;
4206 }
4207
4208 int ws_disconnect(unsigned char *peer_addr)
4209 {
4210         __WDP_LOG_FUNC_ENTER__;
4211         GDBusConnection *g_dbus = NULL;
4212         GVariant *value = NULL;
4213         dbus_method_param_s params;
4214         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4215         int res = 0;
4216
4217         if (!peer_addr) {
4218                 WDP_LOGE("Invalid parameter");
4219                 return -1;
4220         }
4221
4222         if (!g_pd) {
4223                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4224                 return -1;
4225         }
4226
4227         g_dbus = g_pd->g_dbus;
4228         if (!g_dbus) {
4229                 WDP_LOGE("DBus connection is NULL");
4230                 return -1;
4231         }
4232         memset(&params, 0x0, sizeof(dbus_method_param_s));
4233
4234         dbus_set_method_param(&params, "RemoveClient", g_pd->iface_path ,g_dbus);
4235
4236         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4237                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4238         WDP_LOGE("get peer path [%s]", peer_path);
4239
4240         value = g_variant_new ("(oi)", peer_path, 0);
4241
4242         params.params = value;
4243 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4244         DEBUG_PARAMS(params.params);
4245 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4246
4247         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4248         if (res < 0)
4249                 WDP_LOGE("Failed to send command to wpa_supplicant");
4250         else
4251                 WDP_LOGD("Succeeded to stop scan");
4252
4253         __WDP_LOG_FUNC_EXIT__;
4254         return res;
4255 }
4256
4257 int ws_reject_connection(unsigned char *peer_addr)
4258 {
4259         __WDP_LOG_FUNC_ENTER__;
4260         GDBusConnection *g_dbus = NULL;
4261         GVariant *value = NULL;
4262         dbus_method_param_s params;
4263         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4264         int res = 0;
4265
4266         if (!peer_addr) {
4267                 WDP_LOGE("Invalid parameter");
4268                 __WDP_LOG_FUNC_EXIT__;
4269                 return -1;
4270         }
4271
4272         if (!g_pd) {
4273                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4274                 __WDP_LOG_FUNC_EXIT__;
4275                 return -1;
4276         }
4277
4278         g_dbus = g_pd->g_dbus;
4279         if (!g_dbus) {
4280                 WDP_LOGE("DBus connection is NULL");
4281                 __WDP_LOG_FUNC_EXIT__;
4282                 return -1;
4283         }
4284         memset(&params, 0x0, sizeof(dbus_method_param_s));
4285
4286         dbus_set_method_param(&params, "RejectPeer", g_pd->iface_path ,g_dbus);
4287
4288         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4289                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4290         WDP_LOGE("get peer path [%s]", peer_path);
4291
4292         value = g_variant_new ("(o)", peer_path);
4293
4294         params.params = value;
4295 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4296         DEBUG_PARAMS(params.params);
4297 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4298
4299         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4300         if (res < 0)
4301                 WDP_LOGE("Failed to send command to wpa_supplicant");
4302         else
4303                 WDP_LOGD("Succeeded to reject peer[" MACSTR "]", MAC2STR(peer_addr));
4304
4305         _ws_flush();
4306         __WDP_LOG_FUNC_EXIT__;
4307         return res;
4308 }
4309
4310 int ws_cancel_connection(unsigned char *peer_addr)
4311 {
4312         __WDP_LOG_FUNC_ENTER__;
4313
4314         _ws_cancel();
4315
4316         __WDP_LOG_FUNC_EXIT__;
4317         return 0;
4318 }
4319
4320 int ws_get_connected_peers(GList **peers, int *peer_count)
4321 {
4322         __WDP_LOG_FUNC_ENTER__;
4323
4324         __WDP_LOG_FUNC_EXIT__;
4325         return 0;
4326 }
4327
4328 int ws_get_pin(char *pin)
4329 {
4330         __WDP_LOG_FUNC_ENTER__;
4331
4332         __WDP_LOG_FUNC_EXIT__;
4333         return 0;
4334 }
4335
4336 int ws_set_pin(char *pin)
4337 {
4338         __WDP_LOG_FUNC_ENTER__;
4339
4340         __WDP_LOG_FUNC_EXIT__;
4341         return 0;
4342 }
4343
4344 static void __ws_get_pin(GVariant *value, void *user_data)
4345 {
4346         __WDP_LOG_FUNC_ENTER__;
4347         const char *pin = NULL;
4348
4349         g_variant_get(value, "(&s)", &pin);
4350         g_strlcpy((char *)user_data, pin, OEM_PINSTR_LEN + 1);
4351
4352         __WDP_LOG_FUNC_EXIT__;
4353         return;
4354 }
4355
4356 int ws_generate_pin(char **pin)
4357 {
4358         __WDP_LOG_FUNC_ENTER__;
4359         GDBusConnection *g_dbus = NULL;
4360         dbus_method_param_s params;
4361         char n_pin[9] = {0,};
4362         int res = 0;
4363
4364         if (!g_pd) {
4365                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4366                 return -1;
4367         }
4368
4369         g_dbus = g_pd->g_dbus;
4370         if (!g_dbus) {
4371                 WDP_LOGE("DBus connection is NULL");
4372                 return -1;
4373         }
4374         memset(&params, 0x0, sizeof(dbus_method_param_s));
4375
4376         dbus_set_method_param(&params, "GeneratePin", g_pd->iface_path ,g_dbus);
4377         params.params = NULL;
4378
4379         res = dbus_method_call(&params, SUPPLICANT_WPS, __ws_get_pin, (void *)n_pin);
4380         if (res < 0)
4381                 WDP_LOGE("Failed to send command to wpa_supplicant");
4382         else
4383                 WDP_LOGD("Succeeded to generate_pin [ %s ]", n_pin);
4384
4385         *pin = strndup(n_pin, OEM_PINSTR_LEN);
4386         __WDP_LOG_FUNC_EXIT__;
4387         return 0;
4388 }
4389
4390 int ws_get_supported_wps_mode()
4391 {
4392         __WDP_LOG_FUNC_ENTER__;
4393
4394         __WDP_LOG_FUNC_EXIT__;
4395         return 0;
4396 }
4397
4398 int _ws_get_persistent_net_id(int *persistent_network_id, const unsigned char *go_dev_mac)
4399 {
4400         __WDP_LOG_FUNC_ENTER__;
4401         int persistent_group_count = 0;
4402         int counter = 0;
4403         int res = 0;
4404
4405         wfd_oem_persistent_group_s *plist = NULL;
4406
4407         res = ws_get_persistent_groups(&plist, &persistent_group_count);
4408         if (res < 0) {
4409                 WDP_LOGE("failed to get persistent groups");
4410                 __WDP_LOG_FUNC_EXIT__;
4411                 return -1;
4412         }
4413
4414         if (persistent_group_count > WS_MAX_PERSISTENT_COUNT) {
4415                 WDP_LOGE("persistent group count greater than max Persistent count");
4416                 persistent_group_count = WS_MAX_PERSISTENT_COUNT;
4417         }
4418
4419         WDP_LOGD("Persistent Group Count=%d", persistent_group_count);
4420
4421         for (counter = 0; counter < persistent_group_count ; counter++) {
4422                 if(!memcmp(go_dev_mac, plist[counter].go_mac_address, WS_MACADDR_LEN)) {
4423                         *persistent_network_id = plist[counter].network_id;
4424                         break;
4425                 } else {
4426                         WDP_LOGD("Invite: Persistent GO[" MACSTR "], GO Addr[" MACSTR "]",
4427                                         MAC2STR(plist[counter].go_mac_address), MAC2STR(go_dev_mac));
4428                 }
4429         }
4430
4431         g_free(plist);
4432         plist = NULL;
4433         WDP_LOGD("persistent network ID : [%d]", *persistent_network_id);
4434
4435         __WDP_LOG_FUNC_EXIT__;
4436         return 0;
4437 }
4438
4439 static void __store_group_iface_path(GVariant *value, void *user_data) {
4440         __WDP_LOG_FUNC_ENTER__;
4441         ws_dbus_plugin_data_s * pd_data;
4442         const char *path = NULL;
4443
4444         if (!g_pd) {
4445                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4446                 return;
4447         }
4448
4449         pd_data = (ws_dbus_plugin_data_s *) g_pd;
4450
4451         g_variant_get(value, "(&o)", &path);
4452         g_strlcpy(pd_data->group_iface_path, path, DBUS_OBJECT_PATH_MAX);
4453
4454         WDP_LOGD("group object path [%s]", pd_data->group_iface_path);
4455         /* subscribe interface p2p signal */
4456 }
4457
4458 int ws_create_group(wfd_oem_group_param_s *param)
4459 {
4460         __WDP_LOG_FUNC_ENTER__;
4461         GDBusConnection *g_dbus = NULL;
4462         GVariantBuilder *builder = NULL;
4463         GVariant *value = NULL;
4464         dbus_method_param_s params;
4465         char persistent_group_obj_path[OBJECT_PATH_MAX] = {0,};
4466         int res = 0;
4467
4468         if (!param) {
4469                 WDP_LOGE("Invalid parameter");
4470                 __WDP_LOG_FUNC_EXIT__;
4471                 return -1;
4472         }
4473
4474         if (!g_pd) {
4475                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4476                 __WDP_LOG_FUNC_EXIT__;
4477                 return -1;
4478         }
4479
4480         g_dbus = g_pd->g_dbus;
4481         if (!g_dbus) {
4482                 WDP_LOGE("DBus connection is NULL");
4483                 __WDP_LOG_FUNC_EXIT__;
4484                 return -1;
4485         }
4486         memset(&params, 0x0, sizeof(dbus_method_param_s));
4487
4488         dbus_set_method_param(&params, "GroupAdd", g_pd->iface_path, g_dbus);
4489
4490         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4491
4492         if (param->persistent > 0) {
4493                 unsigned char mac_address[WS_MACADDR_LEN] = {0x00, };
4494                 int persistent_group_id = -1;
4495
4496                 res = _ws_get_local_dev_mac(mac_address);
4497                 if (res < 0) {
4498                         WDP_LOGE("failed to get local mac address");
4499                         __WDP_LOG_FUNC_EXIT__;
4500                         return -1;
4501                 }
4502
4503                 res = _ws_get_persistent_net_id(&persistent_group_id, mac_address);
4504                 if(res < 0) {
4505                         WDP_LOGE("failed to get persistent group ID");
4506                         __WDP_LOG_FUNC_EXIT__;
4507                         return -1;
4508                 }
4509
4510                 WDP_LOGD("persistent network ID : [%d]", persistent_group_id);
4511
4512                 g_variant_builder_add(builder, "{sv}", "persistent",
4513                                 g_variant_new_boolean(TRUE));
4514                 if(persistent_group_id > -1) {
4515                         g_snprintf(persistent_group_obj_path, OBJECT_PATH_MAX,
4516                                         "%s/" SUPPLICANT_PERSISTENT_GROUPS_PART "/%d",
4517                                         g_pd->iface_path, persistent_group_id);
4518                         g_variant_builder_add(builder, "{sv}", "persistent_group_object",
4519                                         g_variant_new_object_path(persistent_group_obj_path));
4520                 }
4521
4522         } else {
4523                 g_variant_builder_add(builder, "{sv}", "persistent",
4524                                 g_variant_new_boolean(FALSE));
4525         }
4526
4527         if (param->passphrase && strlen(param->passphrase) > 0)
4528                 g_variant_builder_add(builder, "{sv}", "passphrase",
4529                                 g_variant_new_string(param->passphrase));
4530
4531         if (param->freq)
4532                 g_variant_builder_add(builder, "{sv}", "frequency",
4533                                 g_variant_new_int32(param->freq));
4534
4535         value = g_variant_new ("(a{sv})", builder);
4536         g_variant_builder_unref (builder);
4537
4538         params.params = value;
4539 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4540         DEBUG_PARAMS(params.params);
4541 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4542
4543         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE,
4544                         __store_group_iface_path, g_pd);
4545         if (res < 0)
4546                 WDP_LOGE("Failed to send command to wpa_supplicant");
4547         else
4548                 WDP_LOGD("Succeeded to add group");
4549
4550         __WDP_LOG_FUNC_EXIT__;
4551         return res;
4552 }
4553
4554 int ws_destroy_group(const char *ifname)
4555 {
4556         __WDP_LOG_FUNC_ENTER__;
4557         GDBusConnection *g_dbus = NULL;
4558         dbus_method_param_s params;
4559         int res = 0;
4560
4561         if (!ifname) {
4562                 WDP_LOGE("Invalid parameter");
4563                 return -1;
4564         }
4565
4566         if (!g_pd) {
4567                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4568                 return -1;
4569         }
4570
4571         g_dbus = g_pd->g_dbus;
4572         if (!g_dbus) {
4573                 WDP_LOGE("DBus connection is NULL");
4574                 return -1;
4575         }
4576
4577         if (g_pd->group_iface_path[0] == 0) {
4578                 WDP_LOGE("group iface path is NULL");
4579                 return -1;
4580         }
4581
4582         memset(&params, 0x0, sizeof(dbus_method_param_s));
4583
4584         dbus_set_method_param(&params, "Disconnect", g_pd->group_iface_path, g_dbus);
4585         params.params = NULL;
4586
4587         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4588         if (res < 0) {
4589                 WDP_LOGE("Failed to send command to wpa_supplicant");
4590                 __WDP_LOG_FUNC_EXIT__;
4591                 return -1;
4592         } else {
4593                 _ws_flush();
4594                 WDP_LOGD("Succeeded to remove group");
4595         }
4596
4597         __WDP_LOG_FUNC_EXIT__;
4598         return 0;
4599 }
4600
4601 int ws_invite(unsigned char *peer_addr, wfd_oem_invite_param_s *param)
4602 {
4603         __WDP_LOG_FUNC_ENTER__;
4604         GDBusConnection *g_dbus = NULL;
4605         GVariantBuilder *builder = NULL;
4606         GVariant *value = NULL;
4607         dbus_method_param_s params;
4608         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4609         int res = 0;
4610
4611         if (!peer_addr || !param) {
4612                 WDP_LOGE("Invalid parameter");
4613                 return -1;
4614         }
4615
4616         if (!g_pd) {
4617                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4618                 return -1;
4619         }
4620
4621         g_dbus = g_pd->g_dbus;
4622         if (!g_dbus) {
4623                 WDP_LOGE("DBus connection is NULL");
4624                 return -1;
4625         }
4626         memset(&params, 0x0, sizeof(dbus_method_param_s));
4627
4628         dbus_set_method_param(&params, "Invite", g_pd->group_iface_path, g_dbus);
4629
4630         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4631                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
4632         WDP_LOGE("get peer path [%s]", peer_path);
4633
4634         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4635         g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
4636         value = g_variant_new ("(a{sv})", builder);
4637         g_variant_builder_unref (builder);
4638
4639         params.params = value;
4640 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4641         DEBUG_PARAMS(params.params);
4642 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4643
4644         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4645         if (res < 0)
4646                 WDP_LOGE("Failed to send command to wpa_supplicant");
4647         else
4648                 WDP_LOGD("Succeeded to invite peer[" MACSTR "]", MAC2STR(peer_addr));
4649
4650         __WDP_LOG_FUNC_EXIT__;
4651         return 0;
4652 }
4653
4654 // Only group owner can use this command
4655 int ws_wps_start(unsigned char *peer_addr, int wps_mode, const char *pin)
4656 {
4657         __WDP_LOG_FUNC_ENTER__;
4658         GDBusConnection *g_dbus = NULL;
4659         GVariantBuilder *builder = NULL;
4660         GVariant *value = NULL;
4661         GVariant *dev_addr = NULL;
4662         dbus_method_param_s params;
4663         int i = 0;
4664         int res = 0;
4665
4666         if (!g_pd) {
4667                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4668                 return -1;
4669         }
4670
4671         g_dbus = g_pd->g_dbus;
4672         if (!g_dbus) {
4673                 WDP_LOGE("DBus connection is NULL");
4674                 return -1;
4675         }
4676
4677         memset(&params, 0x0, sizeof(dbus_method_param_s));
4678
4679         dbus_set_method_param(&params, "Start", g_pd->group_iface_path, g_dbus);
4680
4681         if (peer_addr != NULL) {
4682                 builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
4683                 for(i = 0; i < WS_MACADDR_LEN; i++)
4684                         g_variant_builder_add(builder, "y", peer_addr[i]);
4685
4686                 dev_addr = g_variant_new ("ay", builder);
4687                 g_variant_builder_unref (builder);
4688         }
4689
4690         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4691         g_variant_builder_add (builder, "{sv}", "Role", g_variant_new_string("enrollee"));
4692         if (peer_addr != NULL)
4693                 g_variant_builder_add (builder, "{sv}", "P2PDeviceAddress", dev_addr);
4694
4695         if (pin != NULL && pin[0] != '\0') {
4696                 g_variant_builder_add (builder, "{sv}", "Type", g_variant_new_string("pin"));
4697                 g_variant_builder_add (builder, "{sv}", "Pin", g_variant_new_string(pin));
4698         } else {
4699                 g_variant_builder_add (builder, "{sv}", "Type", g_variant_new_string("pbc"));
4700         }
4701
4702         value = g_variant_new ("(a{sv})", builder);
4703         g_variant_builder_unref (builder);
4704
4705         params.params = value;
4706 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4707         DEBUG_PARAMS(params.params);
4708 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4709
4710         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
4711         if (res < 0)
4712                 WDP_LOGE("Failed to send command to wpa_supplicant");
4713         else
4714                 WDP_LOGD("Succeeded to run wps");
4715
4716         __WDP_LOG_FUNC_EXIT__;
4717         return 0;
4718 }
4719
4720 int ws_enrollee_start(unsigned char *peer_addr, int wps_mode, const char *pin)
4721 {
4722         __WDP_LOG_FUNC_ENTER__;
4723
4724         WDP_LOGD("Succeeded to start WPS");
4725
4726         __WDP_LOG_FUNC_EXIT__;
4727         return 0;
4728 }
4729
4730 int ws_wps_cancel()
4731 {
4732         __WDP_LOG_FUNC_ENTER__;
4733         GDBusConnection *g_dbus = NULL;
4734         dbus_method_param_s params;
4735         int res = 0;
4736
4737         g_dbus = g_pd->g_dbus;
4738         if (!g_dbus) {
4739                 WDP_LOGE("DBus connection is NULL");
4740                 return -1;
4741         }
4742         memset(&params, 0x0, sizeof(dbus_method_param_s));
4743
4744         dbus_set_method_param(&params, "Cancel", g_pd->group_iface_path, g_dbus);
4745         params.params = NULL;
4746
4747         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
4748         if (res < 0)
4749                 WDP_LOGE("Failed to send command to wpa_supplicant");
4750         else
4751                 WDP_LOGD("Succeeded to cancel WPS");
4752
4753         __WDP_LOG_FUNC_EXIT__;
4754         return 0;
4755 }
4756
4757 int ws_get_dev_name(char *dev_name)
4758 {
4759         __WDP_LOG_FUNC_ENTER__;
4760
4761         __WDP_LOG_FUNC_EXIT__;
4762         return 0;
4763 }
4764
4765 int ws_set_dev_name(char *dev_name)
4766 {
4767         __WDP_LOG_FUNC_ENTER__;
4768         GDBusConnection *g_dbus = NULL;
4769
4770         GVariant *value = NULL;
4771         GVariant *param = NULL;
4772         GVariantBuilder *builder = NULL;
4773         dbus_method_param_s params;
4774         int res = 0;
4775
4776         if (!dev_name) {
4777                 WDP_LOGE("Invalid parameter");
4778                 return -1;
4779         }
4780
4781         if (!g_pd) {
4782                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4783                 return -1;
4784         }
4785
4786         g_dbus = g_pd->g_dbus;
4787         if (!g_dbus) {
4788                 WDP_LOGE("DBus connection is NULL");
4789                 return -1;
4790         }
4791         memset(&params, 0x0, sizeof(dbus_method_param_s));
4792
4793         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4794                          g_dbus);
4795
4796         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
4797         g_variant_builder_add (builder, "{sv}", "DeviceName",
4798                                 g_variant_new_string(dev_name));
4799         g_variant_builder_add (builder, "{sv}", "SsidPostfix",
4800                                  g_variant_new_string(dev_name));
4801         value = g_variant_new ("a{sv}", builder);
4802         g_variant_builder_unref (builder);
4803
4804         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE,
4805                                 "P2PDeviceConfig", value);
4806
4807         params.params = param;
4808 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4809         DEBUG_PARAMS(params.params);
4810 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4811
4812         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
4813         if (res < 0)
4814                 WDP_LOGE("Failed to send command to wpa_supplicant");
4815         else
4816                 WDP_LOGD("Succeeded to set device name");
4817
4818         __WDP_LOG_FUNC_EXIT__;
4819         return res;
4820 }
4821
4822 int ws_get_dev_mac(char *dev_mac)
4823 {
4824         __WDP_LOG_FUNC_ENTER__;
4825
4826         __WDP_LOG_FUNC_EXIT__;
4827         return 0;
4828 }
4829
4830 int ws_get_dev_type(int *pri_dev_type, int *sec_dev_type)
4831 {
4832         __WDP_LOG_FUNC_ENTER__;
4833
4834         __WDP_LOG_FUNC_EXIT__;
4835         return 0;
4836 }
4837
4838 int ws_set_dev_type(int pri_dev_type, int sec_dev_type)
4839 {
4840         __WDP_LOG_FUNC_ENTER__;
4841
4842         __WDP_LOG_FUNC_EXIT__;
4843         return 0;
4844 }
4845
4846 int ws_get_go_intent(int *go_intent)
4847 {
4848         __WDP_LOG_FUNC_ENTER__;
4849         GDBusConnection *g_dbus = NULL;
4850         GVariant *param = NULL;
4851         GVariant *reply = NULL;
4852         GError *error = NULL;
4853         GVariantIter *iter = NULL;
4854
4855
4856         if (!go_intent) {
4857                 WDP_LOGE("Invalid parameter");
4858                 return -1;
4859         }
4860
4861         if (!g_pd) {
4862                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4863                 return -1;
4864         }
4865
4866         g_dbus = g_pd->g_dbus;
4867         if (!g_dbus) {
4868                 WDP_LOGE("DBus connection is NULL");
4869                 return -1;
4870         }
4871
4872         param = g_variant_new("(ss)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig");
4873 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4874         DEBUG_PARAMS(param);
4875 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4876
4877         reply = g_dbus_connection_call_sync (
4878                         g_dbus,
4879                         SUPPLICANT_SERVICE, /* bus name */
4880                         g_pd->iface_path, /* object path */
4881                         DBUS_PROPERTIES_INTERFACE, /* interface name */
4882                         DBUS_PROPERTIES_METHOD_GET, /* method name */
4883                         param, /* GVariant *params */
4884                         NULL, /* reply_type */
4885                         G_DBUS_CALL_FLAGS_NONE, /* flags */
4886                         SUPPLICANT_TIMEOUT , /* timeout */
4887                         NULL, /* cancellable */
4888                         &error); /* error */
4889
4890         if(error != NULL) {
4891                 WDP_LOGE("Error! Failed to get interface State: [%s]",
4892                                 error->message);
4893                 g_error_free(error);
4894                 if(reply)
4895                         g_variant_unref(reply);
4896                 __WDP_LOG_FUNC_EXIT__;
4897                 return -1;
4898         }
4899
4900         if (reply != NULL) {
4901                 g_variant_get(reply, "(a{sv})", &iter);
4902
4903                 if (iter != NULL) {
4904                         gchar *key = NULL;
4905                         GVariant *value = NULL;
4906
4907                         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
4908 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4909                                 CHECK_KEY_VALUE(key, value);
4910 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4911                                 if(g_strcmp0(key, "GOIntent") == 0)
4912                                         g_variant_get(value, "u", go_intent);
4913                         }
4914                         g_variant_iter_free(iter);
4915                 }
4916                 g_variant_unref(reply);
4917         }
4918         __WDP_LOG_FUNC_EXIT__;
4919         return 0;
4920 }
4921
4922 int ws_set_go_intent(int go_intent)
4923 {
4924         __WDP_LOG_FUNC_ENTER__;
4925         GDBusConnection *g_dbus = NULL;
4926
4927         GVariant *value = NULL;
4928         GVariant *param = NULL;
4929         GVariantBuilder *builder = NULL;
4930         dbus_method_param_s params;
4931         int res = 0;
4932
4933         if (!g_pd) {
4934                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4935                 return -1;
4936         }
4937
4938         g_dbus = g_pd->g_dbus;
4939         if (!g_dbus) {
4940                 WDP_LOGE("DBus connection is NULL");
4941                 return -1;
4942         }
4943         memset(&params, 0x0, sizeof(dbus_method_param_s));
4944
4945         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4946                          g_dbus);
4947
4948         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
4949         g_variant_builder_add (builder, "{sv}", "GOIntent",
4950                                 g_variant_new_uint32(go_intent));
4951         value = g_variant_new ("a{sv}", builder);
4952         g_variant_builder_unref (builder);
4953
4954         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
4955
4956         params.params = param;
4957 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4958         DEBUG_PARAMS(params.params);
4959 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4960
4961         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
4962         if (res < 0)
4963                 WDP_LOGE("Failed to send command to wpa_supplicant");
4964         else
4965                 WDP_LOGE("Succeeded to set go intent");
4966         __WDP_LOG_FUNC_EXIT__;
4967         return res;
4968 }
4969
4970 int ws_set_country(char *ccode)
4971 {
4972         __WDP_LOG_FUNC_ENTER__;
4973         __WDP_LOG_FUNC_ENTER__;
4974         GDBusConnection *g_dbus = NULL;
4975
4976         GVariant *value = NULL;
4977         GVariant *param = NULL;
4978
4979         dbus_method_param_s params;
4980         int res = 0;
4981
4982         if (!g_pd) {
4983                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4984                 return -1;
4985         }
4986
4987         g_dbus = g_pd->g_dbus;
4988         if (!g_dbus) {
4989                 WDP_LOGE("DBus connection is NULL");
4990                 return -1;
4991         }
4992         memset(&params, 0x0, sizeof(dbus_method_param_s));
4993
4994         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4995                          g_dbus);
4996
4997         value = g_variant_new_string(ccode);
4998
4999         param = g_variant_new("(ssv)", SUPPLICANT_IFACE, "Country", value);
5000
5001         params.params = param;
5002 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5003         DEBUG_PARAMS(params.params);
5004 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5005
5006         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
5007         if (res < 0)
5008                 WDP_LOGE("Failed to send command to wpa_supplicant");
5009         else
5010                 WDP_LOGD("Succeeded to set country(%s)", ccode);
5011
5012         __WDP_LOG_FUNC_EXIT__;
5013         return res;
5014 }
5015
5016 void __parsing_networks (const char *key, GVariant *value, void *user_data)
5017 {
5018         __WDP_LOG_FUNC_ENTER__;
5019         if(!user_data) {
5020                 __WDP_LOG_FUNC_EXIT__;
5021                 return;
5022         }
5023
5024         ws_network_info_s *network = (ws_network_info_s *)user_data;
5025 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5026         CHECK_KEY_VALUE(key, value);
5027 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5028         if (g_strcmp0(key, "ssid") == 0) {
5029                 const char *ssid = NULL;
5030                 g_variant_get(value, "&s", &ssid);
5031                 WDP_LOGD("ssid [%s]", ssid);
5032                 g_strlcpy(network->ssid, ssid + 1, WS_SSID_LEN + 1);
5033                 network->ssid[strlen(ssid) - 2] = '\0';
5034
5035         } else if (g_strcmp0(key, "bssid") == 0) {
5036                 unsigned char *bssid = NULL;
5037                 g_variant_get(value, "&s", &bssid);
5038                 WDP_LOGD("bssid [%s]", bssid);
5039                 __ws_txt_to_mac(bssid, network->bssid);
5040
5041         } else if (g_strcmp0(key, "proto") == 0) {
5042                 const char *proto = NULL;
5043                 g_variant_get(value, "&s", &proto);
5044                 WDP_LOGD("proto [%s]", proto);
5045
5046                 if (g_strrstr(proto, WFD_OEM_STR_PROTO_WPA) != NULL)
5047                         network->proto |= WFD_OEM_PROTO_WPA;
5048                 if (g_strrstr(proto, WFD_OEM_STR_PROTO_RSN) != NULL)
5049                         network->proto |= WFD_OEM_PROTO_RSN;
5050
5051         } else if (g_strcmp0(key, "key_mgmt") == 0) {
5052                 const char *key_mgmt = NULL;
5053                 g_variant_get(value, "&s", &key_mgmt);
5054                 WDP_LOGD("key_mgmt [%s]", key_mgmt);
5055
5056                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_IEEE8021X) != NULL)
5057                         network->key_mgmt |= WFD_OEM_KEY_MGMT_IEEE8021X;
5058                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_PSK) != NULL)
5059                         network->key_mgmt |= WFD_OEM_KEY_MGMT_PSK;
5060                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_NONE) != NULL)
5061                         network->key_mgmt |= WFD_OEM_KEY_MGMT_NONE;
5062
5063         } else if (g_strcmp0(key, "pairwise") == 0) {
5064                 const char *pairwise = NULL;
5065                 g_variant_get(value, "&s", &pairwise);
5066                 WDP_LOGD("pairwise [%s]", pairwise);
5067
5068                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_NONE) != NULL)
5069                         network->pairwise |= WFD_OEM_CIPHER_NONE;
5070                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_TKIP) != NULL)
5071                         network->pairwise |= WFD_OEM_CIPHER_TKIP;
5072                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_CCMP) != NULL)
5073                         network->pairwise |= WFD_OEM_CIPHER_CCMP;
5074
5075         }  else if (g_strcmp0(key, "group") == 0) {
5076                 const char *group = NULL;
5077                 g_variant_get(value, "&s", &group);
5078                 WDP_LOGD("group [%s]", group);
5079
5080                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_NONE) != NULL)
5081                         network->group |= WFD_OEM_CIPHER_NONE;
5082                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_WEP40) != NULL)
5083                         network->group |= WFD_OEM_CIPHER_WEP40;
5084                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_WEP104) != NULL)
5085                         network->group |= WFD_OEM_CIPHER_WEP104;
5086                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_TKIP) != NULL)
5087                         network->group |= WFD_OEM_CIPHER_TKIP;
5088                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_CCMP) != NULL)
5089                         network->group |= WFD_OEM_CIPHER_CCMP;
5090
5091         } else if (g_strcmp0(key, "auth_alg") == 0) {
5092                 const char *auth_alg = NULL;
5093                 g_variant_get(value, "&s", &auth_alg);
5094                 WDP_LOGD("auth_alg [%s]", auth_alg);
5095
5096                 if (g_strrstr(auth_alg, WFD_OEM_STR_AUTH_ALG_OPEN) != NULL)
5097                         network->auth_alg |= WFD_OEM_AUTH_ALG_OPEN;
5098
5099         } else if (g_strcmp0(key, "mode") == 0) {
5100                 const char *mode = NULL;
5101                 g_variant_get(value, "&s", &mode);
5102                 WDP_LOGD("mode [%s]", mode);
5103
5104                 if (g_strrstr(mode, WFD_OEM_STR_MODE_GC) != NULL)
5105                         network->mode |= WFD_OEM_PERSISTENT_MODE_GC;
5106                 if (g_strrstr(mode, WFD_OEM_STR_MODE_GO) != NULL)
5107                         network->mode |= WFD_OEM_PERSISTENT_MODE_GO;
5108
5109         } else if (g_strcmp0(key, "p2p_client_list") == 0) {
5110                 const char *p2p_client_list = NULL;
5111                 char *ptr = NULL;
5112                 int list_len = 0;
5113                 int num = 0;
5114
5115                 g_variant_get(value, "&s", &p2p_client_list);
5116                 WDP_LOGD("p2p_client_list [%s]", p2p_client_list);
5117                 ptr = (char *)p2p_client_list;
5118                 list_len = strlen(p2p_client_list);
5119                 WDP_LOGD("list_len [%d]", list_len);
5120                 while (ptr && list_len >= (OEM_MACSTR_LEN - 1)) {
5121                         __ws_txt_to_mac((unsigned char *)ptr, (network->p2p_client_list[num]));
5122                         ptr += OEM_MACSTR_LEN;
5123                         list_len -= OEM_MACSTR_LEN;
5124                         if (ptr && ptr[0] == ' ') {
5125                                 ptr += 1;
5126                                 list_len -= 1;
5127                         }
5128                         num++;
5129                         if (num >= OEM_MAX_PEER_NUM)
5130                                 break;
5131                 }
5132                 network->p2p_client_num = num;
5133                 WDP_LOGD("p2p_client_num [%d]", network->p2p_client_num);
5134         }
5135         return;
5136 }
5137
5138 void __ws_extract_p2pdevice_details(const char *key, GVariant *value, void *user_data)
5139 {
5140         __WDP_LOG_FUNC_ENTER__;
5141 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5142         CHECK_KEY_VALUE(key, value);
5143 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5144         if (g_strcmp0(key, "PersistentGroups") == 0) {
5145                 GVariantIter *iter = NULL;
5146                 const char *path = NULL;
5147                 int num = 0;
5148
5149                 ws_network_info_s *networks = NULL;
5150                 networks = (ws_network_info_s *)user_data;
5151                 if(!networks) {
5152                         WDP_LOGE("network is NULL");
5153                         __WDP_LOG_FUNC_EXIT__;
5154                         return;
5155                 }
5156
5157                 g_variant_get(value, "ao", &iter);
5158                 while(g_variant_iter_loop(iter, "&o", &path)) {
5159                         char *loc = NULL;
5160
5161                         if(num >= WS_MAX_PERSISTENT_COUNT)
5162                                 break;
5163
5164                         WDP_LOGD("Retrive persistent path [%s]", path);
5165                         g_strlcpy(networks[num].persistent_path, path, DBUS_OBJECT_PATH_MAX);
5166
5167                         loc = strrchr(networks[num].persistent_path, '/');
5168                         networks[num].network_id = strtoul(loc+1, NULL, 10);
5169
5170                         WDP_LOGD("Retrive persistent path [%s]", networks[num].persistent_path);
5171                         dbus_property_get_all(networks[num].persistent_path, g_pd->g_dbus,
5172                                         SUPPLICANT_P2P_PERSISTENTGROUP, __parsing_networks, &networks[num]);
5173                         num++;
5174                 }
5175
5176                 networks[0].total = num;
5177                 WDP_LOGI("total number [%d]", num);
5178                 g_variant_iter_free(iter);
5179         }
5180         __WDP_LOG_FUNC_EXIT__;
5181 }
5182
5183
5184 int ws_get_persistent_groups(wfd_oem_persistent_group_s **groups, int *group_count)
5185 {
5186         __WDP_LOG_FUNC_ENTER__;
5187         GDBusConnection *g_dbus = NULL;
5188
5189         ws_network_info_s networks[WS_MAX_PERSISTENT_COUNT];
5190         wfd_oem_persistent_group_s *wfd_persistent_groups = NULL;
5191         int i, cnt = 0;
5192
5193         if (!g_pd) {
5194                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5195                 __WDP_LOG_FUNC_EXIT__;
5196                 return -1;
5197         }
5198
5199         g_dbus = g_pd->g_dbus;
5200         if (!g_dbus) {
5201                 WDP_LOGE("DBus connection is NULL");
5202                 __WDP_LOG_FUNC_EXIT__;
5203                 return -1;
5204         }
5205
5206         memset(&networks, 0x0, WS_MAX_PERSISTENT_COUNT * sizeof(ws_network_info_s));
5207         dbus_property_get_all(g_pd->iface_path, g_dbus, SUPPLICANT_P2PDEVICE,
5208                         __ws_extract_p2pdevice_details, &networks[0]);
5209
5210         cnt = networks[0].total;
5211
5212         WDP_LOGD("Persistent Group Count=%d", cnt);
5213         if (cnt > WS_MAX_PERSISTENT_COUNT) {
5214                 WDP_LOGE("Persistent group count exceeded or parsing error");
5215                 __WDP_LOG_FUNC_EXIT__;
5216                 return -1;
5217         }
5218
5219         if(cnt == 0) {
5220                 WDP_LOGE("Persistent group count zero");
5221                 *group_count = 0;
5222                 *groups = NULL;
5223                 __WDP_LOG_FUNC_EXIT__;
5224                 return 0;
5225         }
5226
5227         wfd_persistent_groups = (wfd_oem_persistent_group_s *) g_try_malloc0(cnt * sizeof(wfd_oem_persistent_group_s));
5228         if (wfd_persistent_groups == NULL) {
5229                 WDP_LOGE("Failed to allocate memory for wfd_persistent_groups ");
5230                 __WDP_LOG_FUNC_EXIT__;
5231                 return -1;
5232         }
5233
5234         for (i = 0; i < cnt; i++) {
5235                 int j = 0;
5236
5237                 WDP_LOGD("----persistent group [%d]----", i);
5238                 WDP_LOGD("network_id [%d]", networks[i].network_id);
5239                 WDP_LOGD("ssid [%s]", networks[i].ssid);
5240                 WDP_LOGD("bssid ["MACSTR"]", MAC2STR(networks[i].bssid));
5241                 WDP_LOGD("p2p_client_num [%d]", networks[i].p2p_client_num);
5242                 for(j = 0; j < networks[i].p2p_client_num; j++)
5243                         WDP_LOGD("p2p_client_list ["MACSTR"]", MAC2STR(networks[i].p2p_client_list[j]));
5244
5245                 wfd_persistent_groups[i].network_id = networks[i].network_id;
5246                 g_strlcpy(wfd_persistent_groups[i].ssid, networks[i].ssid, WS_SSID_LEN + 1);
5247                 memcpy(wfd_persistent_groups[i].go_mac_address, networks[i].bssid, WS_MACADDR_LEN);
5248                 wfd_persistent_groups[i].p2p_client_num = networks[i].p2p_client_num;
5249                 if (wfd_persistent_groups[i].p2p_client_num > 0)
5250                         memcpy(wfd_persistent_groups[i].p2p_client_list, networks[i].p2p_client_list,
5251                                         OEM_MACADDR_LEN * OEM_MAX_PEER_NUM * sizeof(char));
5252         }
5253
5254         *group_count = cnt;
5255         *groups = wfd_persistent_groups;
5256
5257         __WDP_LOG_FUNC_EXIT__;
5258         return 0;
5259 }
5260
5261 int ws_remove_persistent_group(char *ssid, unsigned char *bssid)
5262 {
5263         __WDP_LOG_FUNC_ENTER__;
5264         GDBusConnection *g_dbus = NULL;
5265
5266         dbus_method_param_s params;
5267         ws_network_info_s networks[WS_MAX_PERSISTENT_COUNT];
5268         int i, cnt = 0;
5269         int res = 0;
5270
5271         if (!g_pd) {
5272                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5273                 __WDP_LOG_FUNC_EXIT__;
5274                 return -1;
5275         }
5276
5277         g_dbus = g_pd->g_dbus;
5278         if (!g_dbus) {
5279                 WDP_LOGE("DBus connection is NULL");
5280                 __WDP_LOG_FUNC_EXIT__;
5281                 return -1;
5282         }
5283         dbus_property_get_all(g_pd->iface_path, g_dbus, SUPPLICANT_P2PDEVICE,
5284                         __ws_extract_p2pdevice_details, networks);
5285
5286         cnt = networks[0].total;
5287
5288         WDP_LOGD("Persistent Group Count=%d", cnt);
5289         if (cnt > WS_MAX_PERSISTENT_COUNT) {
5290                 WDP_LOGE("Persistent group count exceeded or parsing error");
5291                 __WDP_LOG_FUNC_EXIT__;
5292                 return -1;
5293         }
5294
5295         for (i = 0; i < cnt; i++) {
5296                 int j = 0;
5297
5298                 WDP_LOGD("----persistent group [%d]----", i);
5299                 WDP_LOGD("network_id [%d]", networks[i].network_id);
5300                 WDP_LOGD("network ssid [%s]", networks[i].ssid);
5301                 WDP_LOGD("network bssid ["MACSTR"]", MAC2STR(networks[i].bssid));
5302                 WDP_LOGD("network p2p_client_num [%d]", networks[i].p2p_client_num);
5303                 for(j = 0; j < networks[i].p2p_client_num; j++)
5304                         WDP_LOGD("network p2p_client_list ["MACSTR"]",
5305                                         MAC2STR(networks[i].p2p_client_list[j]));
5306
5307                 WDP_LOGD("ssid [%s]", ssid);
5308                 WDP_LOGD("bssid ["MACSTR"]", MAC2STR(bssid));
5309
5310                 if (!g_strcmp0(ssid, networks[i].ssid) &&
5311                                 !memcmp(bssid, networks[i].bssid, WS_MACADDR_LEN)) {
5312                         WDP_LOGD("Persistent group owner found [%d: %s]",
5313                                         networks[i].network_id, ssid);
5314
5315                         memset(&params, 0x0, sizeof(dbus_method_param_s));
5316                         dbus_set_method_param(&params, "RemovePersistentGroup",
5317                                         g_pd->iface_path, g_dbus);
5318                         params.params = g_variant_new ("(o)", networks[i].persistent_path);
5319 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5320                         DEBUG_PARAMS(params.params);
5321 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5322
5323                         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
5324                         if (res < 0) {
5325                                 WDP_LOGE("Failed to send command to wpa_supplicant");
5326                                 __WDP_LOG_FUNC_EXIT__;
5327                                 return -1;
5328                         }
5329
5330                         WDP_LOGD("Succeeded to remove persistent group");;
5331                         break;
5332                 }
5333         }
5334
5335         if (i == cnt) {
5336                 WDP_LOGE("Persistent group not found [%s]", ssid);
5337                 return -1;
5338         }
5339
5340         __WDP_LOG_FUNC_EXIT__;
5341         return 0;
5342 }
5343
5344 int ws_set_persistent_reconnect(unsigned char *bssid, int reconnect)
5345 {
5346         __WDP_LOG_FUNC_ENTER__;
5347         GDBusConnection *g_dbus = NULL;
5348
5349         GVariant *value = NULL;
5350         GVariant *param = NULL;
5351         GVariantBuilder *builder = NULL;
5352         dbus_method_param_s params;
5353         int res = 0;
5354
5355         if (!g_pd) {
5356                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5357                 return -1;
5358         }
5359
5360         g_dbus = g_pd->g_dbus;
5361         if (!g_dbus) {
5362                 WDP_LOGE("DBus connection is NULL");
5363                 return -1;
5364         }
5365         memset(&params, 0x0, sizeof(dbus_method_param_s));
5366
5367         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
5368                         g_dbus);
5369
5370         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
5371         g_variant_builder_add (builder, "{sv}", "PersistentReconnect",
5372                                 g_variant_new_boolean(reconnect));
5373         value = g_variant_new ("a{sv}", builder);
5374         g_variant_builder_unref (builder);
5375
5376         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
5377
5378         params.params = param;
5379 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5380         DEBUG_PARAMS(params.params);
5381 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5382
5383         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
5384         if (res < 0)
5385                 WDP_LOGE("Failed to send command to wpa_supplicant");
5386         else
5387                 WDP_LOGD("Succeeded to set persistent reconnect");
5388
5389         __WDP_LOG_FUNC_EXIT__;
5390         return res;
5391 }
5392
5393 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
5394 static int __ws_compress_query(char *compressed, char *query, int qtype)
5395 {
5396         char *token = NULL;
5397         char *temp = NULL;
5398         int token_num = 0;
5399         int token_len = 0;
5400         int length = 0;
5401
5402         token = strtok_r(query, ".", &temp);
5403         while (token) {
5404                 if (!strcmp(token, "local")) {
5405                         WDP_LOGD("Query conversion done");
5406                         break;
5407
5408                 } else if (!strncmp(token, "_tcp", 4)) {
5409                         memcpy(&compressed[length], WS_TCP_PTR_HEX, 2);
5410                         length+=2;
5411
5412                 } else if (!strncmp(token, "_udp", 4)) {
5413                         memcpy(&compressed[length], WS_UDP_PTR_HEX, 2);
5414                         length+=2;
5415
5416                 } else {
5417                         WDP_LOGD("Token: [%s]", token);
5418                         token_len = strlen(token);
5419                         compressed[length] = token_len;
5420                         length++;
5421
5422                         memcpy(&compressed[length], token, token_len);
5423                         length+=token_len;
5424                 }
5425                 token_num++;
5426                 token = strtok_r(NULL, ".", &temp);
5427         }
5428         if(qtype == WS_QTYPE_PTR || token_num == 2) {
5429                 memcpy(&compressed[length], WS_PTR_TYPE_HEX, 3);
5430         } else if (qtype == WS_QTYPE_TXT || token_num == 3) {
5431                 memcpy(&compressed[length], WS_TXT_TYPE_HEX, 3);
5432         }
5433         length+=3;
5434         WDP_LOGD("converted query length [%d] token num [%d]", length, token_num);
5435
5436         return length;
5437 }
5438
5439 static int __ws_compress_rdata(char *compressed, char *rdata, int qtype)
5440 {
5441         char *token = NULL;
5442         char *temp = NULL;
5443         int token_len = 0;
5444         int length = 0;
5445
5446         if (qtype == WS_QTYPE_PTR) {
5447
5448                 token = strtok_r(rdata, ".", &temp);
5449                 if (token) {
5450                         WDP_LOGD("Token: %s", token);
5451                         token_len = strlen(token);
5452                         compressed[length] = token_len;
5453                         length++;
5454
5455                         memcpy(&compressed[length], token, token_len);
5456                         length+=token_len;
5457                 }
5458
5459                 compressed[length] = 0xc0;
5460                 compressed[length+1] = 0x27;
5461                 length+=2;
5462
5463         } else if (qtype == WS_QTYPE_TXT) {
5464
5465                 token = strtok_r(rdata, ",", &temp);
5466
5467                 while (token) {
5468                         WDP_LOGD("Token: [%s]", token);
5469
5470                         token_len = strlen(token);
5471                         compressed[length] = token_len;
5472                         length++;
5473
5474                         memcpy(&compressed[length], token, token_len);
5475                         length+=token_len;
5476
5477                         token = strtok_r(NULL, ",", &temp);
5478                 }
5479         } else {
5480                 WDP_LOGD("RDATA is NULL");
5481         }
5482         return length;
5483 }
5484
5485 int _convert_bonjour_to_args(char *query, char *rdata, GVariantBuilder *builder)
5486 {
5487         GVariantBuilder *args = NULL;
5488         char compressed[256] = {0, };
5489         char *temp = NULL;
5490         int length = 0;
5491         int qtype = 0;
5492         int i = 0;
5493
5494         if (!query || !builder) {
5495                 WDP_LOGE("Invalid parameter");
5496                 return -1;
5497         }
5498         if (!rdata || !strlen(rdata)) {
5499                 WDP_LOGD("RDATA is NULL\n");
5500         } else {
5501                 temp = strstr(rdata, query);
5502
5503                 if(temp != NULL && temp - rdata > 0)
5504                         qtype = WS_QTYPE_PTR;
5505                 else
5506                         qtype = WS_QTYPE_TXT;
5507                 temp = NULL;
5508         }
5509
5510         g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("bonjour"));
5511
5512         /* compress query */
5513         length = __ws_compress_query(compressed, query, qtype);
5514
5515         args = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5516         for (i = 0; i < length; i++)
5517                 g_variant_builder_add(args, "y", compressed[i]);
5518         g_variant_builder_add(builder, "{sv}", "query", g_variant_new ("ay", args));
5519         g_variant_builder_unref (args);
5520
5521         memset(compressed, 0x0, 256);
5522         length = 0;
5523         args = NULL;
5524
5525         if(qtype != 0) {
5526                 length = __ws_compress_rdata(compressed, rdata, qtype);
5527
5528                 args = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5529                 for (i = 0; i < length; i++)
5530                         g_variant_builder_add(args, "y", compressed[i]);
5531                 g_variant_builder_add(builder, "{sv}", "response", g_variant_new ("ay", args));
5532                 g_variant_builder_unref (args);
5533         }
5534
5535         return 0;
5536 }
5537
5538 int _check_service_query_exists(wfd_oem_service_s *service)
5539 {
5540         int count = 0;
5541         wfd_oem_service_s *data = NULL;
5542
5543         for (count = 0; count < g_list_length(service_list); count ++) {
5544                 data = (wfd_oem_service_s*) g_list_nth_data(service_list, count);
5545                 if (strncmp(service->query_id, data->query_id, OEM_QUERY_ID_LEN) == 0) {
5546                         WDP_LOGD("Query already exists");
5547                         return 1;
5548                 }
5549         }
5550         return 0;
5551 }
5552
5553 static wfd_oem_service_s* _remove_service_query(char * s_type, char *mac_str, char *query_id)
5554 {
5555         if (NULL == s_type || NULL == mac_str || NULL == query_id)
5556                 return NULL;
5557
5558         int count = 0;
5559         wfd_oem_service_s *data = NULL;
5560
5561         for (count = 0; count < g_list_length(service_list); count ++) {
5562                 data = (wfd_oem_service_s*) g_list_nth_data(service_list, count);
5563                 if (data && !strncmp(data->service_type, s_type, SERVICE_TYPE_LEN) &&
5564                                 memcmp(data->dev_addr, mac_str, OEM_MACSTR_LEN - 1) == 0) {
5565                         strncpy(query_id, data->query_id, OEM_QUERY_ID_LEN);
5566                         break;
5567                 }
5568         }
5569         if (strlen(query_id) <= 0) {
5570                 WDP_LOGD("!! Query ID not found !!");
5571                 return NULL;
5572         }
5573
5574         WDP_LOGD("query id :[0x%s]",query_id);
5575
5576         return data;
5577 }
5578
5579 void __add_service_query(GVariant *value, void *mac_addr)
5580 {
5581         __WDP_LOG_FUNC_ENTER__;
5582         wfd_oem_service_s *service = NULL;
5583
5584         long long unsigned ref = 0;
5585         unsigned char *mac_address = (unsigned char *)mac_addr;
5586         char mac_str[18] = {0, };
5587
5588         int res = 0;
5589
5590         g_variant_get(value, "(t)", &ref);
5591
5592         service = (wfd_oem_service_s*) g_try_malloc0(sizeof(wfd_oem_service_s));
5593         if (!service) {
5594                 WDP_LOGE("Failed to allocate memory for service");
5595                 return;
5596         }
5597
5598         if (mac_address[0] == 0 && mac_address[1] == 0 && mac_address[2] == 0 &&
5599                         mac_address[3] == 0 && mac_address[4] == 0 && mac_address[5] == 0) {
5600                 g_snprintf(mac_str, WS_MACSTR_LEN , "%s", SERV_BROADCAST_ADDRESS);
5601         } else {
5602                 g_snprintf(mac_str, WS_MACSTR_LEN, MACSTR, MAC2STR(mac_address));
5603         }
5604
5605         g_strlcpy(service->dev_addr, mac_str, OEM_MACSTR_LEN);
5606         g_snprintf(service->query_id, OEM_QUERY_ID_LEN + 1, "0x%llx", ref);
5607
5608         res = _check_service_query_exists(service);
5609         if(res) {
5610                 free(service);
5611         } else {
5612                 service_list = g_list_append(service_list, service);
5613         }
5614
5615         __WDP_LOG_FUNC_EXIT__;
5616         return;
5617
5618 }
5619
5620 /* for now, supplicant dbus interface only provides upnp service fully */
5621 int ws_start_service_discovery(unsigned char *mac_addr, int service_type)
5622 {
5623         __WDP_LOG_FUNC_ENTER__;
5624         GDBusConnection *g_dbus = NULL;
5625         GVariantBuilder *builder = NULL;
5626         GVariant *value = NULL;
5627         dbus_method_param_s params;
5628         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
5629         int i = 0;
5630         int res = 0;
5631
5632         if (!mac_addr ) {
5633                 WDP_LOGE("Invalid parameter");
5634                 __WDP_LOG_FUNC_EXIT__;
5635                 return -1;
5636         }
5637
5638         if (!g_pd) {
5639                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5640                 __WDP_LOG_FUNC_EXIT__;
5641                 return -1;
5642         }
5643
5644         g_dbus = g_pd->g_dbus;
5645         if (!g_dbus) {
5646                 WDP_LOGE("DBus connection is NULL");
5647                 __WDP_LOG_FUNC_EXIT__;
5648                 return -1;
5649         }
5650         memset(&params, 0x0, sizeof(dbus_method_param_s));
5651
5652         dbus_set_method_param(&params, "ServiceDiscoveryRequest", g_pd->iface_path, g_dbus);
5653
5654         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
5655
5656         if(mac_addr) {
5657                 g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
5658                                 COMPACT_MACSTR, g_pd->iface_path, MAC2STR(mac_addr));
5659                 WDP_LOGD("get peer path [%s]", peer_path);
5660                 g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
5661         }
5662
5663         if (service_type == WFD_OEM_SERVICE_TYPE_ALL) {
5664
5665                 char *service_all = "\x02\x00\x00\x01";
5666                 GVariantBuilder *query = NULL;
5667
5668                 query = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5669                 for (i = 0; i < SERVICE_QUERY_LEN; i++)
5670                         g_variant_builder_add(query, "y", service_all[i]);
5671                 g_variant_builder_add (builder, "{sv}", "query", g_variant_new ("ay", query));
5672                 g_variant_builder_unref (query);
5673
5674                 } else if (service_type == WFD_OEM_SERVICE_TYPE_UPNP) {
5675
5676                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
5677                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
5678
5679         } else if (service_type == WFD_OEM_SERVICE_TYPE_BONJOUR) {
5680
5681                 char *service_bonjour = "\x02\x00\x01\x01";
5682                 GVariantBuilder *query = NULL;
5683
5684                 query = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5685                 for (i = 0; i < SERVICE_QUERY_LEN; i++)
5686                         g_variant_builder_add(query, "y", service_bonjour[i]);
5687                 g_variant_builder_add (builder, "{sv}", "query", g_variant_new ("ay", query));
5688                 g_variant_builder_unref (query);
5689         }
5690
5691         value = g_variant_new ("(a{sv})", builder);
5692         g_variant_builder_unref (builder);
5693
5694         params.params = value;
5695 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5696         DEBUG_PARAMS(params.params);
5697 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5698
5699         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, __add_service_query, mac_addr);
5700         if (res < 0)
5701                 WDP_LOGE("Failed to send command to wpa_supplicant");
5702         else
5703                 WDP_LOGD("Succeeded to start service discovery");
5704
5705         __WDP_LOG_FUNC_EXIT__;
5706         return res;
5707 }
5708
5709 int ws_cancel_service_discovery(unsigned char *mac_addr, int service_type)
5710 {
5711         __WDP_LOG_FUNC_ENTER__;
5712         GDBusConnection *g_dbus = NULL;
5713         dbus_method_param_s params;
5714         wfd_oem_service_s *data = NULL;
5715         char query_id[OEM_QUERY_ID_LEN + 1] = {0, };
5716         char s_type[OEM_SERVICE_TYPE_LEN + 1] ={0, };
5717         char mac_str[18] = {0, };
5718
5719         int res = 0;
5720
5721         if (!mac_addr ) {
5722                 WDP_LOGE("Invalid parameter");
5723                 __WDP_LOG_FUNC_EXIT__;
5724                 return -1;
5725         }
5726
5727         if (!g_pd) {
5728                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5729                 __WDP_LOG_FUNC_EXIT__;
5730                 return -1;
5731         }
5732
5733         g_dbus = g_pd->g_dbus;
5734         if (!g_dbus) {
5735                 WDP_LOGE("DBus connection is NULL");
5736                 __WDP_LOG_FUNC_EXIT__;
5737                 return -1;
5738         }
5739
5740         if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
5741                 mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
5742                 snprintf(mac_str, WS_MACSTR_LEN , "%s", SERV_BROADCAST_ADDRESS);
5743         } else {
5744                 snprintf(mac_str, WS_MACSTR_LEN, MACSTR, MAC2STR(mac_addr));
5745         }
5746
5747         switch(service_type) {
5748                 case WFD_OEM_SERVICE_TYPE_ALL:
5749                         strncpy(s_type, SERV_DISC_REQ_ALL, OEM_SERVICE_TYPE_LEN);
5750                 break;
5751                 case WFD_OEM_SERVICE_TYPE_BONJOUR:
5752                         strncpy(s_type, SERV_DISC_REQ_BONJOUR, OEM_SERVICE_TYPE_LEN);
5753                 break;
5754                 case WFD_OEM_SERVICE_TYPE_UPNP:
5755                         strncpy(s_type, SERV_DISC_REQ_UPNP, OEM_SERVICE_TYPE_LEN);
5756                 break;
5757                 default:
5758                         WDP_LOGE("Invalid Service type");
5759                         __WDP_LOG_FUNC_EXIT__;
5760                         return -1;
5761         }
5762
5763         WDP_LOGD("Cancel service discovery service_type [%d]", service_type);
5764         WDP_LOGD("Cancel service discovery s_type [%s]", s_type);
5765
5766         data = _remove_service_query(s_type, mac_str, query_id);
5767         if (NULL == data) {
5768                 __WDP_LOG_FUNC_EXIT__;
5769                 return -1;
5770         }
5771         memset(&params, 0x0, sizeof(dbus_method_param_s));
5772
5773         dbus_set_method_param(&params, "ServiceDiscoveryCancelRequest", g_pd->iface_path, g_dbus);
5774
5775         params.params = g_variant_new ("(t)", strtoul(query_id, NULL, 16));
5776
5777         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
5778         if (res < 0)
5779                 WDP_LOGE("Failed to send command to wpa_supplicant");
5780         else
5781                 WDP_LOGD("Succeeded to cancel service discovery");
5782
5783         service_list = g_list_remove(service_list, data);
5784         free(data);
5785
5786         __WDP_LOG_FUNC_EXIT__;
5787         return res;
5788 }
5789
5790 int ws_serv_add(wfd_oem_new_service_s *service)
5791 {
5792         __WDP_LOG_FUNC_ENTER__;
5793         GDBusConnection *g_dbus = NULL;
5794         GVariantBuilder *builder = NULL;
5795         GVariant *value = NULL;
5796         dbus_method_param_s params;
5797         int res = 0;
5798
5799         if (!g_pd) {
5800                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5801                 return -1;
5802         }
5803
5804         g_dbus = g_pd->g_dbus;
5805         if (!g_dbus) {
5806                 WDP_LOGE("DBus connection is NULL");
5807                 return -1;
5808         }
5809         memset(&params, 0x0, sizeof(dbus_method_param_s));
5810
5811         dbus_set_method_param(&params, "AddService", g_pd->iface_path, g_dbus);
5812
5813         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
5814
5815         if (service->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
5816
5817                 WDP_LOGD("Service type: WFD_OEM_SERVICE_TYPE_BONJOUR");
5818                 WDP_LOGD("Query: %s", service->data.bonjour.query);
5819                 WDP_LOGD("RData: %s", service->data.bonjour.rdata);
5820
5821                 res = _convert_bonjour_to_args(service->data.bonjour.query,
5822                                                             service->data.bonjour.rdata, builder);
5823                 if (res < 0) {
5824                         WDP_LOGE("Failed to convert Key string");
5825                         g_variant_builder_unref (builder);
5826                         return -1;
5827                 }
5828
5829         } else if (service->protocol == WFD_OEM_SERVICE_TYPE_UPNP) {
5830                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
5831                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
5832                 g_variant_builder_add (builder, "{sv}", "service", g_variant_new_string(service->data.upnp.service));
5833         }
5834
5835         value = g_variant_new ("(a{sv})", builder);
5836         g_variant_builder_unref (builder);
5837
5838         params.params = value;
5839 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5840         DEBUG_PARAMS(params.params);
5841 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5842
5843         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
5844         if (res < 0)
5845                 WDP_LOGE("Failed to send command to wpa_supplicant");
5846         else
5847                 WDP_LOGD("Succeeded to add service");
5848
5849         __WDP_LOG_FUNC_EXIT__;
5850         return 0;
5851 }
5852
5853 int ws_serv_del(wfd_oem_new_service_s *service)
5854 {
5855         __WDP_LOG_FUNC_ENTER__;
5856         GDBusConnection *g_dbus = NULL;
5857         GVariantBuilder *builder = NULL;
5858         GVariant *value = NULL;
5859         dbus_method_param_s params;
5860         int res = 0;
5861
5862         if (!g_pd) {
5863                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5864                 return -1;
5865         }
5866
5867         g_dbus = g_pd->g_dbus;
5868         if (!g_dbus) {
5869                 WDP_LOGE("DBus connection is NULL");
5870                 return -1;
5871         }
5872         memset(&params, 0x0, sizeof(dbus_method_param_s));
5873
5874         dbus_set_method_param(&params, "DeleteService", g_pd->iface_path, g_dbus);
5875
5876         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
5877
5878         if (service->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
5879
5880                 WDP_LOGD("Service type: WFD_OEM_SERVICE_TYPE_BONJOUR");
5881                 WDP_LOGD("Query: %s", service->data.bonjour.query);
5882
5883                 res = _convert_bonjour_to_args(service->data.bonjour.query,
5884                                                             NULL, builder);
5885                 if (res < 0) {
5886                         WDP_LOGE("Failed to convert Key string");
5887                         g_variant_builder_unref (builder);
5888                         return -1;
5889                 }
5890
5891         } else if (service->protocol == WFD_OEM_SERVICE_TYPE_UPNP) {
5892                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
5893                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
5894                 g_variant_builder_add (builder, "{sv}", "service", g_variant_new_string(service->data.upnp.service));
5895         }
5896
5897         value = g_variant_new ("(a{sv})", builder);
5898         g_variant_builder_unref (builder);
5899
5900         params.params = value;
5901 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5902         DEBUG_PARAMS(params.params);
5903 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5904
5905         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
5906         if (res < 0)
5907                 WDP_LOGE("Failed to send command to wpa_supplicant");
5908         else
5909                 WDP_LOGD("Succeeded to del service");
5910
5911         __WDP_LOG_FUNC_EXIT__;
5912         return 0;
5913 }
5914 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
5915
5916 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
5917
5918 int _ws_disable_display()
5919 {
5920         __WDP_LOG_FUNC_ENTER__;
5921         GDBusConnection *g_dbus = NULL;
5922         GVariantBuilder *builder = NULL;
5923         GVariant *value = NULL;
5924         GVariant *param = NULL;
5925         dbus_method_param_s params;
5926         int res = 0;
5927
5928         if (!g_pd) {
5929                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5930                 return -1;
5931         }
5932
5933         g_dbus = g_pd->g_dbus;
5934         if (!g_dbus) {
5935                 WDP_LOGE("DBus connection is NULL");
5936                 return -1;
5937         }
5938         memset(&params, 0x0, sizeof(dbus_method_param_s));
5939
5940         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, SUPPLICANT_PATH,
5941                          g_dbus);
5942
5943         builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5944         value = g_variant_new ("ay", builder);
5945         g_variant_builder_unref (builder);
5946
5947         param = g_variant_new("(ssv)", SUPPLICANT_INTERFACE, "WFDIEs", value);
5948
5949         params.params = param;
5950 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5951         DEBUG_PARAMS(params.params);
5952 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5953
5954         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
5955         if (res < 0)
5956                 WDP_LOGE("Failed to send command to wpa_supplicant");
5957         else
5958                 WDP_LOGD("Succeeded to disable Wi-Fi display");
5959
5960         __WDP_LOG_FUNC_EXIT__;
5961         return res;
5962 }
5963
5964 int ws_miracast_init(int enable)
5965 {
5966         __WDP_LOG_FUNC_ENTER__;
5967         wfd_oem_display_s wifi_display;
5968         int res = 0;
5969
5970         memset(&wifi_display, 0x0, sizeof(wfd_oem_display_s));
5971
5972         wifi_display.availability = enable;
5973         wifi_display.hdcp_support = 1;
5974         wifi_display.port = 0x07E6;
5975         wifi_display.max_tput = 0x0028;
5976
5977         res = ws_set_display(&wifi_display);
5978         if (res < 0) {
5979                 WDP_LOGE("Failed to set miracast parameter(device info)");
5980                 __WDP_LOG_FUNC_EXIT__;
5981                 return -1;
5982         }
5983
5984         if(!enable) {
5985                 res = _ws_disable_display();
5986                 if (res < 0)
5987                         WDP_LOGE("Failed to disable wifi display");
5988                 else
5989                         WDP_LOGD("Succeeded to disable wifi display");
5990         }
5991         __WDP_LOG_FUNC_EXIT__;
5992         return res;
5993 }
5994
5995 int ws_set_display(wfd_oem_display_s *wifi_display)
5996 {
5997         __WDP_LOG_FUNC_ENTER__;
5998         GDBusConnection *g_dbus = NULL;
5999
6000         GVariant *value = NULL;
6001         GVariant *param = NULL;
6002         GVariantBuilder *builder = NULL;
6003         dbus_method_param_s params;
6004         int i = 0;
6005         int res = 0;
6006
6007         unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
6008
6009         if (!wifi_display) {
6010                 WDP_LOGE("Invalid parameter");
6011                 return -1;
6012         }
6013         g_dbus = g_pd->g_dbus;
6014         if (!g_dbus) {
6015                 WDP_LOGE("DBus connection is NULL");
6016                 return -1;
6017         }
6018         memset(&params, 0x0, sizeof(dbus_method_param_s));
6019
6020         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, SUPPLICANT_PATH,
6021                          g_dbus);
6022
6023         ies[2] = WFD_SUBELEM_LEN_DEV_INFO;
6024         ies[3] = wifi_display->hdcp_support;
6025         ies[4] = (wifi_display->type) | (wifi_display->availability<<4);
6026         ies[5] = wifi_display->port>>8;
6027         ies[6] = wifi_display->port&0xff;
6028         ies[7] = wifi_display->max_tput>>8;
6029         ies[8] = wifi_display->max_tput&0xff;
6030
6031         builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
6032         for(i = 0; i < WFD_SUBELEM_LEN_DEV_INFO + 3; i++)
6033                 g_variant_builder_add(builder, "y", ies[i]);
6034         value = g_variant_new ("ay", builder);
6035         g_variant_builder_unref (builder);
6036
6037         param = g_variant_new("(ssv)", SUPPLICANT_INTERFACE, "WFDIEs", value);
6038
6039         params.params = param;
6040 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6041         DEBUG_PARAMS(params.params);
6042 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6043
6044         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
6045         if (res < 0)
6046                 WDP_LOGE("Failed to send command to wpa_supplicant");
6047         else
6048                 WDP_LOGD("Succeeded to set Wi-Fi Display");
6049
6050         __WDP_LOG_FUNC_EXIT__;
6051         return res;
6052 }
6053 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
6054
6055 int ws_refresh()
6056 {
6057         __WDP_LOG_FUNC_ENTER__;
6058
6059         _ws_cancel();
6060         _ws_flush();
6061
6062         __WDP_LOG_FUNC_EXIT__;
6063         return 0;
6064 }
6065
6066 int ws_save_config(void)
6067 {
6068         __WDP_LOG_FUNC_ENTER__;
6069         GDBusConnection *g_dbus = NULL;
6070         dbus_method_param_s params;
6071         int res = 0;
6072
6073         g_dbus = g_pd->g_dbus;
6074         if (!g_dbus) {
6075                 WDP_LOGE("DBus connection is NULL");
6076                 __WDP_LOG_FUNC_EXIT__;
6077                 return -1;
6078         }
6079         memset(&params, 0x0, sizeof(dbus_method_param_s));
6080
6081         dbus_set_method_param(&params, "SaveConfig", g_pd->iface_path, g_dbus);
6082         params.params = NULL;
6083
6084         res = dbus_method_call(&params, SUPPLICANT_IFACE, NULL, NULL);
6085         if (res < 0)
6086                 WDP_LOGE("Failed to save config to wpa_supplicant");
6087         else
6088                 WDP_LOGD("Succeeded to save config");
6089
6090         __WDP_LOG_FUNC_EXIT__;
6091         return res;
6092 }
6093
6094 int ws_set_operating_channel(int channel)
6095 {
6096         __WDP_LOG_FUNC_ENTER__;
6097         GDBusConnection *g_dbus = NULL;
6098         GVariant *value = NULL;
6099         GVariant *param = NULL;
6100         GVariantBuilder *builder = NULL;
6101         dbus_method_param_s params;
6102         int res = 0;
6103
6104         g_dbus = g_pd->g_dbus;
6105         if (!g_dbus) {
6106                 WDP_LOGE("DBus connection is NULL");
6107                 __WDP_LOG_FUNC_EXIT__;
6108                 return -1;
6109         }
6110
6111         memset(&params, 0x0, sizeof(dbus_method_param_s));
6112
6113         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path, g_dbus);
6114
6115         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
6116         g_variant_builder_add (builder, "{sv}", "OperChannel", g_variant_new_uint32(channel));
6117         value = g_variant_new ("a{sv}", builder);
6118         g_variant_builder_unref (builder);
6119
6120         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
6121         params.params = param;
6122
6123         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
6124         if (res < 0)
6125                 WDP_LOGE("Failed to send command to wpa_supplicant");
6126         else
6127                 WDP_LOGD("Succeeded to set Operating Channel");
6128
6129         __WDP_LOG_FUNC_EXIT__;
6130         return res;
6131 }
6132
6133 int ws_remove_all_network(void)
6134 {
6135         __WDP_LOG_FUNC_ENTER__;
6136         GDBusConnection *g_dbus = NULL;
6137         dbus_method_param_s params;
6138         int res = 0;
6139
6140         g_dbus = g_pd->g_dbus;
6141         if (!g_dbus) {
6142                 WDP_LOGE("DBus connection is NULL");
6143                 __WDP_LOG_FUNC_EXIT__;
6144                 return -1;
6145         }
6146         memset(&params, 0x0, sizeof(dbus_method_param_s));
6147
6148         dbus_set_method_param(&params, "RemoveAllNetworks", g_pd->iface_path, g_dbus);
6149         params.params = NULL;
6150
6151         res = dbus_method_call(&params, SUPPLICANT_IFACE, NULL, NULL);
6152         if (res < 0)
6153                 WDP_LOGE("Failed to send [RemoveAllNetworks] command to wpa_supplicant");
6154         else
6155                 WDP_LOGD("Succeeded to remove all networks from supplicant");
6156
6157         WDP_LOGD("Succeeded to remove all network");
6158         __WDP_LOG_FUNC_EXIT__;
6159         return res;
6160 }
6161
6162 int ws_get_wpa_status(int *wpa_status)
6163 {
6164         __WDP_LOG_FUNC_ENTER__;
6165         GDBusConnection *g_dbus = NULL;
6166         GVariant *param = NULL;
6167         GVariant *reply = NULL;
6168         GError *error = NULL;
6169
6170         if (!wpa_status) {
6171                 WDP_LOGE("Invalid parameter");
6172                 __WDP_LOG_FUNC_EXIT__;
6173                 return -1;
6174         }
6175
6176         *wpa_status = WFD_OEM_WPA_STATE_MAX;
6177
6178         g_dbus = g_pd->g_dbus;
6179         if (!g_dbus) {
6180                 WDP_LOGE("DBus connection is NULL");
6181                 __WDP_LOG_FUNC_EXIT__;
6182                 return -1;
6183         }
6184
6185         param = g_variant_new("(s)", SUPPLICANT_IFACE);
6186
6187         reply = g_dbus_connection_call_sync (
6188                         g_pd->g_dbus,
6189                         SUPPLICANT_SERVICE, /* bus name */
6190                         g_pd->iface_path, /* object path */
6191                         DBUS_PROPERTIES_INTERFACE, /* interface name */
6192                         DBUS_PROPERTIES_METHOD_GETALL, /* method name */
6193                         param, /* GVariant *params */
6194                         NULL, /* reply_type */
6195                         G_DBUS_CALL_FLAGS_NONE, /* flags */
6196                         SUPPLICANT_TIMEOUT , /* timeout */
6197                         NULL, /* cancellable */
6198                         &error); /* error */
6199
6200         if(error != NULL) {
6201                 WDP_LOGE("Error! Failed to get properties: [%s]",
6202                                                         error->message);
6203                 g_error_free(error);
6204                 if (reply)
6205                         g_variant_unref(reply);
6206                 __WDP_LOG_FUNC_EXIT__;
6207                 return -1;
6208         }
6209
6210         gchar *reply_str = NULL;
6211         if (reply)
6212                 reply_str = g_variant_print(reply, TRUE);
6213         WDP_LOGE("reply [%s]", reply_str ? reply_str : "NULL");
6214         g_free(reply_str);
6215
6216         if(reply != NULL) {
6217                 GVariantIter *iter = NULL;
6218                 g_variant_get(reply, "(a{sv})", &iter);
6219
6220                 if (iter != NULL) {
6221                         gchar *key = NULL;
6222                         GVariant *value = NULL;
6223
6224                         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
6225                                 if(g_strcmp0(key, "State") == 0) {
6226                                         const gchar *state = NULL;
6227                                         g_variant_get(value, "&s", &state);
6228                                         WDP_LOGI("state : [%s]", state);
6229
6230                                         if (g_strcmp0(state, "disconnected") == 0)
6231                                                 *wpa_status = WFD_OEM_WPA_STATE_DISCONNECTED;
6232                                         else if (g_strcmp0(state, "inactive") == 0)
6233                                                 *wpa_status = WFD_OEM_WPA_STATE_INACTIVE;
6234                                         else if (g_strcmp0(state, "scanning") == 0)
6235                                                 *wpa_status = WFD_OEM_WPA_STATE_SCANNING;
6236                                         else if (g_strcmp0(state, "authenticating") == 0)
6237                                                 *wpa_status = WFD_OEM_WPA_STATE_AUTHENTICATING;
6238                                         else if (g_strcmp0(state, "associating") == 0)
6239                                                 *wpa_status = WFD_OEM_WPA_STATE_ASSOCIATING;
6240                                         else if (g_strcmp0(state, "associated") == 0)
6241                                                 *wpa_status = WFD_OEM_WPA_STATE_ASSOCIATED;
6242                                         else if (g_strcmp0(state, "4way_handshake") == 0)
6243                                                 *wpa_status = WFD_OEM_WPA_STATE_4WAY_HANDSHAKE;
6244                                         else if (g_strcmp0(state, "group_handshake") == 0)
6245                                                 *wpa_status = WFD_OEM_WPA_STATE_GROUP_HANDSHAKE;
6246                                         else if (g_strcmp0(state, "completed") == 0)
6247                                                 *wpa_status = WFD_OEM_WPA_STATE_COMPLETED;
6248                                         else
6249                                                 *wpa_status = WFD_OEM_WPA_STATE_MAX;
6250                                 }
6251                         }
6252                         g_variant_iter_free(iter);
6253                 }
6254                 g_variant_unref(reply);
6255         } else {
6256                 WDP_LOGD("No properties");
6257         }
6258
6259         WDP_LOGI("wpa_status : [%d]", *wpa_status);
6260
6261         __WDP_LOG_FUNC_EXIT__;
6262         return 0;
6263 }
6264
6265 #if defined(TIZEN_FEATURE_ASP)
6266 int ws_advertise_service(wfd_oem_asp_service_s *service, int replace)
6267 {
6268         __WDP_LOG_FUNC_ENTER__;
6269         GDBusConnection *g_dbus = NULL;
6270         GVariantBuilder *builder = NULL;
6271         GVariant *value = NULL;
6272         dbus_method_param_s params;
6273         unsigned int config_method = 0x1108;
6274         int auto_accept = 0;
6275         gboolean rep;
6276         int res = 0;
6277
6278         g_dbus = g_pd->g_dbus;
6279         if (!g_dbus) {
6280                 WDP_LOGE("DBus connection is NULL");
6281                 return -1;
6282         }
6283
6284         if (service->config_method == 2) {
6285                 config_method = WS_CONFIG_METHOD_KEYPAD |
6286                                 WS_CONFIG_METHOD_DISPLAY;
6287         } else if (service->config_method == 3) {
6288                 config_method = WS_CONFIG_METHOD_DISPLAY;
6289         } else if (service->config_method == 4) {
6290                 config_method = WS_CONFIG_METHOD_KEYPAD;
6291         }
6292
6293         if(service->auto_accept) {
6294                 if(service->role == 0)
6295                         auto_accept = 1;
6296                 else
6297                         auto_accept = 2;
6298         } else {
6299                 auto_accept = 0;
6300         }
6301
6302         rep = (replace == 1);
6303
6304         memset(&params, 0x0, sizeof(dbus_method_param_s));
6305
6306         dbus_set_method_param(&params, "AddService", g_pd->iface_path, g_dbus);
6307
6308         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
6309
6310         g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("asp"));
6311         g_variant_builder_add (builder, "{sv}", "auto_accept", g_variant_new_int32(auto_accept));
6312         g_variant_builder_add (builder, "{sv}", "adv_id", g_variant_new_uint32(service->adv_id));
6313         g_variant_builder_add (builder, "{sv}", "svc_state", g_variant_new_uint32(service->status));
6314         g_variant_builder_add (builder, "{sv}", "config_method", g_variant_new_uint32(config_method));
6315         g_variant_builder_add (builder, "{sv}", "replace", g_variant_new_boolean(rep));
6316         if(service->service_type != NULL)
6317                 g_variant_builder_add (builder, "{sv}", "adv_str", g_variant_new_string(service->service_type));
6318         if(service->service_info != NULL)
6319                 g_variant_builder_add (builder, "{sv}", "svc_info", g_variant_new_string(service->service_info));
6320
6321         value = g_variant_new ("(a{sv})", builder);
6322         g_variant_builder_unref (builder);
6323 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6324         WDP_LOGE("params [%s]", g_variant_print(value, TRUE));
6325 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6326
6327         params.params = value;
6328
6329         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
6330         if (res < 0)
6331                 WDP_LOGE("Failed to send command to wpa_supplicant");
6332         else
6333                 WDP_LOGD("Succeeded to add service");
6334
6335         __WDP_LOG_FUNC_EXIT__;
6336         return 0;
6337 }
6338
6339 int ws_cancel_advertise_service(wfd_oem_asp_service_s *service)
6340 {
6341         __WDP_LOG_FUNC_ENTER__;
6342         GDBusConnection *g_dbus = NULL;
6343         GVariantBuilder *builder = NULL;
6344         GVariant *value = NULL;
6345         dbus_method_param_s params;
6346         int res = 0;
6347
6348         g_dbus = g_pd->g_dbus;
6349         if (!g_dbus) {
6350                 WDP_LOGE("DBus connection is NULL");
6351                 return -1;
6352         }
6353         memset(&params, 0x0, sizeof(dbus_method_param_s));
6354
6355         dbus_set_method_param(&params, "DeleteService", g_pd->iface_path, g_dbus);
6356
6357         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
6358
6359         g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("asp"));
6360         g_variant_builder_add (builder, "{sv}", "adv_id", g_variant_new_uint32(service->adv_id));
6361
6362         value = g_variant_new ("(a{sv})", builder);
6363         g_variant_builder_unref (builder);
6364 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6365         WDP_LOGD("params [%s]", g_variant_print(value, TRUE));
6366 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6367         params.params = value;
6368
6369         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
6370         if (res < 0)
6371                 WDP_LOGE("Failed to send command to wpa_supplicant");
6372         else
6373                 WDP_LOGD("Succeeded to del service");
6374
6375         __WDP_LOG_FUNC_EXIT__;
6376         return 0;
6377 }
6378
6379 static void __ws_add_seek(wfd_oem_asp_service_s *service)
6380 {
6381         __WDP_LOG_FUNC_ENTER__;
6382         wfd_oem_asp_service_s *seek = NULL;
6383         if (!service) {
6384                 WDP_LOGE("invalid parameters");
6385                 return;
6386         }
6387
6388         seek = g_try_malloc0(sizeof(wfd_oem_asp_service_s));
6389         if (!seek) {
6390                 WDP_LOGE("Failed to allocate memory for service");
6391                 return;
6392         }
6393
6394         service->search_id = (intptr_t)seek;
6395         memcpy(seek, service, sizeof(wfd_oem_asp_service_s));
6396         if(service->service_type)
6397                 seek->service_type = strdup(service->service_type);
6398         seek_list = g_list_prepend(seek_list, seek);
6399
6400         __WDP_LOG_FUNC_EXIT__;
6401         return;
6402 }
6403
6404 static wfd_oem_asp_service_s * __ws_get_seek(long long unsigned search_id)
6405 {
6406         __WDP_LOG_FUNC_ENTER__;
6407         wfd_oem_asp_service_s *seek = NULL;
6408         GList *list = NULL;
6409
6410         for(list = seek_list; list != NULL; list = list->next) {
6411                 seek = list->data;
6412                 if(seek && (seek->search_id == search_id)) {
6413                         WDP_LOGD("List found");
6414                         break;
6415                 } else {
6416                         seek = NULL;
6417                 }
6418         }
6419         __WDP_LOG_FUNC_EXIT__;
6420         return seek;
6421 }
6422
6423 static void __ws_remove_seek(wfd_oem_asp_service_s *service)
6424 {
6425         __WDP_LOG_FUNC_ENTER__;
6426         wfd_oem_asp_service_s *seek = NULL;
6427         GList *list = NULL;
6428
6429         for(list = seek_list; list != NULL; list = list->next) {
6430
6431                 seek = list->data;
6432                 if (seek && (seek->search_id == service->search_id)) {
6433                         WDP_LOGD("List remove");
6434                         seek_list = g_list_remove(seek_list, seek);
6435                         g_free(seek->service_type);
6436                         g_free(seek->service_info);
6437                         g_free(seek);
6438                 }
6439         }
6440         __WDP_LOG_FUNC_EXIT__;
6441         return;
6442 }
6443
6444 static void __get_asp_search_id(GVariant *value, void *args)
6445 {
6446         __WDP_LOG_FUNC_ENTER__;
6447         wfd_oem_asp_service_s *service = NULL;
6448         wfd_oem_asp_service_s *seek = NULL;
6449         long long unsigned search_id = 0;
6450
6451         g_variant_get(value, "(t)", &search_id);
6452
6453         service = (wfd_oem_asp_service_s *)args;
6454         if (!service) {
6455                 WDP_LOGE("invalid parameters");
6456                 __WDP_LOG_FUNC_EXIT__;
6457                 return;
6458         }
6459
6460         seek = g_try_malloc0(sizeof(wfd_oem_asp_service_s));
6461         if (!seek) {
6462                 WDP_LOGE("Failed to allocate memory for service");
6463                 __WDP_LOG_FUNC_EXIT__;
6464                 return;
6465         }
6466
6467         service->search_id = search_id;
6468         memcpy(seek, service, sizeof(wfd_oem_asp_service_s));
6469         if(service->service_type)
6470                 seek->service_type = strdup(service->service_type);
6471         if(service->service_info)
6472                 seek->service_info = strdup(service->service_info);
6473         seek_list = g_list_append(seek_list, seek);
6474
6475         __WDP_LOG_FUNC_EXIT__;
6476         return;
6477 }
6478
6479 int ws_seek_service(wfd_oem_asp_service_s *service)
6480 {
6481         __WDP_LOG_FUNC_ENTER__;
6482         GDBusConnection *g_dbus = NULL;
6483         GList *list = NULL;
6484         wfd_oem_asp_service_s *seek = NULL;
6485         int res = 0;
6486
6487         g_dbus = g_pd->g_dbus;
6488         if (!g_dbus) {
6489                 WDP_LOGE("DBus connection is NULL");
6490                 __WDP_LOG_FUNC_EXIT__;
6491                 return -1;
6492         }
6493         list = g_list_last(seek_list);
6494         if(list == NULL) {
6495                 service->tran_id = 1;
6496
6497         } else {
6498                 seek = list->data;
6499                 if(seek)
6500                         service->tran_id = seek->tran_id + 1;
6501                 else
6502                         service->tran_id = 1;
6503         }
6504
6505         if(service->service_info) {
6506                 GVariantBuilder *builder = NULL;
6507                 GVariant *value = NULL;
6508                 dbus_method_param_s params;
6509
6510                 memset(&params, 0x0, sizeof(dbus_method_param_s));
6511                 dbus_set_method_param(&params, "ServiceDiscoveryRequest",
6512                                 g_pd->iface_path, g_dbus);
6513
6514                 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
6515
6516                 g_variant_builder_add (builder, "{sv}", "service_type",
6517                                 g_variant_new_string("asp"));
6518                 g_variant_builder_add (builder, "{sv}", "transaction_id",
6519                                 g_variant_new_byte(service->tran_id));
6520                 if(service->service_type != NULL)
6521                         g_variant_builder_add (builder, "{sv}", "svc_str",
6522                                         g_variant_new_string(service->service_type));
6523
6524                 if(service->service_info != NULL)
6525                         g_variant_builder_add (builder, "{sv}", "svc_info",
6526                                         g_variant_new_string(service->service_info));
6527
6528                 value = g_variant_new ("(a{sv})", builder);
6529                 g_variant_builder_unref (builder);
6530
6531 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6532                 WDP_LOGD("params [%s]", g_variant_print(value, TRUE));
6533 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6534
6535                 params.params = value;
6536                 res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE,
6537                                 __get_asp_search_id, service);
6538
6539         } else {
6540                 __ws_add_seek(service);
6541         }
6542
6543         if (res < 0)
6544                 WDP_LOGE("Failed to send command to wpa_supplicant");
6545         else
6546                 WDP_LOGD("Succeeded to seek service");
6547
6548         __WDP_LOG_FUNC_EXIT__;
6549         return res;
6550 }
6551
6552 int ws_cancel_seek_service(wfd_oem_asp_service_s *service)
6553 {
6554         __WDP_LOG_FUNC_ENTER__;
6555         GDBusConnection *g_dbus = NULL;
6556         wfd_oem_asp_service_s *seek = NULL;
6557         dbus_method_param_s params;
6558         int res = 0;
6559
6560         g_dbus = g_pd->g_dbus;
6561         if (!g_dbus) {
6562                 WDP_LOGE("DBus connection is NULL");
6563                 __WDP_LOG_FUNC_EXIT__;
6564                 return -1;
6565         }
6566
6567         seek = __ws_get_seek(service->search_id);
6568         if (!seek) {
6569                 WDP_LOGE("seek data is NULL");
6570                 __WDP_LOG_FUNC_EXIT__;
6571                 return -1;
6572         }
6573
6574         if(seek->service_info) {
6575
6576                 memset(&params, 0x0, sizeof(dbus_method_param_s));
6577                 dbus_set_method_param(&params, "ServiceDiscoveryCancelRequest",
6578                                 g_pd->iface_path, g_dbus);
6579
6580                 params.params = g_variant_new ("(t)", service->search_id);
6581
6582 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6583                 WDP_LOGD("params [%s]", g_variant_print(params.params, TRUE));
6584 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6585
6586                 res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
6587                 if (res < 0) {
6588                         WDP_LOGE("Failed to send command to wpa_supplicant");
6589                 } else {
6590                         WDP_LOGD("Succeeded to cancel seek service");
6591                 }
6592         }
6593         if(res == 0)
6594                 __ws_remove_seek(seek);
6595
6596         __WDP_LOG_FUNC_EXIT__;
6597         return res;
6598 }
6599
6600 int ws_asp_prov_disc_req(wfd_oem_asp_prov_s *asp_params)
6601 {
6602         __WDP_LOG_FUNC_ENTER__;
6603         GDBusConnection *g_dbus = NULL;
6604         GVariantBuilder *builder = NULL;
6605         GVariantBuilder *mac_builder = NULL;
6606         GVariant *value = NULL;
6607         dbus_method_param_s params;
6608         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
6609         int config_method = 0x1000;
6610         int res = 0;
6611         int i = 0;
6612
6613         if (!asp_params) {
6614                 WDP_LOGE("Invalid parameter");
6615                 __WDP_LOG_FUNC_EXIT__;
6616                 return -1;
6617         }
6618         g_dbus = g_pd->g_dbus;
6619         if (!g_dbus) {
6620                 WDP_LOGE("DBus connection is NULL");
6621                 __WDP_LOG_FUNC_EXIT__;
6622                 return -1;
6623         }
6624
6625         if(asp_params->network_config == WFD_OEM_ASP_WPS_TYPE_PIN_DISPLAY)
6626                 config_method = 0x8;
6627         else if(asp_params->network_config == WFD_OEM_ASP_WPS_TYPE_PIN_KEYPAD)
6628                 config_method = 0x100;
6629
6630         memset(&params, 0x0, sizeof(dbus_method_param_s));
6631
6632         dbus_set_method_param(&params, "ASPProvisionDiscoveryRequest", g_pd->iface_path, g_dbus);
6633
6634         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
6635                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(asp_params->service_mac));
6636         WDP_LOGD("get peer path [%s]", peer_path);
6637
6638         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
6639         g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
6640
6641         g_variant_builder_add (builder, "{sv}", "adv_id", g_variant_new_uint32(asp_params->adv_id));
6642         g_variant_builder_add (builder, "{sv}", "session_id", g_variant_new_uint32(asp_params->session_id));
6643         g_variant_builder_add (builder, "{sv}", "role", g_variant_new_byte(asp_params->network_role));
6644         g_variant_builder_add (builder, "{sv}", "method", g_variant_new_int32(config_method));
6645         if (asp_params->status > 0)
6646                 g_variant_builder_add (builder, "{sv}", "status", g_variant_new_int32(asp_params->status));
6647         if (asp_params->session_information)
6648                 g_variant_builder_add (builder, "{sv}", "info", g_variant_new_string(asp_params->session_information));
6649
6650         mac_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
6651         for (i = 0; i < OEM_MACADDR_LEN; i++)
6652                 g_variant_builder_add(mac_builder, "y", asp_params->service_mac[i]);
6653         g_variant_builder_add (builder, "{sv}", "adv_mac",
6654                         g_variant_new ("ay", mac_builder));
6655         g_variant_builder_unref (mac_builder);
6656
6657         mac_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
6658         for (i = 0; i < OEM_MACADDR_LEN; i++)
6659                 g_variant_builder_add(mac_builder, "y", asp_params->session_mac[i]);
6660         g_variant_builder_add (builder, "{sv}", "session_mac",
6661                         g_variant_new ("ay", mac_builder));
6662         g_variant_builder_unref (mac_builder);
6663
6664         value = g_variant_new ("(a{sv})", builder);
6665         g_variant_builder_unref (builder);
6666 #if defined (TIZEN_DEBUG_DBUS_VALUE)
6667         WDP_LOGD("params [%s]", g_variant_print(value, TRUE));
6668 #endif /* TIZEN_DEBUG_DBUS_VALUE */
6669
6670         params.params = value;
6671
6672         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
6673         if (res < 0)
6674                 WDP_LOGE("Failed to send command to wpa_supplicant");
6675         else
6676                 WDP_LOGD("Succeeded to send connection command to peer[" MACSTR "]", MAC2STR(asp_params->service_mac));
6677
6678         __WDP_LOG_FUNC_EXIT__;
6679         return res;
6680 }
6681 #endif /* TIZEN_FEATURE_ASP */