summaryrefslogtreecommitdiff
path: root/ug-wifidirect/src/wfd_ug.c
blob: c20e1cf80b22b39c3533ed29f1c836083e1c336c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
*  WiFi-Direct UG
*
* Copyright 2012  Samsung Electronics Co., Ltd

* Licensed under the Flora License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

* http://www.tizenopensource.org/license

* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


#ifndef UG_MODULE_API
#define UG_MODULE_API __attribute__ ((visibility("default")))
#endif


#include <sys/time.h>
#include <libintl.h>
#include <sys/utsname.h>

#include <vconf.h>
#include <Elementary.h>
#include <ui-gadget-module.h>
#include <app_control.h>

#include <wifi-direct.h>
#include "wfd_ug.h"
#include "wfd_ug_view.h"
#include "wfd_client.h"
#ifdef MOTION_CONTROL_ENABLE
#include "wfd_motion_control.h"
#endif

void initialize_gen_item_class();

struct ug_data *global_ugd = NULL;

struct ug_data *wfd_get_ug_data()
{
	return global_ugd;
}

static void __wfd_main_vconf_change_cb(keynode_t *key, void *data)
{
	__FUNC_ENTER__;
	WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
	WFD_RET_IF(NULL == data, "ERROR : data is NULL");
	struct ug_data *ugd = (struct ug_data *) data;

	char *vconf_name = vconf_keynode_get_name(key);

	if (!g_strcmp0(vconf_name, VCONFKEY_SETAPPL_DEVICE_NAME_STR)){
		char *name_value = NULL;
		name_value = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
		WFD_RET_IF (!name_value, "Get string is failed");
		DBG(LOG_INFO,"name : %s", name_value);

		if (ugd->device_name_item && g_strcmp0(ugd->dev_name, name_value))
			wfd_ug_view_refresh_glitem(ugd->device_name_item);

		free(name_value);
	} else {
		DBG(LOG_ERROR, "vconf_name is error");
	}
	__FUNC_EXIT__;
}

static void _wifi_on_state_cb(keynode_t *key, void *data)
{
	WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
	WFD_RET_IF(NULL == data, "ERROR : data is NULL");

	struct ug_data *ugd = (struct ug_data *)data;
	int wifi_state = 0;

	if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
		DBG(LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
		return;
	}

	DBG(LOG_INFO, "WiFi State [%d]\n", wifi_state);
	if (wifi_state > VCONFKEY_WIFI_OFF && wifi_state < VCONFKEY_WIFI_STATE_MAX) {
		if (WIFI_DIRECT_ERROR_NONE != wifi_direct_get_state(&ugd->wfd_status)) {
			DBG(LOG_ERROR, "Failed to Get WiFi Direct State");
			return;
		}
		if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
			DBG(LOG_INFO, "Activate WiFi Direct...");
			if (FALSE != wfd_client_switch_on(ugd)) {
				DBG(LOG_ERROR, "Failed to Activate WiFi Direct");
			}
		}
	}
}

#ifdef WFD_HOSTAPD_ENABLER
static void __wfd_hotspot_mode_vconf_change_cb(keynode_t *key, void *data)
{
	DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb");
	if (NULL == key || NULL == data) {
		DBG(LOG_INFO, "Invalid parameters \n");
		return;
	}
	struct ug_data *ugd = (struct ug_data *) data;
	int hotspot_mode = 0;
	int res = 0;

	res = vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &hotspot_mode);
	if (res) {
		WDS_LOGE("Failed to get vconf value for PLMN(%d)", res);
		return;
	}
	DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb mode %d", hotspot_mode);

	if (VCONFKEY_MOBILE_HOTSPOT_MODE_NONE == hotspot_mode) {
		if (NULL != ugd->act_popup) {
			evas_object_del(ugd->act_popup);
		}
#ifdef WFD_ON_OFF_GENLIST
		wfd_ug_refresh_on_off_check(ugd);
#endif
	} else if (hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI ||
				hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP) {
		if (NULL != ugd->warn_popup) {
			evas_object_del(ugd->warn_popup);
			ugd->warn_popup = NULL;
		}
	}

}
#endif

Ea_Theme_Color_Table *_color_table_set(void)
{
	Ea_Theme_Color_Table *table;

	table = ea_theme_color_table_new(COLOR_TABLE);
	DBG(LOG_INFO, "Wi-Fi direct color table : %p", table);
	ea_theme_colors_set(table, EA_THEME_STYLE_DEFAULT);

	return table;
}

Ea_Theme_Font_Table *_font_table_set(void)
{
	Ea_Theme_Font_Table *table;

	table = ea_theme_color_table_new(FONT_TABLE);
	DBG(LOG_INFO, "Wi-Fi direct font table : %p", table);
	ea_theme_fonts_set(table);

	return table;
}

/**
 *	This function let the ug create backgroud
 *	@return  backgroud
 *	@param[in] ugd the pointer to the parent object
 *	@param[in] ugd the pointer to the main data structure
 */
static Evas_Object *_create_bg(Evas_Object *parent, char *style)
{
	__FUNC_ENTER__;
	Evas_Object *bg;

	bg = elm_bg_add(parent);
	evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_object_style_set(bg, style);
	evas_object_show(bg);

	__FUNC_EXIT__;
	return bg;
}

/**
 *	This function let the ug create full view
 *	@return  full view
 *	@param[in] ugd the pointer to the parent object
 *	@param[in] ugd the pointer to the main data structure
 */
static Evas_Object *_create_fullview(Evas_Object *parent, struct ug_data *ugd)
{
	__FUNC_ENTER__;
	Evas_Object *base;

	if (parent == NULL) {
		DBG(LOG_ERROR, "Incorrenct parameter");
		return NULL;
	}

	/* Create Full view */
	base = elm_layout_add(parent);
	if (!base) {
		DBG(LOG_ERROR, "Failed to add layout");
		return NULL;
	}

	elm_layout_theme_set(base, "layout", "application", "default");
	evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);

	__FUNC_EXIT__;
	return base;
}

/**
 *	This function let the ug create frame view
 *	@return  frame view
 *	@param[in] ugd the pointer to the parent object
 *	@param[in] ugd the pointer to the main data structure
 */
static Evas_Object *_create_frameview(Evas_Object *parent, struct ug_data *ugd)
{
	__FUNC_ENTER__;
	Evas_Object *base;

	if (parent == NULL) {
		DBG(LOG_ERROR, "Incorrenct parameter");
		return NULL;
	}

	/* Create Frame view */
	base = elm_layout_add(parent);
	if (!base) {
		DBG(LOG_ERROR, "Failed to add layout");
		return NULL;
	}

	elm_layout_theme_set(base, "layout", "application", "default");
	evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);

	__FUNC_EXIT__;
	return base;
}

/**
 *	This function let the ug destroy the ug
 *	@return   void
 *	@param[in] data the pointer to the main data structure
 */
void wfd_destroy_ug(void *data)
{
	__FUNC_ENTER__;
	struct ug_data *ugd = (struct ug_data *) data;

#ifdef WFD_DBUS_LAUNCH
	if (ugd->dbus_cancellable != NULL) {
		g_cancellable_cancel(ugd->dbus_cancellable);
		g_object_unref(ugd->dbus_cancellable);
		ugd->dbus_cancellable = NULL;
		if (ugd->conn) {
			g_object_unref(ugd->conn);
			ugd->conn = NULL;
		}
		DBG(LOG_INFO, "Cancelled dbus call");
		return;
	} else
#endif
	{
		DBG(LOG_INFO, "dbus_cancellable is NULL");
		ug_destroy_me(ugd->ug);
	}

	__FUNC_EXIT__;
	return;
}

static void wfd_ug_layout_del_cb(void *data , Evas *e, Evas_Object *obj, void *event_info)
{
	__FUNC_ENTER__;

	struct ug_data *ugd = (struct ug_data *) data;
	if (ugd == NULL) {
		DBG(LOG_ERROR, "Incorrect parameter(NULL)");
		return;
	}

	wfd_client_free_raw_discovered_peers(ugd);
	wfd_ug_view_free_peers(ugd);
	destroy_wfd_ug_view(ugd);

	__FUNC_EXIT__;
}

#ifdef WFD_DBUS_LAUNCH
/**
 *	This function let the ug initialize wfd
 *	@return   void
 *	@param[in] data the pointer to the main data structure
 *	@param[in] evas the pointer to the evas canvas
 *	@param[in] obj the pointer to the evas object
 *	@param[in] event_info the pointer to the event information
 */
static void _wfd_init_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
{
	__FUNC_ENTER__;
	int res = -1;
	struct ug_data *ugd = (struct ug_data *)data;
	WFD_RET_IF(ugd == NULL || ugd->base == NULL, "Incorrect parameter(NULL)\n");

	evas_object_event_callback_del(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb);

	res = launch_wifi_direct_manager(ugd);
	if (res != 0) {
		DBG(LOG_ERROR, "Failed to launch wifi direct manager\n");
	}

	__FUNC_EXIT__;
	return;
}
#endif

static void *on_create(ui_gadget_h ug, enum ug_mode mode, app_control_h control, void *priv)
{
	__FUNC_ENTER__;
	struct ug_data *ugd;
	int wfd_status = -1;
	int rots[4] = { 0, 90, 180, 270 };
	int ret;

	if (!ug || !priv) {
		return NULL;
	}

	ugd = priv;
	ugd->ug = ug;

	bindtextdomain(PACKAGE, LOCALEDIR);

	ugd->win = ug_get_window();
	if (!ugd->win) {
		return NULL;
	}

	elm_win_wm_rotation_available_rotations_set(ugd->win, rots, 1);


	/* check the input parameters from app at first */
	ugd->wfds = NULL;
	ugd->device_filter = -1; /* show all devices */
	ugd->is_auto_exit = false;
	ugd->is_multi_connect = true;
	ugd->is_init_ok = false;
	ugd->title = strdup(_("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"));

	if (control) {
		char *wfds = NULL;
		char *device_filter = NULL;
		char *auto_exit = NULL;
		char *multi_connect = NULL;
		char *title = NULL;
		char* viewtype = NULL;

		/*
		* get the control name
		* default value: Wi-Fi Direct
		*/
		ret = app_control_get_extra_data(control, "wfds", &wfds);
		if (ret == APP_CONTROL_ERROR_NONE && wfds) {
			DBG_SECURE(LOG_INFO, "Wfds name: %s", wfds);
			ugd->wfds = strdup(wfds);
			WFD_IF_FREE_MEM(wfds);
		}

		ret = app_control_get_extra_data(control, "viewtype", &viewtype);
		if(ret == APP_CONTROL_ERROR_NONE && viewtype) {
			DBG(LOG_INFO, "viewtype: %s\n", viewtype);
			ugd->view_type = strdup(viewtype);
			WFD_IF_FREE_MEM(viewtype);
		}

		/*
		* get the device filter
		* default value: NULL
		*/
		ret = app_control_get_extra_data(control, "device_filter", &device_filter);
		if (ret == APP_CONTROL_ERROR_NONE && device_filter) {
			DBG(LOG_INFO, "Device filter: %s", device_filter);
			if (0 == strncmp(device_filter, "computer", 8)) {
				ugd->device_filter = WFD_DEVICE_TYPE_COMPUTER;
			} else if (0 == strncmp(device_filter, "input_device", 12)) {
				ugd->device_filter = WFD_DEVICE_TYPE_INPUT_DEVICE;
			} else if (0 == strncmp(device_filter, "printer", 6)) {
				ugd->device_filter = WFD_DEVICE_TYPE_PRINTER;
			} else if (0 == strncmp(device_filter, "camera", 6)) {
				ugd->device_filter = WFD_DEVICE_TYPE_CAMERA;
			} else if (0 == strncmp(device_filter, "storage", 7)) {
				ugd->device_filter = WFD_DEVICE_TYPE_STORAGE;
			} else if (0 == strncmp(device_filter, "network_infra", 13)) {
				ugd->device_filter = WFD_DEVICE_TYPE_NW_INFRA;
			} else if (0 == strncmp(device_filter, "display", 7)) {
				ugd->device_filter = WFD_DEVICE_TYPE_DISPLAYS;
			} else if (0 == strncmp(device_filter, "multimedia_device", 17)) {
				ugd->device_filter = WFD_DEVICE_TYPE_MM_DEVICES;
			} else if (0 == strncmp(device_filter, "game_device", 11)) {
				ugd->device_filter = WFD_DEVICE_TYPE_GAME_DEVICES;
			} else if (0 == strncmp(device_filter, "telephone", 9)) {
				ugd->device_filter = WFD_DEVICE_TYPE_TELEPHONE;
			} else if (0 == strncmp(device_filter, "audio", 5)) {
				ugd->device_filter = WFD_DEVICE_TYPE_AUDIO;
			} else {
				ugd->device_filter = WFD_DEVICE_TYPE_OTHER;
			}
			WFD_IF_FREE_MEM(device_filter);
		}

		/*
		* get whether support auto exit after connection
		* default value: false
		*/
		ret = app_control_get_extra_data(control, "auto_exit", &auto_exit);
		if (ret == APP_CONTROL_ERROR_NONE && auto_exit) {
			DBG(LOG_INFO, "Auto exit: %s", auto_exit);
			if (0 == strncmp(auto_exit, "on", 2)) {
				ugd->is_auto_exit = true;
			} else {
				ugd->is_auto_exit = false;
			}
			WFD_IF_FREE_MEM(auto_exit);
		}

		/*
		* get whether support multi connection,
		* default value: true
		*/
		ret = app_control_get_extra_data(control, "multi_connect", &multi_connect);
		if (ret == APP_CONTROL_ERROR_NONE && multi_connect) {
			DBG(LOG_INFO, "Multi connection: %s", multi_connect);
			if (0 == strncmp(multi_connect, "off", 2)) {
				ugd->is_multi_connect = false;
			} else {
				ugd->is_multi_connect = true;
			}
			WFD_IF_FREE_MEM(multi_connect);
		}

		/*
		* get the title of UG
		* default value: Wi-Fi Direct
		*/
		ret = app_control_get_extra_data(control, "title_string", &title);
		if (ret == APP_CONTROL_ERROR_NONE && title) {
			DBG(LOG_INFO, "Title of UG: %s", title);
			WFD_IF_FREE_MEM(ugd->title);
			ugd->title = strdup(title);
			WFD_IF_FREE_MEM(title);
		}
	}

	if (mode == UG_MODE_FULLVIEW) {
		ugd->base = _create_fullview(ugd->win, ugd);
	} else {
		ugd->base = _create_frameview(ugd->win, ugd);
	}

	if (ugd->base) {
		evas_object_event_callback_add(ugd->base, EVAS_CALLBACK_DEL, wfd_ug_layout_del_cb, ugd);
		ugd->bg = _create_bg(ugd->win, "group_list");
		elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
	} else {
		DBG(LOG_ERROR, "Failed to create base layout\n");
		return NULL;
	}

	/* check status of wifi-direct from vconf */
	wfd_status = wfd_get_vconf_status();
	if (wfd_status < 0) {
		return NULL;
	}

	/* Enablee Changeable UI feature */
	ea_theme_changeable_ui_enabled_set(EINA_TRUE);

	ugd->color_table = _color_table_set();
	ugd->font_table = _font_table_set();

	/* draw UI */
	initialize_gen_item_class();
	create_wfd_ug_view(ugd);
	wfd_ug_view_init_genlist(ugd, true);
#ifdef MOTION_CONTROL_ENABLE
	motion_create(ugd);
#endif

	/*
	* if not deactivated, do initialization at once;
	* otherwise, do initialization later
	*/
	if (wfd_status != VCONFKEY_WIFI_DIRECT_DEACTIVATED) {
		init_wfd_client(ugd);
	} else {
		ugd->wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;

#ifdef WFD_DBUS_LAUNCH
		evas_object_event_callback_add(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb, ugd);
#else
		ret = init_wfd_client(ugd);
		WFD_RETV_IF(ret != 0, NULL,  "Failed to initialize WFD client library\n");

		/* Activate WiFi Direct */
		DBG(LOG_INFO, "Activating WiFi Direct...");
		if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
			ret = wfd_client_switch_on(ugd);
			WFD_RETV_IF(ret != 0, NULL, "Failed to activate WFD\n");
		}
#endif
	}
	ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
			__wfd_main_vconf_change_cb, ugd);
	if (ret) {
		DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_SETAPPL_DEVICE_NAME_STR)");
	}

	ret = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb, ugd);
	if (ret) {
		DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_WIFI_STATE)");

	}

#ifdef WFD_HOSTAPD_ENABLER
	ret = vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
		__wfd_hotspot_mode_vconf_change_cb, ugd);
	if (ret) {
		DBG(LOG_ERROR, "Failed to set vconf notification callback(MOBILE_HOTSPOT_MODE)");
	}
#endif

	__FUNC_EXIT__;
	return ugd->base;
}

static void on_start(ui_gadget_h ug, app_control_h control, void *priv)
{
	__FUNC_ENTER__;
	struct ug_data *ugd;
	int res;

	if (!ug || !priv) {
		return;
	}

	ugd = priv;

	struct utsname kernel_info;
	res = uname(&kernel_info);
	if (res != 0) {
		DBG(LOG_ERROR, "Failed to detect target type\n");
	} else {
		DBG_SECURE(LOG_INFO, "HW ID of this device [%s]\n", kernel_info.machine);
		if (strncmp(kernel_info.machine, "arm", 3) != 0) {
			wfd_ug_warn_popup(ugd, _("IDS_ST_POP_NOT_SUPPORTED"), POPUP_TYPE_TERMINATE_NOT_SUPPORT);
			return;
		}
	}

	__FUNC_EXIT__;
}

static void on_pause(ui_gadget_h ug, app_control_h control, void *priv)
{
	__FUNC_ENTER__;

	WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
	struct ug_data *ugd = priv;
	ugd->is_paused = true;

	wfd_refresh_wifi_direct_state(ugd);
	DBG(LOG_INFO, "on pause, wfd status: %d\n", ugd->wfd_status);

 	if ((WIFI_DIRECT_STATE_DISCOVERING == ugd->wfd_status) &&
		(WIFI_DIRECT_ERROR_NONE != wifi_direct_cancel_discovery())) {
		DBG(LOG_ERROR, "Failed to send cancel discovery state [%d]\n", ugd->wfd_status);
		__FUNC_EXIT__;
		return;
	}

	ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;

	__FUNC_EXIT__;
}

static void on_resume(ui_gadget_h ug, app_control_h control, void *priv)
{
	__FUNC_ENTER__;

	WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
	struct ug_data *ugd = priv;
	ugd->is_paused = false;
	int ret;
	wfd_refresh_wifi_direct_state(ugd);
	DBG(LOG_INFO, "on resume, status: %d\n", ugd->wfd_status);
	ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;

	elm_genlist_realized_items_update(ugd->genlist);

	if (ugd->wfd_status > WIFI_DIRECT_STATE_DEACTIVATED && ugd->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
		DBG(LOG_INFO, "Start discovery again\n");
		ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL_START;
		ret = wifi_direct_start_discovery_specific_channel(false, 1, WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL);
		if (ret != WIFI_DIRECT_ERROR_NONE) {
			ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_NONE;
			DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", ret);
			wifi_direct_cancel_discovery();
		}
	}

	__FUNC_EXIT__;
}

static void on_destroy(ui_gadget_h ug, app_control_h control, void *priv)
{
	__FUNC_ENTER__;

	WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");

	struct ug_data *ugd = priv;
	int ret;
	WFD_RET_IF(ugd->base == NULL, "The param is NULL\n");

#ifdef MOTION_CONTROL_ENABLE
	motion_destroy();
#endif

	/* DeInit WiFi Direct */
	ret = deinit_wfd_client(ugd);
	if (ret) {
		DBG(LOG_ERROR,"Failed to DeInit WiFi Direct");
	}

	if (ugd->scan_toolbar) {
		wfd_ug_view_refresh_button(ugd->scan_toolbar,
			_("IDS_WIFI_SK4_SCAN"), FALSE);
	}

	ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
			__wfd_main_vconf_change_cb);
	if (ret == -1) {
		DBG(LOG_ERROR,"Failed to ignore vconf key callback\n");
	}

	ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb);
	if (ret == -1) {
		DBG(LOG_ERROR,"Failed to ignore vconf key callback\n");
	}

#ifdef WFD_HOSTAPD_ENABLER
	ret = vconf_ignore_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
			__wfd_hotspot_mode_vconf_change_cb);
	if (ret == -1) {
		DBG(LOG_ERROR,"Failed to ignore vconf key callback MOBILE_HOTSPOT_MODE\n");
	}
#endif

	wfd_ug_view_free_peers(ugd);
	WFD_IF_FREE_MEM(ugd->title);
	WFD_IF_FREE_MEM(ugd->wfds);
	WFD_IF_FREE_MEM(ugd->view_type);

	WFD_IF_DEL_OBJ(ugd->bg);

#ifdef WFD_DBUS_LAUNCH
	if (ugd->base)
		evas_object_event_callback_del(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb);
#endif
	WFD_IF_DEL_OBJ(ugd->base);
	DBG(LOG_INFO, "WFD client deregistered");

	__FUNC_EXIT__;
	return;
}

static void on_message(ui_gadget_h ug, app_control_h msg, app_control_h control, void *priv)
{
	__FUNC_ENTER__;
	char* app_msg = NULL;
	struct ug_data *ugd = priv;

	if (!ug || !priv) {
		DBG(LOG_ERROR, "The param is NULL\n");
		return;
	}

	if (msg) {
		app_control_get_extra_data(msg, "msg", &app_msg);
		DBG(LOG_DEBUG, "Msg from app: %s", app_msg);

		if (!strcmp(app_msg, "destroy")) {
			if(!ugd->rename_popup) {
				DBG(LOG_INFO, "Destroying UG.");
				wfd_ug_view_free_peers(ugd);
				wfd_destroy_ug(ugd);
			}
		}
		WFD_IF_FREE_MEM(app_msg);
	}
	__FUNC_EXIT__;
}

static void on_event(ui_gadget_h ug, enum ug_event event, app_control_h control, void *priv)
{
	__FUNC_ENTER__;

	if (!ug || !priv) {
		DBG(LOG_ERROR, "The param is NULL\n");
		return;
	}

	switch (event) {
	case UG_EVENT_LOW_MEMORY:
		DBG(LOG_INFO, "UG_EVENT_LOW_MEMORY\n");
		break;
	case UG_EVENT_LOW_BATTERY:
		DBG(LOG_INFO, "UG_EVENT_LOW_BATTERY\n");
		break;
	case UG_EVENT_LANG_CHANGE:
		DBG(LOG_INFO, "UG_EVENT_LANG_CHANGE\n");
		break;
	case UG_EVENT_ROTATE_PORTRAIT:
		_ctxpopup_move();
		DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT\n");
		break;
	case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
		DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN\n");
		break;
	case UG_EVENT_ROTATE_LANDSCAPE:
		_ctxpopup_move();
		DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE\n");
		break;
	case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
		_ctxpopup_move();
		DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN\n");
		break;
	default:
		DBG(LOG_INFO, "default\n");
		break;
	}

	__FUNC_EXIT__;
}

static void on_key_event(ui_gadget_h ug, enum ug_key_event event, app_control_h control, void *priv)
{
	__FUNC_ENTER__;

	if (!ug || !priv) {
		DBG(LOG_ERROR, "The param is NULL\n");
		return;
	}

	switch (event) {
	case UG_KEY_EVENT_END:
		DBG(LOG_INFO, "UG_KEY_EVENT_END\n");
		break;
	default:
		break;
	}

	__FUNC_EXIT__;
}

UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
{
	__FUNC_ENTER__;
	struct ug_data *ugd;

	if (!ops) {
		DBG(LOG_ERROR, "The param is NULL\n");
		return -1;
	}

	ugd = calloc(1, sizeof(struct ug_data));
	if (ugd == NULL) {
		DBG(LOG_ERROR, "Failed to allocate memory for UG data\n");
		return -1;
	}

	global_ugd = ugd;

	ops->create = on_create;
	ops->start = on_start;
	ops->pause = on_pause;
	ops->resume = on_resume;
	ops->destroy = on_destroy;
	ops->message = on_message;
	ops->event = on_event;
	ops->key_event = on_key_event;
	ops->priv = ugd;
	ops->opt = UG_OPT_INDICATOR_ENABLE;

	__FUNC_EXIT__;
	return 0;
}

UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
{
	__FUNC_ENTER__;

	struct ug_data *ugd;

	if (!ops) {
		DBG(LOG_ERROR, "The param is NULL\n");
		return;
	}

	ugd = ops->priv;

	WFD_IF_FREE_MEM(ugd);

	__FUNC_EXIT__;
}

UG_MODULE_API int setting_plugin_reset(app_control_h control, void *priv)
{
	__FUNC_ENTER__;
	int res = -1;
	wifi_direct_state_e state;

	res = wifi_direct_initialize();
	if (res != WIFI_DIRECT_ERROR_NONE) {
		DBG(LOG_ERROR, "Failed to initialize wifi direct. [%d]\n", res);
		return -1;
	}

	res = wifi_direct_get_state(&state);
	if (res != WIFI_DIRECT_ERROR_NONE) {
		DBG(LOG_ERROR, "Failed to get link status. [%d]\n", res);
		return -1;
	}

	if (state < WIFI_DIRECT_STATE_ACTIVATING) {
		DBG(LOG_INFO, "No need to reset Wi-Fi Direct.\n");
	} else {
		/*if connected, disconnect all devices*/
		if (WIFI_DIRECT_STATE_CONNECTED == state) {
			res = wifi_direct_disconnect_all();
			if (res != WIFI_DIRECT_ERROR_NONE) {
				DBG(LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
				return -1;
			}
		}

		res = wifi_direct_deactivate();
		if (res != WIFI_DIRECT_ERROR_NONE) {
			DBG(LOG_ERROR, "Failed to reset Wi-Fi Direct. [%d]\n", res);
			return -1;
		}
	}

	res = wifi_direct_deinitialize();
	if (res != WIFI_DIRECT_ERROR_NONE) {
		DBG(LOG_ERROR, "Failed to deinitialize wifi direct. [%d]\n", res);
		return -1;
	}

	__FUNC_EXIT__;
	return 0;
}

UG_MODULE_API int setting_plugin_search_init(app_control_h control, void *priv, char** applocale)
{
	__FUNC_ENTER__;

	*applocale = strdup("ug-setting-wifidirect-efl");
	void *node = NULL;

	Eina_List **pplist = (Eina_List**)priv;

	node = setting_plugin_search_item_add("IDS_WIFI_BUTTON_MULTI_CONNECT", "viewtype:IDS_WIFI_BUTTON_MULTI_CONNECT", NULL, 5, NULL);
	*pplist = eina_list_append(*pplist, node);

	 __FUNC_EXIT__;
	return 0;
}