Merge branch 'tizen' into tizen_5.5
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-device-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 <dlog.h>
22 #include <string.h>
23
24 #include <bluetooth.h>
25
26 #include "oal-event.h"
27 #include "oal-internal.h"
28 #include "oal-common.h"
29 #include "oal-manager.h"
30 #include "oal-utils.h"
31 #include "oal-device-mgr.h"
32
33 static const bt_interface_t * blued_api;
34 #ifdef TIZEN_BT_HAL
35 static int dbfw_rssi;
36 #endif
37
38 void device_mgr_init(const bt_interface_t * stack_if)
39 {
40         blued_api = stack_if;
41 }
42
43 void device_mgr_cleanup(void)
44 {
45         BT_DBG();
46         blued_api = NULL;
47 }
48
49 oal_status_t device_query_attributes(bt_address_t *addr)
50 {
51         int res;
52         bdstr_t bdstr;
53
54         CHECK_OAL_INITIALIZED();
55
56         OAL_CHECK_PARAMETER(addr, return);
57
58         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
59
60         res = blued_api->get_remote_device_properties((bt_bdaddr_t *)addr);
61         if (res != BT_STATUS_SUCCESS) {
62                 BT_ERR("get_remote_device_properties error: [%s]", status2string(res));
63                 return convert_to_oal_status(res);
64         }
65
66         return OAL_STATUS_SUCCESS;
67 }
68
69 oal_status_t device_query_services(bt_address_t * addr)
70 {
71         int res;
72         bdstr_t bdstr;
73
74         CHECK_OAL_INITIALIZED();
75
76         OAL_CHECK_PARAMETER(addr, return);
77
78         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
79
80         res = blued_api->get_remote_services((bt_bdaddr_t *)addr);
81         if (res != BT_STATUS_SUCCESS) {
82                 BT_ERR("get_remote_services error: [%s]", status2string(res));
83                 return convert_to_oal_status(res);
84         }
85
86         return OAL_STATUS_SUCCESS;
87 }
88
89 oal_status_t device_stop_query_sevices(bt_address_t * addr)
90 {
91         CHECK_OAL_INITIALIZED();
92
93         OAL_CHECK_PARAMETER(addr, return);
94
95         API_TRACE("Stop SDP search");
96         /* Currently no HAL Interface for Stopping Service Search */
97         return BT_STATUS_UNSUPPORTED;
98 }
99
100 oal_status_t device_set_alias(bt_address_t * addr, char * alias)
101 {
102         int res;
103         bt_property_t prop;
104         bdstr_t bdstr;
105
106         CHECK_OAL_INITIALIZED();
107         OAL_CHECK_PARAMETER(addr, return);
108         OAL_CHECK_PARAMETER(alias, return);
109
110         API_TRACE("%s ->Alias: %s", bdt_bd2str(addr, &bdstr), alias);
111
112         prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME;
113         prop.len = strlen(alias);
114         prop.val = alias;
115         res = blued_api->set_remote_device_property((bt_bdaddr_t*)addr, &prop);
116         if (res != BT_STATUS_SUCCESS) {
117                 BT_ERR("set_remote_device_property error: [%s]", status2string(res));
118                 BT_ERR("Alias: %s", alias);
119                 return convert_to_oal_status(res);
120         }
121
122         return OAL_STATUS_SUCCESS;
123 }
124
125 oal_status_t device_create_bond(bt_address_t *addr, oal_conn_type_e transport)
126 {
127         int res;
128         bdstr_t bdstr;
129
130         CHECK_OAL_INITIALIZED();
131
132         OAL_CHECK_PARAMETER(addr, return);
133
134         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
135
136         res = blued_api->create_bond((bt_bdaddr_t *)addr, transport);
137         if (res != BT_STATUS_SUCCESS) {
138                 BT_ERR("create_bond error: [%s]", status2string(res));
139                 return convert_to_oal_status(res);
140         }
141
142         return OAL_STATUS_SUCCESS;
143 }
144
145 oal_status_t device_destroy_bond(bt_address_t * addr)
146 {
147         int res;
148         bdstr_t bdstr;
149
150         CHECK_OAL_INITIALIZED();
151
152         OAL_CHECK_PARAMETER(addr, return);
153
154         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
155
156         res = blued_api->remove_bond((bt_bdaddr_t *)addr);
157         if (res != BT_STATUS_SUCCESS) {
158                 BT_ERR("remove_bond error: [%s]", status2string(res));
159                 return convert_to_oal_status(res);
160         }
161
162         return OAL_STATUS_SUCCESS;
163 }
164
165 oal_status_t device_stop_bond(bt_address_t * addr)
166 {
167         int res;
168         bdstr_t bdstr;
169
170         CHECK_OAL_INITIALIZED();
171
172         OAL_CHECK_PARAMETER(addr, return);
173
174         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
175
176         res = blued_api->cancel_bond((bt_bdaddr_t *)addr);
177         if (res != BT_STATUS_SUCCESS) {
178                 BT_ERR("cancel_bond error: [%s]", status2string(res));
179                 return convert_to_oal_status(res);
180         }
181
182         return OAL_STATUS_SUCCESS;
183 }
184
185 oal_status_t device_accept_pin_request(bt_address_t * addr, const char * pin)
186 {
187         int res;
188         bdstr_t bdstr;
189
190         CHECK_OAL_INITIALIZED();
191
192         OAL_CHECK_PARAMETER(addr, return);
193         OAL_CHECK_PARAMETER(pin, return);
194
195         API_TRACE("[%s] PIN: %s", bdt_bd2str(addr, &bdstr), pin);
196
197         res = blued_api->pin_reply((bt_bdaddr_t *)addr, TRUE, strlen(pin), (bt_pin_code_t *)pin);
198         if (res != BT_STATUS_SUCCESS) {
199                 BT_ERR("pin_reply error: [%s]", status2string(res));
200                 BT_ERR("PIN: %s", pin);
201                 return convert_to_oal_status(res);
202         }
203
204         return OAL_STATUS_SUCCESS;
205 }
206
207 oal_status_t device_reject_pin_request(bt_address_t * addr)
208 {
209         int res;
210         bdstr_t bdstr;
211
212         CHECK_OAL_INITIALIZED();
213
214         OAL_CHECK_PARAMETER(addr, return);
215
216         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
217
218         res = blued_api->pin_reply((bt_bdaddr_t *)addr, FALSE, 0, NULL);
219         if (res != BT_STATUS_SUCCESS) {
220                 BT_ERR("pin_reply error: [%s]", status2string(res));
221                 return convert_to_oal_status(res);
222         }
223
224         return OAL_STATUS_SUCCESS;
225 }
226
227 oal_status_t device_accept_passkey_entry(bt_address_t * addr, uint32_t passkey)
228 {
229         int res;
230         bdstr_t bdstr;
231
232         CHECK_OAL_INITIALIZED();
233
234         OAL_CHECK_PARAMETER(addr, return);
235
236         API_TRACE("[%s] Passkey: %d", bdt_bd2str(addr, &bdstr), passkey);
237
238         res = blued_api->ssp_reply((bt_bdaddr_t *)addr, BT_SSP_VARIANT_PASSKEY_ENTRY, TRUE, passkey);
239         if (res != BT_STATUS_SUCCESS) {
240                 BT_ERR("ssp_reply error: [%s]", status2string(res));
241                 BT_ERR("Passkey: %d", passkey);
242                 return convert_to_oal_status(res);
243
244         }
245
246         return OAL_STATUS_SUCCESS;
247 }
248
249 oal_status_t device_reject_passkey_entry(bt_address_t * addr)
250 {
251         int res;
252         bdstr_t bdstr;
253
254         CHECK_OAL_INITIALIZED();
255
256         OAL_CHECK_PARAMETER(addr, return);
257
258         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
259
260         res = blued_api->ssp_reply((bt_bdaddr_t *)addr, BT_SSP_VARIANT_PASSKEY_ENTRY, FALSE, 0);
261         if (res != BT_STATUS_SUCCESS) {
262                 BT_ERR("ssp_reply error: [%s]", status2string(res));
263                 return convert_to_oal_status(res);
264         }
265
266         return OAL_STATUS_SUCCESS;
267 }
268
269 oal_status_t device_reply_passkey_confirmation(bt_address_t * addr, int accept)
270 {
271         int res;
272         bdstr_t bdstr;
273
274         CHECK_OAL_INITIALIZED();
275
276         OAL_CHECK_PARAMETER(addr, return);
277
278         API_TRACE("[%s] accept: %d", bdt_bd2str(addr, &bdstr), accept);
279
280         res = blued_api->ssp_reply((bt_bdaddr_t *)addr, BT_SSP_VARIANT_PASSKEY_CONFIRMATION, accept, 0);
281         if (res != BT_STATUS_SUCCESS) {
282                 BT_ERR("ssp_reply error: [%s]", status2string(res));
283                 BT_ERR("%d", accept);
284                 return convert_to_oal_status(res);
285         }
286
287         return OAL_STATUS_SUCCESS;
288
289 }
290
291 oal_status_t device_reply_ssp_consent(bt_address_t * addr, int accept)
292 {
293         int res;
294         bdstr_t bdstr;
295
296         CHECK_OAL_INITIALIZED();
297
298         OAL_CHECK_PARAMETER(addr, return);
299
300         API_TRACE("[%s] %d", bdt_bd2str(addr, &bdstr), accept);
301
302         res = blued_api->ssp_reply((bt_bdaddr_t *)addr, BT_SSP_VARIANT_CONSENT, accept, 0);
303         if (res != BT_STATUS_SUCCESS) {
304                 BT_ERR("ssp_reply error: [%s]", status2string(res));
305                 BT_ERR("%d", accept);
306                 return convert_to_oal_status(res);
307         }
308
309         return OAL_STATUS_SUCCESS;
310 }
311
312 oal_status_t device_reply_auth_request(bt_address_t * addr, oal_service_t service_type, int accept, int always)
313 {
314         int res;
315         bdstr_t bdstr;
316
317         CHECK_OAL_INITIALIZED();
318
319         OAL_CHECK_PARAMETER(addr, return);
320
321         API_TRACE("[%s] Accept: %d, always: %d, service_type: %d", bdt_bd2str(addr, &bdstr), accept, always, service_type);
322
323         res = blued_api->authorize_response((bt_bdaddr_t *)addr, service_type, accept, always);
324         if (res != BT_STATUS_SUCCESS) {
325                 BT_ERR("authorize_response error: [%s]", status2string(res));
326                 BT_ERR("Accept: [%d], always: [%d], service_type: [%d]", accept, always, service_type);
327                 return convert_to_oal_status(res);
328         }
329
330         return OAL_STATUS_SUCCESS;
331 }
332
333 oal_status_t device_set_authorized(bt_address_t * addr, int authorize)
334 {
335         int res;
336         bdstr_t bdstr;
337
338         CHECK_OAL_INITIALIZED();
339
340         OAL_CHECK_PARAMETER(addr, return);
341
342         API_TRACE("[%s] %d", bdt_bd2str(addr, &bdstr), authorize);
343
344         res = blued_api->set_authorization((bt_bdaddr_t *)addr, authorize);
345         if (res != BT_STATUS_SUCCESS) {
346                 BT_ERR("set_authorization error: [%s]", status2string(res));
347                 BT_ERR("%d", authorize);
348                 return convert_to_oal_status(res);
349         }
350
351         return OAL_STATUS_SUCCESS;
352 }
353
354 gboolean device_get_acl_conn_state(bt_address_t * addr)
355 {
356         int res;
357         bdstr_t bdstr;
358
359         CHECK_OAL_INITIALIZED();
360         OAL_CHECK_PARAMETER(addr, return);
361
362         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
363
364         res = blued_api->get_connection_state((bt_bdaddr_t *)addr);
365         BT_ERR("get_connection_state returned: [%s]", res ? "TRUE" : "FALSE");
366
367         return res;
368 }
369
370 gboolean device_get_svc_conn_state(bt_address_t * addr, oal_service_t svc_id)
371 {
372         int res;
373         bdstr_t bdstr;
374
375         CHECK_OAL_INITIALIZED();
376         OAL_CHECK_PARAMETER(addr, return);
377
378         API_TRACE("[%s] %d", bdt_bd2str(addr, &bdstr), svc_id);
379
380 #ifdef TIZEN_BT_HAL
381         res = blued_api->get_service_connection_state((bt_bdaddr_t *)addr, svc_id);
382 #else
383         BT_ERR("Not supported");
384         res = FALSE;
385 #endif
386         BT_ERR("get_service_connection_state returned: [%s]", res ? "TRUE" : "FALSE");
387
388         return res;
389 }
390
391 oal_status_t device_register_osp_server(oal_osp_server_type_e type, char *uuid, char *path, int fd)
392 {
393         int res;
394         uint32_t server_type;
395
396         CHECK_OAL_INITIALIZED();
397
398         API_TRACE("type: %d", type);
399
400 #ifdef TIZEN_BT_HAL
401         switch (type) {
402         case OAL_OSP_SERVER_OBEX:
403                 server_type = BT_OSP_SERVER_OBEX;
404                 break;
405         case OAL_OSP_SERVER_RFCOMM:
406                 server_type = BT_OSP_SERVER_RFCOMM;
407                 break;
408         default:
409                 BT_ERR("unknown type: %d", type);
410                 return OAL_STATUS_INVALID_PARAM;
411         }
412
413         res = blued_api->register_agent_osp_server(server_type, uuid, path, fd);
414         if (res != BT_STATUS_SUCCESS) {
415                 BT_ERR("register_agent_osp_server error: [%s]", status2string(res));
416                 return convert_to_oal_status(res);
417         }
418
419         return OAL_STATUS_SUCCESS;
420 #else
421         BT_ERR("Not supported");
422         res = OAL_STATUS_NOT_SUPPORT;
423         return res;
424 #endif
425 }
426
427 oal_status_t device_unregister_osp_server(oal_osp_server_type_e type, char *uuid)
428 {
429         int res;
430         uint32_t server_type;
431
432         CHECK_OAL_INITIALIZED();
433
434         API_TRACE("type: %d", type);
435
436 #ifdef TIZEN_BT_HAL
437         switch (type) {
438         case OAL_OSP_SERVER_OBEX:
439                 server_type = BT_OSP_SERVER_OBEX;
440                 break;
441         case OAL_OSP_SERVER_RFCOMM:
442                 server_type = BT_OSP_SERVER_RFCOMM;
443                 break;
444         default:
445                 BT_ERR("unknown type: %d", type);
446                 return OAL_STATUS_INVALID_PARAM;
447         }
448
449         res = blued_api->unregister_agent_osp_server(server_type, uuid);
450         if (res != BT_STATUS_SUCCESS) {
451                 BT_ERR("unregister_agent_osp_server error: [%s]", status2string(res));
452                 return convert_to_oal_status(res);
453         }
454
455         return OAL_STATUS_SUCCESS;
456 #else
457         BT_ERR("Not supported");
458         res = OAL_STATUS_NOT_SUPPORT;
459         return res;
460 #endif
461 }
462
463 oal_status_t device_set_trust_profile(bt_address_t *addr, oal_trusted_profile_e profile, gboolean trust)
464 {
465         int res;
466         bdstr_t bdstr;
467
468         CHECK_OAL_INITIALIZED();
469
470         OAL_CHECK_PARAMETER(addr, return);
471
472         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
473
474 #ifdef TIZEN_BT_HAL
475         res = blued_api->set_trusted_profile((bt_bdaddr_t *)addr, profile, (trust ? 1 : 0));
476         if (res != BT_STATUS_SUCCESS) {
477                 BT_ERR("set_trusted_profile error: [%s]", status2string(res));
478                 return convert_to_oal_status(res);
479         }
480
481         return OAL_STATUS_SUCCESS;
482 #else
483         BT_ERR("Not supported");
484         res = OAL_STATUS_NOT_SUPPORT;
485         return res;
486 #endif
487 }
488
489 oal_status_t device_get_trust_profile(bt_address_t *addr, oal_trusted_profile_e profile, unsigned int *trusted)
490 {
491         int res;
492         bdstr_t bdstr;
493
494         CHECK_OAL_INITIALIZED();
495
496         OAL_CHECK_PARAMETER(addr, return);
497
498         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
499
500 #ifdef TIZEN_BT_HAL
501         res = blued_api->get_trusted_profile((bt_bdaddr_t *)addr, profile, trusted);
502         if (res != BT_STATUS_SUCCESS) {
503                 BT_ERR("get_trusted_profile error: [%s]", status2string(res));
504                 return convert_to_oal_status(res);
505         }
506
507         return OAL_STATUS_SUCCESS;
508 #else
509         BT_ERR("Not supported");
510         res = OAL_STATUS_NOT_SUPPORT;
511         return res;
512 #endif
513 }
514
515 oal_status_t device_enable_rssi_monitoring(bt_address_t *addr, unsigned int link_type,
516                 int low_threshold, int in_range_threshold, int high_threshold)
517 {
518         int res;
519         bdstr_t bdstr;
520
521         CHECK_OAL_INITIALIZED();
522
523         OAL_CHECK_PARAMETER(addr, return);
524
525         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
526
527 #ifdef TIZEN_BT_HAL
528         res = blued_api->enable_rssi_monitoring((bt_bdaddr_t *)addr, link_type,
529                         low_threshold, in_range_threshold, high_threshold);
530         if (res != BT_STATUS_SUCCESS) {
531                 BT_ERR("enable_rssi_monitoring error: [%s]", status2string(res));
532                 return convert_to_oal_status(res);
533         }
534
535         return OAL_STATUS_SUCCESS;
536 #else
537         BT_ERR("Not supported");
538         res = OAL_STATUS_NOT_SUPPORT;
539         return res;
540 #endif
541 }
542
543 oal_status_t device_get_connected_link_rssi_strength(bt_address_t *addr, unsigned int link_type)
544 {
545         int res;
546         bdstr_t bdstr;
547
548         CHECK_OAL_INITIALIZED();
549
550         OAL_CHECK_PARAMETER(addr, return);
551
552         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
553
554 #ifdef TIZEN_BT_HAL
555         res = blued_api->get_connected_link_rssi_strength((bt_bdaddr_t *)addr, link_type);
556         if (res != BT_STATUS_SUCCESS) {
557                 BT_ERR("get_connected_link_rssi_strength error: [%s]", status2string(res));
558                 return convert_to_oal_status(res);
559         }
560
561         return OAL_STATUS_SUCCESS;
562 #else
563         BT_ERR("Not supported");
564         res = OAL_STATUS_NOT_SUPPORT;
565         return res;
566 #endif
567 }
568
569 oal_status_t device_enable_gap_auth_notifications(oal_gap_auth_type_e type, gboolean enable)
570 {
571         int res;
572
573         CHECK_OAL_INITIALIZED();
574
575         API_TRACE("type: [%d], enable: %d", type, enable);
576
577 #ifdef TIZEN_BT_HAL
578         res = blued_api->enable_gap_auth_notifications(type, (enable ? 1 : 0));
579         if (res != BT_STATUS_SUCCESS) {
580                 BT_ERR("enable_gap_auth_notifications error: [%s]", status2string(res));
581                 return convert_to_oal_status(res);
582         }
583
584         return OAL_STATUS_SUCCESS;
585 #else
586         BT_ERR("Not supported");
587         res = OAL_STATUS_NOT_SUPPORT;
588         return res;
589 #endif
590 }
591
592 oal_status_t device_disconnect(bt_address_t * addr)
593 {
594         int res;
595         bdstr_t bdstr;
596
597         CHECK_OAL_INITIALIZED();
598
599         OAL_CHECK_PARAMETER(addr, return);
600
601         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
602
603         res = blued_api->device_disconnect((bt_bdaddr_t *)addr);
604         if (res != BT_STATUS_SUCCESS) {
605                 BT_ERR("device_disconnect error: [%s]", status2string(res));
606                 return convert_to_oal_status(res);
607         }
608
609         return OAL_STATUS_SUCCESS;
610 }
611
612 void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
613                 int num_properties, bt_property_t *properties)
614 {
615         oal_event_t event;
616         gpointer event_data = NULL;
617         remote_device_t *dev_info;
618         ble_adv_data_t adv_info;
619         gsize size = 0;
620         bdstr_t bdstr;
621
622         BT_DBG("[%s]status: [%d] num properties [%d]", bdt_bd2str((bt_address_t*)bd_addr, &bdstr),
623                         status, num_properties);
624
625         dev_info = g_new0(remote_device_t, 1);
626         memcpy(dev_info->address.addr, bd_addr->address, 6);
627         parse_device_properties(num_properties, properties, dev_info, &adv_info);
628
629         if (num_properties == 0) {
630                 BT_ERR("!!Unexpected!! num properties is 0 status [%d]", status);
631                 /* It is possible that app called get bonded device info for a device
632                    which is not yet bonded or udner bonding, in such case, stack will return no properties.
633                    It is also possible that after bonding is done, BT MW attempted to fetch
634                    bonded device info, but due to internal stack error, 0 properties with status FAIL
635                    are received from stack. In such cases, simply send the event to BT MW
636                    and let it handle this event */
637                 event_dev_properties_t *dev_props_event = g_new0(event_dev_properties_t, 1);
638                 memcpy(&dev_props_event->device_info,
639                                 dev_info, sizeof(remote_device_t));
640                 event_data = dev_props_event;
641                 event = OAL_EVENT_DEVICE_PROPERTIES;
642                 size = sizeof(event_dev_properties_t);
643                 g_free(dev_info);
644         } else if (num_properties == 1) {
645                 /* For one particular property a dedicated event to be sent */
646                 switch (properties[0].type) {
647                 case BT_PROPERTY_BDNAME:
648                         event = OAL_EVENT_DEVICE_NAME;
649                         event_data = dev_info;
650                         send_event_trace(event, event_data, sizeof(remote_device_t),
651                                 (bt_address_t*)bd_addr, "Name: %s", dev_info->name);
652                         return;
653                 case BT_PROPERTY_UUIDS: {
654                         event_dev_services_t *services_info;
655                         bt_uuid_t *uuids = (bt_uuid_t *) properties[0].val;
656                         BT_INFO("Properties len [%d] event structure size [%zu]", properties[0].len, sizeof(event_dev_services_t));
657
658                         services_info = g_malloc(sizeof(event_dev_services_t) + properties[0].len);
659                         services_info->address = dev_info->address;
660                         memcpy(services_info->service_list, uuids, properties[0].len);
661                         services_info->num = properties[0].len/sizeof(bt_uuid_t);
662                         BT_INFO("Number of UUID [%d]", services_info->num);
663                         event = OAL_EVENT_DEVICE_SERVICES;
664                         event_data = services_info;
665                         size = sizeof(event_dev_services_t) + properties[0].len;
666                         g_free(dev_info);
667                         break;
668                 }
669                 default:
670                         BT_ERR("Single Property [%d] not handled", properties[0].type);
671                         g_free(dev_info);
672                         return;
673                 }
674         } else {
675                 event_dev_properties_t *dev_props_event = g_new0(event_dev_properties_t, 1);
676                 if (dev_info->type != DEV_TYPE_BREDR) {
677                         int i;
678
679                         BT_INFO("BLE Device");
680                         /* BLE Single or DUAL mode found, so it should have Adv data */
681                         dev_props_event->adv_len = adv_info.len;
682                         if (dev_props_event->adv_len > 0)
683                                 memcpy(dev_props_event->adv_data,
684                                         adv_info.adv_data, adv_info.len);
685
686                         for (i = 0; i < dev_props_event->adv_len; i++)
687                                 BT_INFO("Adv Data[%d] = [0x%x]",
688                                         i, dev_props_event->adv_data[i]);
689                         memcpy(&dev_props_event->device_info,
690                                 dev_info, sizeof(remote_device_t));
691                 } else {
692                         BT_INFO("BREDR type Device");
693                         memcpy(&dev_props_event->device_info,
694                                 dev_info, sizeof(remote_device_t));
695                 }
696
697                 event_data = dev_props_event;
698                 event = OAL_EVENT_DEVICE_PROPERTIES;
699                 size = sizeof(event_dev_properties_t);
700                 g_free(dev_info);
701         }
702
703         send_event_bda_trace(event, event_data, size, (bt_address_t*)bd_addr);
704 }
705
706 void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
707                 bt_bond_state_t state)
708 {
709         bt_address_t * address = g_new0(bt_address_t, 1);
710         oal_event_t event;
711         gsize size = 0;
712
713         BT_DBG("status: %d, state: %d", status, state);
714
715         memcpy(address->addr, bd_addr->address, 6);
716
717         switch (state) {
718         case BT_BOND_STATE_BONDED:
719                 event = OAL_EVENT_DEVICE_BONDING_SUCCESS;
720                 break;
721         case BT_BOND_STATE_NONE:
722                 /* Reaches both when bonding removed or bonding cancelled */
723                 if (BT_STATUS_SUCCESS != status) {
724                         event_dev_bond_failed_t * bond_fail_info = g_new0(event_dev_bond_failed_t, 1);
725                         bond_fail_info->status = convert_to_oal_status(status);
726                         bond_fail_info->address = *address;
727                         size = sizeof(event_dev_bond_failed_t);
728                         send_event_bda_trace(OAL_EVENT_DEVICE_BONDING_FAILED, bond_fail_info, size, (bt_address_t*)bd_addr);
729                         g_free(address);
730                         return;
731                 } else
732                         event = OAL_EVENT_DEVICE_BONDING_REMOVED;
733                 break;
734         case BT_BOND_STATE_BONDING:
735                 g_free(address);
736                 return;
737         default:
738                 BT_ERR("Unexpected Bond state %d", state);
739                 g_free(address);
740                 return;
741         }
742         send_event_bda_trace(event, address, sizeof(bt_address_t), (bt_address_t*)bd_addr);
743 }
744
745 void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
746                 bt_acl_state_t state)
747 {
748         event_dev_conn_status_t * conn_status = g_new0(event_dev_conn_status_t, 1);
749         //bt_address_t * address = g_new0(bt_address_t, 1);
750         oal_event_t event;
751         gsize size = 0;
752
753         BT_DBG("ACL State:%d, state: %d", status, state);
754
755         memcpy(conn_status->address.addr, bd_addr->address, 6);
756
757 #ifdef TIZEN_BT_HAL
758         conn_status->status = convert_to_oal_status(status);
759         conn_status->rssi = dbfw_rssi;
760 #else
761         if (BT_STATUS_SUCCESS != status) {
762                 /* At present only timeout will cause non-success status, later we can add more */
763                 conn_status->status = OAL_STATUS_CONN_TIMEOUT;
764                 BT_ERR("ACL State Error:%d, state: %d", status, state);
765         } else
766                 conn_status->status = OAL_STATUS_SUCCESS;
767
768         memcpy(conn_status->address.addr, bd_addr->address, 6);
769 #endif
770
771         BT_INFO("ACL STATE :%d, conn_status->status :%d, BT_ACL_STATE: %d", status, conn_status->status, state);
772
773         switch (state) {
774         case BT_ACL_STATE_CONNECTED:
775                 event = OAL_EVENT_DEVICE_ACL_CONNECTED;
776                 conn_status->status = OAL_STATUS_SUCCESS;
777                 break;
778         case BT_ACL_STATE_DISCONNECTED:
779                 event = OAL_EVENT_DEVICE_ACL_DISCONNECTED;
780                 break;
781         default:
782                 BT_ERR("Unexpected ACL state %d", state);
783                 g_free(conn_status);
784                 return;
785         }
786
787         size = sizeof(event_dev_conn_status_t);
788         send_event_bda_trace(event, conn_status, size, (bt_address_t*)bd_addr);
789 }
790
791 #ifdef TIZEN_BT_HAL
792 void cb_device_le_conn_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
793                 bt_le_conn_state_t state)
794 {
795         event_dev_conn_status_t * conn_status = g_new0(event_dev_conn_status_t, 1);
796         //bt_address_t * address = g_new0(bt_address_t, 1);
797         oal_event_t event;
798         gsize size = 0;
799
800         BT_DBG("LE conn status:%d, state: %d", status, state);
801         memcpy(conn_status->address.addr, bd_addr->address, 6);
802         conn_status->status = convert_to_oal_status(status);
803         conn_status->rssi = dbfw_rssi;
804
805         switch (state) {
806         case BT_LE_CONN_STATE_CONNECTED:
807                 event = OAL_EVENT_DEVICE_LE_CONNECTED;
808                 break;
809         case BT_LE_CONN_STATE_DISCONNECTED:
810                 event = OAL_EVENT_DEVICE_LE_DISCONNECTED;
811                 break;
812         default:
813                 BT_ERR("Unexpected ACL state %d", state);
814                 g_free(conn_status);
815                 return;
816         }
817
818         size = sizeof(event_dev_conn_status_t);
819         send_event_bda_trace(event, conn_status, size, (bt_address_t*)bd_addr);
820 }
821 #endif
822
823 void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class)
824 {
825         remote_device_t * dev_info = g_new0(remote_device_t, 1);
826         gsize size = 0;
827         BT_DBG("+");
828
829         memcpy(dev_info->address.addr, bd_addr->address, 6);
830         g_strlcpy(dev_info->name, (const gchar *)bdname->name, BT_DEVICE_NAME_LENGTH_MAX);
831         dev_info->cod = device_class;
832         size = sizeof(remote_device_t);
833
834         send_event_bda_trace(OAL_EVENT_DEVICE_PIN_REQUEST, dev_info, size, (bt_address_t*)bd_addr);
835 }
836
837 void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
838                 bt_ssp_variant_t pairing_variant, uint32_t pass_key)
839 {
840         oal_event_t event;
841         gpointer event_data = NULL;
842         gsize size = 0;
843         BT_DBG("+");
844
845         switch (pairing_variant) {
846         case BT_SSP_VARIANT_PASSKEY_ENTRY:
847         {
848                 remote_device_t * dev_info = g_new0(remote_device_t, 1);
849                 memcpy(dev_info->address.addr, bd_addr->address, 6);
850                         g_strlcpy(dev_info->name, (const gchar *)bdname->name, BT_DEVICE_NAME_LENGTH_MAX);
851                 dev_info->cod = device_class;
852                 event = OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST;
853                 event_data = dev_info;
854                 size = sizeof(remote_device_t);
855                 break;
856         }
857         case BT_SSP_VARIANT_PASSKEY_NOTIFICATION:
858         {
859                 event_dev_passkey_t * passkey_data = g_new0(event_dev_passkey_t, 1);
860
861                 memcpy(passkey_data->device_info.address.addr, bd_addr->address, 6);
862                         g_strlcpy(passkey_data->device_info.name, (const gchar *)bdname->name, BT_DEVICE_NAME_LENGTH_MAX);
863                 passkey_data->device_info.cod = device_class;
864                 passkey_data->pass_key = pass_key;
865                 event = OAL_EVENT_DEVICE_PASSKEY_DISPLAY;
866                 event_data = passkey_data;
867                 size = sizeof(event_dev_passkey_t);
868                 break;
869         }
870         case BT_SSP_VARIANT_PASSKEY_CONFIRMATION:
871         {
872                 event_dev_passkey_t * passkey_data = g_new0(event_dev_passkey_t, 1);
873
874                 memcpy(passkey_data->device_info.address.addr, bd_addr->address, 6);
875                                 g_strlcpy(passkey_data->device_info.name, (const gchar *)bdname->name, BT_DEVICE_NAME_LENGTH_MAX);
876                 passkey_data->device_info.cod = device_class;
877                 passkey_data->pass_key = pass_key;
878                 event = OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST;
879                 event_data = passkey_data;
880                 size = sizeof(event_dev_passkey_t);
881                 break;
882         }
883         case BT_SSP_VARIANT_CONSENT:
884         {
885                 remote_device_t * dev_info = g_new0(remote_device_t, 1);
886
887                 memcpy(dev_info->address.addr, bd_addr->address, 6);
888                         g_strlcpy(dev_info->name, (const gchar *)bdname->name, BT_DEVICE_NAME_LENGTH_MAX);
889                 dev_info->cod = device_class;
890                 event = OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST;
891                 event_data = dev_info;
892                 size = sizeof(remote_device_t);
893                 break;
894         }
895         default:
896         {
897                 BT_ERR("Unhandled SSP request [%d]", pairing_variant);
898                 return;
899         }
900         }
901         send_event_bda_trace(event, event_data, size, (bt_address_t*)bd_addr);
902 }
903
904 void cb_device_authorize_request(bt_bdaddr_t *bd_addr, bt_service_id_t service_d)
905 {
906         event_dev_authorize_req_t * auth_req = g_new0(event_dev_authorize_req_t, 1);
907
908         BT_INFO("service_d: %d", service_d);
909         memcpy(auth_req->address.addr, bd_addr->address, 6);
910         auth_req->service_id = service_d;
911
912         send_event_bda_trace(OAL_EVENT_DEVICE_AUTHORIZE_REQUEST, auth_req, sizeof(event_dev_authorize_req_t), (bt_address_t*)bd_addr);
913 }
914
915 void cb_device_trust_state_changed(bt_bdaddr_t *bd_addr, bt_device_trust_state_t trusted)
916 {
917         oal_event_t event;
918         event_dev_trust_t * trust_val = g_new0(event_dev_trust_t, 1);
919
920         if (trusted == BT_DEVICE_TRUSTED) {
921                 BT_INFO("Device is Trusted");
922                 event = OAL_EVENT_DEVICE_TRUSTED;
923         } else {
924                 BT_INFO("Device is Un Trusted");
925                 event = OAL_EVENT_DEVICE_UNTRUSTED;
926         }
927         memcpy(trust_val->address.addr, bd_addr->address, 6);
928         trust_val->status = OAL_STATUS_SUCCESS;
929         send_event_bda_trace(event, trust_val, sizeof(event_dev_trust_t), (bt_address_t*)bd_addr);
930 }
931
932 #ifdef TIZEN_BT_HAL
933 void cb_socket_conn_authorize_request(bt_bdaddr_t *bd_addr, bt_uuid_t *uuid, uint8_t *name, uint8_t *path, uint32_t fd)
934 {
935         event_socket_authorize_req_t *auth_req = g_new0(event_socket_authorize_req_t, 1);
936
937         memcpy(auth_req->address.addr, bd_addr->address, 6);
938         memcpy(auth_req->uuid.uuid, uuid->uu, 16);
939         memcpy(auth_req->name, name, sizeof(auth_req->name) - 1);
940         memcpy(auth_req->path, path, sizeof(auth_req->path) - 1);
941         auth_req->fd = fd;
942
943         send_event_bda_trace(OAL_EVENT_SOCKET_AUTHORIZE_REQUEST, auth_req, sizeof(event_socket_authorize_req_t), (bt_address_t*)bd_addr);
944 }
945
946 void cb_device_trusted_profiles_changed(bt_bdaddr_t *bd_addr, uint32_t trust_val)
947 {
948         event_device_trusted_profiles_t *ev = g_new0(event_device_trusted_profiles_t, 1);
949
950         BT_DBG("+");
951
952         memcpy(ev->address.addr, bd_addr->address, 6);
953         ev->trust_val = trust_val;
954         send_event_bda_trace(OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED,
955                         ev, sizeof(event_device_trusted_profiles_t), (bt_address_t*)bd_addr);
956         BT_DBG("-");
957 }
958
959 void cb_rssi_monitor_state_changed(bt_bdaddr_t *bd_addr, int32_t link_type, uint8_t state)
960 {
961         event_dev_rssi_info_t *ev = g_new0(event_dev_rssi_info_t, 1);
962         oal_event_t event = OAL_EVENT_RSSI_MONITORING_ENABLED;
963
964         BT_DBG("+");
965
966         memcpy(ev->address.addr, bd_addr->address, 6);
967         ev->link_type = link_type;
968         if (0 == state)
969                 event = OAL_EVENT_RSSI_MONITORING_DISABLED;
970
971         send_event_bda_trace(event, ev, sizeof(event_dev_rssi_info_t), (bt_address_t*)bd_addr);
972
973         BT_DBG("-");
974 }
975
976 void cb_rssi_alert(bt_bdaddr_t *bd_addr, int32_t link_type, int32_t alert_type, int32_t rssi)
977 {
978         event_dev_rssi_info_t *ev = g_new0(event_dev_rssi_info_t, 1);
979
980         BT_DBG("+");
981
982         memcpy(ev->address.addr, bd_addr->address, 6);
983         ev->link_type = link_type;
984         ev->alert_type = alert_type;
985         ev->rssi = rssi;
986
987         send_event_bda_trace(OAL_EVENT_RSSI_ALERT_RECEIVED,
988                         ev, sizeof(event_dev_rssi_info_t), (bt_address_t*)bd_addr);
989         BT_DBG("-");
990 }
991
992 void cb_raw_rssi_received(bt_bdaddr_t *bd_addr, int32_t link_type, int32_t rssi)
993 {
994         event_dev_rssi_info_t *ev = g_new0(event_dev_rssi_info_t, 1);
995
996         BT_DBG("+");
997
998         memcpy(ev->address.addr, bd_addr->address, 6);
999         ev->link_type = link_type;
1000         ev->rssi = rssi;
1001
1002         send_event_bda_trace(OAL_EVENT_RAW_RSSI_RECEIVED,
1003                         ev, sizeof(event_dev_rssi_info_t), (bt_address_t*)bd_addr);
1004         BT_DBG("-");
1005 }
1006
1007 static gboolean __device_init_dbfw_rssi_cb(gpointer user_data)
1008 {
1009         dbfw_rssi = 0;
1010         return FALSE;
1011 }
1012
1013 static void __device_parse_dbfw_set_rssi(unsigned char *data, uint32_t length)
1014 {
1015         ret_if(length < 5);
1016         dbfw_rssi = 0xFFFFFF00 | data[4];
1017         BT_INFO("dbfw_rssi = %d", dbfw_rssi);
1018         g_timeout_add(1000, __device_init_dbfw_rssi_cb, NULL);
1019 }
1020
1021 void cb_dbfw_plus_info_received(unsigned char *data, uint32_t length, uint8_t event_code)
1022 {
1023         char evt_str[18];
1024         char *data_str = NULL;
1025         int i;
1026
1027         ret_if(data == NULL);
1028
1029         data_str = g_malloc0(length * 2 + 1);
1030         for (i = 0; i < length; i++)
1031                 snprintf(&data_str[i * 2], 3, "%02X", data[i]);
1032         data_str[i * 2] = '\0';
1033
1034         switch(event_code) {
1035                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO:
1036                         BT_DBG("## Event Code: Linkloss Debug Info");
1037                         snprintf(evt_str, 18, "[DBFW]Linkloss(D)");
1038                         __device_parse_dbfw_set_rssi(data, length);
1039                         break;
1040                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO:
1041                         BT_DBG("## Event Code: Linkloss Clock Info");
1042                         snprintf(evt_str, 18, "[DBFW]Linkloss(C)");
1043                         break;
1044                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS:
1045                         BT_DBG("## Event Code: Linkloss Page slots");
1046                         snprintf(evt_str, 18, "[DBFW]Linkloss(P)");
1047                         break;
1048                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE:
1049                         BT_DBG("## Event Code: Linkloss LMP trace");
1050                         snprintf(evt_str, 18, "[DBFW]Linkloss(L)");
1051                         break;
1052                 case BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO:
1053                         BT_DBG("## Event Code: A2DP Info");
1054                         snprintf(evt_str, 18, "[DBFW]A2DP");
1055                         break;
1056                 case BT_DBFW_PLUS_EVENT_CODE_HFP_INFO:
1057                         BT_DBG("## Event Code: HFP Info");
1058                         snprintf(evt_str, 18, "[DBFW]HFP");
1059                         break;
1060                 case BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO:
1061                         BT_DBG("## Event Code: HFP SCO Packet Type");
1062                         snprintf(evt_str, 18, "[DBFW]SCO");
1063                         break;
1064                 default:
1065                         BT_DBG("## Unknown event code (0x%02x)", event_code);
1066                         snprintf(evt_str, 18, "[DBFW]0x%02X", event_code);
1067                         break;
1068         }
1069
1070         g_free(data_str);
1071 }
1072
1073 #endif