[capi-network-wifi-direct]Sync with Tizen 2.4
[platform/core/api/wifi-direct.git] / src / wifi-direct-client-proxy.c
1 /*
2  * libwifi-direct
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sungsik Jang <sungsik.jang@samsung.com>, Dongwook Lee <dwmax.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 /*****************************************************************************
24  *      Standard headers
25  *****************************************************************************/
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <unistd.h>
31 #include <netdb.h>
32 #include <sys/socket.h>
33 #include <string.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <signal.h>
39 #include <linux/unistd.h>
40 #include <sys/poll.h>
41 #include <pthread.h>
42 #include <errno.h>
43
44 #include <glib.h>
45 #include <gio/gio.h>
46
47 /*****************************************************************************
48  *      System headers
49  *****************************************************************************/
50 #include <vconf.h>
51 #include <system_info.h>
52
53 /*****************************************************************************
54  *      Wi-Fi Direct Service headers
55  *****************************************************************************/
56 #include "wifi-direct.h"
57 #include "wifi-direct-internal.h"
58 #include "wifi-direct-client-proxy.h"
59 #include "wifi-direct-ipc.h"
60
61 /*****************************************************************************
62  *      Macros and Typedefs
63  *****************************************************************************/
64
65 /*****************************************************************************
66  *      Global Variables
67  *****************************************************************************/
68 wifi_direct_client_info_s g_client_info = {
69         .is_registered = FALSE,
70         .client_id = -1,
71         .sync_sockfd = -1,
72         .async_sockfd = -1,
73         .activation_cb = NULL,
74         .discover_cb = NULL,
75         .connection_cb = NULL,
76         .ip_assigned_cb = NULL,
77         .peer_found_cb = NULL,
78         .user_data_for_cb_activation = NULL,
79         .user_data_for_cb_discover = NULL,
80         .user_data_for_cb_connection = NULL,
81         .user_data_for_cb_ip_assigned = NULL,
82         .user_data_for_cb_peer_found = NULL,
83         .user_data_for_cb_device_name = NULL,
84 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
85         .service_cb = NULL,
86         .user_data_for_cb_service = NULL,
87 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
88
89         .mutex = PTHREAD_MUTEX_INITIALIZER
90 };
91
92 /*****************************************************************************
93  *      Local Functions Definition
94  *****************************************************************************/
95
96 #ifdef __NR_gettid
97 pid_t gettid(void)
98 {
99         return syscall(__NR_gettid);
100 }
101 #else
102 #error "__NR_gettid is not defined, please include linux/unistd.h"
103 #endif
104
105 static wifi_direct_client_info_s *__wfd_get_control()
106 {
107         return &g_client_info;
108 }
109
110 static void __wfd_reset_control()
111 {
112
113         if (g_client_info.g_source_id > 0)
114                 g_source_remove(g_client_info.g_source_id);
115         g_client_info.g_source_id = -1;
116
117         // Protect standard input / output / error
118         if (g_client_info.sync_sockfd > 0)
119                 close(g_client_info.sync_sockfd);
120         g_client_info.sync_sockfd = -1;
121
122         if (g_client_info.async_sockfd > 0)
123                 close(g_client_info.async_sockfd);
124         g_client_info.async_sockfd = -1;
125
126         g_client_info.is_registered = FALSE;
127
128         // Initialize callbacks
129         g_client_info.activation_cb = NULL;
130         g_client_info.discover_cb = NULL;
131         g_client_info.connection_cb = NULL;
132         g_client_info.ip_assigned_cb = NULL;
133         g_client_info.peer_found_cb = NULL;
134         g_client_info.user_data_for_cb_activation = NULL;
135         g_client_info.user_data_for_cb_discover = NULL;
136         g_client_info.user_data_for_cb_connection = NULL;
137         g_client_info.user_data_for_cb_ip_assigned = NULL;
138         g_client_info.user_data_for_cb_peer_found = NULL;
139         g_client_info.user_data_for_cb_device_name = NULL;
140
141 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
142         g_client_info.service_cb = NULL;
143         g_client_info.user_data_for_cb_service= NULL;
144 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
145
146
147         pthread_mutex_destroy(&g_client_info.mutex);
148 }
149
150 static int macaddr_atoe(char *p, unsigned char mac[])
151 {
152         int i = 0;
153
154         for (;;) {
155                 mac[i++] = (char) strtoul(p, &p, 16);
156                 if (!*p++ || i == 6)
157                         break;
158         }
159
160         return (i == 6);
161 }
162
163 static char *__wfd_print_event(wfd_client_event_e event)
164 {
165         switch (event)
166         {
167         case WIFI_DIRECT_CLI_EVENT_INVALID:
168                 return "WIFI_DIRECT_CLI_EVENT_INVALID";
169                 break;
170         case WIFI_DIRECT_CLI_EVENT_ACTIVATION:
171                 return "ACTIVATION";
172                 break;
173         case WIFI_DIRECT_CLI_EVENT_DEACTIVATION:
174                 return "DEACTIVATION";
175                 break;
176         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START:
177                 return "WIFI_DIRECT_CLI_EVENT_DISCOVER_START";
178                 break;
179         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_LISTEN_ONLY:
180                 return "WIFI_DIRECT_CLI_EVENT_DISCOVER_START_LISTEN_ONLY";
181                 break;
182         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_SEARCH_LISTEN:
183                 return "WIFI_DIRECT_CLI_EVENT_DISCOVER_START_SEARCH_LISTEN";
184                 break;
185         case WIFI_DIRECT_CLI_EVENT_DISCOVER_END:
186                 return "WIFI_DIRECT_CLI_EVENT_DISCOVER_END";
187                 break;
188         case WIFI_DIRECT_CLI_EVENT_DISCOVER_FOUND_PEERS:
189                 return "WIFI_DIRECT_CLI_EVENT_DISCOVER_FOUND_PEERS";
190                 break;
191         case WIFI_DIRECT_CLI_EVENT_CONNECTION_START:
192                 return "WIFI_DIRECT_CLI_EVENT_CONNECTION_START";
193                 break;
194         case WIFI_DIRECT_CLI_EVENT_CONNECTION_REQ:
195                 return "WIFI_DIRECT_CLI_EVENT_CONNECTION_REQ";
196                 break;
197         case WIFI_DIRECT_CLI_EVENT_CONNECTION_RSP:
198                 return "WIFI_DIRECT_CLI_EVENT_CONNECTION_RSP";
199                 break;
200         case WIFI_DIRECT_CLI_EVENT_CONNECTION_WPS_REQ:
201                 return "WIFI_DIRECT_CLI_EVENT_CONNECTION_WPS_REQ";
202                 break;
203         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_RSP:
204                 return "WIFI_DIRECT_CLI_EVENT_DISCONNECTION_RSP";
205                 break;
206         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_IND:
207                 return "WIFI_DIRECT_CLI_EVENT_DISCONNECTION_IND";
208                 break;
209         case WIFI_DIRECT_CLI_EVENT_DISASSOCIATION_IND:
210                 return "WIFI_DIRECT_CLI_EVENT_DISASSOCIATION_IND";
211                 break;
212         case WIFI_DIRECT_CLI_EVENT_GROUP_CREATE_RSP:
213                 return "WIFI_DIRECT_CLI_EVENT_GROUP_CREATE_RSP";
214                 break;
215         case WIFI_DIRECT_CLI_EVENT_GROUP_DESTROY_RSP:
216                 return "WIFI_DIRECT_CLI_EVENT_GROUP_DESTROY_RSP";
217                 break;
218         case WIFI_DIRECT_CLI_EVENT_IP_LEASED_IND:
219                 return "WIFI_DIRECT_CLI_EVENT_IP_LEASED_IND";
220                 break;
221 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
222         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FOUND:
223                 return "WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FOUND";
224                 break;
225         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_STARTED:
226                 return "WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_STARTED";
227                 break;
228         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FINISHED:
229                 return "WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FINSIHED";
230                 break;
231 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
232
233         default:
234                 return "WIFI_DIRECT_CLI_EVENT_unknown";
235                 break;
236         }
237 }
238
239 static char *__wfd_print_error(wifi_direct_error_e error)
240 {
241         switch (error)
242         {
243         case WIFI_DIRECT_ERROR_NONE:
244                 return "WIFI_DIRECT_ERROR_NONE";
245         case WIFI_DIRECT_ERROR_NOT_PERMITTED:
246                 return "WIFI_DIRECT_ERROR_NOT_PERMITTED";
247         case WIFI_DIRECT_ERROR_OUT_OF_MEMORY:
248                 return "WIFI_DIRECT_ERROR_OUT_OF_MEMORY";
249         case WIFI_DIRECT_ERROR_PERMISSION_DENIED:
250                 return "WIFI_DIRECT_ERROR_PERMISSION_DENIED";
251         case WIFI_DIRECT_ERROR_RESOURCE_BUSY:
252                 return "WIFI_DIRECT_ERROR_RESOURCE_BUSY";
253         case WIFI_DIRECT_ERROR_INVALID_PARAMETER:
254                 return "WIFI_DIRECT_ERROR_INVALID_PARAMETER";
255         case WIFI_DIRECT_ERROR_NOT_INITIALIZED:
256                 return "WIFI_DIRECT_ERROR_NOT_INITIALIZED";
257         case WIFI_DIRECT_ERROR_COMMUNICATION_FAILED:
258                 return "WIFI_DIRECT_ERROR_COMMUNICATION_FAILED";
259         case WIFI_DIRECT_ERROR_WIFI_USED:
260                 return "WIFI_DIRECT_ERROR_WIFI_USED";
261         case WIFI_DIRECT_ERROR_MOBILE_AP_USED:
262                 return "WIFI_DIRECT_ERROR_MOBILE_AP_USED";
263         case WIFI_DIRECT_ERROR_CONNECTION_FAILED:
264                 return "WIFI_DIRECT_ERROR_CONNECTION_FAILED";
265         case WIFI_DIRECT_ERROR_AUTH_FAILED:
266                 return "WIFI_DIRECT_ERROR_AUTH_FAILED";
267         case WIFI_DIRECT_ERROR_OPERATION_FAILED:
268                 return "WIFI_DIRECT_ERROR_OPERATION_FAILED";
269         case WIFI_DIRECT_ERROR_TOO_MANY_CLIENT:
270                 return "WIFI_DIRECT_ERROR_TOO_MANY_CLIENT";
271         case WIFI_DIRECT_ERROR_ALREADY_INITIALIZED:
272                 return "WIFI_DIRECT_ERROR_ALREADY_INITIALIZED";
273         default:
274                 WDC_LOGE("Invalid error value: [%d]", error);
275                 return "Invalid error";
276         }
277 }
278
279 static int __wfd_convert_client_event(wfd_client_event_e event)
280 {
281         __WDC_LOG_FUNC_START__;
282
283         switch (event)
284         {
285         case WIFI_DIRECT_CLI_EVENT_ACTIVATION:
286                 return WIFI_DIRECT_DEVICE_STATE_ACTIVATED;
287                 break;
288         case WIFI_DIRECT_CLI_EVENT_DEACTIVATION:
289                 return WIFI_DIRECT_DEVICE_STATE_DEACTIVATED;
290                 break;
291
292         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_LISTEN_ONLY:
293                 return WIFI_DIRECT_ONLY_LISTEN_STARTED;
294                 break;
295         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START:
296         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_SEARCH_LISTEN:
297                 return WIFI_DIRECT_DISCOVERY_STARTED;
298                 break;
299         case WIFI_DIRECT_CLI_EVENT_DISCOVER_FOUND_PEERS:
300                 return WIFI_DIRECT_DISCOVERY_FOUND;
301                 break;
302         case WIFI_DIRECT_CLI_EVENT_DISCOVER_END:
303                 return WIFI_DIRECT_DISCOVERY_FINISHED;
304                 break;
305         case WIFI_DIRECT_CLI_EVENT_CONNECTION_REQ:
306                 return WIFI_DIRECT_CONNECTION_REQ;
307                 break;
308         case WIFI_DIRECT_CLI_EVENT_CONNECTION_WPS_REQ:
309                 return WIFI_DIRECT_CONNECTION_WPS_REQ;
310                 break;
311         case WIFI_DIRECT_CLI_EVENT_CONNECTION_START:
312                 return WIFI_DIRECT_CONNECTION_IN_PROGRESS;
313                 break;
314         case WIFI_DIRECT_CLI_EVENT_CONNECTION_RSP:
315                 return WIFI_DIRECT_CONNECTION_RSP;
316                 break;
317         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_RSP:
318                 return WIFI_DIRECT_DISCONNECTION_RSP;
319                 break;
320         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_IND:
321                 return WIFI_DIRECT_DISCONNECTION_IND;
322                 break;
323         case WIFI_DIRECT_CLI_EVENT_DISASSOCIATION_IND:
324                 return WIFI_DIRECT_DISASSOCIATION_IND;
325                 break;
326         case WIFI_DIRECT_CLI_EVENT_GROUP_CREATE_RSP:
327                 return WIFI_DIRECT_GROUP_CREATED;
328                 break;
329         case WIFI_DIRECT_CLI_EVENT_GROUP_DESTROY_RSP:
330                 return WIFI_DIRECT_GROUP_DESTROYED;
331                 break;
332 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
333         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FOUND:
334                 return WIFI_DIRECT_SERVICE_DISCOVERY_FOUND;
335                 break;
336         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_STARTED:
337                 return WIFI_DIRECT_SERVICE_DISCOVERY_STARTED;
338                 break;
339         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FINISHED:
340                 return WIFI_DIRECT_SERVICE_DISCOVERY_FINISHED;
341                 break;
342 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
343         default:
344                 WDC_LOGE("Invalid event : [%d]", event);
345                 break;
346         }
347
348         __WDC_LOG_FUNC_END__;
349         return -1;
350 }
351
352 char *__wfd_client_print_cmd(wifi_direct_cmd_e cmd)
353 {
354         switch (cmd)
355         {
356         case WIFI_DIRECT_CMD_REGISTER:
357                 return "WIFI_DIRECT_CMD_REGISTER";
358         case WIFI_DIRECT_CMD_INIT_ASYNC_SOCKET:
359                 return "WIFI_DIRECT_CMD_INIT_ASYNC_SOCKET";
360         case WIFI_DIRECT_CMD_DEREGISTER:
361                 return "WIFI_DIRECT_CMD_DEREGISTER";
362         case WIFI_DIRECT_CMD_ACTIVATE:
363                 return "WIFI_DIRECT_CMD_ACTIVATE";
364         case WIFI_DIRECT_CMD_DEACTIVATE:
365                 return "WIFI_DIRECT_CMD_DEACTIVATE";
366         case WIFI_DIRECT_CMD_START_DISCOVERY:
367                 return "WIFI_DIRECT_CMD_START_DISCOVERY";
368         case WIFI_DIRECT_CMD_START_DISCOVERY_SPECIFIC_CHANNEL:
369                 return "WIFI_DIRECT_CMD_START_DISCOVERY_SPECIFIC_CHANNEL";
370         case WIFI_DIRECT_CMD_CANCEL_DISCOVERY:
371                 return "WIFI_DIRECT_CMD_CANCEL_DISCOVERY";
372         case WIFI_DIRECT_CMD_GET_DISCOVERY_RESULT:
373                 return "WIFI_DIRECT_CMD_GET_DISCOVERY_RESULT";
374         case WIFI_DIRECT_CMD_GET_LINK_STATUS:
375                 return "WIFI_DIRECT_CMD_GET_LINK_STATUS";
376         case WIFI_DIRECT_CMD_CONNECT:
377                 return "WIFI_DIRECT_CMD_CONNECT";
378         case WIFI_DIRECT_CMD_CANCEL_CONNECT:
379                 return "WIFI_DIRECT_CMD_CANCEL_CONNECT";
380         case WIFI_DIRECT_CMD_CANCEL_CONNECTION:
381                 return "WIFI_DIRECT_CMD_CANCEL_CONNECTION";
382         case WIFI_DIRECT_CMD_REJECT_CONNECTION:
383                 return "WIFI_DIRECT_CMD_REJECT_CONNECTION";
384         case WIFI_DIRECT_CMD_DISCONNECT_ALL:
385                 return "WIFI_DIRECT_CMD_DISCONNECT_ALL";
386         case WIFI_DIRECT_CMD_CREATE_GROUP:
387                 return "WIFI_DIRECT_CMD_CREATE_GROUP";
388         case WIFI_DIRECT_CMD_IS_GROUPOWNER:
389                 return "WIFI_DIRECT_CMD_IS_GROUPOWNER";
390         case WIFI_DIRECT_CMD_GET_SSID:
391                 return "WIFI_DIRECT_CMD_GET_SSID";
392         case WIFI_DIRECT_CMD_SET_SSID:
393                 return "WIFI_DIRECT_CMD_SET_SSID";
394         case WIFI_DIRECT_CMD_GET_IP_ADDR:
395                 return "WIFI_DIRECT_CMD_GET_IP_ADDR";
396         case WIFI_DIRECT_CMD_GET_CONFIG:
397                 return "WIFI_DIRECT_CMD_GET_CONFIG";
398         case WIFI_DIRECT_CMD_SET_CONFIG:
399                 return "WIFI_DIRECT_CMD_SET_CONFIG";
400         case WIFI_DIRECT_CMD_SEND_CONNECT_REQ:
401                 return "WIFI_DIRECT_CMD_SEND_CONNECT_REQ";
402         case WIFI_DIRECT_CMD_ACTIVATE_PUSHBUTTON:
403                 return "WIFI_DIRECT_CMD_ACTIVATE_PUSHBUTTON";
404         case WIFI_DIRECT_CMD_SET_WPS_PIN:
405                 return "WIFI_DIRECT_CMD_SET_WPS_PIN";
406         case WIFI_DIRECT_CMD_GET_WPS_PIN:
407                 return "WIFI_DIRECT_CMD_GET_WPS_PIN";
408         case WIFI_DIRECT_CMD_GENERATE_WPS_PIN:
409                 return "WIFI_DIRECT_CMD_GENERATE_WPS_PIN";
410         case WIFI_DIRECT_CMD_SET_WPA:
411                 return "WIFI_DIRECT_CMD_SET_WPA";
412         case WIFI_DIRECT_CMD_GET_SUPPORTED_WPS_MODE:
413                 return "WIFI_DIRECT_CMD_GET_SUPPORTED_WPS_MODE";
414         case WIFI_DIRECT_CMD_GET_LOCAL_WPS_MODE:
415                 return "WIFI_DIRECT_CMD_GET_LOCAL_WPS_MODE";
416         case WIFI_DIRECT_CMD_SET_REQ_WPS_MODE:
417                 return "WIFI_DIRECT_CMD_SET_REQ_WPS_MODE";
418         case WIFI_DIRECT_CMD_GET_REQ_WPS_MODE:
419                 return "WIFI_DIRECT_CMD_GET_REQ_WPS_MODE";
420         case WIFI_DIRECT_CMD_GET_CONNECTED_PEERS_INFO:
421                 return "WIFI_DIRECT_CMD_GET_CONNECTED_PEERS_INFO";
422         case WIFI_DIRECT_CMD_DESTROY_GROUP:
423                 return "WIFI_DIRECT_CMD_DESTROY_GROUP";
424         case WIFI_DIRECT_CMD_DISCONNECT:
425                 return "WIFI_DIRECT_CMD_DISCONNECT";
426         case WIFI_DIRECT_CMD_SET_GO_INTENT:
427                 return "WIFI_DIRECT_CMD_SET_GO_INTENT";
428         case WIFI_DIRECT_CMD_GET_GO_INTENT:
429                 return "WIFI_DIRECT_CMD_GET_GO_INTENT";
430         case WIFI_DIRECT_CMD_GET_MAC_ADDR:
431                 return "WIFI_DIRECT_CMD_GET_MAC_ADDR";
432         case WIFI_DIRECT_CMD_IS_AUTONOMOUS_GROUP:
433                 return "WIFI_DIRECT_CMD_IS_AUTONOMOUS_GROUP";
434         case WIFI_DIRECT_CMD_SET_MAX_CLIENT:
435                 return "WIFI_DIRECT_CMD_SET_MAX_CLIENT";
436         case WIFI_DIRECT_CMD_GET_MAX_CLIENT:
437                 return "WIFI_DIRECT_CMD_GET_MAX_CLIENT";
438         case WIFI_DIRECT_CMD_SET_AUTOCONNECTION_MODE:
439                 return "WIFI_DIRECT_CMD_SET_AUTOCONNECTION_MODE";
440         case WIFI_DIRECT_CMD_IS_AUTOCONNECTION_MODE:
441                 return "WIFI_DIRECT_CMD_IS_AUTOCONNECTION_MODE";
442         case WIFI_DIRECT_CMD_IS_DISCOVERABLE:
443                 return "WIFI_DIRECT_CMD_IS_DISCOVERABLE";
444         case WIFI_DIRECT_CMD_IS_LISTENING_ONLY:
445                 return "WIFI_DIRECT_CMD_IS_LISTENING_ONLY";
446         case WIFI_DIRECT_CMD_GET_OPERATING_CHANNEL:
447                 return "WIFI_DIRECT_CMD_GET_OPERATING_CHANNEL";
448         case WIFI_DIRECT_CMD_ACTIVATE_PERSISTENT_GROUP:
449                 return "WIFI_DIRECT_CMD_ACTIVATE_PERSISTENT_GROUP";
450         case WIFI_DIRECT_CMD_DEACTIVATE_PERSISTENT_GROUP:
451                 return "WIFI_DIRECT_CMD_DEACTIVATE_PERSISTENT_GROUP";
452         case WIFI_DIRECT_CMD_IS_PERSISTENT_GROUP_ACTIVATED:
453                 return "WIFI_DIRECT_CMD_IS_PERSISTENT_GROUP_ACTIVATED";
454         case WIFI_DIRECT_CMD_GET_PERSISTENT_GROUP_INFO:
455                 return "WIFI_DIRECT_CMD_GET_PERSISTENT_GROUP_INFO";
456         case WIFI_DIRECT_CMD_REMOVE_PERSISTENT_GROUP:
457                 return "WIFI_DIRECT_CMD_REMOVE_PERSISTENT_GROUP";
458         case WIFI_DIRECT_CMD_GET_DEVICE_NAME:
459                 return "WIFI_DIRECT_CMD_GET_DEVICE_NAME";
460         case WIFI_DIRECT_CMD_SET_DEVICE_NAME:
461                 return "WIFI_DIRECT_CMD_SET_DEVICE_NAME";
462         case WIFI_DIRECT_CMD_SET_OEM_LOGLEVEL:
463                 return "WIFI_DIRECT_CMD_SET_OEM_LOGLEVEL";
464         case WIFI_DIRECT_CMD_GET_PEER_INFO:
465                 return "WIFI_DIRECT_CMD_GET_PEER_INFO";
466
467         case WIFI_DIRECT_CMD_SET_PASSPHRASE:
468                 return "WIFI_DIRECT_CMD_SET_PASSPHRASE";
469         case WIFI_DIRECT_CMD_GET_PASSPHRASE:
470                 return "WIFI_DIRECT_CMD_GET_PASSPHRASE";
471         case WIFI_DIRECT_CMD_SET_AUTOCONNECTION_PEER:
472                 return "WIFI_DIRECT_CMD_SET_AUTOCONNECTION_PEER";
473
474 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
475         case WIFI_DIRECT_CMD_REGISTER_LOCAL_SERVICE:
476                 return "WIFI_DIRECT_CMD_REGISTER_LOCAL_SERVICE";
477         case WIFI_DIRECT_CMD_DEREGISTER_LOCAL_SERVICE:
478                 return "WIFI_DIRECT_CMD_DEREGISTER_LOCAL_SERVICE";
479         case WIFI_DIRECT_CMD_START_SERVICE_DISCOVERY:
480                 return "WIFI_DIRECT_CMD_START_SERVICE_DISCOVERY";
481         case WIFI_DIRECT_CMD_CANCEL_SERVICE_DISCOVERY:
482                 return "WIFI_DIRECT_CMD_CANCEL_SERVICE_DISCOVERY";
483 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
484
485 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
486         case WIFI_DIRECT_CMD_INIT_MIRACAST:
487                 return "WIFI_DIRECT_CMD_INIT_MIRACAST";
488         case WIFI_DIRECT_CMD_INIT_DISPLAY:
489                 return "WIFI_DIRECT_CMD_INIT_DISPLAY";
490         case WIFI_DIRECT_CMD_DEINIT_DISPLAY:
491                 return "WIFI_DIRECT_CMD_DEINIT_DISPLAY";
492         case WIFI_DIRECT_CMD_SET_DISPLAY:
493                 return "WIFI_DIRECT_CMD_SET_DISPLAY";
494         case WIFI_DIRECT_CMD_SET_DISPLAY_AVAILABILITY:
495                 return "WIFI_DIRECT_CMD_SET_DISPLAY_AVAILABILITY";
496         case WIFI_DIRECT_CMD_GET_PEER_DISPLAY_TYPE:
497                 return "WIFI_DIRECT_CMD_GET_PEER_DISPLAY_TYPE";
498         case WIFI_DIRECT_CMD_GET_PEER_DISPLAY_AVAILABILITY:
499                 return "WIFI_DIRECT_CMD_GET_PEER_DISPLAY_AVAILABILITY";
500         case WIFI_DIRECT_CMD_GET_PEER_DISPLAY_HDCP:
501                 return "WIFI_DIRECT_CMD_GET_PEER_DISPLAY_HDCP";
502         case WIFI_DIRECT_CMD_GET_PEER_DISPLAY_PORT:
503                 return "WIFI_DIRECT_CMD_GET_PEER_DISPLAY_PORT";
504         case WIFI_DIRECT_CMD_GET_PEER_DISPLAY_THROUGHPUT:
505                 return "WIFI_DIRECT_CMD_GET_PEER_DISPLAY_THROUGHPUT";
506 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
507         default:
508                 return "WIFI_DIRECT_CMD_INVALID";
509
510         }
511 }
512
513 static int __wfd_client_check_socket(int sock, int timeout)
514 {
515         struct pollfd p_fd;
516         int res = 0;
517
518         if (sock < 0 || timeout < 0) {
519                 WDC_LOGE("Invalid parameter");
520                 return -1;
521         }
522
523         p_fd.fd = sock;
524         p_fd.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
525         res = poll((struct pollfd *) &p_fd, 1, timeout);
526
527         if (res < 0) {
528                 WDC_LOGE("Polling error from socket[%d]. [%s]", sock, strerror(errno));
529                 return -1;
530         } else if (res == 0) {
531                 WDC_LOGD( "poll timeout. socket is busy");
532                 return 1;
533         } else {
534                 if (p_fd.revents & POLLERR) {
535                         WDC_LOGE("Error! POLLERR from socket[%d]", sock);
536                         return -1;
537                 } else if (p_fd.revents & POLLHUP) {
538                         WDC_LOGE("Error! POLLHUP from socket[%d]", sock);
539                         return -1;
540                 } else if (p_fd.revents & POLLNVAL) {
541                         WDC_LOGE("Error! POLLNVAL from socket[%d]", sock);
542                         return -1;
543                 } else if (p_fd.revents & POLLIN) {
544                         WDC_LOGD("POLLIN from socket [%d]", sock);
545                         return 0;
546                 }
547         }
548
549         WDC_LOGD("Unknown poll event [%d]", p_fd.revents);
550         return -1;
551 }
552
553 static int __wfd_client_write_socket(int sockfd, void *data, int data_len)
554 {
555         __WDC_LOG_FUNC_START__;
556         int wbytes = 0;
557
558         if (sockfd < 0 || !data || data_len <= 0) {
559                 WDC_LOGE("Invalid parameter");
560                 __WDC_LOG_FUNC_END__;
561                 return -1;
562         }
563
564         WDC_LOGD("Write [%d] bytes to socket [%d].", data_len, sockfd);
565         wbytes = write(sockfd, (char*) data, data_len);
566         if (wbytes <= 0) {
567                 WDC_LOGE("Error!!! writing to the socket. Error = %s", strerror(errno));
568                 __WDC_LOG_FUNC_END__;
569                 return -1;
570         }
571
572         __WDC_LOG_FUNC_END__;
573         return 0;
574 }
575
576 static int __wfd_client_read_socket(int sockfd, char *data, int data_len)
577 {
578         __WDC_LOG_FUNC_START__;
579         int rbytes = 0;
580         int total_rbytes = 0;
581         int res = 0;
582
583         if (sockfd < 0) {
584                 WDC_LOGE("Error!!! Invalid socket FD [%d]", sockfd);
585                 __WDC_LOG_FUNC_END__;
586                 return -1;
587         }
588
589         if (!data || data_len <= 0) {
590                 WDC_LOGE("Error!!! Invalid parameter");
591                 __WDC_LOG_FUNC_END__;
592                 return -1;
593         }
594
595         res = __wfd_client_check_socket(sockfd, 10000);
596         if (res < 0) {
597                 WDC_LOGE("Socket error");
598                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
599         } else if (res > 0) {
600                 WDC_LOGE("Socket is busy");
601                 return WIFI_DIRECT_ERROR_RESOURCE_BUSY;
602         }
603
604         while(data_len) {
605                 rbytes = read(sockfd, data, data_len);
606                 if (rbytes <= 0) {
607                         WDC_LOGE("Failed to read socket[%d] [%s]", sockfd, strerror(errno));
608                         return -1;
609                 }
610                 total_rbytes += rbytes;
611                 data += rbytes;
612                 data_len -= rbytes;
613         }
614
615         __WDC_LOG_FUNC_END__;
616         return total_rbytes;
617 }
618
619 static int __wfd_client_send_request(int sockfd, wifi_direct_client_request_s *req,
620                                                                 wifi_direct_client_response_s *rsp)
621 {
622         __WDC_LOG_FUNC_START__;
623         int res = 0;
624
625         if (!req || !rsp || sockfd < 0) {
626                 WDC_LOGE("Invalid parameter");
627                 __WDC_LOG_FUNC_END__;
628                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
629         }
630
631         pthread_mutex_lock(&g_client_info.mutex);
632         res = __wfd_client_write_socket(sockfd, req, sizeof(wifi_direct_client_request_s));
633         if (res != WIFI_DIRECT_ERROR_NONE) {
634                 WDC_LOGE("Failed to write into socket [%s]", __wfd_print_error(res));
635                 __wfd_reset_control();
636                 pthread_mutex_unlock(&g_client_info.mutex);
637                 __WDC_LOG_FUNC_END__;
638                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
639         }
640         WDC_LOGD("Succeeded to send request [%d: %s]", req->cmd, __wfd_client_print_cmd(req->cmd));
641
642         res = __wfd_client_read_socket(sockfd, (char*) rsp, sizeof(wifi_direct_client_response_s));
643         pthread_mutex_unlock(&g_client_info.mutex);
644         if (res <= 0) {
645                 WDC_LOGE("Failed to read socket [%d]", res);
646                 __wfd_reset_control();
647                 __WDC_LOG_FUNC_END__;
648                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
649         }
650
651         if (rsp->cmd != req->cmd) {
652                 WDC_LOGE("Invalid resp [%d], Original request [%d]", rsp->cmd, req->cmd);
653                 __WDC_LOG_FUNC_END__;
654                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
655         }
656
657         if (rsp->result != WIFI_DIRECT_ERROR_NONE) {
658                 WDC_LOGE("Result received [%s]", __wfd_print_error(rsp->result));
659                 __WDC_LOG_FUNC_END__;
660                 return rsp->result;
661         }
662
663         __WDC_LOG_FUNC_END__;
664         return WIFI_DIRECT_ERROR_NONE;
665 }
666
667 static gboolean __wfd_client_process_event(GIOChannel *source,
668                                                                                    GIOCondition condition,
669                                                                                    gpointer data)
670 {
671         wfd_client_event_e event = WIFI_DIRECT_CLI_EVENT_INVALID;
672         wifi_direct_client_info_s *client = __wfd_get_control();
673         int sockfd = client->async_sockfd;
674         wifi_direct_client_noti_s client_noti;
675         wifi_direct_error_e error = WIFI_DIRECT_ERROR_NONE;
676         char param1[64] = { 0, };
677         char param2[256] = { 0, };
678 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
679         int service_type;
680 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
681         int res = 0;
682
683         memset(&client_noti, 0, sizeof(wifi_direct_client_noti_s));
684
685         // 1.Read socket
686         res = __wfd_client_read_socket(sockfd, (char*) &client_noti,
687                                                                 sizeof(wifi_direct_client_noti_s));
688         if (res <= 0) {
689                 WDC_LOGE("Error!!! Reading Async Event[%d]", sockfd);
690                 __wfd_reset_control();
691                 __WDC_LOG_FUNC_END__;
692                 return false;
693         }
694         WDC_LOGD( "Received Event is [%d,%s], error[%d]", client_noti.event,
695                                         __wfd_print_event(client_noti.event), client_noti.error);
696
697         event = client_noti.event;
698         error = client_noti.error;
699 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
700         service_type = client_noti.type;
701 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
702         memcpy(param1, client_noti.param1, sizeof(client_noti.param1));
703         memcpy(param2, client_noti.param2, sizeof(client_noti.param2));
704
705
706         // 2. dispatch event
707         switch (event) {
708         case WIFI_DIRECT_CLI_EVENT_ACTIVATION:
709         case WIFI_DIRECT_CLI_EVENT_DEACTIVATION:
710                 if (!client->activation_cb) {
711                         WDC_LOGE("activation_cb is NULL!!");
712                         break;
713                 }
714                 client->activation_cb(error,
715                                         (wifi_direct_device_state_e) __wfd_convert_client_event(event),
716                                         client->user_data_for_cb_activation);
717                 break;
718         case WIFI_DIRECT_CLI_EVENT_DISCOVER_FOUND_PEERS:
719                 if (client->peer_found_cb) {
720                         client->peer_found_cb(error,
721                                         (wifi_direct_discovery_state_e) __wfd_convert_client_event(event),
722                                         param1, client->user_data_for_cb_discover);
723                 }
724         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START:
725         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_LISTEN_ONLY:
726         case WIFI_DIRECT_CLI_EVENT_DISCOVER_START_SEARCH_LISTEN:
727         case WIFI_DIRECT_CLI_EVENT_DISCOVER_END:
728                 if (!client->discover_cb) {
729                         WDC_LOGE("discover_cb is NULL!!");
730                         break;
731                 }
732                 client->discover_cb(error,
733                                         (wifi_direct_discovery_state_e) __wfd_convert_client_event(event),
734                                         client->user_data_for_cb_discover);
735                 break;
736         case WIFI_DIRECT_CLI_EVENT_CONNECTION_START:
737         case WIFI_DIRECT_CLI_EVENT_CONNECTION_REQ:
738         case WIFI_DIRECT_CLI_EVENT_CONNECTION_RSP:
739         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_RSP:
740         case WIFI_DIRECT_CLI_EVENT_CONNECTION_WPS_REQ:
741         case WIFI_DIRECT_CLI_EVENT_DISCONNECTION_IND:
742         case WIFI_DIRECT_CLI_EVENT_DISASSOCIATION_IND:
743         case WIFI_DIRECT_CLI_EVENT_GROUP_CREATE_RSP:
744         case WIFI_DIRECT_CLI_EVENT_GROUP_DESTROY_RSP:
745                 if (!client->connection_cb) {
746                         WDC_LOGE("connection_cb is NULL!!");
747                         break;
748                 }
749                 client->connection_cb(error,
750                                         (wifi_direct_connection_state_e) __wfd_convert_client_event(event),
751                                         param1, client->user_data_for_cb_connection);
752                 break;
753
754         // ToDo:  Handling IP lease event...
755         case WIFI_DIRECT_CLI_EVENT_IP_LEASED_IND:
756                 if (!client->ip_assigned_cb) {
757                         WDC_LOGE("ip_assigned_cb is NULL!!");
758                         break;
759                 }
760                 char *ifname = NULL;
761                 ifname = vconf_get_str(VCONFKEY_IFNAME);
762                 if (!ifname) {
763                         WDC_LOGD("vconf (%s) value is NULL!!!", VCONFKEY_IFNAME);
764                         break;
765                 }
766                 WDC_LOGD("VCONFKEY_IFNAME(%s) : %s", VCONFKEY_IFNAME, ifname);
767                 client->ip_assigned_cb(param1, param2, ifname,
768                                         client->user_data_for_cb_ip_assigned);
769                 free(ifname);
770                 break;
771
772 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
773         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_STARTED:
774         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FOUND:
775         case WIFI_DIRECT_CLI_EVENT_SERVICE_DISCOVERY_FINISHED:
776                 if (!client->service_cb) {
777                         WDC_LOGE("service_cb is NULL!!\n");
778                         break;
779                 }
780                 client->service_cb(error,
781                                         (wifi_direct_service_discovery_state_e) __wfd_convert_client_event(event),
782                                         (wifi_direct_service_type_e) service_type, param2, param1, client->user_data_for_cb_service);
783                 break;
784 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
785
786         default:
787                 break;
788         }
789
790         __WDC_LOG_FUNC_END__;
791
792         return TRUE;
793 }
794
795 void __wfd_client_print_entry_list(wfd_discovery_entry_s *list, int num)
796 {
797         int i = 0;
798
799         WDC_LOGD("------------------------------------------");
800         for (i = 0; i < num; i++)
801         {
802                 WDC_LOGD("== Peer index : %d ==", i);
803                 WDC_LOGD("is Group Owner ? %s", list[i].is_group_owner ? "YES" : "NO");
804                 WDC_LOGD("device_name : %s", list[i].device_name);
805                 WDC_LOGD("MAC address : "MACSECSTR, MAC2SECSTR(list[i].mac_address));
806                 WDC_LOGD("wps cfg method : %x", list[i].wps_cfg_methods);
807                 WDC_LOGD("Device Type: %d - %d", list[i].category, list[i].subcategory);
808                 WDC_LOGD("Listen channel: %d", list[i].channel);
809         }
810         WDC_LOGD("------------------------------------------");
811 }
812
813 void __wfd_client_print_connected_peer_info(wfd_connected_peer_info_s *list, int num)
814 {
815         int i = 0;
816
817         WDC_LOGD("------------------------------------------\n");
818         for (i = 0; i < num; i++) {
819                 WDC_LOGD("== Peer index : %d ==\n", i);
820                 WDC_LOGD("device_name : %s\n", list[i].device_name);
821                 WDC_LOGD("Device MAC : " MACSECSTR "\n", MAC2SECSTR(list[i].mac_address));
822                 WDC_LOGD("Device Type: %d - %d", list[i].category, list[i].subcategory);
823                 WDC_LOGD("channel : %d\n", list[i].channel);
824                 WDC_LOGD("IP ["IPSECSTR"]\n", IP2SECSTR(list[i].ip_address));
825         }
826         WDC_LOGD("------------------------------------------\n");
827 }
828
829 void __wfd_client_print_persistent_group_info(wfd_persistent_group_info_s *list, int num)
830 {
831         int i = 0;
832
833         WDC_LOGD("------------------------------------------\n");
834         for (i = 0; i < num; i++) {
835                 WDC_LOGD("== Persistent Group index : %d ==", i);
836                 WDC_LOGD("ssid : %s", list[i].ssid);
837                 WDC_LOGD("GO MAC : " MACSECSTR, MAC2SECSTR(list[i].go_mac_address));
838         }
839         WDC_LOGD("------------------------------------------\n");
840 }
841
842 static int __wfd_client_async_event_init(int clientid)
843 {
844         __WDC_LOG_FUNC_START__;
845         int sockfd = 0;
846         struct sockaddr_un saddr;
847         wifi_direct_client_request_s req;
848         int res = 0;
849
850         sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
851         if (sockfd < 0) {
852                 WDC_LOGE("Failed to async socket[%s]", strerror(errno));
853                 __WDC_LOG_FUNC_END__;
854                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
855         }
856         WDC_LOGD("Succeeded to create async socket[%d]", sockfd);
857
858         memset(&saddr, 0, sizeof(saddr));
859         saddr.sun_family = AF_UNIX;
860         g_snprintf(saddr.sun_path, sizeof(saddr.sun_path), WFD_SOCK_FILE_PATH);
861
862         WDC_LOGD("Connecting to server socket to register async socket [%d]", sockfd);
863         res = connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));
864         if (res < 0) {
865                 WDC_LOGE("Error!!! connecting to server socket. Error = [%s].", strerror(errno));
866                 close(sockfd);
867                 __WDC_LOG_FUNC_END__;
868                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
869         }
870
871         memset(&req, 0, sizeof(wifi_direct_client_request_s));
872
873         req.cmd = WIFI_DIRECT_CMD_INIT_ASYNC_SOCKET;
874         req.client_id = clientid;
875
876         res = __wfd_client_write_socket(sockfd, &req, sizeof(wifi_direct_client_request_s));
877         if (res < WIFI_DIRECT_ERROR_NONE) {
878                 WDC_LOGE("Failed to write to socket[%s]", strerror(errno));
879                 WDC_LOGE("Error!!! [%s]", __wfd_print_error(res));
880                 close(sockfd);
881                 __WDC_LOG_FUNC_END__;
882                 return res;
883         }
884         g_client_info.async_sockfd = sockfd;
885
886         WDC_LOGE("Async socket is created= %d", sockfd);
887
888         return sockfd;
889 }
890
891 static int __wfd_client_launch_server_dbus(void)
892 {
893         GDBusConnection *netconfig_bus = NULL;
894         GError *g_error = NULL;
895
896 #if !GLIB_CHECK_VERSION(2,36,0)
897         g_type_init();
898 #endif
899         netconfig_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &g_error);
900         if (netconfig_bus == NULL) {
901                 if(g_error != NULL) {
902                         WDC_LOGE("Couldn't connect to system bus "
903                                         "error [%d: %s]", g_error->code, g_error->message);
904                         g_error_free(g_error);
905                 }
906                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
907         }
908
909         g_dbus_connection_call_sync(netconfig_bus,
910                         NETCONFIG_SERVICE,
911                         NETCONFIG_WIFI_PATH,
912                         NETCONFIG_WIFI_INTERFACE,
913                         NETCONFIG_WIFI_LAUNCHDIRECT,
914                         NULL,
915                         NULL,
916                         G_DBUS_CALL_FLAGS_NONE,
917                         DBUS_REPLY_TIMEOUT,
918                         NULL,
919                         &g_error);
920
921         if(g_error !=NULL) {
922                 WDC_LOGE("g_dbus_connection_call_sync() failed"
923                                 "error [%d: %s]", g_error->code, g_error->message);
924                 g_error_free(g_error);
925                 return WIFI_DIRECT_ERROR_PERMISSION_DENIED;
926         }
927
928         g_object_unref(netconfig_bus);
929
930         WDC_LOGD("Successfully launched wfd-manager");
931         return WIFI_DIRECT_ERROR_NONE;
932 }
933
934 int wifi_direct_initialize(void)
935 {
936         __WDC_LOG_FUNC_START__;
937
938         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
939
940         struct sockaddr_un saddr;
941         wifi_direct_client_request_s req;
942         wifi_direct_client_response_s resp;
943         int retry_count = 10;
944         int sockfd = 0;
945         bool wifi_direct_enable;
946         int res = 0;
947
948         if (g_client_info.is_registered == TRUE) {
949                 WDC_LOGW("Warning!!! Already registered\nUpdate user data and callback!");
950                 __WDC_LOG_FUNC_END__;
951                 return WIFI_DIRECT_ERROR_ALREADY_INITIALIZED;
952         }
953         res = system_info_get_platform_bool("tizen.org/feature/network.wifi.direct", &wifi_direct_enable);
954         if (res < 0) {
955                 WDC_LOGE("Failed to get sys info");
956                 __WDC_LOG_FUNC_END__;
957                 return res;
958         }
959         if(!wifi_direct_enable) {
960                 WDC_LOGE("Wi-Fi Direct not supported");
961                 return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
962         }
963         sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
964         if (sockfd < 0) {
965                 WDC_LOGE("Error!!! creating sync socket[%s]", strerror(errno));
966                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
967         }
968         WDC_LOGD("Created sync socket [%d]", sockfd);
969
970         memset(&saddr, 0, sizeof(saddr));
971         saddr.sun_family = AF_UNIX;
972         g_snprintf(saddr.sun_path, sizeof(saddr.sun_path), WFD_SOCK_FILE_PATH);
973
974         WDC_LOGD("Connecting to server socket to register sync socket [%d]", sockfd);
975         while (retry_count > 0) {
976                 res = connect(sockfd, (struct sockaddr*) &saddr, sizeof(saddr));
977                 if (!res){
978                         WDC_LOGD("Succeeded to connect to server socket[%s]", strerror(errno));
979                         break;
980                 }
981
982                 WDC_LOGD("Launching wfd-server..\n");
983                 res = __wfd_client_launch_server_dbus();
984                 if (res != WIFI_DIRECT_ERROR_NONE)
985                         WDC_LOGE("Failed to send dbus msg[%s]", strerror(errno));
986                 retry_count--;
987
988                 /* wait a little before retrying the next socket connection */
989                 usleep(150000);
990         }
991
992         if (res < 0) {
993                 WDC_LOGE("Failed to connect to wfd-manager socket[%s]", strerror(errno));
994                 close(sockfd);
995                 __WDC_LOG_FUNC_END__;
996                 return res;
997         }
998
999         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1000         memset(&resp, 0, sizeof(wifi_direct_client_response_s));
1001
1002         req.cmd = WIFI_DIRECT_CMD_REGISTER;
1003         req.client_id = gettid();
1004         WDC_LOGD("Client ID = %d", req.client_id);
1005
1006         res = __wfd_client_send_request(sockfd, &req, &resp);
1007         if (res < 0) {
1008                 WDC_LOGE("Failed to register client");
1009                 close(sockfd);
1010                 __WDC_LOG_FUNC_END__;
1011                 return res;
1012         }
1013         g_client_info.sync_sockfd = sockfd;
1014         g_client_info.client_id = resp.client_id;
1015         g_client_info.is_registered = TRUE;
1016
1017         int async_sockfd = -1;
1018         async_sockfd = __wfd_client_async_event_init(g_client_info.client_id);
1019         if (async_sockfd < 0) {
1020                 WDC_LOGE("Failed to create async socket \n");
1021                 close(sockfd);
1022                 __wfd_reset_control();
1023                 __WDC_LOG_FUNC_END__;
1024                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
1025         }
1026         g_client_info.async_sockfd = async_sockfd;
1027
1028         GIOChannel *gio = g_io_channel_unix_new(g_client_info.async_sockfd);
1029         int g_source_id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1030                                                         (GIOFunc) __wfd_client_process_event, NULL);
1031         g_io_channel_unref(gio);
1032
1033         g_client_info.g_source_id = g_source_id;
1034
1035         // Initialize callbacks
1036         g_client_info.activation_cb = NULL;
1037         g_client_info.discover_cb = NULL;
1038         g_client_info.connection_cb = NULL;
1039         g_client_info.ip_assigned_cb = NULL;
1040
1041         g_client_info.peer_found_cb = NULL;
1042         g_client_info.user_data_for_cb_activation = NULL;
1043         g_client_info.user_data_for_cb_discover = NULL;
1044         g_client_info.user_data_for_cb_connection = NULL;
1045         g_client_info.user_data_for_cb_ip_assigned = NULL;
1046         g_client_info.user_data_for_cb_peer_found = NULL;
1047         g_client_info.user_data_for_cb_device_name = NULL;
1048
1049 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1050         g_client_info.service_cb = NULL;
1051         g_client_info.user_data_for_cb_service= NULL;
1052 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1053
1054         __WDC_LOG_FUNC_END__;
1055         return WIFI_DIRECT_ERROR_NONE;
1056 }
1057
1058 int wifi_direct_deinitialize(void)
1059 {
1060         __WDC_LOG_FUNC_START__;
1061
1062         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1063
1064         if (g_client_info.is_registered == false) {
1065                 WDC_LOGE("Client is already deregistered");
1066                 __WDC_LOG_FUNC_END__;
1067                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1068         }
1069
1070         g_client_info.activation_cb = NULL;
1071         g_client_info.discover_cb = NULL;
1072         g_client_info.connection_cb = NULL;
1073         g_client_info.ip_assigned_cb = NULL;
1074         g_client_info.peer_found_cb = NULL;
1075         g_client_info.user_data_for_cb_activation = NULL;
1076         g_client_info.user_data_for_cb_discover = NULL;
1077         g_client_info.user_data_for_cb_connection = NULL;
1078         g_client_info.user_data_for_cb_ip_assigned = NULL;
1079         g_client_info.user_data_for_cb_peer_found = NULL;
1080
1081 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1082         g_client_info.service_cb = NULL;
1083         g_client_info.user_data_for_cb_service = NULL;
1084 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1085
1086         wifi_direct_client_request_s req;
1087         wifi_direct_client_response_s rsp;
1088         int res = WIFI_DIRECT_ERROR_NONE;
1089
1090         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1091         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1092
1093         req.cmd = WIFI_DIRECT_CMD_DEREGISTER;
1094         req.client_id = g_client_info.client_id;
1095
1096         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1097         if (res < 0)
1098                 WDC_LOGD("Failed to deinitialize. But continue deinitialization");
1099         else
1100                 WDC_LOGD("Deinit Successfull");
1101
1102         __wfd_reset_control();
1103         __WDC_LOG_FUNC_END__;
1104         return WIFI_DIRECT_ERROR_NONE;
1105 }
1106
1107
1108 int wifi_direct_set_device_state_changed_cb(wifi_direct_device_state_changed_cb cb,
1109                                                                                                 void *user_data)
1110 {
1111         __WDC_LOG_FUNC_START__;
1112
1113         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1114
1115         if (!cb) {
1116                 WDC_LOGE("Invalid parameter");
1117                 __WDC_LOG_FUNC_END__;
1118                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1119         }
1120
1121         if (g_client_info.is_registered == false) {
1122                 WDC_LOGE("Client is not initialized.");
1123                 __WDC_LOG_FUNC_END__;
1124                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1125         }
1126
1127         g_client_info.activation_cb = cb;
1128         g_client_info.user_data_for_cb_activation = user_data;
1129
1130         __WDC_LOG_FUNC_END__;
1131         return WIFI_DIRECT_ERROR_NONE;
1132 }
1133
1134
1135 int wifi_direct_unset_device_state_changed_cb(void)
1136 {
1137         __WDC_LOG_FUNC_START__;
1138
1139         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1140
1141         if (g_client_info.is_registered == false) {
1142                 WDC_LOGE("Client is not initialized.\n");
1143                 __WDC_LOG_FUNC_END__;
1144                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1145         }
1146
1147         g_client_info.activation_cb = NULL;
1148         g_client_info.user_data_for_cb_activation = NULL;
1149
1150         __WDC_LOG_FUNC_END__;
1151         return WIFI_DIRECT_ERROR_NONE;
1152 }
1153
1154
1155 int
1156 wifi_direct_set_discovery_state_changed_cb(wifi_direct_discovery_state_chagned_cb cb,
1157                                                                                                 void *user_data)
1158 {
1159         __WDC_LOG_FUNC_START__;
1160
1161         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1162
1163         if (!cb) {
1164                 WDC_LOGE("Callback is NULL.\n");
1165                 __WDC_LOG_FUNC_END__;
1166                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1167         }
1168
1169         if (g_client_info.is_registered == false) {
1170                 WDC_LOGE("Client is not initialized.\n");
1171                 __WDC_LOG_FUNC_END__;
1172                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1173         }
1174
1175         g_client_info.discover_cb = cb;
1176         g_client_info.user_data_for_cb_discover = user_data;
1177
1178         __WDC_LOG_FUNC_END__;
1179         return WIFI_DIRECT_ERROR_NONE;
1180 }
1181
1182
1183 int wifi_direct_unset_discovery_state_changed_cb(void)
1184 {
1185         __WDC_LOG_FUNC_START__;
1186
1187         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1188
1189         if (g_client_info.is_registered == false) {
1190                 WDC_LOGE("Client is not initialized.\n");
1191                 __WDC_LOG_FUNC_END__;
1192                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1193         }
1194
1195         g_client_info.discover_cb = NULL;
1196         g_client_info.user_data_for_cb_discover = NULL;
1197
1198         __WDC_LOG_FUNC_END__;
1199         return WIFI_DIRECT_ERROR_NONE;
1200 }
1201
1202 int
1203 wifi_direct_set_peer_found_cb(wifi_direct_peer_found_cb cb,
1204                                                                                                 void *user_data)
1205 {
1206         __WDC_LOG_FUNC_START__;
1207
1208         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1209
1210         if (!cb) {
1211                 WDC_LOGE("Callback is NULL.\n");
1212                 __WDC_LOG_FUNC_END__;
1213                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1214         }
1215
1216         if (g_client_info.is_registered == false) {
1217                 WDC_LOGE("Client is not initialized.\n");
1218                 __WDC_LOG_FUNC_END__;
1219                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1220         }
1221
1222         g_client_info.peer_found_cb = cb;
1223         g_client_info.user_data_for_cb_peer_found = user_data;
1224
1225         __WDC_LOG_FUNC_END__;
1226         return WIFI_DIRECT_ERROR_NONE;
1227 }
1228
1229
1230 int wifi_direct_unset_peer_found_cb(void)
1231 {
1232         __WDC_LOG_FUNC_START__;
1233
1234         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1235
1236         if (g_client_info.is_registered == false) {
1237                 WDC_LOGE("Client is not initialized.\n");
1238                 __WDC_LOG_FUNC_END__;
1239                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1240         }
1241
1242         g_client_info.peer_found_cb = NULL;
1243         g_client_info.user_data_for_cb_peer_found = NULL;
1244
1245         __WDC_LOG_FUNC_END__;
1246         return WIFI_DIRECT_ERROR_NONE;
1247 }
1248
1249 int wifi_direct_set_service_state_changed_cb
1250 (wifi_direct_service_state_changed_cb cb, void *user_data)
1251 {
1252 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1253         __WDC_LOG_FUNC_START__;
1254
1255         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
1256
1257         if (!cb) {
1258                 WDC_LOGE("Callback is NULL.");
1259                 __WDC_LOG_FUNC_END__;
1260                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1261         }
1262
1263         if (g_client_info.is_registered == false) {
1264                 WDC_LOGE("Client is not initialized.");
1265                 __WDC_LOG_FUNC_END__;
1266                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1267         }
1268
1269         g_client_info.service_cb = cb;
1270         g_client_info.user_data_for_cb_service = user_data;
1271
1272         __WDC_LOG_FUNC_END__;
1273         return WIFI_DIRECT_ERROR_NONE;
1274 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1275         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
1276 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1277 }
1278
1279
1280 int wifi_direct_unset_service_state_changed_cb(void)
1281 {
1282 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1283         __WDC_LOG_FUNC_START__;
1284
1285         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
1286
1287         if (g_client_info.is_registered == false) {
1288                 WDC_LOGE("Client is not initialized.");
1289                 __WDC_LOG_FUNC_END__;
1290                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1291         }
1292
1293         g_client_info.service_cb = NULL;
1294         g_client_info.user_data_for_cb_service = NULL;
1295
1296         __WDC_LOG_FUNC_END__;
1297         return WIFI_DIRECT_ERROR_NONE;
1298 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1299         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
1300 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1301 }
1302
1303 int wifi_direct_set_connection_state_changed_cb(wifi_direct_connection_state_changed_cb cb,
1304                                                                                                 void *user_data)
1305 {
1306         __WDC_LOG_FUNC_START__;
1307
1308         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1309
1310         if (!cb) {
1311                 WDC_LOGE("Callback is NULL.\n");
1312                 __WDC_LOG_FUNC_END__;
1313                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1314         }
1315
1316         if (g_client_info.is_registered == false) {
1317                 WDC_LOGE("Client is not initialized.\n");
1318                 __WDC_LOG_FUNC_END__;
1319                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1320         }
1321
1322         g_client_info.connection_cb = cb;
1323         g_client_info.user_data_for_cb_connection = user_data;
1324
1325         __WDC_LOG_FUNC_END__;
1326         return WIFI_DIRECT_ERROR_NONE;
1327 }
1328
1329
1330 int wifi_direct_unset_connection_state_changed_cb(void)
1331 {
1332         __WDC_LOG_FUNC_START__;
1333
1334         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1335
1336         if (g_client_info.is_registered == false) {
1337                 WDC_LOGE("Client is not initialized");
1338                 __WDC_LOG_FUNC_END__;
1339                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1340         }
1341
1342         g_client_info.connection_cb = NULL;
1343         g_client_info.user_data_for_cb_connection = NULL;
1344
1345         __WDC_LOG_FUNC_END__;
1346         return WIFI_DIRECT_ERROR_NONE;
1347 }
1348
1349
1350 int wifi_direct_set_client_ip_address_assigned_cb(wifi_direct_client_ip_address_assigned_cb cb,
1351                                                                                                 void* user_data)
1352 {
1353         __WDC_LOG_FUNC_START__;
1354
1355         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1356
1357         if (!cb) {
1358                 WDC_LOGE("Callback is NULL");
1359                 __WDC_LOG_FUNC_END__;
1360                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1361         }
1362
1363         if (g_client_info.is_registered == false) {
1364                 WDC_LOGE("Client is not initialized");
1365                 __WDC_LOG_FUNC_END__;
1366                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1367         }
1368
1369         g_client_info.ip_assigned_cb = cb;
1370         g_client_info.user_data_for_cb_ip_assigned = user_data;
1371
1372         __WDC_LOG_FUNC_END__;
1373         return WIFI_DIRECT_ERROR_NONE;
1374 }
1375
1376 int wifi_direct_unset_client_ip_address_assigned_cb(void)
1377 {
1378         __WDC_LOG_FUNC_START__;
1379
1380         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1381
1382         if (g_client_info.is_registered == false) {
1383                 WDC_LOGE("Client is not initialized");
1384                 __WDC_LOG_FUNC_END__;
1385                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1386         }
1387
1388         g_client_info.ip_assigned_cb = NULL;
1389         g_client_info.user_data_for_cb_ip_assigned = NULL;
1390
1391         __WDC_LOG_FUNC_END__;
1392         return WIFI_DIRECT_ERROR_NONE;
1393 }
1394
1395 int wifi_direct_activate(void)
1396 {
1397         __WDC_LOG_FUNC_START__;
1398
1399         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1400
1401         wifi_direct_client_request_s req;
1402         wifi_direct_client_response_s rsp;
1403         int res = WIFI_DIRECT_ERROR_NONE;
1404
1405         if ((g_client_info.is_registered == false) ||
1406                         (g_client_info.client_id == WFD_INVALID_ID)) {
1407                 WDC_LOGE("Client is NOT registered");
1408                 __WDC_LOG_FUNC_END__;
1409                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1410         }
1411
1412         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1413         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1414
1415         req.cmd = WIFI_DIRECT_CMD_ACTIVATE;
1416         req.client_id = g_client_info.client_id;
1417
1418         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1419         if (res != WIFI_DIRECT_ERROR_NONE) {
1420                 __WDC_LOG_FUNC_END__;
1421                 return res;
1422         }
1423         WDC_LOGD("wifi_direct_activate() SUCCESS");
1424
1425         __WDC_LOG_FUNC_END__;
1426         return WIFI_DIRECT_ERROR_NONE;
1427 }
1428
1429 int wifi_direct_deactivate(void)
1430 {
1431         __WDC_LOG_FUNC_START__;
1432
1433         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1434
1435         wifi_direct_client_request_s req;
1436         wifi_direct_client_response_s rsp;
1437         int res = WIFI_DIRECT_ERROR_NONE;
1438
1439         if ((g_client_info.is_registered == false) ||
1440                         (g_client_info.client_id == WFD_INVALID_ID)) {
1441                 WDC_LOGE("Client is NOT registered");
1442                 __WDC_LOG_FUNC_END__;
1443                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1444         }
1445
1446         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1447         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1448
1449         req.cmd = WIFI_DIRECT_CMD_DEACTIVATE;
1450         req.client_id = g_client_info.client_id;
1451
1452         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1453         if (res != WIFI_DIRECT_ERROR_NONE) {
1454                 __WDC_LOG_FUNC_END__;
1455                 return res;
1456         }
1457         WDC_LOGD("wifi_direct_deactivate() SUCCESS");
1458
1459         __WDC_LOG_FUNC_END__;
1460         return WIFI_DIRECT_ERROR_NONE;
1461 }
1462
1463 int wifi_direct_start_discovery(bool listen_only, int timeout)
1464 {
1465         __WDC_LOG_FUNC_START__;
1466
1467         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1468
1469         wifi_direct_client_request_s req;
1470         wifi_direct_client_response_s rsp;
1471         int res = WIFI_DIRECT_ERROR_NONE;
1472
1473         if ((g_client_info.is_registered == false) ||
1474                         (g_client_info.client_id == WFD_INVALID_ID)) {
1475                 WDC_LOGE("Client is NOT registered");
1476                 __WDC_LOG_FUNC_END__;
1477                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1478         }
1479
1480         if (timeout < 0) {
1481                 WDC_LOGE("Nagative value. Param [timeout]!");
1482                 __WDC_LOG_FUNC_END__;
1483                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1484         }
1485
1486         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1487         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1488
1489         req.cmd = WIFI_DIRECT_CMD_START_DISCOVERY;
1490         req.client_id = g_client_info.client_id;
1491         req.data.int1 = listen_only;
1492         req.data.int2 = timeout;
1493         WDC_LOGE("listen only (%d) timeout (%d)", listen_only, timeout);
1494
1495         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1496         if (res != WIFI_DIRECT_ERROR_NONE) {
1497                 __WDC_LOG_FUNC_END__;
1498                 return res;
1499         }
1500         WDC_LOGD("wifi_direct_start_discovery() SUCCESS");
1501
1502         __WDC_LOG_FUNC_END__;
1503         return WIFI_DIRECT_ERROR_NONE;
1504 }
1505
1506 int wifi_direct_start_discovery_specific_channel(bool listen_only, int timeout, wifi_direct_discovery_channel_e channel)
1507 {
1508         __WDC_LOG_FUNC_START__;
1509
1510         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1511
1512         wifi_direct_client_request_s req;
1513         wifi_direct_client_response_s rsp;
1514         int res = WIFI_DIRECT_ERROR_NONE;
1515
1516         if ((g_client_info.is_registered == false) ||
1517                         (g_client_info.client_id == WFD_INVALID_ID)) {
1518                 WDC_LOGE("Client is NOT registered");
1519                 __WDC_LOG_FUNC_END__;
1520                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1521         }
1522
1523         if (timeout < 0) {
1524                 WDC_LOGE("Nagative value. Param [timeout]!");
1525                 __WDC_LOG_FUNC_END__;
1526                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1527         }
1528
1529         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1530         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1531
1532         req.cmd = WIFI_DIRECT_CMD_START_DISCOVERY_SPECIFIC_CHANNEL;
1533         req.client_id = g_client_info.client_id;
1534         req.data.int1 = timeout;
1535         req.data.int2 = channel;
1536
1537         WDC_LOGD("timeout (%d) channel (%d)", timeout, channel);
1538
1539         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1540         if (res != WIFI_DIRECT_ERROR_NONE) {
1541                 __WDC_LOG_FUNC_END__;
1542                 return res;
1543         }
1544         WDC_LOGD("wifi_direct_start_discovery_specific_channel() SUCCESS");
1545
1546         __WDC_LOG_FUNC_END__;
1547         return WIFI_DIRECT_ERROR_NONE;
1548 }
1549
1550 int wifi_direct_cancel_discovery(void)
1551 {
1552         __WDC_LOG_FUNC_START__;
1553
1554         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1555
1556         wifi_direct_client_request_s req;
1557         wifi_direct_client_response_s rsp;
1558         int res = WIFI_DIRECT_ERROR_NONE;
1559
1560         if ((g_client_info.is_registered == false) ||
1561                         (g_client_info.client_id == WFD_INVALID_ID)) {
1562                 WDC_LOGE("Client is NOT registered");
1563                 __WDC_LOG_FUNC_END__;
1564                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1565         }
1566
1567         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1568         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1569
1570         req.cmd = WIFI_DIRECT_CMD_CANCEL_DISCOVERY;
1571         req.client_id = g_client_info.client_id;
1572
1573         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1574         if (res != WIFI_DIRECT_ERROR_NONE) {
1575                 __WDC_LOG_FUNC_END__;
1576                 return res;
1577         }
1578         WDC_LOGD("wifi_direct_cancel_discovery() SUCCESS");
1579
1580         __WDC_LOG_FUNC_END__;
1581         return WIFI_DIRECT_ERROR_NONE;
1582 }
1583
1584 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1585 static char **get_service_list(char *services, unsigned int *count)
1586 {
1587         __WDC_LOG_FUNC_START__;
1588         char **result = NULL;
1589         char *pos1 = NULL;
1590         char *pos2 = NULL;
1591         unsigned int cnt = 0;
1592         unsigned int i = 0;
1593         unsigned int j = 0;
1594
1595         if (!count || !services || (services && strlen(services) <= 0)) {
1596                 WDC_LOGE("Invalid parameters.");
1597                 __WDC_LOG_FUNC_END__;
1598                 return NULL;
1599         }
1600
1601         pos1 = services;
1602         pos2 = g_strdup(services);
1603
1604         pos1 = strtok (pos1,",\n");
1605         while (pos1) {
1606                 cnt++;
1607                 pos1 = strtok (NULL, ",\n");
1608         }
1609         WDC_LOGD("Total Service Count = %d", cnt);
1610
1611         if (cnt > 0) {
1612                 result = (char**) g_try_malloc0_n(cnt, sizeof(char *));
1613                 if (!result) {
1614                         WDC_LOGE("Failed to allocate memory for result");
1615                         g_free(pos2);
1616                         return NULL;
1617                 }
1618                 pos2 = strtok (pos2,",\n");
1619                 while (pos2 != NULL) {
1620                         char *s = strchr(pos2, ' ');
1621                         if (s) {
1622                                 *s = '\0';
1623                                 result[i++] = strdup(pos2);
1624                                 pos2 = strtok (NULL, ",\n");
1625                         }
1626                 }
1627         }
1628
1629         g_free(pos1);
1630         g_free(pos2);
1631
1632         if (cnt == i) {
1633                 *count = cnt;
1634                 return result;
1635         } else {
1636                 *count = 0;
1637                 if (result) {
1638                         for (j=0; j<i && result[j] != NULL; j++)
1639                                 free(result[j]);
1640                         free(result);
1641                 }
1642                 return NULL;
1643         }
1644 }
1645 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1646
1647 int wifi_direct_foreach_discovered_peers(wifi_direct_discovered_peer_cb cb,
1648                                                                                                 void *user_data)
1649 {
1650         __WDC_LOG_FUNC_START__;
1651
1652         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1653
1654         wifi_direct_client_request_s req;
1655         wifi_direct_client_response_s rsp;
1656         int res = WIFI_DIRECT_ERROR_NONE;
1657         int i;
1658
1659         if ((g_client_info.is_registered == false) ||
1660                         (g_client_info.client_id == WFD_INVALID_ID)) {
1661                 WDC_LOGE("Client is NOT registered");
1662                 __WDC_LOG_FUNC_END__;
1663                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1664         }
1665
1666         if (!cb) {
1667                 WDC_LOGE("NULL Param [callback]!");
1668                 __WDC_LOG_FUNC_END__;
1669                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1670         }
1671
1672         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1673         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1674
1675         req.cmd = WIFI_DIRECT_CMD_GET_DISCOVERY_RESULT;
1676         req.client_id = g_client_info.client_id;
1677
1678         pthread_mutex_lock(&g_client_info.mutex);
1679         res = __wfd_client_write_socket(g_client_info.sync_sockfd, &req, sizeof(wifi_direct_client_request_s));
1680         if (res != WIFI_DIRECT_ERROR_NONE) {
1681                 WDC_LOGE("Failed to write into socket [%s]", __wfd_print_error(res));
1682                 __wfd_reset_control();
1683                 pthread_mutex_unlock(&g_client_info.mutex);
1684                 __WDC_LOG_FUNC_END__;
1685                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
1686         }
1687         WDC_LOGD("Succeeded to send request [%d: %s]", req.cmd, __wfd_client_print_cmd(req.cmd));
1688
1689         res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
1690                                                                 sizeof(wifi_direct_client_response_s));
1691         if (res <= 0) {
1692                 WDC_LOGE("Failed to read socket [%d]", res);
1693                 __wfd_reset_control();
1694                 pthread_mutex_unlock(&g_client_info.mutex);
1695                 __WDC_LOG_FUNC_END__;
1696                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
1697         }
1698
1699         if (rsp.cmd != req.cmd) {
1700                 WDC_LOGE("Invalid resp [%d]", rsp.cmd);
1701                 pthread_mutex_unlock(&g_client_info.mutex);
1702                 __WDC_LOG_FUNC_END__;
1703                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
1704         }
1705
1706         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
1707                 WDC_LOGE("Result received [%s]", __wfd_print_error(rsp.result));
1708                 pthread_mutex_unlock(&g_client_info.mutex);
1709                 __WDC_LOG_FUNC_END__;
1710                 return rsp.result;
1711         }
1712
1713         short num = rsp.param1;
1714         short num_tmp = 0;
1715         wfd_discovery_entry_s *buff = NULL;
1716         wfd_discovery_entry_s *buff_tmp = NULL;
1717
1718         WDC_LOGD("Num of found peers = %d", num);
1719
1720         if (num > 1023) {
1721                 WDC_LOGE("Discovered peer number restricted by 255(real number:%d)", num);
1722                 num_tmp = num -1023;
1723                 num = 1023;
1724         }
1725
1726         if (num > 0) {
1727                 buff = (wfd_discovery_entry_s*) g_try_malloc0_n(num, sizeof (wfd_discovery_entry_s));
1728                 if (!buff) {
1729                         WDC_LOGE("Failed to alloc memory");
1730                         pthread_mutex_unlock(&g_client_info.mutex);
1731                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
1732                 }
1733
1734                 res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff,
1735                                                                         num * sizeof(wfd_discovery_entry_s));
1736                 if (num_tmp) {
1737                         WDC_LOGD("Rest data should be read out");
1738                         buff_tmp = (wfd_discovery_entry_s*) g_try_malloc0_n(num_tmp, sizeof (wfd_discovery_entry_s));
1739                         if (buff_tmp) {
1740                                 __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff_tmp,
1741                                                                                 num_tmp * sizeof(wfd_discovery_entry_s));
1742                                 g_free(buff_tmp);
1743                         }
1744                 }
1745                 pthread_mutex_unlock(&g_client_info.mutex);
1746                 if (res <= 0) {
1747                         free(buff);
1748                         WDC_LOGE("Failed to read socket");
1749                         __wfd_reset_control();
1750                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
1751                 }
1752
1753                 __wfd_client_print_entry_list(buff, num);
1754                 WDC_LOGD("wifi_direct_foreach_discovered_peers() SUCCESS");
1755
1756                 wifi_direct_discovered_peer_info_s *peer_list;
1757
1758                 for (i = 0; i < num; i++) {
1759                         peer_list = (wifi_direct_discovered_peer_info_s *) g_try_malloc0(sizeof(wifi_direct_discovered_peer_info_s));
1760                         peer_list->device_name = g_strndup(buff[i].device_name, WIFI_DIRECT_MAX_DEVICE_NAME_LEN+1);
1761                         peer_list->mac_address = (char*) g_try_malloc0(MACSTR_LEN);
1762                         g_snprintf(peer_list->mac_address, MACSTR_LEN, MACSTR, MAC2STR(buff[i].mac_address));
1763                         peer_list->interface_address = (char*) g_try_malloc0(MACSTR_LEN);
1764                         g_snprintf(peer_list->interface_address, MACSTR_LEN, MACSTR, MAC2STR(buff[i].intf_address));
1765                         peer_list->channel = buff[i].channel;
1766                         peer_list->is_connected = buff[i].is_connected;
1767                         peer_list->is_group_owner = buff[i].is_group_owner;
1768                         peer_list->is_persistent_group_owner = buff[i].is_persistent_go;
1769                         peer_list->primary_device_type = buff[i].category;
1770                         peer_list->secondary_device_type = buff[i].subcategory;
1771                         peer_list->supported_wps_types= buff[i].wps_cfg_methods;
1772 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1773                         peer_list->is_miracast_device = buff[i].is_wfd_device;
1774 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1775
1776                         if (!cb(peer_list, user_data))
1777                                 break;
1778                 }
1779
1780                 g_free(buff);
1781         } else {
1782                 pthread_mutex_unlock(&g_client_info.mutex);
1783         }
1784
1785         __WDC_LOG_FUNC_END__;
1786         return WIFI_DIRECT_ERROR_NONE;
1787 }
1788
1789 int wifi_direct_connect(char *mac_address)
1790 {
1791         __WDC_LOG_FUNC_START__;
1792
1793         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1794
1795         unsigned char la_mac_addr[6];
1796         wifi_direct_client_request_s req;
1797         wifi_direct_client_response_s rsp;
1798         int res = WIFI_DIRECT_ERROR_NONE;
1799
1800         if ((g_client_info.is_registered == false) ||
1801                         (g_client_info.client_id == WFD_INVALID_ID)) {
1802                 WDC_LOGE("Client is NOT registered");
1803                 __WDC_LOG_FUNC_END__;
1804                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1805         }
1806
1807         if (!mac_address) {
1808                 WDC_LOGE("mac_addr is NULL");
1809                 __WDC_LOG_FUNC_END__;
1810                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1811         }
1812
1813         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1814         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1815
1816         req.cmd = WIFI_DIRECT_CMD_CONNECT;
1817         req.client_id = g_client_info.client_id;
1818         macaddr_atoe(mac_address, la_mac_addr);
1819         memcpy(req.data.mac_addr, la_mac_addr, MACADDR_LEN);
1820
1821         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1822         if (res != WIFI_DIRECT_ERROR_NONE) {
1823                 __WDC_LOG_FUNC_END__;
1824                 return res;
1825         }
1826         WDC_LOGD("wifi_direct_connect() SUCCESS");
1827
1828         __WDC_LOG_FUNC_END__;
1829         return WIFI_DIRECT_ERROR_NONE;
1830 }
1831
1832 int wifi_direct_cancel_connection(char *mac_address)
1833 {
1834         __WDC_LOG_FUNC_START__;
1835
1836         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1837
1838         wifi_direct_client_request_s req;
1839         wifi_direct_client_response_s rsp;
1840         int res = WIFI_DIRECT_ERROR_NONE;
1841
1842         if ((g_client_info.is_registered == false)
1843                 || (g_client_info.client_id == WFD_INVALID_ID))
1844         {
1845                 WDC_LOGE("Client is NOT registered.");
1846                 __WDC_LOG_FUNC_END__;
1847                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1848         }
1849
1850         if (!mac_address) {
1851                 WDC_LOGE("mac_addr is NULL");
1852                 __WDC_LOG_FUNC_END__;
1853                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1854         }
1855
1856         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1857         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1858
1859         req.cmd = WIFI_DIRECT_CMD_CANCEL_CONNECTION;
1860         req.client_id = g_client_info.client_id;
1861         macaddr_atoe(mac_address, req.data.mac_addr);
1862
1863         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1864         if (res != WIFI_DIRECT_ERROR_NONE) {
1865                 __WDC_LOG_FUNC_END__;
1866                 return res;
1867         }
1868         WDC_LOGD("wifi_direct_cancel_connect() SUCCESS");
1869
1870         __WDC_LOG_FUNC_END__;
1871         return WIFI_DIRECT_ERROR_NONE;
1872 }
1873
1874
1875 int wifi_direct_reject_connection(char *mac_address)
1876 {
1877         __WDC_LOG_FUNC_START__;
1878
1879         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1880
1881         wifi_direct_client_request_s req;
1882         wifi_direct_client_response_s rsp;
1883         int res = WIFI_DIRECT_ERROR_NONE;
1884
1885         if ((g_client_info.is_registered == false) ||
1886                         (g_client_info.client_id == WFD_INVALID_ID)) {
1887                 WDC_LOGE("Client is NOT registered");
1888                 __WDC_LOG_FUNC_END__;
1889                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1890         }
1891
1892         if (!mac_address) {
1893                 WDC_LOGE("mac_addr is NULL");
1894                 __WDC_LOG_FUNC_END__;
1895                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1896         }
1897
1898         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1899         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1900
1901         req.cmd = WIFI_DIRECT_CMD_REJECT_CONNECTION;
1902         req.client_id = g_client_info.client_id;
1903         macaddr_atoe(mac_address, req.data.mac_addr);
1904
1905         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1906         if (res != WIFI_DIRECT_ERROR_NONE) {
1907                 __WDC_LOG_FUNC_END__;
1908                 return res;
1909         }
1910         WDC_LOGE("wifi_direct_reject_connection() SUCCESS");
1911
1912         __WDC_LOG_FUNC_END__;
1913         return WIFI_DIRECT_ERROR_NONE;
1914 }
1915
1916
1917 int wifi_direct_disconnect_all(void)
1918 {
1919         __WDC_LOG_FUNC_START__;
1920
1921         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1922
1923         wifi_direct_client_request_s req;
1924         wifi_direct_client_response_s rsp;
1925         int res = WIFI_DIRECT_ERROR_NONE;
1926
1927         if ((g_client_info.is_registered == false) ||
1928                         (g_client_info.client_id == WFD_INVALID_ID)) {
1929                 WDC_LOGE("Client is NOT registered");
1930                 __WDC_LOG_FUNC_END__;
1931                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1932         }
1933
1934         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1935         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1936
1937         req.cmd = WIFI_DIRECT_CMD_DISCONNECT_ALL;
1938         req.client_id = g_client_info.client_id;
1939
1940         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1941         if (res != WIFI_DIRECT_ERROR_NONE) {
1942                 __WDC_LOG_FUNC_END__;
1943                 return res;
1944         }
1945         WDC_LOGE("wifi_direct_disconnect_all() SUCCESS");
1946
1947         __WDC_LOG_FUNC_END__;
1948         return WIFI_DIRECT_ERROR_NONE;
1949 }
1950
1951
1952 int wifi_direct_disconnect(char *mac_address)
1953 {
1954         __WDC_LOG_FUNC_START__;
1955
1956         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
1957
1958         unsigned char la_mac_addr[6];
1959         wifi_direct_client_request_s req;
1960         wifi_direct_client_response_s rsp;
1961         int res = WIFI_DIRECT_ERROR_NONE;
1962
1963         if ((g_client_info.is_registered == false) ||
1964                         (g_client_info.client_id == WFD_INVALID_ID)) {
1965                 WDC_LOGE("Client is NOT registered");
1966                 __WDC_LOG_FUNC_END__;
1967                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
1968         }
1969
1970         if (!mac_address) {
1971                 WDC_LOGE("mac_address is NULL");
1972                 __WDC_LOG_FUNC_END__;
1973                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
1974         }
1975
1976         memset(&req, 0, sizeof(wifi_direct_client_request_s));
1977         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
1978
1979         req.cmd = WIFI_DIRECT_CMD_DISCONNECT;
1980         req.client_id = g_client_info.client_id;
1981         macaddr_atoe(mac_address, la_mac_addr);
1982         memcpy(req.data.mac_addr, la_mac_addr, 6);
1983
1984         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
1985         if (res != WIFI_DIRECT_ERROR_NONE) {
1986                 __WDC_LOG_FUNC_END__;
1987                 return res;
1988         }
1989         WDC_LOGE("wifi_direct_disconnect() SUCCESS");
1990
1991         __WDC_LOG_FUNC_END__;
1992         return WIFI_DIRECT_ERROR_NONE;
1993
1994 }
1995
1996 int wifi_direct_accept_connection(char *mac_address)
1997 {
1998         __WDC_LOG_FUNC_START__;
1999
2000         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2001
2002         unsigned char la_mac_addr[6];
2003         wifi_direct_client_request_s req;
2004         wifi_direct_client_response_s rsp;
2005         int res = WIFI_DIRECT_ERROR_NONE;
2006
2007         if ((g_client_info.is_registered == false) ||
2008                         (g_client_info.client_id == WFD_INVALID_ID)) {
2009                 WDC_LOGE("Client is NOT registered");
2010                 __WDC_LOG_FUNC_END__;
2011                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2012         }
2013
2014         if (!mac_address) {
2015                 WDC_LOGE("mac_addr is NULL");
2016                 __WDC_LOG_FUNC_END__;
2017                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2018         }
2019
2020         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2021         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2022
2023         req.cmd = WIFI_DIRECT_CMD_SEND_CONNECT_REQ;
2024         req.client_id = g_client_info.client_id;
2025         macaddr_atoe(mac_address, la_mac_addr);
2026         memcpy(req.data.mac_addr, la_mac_addr, 6);
2027
2028         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2029         if (res != WIFI_DIRECT_ERROR_NONE) {
2030                 __WDC_LOG_FUNC_END__;
2031                 return res;
2032         }
2033         WDC_LOGE("wifi_direct_connect() SUCCESS \n");
2034
2035         __WDC_LOG_FUNC_END__;
2036         return WIFI_DIRECT_ERROR_NONE;
2037 }
2038
2039
2040 int wifi_direct_foreach_connected_peers(wifi_direct_connected_peer_cb cb,
2041                                                                                                 void *user_data)
2042 {
2043         __WDC_LOG_FUNC_START__;
2044
2045         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2046
2047         wifi_direct_client_request_s req;
2048         wifi_direct_client_response_s rsp;
2049         int res = WIFI_DIRECT_ERROR_NONE;
2050         int i;
2051
2052         if ((g_client_info.is_registered == false) ||
2053                         (g_client_info.client_id == WFD_INVALID_ID)) {
2054                 WDC_LOGE("Client is NOT registered");
2055                 __WDC_LOG_FUNC_END__;
2056                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2057         }
2058
2059         if (!cb) {
2060                 WDC_LOGE("NULL Param [callback]!");
2061                 __WDC_LOG_FUNC_END__;
2062                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2063         }
2064
2065         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2066         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2067
2068         req.cmd = WIFI_DIRECT_CMD_GET_CONNECTED_PEERS_INFO;
2069         req.client_id = g_client_info.client_id;
2070
2071         pthread_mutex_lock(&g_client_info.mutex);
2072         res = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
2073                                                                         sizeof(wifi_direct_client_request_s));
2074         if (res != WIFI_DIRECT_ERROR_NONE) {
2075                 WDC_LOGE("Failed to write into socket [%s]", __wfd_print_error(res));
2076                 __wfd_reset_control();
2077                 pthread_mutex_unlock(&g_client_info.mutex);
2078                 __WDC_LOG_FUNC_END__;
2079                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2080         }
2081         WDC_LOGD("Succeeded to send request [%d: %s]", req.cmd, __wfd_client_print_cmd(req.cmd));
2082
2083         res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
2084                                                                 sizeof(wifi_direct_client_response_s));
2085         if (res <= 0) {
2086                 WDC_LOGE("Failed to read socket [%d]", res);
2087                 __wfd_reset_control();
2088                 pthread_mutex_unlock(&g_client_info.mutex);
2089                 __WDC_LOG_FUNC_END__;
2090                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2091         }
2092
2093         if (rsp.cmd != req.cmd) {
2094                 WDC_LOGE("Invalid resp [%d]", rsp.cmd);
2095                 pthread_mutex_unlock(&g_client_info.mutex);
2096                 __WDC_LOG_FUNC_END__;
2097                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2098         }
2099
2100         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
2101                 WDC_LOGE("Result received [%s]", __wfd_print_error(rsp.result));
2102                 pthread_mutex_unlock(&g_client_info.mutex);
2103                 __WDC_LOG_FUNC_END__;
2104                 return rsp.result;
2105         }
2106
2107         short num = rsp.param1;
2108         wfd_connected_peer_info_s *buff = NULL;
2109
2110         WDC_LOGD("Num of connected peers = %d", (int) rsp.param1);
2111
2112         if (num > 8 || num < 1) {
2113                 WDC_LOGE("Invalid number of connected peer(%d)", num);
2114                 buff = (wfd_connected_peer_info_s*) g_try_malloc0_n(num, sizeof(wfd_connected_peer_info_s));
2115                 if (!buff) {
2116                         WDC_LOGE("malloc() failed!!!");
2117                         pthread_mutex_unlock(&g_client_info.mutex);
2118                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
2119                 }
2120
2121                 res= __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff,
2122                                                                 num * sizeof(wfd_connected_peer_info_s));
2123                 pthread_mutex_unlock(&g_client_info.mutex);
2124         } else if (num < 1) {
2125                 WDC_LOGE("Invalid number of connected peer(%d)", num);
2126                 pthread_mutex_unlock(&g_client_info.mutex);
2127         } else {
2128                 buff = (wfd_connected_peer_info_s*) g_try_malloc0_n(num, sizeof(wfd_connected_peer_info_s));
2129                 if (!buff) {
2130                         WDC_LOGE("malloc() failed!!!");
2131                         pthread_mutex_unlock(&g_client_info.mutex);
2132                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
2133                 }
2134
2135                 res= __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff,
2136                                                                 num * sizeof(wfd_connected_peer_info_s));
2137                 pthread_mutex_unlock(&g_client_info.mutex);
2138                 if (res <= 0) {
2139                         free(buff);
2140                         WDC_LOGE("socket read error");
2141                         __wfd_reset_control();
2142                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
2143                 }
2144
2145                 __wfd_client_print_connected_peer_info(buff, num);
2146                 WDC_LOGD("wifi_direct_foreach_connected_peers() SUCCESS");
2147
2148                 wifi_direct_connected_peer_info_s *peer_list = NULL;
2149
2150                 for (i = 0; i < num; i++) {
2151                         peer_list = (wifi_direct_connected_peer_info_s *) g_try_malloc0(sizeof(wifi_direct_connected_peer_info_s));
2152                         peer_list->device_name = g_strdup(buff[i].device_name);
2153                         peer_list->ip_address= (char*) g_try_malloc0(IPSTR_LEN);
2154                         g_snprintf(peer_list->ip_address, IPSTR_LEN, IPSTR, IP2STR(buff[i].ip_address));
2155                         peer_list->mac_address = (char*) calloc(1, MACSTR_LEN);
2156                         g_snprintf(peer_list->mac_address, MACSTR_LEN, MACSTR, MAC2STR(buff[i].mac_address));
2157                         peer_list->interface_address = (char*) g_try_malloc0( MACSTR_LEN);
2158                         g_snprintf(peer_list->interface_address, MACSTR_LEN, MACSTR, MAC2STR(buff[i].intf_address));
2159                         peer_list->p2p_supported = buff[i].is_p2p;
2160                         peer_list->primary_device_type = buff[i].category;
2161                         peer_list->secondary_device_type = buff[i].subcategory;
2162                         peer_list->channel = buff[i].channel;
2163 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
2164                         peer_list->is_miracast_device = buff[i].is_wfd_device;
2165 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
2166
2167                         if (!cb(peer_list, user_data))
2168                                 break;
2169                 }
2170                 g_free(buff);
2171                 buff = NULL;
2172         }
2173         g_free(buff);
2174
2175         __WDC_LOG_FUNC_END__;
2176         return WIFI_DIRECT_ERROR_NONE;
2177 }
2178
2179
2180 int wifi_direct_create_group(void)
2181 {
2182         __WDC_LOG_FUNC_START__;
2183
2184         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2185
2186         wifi_direct_client_request_s req;
2187         wifi_direct_client_response_s rsp;
2188         int res = WIFI_DIRECT_ERROR_NONE;
2189
2190         if ((g_client_info.is_registered == false) ||
2191                         (g_client_info.client_id == WFD_INVALID_ID)) {
2192                 WDC_LOGE("Client is NOT registered");
2193                 __WDC_LOG_FUNC_END__;
2194                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2195         }
2196
2197         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2198         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2199
2200         req.cmd = WIFI_DIRECT_CMD_CREATE_GROUP;
2201         req.client_id = g_client_info.client_id;
2202
2203         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2204         if (res != WIFI_DIRECT_ERROR_NONE) {
2205                 __WDC_LOG_FUNC_END__;
2206                 return res;
2207         }
2208         WDC_LOGE("wifi_direct_create_group() SUCCESS \n");
2209
2210         __WDC_LOG_FUNC_END__;
2211         return WIFI_DIRECT_ERROR_NONE;
2212 }
2213
2214
2215 int wifi_direct_destroy_group(void)
2216 {
2217         __WDC_LOG_FUNC_START__;
2218
2219         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2220
2221         wifi_direct_client_request_s req;
2222         wifi_direct_client_response_s rsp;
2223         int res = WIFI_DIRECT_ERROR_NONE;
2224
2225         if ((g_client_info.is_registered == false) ||
2226                         (g_client_info.client_id == WFD_INVALID_ID)) {
2227                 WDC_LOGE("Client is NOT registered");
2228                 __WDC_LOG_FUNC_END__;
2229                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2230         }
2231
2232         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2233         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2234
2235         req.cmd = WIFI_DIRECT_CMD_DESTROY_GROUP;
2236         req.client_id = g_client_info.client_id;
2237
2238         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2239         if (res != WIFI_DIRECT_ERROR_NONE) {
2240                 __WDC_LOG_FUNC_END__;
2241                 return res;
2242         }
2243         WDC_LOGE("wifi_direct_destroy_group() SUCCESS");
2244
2245         __WDC_LOG_FUNC_END__;
2246         return WIFI_DIRECT_ERROR_NONE;
2247 }
2248
2249
2250 int wifi_direct_is_group_owner(bool *owner)
2251 {
2252         __WDC_LOG_FUNC_START__;
2253
2254         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2255
2256         wifi_direct_client_request_s req;
2257         wifi_direct_client_response_s rsp;
2258         int res = WIFI_DIRECT_ERROR_NONE;
2259
2260         if ((g_client_info.is_registered == false) ||
2261                         (g_client_info.client_id == WFD_INVALID_ID)) {
2262                 WDC_LOGE("Client is NOT registered");
2263                 __WDC_LOG_FUNC_END__;
2264                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2265         }
2266
2267         if (!owner) {
2268                 WDC_LOGE("NULL Param [owner]!");
2269                 __WDC_LOG_FUNC_END__;
2270                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2271         }
2272
2273         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2274         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2275
2276         req.cmd = WIFI_DIRECT_CMD_IS_GROUPOWNER;
2277         req.client_id = g_client_info.client_id;
2278
2279         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2280         if (res != WIFI_DIRECT_ERROR_NONE) {
2281                 __WDC_LOG_FUNC_END__;
2282                 return res;
2283         }
2284         WDC_LOGD("wifi_direct_is_group_owner() SUCCESS");
2285         *owner = (bool) rsp.param1;
2286
2287         __WDC_LOG_FUNC_END__;
2288         return WIFI_DIRECT_ERROR_NONE;
2289 }
2290
2291 int wifi_direct_is_autonomous_group(bool *autonomous_group)
2292 {
2293         __WDC_LOG_FUNC_START__;
2294
2295         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2296
2297         wifi_direct_client_request_s req;
2298         wifi_direct_client_response_s rsp;
2299         int res = WIFI_DIRECT_ERROR_NONE;
2300
2301         if ((g_client_info.is_registered == false) ||
2302                         (g_client_info.client_id == WFD_INVALID_ID)) {
2303                 WDC_LOGE("Client is NOT registered");
2304                 __WDC_LOG_FUNC_END__;
2305                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2306         }
2307
2308         if (!autonomous_group) {
2309                 WDC_LOGE("NULL Param [autonomous_group]!\n");
2310                 __WDC_LOG_FUNC_END__;
2311                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2312         }
2313
2314         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2315         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2316
2317         req.cmd = WIFI_DIRECT_CMD_IS_AUTONOMOUS_GROUP;
2318         req.client_id = g_client_info.client_id;
2319
2320         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2321         if (res != WIFI_DIRECT_ERROR_NONE) {
2322                 __WDC_LOG_FUNC_END__;
2323                 return res;
2324         }
2325         WDC_LOGD("wifi_direct_is_autonomous_group() SUCCESS");
2326         *autonomous_group = (bool) rsp.param1;
2327
2328         __WDC_LOG_FUNC_END__;
2329         return WIFI_DIRECT_ERROR_NONE;
2330 }
2331
2332
2333 int wifi_direct_set_group_owner_intent(int intent)
2334 {
2335         __WDC_LOG_FUNC_START__;
2336
2337         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2338
2339         wifi_direct_client_request_s req;
2340         wifi_direct_client_response_s rsp;
2341         int res = WIFI_DIRECT_ERROR_NONE;
2342
2343         if ((g_client_info.is_registered == false) ||
2344                         (g_client_info.client_id == WFD_INVALID_ID)) {
2345                 WDC_LOGE("Client is NOT registered");
2346                 __WDC_LOG_FUNC_END__;
2347                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2348         }
2349
2350         if (intent < 0 || intent > 15) {
2351                 WDC_LOGE("Invalid Param : intent[%d]", intent);
2352                 __WDC_LOG_FUNC_END__;
2353                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2354         }
2355
2356         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2357         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2358
2359         req.cmd = WIFI_DIRECT_CMD_SET_GO_INTENT;
2360         req.client_id = g_client_info.client_id;
2361         req.data.int1 = intent;
2362
2363         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2364         if (res != WIFI_DIRECT_ERROR_NONE) {
2365                 __WDC_LOG_FUNC_END__;
2366                 return res;
2367         }
2368         WDC_LOGD("wifi_direct_set_group_owner_intent() SUCCESS");
2369
2370         __WDC_LOG_FUNC_END__;
2371         return WIFI_DIRECT_ERROR_NONE;
2372 }
2373
2374 int wifi_direct_get_group_owner_intent(int *intent)
2375 {
2376         __WDC_LOG_FUNC_START__;
2377
2378         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2379
2380         wifi_direct_client_request_s req;
2381         wifi_direct_client_response_s rsp;
2382         int res = WIFI_DIRECT_ERROR_NONE;
2383
2384         if ((g_client_info.is_registered == false) ||
2385                         (g_client_info.client_id == WFD_INVALID_ID)) {
2386                 WDC_LOGE("Client is NOT registered");
2387                 __WDC_LOG_FUNC_END__;
2388                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2389         }
2390
2391         if (!intent) {
2392                 WDC_LOGE("Invalid Parameter");
2393                 __WDC_LOG_FUNC_END__;
2394                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2395         }
2396
2397         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2398         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2399
2400         req.cmd = WIFI_DIRECT_CMD_GET_GO_INTENT;
2401         req.client_id = g_client_info.client_id;
2402
2403         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2404         if (res != WIFI_DIRECT_ERROR_NONE) {
2405                 __WDC_LOG_FUNC_END__;
2406                 return res;
2407         }
2408         WDC_LOGD("int wifi_direct_get_group_owner_intent() intent[%d] SUCCESS", rsp.param1);
2409         *intent = rsp.param1;
2410
2411         __WDC_LOG_FUNC_END__;
2412         return WIFI_DIRECT_ERROR_NONE;
2413 }
2414
2415 int wifi_direct_set_max_clients(int max)
2416 {
2417         __WDC_LOG_FUNC_START__;
2418
2419         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2420
2421         wifi_direct_client_request_s req;
2422         wifi_direct_client_response_s rsp;
2423         int res = WIFI_DIRECT_ERROR_NONE;
2424
2425         if ((g_client_info.is_registered == false) ||
2426                         (g_client_info.client_id == WFD_INVALID_ID)) {
2427                 WDC_LOGE("Client is NOT registered");
2428                 __WDC_LOG_FUNC_END__;
2429                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2430         }
2431         WDC_LOGD("max client [%d]\n", max);
2432
2433         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2434         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2435
2436         req.cmd = WIFI_DIRECT_CMD_SET_MAX_CLIENT;
2437         req.client_id = g_client_info.client_id;
2438         req.data.int1 = max;
2439
2440         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2441         if (res != WIFI_DIRECT_ERROR_NONE) {
2442                 __WDC_LOG_FUNC_END__;
2443                 return res;
2444         }
2445         WDC_LOGD("int wifi_direct_set_max_clients() SUCCESS");
2446
2447         __WDC_LOG_FUNC_END__;
2448         return WIFI_DIRECT_ERROR_NONE;
2449 }
2450
2451 int wifi_direct_get_max_clients(int *max)
2452 {
2453         __WDC_LOG_FUNC_START__;
2454
2455         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2456
2457         wifi_direct_client_request_s req;
2458         wifi_direct_client_response_s rsp;
2459         int res = WIFI_DIRECT_ERROR_NONE;
2460
2461         if ((g_client_info.is_registered == false) ||
2462                         (g_client_info.client_id == WFD_INVALID_ID)) {
2463                 WDC_LOGE("Client is NOT registered");
2464                 __WDC_LOG_FUNC_END__;
2465                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2466         }
2467
2468         if (!max) {
2469                 WDC_LOGE("Invalid Parameter");
2470                 __WDC_LOG_FUNC_END__;
2471                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2472         }
2473
2474         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2475         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2476
2477         req.cmd = WIFI_DIRECT_CMD_GET_MAX_CLIENT;
2478         req.client_id = g_client_info.client_id;
2479
2480         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2481         if (res != WIFI_DIRECT_ERROR_NONE) {
2482                 __WDC_LOG_FUNC_END__;
2483                 return res;
2484         }
2485         WDC_LOGD("int wifi_direct_get_max_clients() max_client[%d] SUCCESS", rsp.param1);
2486         *max = rsp.param1;
2487
2488         __WDC_LOG_FUNC_END__;
2489         return WIFI_DIRECT_ERROR_NONE;
2490 }
2491
2492 int wifi_direct_get_operating_channel(int *channel)
2493 {
2494         __WDC_LOG_FUNC_START__;
2495
2496         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2497
2498         wifi_direct_client_request_s req;
2499         wifi_direct_client_response_s rsp;
2500         int res = WIFI_DIRECT_ERROR_NONE;
2501
2502         if ((g_client_info.is_registered == false) ||
2503                         (g_client_info.client_id == WFD_INVALID_ID)) {
2504                 WDC_LOGE("Client is NOT registered");
2505                 __WDC_LOG_FUNC_END__;
2506                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2507         }
2508
2509         if (!channel) {
2510                 WDC_LOGE("NULL Param [channel]!\n");
2511                 __WDC_LOG_FUNC_END__;
2512                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2513         }
2514
2515         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2516         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2517
2518         req.cmd = WIFI_DIRECT_CMD_GET_OPERATING_CHANNEL;
2519         req.client_id = g_client_info.client_id;
2520
2521         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2522         if (res != WIFI_DIRECT_ERROR_NONE) {
2523                 __WDC_LOG_FUNC_END__;
2524                 return res;
2525         }
2526         WDC_LOGD("channel = [%d]", (int) rsp.param1);
2527         *channel = rsp.param1;
2528
2529         __WDC_LOG_FUNC_END__;
2530
2531         return WIFI_DIRECT_ERROR_NONE;
2532
2533 }
2534
2535 int wifi_direct_activate_pushbutton(void)
2536 {
2537         __WDC_LOG_FUNC_START__;
2538
2539         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2540
2541         wifi_direct_client_request_s req;
2542         wifi_direct_client_response_s rsp;
2543         int res = WIFI_DIRECT_ERROR_NONE;
2544
2545         if ((g_client_info.is_registered == false) ||
2546                         (g_client_info.client_id == WFD_INVALID_ID)) {
2547                 WDC_LOGE("Client is NOT registered");
2548                 __WDC_LOG_FUNC_END__;
2549                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2550         }
2551
2552         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2553         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2554
2555         req.cmd = WIFI_DIRECT_CMD_ACTIVATE_PUSHBUTTON;
2556         req.client_id = g_client_info.client_id;
2557
2558         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2559         if (res != WIFI_DIRECT_ERROR_NONE) {
2560                 __WDC_LOG_FUNC_END__;
2561                 return res;
2562         }
2563         WDC_LOGD("wifi_direct_activate_pushbutton() SUCCESS");
2564
2565         __WDC_LOG_FUNC_END__;
2566         return WIFI_DIRECT_ERROR_NONE;
2567 }
2568
2569 int wifi_direct_set_wps_pin(char *pin)
2570 {
2571         __WDC_LOG_FUNC_START__;
2572
2573         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2574
2575         wifi_direct_client_request_s req;
2576         wifi_direct_client_response_s rsp;
2577         int status = WIFI_DIRECT_ERROR_NONE;
2578
2579         if ((g_client_info.is_registered == false) ||
2580                         (g_client_info.client_id == WFD_INVALID_ID)) {
2581                 WDC_LOGE("Client is NOT registered");
2582                 __WDC_LOG_FUNC_END__;
2583                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2584         }
2585
2586         if (!pin) {
2587                 WDC_LOGE("NULL Param [pin]!");
2588                 __WDC_LOG_FUNC_END__;
2589                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2590         }
2591         WDC_LOGE("pin = [%s]\n", pin);
2592
2593         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2594         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2595
2596         req.cmd = WIFI_DIRECT_CMD_SET_WPS_PIN;
2597         req.client_id = g_client_info.client_id;
2598         req.cmd_data_len = WIFI_DIRECT_WPS_PIN_LEN+1;
2599
2600         pthread_mutex_lock(&g_client_info.mutex);
2601         status = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
2602                                                                   sizeof(wifi_direct_client_request_s));
2603         if (status != WIFI_DIRECT_ERROR_NONE) {
2604                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
2605                 __wfd_reset_control();
2606                 pthread_mutex_unlock(&g_client_info.mutex);
2607                 __WDC_LOG_FUNC_END__;
2608                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2609         }
2610         WDC_LOGD("writing msg hdr is success!\n");
2611
2612         status = __wfd_client_write_socket(g_client_info.sync_sockfd, pin,
2613                                                                   WIFI_DIRECT_WPS_PIN_LEN);
2614         if (status != WIFI_DIRECT_ERROR_NONE) {
2615                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
2616                 __wfd_reset_control();
2617                 pthread_mutex_unlock(&g_client_info.mutex);
2618                 __WDC_LOG_FUNC_END__;
2619                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2620         }
2621
2622         status = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
2623                                                                   sizeof(wifi_direct_client_response_s));
2624         pthread_mutex_unlock(&g_client_info.mutex);
2625         if (status <= 0) {
2626                 WDC_LOGE("Error!!! reading socket, status = %d", status);
2627                 __wfd_reset_control();
2628                 __WDC_LOG_FUNC_END__;
2629                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
2630         }
2631
2632         if (rsp.cmd != req.cmd) {
2633                 WDC_LOGE("Error!!! Invalid resp cmd = %d", rsp.cmd);
2634                 __WDC_LOG_FUNC_END__;
2635                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
2636         }
2637
2638         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
2639                 WDC_LOGD("Error!!! Result received = %d", rsp.result);
2640                 __WDC_LOG_FUNC_END__;
2641                 return rsp.result;
2642         }
2643
2644         __WDC_LOG_FUNC_END__;
2645         return WIFI_DIRECT_ERROR_NONE;
2646 }
2647
2648
2649 int wifi_direct_get_wps_pin(char **pin)
2650 {
2651         __WDC_LOG_FUNC_START__;
2652
2653         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2654
2655         wifi_direct_client_request_s req;
2656         wifi_direct_client_response_s rsp;
2657         char la_pin[WIFI_DIRECT_WPS_PIN_LEN + 1] = { 0, };
2658         int res = WIFI_DIRECT_ERROR_NONE;
2659
2660         if ((g_client_info.is_registered == false) ||
2661                         (g_client_info.client_id == WFD_INVALID_ID)) {
2662                 WDC_LOGE("Client is NOT registered");
2663                 __WDC_LOG_FUNC_END__;
2664                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2665         }
2666
2667         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2668         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2669
2670         req.cmd = WIFI_DIRECT_CMD_GET_WPS_PIN;
2671         req.client_id = g_client_info.client_id;
2672
2673         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2674         if (res != WIFI_DIRECT_ERROR_NONE) {
2675                 __WDC_LOG_FUNC_END__;
2676                 return res;
2677         }
2678         WDC_LOGD("wifi_direct_get_wps_pin() SUCCESS");
2679         g_strlcpy(la_pin, rsp.param2, WIFI_DIRECT_WPS_PIN_LEN +1);
2680         *pin = g_strdup(la_pin);
2681
2682         __WDC_LOG_FUNC_END__;
2683         return WIFI_DIRECT_ERROR_NONE;
2684 }
2685
2686 int wifi_direct_get_supported_wps_mode(int *wps_mode)
2687 {
2688         __WDC_LOG_FUNC_START__;
2689
2690         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2691
2692         wifi_direct_client_request_s req;
2693         wifi_direct_client_response_s rsp;
2694         int res = WIFI_DIRECT_ERROR_NONE;
2695
2696         if ((g_client_info.is_registered == false) ||
2697                         (g_client_info.client_id == WFD_INVALID_ID)) {
2698                 WDC_LOGE("Client is NOT registered");
2699                 __WDC_LOG_FUNC_END__;
2700                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2701         }
2702
2703         if (!wps_mode) {
2704                 WDC_LOGE("NULL Param [wps_mode]!");
2705                 __WDC_LOG_FUNC_END__;
2706                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2707         }
2708
2709         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2710         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2711
2712         req.cmd = WIFI_DIRECT_CMD_GET_SUPPORTED_WPS_MODE;
2713         req.client_id = g_client_info.client_id;
2714
2715         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2716         if (res != WIFI_DIRECT_ERROR_NONE) {
2717                 __WDC_LOG_FUNC_END__;
2718                 return res;
2719         }
2720         WDC_LOGD("Supported wps config = [%d]", (int) rsp.param1);
2721         *wps_mode = rsp.param1;
2722
2723         __WDC_LOG_FUNC_END__;
2724         return WIFI_DIRECT_ERROR_NONE;
2725 }
2726
2727 int wifi_direct_foreach_supported_wps_types(wifi_direct_supported_wps_type_cb cb, void* user_data)
2728 {
2729         __WDC_LOG_FUNC_START__;
2730
2731         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2732
2733         wifi_direct_client_request_s req;
2734         wifi_direct_client_response_s rsp;
2735         int res = WIFI_DIRECT_ERROR_NONE;
2736
2737         if ((g_client_info.is_registered == false) ||
2738                         (g_client_info.client_id == WFD_INVALID_ID)) {
2739                 WDC_LOGE("Client is NOT registered");
2740                 __WDC_LOG_FUNC_END__;
2741                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2742         }
2743
2744         if (!cb) {
2745                 WDC_LOGE("NULL Param [callback]!");
2746                 __WDC_LOG_FUNC_END__;
2747                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2748         }
2749
2750         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2751         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2752
2753         req.cmd = WIFI_DIRECT_CMD_GET_SUPPORTED_WPS_MODE;
2754         req.client_id = g_client_info.client_id;
2755
2756         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2757         if (res != WIFI_DIRECT_ERROR_NONE) {
2758                 __WDC_LOG_FUNC_END__;
2759                 return res;
2760         }
2761         WDC_LOGD("Supported wps config = [%d]", (int) rsp.param1);
2762
2763         int wps_mode;
2764         bool result = TRUE;
2765
2766         wps_mode = rsp.param1;
2767         if (wps_mode & WIFI_DIRECT_WPS_TYPE_PBC)
2768                 result = cb(WIFI_DIRECT_WPS_TYPE_PBC, user_data);
2769         if ((result == true) && (wps_mode & WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY))
2770                 result = cb(WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY, user_data);
2771         if ((result == true) && (wps_mode & WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD))
2772                 result = cb(WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD, user_data);
2773
2774         __WDC_LOG_FUNC_END__;
2775         return WIFI_DIRECT_ERROR_NONE;
2776 }
2777
2778 int wifi_direct_get_local_wps_type(wifi_direct_wps_type_e *type)
2779 {
2780         __WDC_LOG_FUNC_START__;
2781
2782         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2783
2784         wifi_direct_client_request_s req;
2785         wifi_direct_client_response_s rsp;
2786         int res = WIFI_DIRECT_ERROR_NONE;
2787
2788         if ((g_client_info.is_registered == false) ||
2789                         (g_client_info.client_id == WFD_INVALID_ID)) {
2790                 WDC_LOGE("Client is NOT registered");
2791                 __WDC_LOG_FUNC_END__;
2792                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2793         }
2794
2795         if (type == NULL) {
2796                 WDC_LOGE("NULL Param [type]!\n");
2797                 __WDC_LOG_FUNC_END__;
2798                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2799         }
2800
2801         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2802         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2803
2804         req.cmd = WIFI_DIRECT_CMD_GET_LOCAL_WPS_MODE;
2805         req.client_id = g_client_info.client_id;
2806
2807         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2808         if (res != WIFI_DIRECT_ERROR_NONE) {
2809                 __WDC_LOG_FUNC_END__;
2810                 return res;
2811         }
2812         WDC_LOGD("wifi_direct_get_wps_type() SUCCESS");
2813         *type = rsp.param1;
2814
2815         __WDC_LOG_FUNC_END__;
2816         return WIFI_DIRECT_ERROR_NONE;
2817 }
2818
2819 int wifi_direct_set_req_wps_type(wifi_direct_wps_type_e type)
2820 {
2821         __WDC_LOG_FUNC_START__;
2822
2823         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2824
2825         wifi_direct_client_request_s req;
2826         wifi_direct_client_response_s rsp;
2827         int res = WIFI_DIRECT_ERROR_NONE;
2828
2829         if ((g_client_info.is_registered == false) ||
2830                         (g_client_info.client_id == WFD_INVALID_ID)) {
2831                 WDC_LOGE("Client is NOT registered");
2832                 __WDC_LOG_FUNC_END__;
2833                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2834         }
2835
2836         if (type == WIFI_DIRECT_WPS_TYPE_PBC ||
2837                         type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY ||
2838                         type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
2839                 WDC_LOGD("Param wps_mode [%d]", type);
2840         } else {
2841                 WDC_LOGE("Invalid Param [wps_mode]!");
2842                 __WDC_LOG_FUNC_END__;
2843                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2844         }
2845
2846         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2847         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2848
2849         req.cmd = WIFI_DIRECT_CMD_SET_REQ_WPS_MODE;
2850         req.client_id = g_client_info.client_id;
2851         req.data.int1 = type;
2852
2853         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2854         if (res != WIFI_DIRECT_ERROR_NONE) {
2855                 __WDC_LOG_FUNC_END__;
2856                 return res;
2857         }
2858         WDC_LOGD("wifi_direct_set_req_wps_type() SUCCESS");
2859
2860         __WDC_LOG_FUNC_END__;
2861         return WIFI_DIRECT_ERROR_NONE;
2862 }
2863
2864 int wifi_direct_get_req_wps_type(wifi_direct_wps_type_e *type)
2865 {
2866         __WDC_LOG_FUNC_START__;
2867
2868         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2869
2870         wifi_direct_client_request_s req;
2871         wifi_direct_client_response_s rsp;
2872         int res = WIFI_DIRECT_ERROR_NONE;
2873
2874         if ((g_client_info.is_registered == false) ||
2875                         (g_client_info.client_id == WFD_INVALID_ID)) {
2876                 WDC_LOGE("Client is NOT registered");
2877                 __WDC_LOG_FUNC_END__;
2878                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2879         }
2880
2881         if (type == NULL) {
2882                 WDC_LOGE("NULL Param [type]!\n");
2883                 __WDC_LOG_FUNC_END__;
2884                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2885         }
2886
2887         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2888         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2889
2890         req.cmd = WIFI_DIRECT_CMD_GET_REQ_WPS_MODE;
2891         req.client_id = g_client_info.client_id;
2892
2893         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2894         if (res != WIFI_DIRECT_ERROR_NONE) {
2895                 __WDC_LOG_FUNC_END__;
2896                 return res;
2897         }
2898         WDC_LOGD("wifi_direct_get_req_wps_type() SUCCESS");
2899         *type = rsp.param1;
2900
2901         __WDC_LOG_FUNC_END__;
2902         return WIFI_DIRECT_ERROR_NONE;
2903 }
2904
2905 int wifi_direct_get_ssid(char **ssid)
2906 {
2907         __WDC_LOG_FUNC_START__;
2908
2909         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2910
2911         wifi_direct_client_request_s req;
2912         wifi_direct_client_response_s rsp;
2913         char la_ssid[WIFI_DIRECT_MAX_SSID_LEN + 1] = { 0, };
2914         int res = WIFI_DIRECT_ERROR_NONE;
2915
2916         if ((g_client_info.is_registered == false) ||
2917                         (g_client_info.client_id == WFD_INVALID_ID)) {
2918                 WDC_LOGE("Client is NOT registered");
2919                 __WDC_LOG_FUNC_END__;
2920                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2921         }
2922
2923         if (!ssid) {
2924                 WDC_LOGE("NULL Param [ssid]!");
2925                 __WDC_LOG_FUNC_END__;
2926                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2927         }
2928
2929         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2930         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2931
2932         req.cmd = WIFI_DIRECT_CMD_GET_SSID;
2933         req.client_id = g_client_info.client_id;
2934
2935         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2936         if (res != WIFI_DIRECT_ERROR_NONE) {
2937                 __WDC_LOG_FUNC_END__;
2938                 return res;
2939         }
2940         WDC_LOGD("wifi_direct_get_ssid() %s SUCCESS", rsp.param2);
2941         g_strlcpy(la_ssid, rsp.param2, WIFI_DIRECT_MAX_SSID_LEN + 1);
2942         *ssid = g_strdup(la_ssid);
2943
2944         __WDC_LOG_FUNC_END__;
2945         return WIFI_DIRECT_ERROR_NONE;
2946 }
2947
2948 int wifi_direct_get_device_name(char **device_name)
2949 {
2950         __WDC_LOG_FUNC_START__;
2951
2952         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2953
2954         wifi_direct_client_request_s req;
2955         wifi_direct_client_response_s rsp;
2956         char la_device_name[WIFI_DIRECT_MAX_DEVICE_NAME_LEN + 1] = { 0, };
2957         int res = WIFI_DIRECT_ERROR_NONE;
2958
2959         if ((g_client_info.is_registered == false) ||
2960                         (g_client_info.client_id == WFD_INVALID_ID)) {
2961                 WDC_LOGE("Client is NOT registered");
2962                 __WDC_LOG_FUNC_END__;
2963                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
2964         }
2965
2966         if (!device_name) {
2967                 WDC_LOGE("NULL Param [device_name]!");
2968                 __WDC_LOG_FUNC_END__;
2969                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
2970         }
2971
2972         memset(&req, 0, sizeof(wifi_direct_client_request_s));
2973         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
2974
2975         req.cmd = WIFI_DIRECT_CMD_GET_DEVICE_NAME;
2976         req.client_id = g_client_info.client_id;
2977
2978         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
2979         if (res != WIFI_DIRECT_ERROR_NONE) {
2980                 __WDC_LOG_FUNC_END__;
2981                 return res;
2982         }
2983         WDC_LOGD("wifi_direct_get_device_name() %s SUCCESS \n", rsp.param2);
2984         g_strlcpy(la_device_name, rsp.param2, WIFI_DIRECT_MAX_DEVICE_NAME_LEN + 1);
2985         *device_name = g_strdup(la_device_name);
2986
2987         __WDC_LOG_FUNC_END__;
2988         return WIFI_DIRECT_ERROR_NONE;
2989 }
2990
2991 int wifi_direct_set_device_name(const char *device_name)
2992 {
2993         __WDC_LOG_FUNC_START__;
2994
2995         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
2996
2997         wifi_direct_client_request_s req;
2998         wifi_direct_client_response_s rsp;
2999         int status = WIFI_DIRECT_ERROR_NONE;
3000
3001         if ((g_client_info.is_registered == false) ||
3002                         (g_client_info.client_id == WFD_INVALID_ID)) {
3003                 WDC_LOGE("Client is NOT registered");
3004                 __WDC_LOG_FUNC_END__;
3005                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3006         }
3007
3008         if (!device_name) {
3009                 WDC_LOGE("NULL Param [device_name]!");
3010                 __WDC_LOG_FUNC_END__;
3011                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3012         }
3013         WDC_LOGE("device_name = [%s]", device_name);
3014
3015         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3016         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3017
3018         req.cmd = WIFI_DIRECT_CMD_SET_DEVICE_NAME;
3019         req.client_id = g_client_info.client_id;
3020
3021         pthread_mutex_lock(&g_client_info.mutex);
3022         status = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
3023                                                                   sizeof(wifi_direct_client_request_s));
3024         if (status != WIFI_DIRECT_ERROR_NONE) {
3025                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
3026                 __wfd_reset_control();
3027                 pthread_mutex_unlock(&g_client_info.mutex);
3028                 __WDC_LOG_FUNC_END__;
3029                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3030         }
3031         WDC_LOGD( "writing msg hdr is success!\n");
3032
3033         status = __wfd_client_write_socket(g_client_info.sync_sockfd, (void*) device_name,
3034                                                                   WIFI_DIRECT_MAX_DEVICE_NAME_LEN);
3035         if (status != WIFI_DIRECT_ERROR_NONE) {
3036                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
3037                 __wfd_reset_control();
3038                 pthread_mutex_unlock(&g_client_info.mutex);
3039                 __WDC_LOG_FUNC_END__;
3040                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3041         }
3042
3043         status = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
3044                                                                   sizeof(wifi_direct_client_response_s));
3045         pthread_mutex_unlock(&g_client_info.mutex);
3046         if (status <= 0) {
3047                 WDC_LOGE("Error!!! reading socket, status = %d", status);
3048                 __wfd_reset_control();
3049                 __WDC_LOG_FUNC_END__;
3050                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3051         } else {
3052                 if (rsp.cmd == WIFI_DIRECT_CMD_SET_DEVICE_NAME) {
3053                         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
3054                                 WDC_LOGE("Error!!! Result received = %d", rsp.result);
3055                                 WDC_LOGE("Error!!! [%s]", __wfd_print_error(rsp.result));
3056                                 __WDC_LOG_FUNC_END__;
3057                                 return rsp.result;
3058                         }
3059                 } else {
3060                         WDC_LOGE("Error!!! Invalid resp cmd = %d", rsp.cmd);
3061                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
3062                 }
3063         }
3064
3065         __WDC_LOG_FUNC_END__;
3066         return WIFI_DIRECT_ERROR_NONE;
3067 }
3068
3069 int wifi_direct_get_network_interface_name(char **name)
3070 {
3071         __WDC_LOG_FUNC_START__;
3072
3073         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3074
3075         wifi_direct_state_e status = 0;
3076         char *get_str = NULL;
3077         int result;
3078
3079         if ((g_client_info.is_registered == false) ||
3080                         (g_client_info.client_id == WFD_INVALID_ID)) {
3081                 WDC_LOGE("Client is NOT registered");
3082                 __WDC_LOG_FUNC_END__;
3083                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3084         }
3085
3086         if (!name) {
3087                 WDC_LOGE("NULL Param [name]!\n");
3088                 __WDC_LOG_FUNC_END__;
3089                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3090         }
3091
3092         result = wifi_direct_get_state(&status);
3093         WDC_LOGD("wifi_direct_get_state() state=[%d], result=[%d]\n", status, result);
3094
3095         if (status < WIFI_DIRECT_STATE_CONNECTED) {
3096                 WDC_LOGE("Device is not connected!\n");
3097                 __WDC_LOG_FUNC_END__;
3098                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3099         }
3100
3101         get_str = vconf_get_str(VCONFKEY_IFNAME);
3102         if (!get_str) {
3103                 WDC_LOGD( "vconf (%s) value is NULL!!!\n", VCONFKEY_IFNAME);
3104                 __WDC_LOG_FUNC_END__;
3105                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3106         }
3107         WDC_LOGD( "VCONFKEY_IFNAME(%s) : %s\n", VCONFKEY_IFNAME, get_str);
3108         *name = g_strdup(get_str);
3109         g_free(get_str);
3110
3111         __WDC_LOG_FUNC_END__;
3112         return WIFI_DIRECT_ERROR_NONE;
3113 }
3114
3115 int wifi_direct_get_ip_address(char **ip_address)
3116 {
3117         __WDC_LOG_FUNC_START__;
3118
3119         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3120
3121         wifi_direct_client_request_s req;
3122         wifi_direct_client_response_s rsp;
3123         int res = 0;
3124
3125         if ((g_client_info.is_registered == false) ||
3126                         (g_client_info.client_id == WFD_INVALID_ID)) {
3127                 WDC_LOGE("Client is NOT registered");
3128                 __WDC_LOG_FUNC_END__;
3129                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3130         }
3131
3132         if (!ip_address) {
3133                 WDC_LOGE("NULL Param [ip_address]!\n");
3134                 __WDC_LOG_FUNC_END__;
3135                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3136         }
3137
3138         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3139         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3140
3141         req.cmd = WIFI_DIRECT_CMD_GET_IP_ADDR;
3142         req.client_id = g_client_info.client_id;
3143
3144         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3145         if (res != WIFI_DIRECT_ERROR_NONE) {
3146                 __WDC_LOG_FUNC_END__;
3147                 return res;
3148         }
3149         *ip_address = g_strdup(rsp.param2);
3150         WDC_LOGD("IP address = [%s]", *ip_address);
3151
3152         __WDC_LOG_FUNC_END__;
3153         return WIFI_DIRECT_ERROR_NONE;
3154 }
3155
3156 int wifi_direct_get_subnet_mask(char **subnet_mask)
3157 {
3158         __WDC_LOG_FUNC_START__;
3159
3160         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3161
3162         wifi_direct_state_e status = 0;
3163         char *get_str = NULL;
3164         int result;
3165
3166         if ((g_client_info.is_registered == false) ||
3167                         (g_client_info.client_id == WFD_INVALID_ID)) {
3168                 WDC_LOGE("Client is NOT registered");
3169                 __WDC_LOG_FUNC_END__;
3170                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3171         }
3172
3173         if (!subnet_mask) {
3174                 WDC_LOGE("NULL Param [subnet_mask]!");
3175                 __WDC_LOG_FUNC_END__;
3176                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3177         }
3178
3179         result = wifi_direct_get_state(&status);
3180         WDC_LOGD("wifi_direct_get_state() state=[%d], result=[%d]", status, result);
3181         if( status < WIFI_DIRECT_STATE_CONNECTED) {
3182                 WDC_LOGE("Device is not connected!");
3183                 __WDC_LOG_FUNC_END__;
3184                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3185         }
3186
3187         get_str = vconf_get_str(VCONFKEY_SUBNET_MASK);
3188         if (!get_str) {
3189                 WDC_LOGD("vconf (%s) value is NULL!!!", VCONFKEY_SUBNET_MASK);
3190                 __WDC_LOG_FUNC_END__;
3191                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3192         }
3193         WDC_LOGD("VCONFKEY_SUBNET_MASK(%s) : %s", VCONFKEY_SUBNET_MASK, get_str);
3194         *subnet_mask = g_strdup(get_str);
3195         g_free(get_str);
3196
3197         __WDC_LOG_FUNC_END__;
3198         return WIFI_DIRECT_ERROR_NONE;
3199 }
3200
3201 int wifi_direct_get_gateway_address(char **gateway_address)
3202 {
3203         __WDC_LOG_FUNC_START__;
3204
3205         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3206
3207         wifi_direct_state_e status = 0;
3208         char *get_str = NULL;
3209         int result;
3210
3211         if ((g_client_info.is_registered == false) ||
3212                         (g_client_info.client_id == WFD_INVALID_ID)) {
3213                 WDC_LOGE("Client is NOT registered");
3214                 __WDC_LOG_FUNC_END__;
3215                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3216         }
3217
3218         if (!gateway_address) {
3219                 WDC_LOGE("NULL Param [gateway_address]!");
3220                 __WDC_LOG_FUNC_END__;
3221                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3222         }
3223
3224         result = wifi_direct_get_state(&status);
3225         WDC_LOGD("wifi_direct_get_state() state=[%d], result=[%d]", status, result);
3226         if(status < WIFI_DIRECT_STATE_CONNECTED) {
3227                 WDC_LOGE("Device is not connected!");
3228                 __WDC_LOG_FUNC_END__;
3229                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3230         }
3231
3232         get_str = vconf_get_str(VCONFKEY_GATEWAY);
3233         if (!get_str) {
3234                 WDC_LOGD("vconf (%s) value is NULL!!!", VCONFKEY_GATEWAY);
3235                 __WDC_LOG_FUNC_END__;
3236                 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
3237         }
3238         WDC_LOGD("VCONFKEY_GATEWAY(%s) : %s", VCONFKEY_GATEWAY, get_str);
3239         *gateway_address = g_strdup(get_str);
3240         g_free(get_str);
3241
3242         __WDC_LOG_FUNC_END__;
3243         return WIFI_DIRECT_ERROR_NONE;
3244 }
3245
3246 int wifi_direct_get_mac_address(char **mac_address)
3247 {
3248         __WDC_LOG_FUNC_START__;
3249
3250         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3251
3252         wifi_direct_client_request_s req;
3253         wifi_direct_client_response_s rsp;
3254         int res = WIFI_DIRECT_ERROR_NONE;
3255
3256         if ((g_client_info.is_registered == false) ||
3257                         (g_client_info.client_id == WFD_INVALID_ID)) {
3258                 WDC_LOGE("Client is NOT registered");
3259                 __WDC_LOG_FUNC_END__;
3260                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3261         }
3262
3263         if (!mac_address) {
3264                 WDC_LOGE("NULL Param [mac_address]!");
3265                 __WDC_LOG_FUNC_END__;
3266                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3267         }
3268
3269         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3270         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3271
3272         req.cmd = WIFI_DIRECT_CMD_GET_MAC_ADDR;
3273         req.client_id = g_client_info.client_id;
3274
3275         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3276         if (res != WIFI_DIRECT_ERROR_NONE) {
3277                 __WDC_LOG_FUNC_END__;
3278                 return res;
3279         }
3280         *mac_address = g_strdup(rsp.param2);
3281         WDC_SECLOGD("MAC address = [%s]", *mac_address);
3282
3283         __WDC_LOG_FUNC_END__;
3284         return WIFI_DIRECT_ERROR_NONE;
3285 }
3286
3287
3288 int wifi_direct_get_state(wifi_direct_state_e *state)
3289 {
3290         __WDC_LOG_FUNC_START__;
3291
3292         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3293
3294         wifi_direct_client_request_s req;
3295         wifi_direct_client_response_s rsp;
3296         int res = WIFI_DIRECT_ERROR_NONE;
3297
3298         if (!state) {
3299                 WDC_LOGE("NULL Param [state]!");
3300                 __WDC_LOG_FUNC_END__;
3301                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3302         }
3303
3304         if ((g_client_info.is_registered == false) ||
3305                         (g_client_info.client_id == WFD_INVALID_ID)) {
3306                 WDC_LOGE("Client is NOT registered");
3307                 __WDC_LOG_FUNC_END__;
3308                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3309         }
3310
3311         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3312         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3313
3314         req.cmd = WIFI_DIRECT_CMD_GET_LINK_STATUS;
3315         req.client_id = g_client_info.client_id;
3316
3317         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3318         if (res != WIFI_DIRECT_ERROR_NONE) {
3319                 __WDC_LOG_FUNC_END__;
3320                 return res;
3321         }
3322         WDC_LOGD("Link Status = %d", (int) rsp.param1);
3323         *state = (wifi_direct_state_e) rsp.param1;
3324
3325         /* for CAPI : there is no WIFI_DIRECT_STATE_GROUP_OWNER type in CAPI */
3326         if(*state == WIFI_DIRECT_STATE_GROUP_OWNER)
3327                 *state = WIFI_DIRECT_STATE_CONNECTED;
3328
3329         __WDC_LOG_FUNC_END__;
3330         return WIFI_DIRECT_ERROR_NONE;
3331 }
3332
3333
3334 int wifi_direct_is_discoverable(bool* discoverable)
3335 {
3336         __WDC_LOG_FUNC_START__;
3337
3338         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3339
3340         wifi_direct_client_request_s req;
3341         wifi_direct_client_response_s rsp;
3342         int res = WIFI_DIRECT_ERROR_NONE;
3343
3344         if ((g_client_info.is_registered == false) ||
3345                         (g_client_info.client_id == WFD_INVALID_ID)) {
3346                 WDC_LOGE("Client is NOT registered");
3347                 __WDC_LOG_FUNC_END__;
3348                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3349         }
3350
3351         if (!discoverable) {
3352                 WDC_LOGE("NULL Param [discoverable]!");
3353                 __WDC_LOG_FUNC_END__;
3354                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3355         }
3356
3357         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3358         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3359
3360         req.cmd = WIFI_DIRECT_CMD_IS_DISCOVERABLE;
3361         req.client_id = g_client_info.client_id;
3362
3363         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3364         if (res != WIFI_DIRECT_ERROR_NONE) {
3365                 __WDC_LOG_FUNC_END__;
3366                 return res;
3367         }
3368         WDC_LOGD("wifi_direct_is_discoverable() SUCCESS");
3369         *discoverable = (bool) rsp.param1;
3370
3371         __WDC_LOG_FUNC_END__;
3372         return WIFI_DIRECT_ERROR_NONE;
3373 }
3374
3375 int wifi_direct_is_listening_only(bool* listen_only)
3376 {
3377         __WDC_LOG_FUNC_START__;
3378
3379         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3380
3381         wifi_direct_client_request_s req;
3382         wifi_direct_client_response_s rsp;
3383         int res = WIFI_DIRECT_ERROR_NONE;
3384
3385         if ((g_client_info.is_registered == false) ||
3386                         (g_client_info.client_id == WFD_INVALID_ID)) {
3387                 WDC_LOGE("Client is NOT registered");
3388                 __WDC_LOG_FUNC_END__;
3389                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3390         }
3391
3392         if (!listen_only) {
3393                 WDC_LOGE("NULL Param [listen_only]!");
3394                 __WDC_LOG_FUNC_END__;
3395                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3396         }
3397
3398         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3399         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3400
3401         req.cmd = WIFI_DIRECT_CMD_IS_LISTENING_ONLY;
3402         req.client_id = g_client_info.client_id;
3403
3404         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3405         if (res != WIFI_DIRECT_ERROR_NONE) {
3406                 __WDC_LOG_FUNC_END__;
3407                 return res;
3408         }
3409         WDC_LOGD("wifi_direct_is_listening_only() SUCCESS");
3410         *listen_only = (bool) rsp.param1;
3411
3412         __WDC_LOG_FUNC_END__;
3413         return WIFI_DIRECT_ERROR_NONE;
3414 }
3415
3416
3417 int wifi_direct_get_primary_device_type(wifi_direct_primary_device_type_e* type)
3418 {
3419         __WDC_LOG_FUNC_START__;
3420
3421         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3422
3423         if (!type) {
3424                 WDC_LOGE("NULL Param [type]!");
3425                 __WDC_LOG_FUNC_END__;
3426                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3427         }
3428
3429         if ((g_client_info.is_registered == false) ||
3430                         (g_client_info.client_id == WFD_INVALID_ID)) {
3431                 WDC_LOGE("Client is NOT registered");
3432                 __WDC_LOG_FUNC_END__;
3433                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3434         }
3435
3436 #ifdef TIZEN_TV
3437         WDC_LOGD("Current primary_dev_type [%d]", WIFI_DIRECT_PRIMARY_DEVICE_TYPE_DISPLAY);
3438         *type = WIFI_DIRECT_PRIMARY_DEVICE_TYPE_DISPLAY;
3439 #else /* TIZEN_TV */
3440         WDC_LOGD("Current primary_dev_type [%d]", WIFI_DIRECT_PRIMARY_DEVICE_TYPE_TELEPHONE);
3441         *type = WIFI_DIRECT_PRIMARY_DEVICE_TYPE_TELEPHONE;
3442 #endif /* TIZEN_TV */
3443
3444         __WDC_LOG_FUNC_END__;
3445         return WIFI_DIRECT_ERROR_NONE;
3446 }
3447
3448 int wifi_direct_get_secondary_device_type(wifi_direct_secondary_device_type_e* type)
3449 {
3450         __WDC_LOG_FUNC_START__;
3451
3452         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3453
3454         if ((g_client_info.is_registered == false) ||
3455                         (g_client_info.client_id == WFD_INVALID_ID)) {
3456                 WDC_LOGE("Client is NOT registered");
3457                 __WDC_LOG_FUNC_END__;
3458                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3459         }
3460
3461         if (NULL == type) {
3462                 WDC_LOGE("NULL Param [type]!");
3463                 __WDC_LOG_FUNC_END__;
3464                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3465         }
3466
3467 #ifdef TIZEN_TV
3468         WDC_LOGD("Current second_dev_type [%d]", WIFI_DIRECT_SECONDARY_DEVICE_TYPE_DISPLAY_TV);
3469         *type = WIFI_DIRECT_SECONDARY_DEVICE_TYPE_DISPLAY_TV;
3470 #else /* TIZEN_TV */
3471         WDC_LOGD("Current second_dev_type [%d]", WIFI_DIRECT_SECONDARY_DEVICE_TYPE_TELEPHONE_SMARTPHONE_DUAL);
3472         *type = WIFI_DIRECT_SECONDARY_DEVICE_TYPE_TELEPHONE_SMARTPHONE_DUAL;    // smart phone dual mode (wifi and cellular)
3473 #endif /* TIZEN_TV */
3474
3475         __WDC_LOG_FUNC_END__;
3476         return WIFI_DIRECT_ERROR_NONE;
3477 }
3478
3479 int wifi_direct_set_autoconnection_mode(bool mode)
3480 {
3481         __WDC_LOG_FUNC_START__;
3482
3483         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3484
3485         wifi_direct_client_request_s req;
3486         wifi_direct_client_response_s rsp;
3487         int res = WIFI_DIRECT_ERROR_NONE;
3488
3489         if ((g_client_info.is_registered == false) ||
3490                         (g_client_info.client_id == WFD_INVALID_ID)) {
3491                 WDC_LOGE("Client is NOT registered");
3492                 __WDC_LOG_FUNC_END__;
3493                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3494         }
3495
3496         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3497         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3498
3499         req.cmd = WIFI_DIRECT_CMD_SET_AUTOCONNECTION_MODE;
3500         req.client_id = g_client_info.client_id;
3501         req.data.int1 = mode;
3502
3503         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3504         if (res != WIFI_DIRECT_ERROR_NONE) {
3505                 __WDC_LOG_FUNC_END__;
3506                 return res;
3507         }
3508
3509         __WDC_LOG_FUNC_END__;
3510         return WIFI_DIRECT_ERROR_NONE;
3511 }
3512
3513 int wifi_direct_is_autoconnection_mode(bool *mode)
3514 {
3515         __WDC_LOG_FUNC_START__;
3516
3517         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3518
3519         wifi_direct_client_request_s req;
3520         wifi_direct_client_response_s rsp;
3521         int res = WIFI_DIRECT_ERROR_NONE;
3522
3523         if ((g_client_info.is_registered == false) ||
3524                         (g_client_info.client_id == WFD_INVALID_ID)) {
3525                 WDC_LOGE("Client is NOT registered");
3526                 __WDC_LOG_FUNC_END__;
3527                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3528         }
3529
3530         if (!mode) {
3531                 WDC_LOGE("NULL Param [mode]!");
3532                 __WDC_LOG_FUNC_END__;
3533                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3534         }
3535
3536         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3537         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3538
3539         req.cmd = WIFI_DIRECT_CMD_IS_AUTOCONNECTION_MODE;
3540         req.client_id = g_client_info.client_id;
3541
3542         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3543         if (res != WIFI_DIRECT_ERROR_NONE) {
3544                 __WDC_LOG_FUNC_END__;
3545                 return res;
3546         }
3547         WDC_LOGD("wifi_direct_is_autoconnection_mode() SUCCESS");
3548         *mode = (bool) rsp.param1;
3549
3550         __WDC_LOG_FUNC_END__;
3551         return WIFI_DIRECT_ERROR_NONE;
3552
3553 }
3554
3555
3556 int wifi_direct_set_persistent_group_enabled(bool enabled)
3557 {
3558         __WDC_LOG_FUNC_START__;
3559
3560         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3561
3562         wifi_direct_client_request_s req;
3563         wifi_direct_client_response_s rsp;
3564         int res = WIFI_DIRECT_ERROR_NONE;
3565
3566         if ((g_client_info.is_registered == false) ||
3567                         (g_client_info.client_id == WFD_INVALID_ID)) {
3568                 WDC_LOGE("Client is NOT registered");
3569                 __WDC_LOG_FUNC_END__;
3570                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3571         }
3572
3573         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3574         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3575
3576         if (enabled == true)
3577                 req.cmd = WIFI_DIRECT_CMD_ACTIVATE_PERSISTENT_GROUP;
3578         else
3579                 req.cmd = WIFI_DIRECT_CMD_DEACTIVATE_PERSISTENT_GROUP;
3580         req.client_id = g_client_info.client_id;
3581
3582         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3583         if (res != WIFI_DIRECT_ERROR_NONE) {
3584                 __WDC_LOG_FUNC_END__;
3585                 return res;
3586         }
3587         WDC_LOGD("wifi_direct_set_persistent_group_enabled() SUCCESS");
3588
3589         __WDC_LOG_FUNC_END__;
3590         return WIFI_DIRECT_ERROR_NONE;
3591 }
3592
3593
3594 int wifi_direct_is_persistent_group_enabled(bool *enabled)
3595 {
3596         __WDC_LOG_FUNC_START__;
3597
3598         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3599
3600         wifi_direct_client_request_s req;
3601         wifi_direct_client_response_s rsp;
3602         int res = WIFI_DIRECT_ERROR_NONE;
3603
3604         if ((g_client_info.is_registered == false) ||
3605                         (g_client_info.client_id == WFD_INVALID_ID)) {
3606                 WDC_LOGE("Client is NOT registered");
3607                 __WDC_LOG_FUNC_END__;
3608                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3609         }
3610
3611         if (!enabled) {
3612                 WDC_LOGE("NULL Param [enabled]!");
3613                 __WDC_LOG_FUNC_END__;
3614                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3615         }
3616
3617         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3618         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3619
3620         req.cmd = WIFI_DIRECT_CMD_IS_PERSISTENT_GROUP_ACTIVATED;
3621         req.client_id = g_client_info.client_id;
3622
3623         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3624         if (res != WIFI_DIRECT_ERROR_NONE) {
3625                 __WDC_LOG_FUNC_END__;
3626                 return res;
3627         }
3628         WDC_LOGD("wifi_direct_is_persistent_group_enabled() SUCCESS");
3629         *enabled = (bool) rsp.param1;
3630
3631         __WDC_LOG_FUNC_END__;
3632         return WIFI_DIRECT_ERROR_NONE;
3633 }
3634
3635 int wifi_direct_foreach_persistent_groups(wifi_direct_persistent_group_cb cb,
3636                                                                                                 void* user_data)
3637 {
3638         __WDC_LOG_FUNC_START__;
3639
3640         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3641
3642         wifi_direct_client_request_s req;
3643         wifi_direct_client_response_s rsp;
3644         int res = WIFI_DIRECT_ERROR_NONE;
3645         int i;
3646
3647         if ((g_client_info.is_registered == false) ||
3648                         (g_client_info.client_id == WFD_INVALID_ID)) {
3649                 WDC_LOGE("Client is NOT registered");
3650                 __WDC_LOG_FUNC_END__;
3651                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3652         }
3653
3654         if (!cb) {
3655                 WDC_LOGE("NULL Param [callback]!");
3656                 __WDC_LOG_FUNC_END__;
3657                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3658         }
3659
3660         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3661         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3662
3663         req.cmd = WIFI_DIRECT_CMD_GET_PERSISTENT_GROUP_INFO;
3664         req.client_id = g_client_info.client_id;
3665
3666         pthread_mutex_lock(&g_client_info.mutex);
3667         res = __wfd_client_write_socket(g_client_info.sync_sockfd, &req, sizeof(wifi_direct_client_request_s));
3668         if (res != WIFI_DIRECT_ERROR_NONE) {
3669                 WDC_LOGE("Failed to write into socket [%s]", __wfd_print_error(res));
3670                 __wfd_reset_control();
3671                 pthread_mutex_unlock(&g_client_info.mutex);
3672                 __WDC_LOG_FUNC_END__;
3673                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3674         }
3675         WDC_LOGD("Succeeded to send request [%d: %s]", req.cmd, __wfd_client_print_cmd(req.cmd));
3676
3677         res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
3678                                                         sizeof(wifi_direct_client_response_s));
3679         if (res <= 0) {
3680                 WDC_LOGE("Failed to read socket [%d]", res);
3681                 __wfd_reset_control();
3682                 pthread_mutex_unlock(&g_client_info.mutex);
3683                 __WDC_LOG_FUNC_END__;
3684                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3685         }
3686
3687         if (rsp.cmd != req.cmd) {
3688                 WDC_LOGE("Invalid resp [%d]", rsp.cmd);
3689                 pthread_mutex_unlock(&g_client_info.mutex);
3690                 __WDC_LOG_FUNC_END__;
3691                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3692         }
3693
3694         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
3695                 WDC_LOGE("Result received [%s]", __wfd_print_error(rsp.result));
3696                 pthread_mutex_unlock(&g_client_info.mutex);
3697                 __WDC_LOG_FUNC_END__;
3698                 return rsp.result;
3699         }
3700
3701         short num = rsp.param1;
3702         short num_tmp = 0;
3703         wfd_persistent_group_info_s *buff = NULL;
3704         wfd_persistent_group_info_s *buff_tmp = NULL;
3705
3706         WDC_LOGD("Num of persistent groups = %d", (int) rsp.param1);
3707
3708         if (num > 1023) {
3709                 WDC_LOGE("Discovered peer number restricted by 255(real number:%d)", num);
3710                 num_tmp = num -1023;
3711                 num = 1023;
3712         }
3713
3714         if (num > 0) {
3715                 buff = (wfd_persistent_group_info_s *) g_try_malloc0_n(num, sizeof(wfd_persistent_group_info_s));
3716                 if (!buff) {
3717                         WDC_LOGE("malloc() failed!!!.");
3718                         pthread_mutex_unlock(&g_client_info.mutex);
3719                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
3720                 }
3721
3722                 res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff,
3723                                                                         num * sizeof(wfd_persistent_group_info_s));
3724                 if (num_tmp) {
3725                         WDC_LOGD("Rest data should be read out");
3726                         buff_tmp = (wfd_persistent_group_info_s*) calloc(num_tmp, sizeof (wfd_persistent_group_info_s));
3727                         if (buff_tmp) {
3728                                 __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff_tmp,
3729                                                                         num_tmp * sizeof(wfd_persistent_group_info_s));
3730                                 g_free(buff_tmp);
3731                         }
3732                 }
3733                 pthread_mutex_unlock(&g_client_info.mutex);
3734                 if (res <= 0){
3735                         free(buff);
3736                         WDC_LOGE("socket read error.");
3737                         __wfd_reset_control();
3738                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
3739                 }
3740
3741                 __wfd_client_print_persistent_group_info(buff, num);
3742                 WDC_LOGD("wifi_direct_foreach_persistent_groups() SUCCESS");
3743
3744                 char *ssid;
3745                 char *go_mac_address;
3746
3747                 for (i = 0; i < num; i++) {
3748                         ssid = g_strndup(buff[i].ssid, WIFI_DIRECT_MAX_SSID_LEN+1);
3749                         if (!ssid) {
3750                                 WDC_LOGD("Failed to copy ssid");
3751                                 break;
3752                         }
3753                         go_mac_address = (char*) g_try_malloc0(MACSTR_LEN);
3754                         if (!go_mac_address) {
3755                                 WDC_LOGD("Failed to allocate memory for GO MAC address");
3756                                 g_free(ssid);
3757                                 g_free(buff);
3758                                 return WIFI_DIRECT_ERROR_OUT_OF_MEMORY;
3759                         }
3760                         g_snprintf(go_mac_address, MACSTR_LEN, MACSTR, MAC2STR(buff[i].go_mac_address));
3761
3762                         res = cb(go_mac_address, ssid, user_data);
3763                         g_free(ssid);
3764                         ssid = NULL;
3765                         g_free(go_mac_address);
3766                         go_mac_address = NULL;
3767                         if (!res)
3768                                 break;
3769                 }
3770                 g_free(buff);
3771
3772         } else {
3773                 pthread_mutex_unlock(&g_client_info.mutex);
3774         }
3775
3776         __WDC_LOG_FUNC_END__;
3777         return WIFI_DIRECT_ERROR_NONE;
3778 }
3779
3780 int wifi_direct_remove_persistent_group(char *mac_address, const char *ssid)
3781 {
3782         __WDC_LOG_FUNC_START__;
3783
3784         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
3785
3786         wifi_direct_client_request_s req;
3787         wifi_direct_client_response_s rsp;
3788         wfd_persistent_group_info_s persistent_group_info;
3789         int status = WIFI_DIRECT_ERROR_NONE;
3790
3791         if ((g_client_info.is_registered == false) ||
3792                         (g_client_info.client_id == WFD_INVALID_ID)) {
3793                 WDC_LOGE("Client is NOT registered");
3794                 __WDC_LOG_FUNC_END__;
3795                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3796         }
3797
3798         if (!mac_address || !ssid) {
3799                 WDC_LOGE("NULL Param");
3800                 __WDC_LOG_FUNC_END__;
3801                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3802         }
3803
3804         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3805         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3806
3807         req.cmd = WIFI_DIRECT_CMD_REMOVE_PERSISTENT_GROUP;
3808         req.client_id = g_client_info.client_id;
3809
3810         pthread_mutex_lock(&g_client_info.mutex);
3811         status = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
3812                                                                   sizeof(wifi_direct_client_request_s));
3813         if (status != WIFI_DIRECT_ERROR_NONE) {
3814                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
3815                 __wfd_reset_control();
3816                 pthread_mutex_unlock(&g_client_info.mutex);
3817                 __WDC_LOG_FUNC_END__;
3818                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3819         }
3820         WDC_LOGD("writing msg hdr is success!");
3821
3822         g_strlcpy(persistent_group_info.ssid, ssid, WIFI_DIRECT_MAX_SSID_LEN + 1);
3823         macaddr_atoe(mac_address, persistent_group_info.go_mac_address);
3824
3825         status = __wfd_client_write_socket(g_client_info.sync_sockfd, &persistent_group_info,
3826                                                                   sizeof(wfd_persistent_group_info_s));
3827         if (status != WIFI_DIRECT_ERROR_NONE) {
3828                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
3829                 __wfd_reset_control();
3830                 pthread_mutex_unlock(&g_client_info.mutex);
3831                 __WDC_LOG_FUNC_END__;
3832                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3833         }
3834
3835         status = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
3836                                                                   sizeof(wifi_direct_client_response_s));
3837         pthread_mutex_unlock(&g_client_info.mutex);
3838         if (status <= 0) {
3839                 WDC_LOGE("Error!!! reading socket, status = %d", status);
3840                 __wfd_reset_control();
3841                 __WDC_LOG_FUNC_END__;
3842                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
3843         }
3844
3845         if (rsp.cmd !=req.cmd) {
3846                 WDC_LOGE("Error!!! Invalid resp cmd = %d", rsp.cmd);
3847                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
3848         }
3849
3850         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
3851                 WDC_LOGD("Error!!! Result received = %d", rsp.result);
3852                 __WDC_LOG_FUNC_END__;
3853                 return rsp.result;
3854         }
3855
3856         __WDC_LOG_FUNC_END__;
3857         return WIFI_DIRECT_ERROR_NONE;
3858 }
3859
3860 int wifi_direct_set_p2poem_loglevel(int increase_log_level)
3861 {
3862         __WDC_LOG_FUNC_START__;
3863         wifi_direct_client_request_s req;
3864         wifi_direct_client_response_s rsp;
3865         int res = WIFI_DIRECT_ERROR_NONE;
3866
3867         if ((g_client_info.is_registered == false) ||
3868                         (g_client_info.client_id == WFD_INVALID_ID)) {
3869                 WDC_LOGE("Client is NOT registered");
3870                 __WDC_LOG_FUNC_END__;
3871                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3872         }
3873
3874         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3875         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3876
3877         req.cmd = WIFI_DIRECT_CMD_SET_OEM_LOGLEVEL;
3878         req.client_id = g_client_info.client_id;
3879         if (increase_log_level == 0)
3880                 req.data.int1 = false;
3881         else
3882                 req.data.int1 = true;
3883
3884         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3885         if (res != WIFI_DIRECT_ERROR_NONE) {
3886                 __WDC_LOG_FUNC_END__;
3887                 return res;
3888         }
3889
3890         __WDC_LOG_FUNC_END__;
3891         return WIFI_DIRECT_ERROR_NONE;
3892 }
3893
3894 int wifi_direct_start_service_discovery(char *mac_address,
3895                 wifi_direct_service_type_e type)
3896 {
3897 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
3898         __WDC_LOG_FUNC_START__;
3899
3900         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
3901
3902         unsigned char la_mac_addr[MACADDR_LEN] = {0, };
3903         wifi_direct_client_request_s req;
3904         wifi_direct_client_response_s rsp;
3905         int res = WIFI_DIRECT_ERROR_NONE;
3906
3907         if ((g_client_info.is_registered == false) ||
3908                         (g_client_info.client_id == WFD_INVALID_ID)) {
3909                 WDC_LOGE("Client is NOT registered.");
3910                 __WDC_LOG_FUNC_END__;
3911                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3912         }
3913
3914         if (type >= WIFI_DIRECT_SERVICE_TYPE_ALL &&
3915                         type <= WIFI_DIRECT_SERVICE_TYPE_CONTACT_INFO) {
3916                 WDC_LOGD("Param service_type [%d]", type);
3917         } else {
3918                 WDC_LOGE("Invalid Param [type]!");
3919                 __WDC_LOG_FUNC_END__;
3920                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3921         }
3922
3923         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3924         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3925
3926         req.cmd = WIFI_DIRECT_CMD_START_SERVICE_DISCOVERY;
3927         req.client_id = g_client_info.client_id;
3928         req.data.int1 = type;
3929         if (mac_address)
3930                 macaddr_atoe(mac_address, la_mac_addr);
3931         memcpy(req.data.mac_addr, la_mac_addr, MACADDR_LEN);
3932
3933         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3934         if (res != WIFI_DIRECT_ERROR_NONE) {
3935                 __WDC_LOG_FUNC_END__;
3936                 return res;
3937         }
3938
3939         __WDC_LOG_FUNC_END__;
3940         return WIFI_DIRECT_ERROR_NONE;
3941 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
3942         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
3943 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
3944 }
3945
3946
3947 int wifi_direct_cancel_service_discovery(char *mac_address,
3948                 wifi_direct_service_type_e type)
3949 {
3950 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
3951         __WDC_LOG_FUNC_START__;
3952
3953         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
3954
3955         unsigned char la_mac_addr[MACADDR_LEN] = {0, };
3956         wifi_direct_client_request_s req;
3957         wifi_direct_client_response_s rsp;
3958         int res = WIFI_DIRECT_ERROR_NONE;
3959
3960         if ((g_client_info.is_registered == false) ||
3961                         (g_client_info.client_id == WFD_INVALID_ID)) {
3962                 WDC_LOGE("Client is NOT registered.");
3963                 __WDC_LOG_FUNC_END__;
3964                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
3965         }
3966
3967         if (type >= WIFI_DIRECT_SERVICE_TYPE_ALL &&
3968                         type <= WIFI_DIRECT_SERVICE_TYPE_CONTACT_INFO) {
3969                 WDC_LOGD("Param service_type [%d]", type);
3970         } else {
3971                 WDC_LOGE("Invalid Param [type]!");
3972                 __WDC_LOG_FUNC_END__;
3973                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
3974         }
3975
3976         memset(&req, 0, sizeof(wifi_direct_client_request_s));
3977         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
3978
3979         req.cmd = WIFI_DIRECT_CMD_CANCEL_SERVICE_DISCOVERY;
3980         req.client_id = g_client_info.client_id;
3981         req.data.int1 = type;
3982         if (mac_address)
3983                 macaddr_atoe(mac_address, la_mac_addr);
3984         memcpy(req.data.mac_addr, la_mac_addr, MACADDR_LEN);
3985
3986         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
3987         if (res != WIFI_DIRECT_ERROR_NONE) {
3988                 __WDC_LOG_FUNC_END__;
3989                 return res;
3990         }
3991
3992         __WDC_LOG_FUNC_END__;
3993         return WIFI_DIRECT_ERROR_NONE;
3994 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
3995         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
3996 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
3997 }
3998
3999 int wifi_direct_register_service(wifi_direct_service_type_e type, char *info1, char *info2, unsigned int *service_id)
4000 {
4001 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
4002         __WDC_LOG_FUNC_START__;
4003
4004         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
4005
4006         wifi_direct_client_request_s req;
4007         wifi_direct_client_response_s rsp;
4008         char *buf = NULL;
4009         int status = WIFI_DIRECT_ERROR_NONE;
4010         int len = 0;
4011
4012         if ((g_client_info.is_registered == false) ||
4013                         (g_client_info.client_id == WFD_INVALID_ID)) {
4014                 WDC_LOGE("Client is NOT registered.");
4015                 __WDC_LOG_FUNC_END__;
4016                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4017         }
4018
4019         if (!info1) {
4020                 WDC_LOGE("data is NULL");
4021                 __WDC_LOG_FUNC_END__;
4022                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4023         }
4024
4025         if (type < WIFI_DIRECT_SERVICE_TYPE_ALL ||
4026                         type > WIFI_DIRECT_SERVICE_TYPE_VENDOR) {
4027                 WDC_LOGE("Invalid Param [type]!");
4028                 __WDC_LOG_FUNC_END__;
4029                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4030         }
4031         WDC_LOGD("Service type [%d]", type);
4032
4033         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4034         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4035
4036         len = strlen(info1) + strlen(info2) + 2;
4037         WDC_LOGD("info [%s|%s], len [%d]", info1, info2, len);
4038         buf= g_try_malloc0(sizeof(wifi_direct_client_request_s) + len);
4039         if (NULL == buf) {
4040                 WDC_LOGE("Failed to allocate memory for buf");
4041                 return WIFI_DIRECT_ERROR_OUT_OF_MEMORY;
4042         }
4043
4044         req.cmd = WIFI_DIRECT_CMD_REGISTER_SERVICE;
4045         req.client_id = g_client_info.client_id;
4046         req.data.int1 = type;
4047         req.cmd_data_len = len;
4048
4049         memcpy(buf, &req, sizeof(wifi_direct_client_request_s));
4050         g_snprintf(buf + sizeof(wifi_direct_client_request_s), len, "%s|%s", info1, info2);
4051
4052         pthread_mutex_lock(&g_client_info.mutex);
4053         status = __wfd_client_write_socket(g_client_info.sync_sockfd, buf,
4054                                                                         sizeof(wifi_direct_client_request_s) + len);
4055         g_free(buf);
4056         if (status != WIFI_DIRECT_ERROR_NONE) {
4057                 WDC_LOGE("Error!!! writing to socket, Errno = %s", strerror(errno));
4058                 __wfd_reset_control();
4059                 pthread_mutex_unlock(&g_client_info.mutex);
4060                 __WDC_LOG_FUNC_END__;
4061                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4062         }
4063         WDC_LOGD("Success writing data to the socket!");
4064
4065         status = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
4066                                                                 sizeof(wifi_direct_client_response_s));
4067         pthread_mutex_unlock(&g_client_info.mutex);
4068         if (status <= 0) {
4069                 WDC_LOGE("Error!!! reading socket, status = %d errno = %s", status, strerror(errno));
4070                 __wfd_reset_control();
4071                 __WDC_LOG_FUNC_END__;
4072                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4073         }
4074
4075         if (rsp.cmd != req.cmd) {
4076                 WDC_LOGE("Error!!! Invalid resp cmd = %d", rsp.cmd);
4077                 __WDC_LOG_FUNC_END__;
4078                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
4079         }
4080
4081         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
4082                 WDC_LOGE("Error!!! Result received = %d", rsp.result);
4083                 __WDC_LOG_FUNC_END__;
4084                 return rsp.result;
4085         }
4086
4087         *service_id = rsp.param1;
4088
4089         __WDC_LOG_FUNC_END__;
4090         return WIFI_DIRECT_ERROR_NONE;
4091 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
4092         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4093 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
4094 }
4095
4096 int wifi_direct_deregister_service(unsigned int service_id)
4097 {
4098 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
4099         __WDC_LOG_FUNC_START__;
4100
4101         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_SERVICE_DISCOVERY_FEATURE);
4102
4103         wifi_direct_client_request_s req;
4104         wifi_direct_client_response_s rsp;
4105         int res = 0;
4106
4107         if ((g_client_info.is_registered == false) ||
4108                         (g_client_info.client_id == WFD_INVALID_ID)) {
4109                 WDC_LOGE("Client is NOT registered.");
4110                 __WDC_LOG_FUNC_END__;
4111                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4112         }
4113
4114         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4115         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4116
4117         req.cmd = WIFI_DIRECT_CMD_DEREGISTER_SERVICE;
4118         req.client_id = g_client_info.client_id;
4119         req.data.int1 = service_id;
4120
4121         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4122         if (res != WIFI_DIRECT_ERROR_NONE) {
4123                 __WDC_LOG_FUNC_END__;
4124                 return res;
4125         }
4126
4127         __WDC_LOG_FUNC_END__;
4128         return WIFI_DIRECT_ERROR_NONE;
4129 #else /* TIZEN_FEATURE_SERVICE_DISCOVERY */
4130         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4131 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
4132 }
4133
4134 int wifi_direct_init_miracast(bool enable)
4135 {
4136 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4137         __WDC_LOG_FUNC_START__;
4138
4139         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4140
4141         wifi_direct_client_request_s req;
4142         wifi_direct_client_response_s rsp;
4143         int res = WIFI_DIRECT_ERROR_NONE;
4144
4145         if ((g_client_info.is_registered == false) ||
4146                         (g_client_info.client_id == WFD_INVALID_ID)) {
4147                 WDC_LOGE("Client is NOT registered.");
4148                 __WDC_LOG_FUNC_END__;
4149                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4150         }
4151
4152         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4153         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4154
4155         req.cmd = WIFI_DIRECT_CMD_INIT_MIRACAST;
4156         req.client_id = g_client_info.client_id;
4157         req.data.int1 = enable;
4158
4159         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4160         if (res != WIFI_DIRECT_ERROR_NONE) {
4161                 __WDC_LOG_FUNC_END__;
4162                 return res;
4163         }
4164
4165         __WDC_LOG_FUNC_END__;
4166         return WIFI_DIRECT_ERROR_NONE;
4167 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4168         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4169 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4170 }
4171
4172 int wifi_direct_get_peer_info(char* mac_address, wifi_direct_discovered_peer_info_s** peer_info)
4173 {
4174         __WDC_LOG_FUNC_START__;
4175
4176         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
4177
4178         wifi_direct_client_request_s req;
4179         wifi_direct_client_response_s rsp;
4180         int res = WIFI_DIRECT_ERROR_NONE;
4181
4182         if ((g_client_info.is_registered == false) ||
4183                         (g_client_info.client_id == WFD_INVALID_ID)) {
4184                 WDC_LOGE("Client is NOT registered.");
4185                 __WDC_LOG_FUNC_END__;
4186                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4187         }
4188
4189         if (!mac_address) {
4190                 WDC_LOGE("mac_addr is NULL");
4191                 __WDC_LOG_FUNC_END__;
4192                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4193         }
4194
4195         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4196         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4197
4198         req.cmd = WIFI_DIRECT_CMD_GET_PEER_INFO;
4199         req.client_id = g_client_info.client_id;
4200         macaddr_atoe(mac_address, req.data.mac_addr);
4201
4202         pthread_mutex_lock(&g_client_info.mutex);
4203         res = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
4204                                                                 sizeof(wifi_direct_client_request_s));
4205         if (res != WIFI_DIRECT_ERROR_NONE) {
4206                 WDC_LOGE("Failed to write into socket [%s]", __wfd_print_error(res));
4207                 __wfd_reset_control();
4208                 pthread_mutex_unlock(&g_client_info.mutex);
4209                 __WDC_LOG_FUNC_END__;
4210                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4211         }
4212         WDC_LOGD("Succeeded to send request [%d: %s]", req.cmd, __wfd_client_print_cmd(req.cmd));
4213
4214         res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
4215                                                                 sizeof(wifi_direct_client_response_s));
4216         if (res <= 0) {
4217                 WDC_LOGE("Failed to read socket [%d]", res);
4218                 __wfd_reset_control();
4219                 pthread_mutex_unlock(&g_client_info.mutex);
4220                 __WDC_LOG_FUNC_END__;
4221                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4222         }
4223
4224         if (rsp.cmd != req.cmd) {
4225                 WDC_LOGE("Invalid resp [%d]", rsp.cmd);
4226                 pthread_mutex_unlock(&g_client_info.mutex);
4227                 __WDC_LOG_FUNC_END__;
4228                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4229         }
4230
4231         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
4232                 WDC_LOGE("Result received [%s]", __wfd_print_error(rsp.result));
4233                 pthread_mutex_unlock(&g_client_info.mutex);
4234                 __WDC_LOG_FUNC_END__;
4235                 return rsp.result;
4236         }
4237
4238         wfd_discovery_entry_s *buff = NULL;
4239
4240         buff = (wfd_discovery_entry_s*) g_try_malloc0(sizeof (wfd_discovery_entry_s));
4241         if (!buff) {
4242                 WDC_LOGE("Failed to alloc memory");
4243                 pthread_mutex_unlock(&g_client_info.mutex);
4244                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
4245         }
4246
4247         res = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) buff,
4248                                                                         sizeof(wfd_discovery_entry_s));
4249         pthread_mutex_unlock(&g_client_info.mutex);
4250         if (res <= 0) {
4251                 free(buff);
4252                 WDC_LOGE("Failed to read socket");
4253                 __wfd_reset_control();
4254                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
4255         }
4256         __wfd_client_print_entry_list(buff, 1);
4257         WDC_LOGD("wifi_direct_get_peer() SUCCESS");
4258
4259         wifi_direct_discovered_peer_info_s *peer = NULL;
4260
4261         peer = (wifi_direct_discovered_peer_info_s *) g_try_malloc0(sizeof(wifi_direct_discovered_peer_info_s));
4262         if (!peer) {
4263                         WDC_LOGE("Failed to alloc memory");
4264                         pthread_mutex_unlock(&g_client_info.mutex);
4265                         g_free(buff);
4266                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
4267                 }
4268         peer->device_name = g_strdup(buff->device_name);
4269         peer->mac_address = (char*) g_try_malloc0(MACSTR_LEN);
4270         g_snprintf(peer->mac_address, MACSTR_LEN, MACSTR, MAC2STR(buff->mac_address));
4271         peer->interface_address = (char*) g_try_malloc0(MACSTR_LEN);
4272         g_snprintf(peer->interface_address, MACSTR_LEN, MACSTR, MAC2STR(buff->intf_address));
4273         peer->channel = buff->channel;
4274         peer->is_connected = buff->is_connected;
4275         peer->is_group_owner = buff->is_group_owner;
4276         peer->is_persistent_group_owner = buff->is_persistent_go;
4277         peer->primary_device_type = buff->category;
4278         peer->secondary_device_type = buff->subcategory;
4279         peer->supported_wps_types= buff->wps_cfg_methods;
4280 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4281         peer->is_miracast_device = buff->is_wfd_device;
4282 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4283
4284         g_free(buff);
4285         *peer_info = peer;
4286
4287         __WDC_LOG_FUNC_END__;
4288         return WIFI_DIRECT_ERROR_NONE;
4289 }
4290
4291 int wifi_direct_set_passphrase(const char *passphrase)
4292 {
4293         __WDC_LOG_FUNC_START__;
4294
4295         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
4296
4297         wifi_direct_client_request_s req;
4298         wifi_direct_client_response_s rsp;
4299         int status = WIFI_DIRECT_ERROR_NONE;
4300
4301         if ((g_client_info.is_registered == false) ||
4302                         (g_client_info.client_id == WFD_INVALID_ID)) {
4303                 WDC_LOGE("Client is NOT registered.");
4304                 __WDC_LOG_FUNC_END__;
4305                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4306         }
4307
4308         if (!passphrase) {
4309                 WDC_LOGE("NULL Param [passphrase]!");
4310                 __WDC_LOG_FUNC_END__;
4311                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4312         }
4313         WDC_LOGD("passphrase = [%s]", passphrase);
4314
4315         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4316         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4317
4318         req.cmd = WIFI_DIRECT_CMD_SET_PASSPHRASE;
4319         req.client_id = g_client_info.client_id;
4320         req.cmd_data_len = 64;
4321
4322         pthread_mutex_lock(&g_client_info.mutex);
4323         status = __wfd_client_write_socket(g_client_info.sync_sockfd, &req,
4324                                                                   sizeof(wifi_direct_client_request_s));
4325         if (status != WIFI_DIRECT_ERROR_NONE) {
4326                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
4327                 __wfd_reset_control();
4328                 pthread_mutex_unlock(&g_client_info.mutex);
4329                 __WDC_LOG_FUNC_END__;
4330                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4331         }
4332         WDC_LOGD( "writing msg hdr is success!\n");
4333
4334         status = __wfd_client_write_socket(g_client_info.sync_sockfd, (void*) passphrase,
4335                                                                                                 req.cmd_data_len);
4336         if (status != WIFI_DIRECT_ERROR_NONE) {
4337                 WDC_LOGE("Error!!! writing to socket[%s]", __wfd_print_error(status));
4338                 __wfd_reset_control();
4339                 pthread_mutex_unlock(&g_client_info.mutex);
4340                 __WDC_LOG_FUNC_END__;
4341                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4342         }
4343
4344         status = __wfd_client_read_socket(g_client_info.sync_sockfd, (char*) &rsp,
4345                                                                   sizeof(wifi_direct_client_response_s));
4346         pthread_mutex_unlock(&g_client_info.mutex);
4347
4348         if (status <= 0) {
4349                 WDC_LOGE("Error!!! reading socket, status = %d", status);
4350                 __wfd_reset_control();
4351                 __WDC_LOG_FUNC_END__;
4352                 return WIFI_DIRECT_ERROR_COMMUNICATION_FAILED;
4353         } else {
4354                 if (rsp.cmd == WIFI_DIRECT_CMD_SET_PASSPHRASE) {
4355                         if (rsp.result != WIFI_DIRECT_ERROR_NONE) {
4356                                 WDC_LOGE("Error!!! Result received = %d", rsp.result);
4357                                 WDC_LOGE("Error!!! [%s]", __wfd_print_error(rsp.result));
4358                                 __WDC_LOG_FUNC_END__;
4359                                 return rsp.result;
4360                         }
4361                 } else {
4362                         WDC_LOGE("Error!!! Invalid resp cmd = %d", rsp.cmd);
4363                         return WIFI_DIRECT_ERROR_OPERATION_FAILED;
4364                 }
4365         }
4366
4367         __WDC_LOG_FUNC_END__;
4368         return WIFI_DIRECT_ERROR_NONE;
4369 }
4370
4371 int wifi_direct_get_passphrase(char** passphrase)
4372 {
4373         __WDC_LOG_FUNC_START__;
4374
4375         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
4376
4377         wifi_direct_client_request_s req;
4378         wifi_direct_client_response_s rsp;
4379         char la_passphrase[WIFI_DIRECT_WPA_LEN+1] = {0,};
4380         int res = WIFI_DIRECT_ERROR_NONE;
4381
4382         if ((g_client_info.is_registered == false) ||
4383                         (g_client_info.client_id == WFD_INVALID_ID)) {
4384                 WDC_LOGE("Client is NOT registered.");
4385                 __WDC_LOG_FUNC_END__;
4386                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4387         }
4388
4389         if(!passphrase){
4390                 WDC_LOGE("NULL Param [passphrase]!");
4391                 __WDC_LOG_FUNC_END__;
4392                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4393         }
4394
4395         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4396         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4397
4398         req.cmd = WIFI_DIRECT_CMD_GET_PASSPHRASE;
4399         req.client_id = g_client_info.client_id;
4400
4401         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4402         if (res != WIFI_DIRECT_ERROR_NONE) {
4403                 WDC_LOGE("Error!!! Invalid resp = %d", res);
4404                 __WDC_LOG_FUNC_END__;
4405                 return res;
4406         }
4407
4408         WDC_LOGD("wifi_direct_get_wpa_passphrase() SUCCESS");
4409         g_strlcpy(la_passphrase, rsp.param2, WIFI_DIRECT_WPA_LEN + 1);
4410         *passphrase = g_strdup(la_passphrase);
4411
4412         __WDC_LOG_FUNC_END__;
4413         return WIFI_DIRECT_ERROR_NONE;
4414 }
4415
4416 int wifi_direct_set_autoconnection_peer(char *mac_address)
4417 {
4418         __WDC_LOG_FUNC_START__;
4419
4420         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_FEATURE);
4421
4422         unsigned char la_mac_addr[6];
4423         wifi_direct_client_request_s req;
4424         wifi_direct_client_response_s rsp;
4425         int res = WIFI_DIRECT_ERROR_NONE;
4426
4427         if ((g_client_info.is_registered == false) ||
4428                         (g_client_info.client_id == WFD_INVALID_ID)) {
4429                 WDC_LOGE("Client is NOT registered");
4430                 __WDC_LOG_FUNC_END__;
4431                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4432         }
4433
4434         if (!mac_address) {
4435                 WDC_LOGE("mac_addr is NULL");
4436                 __WDC_LOG_FUNC_END__;
4437                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4438         }
4439
4440         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4441         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4442
4443         req.cmd = WIFI_DIRECT_CMD_SET_AUTOCONNECTION_PEER;
4444         req.client_id = g_client_info.client_id;
4445         macaddr_atoe(mac_address, la_mac_addr);
4446         memcpy(req.data.mac_addr, la_mac_addr, MACADDR_LEN);
4447
4448         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4449         if (res != WIFI_DIRECT_ERROR_NONE) {
4450                 __WDC_LOG_FUNC_END__;
4451                 return res;
4452         }
4453         WDC_LOGD("wifi_direct_set_autoconnection_peer() SUCCESS");
4454
4455         __WDC_LOG_FUNC_END__;
4456         return WIFI_DIRECT_ERROR_NONE;
4457 }
4458
4459 int wifi_direct_init_display(void)
4460 {
4461         __WDC_LOG_FUNC_START__;
4462 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4463
4464         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4465
4466         wifi_direct_client_request_s req;
4467         wifi_direct_client_response_s rsp;
4468         int res = WIFI_DIRECT_ERROR_NONE;
4469
4470         if ((g_client_info.is_registered == false) ||
4471                         (g_client_info.client_id == WFD_INVALID_ID)) {
4472                 WDC_LOGE("Client is NOT registered.");
4473                 __WDC_LOG_FUNC_END__;
4474                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4475         }
4476
4477         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4478         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4479
4480         req.cmd = WIFI_DIRECT_CMD_INIT_DISPLAY;
4481         req.client_id = g_client_info.client_id;
4482
4483         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4484         if (res != WIFI_DIRECT_ERROR_NONE) {
4485                 __WDC_LOG_FUNC_END__;
4486                 return res;
4487         }
4488
4489         __WDC_LOG_FUNC_END__;
4490         return WIFI_DIRECT_ERROR_NONE;
4491 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4492         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4493 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4494 }
4495
4496 int wifi_direct_deinit_display(void)
4497 {
4498         __WDC_LOG_FUNC_START__;
4499 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4500
4501         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4502
4503         wifi_direct_client_info_s *client_info = __wfd_get_control();
4504         wifi_direct_client_request_s req;
4505         wifi_direct_client_response_s rsp;
4506         int res = WIFI_DIRECT_ERROR_NONE;
4507
4508         if ((client_info->is_registered == false) ||
4509                 (client_info->client_id == WFD_INVALID_ID)) {
4510                         WDC_LOGE("Client is NOT registered");
4511                         __WDC_LOG_FUNC_END__;
4512                         return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4513         }
4514
4515         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4516         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4517
4518         req.cmd = WIFI_DIRECT_CMD_DEINIT_DISPLAY;
4519         req.client_id = g_client_info.client_id;
4520
4521         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4522         if (res != WIFI_DIRECT_ERROR_NONE) {
4523                 WDC_LOGE("Error!!! Invalid resp = %d", res);
4524                 __WDC_LOG_FUNC_END__;
4525                 return res;
4526         }
4527
4528         __WDC_LOG_FUNC_END__;
4529         return WIFI_DIRECT_ERROR_NONE;
4530 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4531         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4532 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4533 }
4534
4535 int wifi_direct_set_display(wifi_direct_display_type_e type, int port, int hdcp)
4536 {
4537         __WDC_LOG_FUNC_START__;
4538 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4539
4540         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4541
4542         wifi_direct_client_info_s *client_info = __wfd_get_control();
4543         wifi_direct_client_request_s req;
4544         wifi_direct_client_response_s rsp;
4545         int res = WIFI_DIRECT_ERROR_NONE;
4546
4547         if ((client_info->is_registered == false) ||
4548                 (client_info->client_id == WFD_INVALID_ID)) {
4549                         WDC_LOGE("Client is NOT registered");
4550                         __WDC_LOG_FUNC_END__;
4551                         return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4552         }
4553
4554         if (type < WIFI_DIRECT_DISPLAY_TYPE_SOURCE || type > WIFI_DIRECT_DISPLAY_TYPE_DUAL || port < 0 || hdcp < 0) {
4555                 WDC_LOGE("Invalid paramaeters passed type[%d], port[%d], hdcp[%d]!");
4556                 __WDC_LOG_FUNC_END__;
4557                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4558         }
4559
4560         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4561         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4562
4563         req.cmd = WIFI_DIRECT_CMD_SET_DISPLAY;
4564         req.client_id = g_client_info.client_id;
4565         req.data.int1 = type;
4566         req.data.int2 = port;
4567         req.data.int3 = hdcp;
4568
4569         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4570         if (res != WIFI_DIRECT_ERROR_NONE) {
4571                 WDC_LOGE("Error!!! Invalid resp = %d", res);
4572                 __WDC_LOG_FUNC_END__;
4573                 return res;
4574         }
4575
4576         __WDC_LOG_FUNC_END__;
4577         return WIFI_DIRECT_ERROR_NONE;
4578 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4579         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4580 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4581 }
4582
4583 int wifi_direct_set_display_availability(bool availability)
4584 {
4585         __WDC_LOG_FUNC_START__;
4586 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4587
4588         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4589
4590         wifi_direct_client_request_s req;
4591         wifi_direct_client_response_s rsp;
4592         int res = WIFI_DIRECT_ERROR_NONE;
4593
4594         if ((g_client_info.is_registered == false) ||
4595                         (g_client_info.client_id == WFD_INVALID_ID)) {
4596                 WDC_LOGE("Client is NOT registered.");
4597                 __WDC_LOG_FUNC_END__;
4598                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4599         }
4600
4601         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4602         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4603
4604         req.cmd = WIFI_DIRECT_CMD_SET_DISPLAY_AVAILABILITY;
4605         req.client_id = g_client_info.client_id;
4606         req.data.int1 = availability;
4607
4608         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4609         if (res != WIFI_DIRECT_ERROR_NONE) {
4610                 WDC_LOGE("Error!!! Invalid resp = %d", res);
4611                 __WDC_LOG_FUNC_END__;
4612                 return res;
4613         }
4614         __WDC_LOG_FUNC_END__;
4615         return WIFI_DIRECT_ERROR_NONE;
4616 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4617         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4618 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4619 }
4620
4621 int wifi_direct_get_peer_display_type(char *mac_address, wifi_direct_display_type_e *type)
4622 {
4623         __WDC_LOG_FUNC_START__;
4624 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4625
4626         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4627
4628         unsigned char la_mac_addr[6];
4629         wifi_direct_client_request_s req;
4630         wifi_direct_client_response_s rsp;
4631         int res = WIFI_DIRECT_ERROR_NONE;
4632
4633         if ((g_client_info.is_registered == false) ||
4634                         (g_client_info.client_id == WFD_INVALID_ID)) {
4635                 WDC_LOGE("Client is NOT registered");
4636                 __WDC_LOG_FUNC_END__;
4637                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4638         }
4639
4640         if (!mac_address || !type) {
4641                 WDC_LOGE("NULL Param!");
4642                 __WDC_LOG_FUNC_END__;
4643                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4644         }
4645
4646         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4647         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4648
4649         req.cmd = WIFI_DIRECT_CMD_GET_PEER_DISPLAY_TYPE;
4650         req.client_id = g_client_info.client_id;
4651         macaddr_atoe(mac_address, la_mac_addr);
4652         memcpy(req.data.mac_addr, la_mac_addr, 6);
4653
4654         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4655         if (res != WIFI_DIRECT_ERROR_NONE) {
4656                 __WDC_LOG_FUNC_END__;
4657                 return res;
4658         }
4659         WDC_LOGD("wifi_direct_get_peer_display_type() SUCCESS");
4660         *type = rsp.param1;
4661
4662
4663         __WDC_LOG_FUNC_END__;
4664         return WIFI_DIRECT_ERROR_NONE;
4665
4666         __WDC_LOG_FUNC_END__;
4667         return WIFI_DIRECT_ERROR_NONE;
4668 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4669         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4670 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4671 }
4672
4673 int wifi_direct_get_peer_display_availability(char *mac_address, bool *availability)
4674 {
4675         __WDC_LOG_FUNC_START__;
4676 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4677
4678         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4679
4680         unsigned char la_mac_addr[6];
4681         wifi_direct_client_request_s req;
4682         wifi_direct_client_response_s rsp;
4683         int res = WIFI_DIRECT_ERROR_NONE;
4684
4685         if ((g_client_info.is_registered == false) ||
4686                         (g_client_info.client_id == WFD_INVALID_ID)) {
4687                 WDC_LOGE("Client is NOT registered");
4688                 __WDC_LOG_FUNC_END__;
4689                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4690         }
4691
4692         if (!mac_address || !availability) {
4693                 WDC_LOGE("NULL Param!");
4694                 __WDC_LOG_FUNC_END__;
4695                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4696         }
4697
4698         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4699         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4700
4701         req.cmd = WIFI_DIRECT_CMD_GET_PEER_DISPLAY_AVAILABILITY;
4702         req.client_id = g_client_info.client_id;
4703         macaddr_atoe(mac_address, la_mac_addr);
4704         memcpy(req.data.mac_addr, la_mac_addr, 6);
4705
4706         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4707         if (res != WIFI_DIRECT_ERROR_NONE) {
4708                 __WDC_LOG_FUNC_END__;
4709                 return res;
4710         }
4711         WDC_LOGD("wifi_direct_get_peer_display_availability() SUCCESS");
4712         *availability = rsp.param1;
4713
4714
4715         __WDC_LOG_FUNC_END__;
4716         return WIFI_DIRECT_ERROR_NONE;
4717
4718         __WDC_LOG_FUNC_END__;
4719         return WIFI_DIRECT_ERROR_NONE;
4720 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4721         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4722 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4723 }
4724
4725 int wifi_direct_get_peer_display_hdcp(char *mac_address, int *hdcp)
4726 {
4727         __WDC_LOG_FUNC_START__;
4728 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4729
4730         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4731
4732         unsigned char la_mac_addr[6];
4733         wifi_direct_client_request_s req;
4734         wifi_direct_client_response_s rsp;
4735         int res = WIFI_DIRECT_ERROR_NONE;
4736
4737         if ((g_client_info.is_registered == false) ||
4738                         (g_client_info.client_id == WFD_INVALID_ID)) {
4739                 WDC_LOGE("Client is NOT registered");
4740                 __WDC_LOG_FUNC_END__;
4741                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4742         }
4743
4744         if (!mac_address || !hdcp) {
4745                 WDC_LOGE("NULL Param!");
4746                 __WDC_LOG_FUNC_END__;
4747                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4748         }
4749
4750         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4751         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4752
4753         req.cmd = WIFI_DIRECT_CMD_GET_PEER_DISPLAY_HDCP;
4754         req.client_id = g_client_info.client_id;
4755         macaddr_atoe(mac_address, la_mac_addr);
4756         memcpy(req.data.mac_addr, la_mac_addr, 6);
4757
4758         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4759         if (res != WIFI_DIRECT_ERROR_NONE) {
4760                 __WDC_LOG_FUNC_END__;
4761                 return res;
4762         }
4763         WDC_LOGD("wifi_direct_get_peer_display_hdcp() SUCCESS");
4764         *hdcp = rsp.param1;
4765
4766
4767         __WDC_LOG_FUNC_END__;
4768         return WIFI_DIRECT_ERROR_NONE;
4769
4770         __WDC_LOG_FUNC_END__;
4771         return WIFI_DIRECT_ERROR_NONE;
4772 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4773         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4774 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4775 }
4776
4777 int wifi_direct_get_peer_display_port(char *mac_address, int *port)
4778 {
4779         __WDC_LOG_FUNC_START__;
4780 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4781
4782         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4783
4784         unsigned char la_mac_addr[6];
4785         wifi_direct_client_request_s req;
4786         wifi_direct_client_response_s rsp;
4787         int res = WIFI_DIRECT_ERROR_NONE;
4788
4789         if ((g_client_info.is_registered == false) ||
4790                         (g_client_info.client_id == WFD_INVALID_ID)) {
4791                 WDC_LOGE("Client is NOT registered");
4792                 __WDC_LOG_FUNC_END__;
4793                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4794         }
4795
4796         if (!mac_address || !port) {
4797                 WDC_LOGE("NULL Param!");
4798                 __WDC_LOG_FUNC_END__;
4799                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4800         }
4801
4802         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4803         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4804
4805         req.cmd = WIFI_DIRECT_CMD_GET_PEER_DISPLAY_PORT;
4806         req.client_id = g_client_info.client_id;
4807         macaddr_atoe(mac_address, la_mac_addr);
4808         memcpy(req.data.mac_addr, la_mac_addr, 6);
4809
4810         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4811         if (res != WIFI_DIRECT_ERROR_NONE) {
4812                 __WDC_LOG_FUNC_END__;
4813                 return res;
4814         }
4815         WDC_LOGD("wifi_direct_get_peer_display_port() SUCCESS");
4816         *port = rsp.param1;
4817
4818
4819         __WDC_LOG_FUNC_END__;
4820         return WIFI_DIRECT_ERROR_NONE;
4821
4822         __WDC_LOG_FUNC_END__;
4823         return WIFI_DIRECT_ERROR_NONE;
4824 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4825         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4826 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4827 }
4828
4829 int wifi_direct_get_peer_display_throughput(char *mac_address, int *throughput)
4830 {
4831         __WDC_LOG_FUNC_START__;
4832 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
4833
4834         CHECK_FEATURE_SUPPORTED(WIFIDIRECT_DISPLAY_FEATURE);
4835
4836         unsigned char la_mac_addr[6];
4837         wifi_direct_client_request_s req;
4838         wifi_direct_client_response_s rsp;
4839         int res = WIFI_DIRECT_ERROR_NONE;
4840
4841         if ((g_client_info.is_registered == false) ||
4842                         (g_client_info.client_id == WFD_INVALID_ID)) {
4843                 WDC_LOGE("Client is NOT registered");
4844                 __WDC_LOG_FUNC_END__;
4845                 return WIFI_DIRECT_ERROR_NOT_INITIALIZED;
4846         }
4847
4848         if (!mac_address || !throughput) {
4849                 WDC_LOGE("NULL Param!");
4850                 __WDC_LOG_FUNC_END__;
4851                 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
4852         }
4853
4854         memset(&req, 0, sizeof(wifi_direct_client_request_s));
4855         memset(&rsp, 0, sizeof(wifi_direct_client_response_s));
4856
4857         req.cmd = WIFI_DIRECT_CMD_GET_PEER_DISPLAY_THROUGHPUT;
4858         req.client_id = g_client_info.client_id;
4859         macaddr_atoe(mac_address, la_mac_addr);
4860         memcpy(req.data.mac_addr, la_mac_addr, 6);
4861
4862         res = __wfd_client_send_request(g_client_info.sync_sockfd, &req, &rsp);
4863         if (res != WIFI_DIRECT_ERROR_NONE) {
4864                 __WDC_LOG_FUNC_END__;
4865                 return res;
4866         }
4867         WDC_LOGD("wifi_direct_get_peer_display_throughput() SUCCESS");
4868         *throughput = rsp.param1;
4869
4870
4871         __WDC_LOG_FUNC_END__;
4872         return WIFI_DIRECT_ERROR_NONE;
4873
4874         __WDC_LOG_FUNC_END__;
4875         return WIFI_DIRECT_ERROR_NONE;
4876 #else /* TIZEN_FEATURE_WIFI_DISPLAY */
4877         return WIFI_DIRECT_ERROR_NOT_SUPPORTED;
4878 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
4879 }