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