4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://www.tizenopensource.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 "wfd-app-util.h"
33 #include "wfd-app-strings.h"
37 * This function let the app make a callback for connected peer
39 * @param[in] peer the pointer to the connected peer
40 * @param[in] user_data the pointer to the main data structure
42 bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s *peer, void *user_data)
44 __WDPOP_LOG_FUNC_ENTER__;
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");
52 int peer_cnt = ad->raw_connected_peer_cnt;
53 WDPOP_LOGD( "%dth connected peer. [%s]\n", peer_cnt, peer->device_name);
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++;
60 free(peer->device_name);
61 free(peer->mac_address);
64 __WDPOP_LOG_FUNC_EXIT__;
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
73 int _wfd_app_get_connected_peers(void *user_data)
75 __WDPOP_LOG_FUNC_ENTER__;
77 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
79 WDPOP_LOGD( "NULL parameters.\n");
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);
92 __WDPOP_LOG_FUNC_EXIT__;
97 * This function let the app delete the notification
100 void _del_wfd_notification()
102 __WDPOP_LOG_FUNC_ENTER__;
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);
112 __WDPOP_LOG_FUNC_EXIT__;
116 * This function let the app add the notification when it is connected
118 * @param[in] user_data the pointer to the main data structure
120 void _add_wfd_peers_connected_notification(void *user_data)
122 __WDPOP_LOG_FUNC_ENTER__;
124 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
125 if (NULL == ad || NULL == ad->noti) {
126 WDPOP_LOGD( "NULL parameters.\n");
130 char msg[WFD_MAX_SIZE] = {0};
131 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
133 /* delete all notifications */
134 _del_wfd_notification();
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);
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);
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);
161 appsvc_set_pkgname(b, PACKAGE);
162 appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
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);
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);
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);
187 __WDPOP_LOG_FUNC_EXIT__;
191 * This function let the app add the notification when it shoule be turned off
193 * @param[in] user_data the pointer to the main data structure
195 void _add_wfd_turn_off_notification(void *user_data)
197 __WDPOP_LOG_FUNC_ENTER__;
199 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
200 if (NULL == ad || NULL == ad->noti) {
201 WDPOP_LOGD( "NULL parameters.\n");
205 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
207 /* delete all notifications */
208 _del_wfd_notification();
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);
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);
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);
234 appsvc_set_pkgname(b, PACKAGE);
235 appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
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);
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);
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);
260 __WDPOP_LOG_FUNC_EXIT__;
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
268 static Eina_Bool _wfd_automatic_deactivated_for_connection_cb(void *user_data)
271 int wfd_transfer_state = 0;
272 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
275 WDPOP_LOGD( "NULL parameters.\n");
276 return ECORE_CALLBACK_CANCEL;
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;
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;
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);
297 WDPOP_LOGD( "Has RX/TX packet, restart.\n");
298 ad->last_wfd_transmit_time = time(NULL);
299 return ECORE_CALLBACK_RENEW;
302 return ECORE_CALLBACK_CANCEL;
306 * This function let the app make a callback for registering activation event
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
312 void _cb_activation(int error_code, wifi_direct_device_state_e device_state, void *user_data)
314 __WDPOP_LOG_FUNC_ENTER__;
315 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
317 switch (device_state) {
318 case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
319 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
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");
326 /* when deactivated, stop the timer */
327 if (ad->transmit_timer) {
328 ecore_timer_del(ad->transmit_timer);
329 ad->transmit_timer = NULL;
339 __WDPOP_LOG_FUNC_EXIT__;
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
348 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data, const char *mac_address)
350 __WDPOP_LOG_FUNC_ENTER__;
351 wfd_appdata_t *ad = (wfd_appdata_t *) data;
355 WDPOP_LOGD( "Incorrect parameter(NULL)\n");
359 WDPOP_LOGD( "find peer by MAC [%s] \n", mac_address);
361 for (i = 0; i < ad->discovered_peer_count; i++) {
362 WDPOP_LOGD( "check %dth peer\n", i);
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];
371 __WDPOP_LOG_FUNC_EXIT__;
376 * This function let the app make a callback for discovering peer
378 * @param[in] peer the pointer to the discovered peer
379 * @param[in] user_data the pointer to the main data structure
381 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s *peer, void *user_data)
383 __WDPOP_LOG_FUNC_ENTER__;
384 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
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);
390 WDPOP_LOGD( "peer's device name is NULL\n");
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);
397 WDPOP_LOGD( "peer's mac is NULL\n");
400 ad->discovered_peer_count++;
402 __WDPOP_LOG_FUNC_EXIT__;
408 * This function let the app make a callback for registering discover event
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
414 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state, void *user_data)
416 __WDPOP_LOG_FUNC_ENTER__;
417 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
420 switch (discovery_state) {
421 case WIFI_DIRECT_DISCOVERY_STARTED:
422 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
425 case WIFI_DIRECT_ONLY_LISTEN_STARTED:
426 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
429 case WIFI_DIRECT_DISCOVERY_FINISHED:
430 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
433 case WIFI_DIRECT_DISCOVERY_FOUND:
434 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
436 if (NULL != ad->discovered_peers) {
437 free(ad->discovered_peers);
438 ad->discovered_peers = NULL;
441 ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
442 ad->discovered_peer_count = 0;
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);
454 __WDPOP_LOG_FUNC_EXIT__;
458 * This function let the app make a callback for registering connection event
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
465 void _cb_connection(int error_code, wifi_direct_connection_state_e connection_state, const char *mac_address, void *user_data)
467 __WDPOP_LOG_FUNC_ENTER__;
469 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
471 char msg[WFD_POP_STR_MAX_LEN] = {0};
472 wfd_device_info_t *peer_info = NULL;
474 /* find the peer's name by the mac address */
475 if (NULL == mac_address) {
476 WDPOP_LOGE("ERROR : mac address is NULL !!\n");
480 /* when disconnection, mac_address is empty */
481 if (connection_state <= WIFI_DIRECT_CONNECTION_RSP ||
482 connection_state == WIFI_DIRECT_INVITATION_REQ) {
483 memset(ad->peer_mac, 0, sizeof(ad->peer_mac));
484 memset(ad->peer_name, 0, sizeof(ad->peer_name));
485 strncpy(ad->peer_mac, mac_address, strlen(mac_address));
486 peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
488 if (NULL == peer_info) {
489 WDPOP_LOGD( "peer_info is NULL !!\n");
490 } else if (0 == strlen(peer_info->ssid)) {
491 WDPOP_LOGD( "SSID from connection is invalid !!\n");
493 WDPOP_LOGD( "SSID from connection is %s.\n", peer_info->ssid);
494 strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
497 if (0 == strlen(ad->peer_name)) {
498 strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
502 switch (connection_state) {
503 case WIFI_DIRECT_CONNECTION_RSP:
505 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
508 if (error_code == WIFI_DIRECT_ERROR_NONE) {
509 WDPOP_LOGD( "Link Complete!\n");
511 /* add connected notification */
512 _add_wfd_peers_connected_notification(ad);
514 /* tickernoti popup */
515 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECTED, ad->peer_name);
516 wfd_tickernoti_popup(msg);
517 } else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED) {
519 "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
520 wfd_tickernoti_popup(_("IDS_WFD_POP_PIN_INVALID"));
522 if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT) {
524 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
525 } else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED) {
527 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
530 /* tickernoti popup */
531 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECT_FAILED, ad->peer_name);
532 wfd_tickernoti_popup(msg);
537 case WIFI_DIRECT_CONNECTION_WPS_REQ:
539 wifi_direct_wps_type_e wps_mode;
541 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
544 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
545 result = wifi_direct_get_wps_type(&wps_mode);
547 "wifi_direct_get_wps_type() result=[%d]\n", result);
549 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
551 "wps_config is WIFI_DIRECT_WPS_TYPE_PBC. Ignore it..\n");
552 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
554 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
556 if (wifi_direct_generate_wps_pin() != WIFI_DIRECT_ERROR_NONE) {
557 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
561 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
562 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
566 strncpy(ad->pin_number, pin, 64);
570 WDPOP_LOGD( "pin=[%s]\n", ad->pin_number);
572 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
573 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
574 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
575 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, (void *) NULL);
577 WDPOP_LOGD( "wps_config is unkown!\n");
583 case WIFI_DIRECT_CONNECTION_REQ:
585 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
587 wifi_direct_wps_type_e wps_mode;
588 bool auto_connection_mode;
590 result = wifi_direct_get_wps_type(&wps_mode);
591 WDPOP_LOGD( "wifi_direct_get_wps_type() result=[%d]\n", result);
593 result = wifi_direct_is_autoconnection_mode(&auto_connection_mode);
594 WDPOP_LOGD( "wifi_direct_is_autoconnection_mode() result=[%d]\n", result);
596 if (auto_connection_mode == TRUE) {
597 result = wifi_direct_accept_connection(ad->peer_mac);
598 printf("wifi_direct_accept_connection() result=[%d]\n", result);
600 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
601 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PBC\n");
602 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
603 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
604 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
605 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
606 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
607 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
608 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
610 WDPOP_LOGD( "wps_config is unkown!\n");
616 case WIFI_DIRECT_DISCONNECTION_IND:
618 _del_wfd_notification();
619 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
621 result = wifi_direct_set_autoconnection_mode(false);
622 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
624 /* tickernoti popup */
625 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
626 wfd_tickernoti_popup(msg);
630 case WIFI_DIRECT_DISCONNECTION_RSP:
632 _del_wfd_notification();
635 result = wifi_direct_set_autoconnection_mode(false);
636 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
638 /* tickernoti popup */
639 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
640 wfd_tickernoti_popup(msg);
643 case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
645 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
646 /* tickernoti popup */
647 wfd_tickernoti_popup(_("IDS_WFD_POP_CONNECTING"));
650 case WIFI_DIRECT_INVITATION_REQ:
652 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_INVITATION_REQ\n");
653 bool auto_connection_mode = FALSE;
655 wifi_direct_is_autoconnection_mode(&auto_connection_mode);
656 if (auto_connection_mode == TRUE) {
657 result = wifi_direct_connect(ad->peer_mac);
658 printf("wifi_direct_accept_connection() result=[%d]\n", result);
660 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_INVITATION_REQ, NULL);
669 /* if connected, start the transmit timer */
670 wifi_direct_get_state(&ad->wfd_status);
671 WDPOP_LOGD( "status: %d", ad->wfd_status);
673 if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
674 if (ad->transmit_timer) {
675 ecore_timer_del(ad->transmit_timer);
676 ad->transmit_timer = NULL;
679 if (NULL == ad->transmit_timer) {
680 WDPOP_LOGD( "start the transmit timer\n");
681 ad->last_wfd_transmit_time = time(NULL);
682 ad->transmit_timer = ecore_timer_add(5.0,
683 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_connection_cb, ad);
687 __WDPOP_LOG_FUNC_EXIT__;
691 * This function let the app make a change callback for flight mode
693 * @param[in] key the pointer to the key
694 * @param[in] user_data the pointer to the main data structure
696 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
698 __WDPOP_LOG_FUNC_ENTER__;
701 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
704 WDPOP_LOGE("NULL parameters.\n");
708 res = vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
710 WDPOP_LOGE("Failed to get flight state from vconf. [%d]\n", res);
714 if (flight_mode == FALSE) {
715 WDPOP_LOGD( "Flight mode is off\n");
719 /* If flight mode is on, turn off WFD */
720 wifi_direct_get_state(&ad->wfd_status);
721 if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
722 WDPOP_LOGD( "Wi-Fi Direct is deactivated.\n");
726 /*if connected, disconnect all devices*/
727 if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
728 res = wifi_direct_disconnect_all();
729 if (res != WIFI_DIRECT_ERROR_NONE) {
730 WDPOP_LOGE("Failed to send disconnection request to all. [%d]\n", res);
735 res = wifi_direct_deactivate();
736 if (res != WIFI_DIRECT_ERROR_NONE) {
737 WDPOP_LOGE("Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
741 __WDPOP_LOG_FUNC_EXIT__;
745 * This function let the app do initialization
746 * @return If success, return TRUE, else return FALSE
747 * @param[in] ad the pointer to the main data structure
749 int init_wfd_popup_client(wfd_appdata_t *ad)
751 __WDPOP_LOG_FUNC_ENTER__;
754 WDPOP_LOGD( "NULL parameters.\n");
760 ret = wifi_direct_initialize();
761 if (ret != WIFI_DIRECT_ERROR_NONE) {
762 WDPOP_LOGE("Failed to initialize Wi-Fi Direct. error code = [%d]\n", ret);
766 ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *)ad);
767 if (ret != WIFI_DIRECT_ERROR_NONE) {
768 WDPOP_LOGE("Failed to register _cb_activation. error code = [%d]\n", ret);
772 ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *)ad);
773 if (ret != WIFI_DIRECT_ERROR_NONE) {
774 WDPOP_LOGE("Failed to register _cb_discover. error code = [%d]\n", ret);
778 ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void *)ad);
779 if (ret != WIFI_DIRECT_ERROR_NONE) {
780 WDPOP_LOGE("Failed to register _cb_connection. error code = [%d]\n", ret);
784 /* initialize notification */
785 ad->noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
786 if (NULL == ad->noti) {
787 WDPOP_LOGD( "notification_new failed.\n");
791 /* register flight mode */
793 result = vconf_notify_key_changed(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, _wfd_flight_mode_changed, ad);
795 WDPOP_LOGE("Failed to register vconf callback for flight mode\n");
799 __WDPOP_LOG_FUNC_EXIT__;
801 if (ret == WIFI_DIRECT_ERROR_NONE) {
809 * This function let the app do de-initialization
810 * @return If success, return TRUE, else return FALSE
811 * @param[in] ad the pointer to the main data structure
813 int deinit_wfd_popup_client(wfd_appdata_t *ad)
815 __WDPOP_LOG_FUNC_ENTER__;
817 if (NULL == ad || NULL == ad->noti) {
818 WDPOP_LOGD( "NULL parameters.\n");
824 ret = wifi_direct_deinitialize();
826 _del_wfd_notification(ad);
827 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
828 noti_err = notification_free(ad->noti);
829 if (noti_err != NOTIFICATION_ERROR_NONE) {
830 WDPOP_LOGD( "Fail to notification_free.(%d)\n", noti_err);
831 ret = WIFI_DIRECT_ERROR_RESOURCE_BUSY;
834 /* remove callback for flight mode */
836 result = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_flight_mode_changed);
838 WDPOP_LOGE("Failed to ignore vconf key callback for flight mode\n");
841 if (ad->transmit_timer) {
842 ecore_timer_del(ad->transmit_timer);
843 ad->transmit_timer = NULL;
846 __WDPOP_LOG_FUNC_EXIT__;
848 if (ret == WIFI_DIRECT_ERROR_NONE) {