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