summaryrefslogtreecommitdiff
path: root/src/bluetooth-device.c
blob: 9c33d096a152b2dd61b2f07f69d13c427420f071 (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
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 <glib.h>
#include <dlog.h>
#include <stdio.h>
#include <bluetooth-api.h>

#include "bluetooth.h"
#include "bluetooth_internal.h"
#include "bluetooth_private.h"

typedef struct {
	unsigned int uuid;
	bt_service_class_t service_mask;
} bt_uuid2svc_table_t;

static bt_uuid2svc_table_t uuid2svc_mask[] = {
	{ BLUETOOTH_SPP_PROFILE_UUID, BT_SC_SPP_SERVICE_MASK },
	{ BLUETOOTH_LAP_PROFILE_UUID, BT_SC_LAP_SERVICE_MASK },
	{ BLUETOOTH_DUN_PROFILE_UUID, BT_SC_DUN_SERVICE_MASK },
	{ BLUETOOTH_OBEX_IR_MC_SYNC_SERVICE_UUID, BT_SC_SYNC_SERVICE_MASK },
	{ BLUETOOTH_OBEX_OBJECT_PUSH_SERVICE_UUID, BT_SC_OPP_SERVICE_MASK },
	{ BLUETOOTH_OBEX_MESSAGE_ACCESS_SERVICE_UUID, BT_SC_MAP_SERVICE_MASK },
	{ BLUETOOTH_OBEX_FILE_TRANSFER_UUID, BT_SC_FTP_SERVICE_MASK },
	{ BLUETOOTH_HS_PROFILE_UUID, BT_SC_HSP_SERVICE_MASK },
	{ BLUETOOTH_CTP_PROFILE_UUID, BT_SC_CTP_SERVICE_MASK  },
	{ BLUETOOTH_AUDIO_SOURCE_UUID, BT_SC_A2DP_SOURCE_SERVICE_MASK },
	{ BLUETOOTH_AUDIO_SINK_UUID, BT_SC_A2DP_SERVICE_MASK },
	{ BLUETOOTH_AV_REMOTE_CONTROL_UUID, BT_SC_AVRCP_SERVICE_MASK },
	{ BLUETOOTH_ICP_PROFILE_UUID, BT_SC_ICP_SERVICE_MASK },
	{ BLUETOOTH_FAX_PROFILE_UUID, BT_SC_FAX_SERVICE_MASK },
	{ BLUETOOTH_PAN_PANU_PROFILE_UUID, BT_SC_PANU_SERVICE_MASK },
	{ BLUETOOTH_PAN_NAP_PROFILE_UUID, BT_SC_NAP_SERVICE_MASK },
	{ BLUETOOTH_PAN_GN_PROFILE_UUID, BT_SC_GN_SERVICE_MASK },
	{ BLUETOOTH_OBEX_IMAGING_RESPONDER_UUID, BT_SC_BIP_SERVICE_MASK },
	{ BLUETOOTH_HF_PROFILE_UUID, BT_SC_HFP_SERVICE_MASK },
	{ BLUETOOTH_HID_PROFILE_UUID, BT_SC_HID_SERVICE_MASK },
	{ BLUETOOTH_SIM_ACCESS_PROFILE_UUID, BT_SC_SAP_SERVICE_MASK },
	{ BLUETOOTH_OBEX_PBA_PROFILE_UUID, BT_SC_PBAP_SERVICE_MASK },
	{ BLUETOOTH_OBEX_BPPS_PROFILE_UUID, BT_SC_BPP_SERVICE_MASK },
	{ BLUETOOTH_OBEX_PRINTING_STATUS_UUID, BT_SC_BPP_SERVICE_MASK },
	{ BLUETOOTH_PNP_INFORMATION_UUID, BT_SC_NONE },
	{ BLUETOOTH_AV_REMOTE_CONTROL_TARGET_UUID, BT_SC_NONE },
	{ BLUETOOTH_VIDEO_SINK_UUID, BT_SC_NONE },
	{ BLUETOOTH_VIDEO_SOURCE_UUID, BT_SC_NONE },
	{ BLUETOOTH_HEADSET_AG_SERVICE_UUID, BT_SC_NONE },
	{ BLUETOOTH_REFERENCE_PRINTING, BT_SC_NONE },
	{ BLUETOOTH_OBEX_IMAGING_UUID, BT_SC_NONE },
	{ BLUETOOTH_HFG_PROFILE_UUID, BT_SC_NONE },
	{ BLUETOOTH_DIRECT_PRINTING_REFERENCE_OBJ_UUID, BT_SC_NONE },
	{ BLUETOOTH_BASIC_PRINTING, BT_SC_NONE },
	{ BLUETOOTH_HCR_PROFILE_UUID, BT_SC_NONE },
	{ BLUETOOTH_OBEX_SYNCML_TRANSFER_UUID, BT_SC_NONE },
	{ 0, BT_SC_NONE }
};


int bt_device_create_bond(const char *device_address)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address); /* LCOV_EXCL_START */
	error_code = _bt_get_error_code(bluetooth_bond_device(&addr_hex));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_device_create_bond_by_type(const char *device_address,
			bt_device_connection_link_type_e conn_type)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	error_code = _bt_get_error_code(bluetooth_bond_device_by_type(&addr_hex, conn_type));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
} /* LCOV_EXCL_STOP */

int bt_device_cancel_bonding(void)
{
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	error_code = _bt_get_error_code(bluetooth_cancel_bonding()); /* LCOV_EXCL_LINE */
	if (error_code != BT_ERROR_NONE) { /* LCOV_EXCL_LINE */
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code); /* LCOV_EXCL_LINE */
	}

	return error_code; /* LCOV_EXCL_LINE */
}

int bt_device_destroy_bond(const char *device_address)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address); /* LCOV_EXCL_LINE */

	_bt_convert_address_to_hex(&addr_hex, device_address); /* LCOV_EXCL_LINE */
	error_code = _bt_get_error_code(bluetooth_unbond_device(&addr_hex)); /* LCOV_EXCL_LINE */
	if (error_code != BT_ERROR_NONE) { /* LCOV_EXCL_LINE */
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code); /* LCOV_EXCL_LINE */
	}

	return error_code; /* LCOV_EXCL_LINE */
}

int bt_device_set_alias(const char *device_address, const char *alias)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);
	BT_CHECK_INPUT_PARAMETER(alias); /* LCOV_EXCL_LINE */

	_bt_convert_address_to_hex(&addr_hex, device_address); /* LCOV_EXCL_LINE */
	error_code = _bt_get_error_code(bluetooth_set_alias(&addr_hex, alias)); /* LCOV_EXCL_LINE */
	if (error_code != BT_ERROR_NONE) { /* LCOV_EXCL_LINE */
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code); /* LCOV_EXCL_LINE */
	}
	return error_code; /* LCOV_EXCL_LINE */
}

int bt_device_set_authorization(const char *device_address, bt_device_authorization_e authorization)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	gboolean trusted = FALSE;
	int error_code = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address); /* LCOV_EXCL_LINE */
	if (authorization == BT_DEVICE_AUTHORIZED) /* LCOV_EXCL_LINE */
		trusted = TRUE; /* LCOV_EXCL_LINE */

	error_code = _bt_get_error_code(bluetooth_authorize_device(&addr_hex, trusted)); /* LCOV_EXCL_LINE */
	if (error_code != BT_ERROR_NONE) { /* LCOV_EXCL_LINE */
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code); /* LCOV_EXCL_LINE */
	}
	return error_code; /* LCOV_EXCL_LINE */
}

int bt_device_start_service_search(const char *device_address)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int ret = 0;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address); /* LCOV_EXCL_START */
	ret = _bt_get_error_code(bluetooth_search_service(&addr_hex));

	/* In service search, BT_ERROR_SERVICE_SEARCH_FAILED is returned instead of BT_ERROR_OPERATION_FAILED. */
	if (ret == BT_ERROR_OPERATION_FAILED)
		ret = BT_ERROR_SERVICE_SEARCH_FAILED;

	if (ret != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret;
}

int bt_device_cancel_service_search(void)
{
	int ret = 0;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	ret = _bt_get_error_code(bluetooth_cancel_service_search());
	if (ret != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret; /* LCOV_EXCL_STOP */
}

int bt_device_foreach_connected_profiles(const char *remote_address, bt_device_connected_profile callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	gboolean is_connected = FALSE;
	int ret;
	int i = 0;
	int profiles[] = {BT_PROFILE_RFCOMM, BT_PROFILE_A2DP,
			BT_PROFILE_HSP, BT_PROFILE_HID, BT_PROFILE_NAP,
			BT_PROFILE_AG, BT_PROFILE_GATT, BT_PROFILE_NAP_SERVER,
			0};

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(callback); /* LCOV_EXCL_LINE */

	_bt_convert_address_to_hex(&addr_hex, remote_address); /* LCOV_EXCL_LINE */

	while (profiles[i] != 0) {
		ret = _bt_get_error_code(bluetooth_is_device_connected(&addr_hex,
								profiles[i],
								&is_connected));
		if (ret != BT_ERROR_NONE) { /* LCOV_EXCL_LINE */
			BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret); /* LCOV_EXCL_LINE */
			return ret;
		}

		if (is_connected == TRUE) { /* LCOV_EXCL_LINE */
			if (!callback(profiles[i], user_data)) /* LCOV_EXCL_LINE */
				break;
		}

		i++; /* LCOV_EXCL_LINE */
	}

	return BT_ERROR_NONE;
}

int bt_device_is_profile_connected(const char *remote_address, bt_profile_e bt_profile,
				bool *connected_status)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int ret;
	gboolean is_connected = FALSE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_is_device_connected(&addr_hex, bt_profile,
								&is_connected));
	*connected_status = is_connected ? true : false;

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret); /* LCOV_EXCL_LINE */
		return ret;
	}

	return ret;

}

int bt_device_set_bond_created_cb(bt_device_bond_created_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_BOND_CREATED, callback, user_data);

	BT_DBG("+");

	return BT_ERROR_NONE;
}

int bt_device_set_bond_destroyed_cb(bt_device_bond_destroyed_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_BOND_DESTROYED, callback, user_data);

	BT_DBG("+");

	return BT_ERROR_NONE;
}

int bt_device_set_authorization_changed_cb(bt_device_authorization_changed_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_AUTHORIZATION_CHANGED, callback, user_data);

	return BT_ERROR_NONE;
}

int bt_device_set_service_searched_cb(bt_device_service_searched_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_SERVICE_SEARCHED, callback, user_data);

	return BT_ERROR_NONE;
}
/* LCOV_EXCL_START */
int bt_device_get_connection_state(const char *remote_address,
		bt_device_connection_link_type_e link_type, bool *connected)
{
	int ret;
	bluetooth_device_address_t addr_hex = { {0,} };
	bluetooth_connected_link_t connected_link = BLUETOOTH_CONNECTED_LINK_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(connected);

	if (link_type != BT_DEVICE_CONNECTION_LINK_BREDR &&
	    link_type != BT_DEVICE_CONNECTION_LINK_LE)
		return BT_ERROR_INVALID_PARAMETER;

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_get_connected_link_type(&addr_hex, &connected_link));
	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x) : Failed to run function",
				_bt_convert_error_to_string(ret), ret);
		return ret;
	}

	if (connected_link == BLUETOOTH_CONNECTED_LINK_NONE) {
		*connected = false;
		return BT_ERROR_NONE;
	}

	if (connected_link == BLUETOOTH_CONNECTED_LINK_BREDR_LE) {
		*connected = true;
		return BT_ERROR_NONE;
	}

	if (link_type == BT_DEVICE_CONNECTION_LINK_BREDR) {
		if (connected_link == BLUETOOTH_CONNECTED_LINK_BREDR)
			*connected = true;
		else
			*connected = false;
	} else if (link_type == BT_DEVICE_CONNECTION_LINK_LE) {
		if (connected_link == BLUETOOTH_CONNECTED_LINK_LE)
			*connected = true;
		else
			*connected = false;
	}

	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */
int bt_device_set_connection_state_changed_cb(bt_device_connection_state_changed_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_DEVICE_CONNECTION_STATUS, callback, user_data);

	return BT_ERROR_NONE;
}

/* LCOV_EXCL_START */
int bt_device_set_att_mtu_changed_cb(bt_device_att_mtu_changed_cb callback, void *user_data)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_GATT_ATT_MTU_CHANGE_STATUS, callback, user_data);

	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */

int bt_device_unset_bond_created_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_BOND_CREATED);

	BT_DBG("+");

	return BT_ERROR_NONE;
}

int bt_device_unset_bond_destroyed_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_BOND_DESTROYED);

	BT_DBG("+");

	return BT_ERROR_NONE;
}

int bt_device_unset_authorization_changed_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_AUTHORIZATION_CHANGED);
	return BT_ERROR_NONE;
}

int bt_device_unset_service_searched_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_SERVICE_SEARCHED);
	return BT_ERROR_NONE;
}

int bt_device_unset_connection_state_changed_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_DEVICE_CONNECTION_STATUS);
	return BT_ERROR_NONE;
}

/* LCOV_EXCL_START */
int bt_device_unset_rssi_alert_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_RSSI_ALERT_EVENT);
	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */

/* LCOV_EXCL_START */
int bt_device_unset_att_mtu_changed_cb(void)
{
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_GATT_ATT_MTU_CHANGE_STATUS);
	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */

/* LCOV_EXCL_START */
int bt_device_enable_rssi_monitor(const char *remote_address,
				bt_device_connection_link_type_e link_type,
				int low_threshold, int in_range_threshold,
				bt_rssi_monitor_enabled_cb cb_enable,
				void *user_data_enable,
				bt_rssi_alert_cb cb_alert,
				void *user_data_alert)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;
	bt_rssi_threshold_t rssi_threshold = { 0, };
	BT_DBG("Enable RSSI");
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);
	rssi_threshold.high_threshold = 127;
	rssi_threshold.low_threshold = low_threshold;
	rssi_threshold.in_range_threshold = in_range_threshold;

	error_code = _bt_get_error_code(bluetooth_enable_rssi(&addr_hex, link_type, &rssi_threshold));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
	} else {
		_bt_set_cb(BT_EVENT_RSSI_ENABLED_EVENT, cb_enable, user_data_enable);
		_bt_set_cb(BT_EVENT_RSSI_ALERT_EVENT, cb_alert, user_data_alert);
	}

	return error_code;
}

int bt_device_disable_rssi_monitor(const char *remote_address,
				bt_device_connection_link_type_e link_type,
				bt_rssi_monitor_enabled_cb cb_disable,
				void *user_data_disable)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;
	bt_rssi_threshold_t rssi_threshold = { 0, };
	BT_DBG("Enable RSSI");
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);
	error_code = _bt_get_error_code(bluetooth_enable_rssi(&addr_hex, link_type, &rssi_threshold));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
	else
		_bt_set_cb(BT_EVENT_RSSI_ENABLED_EVENT, cb_disable, user_data_disable);

	return error_code;
}

int bt_device_get_rssi_strength(const char *remote_address,
				bt_device_connection_link_type_e link_type,
				bt_rssi_strength_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;
	BT_DBG("Get RSSI Strength");
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);
	error_code = _bt_get_error_code(bluetooth_get_rssi_strength(&addr_hex, link_type));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
	else
		_bt_set_cb(BT_EVENT_GET_RSSI_EVENT, callback, user_data);

	return error_code;
}


int bt_device_le_conn_update(const char *device_address,
	const bt_le_conn_update_s *parameters)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bluetooth_le_connection_param_t param = { 0 };
	int ret = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);
	BT_CHECK_INPUT_PARAMETER(parameters);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	param.interval_min = parameters->interval_min;
	param.interval_max = parameters->interval_max;
	param.latency = parameters->latency;
	param.timeout = parameters->time_out;

	ret = _bt_get_error_code(bluetooth_le_conn_update(&addr_hex, &param));

	if (BT_ERROR_NONE != ret)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret;
}
/* LCOV_EXCL_STOP */

static bt_service_class_t __bt_device_get_service_mask_value(unsigned int service)
{
	int i;

	for (i = 0; uuid2svc_mask[i].uuid != 0; i++) {
		if (uuid2svc_mask[i].uuid == service)
			return uuid2svc_mask[i].service_mask;
	}

	return BT_SC_NONE; /* LCOV_EXCL_LINE */
}

int bt_device_get_service_mask_from_uuid_list(char **uuids,
				      int no_of_service,
				      bt_service_class_t *service_mask_list)
{
	int i = 0;
	unsigned int service = 0;
	char **parts = NULL;
	bt_service_class_t service_mask = 0;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INPUT_PARAMETER(uuids);
	BT_CHECK_INPUT_PARAMETER(service_mask_list);
	BT_CHECK_INIT_STATUS();

	BT_DBG("Get service mask from uuid list");
	BT_DBG("no_of_service = %d", no_of_service);

	for (i = 0; i < no_of_service; i++) {
		parts = g_strsplit(uuids[i], "-", -1);

		if (parts == NULL || parts[0] == NULL) {
			g_strfreev(parts); /* LCOV_EXCL_LINE */
			continue; /* LCOV_EXCL_LINE */
		}

		service = g_ascii_strtoull(parts[0], NULL, 16);
		g_strfreev(parts);

		service_mask |= __bt_device_get_service_mask_value(service);
	}

	*service_mask_list = service_mask;
	BT_DBG("service_mask = %x, service_mask_list = %x", (int)(uintptr_t)service_mask,
	       (int)(uintptr_t)service_mask_list);

	return BT_ERROR_NONE;
}

/* LCOV_EXCL_START */
int bt_device_set_pin_code(const char *remote_address, const char *pin_code)
{
	int error_code = BT_ERROR_NONE;
	bluetooth_device_address_t addr_hex = { {0,} };
	bluetooth_device_pin_code_t pin = { {0} };

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(pin_code);
	if (strlen(pin_code) > BLUETOOTH_PIN_CODE_MAX_LENGTH) {
		BT_ERR("INVALID_PARAMETER(0x%08x)", BT_ERROR_INVALID_PARAMETER);
		return BT_ERROR_INVALID_PARAMETER;
	}

	_bt_convert_address_to_hex(&addr_hex, remote_address);
	strncpy(pin.pin_code, pin_code, BLUETOOTH_PIN_CODE_MAX_LENGTH);
	pin.pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH] = '\0';

	error_code = _bt_get_error_code(bluetooth_set_pin_code(&addr_hex, &pin));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_device_unset_pin_code(const char *remote_address)
{
	int error_code = BT_ERROR_NONE;
	bluetooth_device_address_t addr_hex = { {0,} };

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	error_code = _bt_get_error_code(bluetooth_unset_pin_code(&addr_hex));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_device_update_le_connection_mode(const char *remote_address, bt_device_le_connection_mode_e mode)
{
	int ret;
	bluetooth_device_address_t addr_hex = { {0,} };

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	if (mode < BT_DEVICE_LE_CONNECTION_MODE_BALANCED ||
	    mode > BT_DEVICE_LE_CONNECTION_MODE_LOW_ENERGY) {
		return BT_ERROR_INVALID_PARAMETER;
	}

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_update_le_connection_mode(&addr_hex, mode));
	if (BT_ERROR_NONE != ret)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret;
}

int bt_device_request_att_mtu(const char *remote_address, unsigned int mtu)
{
	int ret;
	bluetooth_device_address_t addr_hex = { {0,} };

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_request_att_mtu(&addr_hex, mtu));
	if (BT_ERROR_NONE != ret)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret;
}

int bt_device_get_att_mtu(const char *remote_address, unsigned int *mtu)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int ret;
	unsigned int mtu_value = 0;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_get_att_mtu(&addr_hex, &mtu_value));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
		return ret;
	}

	*mtu = mtu_value;

	return ret;

}

int bt_device_get_id_address(const char *remote_rpa, char **id_address)
{
	bluetooth_device_address_t id_addr = { {0} };
	bluetooth_device_address_t addr_hex = { {0,} };
	int ret = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_rpa);

	_bt_convert_address_to_hex(&addr_hex, remote_rpa);

	ret = _bt_get_error_code(bluetooth_get_device_ida(&addr_hex, &id_addr));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
		return ret;
	}

	ret = _bt_convert_address_to_string(id_address, &id_addr);
	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
		return ret;
	}

	return ret;
}

int bt_device_set_profile_trusted(const char *device_address,
		bt_trusted_profile_t profile, bool trust)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	error_code = _bt_get_error_code(bluetooth_set_profile_trusted(&addr_hex,
			profile, trust));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_device_get_profile_trusted(const char *device_address,
		bt_trusted_profile_t profile, int *trust)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int profile_trusted;
	int error_code = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	error_code = _bt_get_error_code(bluetooth_get_profile_trusted(&addr_hex,
			profile, &profile_trusted));

	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	if (trust)
		*trust = profile_trusted;
	return error_code;
}

int bt_device_set_trusted_profile_cb(bt_device_trusted_profiles_cb callback, void *user_data)
{
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_SUPPORTED_TRUSTED_PROFILE_EVENT, callback, user_data);

	return BT_ERROR_NONE;
}

int bt_device_unset_trusted_profile_cb(void)
{
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_SUPPORTED_TRUSTED_PROFILE_EVENT);
	return BT_ERROR_NONE;
}

int bt_device_set_profile_restricted(const char *device_address,
		bt_restricted_profile_t profile, bool restricted)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	error_code = _bt_get_error_code(bluetooth_set_profile_trusted(&addr_hex,
			profile, !restricted));

	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_device_get_profile_restricted(const char *device_address,
		bt_restricted_profile_t profile, int *restricted)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int profile_trusted;
	int error_code = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(device_address);

	_bt_convert_address_to_hex(&addr_hex, device_address);
	error_code = _bt_get_error_code(bluetooth_get_profile_trusted(&addr_hex,
			profile, &profile_trusted));

	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	if (restricted)
		*restricted = !profile_trusted;
	return error_code;
}

int bt_adapter_get_bonded_device_is_alias_set(const char *remote_address, gboolean *alias_set)
{
	int ret;
	bluetooth_device_address_t addr_hex = { {0,} };
	gboolean is_alias_set = FALSE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(alias_set);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_get_is_alias_set(&addr_hex, &is_alias_set));
	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x) : Failed to run function",
					_bt_convert_error_to_string(ret), ret);
	} else {
		*alias_set = is_alias_set;
		BT_DBG("alias_set: %s", is_alias_set ? "TRUE" : "FALSE");
	}

	return ret;
}

int bt_device_le_set_data_length(const char *remote_address,
		unsigned int max_tx_Octets,  unsigned int max_tx_Time)
{
	int ret = BT_ERROR_NONE;

	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON);
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	bluetooth_device_address_t addr_hex = { {0, } };

	/* Range for host suggested txtime is 0x001B-0x00FB  and
	 * txocets is 0x0148- 0x0848 as per BT 4.2 spec */
	if ((max_tx_Octets < 0x001B || max_tx_Octets > 0x00FB)
		&& (max_tx_Time < 0x0148 || max_tx_Time > 0x0848)) {
		return BT_ERROR_INVALID_PARAMETER;
	}

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_le_set_data_length(
					&addr_hex,
					max_tx_Octets,
					max_tx_Time));

	if (ret != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);

	return ret;
}

int bt_device_le_set_data_length_change_cb(
	_bt_le_set_data_length_changed_cb callback, void *user_data)
{
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_LE_DATA_LENGTH_CHANGED, callback, user_data);

	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */