In __ws_segment_to_service, max len can be 255
[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 #define NETCONFIG_SERVICE "net.netconfig"
54 #define NETCONFIG_WIFI_INTERFACE "net.netconfig.wifi"
55 #define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
56
57 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
58
59 #if defined TIZEN_MOBILE
60 #define DEFAULT_MAC_FILE_PATH tzplatform_mkpath(TZ_SYS_ETC, ".mac.info")
61 #endif
62
63 #if defined TIZEN_WIFI_MODULE_BUNDLE
64 #define DEFAULT_MAC_FILE_PATH "/sys/class/net/wlan0/address"
65 #endif
66
67 #ifndef DEFAULT_MAC_FILE_PATH
68 #define DEFAULT_MAC_FILE_PATH "/sys/class/net/p2p0/address"
69 #endif
70
71 static wfd_oem_ops_s supplicant_ops = {
72         .init = ws_init,
73         .deinit = ws_deinit,
74         .activate = ws_activate,
75         .deactivate = ws_deactivate,
76
77         .start_scan = ws_start_scan,
78         .stop_scan = ws_stop_scan,
79         .get_visibility = ws_get_visibility,
80         .set_visibility = ws_set_visibility,
81         .get_scan_result = ws_get_scan_result,
82         .get_peer_info = ws_get_peer_info,
83
84         .prov_disc_req = ws_prov_disc_req,
85
86         .connect = ws_connect,
87         .disconnect = ws_disconnect,
88         .reject_connection = ws_reject_connection,
89         .cancel_connection = ws_cancel_connection,
90
91         .get_connected_peers = ws_get_connected_peers,
92         .get_pin = ws_get_pin,
93         .set_pin = ws_set_pin,
94         .generate_pin = ws_generate_pin,
95         .get_supported_wps_mode = ws_get_supported_wps_mode,
96
97         .create_group = ws_create_group,
98         .destroy_group = ws_destroy_group,
99         .invite = ws_invite,
100         .wps_start = ws_wps_start,
101         .enrollee_start = ws_enrollee_start,
102         .wps_cancel = ws_wps_cancel,
103
104         .get_dev_name = ws_get_dev_name,
105         .set_dev_name = ws_set_dev_name,
106         .get_dev_mac = ws_get_dev_mac,
107         .get_dev_type = ws_get_dev_type,
108         .set_dev_type = ws_set_dev_type,
109         .get_go_intent = ws_get_go_intent,
110         .set_go_intent = ws_set_go_intent,
111         .set_country = ws_set_country,
112         .get_persistent_groups = ws_get_persistent_groups,
113         .remove_persistent_group = ws_remove_persistent_group,
114         .set_persistent_reconnect = ws_set_persistent_reconnect,
115
116 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
117         .start_service_discovery = ws_start_service_discovery,
118         .cancel_service_discovery = ws_cancel_service_discovery,
119
120         .serv_add = ws_serv_add,
121         .serv_del = ws_serv_del,
122 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
123
124 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
125         .miracast_init = ws_miracast_init,
126         .set_display = ws_set_display,
127 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
128
129         .refresh = ws_refresh,
130
131         };
132
133 static ws_dbus_plugin_data_s *g_pd;
134
135 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
136 static GList *service_list;
137 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
138
139 static void _supplicant_signal_cb(GDBusConnection *connection,
140                 const gchar *sender, const gchar *object_path, const gchar *interface,
141                 const gchar *signal, GVariant *parameters, gpointer user_data);
142
143 static void _p2pdevice_signal_cb(GDBusConnection *connection,
144                 const gchar *sender, const gchar *object_path, const gchar *interface,
145                 const gchar *signal, GVariant *parameters, gpointer user_data);
146
147 static void _group_signal_cb(GDBusConnection *connection,
148                 const gchar *sender, const gchar *object_path, const gchar *interface,
149                 const gchar *signal, GVariant *parameters, gpointer user_data);
150
151 static int __ws_txt_to_mac(unsigned char *txt, unsigned char *mac)
152 {
153         int i = 0;
154
155         if (!txt || !mac) {
156                 WDP_LOGE("Invalid parameter");
157                 return -1;
158         }
159
160         for (;;) {
161                 mac[i++] = (char) strtoul((char *)txt, (char **)&txt, 16);
162                 if (!*txt++ || i == 6)
163                         break;
164         }
165
166         if (i != WS_MACADDR_LEN)
167                 return -1;
168
169         WDP_LOGD("Converted MAC address [" MACSECSTR "]", MAC2SECSTR(mac));
170         return 0;
171 }
172
173 static int __ws_mac_compact_to_normal(char *compact, unsigned char *mac)
174 {
175         g_snprintf((char *)mac, OEM_MACSTR_LEN, "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
176                         compact[0], compact[1], compact[2], compact[3],
177                         compact[4], compact[5], compact[6], compact[7],
178                         compact[8], compact[9], compact[10], compact[11]);
179         return 0;
180 }
181
182 static char *__ws_wps_to_txt(int wps_mode)
183 {
184         switch (wps_mode) {
185         case WFD_OEM_WPS_MODE_PBC:
186                 return WS_DBUS_STR_PBC;
187                 break;
188         case WFD_OEM_WPS_MODE_DISPLAY:
189                 return WS_DBUS_STR_DISPLAY;
190                 break;
191         case WFD_OEM_WPS_MODE_KEYPAD:
192                 return WS_DBUS_STR_KEYPAD;
193                 break;
194         default:
195                 return "";
196                 break;
197         }
198 }
199 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
200 static int __ws_byte_to_hex(char *buf, int buf_size, unsigned char *data, int data_len)
201 {
202         int i;
203         char *pos = buf;
204         char *end = buf + buf_size;
205         int ret;
206         if (buf_size == 0)
207                 return 0;
208         for (i = 0; i < data_len; i++) {
209                 ret = snprintf(pos, end - pos, "%02x", data[i]);
210                 if (ret < 0 || ret >= end - pos) {
211                         end[-1] = '\0';
212                         return pos - buf;
213                 }
214                 pos += ret;
215         }
216         end[-1] = '\0';
217         return pos - buf;
218 }
219
220 static int __ws_hex_to_num(char *src, int len)
221 {
222         char *temp = NULL;
223         int num = 0;
224
225         if (!src || len < 0) {
226                 WDP_LOGE("Invalid parameter");
227                 return -1;
228         }
229
230         temp = (char*) g_try_malloc0(len+1);
231         if (!temp) {
232                 WDP_LOGE("Failed to allocate memory");
233                 return -1;
234         }
235
236         memcpy(temp, src, len);
237         num = strtoul(temp, NULL, 16);
238         g_free(temp);
239
240         return num;
241 }
242
243 static int __ws_segment_to_service(char *segment, wfd_oem_new_service_s **service)
244 {
245         wfd_oem_new_service_s *serv_tmp = NULL;
246         char *ptr = NULL;
247         char *temp = NULL;
248         int len = 0;
249         int i = 0;
250
251         if (!segment || !service) {
252                 WDP_LOGE("Invalid parameter");
253                 return -1;
254         }
255
256         ptr = segment;
257         WDP_LOGD("Segment: %s", segment);
258
259         serv_tmp = (wfd_oem_new_service_s*) g_try_malloc0(sizeof(wfd_oem_new_service_s));
260         if (!serv_tmp) {
261                 WDP_LOGE("Failed to allocate memory for service");
262                 return -1;
263         }
264
265         serv_tmp->protocol = __ws_hex_to_num(ptr, 2);
266         serv_tmp->trans_id = __ws_hex_to_num(ptr+2, 2);
267         serv_tmp->status = __ws_hex_to_num(ptr+4, 2);
268         ptr += 6;
269         WDP_LOGD("Protocol[%d], Transaction ID[%d], Status[%d]", serv_tmp->protocol, serv_tmp->trans_id, serv_tmp->status);
270
271         if (serv_tmp->status != 0) {
272                 WDP_LOGE("Service status is not success");
273                 free(serv_tmp);
274                 return -1;
275         }
276
277         if (serv_tmp->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
278                 WDP_LOGD("===== Bonjour service =====");
279                 char compr[5] = {0, };
280                 char query[256] = {0, };
281                 char rdata[256] = {0, };
282                 int dns_type = 0;
283
284                 while (*ptr != 0 && strncmp(ptr, "c0", 2)) {
285                         len = __ws_hex_to_num(ptr, 2);
286                         ptr += 2;
287                         if (len && len <= 0xff) {
288                                 temp = (char*) calloc(1, len+2);
289                                 if (temp) {
290                                         temp[0] = '.';
291                                         for (i = 0; i < len; i++) {
292                                                 temp[i+1] = (char) __ws_hex_to_num(ptr, 2);
293                                                 ptr += 2;
294                                         }
295                                         strncat(query, temp, len+1);
296                                         g_free(temp);
297                                         temp = NULL;
298                                 }
299                         }
300                 }
301
302                 if (!strncmp(ptr, "c0", 2)) {
303                         memcpy(compr, ptr, 4);
304                         ptr += 2;
305
306                         if (!strncmp(ptr, "27", 2)) {
307                                 WDP_LOGD("Segment ended");
308                                 ptr += 2;
309                         } else {
310                                 ptr += 2;
311                                 dns_type = __ws_hex_to_num(ptr, 4);
312                                 ptr += 6;
313                                 if (dns_type == 12) {
314                                         if (!strncmp(compr, "c011", 4))
315                                                 strncat(query, ".local.", 7);
316                                         else if (!strncmp(compr, "c00c", 4))
317                                                 strncat(query, "._tcp.local.", 12);
318                                         else if (!strncmp(compr, "c01c", 4))
319                                                 strncat(query, "._udp.local.", 12);
320                                 }
321                         }
322                 }
323                 serv_tmp->data.bonjour.query = strdup(query + 1);
324                 while (*ptr != 0 && strncmp(ptr, "c0", 2)) {
325                         len = __ws_hex_to_num(ptr, 2);
326                         ptr += 2;
327                         if (len && len <= 0xff) {
328                                 temp = (char*) g_try_malloc0(len+2);
329                                 if (temp) {
330                                         temp[0] = '.';
331                                         for (i = 0; i < len; i++) {
332                                                 temp[i+1] = (char) __ws_hex_to_num(ptr, 2);
333                                                 ptr += 2;
334                                         }
335                                         strncat(rdata, temp, len+1);
336                                         g_free(temp);
337                                         temp = NULL;
338                                 }
339                         }
340                 }
341                 serv_tmp->data.bonjour.rdata = strdup(rdata + 1);
342
343                 WDP_LOGD("Query: %s", serv_tmp->data.bonjour.query);
344                 WDP_LOGD("RData: %s", serv_tmp->data.bonjour.rdata);
345         } else {
346                 WDP_LOGE("Not supported yet. Only bonjour service supproted [%d]",
347                                         serv_tmp->protocol);
348                 g_free(serv_tmp);
349                 return -1;
350         }
351
352         *service = serv_tmp;
353
354         return 0;
355 }
356 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
357
358 static void __ws_path_to_addr(char *peer_path,
359                 unsigned char *dev_addr, GVariant *parameter)
360 {
361         __WDP_LOG_FUNC_ENTER__;
362
363         static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
364         const char *path = NULL;
365         char *loc = NULL;
366
367         g_variant_get(parameter, "(&o)", &path);
368         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
369         WDP_LOGD("Retrive Added path [%s]", peer_path);
370
371         loc = strrchr(peer_path,'/');
372         if(loc != NULL)
373                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
374
375         __ws_txt_to_mac(peer_dev, dev_addr);
376         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(dev_addr));
377
378         __WDP_LOG_FUNC_EXIT__;
379         return;
380 }
381
382 static int __ws_unpack_ay(unsigned char *dst, GVariant *src, int size)
383 {
384         GVariantIter *iter = NULL;
385         int length = 0;
386         int res = 1;
387
388         if (!dst || !src || size == 0) {
389                 WDP_LOGE("Invalid parameter");
390                 return -1;
391         }
392         g_variant_get(src, "ay", &iter);
393         if (iter == NULL) {
394                 WDP_LOGE("failed to get iterator");
395                 return -1;
396         }
397
398         while (g_variant_iter_loop (iter, "y", &dst[length])) {
399                 length++;
400                 if(length >= size)
401                         break;
402         }
403         g_variant_iter_free(iter);
404
405         if (length < size) {
406                 WDP_LOGE("array is shorter than size");
407                 res = -1;
408         }
409
410         return res;
411 }
412
413 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
414 static int __parsing_wfd_info(unsigned char *wfd_dev_info,
415                 wfd_oem_display_s *display )
416 {
417         __WDP_LOG_FUNC_ENTER__;
418
419         int wfd_info = 0;
420         if (!wfd_dev_info || !display) {
421                 WDP_LOGE("Invalid parameter");
422                 __WDP_LOG_FUNC_EXIT__;
423                 return -1;
424         }
425
426         wfd_info = (wfd_dev_info[3]<<8 | wfd_dev_info[4]);
427
428         if (wfd_info & WS_WFD_INFO_PRIMARY_SINK)
429                 display->type |= WS_WFD_INFO_PRIMARY_SINK;
430         if (wfd_info & WS_WFD_INFO_SECONDARY_SINK)
431                 display->type |= WS_WFD_INFO_SECONDARY_SINK;
432
433         display->availability = (wfd_info & WS_WFD_INFO_AVAILABILITY) >> 4;
434         display->hdcp_support = (wfd_info & WS_WFD_INFO_HDCP_SUPPORT) >> 8;
435
436         display->port = (wfd_dev_info[5]<<8 | wfd_dev_info[6]);
437         display->max_tput = (wfd_dev_info[7]<<8 | wfd_dev_info[8]);
438
439         WDP_LOGD("type [%d],availability [%d],hdcp_support [%d],ctrl_port [%d] "
440                         "max_tput[%d]", display->type, display->availability,
441                         display->hdcp_support, display->port,display->max_tput);
442
443         __WDP_LOG_FUNC_EXIT__;
444         return 0;
445 }
446 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
447
448 static int _ws_get_local_dev_mac(unsigned char *dev_mac)
449 {
450         __WDP_LOG_FUNC_ENTER__;
451         FILE *fd = NULL;
452         const char *file_path = DEFAULT_MAC_FILE_PATH;
453         char local_mac[OEM_MACSTR_LEN] = {0, };
454         char *ptr = NULL;
455         int res = 0;
456
457         errno = 0;
458         fd = fopen(file_path, "r");
459         if (!fd) {
460                 WDP_LOGE("Failed to open MAC info file [%s] (%s)",file_path, strerror(errno));
461                 __WDP_LOG_FUNC_EXIT__;
462                 return -1;
463         }
464
465         errno = 0;
466         ptr = fgets((char *)local_mac, WS_MACSTR_LEN, fd);
467         if (!ptr) {
468                 WDP_LOGE("Failed to read file or no data read(%s)", strerror(errno));
469                 fclose(fd);
470                 __WDP_LOG_FUNC_EXIT__;
471                 return -1;
472         }
473         WDP_SECLOGD("Local MAC address [%s]", ptr);
474         WDP_SECLOGD("Local MAC address [%s]", local_mac);
475
476         res = __ws_txt_to_mac((unsigned char *)local_mac, dev_mac);
477         if (res < 0) {
478                 WDP_LOGE("Failed to convert text to MAC address");
479                 fclose(fd);
480                 __WDP_LOG_FUNC_EXIT__;
481                 return -1;
482         }
483
484         WDP_LOGD("Local Device MAC address [" MACSECSTR "]", MAC2SECSTR(dev_mac));
485
486         fclose(fd);
487         __WDP_LOG_FUNC_EXIT__;
488         return 0;
489 }
490
491 static void _supplicant_signal_cb(GDBusConnection *connection,
492                 const gchar *sender, const gchar *object_path, const gchar *interface,
493                 const gchar *signal, GVariant *parameters, gpointer user_data)
494 {
495 #if defined (TIZEN_DEBUG_DBUS_VALUE)
496         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
497 #endif /* TIZEN_DEBUG_DBUS_VALUE */
498
499         if (!g_pd) {
500                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
501                 return;
502         }
503
504         if (!g_strcmp0(signal,"InterfaceAdded")) {
505                 WDP_LOGD("InterfaceAdded");
506
507         } else if (!g_strcmp0(signal,"InterfaceRemoved")) {
508                 WDP_LOGD("InterfaceRemoved");
509                 static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
510                 const char *path = NULL;
511
512                 g_variant_get(parameters, "(&o)", &path);
513                 g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
514
515                 WDP_LOGD("Retrive removed path [%s]", interface_path);
516
517                 if (!g_strcmp0(g_pd->group_iface_path, interface_path)) {
518
519                         WDP_LOGD("p2p group interface removed");
520                         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
521                 }
522 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE) && defined(TIZEN_MOBILE)
523                 else if (!g_strcmp0(g_pd->iface_path, interface_path)) {
524
525                         WDP_LOGD("p2p interface removed");
526                         wfd_oem_event_s event;
527
528                         ws_deactivate(1);
529
530                         memset(&event, 0x0, sizeof(wfd_oem_event_s));
531                         event.event_id = WFD_OEM_EVENT_DEACTIVATED;
532                         g_pd->callback(g_pd->user_data, &event);
533
534                         memset(g_pd->iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
535                 }
536 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE && TIZEN_MOBILE */
537         } else if(!g_strcmp0(signal,"PropertiesChanged")){
538                 WDP_LOGD("PropertiesChanged");
539         }
540 }
541
542 static void __ws_get_peer_property(const char *key, GVariant *value, void *user_data)
543 {
544         __WDP_LOG_FUNC_ENTER__;
545
546         wfd_oem_device_s *peer = (wfd_oem_device_s *)user_data;
547         if(!peer) {
548                 __WDP_LOG_FUNC_EXIT__;
549                 return;
550         }
551 #if defined (TIZEN_DEBUG_DBUS_VALUE)
552         CHECK_KEY_VALUE(key, value);
553 #endif /* TIZEN_DEBUG_DBUS_VALUE */
554
555         if (g_strcmp0(key, "DeviceName") == 0) {
556                 const char *name = NULL;
557
558                 g_variant_get(value, "&s", &name);
559                 g_strlcpy(peer->dev_name, name, WS_SSID_LEN);
560                 WDP_LOGD("Device name [%s]", peer->dev_name);
561
562         } else if (g_strcmp0(key, "config_method") == 0) {
563                 int config_methods = 0;
564                 g_variant_get(value, "q", &config_methods);
565
566                 if (config_methods & WS_CONFIG_METHOD_DISPLAY)
567                         peer->config_methods |= WFD_OEM_WPS_MODE_DISPLAY;
568                 if (config_methods & WS_CONFIG_METHOD_PUSHBUTTON)
569                         peer->config_methods |= WFD_OEM_WPS_MODE_PBC;
570                 if (config_methods & WS_CONFIG_METHOD_KEYPAD)
571                         peer->config_methods |= WFD_OEM_WPS_MODE_KEYPAD;
572                 WDP_LOGD("Config method [0x%x]", peer->config_methods);
573
574         } else if (g_strcmp0(key, "level") == 0) {
575
576         } else if (g_strcmp0(key, "devicecapability") == 0) {
577                 unsigned char devicecapability = 0;
578
579                 g_variant_get(value, "y", &devicecapability);
580                 peer->dev_flags = (int)devicecapability;
581                 WDP_LOGD("Device Capa [0x%02x]", peer->dev_flags);
582
583         } else if (g_strcmp0(key, "groupcapability") == 0) {
584                 unsigned char groupcapability = 0;
585
586                 g_variant_get(value, "y", &groupcapability);
587                 WDP_LOGD("Group Capa [0x%02x]", groupcapability);
588                 if (groupcapability & WS_GROUP_CAP_GROUP_OWNER) {
589                         peer->group_flags = WFD_OEM_GROUP_FLAG_GROUP_OWNER;
590                         peer->dev_role = WFD_OEM_DEV_ROLE_GO;
591                 }
592                 if (groupcapability & WS_GROUP_CAP_PERSISTENT_GROUP)
593                         peer->group_flags = WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
594
595         } else if (g_strcmp0(key, "PrimaryDeviceType") == 0) {
596                 unsigned char primarydevicetype[WS_DEVTYPE_LEN] = {0,};
597
598                 if(__ws_unpack_ay(primarydevicetype, value, WS_DEVTYPE_LEN)) {
599                         peer->pri_dev_type = primarydevicetype[1];
600                         peer->sec_dev_type = primarydevicetype[WS_DEVTYPE_LEN -1];
601                 }
602         } else if (g_strcmp0(key, "SecondaryDeviceTypes") == 0) {
603         } else if (g_strcmp0(key, "VendorExtension") == 0) {
604 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
605         } else if (g_strcmp0(key, "IEs") == 0) {
606                 unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
607
608                 if(__ws_unpack_ay(ies, value, WFD_SUBELEM_LEN_DEV_INFO + 3))
609                         __parsing_wfd_info(ies, &(peer->display));
610 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
611         } else if (g_strcmp0(key, "DeviceAddress") == 0) {
612
613                 if (__ws_unpack_ay(peer->dev_addr, value, WS_MACADDR_LEN))
614                         WDP_LOGD("Device address [" MACSTR "]", MAC2STR(peer->dev_addr));
615
616         } else if (g_strcmp0(key, "InterfaceAddress") == 0) {
617
618                 if (__ws_unpack_ay(peer->intf_addr, value, WS_MACADDR_LEN))
619                         WDP_LOGD("Interface address [" MACSTR "]", MAC2STR(peer->intf_addr));
620
621         } else if (g_strcmp0(key, "GODeviceAddress") == 0) {
622
623                 if (__ws_unpack_ay(peer->go_dev_addr, value, WS_MACADDR_LEN))
624                         WDP_LOGD("GODevice address [" MACSTR "]", MAC2STR(peer->go_dev_addr));
625
626                 if(!ISZEROMACADDR(peer->go_dev_addr))
627                         peer->dev_role = WFD_OEM_DEV_ROLE_GC;
628
629         } else {
630                 WDP_LOGE("Unknown value");
631         }
632         __WDP_LOG_FUNC_EXIT__;
633         return;
634 }
635
636 static void __ws_peer_property(const char *key, GVariant *value, void *user_data)
637 {
638         __WDP_LOG_FUNC_ENTER__;
639         if(!user_data) {
640                 __WDP_LOG_FUNC_EXIT__;
641                 return;
642         }
643
644         wfd_oem_dev_data_s *peer = (wfd_oem_dev_data_s *)user_data;
645 #if defined (TIZEN_DEBUG_DBUS_VALUE)
646         CHECK_KEY_VALUE(key, value);
647 #endif /* TIZEN_DEBUG_DBUS_VALUE */
648         if (g_strcmp0(key, "DeviceName") == 0) {
649                 const char *name = NULL;
650
651                 g_variant_get(value, "&s", &name);
652                 g_strlcpy(peer->name, name, WS_SSID_LEN);
653                 WDP_LOGD("Device Name [%s]", peer->name);
654
655         } else if (g_strcmp0(key, "config_method") == 0) {
656                 int config_methods = 0;
657
658                 g_variant_get(value, "q", &config_methods);
659
660                 if (config_methods & WS_CONFIG_METHOD_DISPLAY)
661                         peer->config_methods |= WFD_OEM_WPS_MODE_DISPLAY;
662                 if (config_methods & WS_CONFIG_METHOD_PUSHBUTTON)
663                         peer->config_methods |= WFD_OEM_WPS_MODE_PBC;
664                 if (config_methods & WS_CONFIG_METHOD_KEYPAD)
665                         peer->config_methods |= WFD_OEM_WPS_MODE_KEYPAD;
666                 WDP_LOGD("Config method [0x%x]", peer->config_methods);
667
668         } else if (g_strcmp0(key, "level") == 0) {
669
670         } else if (g_strcmp0(key, "devicecapability") == 0) {
671                 unsigned char devicecapability = 0;
672
673                 g_variant_get(value, "y", &devicecapability);
674                 peer->dev_flags = (int)devicecapability;
675                 WDP_LOGD("Device Capa [0x%02x]", peer->dev_flags);
676
677         } else if (g_strcmp0(key, "groupcapability") == 0) {
678                 unsigned char groupcapability = 0;
679
680                 g_variant_get(value, "y", &groupcapability);
681                 WDP_LOGD("Group Capa [0x%02x]", groupcapability);
682                 if (groupcapability & WS_GROUP_CAP_GROUP_OWNER) {
683                         peer->group_flags = WFD_OEM_GROUP_FLAG_GROUP_OWNER;
684                         peer->dev_role = WFD_OEM_DEV_ROLE_GO;
685                 }
686                 if (groupcapability & WS_GROUP_CAP_PERSISTENT_GROUP)
687                         peer->group_flags = WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
688
689         } else if (g_strcmp0(key, "PrimaryDeviceType") == 0) {
690                 unsigned char primarydevicetype[WS_DEVTYPE_LEN] = {0,};
691
692                 if (__ws_unpack_ay(primarydevicetype, value, WS_DEVTYPE_LEN)) {
693                         peer->pri_dev_type = primarydevicetype[1];
694                         peer->sec_dev_type = primarydevicetype[WS_DEVTYPE_LEN -1];
695                 }
696         } else if (g_strcmp0(key, "SecondaryDeviceTypes") == 0) {
697         } else if (g_strcmp0(key, "VendorExtension") == 0) {
698 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
699         } else if (g_strcmp0(key, "IEs") == 0) {
700                 unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
701
702                 if(__ws_unpack_ay(ies, value, WFD_SUBELEM_LEN_DEV_INFO + 3))
703                         __parsing_wfd_info(ies, &(peer->display));
704 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
705         } else if (g_strcmp0(key, "DeviceAddress") == 0) {
706
707                 if (__ws_unpack_ay(peer->p2p_dev_addr, value, WS_MACADDR_LEN))
708                         WDP_LOGD("Device address [" MACSTR "]", MAC2STR(peer->p2p_dev_addr));
709
710         } else if (g_strcmp0(key, "InterfaceAddress") == 0) {
711
712                 if (__ws_unpack_ay(peer->p2p_intf_addr, value, WS_MACADDR_LEN))
713                         WDP_LOGD("Interface Address [" MACSTR "]", MAC2STR(peer->p2p_intf_addr));
714
715         } else if (g_strcmp0(key, "GODeviceAddress") == 0) {
716
717                 unsigned char go_dev_addr[OEM_MACADDR_LEN] = {0,};
718                 if (__ws_unpack_ay(go_dev_addr, value, WS_MACADDR_LEN))
719                         WDP_LOGD("[" MACSTR "]", MAC2STR(go_dev_addr));
720
721                 if(!ISZEROMACADDR(go_dev_addr))
722                         peer->dev_role = WFD_OEM_DEV_ROLE_GC;
723
724         } else {
725                 WDP_LOGE("Unknown value");
726         }
727         __WDP_LOG_FUNC_EXIT__;
728         return;
729 }
730
731 void __ws_interface_property(const char *key, GVariant *value, void *user_data)
732 {
733         __WDP_LOG_FUNC_ENTER__;
734         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
735         if(!event)
736                 return;
737 #if defined (TIZEN_DEBUG_DBUS_VALUE)
738         CHECK_KEY_VALUE(key, value);
739 #endif /* TIZEN_DEBUG_DBUS_VALUE */
740         if (g_strcmp0(key, "Ifname") == 0) {
741                 const char *ifname = NULL;
742
743                 g_variant_get(value, "&s", &ifname);
744                 g_strlcpy(event->ifname, ifname, OEM_IFACE_NAME_LEN+1);
745                 WDP_LOGD("Ifname [%s]", event->ifname);
746
747         }
748         __WDP_LOG_FUNC_EXIT__;
749         return;
750 }
751
752 void __ws_group_property(const char *key, GVariant *value, void *user_data)
753 {
754         __WDP_LOG_FUNC_ENTER__;
755         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
756         if(!event || !event->edata)
757                 return;
758
759         wfd_oem_group_data_s *group = (wfd_oem_group_data_s *)event->edata;
760 #if defined (TIZEN_DEBUG_DBUS_VALUE)
761         CHECK_KEY_VALUE(key, value);
762 #endif /* TIZEN_DEBUG_DBUS_VALUE */
763         if (g_strcmp0(key, "Role") == 0) {
764                 const char *role = NULL;
765
766                 g_variant_get(value, "&s", &role);
767                 WDP_LOGD("Role [%s]", role);
768
769                 if (!strncmp(role, "GO", 2))
770                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
771                 else if (!strncmp(role, "client", 6))
772                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
773
774         } else if (g_strcmp0(key, "Frequency") == 0) {
775                 int frequency = 0;
776
777                 g_variant_get(value, "q", &frequency);
778                 group->freq = (int)frequency;
779
780         } else if (g_strcmp0(key, "Passphrase") == 0) {
781                 const char *passphrase = NULL;
782
783                 g_variant_get(value, "&s", &passphrase);
784                 g_strlcpy(group->pass, passphrase, OEM_PASS_PHRASE_LEN+1);
785                 WDP_LOGD("passphrase [%s]", group->pass);
786
787         } else if (g_strcmp0(key, "Group") == 0) {
788
789         } else if (g_strcmp0(key, "SSID") == 0) {
790                 unsigned char ssid[WS_SSID_LEN +1] = {0,};
791
792                 __ws_unpack_ay(ssid, value, WS_SSID_LEN);
793                 memcpy(group->ssid, ssid, WS_SSID_LEN+1);
794                 WDP_LOGD("ssid [%s]", group->ssid);
795
796         } else if (g_strcmp0(key, "BSSID") == 0) {
797
798                 if (__ws_unpack_ay(group->go_dev_addr, value, WS_MACADDR_LEN))
799                         WDP_LOGD("[" MACSTR "]", MAC2STR(group->go_dev_addr));
800
801         } else {
802                 WDP_LOGE("Unknown value");
803         }
804         __WDP_LOG_FUNC_EXIT__;
805         return;
806 }
807
808 void __ws_extract_invitation_details(const char *key, GVariant *value, void *user_data)
809 {
810         __WDP_LOG_FUNC_ENTER__;
811         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
812         if(!event || !event->edata)
813                 return;
814
815         wfd_oem_invite_data_s *invitation = (wfd_oem_invite_data_s *)event->edata;
816 #if defined (TIZEN_DEBUG_DBUS_VALUE)
817         CHECK_KEY_VALUE(key, value);
818 #endif /* TIZEN_DEBUG_DBUS_VALUE */
819         if (g_strcmp0(key, "sa") == 0) {
820                 if (__ws_unpack_ay(invitation->sa, value, WS_MACADDR_LEN))
821                         WDP_LOGD("SA [" MACSTR "]", MAC2STR(invitation->sa));
822
823         } else if (g_strcmp0(key, "go_dev_addr") == 0) {
824                 if (__ws_unpack_ay(invitation->go_dev_addr, value, WS_MACADDR_LEN))
825                                         WDP_LOGD("GO device address [" MACSTR "]", MAC2STR(invitation->go_dev_addr));
826
827         } else if (g_strcmp0(key, "bssid") == 0) {
828                 if (__ws_unpack_ay(invitation->bssid, value, WS_MACADDR_LEN))
829                                         WDP_LOGD("BSSID [" MACSTR "]", MAC2STR(invitation->bssid));
830
831         } else if (g_strcmp0(key, "persistent_id") == 0) {
832                 g_variant_get(value, "i", &(invitation->persistent_id));
833                 WDP_LOGD("persistent id [%d]", invitation->persistent_id);
834
835         } else if (g_strcmp0(key, "op_freq") == 0) {
836                 g_variant_get(value, "i", &(invitation->oper_freq));
837                 WDP_LOGD("op freq [%d]", invitation->oper_freq);
838         } else {
839                 WDP_LOGE("Unknown value");
840         }
841         __WDP_LOG_FUNC_EXIT__;
842         return;
843 }
844
845 void __ws_extract_group_details(const char *key, GVariant *value, void *user_data)
846 {
847         __WDP_LOG_FUNC_ENTER__;
848         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
849         if(!event || !event->edata)
850                 return;
851
852         if (!g_pd) {
853                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
854                 return;
855         }
856
857 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
858         wfd_oem_group_data_s *group = (wfd_oem_group_data_s *)event->edata;
859 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
860 #if defined (TIZEN_DEBUG_DBUS_VALUE)
861         CHECK_KEY_VALUE(key, value);
862 #endif /* TIZEN_DEBUG_DBUS_VALUE */
863         if (g_strcmp0(key, "interface_object") == 0) {
864                 static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
865                 const char *i_path = NULL;
866
867                 g_variant_get(value, "&o", &i_path);
868                 g_strlcpy(interface_path, i_path, DBUS_OBJECT_PATH_MAX);
869                 WDP_LOGD("Retrive Added path [%s]", interface_path);
870                 g_strlcpy(g_pd->group_iface_path, interface_path, DBUS_OBJECT_PATH_MAX);
871                 dbus_property_get_all(interface_path, g_pd->g_dbus,
872                                 SUPPLICANT_IFACE, __ws_interface_property, event);
873
874         } else if (g_strcmp0(key, "role") == 0) {
875                 const char *role = NULL;
876
877                 g_variant_get(value, "&s", &role);
878                 WDP_LOGD("Role [%s]", role);
879
880                 if (!strncmp(role, "GO", 2))
881                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
882                 else if (!strncmp(role, "client", 6))
883                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
884 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
885         } else if (g_strcmp0(key, "IpAddr") == 0) {
886
887                 if (__ws_unpack_ay(group->ip_addr, value, OEM_IPADDR_LEN))
888                         WDP_LOGD("IP address [" IPSTR "]", IP2STR(group->ip_addr));
889
890         } else if (g_strcmp0(key, "IpAddrMask") == 0) {
891
892                 if (__ws_unpack_ay(group->ip_addr_mask, value, OEM_IPADDR_LEN))
893                         WDP_LOGD("IP mask [" IPSTR "]", IP2STR(group->ip_addr_mask));
894
895         } else if (g_strcmp0(key, "IpAddrGo") == 0) {
896
897                 if (__ws_unpack_ay(group->ip_addr_go, value, OEM_IPADDR_LEN))
898                         WDP_LOGD("GO IP address [" IPSTR "]", IP2STR(group->ip_addr_go));
899 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
900         } else if (g_strcmp0(key, "group_object") == 0) {
901                 static char group_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
902                 const char *g_path;
903
904                 g_variant_get(value, "&o", &g_path);
905                 g_strlcpy(group_path, g_path, DBUS_OBJECT_PATH_MAX);
906                 WDP_LOGD("Retrive group path [%s]", group_path);
907                 dbus_property_get_all(group_path, g_pd->g_dbus, SUPPLICANT_P2P_GROUP,
908                                 __ws_group_property, event);
909
910                 g_pd->group_sub_id =
911                         g_dbus_connection_signal_subscribe(
912                                 g_pd->g_dbus,
913                                 SUPPLICANT_SERVICE, /* bus name */
914                                 SUPPLICANT_P2P_GROUP, /* interface */
915                                 NULL, /* member */
916                                 group_path, /* object path */
917                                 NULL, /* arg0 */
918                                 G_DBUS_SIGNAL_FLAGS_NONE,
919                                 _group_signal_cb,
920                                 NULL, NULL);
921         }
922         __WDP_LOG_FUNC_EXIT__;
923         return;
924 }
925
926 void __ws_extract_gonegfailaure_details(const char *key, GVariant *value, void *user_data)
927 {
928         __WDP_LOG_FUNC_ENTER__;
929         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
930         if(!event || !event->edata)
931                 return;
932
933         wfd_oem_conn_data_s *conn = (wfd_oem_conn_data_s *)event->edata;
934 #if defined (TIZEN_DEBUG_DBUS_VALUE)
935         CHECK_KEY_VALUE(key, value);
936 #endif /* TIZEN_DEBUG_DBUS_VALUE */
937         if (g_strcmp0(key, "peer_object") == 0) {
938                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
939                 const char *path;
940
941                 g_variant_get(value, "&o", &path);
942                 g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
943                 WDP_LOGD("Retrive peer path [%s]", peer_path);
944
945         } else if (g_strcmp0(key, "status") == 0) {
946                 int status = 0;
947
948                 g_variant_get(value, "i", &status);
949                 WDP_LOGD("Retrive status [%d]", status);
950                 conn->status = status;
951         }
952         __WDP_LOG_FUNC_EXIT__;
953         return;
954 }
955
956 void __ws_extract_gonegsuccess_details(const char *key, GVariant *value, void *user_data)
957 {
958         __WDP_LOG_FUNC_ENTER__;
959         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
960         if(!event || !event->edata)
961                 return;
962
963         wfd_oem_conn_data_s *edata = (wfd_oem_conn_data_s *)event->edata;
964 #if defined (TIZEN_DEBUG_DBUS_VALUE)
965         CHECK_KEY_VALUE(key, value);
966 #endif /* TIZEN_DEBUG_DBUS_VALUE */
967         if (g_strcmp0(key, "peer_object") == 0) {
968
969         } else if (g_strcmp0(key, "status") == 0) {
970
971         } else if (g_strcmp0(key, "passphrase") == 0) {
972
973         } else if (g_strcmp0(key, "role_go") == 0) {
974                 //local device role
975                 const char *role = NULL;
976
977                 g_variant_get(value, "&s", &role);
978                 if (!strncmp(role, "GO", 2))
979                         event->dev_role = WFD_OEM_DEV_ROLE_GO;
980                 else if (!strncmp(role, "client", 6))
981                         event->dev_role = WFD_OEM_DEV_ROLE_GC;
982
983         } else if (g_strcmp0(key, "ssid") == 0) {
984                 unsigned char ssid[WS_SSID_LEN +1] = {0,};
985
986                 __ws_unpack_ay(ssid, value, WS_SSID_LEN);
987                 memcpy(edata->ssid, ssid, WS_SSID_LEN+1);
988                 WDP_LOGD("ssid [%s]", edata->ssid);
989
990         } else if (g_strcmp0(key, "peer_device_addr") == 0) {
991
992                 if(__ws_unpack_ay(edata->peer_device_addr, value, WS_MACADDR_LEN))
993                         WDP_LOGD("Device address[" MACSTR "]", MAC2STR(edata->peer_device_addr));
994
995         } else if(g_strcmp0(key, "peer_interface_addr") == 0) {
996
997                 if(__ws_unpack_ay(edata->peer_intf_addr, value, WS_MACADDR_LEN))
998                         WDP_LOGD("Interface address [" MACSTR "]", MAC2STR(edata->peer_intf_addr));
999
1000         } else if (g_strcmp0(key, "wps_method") == 0) {
1001
1002         } else if (g_strcmp0(key, "frequency_list") == 0) {
1003
1004         } else if (g_strcmp0(key, "persistent_group") == 0) {
1005
1006                 g_variant_get(value, "i", &(edata->persistent_group));
1007                 WDP_LOGD("persistent_group [%d]", edata->persistent_group);
1008
1009         } else if (g_strcmp0(key, "peer_config_timeout") == 0) {
1010
1011         }
1012         __WDP_LOG_FUNC_EXIT__;
1013         return;
1014 }
1015
1016 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1017 void __ws_extract_peer_service(wfd_oem_event_s *data, unsigned char *service_hex, int tlvs_len)
1018 {
1019         GList *services = NULL;
1020         wfd_oem_new_service_s *new_service = NULL;
1021         char *segment = NULL;
1022         int count = 0;
1023         int ptr = 0;
1024         int length = 0;
1025         int res = 0;
1026
1027         while(ptr + 2 < WS_MAX_SERVICE_LEN &&
1028                         (length = (service_hex[ptr+1]*256) + service_hex[ptr]) > 0) {
1029                 segment = (char*) g_try_malloc0(length*2+1);
1030                 if(segment) {
1031                         __ws_byte_to_hex(segment, length * 2 + 1, &service_hex[ptr + 3], length);
1032                         res = __ws_segment_to_service(segment, &new_service);
1033                         if (res < 0) {
1034                                 WDP_LOGE("Failed to convert segment as service instance");
1035                                 g_free(segment);
1036                                 segment = NULL;
1037                                 continue;
1038                         }
1039                         services = g_list_append(services, new_service);
1040                         count++;
1041                         ptr += length + 4;
1042                         g_free(segment);
1043                         segment = NULL;
1044                 }
1045                 data->edata_type = WFD_OEM_EDATA_TYPE_NEW_SERVICE;
1046                 data->dev_role = count;
1047                 data->edata = (void*) services;
1048         }
1049 }
1050
1051 void __ws_extract_servicediscoveryresponse_details(const char *key, GVariant *value, void *user_data)
1052 {
1053         __WDP_LOG_FUNC_ENTER__;
1054         wfd_oem_event_s *event = (wfd_oem_event_s *)user_data;
1055
1056         if(!event)
1057                 return;
1058 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1059         CHECK_KEY_VALUE(key, value);
1060 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1061         if (g_strcmp0(key, "peer_object") == 0) {
1062                 static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
1063                 const char *path = NULL;
1064                 char *loc = NULL;
1065
1066                 g_variant_get(value, "&o", &path);
1067                 if(path == NULL)
1068                         return;
1069
1070                 WDP_LOGD("Retrive Added path [%s]", path);
1071                 loc = strrchr(path,'/');
1072                 if(loc != NULL)
1073                         __ws_mac_compact_to_normal(loc + 1, peer_dev);
1074                 __ws_txt_to_mac(peer_dev, event->dev_addr);
1075
1076         } else if (g_strcmp0(key, "update_indicator")) {
1077
1078         } else if (g_strcmp0(key, "tlvs")) {
1079                 GVariantIter *iter = NULL;
1080                 unsigned char service_hex[WS_MAX_SERVICE_LEN];
1081                 int byte_length = 0;
1082
1083                 g_variant_get(value, "ay", &iter);
1084                 if (iter == NULL) {
1085                         WDP_LOGE("failed to get iterator");
1086                         return;
1087                 }
1088
1089                 memset(service_hex, 0x0, WS_MAX_SERVICE_LEN);
1090                 while (g_variant_iter_loop (iter, "y", &service_hex[byte_length]))
1091                         byte_length++;
1092                 g_variant_iter_free(iter);
1093
1094                 __ws_extract_peer_service(event, service_hex, byte_length);
1095         }
1096
1097         __WDP_LOG_FUNC_EXIT__;
1098 }
1099 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1100
1101 static int _ws_flush()
1102 {
1103         __WDP_LOG_FUNC_ENTER__;
1104         GDBusConnection *g_dbus = NULL;
1105         dbus_method_param_s params;
1106         int res = 0;
1107
1108         if (!g_pd) {
1109                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1110                 return -1;
1111         }
1112
1113         g_dbus = g_pd->g_dbus;
1114         if (!g_dbus) {
1115                 WDP_LOGE("DBus connection is NULL");
1116                 return -1;
1117         }
1118         memset(&params, 0x0, sizeof(dbus_method_param_s));
1119
1120         dbus_set_method_param(&params, "Flush", g_pd->iface_path, g_dbus);
1121         params.params = NULL;
1122
1123         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
1124         if (res < 0)
1125                 WDP_LOGE("Failed to send command to wpa_supplicant");
1126         else
1127                 WDP_LOGD("Succeeded to flush");
1128
1129         __WDP_LOG_FUNC_EXIT__;
1130         return 0;
1131 }
1132
1133 static int _ws_cancel()
1134 {
1135         __WDP_LOG_FUNC_ENTER__;
1136         GDBusConnection *g_dbus = NULL;
1137         dbus_method_param_s params;
1138         int res = 0;
1139
1140         if (!g_pd) {
1141                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1142                 return -1;
1143         }
1144
1145
1146         g_dbus = g_pd->g_dbus;
1147         if (!g_dbus) {
1148                 WDP_LOGE("DBus connection is NULL");
1149                 return -1;
1150         }
1151         memset(&params, 0x0, sizeof(dbus_method_param_s));
1152
1153         dbus_set_method_param(&params, "Cancel", g_pd->iface_path , g_dbus);
1154         params.params = NULL;
1155
1156         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
1157         if (res < 0)
1158                 WDP_LOGE("Failed to send command to wpa_supplicant");
1159         else
1160                 WDP_LOGD("Succeeded to cancel");
1161
1162         __WDP_LOG_FUNC_EXIT__;
1163         return 0;
1164 }
1165
1166 static void _ws_process_device_found(GDBusConnection *connection,
1167                 const gchar *object_path, GVariant *parameters)
1168 {
1169         __WDP_LOG_FUNC_ENTER__;
1170         wfd_oem_event_s event;
1171         wfd_oem_dev_data_s *edata = NULL;
1172         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1173
1174         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1175         if (!edata) {
1176                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1177                                 strerror(errno));
1178                 __WDP_LOG_FUNC_EXIT__;
1179                 return;
1180         }
1181         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1182
1183         event.edata = (void*) edata;
1184         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1185         event.event_id = WFD_OEM_EVENT_PEER_FOUND;
1186
1187         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1188
1189         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1190                         __ws_peer_property, event.edata);
1191
1192         g_pd->callback(g_pd->user_data, &event);
1193         g_free(event.edata);
1194
1195         __WDP_LOG_FUNC_EXIT__;
1196 }
1197
1198 static void _ws_process_device_lost(GDBusConnection *connection,
1199                 const gchar *object_path, GVariant *parameters)
1200 {
1201         __WDP_LOG_FUNC_ENTER__;
1202         wfd_oem_event_s event;
1203         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1204
1205         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1206
1207         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1208         event.event_id = WFD_OEM_EVENT_PEER_DISAPPEARED;
1209
1210         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1211
1212         g_pd->callback(g_pd->user_data, &event);
1213
1214         __WDP_LOG_FUNC_EXIT__;
1215 }
1216
1217 static void _ws_process_find_stoppped(GDBusConnection *connection,
1218                 const gchar *object_path, GVariant *parameters)
1219 {
1220         __WDP_LOG_FUNC_ENTER__;
1221         wfd_oem_event_s event;
1222
1223         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1224
1225         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1226         event.event_id = WFD_OEM_EVENT_DISCOVERY_FINISHED;
1227
1228         g_pd->callback(g_pd->user_data, &event);
1229
1230         __WDP_LOG_FUNC_EXIT__;
1231 }
1232
1233 static void _ws_process_prov_disc_req_display_pin(GDBusConnection *connection,
1234                 const gchar *object_path, GVariant *parameters)
1235 {
1236         __WDP_LOG_FUNC_ENTER__;
1237
1238         wfd_oem_event_s event;
1239         wfd_oem_dev_data_s *edata = NULL;
1240
1241         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1242         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1243         const char *path = NULL;
1244         const char *pin = NULL;
1245         char *loc = NULL;
1246
1247         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1248         if (!edata) {
1249                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1250                                 strerror(errno));
1251                 __WDP_LOG_FUNC_EXIT__;
1252                 return;
1253         }
1254         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1255
1256         event.edata = (void*) edata;
1257         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1258         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1259         event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
1260
1261         g_variant_get(parameters, "(&o&s)", &path, &pin);
1262         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1263         WDP_LOGD("Retrive Added path [%s]", peer_path);
1264
1265         loc = strrchr(peer_path,'/');
1266         if(loc != NULL)
1267                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1268         __ws_txt_to_mac(peer_dev, event.dev_addr);
1269         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1270
1271         g_strlcpy(event.wps_pin, pin, WS_PINSTR_LEN + 1);
1272         WDP_LOGD("Retrive pin [%s]", event.wps_pin);
1273
1274         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1275                         __ws_peer_property, event.edata);
1276
1277         g_pd->callback(g_pd->user_data, &event);
1278         g_free(event.edata);
1279
1280         __WDP_LOG_FUNC_EXIT__;
1281 }
1282
1283 static void _ws_process_prov_disc_resp_display_pin(GDBusConnection *connection,
1284                 const gchar *object_path, GVariant *parameters)
1285 {
1286         __WDP_LOG_FUNC_ENTER__;
1287
1288         wfd_oem_event_s event;
1289         wfd_oem_dev_data_s *edata = NULL;
1290
1291         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1292         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1293         const char *path = NULL;
1294         const char *pin = NULL;
1295         char *loc = NULL;
1296
1297         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1298         if (!edata) {
1299                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1300                                 strerror(errno));
1301                 __WDP_LOG_FUNC_EXIT__;
1302                 return;
1303         }
1304         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1305
1306         event.edata = (void*) edata;
1307         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1308         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1309         event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
1310
1311         g_variant_get(parameters, "(&o&s)", &path, &pin);
1312         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1313         WDP_LOGD("Retrive Added path [%s]", peer_path);
1314
1315         loc = strrchr(peer_path,'/');
1316         if(loc != NULL)
1317                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1318         __ws_txt_to_mac(peer_dev, event.dev_addr);
1319         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1320
1321         g_strlcpy(event.wps_pin, pin, WS_PINSTR_LEN + 1);
1322         WDP_LOGD("Retrive pin [%s]", event.wps_pin);
1323
1324         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1325                         __ws_peer_property, event.edata);
1326
1327         g_pd->callback(g_pd->user_data, &event);
1328         g_free(event.edata);
1329
1330         __WDP_LOG_FUNC_EXIT__;
1331 }
1332
1333 static void _ws_process_prov_disc_req_enter_pin(GDBusConnection *connection,
1334                 const gchar *object_path, GVariant *parameters)
1335 {
1336         __WDP_LOG_FUNC_ENTER__;
1337         wfd_oem_event_s event;
1338         wfd_oem_dev_data_s *edata = NULL;
1339         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1340
1341         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1342         if (!edata) {
1343                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1344                                 strerror(errno));
1345                 __WDP_LOG_FUNC_EXIT__;
1346                 return;
1347         }
1348         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1349
1350         event.edata = (void*) edata;
1351         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1352         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1353         event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
1354
1355         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1356
1357         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1358                         __ws_peer_property, event.edata);
1359
1360         g_pd->callback(g_pd->user_data, &event);
1361         g_free(event.edata);
1362
1363         __WDP_LOG_FUNC_EXIT__;
1364 }
1365
1366 static void _ws_process_prov_disc_resp_enter_pin(GDBusConnection *connection,
1367                 const gchar *object_path, GVariant *parameters)
1368 {
1369         __WDP_LOG_FUNC_ENTER__;
1370         wfd_oem_event_s event;
1371         wfd_oem_dev_data_s *edata = NULL;
1372         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1373
1374         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1375         if (!edata) {
1376                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1377                                 strerror(errno));
1378                 __WDP_LOG_FUNC_EXIT__;
1379                 return;
1380         }
1381         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1382
1383         event.edata = (void*) edata;
1384         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1385         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1386         event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
1387
1388         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1389
1390         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1391                         __ws_peer_property, event.edata);
1392
1393         g_pd->callback(g_pd->user_data, &event);
1394         g_free(event.edata);
1395
1396         __WDP_LOG_FUNC_EXIT__;
1397 }
1398
1399 static void _ws_process_prov_disc_pbc_req(GDBusConnection *connection,
1400                 const gchar *object_path, GVariant *parameters)
1401 {
1402         __WDP_LOG_FUNC_ENTER__;
1403         wfd_oem_event_s event;
1404         wfd_oem_dev_data_s *edata = NULL;
1405         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1406
1407         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1408         if (!edata) {
1409                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1410                                 strerror(errno));
1411                 __WDP_LOG_FUNC_EXIT__;
1412                 return;
1413         }
1414         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1415
1416         event.edata = (void*) edata;
1417         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1418         event.event_id = WFD_OEM_EVENT_PROV_DISC_REQ;
1419         event.wps_mode = WFD_OEM_WPS_MODE_PBC;
1420
1421         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1422
1423         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1424                         __ws_peer_property, event.edata);
1425
1426         g_pd->callback(g_pd->user_data, &event);
1427         g_free(event.edata);
1428
1429         __WDP_LOG_FUNC_EXIT__;
1430 }
1431
1432 static void _ws_process_prov_disc_pbc_resp(GDBusConnection *connection,
1433                 const gchar *object_path, GVariant *parameters)
1434 {
1435         __WDP_LOG_FUNC_ENTER__;
1436         wfd_oem_event_s event;
1437         wfd_oem_dev_data_s *edata = NULL;
1438         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1439
1440         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1441         if (!edata) {
1442                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1443                                 strerror(errno));
1444                 __WDP_LOG_FUNC_EXIT__;
1445                 return;
1446         }
1447         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1448
1449         event.edata = (void*) edata;
1450         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1451         event.event_id = WFD_OEM_EVENT_PROV_DISC_RESP;
1452         event.wps_mode = WFD_OEM_WPS_MODE_PBC;
1453
1454         __ws_path_to_addr(peer_path, event.dev_addr, parameters);
1455
1456         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1457                         __ws_peer_property, event.edata);
1458
1459         g_pd->callback(g_pd->user_data, &event);
1460         g_free(event.edata);
1461
1462         __WDP_LOG_FUNC_EXIT__;
1463 }
1464
1465 static void _ws_process_prov_disc_failure(GDBusConnection *connection,
1466                 const gchar *object_path, GVariant *parameters)
1467 {
1468         __WDP_LOG_FUNC_ENTER__;
1469         wfd_oem_event_s event;
1470         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1471         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1472         const char *path = NULL;
1473         int prov_status = 0;
1474         char *loc = NULL;
1475
1476         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1477
1478         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1479         event.event_id = WFD_OEM_EVENT_PROV_DISC_FAIL;
1480
1481         g_variant_get(parameters, "(&oi)", &path, &prov_status);
1482         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1483         WDP_LOGD("Retrive Added path [%s]", peer_path);
1484         WDP_LOGD("Retrive Failure stateus [%d]", prov_status);
1485
1486         loc = strrchr(peer_path,'/');
1487         if(loc != NULL)
1488                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1489         __ws_txt_to_mac(peer_dev, event.dev_addr);
1490         WDP_LOGE("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1491
1492         g_pd->callback(g_pd->user_data, &event);
1493
1494         __WDP_LOG_FUNC_EXIT__;
1495 }
1496
1497 static void _ws_process_group_started(GDBusConnection *connection,
1498                 const gchar *object_path, GVariant *parameters)
1499 {
1500         __WDP_LOG_FUNC_ENTER__;
1501         GVariantIter *iter = NULL;
1502         wfd_oem_event_s event;
1503         wfd_oem_group_data_s *edata = NULL;
1504
1505         edata = (wfd_oem_group_data_s*)calloc(1, sizeof(wfd_oem_group_data_s));
1506         if (!edata) {
1507                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1508                                 strerror(errno));
1509                 __WDP_LOG_FUNC_EXIT__;
1510                 return;
1511         }
1512         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1513
1514         event.edata = (void*) edata;
1515         event.edata_type = WFD_OEM_EDATA_TYPE_GROUP;
1516         event.event_id = WFD_OEM_EVENT_GROUP_CREATED;
1517
1518         if(parameters != NULL){
1519                 g_variant_get(parameters, "(a{sv})", &iter);
1520
1521                 if (iter != NULL) {
1522                         dbus_property_foreach(iter, __ws_extract_group_details, &event);
1523                         g_variant_iter_free(iter);
1524                 }
1525         } else {
1526                 WDP_LOGE("No properties");
1527         }
1528
1529         g_pd->callback(g_pd->user_data, &event);
1530         g_free(event.edata);
1531
1532         __WDP_LOG_FUNC_EXIT__;
1533 }
1534
1535 static void _ws_process_go_neg_success(GDBusConnection *connection,
1536                 const gchar *object_path, GVariant *parameters)
1537 {
1538         __WDP_LOG_FUNC_ENTER__;
1539         GVariantIter *iter = NULL;
1540         wfd_oem_event_s event;
1541         wfd_oem_conn_data_s *edata = NULL;
1542
1543         edata = (wfd_oem_conn_data_s*)calloc(1, sizeof(wfd_oem_conn_data_s));
1544         if (!edata) {
1545                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1546                                 strerror(errno));
1547                 __WDP_LOG_FUNC_EXIT__;
1548                 return;
1549         }
1550         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1551
1552         event.edata = edata;
1553         event.edata_type = WFD_OEM_EDATA_TYPE_CONN;
1554         event.event_id = WFD_OEM_EVENT_GO_NEG_DONE;
1555
1556         if (parameters != NULL){
1557                 g_variant_get(parameters, "(a{sv})", &iter);
1558
1559                 if (iter != NULL) {
1560                         dbus_property_foreach(iter, __ws_extract_gonegsuccess_details, &event);
1561                         g_variant_iter_free(iter);
1562                 }
1563         } else {
1564                 WDP_LOGE("No properties");
1565         }
1566
1567         g_pd->callback(g_pd->user_data, &event);
1568         g_free(edata);
1569
1570         __WDP_LOG_FUNC_EXIT__;
1571 }
1572
1573 static void _ws_process_go_neg_failure(GDBusConnection *connection,
1574                 const gchar *object_path, GVariant *parameters)
1575 {
1576         __WDP_LOG_FUNC_ENTER__;
1577         GVariantIter *iter = NULL;
1578         wfd_oem_event_s event;
1579         wfd_oem_conn_data_s *edata = NULL;
1580
1581         edata = (wfd_oem_conn_data_s *) g_try_malloc0(sizeof(wfd_oem_conn_data_s));
1582         if (!edata) {
1583                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1584                                 strerror(errno));
1585                 __WDP_LOG_FUNC_EXIT__;
1586                 return;
1587         }
1588         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1589
1590         event.edata = (void*) edata;
1591         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1592         event.event_id = WFD_OEM_EVENT_GO_NEG_FAIL;
1593
1594         if (parameters != NULL) {
1595                 g_variant_get(parameters, "(a{sv})", &iter);
1596
1597                 if (iter != NULL) {
1598                         dbus_property_foreach(iter, __ws_extract_gonegfailaure_details, &event);
1599                         g_variant_iter_free(iter);
1600                 }
1601         } else {
1602                 WDP_LOGE("No properties");
1603         }
1604
1605         g_pd->callback(g_pd->user_data, &event);
1606         g_free(event.edata);
1607
1608         __WDP_LOG_FUNC_EXIT__;
1609 }
1610
1611 static void _ws_process_go_neg_request(GDBusConnection *connection,
1612                 const gchar *object_path, GVariant *parameters)
1613 {
1614         __WDP_LOG_FUNC_ENTER__;
1615         wfd_oem_event_s event;
1616         wfd_oem_dev_data_s *edata = NULL;
1617         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
1618         static unsigned char peer_dev[OEM_MACSTR_LEN] = {'\0',};
1619         const char *path = NULL;
1620         char * loc = NULL;
1621
1622         int dev_passwd_id = 0;
1623         int device_go_intent = 0;
1624
1625         edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
1626         if (!edata) {
1627                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1628                                 strerror(errno));
1629                 __WDP_LOG_FUNC_EXIT__;
1630                 return;
1631         }
1632         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1633
1634         event.edata = (void*) edata;
1635         event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
1636         event.event_id = WFD_OEM_EVENT_GO_NEG_REQ;
1637
1638         g_variant_get(parameters, "(&oqy)", &path, &dev_passwd_id, &device_go_intent);
1639         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
1640
1641         WDP_LOGD("Retrive peer path [%s]", peer_path);
1642         WDP_LOGD("Retrive dev_passwd_id [%d]", dev_passwd_id);
1643         WDP_LOGD("Retrive device_go_intent [%d]", device_go_intent);
1644
1645         if (dev_passwd_id == WS_DEV_PASSWD_ID_PUSH_BUTTON)
1646                 event.wps_mode = WFD_OEM_WPS_MODE_PBC;
1647         else if (dev_passwd_id == WS_DEV_PASSWD_ID_REGISTRAR_SPECIFIED)
1648                 event.wps_mode = WFD_OEM_WPS_MODE_DISPLAY;
1649         else if (dev_passwd_id == WS_DEV_PASSWD_ID_USER_SPECIFIED)
1650                 event.wps_mode = WFD_OEM_WPS_MODE_KEYPAD;
1651         else
1652                 event.wps_mode = WFD_OEM_WPS_MODE_NONE;
1653         edata->device_go_intent = device_go_intent;
1654
1655         loc = strrchr(peer_path,'/');
1656         if(loc != NULL)
1657                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
1658         __ws_txt_to_mac(peer_dev, event.dev_addr);
1659         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(event.dev_addr));
1660
1661         dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
1662                         __ws_peer_property, event.edata);
1663
1664         g_pd->callback(g_pd->user_data, &event);
1665         g_free(event.edata);
1666
1667         __WDP_LOG_FUNC_EXIT__;
1668 }
1669 static void _ws_process_invitation_received(GDBusConnection *connection,
1670                 const gchar *object_path, GVariant *parameters)
1671 {
1672         __WDP_LOG_FUNC_ENTER__;
1673         GVariantIter *iter = NULL;
1674         wfd_oem_event_s event;
1675         wfd_oem_invite_data_s *edata = NULL;
1676
1677         edata = (wfd_oem_invite_data_s *) g_try_malloc0(sizeof(wfd_oem_invite_data_s));
1678         if (!edata) {
1679                 WDP_LOGF("Failed to allocate memory for event. [%s]",
1680                                 strerror(errno));
1681                 __WDP_LOG_FUNC_EXIT__;
1682                 return;
1683         }
1684         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1685
1686         event.edata = (void*) edata;
1687         event.edata_type = WFD_OEM_EDATA_TYPE_INVITE;
1688         event.event_id = WFD_OEM_EVENT_INVITATION_REQ;
1689
1690         if (parameters != NULL) {
1691                 g_variant_get(parameters, "(a{sv})", &iter);
1692
1693                 if (iter != NULL) {
1694                         dbus_property_foreach(iter, __ws_extract_invitation_details, &event);
1695                         g_variant_iter_free(iter);
1696                 }
1697         } else {
1698                 WDP_LOGE("No properties");
1699         }
1700         memcpy(&(event.dev_addr), edata->sa, OEM_MACADDR_LEN);
1701
1702         g_pd->callback(g_pd->user_data, &event);
1703         g_free(event.edata);
1704
1705         __WDP_LOG_FUNC_EXIT__;
1706 }
1707
1708 static void _ws_process_invitation_result(GDBusConnection *connection,
1709                 const gchar *object_path, GVariant *parameters)
1710 {
1711         __WDP_LOG_FUNC_ENTER__;
1712         wfd_oem_event_s event;
1713         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1714
1715 //      g_pd->callback(g_pd->user_data, event);
1716
1717         __WDP_LOG_FUNC_EXIT__;
1718 }
1719
1720 static void _ws_process_group_finished(GDBusConnection *connection,
1721                 const gchar *object_path, GVariant *parameters)
1722 {
1723         __WDP_LOG_FUNC_ENTER__;
1724         wfd_oem_event_s event;
1725
1726         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1727
1728         event.event_id = WFD_OEM_EVENT_GROUP_DESTROYED;
1729         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1730
1731         g_dbus_connection_signal_unsubscribe(g_pd->g_dbus, g_pd->group_sub_id);
1732         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
1733         _ws_flush();
1734
1735         g_pd->callback(g_pd->user_data, &event);
1736
1737         __WDP_LOG_FUNC_EXIT__;
1738 }
1739
1740 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1741 static void _ws_process_service_discovery_response(GDBusConnection *connection,
1742                 const gchar *object_path, GVariant *parameters)
1743 {
1744         __WDP_LOG_FUNC_ENTER__;
1745         GVariantIter *iter = NULL;
1746         wfd_oem_event_s event;
1747
1748         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1749
1750         event.event_id = WFD_OEM_EVENT_SERV_DISC_RESP;
1751
1752         if(parameters != NULL) {
1753                 g_variant_get(parameters, "(a{sv})", &iter);
1754                 if(iter != NULL) {
1755                         dbus_property_foreach(iter, __ws_extract_servicediscoveryresponse_details, &event);
1756                         event.edata_type = WFD_OEM_EDATA_TYPE_NEW_SERVICE;
1757                         g_variant_iter_free(iter);
1758                 }
1759         } else {
1760                 WDP_LOGE("No Properties");
1761         }
1762
1763         g_pd->callback(g_pd->user_data, &event);
1764
1765         if (event.edata_type == WFD_OEM_EDATA_TYPE_NEW_SERVICE)
1766                 g_list_free((GList*) event.edata);
1767
1768         __WDP_LOG_FUNC_EXIT__;
1769 }
1770 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1771
1772 static void _ws_process_persistent_group_added(GDBusConnection *connection,
1773                 const gchar *object_path, GVariant *parameters)
1774 {
1775         __WDP_LOG_FUNC_ENTER__;
1776         wfd_oem_event_s event;
1777         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1778
1779 //      g_pd->callback(g_pd->user_data, &event);
1780
1781         __WDP_LOG_FUNC_EXIT__;
1782 }
1783
1784 static void _ws_process_persistent_group_removed(GDBusConnection *connection,
1785                 const gchar *object_path, GVariant *parameters)
1786 {
1787         __WDP_LOG_FUNC_ENTER__;
1788         wfd_oem_event_s event;
1789         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1790
1791 //      g_pd->callback(g_pd->user_data, &event);
1792
1793         __WDP_LOG_FUNC_EXIT__;
1794 }
1795
1796 static void _ws_process_wps_failed(GDBusConnection *connection,
1797                 const gchar *object_path, GVariant *parameters)
1798 {
1799         __WDP_LOG_FUNC_ENTER__;
1800         GVariantIter *iter = NULL;
1801         wfd_oem_event_s event;
1802         const char *name = NULL;
1803
1804         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1805
1806         event.event_id = WFD_OEM_EVENT_WPS_FAIL;
1807         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1808
1809         g_variant_get(parameters, "(&sa{sv})", &name, &iter);
1810
1811         WDP_LOGD("code [%s]", name);
1812
1813         if (iter != NULL) {
1814
1815                 gchar *key = NULL;
1816                 GVariant *value = NULL;
1817
1818                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1819 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1820                         CHECK_KEY_VALUE(key, value);
1821 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1822                 }
1823                 g_variant_iter_free(iter);
1824         }
1825
1826         g_pd->callback(g_pd->user_data, &event);
1827
1828         __WDP_LOG_FUNC_EXIT__;
1829 }
1830
1831 static void _ws_process_group_formation_failure(GDBusConnection *connection,
1832                 const gchar *object_path, GVariant *parameters)
1833 {
1834         __WDP_LOG_FUNC_ENTER__;
1835         wfd_oem_event_s event;
1836
1837         memset(&event, 0x0, sizeof(wfd_oem_event_s));
1838
1839         event.event_id = WFD_OEM_EVENT_GROUP_FORMATION_FAILURE;
1840         event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
1841
1842         g_pd->callback(g_pd->user_data, &event);
1843
1844         __WDP_LOG_FUNC_EXIT__;
1845 }
1846
1847 static struct {
1848         const char *interface;
1849         const char *member;
1850         void (*function) (GDBusConnection *connection,const gchar *object_path,
1851                         GVariant *parameters);
1852 } ws_p2pdevice_signal_map[] = {
1853         {
1854                 SUPPLICANT_P2PDEVICE,
1855                 "DeviceFound",
1856                 _ws_process_device_found
1857         },
1858         {
1859                 SUPPLICANT_P2PDEVICE,
1860                 "DeviceLost",
1861                 _ws_process_device_lost
1862         },
1863         {
1864                 SUPPLICANT_P2PDEVICE,
1865                 "FindStopped",
1866                 _ws_process_find_stoppped
1867         },
1868         {
1869                 SUPPLICANT_P2PDEVICE,
1870                 "ProvisionDiscoveryRequestDisplayPin",
1871                 _ws_process_prov_disc_req_display_pin
1872         },
1873         {
1874                 SUPPLICANT_P2PDEVICE,
1875                 "ProvisionDiscoveryResponseDisplayPin",
1876                 _ws_process_prov_disc_resp_display_pin
1877         },
1878         {
1879                 SUPPLICANT_P2PDEVICE,
1880                 "ProvisionDiscoveryRequestEnterPin",
1881                 _ws_process_prov_disc_req_enter_pin
1882         },
1883         {
1884                 SUPPLICANT_P2PDEVICE,
1885                 "ProvisionDiscoveryResponseEnterPin",
1886                 _ws_process_prov_disc_resp_enter_pin
1887         },
1888         {
1889                 SUPPLICANT_P2PDEVICE,
1890                 "ProvisionDiscoveryPBCRequest",
1891                 _ws_process_prov_disc_pbc_req
1892         },
1893         {
1894                 SUPPLICANT_P2PDEVICE,
1895                 "ProvisionDiscoveryPBCResponse",
1896                 _ws_process_prov_disc_pbc_resp
1897         },
1898         {
1899                 SUPPLICANT_P2PDEVICE,
1900                 "ProvisionDiscoveryFailure",
1901                 _ws_process_prov_disc_failure
1902         },
1903         {
1904                 SUPPLICANT_P2PDEVICE,
1905                 "GroupStarted",
1906                 _ws_process_group_started
1907         },
1908         {
1909                 SUPPLICANT_P2PDEVICE,
1910                 "GONegotiationSuccess",
1911                 _ws_process_go_neg_success
1912         },
1913         {
1914                 SUPPLICANT_P2PDEVICE,
1915                 "GONegotiationFailure",
1916                 _ws_process_go_neg_failure
1917         },
1918         {
1919                 SUPPLICANT_P2PDEVICE,
1920                 "GONegotiationRequest",
1921                 _ws_process_go_neg_request
1922         },
1923         {
1924                 SUPPLICANT_P2PDEVICE,
1925                 "InvitationReceived",
1926                 _ws_process_invitation_received
1927         },
1928         {
1929                 SUPPLICANT_P2PDEVICE,
1930                 "InvitationResult",
1931                 _ws_process_invitation_result
1932         },
1933         {
1934                 SUPPLICANT_P2PDEVICE,
1935                 "GroupFinished",
1936                 _ws_process_group_finished
1937         },
1938 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1939         {
1940                 SUPPLICANT_P2PDEVICE,
1941                 "ServiceDiscoveryResponse",
1942                 _ws_process_service_discovery_response
1943         },
1944 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1945         {
1946                 SUPPLICANT_P2PDEVICE,
1947                 "PersistentGroupAdded",
1948                 _ws_process_persistent_group_added
1949         },
1950         {
1951                 SUPPLICANT_P2PDEVICE,
1952                 "PersistentGroupRemoved",
1953                 _ws_process_persistent_group_removed
1954         },
1955         {
1956                 SUPPLICANT_P2PDEVICE,
1957                 "WpsFailed",
1958                 _ws_process_wps_failed
1959         },
1960         {
1961                 SUPPLICANT_P2PDEVICE,
1962                 "GroupFormationFailure",
1963                 _ws_process_group_formation_failure
1964         },
1965         {
1966                 NULL,
1967                 NULL,
1968                 NULL
1969         }
1970 };
1971
1972 static void _p2pdevice_signal_cb(GDBusConnection *connection,
1973                 const gchar *sender, const gchar *object_path, const gchar *interface,
1974                 const gchar *signal, GVariant *parameters, gpointer user_data)
1975 {
1976         int i = 0;
1977 #if defined (TIZEN_DEBUG_DBUS_VALUE)
1978         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
1979 #endif /* TIZEN_DEBUG_DBUS_VALUE */
1980
1981         if (!g_pd) {
1982                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
1983                 return;
1984         }
1985
1986         for (i = 0; ws_p2pdevice_signal_map[i].member != NULL; i++) {
1987                 if (!g_strcmp0(signal, ws_p2pdevice_signal_map[i].member) &&
1988                                 ws_p2pdevice_signal_map[i].function != NULL)
1989                         ws_p2pdevice_signal_map[i].function(connection, object_path, parameters);
1990         }
1991 }
1992
1993
1994 static void __ws_parse_peer_joined(char *peer_path,
1995                 unsigned char *dev_addr, unsigned char *ip_addr, GVariant *parameter)
1996 {
1997         __WDP_LOG_FUNC_ENTER__;
1998
1999         GVariantIter *iter;
2000         static unsigned char peer_dev[WS_MACSTR_LEN] = {'\0',};
2001         const char *path = NULL;
2002         char *loc = NULL;
2003 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2004         int i = 0;
2005 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2006
2007         g_variant_get(parameter, "(&oay)", &path, &iter);
2008         g_strlcpy(peer_path, path, DBUS_OBJECT_PATH_MAX);
2009         WDP_LOGD("Retrive Added path [%s]", peer_path);
2010
2011         loc = strrchr(peer_path,'/');
2012         if(loc != NULL)
2013                 __ws_mac_compact_to_normal(loc + 1, peer_dev);
2014         __ws_txt_to_mac(peer_dev, dev_addr);
2015         WDP_LOGD("peer mac [" MACSTR "]", MAC2STR(dev_addr));
2016 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2017         for(i = 0; i < OEM_IPADDR_LEN; i++)
2018                 g_variant_iter_loop (iter, "y", &ip_addr[i]);
2019         g_variant_iter_free(iter);
2020
2021         WDP_LOGD("peer ip [" IPSTR "]", IP2STR(ip_addr));
2022 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2023
2024         __WDP_LOG_FUNC_EXIT__;
2025         return;
2026 }
2027
2028
2029 static void _group_signal_cb(GDBusConnection *connection,
2030                 const gchar *sender, const gchar *object_path, const gchar *interface,
2031                 const gchar *signal, GVariant *parameters, gpointer user_data)
2032 {
2033 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2034         DEBUG_SIGNAL(sender, object_path, interface, signal, parameters);
2035 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2036
2037         if (!g_pd) {
2038                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2039                 return;
2040         }
2041
2042         if (!g_strcmp0(signal,"PeerJoined")) {
2043
2044                 wfd_oem_event_s event;
2045                 wfd_oem_dev_data_s *edata = NULL;
2046
2047                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2048
2049                 edata = (wfd_oem_dev_data_s *) g_try_malloc0(sizeof(wfd_oem_dev_data_s));
2050                 if (!edata) {
2051                         WDP_LOGF("Failed to allocate memory for event. [%s]",
2052                                         strerror(errno));
2053                         __WDP_LOG_FUNC_EXIT__;
2054                         return;
2055                 }
2056                 memset(&event, 0x0, sizeof(wfd_oem_event_s));
2057
2058                 event.edata = (void*) edata;
2059                 event.edata_type = WFD_OEM_EDATA_TYPE_DEVICE;
2060                 event.event_id = WFD_OEM_EVENT_STA_CONNECTED;
2061
2062                 __ws_parse_peer_joined(peer_path, event.dev_addr, event.ip_addr_peer, parameters);
2063
2064                 dbus_property_get_all(peer_path, g_pd->g_dbus, SUPPLICANT_P2P_PEER,
2065                                 __ws_peer_property, event.edata);
2066
2067                 g_pd->callback(g_pd->user_data, &event);
2068                 g_free(edata);
2069
2070         } else if (!g_strcmp0(signal,"PeerDisconnected")) {
2071
2072                 wfd_oem_event_s event;
2073
2074                 static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2075
2076                 memset(&event, 0x0, sizeof(wfd_oem_event_s));
2077
2078                 event.edata_type = WFD_OEM_EDATA_TYPE_NONE;
2079                 event.event_id = WFD_OEM_EVENT_STA_DISCONNECTED;
2080
2081                 __ws_path_to_addr(peer_path, event.dev_addr, parameters);
2082
2083                 g_pd->callback(g_pd->user_data, &event);
2084         }
2085 }
2086
2087 static void __register_p2pdevice_signal(GVariant *value, void *user_data)
2088 {
2089         __WDP_LOG_FUNC_ENTER__;
2090         ws_dbus_plugin_data_s * pd_data;
2091         static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2092         const char *path = NULL;
2093
2094         if (!g_pd) {
2095                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2096                 return;
2097         }
2098
2099         pd_data = (ws_dbus_plugin_data_s *)g_pd;
2100
2101         g_variant_get(value, "(&o)", &path);
2102         g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
2103         g_strlcpy(pd_data->iface_path, path, DBUS_OBJECT_PATH_MAX);
2104
2105         WDP_LOGD("interface object path [%s]", interface_path);
2106         /* subscribe interface p2p signal */
2107         WDP_LOGD("register P2PDevice iface signal");
2108         pd_data->p2pdevice_sub_id = g_dbus_connection_signal_subscribe(
2109                 pd_data->g_dbus,
2110                 SUPPLICANT_SERVICE, /* bus name */
2111                 SUPPLICANT_P2PDEVICE, /* interface */
2112                 NULL, /* member */
2113                 NULL, /* object path */
2114                 NULL, /* arg0 */
2115                 G_DBUS_SIGNAL_FLAGS_NONE,
2116                 _p2pdevice_signal_cb,
2117                 NULL, NULL);
2118         __WDP_LOG_FUNC_EXIT__;
2119 }
2120
2121 static int _ws_create_interface(const char *iface_name, handle_reply function, void *user_data)
2122 {
2123         __WDP_LOG_FUNC_ENTER__;
2124         GDBusConnection *g_dbus = NULL;
2125         GVariantBuilder *builder = NULL;
2126         dbus_method_param_s params;
2127
2128         int res = 0;
2129
2130         if (!g_pd) {
2131                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2132                 return -1;
2133         }
2134
2135         g_dbus = g_pd->g_dbus;
2136         if (!g_dbus) {
2137                 WDP_LOGE("DBus connection is NULL");
2138                 return -1;
2139         }
2140         memset(&params, 0x0, sizeof(dbus_method_param_s));
2141
2142         dbus_set_method_param(&params, "CreateInterface", SUPPLICANT_PATH, g_dbus);
2143
2144         builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
2145         g_variant_builder_add(builder, "{sv}", "Ifname", g_variant_new_string(iface_name));
2146         g_variant_builder_add(builder, "{sv}", "ConfigFile", g_variant_new_string(CONF_FILE_PATH));
2147         params.params = g_variant_new("(a{sv})", builder);
2148         g_variant_builder_unref(builder);
2149         res = dbus_method_call(&params, SUPPLICANT_INTERFACE, function, user_data);
2150         if (res < 0)
2151                 WDP_LOGE("Failed to send command to wpa_supplicant");
2152         else
2153                 WDP_LOGD("Succeeded to CreateInterface");
2154
2155         __WDP_LOG_FUNC_EXIT__;
2156         return 0;
2157 }
2158
2159 static int _ws_get_interface(const char *iface_name, handle_reply function, void *user_data)
2160 {
2161         __WDP_LOG_FUNC_ENTER__;
2162         GDBusConnection *g_dbus = NULL;
2163         dbus_method_param_s params;
2164         int res = 0;
2165
2166         if (!g_pd) {
2167                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2168                 return -1;
2169         }
2170
2171         g_dbus = g_pd->g_dbus;
2172         if (!g_dbus) {
2173                 WDP_LOGE("DBus connection is NULL");
2174                 return -1;
2175         }
2176
2177         dbus_set_method_param(&params, SUPPLICANT_METHOD_GETINTERFACE,
2178                         SUPPLICANT_PATH, g_pd->g_dbus);
2179
2180         params.params = g_variant_new("(s)", iface_name);
2181 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2182         DEBUG_PARAMS(params.params);
2183 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2184
2185         res = dbus_method_call(&params, SUPPLICANT_INTERFACE,
2186                         function, user_data);
2187
2188         if (res < 0)
2189                 WDP_LOGE("Failed to send command to wpa_supplicant");
2190         else
2191                 WDP_LOGD("Succeeded to get interface");
2192
2193         __WDP_LOG_FUNC_EXIT__;
2194         return res;
2195 }
2196
2197 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
2198 static void __ws_remove_interface(GVariant *value, void *user_data)
2199 {
2200         __WDP_LOG_FUNC_ENTER__;
2201         GDBusConnection *g_dbus = NULL;
2202         dbus_method_param_s params;
2203         const char *path = NULL;
2204         static char interface_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
2205         int res = 0;
2206
2207         if (!g_pd) {
2208                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2209                 return;
2210         }
2211
2212         g_dbus = g_pd->g_dbus;
2213         if (!g_dbus) {
2214                 WDP_LOGE("DBus connection is NULL");
2215                 return;
2216         }
2217
2218         g_variant_get(value, "(&o)", &path);
2219         g_strlcpy(interface_path, path, DBUS_OBJECT_PATH_MAX);
2220         WDP_LOGD("interface object path [%s]", interface_path);
2221
2222         memset(&params, 0x0, sizeof(dbus_method_param_s));
2223
2224         dbus_set_method_param(&params, "RemoveInterface", SUPPLICANT_PATH, g_dbus);
2225         params.params = g_variant_new("(o)", interface_path);
2226
2227         res = dbus_method_call(&params, SUPPLICANT_INTERFACE, NULL, NULL);
2228         if (res < 0)
2229                 WDP_LOGE("Failed to send command to wpa_supplicant");
2230         else
2231                 WDP_LOGD("Succeeded to RemoveInterface");
2232
2233         __WDP_LOG_FUNC_EXIT__;
2234         return;
2235 }
2236 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
2237
2238 static int _ws_init_dbus_connection(void)
2239 {
2240         __WDP_LOG_FUNC_ENTER__;
2241         GDBusConnection *conn = NULL;
2242         GError *error = NULL;
2243         int res = 0;
2244
2245         if (!g_pd) {
2246                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2247                 return -1;
2248         }
2249
2250         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
2251
2252         if (conn == NULL) {
2253                 if(error != NULL){
2254                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
2255                                         error->message);
2256                         g_error_free(error);
2257                 }
2258                 __WDP_LOG_FUNC_EXIT__;
2259                 return -1;
2260         }
2261
2262         g_pd->g_dbus = conn;
2263
2264         WDP_LOGD("register supplicant signal");
2265         /* subscribe supplicant signal */
2266         g_pd->supp_sub_id = g_dbus_connection_signal_subscribe(
2267                 g_pd->g_dbus,
2268                 SUPPLICANT_SERVICE, /* bus name */
2269                 SUPPLICANT_INTERFACE, /* interface */
2270                 NULL, /* member */
2271                 SUPPLICANT_PATH, /* object path */
2272                 NULL, /* arg0 */
2273                 G_DBUS_SIGNAL_FLAGS_NONE,
2274                 _supplicant_signal_cb,
2275                 NULL, NULL);
2276 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
2277         if(_ws_get_interface(COMMON_IFACE_NAME, NULL, NULL) < 0)
2278                 _ws_create_interface(COMMON_IFACE_NAME, NULL, NULL);
2279         if(_ws_get_interface(P2P_IFACE_NAME, __register_p2pdevice_signal, NULL) < 0)
2280                 res = _ws_create_interface(P2P_IFACE_NAME, __register_p2pdevice_signal, NULL);
2281 #else /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
2282         if(_ws_get_interface(COMMON_IFACE_NAME, __register_p2pdevice_signal, NULL) < 0)
2283                 res = _ws_create_interface(COMMON_IFACE_NAME, __register_p2pdevice_signal, NULL);
2284 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
2285
2286         if (res < 0)
2287                         WDP_LOGE("Failed to subscribe interface signal");
2288         else
2289                 WDP_LOGI("Successfully register signal filters");
2290
2291         __WDP_LOG_FUNC_EXIT__;
2292         return res;
2293 }
2294
2295 static int _ws_deinit_dbus_connection(void)
2296 {
2297         GDBusConnection *g_dbus = NULL;
2298
2299         if (!g_pd) {
2300                 WDP_LOGE("Invalid parameter");
2301                 __WDP_LOG_FUNC_EXIT__;
2302                 return -1;
2303         }
2304
2305         g_dbus = g_pd->g_dbus;
2306         if (!g_dbus) {
2307                 WDP_LOGE("DBus connection is NULL");
2308                 return -1;
2309         }
2310
2311         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->supp_sub_id);
2312         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->p2pdevice_sub_id);
2313         g_dbus_connection_signal_unsubscribe(g_dbus, g_pd->group_sub_id);
2314
2315         g_pd->group_iface_sub_id = 0;
2316         g_pd->p2pdevice_sub_id = 0;
2317         g_pd->group_sub_id = 0;
2318         memset(g_pd->group_iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
2319         memset(g_pd->iface_path, 0x0, DBUS_OBJECT_PATH_MAX);
2320
2321         g_object_unref(g_dbus);
2322         return 0;
2323 }
2324
2325 int wfd_plugin_load(wfd_oem_ops_s **ops)
2326 {
2327         if (!ops) {
2328                 WDP_LOGE("Invalid parameter");
2329                 return -1;
2330         }
2331
2332         *ops = &supplicant_ops;
2333
2334         return 0;
2335 }
2336
2337 static int _ws_reset_plugin(ws_dbus_plugin_data_s *f_pd)
2338 {
2339         __WDP_LOG_FUNC_ENTER__;
2340
2341         if (!f_pd) {
2342                 WDP_LOGE("Invalid parameter");
2343                 __WDP_LOG_FUNC_EXIT__;
2344                 return -1;
2345         }
2346
2347         _ws_deinit_dbus_connection();
2348
2349         if (f_pd->activated)
2350                 ws_deactivate(f_pd->concurrent);
2351
2352         g_free(f_pd);
2353
2354         __WDP_LOG_FUNC_EXIT__;
2355         return 0;
2356 }
2357
2358 #ifndef TIZEN_WIFI_MODULE_BUNDLE
2359 static int __ws_check_net_interface(char* if_name)
2360 {
2361         struct ifreq ifr;
2362         int fd;
2363
2364         if (if_name == NULL) {
2365                 WDP_LOGE("Invalid param");
2366                 return -1;
2367         }
2368
2369         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
2370         if (fd < 0) {
2371                 WDP_LOGE("socket create error: %d", fd);
2372                 return -2;
2373         }
2374
2375         memset(&ifr, 0, sizeof(ifr));
2376         strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
2377         ifr.ifr_name[IFNAMSIZ-1] = '\0';
2378
2379         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
2380                 close(fd);
2381                 WDP_LOGE("ioctl error: SIOCGIFFLAGS: %s [ %s ]", strerror(errno), if_name); /* interface is not found. */
2382                 return -3;
2383         }
2384
2385         close(fd);
2386
2387         if (ifr.ifr_flags & IFF_UP) {
2388                 WDP_LOGD("%s interface is up", if_name);
2389                 return 1;
2390         } else if (!(ifr.ifr_flags & IFF_UP)) {
2391                 WDP_LOGD("%s interface is down", if_name);
2392                 return 0;
2393         }
2394         return 0;
2395 }
2396 #endif
2397
2398 int ws_init(wfd_oem_event_cb callback, void *user_data)
2399 {
2400         __WDP_LOG_FUNC_ENTER__;
2401
2402         if (g_pd)
2403                 _ws_reset_plugin(g_pd);
2404
2405         errno = 0;
2406         g_pd = (ws_dbus_plugin_data_s*) g_try_malloc0 (sizeof(ws_dbus_plugin_data_s));
2407         if (!g_pd) {
2408                 WDP_LOGE("Failed to allocate memory for plugin data. [%s]", strerror(errno));
2409                 return -1;
2410         }
2411
2412         g_pd->callback = callback;
2413         g_pd->user_data = user_data;
2414         g_pd->initialized = TRUE;
2415
2416         __WDP_LOG_FUNC_EXIT__;
2417         return 0;
2418 }
2419
2420 int ws_deinit()
2421 {
2422         __WDP_LOG_FUNC_ENTER__;
2423
2424         if (g_pd) {
2425                 _ws_reset_plugin(g_pd);
2426                 g_pd = NULL;
2427         }
2428
2429         __WDP_LOG_FUNC_EXIT__;
2430         return 0;
2431 }
2432
2433 gboolean _ws_util_execute_file(const char *file_path,
2434         char *const args[], char *const envs[])
2435 {
2436         pid_t pid = 0;
2437         int rv = 0;
2438         errno = 0;
2439         register unsigned int index = 0;
2440
2441         while (args[index] != NULL) {
2442                 WDP_LOGD("[%s]", args[index]);
2443                 index++;
2444         }
2445
2446         if (!(pid = fork())) {
2447                 WDP_LOGD("pid(%d), ppid(%d)", getpid(), getppid());
2448                 WDP_LOGD("Inside child, exec (%s) command", file_path);
2449
2450                 errno = 0;
2451                 if (execve(file_path, args, envs) == -1) {
2452                         WDP_LOGE("Fail to execute command (%s)", strerror(errno));
2453                         exit(1);
2454                 }
2455         } else if (pid > 0) {
2456                 if (waitpid(pid, &rv, 0) == -1)
2457                         WDP_LOGD("wait pid (%u) rv (%d)", pid, rv);
2458                 if (WIFEXITED(rv)) {
2459                         WDP_LOGD("exited, rv=%d", WEXITSTATUS(rv));
2460                 } else if (WIFSIGNALED(rv)) {
2461                         WDP_LOGD("killed by signal %d", WTERMSIG(rv));
2462                 } else if (WIFSTOPPED(rv)) {
2463                         WDP_LOGD("stopped by signal %d", WSTOPSIG(rv));
2464                 } else if (WIFCONTINUED(rv)) {
2465                         WDP_LOGD("continued");
2466                 }
2467
2468                 return TRUE;
2469         }
2470
2471         WDP_LOGE("failed to fork (%s)", strerror(errno));
2472         return FALSE;
2473 }
2474
2475 #ifndef TIZEN_WIFI_MODULE_BUNDLE
2476 static int __ws_p2p_firmware_start(void)
2477 {
2478         GError *error = NULL;
2479         GVariant *reply = NULL;
2480         GVariant *param = NULL;
2481         GDBusConnection *connection = NULL;
2482         const char *device = "p2p";
2483
2484         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
2485         if (connection == NULL) {
2486                 if(error != NULL){
2487                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
2488                                         error->message);
2489                         g_error_free(error);
2490                 }
2491                 __WDP_LOG_FUNC_EXIT__;
2492                 return -1;
2493         }
2494         param = g_variant_new("(s)", device);
2495
2496         reply = g_dbus_connection_call_sync (connection,
2497                         NETCONFIG_SERVICE, /* bus name */
2498                         NETCONFIG_WIFI_PATH, /* object path */
2499                         NETCONFIG_WIFI_INTERFACE ".Firmware", /* interface name */
2500                         "Start", /* method name */
2501                         param, /* GVariant *params */
2502                         NULL, /* reply_type */
2503                         G_DBUS_CALL_FLAGS_NONE, /* flags */
2504                         NETCONFIG_DBUS_REPLY_TIMEOUT , /* timeout */
2505                         NULL, /* cancellable */
2506                         &error); /* error */
2507
2508         if(error != NULL){
2509                 if(strstr(error->message, ".AlreadyExists") != NULL) {
2510                         WDP_LOGD("p2p already enabled");
2511                         g_error_free(error);
2512
2513                 } else {
2514                         WDP_LOGE("Error! Failed to call net-config method: [%s]",
2515                                         error->message);
2516                         g_error_free(error);
2517                         if(reply)
2518                                  g_variant_unref(reply);
2519                         g_object_unref(connection);
2520                         __WDP_LOG_FUNC_EXIT__;
2521                         return -1;
2522                 }
2523         }
2524         if(reply)
2525                  g_variant_unref(reply);
2526         g_object_unref(connection);
2527         return 0;
2528 }
2529
2530 static int __ws_p2p_firmware_stop(void)
2531 {
2532         GError *error = NULL;
2533         GVariant *reply = NULL;
2534         GVariant *param = NULL;
2535         GDBusConnection *connection = NULL;
2536         const char *device = "p2p";
2537
2538         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
2539         if (connection == NULL) {
2540                 if(error != NULL){
2541                         WDP_LOGE("Error! Failed to connect to the D-BUS daemon: [%s]",
2542                                         error->message);
2543                         g_error_free(error);
2544                 }
2545                 __WDP_LOG_FUNC_EXIT__;
2546                 return -1;
2547         }
2548         param = g_variant_new("(s)", device);
2549
2550         reply = g_dbus_connection_call_sync (connection,
2551                         NETCONFIG_SERVICE, /* bus name */
2552                         NETCONFIG_WIFI_PATH, /* object path */
2553                         NETCONFIG_WIFI_INTERFACE ".Firmware", /* interface name */
2554                         "Stop", /* method name */
2555                         param, /* GVariant *params */
2556                         NULL, /* reply_type */
2557                         G_DBUS_CALL_FLAGS_NONE, /* flags */
2558                         NETCONFIG_DBUS_REPLY_TIMEOUT , /* timeout */
2559                         NULL, /* cancellable */
2560                         &error); /* error */
2561
2562         if(error != NULL){
2563                 if(strstr(error->message, ".AlreadyExists") != NULL) {
2564                         WDP_LOGD("p2p already disabled");
2565                         g_error_free(error);
2566
2567                 } else {
2568                         WDP_LOGE("Error! Failed to call net-config method: [%s]",
2569                                         error->message);
2570                         g_error_free(error);
2571                         if(reply)
2572                                  g_variant_unref(reply);
2573                         g_object_unref(connection);
2574                         __WDP_LOG_FUNC_EXIT__;
2575                         return -1;
2576                 }
2577         }
2578         if(reply)
2579                  g_variant_unref(reply);
2580         g_object_unref(connection);
2581         return 0;
2582 }
2583 #endif
2584
2585 static int __ws_p2p_supplicant_start(void)
2586 {
2587         gboolean rv = FALSE;
2588         const char *path = "/usr/sbin/p2p_supp.sh";
2589         char *const args[] = { "/usr/sbin/p2p_supp.sh", "start_dbus", NULL };
2590         char *const envs[] = { NULL };
2591
2592         rv = _ws_util_execute_file(path, args, envs);
2593
2594         if (rv != TRUE) {
2595                 WDP_LOGE("Failed to start p2p_supp.sh");
2596                 return -1;
2597         }
2598
2599         WDP_LOGI("Successfully started p2p_supp.sh");
2600         return 0;
2601 }
2602
2603
2604 static int __ws_p2p_supplicant_stop(void)
2605 {
2606         gboolean rv = FALSE;
2607         const char *path = "/usr/sbin/p2p_supp.sh";
2608         char *const args[] = { "/usr/sbin/p2p_supp.sh", "stop", NULL };
2609         char *const envs[] = { NULL };
2610
2611         rv = _ws_util_execute_file(path, args, envs);
2612
2613         if (rv != TRUE) {
2614                 WDP_LOGE("Failed to stop p2p_supp.sh");
2615                 return -1;
2616         }
2617
2618         WDP_LOGI("Successfully stopped p2p_supp.sh");
2619         return 0;
2620 }
2621 #if 0
2622 static int __ws_p2p_on(void)
2623 {
2624         DBusError error;
2625         DBusMessage *reply = NULL;
2626         DBusMessage *message = NULL;
2627         DBusConnection *connection = NULL;
2628
2629         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
2630         if (connection == NULL) {
2631                 WDP_LOGE("Failed to get system bus");
2632                 return -EIO;
2633         }
2634
2635         message = dbus_message_new_method_call(NETCONFIG_SERVICE,
2636                         NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE, "LoadP2pDriver");
2637         if (message == NULL) {
2638                 WDP_LOGE("Failed DBus method call");
2639                 dbus_connection_unref(connection);
2640                 return -EIO;
2641         }
2642
2643         dbus_error_init(&error);
2644
2645         reply = dbus_connection_send_with_reply_and_block(connection, message,
2646                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
2647         if (dbus_error_is_set(&error) == TRUE) {
2648                 if (NULL != strstr(error.message, ".AlreadyExists")) {
2649                         // p2p already enabled
2650                 } else {
2651                         WDP_LOGE("dbus_connection_send_with_reply_and_block() failed. "
2652                                         "DBus error [%s: %s]", error.name, error.message);
2653
2654                         dbus_error_free(&error);
2655                 }
2656
2657                 dbus_error_free(&error);
2658         }
2659
2660         if (reply != NULL)
2661                 dbus_message_unref(reply);
2662
2663         dbus_message_unref(message);
2664         dbus_connection_unref(connection);
2665
2666         return 0;
2667 }
2668
2669 static int __ws_p2p_off(void)
2670 {
2671         DBusError error;
2672         DBusMessage *reply = NULL;
2673         DBusMessage *message = NULL;
2674         DBusConnection *connection = NULL;
2675
2676         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
2677         if (connection == NULL) {
2678                 WDP_LOGE("Failed to get system bus");
2679                 return -EIO;
2680         }
2681
2682         message = dbus_message_new_method_call(NETCONFIG_SERVICE,
2683                         NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE, "RemoveP2pDriver");
2684         if (message == NULL) {
2685                 WDP_LOGE("Failed DBus method call");
2686                 dbus_connection_unref(connection);
2687                 return -EIO;
2688         }
2689
2690         dbus_error_init(&error);
2691
2692         reply = dbus_connection_send_with_reply_and_block(connection, message,
2693                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
2694         if (dbus_error_is_set(&error) == TRUE) {
2695                 if (NULL != strstr(error.message, ".AlreadyExists")) {
2696                         // p2p already disabled
2697                 } else {
2698                         WDP_LOGE("dbus_connection_send_with_reply_and_block() failed. "
2699                                         "DBus error [%s: %s]", error.name, error.message);
2700
2701                         dbus_error_free(&error);
2702                 }
2703
2704                 dbus_error_free(&error);
2705         }
2706
2707         if (reply != NULL)
2708                 dbus_message_unref(reply);
2709
2710         dbus_message_unref(message);
2711         dbus_connection_unref(connection);
2712
2713         return 0;
2714 }
2715 #endif
2716
2717 int __ws_init_p2pdevice()
2718 {
2719         __WDP_LOG_FUNC_ENTER__;
2720         GDBusConnection *g_dbus = NULL;
2721
2722         GVariant *value = NULL;
2723         GVariant *param = NULL;
2724         GVariantBuilder *builder = NULL;
2725         GVariantBuilder *type_builder = NULL;
2726         dbus_method_param_s params;
2727
2728         const char *primary_device_type = PRIMARY_DEVICE_TYPE;
2729
2730 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2731         const char *ip_addr_go = DEFAULT_IP_GO;
2732         const char *ip_addr_mask = DEFAULT_IP_MASK;
2733         const char *ip_addr_start = DEFAULT_IP_START;
2734         const char *ip_addr_end = DEFAULT_IP_END;
2735 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2736         int i = 0;
2737         int res = 0;
2738
2739         if (!g_pd) {
2740                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2741                 return -1;
2742         }
2743
2744         for(i = 0; i < WS_DEVTYPE_LEN; i++)
2745                 WDP_LOGD("device type[%02x]", primary_device_type[i]);
2746
2747         g_dbus = g_pd->g_dbus;
2748         if (!g_dbus) {
2749                 WDP_LOGE("DBus connection is NULL");
2750                 return -1;
2751         }
2752         memset(&params, 0x0, sizeof(dbus_method_param_s));
2753
2754         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
2755                          g_dbus);
2756
2757         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
2758         g_variant_builder_add (builder, "{sv}", "DeviceName",
2759                                         g_variant_new_string(DEFAULT_DEVICE_NAME));
2760
2761         g_variant_builder_add (builder, "{sv}", "GOIntent",
2762                                         g_variant_new_uint32(DEFAULT_GO_INTENT));
2763
2764         g_variant_builder_add (builder, "{sv}", "PersistentReconnect",
2765                                         g_variant_new_boolean(DEFAULT_PERSISTENT_RECONNECT));
2766
2767         g_variant_builder_add (builder, "{sv}", "ListenRegClass",
2768                                         g_variant_new_uint32(DEFAULT_LISTEN_REG_CLASS));
2769
2770         g_variant_builder_add (builder, "{sv}", "ListenChannel",
2771                                         g_variant_new_uint32(DEFAULT_LISTEN_CHANNEL));
2772
2773         g_variant_builder_add (builder, "{sv}", "OperRegClass",
2774                                         g_variant_new_uint32(DEFAULT_OPER_REG_CLASS));
2775
2776         g_variant_builder_add (builder, "{sv}", "OperChannel",
2777                                         g_variant_new_uint32(DEFAULT_OPER_CHANNEL));
2778
2779         g_variant_builder_add (builder, "{sv}", "SsidPostfix",
2780                                         g_variant_new_string(DEFAULT_DEVICE_NAME));
2781
2782         g_variant_builder_add (builder, "{sv}", "NoGroupIface",
2783                                         g_variant_new_boolean(DEFAULT_NO_GROUP_IFACE));
2784
2785         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
2786         for(i = 0; i < WS_DEVTYPE_LEN; i++)
2787                 g_variant_builder_add(type_builder, "y", primary_device_type[i]);
2788         g_variant_builder_add (builder, "{sv}", "PrimaryDeviceType",
2789                         g_variant_new ("ay", type_builder));
2790         g_variant_builder_unref (type_builder);
2791 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
2792         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
2793         for(i = 0; i < OEM_IPADDR_LEN; i++)
2794                 g_variant_builder_add(type_builder, "y", ip_addr_go[i]);
2795         g_variant_builder_add (builder, "{sv}", "IpAddrGO",
2796                         g_variant_new ("ay", type_builder));
2797         g_variant_builder_unref (type_builder);
2798
2799         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
2800         for(i = 0; i < OEM_IPADDR_LEN; i++)
2801                 g_variant_builder_add(type_builder, "y", ip_addr_mask[i]);
2802         g_variant_builder_add (builder, "{sv}", "IpAddrMask",
2803                         g_variant_new ("ay", type_builder));
2804         g_variant_builder_unref (type_builder);
2805
2806         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
2807         for(i = 0; i < OEM_IPADDR_LEN; i++)
2808                 g_variant_builder_add(type_builder, "y", ip_addr_start[i]);
2809         g_variant_builder_add (builder, "{sv}", "IpAddrStart",
2810                         g_variant_new ("ay", type_builder));
2811         g_variant_builder_unref (type_builder);
2812
2813         type_builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
2814         for(i = 0; i < OEM_IPADDR_LEN; i++)
2815                 g_variant_builder_add(type_builder, "y", ip_addr_end[i]);
2816         g_variant_builder_add (builder, "{sv}", "IpAddrEnd",
2817                         g_variant_new ("ay", type_builder));
2818         g_variant_builder_unref (type_builder);
2819 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
2820         value = g_variant_new ("a{sv}", builder);
2821         g_variant_builder_unref (builder);
2822
2823         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
2824
2825         params.params = param;
2826 #if defined (TIZEN_DEBUG_DBUS_VALUE)
2827         DEBUG_PARAMS(params.params);
2828 #endif /* TIZEN_DEBUG_DBUS_VALUE */
2829
2830         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
2831         if (res < 0)
2832                 WDP_LOGE("Failed to send command to wpa_supplicant");
2833         else
2834                 WDP_LOGD("Succeeded to initialize p2pdevice");
2835         __WDP_LOG_FUNC_EXIT__;
2836         return res;
2837 }
2838
2839 int __ws_set_config_methods()
2840 {
2841         __WDP_LOG_FUNC_ENTER__;
2842         GDBusConnection *g_dbus = NULL;
2843
2844         GVariant *value = NULL;
2845         GVariant *param = NULL;
2846
2847         dbus_method_param_s params;
2848         int res = 0;
2849
2850         if (!g_pd) {
2851                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2852                 return -1;
2853         }
2854
2855         g_dbus = g_pd->g_dbus;
2856         if (!g_dbus) {
2857                 WDP_LOGE("DBus connection is NULL");
2858                 return -1;
2859         }
2860         memset(&params, 0x0, sizeof(dbus_method_param_s));
2861
2862         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
2863                          g_dbus);
2864
2865         value = g_variant_new_string(DEFAULT_CONFIG_METHOD);
2866
2867         param = g_variant_new("(ssv)", SUPPLICANT_WPS, "ConfigMethods", value);
2868         params.params = param;
2869
2870         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
2871         if (res < 0)
2872                 WDP_LOGE("Failed to send command to wpa_supplicant");
2873         else
2874                 WDP_LOGD("Succeeded to set config method(%s)", DEFAULT_CONFIG_METHOD);
2875
2876         __WDP_LOG_FUNC_EXIT__;
2877         return res;
2878 }
2879
2880 int ws_activate(int concurrent)
2881 {
2882         __WDP_LOG_FUNC_ENTER__;
2883         int res = 0;
2884         int retry_count = 0;
2885
2886         if (!g_pd) {
2887                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2888                 return -1;
2889         }
2890
2891         res = __ws_p2p_supplicant_start();
2892         if (res < 0) {
2893                 res = __ws_p2p_supplicant_stop();
2894                 WDP_LOGI("P2P supplicant stopped with error %d", res);
2895                 __WDP_LOG_FUNC_EXIT__;
2896                 return -1;
2897         }
2898 #ifndef TIZEN_WIFI_MODULE_BUNDLE
2899         while (retry_count < WS_CONN_RETRY_COUNT) {
2900                 /* load wlan driver */
2901                 if(concurrent == 0)
2902                         res = __ws_p2p_firmware_start();
2903                 if (res < 0) {
2904                         WDP_LOGE("Failed to load driver [ret=%d]", res);
2905                         return -1;
2906                 }
2907                 WDP_LOGI("P2P firmware started with error %d", res);
2908
2909                 if (__ws_check_net_interface(COMMON_IFACE_NAME) < 0) {
2910                         usleep(150000); // wait for 150ms
2911                         concurrent = 0;
2912                         retry_count++;
2913                         WDP_LOGE("interface is not up: retry, %d", retry_count);
2914                 } else {
2915                         break;
2916                 }
2917         }
2918 #endif
2919         if (retry_count >= WS_CONN_RETRY_COUNT) {
2920                 WDP_LOGE("Driver loading is failed", res);
2921                 __WDP_LOG_FUNC_EXIT__;
2922                 return -1;
2923         }
2924         if (retry_count > 0) {
2925                 // Give driver marginal time to config net
2926                 WDP_LOGE("Driver loading is done. Wait marginal time for driver");
2927                 sleep(1); // 1s
2928         }
2929
2930         g_pd->concurrent = concurrent;
2931
2932         res = _ws_init_dbus_connection();
2933         if (res < 0) {
2934                 res = __ws_p2p_supplicant_stop();
2935                 WDP_LOGI("[/usr/sbin/p2p_supp.sh stop] returns %d", res);
2936 #ifndef TIZEN_WIFI_MODULE_BUNDLE
2937                 res = __ws_p2p_firmware_stop();
2938                 WDP_LOGI("P2P firmware stopped with error %d", res);
2939 #endif
2940                 __WDP_LOG_FUNC_EXIT__;
2941                 return -1;
2942         }
2943
2944         g_pd->activated = TRUE;
2945         __ws_init_p2pdevice();
2946         __ws_set_config_methods();
2947
2948         __WDP_LOG_FUNC_EXIT__;
2949         return 0;
2950 }
2951
2952 int ws_deactivate(int concurrent)
2953 {
2954         __WDP_LOG_FUNC_ENTER__;
2955         int res = 0;
2956
2957         if (!g_pd) {
2958                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
2959                 return -1;
2960         }
2961
2962         if (!g_pd->activated) {
2963                 WDP_LOGE("Wi-Fi Direct is not activated");
2964                 return -1;
2965         }
2966
2967         ws_stop_scan();
2968
2969         g_pd->concurrent = concurrent;
2970 #if defined (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD)
2971         _ws_get_interface(P2P_IFACE_NAME, __ws_remove_interface, NULL);
2972         if(concurrent == 0)
2973                 _ws_get_interface(COMMON_IFACE_NAME, __ws_remove_interface, NULL);
2974 #endif /* (TIZEN_MOBILE) && (TIZEN_WLAN_BOARD_SPRD) */
2975
2976         _ws_deinit_dbus_connection();
2977
2978         if(concurrent == 0) {
2979                 res = __ws_p2p_supplicant_stop();
2980                 WDP_LOGI("[/usr/sbin/p2p_supp.sh stop] returns %d", res);
2981 #ifndef TIZEN_WIFI_MODULE_BUNDLE
2982                 res = __ws_p2p_firmware_stop();
2983                 WDP_LOGI("P2P firmware stopped with error %d", res);
2984 #endif
2985         }
2986         g_pd->activated = FALSE;
2987
2988         __WDP_LOG_FUNC_EXIT__;
2989         return 0;
2990 }
2991
2992 #if 0
2993 static gboolean _retry_start_scan(gpointer data)
2994 {
2995         __WDP_LOG_FUNC_ENTER__;
2996
2997         WDP_LOGD("Succeeded to start scan");
2998
2999         __WDP_LOG_FUNC_EXIT__;
3000         return 0;
3001 }
3002 #endif
3003
3004 int ws_start_scan(wfd_oem_scan_param_s *param)
3005 {
3006         __WDP_LOG_FUNC_ENTER__;
3007         GDBusConnection *g_dbus = NULL;
3008         GVariantBuilder *builder = NULL;
3009         GVariant *value = NULL;
3010         dbus_method_param_s params;
3011         int res = 0;
3012
3013         if (!param) {
3014                 WDP_LOGE("Invalid parameter");
3015                 return -1;
3016         }
3017
3018         if (!g_pd) {
3019                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3020                 return -1;
3021         }
3022
3023         g_dbus = g_pd->g_dbus;
3024         if (!g_dbus) {
3025                 WDP_LOGE("DBus connection is NULL");
3026                 return -1;
3027         }
3028         memset(&params, 0x0, sizeof(dbus_method_param_s));
3029
3030         if (param->scan_mode == WFD_OEM_SCAN_MODE_ACTIVE) {
3031
3032                 dbus_set_method_param(&params, "Find",  g_pd->iface_path, g_dbus);
3033
3034                 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3035
3036                         if (param->scan_time)
3037                                 g_variant_builder_add (builder, "{sv}", "Timeout",
3038                                                         g_variant_new_int32(param->scan_time));
3039                         if (param->scan_type == WFD_OEM_SCAN_TYPE_SOCIAL)
3040                                 g_variant_builder_add (builder, "{sv}", "DiscoveryType",
3041                                                         g_variant_new_string("social"));
3042
3043                         value = g_variant_new ("(a{sv})", builder);
3044                         g_variant_builder_unref (builder);
3045         } else {
3046
3047                 dbus_set_method_param(&params, "Listen", g_pd->iface_path, g_dbus);
3048                 value = g_variant_new ("(i)", param->scan_time);
3049         }
3050
3051         params.params = value;
3052 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3053         DEBUG_PARAMS(params.params);
3054 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3055
3056         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3057         if (res < 0)
3058                 WDP_LOGE("Failed to send command to wpa_supplicant");
3059         else
3060                 WDP_LOGD("Succeeded to start scan");
3061
3062         __WDP_LOG_FUNC_EXIT__;
3063         return res;
3064 }
3065
3066 int ws_restart_scan(int freq)
3067 {
3068         __WDP_LOG_FUNC_ENTER__;
3069         GDBusConnection *g_dbus = NULL;
3070         GVariantBuilder *builder = NULL;
3071         GVariant *value = NULL;
3072         dbus_method_param_s params;
3073         int res = 0;
3074
3075         if (!g_pd) {
3076                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3077                 return -1;
3078         }
3079
3080         g_dbus = g_pd->g_dbus;
3081         if (!g_dbus) {
3082                 WDP_LOGE("DBus connection is NULL");
3083                 return -1;
3084         }
3085         memset(&params, 0x0, sizeof(dbus_method_param_s));
3086
3087         dbus_set_method_param(&params, "Find", g_pd->iface_path, g_dbus);
3088
3089         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3090         g_variant_builder_add (builder, "{sv}", "Timeout", g_variant_new_int32(2));
3091         g_variant_builder_add (builder, "{sv}", "DiscoveryType",
3092                                 g_variant_new_string("social"));
3093         value = g_variant_new ("(a{sv})", builder);
3094         g_variant_builder_unref (builder);
3095
3096         params.params = value;
3097 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3098         DEBUG_PARAMS(params.params);
3099 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3100
3101         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3102         if (res < 0)
3103                 WDP_LOGE("Failed to send command to wpa_supplicant");
3104         else
3105                 WDP_LOGD("Succeeded to start scan");
3106
3107         __WDP_LOG_FUNC_EXIT__;
3108         return res;
3109 }
3110
3111 int ws_stop_scan()
3112 {
3113         __WDP_LOG_FUNC_ENTER__;
3114         GDBusConnection *g_dbus = NULL;
3115         dbus_method_param_s params;
3116         int res = 0;
3117
3118         if (!g_pd) {
3119                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3120                 return -1;
3121         }
3122
3123         g_dbus = g_pd->g_dbus;
3124         if (!g_dbus) {
3125                 WDP_LOGE("DBus connection is NULL");
3126                 return -1;
3127         }
3128         memset(&params, 0x0, sizeof(dbus_method_param_s));
3129
3130         dbus_set_method_param(&params, "StopFind", g_pd->iface_path, g_dbus);
3131         params.params = NULL;
3132
3133         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3134         if (res < 0)
3135                         WDP_LOGE("Failed to send command to wpa_supplicant");
3136         else
3137                 WDP_LOGD("Succeeded to stop scan");
3138
3139         __WDP_LOG_FUNC_EXIT__;
3140         return res;
3141 }
3142
3143 int ws_get_visibility(int *visibility)
3144 {
3145         __WDP_LOG_FUNC_ENTER__;
3146
3147         __WDP_LOG_FUNC_EXIT__;
3148         return 0;
3149 }
3150
3151 int ws_set_visibility(int visibility)
3152 {
3153         __WDP_LOG_FUNC_ENTER__;
3154
3155         __WDP_LOG_FUNC_EXIT__;
3156         return 0;
3157 }
3158
3159 int ws_get_scan_result(GList **peers, int *peer_count)
3160 {
3161         __WDP_LOG_FUNC_ENTER__;
3162
3163         __WDP_LOG_FUNC_EXIT__;
3164         return 0;
3165 }
3166
3167 int ws_get_peer_info(unsigned char *peer_addr, wfd_oem_device_s **peer)
3168 {
3169         __WDP_LOG_FUNC_ENTER__;
3170         GDBusConnection *g_dbus = NULL;
3171         wfd_oem_device_s *ws_dev = NULL;
3172         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3173         int res = 0;
3174
3175         if (!peer_addr || !peer) {
3176                 WDP_LOGE("Invalid parameter");
3177                 return -1;
3178         }
3179
3180         if (!g_pd) {
3181                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3182                 return -1;
3183         }
3184
3185         g_dbus = g_pd->g_dbus;
3186         if (!g_dbus) {
3187                 WDP_LOGE("DBus connection is NULL");
3188                 return -1;
3189         }
3190
3191         ws_dev = (wfd_oem_device_s *) g_try_malloc0(sizeof(wfd_oem_device_s));
3192         if (!ws_dev) {
3193                 WDP_LOGF("Failed to allocate memory device. [%s]",
3194                                 strerror(errno));
3195                 __WDP_LOG_FUNC_EXIT__;
3196                 return -1;
3197         }
3198
3199         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3200                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3201
3202         WDP_LOGD("get peer path [%s]", peer_path);
3203
3204         res = dbus_property_get_all(peer_path, g_dbus, SUPPLICANT_P2P_PEER,
3205                                 __ws_get_peer_property, ws_dev);
3206
3207         if (res < 0) {
3208                         WDP_LOGE("Failed to send command to wpa_supplicant");
3209                         g_free(ws_dev);
3210                         __WDP_LOG_FUNC_EXIT__;
3211                         return -1;
3212         } else {
3213                 WDP_LOGD("succeeded to get peer info");
3214                 *peer = ws_dev;
3215         }
3216         __WDP_LOG_FUNC_EXIT__;
3217         return 0;
3218 }
3219
3220 int ws_prov_disc_req(unsigned char *peer_addr, wfd_oem_wps_mode_e wps_mode, int join)
3221 {
3222         __WDP_LOG_FUNC_ENTER__;
3223         GDBusConnection *g_dbus = NULL;
3224         GVariant *value = NULL;
3225         dbus_method_param_s params;
3226         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3227         int res = 0;
3228
3229         if (!peer_addr) {
3230                 WDP_LOGE("Invalid parameter");
3231                 __WDP_LOG_FUNC_EXIT__;
3232                 return -1;
3233         }
3234
3235         if (!g_pd) {
3236                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3237                 __WDP_LOG_FUNC_EXIT__;
3238                 return -1;
3239         }
3240
3241         g_dbus = g_pd->g_dbus;
3242         if (!g_dbus) {
3243                 WDP_LOGE("DBus connection is NULL");
3244                 __WDP_LOG_FUNC_EXIT__;
3245                 return -1;
3246         }
3247         memset(&params, 0x0, sizeof(dbus_method_param_s));
3248
3249         dbus_set_method_param(&params, "ProvisionDiscoveryRequest", g_pd->iface_path, g_dbus);
3250
3251         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3252                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3253         WDP_LOGD("get peer path [%s]", peer_path);
3254
3255         value = g_variant_new ("(os)", peer_path, __ws_wps_to_txt(wps_mode));
3256
3257         params.params = value;
3258 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3259         DEBUG_PARAMS(params.params);
3260 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3261
3262         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3263         if (res < 0)
3264                 WDP_LOGE("Failed to send command to wpa_supplicant");
3265         else
3266                 WDP_LOGD("Succeeded to send prov disc to peer[" MACSTR "]", MAC2STR(peer_addr));
3267
3268         __WDP_LOG_FUNC_EXIT__;
3269         return res;
3270 }
3271
3272 int ws_connect(unsigned char *peer_addr, wfd_oem_conn_param_s *param)
3273 {
3274         __WDP_LOG_FUNC_ENTER__;
3275         GDBusConnection *g_dbus = NULL;
3276         GVariantBuilder *builder = NULL;
3277         GVariant *value = NULL;
3278         dbus_method_param_s params;
3279         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3280         int res = 0;
3281
3282         if (!peer_addr || !param) {
3283                 WDP_LOGE("Invalid parameter");
3284                 __WDP_LOG_FUNC_EXIT__;
3285                 return -1;
3286         }
3287
3288         if (!g_pd) {
3289                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3290                 __WDP_LOG_FUNC_EXIT__;
3291                 return -1;
3292         }
3293
3294         g_dbus = g_pd->g_dbus;
3295         if (!g_dbus) {
3296                 WDP_LOGE("DBus connection is NULL");
3297                 __WDP_LOG_FUNC_EXIT__;
3298                 return -1;
3299         }
3300         memset(&params, 0x0, sizeof(dbus_method_param_s));
3301
3302         dbus_set_method_param(&params, "Connect", g_pd->iface_path, g_dbus);
3303
3304         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3305                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3306         WDP_LOGD("get peer path [%s]", peer_path);
3307
3308         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3309         g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
3310         if (param->conn_flags & WFD_OEM_CONN_TYPE_PERSISTENT)
3311                 g_variant_builder_add (builder, "{sv}", "persistent", g_variant_new_boolean(TRUE));
3312
3313         if (param->conn_flags & WFD_OEM_CONN_TYPE_JOIN)
3314                 g_variant_builder_add (builder, "{sv}", "join", g_variant_new_boolean(TRUE));
3315
3316         if (param->conn_flags& WFD_OEM_CONN_TYPE_AUTH)
3317                 g_variant_builder_add (builder, "{sv}", "autorize_only", g_variant_new_boolean(TRUE));
3318
3319         if (param->wps_pin[0] != '\0')
3320                 g_variant_builder_add (builder, "{sv}", "pin", g_variant_new_string(param->wps_pin));
3321
3322         g_variant_builder_add (builder, "{sv}", "wps_method",
3323                                 g_variant_new_string(__ws_wps_to_txt(param->wps_mode)));
3324
3325         value = g_variant_new ("(a{sv})", builder);
3326         g_variant_builder_unref (builder);
3327
3328         params.params = value;
3329 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3330         DEBUG_PARAMS(params.params);
3331 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3332
3333         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3334         if (res < 0)
3335                 WDP_LOGE("Failed to send command to wpa_supplicant");
3336         else
3337                 WDP_LOGD("Succeeded to send connection command to peer[" MACSTR "]", MAC2STR(peer_addr));
3338
3339         __WDP_LOG_FUNC_EXIT__;
3340         return res;
3341 }
3342
3343 int ws_disconnect(unsigned char *peer_addr)
3344 {
3345         __WDP_LOG_FUNC_ENTER__;
3346         GDBusConnection *g_dbus = NULL;
3347         GVariant *value = NULL;
3348         dbus_method_param_s params;
3349         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3350         int res = 0;
3351
3352         if (!peer_addr) {
3353                 WDP_LOGE("Invalid parameter");
3354                 return -1;
3355         }
3356
3357         if (!g_pd) {
3358                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3359                 return -1;
3360         }
3361
3362         g_dbus = g_pd->g_dbus;
3363         if (!g_dbus) {
3364                 WDP_LOGE("DBus connection is NULL");
3365                 return -1;
3366         }
3367         memset(&params, 0x0, sizeof(dbus_method_param_s));
3368
3369         dbus_set_method_param(&params, "RemoveClient", g_pd->iface_path ,g_dbus);
3370
3371         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3372                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3373         WDP_LOGE("get peer path [%s]", peer_path);
3374
3375         value = g_variant_new ("(oi)", peer_path, 0);
3376
3377         params.params = value;
3378 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3379         DEBUG_PARAMS(params.params);
3380 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3381
3382         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3383         if (res < 0)
3384                 WDP_LOGE("Failed to send command to wpa_supplicant");
3385         else
3386                 WDP_LOGD("Succeeded to stop scan");
3387
3388         __WDP_LOG_FUNC_EXIT__;
3389         return res;
3390 }
3391
3392 int ws_reject_connection(unsigned char *peer_addr)
3393 {
3394         __WDP_LOG_FUNC_ENTER__;
3395         GDBusConnection *g_dbus = NULL;
3396         GVariant *value = NULL;
3397         dbus_method_param_s params;
3398         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3399         int res = 0;
3400
3401         if (!peer_addr) {
3402                 WDP_LOGE("Invalid parameter");
3403                 return -1;
3404         }
3405
3406         if (!g_pd) {
3407                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3408                 return -1;
3409         }
3410
3411         g_dbus = g_pd->g_dbus;
3412         if (!g_dbus) {
3413                 WDP_LOGE("DBus connection is NULL");
3414                 return -1;
3415         }
3416         memset(&params, 0x0, sizeof(dbus_method_param_s));
3417
3418         dbus_set_method_param(&params, "RejectPeer", g_pd->iface_path ,g_dbus);
3419
3420         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3421                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3422         WDP_LOGE("get peer path [%s]", peer_path);
3423
3424         value = g_variant_new ("(o)", peer_path);
3425
3426         params.params = value;
3427 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3428         DEBUG_PARAMS(params.params);
3429 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3430
3431         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3432         if (res < 0)
3433                 WDP_LOGE("Failed to send command to wpa_supplicant");
3434         else
3435                 WDP_LOGD("Succeeded to reject peer[" MACSTR "]", MAC2STR(peer_addr));
3436
3437         __WDP_LOG_FUNC_EXIT__;
3438         return res;
3439 }
3440
3441 int ws_cancel_connection(unsigned char *peer_addr)
3442 {
3443         __WDP_LOG_FUNC_ENTER__;
3444
3445         _ws_cancel();
3446
3447         __WDP_LOG_FUNC_EXIT__;
3448         return 0;
3449 }
3450
3451 int ws_get_connected_peers(GList **peers, int *peer_count)
3452 {
3453         __WDP_LOG_FUNC_ENTER__;
3454
3455         __WDP_LOG_FUNC_EXIT__;
3456         return 0;
3457 }
3458
3459 int ws_get_pin(char *pin)
3460 {
3461         __WDP_LOG_FUNC_ENTER__;
3462
3463         __WDP_LOG_FUNC_EXIT__;
3464         return 0;
3465 }
3466
3467 int ws_set_pin(char *pin)
3468 {
3469         __WDP_LOG_FUNC_ENTER__;
3470
3471         __WDP_LOG_FUNC_EXIT__;
3472         return 0;
3473 }
3474
3475 static void __ws_get_pin(GVariant *value, void *user_data)
3476 {
3477         __WDP_LOG_FUNC_ENTER__;
3478         const char *pin = NULL;
3479
3480         g_variant_get(value, "(&s)", &pin);
3481         g_strlcpy((char *)user_data, pin, OEM_PINSTR_LEN + 1);
3482
3483         __WDP_LOG_FUNC_EXIT__;
3484         return;
3485 }
3486
3487 int ws_generate_pin(char **pin)
3488 {
3489         __WDP_LOG_FUNC_ENTER__;
3490         GDBusConnection *g_dbus = NULL;
3491         dbus_method_param_s params;
3492         char n_pin[9] = {0,};
3493         int res = 0;
3494
3495         if (!g_pd) {
3496                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3497                 return -1;
3498         }
3499
3500         g_dbus = g_pd->g_dbus;
3501         if (!g_dbus) {
3502                 WDP_LOGE("DBus connection is NULL");
3503                 return -1;
3504         }
3505         memset(&params, 0x0, sizeof(dbus_method_param_s));
3506
3507         dbus_set_method_param(&params, "GeneratePin", g_pd->iface_path ,g_dbus);
3508         params.params = NULL;
3509
3510         res = dbus_method_call(&params, SUPPLICANT_WPS, __ws_get_pin, (void *)n_pin);
3511         if (res < 0)
3512                 WDP_LOGE("Failed to send command to wpa_supplicant");
3513         else
3514                 WDP_LOGD("Succeeded to generate_pin [ %s ]", n_pin);
3515
3516         *pin = strndup(n_pin, OEM_PINSTR_LEN);
3517         __WDP_LOG_FUNC_EXIT__;
3518         return 0;
3519 }
3520
3521 int ws_get_supported_wps_mode()
3522 {
3523         __WDP_LOG_FUNC_ENTER__;
3524
3525         __WDP_LOG_FUNC_EXIT__;
3526         return 0;
3527 }
3528
3529 int _ws_get_persistent_net_id(int *persistent_network_id, const unsigned char *go_dev_mac)
3530 {
3531         __WDP_LOG_FUNC_ENTER__;
3532         int persistent_group_count = 0;
3533         int counter = 0;
3534         int res = 0;
3535
3536         wfd_oem_persistent_group_s *plist = NULL;
3537
3538         res = ws_get_persistent_groups(&plist, &persistent_group_count);
3539         if (res < 0) {
3540                 WDP_LOGE("failed to get persistent groups");
3541                 __WDP_LOG_FUNC_EXIT__;
3542                 return -1;
3543         }
3544
3545         if (persistent_group_count > WS_MAX_PERSISTENT_COUNT) {
3546                 WDP_LOGE("persistent group count greater than max Persistent count");
3547                 persistent_group_count = WS_MAX_PERSISTENT_COUNT;
3548         }
3549
3550         WDP_LOGD("Persistent Group Count=%d", persistent_group_count);
3551
3552         for (counter = 0; counter < persistent_group_count ; counter++) {
3553                 if(!memcmp(go_dev_mac, plist[counter].go_mac_address, WS_MACADDR_LEN)) {
3554                         *persistent_network_id = plist[counter].network_id;
3555                         break;
3556                 } else {
3557                         WDP_LOGD("Invite: Persistent GO[" MACSTR "], GO Addr[" MACSTR "]",
3558                                         MAC2STR(plist[counter].go_mac_address), MAC2STR(go_dev_mac));
3559                 }
3560         }
3561
3562         g_free(plist);
3563         plist = NULL;
3564         WDP_LOGD("persistent network ID : [%d]", *persistent_network_id);
3565
3566         __WDP_LOG_FUNC_EXIT__;
3567         return 0;
3568 }
3569
3570 static void __store_group_iface_path(GVariant *value, void *user_data) {
3571         __WDP_LOG_FUNC_ENTER__;
3572         ws_dbus_plugin_data_s * pd_data;
3573         const char *path = NULL;
3574
3575         if (!g_pd) {
3576                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3577                 return;
3578         }
3579
3580         pd_data = (ws_dbus_plugin_data_s *) g_pd;
3581
3582         g_variant_get(value, "(&o)", &path);
3583         g_strlcpy(pd_data->group_iface_path, path, DBUS_OBJECT_PATH_MAX);
3584
3585         WDP_LOGD("group object path [%s]", pd_data->group_iface_path);
3586         /* subscribe interface p2p signal */
3587 }
3588
3589 int ws_create_group(wfd_oem_group_param_s *param)
3590 {
3591         __WDP_LOG_FUNC_ENTER__;
3592         GDBusConnection *g_dbus = NULL;
3593         GVariantBuilder *builder = NULL;
3594         GVariant *value = NULL;
3595         dbus_method_param_s params;
3596         char persistent_group_obj_path[OBJECT_PATH_MAX] = {0,};
3597         int res = 0;
3598
3599         if (!param) {
3600                 WDP_LOGE("Invalid parameter");
3601                 __WDP_LOG_FUNC_EXIT__;
3602                 return -1;
3603         }
3604
3605         if (!g_pd) {
3606                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3607                 __WDP_LOG_FUNC_EXIT__;
3608                 return -1;
3609         }
3610
3611         g_dbus = g_pd->g_dbus;
3612         if (!g_dbus) {
3613                 WDP_LOGE("DBus connection is NULL");
3614                 __WDP_LOG_FUNC_EXIT__;
3615                 return -1;
3616         }
3617         memset(&params, 0x0, sizeof(dbus_method_param_s));
3618
3619         dbus_set_method_param(&params, "GroupAdd", g_pd->iface_path, g_dbus);
3620
3621         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3622
3623         if (param->persistent > 0) {
3624                 unsigned char mac_address[WS_MACADDR_LEN] = {0x00, };
3625                 int persistent_group_id = -1;
3626
3627                 res = _ws_get_local_dev_mac(mac_address);
3628                 if (res < 0) {
3629                         WDP_LOGE("failed to get local mac address");
3630                         __WDP_LOG_FUNC_EXIT__;
3631                         return -1;
3632                 }
3633
3634                 res = _ws_get_persistent_net_id(&persistent_group_id, mac_address);
3635                 if(res < 0) {
3636                         WDP_LOGE("failed to get persistent group ID");
3637                         __WDP_LOG_FUNC_EXIT__;
3638                         return -1;
3639                 }
3640
3641                 WDP_LOGD("persistent network ID : [%d]", persistent_group_id);
3642
3643                 g_variant_builder_add(builder, "{sv}", "persistent",
3644                                 g_variant_new_boolean(TRUE));
3645                 if(persistent_group_id > -1) {
3646                         g_snprintf(persistent_group_obj_path, OBJECT_PATH_MAX,
3647                                         "%s/" SUPPLICANT_PERSISTENT_GROUPS_PART "/%d",
3648                                         g_pd->iface_path, persistent_group_id);
3649                         g_variant_builder_add(builder, "{sv}", "persistent_group_object",
3650                                         g_variant_new_object_path(persistent_group_obj_path));
3651                 }
3652
3653         } else {
3654                 g_variant_builder_add(builder, "{sv}", "persistent",
3655                                 g_variant_new_boolean(FALSE));
3656         }
3657
3658         if (param->passphrase && strlen(param->passphrase) > 0)
3659                 g_variant_builder_add(builder, "{sv}", "passphrase",
3660                                 g_variant_new_string(param->passphrase));
3661
3662         if (param->freq)
3663                 g_variant_builder_add(builder, "{sv}", "frequency",
3664                                 g_variant_new_int32(param->freq));
3665
3666         value = g_variant_new ("(a{sv})", builder);
3667         g_variant_builder_unref (builder);
3668
3669         params.params = value;
3670 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3671         DEBUG_PARAMS(params.params);
3672 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3673
3674         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE,
3675                         __store_group_iface_path, g_pd);
3676         if (res < 0)
3677                 WDP_LOGE("Failed to send command to wpa_supplicant");
3678         else
3679                 WDP_LOGD("Succeeded to add group");
3680
3681         __WDP_LOG_FUNC_EXIT__;
3682         return res;
3683 }
3684
3685 int ws_destroy_group(const char *ifname)
3686 {
3687         __WDP_LOG_FUNC_ENTER__;
3688         GDBusConnection *g_dbus = NULL;
3689         dbus_method_param_s params;
3690         int res = 0;
3691
3692         if (!ifname) {
3693                 WDP_LOGE("Invalid parameter");
3694                 return -1;
3695         }
3696
3697         if (!g_pd) {
3698                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3699                 return -1;
3700         }
3701
3702         g_dbus = g_pd->g_dbus;
3703         if (!g_dbus) {
3704                 WDP_LOGE("DBus connection is NULL");
3705                 return -1;
3706         }
3707
3708         if (g_pd->group_iface_path[0] == 0) {
3709                 WDP_LOGE("group iface path is NULL");
3710                 return -1;
3711         }
3712
3713         memset(&params, 0x0, sizeof(dbus_method_param_s));
3714
3715         dbus_set_method_param(&params, "Disconnect", g_pd->group_iface_path, g_dbus);
3716         params.params = NULL;
3717
3718         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3719         if (res < 0) {
3720                 WDP_LOGE("Failed to send command to wpa_supplicant");
3721                 __WDP_LOG_FUNC_EXIT__;
3722                 return -1;
3723         } else {
3724                 _ws_flush();
3725                 WDP_LOGD("Succeeded to remove group");
3726         }
3727
3728         __WDP_LOG_FUNC_EXIT__;
3729         return 0;
3730 }
3731
3732 int ws_invite(unsigned char *peer_addr, wfd_oem_invite_param_s *param)
3733 {
3734         __WDP_LOG_FUNC_ENTER__;
3735         GDBusConnection *g_dbus = NULL;
3736         GVariantBuilder *builder = NULL;
3737         GVariant *value = NULL;
3738         dbus_method_param_s params;
3739         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
3740         int res = 0;
3741
3742         if (!peer_addr || !param) {
3743                 WDP_LOGE("Invalid parameter");
3744                 return -1;
3745         }
3746
3747         if (!g_pd) {
3748                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3749                 return -1;
3750         }
3751
3752         g_dbus = g_pd->g_dbus;
3753         if (!g_dbus) {
3754                 WDP_LOGE("DBus connection is NULL");
3755                 return -1;
3756         }
3757         memset(&params, 0x0, sizeof(dbus_method_param_s));
3758
3759         dbus_set_method_param(&params, "Invite", g_pd->group_iface_path, g_dbus);
3760
3761         g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
3762                         COMPACT_MACSTR, g_pd->iface_path, MAC2STR(peer_addr));
3763         WDP_LOGE("get peer path [%s]", peer_path);
3764
3765         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3766         g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
3767         value = g_variant_new ("(a{sv})", builder);
3768         g_variant_builder_unref (builder);
3769
3770         params.params = value;
3771 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3772         DEBUG_PARAMS(params.params);
3773 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3774
3775         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
3776         if (res < 0)
3777                 WDP_LOGE("Failed to send command to wpa_supplicant");
3778         else
3779                 WDP_LOGD("Succeeded to invite peer[" MACSTR "]", MAC2STR(peer_addr));
3780
3781         __WDP_LOG_FUNC_EXIT__;
3782         return 0;
3783 }
3784
3785 // Only group owner can use this command
3786 int ws_wps_start(unsigned char *peer_addr, int wps_mode, const char *pin)
3787 {
3788         __WDP_LOG_FUNC_ENTER__;
3789         GDBusConnection *g_dbus = NULL;
3790         GVariantBuilder *builder = NULL;
3791         GVariant *value = NULL;
3792         GVariant *dev_addr = NULL;
3793         dbus_method_param_s params;
3794         int i = 0;
3795         int res = 0;
3796
3797         if (!g_pd) {
3798                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3799                 return -1;
3800         }
3801
3802         g_dbus = g_pd->g_dbus;
3803         if (!g_dbus) {
3804                 WDP_LOGE("DBus connection is NULL");
3805                 return -1;
3806         }
3807
3808         memset(&params, 0x0, sizeof(dbus_method_param_s));
3809
3810         dbus_set_method_param(&params, "Start", g_pd->group_iface_path, g_dbus);
3811
3812         if (peer_addr != NULL) {
3813                 builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
3814                 for(i = 0; i < WS_MACADDR_LEN; i++)
3815                         g_variant_builder_add(builder, "y", peer_addr[i]);
3816
3817                 dev_addr = g_variant_new ("ay", builder);
3818                 g_variant_builder_unref (builder);
3819         }
3820
3821         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
3822         g_variant_builder_add (builder, "{sv}", "Role", g_variant_new_string("enrollee"));
3823         if (peer_addr != NULL)
3824                 g_variant_builder_add (builder, "{sv}", "P2PDeviceAddress", dev_addr);
3825
3826         if (pin != NULL && pin[0] != '\0') {
3827                 g_variant_builder_add (builder, "{sv}", "Type", g_variant_new_string("pin"));
3828                 g_variant_builder_add (builder, "{sv}", "Pin", g_variant_new_string(pin));
3829         } else {
3830                 g_variant_builder_add (builder, "{sv}", "Type", g_variant_new_string("pbc"));
3831         }
3832
3833         value = g_variant_new ("(a{sv})", builder);
3834         g_variant_builder_unref (builder);
3835
3836         params.params = value;
3837 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3838         DEBUG_PARAMS(params.params);
3839 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3840
3841         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
3842         if (res < 0)
3843                 WDP_LOGE("Failed to send command to wpa_supplicant");
3844         else
3845                 WDP_LOGD("Succeeded to run wps");
3846
3847         __WDP_LOG_FUNC_EXIT__;
3848         return 0;
3849 }
3850
3851 int ws_enrollee_start(unsigned char *peer_addr, int wps_mode, const char *pin)
3852 {
3853         __WDP_LOG_FUNC_ENTER__;
3854
3855         WDP_LOGD("Succeeded to start WPS");
3856
3857         __WDP_LOG_FUNC_EXIT__;
3858         return 0;
3859 }
3860
3861 int ws_wps_cancel()
3862 {
3863         __WDP_LOG_FUNC_ENTER__;
3864         GDBusConnection *g_dbus = NULL;
3865         dbus_method_param_s params;
3866         int res = 0;
3867
3868         g_dbus = g_pd->g_dbus;
3869         if (!g_dbus) {
3870                 WDP_LOGE("DBus connection is NULL");
3871                 return -1;
3872         }
3873         memset(&params, 0x0, sizeof(dbus_method_param_s));
3874
3875         dbus_set_method_param(&params, "Cancel", g_pd->group_iface_path, g_dbus);
3876         params.params = NULL;
3877
3878         res = dbus_method_call(&params, SUPPLICANT_WPS, NULL, NULL);
3879         if (res < 0)
3880                 WDP_LOGE("Failed to send command to wpa_supplicant");
3881         else
3882                 WDP_LOGD("Succeeded to cancel WPS");
3883
3884         __WDP_LOG_FUNC_EXIT__;
3885         return 0;
3886 }
3887
3888 int ws_get_dev_name(char *dev_name)
3889 {
3890         __WDP_LOG_FUNC_ENTER__;
3891
3892         __WDP_LOG_FUNC_EXIT__;
3893         return 0;
3894 }
3895
3896 int ws_set_dev_name(char *dev_name)
3897 {
3898         __WDP_LOG_FUNC_ENTER__;
3899         GDBusConnection *g_dbus = NULL;
3900
3901         GVariant *value = NULL;
3902         GVariant *param = NULL;
3903         GVariantBuilder *builder = NULL;
3904         dbus_method_param_s params;
3905         int res = 0;
3906
3907         if (!dev_name) {
3908                 WDP_LOGE("Invalid parameter");
3909                 return -1;
3910         }
3911
3912         if (!g_pd) {
3913                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3914                 return -1;
3915         }
3916
3917         g_dbus = g_pd->g_dbus;
3918         if (!g_dbus) {
3919                 WDP_LOGE("DBus connection is NULL");
3920                 return -1;
3921         }
3922         memset(&params, 0x0, sizeof(dbus_method_param_s));
3923
3924         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
3925                          g_dbus);
3926
3927         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
3928         g_variant_builder_add (builder, "{sv}", "DeviceName",
3929                                 g_variant_new_string(dev_name));
3930         g_variant_builder_add (builder, "{sv}", "SsidPostfix",
3931                                  g_variant_new_string(dev_name));
3932         value = g_variant_new ("a{sv}", builder);
3933         g_variant_builder_unref (builder);
3934
3935         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE,
3936                                 "P2PDeviceConfig", value);
3937
3938         params.params = param;
3939 #if defined (TIZEN_DEBUG_DBUS_VALUE)
3940         DEBUG_PARAMS(params.params);
3941 #endif /* TIZEN_DEBUG_DBUS_VALUE */
3942
3943         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
3944         if (res < 0)
3945                 WDP_LOGE("Failed to send command to wpa_supplicant");
3946         else
3947                 WDP_LOGD("Succeeded to set device name");
3948
3949         __WDP_LOG_FUNC_EXIT__;
3950         return res;
3951 }
3952
3953 int ws_get_dev_mac(char *dev_mac)
3954 {
3955         __WDP_LOG_FUNC_ENTER__;
3956
3957         __WDP_LOG_FUNC_EXIT__;
3958         return 0;
3959 }
3960
3961 int ws_get_dev_type(int *pri_dev_type, int *sec_dev_type)
3962 {
3963         __WDP_LOG_FUNC_ENTER__;
3964
3965         __WDP_LOG_FUNC_EXIT__;
3966         return 0;
3967 }
3968
3969 int ws_set_dev_type(int pri_dev_type, int sec_dev_type)
3970 {
3971         __WDP_LOG_FUNC_ENTER__;
3972
3973         __WDP_LOG_FUNC_EXIT__;
3974         return 0;
3975 }
3976
3977 int ws_get_go_intent(int *go_intent)
3978 {
3979         __WDP_LOG_FUNC_ENTER__;
3980         GDBusConnection *g_dbus = NULL;
3981         GVariant *param = NULL;
3982         GVariant *reply = NULL;
3983         GError *error = NULL;
3984         GVariantIter *iter = NULL;
3985
3986
3987         if (!go_intent) {
3988                 WDP_LOGE("Invalid parameter");
3989                 return -1;
3990         }
3991
3992         if (!g_pd) {
3993                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
3994                 return -1;
3995         }
3996
3997         g_dbus = g_pd->g_dbus;
3998         if (!g_dbus) {
3999                 WDP_LOGE("DBus connection is NULL");
4000                 return -1;
4001         }
4002
4003         param = g_variant_new("(ss)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig");
4004 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4005         DEBUG_PARAMS(param);
4006 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4007
4008         reply = g_dbus_connection_call_sync (
4009                         g_dbus,
4010                         SUPPLICANT_SERVICE, /* bus name */
4011                         g_pd->iface_path, /* object path */
4012                         DBUS_PROPERTIES_INTERFACE, /* interface name */
4013                         DBUS_PROPERTIES_METHOD_GET, /* method name */
4014                         param, /* GVariant *params */
4015                         NULL, /* reply_type */
4016                         G_DBUS_CALL_FLAGS_NONE, /* flags */
4017                         SUPPLICANT_TIMEOUT , /* timeout */
4018                         NULL, /* cancellable */
4019                         &error); /* error */
4020
4021         if(error != NULL) {
4022                 WDP_LOGE("Error! Failed to get interface State: [%s]",
4023                                 error->message);
4024                 g_error_free(error);
4025                 if(reply)
4026                         g_variant_unref(reply);
4027                 __WDP_LOG_FUNC_EXIT__;
4028                 return -1;
4029         }
4030
4031         if (reply != NULL) {
4032                 g_variant_get(reply, "(a{sv})", &iter);
4033
4034                 if (iter != NULL) {
4035                         gchar *key = NULL;
4036                         GVariant *value = NULL;
4037
4038                         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
4039 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4040                                 CHECK_KEY_VALUE(key, value);
4041 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4042                                 if(g_strcmp0(key, "GOIntent") == 0)
4043                                         g_variant_get(value, "u", go_intent);
4044                         }
4045                         g_variant_iter_free(iter);
4046                 }
4047                 g_variant_unref(reply);
4048         }
4049         __WDP_LOG_FUNC_EXIT__;
4050         return 0;
4051 }
4052
4053 int ws_set_go_intent(int go_intent)
4054 {
4055         __WDP_LOG_FUNC_ENTER__;
4056         GDBusConnection *g_dbus = NULL;
4057
4058         GVariant *value = NULL;
4059         GVariant *param = NULL;
4060         GVariantBuilder *builder = NULL;
4061         dbus_method_param_s params;
4062         int res = 0;
4063
4064         if (!g_pd) {
4065                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4066                 return -1;
4067         }
4068
4069         g_dbus = g_pd->g_dbus;
4070         if (!g_dbus) {
4071                 WDP_LOGE("DBus connection is NULL");
4072                 return -1;
4073         }
4074         memset(&params, 0x0, sizeof(dbus_method_param_s));
4075
4076         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4077                          g_dbus);
4078
4079         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
4080         g_variant_builder_add (builder, "{sv}", "GOIntent",
4081                                 g_variant_new_uint32(go_intent));
4082         value = g_variant_new ("a{sv}", builder);
4083         g_variant_builder_unref (builder);
4084
4085         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
4086
4087         params.params = param;
4088 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4089         DEBUG_PARAMS(params.params);
4090 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4091
4092         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
4093         if (res < 0)
4094                 WDP_LOGE("Failed to send command to wpa_supplicant");
4095         else
4096                 WDP_LOGE("Succeeded to set go intent");
4097         __WDP_LOG_FUNC_EXIT__;
4098         return res;
4099 }
4100
4101 int ws_set_country(char *ccode)
4102 {
4103         __WDP_LOG_FUNC_ENTER__;
4104         __WDP_LOG_FUNC_ENTER__;
4105         GDBusConnection *g_dbus = NULL;
4106
4107         GVariant *value = NULL;
4108         GVariant *param = NULL;
4109
4110         dbus_method_param_s params;
4111         int res = 0;
4112
4113         if (!g_pd) {
4114                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4115                 return -1;
4116         }
4117
4118         g_dbus = g_pd->g_dbus;
4119         if (!g_dbus) {
4120                 WDP_LOGE("DBus connection is NULL");
4121                 return -1;
4122         }
4123         memset(&params, 0x0, sizeof(dbus_method_param_s));
4124
4125         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4126                          g_dbus);
4127
4128         value = g_variant_new_string(ccode);
4129
4130         param = g_variant_new("(ssv)", SUPPLICANT_IFACE, "Country", value);
4131
4132         params.params = param;
4133 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4134         DEBUG_PARAMS(params.params);
4135 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4136
4137         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
4138         if (res < 0)
4139                 WDP_LOGE("Failed to send command to wpa_supplicant");
4140         else
4141                 WDP_LOGD("Succeeded to set country(%s)", ccode);
4142
4143         __WDP_LOG_FUNC_EXIT__;
4144         return res;
4145 }
4146
4147 void __parsing_networks (const char *key, GVariant *value, void *user_data)
4148 {
4149         __WDP_LOG_FUNC_ENTER__;
4150         if(!user_data) {
4151                 __WDP_LOG_FUNC_EXIT__;
4152                 return;
4153         }
4154
4155         ws_network_info_s *network = (ws_network_info_s *)user_data;
4156 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4157         CHECK_KEY_VALUE(key, value);
4158 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4159         if (g_strcmp0(key, "ssid") == 0) {
4160                 const char *ssid = NULL;
4161                 g_variant_get(value, "&s", &ssid);
4162                 WDP_LOGD("ssid [%s]", ssid);
4163                 g_strlcpy(network->ssid, ssid + 1, WS_SSID_LEN + 1);
4164                 network->ssid[strlen(ssid) - 2] = '\0';
4165
4166         } else if (g_strcmp0(key, "bssid") == 0) {
4167                 unsigned char *bssid = NULL;
4168                 g_variant_get(value, "&s", &bssid);
4169                 WDP_LOGD("bssid [%s]", bssid);
4170                 __ws_txt_to_mac(bssid, network->bssid);
4171
4172         } else if (g_strcmp0(key, "proto") == 0) {
4173                 const char *proto = NULL;
4174                 g_variant_get(value, "&s", &proto);
4175                 WDP_LOGD("proto [%s]", proto);
4176
4177                 if (g_strrstr(proto, WFD_OEM_STR_PROTO_WPA) != NULL)
4178                         network->proto |= WFD_OEM_PROTO_WPA;
4179                 if (g_strrstr(proto, WFD_OEM_STR_PROTO_RSN) != NULL)
4180                         network->proto |= WFD_OEM_PROTO_RSN;
4181
4182         } else if (g_strcmp0(key, "key_mgmt") == 0) {
4183                 const char *key_mgmt = NULL;
4184                 g_variant_get(value, "&s", &key_mgmt);
4185                 WDP_LOGD("key_mgmt [%s]", key_mgmt);
4186
4187                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_IEEE8021X) != NULL)
4188                         network->key_mgmt |= WFD_OEM_KEY_MGMT_IEEE8021X;
4189                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_PSK) != NULL)
4190                         network->key_mgmt |= WFD_OEM_KEY_MGMT_PSK;
4191                 if (g_strrstr(key_mgmt, WFD_OEM_STR_KEY_MGMT_NONE) != NULL)
4192                         network->key_mgmt |= WFD_OEM_KEY_MGMT_NONE;
4193
4194         } else if (g_strcmp0(key, "pairwise") == 0) {
4195                 const char *pairwise = NULL;
4196                 g_variant_get(value, "&s", &pairwise);
4197                 WDP_LOGD("pairwise [%s]", pairwise);
4198
4199                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_NONE) != NULL)
4200                         network->pairwise |= WFD_OEM_CIPHER_NONE;
4201                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_TKIP) != NULL)
4202                         network->pairwise |= WFD_OEM_CIPHER_TKIP;
4203                 if (g_strrstr(pairwise, WFD_OEM_STR_CIPHER_CCMP) != NULL)
4204                         network->pairwise |= WFD_OEM_CIPHER_CCMP;
4205
4206         }  else if (g_strcmp0(key, "group") == 0) {
4207                 const char *group = NULL;
4208                 g_variant_get(value, "&s", &group);
4209                 WDP_LOGD("group [%s]", group);
4210
4211                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_NONE) != NULL)
4212                         network->group |= WFD_OEM_CIPHER_NONE;
4213                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_WEP40) != NULL)
4214                         network->group |= WFD_OEM_CIPHER_WEP40;
4215                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_WEP104) != NULL)
4216                         network->group |= WFD_OEM_CIPHER_WEP104;
4217                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_TKIP) != NULL)
4218                         network->group |= WFD_OEM_CIPHER_TKIP;
4219                 if (g_strrstr(group, WFD_OEM_STR_CIPHER_CCMP) != NULL)
4220                         network->group |= WFD_OEM_CIPHER_CCMP;
4221
4222         } else if (g_strcmp0(key, "auth_alg") == 0) {
4223                 const char *auth_alg = NULL;
4224                 g_variant_get(value, "&s", &auth_alg);
4225                 WDP_LOGD("auth_alg [%s]", auth_alg);
4226
4227                 if (g_strrstr(auth_alg, WFD_OEM_STR_AUTH_ALG_OPEN) != NULL)
4228                         network->auth_alg |= WFD_OEM_AUTH_ALG_OPEN;
4229
4230         } else if (g_strcmp0(key, "mode") == 0) {
4231                 const char *mode = NULL;
4232                 g_variant_get(value, "&s", &mode);
4233                 WDP_LOGD("mode [%s]", mode);
4234
4235                 if (g_strrstr(mode, WFD_OEM_STR_MODE_GC) != NULL)
4236                         network->mode |= WFD_OEM_PERSISTENT_MODE_GC;
4237                 if (g_strrstr(mode, WFD_OEM_STR_MODE_GO) != NULL)
4238                         network->mode |= WFD_OEM_PERSISTENT_MODE_GO;
4239
4240         } else if (g_strcmp0(key, "p2p_client_list") == 0) {
4241                 const char *p2p_client_list = NULL;
4242                 char *ptr = NULL;
4243                 int list_len = 0;
4244                 int num = 0;
4245
4246                 g_variant_get(value, "&s", &p2p_client_list);
4247                 WDP_LOGD("p2p_client_list [%s]", p2p_client_list);
4248                 ptr = (char *)p2p_client_list;
4249                 while(ptr != NULL && list_len >= (OEM_MACSTR_LEN - 1)) {
4250                         __ws_txt_to_mac((unsigned char *)ptr, &(network->p2p_client_list[0][num]));
4251                         ptr += OEM_MACSTR_LEN;
4252                         list_len -= OEM_MACSTR_LEN;
4253                         num++;
4254                 }
4255                 network->p2p_client_num = num;
4256         }
4257         return;
4258 }
4259
4260 void __ws_extract_p2pdevice_details(const char *key, GVariant *value, void *user_data)
4261 {
4262
4263         __WDP_LOG_FUNC_ENTER__;
4264 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4265         CHECK_KEY_VALUE(key, value);
4266 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4267         if (g_strcmp0(key, "PersistentGroups") == 0) {
4268                 GVariantIter *iter = NULL;
4269                 const char *path = NULL;
4270                 int num = 0;
4271
4272                 ws_network_info_s *networks = NULL;
4273                 networks = (ws_network_info_s *)user_data;
4274                 if(!networks) {
4275                         WDP_LOGE("network is NULL");
4276                         __WDP_LOG_FUNC_EXIT__;
4277                         return;
4278                 }
4279
4280                 g_variant_get(value, "ao", &iter);
4281                 while(g_variant_iter_loop(iter, "&o", &path)) {
4282                         char *loc = NULL;
4283
4284                         if(num >= WS_MAX_PERSISTENT_COUNT)
4285                                 break;
4286
4287                         WDP_LOGD("Retrive persistent path [%s]", path);
4288                         g_strlcpy(networks[num].persistent_path, path, DBUS_OBJECT_PATH_MAX);
4289
4290                         loc = strrchr(networks[num].persistent_path, '/');
4291                         networks[num].network_id = strtoul(loc+1, NULL, 10);
4292
4293                         WDP_LOGD("Retrive persistent path [%s]", networks[num].persistent_path);
4294                         dbus_property_get_all(networks[num].persistent_path, g_pd->g_dbus,
4295                                         SUPPLICANT_P2P_PERSISTENTGROUP, __parsing_networks, &networks[num]);
4296                         num++;
4297                 }
4298
4299                 networks[0].total = num;
4300                 WDP_LOGE("total number [%d]", num);
4301                 g_variant_iter_free(iter);
4302         }
4303         __WDP_LOG_FUNC_EXIT__;
4304 }
4305
4306
4307 int ws_get_persistent_groups(wfd_oem_persistent_group_s **groups, int *group_count)
4308 {
4309         __WDP_LOG_FUNC_ENTER__;
4310         GDBusConnection *g_dbus = NULL;
4311
4312         ws_network_info_s networks[WS_MAX_PERSISTENT_COUNT];
4313         wfd_oem_persistent_group_s *wfd_persistent_groups = NULL;
4314         int i, cnt = 0;
4315
4316         if (!g_pd) {
4317                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4318                 return -1;
4319         }
4320
4321         g_dbus = g_pd->g_dbus;
4322         if (!g_dbus) {
4323                 WDP_LOGE("DBus connection is NULL");
4324                 return -1;
4325         }
4326         dbus_property_get_all(g_pd->iface_path, g_dbus, SUPPLICANT_P2PDEVICE, __ws_extract_p2pdevice_details, &networks[0]);
4327
4328         cnt = networks[0].total;
4329
4330         WDP_LOGD("Persistent Group Count=%d", cnt);
4331         if (cnt > WS_MAX_PERSISTENT_COUNT) {
4332                 WDP_LOGE("Persistent group count exceeded or parsing error");
4333                 __WDP_LOG_FUNC_EXIT__;
4334                 return -1;
4335         }
4336
4337         if(cnt == 0) {
4338                 WDP_LOGE("Persistent group count zero");
4339                 *group_count = 0;
4340                 *groups = NULL;
4341                 __WDP_LOG_FUNC_EXIT__;
4342                 return 0;
4343         }
4344
4345         wfd_persistent_groups = (wfd_oem_persistent_group_s*) g_try_malloc0(cnt * sizeof(wfd_oem_persistent_group_s));
4346         if (wfd_persistent_groups == NULL) {
4347                 WDP_LOGE("Failed to allocate memory for wfd_persistent_groups ");
4348                 return -1;
4349         }
4350
4351         for(i = 0; i < cnt; i++) {
4352                 WDP_LOGD("----persistent group [%d]----", i);
4353                 WDP_LOGD("network_id=%d", networks[i].network_id);
4354                 WDP_LOGD("ssid=%s", networks[i].ssid);
4355                 WDP_LOGD("bssid=" MACSTR, MAC2STR(networks[i].bssid));
4356
4357                 wfd_persistent_groups[i].network_id = networks[i].network_id;
4358                 g_strlcpy(wfd_persistent_groups[i].ssid, networks[i].ssid, WS_SSID_LEN + 1);
4359                 memcpy(wfd_persistent_groups[i].go_mac_address, networks[i].bssid, WS_MACADDR_LEN);
4360         }
4361
4362         *group_count = cnt;
4363         *groups = wfd_persistent_groups;
4364
4365         __WDP_LOG_FUNC_EXIT__;
4366         return 0;
4367 }
4368
4369 int ws_remove_persistent_group(char *ssid, unsigned char *bssid)
4370 {
4371         __WDP_LOG_FUNC_ENTER__;
4372         GDBusConnection *g_dbus = NULL;
4373
4374         dbus_method_param_s params;
4375         ws_network_info_s networks[WS_MAX_PERSISTENT_COUNT];
4376         int i, cnt = 0;
4377         int res = 0;
4378
4379         if (!g_pd) {
4380                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4381                 return -1;
4382         }
4383
4384         g_dbus = g_pd->g_dbus;
4385         if (!g_dbus) {
4386                 WDP_LOGE("DBus connection is NULL");
4387                 return -1;
4388         }
4389         dbus_property_get_all(g_pd->iface_path, g_dbus, SUPPLICANT_P2PDEVICE, __ws_extract_p2pdevice_details, networks);
4390
4391         cnt = networks[0].total;
4392
4393         WDP_LOGD("Persistent Group Count=%d", cnt);
4394         if (cnt > WS_MAX_PERSISTENT_COUNT) {
4395                 WDP_LOGE("Persistent group count exceeded or parsing error");
4396                 __WDP_LOG_FUNC_EXIT__;
4397                 return -1;
4398         }
4399
4400         for(i=0;i<cnt;i++) {
4401                 WDP_LOGD("----persistent group [%d]----", i);
4402                 WDP_LOGD("network_id=%d", networks[i].network_id);
4403                 WDP_LOGD("network ssid=%s", networks[i].ssid);
4404                 WDP_LOGD("network bssid=" MACSTR, MAC2STR(networks[i].bssid));
4405
4406                 WDP_LOGD("ssid=%s", ssid);
4407                 WDP_LOGD("bssid=" MACSTR, MAC2STR(bssid));
4408
4409
4410                 if (!memcmp(bssid, networks[i].bssid, WS_MACADDR_LEN) &&
4411                                 !strcmp(ssid, networks[i].ssid)) {
4412
4413                         WDP_LOGD("Persistent group found [%d: %s]", networks[i].network_id, ssid);
4414
4415                         memset(&params, 0x0, sizeof(dbus_method_param_s));
4416                         dbus_set_method_param(&params, "RemovePersistentGroup",
4417                                         g_pd->iface_path, g_dbus);
4418                         params.params = g_variant_new ("(o)", networks[i].persistent_path);
4419 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4420                         DEBUG_PARAMS(params.params);
4421 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4422
4423                         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4424                         if (res < 0) {
4425                                 WDP_LOGE("Failed to send command to wpa_supplicant");
4426                                 __WDP_LOG_FUNC_EXIT__;
4427                                 return -1;
4428                         }
4429                         WDP_LOGD("Succeeded to remove persistent group");;
4430                         break;
4431                 }
4432         }
4433
4434         if (i == cnt) {
4435                 WDP_LOGE("Persistent group not found [%s]", ssid);
4436                 return -1;
4437         }
4438
4439         __WDP_LOG_FUNC_EXIT__;
4440         return 0;
4441 }
4442
4443 int ws_set_persistent_reconnect(unsigned char *bssid, int reconnect)
4444 {
4445         __WDP_LOG_FUNC_ENTER__;
4446         GDBusConnection *g_dbus = NULL;
4447
4448         GVariant *value = NULL;
4449         GVariant *param = NULL;
4450         GVariantBuilder *builder = NULL;
4451         dbus_method_param_s params;
4452         int res = 0;
4453
4454         if (!g_pd) {
4455                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4456                 return -1;
4457         }
4458
4459         g_dbus = g_pd->g_dbus;
4460         if (!g_dbus) {
4461                 WDP_LOGE("DBus connection is NULL");
4462                 return -1;
4463         }
4464         memset(&params, 0x0, sizeof(dbus_method_param_s));
4465
4466         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, g_pd->iface_path,
4467                         g_dbus);
4468
4469         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
4470         g_variant_builder_add (builder, "{sv}", "PersistentReconnect",
4471                                 g_variant_new_boolean(reconnect));
4472         value = g_variant_new ("a{sv}", builder);
4473         g_variant_builder_unref (builder);
4474
4475         param = g_variant_new("(ssv)", SUPPLICANT_P2PDEVICE, "P2PDeviceConfig", value);
4476
4477         params.params = param;
4478 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4479         DEBUG_PARAMS(params.params);
4480 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4481
4482         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
4483         if (res < 0)
4484                 WDP_LOGE("Failed to send command to wpa_supplicant");
4485         else
4486                 WDP_LOGD("Succeeded to set persistent reconnect");
4487
4488         __WDP_LOG_FUNC_EXIT__;
4489         return res;
4490 }
4491
4492 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
4493 static int __ws_compress_query(char *compressed, char *query, int qtype)
4494 {
4495         char *token = NULL;
4496         char *temp = NULL;
4497         int token_num = 0;
4498         int token_len = 0;
4499         int length = 0;
4500
4501         token = strtok_r(query, ".", &temp);
4502         while (token) {
4503                 if (!strcmp(token, "local")) {
4504                         WDP_LOGD("Query conversion done");
4505                         break;
4506
4507                 } else if (!strncmp(token, "_tcp", 4)) {
4508                         memcpy(&compressed[length], WS_TCP_PTR_HEX, 2);
4509                         length+=2;
4510
4511                 } else if (!strncmp(token, "_udp", 4)) {
4512                         memcpy(&compressed[length], WS_UDP_PTR_HEX, 2);
4513                         length+=2;
4514
4515                 } else {
4516                         WDP_LOGD("Token: [%s]", token);
4517                         token_len = strlen(token);
4518                         compressed[length] = token_len;
4519                         length++;
4520
4521                         memcpy(&compressed[length], token, token_len);
4522                         length+=token_len;
4523                 }
4524                 token_num++;
4525                 token = strtok_r(NULL, ".", &temp);
4526         }
4527         if(qtype == WS_QTYPE_PTR || token_num == 2) {
4528                 memcpy(&compressed[length], WS_PTR_TYPE_HEX, 3);
4529         } else if (qtype == WS_QTYPE_TXT || token_num == 3) {
4530                 memcpy(&compressed[length], WS_TXT_TYPE_HEX, 3);
4531         }
4532         length+=3;
4533         WDP_LOGD("converted query length [%d] token num [%d]", length, token_num);
4534
4535         return length;
4536 }
4537
4538 static int __ws_compress_rdata(char *compressed, char *rdata, int qtype)
4539 {
4540         char *token = NULL;
4541         char *temp = NULL;
4542         int token_len = 0;
4543         int length = 0;
4544
4545         if (qtype == WS_QTYPE_PTR) {
4546
4547                 token = strtok_r(rdata, ".", &temp);
4548                 if (token) {
4549                         WDP_LOGD("Token: %s", token);
4550                         token_len = strlen(token);
4551                         compressed[length] = token_len;
4552                         length++;
4553
4554                         memcpy(&compressed[length], token, token_len);
4555                         length+=token_len;
4556                 }
4557
4558                 compressed[length] = 0xc0;
4559                 compressed[length+1] = 0x27;
4560                 length+=2;
4561
4562         } else if (qtype == WS_QTYPE_TXT) {
4563
4564                 token = strtok_r(rdata, ",", &temp);
4565
4566                 while (token) {
4567                         WDP_LOGD("Token: [%s]", token);
4568
4569                         token_len = strlen(token);
4570                         compressed[length] = token_len;
4571                         length++;
4572
4573                         memcpy(&compressed[length], token, token_len);
4574                         length+=token_len;
4575
4576                         token = strtok_r(NULL, ",", &temp);
4577                 }
4578         } else {
4579                 WDP_LOGD("RDATA is NULL");
4580         }
4581         return length;
4582 }
4583
4584 int _convert_bonjour_to_args(char *query, char *rdata, GVariantBuilder *builder)
4585 {
4586         GVariantBuilder *args = NULL;
4587         char compressed[256] = {0, };
4588         char *temp = NULL;
4589         int length = 0;
4590         int qtype = 0;
4591         int i = 0;
4592
4593         if (!query || !builder) {
4594                 WDP_LOGE("Invalid parameter");
4595                 return -1;
4596         }
4597         if (!rdata || !strlen(rdata)) {
4598                 WDP_LOGD("RDATA is NULL\n");
4599         } else {
4600                 temp = strstr(rdata, query);
4601
4602                 if(temp != NULL && temp - rdata > 0)
4603                         qtype = WS_QTYPE_PTR;
4604                 else
4605                         qtype = WS_QTYPE_TXT;
4606                 temp = NULL;
4607         }
4608
4609         /* compress query */
4610         length = __ws_compress_query(compressed, query, qtype);
4611
4612         args = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
4613         for (i = 0; i < length; i++)
4614                 g_variant_builder_add(args, "y", compressed[i]);
4615         g_variant_builder_add (builder, "{sv}", "query", g_variant_new ("ay", args));
4616         g_variant_builder_unref (args);
4617
4618         memset(compressed, 0x0, 256);
4619         length = 0;
4620         args = NULL;
4621
4622         if(qtype != 0) {
4623                 length = __ws_compress_rdata(compressed, rdata, qtype);
4624
4625                 args = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
4626                 for (i = 0; i < length; i++)
4627                         g_variant_builder_add(args, "y", compressed[i]);
4628                 g_variant_builder_add (builder, "{sv}", "response", g_variant_new ("ay", args));
4629                 g_variant_builder_unref (args);
4630         }
4631
4632         return 0;
4633 }
4634
4635 int _check_service_query_exists(wfd_oem_service_s *service)
4636 {
4637         int count = 0;
4638         wfd_oem_service_s *data = NULL;
4639
4640         for (count = 0; count < g_list_length(service_list); count ++) {
4641                 data = (wfd_oem_service_s*) g_list_nth_data(service_list, count);
4642                 if (strncmp(service->query_id, data->query_id, OEM_QUERY_ID_LEN) == 0) {
4643                         WDP_LOGD("Query already exists");
4644                         return 1;
4645                 }
4646         }
4647         return 0;
4648 }
4649
4650 static wfd_oem_service_s* _remove_service_query(char * s_type, char *mac_str, char *query_id)
4651 {
4652         if (NULL == s_type || NULL == mac_str || NULL == query_id)
4653                 return NULL;
4654
4655         int count = 0;
4656         wfd_oem_service_s *data = NULL;
4657
4658         for (count = 0; count < g_list_length(service_list); count ++) {
4659                 data = (wfd_oem_service_s*) g_list_nth_data(service_list, count);
4660                 if (data && !strncmp(data->service_type, s_type, SERVICE_TYPE_LEN) &&
4661                                 memcmp(data->dev_addr, mac_str, OEM_MACSTR_LEN - 1) == 0) {
4662                         strncpy(query_id, data->query_id, OEM_QUERY_ID_LEN);
4663                         break;
4664                 }
4665         }
4666         if (strlen(query_id) <= 0) {
4667                 WDP_LOGD("!! Query ID not found !!");
4668                 return NULL;
4669         }
4670
4671         WDP_LOGD("query id :[0x%s]",query_id);
4672
4673         return data;
4674 }
4675
4676 void __add_service_query(GVariant *value, void *mac_addr)
4677 {
4678         __WDP_LOG_FUNC_ENTER__;
4679         wfd_oem_service_s *service = NULL;
4680
4681         long long unsigned ref = 0;
4682         unsigned char *mac_address = (unsigned char *)mac_addr;
4683         char mac_str[18] = {0, };
4684
4685         int res = 0;
4686
4687         g_variant_get(value, "(t)", &ref);
4688
4689         service = (wfd_oem_service_s*) g_try_malloc0(sizeof(wfd_oem_service_s));
4690         if (!service) {
4691                 WDP_LOGE("Failed to allocate memory for service");
4692                 return;
4693         }
4694
4695         if (mac_address[0] == 0 && mac_address[1] == 0 && mac_address[2] == 0 &&
4696                         mac_address[3] == 0 && mac_address[4] == 0 && mac_address[5] == 0) {
4697                 g_snprintf(mac_str, WS_MACSTR_LEN , "%s", SERV_BROADCAST_ADDRESS);
4698         } else {
4699                 g_snprintf(mac_str, WS_MACSTR_LEN, MACSTR, MAC2STR(mac_address));
4700         }
4701
4702         g_strlcpy(service->dev_addr, mac_str, OEM_MACSTR_LEN);
4703         g_snprintf(service->query_id, OEM_QUERY_ID_LEN + 1, "0x%llx", ref);
4704
4705         res = _check_service_query_exists(service);
4706         if(res) {
4707                 free(service);
4708         } else {
4709                 service_list = g_list_append(service_list, service);
4710         }
4711
4712         __WDP_LOG_FUNC_EXIT__;
4713         return;
4714
4715 }
4716
4717 /* for now, supplicant dbus interface only provides upnp service fully */
4718 int ws_start_service_discovery(unsigned char *mac_addr, int service_type)
4719 {
4720         __WDP_LOG_FUNC_ENTER__;
4721         GDBusConnection *g_dbus = NULL;
4722         GVariantBuilder *builder = NULL;
4723         GVariant *value = NULL;
4724         dbus_method_param_s params;
4725         static char peer_path[DBUS_OBJECT_PATH_MAX] = {'\0',};
4726         int i = 0;
4727         int res = 0;
4728
4729         if (!mac_addr ) {
4730                 WDP_LOGE("Invalid parameter");
4731                 __WDP_LOG_FUNC_EXIT__;
4732                 return -1;
4733         }
4734
4735         if (!g_pd) {
4736                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4737                 __WDP_LOG_FUNC_EXIT__;
4738                 return -1;
4739         }
4740
4741         g_dbus = g_pd->g_dbus;
4742         if (!g_dbus) {
4743                 WDP_LOGE("DBus connection is NULL");
4744                 __WDP_LOG_FUNC_EXIT__;
4745                 return -1;
4746         }
4747         memset(&params, 0x0, sizeof(dbus_method_param_s));
4748
4749         dbus_set_method_param(&params, "ServiceDiscoveryRequest", g_pd->iface_path, g_dbus);
4750
4751         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4752
4753         if(mac_addr) {
4754                 g_snprintf(peer_path, DBUS_OBJECT_PATH_MAX, "%s/Peers/"
4755                                 COMPACT_MACSTR, g_pd->iface_path, MAC2STR(mac_addr));
4756                 WDP_LOGD("get peer path [%s]", peer_path);
4757                 g_variant_builder_add (builder, "{sv}", "peer", g_variant_new_object_path(peer_path));
4758         }
4759
4760         if (service_type == WFD_OEM_SERVICE_TYPE_ALL) {
4761
4762                 char *service_all = "\x02\x00\x00\x01";
4763                 GVariantBuilder *query = NULL;
4764
4765                 query = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
4766                 for (i = 0; i < SERVICE_QUERY_LEN; i++)
4767                         g_variant_builder_add(query, "y", service_all[i]);
4768                 g_variant_builder_add (builder, "{sv}", "query", g_variant_new ("ay", query));
4769                 g_variant_builder_unref (query);
4770
4771                 } else if (service_type == WFD_OEM_SERVICE_TYPE_UPNP) {
4772
4773                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
4774                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
4775
4776         } else if (service_type == WFD_OEM_SERVICE_TYPE_BONJOUR) {
4777
4778                 char *service_bonjour = "\x02\x00\x01\x01";
4779                 GVariantBuilder *query = NULL;
4780
4781                 query = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
4782                 for (i = 0; i < SERVICE_QUERY_LEN; i++)
4783                         g_variant_builder_add(query, "y", service_bonjour[i]);
4784                 g_variant_builder_add (builder, "{sv}", "query", g_variant_new ("ay", query));
4785                 g_variant_builder_unref (query);
4786         }
4787
4788         value = g_variant_new ("(a{sv})", builder);
4789         g_variant_builder_unref (builder);
4790
4791         params.params = value;
4792 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4793         DEBUG_PARAMS(params.params);
4794 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4795
4796         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, __add_service_query, mac_addr);
4797         if (res < 0)
4798                 WDP_LOGE("Failed to send command to wpa_supplicant");
4799         else
4800                 WDP_LOGD("Succeeded to start service discovery");
4801
4802         __WDP_LOG_FUNC_EXIT__;
4803         return res;
4804 }
4805
4806 int ws_cancel_service_discovery(unsigned char *mac_addr, int service_type)
4807 {
4808         __WDP_LOG_FUNC_ENTER__;
4809         GDBusConnection *g_dbus = NULL;
4810         dbus_method_param_s params;
4811         wfd_oem_service_s *data = NULL;
4812         char query_id[OEM_QUERY_ID_LEN + 1] = {0, };
4813         char s_type[OEM_SERVICE_TYPE_LEN + 1] ={0, };
4814         char mac_str[18] = {0, };
4815
4816         int res = 0;
4817
4818         if (!mac_addr ) {
4819                 WDP_LOGE("Invalid parameter");
4820                 __WDP_LOG_FUNC_EXIT__;
4821                 return -1;
4822         }
4823
4824         if (!g_pd) {
4825                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4826                 __WDP_LOG_FUNC_EXIT__;
4827                 return -1;
4828         }
4829
4830         g_dbus = g_pd->g_dbus;
4831         if (!g_dbus) {
4832                 WDP_LOGE("DBus connection is NULL");
4833                 __WDP_LOG_FUNC_EXIT__;
4834                 return -1;
4835         }
4836
4837         if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
4838                 mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
4839                 snprintf(mac_str, WS_MACSTR_LEN , "%s", SERV_BROADCAST_ADDRESS);
4840         } else {
4841                 snprintf(mac_str, WS_MACSTR_LEN, MACSTR, MAC2STR(mac_addr));
4842         }
4843
4844         switch(service_type) {
4845                 case WFD_OEM_SERVICE_TYPE_ALL:
4846                         strncpy(s_type, SERV_DISC_REQ_ALL, OEM_SERVICE_TYPE_LEN);
4847                 break;
4848                 case WFD_OEM_SERVICE_TYPE_BONJOUR:
4849                         strncpy(s_type, SERV_DISC_REQ_BONJOUR, OEM_SERVICE_TYPE_LEN);
4850                 break;
4851                 case WFD_OEM_SERVICE_TYPE_UPNP:
4852                         strncpy(s_type, SERV_DISC_REQ_UPNP, OEM_SERVICE_TYPE_LEN);
4853                 break;
4854                 default:
4855                         WDP_LOGE("Invalid Service type");
4856                         __WDP_LOG_FUNC_EXIT__;
4857                         return -1;
4858         }
4859
4860         WDP_LOGD("Cancel service discovery service_type [%d]", service_type);
4861         WDP_LOGD("Cancel service discovery s_type [%s]", s_type);
4862
4863         data = _remove_service_query(s_type, mac_str, query_id);
4864         if (NULL == data) {
4865                 __WDP_LOG_FUNC_EXIT__;
4866                 return -1;
4867         }
4868         memset(&params, 0x0, sizeof(dbus_method_param_s));
4869
4870         dbus_set_method_param(&params, "ServiceDiscoveryCancelRequest", g_pd->iface_path, g_dbus);
4871
4872         params.params = g_variant_new ("(t)", strtoul(query_id, NULL, 16));
4873
4874         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4875         if (res < 0)
4876                 WDP_LOGE("Failed to send command to wpa_supplicant");
4877         else
4878                 WDP_LOGD("Succeeded to cancel service discovery");
4879
4880         service_list = g_list_remove(service_list, data);
4881         free(data);
4882
4883         __WDP_LOG_FUNC_EXIT__;
4884         return res;
4885 }
4886
4887 int ws_serv_add(wfd_oem_new_service_s *service)
4888 {
4889         __WDP_LOG_FUNC_ENTER__;
4890         GDBusConnection *g_dbus = NULL;
4891         GVariantBuilder *builder = NULL;
4892         GVariant *value = NULL;
4893         dbus_method_param_s params;
4894         int res = 0;
4895
4896         if (!g_pd) {
4897                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4898                 return -1;
4899         }
4900
4901         g_dbus = g_pd->g_dbus;
4902         if (!g_dbus) {
4903                 WDP_LOGE("DBus connection is NULL");
4904                 return -1;
4905         }
4906         memset(&params, 0x0, sizeof(dbus_method_param_s));
4907
4908         dbus_set_method_param(&params, "AddService", g_pd->iface_path, g_dbus);
4909
4910         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}") );
4911
4912         if (service->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
4913
4914                 WDP_LOGD("Service type: WFD_OEM_SERVICE_TYPE_BONJOUR");
4915                 WDP_LOGD("Query: %s", service->data.bonjour.query);
4916                 WDP_LOGD("RData: %s", service->data.bonjour.rdata);
4917
4918                 res = _convert_bonjour_to_args(service->data.bonjour.query,
4919                                                             service->data.bonjour.rdata, builder);
4920                 if (res < 0) {
4921                         WDP_LOGE("Failed to convert Key string");
4922                         g_variant_builder_unref (builder);
4923                         return -1;
4924                 }
4925
4926         } else if (service->protocol == WFD_OEM_SERVICE_TYPE_UPNP) {
4927                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
4928                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
4929                 g_variant_builder_add (builder, "{sv}", "service", g_variant_new_string(service->data.upnp.service));
4930         }
4931
4932         value = g_variant_new ("(a{sv})", builder);
4933         g_variant_builder_unref (builder);
4934
4935         params.params = value;
4936 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4937         DEBUG_PARAMS(params.params);
4938 #endif /* TIZEN_DEBUG_DBUS_VALUE */
4939
4940         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
4941         if (res < 0)
4942                 WDP_LOGE("Failed to send command to wpa_supplicant");
4943         else
4944                 WDP_LOGD("Succeeded to add service");
4945
4946         __WDP_LOG_FUNC_EXIT__;
4947         return 0;
4948 }
4949
4950 int ws_serv_del(wfd_oem_new_service_s *service)
4951 {
4952         __WDP_LOG_FUNC_ENTER__;
4953         GDBusConnection *g_dbus = NULL;
4954         GVariantBuilder *builder = NULL;
4955         GVariant *value = NULL;
4956         dbus_method_param_s params;
4957         int res = 0;
4958
4959         if (!g_pd) {
4960                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
4961                 return -1;
4962         }
4963
4964         g_dbus = g_pd->g_dbus;
4965         if (!g_dbus) {
4966                 WDP_LOGE("DBus connection is NULL");
4967                 return -1;
4968         }
4969         memset(&params, 0x0, sizeof(dbus_method_param_s));
4970
4971         dbus_set_method_param(&params, "DeleteService", g_pd->iface_path, g_dbus);
4972
4973         builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
4974
4975         if (service->protocol == WFD_OEM_SERVICE_TYPE_BONJOUR) {
4976
4977                 WDP_LOGD("Service type: WFD_OEM_SERVICE_TYPE_BONJOUR");
4978                 WDP_LOGD("Query: %s", service->data.bonjour.query);
4979
4980                 res = _convert_bonjour_to_args(service->data.bonjour.query,
4981                                                             NULL, builder);
4982                 if (res < 0) {
4983                         WDP_LOGE("Failed to convert Key string");
4984                         g_variant_builder_unref (builder);
4985                         return -1;
4986                 }
4987
4988         } else if (service->protocol == WFD_OEM_SERVICE_TYPE_UPNP) {
4989                 g_variant_builder_add (builder, "{sv}", "service_type", g_variant_new_string("upnp"));
4990                 g_variant_builder_add (builder, "{sv}", "version", g_variant_new_uint16(TRUE));
4991                 g_variant_builder_add (builder, "{sv}", "service", g_variant_new_string(service->data.upnp.service));
4992         }
4993
4994         value = g_variant_new ("(a{sv})", builder);
4995         g_variant_builder_unref (builder);
4996
4997         params.params = value;
4998 #if defined (TIZEN_DEBUG_DBUS_VALUE)
4999         DEBUG_PARAMS(params.params);
5000 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5001
5002         res = dbus_method_call(&params, SUPPLICANT_P2PDEVICE, NULL, NULL);
5003         if (res < 0)
5004                 WDP_LOGE("Failed to send command to wpa_supplicant");
5005         else
5006                 WDP_LOGD("Succeeded to del service");
5007
5008         __WDP_LOG_FUNC_EXIT__;
5009         return 0;
5010 }
5011 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
5012
5013 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
5014
5015 int _ws_disable_display()
5016 {
5017         __WDP_LOG_FUNC_ENTER__;
5018         GDBusConnection *g_dbus = NULL;
5019         GVariantBuilder *builder = NULL;
5020         GVariant *value = NULL;
5021         GVariant *param = NULL;
5022         dbus_method_param_s params;
5023         int res = 0;
5024
5025         if (!g_pd) {
5026                 WDP_LOGE("ws_dbus_plugin_data_s is not created yet");
5027                 return -1;
5028         }
5029
5030         g_dbus = g_pd->g_dbus;
5031         if (!g_dbus) {
5032                 WDP_LOGE("DBus connection is NULL");
5033                 return -1;
5034         }
5035         memset(&params, 0x0, sizeof(dbus_method_param_s));
5036
5037         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, SUPPLICANT_PATH,
5038                          g_dbus);
5039
5040         builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5041         value = g_variant_new ("ay", builder);
5042         g_variant_builder_unref (builder);
5043
5044         param = g_variant_new("(ssv)", SUPPLICANT_INTERFACE, "WFDIEs", value);
5045
5046         params.params = param;
5047 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5048         DEBUG_PARAMS(params.params);
5049 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5050
5051         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
5052         if (res < 0)
5053                 WDP_LOGE("Failed to send command to wpa_supplicant");
5054         else
5055                 WDP_LOGD("Succeeded to disable Wi-Fi display");
5056
5057         __WDP_LOG_FUNC_EXIT__;
5058         return res;
5059 }
5060
5061 int ws_miracast_init(int enable)
5062 {
5063         __WDP_LOG_FUNC_ENTER__;
5064         wfd_oem_display_s wifi_display;
5065         int res = 0;
5066
5067         memset(&wifi_display, 0x0, sizeof(wfd_oem_display_s));
5068
5069         wifi_display.availability = enable;
5070         wifi_display.hdcp_support = 1;
5071         wifi_display.port = 0x07E6;
5072         wifi_display.max_tput = 0x0028;
5073
5074         res = ws_set_display(&wifi_display);
5075         if (res < 0) {
5076                 WDP_LOGE("Failed to set miracast parameter(device info)");
5077                 __WDP_LOG_FUNC_EXIT__;
5078                 return -1;
5079         }
5080
5081         if(!enable) {
5082                 res = _ws_disable_display();
5083                 if (res < 0)
5084                         WDP_LOGE("Failed to disable wifi display");
5085                 else
5086                         WDP_LOGD("Succeeded to disable wifi display");
5087         }
5088         __WDP_LOG_FUNC_EXIT__;
5089         return res;
5090 }
5091
5092 int ws_set_display(wfd_oem_display_s *wifi_display)
5093 {
5094         __WDP_LOG_FUNC_ENTER__;
5095         GDBusConnection *g_dbus = NULL;
5096
5097         GVariant *value = NULL;
5098         GVariant *param = NULL;
5099         GVariantBuilder *builder = NULL;
5100         dbus_method_param_s params;
5101         int i = 0;
5102         int res = 0;
5103
5104         unsigned char ies[WFD_SUBELEM_LEN_DEV_INFO + 3] = {0,};
5105
5106         if (!wifi_display) {
5107                 WDP_LOGE("Invalid parameter");
5108                 return -1;
5109         }
5110         g_dbus = g_pd->g_dbus;
5111         if (!g_dbus) {
5112                 WDP_LOGE("DBus connection is NULL");
5113                 return -1;
5114         }
5115         memset(&params, 0x0, sizeof(dbus_method_param_s));
5116
5117         dbus_set_method_param(&params, DBUS_PROPERTIES_METHOD_SET, SUPPLICANT_PATH,
5118                          g_dbus);
5119
5120         ies[2] = WFD_SUBELEM_LEN_DEV_INFO;
5121         ies[3] = wifi_display->hdcp_support;
5122         ies[4] = (wifi_display->type) | (wifi_display->availability<<4);
5123         ies[5] = wifi_display->port>>8;
5124         ies[6] = wifi_display->port&0xff;
5125         ies[7] = wifi_display->max_tput>>8;
5126         ies[8] = wifi_display->max_tput&0xff;
5127
5128         builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
5129         for(i = 0; i < WFD_SUBELEM_LEN_DEV_INFO + 3; i++)
5130                 g_variant_builder_add(builder, "y", ies[i]);
5131         value = g_variant_new ("ay", builder);
5132         g_variant_builder_unref (builder);
5133
5134         param = g_variant_new("(ssv)", SUPPLICANT_INTERFACE, "WFDIEs", value);
5135
5136         params.params = param;
5137 #if defined (TIZEN_DEBUG_DBUS_VALUE)
5138         DEBUG_PARAMS(params.params);
5139 #endif /* TIZEN_DEBUG_DBUS_VALUE */
5140
5141         res = dbus_method_call(&params, DBUS_PROPERTIES_INTERFACE, NULL, NULL);
5142         if (res < 0)
5143                 WDP_LOGE("Failed to send command to wpa_supplicant");
5144         else
5145                 WDP_LOGD("Succeeded to set Wi-Fi Display");
5146
5147         __WDP_LOG_FUNC_EXIT__;
5148         return res;
5149 }
5150 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
5151
5152 int ws_refresh()
5153 {
5154         __WDP_LOG_FUNC_ENTER__;
5155
5156         _ws_cancel();
5157         _ws_flush();
5158
5159         __WDP_LOG_FUNC_EXIT__;
5160         return 0;
5161 }