Fix bug(N_SE-23716)
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-client.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://www.tizenopensource.org/license
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21  * This file implements wifi direct application client  functions.
22  *
23  * @file    wfd-app-client.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "wfd-app.h"
32 #include "wfd-app-util.h"
33 #include "wfd-app-strings.h"
34 #include <vconf.h>
35
36 /**
37  *      This function let the app make a callback for connected peer
38  *      @return   TRUE
39  *      @param[in] peer the pointer to the connected peer
40  *      @param[in] user_data the pointer to the main data structure
41  */
42 bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s *peer, void *user_data)
43 {
44         __WDPOP_LOG_FUNC_ENTER__;
45
46         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
47         if (NULL == ad || NULL == peer || NULL == peer->device_name || NULL == peer->mac_address) {
48                 WDPOP_LOGD( "NULL parameters.\n");
49                 return FALSE;
50         }
51
52         int peer_cnt = ad->raw_connected_peer_cnt;
53         WDPOP_LOGD( "%dth connected peer. [%s]\n", peer_cnt, peer->device_name);
54
55         strncpy(ad->raw_connected_peers[peer_cnt].ssid, peer->device_name, sizeof(ad->raw_connected_peers[peer_cnt].ssid));
56         strncpy(ad->raw_connected_peers[peer_cnt].mac_address, peer->mac_address, WFD_MAC_ADDRESS_SIZE);
57         WDPOP_LOGD( "\tSSID: [%s]\n", ad->raw_connected_peers[peer_cnt].ssid);
58         ad->raw_connected_peer_cnt++;
59
60         free(peer->device_name);
61         free(peer->mac_address);
62         free(peer);
63
64         __WDPOP_LOG_FUNC_EXIT__;
65         return TRUE;
66 }
67
68 /**
69  *      This function let the app get the connected peers
70  *      @return   If success, return 0, else return -1
71  *      @param[in] ugd the pointer to the main data structure
72  */
73 int _wfd_app_get_connected_peers(void *user_data)
74 {
75         __WDPOP_LOG_FUNC_ENTER__;
76
77         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
78         if (NULL == ad) {
79                 WDPOP_LOGD( "NULL parameters.\n");
80                 return -1;
81         }
82
83         int res = 0;
84
85         ad->raw_connected_peer_cnt = 0;
86         res = wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb, (void *)ad);
87         if (res != WIFI_DIRECT_ERROR_NONE) {
88                 ad->raw_connected_peer_cnt = 0;
89                 WDPOP_LOGD( "Get connected peer failed: %d\n", res);
90         }
91
92         __WDPOP_LOG_FUNC_EXIT__;
93         return 0;
94 }
95
96 /**
97  *      This function let the app delete the notification
98  *      @return   void
99  */
100 void _del_wfd_notification()
101 {
102         __WDPOP_LOG_FUNC_ENTER__;
103
104         /* delete the notification */
105         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
106         noti_err  = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
107         if (noti_err != NOTIFICATION_ERROR_NONE) {
108                 WDPOP_LOGD( "Fail to notification_delete_all_by_type.(%d)\n", noti_err);
109                 return;
110         }
111
112         __WDPOP_LOG_FUNC_EXIT__;
113 }
114
115 /**
116  *      This function let the app add the notification when it is connected
117  *      @return   void
118  *      @param[in] user_data the pointer to the main data structure
119  */
120 void _add_wfd_peers_connected_notification(void *user_data)
121 {
122         __WDPOP_LOG_FUNC_ENTER__;
123
124         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
125         if (NULL == ad || NULL == ad->noti) {
126                 WDPOP_LOGD( "NULL parameters.\n");
127                 return;
128         }
129
130         char msg[WFD_MAX_SIZE] = {0};
131         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
132
133         /* delete all notifications */
134         _del_wfd_notification();
135
136         /* set the icon */
137         noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON,  RESDIR"/images/A09_notification_icon.png");
138         if (noti_err != NOTIFICATION_ERROR_NONE) {
139                 WDPOP_LOGD( "Fail to notification_set_image. (%d)\n", noti_err);
140                 return;
141         }
142
143         /* set the title and content */
144         _wfd_app_get_connected_peers(ad);
145         snprintf(msg, WFD_MAX_SIZE, "Connected with %d devices via Wi-Fi Direct", ad->raw_connected_peer_cnt);
146         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE, msg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
147         if (noti_err != NOTIFICATION_ERROR_NONE) {
148                 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
149                 return;
150         }
151
152         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
153                 "Tap to change settings", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
154         if (noti_err != NOTIFICATION_ERROR_NONE) {
155                 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
156                 return;
157         }
158
159         bundle *b = NULL;
160         b = bundle_create();
161         appsvc_set_pkgname(b, PACKAGE);
162         appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
163
164         int res = NOTIFICATION_ERROR_NONE;
165         res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
166         if (res != NOTIFICATION_ERROR_NONE) {
167                 WDPOP_LOGD( "Failed to notification_set_execute_option. [%d]", res);
168                 return;
169         }
170
171         bundle_free(b);
172
173         /* set display application list */
174         noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
175         if (noti_err != NOTIFICATION_ERROR_NONE) {
176                 WDPOP_LOGD( "Fail to notification_set_display_applist : %d\n", noti_err);
177                 return;
178         }
179
180         /* notify the quick panel */
181         noti_err = notification_insert(ad->noti, NULL);
182         if (noti_err != NOTIFICATION_ERROR_NONE) {
183                 WDPOP_LOGD( "Fail to notification_insert.(%d)\n", noti_err);
184                 return;
185         }
186
187         __WDPOP_LOG_FUNC_EXIT__;
188 }
189
190 /**
191  *      This function let the app add the notification when it shoule be turned off
192  *      @return   void
193  *      @param[in] user_data the pointer to the main data structure
194  */
195 void _add_wfd_turn_off_notification(void *user_data)
196 {
197         __WDPOP_LOG_FUNC_ENTER__;
198
199         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
200         if (NULL == ad || NULL == ad->noti) {
201                 WDPOP_LOGD( "NULL parameters.\n");
202                 return;
203         }
204
205         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
206
207         /* delete all notifications */
208         _del_wfd_notification();
209
210         /* set the icon */
211         noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON,  RESDIR"/images/A09_notification_icon.png");
212         if (noti_err != NOTIFICATION_ERROR_NONE) {
213                 WDPOP_LOGD( "Fail to notification_set_image. (%d)\n", noti_err);
214                 return;
215         }
216
217         /* set the title and content */
218         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE,
219                 "Disable Wi-Fi Direct after use", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
220         if (noti_err != NOTIFICATION_ERROR_NONE) {
221                 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
222                 return;
223         }
224
225         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
226                 "Disable Wi-Fi Direct after use to save battery", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
227         if (noti_err != NOTIFICATION_ERROR_NONE) {
228                 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
229                 return;
230         }
231
232         bundle *b = NULL;
233         b = bundle_create();
234         appsvc_set_pkgname(b, PACKAGE);
235         appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
236
237         int res = NOTIFICATION_ERROR_NONE;
238         res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
239         if (res != NOTIFICATION_ERROR_NONE) {
240                 WDPOP_LOGD( "Failed to notification_set_execute_option. [%d]", res);
241                 return;
242         }
243
244         bundle_free(b);
245
246         /* set display application list */
247         noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
248         if (noti_err != NOTIFICATION_ERROR_NONE) {
249                 WDPOP_LOGD( "Fail to notification_set_display_applist : %d\n", noti_err);
250                 return;
251         }
252
253         /* notify the quick panel */
254         noti_err = notification_insert(ad->noti, NULL);
255         if (noti_err != NOTIFICATION_ERROR_NONE) {
256                 WDPOP_LOGD( "Fail to notification_insert.(%d)\n", noti_err);
257                 return;
258         }
259
260         __WDPOP_LOG_FUNC_EXIT__;
261 }
262
263 /**
264  *      This function let the app make a callback for deactivating wfd automatically when connected
265  *      @return   if stop the timer, return ECORE_CALLBACK_CANCEL, else return ECORE_CALLBACK_RENEW
266  *      @param[in] user_data the pointer to the main data structure
267  */
268 static Eina_Bool _wfd_automatic_deactivated_for_connection_cb(void *user_data)
269 {
270         int interval = 0;
271         int wfd_transfer_state = 0;
272         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
273
274         if (NULL == ad) {
275                 WDPOP_LOGD( "NULL parameters.\n");
276                 return ECORE_CALLBACK_CANCEL;
277         }
278
279         /* check the timeout, if not timeout, keep the cb */
280         interval = time(NULL) - ad->last_wfd_transmit_time;
281         if (interval < NO_ACTION_TIME_OUT) {
282                 return ECORE_CALLBACK_RENEW;
283         }
284
285         /* get transfer state */
286         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_TRANSFER_STATE, &wfd_transfer_state) < 0) {
287                 WDPOP_LOGD( "Error reading vconf (%s)\n",
288                         VCONFKEY_WIFI_DIRECT_TRANSFER_STATE);
289                 return ECORE_CALLBACK_CANCEL;
290         }
291
292         /* show tickernoti*/
293         if (wfd_transfer_state > VCONFKEY_WIFI_DIRECT_TRANSFER_START) {
294                 WDPOP_LOGD( "No RX/TX packet, turn off WFD automatically.\n");
295                 _add_wfd_turn_off_notification(ad);
296         } else {
297                 WDPOP_LOGD( "Has RX/TX packet, restart.\n");
298                 ad->last_wfd_transmit_time = time(NULL);
299                 return ECORE_CALLBACK_RENEW;
300         }
301
302         return ECORE_CALLBACK_CANCEL;
303 }
304
305 /**
306  *      This function let the app make a callback for registering activation event
307  *      @return   void
308  *      @param[in] error_code the returned error code
309  *      @param[in] device_state the state of device
310  *      @param[in] user_data the pointer to the main data structure
311  */
312 void _cb_activation(int error_code, wifi_direct_device_state_e device_state, void *user_data)
313 {
314         __WDPOP_LOG_FUNC_ENTER__;
315         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
316
317         switch (device_state) {
318         case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
319                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
320                 break;
321
322         case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
323                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
324                 WDPOP_LOGD( "Termination process of wifi-direct popup begins...\n");
325
326                 /* when deactivated, stop the timer */
327                 if (ad->transmit_timer) {
328                         ecore_timer_del(ad->transmit_timer);
329                         ad->transmit_timer = NULL;
330                 }
331
332                 elm_exit();
333                 break;
334
335         default:
336                 break;
337         }
338
339         __WDPOP_LOG_FUNC_EXIT__;
340 }
341
342 /**
343  *      This function let the app find the peer by mac address
344  *      @return   the found peer
345  *      @param[in] data the pointer to the main data structure
346  *      @param[in] mac_address the pointer to mac address
347  */
348 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data, const char *mac_address)
349 {
350         __WDPOP_LOG_FUNC_ENTER__;
351         wfd_appdata_t *ad = (wfd_appdata_t *) data;
352         int i;
353
354         if (ad == NULL) {
355                 WDPOP_LOGD( "Incorrect parameter(NULL)\n");
356                 return NULL;
357         }
358
359         WDPOP_LOGD( "find peer by MAC [%s] \n", mac_address);
360
361         for (i = 0; i < ad->discovered_peer_count; i++) {
362                 WDPOP_LOGD( "check %dth peer\n", i);
363
364                 if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18)) {
365                         WDPOP_LOGD( "found peer. [%d]\n", i);
366                         __WDPOP_LOG_FUNC_EXIT__;
367                         return &ad->discovered_peers[i];
368                 }
369         }
370
371         __WDPOP_LOG_FUNC_EXIT__;
372         return NULL;
373 }
374
375 /**
376  *      This function let the app make a callback for discovering peer
377  *      @return   TRUE
378  *      @param[in] peer the pointer to the discovered peer
379  *      @param[in] user_data the pointer to the main data structure
380  */
381 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s *peer, void *user_data)
382 {
383         __WDPOP_LOG_FUNC_ENTER__;
384         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
385
386         if (NULL != peer->device_name) {
387                 WDPOP_LOGD( "discovered peer ssid[%s]\n", peer->device_name);
388                 strncpy(ad->discovered_peers[ad->discovered_peer_count].ssid, peer->device_name, 32);
389         } else {
390                 WDPOP_LOGD( "peer's device name is NULL\n");
391         }
392
393         if (NULL != peer->mac_address) {
394                 WDPOP_LOGD( "discovered peer mac[%s]\n", peer->mac_address);
395                 strncpy(ad->discovered_peers[ad->discovered_peer_count].mac_address, peer->mac_address, 18);
396         } else {
397                 WDPOP_LOGD( "peer's mac is NULL\n");
398         }
399
400         ad->discovered_peer_count++;
401
402         __WDPOP_LOG_FUNC_EXIT__;
403         return TRUE;
404
405 }
406
407 /**
408  *      This function let the app make a callback for registering discover event
409  *      @return   void
410  *      @param[in] error_code the returned error code
411  *      @param[in] discovery_state the state of discover
412  *      @param[in] user_data the pointer to the main data structure
413  */
414 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state, void *user_data)
415 {
416         __WDPOP_LOG_FUNC_ENTER__;
417         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
418         int ret;
419
420         switch (discovery_state) {
421         case WIFI_DIRECT_DISCOVERY_STARTED:
422                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
423                 break;
424
425         case WIFI_DIRECT_ONLY_LISTEN_STARTED:
426                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
427                 break;
428
429         case WIFI_DIRECT_DISCOVERY_FINISHED:
430                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
431                 break;
432
433         case WIFI_DIRECT_DISCOVERY_FOUND:
434                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
435
436                 if (NULL != ad->discovered_peers) {
437                         free(ad->discovered_peers);
438                         ad->discovered_peers = NULL;
439                 }
440
441                 ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
442                 ad->discovered_peer_count = 0;
443
444                 ret = wifi_direct_foreach_discovered_peers(_wfd_app_discoverd_peer_cb, (void *) ad);
445                 if (ret != WIFI_DIRECT_ERROR_NONE) {
446                         WDPOP_LOGD( "get discovery result failed: %d\n", ret);
447                 }
448                 break;
449
450         default:
451                 break;
452         }
453
454         __WDPOP_LOG_FUNC_EXIT__;
455 }
456
457 /**
458  *      This function let the app make a callback for registering connection event
459  *      @return   void
460  *      @param[in] error_code the returned error code
461  *      @param[in] connection_state the state of connection
462  *      @param[in] mac_address the mac address of peer
463  *      @param[in] user_data the pointer to the main data structure
464  */
465 void _cb_connection(int error_code, wifi_direct_connection_state_e connection_state, const char *mac_address, void *user_data)
466 {
467         __WDPOP_LOG_FUNC_ENTER__;
468
469         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
470         int result = -1;
471         char msg[WFD_POP_STR_MAX_LEN] = {0};
472         wfd_device_info_t *peer_info = NULL;
473
474         /* find the peer's name by the mac address */
475         if (NULL == mac_address) {
476                 WDPOP_LOGE("ERROR : mac address is NULL !!\n");
477                 return;
478         }
479
480         /* when disconnection, mac_address is empty */
481         if (connection_state <= WIFI_DIRECT_CONNECTION_RSP) {
482                 memset(ad->peer_mac, 0, sizeof(ad->peer_mac));
483                 memset(ad->peer_name, 0, sizeof(ad->peer_name));
484                 strncpy(ad->peer_mac, mac_address, strlen(mac_address));
485                 peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
486
487                 if (NULL == peer_info) {
488                         WDPOP_LOGD( "peer_info is NULL !!\n");
489                 } else if (0 == strlen(peer_info->ssid)) {
490                         WDPOP_LOGD( "SSID from connection is invalid !!\n");
491                 } else {
492                         WDPOP_LOGD( "SSID from connection is %s.\n", peer_info->ssid);
493                         strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
494                 }
495
496                 if (0 == strlen(ad->peer_name)) {
497                         strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
498                 }
499         }
500
501         switch (connection_state) {
502         case WIFI_DIRECT_CONNECTION_RSP:
503         {
504                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
505                 wfd_destroy_popup();
506
507                 if (error_code == WIFI_DIRECT_ERROR_NONE) {
508                         WDPOP_LOGD( "Link Complete!\n");
509
510                         /* add connected notification */
511                         _add_wfd_peers_connected_notification(ad);
512
513                         /* tickernoti popup */
514                         snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECTED, ad->peer_name);
515                         wfd_tickernoti_popup(msg);
516                 } else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED) {
517                         WDPOP_LOGD(
518                                         "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
519                         wfd_tickernoti_popup(_("IDS_WFD_POP_PIN_INVALID"));
520                 } else {
521                         if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT) {
522                                 WDPOP_LOGD(
523                                                 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
524                         } else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED) {
525                                 WDPOP_LOGD(
526                                                 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
527                         }
528
529                         /* tickernoti popup */
530                         snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECT_FAILED, ad->peer_name);
531                         wfd_tickernoti_popup(msg);
532                 }
533         }
534         break;
535
536         case WIFI_DIRECT_CONNECTION_WPS_REQ:
537         {
538                 wifi_direct_wps_type_e wps_mode;
539
540                 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
541
542                 WDPOP_LOGD(
543                                 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
544                 result = wifi_direct_get_wps_type(&wps_mode);
545                 WDPOP_LOGD(
546                                 "wifi_direct_get_wps_type() result=[%d]\n", result);
547
548                 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
549                         WDPOP_LOGD(
550                                         "wps_config is WIFI_DIRECT_WPS_TYPE_PBC. Ignore it..\n");
551                 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
552                         char *pin;
553                         WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
554
555                         if (wifi_direct_generate_wps_pin() != WIFI_DIRECT_ERROR_NONE) {
556                                 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
557                                 return;
558                         }
559
560                         if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
561                                 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
562                                 return;
563                         }
564
565                         strncpy(ad->pin_number, pin, 64);
566                         free(pin);
567                         pin = NULL;
568
569                         WDPOP_LOGD( "pin=[%s]\n", ad->pin_number);
570
571                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
572                 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
573                         WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
574                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, (void *) NULL);
575                 } else {
576                         WDPOP_LOGD( "wps_config is unkown!\n");
577
578                 }
579         }
580         break;
581
582         case WIFI_DIRECT_CONNECTION_REQ:
583         {
584                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
585
586                 wifi_direct_wps_type_e wps_mode;
587                 bool auto_connection_mode;
588
589                 result = wifi_direct_get_wps_type(&wps_mode);
590                 WDPOP_LOGD( "wifi_direct_get_wps_type() result=[%d]\n", result);
591
592                 result = wifi_direct_is_autoconnection_mode(&auto_connection_mode);
593                 WDPOP_LOGD( "wifi_direct_is_autoconnection_mode() result=[%d]\n", result);
594
595                 if (auto_connection_mode == TRUE) {
596                         result = wifi_direct_accept_connection(ad->peer_mac);
597                         printf("wifi_direct_accept_connection() result=[%d]\n", result);
598                 } else {
599                         if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
600                                 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PBC\n");
601                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
602                         } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
603                                 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
604                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
605                         } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
606                                 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
607                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
608                         } else {
609                                 WDPOP_LOGD( "wps_config is unkown!\n");
610                         }
611                 }
612         }
613         break;
614
615         case WIFI_DIRECT_DISCONNECTION_IND:
616         {
617                 _del_wfd_notification();
618                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
619
620                 result = wifi_direct_set_autoconnection_mode(false);
621                 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
622
623                 /* tickernoti popup */
624                 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
625                 wfd_tickernoti_popup(msg);
626         }
627         break;
628
629         case WIFI_DIRECT_DISCONNECTION_RSP:
630         {
631                 _del_wfd_notification();
632                 wfd_destroy_popup();
633
634                 result = wifi_direct_set_autoconnection_mode(false);
635                 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
636
637                 /* tickernoti popup */
638                 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
639                 wfd_tickernoti_popup(msg);
640         }
641         break;
642         case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
643         {
644                 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
645                 /* tickernoti popup */
646                 wfd_tickernoti_popup(_("IDS_WFD_POP_CONNECTING"));
647         }
648         default:
649                 break;
650
651         }
652
653         /* if connected, start the transmit timer */
654         wifi_direct_get_state(&ad->wfd_status);
655         WDPOP_LOGD( "status: %d", ad->wfd_status);
656
657         if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
658             if (ad->transmit_timer) {
659                     ecore_timer_del(ad->transmit_timer);
660                     ad->transmit_timer = NULL;
661             }
662         } else {
663                 if (NULL == ad->transmit_timer) {
664                         WDPOP_LOGD( "start the transmit timer\n");
665                         ad->last_wfd_transmit_time = time(NULL);
666                         ad->transmit_timer = ecore_timer_add(5.0,
667                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_connection_cb, ad);
668                 }
669         }
670
671         __WDPOP_LOG_FUNC_EXIT__;
672 }
673
674 /**
675  *      This function let the app make a change callback for flight mode
676  *      @return   void
677  *      @param[in] key the pointer to the key
678  *      @param[in] user_data the pointer to the main data structure
679  */
680 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
681 {
682         __WDPOP_LOG_FUNC_ENTER__;
683         int res = -1;
684         int flight_mode = 0;
685         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
686
687         if (NULL == ad) {
688                 WDPOP_LOGE("NULL parameters.\n");
689                 return;
690         }
691
692         res = vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
693         if (res != 0) {
694                 WDPOP_LOGE("Failed to get flight state from vconf. [%d]\n", res);
695                 return;
696         }
697
698         if (flight_mode == FALSE) {
699                 WDPOP_LOGD( "Flight mode is off\n");
700                 return;
701         }
702
703         /* If flight mode is on, turn off WFD */
704         wifi_direct_get_state(&ad->wfd_status);
705         if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
706                 WDPOP_LOGD( "Wi-Fi Direct is deactivated.\n");
707                 return;
708         }
709
710         /*if connected, disconnect all devices*/
711         if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
712                 res = wifi_direct_disconnect_all();
713                 if (res != WIFI_DIRECT_ERROR_NONE) {
714                         WDPOP_LOGE("Failed to send disconnection request to all. [%d]\n", res);
715                         return;
716                 }
717         }
718
719         res = wifi_direct_deactivate();
720         if (res != WIFI_DIRECT_ERROR_NONE) {
721                 WDPOP_LOGE("Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
722                 return;
723         }
724
725         __WDPOP_LOG_FUNC_EXIT__;
726 }
727
728 /**
729  *      This function let the app do initialization
730  *      @return   If success, return TRUE, else return FALSE
731  *      @param[in] ad the pointer to the main data structure
732  */
733 int init_wfd_popup_client(wfd_appdata_t *ad)
734 {
735         __WDPOP_LOG_FUNC_ENTER__;
736
737         if (NULL == ad) {
738                 WDPOP_LOGD( "NULL parameters.\n");
739                 return FALSE;
740         }
741
742         int ret = -1;
743
744         ret = wifi_direct_initialize();
745         if (ret != WIFI_DIRECT_ERROR_NONE) {
746                 WDPOP_LOGE("Failed to initialize Wi-Fi Direct. error code = [%d]\n", ret);
747                 return FALSE;
748         }
749
750         ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *)ad);
751         if (ret != WIFI_DIRECT_ERROR_NONE) {
752                 WDPOP_LOGE("Failed to register _cb_activation. error code = [%d]\n", ret);
753                 return FALSE;
754         }
755
756         ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *)ad);
757         if (ret != WIFI_DIRECT_ERROR_NONE) {
758                 WDPOP_LOGE("Failed to register _cb_discover. error code = [%d]\n", ret);
759                 return FALSE;
760         }
761
762         ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void *)ad);
763         if (ret != WIFI_DIRECT_ERROR_NONE) {
764                 WDPOP_LOGE("Failed to register _cb_connection. error code = [%d]\n", ret);
765                 return FALSE;
766         }
767
768         /* initialize notification */
769         ad->noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
770         if (NULL == ad->noti) {
771                 WDPOP_LOGD( "notification_new failed.\n");
772                 return FALSE;
773         }
774
775         /* register flight mode */
776         int result = -1;
777         result = vconf_notify_key_changed(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, _wfd_flight_mode_changed, ad);
778         if (result == -1) {
779                 WDPOP_LOGE("Failed to register vconf callback for flight mode\n");
780                 return FALSE;
781         }
782
783         __WDPOP_LOG_FUNC_EXIT__;
784
785         if (ret == WIFI_DIRECT_ERROR_NONE) {
786                 return TRUE;
787         } else {
788                 return FALSE;
789         }
790 }
791
792 /**
793  *      This function let the app do de-initialization
794  *      @return   If success, return TRUE, else return FALSE
795  *      @param[in] ad the pointer to the main data structure
796  */
797 int deinit_wfd_popup_client(wfd_appdata_t *ad)
798 {
799         __WDPOP_LOG_FUNC_ENTER__;
800
801         if (NULL == ad || NULL == ad->noti) {
802                 WDPOP_LOGD( "NULL parameters.\n");
803                 return FALSE;
804         }
805
806         int ret = -1;
807
808         ret = wifi_direct_deinitialize();
809
810         _del_wfd_notification(ad);
811         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
812         noti_err = notification_free(ad->noti);
813         if (noti_err != NOTIFICATION_ERROR_NONE) {
814                 WDPOP_LOGD( "Fail to notification_free.(%d)\n", noti_err);
815                 ret = WIFI_DIRECT_ERROR_RESOURCE_BUSY;
816         }
817
818         /* remove callback for flight mode */
819         int result = -1;
820         result = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_flight_mode_changed);
821         if (result == -1) {
822                 WDPOP_LOGE("Failed to ignore vconf key callback for flight mode\n");
823         }
824
825         if (ad->transmit_timer) {
826                 ecore_timer_del(ad->transmit_timer);
827                 ad->transmit_timer = NULL;
828         }
829
830         __WDPOP_LOG_FUNC_EXIT__;
831
832         if (ret == WIFI_DIRECT_ERROR_NONE) {
833                 return TRUE;
834         } else {
835                 return FALSE;
836         }
837 }