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