[Adapt] Implement set discoverable mode API
[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 #ifdef TIZEN_DPM_ENABLE
45 #include "bt-service-dpm.h"
46 #endif
47
48 /* OAL headers */
49 #include <oal-event.h>
50 #include <oal-manager.h>
51 #include <oal-adapter-mgr.h>
52
53 #define BT_ENABLE_TIMEOUT 20000 /* 20 seconds */
54
55 /*This file will contain state machines related to adapter and remote device */
56
57 /* Global variables */
58 typedef struct {
59         guint event_id;
60         int timeout;
61         time_t start_time;
62         gboolean alarm_init;
63         int alarm_id;
64 } bt_adapter_timer_t;
65
66 static bt_adapter_timer_t visible_timer;
67
68 static guint timer_id = 0;
69
70 /* Adapter default states */
71 static bt_status_t adapter_state = BT_DEACTIVATED;
72 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
73
74 /* Forward declarations */
75 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
76 static void __bt_post_oal_init(void);
77 static void __bt_handle_oal_initialisation(oal_event_t event);
78 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
79 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
80 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
81 static void __bt_adapter_update_bt_enabled(void);
82 static void __bt_adapter_update_bt_disabled(void);
83 static void __bt_adapter_state_set_status(bt_status_t status);
84 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
85 static void __bt_adapter_state_change_callback(int bt_status);
86 static int __bt_adapter_state_handle_request(gboolean enable);
87 static int __bt_adapter_state_discovery_request(gboolean enable);
88 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
89
90 /* Initialize BT stack (Initialize OAL layer) */
91 int _bt_stack_init(void)
92 {
93         int ret;
94
95         BT_INFO("[bt-service] Start to initialize BT stack");
96         /* Adapter enable request is successful, setup event handlers */
97         _bt_service_register_event_handler_callback(
98                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
99
100         ret = oal_bt_init(_bt_service_oal_event_receiver);
101
102         if (OAL_STATUS_PENDING == ret) {
103                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
104                 return BLUETOOTH_ERROR_NONE;
105         } else if (OAL_STATUS_SUCCESS != ret) {
106                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
107                 return BLUETOOTH_ERROR_INTERNAL;
108         }
109
110         __bt_post_oal_init();
111         return BLUETOOTH_ERROR_NONE;
112 }
113
114 int _bt_enable_adapter(void)
115 {
116         return __bt_adapter_state_handle_request(TRUE);
117 }
118
119 int _bt_disable_adapter(void)
120 {
121         return __bt_adapter_state_handle_request(FALSE);
122 }
123
124
125 int _bt_start_discovery(void)
126 {
127         return __bt_adapter_state_discovery_request(TRUE);
128 }
129
130 int _bt_cancel_discovery(void)
131 {
132         return __bt_adapter_state_discovery_request(FALSE);
133 }
134
135 gboolean _bt_is_discovering(void)
136 {
137         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
138                         || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
139                 return TRUE;
140         else
141                 return FALSE;
142 }
143
144 int _bt_get_local_address(void)
145 {
146         int result;
147
148         BT_DBG("+");
149
150         result =  adapter_get_address();
151         if (result != OAL_STATUS_SUCCESS) {
152                 BT_ERR("adapter_get_address failed: %d", result);
153                 result = BLUETOOTH_ERROR_INTERNAL;
154         } else
155                 result = BLUETOOTH_ERROR_NONE;
156
157         BT_DBG("-");
158         return result;
159 }
160
161 int _bt_get_local_version(void)
162 {
163         int result;
164         BT_DBG("+");
165
166         result =  adapter_get_version();
167         if (result != OAL_STATUS_SUCCESS) {
168                 BT_ERR("adapter_get_address failed: %d", result);
169                 result = BLUETOOTH_ERROR_INTERNAL;
170         } else
171                 result = BLUETOOTH_ERROR_NONE;
172
173         BT_DBG("-");
174         return result;
175 }
176
177 int _bt_get_local_name(void)
178 {
179         int result;
180
181         BT_DBG("+");
182
183         result =  adapter_get_name();
184         if (result != OAL_STATUS_SUCCESS) {
185                 BT_ERR("adapter_get_name failed: %d", result);
186                 result = BLUETOOTH_ERROR_INTERNAL;
187         } else
188                 result = BLUETOOTH_ERROR_NONE;
189
190         BT_DBG("-");
191         return result;
192 }
193
194 int _bt_set_local_name(char *local_name)
195 {
196         int result = BLUETOOTH_ERROR_NONE;
197         BT_DBG("+");
198
199         retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
200
201         result =  adapter_set_name(local_name);
202         if (result != OAL_STATUS_SUCCESS) {
203                 BT_ERR("adapter_set_name failed: %d", result);
204                 result = BLUETOOTH_ERROR_INTERNAL;
205         } else
206                 result = BLUETOOTH_ERROR_NONE;
207
208         BT_DBG("-");
209         return result;
210 }
211
212 int _bt_get_discoverable_mode(int *mode)
213 {
214         int scan_mode = 0;
215         int timeout = 0;
216
217         BT_DBG("+");
218
219         retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
220
221         adapter_is_discoverable(&scan_mode);
222         if (TRUE == scan_mode) {
223                 adapter_get_discoverable_timeout(&timeout);
224                 if (timeout > 0)
225                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
226                 else
227                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
228         } else {
229                 adapter_is_connectable(&scan_mode);
230                 if(scan_mode == TRUE)
231                         *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
232                 else {
233                         /*
234                          * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
235                          * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
236                          * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then return -1.
237                          */
238                         return -1;
239                 }
240         }
241
242         BT_DBG("-");
243         return BLUETOOTH_ERROR_NONE;
244 }
245
246 int _bt_get_timeout_value(int *timeout)
247 {
248         time_t current_time;
249         int time_diff;
250
251         /* Take current time */
252         time(&current_time);
253         time_diff = difftime(current_time, visible_timer.start_time);
254
255         BT_DBG("Time diff = %d\n", time_diff);
256         *timeout = visible_timer.timeout - time_diff;
257
258         return BLUETOOTH_ERROR_NONE;
259 }
260
261 static void __bt_visibility_alarm_remove()
262 {
263         if (visible_timer.event_id > 0) {
264                 g_source_remove(visible_timer.event_id);
265                 visible_timer.event_id = 0;
266         }
267
268         if (visible_timer.alarm_id > 0) {
269                 alarmmgr_remove_alarm(visible_timer.alarm_id);
270                 visible_timer.alarm_id = 0;
271         }
272 }
273
274 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
275 {
276         int result = BLUETOOTH_ERROR_NONE;
277         int timeout = 0;
278
279         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
280
281         if (alarm_id != visible_timer.alarm_id)
282                 return 0;
283
284         if (visible_timer.event_id) {
285                 _bt_send_event(BT_ADAPTER_EVENT,
286                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
287                                 g_variant_new("(in)", result, timeout));
288                 g_source_remove(visible_timer.event_id);
289                 visible_timer.event_id = 0;
290                 visible_timer.timeout = 0;
291
292 #ifndef TIZEN_WEARABLE
293                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
294                         BT_ERR("Set vconf failed\n");
295 #endif
296         }
297         /* Switch Off visibility in Bluez */
298         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
299         visible_timer.alarm_id = 0;
300         return 0;
301 }
302
303 static gboolean __bt_timeout_handler(gpointer user_data)
304 {
305         int result = BLUETOOTH_ERROR_NONE;
306         time_t current_time;
307         int time_diff;
308
309         /* Take current time */
310         time(&current_time);
311         time_diff = difftime(current_time, visible_timer.start_time);
312
313         /* Send event to application */
314         _bt_send_event(BT_ADAPTER_EVENT,
315                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
316                         g_variant_new("(in)", result, time_diff));
317
318         if (visible_timer.timeout <= time_diff) {
319                 g_source_remove(visible_timer.event_id);
320                 visible_timer.event_id = 0;
321                 visible_timer.timeout = 0;
322
323 #ifndef TIZEN_WEARABLE
324                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
325                         BT_ERR("Set vconf failed\n");
326 #endif
327                 return FALSE;
328         }
329
330         return TRUE;
331 }
332
333 static void __bt_visibility_alarm_create()
334 {
335         alarm_id_t alarm_id;
336         int result;
337
338         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
339                         0, NULL, &alarm_id);
340         if (result < 0) {
341                 BT_ERR("Failed to create alarm error = %d\n", result);
342         } else {
343                 BT_DBG("Alarm created = %d\n", alarm_id);
344                 visible_timer.alarm_id = alarm_id;
345         }
346 }
347
348 static int __bt_set_visible_time(int timeout)
349 {
350         int result;
351
352         __bt_visibility_alarm_remove();
353
354         visible_timer.timeout = timeout;
355
356 #ifndef TIZEN_WEARABLE
357 #ifdef TIZEN_DPM_ENABLE
358         if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
359 #endif
360                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
361                         BT_ERR("Set vconf failed");
362 #ifdef TIZEN_DPM_ENABLE
363         }
364 #endif
365 #endif
366
367         if (timeout <= 0)
368                 return BLUETOOTH_ERROR_NONE;
369
370         if (!visible_timer.alarm_init) {
371                 /* Set Alarm timer to switch off BT */
372                 result = alarmmgr_init("bt-service");
373                 if (result != 0)
374                         return BLUETOOTH_ERROR_INTERNAL;
375
376                 visible_timer.alarm_init = TRUE;
377         }
378
379         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
380         if (result != 0)
381                 return BLUETOOTH_ERROR_INTERNAL;
382
383         /* Take start time */
384         time(&(visible_timer.start_time));
385         visible_timer.event_id = g_timeout_add_seconds(1,
386                         __bt_timeout_handler, NULL);
387
388         __bt_visibility_alarm_create();
389
390         return BLUETOOTH_ERROR_NONE;
391 }
392
393 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
394 {
395         int result;
396
397         BT_DBG("+");
398
399         BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
400
401 #ifdef TIZEN_DPM_ENABLE
402         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
403                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
404                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
405                 return BLUETOOTH_ERROR_ACCESS_DENIED;
406         }
407         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
408                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
409                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
410                 return BLUETOOTH_ERROR_ACCESS_DENIED;
411         }
412 #endif
413
414         switch (discoverable_mode) {
415         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
416                 result = adapter_set_connectable(TRUE);
417                 timeout = 0;
418                 break;
419         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
420                 result = adapter_set_discoverable();
421                 timeout = 0;
422                 break;
423         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
424                 result = adapter_set_discoverable();
425                 break;
426         default:
427                 return BLUETOOTH_ERROR_INVALID_PARAM;
428         }
429
430         if (result != OAL_STATUS_SUCCESS) {
431                 BT_ERR("set scan mode failed %d", result);
432                 return BLUETOOTH_ERROR_INTERNAL;
433         }
434
435         result = adapter_set_discoverable_timeout(timeout);
436         if (result != OAL_STATUS_SUCCESS) {
437                 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
438                 return BLUETOOTH_ERROR_INTERNAL;
439         }
440
441         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
442                 timeout = -1;
443
444         result = __bt_set_visible_time(timeout);
445
446         BT_DBG("-");
447         return result;
448 }
449
450 gboolean _bt_is_connectable(void)
451 {
452         int connectable = 0;
453         int result;
454
455         BT_DBG("+");
456
457         adapter_is_connectable(&connectable);
458         if (connectable)
459                 result = TRUE;
460         else
461                 result = FALSE;
462
463         BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
464         BT_DBG("-");
465         return result;
466 }
467
468 int _bt_set_connectable(gboolean connectable)
469 {
470         int result = BLUETOOTH_ERROR_NONE;
471
472         BT_DBG("+");
473         result =  adapter_set_connectable(connectable);
474         if (result != OAL_STATUS_SUCCESS) {
475                 BT_ERR("adapter_get_address failed: %d", result);
476                 result = BLUETOOTH_ERROR_INTERNAL;
477         } else
478                 result = BLUETOOTH_ERROR_NONE;
479
480         BT_DBG("-");
481         return result;
482 }
483
484 int _bt_is_service_used(void)
485 {
486         int result;
487
488         BT_DBG("+");
489
490         result =  adapter_get_service_uuids();
491         if (result != OAL_STATUS_SUCCESS) {
492                 BT_ERR("adapter_get_service_uuids failed: %d", result);
493                 result = BLUETOOTH_ERROR_INTERNAL;
494         } else {
495                 result = BLUETOOTH_ERROR_NONE;
496         }
497
498         BT_DBG("-");
499         return result;
500 }
501
502 int _bt_adapter_get_bonded_devices(void)
503 {
504         int result = BLUETOOTH_ERROR_NONE;
505
506         BT_DBG("+");
507         result =  adapter_get_bonded_devices();
508         if (result != OAL_STATUS_SUCCESS) {
509                 BT_ERR("adapter_get_bonded_devices failed: %d", result);
510                 result = BLUETOOTH_ERROR_INTERNAL;
511         } else
512                 result = BLUETOOTH_ERROR_NONE;
513
514         BT_DBG("-");
515         return result;
516 }
517
518 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
519 {
520         BT_DBG("+");
521
522         switch(event_type) {
523         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
524         case OAL_EVENT_OAL_INITIALISED_FAILED:
525                 __bt_handle_oal_initialisation(event_type);
526                 break;
527         case OAL_EVENT_ADAPTER_ENABLED:
528                 __bt_adapter_state_change_callback(BT_ACTIVATED);
529                 break;
530         case OAL_EVENT_ADAPTER_DISABLED:
531                 __bt_adapter_state_change_callback(BT_DEACTIVATED);
532                 break;
533         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
534                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
535                 break;
536         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
537                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
538                 break;
539         case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
540                 bt_address_t *bd_addr = event_data;
541                 bluetooth_device_address_t local_address;
542
543                 /* Copy data */
544                 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
545                 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
546                                 local_address.addr[0], local_address.addr[1], local_address.addr[2],
547                                 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
548
549                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
550                                 (void *) &local_address, sizeof(bluetooth_device_address_t));
551                 break;
552         }
553         case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
554                 char *name = event_data;
555                 bluetooth_device_name_t local_name;
556
557                 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
558                 /* Copy data */
559                 g_strlcpy(local_name.name,
560                                 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
561                 BT_DBG("Adapter Name: %s", local_name.name);
562
563                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
564                                 (void *) &local_name, sizeof(bluetooth_device_name_t));
565                 break;
566         }
567         case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
568                 char *ver = event_data;
569                 bluetooth_version_t local_version;
570
571                 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
572                 g_strlcpy(local_version.version,
573                                 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
574                 BT_DBG("BT Version: %s", local_version.version);
575
576                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
577                                 (void *) &local_version, sizeof(bluetooth_version_t));
578                 break;
579         }
580         case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
581                 gboolean connectable = FALSE;
582
583                 BT_INFO("Adapter discoverable mode:"
584                                 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
585                 _bt_send_event(BT_ADAPTER_EVENT,
586                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
587                                 g_variant_new("(b)", connectable));
588                 break;
589         }
590         case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
591                 gboolean connectable = TRUE;
592
593                 BT_INFO("Adapter discoverable mode:"
594                                 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
595                 _bt_send_event(BT_ADAPTER_EVENT,
596                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
597                                 g_variant_new("(b)", connectable));
598                 break;
599         }
600         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
601                 BT_INFO("Adapter discoverable mode:"
602                                 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
603                 break;
604         }
605         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
606                 int *timeout = event_data;
607
608                 BT_INFO("Discoverable timeout: [%d]", *timeout);
609                 break;
610         }
611         case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
612                 int count;
613                 service_uuid_t *service_list;
614                 event_adapter_services_t *list = event_data;
615
616                 count = list->num;
617                 service_list = list->service_list;
618                 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
619                 break;
620         }
621         case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
622                 int i;
623                 int count;
624                 bluetooth_device_address_t *addr_list;
625
626                 event_device_list_t *bonded_device_list = event_data;
627                 count = bonded_device_list->num;
628
629                 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
630                 for (i = 0; i < count; i++) {
631                         memcpy(addr_list[i].addr,
632                                         bonded_device_list->devices[i].addr,
633                                         BLUETOOTH_ADDRESS_LENGTH);
634                 }
635
636                 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
637                                 (void *)addr_list, bonded_device_list->num);
638                 break;
639         }
640         default:
641                 BT_ERR("Unhandled event..");
642                 break;
643         }
644
645         BT_DBG("-");
646 }
647
648 /* OAL post initialization handler */
649 static void __bt_post_oal_init(void)
650 {
651         BT_DBG("OAL initialized, Init profiles..");
652         /*TODO */
653         return;
654 }
655
656 /* OAL initialization handler */
657 static void __bt_handle_oal_initialisation(oal_event_t event)
658 {
659         BT_DBG("");
660
661         switch(event) {
662         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
663                 __bt_post_oal_init();
664                 break;
665         case OAL_EVENT_OAL_INITIALISED_FAILED:
666                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
667                 g_idle_add(_bt_terminate_service, NULL);
668                 break;
669         default:
670                 BT_ERR("Unknown Event");
671                 break;
672         }
673 }
674
675 /* Internal functions of core adapter service */
676 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
677 {
678         GSList *l;
679         GArray *out_param;
680         invocation_info_t *req_info;
681         BT_INFO("+");
682
683         /* Get method invocation context */
684         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
685                 req_info = l->data;
686                 if (req_info == NULL || req_info->service_function != service_function)
687                         continue;
688
689                 /* Create out param */
690                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
691
692                 switch(service_function) {
693                 case BT_ENABLE_ADAPTER:
694                 case BT_DISABLE_ADAPTER: {
695                         gboolean done = TRUE;
696                         g_array_append_vals(out_param, &done, sizeof(gboolean));
697                         break;
698                 }
699                 case BT_GET_LOCAL_NAME:
700                 case BT_GET_LOCAL_ADDRESS:
701                 case BT_GET_LOCAL_VERSION:
702                         g_array_append_vals(out_param, user_data, size);
703                         break;
704                 case BT_IS_SERVICE_USED: {
705                         int i;
706                         gboolean used = FALSE;
707                         unsigned char *uuid;
708                         char uuid_str[BT_UUID_STRING_SIZE];
709                         char *request_uuid = req_info->user_data;
710                         service_uuid_t *service_list = user_data;
711
712                         BT_INFO("Check for service uuid: %s", request_uuid);
713                         for (i = 0; i < size; i++) {
714                                 uuid = service_list[i].uuid;
715                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
716                                 BT_INFO("Adapter Service: [%s]", uuid_str);
717                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
718                                         BT_INFO("UUID matched!!");
719                                         used = TRUE;
720                                         break;
721                                 }
722                         }
723
724                         g_array_append_vals(out_param, &used, sizeof(gboolean));
725                         break;
726                 }
727                 case BT_GET_BONDED_DEVICES: {
728                         bluetooth_device_address_t *addr_list = user_data;
729                         bonded_devices_req_info_t *bonded_devices_req_info;
730                         char address[BT_ADDRESS_STRING_SIZE];
731                         int count = size;
732                         int res = BLUETOOTH_ERROR_NONE;
733
734                         /*
735                          * BT_GET_BONDED_DEVICES is already processed for this request,
736                          * continue for next BT_GET_BONDED_DEVICES request if any
737                          */
738                         if (NULL != req_info->user_data)
739                                 continue;
740
741                         BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
742                         /* No bonded devices, return method invocation */
743                         if (0 == count || !addr_list)
744                                 break;
745
746                         /* Save address list in user data  for futur reference. */
747                         bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
748                         if (!bonded_devices_req_info) {
749                                 BT_ERR("Memory allocation failed");
750                                 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
751                                 g_free(addr_list);
752                                 break;
753                         }
754
755                         bonded_devices_req_info->count = count;
756                         bonded_devices_req_info->addr_list = addr_list;
757                         bonded_devices_req_info->out_param = out_param;
758                         req_info->user_data = bonded_devices_req_info;
759
760                         while (bonded_devices_req_info->count > 0) {
761                                 bonded_devices_req_info->count -= 1;
762                                 res = _bt_device_get_bonded_device_info(
763                                                 &addr_list[bonded_devices_req_info->count]);
764                                 if (BLUETOOTH_ERROR_NONE == res)
765                                         return;
766                                 else {
767                                         _bt_convert_addr_type_to_string((char *)address,
768                                                         addr_list[bonded_devices_req_info->count].addr);
769                                         BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
770                                         if (bonded_devices_req_info->count == 0) {
771                                                 g_free(bonded_devices_req_info->addr_list);
772                                                 g_free(bonded_devices_req_info);
773                                                 req_info->user_data = NULL;
774                                         }
775                                 }
776                         }
777                         break;
778                 }
779                 default:
780                         BT_ERR("Unknown service function[%d]", service_function);
781                 }
782
783                 _bt_service_method_return(req_info->context, out_param, req_info->result);
784                 g_array_free(out_param, TRUE);
785                 /* Now free invocation info for this request*/
786                 _bt_free_info_from_invocation_list(req_info);
787         }
788 }
789
790 /* Request return handlings */
791 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
792 {
793         BT_INFO("__bt_adapter_post_set_enabled>>");
794         /*TODO Get All properties */
795         /* Add Adapter enabled post processing codes */
796         return FALSE;
797 }
798
799 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
800 {
801         BT_INFO("_bt_adapter_post_set_disabled>>");
802         /* Add Adapter disabled post processing codes */
803         return FALSE;
804 }
805
806 static void __bt_adapter_update_bt_enabled(void)
807 {
808         int result = BLUETOOTH_ERROR_NONE;
809         BT_INFO("_bt_adapter_update_bt_enabled>>");
810         /* Update Bluetooth Status to notify other modules */
811         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
812                 BT_ERR("Set vconf failed\n");
813
814         /* TODO:Add timer function to handle any further post processing */
815         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
816
817         /*Return BT_ADAPTER_ENABLE Method invocation context */
818         __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
819         /*Send BT Enabled event to application */
820         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
821                         g_variant_new("(i)", result));
822 }
823
824 static void __bt_adapter_update_bt_disabled(void)
825 {
826         int result = BLUETOOTH_ERROR_NONE;
827         BT_INFO("_bt_adapter_update_bt_disabled>>");
828
829         int power_off_status = 0;
830         int ret;
831
832         /* Update the vconf BT status in normal Deactivation case only */
833         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
834         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
835
836         /* TODO:Add timer function to handle any further post processing */
837         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
838
839         /* Return BT_ADAPTER_DISABLE Method invocation context */
840         __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
841
842         /* Send BT Disabled event to application */
843         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
844                         g_variant_new("(i)", result));
845 }
846
847
848 static void __bt_adapter_state_set_status(bt_status_t status)
849 {
850         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
851         adapter_state = status;
852 }
853
854 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
855 {
856         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
857         adapter_discovery_state = status;
858 }
859
860 static void __bt_adapter_state_change_callback(int bt_status)
861 {
862         BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
863
864         switch (bt_status) {
865         case BT_DEACTIVATED:
866                 __bt_adapter_state_set_status(bt_status);
867
868                 /* Adapter is disabled, unregister event handlers */
869                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
870                 //_bt_deinit_device_event_handler();
871
872                 /* Add Adapter disabled post processing codes */
873                 __bt_adapter_update_bt_disabled();
874                 break;
875         case BT_ACTIVATED:
876                 __bt_adapter_state_set_status(bt_status);
877                 /* Add Adapter enabled post processing codes */
878                 if (timer_id > 0) {
879                         BT_DBG("g_source is removed");
880                         g_source_remove(timer_id);
881                         timer_id = 0;
882                 }
883                 __bt_adapter_update_bt_enabled();
884                 break;
885         default:
886                 BT_ERR("Incorrect Bluetooth adapter state changed status");
887
888         }
889 }
890
891 static int __bt_adapter_state_handle_request(gboolean enable)
892 {
893         int result = BLUETOOTH_ERROR_NONE;
894         BT_DBG("");
895
896         switch (adapter_state) {
897         case BT_ACTIVATING:
898         {
899                 BT_INFO("Adapter is currently in activating state, state [%d]",
900                                 adapter_state);
901                 if (enable) {
902                         return BLUETOOTH_ERROR_IN_PROGRESS;
903                 } else {
904                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
905                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
906                                 /*TODO Stop Discovery*/
907                                 if (result != OAL_STATUS_SUCCESS)
908                                         BT_ERR("Discover stop failed: %d", result);
909                                 __bt_adapter_update_discovery_status(FALSE);
910                         }
911                         result = adapter_disable();
912                         if (result != OAL_STATUS_SUCCESS) {
913                                 BT_ERR("adapter_enable failed: [%d]", result);
914                                 result = BLUETOOTH_ERROR_INTERNAL;
915                                 /*TODO: perform if anything more needs to be done to handle failure */
916                         } else {
917                                 /* TODO: To be handled */
918                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
919                                 result = BLUETOOTH_ERROR_NONE;
920                         }
921                 }
922                 break;
923         }
924         case BT_ACTIVATED:
925         {
926                 BT_INFO("Adapter is currently in activated state, state [%d]",
927                                 adapter_state);
928                 if (enable) {
929                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
930                 } else {
931                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
932                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
933                                 /*TODO Stop Discovery*/
934                                 if (result != OAL_STATUS_SUCCESS)
935                                         BT_ERR("Discover stop failed: %d", result);
936                                 __bt_adapter_update_discovery_status(FALSE);
937                         }
938                         result = adapter_disable();
939                         if (result != OAL_STATUS_SUCCESS) {
940                                 BT_ERR("adapter_enable failed: [%d]", result);
941                                 result = BLUETOOTH_ERROR_INTERNAL;
942                                 /*TODO: perform if anything more needs to be done to handle failure */
943                         } else {
944                                 /* TODO: To be handled */
945                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
946                                 result = BLUETOOTH_ERROR_NONE;
947                         }
948                 }
949                 break;
950         }
951         case BT_DEACTIVATING:
952         {
953                 BT_INFO("Adapter is currently in deactivating state, state [%d]",
954                                 adapter_state);
955                 if (!enable) {
956                         return BLUETOOTH_ERROR_IN_PROGRESS;
957
958                 } else {
959                         result = adapter_enable();
960                         if (result != OAL_STATUS_SUCCESS) {
961                                 BT_ERR("adapter_enable failed: [%d]", result);
962                                 adapter_disable();
963                                 result = BLUETOOTH_ERROR_INTERNAL;
964                                 /*TODO: perform if anything more needs to be done to handle failure */
965                         } else {
966                                 /* TODO: To be handled */
967                                 __bt_adapter_state_set_status(BT_ACTIVATING);
968                                 result = BLUETOOTH_ERROR_NONE;
969                         }
970                 }
971                 break;
972         }
973         case BT_DEACTIVATED:
974         {
975                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
976                                 adapter_state);
977                 if (!enable) {
978                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
979                 } else {
980                         result = adapter_enable();
981                         if (result != OAL_STATUS_SUCCESS) {
982                                 BT_ERR("adapter_enable failed: [%d]", result);
983                                 adapter_disable();
984                                 result = BLUETOOTH_ERROR_INTERNAL;
985                                 /*TODO: perform if anything more needs to be done to handle failure */
986                         } else {
987                                 /* TODO: To be handled */
988                                 __bt_adapter_state_set_status(BT_ACTIVATING);
989                                 result = BLUETOOTH_ERROR_NONE;
990                         }
991                 }
992                 break;
993         }
994         }
995         if (enable && result == BLUETOOTH_ERROR_NONE) {
996                 /* Adapter enable request is successful, setup event handlers */
997                 _bt_service_register_event_handler_callback(
998                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
999                 _bt_device_state_handle_callback_set_request();
1000         }
1001         return result;
1002 }
1003
1004 static int __bt_adapter_state_discovery_request(gboolean enable)
1005 {
1006         int result = BLUETOOTH_ERROR_NONE;
1007
1008         BT_DBG("+");
1009         switch (adapter_discovery_state) {
1010         case ADAPTER_DISCOVERY_STARTED: {
1011                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1012                                 adapter_discovery_state);
1013                 if (enable) {
1014                         return BLUETOOTH_ERROR_IN_PROGRESS;
1015                 } else {
1016                         result = adapter_stop_inquiry();
1017                         if (result != OAL_STATUS_SUCCESS) {
1018                                 BT_ERR("Discover stop failed: %d", result);
1019                                 result = BLUETOOTH_ERROR_INTERNAL;
1020                         } else {
1021                                 BT_ERR("Stop Discovery Triggered successfully");
1022                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1023                                 result = BLUETOOTH_ERROR_NONE;
1024                         }
1025                 }
1026                 break;
1027         }
1028         case ADAPTER_DISCOVERY_STARTING: {
1029                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1030                                 adapter_discovery_state);
1031                 if (enable) {
1032                         return BLUETOOTH_ERROR_IN_PROGRESS;
1033                 } else {
1034                         result = adapter_stop_inquiry();
1035                         if (result != OAL_STATUS_SUCCESS) {
1036                                 BT_ERR("Discover stop failed: %d", result);
1037                                 result = BLUETOOTH_ERROR_INTERNAL;
1038                         } else {
1039                                 BT_ERR("Stop Discovery Triggered successfully");
1040                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1041                                 result = BLUETOOTH_ERROR_NONE;
1042                         }
1043                 }
1044                 break;
1045         }
1046         case ADAPTER_DISCOVERY_STOPPED: {
1047                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1048                                 adapter_discovery_state);
1049                 if (!enable)
1050                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1051                 else {
1052                         result = adapter_start_inquiry();
1053                 if (result != OAL_STATUS_SUCCESS) {
1054                                 BT_ERR("Start Discovery failed: %d", result);
1055                                 result = BLUETOOTH_ERROR_INTERNAL;
1056                         } else {
1057                                 BT_ERR("Start Discovery Triggered successfully");
1058                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1059                                 result = BLUETOOTH_ERROR_NONE;
1060                         }
1061                 }
1062                 break;
1063         }
1064         case ADAPTER_DISCOVERY_STOPPING: {
1065                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1066                                 adapter_discovery_state);
1067                 if (!enable)
1068                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1069                 else {
1070                         result = adapter_start_inquiry();
1071                         if (result != OAL_STATUS_SUCCESS) {
1072                                 BT_ERR("Start Discovery failed: %d", result);
1073                                 result = BLUETOOTH_ERROR_INTERNAL;
1074                         } else {
1075                                 BT_ERR("Start Discovery Triggered successfully");
1076                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1077                                 result = BLUETOOTH_ERROR_NONE;
1078                         }
1079                 }
1080                 break;
1081         }
1082         }
1083
1084         BT_DBG("-");
1085         return result;
1086 }
1087
1088 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1089 {
1090         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1091         GVariant *param = NULL;
1092         int result = BLUETOOTH_ERROR_NONE;
1093
1094         switch (bt_discovery_status) {
1095         case ADAPTER_DISCOVERY_STOPPED:
1096         {
1097                 __bt_adapter_update_discovery_status(bt_discovery_status);
1098                 param = g_variant_new("(i)", result);
1099                 _bt_send_event(BT_ADAPTER_EVENT,
1100                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1101                                 param);
1102                 break;
1103         }
1104         case ADAPTER_DISCOVERY_STARTED:
1105         {
1106                 __bt_adapter_update_discovery_status(bt_discovery_status);
1107                 param = g_variant_new("(i)", result);
1108                 _bt_send_event(BT_ADAPTER_EVENT,
1109                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1110                                 param);
1111                 break;
1112         }
1113         default:
1114                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1115         }
1116 }