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 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);
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");
492 WDPOP_LOGD( "SSID from connection is %s.\n", peer_info->ssid);
493 strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
496 if (0 == strlen(ad->peer_name)) {
497 strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
501 switch (connection_state) {
502 case WIFI_DIRECT_CONNECTION_RSP:
504 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
507 if (error_code == WIFI_DIRECT_ERROR_NONE) {
508 WDPOP_LOGD( "Link Complete!\n");
510 /* add connected notification */
511 _add_wfd_peers_connected_notification(ad);
513 /* tickernoti popup */
514 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECTED, ad->peer_name);
515 wfd_tickernoti_popup(msg);
517 if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT) {
519 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
520 } else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED) {
522 "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
523 } else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED) {
525 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
528 /* tickernoti popup */
529 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_CONNECT_FAILED, ad->peer_name);
530 wfd_tickernoti_popup(msg);
535 case WIFI_DIRECT_CONNECTION_WPS_REQ:
537 wifi_direct_wps_type_e wps_mode;
539 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
542 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
543 result = wifi_direct_get_wps_type(&wps_mode);
545 "wifi_direct_get_wps_type() result=[%d]\n", result);
547 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
549 "wps_config is WIFI_DIRECT_WPS_TYPE_PBC. Ignore it..\n");
550 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
552 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
554 if (wifi_direct_generate_wps_pin() != WIFI_DIRECT_ERROR_NONE) {
555 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
559 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
560 WDPOP_LOGD( "wifi_direct_generate_wps_pin() is failed\n");
564 strncpy(ad->pin_number, pin, 64);
568 WDPOP_LOGD( "pin=[%s]\n", ad->pin_number);
570 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
571 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
572 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
573 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, (void *) NULL);
575 WDPOP_LOGD( "wps_config is unkown!\n");
581 case WIFI_DIRECT_CONNECTION_REQ:
583 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
585 wifi_direct_wps_type_e wps_mode;
586 bool auto_connection_mode;
588 result = wifi_direct_get_wps_type(&wps_mode);
589 WDPOP_LOGD( "wifi_direct_get_wps_type() result=[%d]\n", result);
591 result = wifi_direct_is_autoconnection_mode(&auto_connection_mode);
592 WDPOP_LOGD( "wifi_direct_is_autoconnection_mode() result=[%d]\n", result);
594 if (auto_connection_mode == TRUE) {
595 result = wifi_direct_accept_connection(ad->peer_mac);
596 printf("wifi_direct_accept_connection() result=[%d]\n", result);
598 if (wps_mode == WIFI_DIRECT_WPS_TYPE_PBC) {
599 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PBC\n");
600 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
601 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
602 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
603 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
604 } else if (wps_mode == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
605 WDPOP_LOGD( "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
606 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
608 WDPOP_LOGD( "wps_config is unkown!\n");
614 case WIFI_DIRECT_DISCONNECTION_IND:
616 _del_wfd_notification();
617 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
619 result = wifi_direct_set_autoconnection_mode(false);
620 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
622 /* tickernoti popup */
623 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
624 wfd_tickernoti_popup(msg);
628 case WIFI_DIRECT_DISCONNECTION_RSP:
630 _del_wfd_notification();
633 result = wifi_direct_set_autoconnection_mode(false);
634 WDPOP_LOGD( "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
636 /* tickernoti popup */
637 snprintf(msg, WFD_POP_STR_MAX_LEN, IDS_WFD_POP_DISCONNECTED, ad->peer_name);
638 wfd_tickernoti_popup(msg);
641 case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
643 WDPOP_LOGD( "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
644 /* tickernoti popup */
645 wfd_tickernoti_popup(_("IDS_WFD_POP_CONNECTING"));
652 /* if connected, start the transmit timer */
653 wifi_direct_get_state(&ad->wfd_status);
654 WDPOP_LOGD( "status: %d", ad->wfd_status);
656 if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
657 if (ad->transmit_timer) {
658 ecore_timer_del(ad->transmit_timer);
659 ad->transmit_timer = NULL;
662 if (NULL == ad->transmit_timer) {
663 WDPOP_LOGD( "start the transmit timer\n");
664 ad->last_wfd_transmit_time = time(NULL);
665 ad->transmit_timer = ecore_timer_add(5.0,
666 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_connection_cb, ad);
670 __WDPOP_LOG_FUNC_EXIT__;
674 * This function let the app make a change callback for flight mode
676 * @param[in] key the pointer to the key
677 * @param[in] user_data the pointer to the main data structure
679 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
681 __WDPOP_LOG_FUNC_ENTER__;
684 wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
687 WDPOP_LOGE("NULL parameters.\n");
691 res = vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
693 WDPOP_LOGE("Failed to get flight state from vconf. [%d]\n", res);
697 if (flight_mode == FALSE) {
698 WDPOP_LOGD( "Flight mode is off\n");
702 /* If flight mode is on, turn off WFD */
703 wifi_direct_get_state(&ad->wfd_status);
704 if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
705 WDPOP_LOGD( "Wi-Fi Direct is deactivated.\n");
709 /*if connected, disconnect all devices*/
710 if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
711 res = wifi_direct_disconnect_all();
712 if (res != WIFI_DIRECT_ERROR_NONE) {
713 WDPOP_LOGE("Failed to send disconnection request to all. [%d]\n", res);
718 res = wifi_direct_deactivate();
719 if (res != WIFI_DIRECT_ERROR_NONE) {
720 WDPOP_LOGE("Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
724 __WDPOP_LOG_FUNC_EXIT__;
728 * This function let the app do initialization
729 * @return If success, return TRUE, else return FALSE
730 * @param[in] ad the pointer to the main data structure
732 int init_wfd_popup_client(wfd_appdata_t *ad)
734 __WDPOP_LOG_FUNC_ENTER__;
737 WDPOP_LOGD( "NULL parameters.\n");
743 ret = wifi_direct_initialize();
744 if (ret != WIFI_DIRECT_ERROR_NONE) {
745 WDPOP_LOGE("Failed to initialize Wi-Fi Direct. error code = [%d]\n", ret);
749 ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *)ad);
750 if (ret != WIFI_DIRECT_ERROR_NONE) {
751 WDPOP_LOGE("Failed to register _cb_activation. error code = [%d]\n", ret);
755 ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *)ad);
756 if (ret != WIFI_DIRECT_ERROR_NONE) {
757 WDPOP_LOGE("Failed to register _cb_discover. error code = [%d]\n", ret);
761 ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void *)ad);
762 if (ret != WIFI_DIRECT_ERROR_NONE) {
763 WDPOP_LOGE("Failed to register _cb_connection. error code = [%d]\n", ret);
767 /* initialize notification */
768 ad->noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
769 if (NULL == ad->noti) {
770 WDPOP_LOGD( "notification_new failed.\n");
774 /* register flight mode */
776 result = vconf_notify_key_changed(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, _wfd_flight_mode_changed, ad);
778 WDPOP_LOGE("Failed to register vconf callback for flight mode\n");
782 __WDPOP_LOG_FUNC_EXIT__;
784 if (ret == WIFI_DIRECT_ERROR_NONE) {
792 * This function let the app do de-initialization
793 * @return If success, return TRUE, else return FALSE
794 * @param[in] ad the pointer to the main data structure
796 int deinit_wfd_popup_client(wfd_appdata_t *ad)
798 __WDPOP_LOG_FUNC_ENTER__;
800 if (NULL == ad || NULL == ad->noti) {
801 WDPOP_LOGD( "NULL parameters.\n");
807 ret = wifi_direct_deinitialize();
809 _del_wfd_notification(ad);
810 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
811 noti_err = notification_free(ad->noti);
812 if (noti_err != NOTIFICATION_ERROR_NONE) {
813 WDPOP_LOGD( "Fail to notification_free.(%d)\n", noti_err);
814 ret = WIFI_DIRECT_ERROR_RESOURCE_BUSY;
817 /* remove callback for flight mode */
819 result = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_flight_mode_changed);
821 WDPOP_LOGE("Failed to ignore vconf key callback for flight mode\n");
824 if (ad->transmit_timer) {
825 ecore_timer_del(ad->transmit_timer);
826 ad->transmit_timer = NULL;
829 __WDPOP_LOG_FUNC_EXIT__;
831 if (ret == WIFI_DIRECT_ERROR_NONE) {