summaryrefslogtreecommitdiff
path: root/src/bluetooth-common.c
blob: 67a6cc1f4252c4814c0565108cf3f6b1f136ce30 (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
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
/*
 * Copyright (c) 2012-2013 Samsung Electronics 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 <glib.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dlog.h>
#include <stdio.h>
#include <stdbool.h>
#include <bluetooth-api.h>

#include "bluetooth.h"
#include "bluetooth_private.h"
#include "bluetooth-media-control.h"
#include "bluetooth-telephony-api.h"

static bool is_initialized = false;
static bt_event_sig_event_slot_s bt_event_slot_container[] = {
	{BT_EVENT_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_NAME_CHANGED, NULL, NULL},
	{BT_EVENT_VISIBILITY_MODE_CHANGED, NULL, NULL},
	{BT_EVENT_DEVICE_DISCOVERY_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_BOND_CREATED, NULL, NULL},
	{BT_EVENT_BOND_DESTROYED, NULL, NULL},
	{BT_EVENT_AUTHORIZATION_CHANGED, NULL, NULL},
	{BT_EVENT_SERVICE_SEARCHED, NULL, NULL},
	{BT_EVENT_DATA_RECEIVED, NULL, NULL},
	{BT_EVENT_CONNECTION_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_RFCOMM_CONNECTION_REQUESTED, NULL, NULL},
	{BT_EVENT_OPP_CONNECTION_REQUESTED, NULL, NULL},
	{BT_EVENT_OPP_PUSH_REQUESTED, NULL, NULL},
	{BT_EVENT_OPP_SERVER_TRANSFER_PROGRESS, NULL, NULL},
	{BT_EVENT_OPP_SERVER_TRANSFER_FINISHED, NULL, NULL},
	{BT_EVENT_OPP_CLIENT_PUSH_RESPONSED, NULL, NULL},
	{BT_EVENT_OPP_CLIENT_PUSH_PROGRESS, NULL, NULL},
	{BT_EVENT_OPP_CLIENT_PUSH_FINISHED, NULL, NULL},
	{BT_EVENT_PAN_CONNECTION_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_NAP_CONNECTION_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_HDP_CONNECTED, NULL, NULL},
	{BT_EVENT_HDP_DISCONNECTED, NULL, NULL},
	{BT_EVENT_HDP_DATA_RECEIVED, NULL, NULL},
	{BT_EVENT_AUDIO_CONNECTION_STATUS, NULL, NULL},
	{BT_EVENT_AG_SCO_CONNECTION_STATUS, NULL, NULL},
	{BT_EVENT_AG_CALL_HANDLING_EVENT, NULL, NULL},
	{BT_EVENT_AG_MULTI_CALL_HANDLING_EVENT, NULL, NULL},
	{BT_EVENT_AG_DTMF_TRANSMITTED, NULL, NULL},
	{BT_EVENT_AG_MICROPHONE_GAIN_CHANGE, NULL, NULL},
	{BT_EVENT_AG_SPEAKER_GAIN_CHANGE, NULL, NULL},
	{BT_EVENT_AVRCP_CONNECTION_STATUS, NULL, NULL},
	{BT_EVENT_AVRCP_EQUALIZER_STATE_CHANGED, NULL, NULL},
	{BT_EVENT_AVRCP_REPEAT_MODE_CHANGED, NULL, NULL},
	{BT_EVENT_AVRCP_SHUFFLE_MODE_CHANGED, NULL, NULL},
	{BT_EVENT_AVRCP_SCAN_MODE_CHANGED, NULL, NULL},
	{BT_EVENT_HID_CONNECTION_STATUS, NULL, NULL},
	{BT_EVENT_DEVICE_CONNECTION_STATUS, NULL, NULL},
	{BT_EVENT_GATT_CHARACTERISTIC_DISCOVERED, NULL, NULL},
	{BT_EVENT_GATT_VALUE_CHANGED, NULL, NULL}
};

/*
 *  Internal Functions
 */
static void __bt_event_proxy(int event, bluetooth_event_param_t * param, void *user_data);
static int __bt_get_cb_index(int event);
static void __bt_convert_lower_to_upper(char *origin);
static int __bt_get_bt_device_sdp_info_s(bt_device_sdp_info_s **dest, bt_sdp_info_t *source);
static void __bt_free_bt_device_sdp_info_s(bt_device_sdp_info_s *sdp_info);
static int __bt_get_bt_adapter_device_discovery_info_s(bt_adapter_device_discovery_info_s **discovery_info, bluetooth_device_info_t *source_info);
static void __bt_free_bt_adapter_device_discovery_info_s(bt_adapter_device_discovery_info_s *discovery_info);


/*
 *  Public Functions
 */

int bt_initialize(void)
{
	if (is_initialized != true) {
		if (bluetooth_register_callback(&__bt_event_proxy, NULL) != BLUETOOTH_ERROR_NONE) {
			BT_ERR("OPERATION_FAILED(0x%08x)", BT_ERROR_OPERATION_FAILED);
			return BT_ERROR_OPERATION_FAILED;
		}
		is_initialized = true;
	}

	return BT_ERROR_NONE;
}

int bt_deinitialize(void)
{
	BT_CHECK_INIT_STATUS();
	if (bluetooth_unregister_callback() != BLUETOOTH_ERROR_NONE) {
		BT_ERR("OPERATION_FAILED(0x%08x)", BT_ERROR_OPERATION_FAILED);
		return BT_ERROR_OPERATION_FAILED;
	}
	is_initialized = false;

	return BT_ERROR_NONE;
}


/*
 *  Common Functions
 */
int _bt_check_init_status(void)
{
	if (is_initialized != true)
	{
		BT_ERR("NOT_INITIALIZED(0x%08x)", BT_ERROR_NOT_INITIALIZED);
		return BT_ERROR_NOT_INITIALIZED;
	}

	return BT_ERROR_NONE;
}

void _bt_set_cb(int events, void *callback, void *user_data)
{
	bt_event_slot_container[events].callback = callback;
	bt_event_slot_container[events].user_data = user_data;
}

void _bt_unset_cb(int events)
{
	if (bt_event_slot_container[events].callback != NULL) {
		bt_event_slot_container[events].callback = NULL;
		bt_event_slot_container[events].user_data = NULL;
	}
}

bool _bt_check_cb(int events)
{
	return (bt_event_slot_container[events].callback != NULL) ? true : false;
}

int _bt_get_error_code(int origin_error)
{
	switch (origin_error) {
	case BLUETOOTH_ERROR_NONE:
		return BT_ERROR_NONE;
	case BLUETOOTH_ERROR_INVALID_DATA:
	case BLUETOOTH_ERROR_INVALID_PARAM:
	case BLUETOOTH_ERROR_NOT_CONNECTED:
	case BLUETOOTH_ERROR_NOT_SUPPORT:
		return BT_ERROR_INVALID_PARAMETER;
	case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
	case BLUETOOTH_ERROR_OUT_OF_MEMORY:
		return BT_ERROR_OUT_OF_MEMORY;
	case BLUETOOTH_ERROR_TIMEOUT:
		return BT_ERROR_TIMED_OUT;
	case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
		return BT_ERROR_NOT_ENABLED;
	case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
	case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
	case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
		return BT_ERROR_ALREADY_DONE;
	case BLUETOOTH_ERROR_NOT_PAIRED:
		return BT_ERROR_REMOTE_DEVICE_NOT_BONDED;
	case BLUETOOTH_ERROR_NOT_IN_OPERATION:
		return BT_ERROR_NOT_IN_PROGRESS;
	case BLUETOOTH_ERROR_IN_PROGRESS:
		return BT_ERROR_NOW_IN_PROGRESS;
	case BLUETOOTH_ERROR_DEVICE_BUSY:
		return BT_ERROR_RESOURCE_BUSY;
	case BLUETOOTH_ERROR_CANCEL:
	case BLUETOOTH_ERROR_CANCEL_BY_USER:
		return BT_ERROR_CANCELLED;
	case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
		return BT_ERROR_AUTH_FAILED;
	case BLUETOOTH_ERROR_HOST_DOWN:
		return BT_ERROR_REMOTE_DEVICE_NOT_FOUND;
	case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
		return BT_ERROR_SERVICE_SEARCH_FAILED;
	case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
	case BLUETOOTH_ERROR_PARING_FAILED:
	case BLUETOOTH_ERROR_MAX_CONNECTION:
	case BLUETOOTH_ERROR_ALREADY_CONNECT:
	case BLUETOOTH_ERROR_NO_RESOURCES:
	case BLUETOOTH_ERROR_REGISTRATION_FAILED:
	case BLUETOOTH_ERROR_CONNECTION_BUSY:
	case BLUETOOTH_ERROR_MAX_CLIENT:
	case BLUETOOTH_ERROR_INVALID_CALLBACK:
	case BLUETOOTH_ERROR_NOT_FOUND:
	case BLUETOOTH_ERROR_INTERNAL:
	case BLUETOOTH_ERROR_CONNECTION_ERROR:
	case BLUETOOTH_ERROR_ACCESS_DENIED:
	default:
		return BT_ERROR_OPERATION_FAILED;
	}

}

int _bt_get_bt_device_info_s(bt_device_info_s **dest_dev, bluetooth_device_info_t *source_dev)
{
	int i = 0;

	BT_CHECK_INPUT_PARAMETER(source_dev);

	*dest_dev = (bt_device_info_s *)malloc(sizeof(bt_device_info_s));
	if (*dest_dev == NULL) {
		return BT_ERROR_OUT_OF_MEMORY;
	}

	if (strlen(source_dev->device_name.name) > 0) {
		(*dest_dev)->remote_name = strdup(source_dev->device_name.name);
	} else {
		(*dest_dev)->remote_name = NULL;
	}

	_bt_convert_address_to_string(&((*dest_dev)->remote_address), &(source_dev->device_address));

	(*dest_dev)->bt_class.major_device_class = source_dev->device_class.major_class;
	(*dest_dev)->bt_class.minor_device_class = source_dev->device_class.minor_class;
	(*dest_dev)->bt_class.major_service_class_mask = source_dev->device_class.service_class;
	if (source_dev->service_index > 0) {
		(*dest_dev)->service_uuid = (char **)malloc(sizeof(char *) * source_dev->service_index);
		if ((*dest_dev)->service_uuid != NULL) {
			for (i = 0; i < source_dev->service_index; i++) {
				(*dest_dev)->service_uuid[i] = strdup(source_dev->uuids[i]);
				if ((*dest_dev)->service_uuid[i] != NULL) {
					__bt_convert_lower_to_upper((*dest_dev)->service_uuid[i]);
				}
			}
		}
	} else {
		(*dest_dev)->service_uuid = NULL;
	}
	(*dest_dev)->service_count = source_dev->service_index;
	(*dest_dev)->is_bonded = (bool)source_dev->paired;
	(*dest_dev)->is_connected = (bool)source_dev->connected;
	(*dest_dev)->is_authorized = (bool)source_dev->trust;

	return BT_ERROR_NONE;
}

void _bt_free_bt_device_info_s(bt_device_info_s *device_info)
{
	int i = 0;

	if (device_info == NULL)
		return;

	if (device_info->remote_name != NULL)
		free(device_info->remote_name);

	if (device_info->remote_address != NULL)
		free(device_info->remote_address);

	if (device_info->service_uuid != NULL) {
		for (i = 0; i < device_info->service_count; i++) {
			if (device_info->service_uuid[i] != NULL)
				free(device_info->service_uuid[i]);
		}
		free(device_info->service_uuid);
	}

	free(device_info);
	device_info = NULL;
}

int _bt_convert_address_to_string(char **addr_str, bluetooth_device_address_t *addr_hex)
{
	char address[18] = { 0, };

	BT_CHECK_INPUT_PARAMETER(addr_hex);

	snprintf(address, 18, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", addr_hex->addr[0], addr_hex->addr[1], addr_hex->addr[2], addr_hex->addr[3], addr_hex->addr[4], addr_hex->addr[5]);
	*addr_str = strdup(address);

	if (*addr_str != NULL) {
		return BT_ERROR_NONE;
	} else {
		return BT_ERROR_OUT_OF_MEMORY;
	}
}

void _bt_convert_address_to_hex(bluetooth_device_address_t *addr_hex, const char *addr_str)
{
	int i = 0;
	unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };

	if (addr_str == NULL)
		return;

	i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]);
	if (i != BLUETOOTH_ADDRESS_LENGTH) {
		BT_ERR("Invalid format string - %s", addr_str);
	}

	for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++) {
		addr_hex->addr[i] = (unsigned char)addr[i];
	}
}

char *_bt_convert_error_to_string(int error)
{
	switch (error) {
	case BT_ERROR_CANCELLED:
		return "CANCELLED";
	case BT_ERROR_INVALID_PARAMETER:
		return "INVALID_PARAMETER";
	case BT_ERROR_OUT_OF_MEMORY:
		return "OUT_OF_MEMORY";
	case BT_ERROR_RESOURCE_BUSY:
		return "RESOURCE_BUSY";
	case BT_ERROR_TIMED_OUT:
		return "TIMED_OUT";
	case BT_ERROR_NOW_IN_PROGRESS:
		return "NOW_IN_PROGRESS";
	case BT_ERROR_NOT_INITIALIZED:
		return "NOT_INITIALIZED";
	case BT_ERROR_NOT_ENABLED:
		return "DEVICE_NOT_ENABLED";
	case BT_ERROR_ALREADY_DONE:
		return "ALREADY_DONE";
	case BT_ERROR_OPERATION_FAILED:
		return "OPERATION_FAILED";
	case BT_ERROR_NOT_IN_PROGRESS:
		return "NOT_IN_PROGRESS";
	case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
		return "REMOTE_DEVICE_NOT_BONDED";
	case BT_ERROR_AUTH_REJECTED:
		return "AUTH_REJECTED";
	case BT_ERROR_AUTH_FAILED:
		return "AUTH_FAILED";
	case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
		return "REMOTE_DEVICE_NOT_FOUND";
	case BT_ERROR_SERVICE_SEARCH_FAILED:
		return "SERVICE_SEARCH_FAILED";
	default:
		return "UNKNOWN";
	}
}

bt_adapter_visibility_mode_e _bt_get_bt_visibility_mode_e(bluetooth_discoverable_mode_t mode)
{
	switch (mode) {
	case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
		return BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE;
	case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
		return BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE;
	default:
		return BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
	}
}

/*
 *  Internal Functions
 */

static int __bt_get_bt_device_sdp_info_s(bt_device_sdp_info_s **dest, bt_sdp_info_t *source)
{
	int i = 0;

	*dest = (bt_device_sdp_info_s *)malloc(sizeof(bt_device_sdp_info_s));
	if (*dest == NULL) {
		return BT_ERROR_OUT_OF_MEMORY;
	}

	if (_bt_convert_address_to_string(&((*dest)->remote_address), &(source->device_addr)) != BT_ERROR_NONE) {
		__bt_free_bt_device_sdp_info_s(*dest);
		return BT_ERROR_OUT_OF_MEMORY;
	}

	if (source->service_index > 0) {
		(*dest)->service_uuid = (char **)malloc(sizeof(char *) * source->service_index);
		if ((*dest)->service_uuid == NULL) {
			__bt_free_bt_device_sdp_info_s(*dest);
			return BT_ERROR_OUT_OF_MEMORY;
		}

		for (i = 0; i < source->service_index; i++) {
			(*dest)->service_uuid[i] = strdup(source->uuids[i]);
			if ((*dest)->service_uuid[i] == NULL) {
				__bt_free_bt_device_sdp_info_s(*dest);
				return BT_ERROR_OUT_OF_MEMORY;
			}
			__bt_convert_lower_to_upper((*dest)->service_uuid[i]);
		}
	} else {
		(*dest)->service_uuid = NULL;
	}
	(*dest)->service_count = source->service_index;

	return BT_ERROR_NONE;
}

static void __bt_free_bt_device_sdp_info_s(bt_device_sdp_info_s *sdp_info)
{
	int i = 0;

	if (sdp_info == NULL)
		return;

	if (sdp_info->remote_address != NULL)
		free(sdp_info->remote_address);

	if (sdp_info->service_uuid != NULL) {
		for (i = 0; i < sdp_info->service_count; i++) {
			if (sdp_info->service_uuid[i] != NULL)
				free(sdp_info->service_uuid[i]);
		}
		free(sdp_info->service_uuid);
	}

	free(sdp_info);
	sdp_info = NULL;
}

void _bt_audio_event_proxy(int event, bt_audio_event_param_t *param, void *user_data)
{
	bluetooth_event_param_t new_param;
	new_param.event = param->event;
	new_param.param_data = param->param_data;
	new_param.result = param->result;
	new_param.user_data = NULL;
	__bt_event_proxy(event, &new_param, user_data);
}

void _bt_telephony_event_proxy(int event, telephony_event_param_t *param, void *user_data)
{
	bluetooth_event_param_t new_param;
	new_param.event = param->event;
	new_param.param_data = param->param_data;
	new_param.result = param->result;
	new_param.user_data = NULL;
	__bt_event_proxy(event, &new_param, user_data);
}

void _bt_avrcp_event_proxy(int event, media_event_param_t *param, void *user_data)
{
	bluetooth_event_param_t new_param;
	new_param.event = param->event;
	new_param.param_data = param->param_data;
	new_param.result = param->result;
	new_param.user_data = param->user_data;
	__bt_event_proxy(event, &new_param, user_data);
}

void _bt_hid_event_proxy(int event, hid_event_param_t *param, void *user_data)
{
	bluetooth_event_param_t new_param;
	new_param.event = param->event;
	new_param.param_data = param->param_data;
	new_param.result = param->result;
	new_param.user_data = param->user_data;
	__bt_event_proxy(event, &new_param, user_data);
}

static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *user_data)
{
	int i;
	int call_id;
	int *avrcp_mode;
	bluetooth_rfcomm_connection_t *connection_ind = NULL;
	bluetooth_rfcomm_disconnection_t *disconnection_ind = NULL;
	bt_socket_connection_s rfcomm_connection;
	bt_device_sdp_info_s *sdp_info = NULL;
	bt_adapter_device_discovery_info_s *discovery_info = NULL;
	bt_device_info_s *bonded_device = NULL;
	bluetooth_rfcomm_connection_request_t *reqeust_ind = NULL;
	bt_obex_server_authorize_into_t *auth_info = NULL;
	bt_obex_server_transfer_info_t *transfer_info = NULL;
	bt_opc_transfer_info_t *client_info = NULL;
	bluetooth_device_address_t *bd_addr = NULL;
	telephony_event_callid_t *call_data = NULL;
	char *device_addr = NULL;
	int error_code = BT_ERROR_NONE;
	int event_index = -1;
	bluetooth_network_device_info_t *dev_info = NULL;
	bt_hdp_connected_t *hdp_conn_info = NULL;
	bt_hdp_disconnected_t *hdp_disconn_info = NULL;
	bt_hdp_data_ind_t *hdp_data_ind = NULL;
	bt_gatt_discovered_char_t *svc_char = NULL;
	bt_gatt_char_value_t *char_val = NULL;

	event_index = __bt_get_cb_index(event);
	if (event_index == -1 || bt_event_slot_container[event_index].callback == NULL) {
		return;
	}

	memset(&rfcomm_connection, 0x00, sizeof(bt_socket_connection_s));

	switch (event) {
	case BLUETOOTH_EVENT_ENABLED:
		BT_INFO("bt_adapter_state_changed_cb() will be called with BT_ADAPTER_ENABLED");
		((bt_adapter_state_changed_cb) bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), BT_ADAPTER_ENABLED, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_DISABLED:
		BT_INFO("bt_adapter_state_changed_cb() will be called with BT_ADAPTER_DISABLED");
		((bt_adapter_state_changed_cb) bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), BT_ADAPTER_DISABLED, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_LOCAL_NAME_CHANGED:
		BT_INFO("bt_adapter_name_changed_cb() will be called");
		((bt_adapter_name_changed_cb)bt_event_slot_container[event_index].callback)
		    ((char *)(param->param_data), bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED:
		BT_INFO("bt_adapter_visibility_mode_changed_cb() will be called");
		((bt_adapter_visibility_mode_changed_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), _bt_get_bt_visibility_mode_e(*(bt_adapter_visibility_mode_e *)(param->param_data)), bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_DISCOVERY_STARTED:
		BT_INFO("bt_adapter_device_discovery_state_changed_cb() will be called with BT_ADAPTER_DEVICE_DISCOVERY_STARTED");
		((bt_adapter_device_discovery_state_changed_cb) bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), BT_ADAPTER_DEVICE_DISCOVERY_STARTED, NULL, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_DISCOVERY_FINISHED:
		BT_INFO("bt_adapter_device_discovery_state_changed_cb() will be called with BT_ADAPTER_DEVICE_DISCOVERY_FINISHED");
		((bt_adapter_device_discovery_state_changed_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), BT_ADAPTER_DEVICE_DISCOVERY_FINISHED, NULL, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED:
		BT_INFO("bt_adapter_device_discovery_state_changed_cb() will be called with BT_ADAPTER_DEVICE_DISCOVERY_FOUND");
		if (__bt_get_bt_adapter_device_discovery_info_s(&discovery_info, (bluetooth_device_info_t *)(param->param_data)) == BT_ERROR_NONE) {
			((bt_adapter_device_discovery_state_changed_cb)bt_event_slot_container[event_index].callback)
			    (_bt_get_error_code(param->result), BT_ADAPTER_DEVICE_DISCOVERY_FOUND, discovery_info, bt_event_slot_container[event_index].user_data);
			__bt_free_bt_adapter_device_discovery_info_s(discovery_info);
		} else {
			((bt_adapter_device_discovery_state_changed_cb)bt_event_slot_container[event_index].callback)
			    (_bt_get_error_code(param->result), BT_ADAPTER_DEVICE_DISCOVERY_FOUND, NULL, bt_event_slot_container[event_index].user_data);
		}
		break;
	case BLUETOOTH_EVENT_BONDING_FINISHED:
		BT_INFO("bt_device_bond_created_cb() will be called");
		_bt_get_bt_device_info_s(&bonded_device, (bluetooth_device_info_t *)(param->param_data));

		((bt_device_bond_created_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), bonded_device, bt_event_slot_container[event_index].user_data);
		_bt_free_bt_device_info_s(bonded_device);
		bonded_device = NULL;
		break;
	case BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED:
		BT_INFO("bt_device_bond_destroyed_cb() will be called");
		_bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data));
		((bt_device_bond_destroyed_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), device_addr, bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		device_addr = NULL;
		break;
	case BLUETOOTH_EVENT_DEVICE_AUTHORIZED:
		BT_INFO("bt_device_authorization_changed_cb() will be called with BT_DEVICE_AUTHORIZED");
		_bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data));
		((bt_device_authorization_changed_cb)bt_event_slot_container[event_index].callback)
		    (BT_DEVICE_AUTHORIZED, device_addr, bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED:
		BT_INFO("bt_device_authorization_changed_cb() will be called with BT_DEVICE_UNAUTHORIZED");
		_bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data));
		((bt_device_authorization_changed_cb)bt_event_slot_container[event_index].callback)
		    (BT_DEVICE_UNAUTHORIZED, device_addr, bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_DEVICE_CONNECTED:
		BT_INFO("bt_device_connection_state_changed_cb() will be called");
		_bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data));
		((bt_device_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		    (true, device_addr, bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_DEVICE_DISCONNECTED:
		BT_INFO("bt_device_connection_state_changed_cb() will be called");
		_bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data));
		((bt_device_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		    (false, device_addr, bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_SERVICE_SEARCHED:
		BT_INFO("bt_device_service_searched_cb() will be called");
		__bt_get_bt_device_sdp_info_s(&sdp_info, (bt_sdp_info_t *)(param->param_data));
		error_code = _bt_get_error_code(param->result);
		// In service search, BT_ERROR_SERVICE_SEARCH_FAILED is returned instead of BT_ERROR_OPERATION_FAILED.
		if (error_code == BT_ERROR_OPERATION_FAILED)
			error_code = BT_ERROR_SERVICE_SEARCH_FAILED;
		((bt_device_service_searched_cb)bt_event_slot_container[event_index].callback)
		    (error_code, sdp_info, bt_event_slot_container[event_index].user_data);
		__bt_free_bt_device_sdp_info_s(sdp_info);
		break;
	case BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED:
		BT_INFO("bt_socket_data_received_cb() will be called");
		((bt_socket_data_received_cb)bt_event_slot_container[event_index].callback)
		    ((bt_socket_received_data_s *)(param->param_data), bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_RFCOMM_CONNECTED:
		BT_INFO("bt_socket_connection_state_changed_cb() will be called with BT_SOCKET_CONNECTED");
		if (param->result == BLUETOOTH_ERROR_INVALID_PARAM)
			error_code = BT_ERROR_OPERATION_FAILED;
		else
			error_code = _bt_get_error_code(param->result);

		connection_ind = (bluetooth_rfcomm_connection_t *)(param->param_data);

		if (connection_ind) {
			rfcomm_connection.socket_fd = connection_ind->socket_fd;
			rfcomm_connection.local_role = connection_ind->device_role;

			if (strlen(connection_ind->uuid) > 0) {
				rfcomm_connection.service_uuid = strdup(connection_ind->uuid);
				BT_DBG("uuid: [%s]", rfcomm_connection.service_uuid);
			}

			_bt_convert_address_to_string(&(rfcomm_connection.remote_address),
						&(connection_ind->device_addr));
		}

		((bt_socket_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		    (error_code, BT_SOCKET_CONNECTED, &rfcomm_connection, bt_event_slot_container[event_index].user_data);

		if (rfcomm_connection.remote_address != NULL) {
			free(rfcomm_connection.remote_address);
			rfcomm_connection.remote_address = NULL;
		}

		if (rfcomm_connection.service_uuid != NULL) {
			free(rfcomm_connection.service_uuid);
			rfcomm_connection.service_uuid = NULL;
		}

		break;
	case BLUETOOTH_EVENT_RFCOMM_DISCONNECTED:
		BT_INFO("bt_socket_connection_state_changed_cb() will be called with BT_SOCKET_DISCONNECTED");

		disconnection_ind = (bluetooth_rfcomm_disconnection_t *)(param->param_data);

		if (disconnection_ind) {
			rfcomm_connection.socket_fd = disconnection_ind->socket_fd;
			rfcomm_connection.local_role = disconnection_ind->device_role;

			if (strlen(disconnection_ind->uuid) > 0) {
				rfcomm_connection.service_uuid = strdup(disconnection_ind->uuid);
				BT_DBG("uuid: [%s]", rfcomm_connection.service_uuid);
			}

			_bt_convert_address_to_string(&(rfcomm_connection.remote_address),
						&(disconnection_ind->device_addr));
		}

		((bt_socket_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), BT_SOCKET_DISCONNECTED, &rfcomm_connection, bt_event_slot_container[event_index].user_data);

		if (rfcomm_connection.remote_address != NULL) {
			free(rfcomm_connection.remote_address);
			rfcomm_connection.remote_address = NULL;
		}

		if (rfcomm_connection.service_uuid != NULL) {
			free(rfcomm_connection.service_uuid);
			rfcomm_connection.service_uuid = NULL;
		}

		break;
	case BLUETOOTH_EVENT_RFCOMM_AUTHORIZE:
		BT_INFO("bt_socket_connection_requested_cb() will be called");
		reqeust_ind = (bluetooth_rfcomm_connection_request_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, &(reqeust_ind->device_addr));
		((bt_socket_connection_requested_cb)bt_event_slot_container[event_index].callback)
		    (reqeust_ind->socket_fd, device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE:
		BT_INFO("bt_opp_server_connection_requested_cb() will be called");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);
		((bt_opp_server_connection_requested_cb)bt_event_slot_container[event_index].callback)
		    (device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE:
		BT_INFO("bt_opp_server_push_requested_cb() will be called");
		auth_info = (bt_obex_server_authorize_into_t *)(param->param_data);
		((bt_opp_server_push_requested_cb)bt_event_slot_container[event_index].callback)
		    (auth_info->filename, auth_info->length, bt_event_slot_container[event_index].user_data);
		break;

	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED:
		BT_INFO("bt_opp_server_transfer_started_cb() will be called");
		transfer_info = (bt_obex_server_transfer_info_t *)(param->param_data);
		((bt_opp_server_transfer_progress_cb)bt_event_slot_container[event_index].callback)
		    (transfer_info->filename, transfer_info->file_size, 0, bt_event_slot_container[event_index].user_data);
		break;

	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS:
		BT_INFO("bt_opp_server_transfer_in_progress_cb() will be called");
		transfer_info = (bt_obex_server_transfer_info_t *)(param->param_data);
		((bt_opp_server_transfer_progress_cb)bt_event_slot_container[event_index].callback)
		    (transfer_info->filename, transfer_info->file_size, transfer_info->percentage, bt_event_slot_container[event_index].user_data);
		break;

	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED:
		BT_INFO("bt_opp_server_transfer_completed_cb() will be called");
		transfer_info = (bt_obex_server_transfer_info_t *)(param->param_data);

		((bt_opp_server_transfer_finished_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), transfer_info->filename, transfer_info->file_size, bt_event_slot_container[event_index].user_data);

		break;

	case BLUETOOTH_EVENT_OPC_CONNECTED:
		BT_INFO("bt_opp_client_push_responded_cb() will be called");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);

		((bt_opp_client_push_responded_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_OPC_DISCONNECTED:
		BT_INFO("bt_opp_client_push_finished_cb() will be called");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);

		((bt_opp_client_push_finished_cb)bt_event_slot_container[event_index].callback)
		    (_bt_get_error_code(param->result), device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_OPC_TRANSFER_STARTED:
		BT_INFO("BLUETOOTH_EVENT_OPC_TRANSFER_STARTED");
		/* This event don't be used in CAPI */
		break;

	case BLUETOOTH_EVENT_OPC_TRANSFER_PROGRESS:
		BT_INFO("bt_opp_client_transfer_in_progress_cb() will be called");
		client_info = (bt_opc_transfer_info_t *)(param->param_data);
		((bt_opp_client_push_progress_cb)bt_event_slot_container[event_index].callback)
		    (client_info->filename, client_info->size, client_info->percentage, bt_event_slot_container[event_index].user_data);
		break;

	case BLUETOOTH_EVENT_OPC_TRANSFER_COMPLETE:
		BT_INFO("BLUETOOTH_EVENT_OPC_TRANSFER_COMPLETE");
		/* This event don't be used in CAPI */
		break;

	case BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED");
		dev_info = (bluetooth_network_device_info_t *)(param->param_data);

		if (param->result != BLUETOOTH_ERROR_NONE) {
			BT_ERR("Fail to connect the network server");
		}
		_bt_convert_address_to_string(&device_addr, &dev_info->device_address);
		((bt_nap_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(TRUE, device_addr, dev_info->interface_name, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED");
		dev_info = (bluetooth_network_device_info_t *)(param->param_data);

		if (param->result != BLUETOOTH_ERROR_NONE) {
			BT_ERR("Fail to disconnect the network server");
		}
		_bt_convert_address_to_string(&device_addr, &dev_info->device_address);
		((bt_nap_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(FALSE, device_addr, dev_info->interface_name, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_NETWORK_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_NETWORK_DISCONNECTED");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);

		((bt_panu_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE, device_addr, BLUETOOTH_NETWORK_NAP_ROLE,
		bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_NETWORK_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_NETWORK_CONNECTED");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);

		((bt_panu_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE, device_addr, BLUETOOTH_NETWORK_NAP_ROLE,
		bt_event_slot_container[event_index].user_data);
		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_HDP_CONNECTED:
		BT_INFO("HDP Connected ");
		hdp_conn_info = (bt_hdp_connected_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, &hdp_conn_info->device_address);
		((bt_hdp_connected_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), device_addr, hdp_conn_info->app_handle,
		hdp_conn_info->type, hdp_conn_info->channel_id,
		bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;

	case BLUETOOTH_EVENT_HDP_DISCONNECTED:
		BT_INFO("HDP disconnected callback will be ");
		hdp_disconn_info = (bt_hdp_disconnected_t *)(param->param_data);

		_bt_convert_address_to_string(&device_addr, &hdp_disconn_info->device_address);
		((bt_hdp_disconnected_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), device_addr,
		hdp_disconn_info->channel_id, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_HDP_DATA_RECEIVED:
		BT_INFO("HDP data recieved callback will be ");
		hdp_data_ind = (bt_hdp_data_ind_t *)(param->param_data);
		if (param->result != BLUETOOTH_ERROR_NONE) {
			BT_ERR("Fail to receive HDP data");
		}

		((bt_hdp_data_received_cb)bt_event_slot_container[event_index].callback)
		(hdp_data_ind->channel_id, hdp_data_ind->buffer, hdp_data_ind->size,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AG_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AG_CONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE, device_addr, BT_AUDIO_PROFILE_TYPE_HSP_HFP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AG_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AG_DISCONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE, device_addr, BT_AUDIO_PROFILE_TYPE_HSP_HFP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED ");
		((bt_ag_sco_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED ");
		((bt_ag_sco_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL:
		call_data = (telephony_event_callid_t *)param->param_data;
		call_id = call_data->callid;
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL ");
		((bt_ag_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_CALL_HANDLING_EVENT_ANSWER, call_id,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL:
		call_data = (telephony_event_callid_t *)param->param_data;
		call_id = call_data->callid;
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL ");
		((bt_ag_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_CALL_HANDLING_EVENT_RELEASE, call_id,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL:
		call_data = (telephony_event_callid_t *)param->param_data;
		call_id = call_data->callid;
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL ");
		((bt_ag_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_CALL_HANDLING_EVENT_REJECT, call_id,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL ");
		((bt_ag_multi_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_MULTI_CALL_HANDLING_EVENT_RELEASE_HELD_CALLS,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL ");
		((bt_ag_multi_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_MULTI_CALL_HANDLING_EVENT_RELEASE_ACTIVE_CALLS,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL ");
		((bt_ag_multi_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_MULTI_CALL_HANDLING_EVENT_ACTIVATE_HELD_CALL,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL ");
		((bt_ag_multi_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_MULTI_CALL_HANDLING_EVENT_MERGE_CALLS,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER:
		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER ");
		((bt_ag_multi_call_handling_event_cb)bt_event_slot_container[event_index].callback)
		(BT_AG_MULTI_CALL_HANDLING_EVENT_EXPLICIT_CALL_TRANSFER,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF: {
		telephony_event_dtmf_t *call_data = param->param_data;

		BT_INFO("BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF ");
		((bt_ag_dtmf_transmitted_cb)bt_event_slot_container[event_index].callback)
		(call_data->dtmf, bt_event_slot_container[event_index].user_data);
		break;
	}
	case BLUETOOTH_EVENT_AG_SPEAKER_GAIN:
		BT_INFO("BLUETOOTH_EVENT_AG_SPEAKER_GAIN");
		int *spk_gain = (int *)(param->param_data);
		((bt_ag_speaker_gain_changed_cb)bt_event_slot_container[event_index].callback)
		(*spk_gain, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AG_MIC_GAIN:
		BT_INFO("BLUETOOTH_EVENT_AG_MIC_GAIN");
		int *mik_gain = (int *)(param->param_data);
		((bt_ag_microphone_gain_changed_cb)bt_event_slot_container[event_index].callback)
		(*mik_gain, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AG_AUDIO_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AG_AUDIO_CONNECTED");
		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE, NULL, BT_AUDIO_PROFILE_TYPE_HSP_HFP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AG_AUDIO_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AG_AUDIO_DISCONNECTED");
		device_addr = (char *)(param->param_data);

		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE, device_addr, BT_AUDIO_PROFILE_TYPE_HSP_HFP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AV_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AV_CONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE, device_addr, BT_AUDIO_PROFILE_TYPE_A2DP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AV_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_Av_DISCONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_audio_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE, device_addr, BT_AUDIO_PROFILE_TYPE_A2DP,
		bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_CONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_CONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_avrcp_target_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(TRUE, device_addr, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_DISCONNECTED:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_DISCONNECTED ");
		device_addr = (char *)(param->param_data);
		((bt_avrcp_target_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(FALSE, device_addr, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_SETTING_SHUFFLE_STATUS:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_SETTING_SHUFFLE_STATUS ");
		avrcp_mode = (int *)(param->param_data);
		((bt_avrcp_shuffle_mode_changed_cb)bt_event_slot_container[event_index].callback)
		((bt_avrcp_shuffle_mode_e)*avrcp_mode, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_SETTING_EQUALIZER_STATUS:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_SETTING_EQUALIZER_STATUS ");
		avrcp_mode = (int *)(param->param_data);
		((bt_avrcp_equalizer_state_changed_cb)bt_event_slot_container[event_index].callback)
		((bt_avrcp_equalizer_state_e)*avrcp_mode, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_SETTING_REPEAT_STATUS:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_SETTING_REPEAT_STATUS ");
		avrcp_mode = (int *)(param->param_data);
		((bt_avrcp_repeat_mode_changed_cb)bt_event_slot_container[event_index].callback)
		((bt_avrcp_repeat_mode_e)*avrcp_mode, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_EVENT_AVRCP_SETTING_SCAN_STATUS:
		BT_INFO("BLUETOOTH_EVENT_AVRCP_SETTING_SCAN_STATUS ");
		avrcp_mode = (int *)(param->param_data);
		((bt_avrcp_scan_mode_changed_cb)bt_event_slot_container[event_index].callback)
		((bt_avrcp_scan_mode_e)*avrcp_mode, bt_event_slot_container[event_index].user_data);
		break;
	case BLUETOOTH_HID_CONNECTED:
		BT_INFO("BLUETOOTH_HID_CONNECTED");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);
		((bt_hid_host_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), TRUE, device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_HID_DISCONNECTED:
		BT_INFO("BLUETOOTH_HID_DISCONNECTED");
		bd_addr = (bluetooth_device_address_t *)(param->param_data);
		_bt_convert_address_to_string(&device_addr, bd_addr);
		((bt_hid_host_connection_state_changed_cb)bt_event_slot_container[event_index].callback)
		(_bt_get_error_code(param->result), FALSE, device_addr, bt_event_slot_container[event_index].user_data);

		if (device_addr != NULL)
			free(device_addr);
		break;
	case BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED:
		BT_INFO("BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED");
		svc_char = (bt_gatt_discovered_char_t *)(param->param_data);

		if (svc_char == NULL)
			return;

		for (i = 1; i <= svc_char->handle_info.count; i++) {
			((bt_gatt_characteristics_discovered_cb)bt_event_slot_container[event_index].callback)
			 (_bt_get_error_code(param->result), i, svc_char->handle_info.count, svc_char->handle_info.handle[i-1],
			  bt_event_slot_container[event_index].user_data);
		}

		_bt_unset_cb(BT_EVENT_GATT_CHARACTERISTIC_DISCOVERED);

		break;
	case BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED:
		BT_INFO("BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED");
		char_val = (bt_gatt_char_value_t *)(param->param_data);

		if (char_val == NULL)
			return;

		((bt_gatt_characteristic_changed_cb)bt_event_slot_container[event_index].callback)
		 (char_val->char_handle, char_val->char_value, char_val->val_len,
		  bt_event_slot_container[event_index].user_data);

		break;
	default:
		break;
	}
}

static int __bt_get_bt_adapter_device_discovery_info_s(bt_adapter_device_discovery_info_s **discovery_info, bluetooth_device_info_t *source_info) {
	int i;

	BT_CHECK_INPUT_PARAMETER(source_info);

	*discovery_info = (bt_adapter_device_discovery_info_s *)malloc(sizeof(bt_adapter_device_discovery_info_s));
	if (*discovery_info == NULL) {
		return BT_ERROR_OUT_OF_MEMORY;
	}

	if (strlen(source_info->device_name.name) > 0) {
		(*discovery_info)->remote_name = strdup(source_info->device_name.name);
	} else {
		(*discovery_info)->remote_name = NULL;
	}

	_bt_convert_address_to_string(&((*discovery_info)->remote_address), &(source_info->device_address));

	(*discovery_info)->bt_class.major_device_class = source_info->device_class.major_class;
	(*discovery_info)->bt_class.minor_device_class = source_info->device_class.minor_class;
	(*discovery_info)->bt_class.major_service_class_mask = source_info->device_class.service_class;

	if (source_info->service_index > 0) {
		(*discovery_info)->service_uuid = (char **)malloc(sizeof(char *) * source_info->service_index);
		if ((*discovery_info)->service_uuid != NULL) {
			for (i = 0; i < source_info->service_index; i++) {
				(*discovery_info)->service_uuid[i] = strdup(source_info->uuids[i]);
				if ((*discovery_info)->service_uuid[i] != NULL) {
					__bt_convert_lower_to_upper((*discovery_info)->service_uuid[i]);
				}

				BT_DBG("UUID: %s", (*discovery_info)->service_uuid[i]);
			}
		}
	} else {
		(*discovery_info)->service_uuid = NULL;
	}
	(*discovery_info)->service_count = source_info->service_index;

	(*discovery_info)->rssi = (int)source_info->rssi;
	(*discovery_info)->is_bonded = (bool)source_info->paired;

	return BT_ERROR_NONE;
}

static void __bt_free_bt_adapter_device_discovery_info_s(bt_adapter_device_discovery_info_s *discovery_info)
{
	int i;

	if (discovery_info == NULL)
		return;

	if (discovery_info->remote_name != NULL)
		free(discovery_info->remote_name);

	if (discovery_info->remote_address != NULL)
		free(discovery_info->remote_address);

	if (discovery_info->service_uuid != NULL) {
		for (i = 0; i < discovery_info->service_count; i++) {
			if (discovery_info->service_uuid[i] != NULL)
				free(discovery_info->service_uuid[i]);
		}
		free(discovery_info->service_uuid);
	}

	free(discovery_info);
	discovery_info = NULL;
}

static int __bt_get_cb_index(int event)
{
	switch (event) {
	case BLUETOOTH_EVENT_ENABLED:
	case BLUETOOTH_EVENT_DISABLED:
		return BT_EVENT_STATE_CHANGED;
	case BLUETOOTH_EVENT_LOCAL_NAME_CHANGED:
		return BT_EVENT_NAME_CHANGED;
	case BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED:
		return BT_EVENT_VISIBILITY_MODE_CHANGED;
	case BLUETOOTH_EVENT_DISCOVERY_STARTED:
	case BLUETOOTH_EVENT_DISCOVERY_FINISHED:
	case BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED:
		return BT_EVENT_DEVICE_DISCOVERY_STATE_CHANGED;
	case BLUETOOTH_EVENT_BONDING_FINISHED:
		return BT_EVENT_BOND_CREATED;
	case BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED:
		return BT_EVENT_BOND_DESTROYED;
	case BLUETOOTH_EVENT_DEVICE_AUTHORIZED:
	case BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED:
		return BT_EVENT_AUTHORIZATION_CHANGED;
	case BLUETOOTH_EVENT_DEVICE_CONNECTED:
	case BLUETOOTH_EVENT_DEVICE_DISCONNECTED:
		return BT_EVENT_DEVICE_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_SERVICE_SEARCHED:
		return BT_EVENT_SERVICE_SEARCHED;
	case BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED:
		return BT_EVENT_DATA_RECEIVED;
	case BLUETOOTH_EVENT_RFCOMM_CONNECTED:
	case BLUETOOTH_EVENT_RFCOMM_DISCONNECTED:
		return BT_EVENT_CONNECTION_STATE_CHANGED;
	case BLUETOOTH_EVENT_RFCOMM_AUTHORIZE:
		return BT_EVENT_RFCOMM_CONNECTION_REQUESTED;
	case BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE:
		return BT_EVENT_OPP_CONNECTION_REQUESTED;
	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE:
		return BT_EVENT_OPP_PUSH_REQUESTED;
	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED:
		return BT_EVENT_OPP_SERVER_TRANSFER_PROGRESS;
	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS:
		return BT_EVENT_OPP_SERVER_TRANSFER_PROGRESS;
	case BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED:
		return BT_EVENT_OPP_SERVER_TRANSFER_FINISHED;
	case BLUETOOTH_EVENT_OPC_CONNECTED:
		return BT_EVENT_OPP_CLIENT_PUSH_RESPONSED;
	case BLUETOOTH_EVENT_OPC_TRANSFER_STARTED:
		return BT_EVENT_OPP_CLIENT_PUSH_PROGRESS;
	case BLUETOOTH_EVENT_OPC_TRANSFER_PROGRESS:
	case BLUETOOTH_EVENT_OPC_TRANSFER_COMPLETE:
		return BT_EVENT_OPP_CLIENT_PUSH_PROGRESS;
	case BLUETOOTH_EVENT_OPC_DISCONNECTED:
		return BT_EVENT_OPP_CLIENT_PUSH_FINISHED;
	case BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED:
	case BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED:
		return BT_EVENT_NAP_CONNECTION_STATE_CHANGED;
	case BLUETOOTH_EVENT_NETWORK_CONNECTED:
	case BLUETOOTH_EVENT_NETWORK_DISCONNECTED:
		return BT_EVENT_PAN_CONNECTION_STATE_CHANGED;
	case BLUETOOTH_EVENT_HDP_DATA_RECEIVED:
		return BT_EVENT_HDP_DATA_RECEIVED;
	case BLUETOOTH_EVENT_HDP_CONNECTED:
		return BT_EVENT_HDP_CONNECTED;
	case BLUETOOTH_EVENT_HDP_DISCONNECTED:
		return BT_EVENT_HDP_DISCONNECTED;
	case BLUETOOTH_EVENT_AG_CONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_AG_DISCONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_AG_SPEAKER_GAIN:
		return BT_EVENT_AG_SPEAKER_GAIN_CHANGE;
	case BLUETOOTH_EVENT_AG_MIC_GAIN:
		return BT_EVENT_AG_MICROPHONE_GAIN_CHANGE;
	case BLUETOOTH_EVENT_AG_AUDIO_CONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_AG_AUDIO_DISCONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED:
		return BT_EVENT_AG_SCO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED:
		return BT_EVENT_AG_SCO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL:
		return BT_EVENT_AG_CALL_HANDLING_EVENT;
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL:
	case BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER:
		return BT_EVENT_AG_MULTI_CALL_HANDLING_EVENT;
	case BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF:
		return BT_EVENT_AG_DTMF_TRANSMITTED;
	case BLUETOOTH_EVENT_AV_CONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_AV_DISCONNECTED:
		return BT_EVENT_AUDIO_CONNECTION_STATUS;
	case  BLUETOOTH_EVENT_AVRCP_CONNECTED:
	case  BLUETOOTH_EVENT_AVRCP_DISCONNECTED:
		return BT_EVENT_AVRCP_CONNECTION_STATUS;
	case  BLUETOOTH_EVENT_AVRCP_SETTING_SHUFFLE_STATUS:
		return BT_EVENT_AVRCP_SHUFFLE_MODE_CHANGED;
	case  BLUETOOTH_EVENT_AVRCP_SETTING_EQUALIZER_STATUS:
		return BT_EVENT_AVRCP_EQUALIZER_STATE_CHANGED;
	case  BLUETOOTH_EVENT_AVRCP_SETTING_REPEAT_STATUS:
		return BT_EVENT_AVRCP_REPEAT_MODE_CHANGED;
	case  BLUETOOTH_EVENT_AVRCP_SETTING_SCAN_STATUS:
		return BT_EVENT_AVRCP_SCAN_MODE_CHANGED;
	case BLUETOOTH_HID_CONNECTED:
		return  BT_EVENT_HID_CONNECTION_STATUS;
	case BLUETOOTH_HID_DISCONNECTED:
		return  BT_EVENT_HID_CONNECTION_STATUS;
	case BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED:
		return	BT_EVENT_GATT_CHARACTERISTIC_DISCOVERED;
	case BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED:
		return	BT_EVENT_GATT_VALUE_CHANGED;
	default:
		return -1;
	}
}

static void __bt_convert_lower_to_upper(char *origin)
{
	int length = strlen(origin);
	int i = 0;

	for (i = 0; i < length; i++) {
		if (islower(origin[i])) {
			origin[i] = toupper(origin[i]);
		}
	}
}