Fix dlog format error
[apps/native/ug-bluetooth-efl.git] / common / src / libraries / bt-callback.c
1 /*
2 * ug-setting-bluetooth-efl
3 *
4 * Copyright 2012 Samsung Electronics Co., Ltd
5 *
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 *           GirishAshok Joshi <girish.joshi@samsung.com>
8 *           DoHyun Pyun <dh79.pyun@samsung.com>
9 *
10 * Licensed under the Flora License, Version 1.1 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.tizenopensource.org/license
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26 #include <bluetooth.h>
27 #include <efl_extension.h>
28 #include <bundle.h>
29
30 #include "bt-debug.h"
31 #include "bt-main-ug.h"
32 #include "bt-main-view.h"
33 #include "bt-util.h"
34 #include "bt-widget.h"
35 #include "bt-string-define.h"
36 #include "bt-ipc-handler.h"
37 #include "bt-resource.h"
38 #include "bt-net-connection.h"
39 #include "syspopup_caller.h"
40 #include "bt-callback.h"
41
42 /**********************************************************************
43 *                                                 Static Functions
44 ***********************************************************************/
45
46 static Eina_Bool __bt_cb_auto_discovery(void *data)
47 {
48         FN_START;
49
50         int ret;
51         bt_ug_data *ugd = NULL;
52
53         retv_if(data == NULL, ECORE_CALLBACK_CANCEL);
54
55         ugd = (bt_ug_data *)data;
56
57         retv_if((elm_win_focus_get(ugd->win_main) == FALSE), ECORE_CALLBACK_CANCEL);
58
59         _bt_main_remove_all_searched_devices(ugd);
60
61         if (TIZEN_FEATURE_BT_HOG) {
62                 ret = bt_adapter_le_start_scan((bt_adapter_le_scan_result_cb)_bt_cb_new_le_device_found, (void *)ugd);
63                 if (!ret) {
64                         ugd->is_le_discovery_started = TRUE;
65                         BT_DBG("LE Discovery started");
66                 } else {
67                         BT_ERR("LE Discovery failed : Error Cause[%d]", ret);
68                 }
69         }
70
71         ret = bt_adapter_start_device_discovery();
72         if (!ret) {
73                 ugd->op_status = BT_SEARCHING;
74                 elm_object_text_set(ugd->scan_btn, BT_STR_STOP);
75
76                 if (ugd->searched_title == NULL)
77                                 _bt_main_add_searched_title(ugd);
78         } else {
79                 BT_ERR("Discovery failed : Error Cause[%d]", ret);
80         }
81
82         FN_END;
83         return ECORE_CALLBACK_CANCEL;
84 }
85
86 static void __bt_cb_enable(int result, void *data)
87 {
88         FN_START;
89
90         int timeout = 0;
91         bt_ug_data *ugd = NULL;
92
93         ret_if(data == NULL);
94
95         ugd = (bt_ug_data *)data;
96
97         if (ugd->op_status == BT_ACTIVATED) {
98                 BT_DBG("Already enabled state");
99                 return;
100         }
101
102         if (result != BT_ERROR_NONE) {
103                 BT_ERR("Failed to enable Bluetooth [%d]", result);
104                 ugd->op_status = BT_DEACTIVATED;
105                 _bt_update_genlist_item(ugd->onoff_item);
106                 return;
107         } else {
108                 ugd->op_status = BT_ACTIVATED;
109                 elm_genlist_item_item_class_update(ugd->onoff_item,
110                                                 ugd->on_itc);
111                 _bt_update_genlist_item(ugd->onoff_item);
112                 ugd->aul_pairing_req = FALSE;
113         }
114
115         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
116                 BT_ERR("Fail to get the timeout value");
117
118         _bt_util_change_discoverable_mode(ugd, TRUE);
119
120         _bt_util_set_phone_name();
121
122         if (!ugd->device_name_item)
123                 _bt_main_add_device_name_item(ugd, ugd->main_genlist);
124
125         if (ugd->empty_status_item) {
126                 elm_object_item_del(ugd->empty_status_item);
127                 ugd->empty_status_item = NULL;
128         }
129
130         if (ugd->bt_launch_mode == BT_LAUNCH_ONOFF) {
131                 g_idle_add((GSourceFunc) _bt_idle_destroy_ug, ugd);
132                 return;
133         } else if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY) {
134                 return;
135         }
136
137         ugd->scan_btn = _bt_main_create_scan_button(ugd);
138
139         _bt_update_genlist_item(ugd->paired_title);
140         _bt_update_genlist_item(ugd->searched_title);
141
142         /* In the NFC case, we don't need to display the paired device */
143         if (ugd->bt_launch_mode != BT_LAUNCH_USE_NFC)
144                 _bt_main_draw_paired_devices(ugd);
145
146         if (!ecore_idler_add(__bt_cb_auto_discovery, ugd))
147                 BT_ERR("idler can not be added\n\n");
148
149         FN_END;
150 }
151
152 static void __bt_cb_disable(int result, void *data)
153 {
154         FN_START;
155         bt_ug_data *ugd = NULL;
156         ret_if(data == NULL);
157
158         ugd = (bt_ug_data *)data;
159         int ret = 0;
160
161         if (ugd->op_status == BT_DEACTIVATED) {
162                 BT_DBG("Already disabled state");
163                 return;
164         }
165
166         if (ugd->help_more_popup) {
167                 evas_object_del(ugd->help_more_popup);
168                 ugd->help_more_popup = NULL;
169         }
170
171         if (ugd->popup) {
172                 evas_object_del(ugd->popup);
173                 ugd->popup = NULL;
174         }
175
176         if (ugd->rename_popup) {
177                 evas_object_del(ugd->rename_popup);
178                 ugd->rename_popup = NULL;
179                 ugd->rename_entry = NULL;
180         }
181
182         if (result != BT_ERROR_NONE) {
183                 BT_ERR("Failed to disable Bluetooth [%d]", result);
184                 ugd->op_status = BT_ACTIVATED;
185         } else {
186                 /* Delete profile view */
187                 if (ugd->profile_vd != NULL) {
188                         elm_naviframe_item_pop(ugd->navi_bar);
189                         ugd->profile_vd = NULL;
190                 }
191
192                 if (ugd->timeout_id) {
193                         g_source_remove(ugd->timeout_id);
194                         ugd->timeout_id = 0;
195                 }
196
197                 ugd->op_status = BT_DEACTIVATED;
198
199                 evas_object_del(ugd->scan_btn);
200                 ugd->scan_btn = NULL;
201                 elm_genlist_item_item_class_update(ugd->onoff_item,
202                                                 ugd->off_itc);
203
204                 if (ugd->device_name_item) {
205                         elm_object_item_del(ugd->device_name_item);
206                         ugd->device_name_item = NULL;
207                 }
208
209                 if (ugd->bt_launch_mode != BT_LAUNCH_VISIBILITY &&
210                      ugd->bt_launch_mode != BT_LAUNCH_ONOFF) {
211                         elm_object_disabled_set(ugd->scan_btn, EINA_FALSE);
212                         _bt_main_remove_all_paired_devices(ugd);
213                         _bt_main_remove_all_searched_devices(ugd);
214                 }
215
216                 if (ugd->bt_launch_mode == BT_LAUNCH_ONOFF) {
217                         ret = _bt_idle_destroy_ug((void *)ugd);
218                         if (ret != BT_UG_ERROR_NONE)
219                                 BT_DBG("fail to destory ug");
220                         return;
221                 }
222         }
223
224         _bt_update_genlist_item(ugd->onoff_item);
225         _bt_update_genlist_item(ugd->paired_title);
226         _bt_update_genlist_item(ugd->searched_title);
227
228         FN_END;
229 }
230
231 static void __bt_cb_search_completed(int result, void *data)
232 {
233         FN_START;
234
235         bt_ug_data *ugd = NULL;
236
237         ret_if(data == NULL);
238
239         ugd = (bt_ug_data *)data;
240
241         if (ugd->op_status == BT_SEARCHING)
242                 ugd->op_status = BT_ACTIVATED;
243
244         ugd->is_discovery_started = FALSE;
245
246         elm_object_text_set(ugd->scan_btn, BT_STR_SCAN);
247
248         _bt_update_genlist_item(ugd->paired_title);
249         _bt_update_genlist_item(ugd->searched_title);
250
251         if (ugd->searched_device == NULL ||
252                 eina_list_count(ugd->searched_device) == 0) {
253                 /* Don't add the no device item, if no device item already exist */
254                 ret_if(ugd->no_device_item != NULL);
255
256                 if (ugd->op_status != BT_DEACTIVATED)
257                         ugd->no_device_item = _bt_main_add_no_device_found(ugd);
258
259                 _bt_update_genlist_item(ugd->no_device_item);
260         }
261
262         FN_END;
263 }
264
265 static bool __bt_cb_match_discovery_type(unsigned int major_class,
266                                                 unsigned int service_class,
267                                                 unsigned int mask)
268 {
269         FN_START;
270
271         bt_device_major_mask_t major_mask = BT_DEVICE_MAJOR_MASK_MISC;
272
273         if (mask == 0x000000)
274                 return TRUE;
275
276         BT_DBG("mask: %x", mask);
277
278         BT_DBG("service_class: %x", service_class);
279
280         /* Check the service_class */
281         if (service_class & mask)
282                 return TRUE;
283
284         /* Check the major class */
285         switch (major_class) {
286         case BT_MAJOR_DEV_CLS_COMPUTER:
287                 major_mask = BT_DEVICE_MAJOR_MASK_COMPUTER;
288                 break;
289         case BT_MAJOR_DEV_CLS_PHONE:
290                 major_mask = BT_DEVICE_MAJOR_MASK_PHONE;
291                 break;
292         case BT_MAJOR_DEV_CLS_LAN_ACCESS_POINT:
293                 major_mask = BT_DEVICE_MAJOR_MASK_LAN_ACCESS_POINT;
294                 break;
295         case BT_MAJOR_DEV_CLS_AUDIO:
296                 major_mask = BT_DEVICE_MAJOR_MASK_AUDIO;
297                 break;
298         case BT_MAJOR_DEV_CLS_PERIPHERAL:
299                 major_mask = BT_DEVICE_MAJOR_MASK_PERIPHERAL;
300                 break;
301         case BT_MAJOR_DEV_CLS_IMAGING:
302                 major_mask = BT_DEVICE_MAJOR_MASK_IMAGING;
303                 break;
304         case BT_MAJOR_DEV_CLS_WEARABLE:
305                 major_mask = BT_DEVICE_MAJOR_MASK_WEARABLE;
306                 break;
307         case BT_MAJOR_DEV_CLS_TOY:
308                 major_mask = BT_DEVICE_MAJOR_MASK_TOY;
309                 break;
310         case BT_MAJOR_DEV_CLS_HEALTH:
311                 major_mask = BT_DEVICE_MAJOR_MASK_HEALTH;
312                 break;
313         default:
314                 major_mask = BT_DEVICE_MAJOR_MASK_MISC;
315                 break;
316         }
317
318         BT_DBG("major_mask: %x", major_mask);
319
320         if (mask & major_mask)
321                 return TRUE;
322
323         FN_END;
324
325         return FALSE;
326 }
327
328 static void __bt_cb_new_device_found(bt_adapter_device_discovery_info_s *info,
329                                      void *data)
330 {
331         FN_START;
332
333         bt_ug_data *ugd = NULL;
334         bt_dev_t *dev = NULL;
335         bt_device_info_s *device_info = NULL;
336         int i;
337         ret_if(info == NULL);
338         ret_if(data == NULL);
339
340         ugd = (bt_ug_data *)data;
341         ret_if(ugd->op_status != BT_SEARCHING);
342
343         BT_DBG("major_device_class: %d", info->bt_class.major_device_class);
344         BT_DBG("minor_device_class: %d", info->bt_class.minor_device_class);
345         BT_DBG("major_service_class_mask: %d", info->bt_class.major_service_class_mask);
346
347         /* Check the service_class */
348         if (__bt_cb_match_discovery_type(
349                                 info->bt_class.major_device_class,
350                                 info->bt_class.major_service_class_mask,
351                                 ugd->search_type) == FALSE) {
352                 BT_DBG("No matched device type");
353                 return;
354         }
355
356 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
357 /*
358         The Class of Device field shall be set to the following:
359         1. Mandatory to set the Â¡Â®Rendering¡¯ bit for the SNK and the Â¡Â®Capturing¡¯ bit for the SRC in the Service Class field.
360 */
361 #if 0   /* The below code also filters other devices that is not A2DP source role, so it is disabled in the tizen platform */
362         retm_if(!(info->bt_class.major_service_class_mask & BT_COD_SC_CAPTURING),
363                 "Display A2DP source only in A2DP sink role. Skip this device");
364 #endif
365 #endif
366
367         if (info->is_bonded == TRUE) {
368                 BT_DBG("Paired device found");
369                 if (_bt_main_check_and_update_device(ugd->paired_device,
370                                                 info->remote_address,
371                                                 info->remote_name) >= 0) {
372                         /* Update all realized items */
373                         elm_genlist_realized_items_update(ugd->main_genlist);
374                         return;
375                 }
376
377                 device_info = (bt_device_info_s *)g_malloc0(sizeof(bt_device_info_s));
378                 /* Fix : NULL_RETURNS */
379                 if (device_info == NULL)
380                         return;
381
382                 device_info->bt_class = info->bt_class;
383                 device_info->remote_address = g_strdup(info->remote_address);
384                 device_info->remote_name = g_strdup(info->remote_name);
385                 device_info->service_count = info->service_count;
386
387                 if (info->service_uuid != NULL && info->service_count > 0) {
388                         device_info->service_uuid = g_new0(char *, info->service_count + 1);
389
390                         for (i = 0; i < info->service_count; i++)
391                                 device_info->service_uuid[i] = g_strdup(info->service_uuid[i]);
392                 }
393                 dev = _bt_main_create_paired_device_item(device_info);
394
395                 for (i = 0; i < info->service_count; i++)
396                         g_free(device_info->service_uuid[i]);
397
398                 g_free(device_info->remote_address);
399                 g_free(device_info->remote_name);
400                 g_free(device_info);
401
402                 retm_if(!dev, "create paired device item fail!");
403
404                 dev->ugd = (void *)ugd;
405
406                 if (_bt_main_is_matched_profile(ugd->search_type,
407                                                 info->bt_class.major_device_class,
408                                                 info->bt_class.major_service_class_mask,
409                                                 ugd->service,
410                                                 info->bt_class.minor_device_class) == TRUE) {
411                         if (_bt_main_add_paired_device(ugd, dev) != NULL) {
412                                 ugd->paired_device =
413                                     eina_list_append(ugd->paired_device, dev);
414                         }
415                 } else {
416                         BT_ERR("Device class and search type do not match");
417                         free(dev);
418                 }
419                 if (_bt_main_check_and_update_device(ugd->paired_device,
420                                                 info->remote_address,
421                                                 info->remote_name) >= 0) {
422                         _bt_update_device_list(ugd);
423                 }
424                 return;
425         }
426
427         if (ugd->searched_device == NULL)
428                 _bt_update_genlist_item(ugd->searched_title);
429
430         if (_bt_main_check_and_update_device(ugd->searched_device,
431                                         info->remote_address,
432                                         info->remote_name) >= 0) {
433                 _bt_update_device_list(ugd);
434         } else {
435                 dev = _bt_main_create_searched_device_item((void *)info);
436                 if (NULL == dev) {
437                         BT_ERR("Create new device item failed");
438                         return;
439                 }
440
441                 if (_bt_main_is_matched_profile(ugd->search_type,
442                                                 dev->major_class,
443                                                 dev->service_class,
444                                                 ugd->service,
445                                                 dev->minor_class) == TRUE) {
446                         if (_bt_main_add_searched_device(ugd, dev) == NULL) {
447                                 BT_ERR("Fail to add the searched device");
448                                 return;
449                         }
450
451                         ugd->searched_device =
452                             eina_list_append(ugd->searched_device, dev);
453
454                 } else {
455                         BT_DBG("Searched device does not match the profile");
456                         free(dev);
457                 }
458         }
459
460         FN_END;
461 }
462
463 static void __bt_retry_pairing_cb(void *data,
464                                      Evas_Object *obj, void *event_info)
465 {
466         FN_START;
467         ret_if(obj == NULL || data == NULL);
468
469         bt_ug_data *ugd = (bt_ug_data *)data;
470         const char *event = elm_object_text_get(obj);
471
472         if (ugd->popup) {
473                 BT_DBG("delete popup");
474                 evas_object_del(ugd->popup);
475                 ugd->popup = NULL;
476         }
477
478         if ((!g_strcmp0(event, BT_STR_RETRY) || (!g_strcmp0(event, BT_STR_OK))) &&
479                 _bt_main_request_pairing_with_effect(ugd,
480                 ugd->searched_item) != BT_UG_ERROR_NONE) {
481                 ugd->searched_item = NULL;
482         }
483         if (!g_strcmp0(event, BT_STR_CANCEL))
484                 ugd->op_status = BT_ACTIVATED;
485         FN_END;
486 }
487
488
489 /**********************************************************************
490 *                                                Common Functions
491 ***********************************************************************/
492
493 void _bt_cb_state_changed(int result,
494                         bt_adapter_state_e adapter_state,
495                         void *user_data)
496 {
497         BT_INFO("bluetooth %s", adapter_state == BT_ADAPTER_ENABLED ?
498                                 "enabled" : "disabled");
499
500         if (adapter_state == BT_ADAPTER_ENABLED)
501                 __bt_cb_enable(result, user_data);
502         else
503                 __bt_cb_disable(result, user_data);
504 }
505
506 void _bt_cb_new_le_device_found(int result, bt_adapter_le_device_scan_result_info_s *info, void *data)
507 {
508         FN_START;
509
510         bt_ug_data *ugd = NULL;
511         bt_dev_t *dev = NULL;
512         char *remote_name = NULL;
513         int ret;
514
515         ret_if(info == NULL);
516         ret_if(data == NULL);
517
518         ugd = (bt_ug_data *)data;
519         ret_if(ugd->op_status != BT_SEARCHING);
520
521         ret = bt_adapter_le_get_scan_result_device_name(info, BT_ADAPTER_LE_PACKET_ADVERTISING, &remote_name);
522         if (ret != BT_ERROR_NONE)
523                 BT_ERR("Fail to get le device name");
524
525         if (ugd->searched_device == NULL)
526                 _bt_update_genlist_item(ugd->searched_title);
527
528         if (_bt_main_check_and_update_device(ugd->searched_device,
529                                 info->remote_address,
530                                 remote_name) >= 0) {
531                 _bt_update_device_list(ugd);
532         } else {
533                 dev = _bt_main_create_searched_le_device_item((void *)info);
534                 if (NULL == dev) {
535                         BT_ERR("Create new device item failed");
536                         g_free(remote_name);
537                         return;
538                 }
539
540                 if (_bt_main_add_searched_device(ugd, dev) == NULL) {
541                         BT_ERR("Fail to add the searched device");
542                         g_free(remote_name);
543                         return;
544                 }
545
546                 ugd->searched_device =
547                         eina_list_append(ugd->searched_device, dev);
548         }
549
550         g_free(remote_name);
551         FN_END;
552 }
553
554 void _bt_cb_discovery_state_changed(int result,
555                 bt_adapter_device_discovery_state_e discovery_state,
556                         bt_adapter_device_discovery_info_s *discovery_info,
557                         void *user_data)
558 {
559         bt_ug_data *ugd = NULL;
560
561         int ret;
562
563         ret_if(user_data == NULL);
564
565         ugd = (bt_ug_data *)user_data;
566
567         if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_STARTED) {
568                 BT_INFO("BR/EDR discovery started");
569                 ugd->is_discovery_started = TRUE;
570                 /*Now enable the Scan button, so that user may call cancel discovery */
571                         elm_object_disabled_set(ugd->scan_btn, EINA_FALSE);
572         } else if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FOUND)
573                 __bt_cb_new_device_found(discovery_info, user_data);
574         else if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED) {
575                 BT_INFO("BR/EDR discovery finished");
576                 if (!TIZEN_FEATURE_BT_HOG) {
577                         ret_if(ugd->is_discovery_started == FALSE);
578                 } else {
579                         ret_if((ugd->is_discovery_started == FALSE) && (ugd->is_le_discovery_started == FALSE));
580
581                         if (ugd->is_le_discovery_started == TRUE) {
582                                 ret = bt_adapter_le_stop_scan();
583                                 if (!ret)
584                                         ugd->is_le_discovery_started = FALSE;
585                         }
586                 }
587                 __bt_cb_search_completed(result, user_data);
588                 if (ugd->op_status == BT_PAIRING)
589                         elm_object_disabled_set(ugd->scan_btn, EINA_TRUE);
590                 else
591                         elm_object_disabled_set(ugd->scan_btn, EINA_FALSE);
592         } else
593                 BT_ERR("Unknown device discovery state");
594
595 }
596
597 void _bt_cb_visibility_mode_changed
598         (int result, bt_adapter_visibility_mode_e visibility_mode, void *user_data)
599 {
600         FN_START;
601         bt_ug_data *ugd = NULL;
602
603         ugd = (bt_ug_data *)user_data;
604
605         if (visibility_mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE &&
606                         result == BT_ERROR_NONE &&
607                         ugd->ug_status != BT_UG_PAUSE &&
608                         ugd->ug_status != BT_UG_DESTORY) {
609                         BT_DBG("visibility is turned off. turn on again!");
610                         _bt_util_change_discoverable_mode(ugd, TRUE);
611         }
612         FN_END;
613 }
614
615 void _bt_cb_gatt_connection_state_changed(int result, bool connected, char *remote_address, void *user_data)
616 {
617         FN_START;
618
619         bt_ug_data *ugd = NULL;
620         bt_dev_t *dev = NULL;
621         int connected_type;
622         bt_address_t address = { { 0 } };
623
624         ret_if(user_data == NULL);
625         _bt_util_addr_string_to_addr_type(address.bd_addr, remote_address);
626
627         /* TODO : Other GATT scenario should be considered.
628            In this case, only HOG scenario is considered */
629         connected_type = BT_LE_HID_CONNECTED;
630
631
632         ugd = (bt_ug_data *)user_data;
633         ret_if(ugd->op_status == BT_DEACTIVATING);
634
635         if (ugd->op_status == BT_PAIRING)
636                 ugd->op_status = BT_ACTIVATED;
637         /* In the NFC cas, we don't need to display the paired device */
638         if (ugd->bt_launch_mode == BT_LAUNCH_USE_NFC)
639                 return;
640
641         dev = _bt_main_get_dev_info_by_address(ugd->searched_device, remote_address);
642         if (dev == NULL && ugd->searched_item != NULL)
643                 dev = _bt_main_get_dev_info(ugd->searched_device, ugd->searched_item);
644
645         elm_object_disabled_set(ugd->scan_btn, EINA_FALSE);
646
647         if (connected) {
648                 if (result != BT_ERROR_NONE)
649                         BT_ERR("Failed to GATT connect with device [%d]", result);
650                 else
651                         BT_DBG("LE Connected");
652         } else {
653                 BT_DBG("LE Disconnected");
654         }
655
656         _bt_ipc_update_connected_status(ugd, connected_type,
657                         connected, result, &address);
658 }
659
660 void _bt_cb_bonding_created(int result, bt_device_info_s *dev_info,
661                                 void *user_data)
662 {
663         FN_START;
664
665         bt_ug_data *ugd = NULL;
666         bt_dev_t *dev = NULL;
667         Evas_Object *btn1 = NULL;
668         Evas_Object *btn2 = NULL;
669
670         ret_if(dev_info == NULL);
671         ret_if(user_data == NULL);
672
673         ugd = (bt_ug_data *)user_data;
674         ret_if(ugd->op_status == BT_DEACTIVATING);
675
676         if (ugd->op_status == BT_PAIRING)
677                 ugd->op_status = BT_ACTIVATED;
678
679         /* Enable UG to send another pairing request */
680         ugd->aul_pairing_req = FALSE;
681
682         /* In the NFC cas, we don't need to display the paired device */
683         if (ugd->bt_launch_mode == BT_LAUNCH_USE_NFC)
684                 return;
685
686         dev = _bt_main_get_dev_info_by_address(ugd->searched_device,
687                                         dev_info->remote_address);
688
689         if (dev == NULL && ugd->searched_item != NULL)
690                 dev = _bt_main_get_dev_info(ugd->searched_device, ugd->searched_item);
691
692         if (TIZEN_FEATURE_BT_HOG) {
693                 if (dev) {
694                         if (dev->is_le_device)
695                                 BT_DBG("it is the le device");
696                         else
697                                 BT_DBG("it is not le device");
698                 } else {
699                         BT_DBG("dev is null");
700                         return;
701                 }
702         }
703
704         elm_object_disabled_set(ugd->scan_btn, EINA_FALSE);
705
706         if (result != BT_ERROR_NONE) {
707                 BT_ERR("Failed to pair with device [%d]", result);
708                 retm_if(dev == NULL, "dev is NULL");
709
710                 dev->status = BT_IDLE;
711
712                 elm_genlist_item_item_class_update(dev->genlist_item,
713                                                 ugd->searched_device_itc);
714                 _bt_update_genlist_item((Elm_Object_Item *)dev->genlist_item);
715
716
717                 BT_ERR("Authentication Failed");
718                 if (_bt_util_is_battery_low() == TRUE) {
719                         _bt_main_create_information_popup(ugd, BT_STR_LOW_BATTERY);
720                 } else if (result != BT_ERROR_NOT_IN_PROGRESS
721                                 && result != BT_ERROR_CANCELLED){
722                         ugd->op_status = BT_PAIRING;
723
724                         _bt_main_popup_del_cb(ugd, NULL, NULL);
725
726                         ugd->popup_data.type = BT_POPUP_PAIRING_ERROR;
727                         ugd->popup_data.data = g_strdup(dev->name);
728                         ugd->popup = _bt_create_popup(ugd, NULL, ugd, 0);
729                         retm_if(ugd->popup == NULL, "fail to create popup!");
730
731                         btn1 = elm_button_add(ugd->popup);
732                         elm_object_style_set(btn1, "popup");
733                         elm_object_domain_translatable_text_set(
734                                 btn1,
735                                 PKGNAME, "IDS_BR_SK_CANCEL");
736                         elm_object_part_content_set(ugd->popup, "button1", btn1);
737                         evas_object_smart_callback_add(btn1,
738                                         "clicked", __bt_retry_pairing_cb, ugd);
739
740                         btn2 = elm_button_add(ugd->popup);
741                         elm_object_style_set(btn2, "popup");
742                         elm_object_domain_translatable_text_set(
743                                 btn2,
744                                 PKGNAME, "IDS_ST_BUTTON_RETRY");
745                         elm_object_part_content_set(ugd->popup, "button2", btn2);
746                         evas_object_smart_callback_add(btn2,
747                                         "clicked", __bt_retry_pairing_cb, ugd);
748
749                         eext_object_event_callback_add(ugd->popup,
750                                         EEXT_CALLBACK_BACK,
751                                         _bt_back_btn_popup_del_cb, ugd);
752                         evas_object_show(ugd->popup);
753                 }
754
755         } else {
756                 bt_dev_t *new_dev = NULL;
757                 Elm_Object_Item *item = NULL;
758                 void *profile = NULL;
759
760                 if (_bt_main_check_and_update_device(ugd->paired_device,
761                                         dev_info->remote_address,
762                                         dev_info->remote_name) >= 0) {
763                         _bt_update_device_list(ugd);
764                 } else {
765                         if (dev != NULL) {
766                                 /* Remove the item in searched dialogue */
767                                 _bt_main_remove_searched_device(ugd, dev);
768                         }
769
770                         /* Add the item in paired dialogue group */
771                         new_dev = _bt_main_create_paired_device_item(dev_info);
772                         if (new_dev == NULL) {
773                                 BT_ERR("new_dev is NULL");
774                                 return;
775                         }
776
777                         if (TIZEN_FEATURE_BT_HOG) {
778                                 if (dev->is_le_device)
779                                         new_dev->is_le_device = TRUE;
780                                 else
781                                         new_dev->is_le_device = FALSE;
782                         }
783
784 #ifndef TIZEN_BT_A2DP_SINK_ENABLE
785                         profile = _bt_get_registered_net_profile(ugd->connection,
786                                                                 new_dev->bd_addr);
787
788                         if (profile)
789                                 _bt_set_profile_state_changed_cb(profile, new_dev);
790 #endif
791                         if (_bt_main_is_matched_profile(ugd->search_type,
792                                         new_dev->major_class,
793                                         new_dev->service_class,
794                                         ugd->service,
795                                         new_dev->minor_class) == TRUE) {
796                                 item = _bt_main_add_paired_device_on_bond(ugd, new_dev);
797                         } else {
798                                 BT_ERR("Device did not match search type");
799                                 _bt_util_free_device_item(new_dev);
800                                 return;
801                         }
802
803                         if (item) {
804                                 ugd->paired_device =
805                                     eina_list_append(ugd->paired_device, new_dev);
806
807                         }
808
809                         /* Don't try to auto-connect in the network case */
810                         if (profile == NULL && ugd->searched_item != NULL) {
811                                 if (_bt_main_is_connectable_device(new_dev))
812                                         _bt_main_connect_device(ugd, new_dev);
813                         } else {
814                                 BT_DBG("Net profile exists");
815                                 int connected = _bt_util_is_profile_connected(
816                                                 BT_NETWORK_SERVER_CONNECTED,
817                                                 new_dev->bd_addr);
818                                 bt_address_t address = { { 0 } };
819                                 new_dev->connected_mask |=
820                                         connected ? BT_NETWORK_CONNECTED : 0x00;
821                                 memcpy(address.bd_addr, new_dev->bd_addr,
822                                         BT_ADDRESS_LENGTH_MAX);
823
824                                 _bt_ipc_update_connected_status(user_data,
825                                         BT_NETWORK_CONNECTED,
826                                         connected, BT_UG_ERROR_NONE,
827                                         &address);
828                         }
829
830                         ugd->searched_item = NULL;
831                         ugd->paired_item = item;
832
833                 }
834         }
835
836         FN_END;
837 }
838
839 void _bt_cb_bonding_destroyed(int result, char *remote_address,
840                                         void *user_data)
841 {
842         FN_START;
843
844         bt_ug_data *ugd = NULL;
845         bt_dev_t *item = NULL;
846         Eina_List *l = NULL;
847
848         retm_if(remote_address == NULL, "Invalid argument: param is NULL\n");
849         retm_if(user_data == NULL, "Invalid argument: param is NULL\n");
850
851         ugd = (bt_ug_data *)user_data;
852
853         EINA_LIST_FOREACH(ugd->paired_device, l, item) {
854                 if (item == NULL)
855                         break;
856
857                 if (g_strcmp0(item->addr_str, remote_address) == 0) {
858                         item->status = BT_IDLE;
859
860                         if (result != BT_ERROR_NONE) {
861                                 BT_ERR("Failed to unbond: [%d]", result);
862                                 return;
863                         }
864
865                         if (ugd->profile_vd)
866                                 _bt_profile_delete_view(item->ugd);
867
868                         _bt_main_remove_paired_device(ugd, item);
869                         item->connected_mask = 0x00;
870                         item->is_connected = 0x00;
871                         item->genlist_item = NULL;
872
873                         if (item->net_profile != NULL) {
874                                 BT_INFO("unset profile state change callback");
875                                 _bt_unset_profile_state_changed_cb(item->net_profile);
876                                 item->net_profile = NULL;
877                         }
878
879                         if (ugd->no_device_item) {
880                                 elm_object_item_del(ugd->no_device_item);
881                                 ugd->no_device_item = NULL;
882                         }
883
884                         if (_bt_main_add_searched_device(ugd, item) != NULL) {
885                                 ugd->searched_device = eina_list_append(
886                                                 ugd->searched_device, item);
887                         }
888
889                         break;
890                 }
891         }
892
893         if (ugd->paired_device == NULL && ugd->op_status != BT_PAIRING) {
894                 if (!ecore_idler_add(__bt_cb_auto_discovery, ugd))
895                         BT_ERR("idler can not be added\n\n");
896
897         }
898
899         FN_END;
900 }
901
902 void _bt_cb_service_searched(int result, bt_device_sdp_info_s *sdp_info,
903                                 void* user_data)
904 {
905         FN_START;
906
907         int i;
908         bt_ug_data *ugd = NULL;
909         bt_service_class_t service_mask = 0;
910         bt_dev_t *item = NULL;
911         Evas_Object *btn = NULL;
912
913         ret_if(user_data == NULL);
914
915         ugd = (bt_ug_data *)user_data;
916
917         ugd->op_status = BT_ACTIVATED;
918
919         BT_DBG("Result: %d", result);
920
921         item = _bt_main_get_dev_info_by_address(ugd->paired_device,
922                                         sdp_info->remote_address);
923
924         if (item == NULL)
925                 item = _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
926
927         if (item == NULL) {
928                 ugd->waiting_service_response = FALSE;
929                 return;
930         }
931
932         item->status = BT_IDLE;
933         _bt_update_genlist_item((Elm_Object_Item *)item->genlist_item);
934
935         if (result == BT_ERROR_NONE) {
936                 bt_device_get_service_mask_from_uuid_list(
937                                                 sdp_info->service_uuid,
938                                                 sdp_info->service_count,
939                                                 &service_mask);
940
941                 if (sdp_info->service_uuid != NULL && sdp_info->service_count > 0) {
942
943                         _bt_util_free_device_uuids(item);
944                         item->uuids = g_new0(char *, sdp_info->service_count + 1);
945
946                         for (i = 0; i < sdp_info->service_count; i++)
947                                 item->uuids[i] = g_strdup(sdp_info->service_uuid[i]);
948
949                         item->uuid_count = sdp_info->service_count;
950                 }
951
952                 item->service_list = service_mask;
953
954                 if (ugd->waiting_service_response == TRUE)
955                         _bt_main_connect_device(ugd, item);
956         } else {
957                 BT_ERR("Failed to get the service list [%d]", result);
958
959                 if (ugd->waiting_service_response == TRUE) {
960                         _bt_main_popup_del_cb(ugd, NULL, NULL);
961
962                         ugd->popup_data.type = BT_POPUP_GET_SERVICE_LIST_ERROR;
963                         ugd->popup = _bt_create_popup(ugd,
964                                         _bt_main_popup_del_cb, ugd, 2);
965                         retm_if(ugd->popup == NULL, "fail to create popup!");
966                         ugd->back_cb = _bt_util_launch_no_event;
967
968                         btn = elm_button_add(ugd->popup);
969                         elm_object_style_set(btn, "popup");
970                         elm_object_domain_translatable_text_set(
971                                 btn ,
972                                 PKGNAME, "IDS_BR_SK_CANCEL");
973                         elm_object_part_content_set(ugd->popup, "button1", btn);
974                         evas_object_smart_callback_add(btn, "clicked",
975                                 (Evas_Smart_Cb)_bt_main_popup_del_cb, ugd);
976                         eext_object_event_callback_add(ugd->popup, EEXT_CALLBACK_BACK,
977                                         _bt_main_popup_del_cb, ugd);
978
979                         evas_object_show(ugd->popup);
980
981                 }
982         }
983
984         ugd->waiting_service_response = FALSE;
985
986         FN_END;
987 }
988
989 void _bt_cb_hid_state_changed(int result, bool connected,
990                         const char *remote_address,
991                         void *user_data)
992 {
993         FN_START;
994         ret_if(!user_data);
995         ret_if(!remote_address);
996         bt_ug_data *ugd = (bt_ug_data *)user_data;
997         ret_if(ugd->op_status == BT_DEACTIVATING || ugd->op_status == BT_DEACTIVATED);
998
999         bt_address_t address = { { 0 } };
1000
1001         BT_INFO("Bluetooth HID Event [%d] [%d]", result, connected);
1002
1003         _bt_util_addr_string_to_addr_type(address.bd_addr, remote_address);
1004
1005         _bt_ipc_update_connected_status(user_data, BT_HID_CONNECTED,
1006                                         connected, result, &address);
1007         FN_END;
1008 }
1009
1010 void _bt_cb_audio_state_changed(int result, bool connected,
1011                                 const char *remote_address,
1012                                 bt_audio_profile_type_e type,
1013                                 void *user_data)
1014 {
1015         FN_START;
1016         ret_if(!user_data);
1017         ret_if(!remote_address);
1018         bt_ug_data *ugd = (bt_ug_data *)user_data;
1019         ret_if(ugd->op_status == BT_DEACTIVATING || ugd->op_status == BT_DEACTIVATED);
1020
1021         bt_address_t address = { { 0 } };
1022         int connected_type;
1023
1024         BT_INFO("Bluetooth Audio Event [%d] [%d] [%0x]", result, connected, type);
1025
1026         if (type == BT_AUDIO_PROFILE_TYPE_A2DP)
1027                 connected_type = BT_STEREO_HEADSET_CONNECTED;
1028 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
1029         else if (type == BT_AUDIO_PROFILE_TYPE_A2DP_SINK)
1030                 connected_type = BT_MUSIC_PLAYER_CONNECTED;
1031 #endif
1032         else
1033                 connected_type = BT_HEADSET_CONNECTED;
1034
1035         _bt_util_addr_string_to_addr_type(address.bd_addr, remote_address);
1036
1037         if (ugd->bt_launch_mode == BT_LAUNCH_CONNECT_AUDIO_SOURCE) {
1038                 int ret;
1039                 bundle *b = NULL;
1040                 b = bundle_create();
1041                 retm_if(!b, "Unable to create bundle");
1042
1043                 bundle_add_str(b, "event-type", "terminate");
1044
1045                 ret = syspopup_launch("bt-syspopup", b);
1046                 if (0 > ret)
1047                         BT_ERR("Popup launch failed...retry %d", ret);
1048                 bundle_free(b);
1049                 if (connected && result == BT_ERROR_NONE) {
1050                         _bt_ug_destroy(ugd, (void *)&result);
1051                         return;
1052                 }
1053         }
1054
1055         if (connected == 1) {
1056                 if (ugd->popup != NULL) {
1057                         char *bd_addr = evas_object_data_get(ugd->popup, "bd_addr");
1058                         if (bd_addr != NULL) {
1059                                 if (g_strcmp0(bd_addr, remote_address) == 0) {
1060                                         evas_object_del(ugd->popup);
1061                                         ugd->popup = NULL;
1062                                 }
1063                         }
1064                 }
1065         }
1066         _bt_ipc_update_connected_status(user_data, connected_type,
1067                                         connected, result, &address);
1068         FN_END;
1069 }
1070
1071 void _bt_cb_device_connection_state_changed(bool connected,
1072                                                 bt_device_connection_info_s *conn_info,
1073                                                 void *user_data)
1074 {
1075         FN_START;
1076         ret_if(!user_data);
1077
1078         bt_ug_data *ugd = NULL;
1079         bt_dev_t *dev = NULL;
1080
1081         ugd = (bt_ug_data *)user_data;
1082         dev = _bt_main_get_dev_info_by_address(ugd->paired_device, conn_info->remote_address);
1083         retm_if(dev == NULL, "dev is NULL");
1084
1085         BT_INFO("device connection state changed: Connected %d", connected);
1086         if (!connected && ugd->disconn_req == true) {
1087                 ugd->disconn_req = false;
1088                 if (dev->status != BT_DEV_UNPAIRING)
1089                         dev->status = BT_IDLE;
1090                 _bt_sort_paired_device_list(ugd, dev, dev->is_connected);
1091                 _bt_update_genlist_item((Elm_Object_Item *) dev->genlist_item);
1092         }
1093         FN_END;
1094 }
1095 void _bt_cb_adapter_name_changed(char *device_name, void *user_data)
1096 {
1097         FN_START;
1098
1099         DBG_SECURE("Name: [%s]", device_name);
1100
1101         FN_END;
1102 }
1103
1104 void _bt_cb_nap_state_changed(bool connected, const char *remote_address,
1105                                 const char *interface_name, void *user_data)
1106 {
1107         FN_START;
1108         ret_if(!user_data);
1109         ret_if(!remote_address);
1110         bt_ug_data *ugd = (bt_ug_data *)user_data;
1111         ret_if(ugd->op_status == BT_DEACTIVATING || ugd->op_status == BT_DEACTIVATED);
1112
1113         bt_address_t address = { { 0 } };
1114
1115         BT_INFO("Bluetooth NAP Event [%d]", connected);
1116         DBG_SECURE("interface = %s", interface_name);
1117
1118         _bt_util_addr_string_to_addr_type(address.bd_addr, remote_address);
1119
1120         _bt_ipc_update_connected_status(user_data, BT_NETWORK_CONNECTED,
1121                                         connected, BT_UG_ERROR_NONE, &address);
1122         FN_END;
1123 }
1124
1125 void _bt_retry_connection_cb(void *data,
1126                                 Evas_Object *obj, void *event_info)
1127 {
1128         FN_START;
1129         ret_if(obj == NULL || data == NULL);
1130
1131         bt_dev_t *dev = (bt_dev_t *)data;
1132         bt_ug_data *ugd = dev->ugd;
1133         const char *event = elm_object_text_get(obj);
1134
1135         if (ugd->popup) {
1136                 BT_DBG("delete popup");
1137                 _bt_update_genlist_item((Elm_Object_Item *)dev->genlist_item);
1138                 evas_object_del(ugd->popup);
1139                 ugd->popup = NULL;
1140         }
1141
1142         if (!g_strcmp0(event, BT_STR_RETRY))
1143                 _bt_main_connect_device(ugd, dev);
1144
1145         FN_END;
1146 }