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