Tizen directory path migration
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-client.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://www.tizenopensource.org/license
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21  * This file implements wifi direct application client  functions.
22  *
23  * @file    wfd-app-client.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <Elementary.h>
32 #include <app_control.h>
33 #include <vconf.h>
34 #include <notification.h>
35
36 #include <tethering.h>
37 #include <network-cm-intf.h>
38 #include <network-wifi-intf.h>
39
40 #include <dd-display.h>
41
42 #include <dbus/dbus.h>
43
44 #include "wfd-app.h"
45 #include "wfd-app-util.h"
46 #include "wfd-app-strings.h"
47 #include "wfd-app-popup-view.h"
48
49 /**
50  *      This function let the app make a callback for connected peer
51  *      @return   TRUE
52  *      @param[in] peer the pointer to the connected peer
53  *      @param[in] user_data the pointer to the main data structure
54  */
55 bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s *peer, void *user_data)
56 {
57         __WFD_APP_FUNC_ENTER__;
58
59         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
60
61         if (NULL == ad || NULL == peer || NULL == peer->device_name || NULL == peer->mac_address) {
62                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "NULL parameters.\n");
63                 return FALSE;
64         }
65
66         int peer_cnt = ad->raw_connected_peer_cnt;
67         strncpy(ad->raw_connected_peers[peer_cnt].ssid, peer->device_name, sizeof(ad->raw_connected_peers[peer_cnt].ssid) - 1);
68         strncpy(ad->raw_connected_peers[peer_cnt].mac_address, peer->mac_address, WFD_MAC_ADDRESS_SIZE - 1);
69
70         ad->raw_connected_peer_cnt++;
71
72         free(peer->device_name);
73         free(peer->mac_address);
74         free(peer);
75
76         __WFD_APP_FUNC_EXIT__;
77         return TRUE;
78 }
79
80 /**
81  *      This function let the app get the connected peers
82  *      @return   If success, return 0, else return -1
83  *      @param[in] ugd the pointer to the main data structure
84  */
85 int wfd_app_get_connected_peers(void *user_data)
86 {
87         __WFD_APP_FUNC_ENTER__;
88
89         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
90         if (NULL == ad) {
91                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "NULL parameters.\n");
92                 return -1;
93         }
94
95         int res = 0;
96
97         ad->raw_connected_peer_cnt = 0;
98         res = wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb, (void *)ad);
99         if (res != WIFI_DIRECT_ERROR_NONE) {
100                 ad->raw_connected_peer_cnt = 0;
101                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Get connected peer failed: %d\n", res);
102         }
103
104         __WFD_APP_FUNC_EXIT__;
105         return 0;
106 }
107
108 /**
109  *      This function let the app make a callback for deactivating wfd automatically when connected
110  *      @return   if stop the timer, return ECORE_CALLBACK_CANCEL, else return ECORE_CALLBACK_RENEW
111  *      @param[in] user_data the pointer to the main data structure
112  */
113 Eina_Bool wfd_automatic_deactivated_for_connection_cb(void *user_data)
114 {
115         __WFD_APP_FUNC_ENTER__;
116         int interval = 0;
117         int wfd_transfer_state = 0;
118         int res = 0;
119         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
120
121         if (NULL == ad) {
122                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "NULL parameters.\n");
123                 return ECORE_CALLBACK_CANCEL;
124         }
125
126         /* check the timeout, if not timeout, keep the cb */
127         interval = time(NULL) - ad->last_wfd_transmit_time;
128         if (interval < NO_ACTION_TIME_OUT) {
129                 return ECORE_CALLBACK_RENEW;
130         }
131
132         /* get transfer state */
133         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_TRANSFER_STATE, &wfd_transfer_state) < 0) {
134                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Error reading vconf (%s)\n",
135                         VCONFKEY_WIFI_DIRECT_TRANSFER_STATE);
136                 return ECORE_CALLBACK_CANCEL;
137         }
138
139         res = wifi_direct_get_state(&ad->wfd_status);
140         if (res != WIFI_DIRECT_ERROR_NONE) {
141                 return ECORE_CALLBACK_CANCEL;
142         }
143
144         if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
145                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Wi-Fi Direct is unconnected!");
146                 return ECORE_CALLBACK_CANCEL;
147         }
148
149         /* show tickernoti*/
150         if (wfd_transfer_state > VCONFKEY_WIFI_DIRECT_TRANSFER_START) {
151                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Display Toast popup.\n");
152                 notification_status_message_post(D_("IDS_WIFI_BODY_TO_SAVE_BATTERY_POWER_DISABLE_WI_FI_DIRECT_AFTER_USE"));
153                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No RX/TX packet, turn off WFD automatically.\n");
154                 wfd_app_util_add_wfd_turn_off_notification(ad);
155         } else {
156                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Has RX/TX packet, restart.\n");
157                 ad->last_wfd_transmit_time = time(NULL);
158                 return ECORE_CALLBACK_RENEW;
159         }
160
161         ad->transmit_timer = NULL;
162         __WFD_APP_FUNC_EXIT__;
163         return ECORE_CALLBACK_CANCEL;
164 }
165
166 int wfd_app_client_switch_off(void *data)
167 {
168         wfd_appdata_t *ad = (wfd_appdata_t *)data;
169         int res;
170         if(NULL == ad) {
171                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "NULL == ad!\n");
172                 return -1;
173         }
174         res = wifi_direct_get_state(&ad->wfd_status);
175         if (res != WIFI_DIRECT_ERROR_NONE) {
176                 return ECORE_CALLBACK_CANCEL;
177         }
178
179         if (ad->wfd_status >= WIFI_DIRECT_STATE_ACTIVATING) {
180                 /*if connected, disconnect all devices*/
181                 if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
182                         res = wifi_direct_disconnect_all();
183                         if (res != WIFI_DIRECT_ERROR_NONE) {
184                                 return -1;
185                         }
186                 }
187                 res = wifi_direct_deactivate();
188                 if (res != WIFI_DIRECT_ERROR_NONE) {
189                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
190                         return -1;
191                 }
192         }
193
194         return 0;
195 }
196
197 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
198 /**
199  *      This function let the ug make a callback for deactivating wfd automatically
200  *      @return   if stop the timer, return ECORE_CALLBACK_CANCEL, else return ECORE_CALLBACK_RENEW
201  *      @param[in] user_data the pointer to the main data structure
202  */
203 static Eina_Bool _wfd_automatic_deactivated_for_no_connection_cb(void *user_data)
204 {
205         int res = -1;
206         int interval = 0;
207         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
208 #ifdef WFD_SCREEN_MIRRORING_ENABLED
209         int screen_mirroring_status = 0;
210 #endif
211
212         if (NULL == ad) {
213                 return ECORE_CALLBACK_CANCEL;
214         }
215
216         /* check the action, if action is exist, keep the cb */
217         res = wifi_direct_get_state(&ad->wfd_status);
218         if (res != WIFI_DIRECT_ERROR_NONE) {
219                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Get wifi-direct status WIFI_DIRECT_ERROR_NONE!\n");
220                 return ECORE_CALLBACK_CANCEL;
221         }
222
223         if (ad->last_wfd_status != ad->wfd_status) {
224                 ad->last_wfd_status = ad->wfd_status;
225                 ad->last_wfd_time = time(NULL);
226                 return ECORE_CALLBACK_RENEW;
227         }
228
229         /* check the timeout, if not timeout, keep the cb */
230         interval = time(NULL) - ad->last_wfd_time;
231         if (interval < MAX_NO_ACTION_TIME_OUT) {
232                 return ECORE_CALLBACK_RENEW;
233         }
234
235         /* turn off the Wi-Fi Direct */
236         wifi_direct_get_state(&ad->wfd_status);
237         if (ad->wfd_status < WIFI_DIRECT_STATE_ACTIVATING) {
238                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wfd_status < WIFI_DIRECT_STATE_ACTIVATING!\n");
239         } else {
240 #ifdef WFD_SCREEN_MIRRORING_ENABLED
241                 if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
242                 {
243                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
244                 }
245
246                 if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_ACTIVATED) {
247                         ad->last_wfd_time = time(NULL);
248                         return ECORE_CALLBACK_RENEW;
249                 } else {
250                         /* turn off the Wi-Fi Direct */
251                         wfd_app_client_switch_off(ad);
252                         ad->popup = wfd_draw_pop_type_auto_deactivation(ad->win, user_data);
253                 }
254 #endif
255         }
256
257         /* reset monitor timer */
258         if (ad->monitor_timer) {
259                 ad->monitor_timer = NULL;       //ECORE_CALLBACK_CANCEL will release timer.
260         }
261
262         return ECORE_CALLBACK_CANCEL;
263 }
264 #endif
265
266 /**
267  *      This function let the app make a callback for registering activation event
268  *      @return   void
269  *      @param[in] error_code the returned error code
270  *      @param[in] device_state the state of device
271  *      @param[in] user_data the pointer to the main data structure
272  */
273 void _cb_activation(int error_code, wifi_direct_device_state_e device_state, void *user_data)
274 {
275         __WFD_APP_FUNC_ENTER__;
276         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
277
278         switch (device_state) {
279         case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
280         {
281                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "---------------WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
282 #ifdef NOT_CONNECTED_INDICATOR_ICON
283                 wfd_app_util_add_indicator_icon(ad);
284 #endif
285 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
286                 if (NULL == ad->monitor_timer) {
287                         ad->last_wfd_time = time(NULL);
288                         ad->monitor_timer = ecore_timer_add(5.0,
289                                         (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
290                 }
291 #endif
292         }
293         break;
294
295         case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
296         {
297                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "---------------WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
298                 /* when deactivated, stop the timer */
299                 if (ad->transmit_timer) {
300                         ecore_timer_del(ad->transmit_timer);
301                         ad->transmit_timer = NULL;
302                 }
303
304 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
305                 /* when deactivated, stop the timer */
306                 if (ad->monitor_timer) {
307                         ecore_timer_del(ad->monitor_timer);
308                         ad->monitor_timer = NULL;
309                 }
310 #endif
311                 wfd_app_util_del_notification(ad);
312 #ifdef WFD_SCREEN_MIRRORING_ENABLED
313                 wfd_app_util_set_screen_mirroring_deactivated(ad);
314 #endif
315         }
316         break;
317
318         default:
319                 break;
320         }
321
322         __WFD_APP_FUNC_EXIT__;
323 }
324
325 /**
326  *      This function let the app make a callback for registering connection event
327  *      @return   void
328  *      @param[in] error_code the returned error code
329  *      @param[in] connection_state the state of connection
330  *      @param[in] mac_address the mac address of peer
331  *      @param[in] user_data the pointer to the main data structure
332  */
333 void _cb_connection(int error_code, wifi_direct_connection_state_e connection_state, const char *mac_address, void *user_data)
334 {
335         __WFD_APP_FUNC_ENTER__;
336
337         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
338         int result = -1;
339         int wfd_state = -1;
340 #ifdef WFD_SCREEN_MIRRORING_ENABLED
341         int screen_mirroring_status = 0;
342 #endif
343         wifi_direct_discovered_peer_info_s *peer_info = NULL;
344         wfd_connection_info_s *connection = ad->connection;
345 #ifdef NOT_CONNECTED_INDICATOR_ICON
346         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
347 #endif
348         char popup_text[MAX_POPUP_TEXT_SIZE] = {0, };
349
350         /* find the peer's name by the mac address */
351         WFD_RET_IF(NULL == mac_address, "ERROR : mac address is NULL !!\n");
352
353         WFD_APP_LOG(WFD_APP_LOG_ERROR, "-------------------error_code:%d connection_state:%d \n",
354                         error_code, connection_state);
355
356         /* when disconnection, mac_address is empty */
357         if (connection_state <= WIFI_DIRECT_DISASSOCIATION_IND) {
358                 if (connection) {
359                         result = strncmp(connection->peer_addr, mac_address, MACSTR_LENGTH);
360                         if (result) {
361                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Connection event from unknown peer");
362                                 return;
363                         }
364                 } else {
365                         result = wifi_direct_get_peer_info((char *)mac_address, &peer_info);
366                         if (result < 0 || !peer_info) {
367                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Unknown peer");
368                                 if (NULL != peer_info)
369                                         free(peer_info);
370                                 return;
371                         }
372
373                         connection = (wfd_connection_info_s*) calloc(1, sizeof(wfd_connection_info_s));
374                         if (!connection) {
375                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to allocate memory for peer");
376                                 free(peer_info);
377                                 return;
378                         }
379
380                         if (TRUE != dbus_validate_utf8(peer_info->device_name, NULL)) {
381                                 memcpy(connection->peer_name, peer_info->device_name, DEV_NAME_LENGTH-2);
382                                 connection->peer_name[DEV_NAME_LENGTH-2] = '\0';
383                         } else {
384                                 memcpy(connection->peer_name, peer_info->device_name, DEV_NAME_LENGTH);
385                                 connection->peer_name[DEV_NAME_LENGTH] = '\0';
386                         }
387
388                         strncpy(connection->peer_addr, mac_address, MACSTR_LENGTH);
389                         connection->peer_addr[MACSTR_LENGTH] = '\0';
390                         connection->device_type = peer_info->primary_device_type;
391                         connection->wifi_display = peer_info->is_miracast_device;
392                         wifi_direct_get_local_wps_type(&connection->wps_type);
393                         wifi_direct_is_autoconnection_mode(&connection->auto_conn);
394
395                         ad->connection = connection;
396                         free(peer_info);
397                 }
398         }
399
400         switch (connection_state) {
401         case WIFI_DIRECT_CONNECTION_REQ:
402         {
403                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
404
405                 memcpy((char*)ad->mac_addr_connecting, connection->peer_addr, MACSTR_LENGTH);
406
407                 wfd_app_get_connected_peers(ad);
408                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No of connected peers = %d", ad->raw_connected_peer_cnt);
409                 if (ad->raw_connected_peer_cnt >= WFD_MAX_CONNECTED_PEER) {
410                         result = wifi_direct_reject_connection(connection->peer_addr);
411                         if (result != WIFI_DIRECT_ERROR_NONE)
412                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to reject connection(%d)", result);
413                         snprintf(popup_text, MAX_POPUP_TEXT_SIZE,
414                                         D_("IDS_ST_POP_YOU_CAN_CONNECT_UP_TO_PD_DEVICES_AT_THE_SAME_TIME"),
415                                         WFD_MAX_CONNECTED_PEER);
416                         notification_status_message_post(popup_text);
417                         break;
418                 }
419
420                 if (connection->auto_conn) {
421                         result = wifi_direct_accept_connection(connection->peer_addr);
422                         if (result < 0) {
423                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to accept connection");
424                                 break;
425                         }
426                         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Succeeded to accept connection");
427                 } else {
428                         if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PBC) {
429                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PBC\n");
430                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
431                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
432                                 char *pin;
433                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY");
434                                 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
435                                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
436                                         return;
437                                 }
438                                 strncpy(connection->wps_pin, pin, PIN_LENGTH);
439                                 WFD_IF_FREE_MEM(pin);
440                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
441                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
442                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD");
443                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, NULL);
444                         } else {
445                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "wps_config is unkown!\n");
446                                 break;
447                         }
448                         int res = display_change_state(LCD_NORMAL);
449                         if(res < 0)
450                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to change PM state(%d)", res);
451                 }
452         }
453         break;
454
455         case WIFI_DIRECT_CONNECTION_WPS_REQ:
456         {
457                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
458                 if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
459                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
460                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, NULL);
461                 } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
462                         char *pin;
463                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
464
465                         if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
466                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
467                                 return;
468                         }
469
470                         strncpy(connection->wps_pin, pin, PIN_LENGTH);
471                         WFD_IF_FREE_MEM(pin);
472
473                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
474                 } else {
475                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "WPS type: %d", connection->wps_type);
476                 }
477         }
478         break;
479
480         case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
481         {
482                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
483         }
484         break;
485
486         case WIFI_DIRECT_CONNECTION_RSP:
487         {
488                 char *msg = NULL;
489                 char txt[WFD_POP_STR_MAX_LEN] = {0};
490                 wfd_destroy_popup();
491
492                 memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
493
494                 if (error_code != WIFI_DIRECT_ERROR_NONE) {
495                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to connect with peer[%s] -(%d)",
496                                                                 connection->peer_name, error_code);
497                         snprintf(txt, WFD_POP_STR_MAX_LEN,  D_("IDS_WIFI_POP_FAILED_TO_CONNECT_TO_PS"),
498                                                                 connection->peer_name);
499                 } else {
500                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Succeeded to connect with peer[%s] -(%d)",
501                                                                 connection->peer_name, error_code);
502                         snprintf(txt, WFD_POP_STR_MAX_LEN,  D_("IDS_WIFI_BODY_CONNECTED_TO_PS"),
503                                                                 connection->peer_name);
504 #ifdef WFD_SCREEN_MIRRORING_ENABLED
505                         result = vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status);
506                         if (result < 0)
507                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get VCONFKEY_SCREEN_MIRRORING_STATE");
508
509                         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED)
510                                 snprintf(txt, WFD_POP_STR_MAX_LEN,  D_("IDS_WIFI_BODY_CONNECTED_TO_PS"),
511                                                                 connection->peer_name);
512 #endif
513                 }
514                 if (connection)
515                         free(ad->connection);
516                 ad->connection = NULL;
517
518                 wfd_app_get_connected_peers(ad);
519                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No of connected peers = %d", ad->raw_connected_peer_cnt);
520                 /* tickernoti popup */
521                 if (ad->raw_connected_peer_cnt < WFD_MAX_CONNECTED_PEER) {
522                         if (strlen(txt) > 0) {
523                                 msg = elm_entry_utf8_to_markup(txt);
524                                 WFD_RET_IF(!msg, "Failed to elm_entry_markup_to_utf8()!");
525                                 notification_status_message_post(msg);
526                                 WFD_IF_FREE_MEM(msg);
527                         }
528                 }
529         }
530         break;
531
532         case WIFI_DIRECT_DISASSOCIATION_IND:
533         {
534                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISASSOCIATION_IND\n");
535                 wfd_app_util_del_wfd_connected_notification(ad);
536
537 #ifdef WFD_SCREEN_MIRRORING_ENABLED
538                 if (connection && connection->wifi_display)
539                         wfd_app_util_set_screen_mirroring_deactivated(ad);
540 #endif
541                 if (connection)
542                         free(ad->connection);
543                 ad->connection = NULL;
544
545         }
546         break;
547
548         case WIFI_DIRECT_DISCONNECTION_IND:
549         {
550 #if 0   // changed to show notification only when allshare cast device is connected.
551                 _del_wfd_notification();
552 #endif
553         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
554 #ifdef WFD_SCREEN_MIRRORING_ENABLED
555                 wfd_app_util_set_screen_mirroring_deactivated(ad);
556 #endif
557                 notification_status_message_post(D_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
558                 wfd_app_util_del_wfd_connected_notification(ad);
559         }
560         break;
561
562         case WIFI_DIRECT_DISCONNECTION_RSP:
563         {
564 #if 0   // changed to show notification only when allshare cast device is connected.
565                 _del_wfd_notification();
566 #endif
567                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_RSP\n");
568
569                 wfd_app_util_del_wfd_connected_notification(ad);
570
571 #ifdef WFD_SCREEN_MIRRORING_ENABLED
572                 wfd_app_util_set_screen_mirroring_deactivated(ad);
573 #endif
574                 wfd_destroy_popup();
575
576                 result = wifi_direct_set_autoconnection_mode(false);
577                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
578         }
579         break;
580
581         case WIFI_DIRECT_GROUP_DESTROYED:
582         {
583                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_GROUP_DESTROYED\n");
584                 notification_status_message_post(D_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
585         }
586         break;
587
588         default:
589                 break;
590
591         }
592
593         /*
594          * To support ON DEMAND popup destroy. API request blocks and popup destroy
595          * fails.
596          */
597         /* wifi_direct_get_state(&ad->wfd_status); */
598
599         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wfd_state) < 0) {
600                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Error reading vconf (%s)\n", VCONFKEY_WIFI_DIRECT_STATE);
601         }
602         WFD_APP_LOG(WFD_APP_LOG_LOW, "wfd state: %d", wfd_state);
603
604         if (wfd_state == VCONFKEY_WIFI_DIRECT_DEACTIVATED) {
605                 wfd_app_util_del_notification(ad);
606         }
607
608 #ifdef NOT_CONNECTED_INDICATOR_ICON
609         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
610                 noti_err  = notification_delete(ad->noti_wifi_direct_on);
611                 if (noti_err != NOTIFICATION_ERROR_NONE) {
612                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
613                 } else {
614                         noti_err = notification_free(ad->noti_wifi_direct_on);
615                         ad->noti_wifi_direct_on = NULL;
616                         if (noti_err != NOTIFICATION_ERROR_NONE) {
617                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
618                         }
619                 }
620         }
621 #endif
622
623         if (wfd_state < VCONFKEY_WIFI_DIRECT_CONNECTED) {
624             if (ad->transmit_timer) {
625                     ecore_timer_del(ad->transmit_timer);
626                     ad->transmit_timer = NULL;
627             }
628         } else {
629                 if (NULL == ad->transmit_timer) {
630                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer\n");
631                         ad->last_wfd_transmit_time = time(NULL);
632                         ad->transmit_timer = ecore_timer_add(5.0,
633                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
634                 }
635         }
636
637         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
638 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
639                 if (ad->monitor_timer) {
640                         ecore_timer_del(ad->monitor_timer);
641                         ad->monitor_timer = NULL;
642                 }
643 #endif
644         } else {
645 #ifdef NOT_CONNECTED_INDICATOR_ICON
646                 wfd_app_util_add_indicator_icon(ad);
647 #endif
648 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
649                 if (NULL == ad->monitor_timer) {
650                         ad->last_wfd_time = time(NULL);
651                         ad->monitor_timer = ecore_timer_add(5.0,
652                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
653                 }
654 #endif
655         }
656
657         __WFD_APP_FUNC_EXIT__;
658 }
659
660 /**
661  *      This function let the app do initialization
662  *      @return   If success, return TRUE, else return FALSE
663  *      @param[in] ad the pointer to the main data structure
664  */
665 bool init_wfd_client(wfd_appdata_t *ad)
666 {
667         __WFD_APP_FUNC_ENTER__;
668         WFD_RETV_IF(NULL == ad, FALSE, "NULL parameters.\n");
669         int ret = -1;
670         int retrys = 3;
671
672         ad->last_wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
673
674         while (retrys > 0) {
675                 ret = wifi_direct_initialize();
676                 if (ret == WIFI_DIRECT_ERROR_NONE ||
677                         ret == WIFI_DIRECT_ERROR_ALREADY_INITIALIZED)
678                         break;
679                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to initialize Wi-Fi Direct(%d)\n", ret);
680
681                 retrys--;
682                 if (retrys == 0)
683                         return FALSE;
684                 usleep(100*1000);
685         }
686
687         ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void*) ad);
688         if (ret != WIFI_DIRECT_ERROR_NONE) {
689                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_activation(%d)\n", ret);
690                 return FALSE;
691         }
692
693         ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void*) ad);
694         if (ret != WIFI_DIRECT_ERROR_NONE) {
695                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_connection(%d)\n", ret);
696                 return FALSE;
697         }
698
699 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
700         if (NULL == ad->monitor_timer) {
701                 ad->last_wfd_time = time(NULL);
702                 ad->monitor_timer = ecore_timer_add(5.0,
703                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
704         }
705 #endif
706         __WFD_APP_FUNC_EXIT__;
707         return TRUE;
708 }
709
710 /**
711  *      This function let the app do de-initialization
712  *      @return   If success, return TRUE, else return FALSE
713  *      @param[in] ad the pointer to the main data structure
714  */
715 int deinit_wfd_client(wfd_appdata_t *ad)
716 {
717         __WFD_APP_FUNC_ENTER__;
718         int ret = -1;
719
720         if (NULL == ad) {
721                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameter\n");
722                 return -1;
723         }
724
725 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
726         if (ad->monitor_timer) {
727                 ecore_timer_del(ad->monitor_timer);
728                 ad->monitor_timer = NULL;
729         }
730 #endif
731
732         ret = wifi_direct_unset_device_state_changed_cb();
733         if (ret != WIFI_DIRECT_ERROR_NONE) {
734                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_activation(%d)\n", ret);
735         }
736
737         ret = wifi_direct_unset_connection_state_changed_cb();
738         if (ret != WIFI_DIRECT_ERROR_NONE) {
739                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_connection(%d)\n", ret);
740         }
741
742         ret = wifi_direct_deinitialize();
743         if (ret != WIFI_DIRECT_ERROR_NONE) {
744                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deinitialize Wi-Fi Direct. error code = [%d]\n", ret);
745         }
746
747         __WFD_APP_FUNC_EXIT__;
748         return 0;
749 }