[Adapt:OAL]Added event_data size in OAL send_event
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-adapter-mgr.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
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 <stdlib.h>
22 #include <dlog.h>
23 #include <string.h>
24 #include <vconf.h>
25 #include <sys/wait.h>
26
27 #include <bluetooth.h>
28
29 #include "oal-event.h"
30 #include "oal-internal.h"
31 #include "oal-manager.h"
32 #include "oal-hardware.h"
33
34 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
35
36 static const bt_interface_t * blued_api;
37
38 /* Forward declarations */
39 const char * status2string(bt_status_t status);
40 oal_status_t convert_to_oal_status(bt_status_t status);
41 void parse_device_properties(int num_properties, bt_property_t *properties,
42                 remote_device_t *dev_info, ble_adv_data_t * adv_info);
43 static gboolean retry_enable_adapter(gpointer data);
44 oal_status_t oal_mgr_init_internal(void);
45
46
47 /* Callback registered with Stack */
48 static void cb_adapter_state_change(bt_state_t status);
49
50 static bt_callbacks_t callbacks = {
51         sizeof(callbacks),
52         cb_adapter_state_change,
53         NULL, /* adapter_properties_callback */
54         NULL, /* remote_device_properties_callback */
55         NULL, /* device_found_callback */
56         NULL, /* discovery_state_changed_callback */
57         NULL, /* pin_request_callback */
58         NULL, /* ssp_request_callback */
59         NULL, /* bond_state_changed_callback */
60         NULL, /* acl_state_changed_callback */
61         NULL, /* callback_thread_event */
62         NULL, /* dut_mode_recv_callback */
63         NULL, /* le_test_mode_callback*/
64         NULL, /* energy_info_callback */
65 };
66
67 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
68 {
69         int ret;
70         blued_api = stack_if;
71
72         ret = blued_api->init(&callbacks);
73
74         if (ret != BT_STATUS_SUCCESS) {
75                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
76                 blued_api->cleanup();
77                 return convert_to_oal_status(ret);
78         }
79
80         return OAL_STATUS_SUCCESS;
81 }
82
83 const bt_interface_t* adapter_get_stack_interface(void)
84 {
85         return blued_api;
86 }
87
88 void adapter_mgr_cleanup(void)
89 {
90         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
91         BT_DBG();
92 }
93
94 oal_status_t adapter_enable(void)
95 {
96         int ret = BT_STATUS_SUCCESS;
97
98         API_TRACE();
99         CHECK_OAL_INITIALIZED();
100         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
101                 g_timeout_add(200, retry_enable_adapter, NULL);
102                 return OAL_STATUS_PENDING;
103         }
104
105         ret = blued_api->enable();
106
107         if (ret != BT_STATUS_SUCCESS) {
108                 BT_ERR("Enable failed: [%s]", status2string(ret));
109                 return convert_to_oal_status(ret);
110         }
111
112         return OAL_STATUS_SUCCESS;
113 }
114
115 oal_status_t adapter_disable(void)
116 {
117         int ret;
118
119         API_TRACE();
120
121         CHECK_OAL_INITIALIZED();
122
123         ret = blued_api->disable();
124
125         if (ret != BT_STATUS_SUCCESS) {
126                 BT_ERR("Disable failed: [%s]", status2string(ret));
127                 return convert_to_oal_status(ret);
128         }
129         return OAL_STATUS_SUCCESS;
130 }
131
132 /* Callbacks from Stack */
133 static void cb_adapter_state_change(bt_state_t status)
134 {
135         BT_DBG("+");
136         oal_event_t event;
137
138         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
139
140         send_event(event, NULL, 0);
141 }
142
143 static gboolean retry_enable_adapter(gpointer data)
144 {
145         adapter_enable();
146         return FALSE;
147 }