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