Merge "Invoke HAL_DISCOVERY_STATE_STOPPED event once" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hiddevice.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
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 <stdbool.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <dlog.h>
25
26 #include "bt-hal.h"
27 #include "bt-hal-log.h"
28 #include "bt-hal-msg.h"
29 #include "bt-hal-utils.h"
30
31 #include "bt-hal-hid-dbus-handler.h"
32 #include "bt-hal-event-receiver.h"
33
34 static const bthd_callbacks_t *bt_hal_hid_device_cbacks;
35
36 static bool interface_ready(void)
37 {
38         return bt_hal_hid_device_cbacks != NULL;
39 }
40
41 static void __bt_hal_handle_conn_state(void *buf, uint16_t len)
42 {
43         struct hal_ev_hiddevice_conn_state *ev = buf;
44
45         if (bt_hal_hid_device_cbacks->connection_state_cb)
46                 bt_hal_hid_device_cbacks->connection_state_cb((bt_bdaddr_t *) ev->bdaddr,
47                                 ev->state);
48 }
49
50 static void __bt_hal_handle_hid_device_events(int message, void *buf, uint16_t len)
51 {
52         DBG("+");
53
54         if (!interface_ready())
55                 return;
56
57         switch (message) {
58         case HAL_EV_HIDDEVICE_CONN_STATE:
59                 DBG("Event: HAL_EV_HIDDEVICE_CONN_STATE");
60                 __bt_hal_handle_conn_state(buf, len);
61                 break;
62         default:
63                 DBG("Event Currently not handled!!");
64                 break;
65         }
66
67         DBG("-");
68 }
69
70 static bt_status_t init(bthd_callbacks_t *callbacks)
71 {
72         DBG("");
73
74         if (interface_ready())
75                 return BT_STATUS_DONE;
76
77         bt_hal_hid_device_cbacks = callbacks;
78         DBG("Register HID events callback function");
79         _bt_hal_register_event_handler_cb(HAL_HIDDEVICE, __bt_hal_handle_hid_device_events);
80
81         return BT_STATUS_SUCCESS;
82 }
83
84 static void cleanup(void)
85 {
86         DBG("");
87
88         if (!interface_ready())
89                 return;
90
91         _bt_hal_unregister_hid_dbus_handler_cb();
92         _bt_hal_unregister_event_handler_cb(HAL_HIDDEVICE);
93
94         bt_hal_hid_device_cbacks = NULL;
95 }
96
97 static bthd_interface_t hiddevice_if = {
98         .size = sizeof(hiddevice_if),
99         .init = init,
100         .cleanup = cleanup
101 };
102
103 bthd_interface_t *bt_get_hiddevice_interface(void)
104 {
105         return &hiddevice_if;
106 }
107