4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.tizenopensource.org/license
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.
22 #define UG_MODULE_API __attribute__ ((visibility("default")))
28 #include <sys/utsname.h>
31 #include <Elementary.h>
32 #include <ui-gadget-module.h>
33 #include <app_control.h>
36 #include "wfd_ug_view.h"
37 #include "wfd_client.h"
38 #ifdef MOTION_CONTROL_ENABLE
39 #include "wfd_motion_control.h"
42 void initialize_gen_item_class();
44 struct ug_data *global_ugd = NULL;
46 struct ug_data *wfd_get_ug_data()
51 static void __wfd_main_vconf_change_cb(keynode_t *key, void *data)
54 WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
55 WFD_RET_IF(NULL == data, "ERROR : data is NULL");
56 struct ug_data *ugd = (struct ug_data *) data;
58 char *vconf_name = vconf_keynode_get_name(key);
60 if (!g_strcmp0(vconf_name, VCONFKEY_SETAPPL_DEVICE_NAME_STR)) {
61 char *name_value = NULL;
62 name_value = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
63 WFD_RET_IF(!name_value, "Get string is failed");
64 DBG(LOG_INFO, "name : %s", name_value);
66 if (ugd->device_name_item && g_strcmp0(ugd->dev_name, name_value))
67 wfd_ug_view_refresh_glitem(ugd->device_name_item);
71 DBG(LOG_ERROR, "vconf_name is error");
77 static void _wifi_on_state_cb(keynode_t *key, void *data)
79 WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
80 WFD_RET_IF(NULL == data, "ERROR : data is NULL");
82 struct ug_data *ugd = (struct ug_data *)data;
85 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
86 DBG(LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
90 DBG(LOG_INFO, "WiFi State [%d]\n", wifi_state);
91 if (wifi_state > VCONFKEY_WIFI_OFF && wifi_state < VCONFKEY_WIFI_STATE_MAX) {
92 if (WIFI_DIRECT_ERROR_NONE != wifi_direct_get_state(&ugd->wfd_status)) {
93 DBG(LOG_ERROR, "Failed to Get WiFi Direct State");
96 if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
97 DBG(LOG_INFO, "Activate WiFi Direct...");
98 if (FALSE != wfd_client_switch_on(ugd))
99 DBG(LOG_ERROR, "Failed to Activate WiFi Direct");
106 static void __wfd_hotspot_mode_vconf_change_cb(keynode_t *key, void *data)
108 DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb");
109 if (NULL == key || NULL == data) {
110 DBG(LOG_INFO, "Invalid parameters \n");
113 struct ug_data *ugd = (struct ug_data *) data;
114 int hotspot_mode = 0;
117 res = vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &hotspot_mode);
119 DBG(LOG_INFO, "Failed to get vconf value for PLMN(%d)", res);
122 DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb mode %d", hotspot_mode);
124 if (VCONFKEY_MOBILE_HOTSPOT_MODE_NONE == hotspot_mode) {
125 if (NULL != ugd->act_popup)
126 evas_object_del(ugd->act_popup);
128 #ifdef WFD_ON_OFF_GENLIST
129 wfd_ug_refresh_on_off_check(ugd);
131 } else if (hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI ||
132 hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP) {
133 if (NULL != ugd->warn_popup) {
134 evas_object_del(ugd->warn_popup);
135 ugd->warn_popup = NULL;
142 * This function let the ug create backgroud
144 * @param[in] ugd the pointer to the parent object
145 * @param[in] ugd the pointer to the main data structure
147 static Evas_Object *_create_bg(Evas_Object *parent, char *style)
152 bg = elm_bg_add(parent);
153 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
154 elm_object_style_set(bg, style);
155 evas_object_show(bg);
162 * This function let the ug create full view
164 * @param[in] ugd the pointer to the parent object
165 * @param[in] ugd the pointer to the main data structure
167 static Evas_Object *_create_fullview(Evas_Object *parent, struct ug_data *ugd)
172 if (parent == NULL) {
173 DBG(LOG_ERROR, "Incorrenct parameter");
177 /* Create Full view */
178 base = elm_layout_add(parent);
180 DBG(LOG_ERROR, "Failed to add layout");
184 elm_layout_theme_set(base, "layout", "application", "default");
185 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186 evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
193 * This function let the ug create frame view
195 * @param[in] ugd the pointer to the parent object
196 * @param[in] ugd the pointer to the main data structure
198 static Evas_Object *_create_frameview(Evas_Object *parent, struct ug_data *ugd)
203 if (parent == NULL) {
204 DBG(LOG_ERROR, "Incorrenct parameter");
208 /* Create Frame view */
209 base = elm_layout_add(parent);
211 DBG(LOG_ERROR, "Failed to add layout");
215 elm_layout_theme_set(base, "layout", "application", "default");
216 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
217 evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
224 * This function let the ug destroy the ug
226 * @param[in] data the pointer to the main data structure
228 void wfd_destroy_ug(void *data)
231 struct ug_data *ugd = (struct ug_data *) data;
233 DBG(LOG_INFO, "dbus_cancellable is NULL");
234 ug_destroy_me(ugd->ug);
240 static void wfd_ug_layout_del_cb(void *data , Evas *e, Evas_Object *obj, void *event_info)
244 struct ug_data *ugd = (struct ug_data *) data;
246 DBG(LOG_ERROR, "Incorrect parameter(NULL)");
250 wfd_client_free_raw_discovered_peers(ugd);
251 wfd_ug_view_free_peers(ugd);
252 destroy_wfd_ug_view(ugd);
257 static void *on_create(ui_gadget_h ug, enum ug_mode mode, app_control_h control, void *priv)
270 bindtextdomain(PACKAGE, LOCALEDIR);
272 ugd->win = ug_get_window();
277 if (elm_win_wm_rotation_supported_get(ugd->win)) {
278 int rots[4] = {0, 90, 180, 270};
279 elm_win_wm_rotation_available_rotations_set(ugd->win, (const int *)(&rots), 4);
282 /* check the input parameters from app at first */
284 ugd->device_filter = -1; /* show all devices */
285 ugd->is_auto_exit = false;
286 ugd->is_multi_connect = true;
287 ugd->is_init_ok = false;
288 ugd->title = strdup(D_("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"));
292 char *device_filter = NULL;
293 char *auto_exit = NULL;
294 char *multi_connect = NULL;
296 char* viewtype = NULL;
299 * get the control name
300 * default value: Wi-Fi Direct
302 ret = app_control_get_extra_data(control, "wfds", &wfds);
303 if (ret == APP_CONTROL_ERROR_NONE && wfds) {
304 DBG_SECURE(LOG_INFO, "Wfds name: %s", wfds);
305 ugd->wfds = strdup(wfds);
306 WFD_IF_FREE_MEM(wfds);
309 ret = app_control_get_extra_data(control, "viewtype", &viewtype);
310 if (ret == APP_CONTROL_ERROR_NONE && viewtype) {
311 DBG(LOG_INFO, "viewtype: %s\n", viewtype);
312 ugd->view_type = strdup(viewtype);
313 WFD_IF_FREE_MEM(viewtype);
317 * get the device filter
318 * default value: NULL
320 ret = app_control_get_extra_data(control, "device_filter", &device_filter);
321 if (ret == APP_CONTROL_ERROR_NONE && device_filter) {
322 DBG(LOG_INFO, "Device filter: %s", device_filter);
323 if (0 == strncmp(device_filter, "computer", 8))
324 ugd->device_filter = WFD_DEVICE_TYPE_COMPUTER;
325 else if (0 == strncmp(device_filter, "input_device", 12))
326 ugd->device_filter = WFD_DEVICE_TYPE_INPUT_DEVICE;
327 else if (0 == strncmp(device_filter, "printer", 6))
328 ugd->device_filter = WFD_DEVICE_TYPE_PRINTER;
329 else if (0 == strncmp(device_filter, "camera", 6))
330 ugd->device_filter = WFD_DEVICE_TYPE_CAMERA;
331 else if (0 == strncmp(device_filter, "storage", 7))
332 ugd->device_filter = WFD_DEVICE_TYPE_STORAGE;
333 else if (0 == strncmp(device_filter, "network_infra", 13))
334 ugd->device_filter = WFD_DEVICE_TYPE_NW_INFRA;
335 else if (0 == strncmp(device_filter, "display", 7))
336 ugd->device_filter = WFD_DEVICE_TYPE_DISPLAYS;
337 else if (0 == strncmp(device_filter, "multimedia_device", 17))
338 ugd->device_filter = WFD_DEVICE_TYPE_MM_DEVICES;
339 else if (0 == strncmp(device_filter, "game_device", 11))
340 ugd->device_filter = WFD_DEVICE_TYPE_GAME_DEVICES;
341 else if (0 == strncmp(device_filter, "telephone", 9))
342 ugd->device_filter = WFD_DEVICE_TYPE_TELEPHONE;
343 else if (0 == strncmp(device_filter, "audio", 5))
344 ugd->device_filter = WFD_DEVICE_TYPE_AUDIO;
346 ugd->device_filter = WFD_DEVICE_TYPE_OTHER;
348 WFD_IF_FREE_MEM(device_filter);
352 * get whether support auto exit after connection
353 * default value: false
355 ret = app_control_get_extra_data(control, "auto_exit", &auto_exit);
356 if (ret == APP_CONTROL_ERROR_NONE && auto_exit) {
357 DBG(LOG_INFO, "Auto exit: %s", auto_exit);
358 if (0 == strncmp(auto_exit, "on", 2))
359 ugd->is_auto_exit = true;
361 ugd->is_auto_exit = false;
363 WFD_IF_FREE_MEM(auto_exit);
367 * get whether support multi connection,
368 * default value: true
370 ret = app_control_get_extra_data(control, "multi_connect", &multi_connect);
371 if (ret == APP_CONTROL_ERROR_NONE && multi_connect) {
372 DBG(LOG_INFO, "Multi connection: %s", multi_connect);
373 if (0 == strncmp(multi_connect, "off", 2))
374 ugd->is_multi_connect = false;
376 ugd->is_multi_connect = true;
378 WFD_IF_FREE_MEM(multi_connect);
382 * get the title of UG
383 * default value: Wi-Fi Direct
385 ret = app_control_get_extra_data(control, "title_string", &title);
386 if (ret == APP_CONTROL_ERROR_NONE && title) {
387 DBG(LOG_INFO, "Title of UG: %s", title);
388 WFD_IF_FREE_MEM(ugd->title);
389 ugd->title = strdup(title);
390 WFD_IF_FREE_MEM(title);
394 if (mode == UG_MODE_FULLVIEW)
395 ugd->base = _create_fullview(ugd->win, ugd);
397 ugd->base = _create_frameview(ugd->win, ugd);
400 evas_object_event_callback_add(ugd->base, EVAS_CALLBACK_DEL, wfd_ug_layout_del_cb, ugd);
401 ugd->bg = _create_bg(ugd->win, "group_list");
402 elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
404 DBG(LOG_ERROR, "Failed to create base layout\n");
408 /* check status of wifi-direct from vconf */
409 wfd_status = wfd_get_vconf_status();
414 initialize_gen_item_class();
415 create_wfd_ug_view(ugd);
416 wfd_ug_view_init_genlist(ugd, true);
417 #ifdef MOTION_CONTROL_ENABLE
422 * if not deactivated, do initialization at once;
423 * otherwise, do initialization later
425 if (wfd_status != VCONFKEY_WIFI_DIRECT_DEACTIVATED) {
426 init_wfd_client(ugd);
428 ugd->wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
430 ret = init_wfd_client(ugd);
431 WFD_RETV_IF(ret != 0, NULL, "Failed to initialize WFD client library\n");
433 /* Activate WiFi Direct */
434 DBG(LOG_INFO, "Activating WiFi Direct...");
435 if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
436 ret = wfd_client_switch_on(ugd);
437 WFD_RETV_IF(ret != 0, NULL, "Failed to activate WFD\n");
440 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
441 __wfd_main_vconf_change_cb, ugd);
443 DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_SETAPPL_DEVICE_NAME_STR)");
446 ret = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb, ugd);
448 DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_WIFI_STATE)");
451 ret = vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
452 __wfd_hotspot_mode_vconf_change_cb, ugd);
454 DBG(LOG_ERROR, "Failed to set vconf notification callback(MOBILE_HOTSPOT_MODE)");
460 static void on_start(ui_gadget_h ug, app_control_h control, void *priv)
471 struct utsname kernel_info;
472 res = uname(&kernel_info);
474 DBG(LOG_ERROR, "Failed to detect target type\n");
476 DBG_SECURE(LOG_INFO, "HW ID of this device [%s]\n", kernel_info.machine);
477 if (strncmp(kernel_info.machine, "arm", 3) != 0) {
478 wfd_ug_warn_popup(ugd, D_("IDS_ST_POP_NOT_SUPPORTED"), POPUP_TYPE_TERMINATE_NOT_SUPPORT);
486 static void on_pause(ui_gadget_h ug, app_control_h control, void *priv)
490 WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
491 struct ug_data *ugd = priv;
492 ugd->is_paused = true;
494 wfd_refresh_wifi_direct_state(ugd);
495 DBG(LOG_INFO, "on pause, wfd status: %d\n", ugd->wfd_status);
497 if ((WIFI_DIRECT_STATE_DISCOVERING == ugd->wfd_status) &&
498 (WIFI_DIRECT_ERROR_NONE != wifi_direct_cancel_discovery())) {
499 DBG(LOG_ERROR, "Failed to send cancel discovery state [%d]\n", ugd->wfd_status);
504 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;
509 static void on_resume(ui_gadget_h ug, app_control_h control, void *priv)
513 WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
514 struct ug_data *ugd = priv;
515 ugd->is_paused = false;
517 wfd_refresh_wifi_direct_state(ugd);
518 DBG(LOG_INFO, "on resume, status: %d\n", ugd->wfd_status);
519 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;
521 elm_genlist_realized_items_update(ugd->genlist);
523 if (ugd->wfd_status > WIFI_DIRECT_STATE_DEACTIVATED && ugd->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
524 DBG(LOG_INFO, "Start discovery again\n");
525 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL_START;
526 ret = wifi_direct_start_discovery_specific_channel(false, 1, WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL);
527 if (ret != WIFI_DIRECT_ERROR_NONE) {
528 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_NONE;
529 DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", ret);
530 wifi_direct_cancel_discovery();
537 static void on_destroy(ui_gadget_h ug, app_control_h control, void *priv)
541 WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
543 struct ug_data *ugd = priv;
545 WFD_RET_IF(ugd->base == NULL, "The param is NULL\n");
547 #ifdef MOTION_CONTROL_ENABLE
551 /* DeInit WiFi Direct */
552 ret = deinit_wfd_client(ugd);
554 DBG(LOG_ERROR, "Failed to DeInit WiFi Direct");
556 if (ugd->scan_toolbar) {
557 wfd_ug_view_refresh_button(ugd->scan_toolbar,
558 D_("IDS_WIFI_SK4_SCAN"), FALSE);
561 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
562 __wfd_main_vconf_change_cb);
564 DBG(LOG_ERROR, "Failed to ignore vconf key callback\n");
567 ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb);
569 DBG(LOG_ERROR, "Failed to ignore vconf key callback\n");
572 ret = vconf_ignore_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
573 __wfd_hotspot_mode_vconf_change_cb);
575 DBG(LOG_ERROR, "Failed to ignore vconf key callback MOBILE_HOTSPOT_MODE\n");
577 wfd_ug_view_free_peers(ugd);
578 WFD_IF_FREE_MEM(ugd->title);
579 WFD_IF_FREE_MEM(ugd->wfds);
580 WFD_IF_FREE_MEM(ugd->view_type);
582 WFD_IF_DEL_OBJ(ugd->bg);
583 WFD_IF_DEL_OBJ(ugd->base);
584 DBG(LOG_INFO, "WFD client deregistered");
590 static void on_message(ui_gadget_h ug, app_control_h msg, app_control_h control, void *priv)
593 char* app_msg = NULL;
594 struct ug_data *ugd = priv;
597 DBG(LOG_ERROR, "The param is NULL\n");
602 app_control_get_extra_data(msg, "msg", &app_msg);
604 DBG(LOG_ERROR, "Failed to get app_msg");
609 DBG(LOG_DEBUG, "Msg from app: %s", app_msg);
610 if (!strcmp(app_msg, "destroy")) {
611 if (!ugd->rename_popup) {
612 DBG(LOG_INFO, "Destroying UG.");
613 wfd_ug_view_free_peers(ugd);
617 WFD_IF_FREE_MEM(app_msg);
622 static void on_event(ui_gadget_h ug, enum ug_event event, app_control_h control, void *priv)
627 DBG(LOG_ERROR, "The param is NULL\n");
632 case UG_EVENT_LOW_MEMORY:
633 DBG(LOG_INFO, "UG_EVENT_LOW_MEMORY\n");
635 case UG_EVENT_LOW_BATTERY:
636 DBG(LOG_INFO, "UG_EVENT_LOW_BATTERY\n");
638 case UG_EVENT_LANG_CHANGE:
639 DBG(LOG_INFO, "UG_EVENT_LANG_CHANGE\n");
641 case UG_EVENT_ROTATE_PORTRAIT:
643 DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT\n");
645 case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
646 DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN\n");
648 case UG_EVENT_ROTATE_LANDSCAPE:
650 DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE\n");
652 case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
654 DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN\n");
657 DBG(LOG_INFO, "default\n");
664 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, app_control_h control, void *priv)
669 DBG(LOG_ERROR, "The param is NULL\n");
674 case UG_KEY_EVENT_END:
675 DBG(LOG_INFO, "UG_KEY_EVENT_END\n");
684 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
690 DBG(LOG_ERROR, "The param is NULL\n");
694 ugd = calloc(1, sizeof(struct ug_data));
696 DBG(LOG_ERROR, "Failed to allocate memory for UG data\n");
702 ops->create = on_create;
703 ops->start = on_start;
704 ops->pause = on_pause;
705 ops->resume = on_resume;
706 ops->destroy = on_destroy;
707 ops->message = on_message;
708 ops->event = on_event;
709 ops->key_event = on_key_event;
711 ops->opt = UG_OPT_INDICATOR_ENABLE;
717 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
724 DBG(LOG_ERROR, "The param is NULL\n");
730 WFD_IF_FREE_MEM(ugd);
735 UG_MODULE_API int setting_plugin_reset(app_control_h control, void *priv)
739 wifi_direct_state_e state;
741 res = wifi_direct_initialize();
742 if (res != WIFI_DIRECT_ERROR_NONE) {
743 DBG(LOG_ERROR, "Failed to initialize wifi direct. [%d]\n", res);
747 res = wifi_direct_get_state(&state);
748 if (res != WIFI_DIRECT_ERROR_NONE) {
749 DBG(LOG_ERROR, "Failed to get link status. [%d]\n", res);
753 if (state < WIFI_DIRECT_STATE_ACTIVATING) {
754 DBG(LOG_INFO, "No need to reset Wi-Fi Direct.\n");
756 /*if connected, disconnect all devices*/
757 if (WIFI_DIRECT_STATE_CONNECTED == state) {
758 res = wifi_direct_disconnect_all();
759 if (res != WIFI_DIRECT_ERROR_NONE) {
760 DBG(LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
765 res = wifi_direct_deactivate();
766 if (res != WIFI_DIRECT_ERROR_NONE) {
767 DBG(LOG_ERROR, "Failed to reset Wi-Fi Direct. [%d]\n", res);
772 res = wifi_direct_deinitialize();
773 if (res != WIFI_DIRECT_ERROR_NONE) {
774 DBG(LOG_ERROR, "Failed to deinitialize wifi direct. [%d]\n", res);