Fix issues reported by static analyzer
[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.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
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         g_strlcpy(ad->raw_connected_peers[peer_cnt].ssid, peer->device_name, sizeof(ad->raw_connected_peers[peer_cnt].ssid));
68         g_strlcpy(ad->raw_connected_peers[peer_cnt].mac_address, peer->mac_address, WFD_MAC_ADDRESS_SIZE);
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         /* get transfer state */
132         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_TRANSFER_STATE, &wfd_transfer_state) < 0) {
133                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Error reading vconf (%s)\n",
134                         VCONFKEY_WIFI_DIRECT_TRANSFER_STATE);
135                 return ECORE_CALLBACK_CANCEL;
136         }
137
138         res = wifi_direct_get_state(&ad->wfd_status);
139         if (res != WIFI_DIRECT_ERROR_NONE)
140                 return ECORE_CALLBACK_CANCEL;
141
142         if (ad->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
143                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Wi-Fi Direct is unconnected!");
144                 return ECORE_CALLBACK_CANCEL;
145         }
146
147         /* show tickernoti*/
148         if (wfd_transfer_state > VCONFKEY_WIFI_DIRECT_TRANSFER_START) {
149                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Display Toast popup.\n");
150                 notification_status_message_post(D_("IDS_WIFI_BODY_TO_SAVE_BATTERY_POWER_DISABLE_WI_FI_DIRECT_AFTER_USE"));
151                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No RX/TX packet, turn off WFD automatically.\n");
152                 wfd_app_util_add_wfd_turn_off_notification(ad);
153         } else {
154                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Has RX/TX packet, restart.\n");
155                 ad->last_wfd_transmit_time = time(NULL);
156                 return ECORE_CALLBACK_RENEW;
157         }
158
159         ad->transmit_timer = NULL;
160         __WFD_APP_FUNC_EXIT__;
161         return ECORE_CALLBACK_CANCEL;
162 }
163
164 int wfd_app_client_switch_off(void *data)
165 {
166         wfd_appdata_t *ad = (wfd_appdata_t *)data;
167         int res;
168         if (NULL == ad) {
169                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "NULL == ad!\n");
170                 return -1;
171         }
172         res = wifi_direct_get_state(&ad->wfd_status);
173         if (res != WIFI_DIRECT_ERROR_NONE)
174                 return ECORE_CALLBACK_CANCEL;
175
176         if (ad->wfd_status >= WIFI_DIRECT_STATE_ACTIVATING) {
177                 /*if connected, disconnect all devices*/
178                 if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
179                         res = wifi_direct_disconnect_all();
180                         if (res != WIFI_DIRECT_ERROR_NONE)
181                                 return -1;
182                 }
183                 res = wifi_direct_deactivate();
184                 if (res != WIFI_DIRECT_ERROR_NONE) {
185                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
186                         return -1;
187                 }
188         }
189
190         return 0;
191 }
192
193 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
194 /**
195  *      This function let the ug make a callback for deactivating wfd automatically
196  *      @return   if stop the timer, return ECORE_CALLBACK_CANCEL, else return ECORE_CALLBACK_RENEW
197  *      @param[in] user_data the pointer to the main data structure
198  */
199 static Eina_Bool _wfd_automatic_deactivated_for_no_connection_cb(void *user_data)
200 {
201         int res = -1;
202         int interval = 0;
203         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
204 #ifdef WFD_SCREEN_MIRRORING_ENABLED
205         int screen_mirroring_status = 0;
206 #endif
207
208         if (NULL == ad)
209                 return ECORE_CALLBACK_CANCEL;
210
211         /* check the action, if action is exist, keep the cb */
212         res = wifi_direct_get_state(&ad->wfd_status);
213         if (res != WIFI_DIRECT_ERROR_NONE) {
214                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Get wifi-direct status WIFI_DIRECT_ERROR_NONE!\n");
215                 return ECORE_CALLBACK_CANCEL;
216         }
217
218         if (ad->last_wfd_status != ad->wfd_status) {
219                 ad->last_wfd_status = ad->wfd_status;
220                 ad->last_wfd_time = time(NULL);
221                 return ECORE_CALLBACK_RENEW;
222         }
223
224         /* check the timeout, if not timeout, keep the cb */
225         interval = time(NULL) - ad->last_wfd_time;
226         if (interval < MAX_NO_ACTION_TIME_OUT)
227                 return ECORE_CALLBACK_RENEW;
228
229         /* turn off the Wi-Fi Direct */
230         wifi_direct_get_state(&ad->wfd_status);
231         if (ad->wfd_status < WIFI_DIRECT_STATE_ACTIVATING) {
232                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wfd_status < WIFI_DIRECT_STATE_ACTIVATING!\n");
233         } else {
234 #ifdef WFD_SCREEN_MIRRORING_ENABLED
235                 if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
236                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
237
238                 if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_ACTIVATED) {
239                         ad->last_wfd_time = time(NULL);
240                         return ECORE_CALLBACK_RENEW;
241                 } else {
242                         /* turn off the Wi-Fi Direct */
243                         wfd_app_client_switch_off(ad);
244                         ad->popup = wfd_draw_pop_type_auto_deactivation(ad->win, user_data);
245                 }
246 #endif
247         }
248
249         /* reset monitor timer */
250         if (ad->monitor_timer)
251                 ad->monitor_timer = NULL;       //ECORE_CALLBACK_CANCEL will release timer.
252
253         return ECORE_CALLBACK_CANCEL;
254 }
255 #endif
256
257 /**
258  *      This function let the app make a callback for registering activation event
259  *      @return   void
260  *      @param[in] error_code the returned error code
261  *      @param[in] device_state the state of device
262  *      @param[in] user_data the pointer to the main data structure
263  */
264 void _cb_activation(int error_code, wifi_direct_device_state_e device_state, void *user_data)
265 {
266         __WFD_APP_FUNC_ENTER__;
267         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
268
269         switch (device_state) {
270         case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
271         {
272                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "---------------WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
273 #ifdef NOT_CONNECTED_INDICATOR_ICON
274                 wfd_app_util_add_indicator_icon(ad);
275 #endif
276 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
277                 if (NULL == ad->monitor_timer) {
278                         ad->last_wfd_time = time(NULL);
279                         ad->monitor_timer = ecore_timer_add(5.0,
280                                         (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
281                 }
282 #endif
283         }
284         break;
285
286         case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
287         {
288                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "---------------WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
289                 /* when deactivated, stop the timer */
290                 if (ad->transmit_timer) {
291                         ecore_timer_del(ad->transmit_timer);
292                         ad->transmit_timer = NULL;
293                 }
294
295 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
296                 /* when deactivated, stop the timer */
297                 if (ad->monitor_timer) {
298                         ecore_timer_del(ad->monitor_timer);
299                         ad->monitor_timer = NULL;
300                 }
301 #endif
302                 wfd_app_util_del_notification(ad);
303 #ifdef WFD_SCREEN_MIRRORING_ENABLED
304                 wfd_app_util_set_screen_mirroring_deactivated(ad);
305 #endif
306         }
307         break;
308
309         default:
310                 break;
311         }
312
313         __WFD_APP_FUNC_EXIT__;
314 }
315
316 /**
317  *      This function let the app make a callback for registering connection event
318  *      @return   void
319  *      @param[in] error_code the returned error code
320  *      @param[in] connection_state the state of connection
321  *      @param[in] mac_address the mac address of peer
322  *      @param[in] user_data the pointer to the main data structure
323  */
324 void _cb_connection(int error_code, wifi_direct_connection_state_e connection_state, const char *mac_address, void *user_data)
325 {
326         __WFD_APP_FUNC_ENTER__;
327
328         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
329         int result = -1;
330         int wfd_state = -1;
331 #ifdef WFD_SCREEN_MIRRORING_ENABLED
332         int screen_mirroring_status = 0;
333 #endif
334         wifi_direct_discovered_peer_info_s *peer_info = NULL;
335         wfd_connection_info_s *connection = ad->connection;
336 #ifdef NOT_CONNECTED_INDICATOR_ICON
337         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
338 #endif
339         char popup_text[MAX_POPUP_TEXT_SIZE] = {0, };
340
341         /* find the peer's name by the mac address */
342         WFD_RET_IF(NULL == mac_address, "ERROR : mac address is NULL !!\n");
343
344         WFD_APP_LOG(WFD_APP_LOG_ERROR, "-------------------error_code:%d connection_state:%d \n",
345                         error_code, connection_state);
346
347         /* when disconnection, mac_address is empty */
348         if (connection_state <= WIFI_DIRECT_DISASSOCIATION_IND) {
349                 if (connection) {
350                         result = strncmp(connection->peer_addr, mac_address, MACSTR_LENGTH);
351                         if (result) {
352                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Connection event from unknown peer");
353                                 return;
354                         }
355                 } else {
356                         result = wifi_direct_get_peer_info((char *)mac_address, &peer_info);
357                         if (result < 0 || !peer_info) {
358                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Unknown peer");
359                                 if (NULL != peer_info)
360                                         free(peer_info);
361                                 return;
362                         }
363
364                         connection = (wfd_connection_info_s*) calloc(1, sizeof(wfd_connection_info_s));
365                         if (!connection) {
366                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to allocate memory for peer");
367                                 g_free(peer_info);
368                                 return;
369                         }
370
371                         if (TRUE != dbus_validate_utf8(peer_info->device_name, NULL)) {
372                                 memcpy(connection->peer_name, peer_info->device_name, DEV_NAME_LENGTH-2);
373                                 connection->peer_name[DEV_NAME_LENGTH-2] = '\0';
374                         } else {
375                                 memcpy(connection->peer_name, peer_info->device_name, DEV_NAME_LENGTH);
376                                 connection->peer_name[DEV_NAME_LENGTH] = '\0';
377                         }
378
379                         strncpy(connection->peer_addr, mac_address, MACSTR_LENGTH);
380                         connection->peer_addr[MACSTR_LENGTH] = '\0';
381                         connection->device_type = peer_info->primary_device_type;
382                         connection->wifi_display = peer_info->is_miracast_device;
383                         wifi_direct_get_local_wps_type(&connection->wps_type);
384                         wifi_direct_is_autoconnection_mode(&connection->auto_conn);
385
386                         ad->connection = connection;
387                         free(peer_info);
388                 }
389         }
390
391         switch (connection_state) {
392         case WIFI_DIRECT_CONNECTION_REQ:
393         {
394                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
395                 char *format_str = D_("IDS_ST_POP_YOU_CAN_CONNECT_UP_TO_PD_DEVICES_AT_THE_SAME_TIME");
396
397                 memcpy((char*)ad->mac_addr_connecting, connection->peer_addr, MACSTR_LENGTH);
398
399                 wfd_app_get_connected_peers(ad);
400                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No of connected peers = %d", ad->raw_connected_peer_cnt);
401                 if (ad->raw_connected_peer_cnt >= WFD_MAX_CONNECTED_PEER) {
402                         result = wifi_direct_reject_connection(connection->peer_addr);
403                         if (result != WIFI_DIRECT_ERROR_NONE)
404                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to reject connection(%d)", result);
405                         g_snprintf(popup_text, MAX_POPUP_TEXT_SIZE,
406                                         format_str, WFD_MAX_CONNECTED_PEER);
407                         notification_status_message_post(popup_text);
408                         break;
409                 }
410
411                 if (connection->auto_conn) {
412                         result = wifi_direct_accept_connection(connection->peer_addr);
413                         if (result < 0) {
414                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to accept connection");
415                                 break;
416                         }
417                         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Succeeded to accept connection");
418                 } else {
419                         if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PBC) {
420                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PBC\n");
421                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
422                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
423                                 char *pin;
424                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY");
425                                 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
426                                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
427                                         return;
428                                 }
429                                 strncpy(connection->wps_pin, pin, PIN_LENGTH);
430                                 WFD_IF_FREE_MEM(pin);
431                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
432                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
433                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD");
434                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, NULL);
435                         } else {
436                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "wps_config is unknown!\n");
437                                 break;
438                         }
439                         int res = display_change_state(LCD_NORMAL);
440                         if (res < 0)
441                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to change PM state(%d)", res);
442                 }
443         }
444         break;
445
446         case WIFI_DIRECT_CONNECTION_WPS_REQ:
447         {
448                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
449                 if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
450                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
451                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, NULL);
452                 } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
453                         char *pin;
454                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
455
456                         if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
457                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
458                                 return;
459                         }
460
461                         strncpy(connection->wps_pin, pin, PIN_LENGTH);
462                         WFD_IF_FREE_MEM(pin);
463
464                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
465                 } else {
466                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "WPS type: %d", connection->wps_type);
467                 }
468         }
469         break;
470
471         case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
472         {
473                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
474         }
475         break;
476
477         case WIFI_DIRECT_CONNECTION_RSP:
478         {
479                 char *msg = NULL;
480                 char txt[WFD_POP_STR_MAX_LEN] = {0};
481                 char *format_str = NULL;
482                 wfd_destroy_popup();
483
484                 memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
485
486                 if (error_code != WIFI_DIRECT_ERROR_NONE) {
487                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to connect with peer[%s] -(%d)",
488                                                                 connection->peer_name, error_code);
489                         format_str = D_("IDS_WIFI_POP_FAILED_TO_CONNECT_TO_PS");
490                         snprintf(txt, WFD_POP_STR_MAX_LEN, format_str, connection->peer_name);
491                 } else {
492                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Succeeded to connect with peer[%s] -(%d)",
493                                                                 connection->peer_name, error_code);
494                         format_str = D_("IDS_WIFI_BODY_CONNECTED_TO_PS");
495                         snprintf(txt, WFD_POP_STR_MAX_LEN, format_str, connection->peer_name);
496 #ifdef WFD_SCREEN_MIRRORING_ENABLED
497                         result = vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status);
498                         if (result < 0)
499                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get VCONFKEY_SCREEN_MIRRORING_STATE");
500
501                         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED)
502                                 snprintf(txt, WFD_POP_STR_MAX_LEN,  D_("IDS_WIFI_BODY_CONNECTED_TO_PS"),
503                                                                 connection->peer_name);
504 #endif
505                 }
506                 if (connection)
507                         free(ad->connection);
508                 ad->connection = NULL;
509
510                 wfd_app_get_connected_peers(ad);
511                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No of connected peers = %d", ad->raw_connected_peer_cnt);
512                 /* tickernoti popup */
513                 if (ad->raw_connected_peer_cnt < WFD_MAX_CONNECTED_PEER) {
514                         if (strlen(txt) > 0) {
515                                 msg = elm_entry_utf8_to_markup(txt);
516                                 WFD_RET_IF(!msg, "Failed to elm_entry_markup_to_utf8()!");
517                                 notification_status_message_post(msg);
518                                 WFD_IF_FREE_MEM(msg);
519                         }
520                 }
521         }
522         break;
523
524         case WIFI_DIRECT_DISASSOCIATION_IND:
525         {
526                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISASSOCIATION_IND\n");
527                 wfd_app_util_del_wfd_connected_notification(ad);
528
529 #ifdef WFD_SCREEN_MIRRORING_ENABLED
530                 if (connection && connection->wifi_display)
531                         wfd_app_util_set_screen_mirroring_deactivated(ad);
532 #endif
533                 if (connection)
534                         free(ad->connection);
535                 ad->connection = NULL;
536
537         }
538         break;
539
540         case WIFI_DIRECT_DISCONNECTION_IND:
541         {
542 #if 0   // changed to show notification only when allshare cast device is connected.
543                 _del_wfd_notification();
544 #endif
545         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
546 #ifdef WFD_SCREEN_MIRRORING_ENABLED
547                 wfd_app_util_set_screen_mirroring_deactivated(ad);
548 #endif
549                 notification_status_message_post(D_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
550                 wfd_app_util_del_wfd_connected_notification(ad);
551         }
552         break;
553
554         case WIFI_DIRECT_DISCONNECTION_RSP:
555         {
556 #if 0   // changed to show notification only when allshare cast device is connected.
557                 _del_wfd_notification();
558 #endif
559                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_RSP\n");
560
561                 wfd_app_util_del_wfd_connected_notification(ad);
562
563 #ifdef WFD_SCREEN_MIRRORING_ENABLED
564                 wfd_app_util_set_screen_mirroring_deactivated(ad);
565 #endif
566                 wfd_destroy_popup();
567
568                 result = wifi_direct_set_autoconnection_mode(false);
569                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
570         }
571         break;
572
573         case WIFI_DIRECT_GROUP_DESTROYED:
574         {
575                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_GROUP_DESTROYED\n");
576                 notification_status_message_post(D_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
577         }
578         break;
579
580         default:
581                 break;
582
583         }
584
585         /*
586          * To support ON DEMAND popup destroy. API request blocks and popup destroy
587          * fails.
588          */
589         /* wifi_direct_get_state(&ad->wfd_status); */
590
591         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wfd_state) < 0)
592                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Error reading vconf (%s)\n", VCONFKEY_WIFI_DIRECT_STATE);
593
594         WFD_APP_LOG(WFD_APP_LOG_LOW, "wfd state: %d", wfd_state);
595
596         if (wfd_state == VCONFKEY_WIFI_DIRECT_DEACTIVATED)
597                 wfd_app_util_del_notification(ad);
598
599 #ifdef NOT_CONNECTED_INDICATOR_ICON
600         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
601                 noti_err  = notification_delete(ad->noti_wifi_direct_on);
602                 if (noti_err != NOTIFICATION_ERROR_NONE) {
603                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
604                 } else {
605                         noti_err = notification_free(ad->noti_wifi_direct_on);
606                         ad->noti_wifi_direct_on = NULL;
607                         if (noti_err != NOTIFICATION_ERROR_NONE)
608                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
609                 }
610         }
611 #endif
612
613         if (wfd_state < VCONFKEY_WIFI_DIRECT_CONNECTED) {
614             if (ad->transmit_timer) {
615                     ecore_timer_del(ad->transmit_timer);
616                     ad->transmit_timer = NULL;
617             }
618         } else {
619                 if (NULL == ad->transmit_timer) {
620                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer\n");
621                         ad->last_wfd_transmit_time = time(NULL);
622                         ad->transmit_timer = ecore_timer_add(5.0,
623                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
624                 }
625         }
626
627         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
628 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
629                 if (ad->monitor_timer) {
630                         ecore_timer_del(ad->monitor_timer);
631                         ad->monitor_timer = NULL;
632                 }
633 #endif
634         } else {
635 #ifdef NOT_CONNECTED_INDICATOR_ICON
636                 wfd_app_util_add_indicator_icon(ad);
637 #endif
638 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
639                 if (NULL == ad->monitor_timer) {
640                         ad->last_wfd_time = time(NULL);
641                         ad->monitor_timer = ecore_timer_add(5.0,
642                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
643                 }
644 #endif
645         }
646
647         __WFD_APP_FUNC_EXIT__;
648 }
649
650 /**
651  *      This function let the app do initialization
652  *      @return   If success, return TRUE, else return FALSE
653  *      @param[in] ad the pointer to the main data structure
654  */
655 bool init_wfd_client(wfd_appdata_t *ad)
656 {
657         __WFD_APP_FUNC_ENTER__;
658         WFD_RETV_IF(NULL == ad, FALSE, "NULL parameters.\n");
659         int ret = -1;
660         int retrys = 3;
661
662         ad->last_wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
663
664         while (retrys > 0) {
665                 ret = wifi_direct_initialize();
666                 if (ret == WIFI_DIRECT_ERROR_NONE ||
667                         ret == WIFI_DIRECT_ERROR_ALREADY_INITIALIZED)
668                         break;
669                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to initialize Wi-Fi Direct(%d)\n", ret);
670
671                 retrys--;
672                 if (retrys == 0)
673                         return FALSE;
674                 usleep(100*1000);
675         }
676
677         ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void*) ad);
678         if (ret != WIFI_DIRECT_ERROR_NONE) {
679                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_activation(%d)\n", ret);
680                 return FALSE;
681         }
682
683         ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void*) ad);
684         if (ret != WIFI_DIRECT_ERROR_NONE) {
685                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_connection(%d)\n", ret);
686                 return FALSE;
687         }
688
689 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
690         if (NULL == ad->monitor_timer) {
691                 ad->last_wfd_time = time(NULL);
692                 ad->monitor_timer = ecore_timer_add(5.0,
693                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
694         }
695 #endif
696         __WFD_APP_FUNC_EXIT__;
697         return TRUE;
698 }
699
700 /**
701  *      This function let the app do de-initialization
702  *      @return   If success, return TRUE, else return FALSE
703  *      @param[in] ad the pointer to the main data structure
704  */
705 int deinit_wfd_client(wfd_appdata_t *ad)
706 {
707         __WFD_APP_FUNC_ENTER__;
708         int ret = -1;
709
710         if (NULL == ad) {
711                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameter\n");
712                 return -1;
713         }
714
715 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
716         if (ad->monitor_timer) {
717                 ecore_timer_del(ad->monitor_timer);
718                 ad->monitor_timer = NULL;
719         }
720 #endif
721
722         ret = wifi_direct_unset_device_state_changed_cb();
723         if (ret != WIFI_DIRECT_ERROR_NONE)
724                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_activation(%d)\n", ret);
725
726         ret = wifi_direct_unset_connection_state_changed_cb();
727         if (ret != WIFI_DIRECT_ERROR_NONE)
728                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_connection(%d)\n", ret);
729
730         ret = wifi_direct_deinitialize();
731         if (ret != WIFI_DIRECT_ERROR_NONE)
732                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deinitialize Wi-Fi Direct. error code = [%d]\n", ret);
733
734         __WFD_APP_FUNC_EXIT__;
735         return 0;
736 }