[BT-frwk] Added Set/Get Trusted profile APIs
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-bluetooth.c
1 /*
2  * BLUETOOTH HAL
3  *
4  * Copyright (c) 2015 -2016 Samsung Electronics Co., Ltd All Rights Reserved.
5  *
6  * Contact: Anupam Roy <anupam.r@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include <dlog.h>
27
28 #include "bt-hal.h"
29 #include "bt-hal-log.h"
30 #include "bt-hal-msg.h"
31 #include "bt-hal-utils.h"
32
33 #include <bt-hal-adapter-dbus-handler.h>
34 #include <bt-hal-device-dbus-handler.h>
35 #include <bt-hal-hidhost.h>
36 #include <bt-hal-socket.h>
37 #include <bt-hal-av.h>
38 #include <bt-hal-hf.h>
39 #include <bt-hal-hdp.h>
40 #include <bt-hal-a2dp-sink.h>
41 #include <bt-hal-avrcp-tg.h>
42 #include <bt-hal-avrcp-ctrl.h>
43 #include <bt-hal-gatt.h>
44 #ifdef TIZEN_BT_HAL
45 #include <bt-hal-adapter-le.h>
46 #endif
47
48 #define enum_prop_to_hal(prop, hal_prop, type) do { \
49         static type e; \
50         prop.val = &e; \
51         prop.len = sizeof(e); \
52         e = *((uint8_t *) (hal_prop->val)); \
53 } while (0)
54
55 static const bt_callbacks_t *bt_hal_cbacks = NULL;
56
57
58 /* Forward declarations */
59 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop, uint8_t num_props, uint16_t len);
60 static void __bt_device_props_to_hal(bt_property_t *send_props,
61                 struct hal_property *prop, uint8_t num_props,
62                 uint16_t len);
63 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len);
64 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len);
65 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len);
66 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len);
67 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len);
68 static void __bt_hal_handle_bond_state_changed_event(void *buf, uint16_t len);
69 static void __bt_hal_handle_device_acl_state_changed_event(void *buf, uint16_t len);
70 static void __bt_hal_handle_ssp_request_event(void *buf, uint16_t len);
71 static void __bt_hal_handle_pin_request_event(void *buf, uint16_t len);
72
73 static void __bt_hal_handle_device_trust_state_changed_event(void *buf, uint16_t len);
74
75 static bool interface_ready(void)
76 {
77         return bt_hal_cbacks != NULL;
78 }
79
80 static int init(bt_callbacks_t *callbacks)
81 {
82         int ret;
83         DBG("HAL library Initialization..");
84
85         if (interface_ready())
86                 return BT_STATUS_DONE;
87         else {
88                 bt_hal_cbacks = callbacks;
89                 DBG("Store HAL stack msg handler callback");
90                 _bt_hal_dbus_store_stack_msg_cb(__bt_hal_handle_stack_messages);
91                 ret = _bt_hal_initialize_event_receiver(__bt_hal_handle_stack_messages);
92
93                 if (ret == BT_STATUS_SUCCESS)
94                         return BT_STATUS_SUCCESS;
95                 else
96                         return BT_STATUS_FAIL;
97
98         }
99         return BT_STATUS_SUCCESS;
100 }
101
102 /* Enable Adapter */
103 static int enable(void)
104 {
105         return _bt_hal_dbus_enable_adapter();
106 }
107
108 /* Disable Adapter */
109 static int disable(void)
110 {
111         return _bt_hal_dbus_disable_adapter();
112 }
113
114 #ifdef  TIZEN_BT_HAL
115 /* Tizen Specific Reset Adapter */
116 static int reset(void)
117 {
118         return _bt_hal_dbus_reset_adapter();
119 }
120 #endif
121
122 static void cleanup(void)
123 {
124         return;
125 }
126
127 #ifdef  TIZEN_BT_HAL
128 /* Tizen Specific Get Powered state */
129 static int get_adapter_powered_status(uint8_t *state)
130 {
131         if (!state) {
132                 ERR("Invalid param");
133                 return BT_STATUS_PARM_INVALID;
134         }
135
136         return _bt_hal_get_adapter_powered_state(state);
137 }
138 #endif
139
140 static int get_adapter_properties(void)
141 {
142         return _bt_hal_dbus_get_adapter_properties();
143 }
144
145 static int get_adapter_property(bt_property_type_t type)
146 {
147         return _bt_hal_dbus_get_adapter_property(type);
148 }
149
150 static int set_adapter_property(const bt_property_t *property)
151 {
152         if (!property) {
153                 ERR("Invalid param");
154                 return BT_STATUS_PARM_INVALID;
155         }
156
157         return _bt_hal_dbus_set_adapter_property(property);
158 }
159
160 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)
161 {
162         if (!remote_addr) {
163                 ERR("Invalid param");
164                 return BT_STATUS_PARM_INVALID;
165         }
166
167         return _bt_hal_dbus_get_remote_device_properties(remote_addr);
168 }
169
170 static int get_remote_device_property(bt_bdaddr_t *remote_addr,
171                 bt_property_type_t type)
172 {
173         return BT_STATUS_UNSUPPORTED;
174 }
175
176 static int set_remote_device_property(bt_bdaddr_t *remote_addr,
177                 const bt_property_t *property)
178 {
179         return _bt_hal_dbus_set_remote_device_property(remote_addr, property);
180 }
181
182 static int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
183 {
184         return BT_STATUS_UNSUPPORTED;
185 }
186
187 static int get_remote_services(bt_bdaddr_t *remote_addr)
188 {
189         if (!remote_addr) {
190                 ERR("Invalid param");
191                 return BT_STATUS_PARM_INVALID;
192         }
193         return _bt_hal_dbus_get_remote_device_services(remote_addr);
194 }
195
196 static int start_discovery(void)
197 {
198         return _bt_hal_dbus_start_discovery(false, 0x00);
199 }
200
201 #ifdef  TIZEN_BT_HAL
202 static int start_custom_discovery(bt_disc_role_type_t disc_type)
203 {
204         return _bt_hal_dbus_start_discovery(true, disc_type);
205 }
206 #endif
207
208 static int cancel_discovery(void)
209 {
210         return _bt_hal_dbus_stop_discovery();
211 }
212
213 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
214 {
215         DBG("+");
216         return _bt_hal_device_create_bond(bd_addr, transport);
217 }
218
219 static int cancel_bond(const bt_bdaddr_t *bd_addr)
220 {
221         return _bt_hal_device_cancel_bond(bd_addr);
222 }
223
224 static int remove_bond(const bt_bdaddr_t *bd_addr)
225 {
226         DBG("+");
227         return _bt_hal_device_remove_bond(bd_addr);
228 }
229
230 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
231                 uint8_t pin_len, bt_pin_code_t *pin_code)
232 {
233         return _bt_hal_device_legacy_pin_reply(bd_addr, accept, pin_len, (char*)pin_code);
234 }
235
236 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
237                 uint8_t accept, uint32_t passkey)
238 {
239         return _bt_hal_device_ssp_reply(bd_addr, variant, accept, passkey);
240 }
241
242 static const void *get_profile_interface(const char *profile_id)
243 {
244         /*TODO: Profile interfaces to be included later*/
245         DBG("%s", profile_id);
246
247         if (!interface_ready())
248                 return NULL;
249
250         if (!strncmp(profile_id, BT_PROFILE_HIDHOST_ID, strlen(profile_id)))
251                 return bt_get_hidhost_interface();
252
253         if (!strcmp(profile_id, BT_PROFILE_SOCKETS_ID))
254                 return bt_get_socket_interface();
255
256         if (!strcmp(profile_id, BT_PROFILE_PAN_ID))
257                 return NULL;
258
259         if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
260                 return bt_get_av_interface();
261
262         if (!strcmp(profile_id, BT_PROFILE_AV_RC_ID))
263                 return bt_get_avrcp_tg_interface();
264
265         if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_ID))
266                 return bt_get_hf_interface();
267
268         if (!strcmp(profile_id, BT_PROFILE_GATT_ID))
269                 return bt_get_gatt_interface();
270
271         if (!strcmp(profile_id, BT_PROFILE_HEALTH_ID))
272                 return bt_get_hl_interface();
273
274         if (!strcmp(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
275                 return bt_get_avrcp_ctrl_interface();
276
277         if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
278                 return NULL;
279
280         if (!strcmp(profile_id, BT_PROFILE_MAP_CLIENT_ID))
281                 return NULL;
282
283         if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
284                 return bt_get_a2dp_sink_interface();
285
286         return NULL;
287 }
288
289 static int dut_mode_configure(uint8_t enable)
290 {
291         return BT_STATUS_UNSUPPORTED;
292 }
293
294 static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
295 {
296         return BT_STATUS_UNSUPPORTED;
297 }
298
299 static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
300 {
301         return BT_STATUS_UNSUPPORTED;
302 }
303
304 static int config_hci_snoop_log(uint8_t enable)
305 {
306         return BT_STATUS_UNSUPPORTED;
307 }
308
309 static int get_connection_state(const bt_bdaddr_t *bd_addr)
310 {
311         DBG("");
312         return _bt_hal_device_get_connection_state(bd_addr);
313 }
314
315 static int set_os_callouts(bt_os_callouts_t *callouts)
316 {
317         DBG("callouts: %p", callouts);
318
319         /* TODO: implement */
320
321         return BT_STATUS_UNSUPPORTED;
322 }
323
324 static int read_energy_info(void)
325 {
326         return BT_STATUS_UNSUPPORTED;
327 }
328
329 static int authorize_response(const bt_bdaddr_t *bd_addr, bt_service_id_t service_id,
330                                     uint8_t authorize, uint8_t save_settings)
331 {
332         DBG("+");
333         return _bt_hal_device_authorize_response(bd_addr, service_id, authorize, save_settings);
334 }
335
336 int set_authorization(bt_bdaddr_t *bd_addr, uint8_t auth)
337 {
338         DBG("+");
339         return _bt_hal_device_set_trust(bd_addr, auth);
340 }
341
342 #ifdef TIZEN_BT_HAL
343 static int get_service_connection_state(const bt_bdaddr_t *bd_addr, bt_service_id_t rem_svc_id)
344 {
345         DBG("");
346         return _bt_hal_device_get_service_connection_state(bd_addr, rem_svc_id);
347 }
348
349 static int set_agent_osp_server(uint32_t type, uint8_t enable)
350 {
351         DBG("");
352         return _bt_hal_device_set_osp_server(type, enable);
353 }
354
355 static int set_trusted_profile(bt_bdaddr_t *bd_addr, bt_trusted_profile_t profile, uint8_t trust)
356 {
357         DBG("");
358         return _bt_hal_device_set_trusted_profile(bd_addr, profile, trust);
359 }
360
361 static int get_trusted_profile(bt_bdaddr_t *bd_addr, bt_trusted_profile_t profile, uint32_t *trusted)
362 {
363         DBG("");
364         return _bt_hal_device_get_trusted_profile(bd_addr, profile, trusted);
365 }
366 #endif
367
368 static const bt_interface_t bluetooth_if = {
369         .size = sizeof(bt_interface_t),
370         .init = init,
371         .enable = enable,
372         .disable = disable,
373 #ifdef TIZEN_BT_HAL
374         .reset = reset,
375 #endif
376
377 #ifdef TIZEN_BT_HAL
378         .le_enable = _bt_hal_le_enable,
379         .le_disable = _bt_hal_le_disable,
380 #endif
381         .cleanup = cleanup,
382 #ifdef TIZEN_BT_HAL
383         .get_adapter_powered_status = get_adapter_powered_status,
384 #endif
385         .get_adapter_properties = get_adapter_properties,
386         .get_adapter_property = get_adapter_property,
387         .set_adapter_property = set_adapter_property,
388         .get_remote_device_properties = get_remote_device_properties,
389         .get_remote_device_property = get_remote_device_property,
390         .set_remote_device_property = set_remote_device_property,
391         .get_remote_service_record = get_remote_service_record,
392         .get_remote_services = get_remote_services,
393         .start_discovery = start_discovery,
394 #ifdef TIZEN_BT_HAL
395         .start_custom_discovery = start_custom_discovery,
396 #endif
397         .cancel_discovery = cancel_discovery,
398         .create_bond = create_bond,
399         .remove_bond = remove_bond,
400         .cancel_bond = cancel_bond,
401         .pin_reply = pin_reply,
402         .ssp_reply = ssp_reply,
403         .get_profile_interface = get_profile_interface,
404         .dut_mode_configure = dut_mode_configure,
405         .dut_mode_send = dut_mode_send,
406         .le_test_mode = le_test_mode,
407         .config_hci_snoop_log = config_hci_snoop_log,
408         .get_connection_state = get_connection_state,
409         .set_os_callouts = set_os_callouts,
410         .read_energy_info = read_energy_info,
411         /* Tizen Specific API's*/
412         .authorize_response = authorize_response,
413         .set_authorization = set_authorization,
414 #ifdef TIZEN_BT_HAL
415         .get_service_connection_state = get_service_connection_state,
416         .set_agent_osp_server = set_agent_osp_server,
417         .set_trusted_profile = set_trusted_profile,
418         .get_trusted_profile = get_trusted_profile,
419 #endif
420 };
421
422 static const bt_interface_t *get_bluetooth_interface(void)
423 {
424         DBG("");
425         return &bluetooth_if;
426 }
427
428 static int close_bluetooth(struct hw_device_t *device)
429 {
430         DBG("");
431         cleanup();
432         free(device);
433         return 0;
434 }
435
436 static int open_bluetooth(const struct hw_module_t *module, char const *name,
437                 struct hw_device_t **device)
438 {
439         bluetooth_device_t *dev = malloc(sizeof(bluetooth_device_t));
440
441         DBG("");
442
443         memset(dev, 0, sizeof(bluetooth_device_t));
444         dev->common.tag = HARDWARE_DEVICE_TAG;
445         dev->common.version = 0;
446         dev->common.module = (struct hw_module_t *) module;
447         dev->common.close = close_bluetooth;
448         dev->get_bluetooth_interface = get_bluetooth_interface;
449
450         *device = (struct hw_device_t *) dev;
451
452         return 0;
453 }
454
455 static struct hw_module_methods_t bluetooth_module_methods = {
456         .open = open_bluetooth,
457 };
458
459 struct hw_module_t HAL_MODULE_INFO_SYM = {
460         .tag = HARDWARE_MODULE_TAG,
461         .version_major = 1,
462         .version_minor = 0,
463         .id = BT_HARDWARE_MODULE_ID,
464         .name = "Bluetooth stack",
465         .author = "Intel Corporation",
466         .methods = &bluetooth_module_methods
467 };
468
469 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len)
470 {
471         struct hal_ev_adapter_state_changed *ev = buf;
472
473         DBG("Adapter State: %d", ev->state);
474
475         if (bt_hal_cbacks->adapter_state_changed_cb)
476                 bt_hal_cbacks->adapter_state_changed_cb(ev->state);
477 }
478
479 #ifdef TIZEN_BT_HAL
480 static void __bt_hal_handle_le_state_changed(void *buf, uint16_t len)
481 {
482         struct hal_ev_le_state_changed *ev = buf;
483
484         DBG("Adapter State: %d", ev->state);
485
486         if (bt_hal_cbacks->le_state_changed_cb)
487                 bt_hal_cbacks->le_state_changed_cb(ev->state);
488 }
489 #endif
490
491 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop,
492                 uint8_t num_props, uint16_t len)
493 {
494         void *buf = prop;
495         uint8_t i;
496
497         for (i = 0; i < num_props; i++) {
498                 if (sizeof(*prop) + prop->len > len) {
499                         ERR("invalid adapter properties(%zu > %u), cant process further properties!!!",
500                                         sizeof(*prop) + prop->len, len);
501                         return;
502                 }
503
504                 send_props[i].type = prop->type;
505
506                 switch (prop->type) {
507                         /* TODO: Add Adapter Properties */
508                         default:
509                                 send_props[i].len = prop->len;
510                                 send_props[i].val = prop->val;
511                                 break;
512                 }
513
514                 DBG("prop[%d]: %s", i, btproperty2str(&send_props[i]));
515
516                 len -= sizeof(*prop) + prop->len;
517                 buf += sizeof(*prop) + prop->len;
518                 prop = buf;
519         }
520
521         if (!len)
522                 return;
523 }
524
525 static void __bt_device_props_to_hal(bt_property_t *send_props,
526                 struct hal_property *prop, uint8_t num_props,
527                 uint16_t len)
528 {
529         void *buf = prop;
530         uint8_t i;
531
532         DBG("+");
533
534         for (i = 0; i < num_props; i++) {
535
536                 if (sizeof(*prop) + prop->len > len) {
537                         ERR("invalid device properties (%zu > %u), cant process further properties!!!",
538                                         sizeof(*prop) + prop->len, len);
539                         return;
540                 }
541
542                 send_props[i].type = prop->type;
543
544                 DBG("HAL prop Type [%d]", prop->type);
545
546                 switch (prop->type) {
547                 case HAL_PROP_DEVICE_TYPE:
548                 {
549                         DBG("Device property:HAL_PROP_DEVICE_TYPE:");
550                         enum_prop_to_hal(send_props[i], prop,
551                                         bt_device_type_t);
552                         break;
553                 }
554                 case HAL_PROP_DEVICE_VERSION_INFO:
555                 {
556                         DBG("Device property: HAL_PROP_DEVICE_VERSION_INFO");
557                         static bt_remote_version_t e;
558                         const struct hal_prop_device_info *p;
559                         send_props[i].val = &e;
560                         send_props[i].len = sizeof(e);
561                                 p = (struct hal_prop_device_info *) prop->val;
562                                 e.manufacturer = p->manufacturer;
563                         e.sub_ver = p->sub_version;
564                         e.version = p->version;
565                         break;
566                 }
567                 case HAL_PROP_DEVICE_SERVICE_REC:
568                 {
569                         DBG("Device property: HAL_PROP_DEVICE_SERVICE_REC");
570                         static bt_service_record_t e;
571                         const struct hal_prop_device_service_rec *p;
572                         send_props[i].val = &e;
573                         send_props[i].len = sizeof(e);
574                                 p = (struct hal_prop_device_service_rec *) prop->val;
575                                         memset(&e, 0, sizeof(e));
576                         memcpy(&e.channel, &p->channel, sizeof(e.channel));
577                         memcpy(e.uuid.uu, p->uuid, sizeof(e.uuid.uu));
578                         memcpy(e.name, p->name, p->name_len);
579                         break;
580                 }
581                 default:
582                         send_props[i].len = prop->len;
583                         send_props[i].val = prop->val;
584                         break;
585                 }
586
587                 DBG("prop[%d]: %s, len: %d", i, btproperty2str(&send_props[i]), prop->len);
588                 len -= sizeof(*prop) + prop->len;
589                 buf += sizeof(*prop) + prop->len;
590                 prop = buf;
591
592         }
593
594         if (!len) {
595                 DBG("-");
596                 return;
597         }
598
599         ERR("invalid device properties (%u bytes left), ", len);
600 }
601
602 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len)
603 {
604         struct hal_ev_adapter_props_changed *ev = (struct hal_ev_adapter_props_changed *)buf;
605         bt_property_t props[ev->num_props];
606         DBG("+");
607
608         if (!bt_hal_cbacks->adapter_properties_cb)
609                 return;
610
611         len -= sizeof(*ev);
612         __bt_adapter_props_to_hal(props, ev->props, ev->num_props, len);
613
614         if (bt_hal_cbacks->adapter_properties_cb)
615                 bt_hal_cbacks->adapter_properties_cb(ev->status, ev->num_props, props);
616 }
617
618 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len)
619 {
620         struct hal_ev_discovery_state_changed *ev = (struct hal_ev_discovery_state_changed *)buf;
621
622         DBG("+");
623
624         if (bt_hal_cbacks->discovery_state_changed_cb)
625                 bt_hal_cbacks->discovery_state_changed_cb(ev->state);
626 }
627
628 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len)
629 {
630         struct hal_ev_device_found *ev =  (struct hal_ev_device_found *) buf;
631         bt_property_t props[ev->num_props];
632         DBG("+");
633
634         if (!bt_hal_cbacks->device_found_cb)
635                 return;
636
637         len -= sizeof(*ev);
638         __bt_device_props_to_hal(props, ev->props, ev->num_props, len);
639
640         bt_hal_cbacks->device_found_cb(ev->num_props, props);
641 }
642
643 static void __bt_hal_handle_remote_device_properties_event(void *buf, uint16_t len)
644 {
645         struct hal_ev_remote_device_props *ev = (struct hal_ev_remote_device_props *) buf;
646         bt_bdaddr_t bd_addr;
647         bt_property_t props[ev->num_props];
648
649         DBG("+");
650
651         if (!bt_hal_cbacks->remote_device_properties_cb)
652                 return;
653
654         len -= sizeof(*ev);
655         __bt_device_props_to_hal(props, ev->props, ev->num_props, len);
656         memcpy(bd_addr.address, ev->bdaddr, 6);
657         bt_hal_cbacks->remote_device_properties_cb(
658                         ev->status, &bd_addr, ev->num_props, props);
659         DBG("-");
660 }
661
662 static void __bt_hal_handle_bond_state_changed_event(void *buf, uint16_t len)
663 {
664
665         struct hal_ev_bond_state_changed *ev = (struct hal_ev_bond_state_changed *)buf;
666         bt_bdaddr_t bd_addr;
667         DBG("+");
668
669         memcpy(bd_addr.address, ev->bdaddr, 6);
670         /* BD address*/
671         DBG("[0x%x]", bd_addr.address[0]);
672         DBG("[0x%x]", bd_addr.address[1]);
673         DBG("[0x%x]", bd_addr.address[2]);
674         DBG("[0x%x]", bd_addr.address[3]);
675         DBG("[0x%x]", bd_addr.address[4]);
676         DBG("[0x%x]", bd_addr.address[5]);
677
678         DBG("Bonding  State changed Status [0x%x]", ev->status);
679         DBG("Bonding  State [0x%x]", ev->state);
680
681         if (!bt_hal_cbacks->bond_state_changed_cb) {
682                 ERR("HAL User bond_state_changed_callback is not set!!");
683                 return;
684         }
685
686         bt_hal_cbacks->bond_state_changed_cb(ev->status, &bd_addr, ev->state);
687         DBG("-");
688 }
689
690 static void __bt_hal_handle_device_acl_state_changed_event(void *buf, uint16_t len)
691 {
692         struct hal_ev_acl_state_changed *ev = (struct hal_ev_acl_state_changed*)buf;
693         bt_bdaddr_t bd_addr;
694         DBG("+");
695
696         memcpy(bd_addr.address, ev->bdaddr, 6);
697         /* BD address*/
698         DBG("[0x%x]", bd_addr.address[0]);
699         DBG("[0x%x]", bd_addr.address[1]);
700         DBG("[0x%x]", bd_addr.address[2]);
701         DBG("[0x%x]", bd_addr.address[3]);
702         DBG("[0x%x]", bd_addr.address[4]);
703         DBG("[0x%x]", bd_addr.address[5]);
704
705         DBG("ACL Status [0x%x]", ev->status);
706         DBG("ACL State  [0x%x]", ev->state);
707
708         if (!bt_hal_cbacks->acl_state_changed_cb) {
709                 ERR("HAL User acl_state_changed_cb is not set!!");
710                 return;
711         }
712
713         bt_hal_cbacks->acl_state_changed_cb(ev->status, &bd_addr, ev->state);
714         DBG("-");
715 }
716
717 static void __bt_hal_handle_device_le_conn_state_changed_event(void *buf, uint16_t len)
718 {
719         struct hal_ev_acl_state_changed *ev = (struct hal_ev_acl_state_changed*)buf;
720         bt_bdaddr_t bd_addr;
721         DBG("+");
722
723         memcpy(bd_addr.address, ev->bdaddr, 6);
724         /* BD address */
725         DBG("Address: [%02X:%02X:%02X:%02X:%02X:%02X]", bd_addr.address[0],
726                         bd_addr.address[1], bd_addr.address[2], bd_addr.address[3],
727                         bd_addr.address[4], bd_addr.address[5]);
728         DBG("LE conn Status [0x%x]", ev->status);
729         DBG("LE conn State  [0x%x]", ev->state);
730
731 #ifdef TIZEN_BT_HAL
732         if (!bt_hal_cbacks->le_conn_state_changed_cb) {
733                 ERR("HAL User le_conn_state_changed_cb is not set!!");
734                 return;
735         }
736
737         bt_hal_cbacks->le_conn_state_changed_cb(ev->status, &bd_addr, ev->state);
738 #endif
739         DBG("-");
740 }
741
742 static void __bt_hal_handle_authorize_request_event(void *buf, uint16_t len)
743 {
744         struct hal_ev_authorize_request *ev = (struct hal_ev_authorize_request *)buf;
745         bt_bdaddr_t bd_addr;
746
747         DBG("+");
748
749         memcpy(bd_addr.address, ev->bdaddr, 6);
750
751         /* BD address*/
752         DBG("[0x%x]", bd_addr.address[0]);
753         DBG("[0x%x]", bd_addr.address[1]);
754         DBG("[0x%x]", bd_addr.address[2]);
755         DBG("[0x%x]", bd_addr.address[3]);
756         DBG("[0x%x]", bd_addr.address[4]);
757         DBG("[0x%x]", bd_addr.address[5]);
758
759         DBG("Service Id: [%u]", ev->service_id);
760
761         if (!bt_hal_cbacks->authorize_request_cb) {
762                 ERR("HAL User authorize_request_cb is not set!!");
763                 return;
764         }
765
766         bt_hal_cbacks->authorize_request_cb(&bd_addr, ev->service_id);
767         DBG("-");
768 }
769
770 #ifdef TIZEN_BT_HAL
771 static void __bt_hal_handle_socket_authorize_request_event(void *buf, uint16_t len)
772 {
773         struct hal_ev_sock_conn_auth *ev = buf;
774         bt_bdaddr_t bd_addr;
775         bt_uuid_t uuid;
776
777         DBG("+");
778
779         memcpy(bd_addr.address, ev->bdaddr, 6);
780         memcpy(uuid.uu, ev->uuid, 16);
781
782         if (!bt_hal_cbacks->socket_authorize_request_cb) {
783                 ERR("HAL User socket_authorize_request_cb is not set!!");
784                 return;
785         }
786
787         bt_hal_cbacks->socket_authorize_request_cb(&bd_addr, &uuid);
788         DBG("-");
789 }
790 #endif
791
792 static void __bt_hal_handle_ssp_request_event(void *buf, uint16_t len)
793 {
794         struct hal_ev_ssp_request *ev = (struct hal_ev_ssp_request *)buf;
795         bt_bdaddr_t bd_addr;
796         bt_bdname_t bd_name;
797         DBG("+");
798
799         memcpy(bd_addr.address, ev->bdaddr, 6);
800         memcpy(bd_name.name, ev->name, 248);
801
802         /* BD address*/
803         DBG("[0x%x]", bd_addr.address[0]);
804         DBG("[0x%x]", bd_addr.address[1]);
805         DBG("[0x%x]", bd_addr.address[2]);
806         DBG("[0x%x]", bd_addr.address[3]);
807         DBG("[0x%x]", bd_addr.address[4]);
808         DBG("[0x%x]", bd_addr.address[5]);
809
810         DBG("COD [%d]", ev->class_of_dev);
811         DBG("pairing variant [%d]", ev->pairing_variant);
812         DBG("passkey [%d]", ev->passkey);
813
814         if (!bt_hal_cbacks->ssp_request_cb) {
815                 ERR("HAL User ssp_request_cb is not set!!");
816                 return;
817         }
818
819         bt_hal_cbacks->ssp_request_cb(&bd_addr, &bd_name,
820                         ev->class_of_dev,
821                         ev->pairing_variant, ev->passkey);
822         DBG("-");
823 }
824
825 static void __bt_hal_handle_pin_request_event(void *buf, uint16_t len)
826 {
827         struct hal_ev_pin_request *ev = (struct hal_ev_pin_request *)buf;
828         bt_bdaddr_t bd_addr;
829         bt_bdname_t bd_name;
830         DBG("+");
831
832         memcpy(bd_addr.address, ev->bdaddr, 6);
833         memcpy(bd_name.name, ev->name, 248);
834
835         /* BD address*/
836         DBG("[0x%x]", bd_addr.address[0]);
837         DBG("[0x%x]", bd_addr.address[1]);
838         DBG("[0x%x]", bd_addr.address[2]);
839         DBG("[0x%x]", bd_addr.address[3]);
840         DBG("[0x%x]", bd_addr.address[4]);
841         DBG("[0x%x]", bd_addr.address[5]);
842
843         DBG("COD [%u]", ev->class_of_dev);
844
845         if (!bt_hal_cbacks->pin_request_cb) {
846                 ERR("HAL User pin_request_cb is not set!!");
847                 return;
848         }
849
850         bt_hal_cbacks->pin_request_cb(&bd_addr, &bd_name,
851                         ev->class_of_dev);
852         DBG("-");
853 }
854
855 static void __bt_hal_handle_device_trust_state_changed_event(void *buf, uint16_t len)
856 {
857         struct hal_ev_device_trust_state_changed *ev = (struct hal_ev_device_trust_state_changed*)buf;
858         bt_bdaddr_t bd_addr;
859         DBG("+");
860
861         memcpy(bd_addr.address, ev->bdaddr, 6);
862         /* BD address*/
863         DBG("[0x%x]", bd_addr.address[0]);
864         DBG("[0x%x]", bd_addr.address[1]);
865         DBG("[0x%x]", bd_addr.address[2]);
866         DBG("[0x%x]", bd_addr.address[3]);
867         DBG("[0x%x]", bd_addr.address[4]);
868         DBG("[0x%x]", bd_addr.address[5]);
869
870         DBG("Device Trusted?  [0x%x]", ev->trust);
871
872         if (!bt_hal_cbacks->device_trust_state_changed_cb) {
873                 ERR("HAL User device_trust_state_changed_cb is not set!!");
874                 return;
875         }
876
877         bt_hal_cbacks->device_trust_state_changed_cb(&bd_addr, ev->trust);
878         DBG("-");
879 }
880
881 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len)
882 {
883         DBG("+");
884         switch(message) {
885                 case HAL_EV_ADAPTER_STATE_CHANGED:
886                         DBG("Event: HAL_EV_ADAPTER_STATE_CHANGED");
887                         __bt_hal_handle_adapter_state_changed(buf, len);
888                         break;
889                 case HAL_EV_ADAPTER_PROPS_CHANGED:
890                         DBG("Event: HAL_EV_ADAPTER_PROPS_CHANGED");
891                         __bt_hal_handle_adapter_property_changed(buf, len);
892                         break;
893                 case HAL_EV_DISCOVERY_STATE_CHANGED:
894                         DBG("Event: HAL_EV_DISCOVERY_STATE_CHANGED");
895                         __bt_hal_handle_adapter_discovery_state_changed(buf, len);
896                         break;
897                 case HAL_EV_DEVICE_FOUND:
898                         DBG("Event: HAL_EV_DEVICE_FOUND");
899                         __bt_hal_handle_device_found_event(buf, len);
900                         break;
901                 case HAL_EV_REMOTE_DEVICE_PROPS:
902                         DBG("Event: HAL_EV_REMOTE_DEVICE_PROPS");
903                         __bt_hal_handle_remote_device_properties_event(buf, len);
904                         break;
905                 case HAL_EV_BOND_STATE_CHANGED:
906                         DBG("Event: HAL_EV_BOND_STATE_CHANGED:");
907                         __bt_hal_handle_bond_state_changed_event(buf, len);
908                         break;
909                 case HAL_EV_ACL_STATE_CHANGED:
910                         DBG("Event: HAL_ACL_STATE_CONNECTED or Disconnected");
911                         __bt_hal_handle_device_acl_state_changed_event(buf, len);
912                         break;
913                 case HAL_EV_LE_CONN_STATE_CHANGED:
914                         DBG("Event: HAL_LE_CONN_STATE_CONNECTED or Disconnected");
915                         __bt_hal_handle_device_le_conn_state_changed_event(buf, len);
916                         break;
917                 case HAL_EV_SSP_REQUEST:
918                         DBG("Event: HAL_EV_SSP_REQUEST");
919                         __bt_hal_handle_ssp_request_event(buf, len);
920                         break;
921                 case HAL_EV_PIN_REQUEST:
922                         DBG("Event: HAL_EV_PIN_REQUEST");
923                         __bt_hal_handle_pin_request_event(buf, len);
924                         break;
925                 case HAL_EV_AUTHORIZE_REQUEST:
926                         DBG("Event: HAL_EV_AUTHORIZE_REQUEST");
927                         __bt_hal_handle_authorize_request_event(buf, len);
928                         break;
929                 case HAL_EV_DEVICE_TRUST_CHANGED:
930                         DBG("Event: HAL_EV_DEVICE_TRUST_CHANGED");
931                         __bt_hal_handle_device_trust_state_changed_event(buf, len);
932                         break;
933 #ifdef TIZEN_BT_HAL
934                 case HAL_EV_SOCK_AUTHORIZE_REQUEST:
935                         DBG("Event: HAL_EV_AUTHORIZE_REQUEST");
936                         __bt_hal_handle_socket_authorize_request_event(buf, len);
937                         break;
938                 case HAL_EV_LE_STATE_CHANGED:
939                         DBG("Event: HAL_EV_LE_STATE_CHANGED");
940                         __bt_hal_handle_le_state_changed(buf, len);
941                         break;
942 #endif
943                 default:
944                         DBG("Event Currently not handled!!");
945                         break;
946         }
947         DBG("-");
948 }