BLE Gatt Server Socket notify implementation.
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hidhost.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Atul Kumar Rai <a.rai@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 <stdbool.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdlib.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-hid-dbus-handler.h"
34 #include "bt-hal-event-receiver.h"
35
36 static const bthh_callbacks_t *bt_hal_hid_cbacks;
37
38 static bool interface_ready(void)
39 {
40         return bt_hal_hid_cbacks != NULL;
41 }
42
43 static void __bt_hal_handle_conn_state(void *buf, uint16_t len)
44 {
45         struct hal_ev_hidhost_conn_state *ev = buf;
46
47         if (bt_hal_hid_cbacks->connection_state_cb)
48                 bt_hal_hid_cbacks->connection_state_cb((bt_bdaddr_t *) ev->bdaddr,
49                                 ev->state);
50 }
51
52 static void __bt_hal_handle_hidhost_info(void *buf, uint16_t len)
53 {
54         struct hal_ev_hidhost_info *ev = buf;
55         bthh_hid_info_t info;
56
57         info.attr_mask = ev->attr;
58         info.sub_class = ev->subclass;
59         info.app_id = ev->app_id;
60         info.vendor_id = ev->vendor;
61         info.product_id = ev->product;
62         info.version = ev->version;
63         info.ctry_code = ev->country;
64         info.dl_len = ev->descr_len;
65         memcpy(info.dsc_list, ev->descr, info.dl_len);
66
67         if (bt_hal_hid_cbacks->hid_info_cb)
68                 bt_hal_hid_cbacks->hid_info_cb((bt_bdaddr_t *) ev->bdaddr, &info);
69 }
70
71 static void __bt_hal_handle_proto_mode(void *buf, uint16_t len)
72 {
73         struct hal_ev_hidhost_proto_mode *ev = buf;
74
75         if (bt_hal_hid_cbacks->protocol_mode_cb)
76                 bt_hal_hid_cbacks->protocol_mode_cb((bt_bdaddr_t *) ev->bdaddr,
77                                 ev->status, ev->mode);
78 }
79
80 static void __bt_hal_handle_idle_time(void *buf, uint16_t len)
81 {
82         struct hal_ev_hidhost_idle_time *ev = buf;
83
84         if (bt_hal_hid_cbacks->idle_time_cb)
85                 bt_hal_hid_cbacks->idle_time_cb((bt_bdaddr_t *) ev->bdaddr, ev->status,
86                                 ev->idle_rate);
87 }
88
89 static void __bt_hal_handle_get_report(void *buf, uint16_t len)
90 {
91         struct hal_ev_hidhost_get_report *ev = buf;
92
93         if (len != sizeof(*ev) + ev->len) {
94                 ERR("invalid get report event");
95                 return;
96         }
97
98         if (bt_hal_hid_cbacks->get_report_cb)
99                 bt_hal_hid_cbacks->get_report_cb((bt_bdaddr_t *) ev->bdaddr, ev->status,
100                                 ev->data, ev->len);
101 }
102
103 static void __bt_hal_handle_virtual_unplug(void *buf, uint16_t len)
104 {
105         struct hal_ev_hidhost_virtual_unplug *ev = buf;
106
107         if (bt_hal_hid_cbacks->virtual_unplug_cb)
108                 bt_hal_hid_cbacks->virtual_unplug_cb((bt_bdaddr_t *) ev->bdaddr,
109                                 ev->status);
110 }
111
112 static void __bt_hal_handle_handshake(void *buf, uint16_t len)
113 {
114         struct hal_ev_hidhost_handshake *ev = buf;
115
116         if (bt_hal_hid_cbacks->handshake_cb)
117                 bt_hal_hid_cbacks->handshake_cb((bt_bdaddr_t *) ev->bdaddr, ev->status);
118 }
119
120 static bt_status_t hidhost_connect(bt_bdaddr_t *bd_addr)
121 {
122         DBG("");
123         return _bt_hal_dbus_handler_hidhost_connect(bd_addr);
124 }
125
126 static bt_status_t hidhost_disconnect(bt_bdaddr_t *bd_addr)
127 {
128         DBG("");
129         return _bt_hal_dbus_handler_hidhost_disconnect(bd_addr);
130 }
131
132 static bt_status_t virtual_unplug(bt_bdaddr_t *bd_addr)
133 {
134         DBG("");
135         return BT_STATUS_UNSUPPORTED;
136 }
137
138 static bt_status_t hidhost_set_info(bt_bdaddr_t *bd_addr, bthh_hid_info_t *hid_info)
139 {
140         DBG("");
141         return BT_STATUS_UNSUPPORTED;
142 }
143
144 static bt_status_t get_protocol(bt_bdaddr_t *bd_addr,
145                 bthh_protocol_mode_t protocol_mode)
146 {
147         DBG("");
148         return BT_STATUS_UNSUPPORTED;
149 }
150
151 static bt_status_t set_protocol(bt_bdaddr_t *bd_addr,
152                 bthh_protocol_mode_t protocol_mode)
153 {
154         DBG("");
155         return BT_STATUS_UNSUPPORTED;
156 }
157
158 static bt_status_t get_report(bt_bdaddr_t *bd_addr,
159                 bthh_report_type_t report_type,
160                 uint8_t report_id,
161                 int buffer_size)
162 {
163         DBG("");
164         return BT_STATUS_UNSUPPORTED;
165 }
166
167 static bt_status_t set_report(bt_bdaddr_t *bd_addr,
168                 bthh_report_type_t report_type,
169                 char *report)
170 {
171         DBG("");
172         return BT_STATUS_UNSUPPORTED;
173 }
174
175 static bt_status_t hidhost_send_data(bt_bdaddr_t *bd_addr, char *data)
176 {
177         DBG("");
178         return BT_STATUS_UNSUPPORTED;
179 }
180
181 static void __bt_hal_handle_hid_events(int message, void *buf, uint16_t len)
182 {
183         DBG("+");
184
185         if (!interface_ready())
186                 return;
187
188         switch (message) {
189         case HAL_EV_HIDHOST_CONN_STATE:
190                 DBG("Event: HAL_EV_HIDHOST_CONN_STATE");
191                 __bt_hal_handle_conn_state(buf, len);
192                 break;
193         case HAL_EV_HIDHOST_INFO:
194                 DBG("Event: HAL_EV_HIDHOST_INFO");
195                 __bt_hal_handle_hidhost_info(buf, len);
196                 break;
197         case HAL_EV_HIDHOST_PROTO_MODE:
198                 DBG("Event: HAL_EV_HIDHOST_PROTO_MODE");
199                 __bt_hal_handle_proto_mode(buf, len);
200                 break;
201         case HAL_EV_HIDHOST_IDLE_TIME:
202                 DBG("Event: HAL_EV_HIDHOST_IDLE_TIME");
203                 __bt_hal_handle_idle_time(buf, len);
204                 break;
205         case HAL_EV_HIDHOST_GET_REPORT:
206                 DBG("Event: HAL_EV_HIDHOST_GET_REPORT");
207                 __bt_hal_handle_get_report(buf, len);
208                 break;
209         case HAL_EV_HIDHOST_VIRTUAL_UNPLUG:
210                 DBG("Event: HAL_EV_HIDHOST_VIRTUAL_UNPLUG");
211                 __bt_hal_handle_virtual_unplug(buf, len);
212                 break;
213         case HAL_EV_HIDHOST_HANDSHAKE:
214                 DBG("Event: HAL_EV_HIDHOST_HANDSHAKE");
215                 __bt_hal_handle_handshake(buf, len);
216                 break;
217         default:
218                 DBG("Event Currently not handled!!");
219                 break;
220         }
221
222         DBG("-");
223 }
224
225 static bt_status_t init(bthh_callbacks_t *callbacks)
226 {
227         DBG("");
228
229         if (interface_ready())
230                 return BT_STATUS_DONE;
231
232         bt_hal_hid_cbacks = callbacks;
233         DBG("Register HID events callback function");
234         _bt_hal_register_hid_dbus_handler_cb(__bt_hal_handle_hid_events);
235         _bt_hal_register_event_handler_cb(HAL_HIDHOST, __bt_hal_handle_hid_events);
236
237         return BT_STATUS_SUCCESS;
238 }
239
240 static void cleanup(void)
241 {
242         DBG("");
243
244         if (!interface_ready())
245                 return;
246
247         _bt_hal_unregister_hid_dbus_handler_cb();
248         _bt_hal_unregister_event_handler_cb(HAL_HIDHOST);
249
250         bt_hal_hid_cbacks = NULL;
251 }
252
253 static bthh_interface_t hidhost_if = {
254         .size = sizeof(hidhost_if),
255         .init = init,
256         .connect = hidhost_connect,
257         .disconnect = hidhost_disconnect,
258         .virtual_unplug = virtual_unplug,
259         .set_info = hidhost_set_info,
260         .get_protocol = get_protocol,
261         .set_protocol = set_protocol,
262         .get_report = get_report,
263         .set_report = set_report,
264         .send_data = hidhost_send_data,
265         .cleanup = cleanup
266 };
267
268 bthh_interface_t *bt_get_hidhost_interface(void)
269 {
270         return &hidhost_if;
271 }