summaryrefslogtreecommitdiffstats
path: root/bluetooth-manager.c
blob: 893760d0f01160b68d7bb15d558a8d5f4e79625b (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
/*  Copyright 2016 ALPS ELECTRIC CO., LTD.
*
*   Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
*   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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#include "bluetooth-api.h"
#include "bluetooth-manager.h"

Client cli = { 0 };

stBluetoothManage BluetoothManage = { 0 };

/* ------ LOCAL  FUNCTIONS --------- */

/*
 register the agent, and register signal watch
 */
void bt_manage_dbus_init(void) {
	D_PRINTF("\n");

	//InitDBusCommunication();

}
/* ------ PUBLIC PLUGIN FUNCTIONS --------- */

/*
 * Init the Bluetooth Manager
 * Note: bluetooth-api shall do BluetoothManageInit() first before call other APIs.
 */
int BluetoothManageInit() {
	D_PRINTF("\n");

	BluetoothManage.device = NULL;
	g_mutex_init(&(BluetoothManage.m));

	bt_manage_dbus_init();

	return 0;
}

/*
 * Set the Bluez Adapter Property "Powered" value
 * If success return 0, else return -1;
 */
int adapter_set_powered(gboolean powervalue) {
	D_PRINTF("value:%d\n",powervalue);
	GDBusConnection *connection;
	GError *error = NULL;

	GVariant *value;
	gboolean result;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, ADAPTER_PATH,
	FREEDESKTOP_PROPERTIES, "Set",
			g_variant_new("(ssv)", ADAPTER_INTERFACE, "Powered",
					g_variant_new("b", powervalue)), NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;
}

/*
 * Get the Bluez Adapter Property "Powered" value
 * If success return 0, else return -1;
 */
int adapter_get_powered(gboolean *powervalue) {
	D_PRINTF("\n");
	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value = NULL;
	GVariant *subValue = NULL;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}
	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, ADAPTER_PATH,
	FREEDESKTOP_PROPERTIES, "Get",
			g_variant_new("(ss)", ADAPTER_INTERFACE, "Powered"), NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_get(value, "(v)", &subValue);
	g_variant_get(subValue, "b", powervalue);
	g_variant_unref(value);

	D_PRINTF("get ret :%d\n",*powervalue);
	return 0;
}

/*
 * Call the Bluez Adapter Method "StartDiscovery"
 * If success return 0, else return -1;
 */
int adapter_start_discovery() {
	D_PRINTF("\n");
	GDBusConnection *connection = NULL;
	GError *error = NULL;
	GVariant *value = NULL;
	GVariant *subValue = NULL;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}
	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, ADAPTER_PATH,
	ADAPTER_INTERFACE, "StartDiscovery", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;
}

/*
 * Call the Bluez Adapter Method "StopDiscovery"
 * If success return 0, else return -1;
 */
int adapter_stop_discovery() {
	D_PRINTF("\n");
	GDBusConnection *connection = NULL;
	GError *error = NULL;
	GVariant *value = NULL;
	GVariant *subValue = NULL;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}
	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, ADAPTER_PATH,
	ADAPTER_INTERFACE, "StopDiscovery", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);

	return 0;
}

/*
 * Call the Bluez Adapter Method "RemoveDevice"
 * If success return 0, else return -1;
 */
int adapter_remove_device(struct btd_device * addr) {
	D_PRINTF("\n%s\t%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, ADAPTER_PATH,
	ADAPTER_INTERFACE, "RemoveDevice", g_variant_new("(o)", addr->path), NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;

}

/*
 * Get the store device list.
 */
//FIXME: gdevices should be added the lock/unlock
GSList* adapter_get_devices_list() {
	return BluetoothManage.device;
}

void lock_devices_list(void) {
	g_mutex_lock(&(BluetoothManage.m));
}

void unlock_devices_list(void) {
	g_mutex_unlock(&(BluetoothManage.m));
}

/*
 * Update the device list
 * Call <method>GetManagedObjects
 * reply type is "Dict of {Object Path, Dict of {String, Dict of {String, Variant}}}"
 */
#if 0
// Test Function
/* recursively iterate a container */
void iterate_container_recursive (GVariant *container)
{
	GVariantIter iter;
	GVariant *child;

	g_variant_iter_init (&iter, container);
	while ((child = g_variant_iter_next_value (&iter)))
	{
		g_print ("type '%s'\n", g_variant_get_type_string (child));

		if (g_variant_is_container (child))
		iterate_container_recursive (child);

		g_variant_unref (child);
	}
}
#endif

int adapter_update_devices() {
	D_PRINTF("\n");
	GDBusConnection *connection = NULL;
	GError *error = NULL;
	GVariant *value = NULL;
	GSList *newDeviceList = NULL;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}
	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, "/",
			"org.freedesktop.DBus.ObjectManager", "GetManagedObjects", NULL,
			NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);
		g_error_free(error);
		return -1;
	}

	GVariant *subValue = NULL;
	GVariant *subValue_1 = NULL;
	GVariantIter *subValueIter;

	g_variant_get(value, "(*)", &subValue);

	g_variant_get(subValue, "a*", &subValueIter);
	while (g_variant_iter_loop(subValueIter, "*", &subValue_1)) {

#if 0
		iterate_container_recursive(subValue_1);
#else

//FIXME:Bad solution to get the BT address and name
		GVariantIter dbus_object_iter;
		GVariant *dbusObjecValue;
		GVariant *dbusObjecSubValue;
		gchar *dbusObjecPath;
		struct btd_device *device;

		g_variant_iter_init(&dbus_object_iter, subValue_1);

		//DBus Object
		dbusObjecValue = g_variant_iter_next_value(&dbus_object_iter);

		g_variant_get(dbusObjecValue, "o", &dbusObjecPath);

		//ObjectPath is /org/bluez/hci0/dev_xx_xx_xx_xx_xx_xx
		if ((37 != strlen(dbusObjecPath))
				|| (NULL
						== g_strrstr_len(dbusObjecPath, 19,
								"/org/bluez/hci0/dev"))) {
			g_variant_unref(dbusObjecValue);
			continue;
		}
		device = g_try_malloc0(sizeof(struct btd_device));
		device->path = g_strdup(dbusObjecPath);
		g_variant_unref(dbusObjecValue);
		D_PRINTF("Found new device%s\n",device->path );

		//DBus Interfaces and Method/Properties under Interface
		dbusObjecSubValue = g_variant_iter_next_value(&dbus_object_iter);

		GVariantIter *interfaces_iter;
		GVariant *interfaces_subValue;
		g_variant_get(dbusObjecSubValue, "a*", &interfaces_iter);

		while (g_variant_iter_loop(interfaces_iter, "*", &interfaces_subValue)) {
			// D_PRINTF("\t%s\n",g_variant_get_type_string(interfaces_subValue));

			GVariantIter MethodsSignalProperties_iter;
			GVariant *MethodsSignalProperties_name;
			GVariant *MethodsSignalProperties_value;
			gchar *properties_name;

			g_variant_iter_init(&MethodsSignalProperties_iter,
					interfaces_subValue);

			//DBus Interfaces
			MethodsSignalProperties_name = g_variant_iter_next_value(
					&MethodsSignalProperties_iter);

			g_variant_get(MethodsSignalProperties_name, "s", &properties_name);
			//D_PRINTF("\t%s\n",properties_name);
			g_variant_unref(MethodsSignalProperties_name);

			if (NULL
					== g_strrstr_len(properties_name, 20,
							"org.bluez.Device1")) {
				continue;
			}

			D_PRINTF("\t%s\n",properties_name);

			//DBus XXX
			MethodsSignalProperties_value = g_variant_iter_next_value(
					&MethodsSignalProperties_iter);

			GVariantIter *subValue_iter;
			GVariant *subValueVariant;
			g_variant_get(MethodsSignalProperties_value, "a*", &subValue_iter);

			while (g_variant_iter_loop(subValue_iter, "*", &subValueVariant)) {
				GVariantIter sub_subValue_iter;
				GVariant *sub_subValue_name;
				GVariant *sub_subValue_value;
				gchar *key_1 = NULL;
				gchar *key_2 = NULL;

				g_variant_iter_init(&sub_subValue_iter, subValueVariant);

				//DBus Interfaces
				sub_subValue_name = g_variant_iter_next_value(
						&sub_subValue_iter);

				g_variant_get(sub_subValue_name, "s", &key_1);
				D_PRINTF("\t\t%s\n",key_1);

				//DBus XXX
				sub_subValue_value = g_variant_iter_next_value(
						&sub_subValue_iter);

				GVariant *dbus_value;
				dbus_value = g_variant_get_variant(sub_subValue_value);

				if (g_variant_is_of_type(dbus_value, G_VARIANT_TYPE_STRING)) {
					g_variant_get(dbus_value, "s", &key_2);
					//D_PRINTF("\t\t\t%s\ts%s\n",key_1,key_2);

					if (g_strrstr_len(key_1, 10, "Address")) {
						device->bdaddr = g_strdup(key_2);
					} else if (g_strrstr_len(key_1, 10, "Name")) {
						device->name = g_strdup(key_2);
					}
					g_free(key_2);

				} else if (g_variant_is_of_type(dbus_value,
						G_VARIANT_TYPE_BOOLEAN)) {
					gboolean properties_value;
					g_variant_get(dbus_value, "b", &properties_value);

					if (g_strrstr_len(key_1, 10, "Paired")) {
						device->paired = properties_value;
					} else if (g_strrstr_len(key_1, 10, "Blocked")) {
						device->blocked = properties_value;
					} else if (g_strrstr_len(key_1, 10, "Connected")) {
						device->connected = properties_value;
					} else if (g_strrstr_len(key_1, 10, "Trusted")) {
						device->trusted = properties_value;
					}

				}
				g_variant_unref(sub_subValue_name);
				g_variant_unref(sub_subValue_value);

			}
			g_variant_iter_free(subValue_iter);

			g_variant_unref(MethodsSignalProperties_value);

		}
		g_variant_iter_free(interfaces_iter);

		g_variant_unref(dbusObjecSubValue);

#endif

		newDeviceList = g_slist_append(newDeviceList, device);

	}

	g_variant_iter_free(subValueIter);

	g_variant_unref(value);

	//clean first
	GSList * temp = BluetoothManage.device;
	while (temp) {
		struct btd_device *BDdevice = temp->data;
		temp = temp->next;

		BluetoothManage.device = g_slist_remove_all(BluetoothManage.device,
				BDdevice);
		//D_PRINTF("\n%s\n%s\n",BDdevice->bdaddr,BDdevice->name);
		if (BDdevice->bdaddr) {
			g_free(BDdevice->bdaddr);
		}
		if (BDdevice->name) {
			g_free(BDdevice->name);
		}
		if (BDdevice->path) {
			g_free(BDdevice->path);
		}
		g_free(BDdevice);

	}

	BluetoothManage.device = newDeviceList;

}

/*
 * send pairing command
 * If success return 0, else return -1;
 */
int device_pair(struct btd_device * addr) {
	D_PRINTF("\n%s\n%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
			"org.bluez.Device1", "Pair", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;

}

/*
 * send cancel pairing command
 * If success return 0, else return -1;
 */
int device_cancelPairing(struct btd_device * addr) {
	D_PRINTF("\n%s\n%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
			"org.bluez.Device1", "CancelPairing", NULL, NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;

}
/*
 * send connect command
 * If success return 0, else return -1;
 */
int device_connect(struct btd_device * addr) {
	D_PRINTF("\n%s\n%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
			"org.bluez.Device1", "Connect", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;

}

/*
 * send disconnect command
 * If success return 0, else return -1;
 */
int device_disconnect(struct btd_device * addr) {
	D_PRINTF("\n%s\n%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *value;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
			"org.bluez.Device1", "Disconnect", NULL, NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(value);
	return 0;

}

/*
 * set remote device property
 * If success return 0, else return -1;
 */
int device_set_property(struct btd_device * addr, const char *property_name,
		const char *property_value) {
	D_PRINTF("\n%s\n%s\t%s\n",addr->bdaddr,addr->name,addr->path);

	GDBusConnection *connection;
	GError *error = NULL;
	GVariant *ret;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

	//Only support set "Trusted"
	if (strcmp(property_name, "Trusted")) {
		D_PRINTF("Not support property name\n");
		return -1;
	}

	gboolean value;
	if (atoi(property_value) == 1 || !strcasecmp(property_value, "true")) {
		value = TRUE;
	} else if (atoi(property_value) == 0
			|| !strcasecmp(property_value, "false")) {
		value = FALSE;
	} else {
		D_PRINTF("Not support property value\n");
		return -1;
	}

	ret = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
	FREEDESKTOP_PROPERTIES, "Set",
			g_variant_new("(ssv)", DEVICE_INTERFACE, property_name,
					g_variant_new("b", value)), NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == ret) {
		D_PRINTF ("Error getting object manager client: %s", error->message);
		g_error_free(error);
		return -1;
	}

	g_variant_unref(ret);
	return 0;
}

int isAVPConnected(struct btd_device *addr) {

	GDBusConnection *connection;

	GError *error = NULL;
	GVariant *value;
	GVariant *variantValue;
	gboolean status;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}

//    value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE,  BDdevice->path, FREEDESKTOP_PROPERTIES,
//        "Get", g_variant_new("(ss)",MEDIA_CONTROL1_INTERFACE,"Connected"),
//        NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	value = g_dbus_connection_call_sync(connection, BLUEZ_SERVICE, addr->path,
	FREEDESKTOP_PROPERTIES, "Get",
			g_variant_new("(ss)", MEDIA_CONTROL1_INTERFACE, "Connected"), NULL,
			G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);

		g_error_free(error);
		return -1;
	}

	else {
		g_variant_get(value, "(v)", &variantValue);
		g_variant_get(variantValue, "b", &status);
		printf("Address: %s:%i",addr->bdaddr, status);
		return status;
	}

	return 0;

}

int isHFPConnected(struct btd_device *addr) {

	GDBusConnection *connection;

	GError *error = NULL;
	GVariant *value;
	gchar *ofono_path;
	gboolean status;

	GVariantIter *array;
	GVariant *var = NULL;
	const gchar *key = NULL;

	connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (NULL == connection) {
		D_PRINTF("GDBusconnection is NULL\n");
		return -1;
	}


//path=/hfp/org/bluez/hci0/dev_E0_98_61_7D_D3_1E; interface=org.ofono.Modem; member=GetProperties

	ofono_path = g_strconcat("/hfp", addr->path, NULL);


	value = g_dbus_connection_call_sync(connection, OFONO_SERVICE, ofono_path,
	OFONO_MODEM_INTERFACE, "GetProperties", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (NULL == value) {
		D_PRINTF ("Error getting object manager client: %s\n", error->message);

		g_error_free(error);
		return -1;
	}

	else {

		g_variant_get(value, "(a{sv})", &array);
		while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
			if (g_strcmp0(key, "Powered") == 0) {

				g_variant_get(var, "b", &status);

				return status;

			}
		}
		g_variant_iter_free(array);
		g_variant_unref(value);
		g_free(ofono_path);

		return status;
	}

	return 0;

}

GError* setHMIStatus(enum btStates state) {

    gchar *iconString = NULL;
    GDBusConnection *connection;
    GVariant *params = NULL;
    GVariant *message = NULL;
    GError *error = NULL;

    if (state==INACTIVE) iconString = "qrc:/images/Status/HMI_Status_Bluetooth_Inactive-01.png";
    else if (state==ACTIVE) iconString = "qrc:/images/Status/HMI_Status_Bluetooth_On-01.png";
    else iconString = "qrc:/images/Status/HMI_Status_Bluetooth_Inactive-01.png";

    connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);

    params = g_variant_new("(is)", HOMESCREEN_BT_ICON_POSITION, iconString);

    message = g_dbus_connection_call_sync(connection, HOMESCREEN_SERVICE,
    HOMESCREEN_ICON_PATH, HOMESCREEN_ICON_INTERFACE, "setStatusIcon", params,
            NULL, G_DBUS_CALL_FLAGS_NONE,
            DBUS_REPLY_TIMEOUT, NULL, &error);

    if (error) {
        printf("error: %s\n", error->message);

        return error;
    } else {
        return NULL;
    }

}

/************************************** The End Of File **************************************/