2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <gio/gunixfdlist.h>
19 #include <sys/socket.h>
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"
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
33 /* HIDP header masks */
34 #define BT_HID_HEADER_TRANS_MASK 0xf0
35 #define BT_HID_HEADER_PARAM_MASK 0x0f
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
49 #define BT_HID_DATA_RTYPE_INPUT 0x01
50 #define BT_HID_DATA_RTYPE_OUTPUT 0x02
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
71 GIOChannel *ctrl_data_io;
72 GIOChannel *intr_data_io;
76 guint disconnect_idle_id;
77 } hid_connected_device_info_t;
82 } __attribute__((__packed__));
84 static hid_info_t *hid_info = NULL;
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
92 static int privilege_token_send_mouse = 0;
93 static int privilege_token_send_key = 0;
94 static int privilege_token_reply = 0;
96 static gboolean __hid_disconnect(hid_connected_device_info_t *info);
98 int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr)
101 int ret = BLUETOOTH_ERROR_NONE;
103 GVariant *result = NULL;
105 GDBusConnection *conn;
106 GDBusProxy *server_proxy;
109 GUnixFDList *out_fd_list = NULL;
110 conn = _bt_gdbus_get_system_gconn();
111 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
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,
118 adapter_path, "org.bluez.Input1", NULL, NULL);
119 g_free(adapter_path);
121 if (server_proxy == NULL) {
122 BT_ERR("Failed to get the network server proxy\n");
123 return BLUETOOTH_ERROR_INTERNAL;
126 result = g_dbus_proxy_call_with_unix_fd_list_sync(server_proxy, "GetFD",
128 G_DBUS_CALL_FLAGS_NONE,
134 if (result == 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;
141 ret = BLUETOOTH_ERROR_INTERNAL;
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);
152 g_object_unref(out_fd_list);
153 g_variant_unref(result);
155 g_object_unref(server_proxy);
159 static hid_connected_device_info_t *__find_hid_info_with_address(const char *remote_addr)
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)
171 static void __hid_connected_cb(hid_connected_device_info_t *info,
174 bluetooth_hid_request_t conn_info;
175 bt_event_info_t *event_info = NULL;
177 event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
178 if (event_info == NULL)
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;
185 conn_info.socket_fd = info->ctrl_fd;
186 _bt_convert_addr_string_to_type(conn_info.device_addr.addr, info->address);
188 if (result == BLUETOOTH_ERROR_NONE)
189 BT_INFO_C("Connected [HID Device]");
191 _bt_common_event_cb(BLUETOOTH_HID_DEVICE_CONNECTED,
193 event_info->cb, event_info->user_data);
196 static void __hid_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
201 GError *error = NULL;
202 GVariant *ret = NULL;
203 hid_connected_device_info_t info;
208 ret = g_dbus_proxy_call_finish(proxy, res, &error);
210 g_dbus_error_strip_remote_error(error);
211 BT_ERR("Error : %s \n", error->message);
213 if (g_strcmp0(error->message, "In Progress") == 0)
214 result = BLUETOOTH_ERROR_DEVICE_BUSY;
216 result = BLUETOOTH_ERROR_INTERNAL;
221 info.address = g_malloc0(BT_ADDRESS_STRING_SIZE);
223 path = g_dbus_proxy_get_object_path(proxy);
224 _bt_convert_device_path_to_address(path, info.address);
226 __hid_connected_cb(&info, result);
228 g_free(info.address);
232 g_variant_unref(ret);
236 g_object_unref(proxy);
241 static gboolean __hid_disconnect(hid_connected_device_info_t *info)
243 bluetooth_hid_request_t disconn_info;
244 int fd = info->ctrl_fd;
245 bt_event_info_t *event_info;
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;
253 if (info->intr_data_id > 0) {
254 g_source_remove(info->intr_data_id);
255 info->intr_data_id = 0;
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;
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;
268 if (info->intr_fd >= 0) {
269 close(info->ctrl_fd);
270 close(info->intr_fd);
274 info->disconnect_idle_id = 0;
275 event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
276 if (event_info == NULL)
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);
286 g_free(info->address);
293 void __free_hid_info(hid_info_t *info)
297 _bt_unregister_gdbus(info->object_id);
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;
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;
308 __hid_disconnect(dev_info);
316 static gboolean __is_error_by_disconnect(GError *err)
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");
323 static gboolean __received_cb(GIOChannel *chan, GIOCondition cond,
326 hid_connected_device_info_t *info = data;
327 GIOStatus status = G_IO_STATUS_NORMAL;
331 guint8 header, type, param;
332 bt_event_info_t *event_info;
333 retv_if(info == NULL, FALSE);
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;
342 __hid_disconnect(info);
346 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
348 if (status == G_IO_STATUS_NORMAL) {
349 BT_INFO("Parsing Data");
350 bluetooth_hid_received_data_t data = {0};
352 type = header & BT_HID_HEADER_TRANS_MASK;
353 param = header & BT_HID_HEADER_PARAM_MASK;
354 BT_INFO("type %d , param %d", type, param);
355 BT_INFO("Data Reveived from %s" , info->address);
357 event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
358 if (event_info == NULL)
361 data.address = g_strdup(info->address);
363 case BT_HID_TRANS_HANDSHAKE:
364 BT_INFO("TRANS HANDSHAKE");
365 data.type = HTYPE_TRANS_HANDSHAKE;
366 data.buffer_size = len;
367 data.buffer = (char *) malloc(sizeof(char) * len);
369 memcpy(data.buffer, buffer, len);
372 case BT_HID_TRANS_HID_CONTROL:
373 BT_INFO("HID CONTROL");
374 data.type = HTYPE_TRANS_HID_CONTROL;
375 data.buffer_size = len;
376 data.buffer = (char *) malloc(sizeof(char) * len);
378 memcpy(data.buffer, buffer, len);
381 case BT_HID_TRANS_DATA:
382 BT_INFO("TRANS DATA");
383 data.type = HTYPE_TRANS_DATA;
384 if (param & BT_HID_DATA_RTYPE_INPUT) {
385 BT_INFO("Input Report");
386 data.param = PTYPE_DATA_RTYPE_INPUT;
387 data.buffer_size = len;
388 data.buffer = (char *) malloc(sizeof(char) * len);
390 memcpy(data.buffer, buffer, len);
392 BT_INFO("Out Report");
393 data.param = PTYPE_DATA_RTYPE_OUTPUT;
394 data.buffer_size = len;
395 data.buffer = (char *) malloc(sizeof(char) * len);
397 memcpy(data.buffer, buffer, len);
401 case BT_HID_TRANS_GET_REPORT: {
402 BT_INFO("Get Report");
403 data.type = HTYPE_TRANS_GET_REPORT;
404 if (param & BT_HID_DATA_RTYPE_INPUT) {
405 BT_INFO("Input Report");
406 data.param = PTYPE_DATA_RTYPE_INPUT;
408 BT_INFO("Output Report");
409 data.param = PTYPE_DATA_RTYPE_OUTPUT;
411 data.buffer_size = len;
412 data.buffer = (char *) malloc(sizeof(char) * len);
414 memcpy(data.buffer, buffer, len);
418 case BT_HID_TRANS_SET_REPORT: {
419 BT_INFO("Set Report");
420 data.type = HTYPE_TRANS_SET_REPORT;
421 if (param & BT_HID_DATA_RTYPE_INPUT) {
422 BT_INFO("Input Report");
423 data.param = PTYPE_DATA_RTYPE_INPUT;
425 BT_INFO("Output Report");
426 data.param = PTYPE_DATA_RTYPE_OUTPUT;
428 data.buffer_size = len;
429 data.buffer = (char *) malloc(sizeof(char) * len);
431 memcpy(data.buffer, buffer, len);
435 case BT_HID_TRANS_GET_PROTOCOL:{
436 BT_INFO("Get_PROTOCOL");
437 data.type = HTYPE_TRANS_GET_PROTOCOL;
438 data.param = PTYPE_DATA_RTYPE_INPUT;
439 data.buffer_size = len;
440 data.buffer = (char *) malloc(sizeof(char) * len);
442 memcpy(data.buffer, buffer, len);
446 case BT_HID_TRANS_SET_PROTOCOL:{
447 BT_INFO("Set_PROTOCOL");
448 data.type = HTYPE_TRANS_SET_PROTOCOL;
449 data.param = PTYPE_DATA_RTYPE_INPUT;
450 data.buffer_size = len;
451 data.buffer = (char *) malloc(sizeof(char) * len);
453 memcpy(data.buffer, buffer, len);
458 BT_INFO("unsupported HIDP control message");
459 BT_ERR("Send Handshake Message");
460 guint8 type = BT_HID_TRANS_HANDSHAKE |
461 BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST;
462 data.type = HTYPE_TRANS_UNKNOWN;
463 int fd = g_io_channel_unix_get_fd(chan);
464 int bytes = write(fd, &type, sizeof(type));
465 BT_INFO("Bytes Written %d", bytes);
470 _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DATA_RECEIVED,
471 BLUETOOTH_ERROR_NONE, &data,
472 event_info->cb, event_info->user_data);
477 g_free((char *)data.address);
479 BT_ERR("Error while reading data %d [%s]", status, info->address);
481 BT_ERR("IO Channel read error [%s]", err->message);
482 if (status == G_IO_STATUS_ERROR &&
483 __is_error_by_disconnect(err)) {
484 BT_DBG("cond : %d", cond);
486 __hid_disconnect(info);
490 } else if (status == G_IO_STATUS_EOF) {
491 __hid_disconnect(info);
498 int new_hid_connection(const char *path, int fd, bluetooth_device_address_t *addr)
500 hid_info_t *info = NULL;
501 hid_connected_device_info_t *dev_info = NULL;
507 _bt_convert_addr_type_to_string((char *)address, addr->addr);
508 BT_INFO("Address [%s]", address);
509 dev_info = __find_hid_info_with_address(address);
510 if (dev_info == NULL) {
511 dev_info = (hid_connected_device_info_t *)
512 g_malloc0(sizeof(hid_connected_device_info_t));
514 dev_info->intr_fd = -1;
515 dev_info->ctrl_fd = -1;
516 dev_info->intr_fd = fd;
517 dev_info->address = g_strdup(address);
518 dev_info->intr_data_io = g_io_channel_unix_new(dev_info->intr_fd);
519 g_io_channel_set_encoding(dev_info->intr_data_io, NULL, NULL);
520 g_io_channel_set_flags(dev_info->intr_data_io, G_IO_FLAG_NONBLOCK, NULL);
521 g_io_channel_set_close_on_unref(dev_info->intr_data_io, TRUE);
522 dev_info->intr_data_id = g_io_add_watch(dev_info->intr_data_io,
523 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
524 __received_cb, dev_info);
525 hid_info->device_list = g_slist_append(hid_info->device_list, dev_info);
527 dev_info->ctrl_fd = fd;
528 dev_info->ctrl_data_io = g_io_channel_unix_new(dev_info->ctrl_fd);
529 g_io_channel_set_encoding(dev_info->ctrl_data_io, NULL, NULL);
530 g_io_channel_set_flags(dev_info->ctrl_data_io, G_IO_FLAG_NONBLOCK, NULL);
531 g_io_channel_set_close_on_unref(dev_info->ctrl_data_io, TRUE);
532 dev_info->ctrl_data_id = g_io_add_watch(dev_info->ctrl_data_io,
533 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
534 __received_cb, dev_info);
537 if (dev_info->ctrl_fd != -1 && dev_info->intr_fd != -1)
538 __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE);
542 static hid_info_t *__register_method()
545 hid_info_t *info = NULL;
547 path = g_strdup_printf("/org/socket/server/%d", getpid());
549 object_id = _bt_register_new_conn(path, new_hid_connection);
553 info = g_new(hid_info_t, 1);
554 info->object_id = (guint)object_id;
557 info->device_list = NULL;
562 void _bluetooth_hid_free_hid_info(void)
564 if (hid_info == NULL) {
565 BT_DBG("hid_info is already NULL");
569 __free_hid_info(hid_info);
573 BT_EXPORT_API int bluetooth_hid_device_init(hid_cb_func_ptr callback_ptr, void *user_data)
577 /* Register HID Device events */
578 BT_INFO("BT_HID_DEVICE_EVENT");
579 ret = _bt_register_event(BT_HID_DEVICE_EVENT , (void *)callback_ptr, user_data);
581 if (ret != BLUETOOTH_ERROR_NONE &&
582 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
583 BT_ERR("Fail to init the event handler");
587 _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
589 return BLUETOOTH_ERROR_NONE;
592 BT_EXPORT_API int bluetooth_hid_device_deinit(void)
596 ret = _bt_unregister_event(BT_HID_DEVICE_EVENT);
598 if (ret != BLUETOOTH_ERROR_NONE) {
599 BT_ERR("Fail to deinit the event handler");
603 _bt_set_user_data(BT_HID, NULL, NULL);
605 return BLUETOOTH_ERROR_NONE;
608 BT_EXPORT_API int bluetooth_hid_device_activate(void)
610 bt_register_profile_info_t profile_info;
611 int result = BLUETOOTH_ERROR_NONE;
613 BT_CHECK_ENABLED(return);
615 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_ACTIVATE)
616 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
617 BT_ERR("Don't have a privilege to use this API");
618 return BLUETOOTH_ERROR_PERMISSION_DEINED;
621 if (hid_info != NULL)
622 return BLUETOOTH_ERROR_IN_PROGRESS;
624 hid_info = __register_method();
625 if (hid_info == NULL)
626 return BLUETOOTH_ERROR_INTERNAL;
628 hid_info->uuid = g_strdup(HID_DEVICE_UUID);
630 profile_info.authentication = TRUE;
631 profile_info.authorization = TRUE;
632 profile_info.obj_path = hid_info->path;
633 profile_info.role = "Hid";
634 profile_info.service = hid_info->uuid;
635 profile_info.uuid = hid_info->uuid;
637 BT_INFO("uuid %s", profile_info.uuid);
638 result = _bt_register_profile(&profile_info, FALSE);
643 BT_EXPORT_API int bluetooth_hid_device_deactivate(void)
645 BT_CHECK_ENABLED(return);
647 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DEACTIVATE)
648 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
649 BT_ERR("Don't have a privilege to use this API");
650 return BLUETOOTH_ERROR_PERMISSION_DEINED;
653 if (hid_info == NULL)
654 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
656 _bt_unregister_profile(hid_info->path);
658 _bluetooth_hid_free_hid_info();
660 return BLUETOOTH_ERROR_NONE;
663 BT_EXPORT_API int bluetooth_hid_device_connect(const char *remote_addr)
665 char device_address[BT_ADDRESS_STRING_SIZE] = {0};
666 hid_connected_device_info_t *info = NULL;
669 BT_CHECK_PARAMETER(remote_addr, return);
671 BT_CHECK_ENABLED(return);
673 info = __find_hid_info_with_address(remote_addr);
675 BT_ERR("Connection Already Exists");
676 return BLUETOOTH_ERROR_ALREADY_CONNECT;
678 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_CONNECT)
679 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
680 BT_ERR("Don't have a privilege to use this API");
681 return BLUETOOTH_ERROR_PERMISSION_DEINED;
684 memcpy(device_address, remote_addr, BT_ADDRESS_STRING_SIZE);
685 ret = _bt_connect_profile(device_address, HID_DEVICE_UUID,
686 __hid_connect_response_cb, NULL);
690 BT_EXPORT_API int bluetooth_hid_device_disconnect(const char *remote_addr)
692 BT_CHECK_PARAMETER(remote_addr, return);
694 BT_CHECK_ENABLED(return);
696 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DISCONNECT)
697 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
698 BT_ERR("Don't have a privilege to use this API");
699 return BLUETOOTH_ERROR_PERMISSION_DEINED;
702 hid_connected_device_info_t *info = NULL;
704 info = __find_hid_info_with_address(remote_addr);
706 return BLUETOOTH_ERROR_INVALID_PARAM;
708 _bt_disconnect_profile((char *)remote_addr, HID_DEVICE_UUID, NULL, NULL);
710 info->disconnect_idle_id = g_idle_add((GSourceFunc)__hid_disconnect, info);
713 return BLUETOOTH_ERROR_NONE;
715 BT_EXPORT_API int bluetooth_hid_device_send_mouse_event(const char *remote_addr,
716 hid_send_mouse_event_t send_event)
720 hid_connected_device_info_t *info = NULL;
722 BT_CHECK_PARAMETER(remote_addr, return);
724 switch (privilege_token_send_mouse) {
726 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_MOUSE_EVENT);
728 if (result == BLUETOOTH_ERROR_NONE) {
729 privilege_token_send_mouse = 1; /* Have a permission */
730 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
731 BT_ERR("Don't have a privilege to use this API");
732 privilege_token_send_mouse = -1; /* Don't have a permission */
733 return BLUETOOTH_ERROR_PERMISSION_DEINED;
735 /* Just break - It is not related with permission error */
739 /* Already have a privilege */
742 return BLUETOOTH_ERROR_PERMISSION_DEINED;
744 /* Invalid privilge token value */
745 return BLUETOOTH_ERROR_INTERNAL;
747 info = __find_hid_info_with_address(remote_addr);
749 BT_ERR("Connection Information not found");
750 return BLUETOOTH_ERROR_INVALID_PARAM;
754 if (info->intr_fd != -1 && info->ctrl_fd == -1)
755 socket_fd = info->intr_fd;
757 socket_fd = info->ctrl_fd;
759 written = write(socket_fd, &send_event, sizeof(send_event));
764 BT_EXPORT_API int bluetooth_hid_device_send_key_event(const char *remote_addr,
765 hid_send_key_event_t send_event)
769 hid_connected_device_info_t *info = NULL;
771 BT_CHECK_PARAMETER(remote_addr, return);
773 switch (privilege_token_send_key) {
775 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_KEY_EVENT);
777 if (result == BLUETOOTH_ERROR_NONE) {
778 privilege_token_send_key = 1; /* Have a permission */
779 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
780 BT_ERR("Don't have a privilege to use this API");
781 privilege_token_send_key = -1; /* Don't have a permission */
782 return BLUETOOTH_ERROR_PERMISSION_DEINED;
784 /* Just break - It is not related with permission error */
788 /* Already have a privilege */
791 return BLUETOOTH_ERROR_PERMISSION_DEINED;
793 /* Invalid privilge token value */
794 return BLUETOOTH_ERROR_INTERNAL;
797 info = __find_hid_info_with_address(remote_addr);
799 BT_ERR("Connection Information not found");
800 return BLUETOOTH_ERROR_INVALID_PARAM;
805 if (info->intr_fd != -1 && info->ctrl_fd == -1)
806 socket_fd = info->intr_fd;
808 socket_fd = info->ctrl_fd;
810 written = write(socket_fd, &send_event, sizeof(send_event));
814 BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr,
815 unsigned char btcode, unsigned char report_id,
816 const char *data, unsigned int data_len)
821 hid_connected_device_info_t *info = NULL;
822 char *send_event = NULL;
824 BT_CHECK_PARAMETER(remote_addr, return);
825 BT_CHECK_PARAMETER(data, return);
827 switch (privilege_token_send_key) {
829 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_CUSTOM_EVENT);
831 if (result == BLUETOOTH_ERROR_NONE) {
832 privilege_token_send_key = 1; /* Have a permission */
833 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
834 BT_ERR("Don't have a privilege to use this API");
835 privilege_token_send_key = -1; /* Don't have a permission */
836 return BLUETOOTH_ERROR_PERMISSION_DEINED;
838 /* Just break - It is not related with permission error */
842 /* Already have a privilege */
845 return BLUETOOTH_ERROR_PERMISSION_DEINED;
847 /* Invalid privilge token value */
848 return BLUETOOTH_ERROR_INTERNAL;
851 info = __find_hid_info_with_address(remote_addr);
853 BT_ERR("Connection Information not found");
854 return BLUETOOTH_ERROR_INVALID_PARAM;
857 if (info->intr_fd != -1 && info->ctrl_fd == -1)
858 socket_fd = info->intr_fd;
860 socket_fd = info->ctrl_fd;
862 send_event = g_malloc0(data_len + 2);
864 send_event[0] = (char)btcode;
865 send_event[1] = (char)report_id;
866 memcpy(send_event + 2, data, data_len);
868 written = write(socket_fd, send_event, data_len + 2);
875 BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr,
876 bt_hid_header_type_t htype,
877 bt_hid_param_type_t ptype,
879 unsigned int data_len)
882 struct reports output_report = { 0 };
884 hid_connected_device_info_t *info = NULL;
885 info = __find_hid_info_with_address(remote_addr);
887 BT_ERR("Connection Information not found");
888 return BLUETOOTH_ERROR_INVALID_PARAM;
891 BT_CHECK_PARAMETER(remote_addr, return);
893 switch (privilege_token_reply) {
895 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_REPLY_TO_REPORT);
897 if (result == BLUETOOTH_ERROR_NONE) {
898 privilege_token_reply = 1; /* Have a permission */
899 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
900 BT_ERR("Don't have a privilege to use this API");
901 privilege_token_reply = -1; /* Don't have a permission */
902 return BLUETOOTH_ERROR_PERMISSION_DEINED;
904 /* Just break - It is not related with permission error */
908 /* Already have a privilege */
911 return BLUETOOTH_ERROR_PERMISSION_DEINED;
913 /* Invalid privilge token value */
914 return BLUETOOTH_ERROR_INTERNAL;
917 BT_INFO("htype %d ptype %d", htype, ptype);
919 case HTYPE_TRANS_GET_REPORT: {
921 case PTYPE_DATA_RTYPE_INPUT: {
922 output_report.type = BT_HID_TRANS_DATA |
923 BT_HID_DATA_RTYPE_INPUT;
924 memcpy(output_report.rep_data, data, data_len);
925 bytes = write(info->intr_fd, &output_report,
926 sizeof(output_report));
927 BT_DBG("Bytes Written %d", bytes);
931 BT_INFO("Not Supported");
936 case HTYPE_TRANS_GET_PROTOCOL: {
937 BT_DBG("Replying to Get_PROTOCOL");
938 output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_OUTPUT;
939 output_report.rep_data[0] = data[0];
940 bytes = write(info->intr_fd, &output_report, 2);
941 BT_DBG("Bytes Written %d", bytes);
945 case HTYPE_TRANS_SET_PROTOCOL: {
946 BT_DBG("Reply to Set_Protocol");
947 output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT;
948 memcpy(output_report.rep_data, data, data_len);
949 bytes = write(info->ctrl_fd, &output_report,
950 sizeof(output_report));
951 BT_DBG("Bytes Written %d", bytes);
955 case HTYPE_TRANS_HANDSHAKE: {
956 BT_DBG("Replying Handshake");
957 output_report.type = BT_HID_TRANS_HANDSHAKE | data[0];
958 memset(output_report.rep_data, 0, sizeof(output_report.rep_data));
959 bytes = write(info->intr_fd, &output_report.type,
960 sizeof(output_report.type));
961 BT_DBG("Bytes Written %d", bytes);