[Adapt: OAL] Implement get_bonded device info 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 #include "oal-common.h"
34 #include "oal-utils.h"
35
36 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
37
38 static const bt_interface_t * blued_api;
39
40 static bt_address_t local_address;
41 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
42 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
43 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
44 static int discoverable_timeout = 0;
45
46 /* Forward declarations */
47 oal_status_t convert_to_oal_status(bt_status_t status);
48 static gboolean retry_enable_adapter(gpointer data);
49 oal_status_t oal_mgr_init_internal(void);
50
51
52 /* Callback registered with Stack */
53 static void cb_adapter_state_change(bt_state_t status);
54 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
55 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
56 static void cb_adapter_properties (bt_status_t status,
57                 int num_properties, bt_property_t *properties);
58 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
59                 int num_properties, bt_property_t *properties);
60
61 static bt_callbacks_t callbacks = {
62         sizeof(callbacks),
63         cb_adapter_state_change,
64         cb_adapter_properties,
65         cb_device_properties,
66         cb_adapter_device_found,
67         cb_adapter_discovery_state_changed,
68         NULL, /* pin_request_callback */
69         NULL, /* ssp_request_callback */
70         NULL, /* bond_state_changed_callback */
71         NULL, /* acl_state_changed_callback */
72         NULL, /* callback_thread_event */
73         NULL, /* dut_mode_recv_callback */
74         NULL, /* le_test_mode_callback*/
75         NULL, /* energy_info_callback */
76 };
77
78 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
79 {
80         int ret;
81         blued_api = stack_if;
82
83         ret = blued_api->init(&callbacks);
84
85         if (ret != BT_STATUS_SUCCESS) {
86                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
87                 blued_api->cleanup();
88                 return convert_to_oal_status(ret);
89         }
90
91         return OAL_STATUS_SUCCESS;
92 }
93
94 const bt_interface_t* adapter_get_stack_interface(void)
95 {
96         return blued_api;
97 }
98
99 void adapter_mgr_cleanup(void)
100 {
101         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
102         BT_DBG();
103 }
104
105 oal_status_t adapter_enable(void)
106 {
107         int ret = BT_STATUS_SUCCESS;
108
109         API_TRACE();
110         CHECK_OAL_INITIALIZED();
111         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
112                 g_timeout_add(200, retry_enable_adapter, NULL);
113                 return OAL_STATUS_PENDING;
114         }
115
116         ret = blued_api->enable();
117
118         if (ret != BT_STATUS_SUCCESS) {
119                 BT_ERR("Enable failed: [%s]", status2string(ret));
120                 return convert_to_oal_status(ret);
121         }
122
123         return OAL_STATUS_SUCCESS;
124 }
125
126 oal_status_t adapter_disable(void)
127 {
128         int ret;
129
130         API_TRACE();
131
132         CHECK_OAL_INITIALIZED();
133
134         ret = blued_api->disable();
135
136         if (ret != BT_STATUS_SUCCESS) {
137                 BT_ERR("Disable failed: [%s]", status2string(ret));
138                 return convert_to_oal_status(ret);
139         }
140         return OAL_STATUS_SUCCESS;
141 }
142
143 oal_status_t adapter_start_inquiry(void)
144 {
145         int ret;
146
147         API_TRACE();
148
149         CHECK_OAL_INITIALIZED();
150
151         ret = blued_api->start_discovery();
152         if (ret != BT_STATUS_SUCCESS) {
153                 BT_ERR("start_discovery failed: [%s]", status2string(ret));
154                 return convert_to_oal_status(ret);
155         }
156
157         return OAL_STATUS_SUCCESS;
158 }
159
160 oal_status_t adapter_stop_inquiry(void)
161 {
162         int ret;
163
164         API_TRACE();
165
166         CHECK_OAL_INITIALIZED();
167
168         ret = blued_api->cancel_discovery();
169         if (ret != BT_STATUS_SUCCESS) {
170                 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
171                 return convert_to_oal_status(ret);
172         }
173
174         return OAL_STATUS_SUCCESS;
175 }
176
177 /* Callbacks from Stack */
178 static void cb_adapter_state_change(bt_state_t status)
179 {
180         BT_DBG("+");
181         oal_event_t event;
182
183         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
184
185         send_event(event, NULL, 0);
186 }
187
188 static gboolean retry_enable_adapter(gpointer data)
189 {
190         adapter_enable();
191         return FALSE;
192 }
193
194 oal_status_t adapter_get_address(void)
195 {
196         int ret;
197
198         API_TRACE();
199         CHECK_OAL_INITIALIZED();
200
201         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
202         if (ret != BT_STATUS_SUCCESS) {
203                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
204                 return convert_to_oal_status(ret);
205         }
206
207         return OAL_STATUS_SUCCESS;
208 }
209
210 oal_status_t adapter_get_version(void)
211 {
212         int ret;
213
214         API_TRACE();
215         CHECK_OAL_INITIALIZED();
216
217         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
218         if (ret != BT_STATUS_SUCCESS) {
219                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
220                 return convert_to_oal_status(ret);
221         }
222
223         return OAL_STATUS_SUCCESS;
224 }
225
226 oal_status_t adapter_get_name(void)
227 {
228         int ret;
229
230         CHECK_OAL_INITIALIZED();
231
232         API_TRACE();
233
234         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
235         if (ret != BT_STATUS_SUCCESS) {
236                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
237                 return convert_to_oal_status(ret);
238         }
239
240         return OAL_STATUS_SUCCESS;
241 }
242
243 oal_status_t adapter_set_name(char * name)
244 {
245         int ret;
246         bt_property_t prop;
247
248         CHECK_OAL_INITIALIZED();
249
250         OAL_CHECK_PARAMETER(name, return);
251         API_TRACE("Name: %s", name);
252
253         prop.type = BT_PROPERTY_BDNAME;
254         prop.len = strlen(name);
255         prop.val = name;
256
257         ret = blued_api->set_adapter_property(&prop);
258         if (ret != BT_STATUS_SUCCESS) {
259                 BT_ERR("set_adapter_property: [%s]", status2string(ret));
260                 ret = OAL_STATUS_INTERNAL_ERROR;
261         } else
262                 ret = OAL_STATUS_SUCCESS;
263
264         return ret;
265 }
266
267 oal_status_t adapter_is_discoverable(int *p_discoverable)
268 {
269         OAL_CHECK_PARAMETER(p_discoverable, return);
270
271         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
272
273         API_TRACE("%d", *p_discoverable);
274
275         return OAL_STATUS_SUCCESS;
276 }
277
278 oal_status_t adapter_is_connectable(int *p_connectable)
279 {
280         OAL_CHECK_PARAMETER(p_connectable, return);
281
282         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
283                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
284
285         API_TRACE("%d", *p_connectable);
286
287         return OAL_STATUS_SUCCESS;
288 }
289
290 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
291 {
292         API_TRACE("%d", discoverable_timeout);
293
294         *p_timeout = discoverable_timeout;
295
296         return OAL_STATUS_SUCCESS;
297 }
298
299 oal_status_t adapter_get_service_uuids(void)
300 {
301         int ret;
302
303         CHECK_OAL_INITIALIZED();
304
305         API_TRACE();
306
307         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
308         if (ret != BT_STATUS_SUCCESS) {
309                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
310                 return convert_to_oal_status(ret);
311         }
312
313         return OAL_STATUS_SUCCESS;
314 }
315
316 oal_status_t adapter_get_bonded_devices(void)
317 {
318         int ret;
319
320         CHECK_OAL_INITIALIZED();
321
322         API_TRACE();
323
324         ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
325         if (ret != BT_STATUS_SUCCESS) {
326                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
327                 return convert_to_oal_status(ret);
328         }
329
330         return OAL_STATUS_SUCCESS;
331 }
332
333 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
334 {
335         bt_property_t prop;
336         int res;
337
338         BT_DBG("+");
339
340         CHECK_OAL_INITIALIZED();
341
342         prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
343         prop.len = sizeof(bt_scan_mode_t);
344         prop.val = &mode;
345         res = blued_api->set_adapter_property(&prop);
346         if (res != BT_STATUS_SUCCESS) {
347                 BT_ERR("set scan mode failed [%s]", status2string(res));
348                 return convert_to_oal_status(res);
349         }
350
351         BT_DBG("-");
352         return OAL_STATUS_SUCCESS;
353 }
354
355 oal_status_t adapter_set_connectable(int connectable)
356 {
357         bt_scan_mode_t mode;
358
359         API_TRACE("%d", connectable);
360
361         CHECK_OAL_INITIALIZED();
362
363         mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
364
365         return set_scan_mode(mode);
366 }
367
368 static void cb_adapter_properties(bt_status_t status,
369                                                int num_properties,
370                                                bt_property_t *properties)
371 {
372         int i;
373
374         BT_DBG("status: %d, count: %d", status, num_properties);
375
376         if (status != BT_STATUS_SUCCESS) {
377                 if (num_properties == 1) {
378                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
379                                 status2string(status), num_properties, properties[num_properties-1].type);
380                 } else {
381                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
382                 }
383                 return;
384         }
385
386         for (i = 0; i < num_properties; i++) {
387                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
388                 switch (properties[i].type) {
389                 case BT_PROPERTY_VERSION: {
390                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
391                         local_version[properties[i].len] = '\0';
392
393                         BT_DBG("Version: %s", local_version);
394                         /* Send event to application */
395                         if (num_properties == 1) {
396                                 char *adapter_ver = g_strdup(local_version);
397
398                                 /* Application has requested this property SET/GET hence send EVENT */
399                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
400                         }
401                         break;
402                 }
403                 case BT_PROPERTY_BDNAME: {
404                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
405                         local_name[properties[i].len] = '\0';
406
407                         BT_DBG("Name: %s", local_name);
408                         /* Send event to application */
409                         if (num_properties == 1) {
410                                 char * adap_name = g_strdup(local_name);
411
412                                 /* Application has requested this property SET/GET hence send EVENT */
413                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
414                         }
415                         break;
416                 }
417                 case BT_PROPERTY_BDADDR: {
418                         bt_bdaddr_t * addr;
419
420                         addr =  properties[i].val;
421                         memcpy(local_address.addr, addr->address, 6);
422                         if (num_properties == 1) {
423                                 /* Application has requested this property SET/GET hence send EVENT */
424                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
425                                                 g_memdup(&local_address, sizeof(local_address)),
426                                                 sizeof(local_address));
427                         }
428                         break;
429                 }
430                 case BT_PROPERTY_UUIDS: {
431                         int num_uuid;
432
433                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
434
435                         BT_DBG("num_uuid: %d", num_uuid);
436
437                         /* Send event to application */
438                         if (num_properties == 1) {
439                                 event_adapter_services_t *uuids_event;
440
441                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
442                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
443                                 uuids_event->num = num_uuid;
444
445                                 /* Application has requested this property SET/GET hence send EVENT */
446                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
447                                                 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
448                         }
449                         break;
450                 }
451                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
452                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
453
454                         BT_INFO("Scan mode (%d)", cur_mode);
455
456                         scan_mode = cur_mode;
457
458                         /* Send event to application */
459                         if (num_properties == 1) {
460                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
461
462                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
463                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
464                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
465                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
466
467                                 /* Application has requested this property SET/GET hence send EVENT */
468                                 send_event(event, NULL, 0);
469                         }
470                         break;
471                 }
472                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
473                         int timeout;
474
475                         timeout = *((uint32_t*)properties[i].val);
476
477                         BT_INFO("Discoverability timeout: %d", timeout);
478                         discoverable_timeout = timeout;
479
480                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
481                                         g_memdup(properties[i].val, sizeof(uint32_t)),
482                                         sizeof(uint32_t));
483                         break;
484                 }
485                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
486                         int j;
487                         int num_bonded;
488                         bt_bdaddr_t *bonded_addr_list;
489                         event_device_list_t *event_data;
490
491                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
492                         BT_DBG("num_bonded %d", num_bonded);
493
494                         if (num_properties > 1) /* No explicit req for this prop, ignore */
495                                 break;
496
497                         bonded_addr_list = properties[i].val;
498                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
499                         event_data->num = num_bonded;
500
501                         for (j = 0; j < num_bonded; j++)
502                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
503
504                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
505                                         event_data, (num_bonded * sizeof(bt_bdaddr_t)));
506                         break;
507                 }
508                 default:
509                          BT_WARN("Unhandled property: %d", properties[i].type);
510                          break;
511                 }
512         }
513 }
514
515 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
516 {
517         oal_event_t event;
518
519         event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
520
521         BT_DBG("%d", state);
522         send_event(event, NULL, 0);
523 }
524
525 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
526 {
527         remote_device_t dev_info;
528         ble_adv_data_t adv_info;
529         oal_event_t event;
530         gpointer event_data;
531         gsize size = 0;
532         BT_DBG("+");
533
534         if (num_properties == 0) {
535                 BT_ERR("Unexpected, properties count is zero!!");
536                 return;
537         }
538
539         memset(&dev_info, 0x00, sizeof(remote_device_t));
540         memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
541
542         print_bt_properties(num_properties, properties);
543         parse_device_properties(num_properties, properties, &dev_info, &adv_info);
544
545         BT_INFO("number of properties= [%d] ", num_properties, size);
546
547         if (dev_info.type != DEV_TYPE_BREDR) {
548                 /* BLE Single or DUAL mode found, so it should have Adv data */
549                 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
550
551                 ble_dev_event->adv_len = adv_info.len;
552
553                 if (adv_info.len > 0 && adv_info.adv_data) {
554                         memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
555                         ble_dev_event->adv_len = adv_info.len;
556                 } else
557                         ble_dev_event->adv_len = 0;
558
559                 ble_dev_event->device_info = dev_info;
560
561                 event_data = ble_dev_event;
562                 size = sizeof(event_ble_dev_found_t);
563                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
564         } else {
565                 /* BREDR device, so No Adv data */
566                 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
567
568                 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
569                 event_data = dev_event;
570                 size = sizeof(remote_device_t);
571                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
572         }
573
574         send_event(event, event_data, size);
575
576         BT_DBG("-");
577 }