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