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
|
/*
* bt-connection-popup
*
* Copyright 2012 Samsung Electronics Co., Ltd
*
* Contact: Hocheol Seo <hocheol.seo@samsung.com>
* Injun Yang <injun.yang@samsung.com>
* Seungyoun Ju <sy39.ju@samsung.com>
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.tizenopensource.org/license
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <aul.h>
#include <bluetooth.h>
#include <vconf.h>
#include "bt-connection-main.h"
#include "bt-connection-view.h"
#include "bt-connection-handler.h"
#define VCONFKEY_SAP_STATUS "memory/private/sap/conn_status"
#define BT_ADDRESS_LENGTH 18
static bool is_le_disconnecting;
static int __bt_app_destory_cb(void *data)
{
bt_app_data_t *ad = NULL;
DBG("");
ad = (bt_app_data_t *)data;
if (ad == NULL) {
ERR("Invalid param");
return 0;
}
if (ad->timer) {
ecore_timer_del(ad->timer);
ad->timer = NULL;
}
_bt_destroy_app(ad);
return 0;
}
static void __bt_wms_status_handler(keynode_t *key, void *data)
{
int status;
int type;
bt_app_data_t *ad;
ad = (bt_app_data_t *)data;
if (ad == NULL) {
ERR("Invalid param");
return;
}
type = vconf_keynode_get_type(key);
if (type == VCONF_TYPE_BOOL)
status = vconf_keynode_get_bool(key);
else {
ERR("Invalid vconf type : %d", type);
return;
}
DBG("WMS is %s", status == WMS_CONNECTED ?
"connected" : "disconnected" );
if (status == WMS_CONNECTED) {
/* To show connected view, create new popup */
/* or update edc part */
/* See _bt_update_connect_status_popup() */
if (ad->timer) {
ecore_timer_del(ad->timer);
ad->timer = NULL;
}
_bt_create_connected_popup(ad);
_bt_send_result(ad, KEY_VAL_CONNECTED);
ad->timer = ecore_timer_add(APP_DESTORY_TIMEOUT,
(Ecore_Task_Cb) __bt_app_destory_cb, ad);
}
return;
}
static void __bt_hf_connection_state_changed(int result, bool connected, const char *remote_address, bt_audio_profile_type_e type, void *user_data)
{
if (connected == true) {
DBG("Bluetooth HF is connected");
}
else {
DBG("Bluetooth HF is disconnected");
}
}
static bool __bt_adapter_bonded_device_cb(bt_device_info_s *device_info, void *user_data)
{
int ret;
bool is_connected = false;
char* remote_address = (char*)user_data;
if (!(device_info->bt_class.major_device_class & BT_MAJOR_DEVICE_CLASS_COMPUTER) &&
!(device_info->bt_class.major_device_class & BT_MAJOR_DEVICE_CLASS_PHONE))
return true;
memcpy(remote_address, device_info->remote_address, BT_ADDRESS_LENGTH);
ret = bt_device_is_profile_connected(device_info->remote_address, BT_PROFILE_AG, &is_connected);
if (ret == BT_ERROR_NONE && is_connected) {
remote_address[0] = '\0';
return false;
}
return true;
}
static gboolean __bt_hf_is_connected(const char* remote_address)
{
int ret;
gboolean is_connected = FALSE;
ret = bt_adapter_foreach_bonded_device(__bt_adapter_bonded_device_cb, (void*)remote_address);
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_foreach_bonded_device is failed 0x%X", ret);
return FALSE;
}
if (remote_address[0] == '\0')
is_connected = TRUE;
DBG("Aleady HF is %s", is_connected ? "connected" : "disconnected");
return is_connected;
}
static void __bt_adapter_state_changed(int result,
bt_adapter_state_e adapter_state, void *user_data)
{
int ret;
char remote_address[BT_ADDRESS_LENGTH] = { 0, };
if (user_data == NULL) {
ERR("Invalid param");
return;
}
if (result != BT_ERROR_NONE) {
ERR("BT Adapter operation is failed : %d", result);
return;
}
DBG("BT Adapter is %s", adapter_state == BT_ADAPTER_ENABLED ?
"enabled" : "disabled");
if (adapter_state == BT_ADAPTER_ENABLED) {
if (__bt_hf_is_connected(&remote_address[0]) == FALSE) {
DBG("Make a HF connection");
ret = bt_audio_connect(&remote_address[0], BT_AUDIO_PROFILE_TYPE_AG);
if (ret != BT_ERROR_NONE) {
ERR("HF Connection is failed");
return;
}
}
}
return;
}
int _bt_get_wms_status(void)
{
int ret;
int status = WMS_DISCONNECTED;
ret = vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &status);
if (ret != 0) {
ERR("Vconf get failed");
return WMS_DISCONNECTED;
}
DBG("WMS status : %d", status);
return status;
}
void _bt_get_sap_status(void)
{
int ret;
int status = 0;
ret = vconf_get_int(VCONFKEY_SAP_STATUS, &status);
if (ret != 0) {
ERR("Vconf get failed");
}
DBG("SAP status : %d", status);
return;
}
gboolean _bt_send_result(bt_app_data_t *ad, const char *val)
{
if (ad == NULL)
return FALSE;
bundle *kb;
bundle *res_b;
int ret;
static gboolean is_sent = FALSE;
DBG("");
if (ad->service_clone == NULL) {
ERR("Invalid param");
return FALSE;
}
if (is_sent == TRUE) {
ERR("Aleady send response !!!");
return FALSE;
}
ret = service_to_bundle(ad->service_clone, &kb);
if (ret != SERVICE_ERROR_NONE) {
ERR("service is failed : %d", ret);
return FALSE;
}
aul_create_result_bundle(kb, &res_b);
if (res_b == NULL)
return FALSE;
bundle_add(res_b, "__BT_CONNECTION__", val);
aul_send_service_result(res_b);
bundle_free(res_b);
bundle_free(kb);
is_sent = TRUE;
DBG("Send result : %s", val);
return TRUE;
}
bool __device_check_gatt_cb(bt_profile_e profile, void *user_data)
{
bool *is_connected = (bool *)user_data;
if (profile == BT_PROFILE_GATT) {
*is_connected = true;
return false;
}
return true;
}
bool __bt_le_is_connected(const char *remote_address)
{
int ret;
bool is_connected = false;
if (remote_address == NULL) {
return false;
}
ret = bt_device_foreach_connected_profiles(remote_address,
__device_check_gatt_cb, &is_connected);
if (ret != BT_ERROR_NONE) {
ERR("bt_device_foreach_connected_profiles fail (0x%08x)", ret);
return false;
}
return is_connected;
}
void __bt_gatt_disconnected_cb(int result, void *user_data)
{
DBG("called");
if (result != BT_ERROR_NONE) {
ERR("GATT disconnect fail (0x%08x)", result);
is_le_disconnecting = false;
}
return;
#if 0
char remote_address[BT_ADDRESS_LENGTH] = { 0, };
bool is_advertising = false;
int ret;
if (result != BT_ERROR_NONE) {
ERR("GATT disconnect fail (0x%08x)", result);
return;
}
if (__bt_hf_is_connected(&remote_address[0]) == TRUE) {
DBG("Handsfree is already connected");
return;
}
ret = bt_adapter_is_advertising(&is_advertising);
if (ret == BT_ERROR_NONE && is_advertising) {
ret = bt_adapter_stop_advertising();
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_stop_advertising failed (0x%08x)", ret);
}
}
DBG("Make a HF connection");
ret = bt_audio_connect(&remote_address[0], BT_AUDIO_PROFILE_TYPE_AG);
if (ret != BT_ERROR_NONE) {
ERR("HF Connection is failed");
return;
}
return;
#endif
}
static void __bt_device_connection_state_changed_cb(bool connected,
const char *remote_address,
void *user_data)
{
bool is_advertising = false;
int ret;
char remote_add[BT_ADDRESS_LENGTH] = { 0, };
DBG("address: %s, connected: %d", remote_address, connected);
if (is_le_disconnecting == false || connected == true) {
DBG("is_le_disconnecting IS FALSE");
return;
}
is_le_disconnecting = false;
if (__bt_hf_is_connected(&remote_add[0]) == true) {
DBG("Handsfree is already connected");
return;
}
if (strncmp(remote_address, remote_add, BT_ADDRESS_LENGTH-1) != 0) {
DBG("remote address is different");
return;
}
ret = bt_adapter_is_advertising(&is_advertising);
if (ret == BT_ERROR_NONE && is_advertising) {
ret = bt_adapter_stop_advertising();
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_stop_advertising failed (0x%08x)", ret);
}
}
DBG("Make a HF connection");
ret = bt_audio_connect(&remote_add[0], BT_AUDIO_PROFILE_TYPE_AG);
if (ret != BT_ERROR_NONE) {
ERR("HF Connection is failed");
return;
}
return;
}
gboolean _bt_init(void *data)
{
int ret;
bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
char remote_address[BT_ADDRESS_LENGTH] = { 0, };
bool is_advertising = false;
bt_app_data_t *ad;
ad = (bt_app_data_t *)data;
is_le_disconnecting = false;
if (ad == NULL) {
return FALSE;
}
if (ad->initialize == TRUE) {
DBG("bt_initialize already done");
return TRUE;
}
ret = bt_initialize();
if (ret != BT_ERROR_NONE) {
ERR("bt_initialize is failed : %d", ret);
return FALSE;
}
ret = bt_adapter_set_state_changed_cb(__bt_adapter_state_changed, data);
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_set_state_changed_cb is failed : %d", ret);
bt_deinitialize();
return FALSE;
}
ret = bt_device_set_connection_state_changed_cb(__bt_device_connection_state_changed_cb,
NULL);
if (ret != BT_ERROR_NONE) {
ERR("bt_device_set_connection_state_changed_cb is failed : %d", ret);
return FALSE;
}
ret = bt_adapter_get_state(&adapter_state);
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_get_state is failed : %d", ret);
return FALSE;
}
ret = bt_audio_initialize();
if (ret != BT_ERROR_NONE) {
ERR("bt_audio_initialize is failed : %d", ret);
return FALSE;
}
ret = bt_audio_set_connection_state_changed_cb(__bt_hf_connection_state_changed, NULL);
if (ret != BT_ERROR_NONE) {
ERR("bt_audio_set_connection_state_changed_cb is failed : %d", ret);
bt_hf_deinitialize();
return FALSE;
}
if (adapter_state == BT_ADAPTER_ENABLED) {
DBG("Aleady BT enabled");
#ifdef FEATURE_TIZENW
if (__bt_hf_is_connected(&remote_address[0]) == FALSE) {
DBG("Make a HF connection");
ret = bt_audio_connect(&remote_address[0], BT_AUDIO_PROFILE_TYPE_AG);
if (ret != BT_ERROR_NONE) {
ERR("HF Connection is failed");
return FALSE;
}
}
#else
if (__bt_hf_is_connected(&remote_address[0]) == FALSE) {
if (__bt_le_is_connected(&remote_address[0])) {
ret = bt_adapter_is_advertising(&is_advertising);
if (ret == BT_ERROR_NONE && is_advertising) {
ret = bt_adapter_stop_advertising();
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_stop_advertising failed (0x%08x)",
ret);
}
}
ret = bt_device_disconnect_le(__bt_gatt_disconnected_cb, &remote_address[0]);
if (ret == BT_ERROR_NONE)
is_le_disconnecting = true;
return TRUE;
}
DBG("Make a HF connection");
ret = bt_audio_connect(&remote_address[0], BT_AUDIO_PROFILE_TYPE_AG);
if (ret != BT_ERROR_NONE) {
ERR("HF Connection is failed");
return FALSE;
}
}
#endif /* FEATURE_TIZENW */
return TRUE;
} else {
DBG("Enable BT adapter");
ret = bt_adapter_enable();
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_enable is failed : %d", ret);
return FALSE;
}
}
return TRUE;
}
void _bt_deinit(void *data)
{
int ret;
char remote_address[BT_ADDRESS_LENGTH] = { 0, };
bool is_advertising = FALSE;
bt_app_data_t *ad;
ad = (bt_app_data_t *)data;
if (ad == NULL) return;
if (ad->initialize == FALSE) {
DBG("bt_deinitialize already done");
return;
}
if (__bt_hf_is_connected(&remote_address[0]) == FALSE) {
ret = bt_adapter_is_advertising(&is_advertising);
if (ret == BT_ERROR_NONE && !is_advertising) {
ret = bt_adapter_start_advertising(NULL);
if (ret != BT_ERROR_NONE) {
ERR("bt_adapter_stop_advertising failed (0x%08x)", ret);
}
}
}
ret = bt_audio_unset_connection_state_changed_cb();
if (ret != BT_ERROR_NONE)
ERR("bt_audio_unset_connection_state_changed_cb is failed : %d", ret);
ret = bt_audio_deinitialize();
if (ret != BT_ERROR_NONE)
ERR("bt_audio_deinitialize is failed : %d", ret);
ret = bt_hf_deinitialize();
if (ret != BT_ERROR_NONE)
ERR("bt_hf_deinitialize is failed : %d", ret);
ret = bt_device_unset_connection_state_changed_cb();
if (ret != BT_ERROR_NONE)
ERR("bt_device_unset_connection_state_changed_cb is failed : %d", ret);
ret = bt_adapter_unset_state_changed_cb();
if (ret != BT_ERROR_NONE)
ERR("bt_adapter_unset_state_changed_cb is failed : %d", ret);
ret = bt_deinitialize();
if (ret != BT_ERROR_NONE)
ERR("bt_deinitialize is failed : %d", ret);
return;
}
gboolean _bt_register_vconf_handler(void *data)
{
int ret;
ret =
vconf_notify_key_changed(VCONFKEY_WMS_WMANAGER_CONNECTED,
(vconf_callback_fn) __bt_wms_status_handler, data);
if (ret < 0) {
ERR("Unable to register key handler");
return FALSE;
}
return TRUE;
}
void _bt_unregister_vconf_handler(void)
{
vconf_ignore_key_changed(VCONFKEY_WMS_WMANAGER_CONNECTED,
(vconf_callback_fn) __bt_wms_status_handler);
return;
}
|