Fixed: Coding rule issues.
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-util.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 utils functions.
22  *
23  * @file    wfd-app-util.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <linux/unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <Elementary.h>
33 #include <vconf.h>
34 #include <app_control_internal.h>
35 #include <notification.h>
36 #include <notification_internal.h>
37 #include <notification_text_domain.h>
38 #include <bundle_internal.h>
39 #include <app_control.h>
40 #include <app.h>
41
42 #include <wifi-direct.h>
43
44 #include "wfd-app.h"
45 #include "wfd-app-util.h"
46
47 #define KEY_SELECT "XF86Home"
48
49 char *wfd_app_trim_path(const char *filewithpath)
50 {
51 #if 0
52         char *filename = NULL;
53         if ((filename = strrchr(filewithpath, '/')) == NULL)
54             return (char *) filewithpath;
55         else
56             return (filename + 1);
57 #else
58         static char *filename[100];
59         char *strptr = NULL;
60         int start = 0;
61         const char *space = "                                        ";
62         int len = strlen(filewithpath);
63
64         if (len > 20) {
65                 strptr = (char *) filewithpath + (len - 20);
66                 start = 0;
67         } else if (len < 20) {
68                 strptr = (char *) filewithpath;
69                 start = 20 - len;
70         }
71
72         strncpy((char *) filename, space, strlen(space));
73         strncpy((char *) filename + start, strptr, 50);
74
75         return (char *) filename;
76 #endif
77 }
78
79
80 int wfd_app_gettid()
81 {
82 #ifdef __NR_gettid
83         return syscall(__NR_gettid);
84 #else
85         fprintf(stderr, "__NR_gettid is not defined, please include linux/unistd.h ");
86         return -1;
87 #endif
88 }
89
90 static void __launch_app_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *data)
91 {
92         __WFD_APP_FUNC_ENTER__;
93
94         if (result == APP_CONTROL_RESULT_FAILED) {
95                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "User cancel to reconnect screen mirroring\n");
96 #ifdef WFD_SCREEN_MIRRORING_ENABLED
97                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
98 #endif
99         } else {
100                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control launch result: [%d]\n", result);
101         }
102
103         __WFD_APP_FUNC_EXIT__;
104 }
105
106 static void _move_data_to_app_control(const char *key, const int type,
107                 const bundle_keyval_t *kv, void *data)
108 {
109         __WFD_APP_FUNC_ENTER__;
110         void *ptr = NULL;
111         char *buff = NULL;
112         size_t size = 0;
113
114
115         WFD_RET_IF(data == NULL || key == NULL || type == 0, , "Invialid parameter!");
116
117         app_control_h control = data;
118
119         if (type == BUNDLE_TYPE_STR) {
120                 bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
121                 buff = malloc(sizeof(char) * size + 1);
122                 snprintf(buff, size + 1, "%s", ((char*) ptr));
123                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
124
125                 app_control_add_extra_data(control, key, buff);
126                 free(buff);
127         } else if (type == BUNDLE_TYPE_BYTE) {
128                 bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
129                 buff = malloc(sizeof(char) * size + 1);
130                 snprintf(buff, size + 1, "%s", ((char*) ptr));
131                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
132
133                 app_control_add_extra_data(control, key, buff);
134                 free(buff);
135         } else if (type == BUNDLE_TYPE_STR_ARRAY) {
136                 int i = 0;
137                 void ** array;
138                 unsigned int len = 0;
139                 size_t *element_size = NULL;
140                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR_ARRAY -KEY: %s", key);
141                 bundle_keyval_get_array_val((bundle_keyval_t *) kv, &array, &len, &element_size);
142                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "-Array len: %d", len);
143                 for (i = 0; i < len; i++) {
144                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "-%s", (char*)array[i]);
145
146                         app_control_add_extra_data(control, key, (char*)array[i]);
147                 }
148         }
149
150         __WFD_APP_FUNC_EXIT__;
151 }
152
153 static void _launch_app(char *app_id, void *data)
154 {
155         __WFD_APP_FUNC_ENTER__;
156         WFD_RET_IF(app_id == NULL || data == NULL, "Invialid parameter!");
157
158         int ret = APP_CONTROL_ERROR_NONE;
159         app_control_h control = NULL;
160         ret = app_control_create(&control);
161         if (ret != APP_CONTROL_ERROR_NONE) {
162                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_create() return error : %d", ret);
163                 return;
164         }
165         WFD_RET_IF(control == NULL, "Failed to create app_control handle!");
166
167         app_control_set_operation(control, APP_CONTROL_OPERATION_DEFAULT);
168         app_control_set_app_id(control, app_id);
169         bundle_foreach((bundle *)data, _move_data_to_app_control, control);
170
171         char *launch_type = NULL;
172         bundle_get_str(data, "-t", &launch_type);
173         if (!strcmp(launch_type, "reconnect_by_connecting_wifi_ap"))
174                 ret = app_control_send_launch_request(control, __launch_app_result_cb, NULL);
175         else
176                 ret = app_control_send_launch_request(control, NULL, NULL);
177
178         if (ret != APP_CONTROL_ERROR_NONE) {
179                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_send_launch_request() is failed : %d", ret);
180                 app_control_destroy(control);
181                 return;
182         }
183         app_control_destroy(control);
184
185         __WFD_APP_FUNC_EXIT__;
186         return;
187 }
188
189 #ifdef WFD_SCREEN_MIRRORING_ENABLED
190 void _add_screen_mirroring_activated_indicator(void *user_data)
191 {
192         __WFD_APP_FUNC_ENTER__;
193         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
194         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
195         WFD_RET_IF(ad->noti_screen_mirroring_on, "Indicator noti_screen_mirroring_on already exists");
196
197         if (ad->noti_screen_mirroring_play) {
198                 notification_delete(ad->noti_screen_mirroring_play);
199                 notification_free(ad->noti_screen_mirroring_play);
200                 ad->noti_screen_mirroring_play = NULL;
201         }
202
203         if (ad->noti_screen_mirroring_on != NULL) {
204                 noti_err = notification_free(ad->noti_screen_mirroring_on);
205                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
206         }
207
208         ad->noti_screen_mirroring_on = notification_create(NOTIFICATION_TYPE_ONGOING);
209         WFD_RET_IF(NULL == ad->noti_screen_mirroring_on, "NULL parameters.\n");
210
211         noti_err = notification_set_image(ad->noti_screen_mirroring_on, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, SCREEN_MIRRIONG_INDICATOR_ICON_PATH);
212         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
213
214         noti_err = notification_set_property(ad->noti_screen_mirroring_on, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
215         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_property. [%d]", noti_err);
216
217         noti_err = notification_set_display_applist(ad->noti_screen_mirroring_on, NOTIFICATION_DISPLAY_APP_INDICATOR);
218         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
219
220         /* notify the quick panel */
221         noti_err = notification_insert(ad->noti_screen_mirroring_on, NULL);
222         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
223
224         __WFD_APP_FUNC_EXIT__;
225         return;
226 }
227 #endif
228
229
230 #ifdef WFD_SCREEN_MIRRORING_ENABLED
231 /**
232  *      This function let the app add the notification when it is connected
233  *      @return   void
234  *      @param[in] user_data the pointer to the main data structure
235  */
236 void _add_wfd_peers_connected_notification(void *user_data, char* package_name)
237 {
238         __WFD_APP_FUNC_ENTER__;
239
240         int res = NOTIFICATION_ERROR_NONE;
241         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
242         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
243
244         WFD_RET_IF(NULL == ad || NULL == package_name, "NULL parameters.\n");
245
246         if (ad->noti_screen_mirroring_on) {
247                 notification_delete(ad->noti_screen_mirroring_on);
248                 notification_free(ad->noti_screen_mirroring_on);
249                 ad->noti_screen_mirroring_on = NULL;
250         }
251
252         if (ad->noti_screen_mirroring_play != NULL) {
253                 noti_err = notification_free(ad->noti_screen_mirroring_play);
254                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
255         }
256
257         ad->noti_screen_mirroring_play = notification_create(NOTIFICATION_TYPE_ONGOING);
258         WFD_RET_IF(NULL == ad->noti_screen_mirroring_play, "NULL parameters.\n");
259
260         char msg[WFD_MAX_SIZE] = {0};
261
262         bundle *b = NULL;
263         app_control_h control;
264         res = app_control_create(&control);
265         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
266
267         app_control_set_package(control, package_name);
268         app_control_add_extra_data(control, "-t", "notification");
269
270         res = notification_set_launch_option(ad->noti_screen_mirroring_play, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, control);
271         if (res != NOTIFICATION_ERROR_NONE) {
272                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to otification_set_launch_option. [%d]", res);
273                 app_control_destroy(control);
274                 return;
275         }
276
277         app_control_destroy(control);
278
279         noti_err = notification_set_layout(ad->noti_screen_mirroring_play, NOTIFICATION_LY_ONGOING_EVENT);
280         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_layout. [%d]", noti_err);
281
282         /* set the icon */
283         noti_err = notification_set_image(ad->noti_screen_mirroring_play, NOTIFICATION_IMAGE_TYPE_ICON, SCREEN_MIRRIONG_NOTI_ICON_PATH);
284         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
285
286         noti_err = notification_set_image(ad->noti_screen_mirroring_play, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, SCREEN_MIRRIONG_INDICATOR_PLAY_ICON_PATH);
287         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
288
289         /* set the title and content */
290         wfd_app_get_connected_peers(ad);
291         noti_err = notification_set_text(ad->noti_screen_mirroring_play, NOTIFICATION_TEXT_TYPE_TITLE, D_("IDS_SMR_BODY_SCREEN_MIRRORING_IS_ENABLED"),
292                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
293         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
294
295         snprintf(msg, WFD_MAX_SIZE, D_("IDS_WIFI_BODY_CONNECTED_TO_PS"), ad->raw_connected_peers[0].ssid);
296         noti_err = notification_set_text(ad->noti_screen_mirroring_play, NOTIFICATION_TEXT_TYPE_CONTENT,
297                         msg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
298         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
299
300
301         notification_set_property(ad->noti_screen_mirroring_play, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
302
303         /* notify the quick panel */
304         noti_err = notification_insert(ad->noti_screen_mirroring_play, NULL);
305         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
306
307         __WFD_APP_FUNC_EXIT__;
308         return;
309 }
310 #endif
311
312 /**
313  *      This function let the app make a change callback for flight mode
314  *      @return   void
315  *      @param[in] key the pointer to the key
316  *      @param[in] user_data the pointer to the main data structure
317  */
318 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
319 {
320         __WFD_APP_FUNC_ENTER__;
321         int res = -1;
322         int flight_mode = 0;
323 #ifdef WFD_SCREEN_MIRRORING_ENABLED
324         int screen_mirroring_status = 0;
325 #endif
326         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
327         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
328
329         res = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode);
330         if (res != 0) {
331                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get flight state from vconf. [%d]\n", res);
332                 return;
333         }
334
335         if (flight_mode == FALSE) {
336                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Flight mode is off\n");
337                 return;
338         }
339
340         /* If flight mode is on, turn off WFD */
341         wifi_direct_get_state(&ad->wfd_status);
342         if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
343                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi Direct is deactivated.\n");
344                 return;
345         }
346
347         /* If connected, disconnect all devices*/
348         if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
349                 res = wifi_direct_disconnect_all();
350                 if (res != WIFI_DIRECT_ERROR_NONE) {
351                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
352                         return;
353                 }
354         }
355
356         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Deactivating WiFi DIrect..."
357                 "Due to Flight Mode is Enabled\n");
358         res = wifi_direct_deactivate();
359         if (res != WIFI_DIRECT_ERROR_NONE) {
360                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
361                 return;
362         }
363
364 #ifdef WFD_SCREEN_MIRRORING_ENABLED
365         /* checking Screen Mirroring */
366         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
367                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
368
369         WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
370
371         if (screen_mirroring_status > VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) {
372                 res = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
373                 if (res < 0)
374                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
375         }
376 #endif
377
378         __WFD_APP_FUNC_EXIT__;
379 }
380
381 static void _wfd_cpu_limit_mode_changed(keynode_t *node, void *user_data)
382 {
383         __WFD_APP_FUNC_ENTER__;
384         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
385         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
386         int power_mode = 0;
387         int screen_mirroring_status = 0;
388         int cup_limit_mode = 0;
389
390         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
391                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
392                 return;
393         }
394
395         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
396                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
397                 return;
398         }
399
400         if (vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
401                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
402                 return;
403         }
404
405         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0) {
406                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
407                 return;
408         }
409
410         if ((power_mode == SETTING_PSMODE_POWERFUL || power_mode == SETTING_PSMODE_ADVISOR) &&
411                                 cup_limit_mode){
412                 bundle *b = NULL;
413                 b = bundle_create();
414                 bundle_add_str(b, "-t", "notification_power_saving_on");
415                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
416                 bundle_free(b);
417         }
418
419         __WFD_APP_FUNC_EXIT__;
420         return;
421 }
422
423 static void _wfd_power_saving_mode_changed(keynode_t *node, void *user_data)
424 {
425         __WFD_APP_FUNC_ENTER__;
426         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
427         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
428         int power_mode = 0;
429         int screen_mirroring_status = 0;
430         int cup_limit_mode = 0;
431
432         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
433                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
434                 return;
435         }
436
437         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
438                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
439                 return;
440         }
441
442         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0) {
443                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
444                 return;
445         }
446
447         if (power_mode == SETTING_PSMODE_SURVIVAL ||
448                         power_mode == SETTING_PSMODE_EMERGENCY) {
449                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Ultra power saving mode on\n");
450                 bundle *b = NULL;
451                 b = bundle_create();
452                 bundle_add_str(b, "-t", "quit_by_ultra_power_saving_on");
453
454                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
455                 bundle_free(b);
456         } else if (power_mode == SETTING_PSMODE_POWERFUL ||
457                         power_mode == SETTING_PSMODE_ADVISOR){
458                         if (vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
459                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
460                                 return;
461                         }
462
463                         if (cup_limit_mode) {
464                                 bundle *b = NULL;
465                                 b = bundle_create();
466                                 bundle_add_str(b, "-t", "notification_power_saving_on");
467                                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
468                                 bundle_free(b);
469                         }
470         }
471
472         __WFD_APP_FUNC_EXIT__;
473         return;
474 }
475
476 static void _wfd_wifi_status_changed(keynode_t *node, void *user_data)
477 {
478         __WFD_APP_FUNC_ENTER__;
479         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
480         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
481         int wifi_status = 0;
482 #ifdef WFD_SCREEN_MIRRORING_ENABLED
483         int screen_mirroring_status = 0;
484
485         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
486                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
487                 return;
488         }
489
490         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
491                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
492                 return;
493         }
494 #endif
495
496         if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_status) < 0) {
497                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
498                 return;
499         }
500
501         WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi state is [%d]\n", wifi_status);
502         if (wifi_status == VCONFKEY_WIFI_CONNECTED) {
503                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi is connected\n");
504 #ifdef WFD_SCREEN_MIRRORING_ENABLED
505                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_DISCONNECT_BY_RECONNECT_WIFI_AP;
506                 bundle *b = NULL;
507                 b = bundle_create();
508                 bundle_add_str(b, "-t", "reconnect_by_connecting_wifi_ap");
509
510                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
511                 bundle_free(b);
512 #endif
513         } else if (VCONFKEY_WIFI_OFF == wifi_status) {
514                 /* Deactivate WiFi Direct */
515                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Deactivate WiFi Direct...");
516                 wfd_destroy_popup();
517                 /*
518                  * Currently, WiFi Direct OFF is handled at net-config.
519                  * Also, this patch is added to support ON-DEMAND launch destroy popup.
520                  * This patch will handle 5sec deadlock of popup destroy from
521                  * wfd-manager.
522                  */
523                 /* wfd_app_client_switch_off(ad); */
524         } else {
525                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi state is [%d]\n", wifi_status);
526         }
527
528         __WFD_APP_FUNC_EXIT__;
529         return;
530 }
531
532 #ifdef WFD_SCREEN_MIRRORING_ENABLED
533 /**
534  *      This function let the app make a change callback for allshare cast
535  *      @return   void
536  *      @param[in] key the pointer to the key
537  *      @param[in] user_data the pointer to the main data structure
538  */
539 static void _wfd_allshare_cast_status_changed(keynode_t *node, void *user_data)
540 {
541         __WFD_APP_FUNC_ENTER__;
542
543         int screen_mirroring_status = 0;
544         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
545         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
546
547         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
548                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
549
550         WFD_APP_LOG(WFD_APP_LOG_ERROR, "VCONFKEY_SCREEN_MIRRORING_STATE:%d\n", screen_mirroring_status);
551
552         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
553                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is connected\n");
554                 if (ad->transmit_timer) {
555                         ecore_timer_del(ad->transmit_timer);
556                         ad->transmit_timer = NULL;
557                 }
558                 /* add connected notification */
559                 _add_wfd_peers_connected_notification(ad, PACKAGE_ALLSHARE_CAST);
560         } else if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_ACTIVATED) {
561                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is ACTIVATED\n");
562                 _add_screen_mirroring_activated_indicator(ad);
563         } else {
564                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
565                 if (ad->noti_screen_mirroring_on) {
566                         notification_delete(ad->noti_screen_mirroring_on);
567                         notification_free(ad->noti_screen_mirroring_on);
568                         ad->noti_screen_mirroring_on = NULL;
569                 }
570
571                 if (ad->noti_screen_mirroring_play) {
572                         notification_delete(ad->noti_screen_mirroring_play);
573                         notification_free(ad->noti_screen_mirroring_play);
574                         ad->noti_screen_mirroring_play = NULL;
575                 }
576         }
577
578         return;
579
580         __WFD_APP_FUNC_EXIT__;
581 }
582 #endif
583
584 static Eina_Bool _wfd_hard_key_down_cb(void *data, int type, void *event)
585 {
586         wfd_appdata_t *ad = (wfd_appdata_t *)data;
587         Ecore_Event_Key *ev = (Ecore_Event_Key *)event;
588         int res = 0;
589
590         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Hard Key Pressed CB...");
591         if (NULL == ad || NULL == ev) {
592                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
593                 return EINA_FALSE;
594         }
595
596         if (!strcmp(ev->keyname, "XF86Home")) {
597                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "[KEY]KEY_SELECT pressed");
598                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "Mac : %s", ad->mac_addr_connecting);
599
600                 if (strnlen(ad->mac_addr_connecting, MACSTR_LENGTH) > 0) {
601                         res = wifi_direct_reject_connection(ad->mac_addr_connecting);
602                         if (res != WIFI_DIRECT_ERROR_NONE)
603                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to reject connection(%d)", res);
604                 }
605                 memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
606                 wfd_destroy_popup();
607         } else {
608                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "[KEY][%s] pressed not Handled",
609                         ev->keyname);
610         }
611
612         return EINA_FALSE;
613 }
614
615 int wfd_app_util_register_hard_key_down_cb(void *data)
616 {
617         wfd_appdata_t *ad = (wfd_appdata_t *)data;
618
619         if (NULL == ad) {
620                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
621                 return -1;
622         }
623
624         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Register hard key down press CB !!!");
625         if (NULL == ad->downkey_handler)
626                 ad->downkey_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
627                 _wfd_hard_key_down_cb, ad);
628
629         return 0;
630 }
631
632 int wfd_app_util_deregister_hard_key_down_cb(void *data)
633 {
634         wfd_appdata_t *ad = (wfd_appdata_t *)data;
635
636         if (NULL == ad) {
637                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
638                 return -1;
639         }
640         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Deregister hard key down press CB !!!");
641         if (NULL != ad->downkey_handler) {
642                 ecore_event_handler_del(ad->downkey_handler);
643                 ad->downkey_handler = NULL;
644         }
645         return 0;
646 }
647
648 int wfd_app_util_register_vconf_callbacks(void *data)
649 {
650         wfd_appdata_t *ad = NULL;
651         int ret = 0;
652
653         if (!data) {
654                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid parameter");
655                 return -1;
656         }
657
658         ad = data;
659
660         /* register flight mode */
661         ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
662                                                         _wfd_flight_mode_changed, ad);
663         if (ret < 0) {
664                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for flight mode\n");
665                 return -1;
666         }
667
668 #ifdef WFD_SCREEN_MIRRORING_ENABLED
669         /* allshare cast */
670         /* TODO: Make proper changes for vconfkey */
671         ret = vconf_notify_key_changed(VCONFKEY_SCREEN_MIRRORING_STATE,
672                                                         _wfd_allshare_cast_status_changed, ad);
673         if (ret < 0) {
674                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for allshare cast\n");
675                 return -1;
676         }
677 #endif
678
679         /* wifi */
680         ret = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wfd_wifi_status_changed, ad);
681         if (ret < 0) {
682                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for wifi\n");
683                 return -1;
684         }
685
686         /* power mode */
687         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, _wfd_power_saving_mode_changed, ad);
688         if (ret < 0) {
689                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for power mode\n");
690                 return -1;
691         }
692
693         /* cpu limit mode */
694         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, _wfd_cpu_limit_mode_changed, ad);
695         if (ret < 0) {
696                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for cpu limit mode\n");
697                 return -1;
698         }
699
700         return 0;
701 }
702
703 int wfd_app_util_deregister_vconf_callbacks(void *data)
704 {
705         int ret = 0;
706
707         /* remove callback for flight mode */
708         ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE, _wfd_flight_mode_changed);
709         if (ret == -1)
710                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for flight mode\n");
711
712 #ifdef WFD_SCREEN_MIRRORING_ENABLED
713         /* remove callback for allshare cast */
714         ret = vconf_ignore_key_changed(VCONFKEY_SCREEN_MIRRORING_STATE, _wfd_allshare_cast_status_changed);
715         if (ret == -1)
716                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for allshare cast\n");
717 #endif
718
719         /* remove callback for wifi */
720         ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_wifi_status_changed);
721         if (ret == -1)
722                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for wifi\n");
723
724         /* remove callback for power mode */
725         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, _wfd_power_saving_mode_changed);
726         if (ret == -1)
727                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for power mode\n");
728
729         /* remove callback for cpu limit mode */
730         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, _wfd_cpu_limit_mode_changed);
731         if (ret == -1)
732                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for cpu limit mode\n");
733
734         return 0;
735 }
736
737 /**
738  *      This function let the app delete the notification
739  *      @return   void
740  */
741 void wfd_app_util_del_notification(wfd_appdata_t *ad)
742 {
743         __WFD_APP_FUNC_ENTER__;
744         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
745
746         /* delete the notification */
747         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
748
749         noti_err  = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
750         if (noti_err != NOTIFICATION_ERROR_NONE) {
751                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete_all.(%d)\n", noti_err);
752                 return;
753         }
754
755 #ifdef WFD_SCREEN_MIRRORING_ENABLED
756         if (ad->noti_screen_mirroring_on) {
757                 noti_err = notification_free(ad->noti_screen_mirroring_on);
758                 ad->noti_screen_mirroring_on = NULL;
759                 if (noti_err != NOTIFICATION_ERROR_NONE)
760                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
761         }
762
763         if (ad->noti_screen_mirroring_play) {
764                 noti_err = notification_free(ad->noti_screen_mirroring_play);
765                 ad->noti_screen_mirroring_play = NULL;
766                 if (noti_err != NOTIFICATION_ERROR_NONE)
767                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
768         }
769 #endif
770
771 #ifdef NOT_CONNECTED_INDICATOR_ICON
772         if (ad->noti_wifi_direct_on) {
773                 noti_err = notification_free(ad->noti_wifi_direct_on);
774                 ad->noti_wifi_direct_on = NULL;
775                 if (noti_err != NOTIFICATION_ERROR_NONE)
776                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
777         }
778 #endif
779
780         if (ad->noti_wifi_direct_connected) {
781                 noti_err = notification_free(ad->noti_wifi_direct_connected);
782                 ad->noti_wifi_direct_connected = NULL;
783                 if (noti_err != NOTIFICATION_ERROR_NONE)
784                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
785         }
786
787         __WFD_APP_FUNC_EXIT__;
788         return;
789 }
790
791 #ifdef NOT_CONNECTED_INDICATOR_ICON
792 /**
793  *      This function let the app add the indicator icon when wfd is activated
794  *      @return   void
795  *      @param[in] user_data the pointer to the main data structure
796  */
797 void wfd_app_util_add_indicator_icon(void *user_data)
798 {
799         __WFD_APP_FUNC_ENTER__;
800         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
801         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
802         WFD_RET_IF(ad->noti_wifi_direct_on, "Indicator already exists");
803
804         if (ad->noti_wifi_direct_on != NULL) {
805                 noti_err = notification_free(ad->noti_wifi_direct_on);
806                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
807         }
808
809         ad->noti_wifi_direct_on = notification_create(NOTIFICATION_TYPE_ONGOING);
810         WFD_RET_IF(NULL == ad->noti_wifi_direct_on, "NULL parameters.\n");
811
812         noti_err = notification_set_image(ad->noti_wifi_direct_on, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, WFD_ACTIVATED_NOTI_ICON_PATH);
813         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
814
815         noti_err = notification_set_property(ad->noti_wifi_direct_on, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
816         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_property. [%d]", noti_err);
817
818         noti_err = notification_set_display_applist(ad->noti_wifi_direct_on, NOTIFICATION_DISPLAY_APP_INDICATOR);
819         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
820
821         /* notify the quick panel */
822         noti_err = notification_insert(ad->noti_wifi_direct_on, NULL);
823         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
824
825         __WFD_APP_FUNC_EXIT__;
826         return;
827 }
828 #endif
829
830 #ifdef WFD_SCREEN_MIRRORING_ENABLED
831 /**
832  *      This function let the app set VCONFKEY_SCREEN_MIRRORING_STATE to be DEACTIVATED
833 */
834 void wfd_app_util_set_screen_mirroring_deactivated(wfd_appdata_t *ad)
835 {
836         __WFD_APP_FUNC_ENTER__;
837         WFD_RET_IF(NULL == ad, "NULL == ad!\n");
838         int screen_mirroring_status = -1;
839         int result = -1;
840
841         /* Reconnect by ap connected, no need to set vconf to DEACTIVATED, allshare cast itself will set ACTIVATED*/
842         if (ad->screen_mirroring_state == WFD_POP_SCREEN_MIRROR_DISCONNECT_BY_RECONNECT_WIFI_AP) {
843                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
844                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Reconnect screen mirroring by app connected.\n");
845                 return;
846         }
847
848         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
849                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
850
851         WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
852         /* Set the vconf value to DEACTIVATED only when the previous vconf value is CONNECTED.
853         If the previous vconf value is ACTIVATED, it means that the Screen Mirroring UG changed that key already. So no need to change it. */
854         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
855                 result = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
856                 if (result < 0)
857                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
858
859                 notification_status_message_post(D_("IDS_SMR_POP_SCREEN_MIRRORING_HAS_BEEN_DISABLED"));
860         }
861
862         __WFD_APP_FUNC_EXIT__;
863 }
864 #endif
865 /**
866  *      This function let the app add the notification when it shoule be turned off
867  *      @return   void
868  *      @param[in] user_data the pointer to the main data structure
869  */
870 void wfd_app_util_add_wfd_turn_off_notification(void *user_data)
871 {
872         __WFD_APP_FUNC_ENTER__;
873
874         int res = NOTIFICATION_ERROR_NONE;
875         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
876         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
877
878         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
879
880         /* delete all notifications */
881         wfd_app_util_del_notification(ad);
882
883         if (ad->noti_wifi_direct_connected != NULL) {
884                 noti_err = notification_free(ad->noti_wifi_direct_connected);
885                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
886         }
887
888         ad->noti_wifi_direct_connected = (notification_h) notification_create(NOTIFICATION_TYPE_ONGOING);
889         WFD_RET_IF(NULL == ad->noti_wifi_direct_connected, "NULL parameters.\n");
890
891         app_control_h control;
892         res = app_control_create(&control);
893         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
894
895         app_control_set_app_id(control, PACKAGE);
896         app_control_add_extra_data(control, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
897
898         noti_err = notification_set_launch_option(ad->noti_wifi_direct_connected, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, control);
899         if (noti_err != NOTIFICATION_ERROR_NONE) {
900                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to notification_set_launch_option. [%d]", noti_err);
901                 app_control_destroy(control);
902                 return;
903         }
904
905         app_control_destroy(control);
906
907         noti_err = notification_set_layout(ad->noti_wifi_direct_connected, NOTIFICATION_LY_ONGOING_EVENT);
908         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_layout. [%d]", noti_err);
909
910         /* set the icon */
911         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Icon Path: %s\n", WFD_NOTI_ICON_PATH);
912         noti_err = notification_set_image(ad->noti_wifi_direct_connected, NOTIFICATION_IMAGE_TYPE_ICON, WFD_NOTI_ICON_PATH);
913         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
914
915         noti_err = notification_set_display_applist(ad->noti_wifi_direct_connected, NOTIFICATION_DISPLAY_APP_TICKER | NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
916         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
917
918         noti_err = notification_set_text_domain(ad->noti_wifi_direct_connected, LOCALE_FILE_NAME, LOCALEDIR);
919         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text_domain. [%d]", noti_err);
920
921         /* set the title and content */
922         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_TITLE,
923                 D_("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"), "IDS_WIFI_BODY_WI_FI_DIRECT_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
924         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
925
926         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_CONTENT,
927                 D_("IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB"),
928                 "IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
929         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
930
931         /* notify the quick panel */
932         noti_err = notification_insert(ad->noti_wifi_direct_connected, NULL);
933         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
934
935         __WFD_APP_FUNC_EXIT__;
936         return;
937
938 }
939
940 void wfd_app_util_del_wfd_connected_notification(wfd_appdata_t *ad)
941 {
942         __WFD_APP_FUNC_ENTER__;
943         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
944
945         /* delete the notification */
946         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
947
948         noti_err  = notification_delete(ad->noti_wifi_direct_connected);
949         if (noti_err != NOTIFICATION_ERROR_NONE) {
950                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
951                 return;
952         }
953
954         if (ad->noti_wifi_direct_connected) {
955                 noti_err = notification_free(ad->noti_wifi_direct_connected);
956                 ad->noti_wifi_direct_connected = NULL;
957                 if (noti_err != NOTIFICATION_ERROR_NONE)
958                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
959         }
960
961         __WFD_APP_FUNC_EXIT__;
962         return;
963 }