summaryrefslogtreecommitdiff
path: root/src/bluetooth.c
blob: 0207919cd7bc2c44b4199667033c0621afcab27b (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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
/*
 *
 *  neard - Near Field Communication manager
 *
 *  Copyright (C) 2011  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gdbus.h>

#include "near.h"

#define BLUEZ_SERVICE			"org.bluez"
#define MANAGER_INTF			BLUEZ_SERVICE ".Manager"
#define ADAPTER_INTF			BLUEZ_SERVICE ".Adapter"
#define OOB_INTF			BLUEZ_SERVICE ".OutOfBand"
#define DEFAULT_ADAPTER			"DefaultAdapter"
#define ADAPTER_REMOVED			"AdapterRemoved"
#define DEFAULT_ADAPTER_CHANGED		"DefaultAdapterChanged"
#define ADAPTER_PROPERTY_CHANGED	"PropertyChanged"
#define MANAGER_PATH			"/"
#define OOB_AGENT			"/org/neard/agent/neard_oob"

#define BT_NOINPUTOUTPUT		"NoInputNoOutput"
#define BT_DISPLAY_YESNO		"DisplayYesNo"

#define DBUS_MANAGER_INTF		"org.freedesktop.DBus.ObjectManager"
#define AGENT_REGISTER_TIMEOUT	2

/* BT EIR list */
#define EIR_UUID128_ALL		0x07 /* 128-bit UUID, all listed */
#define EIR_NAME_SHORT		0x08 /* shortened local name */
#define EIR_NAME_COMPLETE	0x09 /* complete local name */

/* Specific OOB EIRs */
#define EIR_CLASS_OF_DEVICE	0x0D  /* class of device */
#define EIR_SP_HASH		0x0E  /* simple pairing hash C */
#define EIR_SP_RANDOMIZER	0x0F  /* simple pairing randomizer R */
/* Optional EIRs */
#define EIR_DEVICE_ID		0x10  /* device ID */
#define EIR_SECURITY_MGR_FLAGS	0x11  /* security manager flags */

#define EIR_SIZE_LEN		1
#define EIR_HEADER_LEN		(EIR_SIZE_LEN + 1)
#define BT_ADDRESS_SIZE		6
#define COD_SIZE		3
#define OOB_SP_SIZE		16
#define EIR_SIZE_MAX		255

struct near_oob_data {
	char *def_adapter;

	char *bd_addr;		/* oob mandatory */

	/* optional */
	char *bt_name;			/* short or long name */
	uint8_t bt_name_len;
	int class_of_device;		/* Class of device */
	near_bool_t powered;
	near_bool_t pairable;
	near_bool_t discoverable;
	uint8_t *uuids;
	int uuids_len;

	uint8_t *spair_hash;			/* OOB hash Key */
	uint8_t *spair_randomizer;		/* OOB randomizer key */
	uint8_t authentication[OOB_SP_SIZE];	/* On BT 2.0 */
	uint8_t security_manager_oob_flags;	/* see BT Core 4.0 */
};

static DBusConnection *bt_conn;
static struct near_oob_data bt_def_oob_data;

static guint watch;
static guint removed_watch;
static guint adapter_watch;
static guint adapter_props_watch;

static guint register_bluez_timer;

static void __bt_eir_free(struct near_oob_data *oob)
{
	DBG("");

	if (oob->def_adapter != NULL) {
		g_free(oob->def_adapter);
		oob->def_adapter = NULL;
	}

	if (oob->bd_addr != NULL) {
		g_free(oob->bd_addr);
		oob->bd_addr = NULL;
	}

	if (oob->bt_name != NULL) {
		g_free(oob->bt_name);
		oob->bt_name = NULL;
	}

	if (oob->spair_hash != NULL) {
		g_free(oob->spair_hash);
		oob->spair_hash = NULL;
	}

	if (oob->spair_randomizer != NULL) {
		g_free(oob->spair_randomizer);
		oob->spair_randomizer = NULL;
	}
}

static void bt_eir_free(struct near_oob_data *oob)
{
	__bt_eir_free(oob);

	g_free(oob);
}

/* D-Bus helper functions */
static int bt_generic_call(DBusConnection *conn,
		struct near_oob_data *oob,		/* user data */
		const char *dest,			/* method call */
		const char *path,
		const char *interface,
		const char *method,
		DBusPendingCallNotifyFunction bt_cb,	/* callback */
		int type, ...)				/* params */
{
	DBusMessage *msg;
	DBusPendingCall *pending;
	va_list args;
	int err;

	DBG("%s", method);

	msg = dbus_message_new_method_call(dest, path, interface, method);

	if (msg == NULL) {
		near_error("Unable to allocate new D-Bus %s message", method);
		return -ENOMEM;
	}

	va_start(args, type);

	if (!dbus_message_append_args_valist(msg, type, args)) {
		va_end(args);
		err = -EIO;
		goto error_done;
	}
	va_end(args);

	if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
		near_error("Sending %s failed", method);
		err = -EIO;
		goto error_done;
	}

	if (pending == NULL) {
		near_error("D-Bus connection not available");
		err = -EIO;
		goto error_done;
	}

	/* Prepare for notification */
	dbus_pending_call_set_notify(pending, bt_cb, oob, NULL);
	err = 0 ;

error_done:
	dbus_message_unref(msg);
	return err;
}

static void bt_create_paired_device_cb(DBusPendingCall *pending,
					void *user_data)
{
	DBusMessage *reply;
	DBusError   error;
	struct near_oob_data *oob = user_data;

	DBG("");

	reply = dbus_pending_call_steal_reply(pending);
	if (reply == NULL)
		return;

	dbus_error_init(&error);

	if (dbus_set_error_from_message(&error, reply)) {
		near_error("%s", error.message);
		dbus_error_free(&error);
		goto cb_done;
	}

	DBG("Successful pairing");

cb_done:
	/* task completed - clean memory*/
	bt_eir_free(oob);

	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);
}

static int bt_create_paired_device(DBusConnection *conn,
						struct near_oob_data *oob,
						const char *capabilities)
{
	const char *agent_path = OOB_AGENT;

	return bt_generic_call(bt_conn, oob, BLUEZ_SERVICE,
			oob->def_adapter, ADAPTER_INTF, "CreatePairedDevice",
			bt_create_paired_device_cb,
			/* params */
			DBUS_TYPE_STRING, &oob->bd_addr,
			DBUS_TYPE_OBJECT_PATH, &agent_path,
			DBUS_TYPE_STRING, &capabilities,
			DBUS_TYPE_INVALID);
}

static void bt_oob_add_remote_data_cb(DBusPendingCall *pending, void *user_data)
{
	DBusMessage *reply;
	DBusError   error;
	struct near_oob_data *oob = user_data;

	DBG("");

	reply = dbus_pending_call_steal_reply(pending);
	if (reply == NULL)
		return;

	dbus_error_init(&error);

	if (dbus_set_error_from_message(&error, reply))
		goto cb_fail;

	near_info("OOB data added");

	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);

	/* Jump to the next: Pairing !!!*/
	DBG("Try to pair devices...");
	bt_create_paired_device(bt_conn, oob, BT_DISPLAY_YESNO);
	return;

cb_fail:
	near_error("%s", error.message);
	dbus_error_free(&error);

	bt_eir_free(oob);

	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);
}

static int bt_oob_add_remote_data(DBusConnection *conn,
						struct near_oob_data *oob)
{
	int16_t hash_len = 16;
	int16_t rdm_len = 16;

	return bt_generic_call(bt_conn, oob, BLUEZ_SERVICE,
			oob->def_adapter, OOB_INTF, "AddRemoteData",
			bt_oob_add_remote_data_cb,
			/* params */
			DBUS_TYPE_STRING, &oob->bd_addr,
			DBUS_TYPE_ARRAY,
				DBUS_TYPE_BYTE, &oob->spair_hash, hash_len,
			DBUS_TYPE_ARRAY,
				DBUS_TYPE_BYTE, &oob->spair_randomizer, rdm_len,
			DBUS_TYPE_INVALID);
}

/* Pairing: JustWorks or OOB  */
static int bt_do_pairing(struct near_oob_data *oob)
{
	int err = 0;

	DBG("%s", oob->bd_addr);

	/* Is this a *real* oob pairing or a "JustWork" */
	if ((oob->spair_hash) && (oob->spair_randomizer))
		err = bt_oob_add_remote_data(bt_conn, oob);
	else
		err = bt_create_paired_device(bt_conn, oob,
				BT_NOINPUTOUTPUT);

	if (err < 0)
		near_error("Pairing failed. Err[%d]", err);

	return err;
}

/*
 */
static int extract_properties(DBusMessage *reply, struct near_oob_data *oob)
{
	char *data = NULL;
	int idata;
	int i, j;

	DBusMessageIter array, dict;

	if (dbus_message_iter_init(reply, &array) == FALSE)
		return -1;

	if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
		return -1;

	dbus_message_iter_recurse(&array, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *key;

		dbus_message_iter_recurse(&dict, &entry);
		dbus_message_iter_get_basic(&entry, &key);

		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &value);

		if (g_str_equal(key, "Address") == TRUE) {
			dbus_message_iter_get_basic(&value, &data);

			/* Now, fill the local struct */
			oob->bd_addr = g_try_malloc0(BT_ADDRESS_SIZE);
			if (oob->bd_addr == NULL)
				return -ENOMEM;

			/* Address is like: "ff:ee:dd:cc:bb:aa" */
			for (i = 5, j = 0 ; i >= 0; i--, j += 3)
				oob->bd_addr[i] = strtol(data + j, NULL, 16);
			DBG("local address: %s", data);

		} else if (g_str_equal(key, "Name") == TRUE) {
			dbus_message_iter_get_basic(&value, &data);
			oob->bt_name = g_strdup(data);
			if (oob->bt_name != NULL) {
				oob->bt_name_len = strlen(oob->bt_name);
				DBG("local name: %s", oob->bt_name);
			}

		} else if (g_str_equal(key, "Class") == TRUE) {
			dbus_message_iter_get_basic(&value, &idata);
			oob->class_of_device = idata;

		} else if (g_str_equal(key, "Powered") == TRUE) {
			dbus_message_iter_get_basic(&value, &idata);
			oob->powered = idata;

		} else if (g_str_equal(key, "Discoverable") == TRUE) {
			dbus_message_iter_get_basic(&value, &idata);
			oob->discoverable = idata;

		} else if (g_str_equal(key, "Pairable") == TRUE) {
			dbus_message_iter_get_basic(&value, &idata);
			oob->pairable = idata;

		} else if (g_str_equal(key, "UUIDs") == TRUE) {
			oob->uuids_len = sizeof(value);
			oob->uuids = g_try_malloc0(oob->uuids_len);
			if (oob->uuids == NULL)
				return -ENOMEM;
			memcpy(oob->uuids, &value, oob->uuids_len);
		}

		dbus_message_iter_next(&dict);
	}

	return 0;
}

static int bt_parse_properties(DBusMessage *reply, void *user_data)
{
	struct near_oob_data *bt_props = user_data;

	DBG("");

	/* Free datas */
	g_free(bt_props->bd_addr);
	g_free(bt_props->bt_name);

	/* Grab properties from dbus */
	if (extract_properties(reply, bt_props) < 0)
		goto fail;

	return 0;

fail:
	g_free(bt_props->bd_addr);
	bt_props->bd_addr = NULL;

	g_free(bt_props->bt_name);
	bt_props->bt_name = NULL;

	return -ENOMEM;
}

static gboolean bt_adapter_property_changed(DBusConnection *conn,
							DBusMessage *message,
							void *user_data)
{
	DBusMessageIter iter;
	DBusMessageIter var;
	const char *property;

	if (dbus_message_iter_init(message, &iter) == FALSE)
		return TRUE;

	dbus_message_iter_get_basic(&iter, &property);
	dbus_message_iter_next(&iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
		return TRUE;

	dbus_message_iter_recurse(&iter, &var);

	if (g_str_equal(property, "Name") == TRUE) {
		const char *name;

		if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
			return TRUE;

		dbus_message_iter_get_basic(&iter, &name);

		g_free(bt_def_oob_data.bt_name);
		bt_def_oob_data.bt_name = g_strdup(name);

		if (bt_def_oob_data.bt_name != NULL)
			bt_def_oob_data.bt_name_len = strlen(name);
		else
			bt_def_oob_data.bt_name_len = 0;

		DBG("%s: %s", property, name);
	} else if (g_str_equal(property, "Class") == TRUE) {
		int class;

		if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_UINT32)
			return TRUE;

		dbus_message_iter_get_basic(&var, &class);
		bt_def_oob_data.class_of_device = class;

		DBG("%s: %x", property, bt_def_oob_data.class_of_device);
	} else if (g_str_equal(property, "Powered") == TRUE) {
		dbus_bool_t powered;

		if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
			return TRUE;

		dbus_message_iter_get_basic(&var, &powered);
		bt_def_oob_data.powered = powered;

		DBG("%s: %u", property, bt_def_oob_data.powered);
	}

	return TRUE;
}

/* Get default local adapter properties */
static void bt_get_properties_cb(DBusPendingCall *pending, void *user_data)
{
	struct near_oob_data *bt_props = user_data;
	DBusMessage *reply;
	DBusError   error;
	int err;

	DBG("");

	reply = dbus_pending_call_steal_reply(pending);
	if (reply == NULL)
		return;

	dbus_error_init(&error);

	if (dbus_set_error_from_message(&error, reply))
		goto cb_fail;

	err = bt_parse_properties(reply, bt_props);
	if (err < 0)
		near_error("Problem parsing local properties %d", err);
	else
		DBG("Get Properties complete: %s", bt_props->def_adapter);

	adapter_props_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
						ADAPTER_INTF,
						ADAPTER_PROPERTY_CHANGED,
						bt_adapter_property_changed,
						NULL, NULL);

	/* clean */
	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);
	return;

cb_fail:
	near_error("%s", error.message);
	dbus_error_free(&error);

	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);
}

static void bt_get_default_adapter_cb(DBusPendingCall *pending, void *user_data)
{
	struct near_oob_data *bt_props = user_data;
	DBusMessage *reply;
	DBusError   error;
	gchar *path;

	DBG("");

	reply = dbus_pending_call_steal_reply(pending);
	if (reply == NULL)
		return;

	dbus_error_init(&error);

	if (dbus_set_error_from_message(&error, reply))
		goto cb_fail;

	if (dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH,
					&path, DBUS_TYPE_INVALID) == FALSE)
		goto cb_fail;

	/* Save the default adapter */
	bt_props->def_adapter = g_strdup(path);
	DBG("Using default adapter %s", bt_props->def_adapter);

	/* clean */
	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);

	/* Jump on getAdapterProperties */
	bt_generic_call(bt_conn, bt_props,
			BLUEZ_SERVICE,
			bt_props->def_adapter,
			ADAPTER_INTF, "GetProperties",
			bt_get_properties_cb,
			DBUS_TYPE_INVALID);
	return;

cb_fail:
	near_error("%s", error.message);
	dbus_error_free(&error);

	dbus_message_unref(reply);
	dbus_pending_call_unref(pending);
}

static int bt_refresh_adapter_props(DBusConnection *conn, void *user_data)
{
	DBG("%p %p", conn, user_data);

	return bt_generic_call(conn, user_data,
			BLUEZ_SERVICE,
			MANAGER_PATH, MANAGER_INTF,
			DEFAULT_ADAPTER,
			bt_get_default_adapter_cb,
			DBUS_TYPE_INVALID);
}

/* Parse and fill the bluetooth oob information block */
static void bt_parse_eir(uint8_t *eir_data, uint16_t eir_data_len,
				struct near_oob_data *oob, uint16_t *props)
{
	char *tmp;
	uint16_t len = 0;

	DBG("total len: %u", eir_data_len);

	while (len < eir_data_len - 1) {
		uint8_t eir_len = eir_data[0];	/* EIR field length */
		uint8_t eir_code;		/* EIR field type*/
		uint8_t data_len;		/* EIR data length */
		uint8_t *data;

		/* check for early termination */
		if (eir_len == 0)
			break;

		len += eir_len + 1;

		/* Do not continue EIR Data parsing if got incorrect length */
		if (len > eir_data_len)
			break;

		data_len = eir_len - 1;

		eir_code = eir_data[1]; /* EIR code */
		data = &eir_data[2];

		DBG("type 0x%.2X data_len %u", eir_code, data_len);

		switch (eir_code) {
		case EIR_NAME_SHORT:
		case EIR_NAME_COMPLETE:
			oob->bt_name = g_try_malloc0(data_len + 1); /* eos */
			if (oob->bt_name) {
				oob->bt_name_len = data_len;
				memcpy(oob->bt_name, data, oob->bt_name_len);
				oob->bt_name[data_len] = 0;	/* end str*/
			}
			break;

		case EIR_CLASS_OF_DEVICE:
			tmp = g_strdup_printf("%02X%02X%02X",
					*data, *(data + 1), *(data + 2));
			if (tmp != NULL) {
				oob->class_of_device = strtol(tmp, NULL, 16);
				*props |= OOB_PROPS_COD;
			}
			g_free(tmp);
			break;

		case EIR_SP_HASH:
			oob->spair_hash = g_try_malloc0(OOB_SP_SIZE);
			if (oob->spair_hash) {
				memcpy(oob->spair_hash, data, OOB_SP_SIZE);
				*props |= OOB_PROPS_SP_HASH;
			}
			break;

		case EIR_SP_RANDOMIZER:
			oob->spair_randomizer = g_try_malloc0(OOB_SP_SIZE);
			if (oob->spair_randomizer) {
				memcpy(oob->spair_randomizer,
						data, OOB_SP_SIZE);
				*props |= OOB_PROPS_SP_RANDOM;
			}
			break;

		case EIR_SECURITY_MGR_FLAGS:
			oob->security_manager_oob_flags = *data;
			break;

		case EIR_UUID128_ALL:
			/* TODO: Process uuids128
			 * */
			break;

		default:	/* ignore and skip */
			near_error("Unknown EIR x%02x (len: %d)", eir_code,
								eir_len);
			break;
		}
		/* Next eir */
		eir_data += eir_len + 1;
	}
}

/*
 * Because of some "old" implementation, "version" will help
 * to determine the record data structure.
 * Some specifications are proprietary (eg. "short mode")
 * and are not fully documented.
 * mime_properties is a bitmask and should reflect the fields found in
 * the incoming oob.
 */
int __near_bluetooth_parse_oob_record(struct carrier_data *data,
						uint16_t *mime_properties,
						near_bool_t pair)
{
	struct near_oob_data *oob;
	uint16_t bt_oob_data_size;
	uint8_t *ptr = data->data;
	uint8_t	marker;
	char *tmp;

	DBG("");

	oob = g_try_malloc0(sizeof(struct near_oob_data));

	if (data->type == BT_MIME_V2_1) {
		/*
		 * Total OOB data size (including size bytes)
		 * Some implementations (e.g. Android 4.1) stores
		 * the data_size in big endian but NDEF forum spec (BT Secure
		 * Simple Pairing) requires a little endian. At the same time,
		 * the NDEF forum NDEF spec define a payload length as single
		 * byte (and the payload size IS the oob data size).
		 */
		bt_oob_data_size = near_get_le16(ptr);
		if (bt_oob_data_size > 0xFF)	/* Big Endian */
			bt_oob_data_size = GUINT16_FROM_BE(bt_oob_data_size);

		bt_oob_data_size -= 2 ; /* remove oob datas size len */

		/* First item: BD_ADDR (mandatory) */
		ptr = &data->data[2];
		oob->bd_addr = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
				ptr[5],	ptr[4], ptr[3], ptr[2], ptr[1], ptr[0]);

		/* Skip to the next element (optional) */
		ptr += BT_ADDRESS_SIZE;
		bt_oob_data_size -= BT_ADDRESS_SIZE ;

		if (bt_oob_data_size)
			bt_parse_eir(ptr, bt_oob_data_size, oob,
							mime_properties);
	} else if (data->type == BT_MIME_V2_0) {
		marker = *ptr++;	/* could be '$' */

		oob->bd_addr = g_strdup_printf(
				"%02X:%02X:%02X:%02X:%02X:%02X",
				ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
		ptr = ptr + BT_ADDRESS_SIZE;

		/* Class of device */
		tmp = g_strdup_printf("%02X%02X%02X",
				*ptr, *(ptr + 1), *(ptr + 2));
		if (tmp != NULL)
			oob->class_of_device = strtol(tmp, NULL, 16);
		g_free(tmp);

		ptr = ptr + 3;

		/* "Short mode" seems to use a 4 bytes code
		 * instead of 16 bytes...
		 */
		if (marker == '$') {   /* Short NFC */
			memcpy(oob->authentication, ptr, 4);
			ptr = ptr + 4;
		} else {
			memcpy(oob->authentication, ptr, 16);
			ptr = ptr + 16;
		}

		/* get the device name */
		oob->bt_name_len = *ptr++;
		oob->bt_name = g_try_malloc0(oob->bt_name_len+1);
		if (oob->bt_name) {
			memcpy(oob->bt_name, ptr, oob->bt_name_len);
			oob->bt_name[oob->bt_name_len+1] = 0;
		}
		ptr = ptr + oob->bt_name_len;
	} else {
		return -EINVAL;
	}

	if (pair == FALSE)
		return 0;

	/* check and get the default adapter */
	oob->def_adapter = g_strdup(bt_def_oob_data.def_adapter);
	if (oob->def_adapter == NULL) {
		near_error("bt_get_default_adapter failed");
		bt_eir_free(oob);
		return -EIO;
	}

	return  bt_do_pairing(oob);
}

int __near_bluetooth_pair(void *data)
{
	struct near_oob_data *oob = data;

	/* check and get the default adapter */
	oob->def_adapter = g_strdup(bt_def_oob_data.def_adapter);
	if (oob->bt_name == NULL) {
		near_error("bt_get_default_adapter failed: %d", -EIO);
		bt_eir_free(oob);
		return -EIO;
	}

	return bt_do_pairing(oob);
}

/* This function is synchronous as oob datas change on each session */
static int bt_sync_oob_readlocaldata(DBusConnection *conn, char *adapter_path,
							char *spair_hash,
							char *spair_randomizer)
{
	DBusMessage *message, *reply;
	DBusError error;
	int hash_len, rndm_len;

	message = dbus_message_new_method_call(BLUEZ_SERVICE, adapter_path,
			OOB_INTF, "ReadLocalData");
	if (!message)
		return 0;

	dbus_error_init(&error);

	reply = dbus_connection_send_with_reply_and_block(conn,
			message, -1, &error);

	dbus_message_unref(message);

	if (!reply) {
		if (dbus_error_is_set(&error) == TRUE) {
			near_error("%s", error.message);
			dbus_error_free(&error);
		} else {
			near_error("Failed to set property");
		}
		return 0;
	}

	if (dbus_message_get_args(reply, NULL,
			DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, spair_hash, &hash_len,
			DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
						spair_randomizer, &rndm_len,
			DBUS_TYPE_INVALID) == FALSE)
		goto done;

	if ((hash_len != OOB_SP_SIZE) || (rndm_len != OOB_SP_SIZE)) {
		DBG("no OOB data found !");
		goto done;
	}

	dbus_message_unref(reply);
	DBG("OOB data found");
	return hash_len;

done:
	dbus_message_unref(reply);
	return 0;
}

/*
 * External API to get bt properties
 * Prepare a "real" oob datas block
 * mime_props is a bitmask we use to add or not specific fields in the
 * oob frame (e.g.: OOB keys)
 * */
struct carrier_data *__near_bluetooth_local_get_properties(uint16_t mime_props)
{
	struct carrier_data *data = NULL;
	uint8_t offset;

	char hash[OOB_SP_SIZE];
	char random[OOB_SP_SIZE];

	/* Check adapter datas */
	if (bt_def_oob_data.def_adapter == NULL) {
		near_error("No bt adapter info");
		goto fail;
	}

	data = g_try_malloc0(sizeof(*data));
	if (data == NULL)
		goto fail;

	data->size = sizeof(uint16_t)	/* stored oob size */
			+ BT_ADDRESS_SIZE;	/* device address */

	offset = sizeof(uint16_t); /* Skip size...will be filled later */

	/* Now prepare data frame */
	memcpy(data->data + offset, bt_def_oob_data.bd_addr, BT_ADDRESS_SIZE);
	offset += BT_ADDRESS_SIZE;

	/* CoD */
	data->size += COD_SIZE +  EIR_HEADER_LEN;

	data->data[offset++] = COD_SIZE + EIR_SIZE_LEN;
	data->data[offset++] = EIR_CLASS_OF_DEVICE;

	memcpy(data->data + offset,
			(uint8_t *)&bt_def_oob_data.class_of_device, COD_SIZE);
	offset += COD_SIZE;

	/*
	 * The following data are generated dynamically so we have to read the
	 * local oob data. Only add OOB pairing keys if needed.
	 */
	if ((mime_props & OOB_PROPS_SP) != 0 &&
			bt_sync_oob_readlocaldata(bt_conn,
					bt_def_oob_data.def_adapter,
					hash, random) == OOB_SP_SIZE) {
		data->size += 2 * (OOB_SP_SIZE + EIR_HEADER_LEN);

		/* OOB datas */
		if (hash != NULL) {
			data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN;
			data->data[offset++] = EIR_SP_HASH;
			memcpy(data->data + offset, hash, OOB_SP_SIZE);
			offset += OOB_SP_SIZE;
		}

		if (random != NULL) {
			data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN;
			data->data[offset++] = EIR_SP_RANDOMIZER;
			memcpy(data->data + offset, random, OOB_SP_SIZE);
			offset += OOB_SP_SIZE;
		}
	}

	/* bt name */
	if (bt_def_oob_data.bt_name != NULL) {
		int name_len;

		data->size += EIR_HEADER_LEN;

		if (data->size + bt_def_oob_data.bt_name_len
				> EIR_SIZE_MAX) {
			name_len = EIR_SIZE_MAX - data->size;
			data->data[offset++] = name_len + EIR_SIZE_LEN;
			/* EIR data type */
			data->data[offset++] = EIR_NAME_COMPLETE;
		} else {
			name_len = bt_def_oob_data.bt_name_len;
			data->data[offset++] = name_len + EIR_SIZE_LEN;
			/* EIR data type */
			data->data[offset++] = EIR_NAME_SHORT;
		}

		data->size += name_len;
		memcpy(data->data + offset, bt_def_oob_data.bt_name, name_len);
		offset += name_len;
	}

	data->data[0] = data->size ;

	if (bt_def_oob_data.powered == TRUE)
		data->state = CPS_ACTIVE;
	else
		data->state = CPS_INACTIVE;

	return data;

fail:
	g_free(data);
	return NULL;
}

/* BT adapter removed handler */
static gboolean bt_adapter_removed(DBusConnection *conn, DBusMessage *message,
							void *user_data)
{
	DBusMessageIter iter;
	struct near_oob_data *bt_props = user_data;
	const char *adapter_path;

	DBG("");

	if (bt_props->def_adapter == NULL)
		return TRUE;

	g_dbus_remove_watch(bt_conn, adapter_props_watch);
	adapter_props_watch = 0;

	if (dbus_message_iter_init(message, &iter) == FALSE)
		return TRUE;

	dbus_message_iter_get_basic(&iter, &adapter_path);

	if (g_strcmp0(adapter_path, bt_props->def_adapter) == 0) {
		near_info("Remove the default adapter [%s]", adapter_path);

		__bt_eir_free(bt_props);
		bt_props->def_adapter = NULL;
	}

	return TRUE;
}

/* BT default adapter changed handler */
static gboolean bt_default_adapter_changed(DBusConnection *conn,
					DBusMessage *message,
					void *user_data)
{
	struct near_oob_data *bt_props = user_data;
	DBusMessageIter iter;
	const char *adapter_path;

	DBG("");

	if (dbus_message_iter_init(message, &iter) == FALSE)
		return TRUE;

	g_dbus_remove_watch(bt_conn, adapter_props_watch);
	adapter_props_watch = 0;

	dbus_message_iter_get_basic(&iter, &adapter_path);
	DBG("New default adapter [%s]", adapter_path);

	/* Disable the old one */
	__bt_eir_free(bt_props);
	bt_props->def_adapter = NULL;

	/* Refresh */
	bt_refresh_adapter_props(conn, user_data);

	return TRUE;
}

static void bt_dbus_disconnect_cb(DBusConnection *conn, void *user_data)
{
	near_error("D-Bus disconnect (BT)");
	bt_conn = NULL;
}

static gboolean register_bluez(gpointer user_data)
{
	DBG("");

	register_bluez_timer = 0;

	removed_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
						MANAGER_INTF,
						ADAPTER_REMOVED,
						bt_adapter_removed,
						&bt_def_oob_data, NULL);


	adapter_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
						MANAGER_INTF,
						DEFAULT_ADAPTER_CHANGED,
						bt_default_adapter_changed,
						&bt_def_oob_data, NULL);

	if (removed_watch == 0 || adapter_watch == 0) {
		near_error("BlueZ event handlers failed to register.");
		g_dbus_remove_watch(bt_conn, removed_watch);
		g_dbus_remove_watch(bt_conn, adapter_watch);

		return FALSE;
	}

	if (bt_refresh_adapter_props(bt_conn, user_data) < 0)
		near_error("Failed to get BT adapter properties");

	return FALSE;
}

static void bt_connect(DBusConnection *conn, void *data)
{
	DBG("connection %p with %p", conn, data);

	if (__near_agent_handover_registered(HO_AGENT_BT) == TRUE) {
		DBG("Agent already registered");
		return;
	}

	/*
	 * BlueZ 5 will register itself as HandoverAgent, give it some time
	 * to do it before going legacy way.
	 */
	register_bluez_timer = g_timeout_add_seconds(AGENT_REGISTER_TIMEOUT,
							register_bluez, data);
}

static void bt_disconnect(DBusConnection *conn, void *user_data)
{
	DBG("%p", conn);

	/* If timer is running no BlueZ watchers were registered yet */
	if (register_bluez_timer > 0) {
		g_source_remove(register_bluez_timer);
		register_bluez_timer = 0;
		return;
	}

	__bt_eir_free(user_data);

	g_dbus_remove_watch(bt_conn, removed_watch);
	removed_watch = 0;

	g_dbus_remove_watch(bt_conn, adapter_watch);
	adapter_watch = 0;

	g_dbus_remove_watch(bt_conn, adapter_props_watch);
	adapter_props_watch = 0;
}

static int bt_prepare_handlers(DBusConnection *conn)
{
	if (__near_agent_handover_registered(HO_AGENT_BT) == TRUE)
		return 0;

	watch = g_dbus_add_service_watch(bt_conn, BLUEZ_SERVICE,
						bt_connect,
						bt_disconnect,
						&bt_def_oob_data, NULL);
	if (watch == 0) {
		near_error("BlueZ service watch handler failed to register.");
		g_dbus_remove_watch(bt_conn, watch);
		return -EIO;
	}

	return 0;
}

void __near_bluetooth_legacy_start(void)
{
	DBG("");

	bt_prepare_handlers(bt_conn);
}

void __near_bluetooth_legacy_stop(void)
{
	DBG("");

	g_dbus_remove_watch(bt_conn, watch);
	watch = 0;

	bt_disconnect(bt_conn, &bt_def_oob_data);
}

/* Bluetooth exiting function */
void __near_bluetooth_cleanup(void)
{
	DBG("");

	if (bt_conn == NULL)
		return;

	__near_bluetooth_legacy_stop();

	dbus_connection_unref(bt_conn);
}

/*
 * Bluetooth initialization function.
 *	Allocate bt local settings storage
 *	and setup event handlers
 */
int __near_bluetooth_init(void)
{
	DBusError err;

	DBG("");

	dbus_error_init(&err);

	/* save the dbus connection */
	bt_conn = near_dbus_get_connection();
	if (bt_conn == NULL) {
		if (dbus_error_is_set(&err) == TRUE) {
			near_error("%s", err.message);
			dbus_error_free(&err);
		} else
			near_error("Can't register with system bus\n");
		return -EIO;
	}

	/* dbus disconnect callback */
	g_dbus_set_disconnect_function(bt_conn, bt_dbus_disconnect_cb,
						NULL, NULL);

	/* Set bluez event handlers */
	return bt_prepare_handlers(bt_conn);
}