[Adapt]Added support for get adapter property APIs
[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 static bt_address_t local_address;
39 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
40 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
41 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
42 static int discoverable_timeout = 0;
43
44 /* Forward declarations */
45 const char * status2string(bt_status_t status);
46 oal_status_t convert_to_oal_status(bt_status_t status);
47 void parse_device_properties(int num_properties, bt_property_t *properties,
48                 remote_device_t *dev_info, ble_adv_data_t * adv_info);
49 static gboolean retry_enable_adapter(gpointer data);
50 oal_status_t oal_mgr_init_internal(void);
51
52
53 /* Callback registered with Stack */
54 static void cb_adapter_state_change(bt_state_t status);
55 static void cb_adapter_properties (bt_status_t status,
56                 int num_properties, bt_property_t *properties);
57
58 static bt_callbacks_t callbacks = {
59         sizeof(callbacks),
60         cb_adapter_state_change,
61         cb_adapter_properties,
62         NULL, /* remote_device_properties_callback */
63         NULL, /* device_found_callback */
64         NULL, /* discovery_state_changed_callback */
65         NULL, /* pin_request_callback */
66         NULL, /* ssp_request_callback */
67         NULL, /* bond_state_changed_callback */
68         NULL, /* acl_state_changed_callback */
69         NULL, /* callback_thread_event */
70         NULL, /* dut_mode_recv_callback */
71         NULL, /* le_test_mode_callback*/
72         NULL, /* energy_info_callback */
73 };
74
75 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
76 {
77         int ret;
78         blued_api = stack_if;
79
80         ret = blued_api->init(&callbacks);
81
82         if (ret != BT_STATUS_SUCCESS) {
83                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
84                 blued_api->cleanup();
85                 return convert_to_oal_status(ret);
86         }
87
88         return OAL_STATUS_SUCCESS;
89 }
90
91 const bt_interface_t* adapter_get_stack_interface(void)
92 {
93         return blued_api;
94 }
95
96 void adapter_mgr_cleanup(void)
97 {
98         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
99         BT_DBG();
100 }
101
102 oal_status_t adapter_enable(void)
103 {
104         int ret = BT_STATUS_SUCCESS;
105
106         API_TRACE();
107         CHECK_OAL_INITIALIZED();
108         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
109                 g_timeout_add(200, retry_enable_adapter, NULL);
110                 return OAL_STATUS_PENDING;
111         }
112
113         ret = blued_api->enable();
114
115         if (ret != BT_STATUS_SUCCESS) {
116                 BT_ERR("Enable failed: [%s]", status2string(ret));
117                 return convert_to_oal_status(ret);
118         }
119
120         return OAL_STATUS_SUCCESS;
121 }
122
123 oal_status_t adapter_disable(void)
124 {
125         int ret;
126
127         API_TRACE();
128
129         CHECK_OAL_INITIALIZED();
130
131         ret = blued_api->disable();
132
133         if (ret != BT_STATUS_SUCCESS) {
134                 BT_ERR("Disable failed: [%s]", status2string(ret));
135                 return convert_to_oal_status(ret);
136         }
137         return OAL_STATUS_SUCCESS;
138 }
139
140 /* Callbacks from Stack */
141 static void cb_adapter_state_change(bt_state_t status)
142 {
143         BT_DBG("+");
144         oal_event_t event;
145
146         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
147
148         send_event(event, NULL, 0);
149 }
150
151 static gboolean retry_enable_adapter(gpointer data)
152 {
153         adapter_enable();
154         return FALSE;
155 }
156
157 oal_status_t adapter_get_address(void)
158 {
159         int ret;
160
161         CHECK_OAL_INITIALIZED();
162
163         API_TRACE();
164
165         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
166         if (ret != BT_STATUS_SUCCESS) {
167                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
168                 return convert_to_oal_status(ret);
169         }
170
171         return OAL_STATUS_SUCCESS;
172 }
173
174 oal_status_t adapter_get_version(void)
175 {
176         int ret;
177
178         CHECK_OAL_INITIALIZED();
179
180         API_TRACE();
181
182         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
183         if (ret != BT_STATUS_SUCCESS) {
184                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
185                 return convert_to_oal_status(ret);
186         }
187
188         return OAL_STATUS_SUCCESS;
189 }
190
191 oal_status_t adapter_get_name(void)
192 {
193         int ret;
194
195         CHECK_OAL_INITIALIZED();
196
197         API_TRACE();
198
199         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
200         if (ret != BT_STATUS_SUCCESS) {
201                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
202                 return convert_to_oal_status(ret);
203         }
204
205         return OAL_STATUS_SUCCESS;
206 }
207
208 oal_status_t adapter_is_discoverable(int *p_discoverable)
209 {
210         OAL_CHECK_PARAMETER(p_discoverable, return);
211
212         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
213
214         API_TRACE("%d", *p_discoverable);
215
216         return OAL_STATUS_SUCCESS;
217 }
218
219 oal_status_t adapter_is_connectable(int *p_connectable)
220 {
221         OAL_CHECK_PARAMETER(p_connectable, return);
222
223         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
224                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
225
226         API_TRACE("%d", *p_connectable);
227
228         return OAL_STATUS_SUCCESS;
229 }
230
231 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
232 {
233         API_TRACE("%d", discoverable_timeout);
234
235         *p_timeout = discoverable_timeout;
236
237         return OAL_STATUS_SUCCESS;
238 }
239
240 static void cb_adapter_properties (bt_status_t status,
241                                                int num_properties,
242                                                bt_property_t *properties)
243 {
244         int i;
245
246         BT_DBG("status: %d, count: %d", status, num_properties);
247
248         if (status != BT_STATUS_SUCCESS) {
249                 if (num_properties == 1) {
250                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
251                                 status2string(status), num_properties, properties[num_properties-1].type);
252                 } else {
253                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
254                 }
255                 return;
256         }
257
258         for (i = 0; i < num_properties; i++) {
259                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
260                 switch (properties[i].type) {
261                 case BT_PROPERTY_VERSION: {
262                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
263                         local_version[properties[i].len] = '\0';
264
265                         BT_DBG("Version: %s", local_version);
266                         /* Send event to application */
267                         if (num_properties == 1) {
268                                 char *adapter_ver = g_strdup(local_version);
269
270                                 /* Application has requested this property SET/GET hence send EVENT */
271                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
272                         }
273                         break;
274                 }
275                 case BT_PROPERTY_BDNAME: {
276                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
277                         local_name[properties[i].len] = '\0';
278
279                         BT_DBG("Name: %s", local_name);
280                         /* Send event to application */
281                         if (num_properties == 1) {
282                                 char * adap_name = g_strdup(local_name);
283
284                                 /* Application has requested this property SET/GET hence send EVENT */
285                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
286                         }
287                         break;
288                 }
289                 case BT_PROPERTY_BDADDR: {
290                         bt_bdaddr_t * addr;
291
292                         addr =  properties[i].val;
293                         memcpy(local_address.addr, addr->address, 6);
294                         if (num_properties == 1) {
295                                 /* Application has requested this property SET/GET hence send EVENT */
296                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
297                                                 g_memdup(&local_address, sizeof(local_address)),
298                                                 sizeof(local_address));
299                         }
300                         break;
301                 }
302                 case BT_PROPERTY_UUIDS: {
303                         int num_uuid;
304
305                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
306
307                         BT_DBG("num_uuid: %d", num_uuid);
308
309                         /* Send event to application */
310                         if (num_properties == 1) {
311                                 event_adapter_services_t *uuids_event;
312
313                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
314                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
315                                 uuids_event->num = num_uuid;
316
317                                 /* Application has requested this property SET/GET hence send EVENT */
318                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
319                                                 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
320                         }
321                         break;
322                 }
323                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
324                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
325
326                         BT_INFO("Scan mode (%d)", cur_mode);
327
328                         scan_mode = cur_mode;
329
330                         /* Send event to application */
331                         if (num_properties == 1) {
332                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
333
334                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
335                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
336                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
337                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
338
339                                 /* Application has requested this property SET/GET hence send EVENT */
340                                 send_event(event, NULL, 0);
341                         }
342                         break;
343                 }
344                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
345                         int timeout;
346
347                         timeout = *((uint32_t*)properties[i].val);
348
349                         BT_INFO("Discoverability timeout: %d", timeout);
350                         discoverable_timeout = timeout;
351
352                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
353                                         g_memdup(properties[i].val, sizeof(uint32_t)),
354                                         sizeof(uint32_t));
355                         break;
356                 }
357                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
358                         int j;
359                         int num_bonded;
360                         bt_bdaddr_t *bonded_addr_list;
361                         event_device_list_t *event_data;
362
363                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
364                         BT_DBG("num_bonded %d", num_bonded);
365
366                         if (num_properties > 1) /* No explicit req for this prop, ignore */
367                                 break;
368
369                         bonded_addr_list = properties[i].val;
370                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
371                         event_data->num = num_bonded;
372
373                         for (j = 0; j < num_bonded; j++)
374                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
375
376                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
377                                         event_data, (num_bonded * sizeof(bt_bdaddr_t)));
378                         break;
379                 }
380                 default:
381                         BT_WARN("Unhandled property: %d", properties[i].type);
382                         break;
383                 }
384         }
385 }