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