[OAL] Add support for 'Reset Adapter'
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-adapter-mgr.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <dlog.h>
23 #include <string.h>
24 #include <vconf.h>
25 #include <sys/wait.h>
26
27 #include <bluetooth.h>
28
29 #include "oal-event.h"
30 #include "oal-internal.h"
31 #include "oal-manager.h"
32 #include "oal-hardware.h"
33 #include "oal-common.h"
34 #include "oal-utils.h"
35 #include "oal-gatt.h"
36
37 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
38
39 static const bt_interface_t * blued_api;
40
41 static bt_address_t local_address;
42 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
43 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
44 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
45 static int discoverable_timeout = 0;
46
47 /* Forward declarations */
48 oal_status_t convert_to_oal_status(bt_status_t status);
49 static gboolean retry_enable_adapter(gpointer data);
50 #ifdef TIZEN_BT_HAL
51 static gboolean retry_enable_le(gpointer data);
52 #endif
53 oal_status_t oal_mgr_init_internal(void);
54
55
56 /* Callback registered with Stack */
57 static void cb_adapter_state_change(bt_state_t status);
58 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
59 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
60 static void cb_adapter_properties (bt_status_t status,
61                 int num_properties, bt_property_t *properties);
62 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
63                 int num_properties, bt_property_t *properties);
64 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
65                                         bt_bond_state_t state);
66 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
67                                             bt_acl_state_t state);
68 extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
69 extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
70                         bt_ssp_variant_t pairing_variant, uint32_t pass_key);
71 extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
72 extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
73 #ifdef TIZEN_BT_HAL
74 extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid);
75 static void cb_ble_state_change(bt_state_t status);
76 #endif
77
78 static bt_callbacks_t callbacks = {
79         sizeof(callbacks),
80         cb_adapter_state_change,
81         cb_adapter_properties,
82         cb_device_properties,
83         cb_adapter_device_found,
84         cb_adapter_discovery_state_changed,
85         cb_device_pin_request,
86         cb_device_ssp_request,
87         cb_device_bond_state_changed,
88         cb_device_acl_state_changed,
89         NULL, /* callback_thread_event */
90         NULL, /* dut_mode_recv_callback */
91         NULL, /* le_test_mode_callback*/
92         NULL, /* energy_info_callback */
93         cb_device_authorize_request,
94         cb_device_trust_state_changed,
95 #ifdef TIZEN_BT_HAL
96         cb_socket_conn_authorize_request,
97         cb_ble_state_change,
98 #endif
99 };
100
101 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
102 {
103         int ret;
104         blued_api = stack_if;
105
106         ret = blued_api->init(&callbacks);
107
108         if (ret != BT_STATUS_SUCCESS) {
109                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
110                 blued_api->cleanup();
111                 return convert_to_oal_status(ret);
112         }
113
114         return OAL_STATUS_SUCCESS;
115 }
116
117 const bt_interface_t* adapter_get_stack_interface(void)
118 {
119         return blued_api;
120 }
121
122 void adapter_mgr_cleanup(void)
123 {
124         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
125         BT_DBG();
126 }
127
128 oal_status_t adapter_enable(void)
129 {
130         int ret = BT_STATUS_SUCCESS;
131
132         API_TRACE();
133         CHECK_OAL_INITIALIZED();
134         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
135                 g_timeout_add(200, retry_enable_adapter, NULL);
136                 return OAL_STATUS_PENDING;
137         }
138
139         ret = blued_api->enable();
140
141         if (ret != BT_STATUS_SUCCESS) {
142                 BT_ERR("Enable failed: [%s]", status2string(ret));
143                 return convert_to_oal_status(ret);
144         }
145
146         return OAL_STATUS_SUCCESS;
147 }
148
149 oal_status_t adapter_disable(void)
150 {
151         int ret;
152
153         API_TRACE();
154
155         CHECK_OAL_INITIALIZED();
156
157         ret = blued_api->disable();
158
159         if (ret != BT_STATUS_SUCCESS) {
160                 BT_ERR("Disable failed: [%s]", status2string(ret));
161                 return convert_to_oal_status(ret);
162         }
163         return OAL_STATUS_SUCCESS;
164 }
165
166 oal_status_t le_enable(void)
167 {
168         int ret = BT_STATUS_SUCCESS;
169
170         API_TRACE();
171         CHECK_OAL_INITIALIZED();
172
173 #ifdef TIZEN_BT_HAL
174         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
175                 g_timeout_add(200, retry_enable_le, NULL);
176                 return OAL_STATUS_PENDING;
177         }
178
179         ret = blued_api->le_enable();
180
181         if (ret != BT_STATUS_SUCCESS) {
182                 BT_ERR("Enable failed: [%s]", status2string(ret));
183                 return convert_to_oal_status(ret);
184         }
185 #else
186         BT_INFO("Not Supported");
187         ret = OAL_STATUS_NOT_SUPPORT;
188 #endif
189
190         return ret;
191 }
192
193 oal_status_t le_disable(void)
194 {
195         int ret;
196
197         API_TRACE();
198
199         CHECK_OAL_INITIALIZED();
200
201 #ifdef TIZEN_BT_HAL
202         ret = blued_api->le_disable();
203
204         if (ret != BT_STATUS_SUCCESS) {
205                 BT_ERR("Disable failed: [%s]", status2string(ret));
206                 return convert_to_oal_status(ret);
207         }
208 #else
209         BT_INFO("Not Supported");
210         ret = OAL_STATUS_NOT_SUPPORT;
211 #endif
212         return ret;
213 }
214
215 oal_status_t adapter_start_custom_inquiry(discovery_type_t disc_type)
216 {
217         int ret;
218
219         API_TRACE();
220
221         CHECK_OAL_INITIALIZED();
222         BT_INFO("Custom Discovery Type [0x%x]", disc_type);
223
224 #ifdef TIZEN_BT_HAL
225         ret = blued_api->start_custom_discovery(disc_type);
226         if (ret != BT_STATUS_SUCCESS) {
227                 BT_ERR("start_custom_discovery failed: [%s]", status2string(ret));
228                 return convert_to_oal_status(ret);
229         }
230 #else
231         BT_INFO("Not Supported");
232         ret = OAL_STATUS_NOT_SUPPORT;
233 #endif
234         return ret;
235 }
236
237 oal_status_t adapter_reset(void)
238 {
239         int ret;
240
241         API_TRACE();
242
243         CHECK_OAL_INITIALIZED();
244         BT_INFO("Adapter Reset");
245
246 #ifdef TIZEN_BT_HAL
247         ret = blued_api->reset();
248         if (ret != BT_STATUS_SUCCESS) {
249                 BT_ERR("Adapter Reset failed: [%s]", status2string(ret));
250                 return convert_to_oal_status(ret);
251         }
252 #else
253         BT_INFO("Not Supported");
254         ret = OAL_STATUS_NOT_SUPPORT;
255 #endif
256         return ret;
257 }
258
259 oal_status_t adapter_start_inquiry(unsigned short duration)
260 {
261         int ret;
262
263         API_TRACE();
264
265         CHECK_OAL_INITIALIZED();
266
267         ret = blued_api->start_discovery();
268         if (ret != BT_STATUS_SUCCESS) {
269                 BT_ERR("start_discovery failed: [%s]", status2string(ret));
270                 return convert_to_oal_status(ret);
271         }
272
273         return OAL_STATUS_SUCCESS;
274 }
275
276 oal_status_t adapter_stop_inquiry(void)
277 {
278         int ret;
279
280         API_TRACE();
281
282         CHECK_OAL_INITIALIZED();
283
284         ret = blued_api->cancel_discovery();
285         if (ret != BT_STATUS_SUCCESS) {
286                 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
287                 return convert_to_oal_status(ret);
288         }
289
290         return OAL_STATUS_SUCCESS;
291 }
292
293 /* Callbacks from Stack */
294 static void cb_adapter_state_change(bt_state_t status)
295 {
296         BT_DBG("+");
297         oal_event_t event;
298
299         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
300
301         send_event(event, NULL, 0);
302 }
303
304 #ifdef TIZEN_BT_HAL
305 /* Callbacks from Stack */
306 static void cb_ble_state_change(bt_state_t status)
307 {
308         BT_DBG("+");
309         oal_event_t event;
310
311         event = (BT_STATE_ON == status)?OAL_EVENT_BLE_ENABLED:OAL_EVENT_BLE_DISABLED;
312
313         send_event(event, NULL, 0);
314 }
315 #endif
316
317 static gboolean retry_enable_adapter(gpointer data)
318 {
319         adapter_enable();
320         return FALSE;
321 }
322
323 #ifdef TIZEN_BT_HAL
324 static gboolean retry_enable_le(gpointer data)
325 {
326         le_enable();
327         return FALSE;
328 }
329 #endif
330 oal_status_t adapter_get_properties(void)
331 {
332         int ret;
333
334         API_TRACE();
335         CHECK_OAL_INITIALIZED();
336
337         ret = blued_api->get_adapter_properties();
338         if (ret != BT_STATUS_SUCCESS) {
339                 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
340                 return convert_to_oal_status(ret);
341         }
342
343         return OAL_STATUS_SUCCESS;
344 }
345
346 oal_status_t adapter_get_address(void)
347 {
348         int ret;
349
350         API_TRACE();
351         CHECK_OAL_INITIALIZED();
352
353         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
354         if (ret != BT_STATUS_SUCCESS) {
355                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
356                 return convert_to_oal_status(ret);
357         }
358
359         return OAL_STATUS_SUCCESS;
360 }
361
362 oal_status_t adapter_get_version(void)
363 {
364         int ret;
365
366         API_TRACE();
367         CHECK_OAL_INITIALIZED();
368
369         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
370         if (ret != BT_STATUS_SUCCESS) {
371                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
372                 return convert_to_oal_status(ret);
373         }
374
375         return OAL_STATUS_SUCCESS;
376 }
377
378 oal_status_t adapter_get_name(void)
379 {
380         int ret;
381
382         CHECK_OAL_INITIALIZED();
383
384         API_TRACE();
385
386         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
387         if (ret != BT_STATUS_SUCCESS) {
388                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
389                 return convert_to_oal_status(ret);
390         }
391
392         return OAL_STATUS_SUCCESS;
393 }
394
395 oal_status_t adapter_set_name(char * name)
396 {
397         int ret;
398         bt_property_t prop;
399
400         CHECK_OAL_INITIALIZED();
401
402         OAL_CHECK_PARAMETER(name, return);
403         API_TRACE("Name: %s", name);
404
405         prop.type = BT_PROPERTY_BDNAME;
406         prop.len = strlen(name);
407         prop.val = name;
408
409         ret = blued_api->set_adapter_property(&prop);
410         if (ret != BT_STATUS_SUCCESS) {
411                 BT_ERR("set_adapter_property: [%s]", status2string(ret));
412                 ret = OAL_STATUS_INTERNAL_ERROR;
413         } else
414                 ret = OAL_STATUS_SUCCESS;
415
416         return ret;
417 }
418
419 oal_status_t adapter_is_discoverable(int *p_discoverable)
420 {
421         OAL_CHECK_PARAMETER(p_discoverable, return);
422
423         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
424
425         API_TRACE("%d", *p_discoverable);
426
427         return OAL_STATUS_SUCCESS;
428 }
429
430 oal_status_t adapter_is_connectable(int *p_connectable)
431 {
432         OAL_CHECK_PARAMETER(p_connectable, return);
433
434         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
435                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
436
437         API_TRACE("%d", *p_connectable);
438
439         return OAL_STATUS_SUCCESS;
440 }
441
442 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
443 {
444         API_TRACE("%d", discoverable_timeout);
445
446         *p_timeout = discoverable_timeout;
447
448         return OAL_STATUS_SUCCESS;
449 }
450
451 oal_status_t adapter_get_service_uuids(void)
452 {
453         int ret;
454
455         CHECK_OAL_INITIALIZED();
456
457         API_TRACE();
458
459         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
460         if (ret != BT_STATUS_SUCCESS) {
461                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
462                 return convert_to_oal_status(ret);
463         }
464
465         return OAL_STATUS_SUCCESS;
466 }
467
468 oal_status_t adapter_get_bonded_devices(void)
469 {
470         int ret;
471
472         CHECK_OAL_INITIALIZED();
473
474         API_TRACE();
475
476         ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
477         if (ret != BT_STATUS_SUCCESS) {
478                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
479                 return convert_to_oal_status(ret);
480         }
481
482         return OAL_STATUS_SUCCESS;
483 }
484
485 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
486 {
487         bt_property_t prop;
488         int res;
489
490         BT_DBG("+");
491
492         CHECK_OAL_INITIALIZED();
493
494         prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
495         prop.len = sizeof(bt_scan_mode_t);
496         prop.val = &mode;
497         res = blued_api->set_adapter_property(&prop);
498         if (res != BT_STATUS_SUCCESS) {
499                 BT_ERR("set scan mode failed [%s]", status2string(res));
500                 return convert_to_oal_status(res);
501         }
502
503         BT_DBG("-");
504         return OAL_STATUS_SUCCESS;
505 }
506
507 oal_status_t adapter_set_connectable(int connectable)
508 {
509         bt_scan_mode_t mode;
510
511         API_TRACE("%d", connectable);
512
513         CHECK_OAL_INITIALIZED();
514
515         mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
516
517         return set_scan_mode(mode);
518 }
519
520 oal_status_t adapter_set_discoverable(void)
521 {
522         CHECK_OAL_INITIALIZED();
523         API_TRACE();
524
525         return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
526 }
527
528 oal_status_t adapter_set_discoverable_timeout(int timeout)
529 {
530         bt_property_t prop;
531         int res;
532         uint32_t prop_val = timeout;
533
534         CHECK_OAL_INITIALIZED();
535         API_TRACE("%d", timeout);
536
537         prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
538         prop.len = sizeof(prop_val);
539         prop.val = &prop_val;
540         res = blued_api->set_adapter_property(&prop);
541         if (res != BT_STATUS_SUCCESS) {
542                 BT_ERR("set_adapter_property failed [%s]", status2string(res));
543                 return convert_to_oal_status(res);
544         }
545         return OAL_STATUS_SUCCESS;
546 }
547
548 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
549                         int adv_type, int chnl_map, int tx_power, int timeout_s)
550 {
551         int res;
552         CHECK_OAL_INITIALIZED();
553         API_TRACE();
554
555         res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
556                         adv_type, chnl_map, tx_power, timeout_s);
557         if (res != OAL_STATUS_SUCCESS) {
558                 BT_ERR("gatts_multi_adv_update: [%d]", res);
559                 return res;
560         }
561         return OAL_STATUS_SUCCESS;
562 }
563
564 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
565                         oal_ble_multi_adv_param_setup_t * adv_param_setup)
566 {
567         int res;
568         CHECK_OAL_INITIALIZED();
569         OAL_CHECK_PARAMETER(adv_param_setup, return);
570
571         API_TRACE();
572
573         res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
574         if (res != OAL_STATUS_SUCCESS) {
575                 BT_ERR("failed: [%d]", res);
576                 return res;
577         }
578         return OAL_STATUS_SUCCESS;
579 }
580
581 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
582 {
583         int res;
584         CHECK_OAL_INITIALIZED();
585         API_TRACE();
586
587         res = gatts_multi_adv_enable(instance_id);
588         if (res != OAL_STATUS_SUCCESS) {
589                 BT_ERR("failed: [%d]", res);
590                 return res;
591         }
592
593         return OAL_STATUS_SUCCESS;
594 }
595
596 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
597 {
598         int res;
599         CHECK_OAL_INITIALIZED();
600         API_TRACE();
601
602         res = gatts_multi_adv_disable(instance_id);
603         if (res != OAL_STATUS_SUCCESS) {
604                 BT_ERR("failed: [%d]", res);
605                 return res;
606         }
607
608         return OAL_STATUS_SUCCESS;
609 }
610
611 static void cb_adapter_properties(bt_status_t status,
612                                                int num_properties,
613                                                bt_property_t *properties)
614 {
615         int i;
616
617         BT_DBG("status: %d, count: %d", status, num_properties);
618
619         if (status != BT_STATUS_SUCCESS) {
620                 if (num_properties == 1) {
621                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
622                                 status2string(status), num_properties, properties[num_properties-1].type);
623                 } else {
624                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
625                 }
626                 return;
627         }
628
629         for (i = 0; i < num_properties; i++) {
630                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
631                 switch (properties[i].type) {
632                 case BT_PROPERTY_VERSION: {
633                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
634                         local_version[properties[i].len] = '\0';
635
636                         BT_DBG("Version: %s", local_version);
637                         /* Send event to application */
638                         if (num_properties == 1) {
639                                 char *adapter_ver = g_strdup(local_version);
640
641                                 /* Application has requested this property SET/GET hence send EVENT */
642                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
643                         }
644                         break;
645                 }
646                 case BT_PROPERTY_BDNAME: {
647                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
648                         local_name[properties[i].len] = '\0';
649
650                         BT_DBG("Name: %s", local_name);
651                         /* Send event to application */
652                         if (num_properties == 1) {
653                                 char * adap_name = g_strdup(local_name);
654
655                                 /* Application has requested this property SET/GET hence send EVENT */
656                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
657                         }
658                         break;
659                 }
660                 case BT_PROPERTY_BDADDR: {
661                         bt_bdaddr_t * addr;
662
663                         addr =  properties[i].val;
664                         memcpy(local_address.addr, addr->address, 6);
665                         if (num_properties == 1) {
666                                 /* Application has requested this property SET/GET hence send EVENT */
667                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
668                                                 g_memdup(&local_address, sizeof(local_address)),
669                                                 sizeof(local_address));
670                         }
671                         break;
672                 }
673                 case BT_PROPERTY_UUIDS: {
674                         int num_uuid;
675
676                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
677
678                         BT_DBG("num_uuid: %d", num_uuid);
679
680                         /* Send event to application */
681                         if (num_properties == 1) {
682                                 event_adapter_services_t *uuids_event;
683
684                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
685                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
686                                 uuids_event->num = num_uuid;
687
688                                 /* Application has requested this property SET/GET hence send EVENT */
689                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
690                                                 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
691                         }
692                         break;
693                 }
694                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
695                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
696
697                         BT_INFO("Scan mode (%d)", cur_mode);
698
699                         scan_mode = cur_mode;
700
701                         /* Send event to application */
702                         if (num_properties == 1) {
703                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
704
705                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
706                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
707                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
708                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
709
710                                 /* Application has requested this property SET/GET hence send EVENT */
711                                 send_event(event, NULL, 0);
712                         }
713                         break;
714                 }
715                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
716                         int timeout;
717
718                         timeout = *((uint32_t*)properties[i].val);
719
720                         BT_INFO("Discoverability timeout: %d", timeout);
721                         discoverable_timeout = timeout;
722
723                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
724                                         g_memdup(properties[i].val, sizeof(uint32_t)),
725                                         sizeof(uint32_t));
726                         break;
727                 }
728                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
729                         int j;
730                         int num_bonded;
731                         bt_bdaddr_t *bonded_addr_list;
732                         event_device_list_t *event_data;
733
734                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
735                         BT_DBG("num_bonded %d", num_bonded);
736
737                         if (num_properties > 1) /* No explicit req for this prop, ignore */
738                                 break;
739
740                         bonded_addr_list = properties[i].val;
741                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
742                         event_data->num = num_bonded;
743
744                         for (j = 0; j < num_bonded; j++)
745                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
746
747                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
748                                         event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
749                         break;
750                 }
751                 default:
752                          BT_WARN("Unhandled property: %d", properties[i].type);
753                          break;
754                 }
755         }
756 }
757
758 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
759 {
760         oal_event_t event;
761
762         event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
763
764         BT_DBG("%d", state);
765         send_event(event, NULL, 0);
766 }
767
768 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
769 {
770         remote_device_t dev_info;
771         ble_adv_data_t adv_info;
772         oal_event_t event;
773         gpointer event_data;
774         gsize size = 0;
775         BT_DBG("+");
776
777         if (num_properties == 0) {
778                 BT_ERR("Unexpected, properties count is zero!!");
779                 return;
780         }
781
782         memset(&dev_info, 0x00, sizeof(remote_device_t));
783         memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
784
785         print_bt_properties(num_properties, properties);
786         parse_device_properties(num_properties, properties, &dev_info, &adv_info);
787
788         BT_INFO("number of properties= [%d] ", num_properties, size);
789
790         if (dev_info.type != DEV_TYPE_BREDR) {
791                 /* BLE Single or DUAL mode found, so it should have Adv data */
792                 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
793
794                 ble_dev_event->adv_len = adv_info.len;
795
796                 if (adv_info.len > 0 && adv_info.adv_data) {
797                         memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
798                         ble_dev_event->adv_len = adv_info.len;
799                 } else
800                         ble_dev_event->adv_len = 0;
801
802                 ble_dev_event->device_info = dev_info;
803
804                 event_data = ble_dev_event;
805                 size = sizeof(event_ble_dev_found_t);
806                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
807         } else {
808                 /* BREDR device, so No Adv data */
809                 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
810
811                 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
812                 event_data = dev_event;
813                 size = sizeof(remote_device_t);
814                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
815         }
816
817         send_event(event, event_data, size);
818
819         BT_DBG("-");
820 }