Fix compilation issues while disabling HAL compilation
[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 #include "oal-gatt.h"
36
37 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
38
39 static const bt_interface_t * blued_api;
40
41 static bt_address_t local_address;
42 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
43 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
44 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
45 static int discoverable_timeout = 0;
46
47 /* Forward declarations */
48 oal_status_t convert_to_oal_status(bt_status_t status);
49 static gboolean retry_enable_adapter(gpointer data);
50 #ifdef TIZEN_BT_HAL
51 static gboolean retry_enable_le(gpointer data);
52 #endif
53 oal_status_t oal_mgr_init_internal(void);
54
55
56 /* Callback registered with Stack */
57 static void cb_adapter_state_change(bt_state_t status);
58 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
59 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
60 static void cb_adapter_properties (bt_status_t status,
61                 int num_properties, bt_property_t *properties);
62 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
63                 int num_properties, bt_property_t *properties);
64 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
65                                         bt_bond_state_t state);
66 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
67                                             bt_acl_state_t state);
68 extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
69 extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
70                         bt_ssp_variant_t pairing_variant, uint32_t pass_key);
71 extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
72 extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
73 #ifdef TIZEN_BT_HAL
74 extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid);
75 static void cb_ble_state_change(bt_state_t status);
76 #endif
77
78 static bt_callbacks_t callbacks = {
79         sizeof(callbacks),
80         cb_adapter_state_change,
81         cb_adapter_properties,
82         cb_device_properties,
83         cb_adapter_device_found,
84         cb_adapter_discovery_state_changed,
85         cb_device_pin_request,
86         cb_device_ssp_request,
87         cb_device_bond_state_changed,
88         cb_device_acl_state_changed,
89         NULL, /* callback_thread_event */
90         NULL, /* dut_mode_recv_callback */
91         NULL, /* le_test_mode_callback*/
92         NULL, /* energy_info_callback */
93         cb_device_authorize_request,
94         cb_device_trust_state_changed,
95 #ifdef TIZEN_BT_HAL
96         cb_socket_conn_authorize_request,
97         cb_ble_state_change,
98 #endif
99 };
100
101 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
102 {
103         int ret;
104         blued_api = stack_if;
105
106         ret = blued_api->init(&callbacks);
107
108         if (ret != BT_STATUS_SUCCESS) {
109                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
110                 blued_api->cleanup();
111                 return convert_to_oal_status(ret);
112         }
113
114         return OAL_STATUS_SUCCESS;
115 }
116
117 const bt_interface_t* adapter_get_stack_interface(void)
118 {
119         return blued_api;
120 }
121
122 void adapter_mgr_cleanup(void)
123 {
124         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
125         BT_DBG();
126 }
127
128 oal_status_t adapter_enable(void)
129 {
130         int ret = BT_STATUS_SUCCESS;
131
132         API_TRACE();
133         CHECK_OAL_INITIALIZED();
134         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
135                 g_timeout_add(200, retry_enable_adapter, NULL);
136                 return OAL_STATUS_PENDING;
137         }
138
139         ret = blued_api->enable();
140
141         if (ret != BT_STATUS_SUCCESS) {
142                 BT_ERR("Enable failed: [%s]", status2string(ret));
143                 return convert_to_oal_status(ret);
144         }
145
146         return OAL_STATUS_SUCCESS;
147 }
148
149 oal_status_t adapter_disable(void)
150 {
151         int ret;
152
153         API_TRACE();
154
155         CHECK_OAL_INITIALIZED();
156
157         ret = blued_api->disable();
158
159         if (ret != BT_STATUS_SUCCESS) {
160                 BT_ERR("Disable failed: [%s]", status2string(ret));
161                 return convert_to_oal_status(ret);
162         }
163         return OAL_STATUS_SUCCESS;
164 }
165
166 oal_status_t le_enable(void)
167 {
168         int ret = BT_STATUS_SUCCESS;
169
170         API_TRACE();
171         CHECK_OAL_INITIALIZED();
172
173 #ifdef TIZEN_BT_HAL
174         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
175                 g_timeout_add(200, retry_enable_le, NULL);
176                 return OAL_STATUS_PENDING;
177         }
178
179         ret = blued_api->le_enable();
180
181         if (ret != BT_STATUS_SUCCESS) {
182                 BT_ERR("Enable failed: [%s]", status2string(ret));
183                 return convert_to_oal_status(ret);
184         }
185 #else
186         BT_INFO("Not Supported");
187         ret = OAL_STATUS_NOT_SUPPORT;
188 #endif
189
190         return ret;
191 }
192
193 oal_status_t le_disable(void)
194 {
195         int ret;
196
197         API_TRACE();
198
199         CHECK_OAL_INITIALIZED();
200
201 #ifdef TIZEN_BT_HAL
202         ret = blued_api->le_disable();
203
204         if (ret != BT_STATUS_SUCCESS) {
205                 BT_ERR("Disable failed: [%s]", status2string(ret));
206                 return convert_to_oal_status(ret);
207         }
208 #else
209         BT_INFO("Not Supported");
210         ret = OAL_STATUS_NOT_SUPPORT;
211 #endif
212         return ret;
213 }
214
215 oal_status_t adapter_start_custom_inquiry(discovery_type_t disc_type)
216 {
217         int ret;
218
219         API_TRACE();
220
221         CHECK_OAL_INITIALIZED();
222         BT_INFO("Custom Discovery Type [0x%x]", disc_type);
223
224 #ifdef TIZEN_BT_HAL
225         ret = blued_api->start_custom_discovery(disc_type);
226         if (ret != BT_STATUS_SUCCESS) {
227                 BT_ERR("start_custom_discovery failed: [%s]", status2string(ret));
228                 return convert_to_oal_status(ret);
229         }
230 #else
231         BT_INFO("Not Supported");
232         ret = OAL_STATUS_NOT_SUPPORT;
233 #endif
234         return ret;
235 }
236
237 oal_status_t adapter_start_inquiry(unsigned short duration)
238 {
239         int ret;
240
241         API_TRACE();
242
243         CHECK_OAL_INITIALIZED();
244
245         ret = blued_api->start_discovery();
246         if (ret != BT_STATUS_SUCCESS) {
247                 BT_ERR("start_discovery failed: [%s]", status2string(ret));
248                 return convert_to_oal_status(ret);
249         }
250
251         return OAL_STATUS_SUCCESS;
252 }
253
254 oal_status_t adapter_stop_inquiry(void)
255 {
256         int ret;
257
258         API_TRACE();
259
260         CHECK_OAL_INITIALIZED();
261
262         ret = blued_api->cancel_discovery();
263         if (ret != BT_STATUS_SUCCESS) {
264                 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
265                 return convert_to_oal_status(ret);
266         }
267
268         return OAL_STATUS_SUCCESS;
269 }
270
271 /* Callbacks from Stack */
272 static void cb_adapter_state_change(bt_state_t status)
273 {
274         BT_DBG("+");
275         oal_event_t event;
276
277         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
278
279         send_event(event, NULL, 0);
280 }
281
282 #ifdef TIZEN_BT_HAL
283 /* Callbacks from Stack */
284 static void cb_ble_state_change(bt_state_t status)
285 {
286         BT_DBG("+");
287         oal_event_t event;
288
289         event = (BT_STATE_ON == status)?OAL_EVENT_BLE_ENABLED:OAL_EVENT_BLE_DISABLED;
290
291         send_event(event, NULL, 0);
292 }
293 #endif
294
295 static gboolean retry_enable_adapter(gpointer data)
296 {
297         adapter_enable();
298         return FALSE;
299 }
300
301 #ifdef TIZEN_BT_HAL
302 static gboolean retry_enable_le(gpointer data)
303 {
304         le_enable();
305         return FALSE;
306 }
307 #endif
308 oal_status_t adapter_get_properties(void)
309 {
310         int ret;
311
312         API_TRACE();
313         CHECK_OAL_INITIALIZED();
314
315         ret = blued_api->get_adapter_properties();
316         if (ret != BT_STATUS_SUCCESS) {
317                 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
318                 return convert_to_oal_status(ret);
319         }
320
321         return OAL_STATUS_SUCCESS;
322 }
323
324 oal_status_t adapter_get_address(void)
325 {
326         int ret;
327
328         API_TRACE();
329         CHECK_OAL_INITIALIZED();
330
331         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
332         if (ret != BT_STATUS_SUCCESS) {
333                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
334                 return convert_to_oal_status(ret);
335         }
336
337         return OAL_STATUS_SUCCESS;
338 }
339
340 oal_status_t adapter_get_version(void)
341 {
342         int ret;
343
344         API_TRACE();
345         CHECK_OAL_INITIALIZED();
346
347         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
348         if (ret != BT_STATUS_SUCCESS) {
349                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
350                 return convert_to_oal_status(ret);
351         }
352
353         return OAL_STATUS_SUCCESS;
354 }
355
356 oal_status_t adapter_get_name(void)
357 {
358         int ret;
359
360         CHECK_OAL_INITIALIZED();
361
362         API_TRACE();
363
364         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
365         if (ret != BT_STATUS_SUCCESS) {
366                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
367                 return convert_to_oal_status(ret);
368         }
369
370         return OAL_STATUS_SUCCESS;
371 }
372
373 oal_status_t adapter_set_name(char * name)
374 {
375         int ret;
376         bt_property_t prop;
377
378         CHECK_OAL_INITIALIZED();
379
380         OAL_CHECK_PARAMETER(name, return);
381         API_TRACE("Name: %s", name);
382
383         prop.type = BT_PROPERTY_BDNAME;
384         prop.len = strlen(name);
385         prop.val = name;
386
387         ret = blued_api->set_adapter_property(&prop);
388         if (ret != BT_STATUS_SUCCESS) {
389                 BT_ERR("set_adapter_property: [%s]", status2string(ret));
390                 ret = OAL_STATUS_INTERNAL_ERROR;
391         } else
392                 ret = OAL_STATUS_SUCCESS;
393
394         return ret;
395 }
396
397 oal_status_t adapter_is_discoverable(int *p_discoverable)
398 {
399         OAL_CHECK_PARAMETER(p_discoverable, return);
400
401         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
402
403         API_TRACE("%d", *p_discoverable);
404
405         return OAL_STATUS_SUCCESS;
406 }
407
408 oal_status_t adapter_is_connectable(int *p_connectable)
409 {
410         OAL_CHECK_PARAMETER(p_connectable, return);
411
412         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
413                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
414
415         API_TRACE("%d", *p_connectable);
416
417         return OAL_STATUS_SUCCESS;
418 }
419
420 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
421 {
422         API_TRACE("%d", discoverable_timeout);
423
424         *p_timeout = discoverable_timeout;
425
426         return OAL_STATUS_SUCCESS;
427 }
428
429 oal_status_t adapter_get_service_uuids(void)
430 {
431         int ret;
432
433         CHECK_OAL_INITIALIZED();
434
435         API_TRACE();
436
437         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
438         if (ret != BT_STATUS_SUCCESS) {
439                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
440                 return convert_to_oal_status(ret);
441         }
442
443         return OAL_STATUS_SUCCESS;
444 }
445
446 oal_status_t adapter_get_bonded_devices(void)
447 {
448         int ret;
449
450         CHECK_OAL_INITIALIZED();
451
452         API_TRACE();
453
454         ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
455         if (ret != BT_STATUS_SUCCESS) {
456                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
457                 return convert_to_oal_status(ret);
458         }
459
460         return OAL_STATUS_SUCCESS;
461 }
462
463 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
464 {
465         bt_property_t prop;
466         int res;
467
468         BT_DBG("+");
469
470         CHECK_OAL_INITIALIZED();
471
472         prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
473         prop.len = sizeof(bt_scan_mode_t);
474         prop.val = &mode;
475         res = blued_api->set_adapter_property(&prop);
476         if (res != BT_STATUS_SUCCESS) {
477                 BT_ERR("set scan mode failed [%s]", status2string(res));
478                 return convert_to_oal_status(res);
479         }
480
481         BT_DBG("-");
482         return OAL_STATUS_SUCCESS;
483 }
484
485 oal_status_t adapter_set_connectable(int connectable)
486 {
487         bt_scan_mode_t mode;
488
489         API_TRACE("%d", connectable);
490
491         CHECK_OAL_INITIALIZED();
492
493         mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
494
495         return set_scan_mode(mode);
496 }
497
498 oal_status_t adapter_set_discoverable(void)
499 {
500         CHECK_OAL_INITIALIZED();
501         API_TRACE();
502
503         return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
504 }
505
506 oal_status_t adapter_set_discoverable_timeout(int timeout)
507 {
508         bt_property_t prop;
509         int res;
510         uint32_t prop_val = timeout;
511
512         CHECK_OAL_INITIALIZED();
513         API_TRACE("%d", timeout);
514
515         prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
516         prop.len = sizeof(prop_val);
517         prop.val = &prop_val;
518         res = blued_api->set_adapter_property(&prop);
519         if (res != BT_STATUS_SUCCESS) {
520                 BT_ERR("set_adapter_property failed [%s]", status2string(res));
521                 return convert_to_oal_status(res);
522         }
523         return OAL_STATUS_SUCCESS;
524 }
525
526 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
527                         int adv_type, int chnl_map, int tx_power, int timeout_s)
528 {
529         int res;
530         CHECK_OAL_INITIALIZED();
531         API_TRACE();
532
533         res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
534                         adv_type, chnl_map, tx_power, timeout_s);
535         if (res != OAL_STATUS_SUCCESS) {
536                 BT_ERR("gatts_multi_adv_update: [%d]", res);
537                 return res;
538         }
539         return OAL_STATUS_SUCCESS;
540 }
541
542 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
543                         oal_ble_multi_adv_param_setup_t * adv_param_setup)
544 {
545         int res;
546         CHECK_OAL_INITIALIZED();
547         OAL_CHECK_PARAMETER(adv_param_setup, return);
548
549         API_TRACE();
550
551         res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
552         if (res != OAL_STATUS_SUCCESS) {
553                 BT_ERR("failed: [%d]", res);
554                 return res;
555         }
556         return OAL_STATUS_SUCCESS;
557 }
558
559 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
560 {
561         int res;
562         CHECK_OAL_INITIALIZED();
563         API_TRACE();
564
565         res = gatts_multi_adv_enable(instance_id);
566         if (res != OAL_STATUS_SUCCESS) {
567                 BT_ERR("failed: [%d]", res);
568                 return res;
569         }
570
571         return OAL_STATUS_SUCCESS;
572 }
573
574 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
575 {
576         int res;
577         CHECK_OAL_INITIALIZED();
578         API_TRACE();
579
580         res = gatts_multi_adv_disable(instance_id);
581         if (res != OAL_STATUS_SUCCESS) {
582                 BT_ERR("failed: [%d]", res);
583                 return res;
584         }
585
586         return OAL_STATUS_SUCCESS;
587 }
588
589 static void cb_adapter_properties(bt_status_t status,
590                                                int num_properties,
591                                                bt_property_t *properties)
592 {
593         int i;
594
595         BT_DBG("status: %d, count: %d", status, num_properties);
596
597         if (status != BT_STATUS_SUCCESS) {
598                 if (num_properties == 1) {
599                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
600                                 status2string(status), num_properties, properties[num_properties-1].type);
601                 } else {
602                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
603                 }
604                 return;
605         }
606
607         for (i = 0; i < num_properties; i++) {
608                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
609                 switch (properties[i].type) {
610                 case BT_PROPERTY_VERSION: {
611                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
612                         local_version[properties[i].len] = '\0';
613
614                         BT_DBG("Version: %s", local_version);
615                         /* Send event to application */
616                         if (num_properties == 1) {
617                                 char *adapter_ver = g_strdup(local_version);
618
619                                 /* Application has requested this property SET/GET hence send EVENT */
620                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
621                         }
622                         break;
623                 }
624                 case BT_PROPERTY_BDNAME: {
625                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
626                         local_name[properties[i].len] = '\0';
627
628                         BT_DBG("Name: %s", local_name);
629                         /* Send event to application */
630                         if (num_properties == 1) {
631                                 char * adap_name = g_strdup(local_name);
632
633                                 /* Application has requested this property SET/GET hence send EVENT */
634                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
635                         }
636                         break;
637                 }
638                 case BT_PROPERTY_BDADDR: {
639                         bt_bdaddr_t * addr;
640
641                         addr =  properties[i].val;
642                         memcpy(local_address.addr, addr->address, 6);
643                         if (num_properties == 1) {
644                                 /* Application has requested this property SET/GET hence send EVENT */
645                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
646                                                 g_memdup(&local_address, sizeof(local_address)),
647                                                 sizeof(local_address));
648                         }
649                         break;
650                 }
651                 case BT_PROPERTY_UUIDS: {
652                         int num_uuid;
653
654                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
655
656                         BT_DBG("num_uuid: %d", num_uuid);
657
658                         /* Send event to application */
659                         if (num_properties == 1) {
660                                 event_adapter_services_t *uuids_event;
661
662                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
663                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
664                                 uuids_event->num = num_uuid;
665
666                                 /* Application has requested this property SET/GET hence send EVENT */
667                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
668                                                 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
669                         }
670                         break;
671                 }
672                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
673                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
674
675                         BT_INFO("Scan mode (%d)", cur_mode);
676
677                         scan_mode = cur_mode;
678
679                         /* Send event to application */
680                         if (num_properties == 1) {
681                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
682
683                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
684                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
685                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
686                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
687
688                                 /* Application has requested this property SET/GET hence send EVENT */
689                                 send_event(event, NULL, 0);
690                         }
691                         break;
692                 }
693                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
694                         int timeout;
695
696                         timeout = *((uint32_t*)properties[i].val);
697
698                         BT_INFO("Discoverability timeout: %d", timeout);
699                         discoverable_timeout = timeout;
700
701                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
702                                         g_memdup(properties[i].val, sizeof(uint32_t)),
703                                         sizeof(uint32_t));
704                         break;
705                 }
706                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
707                         int j;
708                         int num_bonded;
709                         bt_bdaddr_t *bonded_addr_list;
710                         event_device_list_t *event_data;
711
712                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
713                         BT_DBG("num_bonded %d", num_bonded);
714
715                         if (num_properties > 1) /* No explicit req for this prop, ignore */
716                                 break;
717
718                         bonded_addr_list = properties[i].val;
719                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
720                         event_data->num = num_bonded;
721
722                         for (j = 0; j < num_bonded; j++)
723                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
724
725                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
726                                         event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
727                         break;
728                 }
729                 default:
730                          BT_WARN("Unhandled property: %d", properties[i].type);
731                          break;
732                 }
733         }
734 }
735
736 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
737 {
738         oal_event_t event;
739
740         event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
741
742         BT_DBG("%d", state);
743         send_event(event, NULL, 0);
744 }
745
746 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
747 {
748         remote_device_t dev_info;
749         ble_adv_data_t adv_info;
750         oal_event_t event;
751         gpointer event_data;
752         gsize size = 0;
753         BT_DBG("+");
754
755         if (num_properties == 0) {
756                 BT_ERR("Unexpected, properties count is zero!!");
757                 return;
758         }
759
760         memset(&dev_info, 0x00, sizeof(remote_device_t));
761         memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
762
763         print_bt_properties(num_properties, properties);
764         parse_device_properties(num_properties, properties, &dev_info, &adv_info);
765
766         BT_INFO("number of properties= [%d] ", num_properties, size);
767
768         if (dev_info.type != DEV_TYPE_BREDR) {
769                 /* BLE Single or DUAL mode found, so it should have Adv data */
770                 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
771
772                 ble_dev_event->adv_len = adv_info.len;
773
774                 if (adv_info.len > 0 && adv_info.adv_data) {
775                         memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
776                         ble_dev_event->adv_len = adv_info.len;
777                 } else
778                         ble_dev_event->adv_len = 0;
779
780                 ble_dev_event->device_info = dev_info;
781
782                 event_data = ble_dev_event;
783                 size = sizeof(event_ble_dev_found_t);
784                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
785         } else {
786                 /* BREDR device, so No Adv data */
787                 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
788
789                 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
790                 event_data = dev_event;
791                 size = sizeof(remote_device_t);
792                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
793         }
794
795         send_event(event, event_data, size);
796
797         BT_DBG("-");
798 }