[Adapt]Add OAL and implement Bluez HAL lib
[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 /*bt-service headers */
33 #include "bt-internal-types.h"
34 #include "bt-service-common.h"
35 #include "bt-service-util.h"
36 #include "bt-service-main.h"
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-event-receiver.h"
39 #include "bt-request-handler.h"
40 #include "bt-service-event.h"
41
42 /* OAL headers */
43 #include <oal-event.h>
44 #include <oal-manager.h>
45 #include <oal-adapter-mgr.h>
46
47 #define BT_ENABLE_TIMEOUT 20000 /* 20 seconds */
48
49 /*This file will contain state machines related to adapter and remote device */
50
51 /* Forward declarations */
52 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
53 static void __bt_post_oal_init(void);
54 static void __bt_handle_oal_initialisation(oal_event_t event);
55
56 /* Initialize BT stack (Initialize OAL layer) */
57 int _bt_stack_init(void)
58 {
59         int ret;
60
61         BT_INFO("[bt-service] Start to initialize BT stack");
62         /* Adapter enable request is successful, setup event handlers */
63         _bt_service_register_event_handler_callback(
64                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
65
66         ret = oal_bt_init(_bt_service_oal_event_receiver);
67
68         if (OAL_STATUS_PENDING == ret) {
69                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
70                 return BLUETOOTH_ERROR_NONE;
71         } else if (OAL_STATUS_SUCCESS != ret) {
72                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
73                 return BLUETOOTH_ERROR_INTERNAL;
74         }
75
76         __bt_post_oal_init();
77         return BLUETOOTH_ERROR_NONE;
78 }
79
80 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
81 {
82         BT_DBG("");
83
84         switch(event_type) {
85         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
86         case OAL_EVENT_OAL_INITIALISED_FAILED:
87                 __bt_handle_oal_initialisation(event_type);
88                 break;
89         default:
90                 BT_ERR("Unhandled event..");
91                 break;
92         }
93 }
94
95 /* OAL post initialization handler */
96 static void __bt_post_oal_init(void)
97 {
98         BT_DBG("OAL initialized, Init profiles..");
99         /*TODO */
100         return;
101 }
102
103 /* OAL initialization handler */
104 static void __bt_handle_oal_initialisation(oal_event_t event)
105 {
106         BT_DBG("");
107
108         switch(event) {
109         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
110                 __bt_post_oal_init();
111                 break;
112         case OAL_EVENT_OAL_INITIALISED_FAILED:
113                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
114                 g_idle_add(_bt_terminate_service, NULL);
115                 break;
116         default:
117                 BT_ERR("Unknown Event");
118                 break;
119         }
120 }