4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (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
10 * http://floralicense.org/license
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.
21 * This file implements wifi direct application client functions.
23 * @file wfd-app-client.c
24 * @author Sungsik Jang (sungsik.jang@samsung.com)
32 #include <notification.h>
36 #include "wfd-app-util.h"
37 #include "wfd-app-strings.h"
40 * This function let the app make a callback for connected peer
42 * @param[in] peer the pointer to the connected peer
43 * @param[in] user_data the pointer to the main data structure
45 bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s *peer, void *user_data)
47 __WDPOP_LOG_FUNC_ENTER__;
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");
55 int peer_cnt = ad->raw_connected_peer_cnt;
56 WDPOP_LOGD( "%dth connected peer. [%s]\n", peer_cnt, peer->device_name);
58 strncpy(ad->raw_connected_peers[peer_cnt].ssid, peer->device_name, sizeof(ad->raw_connected_peers[peer_cnt].ssid));
59 ad->raw_connected_peers[peer_cnt].ssid[31] = '\0';
60 strncpy(ad->raw_connected_peers[peer_cnt].mac_address, peer->mac_address, WFD_MAC_ADDRESS_SIZE);
61 ad->raw_connected_peers[peer_cnt].mac_address[17] = '\0';
62 WDPOP_LOGD( "\tSSID: [%s]\n", ad->raw_connected_peers[peer_cnt].ssid);
63 ad->raw_connected_peer_cnt++;
65 free(peer->device_name);
66 free(peer->mac_address);
69 __WDPOP_LOG_FUNC_EXIT__;
74 * This function let the app get the connected peers
75 * @return If success, return 0, else return -1
76 * @param[in] ugd the pointer to the main data structure
78 int _wfd_app_get_connected_peers(void *user_data)
80 __WDPOP_LOG_FUNC_ENTER__;
82 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
84 WDPOP_LOGD( "NULL parameters.\n");
90 ad->raw_connected_peer_cnt = 0;
91 res = wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb, (void *)ad);
92 if (res != WIFI_DIRECT_ERROR_NONE) {
93 ad->raw_connected_peer_cnt = 0;
94 WDPOP_LOGD( "Get connected peer failed: %d\n", res);
97 __WDPOP_LOG_FUNC_EXIT__;
102 * This function let the app delete the notification
105 void _del_wfd_notification()
107 __WDPOP_LOG_FUNC_ENTER__;
109 /* delete the notification */
110 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
111 noti_err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
112 if (noti_err != NOTIFICATION_ERROR_NONE) {
113 WDPOP_LOGD( "Fail to notification_delete_all_by_type.(%d)\n", noti_err);
117 __WDPOP_LOG_FUNC_EXIT__;
121 * This function let the app add the notification when it is connected
123 * @param[in] user_data the pointer to the main data structure
125 void _add_wfd_peers_connected_notification(void *user_data)
127 __WDPOP_LOG_FUNC_ENTER__;
129 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
130 if (NULL == ad || NULL == ad->noti) {
131 WDPOP_LOGD( "NULL parameters.\n");
135 char msg[WFD_MAX_SIZE] = {0};
136 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
138 /* delete all notifications */
139 _del_wfd_notification();
142 noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON, RESDIR"/images/A09_notification_icon.png");
143 if (noti_err != NOTIFICATION_ERROR_NONE) {
144 WDPOP_LOGD( "Fail to notification_set_image. (%d)\n", noti_err);
148 /* set the title and content */
149 _wfd_app_get_connected_peers(ad);
150 snprintf(msg, WFD_MAX_SIZE, "Connected with %d devices via Wi-Fi Direct", ad->raw_connected_peer_cnt);
151 noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE, msg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
152 if (noti_err != NOTIFICATION_ERROR_NONE) {
153 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
157 noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
158 "Tap to change settings", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
159 if (noti_err != NOTIFICATION_ERROR_NONE) {
160 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
166 appsvc_set_pkgname(b, PACKAGE);
167 appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
169 int res = NOTIFICATION_ERROR_NONE;
170 res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
171 if (res != NOTIFICATION_ERROR_NONE) {
172 WDPOP_LOGD( "Failed to notification_set_execute_option. [%d]", res);
178 /* set display application list */
179 noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
180 if (noti_err != NOTIFICATION_ERROR_NONE) {
181 WDPOP_LOGD( "Fail to notification_set_display_applist : %d\n", noti_err);
185 /* notify the quick panel */
186 noti_err = notification_insert(ad->noti, NULL);
187 if (noti_err != NOTIFICATION_ERROR_NONE) {
188 WDPOP_LOGD( "Fail to notification_insert.(%d)\n", noti_err);
192 __WDPOP_LOG_FUNC_EXIT__;
196 * This function let the app add the notification when it shoule be turned off
198 * @param[in] user_data the pointer to the main data structure
200 void _add_wfd_turn_off_notification(void *user_data)
202 __WDPOP_LOG_FUNC_ENTER__;
204 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
205 if (NULL == ad || NULL == ad->noti) {
206 WDPOP_LOGD( "NULL parameters.\n");
210 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
212 /* delete all notifications */
213 _del_wfd_notification();
216 noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON, RESDIR"/images/A09_notification_icon.png");
217 if (noti_err != NOTIFICATION_ERROR_NONE) {
218 WDPOP_LOGD( "Fail to notification_set_image. (%d)\n", noti_err);
222 /* set the title and content */
223 noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE,
224 "Disable Wi-Fi Direct after use", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
225 if (noti_err != NOTIFICATION_ERROR_NONE) {
226 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
230 noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
231 "Disable Wi-Fi Direct after use to save battery", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
232 if (noti_err != NOTIFICATION_ERROR_NONE) {
233 WDPOP_LOGD( "Fail to notification_set_text. (%d)\n", noti_err);
239 appsvc_set_pkgname(b, PACKAGE);
240 appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
242 int res = NOTIFICATION_ERROR_NONE;
243 res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
244 if (res != NOTIFICATION_ERROR_NONE) {
245 WDPOP_LOGD( "Failed to notification_set_execute_option. [%d]", res);
251 /* set display application list */
252 noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
253 if (noti_err != NOTIFICATION_ERROR_NONE) {
254 WDPOP_LOGD( "Fail to notification_set_display_applist : %d\n", noti_err);
258 /* notify the quick panel */
259 noti_err = notification_insert(ad->noti, NULL);
260 if (noti_err != NOTIFICATION_ERROR_NONE) {
261 WDPOP_LOGD( "Fail to notification_insert.(%d)\n", noti_err);
265 __WDPOP_LOG_FUNC_EXIT__;
269 * This function let the app make a callback for deactivating wfd automatically when connected
270 * @return if stop the timer, return ECORE_CALLBACK_CANCEL, else return ECORE_CALLBACK_RENEW
271 * @param[in] user_data the pointer to the main data structure
273 static Eina_Bool _wfd_automatic_deactivated_for_connection_cb(void *user_data)
276 int wfd_transfer_state = 0;
277 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
280 WDPOP_LOGD( "NULL parameters.\n");
281 return ECORE_CALLBACK_CANCEL;
284 /* check the timeout, if not timeout, keep the cb */
285 interval = time(NULL) - ad->last_wfd_transmit_time;
286 if (interval < NO_ACTION_TIME_OUT) {
287 return ECORE_CALLBACK_RENEW;
290 /* get transfer state */
291 if (vconf_get_int(VCONFKEY_WIFI_DIRECT_TRANSFER_STATE, &wfd_transfer_state) < 0) {
292 WDPOP_LOGD( "Error reading vconf (%s)\n",
293 VCONFKEY_WIFI_DIRECT_TRANSFER_STATE);
294 return ECORE_CALLBACK_CANCEL;
298 if (wfd_transfer_state > VCONFKEY_WIFI_DIRECT_TRANSFER_START) {
299 WDPOP_LOGD( "No RX/TX packet, turn off WFD automatically.\n");
300 _add_wfd_turn_off_notification(ad);
302 WDPOP_LOGD( "Has RX/TX packet, restart.\n");
303 ad->last_wfd_transmit_time = time(NULL);
304 return ECORE_CALLBACK_RENEW;
307 return ECORE_CALLBACK_CANCEL;
311 * This function let the app make a callback for registering activation event
313 * @param[in] error_code the returned error code
314 * @param[in] device_state the state of device
315 * @param[in] user_data the pointer to the main data structure
317 void _cb_activation(int error_code, wifi_direct_device_state_e device_state, void *user_data)
319 __WDPOP_LOG_FUNC_ENTER__;
320 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
322 switch (device_state) {
323 case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
324 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
327 case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
328 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
329 WDPOP_LOGD( "Termination process of wifi-direct popup begins...\n");
331 /* when deactivated, stop the timer */
332 if (ad->transmit_timer) {
333 ecore_timer_del(ad->transmit_timer);
334 ad->transmit_timer = NULL;
344 __WDPOP_LOG_FUNC_EXIT__;
348 * This function let the app find the peer by mac address
349 * @return the found peer
350 * @param[in] data the pointer to the main data structure
351 * @param[in] mac_address the pointer to mac address
353 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data, const char *mac_address)
355 __WDPOP_LOG_FUNC_ENTER__;
356 wfd_appdata_t *ad = (wfd_appdata_t *) data;
360 WDPOP_LOGD( "Incorrect parameter(NULL)\n");
364 WDPOP_LOGD( "find peer by MAC [%s] \n", mac_address);
366 for (i = 0; i < ad->discovered_peer_count; i++) {
367 WDPOP_LOGD( "check %dth peer\n", i);
369 if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18)) {
370 WDPOP_LOGD( "found peer. [%d]\n", i);
371 __WDPOP_LOG_FUNC_EXIT__;
372 return &ad->discovered_peers[i];
376 __WDPOP_LOG_FUNC_EXIT__;
381 * This function let the app make a callback for discovering peer
383 * @param[in] peer the pointer to the discovered peer
384 * @param[in] user_data the pointer to the main data structure
386 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s *peer, void *user_data)
388 __WDPOP_LOG_FUNC_ENTER__;
389 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
391 if (NULL != peer->device_name) {
392 WDPOP_LOGD( "discovered peer ssid[%s]\n", peer->device_name);
393 strncpy(ad->discovered_peers[ad->discovered_peer_count].ssid, peer->device_name, 32);
394 ad->discovered_peers[ad->discovered_peer_count].ssid[31] = '\0';
396 WDPOP_LOGD( "peer's device name is NULL\n");
399 if (NULL != peer->mac_address) {
400 WDPOP_LOGD( "discovered peer mac[%s]\n", peer->mac_address);
401 strncpy(ad->discovered_peers[ad->discovered_peer_count].mac_address, peer->mac_address, 18);
402 ad->discovered_peers[ad->discovered_peer_count].mac_address[17] = '\0';
404 WDPOP_LOGD( "peer's mac is NULL\n");
407 ad->discovered_peer_count++;
409 __WDPOP_LOG_FUNC_EXIT__;
415 * This function let the app make a callback for registering discover event
417 * @param[in] error_code the returned error code
418 * @param[in] discovery_state the state of discover
419 * @param[in] user_data the pointer to the main data structure
421 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state, void *user_data)
423 __WDPOP_LOG_FUNC_ENTER__;
424 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
427 switch (discovery_state) {
428 case WIFI_DIRECT_DISCOVERY_STARTED:
429 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
432 case WIFI_DIRECT_ONLY_LISTEN_STARTED:
433 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
436 case WIFI_DIRECT_DISCOVERY_FINISHED:
437 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
440 case WIFI_DIRECT_DISCOVERY_FOUND:
441 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
443 if (NULL != ad->discovered_peers) {
444 free(ad->discovered_peers);
445 ad->discovered_peers = NULL;
448 ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
449 ad->discovered_peer_count = 0;
451 ret = wifi_direct_foreach_discovered_peers(_wfd_app_discoverd_peer_cb, (void *) ad);
452 if (ret != WIFI_DIRECT_ERROR_NONE) {
453 WDPOP_LOGD( "get discovery result failed: %d\n", ret);
461 __WDPOP_LOG_FUNC_EXIT__;
465 * This function let the app make a callback for registering connection event
467 * @param[in] error_code the returned error code
468 * @param[in] connection_state the state of connection
469 * @param[in] mac_address the mac address of peer
470 * @param[in] user_data the pointer to the main data structure
472 void _cb_connection(int error_code, wifi_direct_connection_state_e connection_state, const char *mac_address, void *user_data)
474 __WDPOP_LOG_FUNC_ENTER__;
476 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
478 char msg[WFD_POP_STR_MAX_LEN] = {0};
479 wfd_device_info_t *peer_info = NULL;
481 /* find the peer's name by the mac address */
482 if (NULL == mac_address) {
483 WDPOP_LOGE("ERROR : mac address is NULL !!\n");
487 /* when disconnection, mac_address is empty */
488 if (connection_state <= WIFI_DIRECT_CONNECTION_RSP ||
489 connection_state == WIFI_DIRECT_INVITATION_REQ) {
490 memset(ad->peer_mac, 0, sizeof(ad->peer_mac));
491 memset(ad->peer_name, 0, sizeof(ad->peer_name));
492 strncpy(ad->peer_mac, mac_address, strlen(mac_address));
493 ad->peer_mac[17] = '\0';
494 peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
496 if (NULL == peer_info) {
497 WDPOP_LOGD( "peer_info is NULL !!\n");
498 } else if (0 == strlen(peer_info->ssid)) {
499 WDPOP_LOGD( "SSID from connection is invalid !!\n");
501 WDPOP_LOGD( "SSID from connection is %s.\n", peer_info->ssid);
502 strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
503 ad->peer_name[31] = '\0';
506 if (0 == strlen(ad->peer_name)) {
507 strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
508 ad->peer_name[31] = '\0';
512 switch (connection_state) {
513 case WIFI_DIRECT_CONNECTION_RSP:
515 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
518 if (error_code == WIFI_DIRECT_ERROR_NONE) {
519 WDPOP_LOGD( "Link Complete!\n");
521 /* add connected notification */
522 _add_wfd_peers_connected_notification(ad);
524 /* tickernoti popup */
525 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECTED, ad->peer_name);
526 notification_status_message_post(msg);
527 } else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED) {
529 "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
530 notification_status_message_post(_("IDS_WFD_POP_PIN_INVALID"));
532 if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT) {
534 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
535 } else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED) {
537 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
540 /* tickernoti popup */
541 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECT_FAILED, ad->peer_name);
542 notification_status_message_post(msg);
547 case WIFI_DIRECT_CONNECTION_WPS_REQ:
549 wifi_direct_wps_type_e wps_mode;
551 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
554 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
555 result = wifi_direct_get_wps_type(&wps_mode);
557 "wifi_direct_get_wps_type() result=[%d]\n", result);
559 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
561 "wps_config is WIFI_DIRECT_WPS_TYPE_PBC. Ignore it..\n");
562 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
564 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
566 if (wifi_direct_generate_wps_pin() != WIFI_DIRECT_ERROR_NONE) {
567 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
571 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
572 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
576 strncpy(ad->pin_number, pin, 64);
577 ad->pin_number[63] = '\0';
581 WDPOP_LOGD( "pin=[%s]\n", ad->pin_number);
583 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
584 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
585 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
586 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, (void *) NULL);
588 WDPOP_LOGD( "wps_config is unkown!\n");
594 case WIFI_DIRECT_CONNECTION_REQ:
596 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
598 wifi_direct_wps_type_e wps_mode;
599 bool auto_connection_mode;
601 result = wifi_direct_get_wps_type(&wps_mode);
602 WDPOP_LOGD( "wifi_direct_get_wps_type() result=[%d]\n", result);
604 result = wifi_direct_is_autoconnection_mode(&auto_connection_mode);
605 WDPOP_LOGD( "wifi_direct_is_autoconnection_mode() result=[%d]\n", result);
607 if (auto_connection_mode == TRUE) {
608 result = wifi_direct_accept_connection(ad->peer_mac);
609 WDPOP_LOGD("wifi_direct_accept_connection() result=[%d]\n", result);
611 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
612 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PBC\n");
613 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
614 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
615 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
616 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
617 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
618 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
619 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
621 WDPOP_LOGD( "wps_config is unkown!\n");
627 case WIFI_DIRECT_DISCONNECTION_IND:
629 _del_wfd_notification();
630 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
632 result = wifi_direct_set_autoconnection_mode(false);
633 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
635 /* tickernoti popup */
636 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
637 notification_status_message_post(msg);
641 case WIFI_DIRECT_DISCONNECTION_RSP:
643 _del_wfd_notification();
646 result = wifi_direct_set_autoconnection_mode(false);
647 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
649 /* tickernoti popup */
650 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
651 notification_status_message_post(msg);
654 case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
656 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
657 /* tickernoti popup */
658 notification_status_message_post(_("IDS_WFD_POP_CONNECTING"));
661 case WIFI_DIRECT_INVITATION_REQ:
663 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_INVITATION_REQ\n");
664 bool auto_connection_mode = FALSE;
666 wifi_direct_is_autoconnection_mode(&auto_connection_mode);
667 if (auto_connection_mode == TRUE) {
668 result = wifi_direct_connect(ad->peer_mac);
669 WDPOP_LOGD("wifi_direct_accept_connection() result=[%d]\n", result);
671 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_INVITATION_REQ, NULL);
680 /* if connected, start the transmit timer */
681 wifi_direct_get_state(&ad->wfd_status);
682 WDPOP_LOGD( "status: %d", ad->wfd_status);
684 if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
685 if (ad->transmit_timer) {
686 ecore_timer_del(ad->transmit_timer);
687 ad->transmit_timer = NULL;
690 if (NULL == ad->transmit_timer) {
691 WDPOP_LOGD( "start the transmit timer\n");
692 ad->last_wfd_transmit_time = time(NULL);
693 ad->transmit_timer = ecore_timer_add(5.0,
694 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_connection_cb, ad);
698 __WDPOP_LOG_FUNC_EXIT__;
702 * This function let the app make a change callback for flight mode
704 * @param[in] key the pointer to the key
705 * @param[in] user_data the pointer to the main data structure
707 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
709 __WDPOP_LOG_FUNC_ENTER__;
712 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
715 WDPOP_LOGE("NULL parameters.\n");
719 res = vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
721 WDPOP_LOGE("Failed to get flight state from vconf. [%d]\n", res);
725 if (flight_mode == FALSE) {
726 WDPOP_LOGD( "Flight mode is off\n");
730 /* If flight mode is on, turn off WFD */
731 wifi_direct_get_state(&ad->wfd_status);
732 if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
733 WDPOP_LOGD( "Wi-Fi Direct is deactivated.\n");
737 /*if connected, disconnect all devices*/
738 if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
739 res = wifi_direct_disconnect_all();
740 if (res != WIFI_DIRECT_ERROR_NONE) {
741 WDPOP_LOGE("Failed to send disconnection request to all. [%d]\n", res);
746 res = wifi_direct_deactivate();
747 if (res != WIFI_DIRECT_ERROR_NONE) {
748 WDPOP_LOGE("Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
752 __WDPOP_LOG_FUNC_EXIT__;
756 * This function let the app do initialization
757 * @return If success, return TRUE, else return FALSE
758 * @param[in] ad the pointer to the main data structure
760 int init_wfd_popup_client(wfd_appdata_t *ad)
762 __WDPOP_LOG_FUNC_ENTER__;
765 WDPOP_LOGD( "NULL parameters.\n");
771 ret = wifi_direct_initialize();
772 if (ret != WIFI_DIRECT_ERROR_NONE) {
773 WDPOP_LOGE("Failed to initialize Wi-Fi Direct. error code = [%d]\n", ret);
777 ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *)ad);
778 if (ret != WIFI_DIRECT_ERROR_NONE) {
779 WDPOP_LOGE("Failed to register _cb_activation. error code = [%d]\n", ret);
783 ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *)ad);
784 if (ret != WIFI_DIRECT_ERROR_NONE) {
785 WDPOP_LOGE("Failed to register _cb_discover. error code = [%d]\n", ret);
789 ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void *)ad);
790 if (ret != WIFI_DIRECT_ERROR_NONE) {
791 WDPOP_LOGE("Failed to register _cb_connection. error code = [%d]\n", ret);
795 /* initialize notification */
796 ad->noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
797 if (NULL == ad->noti) {
798 WDPOP_LOGD( "notification_new failed.\n");
802 /* register flight mode */
804 result = vconf_notify_key_changed(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, _wfd_flight_mode_changed, ad);
806 WDPOP_LOGE("Failed to register vconf callback for flight mode\n");
810 __WDPOP_LOG_FUNC_EXIT__;
815 * This function let the app do de-initialization
816 * @return If success, return TRUE, else return FALSE
817 * @param[in] ad the pointer to the main data structure
819 int deinit_wfd_popup_client(wfd_appdata_t *ad)
821 __WDPOP_LOG_FUNC_ENTER__;
823 if (NULL == ad || NULL == ad->noti) {
824 WDPOP_LOGD( "NULL parameters.\n");
830 ret = wifi_direct_deinitialize();
832 _del_wfd_notification(ad);
833 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
834 noti_err = notification_free(ad->noti);
835 if (noti_err != NOTIFICATION_ERROR_NONE) {
836 WDPOP_LOGD( "Fail to notification_free.(%d)\n", noti_err);
837 ret = WIFI_DIRECT_ERROR_RESOURCE_BUSY;
840 /* remove callback for flight mode */
842 result = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_flight_mode_changed);
844 WDPOP_LOGE("Failed to ignore vconf key callback for flight mode\n");
847 if (ad->transmit_timer) {
848 ecore_timer_del(ad->transmit_timer);
849 ad->transmit_timer = NULL;
852 __WDPOP_LOG_FUNC_EXIT__;
853 if (ret == WIFI_DIRECT_ERROR_NONE) {