[ug-wifi-direct]Sync with Tizen 2.4
[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                         usleep(200);
420                         result = wifi_direct_accept_connection(connection->peer_addr);
421                         if (result < 0) {
422                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to accept connection");
423                                 break;
424                         }
425                         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Succeeded to accept connection");
426                 } else {
427                         if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PBC) {
428                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PBC\n");
429                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
430                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
431                                 char *pin;
432                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY");
433                                 if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
434                                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
435                                         return;
436                                 }
437                                 strncpy(connection->wps_pin, pin, PIN_LENGTH);
438                                 WFD_IF_FREE_MEM(pin);
439                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
440                         } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
441                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD");
442                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, NULL);
443                         } else {
444                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "wps_config is unkown!\n");
445                                 break;
446                         }
447                         int res = display_change_state(LCD_NORMAL);
448                         if(res < 0)
449                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to change PM state(%d)", res);
450                 }
451         }
452         break;
453
454         case WIFI_DIRECT_CONNECTION_WPS_REQ:
455         {
456                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
457                 if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
458                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
459                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, NULL);
460                 } else if (connection->wps_type == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY) {
461                         char *pin;
462                         WFD_APP_LOG(WFD_APP_LOG_LOW, "WPS type: WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
463
464                         if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE) {
465                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get WPS pin");
466                                 return;
467                         }
468
469                         strncpy(connection->wps_pin, pin, PIN_LENGTH);
470                         WFD_IF_FREE_MEM(pin);
471
472                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
473                 } else {
474                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "WPS type: %d", connection->wps_type);
475                 }
476         }
477         break;
478
479         case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
480         {
481                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
482         }
483         break;
484
485         case WIFI_DIRECT_CONNECTION_RSP:
486         {
487                 char *msg = NULL;
488                 char txt[WFD_POP_STR_MAX_LEN] = {0};
489                 wfd_destroy_popup();
490
491                 memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
492
493                 if (error_code != WIFI_DIRECT_ERROR_NONE) {
494                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to connect with peer[%s] -(%d)",
495                                                                 connection->peer_name, error_code);
496                         snprintf(txt, WFD_POP_STR_MAX_LEN,  _("IDS_WIFI_POP_FAILED_TO_CONNECT_TO_PS"),
497                                                                 connection->peer_name);
498                 } else {
499                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Succeeded to connect with peer[%s] -(%d)",
500                                                                 connection->peer_name, error_code);
501                         snprintf(txt, WFD_POP_STR_MAX_LEN,  _("IDS_WIFI_BODY_CONNECTED_TO_PS"),
502                                                                 connection->peer_name);
503 #ifdef WFD_SCREEN_MIRRORING_ENABLED
504                         result = vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status);
505                         if (result < 0)
506                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get VCONFKEY_SCREEN_MIRRORING_STATE");
507
508                         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED)
509                                 snprintf(txt, WFD_POP_STR_MAX_LEN,  _("IDS_WIFI_BODY_CONNECTED_TO_PS"),
510                                                                 connection->peer_name);
511 #endif
512                 }
513                 if (connection)
514                         free(ad->connection);
515                 ad->connection = NULL;
516
517                 wfd_app_get_connected_peers(ad);
518                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No of connected peers = %d", ad->raw_connected_peer_cnt);
519                 /* tickernoti popup */
520                 if (ad->raw_connected_peer_cnt < WFD_MAX_CONNECTED_PEER) {
521                         if (strlen(txt) > 0) {
522                                 msg = elm_entry_utf8_to_markup(txt);
523                                 WFD_RET_IF(!msg, "Failed to elm_entry_markup_to_utf8()!");
524                                 notification_status_message_post(msg);
525                                 WFD_IF_FREE_MEM(msg);
526                         }
527                 }
528         }
529         break;
530
531         case WIFI_DIRECT_DISASSOCIATION_IND:
532         {
533                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISASSOCIATION_IND\n");
534                 wfd_app_util_del_wfd_connected_notification(ad);
535
536 #ifdef WFD_SCREEN_MIRRORING_ENABLED
537                 if (connection && connection->wifi_display)
538                         wfd_app_util_set_screen_mirroring_deactivated(ad);
539 #endif
540                 if (connection)
541                         free(ad->connection);
542                 ad->connection = NULL;
543
544         }
545         break;
546
547         case WIFI_DIRECT_DISCONNECTION_IND:
548         {
549 #if 0   // changed to show notification only when allshare cast device is connected.
550                 _del_wfd_notification();
551 #endif
552         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
553 #ifdef WFD_SCREEN_MIRRORING_ENABLED
554                 wfd_app_util_set_screen_mirroring_deactivated(ad);
555 #endif
556                 notification_status_message_post(_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
557                 wfd_app_util_del_wfd_connected_notification(ad);
558         }
559         break;
560
561         case WIFI_DIRECT_DISCONNECTION_RSP:
562         {
563 #if 0   // changed to show notification only when allshare cast device is connected.
564                 _del_wfd_notification();
565 #endif
566                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCONNECTION_RSP\n");
567
568                 wfd_app_util_del_wfd_connected_notification(ad);
569
570 #ifdef WFD_SCREEN_MIRRORING_ENABLED
571                 wfd_app_util_set_screen_mirroring_deactivated(ad);
572 #endif
573                 wfd_destroy_popup();
574
575                 result = wifi_direct_set_autoconnection_mode(false);
576                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
577         }
578         break;
579
580         case WIFI_DIRECT_GROUP_DESTROYED:
581         {
582                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_GROUP_DESTROYED\n");
583                 notification_status_message_post(_("IDS_WIFI_BODY_THE_WI_FI_DIRECT_CONNECTION_HAS_BEEN_LOST"));
584         }
585         break;
586
587         default:
588                 break;
589
590         }
591
592         /*
593          * To support ON DEMAND popup destroy. API request blocks and popup destroy
594          * fails.
595          */
596         /* wifi_direct_get_state(&ad->wfd_status); */
597
598         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wfd_state) < 0) {
599                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Error reading vconf (%s)\n", VCONFKEY_WIFI_DIRECT_STATE);
600         }
601         WFD_APP_LOG(WFD_APP_LOG_LOW, "wfd state: %d", wfd_state);
602
603         if (wfd_state == VCONFKEY_WIFI_DIRECT_DEACTIVATED) {
604                 wfd_app_util_del_notification(ad);
605         }
606
607 #ifdef NOT_CONNECTED_INDICATOR_ICON
608         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
609                 noti_err  = notification_delete(ad->noti_wifi_direct_on);
610                 if (noti_err != NOTIFICATION_ERROR_NONE) {
611                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
612                 } else {
613                         noti_err = notification_free(ad->noti_wifi_direct_on);
614                         ad->noti_wifi_direct_on = NULL;
615                         if (noti_err != NOTIFICATION_ERROR_NONE) {
616                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
617                         }
618                 }
619         }
620 #endif
621
622         if (wfd_state < VCONFKEY_WIFI_DIRECT_CONNECTED) {
623             if (ad->transmit_timer) {
624                     ecore_timer_del(ad->transmit_timer);
625                     ad->transmit_timer = NULL;
626             }
627         } else {
628                 if (NULL == ad->transmit_timer) {
629                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer\n");
630                         ad->last_wfd_transmit_time = time(NULL);
631                         ad->transmit_timer = ecore_timer_add(5.0,
632                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
633                 }
634         }
635
636         if (wfd_state >= VCONFKEY_WIFI_DIRECT_CONNECTED) {
637 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
638                 if (ad->monitor_timer) {
639                         ecore_timer_del(ad->monitor_timer);
640                         ad->monitor_timer = NULL;
641                 }
642 #endif
643         } else {
644 #ifdef NOT_CONNECTED_INDICATOR_ICON
645                 wfd_app_util_add_indicator_icon(ad);
646 #endif
647 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
648                 if (NULL == ad->monitor_timer) {
649                         ad->last_wfd_time = time(NULL);
650                         ad->monitor_timer = ecore_timer_add(5.0,
651                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
652                 }
653 #endif
654         }
655
656         __WFD_APP_FUNC_EXIT__;
657 }
658
659 /**
660  *      This function let the app do initialization
661  *      @return   If success, return TRUE, else return FALSE
662  *      @param[in] ad the pointer to the main data structure
663  */
664 bool init_wfd_client(wfd_appdata_t *ad)
665 {
666         __WFD_APP_FUNC_ENTER__;
667         WFD_RETV_IF(NULL == ad, FALSE, "NULL parameters.\n");
668         int ret = -1;
669         int retrys = 3;
670
671         ad->last_wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
672
673         while (retrys > 0) {
674                 ret = wifi_direct_initialize();
675                 if (ret == WIFI_DIRECT_ERROR_NONE ||
676                         ret == WIFI_DIRECT_ERROR_ALREADY_INITIALIZED)
677                         break;
678                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to initialize Wi-Fi Direct(%d)\n", ret);
679
680                 retrys--;
681                 if (retrys == 0)
682                         return FALSE;
683                 usleep(100*1000);
684         }
685
686         ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void*) ad);
687         if (ret != WIFI_DIRECT_ERROR_NONE) {
688                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_activation(%d)\n", ret);
689                 return FALSE;
690         }
691
692         ret = wifi_direct_set_connection_state_changed_cb(_cb_connection, (void*) ad);
693         if (ret != WIFI_DIRECT_ERROR_NONE) {
694                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register _cb_connection(%d)\n", ret);
695                 return FALSE;
696         }
697
698 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
699         if (NULL == ad->monitor_timer) {
700                 ad->last_wfd_time = time(NULL);
701                 ad->monitor_timer = ecore_timer_add(5.0,
702                                 (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
703         }
704 #endif
705         __WFD_APP_FUNC_EXIT__;
706         return TRUE;
707 }
708
709 /**
710  *      This function let the app do de-initialization
711  *      @return   If success, return TRUE, else return FALSE
712  *      @param[in] ad the pointer to the main data structure
713  */
714 int deinit_wfd_client(wfd_appdata_t *ad)
715 {
716         __WFD_APP_FUNC_ENTER__;
717         int ret = -1;
718
719         if (NULL == ad) {
720                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameter\n");
721                 return -1;
722         }
723
724 #ifdef WFD_FIVE_MIN_IDLE_DEACTIVATION
725         if (ad->monitor_timer) {
726                 ecore_timer_del(ad->monitor_timer);
727                 ad->monitor_timer = NULL;
728         }
729 #endif
730
731         ret = wifi_direct_unset_device_state_changed_cb();
732         if (ret != WIFI_DIRECT_ERROR_NONE) {
733                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_activation(%d)\n", ret);
734         }
735
736         ret = wifi_direct_unset_connection_state_changed_cb();
737         if (ret != WIFI_DIRECT_ERROR_NONE) {
738                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deregister _cb_connection(%d)\n", ret);
739         }
740
741         ret = wifi_direct_deinitialize();
742         if (ret != WIFI_DIRECT_ERROR_NONE) {
743                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deinitialize Wi-Fi Direct. error code = [%d]\n", ret);
744         }
745
746         __WFD_APP_FUNC_EXIT__;
747         return 0;
748 }