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