Handle DBFW plus info events
[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_set_name(char * name)
534 {
535         int ret;
536         bt_property_t prop;
537
538         CHECK_OAL_INITIALIZED();
539
540         OAL_CHECK_PARAMETER(name, return);
541         API_TRACE("Name: %s", name);
542
543         prop.type = BT_PROPERTY_BDNAME;
544         prop.len = strlen(name);
545         prop.val = name;
546
547         ret = blued_api->set_adapter_property(&prop);
548         if (ret != BT_STATUS_SUCCESS) {
549                 BT_ERR("set_adapter_property: [%s]", status2string(ret));
550                 ret = OAL_STATUS_INTERNAL_ERROR;
551         } else
552                 ret = OAL_STATUS_SUCCESS;
553
554         return ret;
555 }
556
557 oal_status_t adapter_is_discoverable(int *p_discoverable)
558 {
559         OAL_CHECK_PARAMETER(p_discoverable, return);
560
561         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
562
563         API_TRACE("%d", *p_discoverable);
564
565         return OAL_STATUS_SUCCESS;
566 }
567
568 oal_status_t adapter_is_connectable(int *p_connectable)
569 {
570         OAL_CHECK_PARAMETER(p_connectable, return);
571
572         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
573                 || (scan_mode == BT_SCAN_MODE_CONNECTABLE);
574
575         API_TRACE("%d", *p_connectable);
576
577         return OAL_STATUS_SUCCESS;
578 }
579
580 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
581 {
582         API_TRACE("%d", discoverable_timeout);
583
584         *p_timeout = discoverable_timeout;
585
586         return OAL_STATUS_SUCCESS;
587 }
588
589 oal_status_t adapter_get_service_uuids(void)
590 {
591         int ret;
592
593         CHECK_OAL_INITIALIZED();
594
595         API_TRACE();
596         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
597         if (ret != BT_STATUS_SUCCESS) {
598                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
599                 return convert_to_oal_status(ret);
600         }
601         return OAL_STATUS_SUCCESS;
602 }
603
604 oal_status_t adapter_get_bonded_devices(void)
605 {
606         int ret;
607
608         CHECK_OAL_INITIALIZED();
609
610         API_TRACE();
611
612         ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
613         if (ret != BT_STATUS_SUCCESS) {
614                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
615                 return convert_to_oal_status(ret);
616         }
617
618         return OAL_STATUS_SUCCESS;
619 }
620
621 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
622 {
623         bt_property_t prop;
624         int res;
625
626         BT_DBG("+");
627
628         CHECK_OAL_INITIALIZED();
629
630         prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
631         prop.len = sizeof(bt_scan_mode_t);
632         prop.val = &mode;
633         res = blued_api->set_adapter_property(&prop);
634         if (res != BT_STATUS_SUCCESS) {
635                 BT_ERR("set scan mode failed [%s]", status2string(res));
636                 return convert_to_oal_status(res);
637         }
638
639         BT_DBG("-");
640         return OAL_STATUS_SUCCESS;
641 }
642
643 oal_status_t adapter_set_connectable(int connectable)
644 {
645         bt_scan_mode_t mode;
646
647         API_TRACE("%d", connectable);
648
649         CHECK_OAL_INITIALIZED();
650
651         mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
652
653         return set_scan_mode(mode);
654 }
655
656 oal_status_t adapter_set_discoverable(void)
657 {
658         CHECK_OAL_INITIALIZED();
659         API_TRACE();
660
661         return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
662 }
663
664 oal_status_t adapter_set_discoverable_timeout(int timeout)
665 {
666         bt_property_t prop;
667         int res;
668         uint32_t prop_val = timeout;
669
670         CHECK_OAL_INITIALIZED();
671         API_TRACE("%d", timeout);
672
673         prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
674         prop.len = sizeof(prop_val);
675         prop.val = &prop_val;
676         res = blued_api->set_adapter_property(&prop);
677         if (res != BT_STATUS_SUCCESS) {
678                 BT_ERR("set_adapter_property failed [%s]", status2string(res));
679                 return convert_to_oal_status(res);
680         }
681         return OAL_STATUS_SUCCESS;
682 }
683
684 oal_status_t adapter_ble_set_filter_policy(int filter_policy)
685 {
686         int ret = OAL_STATUS_SUCCESS;
687
688         CHECK_OAL_INITIALIZED();
689         API_TRACE();
690
691         BT_DBG("Filter policy applied is [%d]", filter_policy);
692
693         ret = gatts_set_filter_policy(filter_policy);
694
695         if (ret != OAL_STATUS_SUCCESS) {
696                 BT_ERR("gatts_set_filter_policy: [%d]", ret);
697                 return ret;
698         }
699
700         return OAL_STATUS_SUCCESS;
701 }
702
703 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
704                         int adv_type, int chnl_map, int tx_power, int timeout_s)
705 {
706         int res;
707         CHECK_OAL_INITIALIZED();
708         API_TRACE();
709
710         res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
711                         adv_type, chnl_map, tx_power, timeout_s);
712         if (res != OAL_STATUS_SUCCESS) {
713                 BT_ERR("gatts_multi_adv_update: [%d]", res);
714                 return res;
715         }
716         return OAL_STATUS_SUCCESS;
717 }
718
719 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
720                         oal_ble_multi_adv_param_setup_t * adv_param_setup)
721 {
722         int res;
723         CHECK_OAL_INITIALIZED();
724         OAL_CHECK_PARAMETER(adv_param_setup, return);
725
726         API_TRACE();
727
728         res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
729         if (res != OAL_STATUS_SUCCESS) {
730                 BT_ERR("failed: [%d]", res);
731                 return res;
732         }
733         return OAL_STATUS_SUCCESS;
734 }
735
736 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
737 {
738         int res;
739         CHECK_OAL_INITIALIZED();
740         API_TRACE();
741
742         res = gatts_multi_adv_enable(instance_id);
743         if (res != OAL_STATUS_SUCCESS) {
744                 BT_ERR("failed: [%d]", res);
745                 return res;
746         }
747
748         return OAL_STATUS_SUCCESS;
749 }
750
751 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
752 {
753         int res;
754         CHECK_OAL_INITIALIZED();
755         API_TRACE();
756
757         res = gatts_multi_adv_disable(instance_id);
758         if (res != OAL_STATUS_SUCCESS) {
759                 BT_ERR("failed: [%d]", res);
760                 return res;
761         }
762
763         return OAL_STATUS_SUCCESS;
764 }
765
766 oal_status_t adapter_set_le_static_random_address(int enable)
767 {
768         int ret;
769
770         CHECK_OAL_INITIALIZED();
771         API_TRACE("%d", enable);
772
773 #ifdef TIZEN_BT_HAL
774         ret = blued_api->set_le_static_random_address((enable ? 1 : 0));
775         if (ret != BT_STATUS_SUCCESS)
776                 BT_ERR("Static address set failed: [%s]", status2string(ret));
777         ret = convert_to_oal_status(ret);
778 #else
779         BT_INFO("Not Supported");
780         ret = OAL_STATUS_NOT_SUPPORT;
781 #endif
782
783         return ret;
784 }
785
786 oal_status_t adapter_set_manufacturer_data(oal_manufacturer_data_t *m_data)
787 {
788         int ret;
789
790         CHECK_OAL_INITIALIZED();
791         API_TRACE();
792
793         OAL_CHECK_PARAMETER(m_data, return);
794         ret = blued_api->adapter_le_set_manufacturer_data((bt_manufacturer_data_t*)m_data);
795         if (ret != BT_STATUS_SUCCESS)
796                 BT_ERR("Setting manufacturer data Failed: [%s]",status2string(ret));
797
798         ret = convert_to_oal_status(ret);
799         return ret;
800 }
801
802 oal_status_t adapter_set_white_list(bt_address_t *device_address, int address_type, bool is_add)
803 {
804         int ret;
805         bdstr_t bdstr;
806
807         CHECK_OAL_INITIALIZED();
808         API_TRACE();
809
810         BT_INFO("BT remote device Address: %s", bdt_bd2str(device_address, &bdstr));
811
812         ret = blued_api->adapter_le_set_white_list((bt_bdaddr_t*)device_address, address_type, is_add);
813         if (ret != BT_STATUS_SUCCESS) {
814                 if(is_add)
815                         BT_ERR("Add to White List Failed: [%s]",status2string(ret));
816                 else
817                         BT_ERR("Remove from White List Failed: [%s]",status2string(ret));
818         }
819         ret = convert_to_oal_status(ret);
820         return ret;
821 }
822
823 oal_status_t adapter_ble_set_privacy(int set_privacy)
824 {
825         int res;
826
827         CHECK_OAL_INITIALIZED();
828         API_TRACE();
829
830         res = blued_api->adapter_le_set_privacy(set_privacy);
831         if (res != BT_STATUS_SUCCESS)
832                 BT_ERR("Setting LE Privacy Failed: [%s]", status2string(res));
833         res = convert_to_oal_status(res);
834
835         return res;
836 }
837
838 static void cb_adapter_properties(bt_status_t status,
839                 int num_properties,
840                 bt_property_t *properties)
841 {
842         int i;
843
844         BT_DBG("status: %d, count: %d", status, num_properties);
845
846         print_bt_properties(num_properties, properties);
847
848         if (status != BT_STATUS_SUCCESS) {
849                 if (num_properties == 1) {
850                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
851                                 status2string(status), num_properties, properties[num_properties-1].type);
852                 } else {
853                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
854                 }
855                 return;
856         }
857
858         for (i = 0; i < num_properties; i++) {
859                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
860                 switch (properties[i].type) {
861                 case BT_PROPERTY_VERSION: {
862                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
863                         local_version[properties[i].len] = '\0';
864
865                         BT_DBG("Version: %s", local_version);
866                         /* Send event to application */
867                         if (num_properties == 1) {
868                                 char *adapter_ver = g_strdup(local_version);
869
870                                 /* Application has requested this property SET/GET hence send EVENT */
871                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
872                         }
873                         break;
874                 }
875                 case BT_PROPERTY_BDNAME: {
876                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
877                         local_name[properties[i].len] = '\0';
878
879                         BT_DBG("Name: %s", local_name);
880                         /* Send event to application */
881                         if (num_properties == 1) {
882                                 char * adap_name = g_strdup(local_name);
883
884                                 /* Application has requested this property SET/GET hence send EVENT */
885                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name)+1);
886                         }
887                         break;
888                 }
889                 case BT_PROPERTY_BDADDR: {
890                         bt_bdaddr_t * addr;
891
892                         addr =  properties[i].val;
893                         memcpy(local_address.addr, addr->address, 6);
894                         if (num_properties == 1) {
895                                 /* Application has requested this property SET/GET hence send EVENT */
896                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
897                                                 g_memdup(&local_address, sizeof(local_address)),
898                                                 sizeof(local_address));
899                         }
900                         break;
901                 }
902                 case BT_PROPERTY_UUIDS: {
903                         int num_uuid;
904
905                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
906
907                         BT_DBG("num_uuid: %d", num_uuid);
908
909                         /* Send event to application */
910                         if (num_properties == 1) {
911                                 event_adapter_services_t *uuids_event;
912
913                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
914                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
915                                 uuids_event->num = num_uuid;
916
917                                 /* Application has requested this property SET/GET hence send EVENT */
918                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
919                                                 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
920                         }
921                         break;
922                 }
923                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
924                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
925
926                         BT_INFO("Scan mode (%d)", cur_mode);
927
928                         scan_mode = cur_mode;
929
930                         /* Send event to application */
931                         if (num_properties == 1) {
932                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
933
934                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
935                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
936                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
937                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
938
939                                 /* Application has requested this property SET/GET hence send EVENT */
940                                 send_event(event, NULL, 0);
941                         }
942                         break;
943                 }
944                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
945                         int timeout;
946
947                         timeout = *((uint32_t*)properties[i].val);
948
949                         BT_INFO("Discoverability timeout: %d", timeout);
950                         discoverable_timeout = timeout;
951
952                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
953                                         g_memdup(properties[i].val, sizeof(uint32_t)),
954                                         sizeof(uint32_t));
955                         break;
956                 }
957                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
958                         int j;
959                         int num_bonded;
960                         bt_bdaddr_t *bonded_addr_list;
961                         event_device_list_t *event_data;
962
963                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
964                         BT_DBG("num_bonded %d", num_bonded);
965
966                         if (num_properties > 1) /* No explicit req for this prop, ignore */
967                                 break;
968
969                         bonded_addr_list = properties[i].val;
970                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
971                         event_data->num = num_bonded;
972
973                         for (j = 0; j < num_bonded; j++)
974                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
975
976                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
977                                         event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
978                         break;
979                 }
980                 case BT_PROPERTY_A2DP_ROLE: {
981                         unsigned int a2dp_role;
982
983                         a2dp_role = *((uint32_t*)properties[i].val);
984
985                         BT_INFO("A2DP role: %u", a2dp_role);
986
987                         send_event(OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE,
988                                         g_memdup(properties[i].val, sizeof(uint32_t)),
989                                         sizeof(uint32_t));
990                         break;
991                 }
992                 case BT_PROPERTY_LOCAL_LE_FEATURES: {
993                         event_adapter_le_features_t *le_features;
994
995                         le_features = g_malloc(sizeof(event_adapter_le_features_t));
996
997                         le_features->max_adv_instance = ((bt_local_le_features_t *)(properties[i].val))->max_adv_instance;
998                         le_features->rpa_offloading = ((bt_local_le_features_t *)(properties[i].val))->rpa_offload_supported;
999                         le_features->max_adv_filter = ((bt_local_le_features_t *)(properties[i].val))->max_adv_filter_supported;
1000                         le_features->le_2m_phy_support = ((bt_local_le_features_t *)(properties[i].val))->le_2m_phy_supported;
1001                         le_features->le_coded_phy_support = ((bt_local_le_features_t *)(properties[i].val))->le_2m_phy_supported;
1002
1003                         BT_INFO("LE 2M PHY Support (%d)", le_features->le_2m_phy_support);
1004                         BT_INFO("LE CODED PHY Support (%d)", le_features->le_coded_phy_support);
1005
1006                         send_event(OAL_EVENT_BLE_LOCAL_FEATURES,
1007                                         le_features,
1008                                         sizeof(event_adapter_le_features_t));
1009                         break;
1010                 }
1011                 case BT_PROPERTY_ADAPTER_LE_DISCOVERY_STARTED: {
1012                         BT_INFO("LE Discovery started");
1013                         send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1014                         break;
1015                 }
1016                 case BT_PROPERTY_ADAPTER_LE_DISCOVERY_STOPPED: {
1017                         BT_INFO("LE Discovery stopped");
1018                         send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1019                         break;
1020                 }
1021                 default:
1022                          BT_WARN("Unhandled property: %d", properties[i].type);
1023                          break;
1024                 }
1025         }
1026 }
1027
1028 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
1029 {
1030         oal_event_t event;
1031
1032         event = (BT_DISCOVERY_STARTED == state) ? OAL_EVENT_ADAPTER_INQUIRY_STARTED : OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
1033
1034         BT_DBG("%d", state);
1035         send_event(event, NULL, 0);
1036 }
1037
1038 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
1039 {
1040         remote_device_t dev_info;
1041         ble_adv_data_t adv_info;
1042         oal_event_t event;
1043         gpointer event_data;
1044         gsize size = 0;
1045         BT_DBG("+");
1046
1047         if (num_properties == 0) {
1048                 BT_ERR("Unexpected, properties count is zero!!");
1049                 return;
1050         }
1051
1052         memset(&dev_info, 0x00, sizeof(remote_device_t));
1053         memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
1054
1055         print_bt_properties(num_properties, properties);
1056         parse_device_properties(num_properties, properties, &dev_info, &adv_info);
1057
1058         BT_INFO("number of properties= [%d] ", num_properties);
1059
1060         if (dev_info.type != DEV_TYPE_BREDR) {
1061                 /* BLE Single or DUAL mode found, so it should have Adv data */
1062                 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
1063
1064                 ble_dev_event->adv_len = adv_info.len;
1065
1066                 if (adv_info.len > 0 && adv_info.adv_data) {
1067                         memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
1068                         ble_dev_event->adv_len = adv_info.len;
1069                 } else
1070                         ble_dev_event->adv_len = 0;
1071
1072                 ble_dev_event->device_info = dev_info;
1073
1074                 event_data = ble_dev_event;
1075                 size = sizeof(event_ble_dev_found_t);
1076                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
1077         } else {
1078                 /* BREDR device, so No Adv data */
1079                 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
1080
1081                 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
1082                 event_data = dev_event;
1083                 size = sizeof(remote_device_t);
1084                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
1085         }
1086
1087         send_event(event, event_data, size);
1088
1089         BT_DBG("-");
1090 }