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