9d76bd21a283f7a774b521f2135ce3d1687fe184
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hid-device.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <gio/gio.h>
18 #include <gio/gunixfdlist.h>
19 #include <sys/socket.h>
20
21 #include "bluetooth-api.h"
22 #include "bt-internal-types.h"
23 #include "bluetooth-hid-api.h"
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
27
28 #define HID_UUID                "00001124-0000-1000-8000-00805f9b34fb"
29 #define HID_DEVICE_UUID "00001124-0000-1000-8000-00805f9b43bf"
30 #define REPORTID_MOUSE  1
31 #define BT_HID_BUFFER_LEN 100
32
33 /* HIDP header masks */
34 #define BT_HID_HEADER_TRANS_MASK                        0xf0
35 #define BT_HID_HEADER_PARAM_MASK                        0x0f
36
37 /* HIDP transaction types */
38 #define BT_HID_TRANS_HANDSHAKE                  0x00
39 #define BT_HID_TRANS_HID_CONTROL                        0x10
40 #define BT_HID_TRANS_GET_REPORT                 0x40
41 #define BT_HID_TRANS_SET_REPORT                 0x50
42 #define BT_HID_TRANS_GET_PROTOCOL                       0x60
43 #define BT_HID_TRANS_SET_PROTOCOL                       0x70
44 #define BT_HID_TRANS_GET_IDLE                   0x80
45 #define BT_HID_TRANS_SET_IDLE                   0x90
46 #define BT_HID_TRANS_DATA                               0xa0
47 #define BT_HID_TRANS_DATC                               0xb0
48
49 #define BT_HID_DATA_RTYPE_INPUT                 0x01
50 #define BT_HID_DATA_RTYPE_OUTPUT                        0x02
51
52 #define BT_HID_HSHK_SUCCESSFUL  0x00
53 #define BT_HID_HSHK_NOT_READY   0x01
54 #define BT_HID_HSHK_ERR_INVALID_REPORT_ID       0x02
55 #define BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST     0x03
56 #define BT_HID_HSHK_ERR_INVALID_PARAMETER       0x04
57 #define BT_HID_HSHK_ERR_UNKNOWN 0x0E
58 #define BT_HID_HSHK_ERR_FATAL   0x0F
59
60 typedef struct {
61         guint object_id;
62         gchar *path;
63         int id;
64         char *uuid;
65         GSList *device_list;
66 } hid_info_t;
67
68 typedef struct {
69         int ctrl_fd;
70         int intr_fd;
71         GIOChannel *ctrl_data_io;
72         GIOChannel *intr_data_io;
73         guint ctrl_data_id;
74         guint intr_data_id;
75         char *address;
76         guint disconnect_idle_id;
77 } hid_connected_device_info_t;
78
79 struct reports {
80         guint8 type;
81         guint8 rep_data[20];
82 } __attribute__((__packed__));
83
84 static hid_info_t *hid_info = NULL;
85
86 /* Variable for privilege, only for write API,
87   before we should reduce time to bt-service dbus calling
88   -1 : Don't have a permission to access API
89   0 : Initial value, not yet check
90   1 : Have a permission to access API
91 */
92 static int privilege_token_send_mouse = 0;
93 static int privilege_token_send_key = 0;
94 static int privilege_token_reply = 0;
95
96 static gboolean __hid_disconnect(hid_connected_device_info_t *info);
97
98 int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr)
99 {
100
101         int ret = BLUETOOTH_ERROR_NONE;
102         char *adapter_path;
103         GVariant *result = NULL;
104         GError *err = NULL;
105         GDBusConnection *conn;
106         GDBusProxy *server_proxy;
107         int index1 = 0;
108         int index2 = 0;
109         GUnixFDList *out_fd_list = NULL;
110         conn = _bt_gdbus_get_system_gconn();
111         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
112
113         adapter_path = _bt_get_device_object_path((char *)address);
114         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
115         BT_INFO_C("Device : %s", adapter_path);
116         server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
117                         NULL, BT_BLUEZ_NAME,
118                         adapter_path, "org.bluez.Input1",  NULL, NULL);
119         g_free(adapter_path);
120
121         if (server_proxy == NULL) {
122                 BT_ERR("Failed to get the network server proxy\n");
123                 return BLUETOOTH_ERROR_INTERNAL;
124         }
125
126         result = g_dbus_proxy_call_with_unix_fd_list_sync(server_proxy, "GetFD",
127                                  NULL,
128                                  G_DBUS_CALL_FLAGS_NONE,
129                                  -1,
130                                  NULL,
131                                  &out_fd_list,
132                                  NULL,
133                                  &err);
134         if (result == NULL) {
135                 if (err != NULL) {
136                         g_dbus_error_strip_remote_error(err);
137                         BT_ERR("INPUT server register Error: %s\n", err->message);
138                         if (g_strcmp0(err->message, "Already Exists") == 0) {
139                                 ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
140                         } else {
141                                 ret = BLUETOOTH_ERROR_INTERNAL;
142                         }
143                         g_error_free(err);
144                 }
145         } else {
146                 g_variant_get(result, "(hh)", &index1, &index2);
147                 int fd1 = g_unix_fd_list_get(out_fd_list, index1, NULL);
148                 int fd2 = g_unix_fd_list_get(out_fd_list, index2, NULL);
149
150                 *ctrl = fd1;
151                 *intr = fd2;
152                 g_object_unref(out_fd_list);
153                 g_variant_unref(result);
154         }
155         g_object_unref(server_proxy);
156         return ret;
157 }
158
159 static hid_connected_device_info_t *__find_hid_info_with_address(const char *remote_addr)
160 {
161         GSList *l;
162
163         for (l = hid_info->device_list; l != NULL; l = l->next) {
164                 hid_connected_device_info_t *info = l->data;
165                 if (g_strcmp0((const char *)info->address, (const char *)remote_addr) == 0)
166                         return info;
167         }
168         return NULL;
169 }
170
171 static void __hid_connected_cb(hid_connected_device_info_t *info,
172                         int result)
173 {
174         bluetooth_hid_request_t conn_info;
175         bt_event_info_t *event_info = NULL;
176
177         event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
178         if (event_info == NULL)
179                 return;
180
181         memset(&conn_info, 0x00, sizeof(bluetooth_hid_request_t));
182         if (info->intr_fd != -1 && info->ctrl_fd == -1)
183                 conn_info.socket_fd = info->intr_fd;
184         else
185                 conn_info.socket_fd = info->ctrl_fd;
186         _bt_convert_addr_string_to_type(conn_info.device_addr.addr, info->address);
187
188         if (result == BLUETOOTH_ERROR_NONE)
189                 BT_INFO_C("Connected [HID Device]");
190
191         _bt_common_event_cb(BLUETOOTH_HID_DEVICE_CONNECTED,
192                         result, &conn_info,
193                         event_info->cb, event_info->user_data);
194 }
195
196 static void __hid_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
197                         gpointer user_data)
198
199 {
200         int result;
201         GError *error = NULL;
202         GVariant *ret = NULL;
203         hid_connected_device_info_t info;
204         const char *path;
205
206         BT_DBG("+");
207
208         ret = g_dbus_proxy_call_finish(proxy, res, &error);
209         if (ret == NULL) {
210                 g_dbus_error_strip_remote_error(error);
211                 BT_ERR("Error : %s \n", error->message);
212
213                 if (g_strcmp0(error->message, "In Progress") == 0)
214                         result = BLUETOOTH_ERROR_DEVICE_BUSY;
215                 else
216                         result = BLUETOOTH_ERROR_INTERNAL;
217
218                 info.ctrl_fd = -1;
219                 info.intr_fd = -1;
220
221                 info.address = g_malloc0(BT_ADDRESS_STRING_SIZE);
222
223                 path = g_dbus_proxy_get_object_path(proxy);
224                 _bt_convert_device_path_to_address(path, info.address);
225
226                 __hid_connected_cb(&info, result);
227
228                 g_free(info.address);
229
230                 g_error_free(error);
231         } else {
232                 g_variant_unref(ret);
233         }
234
235         if (proxy)
236                 g_object_unref(proxy);
237
238         BT_DBG("-");
239 }
240
241 static gboolean __hid_disconnect(hid_connected_device_info_t *info)
242 {
243         bluetooth_hid_request_t disconn_info;
244         int fd = info->ctrl_fd;
245         bt_event_info_t *event_info;
246
247         BT_INFO_C("Disconnected [HID Device]");
248         hid_info->device_list = g_slist_remove(hid_info->device_list, info);
249         if (info->ctrl_data_id > 0) {
250                 g_source_remove(info->ctrl_data_id);
251                 info->ctrl_data_id = 0;
252         }
253         if (info->intr_data_id > 0) {
254                 g_source_remove(info->intr_data_id);
255                 info->intr_data_id = 0;
256         }
257
258         if (info->ctrl_data_io) {
259                 g_io_channel_shutdown(info->ctrl_data_io, TRUE, NULL);
260                 g_io_channel_unref(info->ctrl_data_io);
261                 info->ctrl_data_io = NULL;
262         }
263         if (info->intr_data_io) {
264                 g_io_channel_shutdown(info->intr_data_io, TRUE, NULL);
265                 g_io_channel_unref(info->intr_data_io);
266                 info->intr_data_io = NULL;
267         }
268         if (info->intr_fd >= 0) {
269                 close(info->ctrl_fd);
270                 close(info->intr_fd);
271                 info->intr_fd = -1;
272                 info->ctrl_fd = -1;
273         }
274         info->disconnect_idle_id = 0;
275         event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
276         if (event_info == NULL)
277                 return FALSE;
278
279         memset(&disconn_info, 0x00, sizeof(bluetooth_hid_request_t));
280         disconn_info.socket_fd = fd;
281         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr , info->address);
282         _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DISCONNECTED,
283                         BLUETOOTH_ERROR_NONE, &disconn_info,
284                         event_info->cb, event_info->user_data);
285         if (info->address)
286                 g_free(info->address);
287         g_free(info);
288         info = NULL;
289         BT_DBG("-");
290         return FALSE;
291 }
292
293 void __free_hid_info(hid_info_t *info)
294 {
295         BT_DBG("");
296
297         _bt_unregister_gdbus(info->object_id);
298
299         while (info->device_list) {
300                 hid_connected_device_info_t *dev_info = NULL;
301                 dev_info = (hid_connected_device_info_t *)info->device_list->data;
302
303                 if (dev_info->disconnect_idle_id > 0) {
304                         BT_INFO("Disconnect idle still not process remove source");
305                         g_source_remove(dev_info->disconnect_idle_id);
306                         dev_info->disconnect_idle_id = 0;
307                 }
308                 __hid_disconnect(dev_info);
309         }
310
311         g_free(info->path);
312         g_free(info->uuid);
313         g_free(info);
314 }
315
316 static gboolean __is_error_by_disconnect(GError *err)
317 {
318         return !g_strcmp0(err->message, "Connection reset by peer") ||
319                         !g_strcmp0(err->message, "Connection timed out") ||
320                         !g_strcmp0(err->message, "Software caused connection abort");
321 }
322
323 static gboolean __received_cb(GIOChannel *chan, GIOCondition cond,
324                                                                 gpointer data)
325 {
326         hid_connected_device_info_t *info = data;
327         GIOStatus status = G_IO_STATUS_NORMAL;
328         char buffer[20];
329         gsize len = 0;
330         GError *err = NULL;
331         guint8  header, type, param;
332         bt_event_info_t *event_info;
333         retv_if(info == NULL, FALSE);
334
335         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
336                 BT_ERR_C("HID  disconnected: %d", info->ctrl_fd);
337                if (info->disconnect_idle_id > 0) {
338                         BT_INFO("Disconnect idle still not process remove source");
339                         g_source_remove(info->disconnect_idle_id);
340                         info->disconnect_idle_id = 0;
341                }
342                 __hid_disconnect(info);
343                 return FALSE;
344         }
345         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
346                         &len, &err);
347         if (status == G_IO_STATUS_NORMAL) {
348                 BT_INFO("Parsing Data");
349                 bluetooth_hid_received_data_t data = {0};
350                 header = buffer[0];
351                 type = header & BT_HID_HEADER_TRANS_MASK;
352                 param = header & BT_HID_HEADER_PARAM_MASK;
353                 BT_INFO("type %d , param %d", type, param);
354                 BT_INFO("Data Reveived from %s" , info->address);
355
356                 event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
357                 if (event_info == NULL)
358                         return FALSE;
359
360                 data.address = g_strdup(info->address);
361                 switch (type) {
362                         case BT_HID_TRANS_HANDSHAKE:
363                                 BT_INFO("TRANS HANDSHAKE");
364                                 data.type = HTYPE_TRANS_HANDSHAKE;
365                                 data.buffer_size = len;
366                                 data.buffer = (char *) malloc(sizeof(char) * len);
367                                 if (data.buffer)
368                                         memcpy(data.buffer, buffer, len);
369                         break;
370                         case BT_HID_TRANS_HID_CONTROL:
371                                 BT_INFO("HID CONTROL");
372                                 data.type = HTYPE_TRANS_HID_CONTROL;
373                                 data.buffer_size = len;
374                                 data.buffer = (char *) malloc(sizeof(char) * len);
375                                 if (data.buffer)
376                                         memcpy(data.buffer, buffer, len);
377                         break;
378                         case BT_HID_TRANS_DATA:
379                                 BT_INFO("TRANS DATA");
380                                 data.type = HTYPE_TRANS_DATA;
381                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
382                                         BT_INFO("Input Report");
383                                         data.param = PTYPE_DATA_RTYPE_INPUT;
384                                         data.buffer_size = len;
385                                         data.buffer = (char *) malloc(sizeof(char) * len);
386                                         if (data.buffer)
387                                                 memcpy(data.buffer, buffer, len);
388                                 } else {
389                                         BT_INFO("Out Report");
390                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
391                                         data.buffer_size = len;
392                                         data.buffer = (char *) malloc(sizeof(char) * len);
393                                         if (data.buffer)
394                                                 memcpy(data.buffer, buffer, len);
395                                 }
396                         break;
397                         case BT_HID_TRANS_GET_REPORT: {
398                                 BT_INFO("Get Report");
399                                 data.type = HTYPE_TRANS_GET_REPORT;
400                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
401                                         BT_INFO("Input Report");
402                                         data.param = PTYPE_DATA_RTYPE_INPUT;
403                                 } else {
404                                         BT_INFO("Output Report");
405                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
406                                 }
407                                 data.buffer_size = len;
408                                 data.buffer = (char *) malloc(sizeof(char) * len);
409                                 if (data.buffer)
410                                         memcpy(data.buffer, buffer, len);
411                                 break;
412                         }
413                         case BT_HID_TRANS_SET_REPORT: {
414                                 BT_INFO("Set Report");
415                                 data.type = HTYPE_TRANS_SET_REPORT;
416                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
417                                         BT_INFO("Input Report");
418                                         data.param = PTYPE_DATA_RTYPE_INPUT;
419                                 } else {
420                                         BT_INFO("Output Report");
421                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
422                                 }
423                                 data.buffer_size = len;
424                                 data.buffer = (char *) malloc(sizeof(char) * len);
425                                 if (data.buffer)
426                                         memcpy(data.buffer, buffer, len);
427                                 break;
428                         }
429                         case BT_HID_TRANS_GET_PROTOCOL:{
430                                 BT_INFO("Get_PROTOCOL");
431                                 data.type = HTYPE_TRANS_GET_PROTOCOL;
432                                 data.param = PTYPE_DATA_RTYPE_INPUT;
433                                 data.buffer_size = len;
434                                 data.buffer = (char *) malloc(sizeof(char) * len);
435                                 if (data.buffer)
436                                         memcpy(data.buffer, buffer, len);
437                                 break;
438                         }
439                         case BT_HID_TRANS_SET_PROTOCOL:{
440                                 BT_INFO("Set_PROTOCOL");
441                                 data.type = HTYPE_TRANS_SET_PROTOCOL;
442                                 data.param = PTYPE_DATA_RTYPE_INPUT;
443                                 data.buffer_size = len;
444                                 data.buffer = (char *) malloc(sizeof(char) * len);
445                                 if (data.buffer)
446                                         memcpy(data.buffer, buffer, len);
447                                 break;
448                         }
449                         default: {
450                                 BT_INFO("unsupported HIDP control message");
451                                 BT_ERR("Send Handshake Message");
452                                 guint8 type = BT_HID_TRANS_HANDSHAKE |
453                                         BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST;
454                                 data.type = HTYPE_TRANS_UNKNOWN;
455                                 int fd = g_io_channel_unix_get_fd(chan);
456                                 int bytes = write(fd,  &type, sizeof(type));
457                                 BT_INFO("Bytes Written %d", bytes);
458                                 break;
459                         }
460                 }
461
462                 _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DATA_RECEIVED,
463                                 BLUETOOTH_ERROR_NONE, &data,
464                                 event_info->cb, event_info->user_data);
465                 if (data.buffer)
466                         g_free(data.buffer);
467                 if (data.address)
468                         g_free((char *)data.address);
469         } else {
470                 BT_ERR("Error while reading data %d [%s]", status, info->address);
471                 if (err) {
472                         BT_ERR("IO Channel read error [%s]", err->message);
473                         if (status == G_IO_STATUS_ERROR &&
474                                         __is_error_by_disconnect(err)) {
475                                 BT_DBG("cond : %d", cond);
476                                 g_error_free(err);
477                                 __hid_disconnect(info);
478                                 return FALSE;
479                         }
480                         g_error_free(err);
481                 } else if (status == G_IO_STATUS_EOF) {
482                         __hid_disconnect(info);
483                         return FALSE;
484                 }
485         }
486         return TRUE;
487 }
488
489 int new_hid_connection(const char *path, int fd, bluetooth_device_address_t *addr)
490 {
491         hid_info_t *info = NULL;
492         hid_connected_device_info_t *dev_info = NULL;
493         char address[18];
494         info = hid_info;
495
496         if (info == NULL)
497                 return -1;
498         _bt_convert_addr_type_to_string((char *)address, addr->addr);
499         BT_INFO("Address [%s]", address);
500         dev_info = __find_hid_info_with_address(address);
501         if (dev_info == NULL) {
502                 dev_info = (hid_connected_device_info_t *)
503                         g_malloc0(sizeof(hid_connected_device_info_t));
504                 if (dev_info == NULL) {
505                         BT_ERR("Fail to allocation memory");
506                         return -1;
507                 }
508
509                 dev_info->intr_fd = -1;
510                 dev_info->ctrl_fd = -1;
511                 dev_info->intr_fd = fd;
512                 dev_info->address = g_strdup(address);
513                 dev_info->intr_data_io = g_io_channel_unix_new(dev_info->intr_fd);
514                 g_io_channel_set_encoding(dev_info->intr_data_io, NULL, NULL);
515                 g_io_channel_set_flags(dev_info->intr_data_io, G_IO_FLAG_NONBLOCK, NULL);
516                 g_io_channel_set_close_on_unref(dev_info->intr_data_io, TRUE);
517                 dev_info->intr_data_id = g_io_add_watch(dev_info->intr_data_io,
518                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
519                                 __received_cb, dev_info);
520                 hid_info->device_list = g_slist_append(hid_info->device_list, dev_info);
521         } else {
522                 dev_info->ctrl_fd = fd;
523                 dev_info->ctrl_data_io = g_io_channel_unix_new(dev_info->ctrl_fd);
524                 g_io_channel_set_encoding(dev_info->ctrl_data_io, NULL, NULL);
525                 g_io_channel_set_flags(dev_info->ctrl_data_io, G_IO_FLAG_NONBLOCK, NULL);
526                 g_io_channel_set_close_on_unref(dev_info->ctrl_data_io, TRUE);
527                 dev_info->ctrl_data_id = g_io_add_watch(dev_info->ctrl_data_io,
528                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
529                                 __received_cb, dev_info);
530         }
531         if (dev_info->ctrl_fd != -1 && dev_info->intr_fd != -1) {
532                 __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE);
533         }
534
535         return 0;
536 }
537 static hid_info_t *__register_method()
538 {
539         int object_id;
540         hid_info_t *info = NULL;
541         char *path = NULL;
542         path = g_strdup_printf("/org/socket/server/%d", getpid());
543
544         object_id = _bt_register_new_conn(path, new_hid_connection);
545         if (object_id < 0) {
546                 return NULL;
547         }
548         info = g_new(hid_info_t, 1);
549         info->object_id = (guint)object_id;
550         info->path = path;
551         info->id = 0;
552         info->device_list = NULL;
553
554         return info;
555 }
556
557 void _bluetooth_hid_free_hid_info(void)
558 {
559         if (hid_info == NULL) {
560                 BT_DBG("hid_info is already NULL");
561                 return;
562         }
563
564         __free_hid_info(hid_info);
565         hid_info = NULL;
566 }
567
568 BT_EXPORT_API int bluetooth_hid_device_init(hid_cb_func_ptr callback_ptr, void *user_data)
569 {
570         int ret;
571
572         /* Register HID Device events */
573         BT_INFO("BT_HID_DEVICE_EVENT");
574         ret = _bt_register_event(BT_HID_DEVICE_EVENT , (void *)callback_ptr, user_data);
575
576         if (ret != BLUETOOTH_ERROR_NONE &&
577              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
578                 BT_ERR("Fail to init the event handler");
579                 return ret;
580         }
581
582         _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
583
584         return BLUETOOTH_ERROR_NONE;
585 }
586
587 BT_EXPORT_API int bluetooth_hid_device_deinit(void)
588 {
589         int ret;
590
591         ret = _bt_unregister_event(BT_HID_DEVICE_EVENT);
592
593         if (ret != BLUETOOTH_ERROR_NONE) {
594                 BT_ERR("Fail to deinit the event handler");
595                 return ret;
596         }
597
598         _bt_set_user_data(BT_HID, NULL, NULL);
599
600         return BLUETOOTH_ERROR_NONE;
601 }
602
603 BT_EXPORT_API int bluetooth_hid_device_activate(void)
604 {
605         bt_register_profile_info_t profile_info;
606         int result = BLUETOOTH_ERROR_NONE;
607
608         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_ACTIVATE)
609                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
610                 BT_ERR("Don't have a privilege to use this API");
611                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
612         }
613
614         if (hid_info != NULL)
615                 return BLUETOOTH_ERROR_IN_PROGRESS;
616
617         hid_info = __register_method();
618         if (hid_info == NULL)
619                 return BLUETOOTH_ERROR_INTERNAL;
620
621         hid_info->uuid = g_strdup(HID_DEVICE_UUID);
622
623         profile_info.authentication = TRUE;
624         profile_info.authorization = TRUE;
625         profile_info.obj_path = hid_info->path;
626         profile_info.role = g_strdup("Hid");
627         profile_info.service = hid_info->uuid;
628         profile_info.uuid = hid_info->uuid;
629
630         BT_INFO("uuid %s", profile_info.uuid);
631         result = _bt_register_profile(&profile_info, FALSE);
632
633         return result;
634 }
635
636 BT_EXPORT_API int bluetooth_hid_device_deactivate(void)
637 {
638         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DEACTIVATE)
639                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
640                 BT_ERR("Don't have a privilege to use this API");
641                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
642         }
643
644         if (hid_info == NULL)
645                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
646
647         _bt_unregister_profile(hid_info->path);
648
649         _bluetooth_hid_free_hid_info();
650
651         return BLUETOOTH_ERROR_NONE;
652 }
653
654 BT_EXPORT_API int bluetooth_hid_device_connect(const char *remote_addr)
655 {
656         char device_address[BT_ADDRESS_STRING_SIZE] = {0};
657         hid_connected_device_info_t *info = NULL;
658         int ret;
659         BT_DBG("+");
660         BT_CHECK_PARAMETER(remote_addr, return);
661
662         info = __find_hid_info_with_address(remote_addr);
663         if (info) {
664                 BT_ERR("Connection Already Exists");
665                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
666         }
667         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_CONNECT)
668                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
669                 BT_ERR("Don't have a privilege to use this API");
670                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
671         }
672
673         memcpy(device_address, remote_addr, BT_ADDRESS_STRING_SIZE);
674         ret = _bt_connect_profile(device_address, HID_DEVICE_UUID,
675                                 __hid_connect_response_cb, NULL);
676
677         return ret;
678 }
679 BT_EXPORT_API int bluetooth_hid_device_disconnect(const char *remote_addr)
680 {
681         BT_CHECK_PARAMETER(remote_addr, return);
682
683         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DISCONNECT)
684                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
685                 BT_ERR("Don't have a privilege to use this API");
686                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
687         }
688
689         hid_connected_device_info_t *info = NULL;
690
691         info = __find_hid_info_with_address(remote_addr);
692         if (info == NULL)
693                 return BLUETOOTH_ERROR_INVALID_PARAM;
694
695         _bt_disconnect_profile((char *)remote_addr, HID_DEVICE_UUID, NULL, NULL);
696
697         info->disconnect_idle_id = g_idle_add((GSourceFunc)__hid_disconnect, info);
698
699         BT_DBG("-");
700         return BLUETOOTH_ERROR_NONE;
701 }
702 BT_EXPORT_API int bluetooth_hid_device_send_mouse_event(const char *remote_addr,
703                                         hid_send_mouse_event_t send_event)
704 {
705         int result;
706         int written = 0;
707         hid_connected_device_info_t *info = NULL;
708
709         BT_CHECK_PARAMETER(remote_addr, return);
710
711         switch (privilege_token_send_mouse) {
712         case 0:
713                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_MOUSE_EVENT);
714
715                 if (result == BLUETOOTH_ERROR_NONE) {
716                         privilege_token_send_mouse = 1; /* Have a permission */
717                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
718                         BT_ERR("Don't have a privilege to use this API");
719                         privilege_token_send_mouse = -1; /* Don't have a permission */
720                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
721                 } else {
722                         /* Just break - It is not related with permission error */
723                 }
724                 break;
725         case 1:
726                 /* Already have a privilege */
727                 break;
728         case -1:
729                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
730         default:
731                 /* Invalid privilge token value */
732                 return BLUETOOTH_ERROR_INTERNAL;
733         }
734         info = __find_hid_info_with_address(remote_addr);
735         if (info == NULL) {
736                 BT_ERR("Connection Information not found");
737                 return BLUETOOTH_ERROR_INVALID_PARAM;
738         }
739         int socket_fd;
740
741         if (info->intr_fd != -1 && info->ctrl_fd == -1)
742                 socket_fd = info->intr_fd;
743         else
744                 socket_fd = info->ctrl_fd;
745
746         written = write(socket_fd, &send_event, sizeof(send_event));
747
748         return written;
749 }
750
751 BT_EXPORT_API int bluetooth_hid_device_send_key_event(const char *remote_addr,
752                                         hid_send_key_event_t send_event)
753 {
754         int result;
755         int written = 0;
756         hid_connected_device_info_t *info = NULL;
757
758         BT_CHECK_PARAMETER(remote_addr, return);
759
760         switch (privilege_token_send_key) {
761         case 0:
762                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_KEY_EVENT);
763
764                 if (result == BLUETOOTH_ERROR_NONE) {
765                         privilege_token_send_key = 1; /* Have a permission */
766                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
767                         BT_ERR("Don't have a privilege to use this API");
768                         privilege_token_send_key = -1; /* Don't have a permission */
769                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
770                 } else {
771                         /* Just break - It is not related with permission error */
772                 }
773                 break;
774         case 1:
775                 /* Already have a privilege */
776                 break;
777         case -1:
778                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
779         default:
780                 /* Invalid privilge token value */
781                 return BLUETOOTH_ERROR_INTERNAL;
782         }
783
784         info = __find_hid_info_with_address(remote_addr);
785         if (info == NULL) {
786                 BT_ERR("Connection Information not found");
787                 return BLUETOOTH_ERROR_INVALID_PARAM;
788         }
789
790         int socket_fd;
791
792         if (info->intr_fd != -1 && info->ctrl_fd == -1)
793                 socket_fd = info->intr_fd;
794         else
795                 socket_fd = info->ctrl_fd;
796
797         written = write(socket_fd, &send_event, sizeof(send_event));
798         return written;
799 }
800
801 BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr,
802                         unsigned char btcode, unsigned char report_id,
803                         const char *data, unsigned int data_len)
804 {
805         int result;
806         int written = 0;
807         int socket_fd;
808         hid_connected_device_info_t *info = NULL;
809         char *send_event = NULL;
810
811         BT_CHECK_PARAMETER(remote_addr, return);
812         BT_CHECK_PARAMETER(data, return);
813
814         switch (privilege_token_send_key) {
815         case 0:
816                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_CUSTOM_EVENT);
817
818                 if (result == BLUETOOTH_ERROR_NONE) {
819                         privilege_token_send_key = 1; /* Have a permission */
820                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
821                         BT_ERR("Don't have a privilege to use this API");
822                         privilege_token_send_key = -1; /* Don't have a permission */
823                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
824                 } else {
825                         /* Just break - It is not related with permission error */
826                 }
827                 break;
828         case 1:
829                 /* Already have a privilege */
830                 break;
831         case -1:
832                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
833         default:
834                 /* Invalid privilge token value */
835                 return BLUETOOTH_ERROR_INTERNAL;
836         }
837
838         info = __find_hid_info_with_address(remote_addr);
839         if (info == NULL) {
840                 BT_ERR("Connection Information not found");
841                 return BLUETOOTH_ERROR_INVALID_PARAM;
842         }
843
844         if (info->intr_fd != -1 && info->ctrl_fd == -1)
845                 socket_fd = info->intr_fd;
846         else
847                 socket_fd = info->ctrl_fd;
848
849         send_event = (char*)g_malloc0(data_len + 2);
850         if (send_event == NULL)
851                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
852
853         send_event[0] = (char)btcode;
854         send_event[1] = (char)report_id;
855         memcpy(send_event + 2, data, data_len);
856
857         written = write(socket_fd, send_event, data_len + 2);
858
859         g_free(send_event);
860
861         return written;
862 }
863
864 BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr,
865                                 bt_hid_header_type_t htype,
866                                 bt_hid_param_type_t ptype,
867                                 const char *data,
868                                 unsigned int data_len)
869 {
870         int result;
871         struct reports output_report = { 0 };
872         int bytes = 0;
873         hid_connected_device_info_t *info = NULL;
874         info = __find_hid_info_with_address(remote_addr);
875         if (info == NULL) {
876                 BT_ERR("Connection Information not found");
877                 return BLUETOOTH_ERROR_INVALID_PARAM;
878         }
879
880         BT_CHECK_PARAMETER(remote_addr, return);
881
882         switch (privilege_token_reply) {
883         case 0:
884                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_REPLY_TO_REPORT);
885
886                 if (result == BLUETOOTH_ERROR_NONE) {
887                         privilege_token_reply = 1; /* Have a permission */
888                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
889                         BT_ERR("Don't have a privilege to use this API");
890                         privilege_token_reply = -1; /* Don't have a permission */
891                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
892                 } else {
893                         /* Just break - It is not related with permission error */
894                 }
895                 break;
896         case 1:
897                 /* Already have a privilege */
898                 break;
899         case -1:
900                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
901         default:
902                 /* Invalid privilge token value */
903                 return BLUETOOTH_ERROR_INTERNAL;
904         }
905
906         BT_INFO("htype %d ptype %d", htype, ptype);
907         switch (htype) {
908                 case HTYPE_TRANS_GET_REPORT: {
909                         switch (ptype) {
910                                 case PTYPE_DATA_RTYPE_INPUT: {
911                                         output_report.type = BT_HID_TRANS_DATA |
912                                                         BT_HID_DATA_RTYPE_INPUT;
913                                         memcpy(output_report.rep_data, data, data_len);
914                                         bytes = write(info->intr_fd, &output_report,
915                                                                 sizeof(output_report));
916                                         BT_DBG("Bytes Written %d", bytes);
917                                         break;
918                                 }
919                                 default:
920                                         BT_INFO("Not Supported");
921                                         break;
922                         }
923                         break;
924                 case HTYPE_TRANS_GET_PROTOCOL: {
925                         BT_DBG("Replying to Get_PROTOCOL");
926                         output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_OUTPUT;
927                         output_report.rep_data[0] = data[0];
928                         bytes = write(info->intr_fd, &output_report, 2);
929                         BT_DBG("Bytes Written %d", bytes);
930                         break;
931                 }
932                 case HTYPE_TRANS_SET_PROTOCOL: {
933                         BT_DBG("Reply to Set_Protocol");
934                         output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT;
935                         memcpy(output_report.rep_data, data, data_len);
936                         bytes = write(info->ctrl_fd, &output_report,
937                                         sizeof(output_report));
938                         BT_DBG("Bytes Written %d", bytes);
939                         break;
940                 }
941                 case HTYPE_TRANS_HANDSHAKE: {
942                         BT_DBG("Replying Handshake");
943                         output_report.type = BT_HID_TRANS_HANDSHAKE | data[0];
944                         memset(output_report.rep_data, 0, sizeof(output_report.rep_data));
945                         bytes = write(info->intr_fd,  &output_report.type,
946                                         sizeof(output_report.type));
947                         BT_DBG("Bytes Written %d", bytes);
948                         break;
949                 }
950                         default:
951                                 break;
952                 }
953         }
954         return bytes;
955 }