summaryrefslogtreecommitdiff
path: root/src/mh_common_utility.c
blob: de83510c95665ea17b11f1884b66240dd0f97267 (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
/*
* ug-mobile-ap
*
* 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.
*
*/

#include <net_connection.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "mh_common_utility.h"
#include "mobile_hotspot.h"

void _handle_network_cellular_state_changed_cb(keynode_t *key, void *data)
{
	if (key == NULL || data == NULL) {
		ERR("Parameter is NULL\n");
		return;
	}
	mh_appdata_t *ad = (mh_appdata_t *)data;
	int vconf_key = 0;

	if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
		ERR("Invalid vconf key type\n");
		return;
	}

	vconf_key = vconf_keynode_get_int(key);
	SDBG("key = %s, value = %d(int)\n",
			vconf_keynode_get_name(key), vconf_key);

	if (vconf_key != VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE)
		return;

	if (ad->popup) {
		evas_object_del(ad->popup);
		ad->popup = NULL;
	}
	if (ad->is_wifi_teth_enabling) {
		_update_wifi_item(ad, MH_STATE_NONE);
		ad->is_wifi_teth_enabling = false;
	}
	if (ad->is_bt_teth_enabling) {
		_update_bt_item(ad, MH_STATE_NONE);
		ad->is_bt_teth_enabling = false;
	}
	if (ad->is_usb_teth_enabling) {
		_update_usb_item(ad, MH_STATE_NONE);
		ad->is_usb_teth_enabling = false;
	}
	return;
}

void _device_name_changed_cb(keynode_t *key, void *data)
{
	if (key == NULL || data == NULL) {
		ERR("Parameter is NULL\n");
		return;
	}

	mh_appdata_t *ad = (mh_appdata_t *)data;
	char *dev_name = NULL;

	if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
		ERR("Invalid vconf key type\n");
		return;
	}
	dev_name = vconf_keynode_get_str(key);
	if (ad->setup.name_item != NULL) {
		elm_genlist_item_update(ad->setup.name_item);
	}
	if (ad->main.help_item != NULL) {
		elm_genlist_item_update(ad->main.help_item);
	}
	if (ad->rename_popup) {
		elm_entry_entry_set(ad->rename_entry, dev_name);
		elm_entry_cursor_end_set(ad->rename_entry);
	}
	if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
		_update_wifi_item(ad, MH_STATE_PROCESS);
	}
	return;
}

Evas_Object *_create_progressbar(Evas_Object *parent, const char *style)
{
	if (parent == NULL || style == NULL) {
		ERR("Invalid param\n");
		return NULL;
	}

	Evas_Object *progressbar;

	progressbar = elm_progressbar_add(parent);
	if (progressbar == NULL) {
		ERR("progressbar is NULL\n");
		return NULL;
	}

	elm_object_style_set(progressbar, style);
	elm_progressbar_horizontal_set(progressbar, EINA_TRUE);
	elm_progressbar_pulse(progressbar, EINA_TRUE);
	evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, EVAS_HINT_FILL);
	evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_show(progressbar);

	return progressbar;
}

Evas_Object *_create_bg(Evas_Object *parent, const char *style)
{
	__MOBILE_AP_FUNC_ENTER__;

	if (parent == NULL || style == NULL) {
		ERR("The param is NULL\n");
		return NULL;
	}

	Evas_Object *bg = NULL;

	bg = elm_bg_add(parent);
	if (bg == NULL) {
		ERR("bg is NULL\n");
		return NULL;
	}

	evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_object_style_set(bg, style);
	evas_object_show(bg);

	return bg;
}

Evas_Object *_create_win_layout(mh_appdata_t *ad)
{
	__MOBILE_AP_FUNC_ENTER__;

	if (ad->win == NULL) {
		ERR("There is no main window\n");
		return NULL;
	}

	Evas_Object *layout;
	Evas_Object *bg;

	layout = elm_layout_add(ad->win);
	if (layout == NULL) {
		ERR("layout is NULL\n");
		return NULL;
	}

	elm_layout_theme_set(layout, "layout", "application", "default");
	evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
			EVAS_HINT_EXPAND);

	bg = _create_bg(layout, "group_list");
	if (bg == NULL) {
		ERR("bg is NULL\n");
		evas_object_del(layout);
		return NULL;
	}
	elm_object_part_content_set(layout, "elm.swallow.bg", bg);

	evas_object_show(layout);

	ad->layout = layout;
	ad->bg = bg;

	__MOBILE_AP_FUNC_EXIT__;

	return layout;
}

Evas_Object *_create_naviframe(Evas_Object *parent)
{
	__MOBILE_AP_FUNC_ENTER__;

	if (parent == NULL) {
		ERR("parent is NULL\n");
		return NULL;
	}

	Evas_Object *naviframe;

	naviframe = elm_naviframe_add(parent);
	if (naviframe == NULL) {
		ERR("naviframe is NULL\n");
		return NULL;
	}
	evas_object_show(naviframe);

	__MOBILE_AP_FUNC_EXIT__;

	return naviframe;
}

Evas_Object *_create_button(Evas_Object *parent, const char *text, const char *part,
		Evas_Smart_Cb func, void *user_data)
{
	if (parent == NULL || text == NULL || part == NULL || func == NULL) {
		ERR("Invalid param\n");
		return NULL;
	}

	Evas_Object *btn;

	btn = elm_button_add(parent);
	elm_object_style_set(btn, "popup");
	elm_object_domain_translatable_text_set(btn, PACKAGE, text);
	elm_object_part_content_set(parent, part, btn);
	evas_object_smart_callback_add(btn, "clicked", func, user_data);
	evas_object_show(btn);

	return btn;
}

void _handle_mobileap_syspopup_popup_response(keynode_t *key, void *data)
{
	if (!data) {
		ERR("The param is NULL\n");
		return;
	}

	if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
		ERR("Invalid vconf key\n");
		return;
	}

	mh_appdata_t *ad = (mh_appdata_t *)data;
	int vconf_key = 0;

	vconf_key = vconf_keynode_get_int(key);

	if (vconf_key) {
		if (vconf_set_int(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE, 0) < 0) {
			ERR("vconf_set_int is failed\n");
		}
		if (ad->type != TETHERING_TYPE_WIFI) {
			DBG("no need to handle user response\n");
			return;
		}
		if (ad->popup) {
			evas_object_del(ad->popup);
			ad->popup = NULL;
		}
		if (ad->popup_checkbox) {
			evas_object_del(ad->popup_checkbox);
			ad->popup_checkbox = NULL;
		}
		if (vconf_key == 1)
			_update_wifi_item(ad, MH_STATE_NONE);
	}
}

void _handle_usb_status_change(keynode_t *key, void *data)
{
	int vconf_key = 0;

	if (!data) {
		ERR("The param is NULL\n");
		return;
	}

	mh_appdata_t *ad = (mh_appdata_t *)data;

	if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
		ERR("Invalid vconf key\n");
		return;
	}

	vconf_key = vconf_keynode_get_int(key);
	if (vconf_key == SETTING_USB_NONE_MODE) {
		return;
	}
	if (vconf_key != VCONFKEY_SYSMAN_USB_AVAILABLE) {
		if (ad->type == TETHERING_TYPE_USB && ad->popup) {
			evas_object_del(ad->popup);
			ad->popup = NULL;
			_update_tethering_item(ad, MH_STATE_NONE);
		}
		if (ad->main.usb_item)
			elm_object_item_disabled_set(ad->main.usb_item, EINA_TRUE);
	} else {
		if (ad->main.usb_item)
			elm_object_item_disabled_set(ad->main.usb_item, EINA_FALSE);
	}
}

int _get_vconf_hotspot_mode(void)
{
	int value = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;

	if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &value) < 0) {
		ERR("vconf_get_int is failed\n");
		return 0;
	}
	DBG("%s : %d\n", VCONFKEY_MOBILE_HOTSPOT_MODE, value);

	return value;
}

Eina_Bool _get_no_of_connected_device(mh_appdata_t *ad, int *no, tethering_type_e type)
{
	if (ad == NULL) {
		ERR("Invalid param\n");
		return EINA_FALSE;
	}

	if (ad->client_list == NULL) {
		*no = 0;
		return EINA_TRUE;
	}

	GSList *l = NULL;
	tethering_client_h handle;
	tethering_type_e type2;
	tethering_error_e ret;
	int count = 0;

	for (l = ad->client_list; l != NULL; l = g_slist_next(l) ) {
		handle = l->data;
		if (handle == NULL)
			continue;

		ret = tethering_client_get_tethering_type(handle, &type2);
		if (ret != TETHERING_ERROR_NONE) {
			ERR("tethering_client_get_tethering_type is failed [0x%X]\n", ret);
			continue;
		}

		if (type != TETHERING_TYPE_ALL && type != type2)
			continue;

		count++;
	}
	*no = count;

	return EINA_TRUE;
}

void _append_list_client_handle(mh_appdata_t *ad, tethering_client_h client)
{
	if (ad == NULL || client == NULL) {
		ERR("Invalid param\n");
		return;
	}

	int ret;
	tethering_client_h handle;

	ret = tethering_client_clone(&handle, client);
	if (ret != TETHERING_ERROR_NONE) {
		ERR("Unable to clone client handle");
		return;
	}

	ad->client_list = g_slist_append(ad->client_list, handle);

	return;
}

void _release_list_client_handle(mh_appdata_t *ad)
{
	if (ad == NULL) {
		ERR("Invalid param\n");
		return;
	}

	if (ad->client_list == NULL)
		return;

	GSList *l = NULL;
	tethering_client_h handle;

	for (l = ad->client_list; l != NULL; l = g_slist_next(l) ) {
		handle = l->data;
		if (handle == NULL)
			continue;

		tethering_client_destroy(handle);
	}

	g_slist_free(ad->client_list);
	ad->client_list = NULL;

	return;
}

static int __find_mac_address(gconstpointer a, gconstpointer b)
{
	tethering_client_h handle = (tethering_client_h)a;
	const char *udn = (const char *)b;
	char *mac_addr;
	int ret;

	ret = tethering_client_get_mac_address(handle, &mac_addr);
	if (ret != TETHERING_ERROR_NONE)
		return -1;

	ret = g_ascii_strcasecmp(mac_addr, udn);

	if (mac_addr)
		free(mac_addr);

	return ret;
}

void _delete_list_client_handle(mh_appdata_t *ad, const char *mac_addr)
{
	if (ad == NULL || mac_addr == NULL) {
		ERR("Invalid param\n");
		return;
	}

	GSList *l = NULL;
	tethering_client_h handle = NULL;

	l = g_slist_find_custom(ad->client_list, mac_addr, __find_mac_address);
	if (!l) {
		ERR("Not found\n");
		return;
	}

	handle = (tethering_client_h)l->data;
	ad->client_list = g_slist_delete_link(ad->client_list, l);
	if (handle == NULL)
		return;

	tethering_client_destroy(handle);
	return;
}

int _get_list_clients_count(mh_appdata_t *ad)
{
	int client_list_counts;

	if (ad == NULL) {
		ERR("Invalid param\n");
		return 0;
	}

	client_list_counts = g_slist_length(ad->client_list);
	if (client_list_counts > TETHERING_WIFI_MAX_CONNECTED_STA) {
		INFO("client_list_counts = [%d]\n", client_list_counts);
		return TETHERING_WIFI_MAX_CONNECTED_STA;
	} else
		return client_list_counts;
}

void _get_list_clients_informations(mh_appdata_t *ad)
{
	if (ad == NULL) {
		ERR("Invalid param\n");
		return;
	}

	GSList *l = NULL;
	tethering_client_h handle;
	int i = 1;
	int ret;
	tethering_type_e type = 0;
	char *name = NULL;
	char *mac_addr = NULL;

	for (l = ad->client_list; l != NULL; l = g_slist_next(l), i++) {
		handle = (tethering_client_h)l->data;

		ret = tethering_client_get_name(handle, &name);
		if (ret != TETHERING_ERROR_NONE)
			ERR("tethering_client_get_name is failed : %d\n", ret);

		ret = tethering_client_get_mac_address(handle, &mac_addr);
		if (ret != TETHERING_ERROR_NONE)
			ERR("tethering_client_get_mac_address is failed : %d\n", ret);

		ret = tethering_client_get_tethering_type(handle, &type);
		if (ret != TETHERING_ERROR_NONE)
			ERR("tethering_client_get_tethering_type is failed : %d\n", ret);
		if (name) {
			SDBG("Client[%d] : %s\n", i, name);
			free(name);
			name = NULL;
		}

		if (mac_addr) {
			SDBG("MAC[%d] : %s\n", i, mac_addr);
			free(mac_addr);
			mac_addr = NULL;
		}

		SDBG("Type[%d] : %d\n", i, type);
	}

	return;
}

void _free_genlist_item(Elm_Object_Item **item)
{
	if (*item == NULL) {
		return;
	}

	elm_object_item_del(*item);
	*item = NULL;
}

void _free_genlist_itc(Elm_Genlist_Item_Class **itc)
{
	if (*itc == NULL) {
		return;
	}

	elm_genlist_item_class_free(*itc);
	*itc = NULL;
}

int _get_sim_state(void)
{
	int value = 0;

	if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &value) < 0) {
		ERR("vconf_get_int is failed\n");
	}
	DBG("%s : %d\n", VCONFKEY_TELEPHONY_SIM_SLOT, value);
	if (value == VCONFKEY_TELEPHONY_SIM_INSERTED) {
		return value;
	}

#if defined TIZEN_DUALSIM_ENABLE
	if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT2, &value) < 0) {
		ERR("vconf_get_int is failed\n");;
	}
	DBG("%s : %d\n", VCONFKEY_TELEPHONY_SIM_SLOT2, value);
#endif

	return value;
}

connection_cellular_state_e _get_cellular_state(void)
{
	int ret;
	connection_h handle = NULL;
	connection_cellular_state_e cellular_state;

	ret = connection_create(&handle);
	if (ret != CONNECTION_ERROR_NONE) {
		ERR("Connection create failed");
		return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
	}
	ret = connection_get_cellular_state(handle, &cellular_state);
	if (ret != CONNECTION_ERROR_NONE) {
		ERR("connection_get_cellular_state() is failed : %d\n");
		connection_destroy(handle);
		return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
	}

	connection_destroy(handle);
	return cellular_state;
}

int _get_checkbox_status(tethering_type_e type)
{
	int value = 0;
	char *vconf_key = NULL;

	if (type == TETHERING_TYPE_WIFI)
		vconf_key = VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS;
	else if (type == TETHERING_TYPE_BT)
		vconf_key = VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS;
	else if (type == TETHERING_TYPE_USB)
		vconf_key = VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS;
	else
		return 0;
	if (vconf_get_int(vconf_key, &value) < 0) {
		ERR("vconf_get_int() is failed\n");
		return 0;
	}
	DBG("%s : %d\n", vconf_key, value);
	return value;
}

bool _set_checkbox_status(tethering_type_e type, int value)
{
	char *vconf_key = NULL;

	if (type == TETHERING_TYPE_WIFI)
		vconf_key = VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS;
	else if (type == TETHERING_TYPE_BT)
		vconf_key = VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS;
	else if (type == TETHERING_TYPE_USB)
		vconf_key = VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS;
	else
		return false;

	if (vconf_set_int(vconf_key, value) < 0) {
		ERR("vconf_set_int is failed\n");
		return false;
	}
	return true;
}

bool _set_vconf_prev_wifi_state(bool value)
{
	if (vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, value) < 0) {
		ERR("vconf_set_bool failed\n");
		return false;
	}
	return true;
}

bool _get_vconf_prev_wifi_state()
{
	int value = 0;

	if (vconf_get_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, &value) < 0) {
		ERR("vconf_get_bool is failed\n");
		return false;
	}
	DBG("%s : %d\n", VCONF_MOBILE_AP_PREV_WIFI_STATUS, value);

	return value ? true : false;
}

int _send_signal_qp(const char *cmd)
{
	if (cmd == NULL) {
		ERR("Invalid param");
		return -1;
	}

	DBusGConnection *conn = NULL;
	DBusConnection *gconn = NULL;
	DBusMessage *msg = NULL;
	GError *err = NULL;
	char *module = "wifi_hotspot";

	conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err);
	if (err != NULL) {
		ERR("Failed connection to system bus[%s]", err->message);
		g_error_free(err);
		return -1;
	}

	gconn = dbus_g_connection_get_connection(conn);
	if (gconn == NULL) {
		ERR("Failed to get connection \n");
		dbus_g_connection_unref(conn);
		return -1;
	}

	msg = dbus_message_new_signal("/Org/Tizen/Quickpanel",
					  "org.tizen.quickpanel",
					  "ACTIVITY");
	if (!msg) {
		ERR("Unable to allocate D-Bus signal\n");
		dbus_g_connection_unref(conn);
		return -1;
	}

	if (!dbus_message_append_args(msg,
			DBUS_TYPE_STRING, &module,
			DBUS_TYPE_STRING, &cmd,
			DBUS_TYPE_INVALID)) {
		ERR("Event sending failed");
		dbus_message_unref(msg);
		dbus_g_connection_unref(conn);
		return -1;
	}

	DBG("Send dbus signal : %s\n", cmd);
	dbus_connection_send(gconn, msg, NULL);
	dbus_message_unref(msg);
	dbus_g_connection_unref(conn);

	return 0;
}