Add discovery_duration in adapter_start_discovery()
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / adapter / bt-service-core-adapter.c
1 /*
2  * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Anupam Roy <anupam.r@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <gio/gio.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #include <vconf.h>
26 #include <vconf-internal-keys.h>
27 #include <syspopup_caller.h>
28 #include <aul.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
31
32 #include "alarm.h"
33
34 /*bt-service headers */
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-util.h"
38 #include "bt-service-main.h"
39 #include "bt-service-core-adapter.h"
40 #include "bt-service-core-device.h"
41 #include "bt-service-event-receiver.h"
42 #include "bt-request-handler.h"
43 #include "bt-service-event.h"
44 #include "bt-service-audio-common.h"
45 #include "bt-service-core-adapter-le.h"
46 #include "bt-service-gatt.h"
47
48 #ifdef TIZEN_DPM_ENABLE
49 #include "bt-service-dpm.h"
50 #endif
51 #include "bt-service-hidhost.h"
52 #include "bt-service-socket.h"
53 #include "bt-service-hdp.h"
54
55 /* OAL headers */
56 #include <oal-event.h>
57 #include <oal-manager.h>
58 #include <oal-adapter-mgr.h>
59
60 /*This file will contain state machines related to adapter and remote device */
61
62 /* Global variables */
63 typedef struct {
64         guint event_id;
65         int timeout;
66         time_t start_time;
67         gboolean alarm_init;
68         int alarm_id;
69 } bt_adapter_timer_t;
70
71 static bt_adapter_timer_t visible_timer;
72
73 static guint timer_id = 0;
74
75 /* Adapter default states */
76 static bt_status_t adapter_state = BT_DEACTIVATED;
77 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
78
79 /* Forward declarations */
80 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
81 static void __bt_post_oal_init(void);
82 static void __bt_handle_oal_initialisation(oal_event_t event);
83 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
84 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
85 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
86 static void __bt_adapter_update_bt_enabled(void);
87 static void __bt_adapter_update_bt_disabled(void);
88 static void __bt_adapter_state_set_status(bt_status_t status);
89 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
90 static void __bt_adapter_state_change_callback(int bt_status);
91 static int __bt_adapter_state_handle_request(gboolean enable);
92 static int __bt_adapter_state_discovery_request(gboolean enable,
93                 unsigned short max_response, unsigned short duration, unsigned int mask);
94 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
95 static gboolean __bt_is_service_request_present(int service_function);
96 #ifdef TIZEN_MOBILE
97 static void __bt_set_visible_mode(void);
98 #endif
99 static void __bt_set_local_name(void);
100
101 /* Initialize BT stack (Initialize OAL layer) */
102 int _bt_stack_init(void)
103 {
104         int ret;
105
106         BT_INFO("[bt-service] Start to initialize BT stack");
107         /* Adapter enable request is successful, setup event handlers */
108         _bt_service_register_event_handler_callback(
109                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
110
111         ret = oal_bt_init(_bt_service_oal_event_receiver);
112
113         if (OAL_STATUS_PENDING == ret) {
114                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
115                 return BLUETOOTH_ERROR_NONE;
116         } else if (OAL_STATUS_SUCCESS != ret) {
117                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
118                 return BLUETOOTH_ERROR_INTERNAL;
119         }
120
121         return BLUETOOTH_ERROR_NONE;
122 }
123
124 int _bt_enable_adapter(void)
125 {
126         return __bt_adapter_state_handle_request(TRUE);
127 }
128
129 int _bt_disable_adapter(void)
130 {
131         return __bt_adapter_state_handle_request(FALSE);
132 }
133
134
135 int _bt_start_discovery(unsigned short max_response,
136                 unsigned short duration, unsigned int cod_mask)
137 {
138         return __bt_adapter_state_discovery_request(TRUE, max_response, duration, cod_mask);
139 }
140
141 int _bt_cancel_discovery(void)
142 {
143         return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0);
144 }
145
146 gboolean _bt_is_discovering(void)
147 {
148         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
149                         || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
150                 return TRUE;
151         else
152                 return FALSE;
153 }
154
155 int _bt_get_local_address(void)
156 {
157         int result;
158
159         BT_DBG("+");
160
161         result =  adapter_get_address();
162         if (result != OAL_STATUS_SUCCESS) {
163                 BT_ERR("adapter_get_address failed: %d", result);
164                 result = BLUETOOTH_ERROR_INTERNAL;
165         } else
166                 result = BLUETOOTH_ERROR_NONE;
167
168         BT_DBG("-");
169         return result;
170 }
171
172 int _bt_get_local_version(void)
173 {
174         int result;
175         BT_DBG("+");
176
177         result =  adapter_get_version();
178         if (result != OAL_STATUS_SUCCESS) {
179                 BT_ERR("adapter_get_version failed: %d", result);
180                 result = BLUETOOTH_ERROR_INTERNAL;
181         } else
182                 result = BLUETOOTH_ERROR_NONE;
183
184         BT_DBG("-");
185         return result;
186 }
187
188 int _bt_get_local_name(void)
189 {
190         int result;
191
192         BT_DBG("+");
193
194         result =  adapter_get_name();
195         if (result != OAL_STATUS_SUCCESS) {
196                 BT_ERR("adapter_get_name failed: %d", result);
197                 result = BLUETOOTH_ERROR_INTERNAL;
198         } else
199                 result = BLUETOOTH_ERROR_NONE;
200
201         BT_DBG("-");
202         return result;
203 }
204
205 int _bt_set_local_name(char *local_name)
206 {
207         int result = BLUETOOTH_ERROR_NONE;
208         BT_DBG("+");
209
210         retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
211
212         result =  adapter_set_name(local_name);
213         if (result != OAL_STATUS_SUCCESS) {
214                 BT_ERR("adapter_set_name failed: %d", result);
215                 result = BLUETOOTH_ERROR_INTERNAL;
216         } else
217                 result = BLUETOOTH_ERROR_NONE;
218
219         BT_DBG("-");
220         return result;
221 }
222
223 int _bt_get_discoverable_mode(int *mode)
224 {
225         int scan_mode = 0;
226         int timeout = 0;
227
228         BT_DBG("+");
229
230         retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
231
232         adapter_is_discoverable(&scan_mode);
233         if (TRUE == scan_mode) {
234                 adapter_get_discoverable_timeout(&timeout);
235                 if (timeout > 0)
236                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
237                 else
238                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
239         } else {
240                 adapter_is_connectable(&scan_mode);
241                 if(scan_mode == TRUE)
242                         *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
243                 else {
244                         /*
245                          * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
246                          * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
247                          * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
248                          */
249                         *mode = -1;
250                 }
251         }
252
253         BT_DBG("-");
254         return BLUETOOTH_ERROR_NONE;
255 }
256
257 int _bt_get_timeout_value(int *timeout)
258 {
259         time_t current_time;
260         int time_diff;
261
262         /* Take current time */
263         time(&current_time);
264         time_diff = difftime(current_time, visible_timer.start_time);
265
266         BT_DBG("Time diff = %d\n", time_diff);
267         *timeout = visible_timer.timeout - time_diff;
268
269         return BLUETOOTH_ERROR_NONE;
270 }
271
272 static void __bt_visibility_alarm_remove()
273 {
274         if (visible_timer.event_id > 0) {
275                 g_source_remove(visible_timer.event_id);
276                 visible_timer.event_id = 0;
277         }
278
279         if (visible_timer.alarm_id > 0) {
280                 alarmmgr_remove_alarm(visible_timer.alarm_id);
281                 visible_timer.alarm_id = 0;
282         }
283 }
284
285 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
286 {
287         int result = BLUETOOTH_ERROR_NONE;
288         int timeout = 0;
289
290         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
291
292         if (alarm_id != visible_timer.alarm_id)
293                 return 0;
294
295         if (visible_timer.event_id) {
296                 _bt_send_event(BT_ADAPTER_EVENT,
297                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
298                                 g_variant_new("(in)", result, timeout));
299                 g_source_remove(visible_timer.event_id);
300                 visible_timer.event_id = 0;
301                 visible_timer.timeout = 0;
302
303 #ifndef TIZEN_PROFILE_WEARABLE
304                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
305                         BT_ERR("Set vconf failed\n");
306 #endif
307         }
308         /* Switch Off visibility in Bluez */
309         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
310         visible_timer.alarm_id = 0;
311         return 0;
312 }
313
314 static gboolean __bt_timeout_handler(gpointer user_data)
315 {
316         int result = BLUETOOTH_ERROR_NONE;
317         time_t current_time;
318         int time_diff;
319
320         /* Take current time */
321         time(&current_time);
322         time_diff = difftime(current_time, visible_timer.start_time);
323
324         /* Send event to application */
325         _bt_send_event(BT_ADAPTER_EVENT,
326                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
327                         g_variant_new("(in)", result, time_diff));
328
329         if (visible_timer.timeout <= time_diff) {
330                 g_source_remove(visible_timer.event_id);
331                 visible_timer.event_id = 0;
332                 visible_timer.timeout = 0;
333
334 #ifndef TIZEN_PROFILE_WEARABLE
335                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
336                         BT_ERR("Set vconf failed\n");
337 #endif
338                 return FALSE;
339         }
340
341         return TRUE;
342 }
343
344 static void __bt_visibility_alarm_create()
345 {
346         alarm_id_t alarm_id;
347         int result;
348
349         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
350                         0, NULL, &alarm_id);
351         if (result < 0) {
352                 BT_ERR("Failed to create alarm error = %d\n", result);
353         } else {
354                 BT_DBG("Alarm created = %d\n", alarm_id);
355                 visible_timer.alarm_id = alarm_id;
356         }
357 }
358
359 static int __bt_set_visible_time(int timeout)
360 {
361         int result;
362
363         __bt_visibility_alarm_remove();
364
365         visible_timer.timeout = timeout;
366
367 #ifndef TIZEN_PROFILE_WEARABLE
368 #ifdef TIZEN_DPM_ENABLE
369         if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
370 #endif
371                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
372                         BT_ERR("Set vconf failed");
373 #ifdef TIZEN_DPM_ENABLE
374         }
375 #endif
376 #endif
377
378         if (timeout <= 0)
379                 return BLUETOOTH_ERROR_NONE;
380
381         if (!visible_timer.alarm_init) {
382                 /* Set Alarm timer to switch off BT */
383                 result = alarmmgr_init("bt-service");
384                 if (result != 0)
385                         return BLUETOOTH_ERROR_INTERNAL;
386
387                 visible_timer.alarm_init = TRUE;
388         }
389
390         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
391         if (result != 0)
392                 return BLUETOOTH_ERROR_INTERNAL;
393
394         /* Take start time */
395         time(&(visible_timer.start_time));
396         visible_timer.event_id = g_timeout_add_seconds(1,
397                         __bt_timeout_handler, NULL);
398
399         __bt_visibility_alarm_create();
400
401         return BLUETOOTH_ERROR_NONE;
402 }
403
404 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
405 {
406         int result;
407
408         BT_DBG("+");
409
410         BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
411
412 #ifdef TIZEN_DPM_ENABLE
413         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
414                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
415                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
416                 return BLUETOOTH_ERROR_ACCESS_DENIED;
417         }
418         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
419                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
420                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
421                 return BLUETOOTH_ERROR_ACCESS_DENIED;
422         }
423 #endif
424
425         switch (discoverable_mode) {
426         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
427                 result = adapter_set_connectable(TRUE);
428                 timeout = 0;
429                 break;
430         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
431                 result = adapter_set_discoverable();
432                 timeout = 0;
433                 break;
434         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
435                 result = adapter_set_discoverable();
436                 break;
437         default:
438                 return BLUETOOTH_ERROR_INVALID_PARAM;
439         }
440
441         if (result != OAL_STATUS_SUCCESS) {
442                 BT_ERR("set scan mode failed %d", result);
443                 return BLUETOOTH_ERROR_INTERNAL;
444         }
445
446         result = adapter_set_discoverable_timeout(timeout);
447         if (result != OAL_STATUS_SUCCESS) {
448                 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
449                 return BLUETOOTH_ERROR_INTERNAL;
450         }
451
452         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
453                 timeout = -1;
454
455         result = __bt_set_visible_time(timeout);
456
457         BT_DBG("-");
458         return result;
459 }
460
461 gboolean _bt_is_connectable(void)
462 {
463         int connectable = 0;
464         int result;
465
466         BT_DBG("+");
467
468         adapter_is_connectable(&connectable);
469         if (connectable)
470                 result = TRUE;
471         else
472                 result = FALSE;
473
474         BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
475         BT_DBG("-");
476         return result;
477 }
478
479 int _bt_set_connectable(gboolean connectable)
480 {
481         int result = BLUETOOTH_ERROR_NONE;
482
483         BT_DBG("+");
484         result =  adapter_set_connectable(connectable);
485         if (result != OAL_STATUS_SUCCESS) {
486                 BT_ERR("adapter_set_connectable failed: %d", result);
487                 result = BLUETOOTH_ERROR_INTERNAL;
488         } else
489                 result = BLUETOOTH_ERROR_NONE;
490
491         BT_DBG("-");
492         return result;
493 }
494
495 int _bt_is_service_used(void)
496 {
497         int result;
498
499         BT_DBG("+");
500
501         result =  adapter_get_service_uuids();
502         if (result != OAL_STATUS_SUCCESS) {
503                 BT_ERR("adapter_get_service_uuids failed: %d", result);
504                 result = BLUETOOTH_ERROR_INTERNAL;
505         } else {
506                 result = BLUETOOTH_ERROR_NONE;
507         }
508
509         BT_DBG("-");
510         return result;
511 }
512
513 int _bt_adapter_get_bonded_devices(void)
514 {
515         int result = BLUETOOTH_ERROR_NONE;
516
517         BT_DBG("+");
518         result =  adapter_get_bonded_devices();
519         if (result != OAL_STATUS_SUCCESS) {
520                 BT_ERR("adapter_get_bonded_devices failed: %d", result);
521                 result = BLUETOOTH_ERROR_INTERNAL;
522         } else
523                 result = BLUETOOTH_ERROR_NONE;
524
525         BT_DBG("-");
526         return result;
527 }
528
529 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
530 {
531         int result = BLUETOOTH_ERROR_NONE;
532
533         BT_DBG("+");
534
535         switch(event_type) {
536         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
537         case OAL_EVENT_OAL_INITIALISED_FAILED:
538                 __bt_handle_oal_initialisation(event_type);
539                 break;
540         case OAL_EVENT_ADAPTER_ENABLED:
541                 __bt_adapter_state_change_callback(BT_ACTIVATED);
542                 break;
543         case OAL_EVENT_ADAPTER_DISABLED:
544                 __bt_adapter_state_change_callback(BT_DEACTIVATED);
545                 break;
546         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
547                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
548                 break;
549         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
550                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
551                 break;
552         case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
553                 bt_address_t *bd_addr = event_data;
554                 bluetooth_device_address_t local_address;
555
556                 /* Copy data */
557                 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
558                 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
559                                 local_address.addr[0], local_address.addr[1], local_address.addr[2],
560                                 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
561
562                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
563                                 (void *) &local_address, sizeof(bluetooth_device_address_t));
564                 break;
565         }
566         case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
567                 char *name = event_data;
568                 BT_DBG("Adapter Name: %s", name);
569
570                 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
571                         bluetooth_device_name_t local_name;
572
573                         memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
574                         g_strlcpy(local_name.name,
575                                 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
576                         __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
577                                 (void *) &local_name, sizeof(bluetooth_device_name_t));
578                 } else {
579                         /* Send event to application */
580                         _bt_send_event(BT_ADAPTER_EVENT,
581                                         BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
582                                         g_variant_new("(is)", result, name));
583                 }
584                 break;
585         }
586         case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
587                 char *ver = event_data;
588                 bluetooth_version_t local_version;
589
590                 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
591                 g_strlcpy(local_version.version,
592                                 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
593                 BT_DBG("BT Version: %s", local_version.version);
594
595                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
596                                 (void *) &local_version, sizeof(bluetooth_version_t));
597                 break;
598         }
599         case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
600                 int mode = -1;
601                 gboolean connectable = FALSE;
602
603                 BT_INFO("Adapter discoverable mode:"
604                                 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
605                 _bt_send_event(BT_ADAPTER_EVENT,
606                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
607                                 g_variant_new("(b)", connectable));
608
609                 _bt_send_event(BT_ADAPTER_EVENT,
610                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
611                                 g_variant_new("(in)", result, mode));
612                 break;
613         }
614         case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
615                 int mode;
616                 gboolean connectable = TRUE;
617
618                 BT_INFO("Adapter discoverable mode:"
619                                 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
620                 _bt_send_event(BT_ADAPTER_EVENT,
621                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
622                                 g_variant_new("(b)", connectable));
623
624                 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
625                 _bt_send_event(BT_ADAPTER_EVENT,
626                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
627                                 g_variant_new("(in)", result, mode));
628                 break;
629         }
630         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
631                 int mode;
632
633                 BT_INFO("Adapter discoverable mode:"
634                                 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
635
636                 /* Send event to application */
637                 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
638                 _bt_send_event(BT_ADAPTER_EVENT,
639                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
640                                 g_variant_new("(in)", result, mode));
641
642                 break;
643         }
644         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
645                 int *timeout = event_data;
646                 int mode;
647
648                 BT_INFO("Discoverable timeout: [%d]", *timeout);
649
650                 /* Send event to application */
651                 _bt_get_discoverable_mode(&mode);
652                 _bt_send_event(BT_ADAPTER_EVENT,
653                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
654                                 g_variant_new("(in)", result, mode));
655                 break;
656         }
657         case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
658                 int count;
659                 service_uuid_t *service_list;
660                 event_adapter_services_t *list = event_data;
661
662                 count = list->num;
663                 service_list = list->service_list;
664                 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
665                 break;
666         }
667         case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
668                 int i;
669                 int count;
670                 bluetooth_device_address_t *addr_list;
671
672                 event_device_list_t *bonded_device_list = event_data;
673                 count = bonded_device_list->num;
674
675                 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
676                 for (i = 0; i < count; i++) {
677                         memcpy(addr_list[i].addr,
678                                         bonded_device_list->devices[i].addr,
679                                         BLUETOOTH_ADDRESS_LENGTH);
680                 }
681
682                 BT_INFO("Adapter Bonded device List count: [%d]", count);
683                 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
684                                 (void *)addr_list, bonded_device_list->num);
685                 break;
686         }
687         default:
688                 BT_ERR("Unhandled event..");
689                 break;
690         }
691
692         BT_DBG("-");
693 }
694
695 static int __bt_init_profiles()
696 {
697         int ret;
698
699         /*TODO: Init bluetooth profiles */
700         ret = _bt_hidhost_initialize();
701         if (ret != BLUETOOTH_ERROR_NONE) {
702                 BT_ERR("_bt_hidhost_initialize Failed");
703                 return ret;
704         }
705         ret = _bt_socket_init();
706         if (ret != BLUETOOTH_ERROR_NONE) {
707                 BT_ERR("_bt_socket_init Failed");
708                 return ret;
709         }
710         /* Initialize A2DP Source */
711         ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
712         if (ret != BLUETOOTH_ERROR_NONE) {
713                 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
714                 return ret;
715         }
716
717         /* Initialize AVRCP Target */
718         ret = _bt_audio_initialize(BT_AVRCP_MODULE);
719         if (ret != BLUETOOTH_ERROR_NONE) {
720                 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
721                 return ret;
722         }
723
724         /* Initialize A2DP Sink */
725         ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
726         if (ret != BLUETOOTH_ERROR_NONE) {
727                 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
728                 return ret;
729         }
730         /* Initialize HFP Audio Gateway */
731         ret = _bt_audio_initialize(BT_AG_MODULE);
732         if (ret != BLUETOOTH_ERROR_NONE) {
733                 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
734                 return ret;
735         }
736         /* Initialize AVRCP Controller */
737         ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
738         if (ret != BLUETOOTH_ERROR_NONE) {
739                 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
740                 return ret;
741         }
742         /* Registering callback for receiving audio services searched */
743         ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
744         if (ret != BLUETOOTH_ERROR_NONE) {
745                 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
746                 return ret;
747         }
748
749         ret = _bt_hdp_init();
750         if (ret != BLUETOOTH_ERROR_NONE) {
751                 BT_ERR("_bt_hdp_init Failed");
752                 return ret;
753         }
754
755         ret = _bt_le_init();
756         if (ret != BLUETOOTH_ERROR_NONE) {
757                 BT_ERR("_bt_le_init Failed");
758                 return ret;
759         }
760
761         ret = _bt_gatt_init();
762         if (ret != BLUETOOTH_ERROR_NONE) {
763                 BT_ERR("_bt_gatt_init Failed");
764                 return ret;
765         }
766
767         return BLUETOOTH_ERROR_NONE;
768 }
769
770 /* OAL post initialization handler */
771 static void __bt_post_oal_init(void)
772 {
773         int ret;
774         int status = VCONFKEY_BT_STATUS_OFF;
775
776         BT_DBG("OAL initialized, Init profiles..");
777         ret = __bt_init_profiles();
778         if (ret != BLUETOOTH_ERROR_NONE)
779                 BT_ERR("Bluetooth profile init error: %d", ret);
780
781         if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
782                 BT_ERR("Fail to get the enabled value");
783         }
784
785         /* Update Bluetooth Status to OFF */
786         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
787                 BT_ERR("Set vconf failed\n");
788
789         if (status & VCONFKEY_BT_STATUS_ON) {
790                 ret = _bt_enable_adapter();
791                 if (ret != BLUETOOTH_ERROR_NONE)
792                         BT_ERR("_bt_enable_adapter failed with error: %d", ret);
793         }
794
795         return;
796 }
797
798 /* OAL initialization handler */
799 static void __bt_handle_oal_initialisation(oal_event_t event)
800 {
801         BT_DBG("");
802
803         switch(event) {
804         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
805                 __bt_post_oal_init();
806                 break;
807         case OAL_EVENT_OAL_INITIALISED_FAILED:
808                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
809                 g_idle_add(_bt_terminate_service, NULL);
810                 break;
811         default:
812                 BT_ERR("Unknown Event");
813                 break;
814         }
815 }
816
817 static gboolean __bt_is_service_request_present(int service_function)
818 {
819         GSList *l;
820         invocation_info_t *req_info;
821
822         BT_DBG("+");
823
824         /* Get method invocation context */
825         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
826                 req_info = l->data;
827                 if (req_info && req_info->service_function == service_function)
828                         return TRUE;
829         }
830
831         BT_DBG("-");
832         return FALSE;
833 }
834
835 /* Internal functions of core adapter service */
836 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
837 {
838         GSList *l;
839         GArray *out_param;
840         invocation_info_t *req_info;
841         BT_INFO("+");
842
843         /* Get method invocation context */
844         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
845                 req_info = l->data;
846                 if (req_info == NULL || req_info->service_function != service_function)
847                         continue;
848
849                 /* Create out param */
850                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
851
852                 switch(service_function) {
853                 case BT_ENABLE_ADAPTER:
854                 case BT_DISABLE_ADAPTER: {
855                         gboolean done = TRUE;
856                         g_array_append_vals(out_param, &done, sizeof(gboolean));
857                         break;
858                 }
859                 case BT_GET_LOCAL_NAME:
860                 case BT_GET_LOCAL_ADDRESS:
861                 case BT_GET_LOCAL_VERSION:
862                         g_array_append_vals(out_param, user_data, size);
863                         break;
864                 case BT_IS_SERVICE_USED: {
865                         int i;
866                         gboolean used = FALSE;
867                         unsigned char *uuid;
868                         char uuid_str[BT_UUID_STRING_SIZE];
869                         char *request_uuid = req_info->user_data;
870                         service_uuid_t *service_list = user_data;
871
872                         BT_INFO("Check for service uuid: %s", request_uuid);
873                         for (i = 0; i < size; i++) {
874                                 uuid = service_list[i].uuid;
875                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
876                                 BT_INFO("Adapter Service: [%s]", uuid_str);
877                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
878                                         BT_INFO("UUID matched!!");
879                                         used = TRUE;
880                                         break;
881                                 }
882                         }
883
884                         g_array_append_vals(out_param, &used, sizeof(gboolean));
885                         break;
886                 }
887                 case BT_GET_BONDED_DEVICES: {
888                         bluetooth_device_address_t *addr_list = user_data;
889                         bonded_devices_req_info_t *bonded_devices_req_info;
890                         char address[BT_ADDRESS_STRING_SIZE];
891                         int count = size;
892                         int res = BLUETOOTH_ERROR_NONE;
893
894                         /*
895                          * BT_GET_BONDED_DEVICES is already processed for this request,
896                          * continue for next BT_GET_BONDED_DEVICES request if any
897                          */
898                         if (NULL != req_info->user_data)
899                                 continue;
900
901                         BT_DBG("Total num of bonded devices = [%d]", count);
902                         /* No bonded devices, return method invocation */
903                         if (0 == count || !addr_list)
904                                 break;
905
906                         /* Save address list in user data  for futur reference. */
907                         bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
908                         if (!bonded_devices_req_info) {
909                                 BT_ERR("Memory allocation failed");
910                                 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
911                                 g_free(addr_list);
912                                 break;
913                         }
914
915                         bonded_devices_req_info->count = count;
916                         bonded_devices_req_info->addr_list = addr_list;
917                         bonded_devices_req_info->out_param = out_param;
918                         req_info->user_data = bonded_devices_req_info;
919
920                         while (bonded_devices_req_info->count > 0) {
921                                 bonded_devices_req_info->count -= 1;
922                                 res = _bt_device_get_bonded_device_info(
923                                                 &addr_list[bonded_devices_req_info->count]);
924                                 if (BLUETOOTH_ERROR_NONE == res)
925                                         return;
926                                 else {
927                                         _bt_convert_addr_type_to_string((char *)address,
928                                                         addr_list[bonded_devices_req_info->count].addr);
929                                         BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
930                                         if (bonded_devices_req_info->count == 0) {
931                                                 g_free(bonded_devices_req_info->addr_list);
932                                                 g_free(bonded_devices_req_info);
933                                                 req_info->user_data = NULL;
934                                         }
935                                 }
936                         }
937                         break;
938                 }
939                 default:
940                         BT_ERR("Unknown service function[%d]", service_function);
941                 }
942
943                 _bt_service_method_return(req_info->context, out_param, req_info->result);
944                 g_array_free(out_param, TRUE);
945                 /* Now free invocation info for this request*/
946                 _bt_free_info_from_invocation_list(req_info);
947         }
948 }
949
950 /* Request return handlings */
951 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
952 {
953         BT_INFO("__bt_adapter_post_set_enabled>>");
954
955 #ifdef TIZEN_MOBILE
956         __bt_set_visible_mode();
957 #else
958 #ifdef TIZEN_TV
959         if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
960                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
961                 BT_ERR("Fail to set discoverable mode");
962 #endif
963 #endif
964         __bt_set_local_name();
965
966         /* Get All properties */
967         if (OAL_STATUS_SUCCESS != adapter_get_properties())
968                 BT_ERR("adapter_get_properties failed");
969
970         /* Add Adapter enabled post processing codes */
971         return FALSE;
972 }
973
974 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
975 {
976         BT_INFO("_bt_adapter_post_set_disabled>>");
977         /* Add Adapter disabled post processing codes */
978         return FALSE;
979 }
980
981 static void __bt_adapter_update_bt_enabled(void)
982 {
983         int result = BLUETOOTH_ERROR_NONE;
984         BT_INFO("_bt_adapter_update_bt_enabled>>");
985         /* Update Bluetooth Status to notify other modules */
986         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
987                 BT_ERR("Set vconf failed\n");
988
989         /* TODO:Add timer function to handle any further post processing */
990         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
991
992         /*Return BT_ADAPTER_ENABLE Method invocation context */
993         __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
994         /*Send BT Enabled event to application */
995         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
996                         g_variant_new("(i)", result));
997 }
998
999 static void __bt_adapter_update_bt_disabled(void)
1000 {
1001         int result = BLUETOOTH_ERROR_NONE;
1002         BT_INFO("_bt_adapter_update_bt_disabled>>");
1003
1004         int power_off_status = 0;
1005         int ret;
1006
1007         /* Update the vconf BT status in normal Deactivation case only */
1008         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1009         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1010
1011         /* Update Bluetooth Status to notify other modules */
1012         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1013                 BT_ERR("Set vconf failed\n");
1014
1015         /* TODO:Add timer function to handle any further post processing */
1016         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1017
1018         /* Return BT_ADAPTER_DISABLE Method invocation context */
1019         __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1020
1021         /* Send BT Disabled event to application */
1022         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1023                         g_variant_new("(i)", result));
1024 }
1025
1026
1027 static void __bt_adapter_state_set_status(bt_status_t status)
1028 {
1029         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1030         adapter_state = status;
1031 }
1032
1033 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1034 {
1035         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1036         adapter_discovery_state = status;
1037 }
1038
1039 static void __bt_adapter_state_change_callback(int bt_status)
1040 {
1041         BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1042
1043         switch (bt_status) {
1044         case BT_DEACTIVATED:
1045                 __bt_adapter_state_set_status(bt_status);
1046
1047                 /* Adapter is disabled, unregister event handlers */
1048                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1049                 //_bt_deinit_device_event_handler();
1050
1051                 /* Add Adapter disabled post processing codes */
1052                 __bt_adapter_update_bt_disabled();
1053                 break;
1054         case BT_ACTIVATED:
1055                 __bt_adapter_state_set_status(bt_status);
1056                 /* Add Adapter enabled post processing codes */
1057                 if (timer_id > 0) {
1058                         BT_DBG("g_source is removed");
1059                         g_source_remove(timer_id);
1060                         timer_id = 0;
1061                 }
1062                 __bt_adapter_update_bt_enabled();
1063                 break;
1064         default:
1065                 BT_ERR("Incorrect Bluetooth adapter state changed status");
1066
1067         }
1068 }
1069
1070 static int __bt_adapter_state_handle_request(gboolean enable)
1071 {
1072         int result = BLUETOOTH_ERROR_NONE;
1073         BT_DBG("");
1074
1075         switch (adapter_state) {
1076         case BT_ACTIVATING:
1077         {
1078                 BT_INFO("Adapter is currently in activating state, state [%d]",
1079                                 adapter_state);
1080                 if (enable) {
1081                         return BLUETOOTH_ERROR_IN_PROGRESS;
1082                 } else {
1083                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1084                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1085                                 /*TODO Stop Discovery*/
1086                                 if (result != OAL_STATUS_SUCCESS)
1087                                         BT_ERR("Discover stop failed: %d", result);
1088                                 __bt_adapter_update_discovery_status(FALSE);
1089                         }
1090                         result = adapter_disable();
1091                         if (result != OAL_STATUS_SUCCESS) {
1092                                 BT_ERR("adapter_enable failed: [%d]", result);
1093                                 result = BLUETOOTH_ERROR_INTERNAL;
1094                                 /*TODO: perform if anything more needs to be done to handle failure */
1095                         } else {
1096                                 /* TODO: To be handled */
1097                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1098                                 result = BLUETOOTH_ERROR_NONE;
1099                         }
1100                 }
1101                 break;
1102         }
1103         case BT_ACTIVATED:
1104         {
1105                 BT_INFO("Adapter is currently in activated state, state [%d]",
1106                                 adapter_state);
1107                 if (enable) {
1108                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1109                 } else {
1110                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1111                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1112                                 /*TODO Stop Discovery*/
1113                                 if (result != OAL_STATUS_SUCCESS)
1114                                         BT_ERR("Discover stop failed: %d", result);
1115                                 __bt_adapter_update_discovery_status(FALSE);
1116                         }
1117                         result = adapter_disable();
1118                         if (result != OAL_STATUS_SUCCESS) {
1119                                 BT_ERR("adapter_enable failed: [%d]", result);
1120                                 result = BLUETOOTH_ERROR_INTERNAL;
1121                                 /*TODO: perform if anything more needs to be done to handle failure */
1122                         } else {
1123                                 /* TODO: To be handled */
1124                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1125                                 result = BLUETOOTH_ERROR_NONE;
1126                         }
1127                 }
1128                 break;
1129         }
1130         case BT_DEACTIVATING:
1131         {
1132                 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1133                                 adapter_state);
1134                 if (!enable) {
1135                         return BLUETOOTH_ERROR_IN_PROGRESS;
1136
1137                 } else {
1138                         result = adapter_enable();
1139                         if (result != OAL_STATUS_SUCCESS) {
1140                                 BT_ERR("adapter_enable failed: [%d]", result);
1141                                 adapter_disable();
1142                                 result = BLUETOOTH_ERROR_INTERNAL;
1143                                 /*TODO: perform if anything more needs to be done to handle failure */
1144                         } else {
1145                                 /* TODO: To be handled */
1146                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1147                                 result = BLUETOOTH_ERROR_NONE;
1148                         }
1149                 }
1150                 break;
1151         }
1152         case BT_DEACTIVATED:
1153         {
1154                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1155                                 adapter_state);
1156                 if (!enable) {
1157                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1158                 } else {
1159                         result = adapter_enable();
1160                         if (result != OAL_STATUS_SUCCESS) {
1161                                 BT_ERR("adapter_enable failed: [%d]", result);
1162                                 adapter_disable();
1163                                 result = BLUETOOTH_ERROR_INTERNAL;
1164                                 /*TODO: perform if anything more needs to be done to handle failure */
1165                         } else {
1166                                 /* TODO: To be handled */
1167                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1168                                 result = BLUETOOTH_ERROR_NONE;
1169                         }
1170                 }
1171                 break;
1172         }
1173         }
1174         if (enable && result == BLUETOOTH_ERROR_NONE) {
1175                 /* Adapter enable request is successful, setup event handlers */
1176                 _bt_service_register_event_handler_callback(
1177                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1178                 _bt_device_state_handle_callback_set_request();
1179         }
1180         return result;
1181 }
1182
1183 static int __bt_adapter_state_discovery_request(gboolean enable,
1184                 unsigned short max_response, unsigned short duration, unsigned int mask)
1185 {
1186         int result = BLUETOOTH_ERROR_NONE;
1187
1188         BT_DBG("+");
1189         switch (adapter_discovery_state) {
1190         case ADAPTER_DISCOVERY_STARTED: {
1191                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1192                                 adapter_discovery_state);
1193                 if (enable) {
1194                         return BLUETOOTH_ERROR_IN_PROGRESS;
1195                 } else {
1196                         result = adapter_stop_inquiry();
1197                         if (result != OAL_STATUS_SUCCESS) {
1198                                 BT_ERR("Discover stop failed: %d", result);
1199                                 result = BLUETOOTH_ERROR_INTERNAL;
1200                         } else {
1201                                 BT_ERR("Stop Discovery Triggered successfully");
1202                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1203                                 result = BLUETOOTH_ERROR_NONE;
1204                         }
1205                 }
1206                 break;
1207         }
1208         case ADAPTER_DISCOVERY_STARTING: {
1209                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1210                                 adapter_discovery_state);
1211                 if (enable) {
1212                         return BLUETOOTH_ERROR_IN_PROGRESS;
1213                 } else {
1214                         result = adapter_stop_inquiry();
1215                         if (result != OAL_STATUS_SUCCESS) {
1216                                 BT_ERR("Discover stop failed: %d", result);
1217                                 result = BLUETOOTH_ERROR_INTERNAL;
1218                         } else {
1219                                 BT_ERR("Stop Discovery Triggered successfully");
1220                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1221                                 result = BLUETOOTH_ERROR_NONE;
1222                         }
1223                 }
1224                 break;
1225         }
1226         case ADAPTER_DISCOVERY_STOPPED: {
1227                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1228                                 adapter_discovery_state);
1229                 if (!enable)
1230                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1231                 else {
1232                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1233                         result = adapter_start_inquiry(duration);
1234                         if (result != OAL_STATUS_SUCCESS) {
1235                                 BT_ERR("Start Discovery failed: %d", result);
1236                                 result = BLUETOOTH_ERROR_INTERNAL;
1237                         } else {
1238                                 BT_ERR("Start Discovery Triggered successfully");
1239                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1240                                 result = BLUETOOTH_ERROR_NONE;
1241                         }
1242                 }
1243                 break;
1244         }
1245         case ADAPTER_DISCOVERY_STOPPING: {
1246                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1247                                 adapter_discovery_state);
1248                 if (!enable)
1249                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1250                 else {
1251                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1252                         result = adapter_start_inquiry(duration);
1253                         if (result != OAL_STATUS_SUCCESS) {
1254                                 BT_ERR("Start Discovery failed: %d", result);
1255                                 result = BLUETOOTH_ERROR_INTERNAL;
1256                         } else {
1257                                 BT_ERR("Start Discovery Triggered successfully");
1258                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1259                                 result = BLUETOOTH_ERROR_NONE;
1260                         }
1261                 }
1262                 break;
1263         }
1264         }
1265
1266         BT_DBG("-");
1267         return result;
1268 }
1269
1270 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1271 {
1272         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1273         GVariant *param = NULL;
1274         int result = BLUETOOTH_ERROR_NONE;
1275
1276         switch (bt_discovery_status) {
1277         case ADAPTER_DISCOVERY_STOPPED:
1278         {
1279                 __bt_adapter_update_discovery_status(bt_discovery_status);
1280                 param = g_variant_new("(i)", result);
1281                 _bt_send_event(BT_ADAPTER_EVENT,
1282                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1283                                 param);
1284                 break;
1285         }
1286         case ADAPTER_DISCOVERY_STARTED:
1287         {
1288                 __bt_adapter_update_discovery_status(bt_discovery_status);
1289                 param = g_variant_new("(i)", result);
1290                 _bt_send_event(BT_ADAPTER_EVENT,
1291                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1292                                 param);
1293                 break;
1294         }
1295         default:
1296                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1297         }
1298 }
1299
1300 #ifdef TIZEN_MOBILE
1301 static void __bt_set_visible_mode(void)
1302 {
1303         int timeout = 0;
1304
1305         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1306                 BT_ERR("Fail to get the timeout value");
1307
1308 #ifdef TIZEN_DPM_ENABLE
1309         if (timeout == -1 ||
1310                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1311                 if (_bt_set_discoverable_mode(
1312                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1313                                         timeout) != BLUETOOTH_ERROR_NONE) {
1314                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1315                                 BT_ERR("Set vconf failed");
1316                 }
1317         } else {
1318                 if (_bt_set_discoverable_mode(
1319                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1320                                         timeout) != BLUETOOTH_ERROR_NONE) {
1321                         BT_ERR("Set connectable mode failed");
1322                 }
1323         }
1324 #else
1325         if (timeout == -1) {
1326                 if (_bt_set_discoverable_mode(
1327                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1328                                         timeout) != BLUETOOTH_ERROR_NONE) {
1329                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1330                                 BT_ERR("Set vconf failed");
1331                 }
1332         } else {
1333                 if (_bt_set_discoverable_mode(
1334                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1335                                         timeout) != BLUETOOTH_ERROR_NONE) {
1336                         BT_ERR("Set connectable mode failed");
1337                 }
1338         }
1339 #endif
1340 }
1341 #endif
1342
1343 static void __bt_set_local_name(void)
1344 {
1345         char *phone_name = NULL;
1346         char *ptr = NULL;
1347
1348         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1349
1350         if (!phone_name)
1351                 return;
1352
1353         if (strlen(phone_name) != 0) {
1354                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1355                         *ptr = '\0';
1356
1357                 _bt_set_local_name(phone_name);
1358         }
1359         free(phone_name);
1360 }