From 07aebdf6ae78fc1e0ed441b867d1fffee4abf705 Mon Sep 17 00:00:00 2001 From: Deokhyun Kim Date: Wed, 25 Oct 2017 10:38:27 +0900 Subject: [PATCH] Cleanup HID device codes. Change-Id: I699a10bf46bbc8a4af43f1fb587c0771c26a0261 Signed-off-by: Deokhyun Kim Signed-off-by: DoHyun Pyun --- bt-api/bt-event-handler.c | 10 +- bt-api/bt-hid-device.c | 317 ++++++++++++++++++-------------------- bt-api/include/bt-common.h | 7 +- bt-api/include/bt-event-handler.h | 4 - include/bluetooth-api.h | 7 - 5 files changed, 165 insertions(+), 180 deletions(-) diff --git a/bt-api/bt-event-handler.c b/bt-api/bt-event-handler.c index 956562e..d91628b 100644 --- a/bt-api/bt-event-handler.c +++ b/bt-api/bt-event-handler.c @@ -375,7 +375,7 @@ gboolean __bt_reliable_disable_cb(gpointer user_data) _bt_rfcomm_server_free_all(); #endif - _bluetooth_hid_free_hid_info(); + _bt_hid_free_hid_info(); BT_DBG("-"); return FALSE; @@ -1284,10 +1284,10 @@ void __bt_hid_device_event_filter(GDBusConnection *connection, address); int ctrl = -1, intr = -1; _bt_hid_device_get_fd(address, &ctrl, &intr); - if (ctrl != -1 && intr != -1) { - new_hid_connection(NULL, ctrl, &dev_address); - new_hid_connection(NULL, intr, &dev_address); - } + if (ctrl != -1 && intr != -1) + _bt_hid_new_connection(&dev_address, ctrl, intr); + else + BT_ERR("fd is invalid.(ctrl=%d, intr=%d)", ctrl, intr); } else if (strcasecmp(signal_name, BT_INPUT_HID_DEVICE_DISCONNECTED) == 0) { const char *address = NULL; g_variant_get(parameters, "(i&s)", &result, &address); diff --git a/bt-api/bt-hid-device.c b/bt-api/bt-hid-device.c index db0f6bb..21a7da0 100644 --- a/bt-api/bt-hid-device.c +++ b/bt-api/bt-hid-device.c @@ -89,69 +89,6 @@ static int privilege_token_send_mouse = 0; static int privilege_token_send_key = 0; static int privilege_token_reply = 0; -static gboolean __hid_disconnect(hid_connected_device_info_t *info); - -int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr) -{ - - int ret = BLUETOOTH_ERROR_NONE; - char *adapter_path; - GVariant *result = NULL; - GError *err = NULL; - GDBusConnection *conn; - GDBusProxy *server_proxy; - int index1 = 0; - int index2 = 0; - GUnixFDList *out_fd_list = NULL; - conn = _bt_gdbus_get_system_gconn(); - retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL); - - adapter_path = _bt_get_device_object_path((char *)address); - retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL); - BT_INFO_C("Device : %s", adapter_path); - server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, - NULL, BT_BLUEZ_NAME, - adapter_path, "org.bluez.Input1", NULL, NULL); - g_free(adapter_path); - - if (server_proxy == NULL) { - BT_ERR("Failed to get the network server proxy\n"); - return BLUETOOTH_ERROR_INTERNAL; - } - - result = g_dbus_proxy_call_with_unix_fd_list_sync(server_proxy, "GetFD", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &out_fd_list, - NULL, - &err); - if (result == NULL) { - if (err != NULL) { - g_dbus_error_strip_remote_error(err); - BT_ERR("INPUT server register Error: %s\n", err->message); - if (g_strcmp0(err->message, "Already Exists") == 0) - ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED; - else - ret = BLUETOOTH_ERROR_INTERNAL; - - g_error_free(err); - } - } else { - g_variant_get(result, "(hh)", &index1, &index2); - int fd1 = g_unix_fd_list_get(out_fd_list, index1, NULL); - int fd2 = g_unix_fd_list_get(out_fd_list, index2, NULL); - - *ctrl = fd1; - *intr = fd2; - g_object_unref(out_fd_list); - g_variant_unref(result); - } - g_object_unref(server_proxy); - return ret; -} - static GVariant* __bt_hid_agent_dbus_send(const char *path, const char *interface, const char *method, GError **err, GVariant *parameters) @@ -192,25 +129,21 @@ static hid_connected_device_info_t *__find_hid_info_with_address(const char *rem static void __hid_connected_cb(hid_connected_device_info_t *info, int result) { - bluetooth_hid_request_t conn_info; + bluetooth_device_address_t bd_addr; bt_event_info_t *event_info = NULL; event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT); if (event_info == NULL) return; - memset(&conn_info, 0x00, sizeof(bluetooth_hid_request_t)); - if (info->intr_fd != -1 && info->ctrl_fd == -1) - conn_info.socket_fd = info->intr_fd; - else - conn_info.socket_fd = info->ctrl_fd; - _bt_convert_addr_string_to_type(conn_info.device_addr.addr, info->address); + memset(&bd_addr, 0x00, sizeof(bluetooth_device_address_t)); + _bt_convert_addr_string_to_type(bd_addr.addr, info->address); if (result == BLUETOOTH_ERROR_NONE) BT_INFO_C("Connected [HID Device]"); _bt_common_event_cb(BLUETOOTH_HID_DEVICE_CONNECTED, - result, &conn_info, + result, &bd_addr, event_info->cb, event_info->user_data); } @@ -261,8 +194,7 @@ static void __hid_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res, static gboolean __hid_disconnect(hid_connected_device_info_t *info) { - bluetooth_hid_request_t disconn_info; - int fd = info->ctrl_fd; + bluetooth_device_address_t bd_addr; bt_event_info_t *event_info; BT_INFO_C("Disconnected [HID Device]"); @@ -297,11 +229,10 @@ static gboolean __hid_disconnect(hid_connected_device_info_t *info) if (event_info == NULL) return FALSE; - memset(&disconn_info, 0x00, sizeof(bluetooth_hid_request_t)); - disconn_info.socket_fd = fd; - _bt_convert_addr_string_to_type(disconn_info.device_addr.addr , info->address); + memset(&bd_addr, 0x00, sizeof(bluetooth_device_address_t)); + _bt_convert_addr_string_to_type(bd_addr.addr , info->address); _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DISCONNECTED, - BLUETOOTH_ERROR_NONE, &disconn_info, + BLUETOOTH_ERROR_NONE, &bd_addr, event_info->cb, event_info->user_data); if (info->address) g_free(info->address); @@ -515,47 +446,6 @@ static gboolean __received_cb(GIOChannel *chan, GIOCondition cond, return TRUE; } -int new_hid_connection(const char *path, int fd, bluetooth_device_address_t *addr) -{ - hid_connected_device_info_t *dev_info = NULL; - char address[18]; - - _bt_convert_addr_type_to_string((char *)address, addr->addr); - BT_INFO("Address [%s]", address); - dev_info = __find_hid_info_with_address(address); - if (dev_info == NULL) { - dev_info = (hid_connected_device_info_t *) - g_malloc0(sizeof(hid_connected_device_info_t)); - - dev_info->intr_fd = -1; - dev_info->ctrl_fd = -1; - dev_info->intr_fd = fd; - dev_info->address = g_strdup(address); - dev_info->intr_data_io = g_io_channel_unix_new(dev_info->intr_fd); - g_io_channel_set_encoding(dev_info->intr_data_io, NULL, NULL); - g_io_channel_set_flags(dev_info->intr_data_io, G_IO_FLAG_NONBLOCK, NULL); - g_io_channel_set_close_on_unref(dev_info->intr_data_io, TRUE); - dev_info->intr_data_id = g_io_add_watch(dev_info->intr_data_io, - G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - __received_cb, dev_info); - device_list = g_slist_append(device_list, dev_info); - } else { - dev_info->ctrl_fd = fd; - dev_info->ctrl_data_io = g_io_channel_unix_new(dev_info->ctrl_fd); - g_io_channel_set_encoding(dev_info->ctrl_data_io, NULL, NULL); - g_io_channel_set_flags(dev_info->ctrl_data_io, G_IO_FLAG_NONBLOCK, NULL); - g_io_channel_set_close_on_unref(dev_info->ctrl_data_io, TRUE); - dev_info->ctrl_data_id = g_io_add_watch(dev_info->ctrl_data_io, - G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - __received_cb, dev_info); - } - - if (dev_info->ctrl_fd != -1 && dev_info->intr_fd != -1) - __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE); - - return 0; -} - static void __free_hid_info(void *data) { BT_DBG(""); @@ -570,7 +460,109 @@ static void __free_hid_info(void *data) __hid_disconnect(dev_info); } -void _bluetooth_hid_free_hid_info(void) +int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr) +{ + + int ret = BLUETOOTH_ERROR_NONE; + char *adapter_path; + GVariant *result = NULL; + GError *err = NULL; + GDBusConnection *conn; + GDBusProxy *server_proxy; + int index1 = 0; + int index2 = 0; + GUnixFDList *out_fd_list = NULL; + conn = _bt_gdbus_get_system_gconn(); + retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL); + + adapter_path = _bt_get_device_object_path((char *)address); + retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL); + BT_INFO_C("Device : %s", adapter_path); + server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, + NULL, BT_BLUEZ_NAME, + adapter_path, "org.bluez.Input1", NULL, NULL); + g_free(adapter_path); + + if (server_proxy == NULL) { + BT_ERR("Failed to get the network server proxy\n"); + return BLUETOOTH_ERROR_INTERNAL; + } + + result = g_dbus_proxy_call_with_unix_fd_list_sync(server_proxy, "GetFD", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &out_fd_list, + NULL, + &err); + if (result == NULL) { + if (err != NULL) { + g_dbus_error_strip_remote_error(err); + BT_ERR("INPUT server register Error: %s\n", err->message); + if (g_strcmp0(err->message, "Already Exists") == 0) + ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED; + else + ret = BLUETOOTH_ERROR_INTERNAL; + + g_error_free(err); + } + } else { + g_variant_get(result, "(hh)", &index1, &index2); + int fd1 = g_unix_fd_list_get(out_fd_list, index1, NULL); + int fd2 = g_unix_fd_list_get(out_fd_list, index2, NULL); + + *ctrl = fd1; + *intr = fd2; + g_object_unref(out_fd_list); + g_variant_unref(result); + } + g_object_unref(server_proxy); + return ret; +} + +int _bt_hid_new_connection(bluetooth_device_address_t *addr, + int ctrl_fd, int intr_fd) +{ + hid_connected_device_info_t *dev_info = NULL; + char address[18]; + char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 }; + + _bt_convert_addr_type_to_string((char *)address, addr->addr); + _bt_convert_addr_string_to_secure_string(secure_addr, address); + BT_INFO("Address [%s]", secure_addr); + dev_info = __find_hid_info_with_address(address); + if (dev_info != NULL) + __free_hid_info(dev_info); + + dev_info = (hid_connected_device_info_t *) + g_malloc0(sizeof(hid_connected_device_info_t)); + + dev_info->ctrl_fd = ctrl_fd; + dev_info->intr_fd = intr_fd; + dev_info->address = g_strdup(address); + dev_info->ctrl_data_io = g_io_channel_unix_new(dev_info->ctrl_fd); + dev_info->intr_data_io = g_io_channel_unix_new(dev_info->intr_fd); + g_io_channel_set_encoding(dev_info->ctrl_data_io, NULL, NULL); + g_io_channel_set_flags(dev_info->ctrl_data_io, G_IO_FLAG_NONBLOCK, NULL); + g_io_channel_set_close_on_unref(dev_info->ctrl_data_io, TRUE); + g_io_channel_set_encoding(dev_info->intr_data_io, NULL, NULL); + g_io_channel_set_flags(dev_info->intr_data_io, G_IO_FLAG_NONBLOCK, NULL); + g_io_channel_set_close_on_unref(dev_info->intr_data_io, TRUE); + dev_info->ctrl_data_id = g_io_add_watch(dev_info->ctrl_data_io, + G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, + __received_cb, dev_info); + dev_info->intr_data_id = g_io_add_watch(dev_info->intr_data_io, + G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, + __received_cb, dev_info); + device_list = g_slist_append(device_list, dev_info); + + __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE); + + return 0; +} + +void _bt_hid_free_hid_info(void) { g_slist_free_full(device_list, __free_hid_info); @@ -681,7 +673,7 @@ BT_EXPORT_API int bluetooth_hid_device_deactivate(void) g_variant_unref(reply); - _bluetooth_hid_free_hid_info(); + _bt_hid_free_hid_info(); return BLUETOOTH_ERROR_NONE; } @@ -775,14 +767,14 @@ BT_EXPORT_API int bluetooth_hid_device_send_mouse_event(const char *remote_addr, BT_ERR("Connection Information not found"); return BLUETOOTH_ERROR_INVALID_PARAM; } - int socket_fd; - - if (info->intr_fd != -1 && info->ctrl_fd == -1) - socket_fd = info->intr_fd; - else - socket_fd = info->ctrl_fd; - written = write(socket_fd, &send_event, sizeof(send_event)); + if (info->intr_fd >= 0) { + written = write(info->intr_fd, &send_event, sizeof(send_event)); + } else { + BT_ERR("intr_fd(%d) is invalid.", info->intr_fd); + __free_hid_info(info); + return BLUETOOTH_ERROR_INTERNAL; + } return written; } @@ -826,14 +818,14 @@ BT_EXPORT_API int bluetooth_hid_device_send_key_event(const char *remote_addr, return BLUETOOTH_ERROR_INVALID_PARAM; } - int socket_fd; - - if (info->intr_fd != -1 && info->ctrl_fd == -1) - socket_fd = info->intr_fd; - else - socket_fd = info->ctrl_fd; + if (info->intr_fd >= 0) { + written = write(info->intr_fd, &send_event, sizeof(send_event)); + } else { + BT_ERR("intr_fd(%d) is invalid.", info->intr_fd); + __free_hid_info(info); + return BLUETOOTH_ERROR_INTERNAL; + } - written = write(socket_fd, &send_event, sizeof(send_event)); return written; } @@ -843,7 +835,6 @@ BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr { int result; int written = 0; - int socket_fd; hid_connected_device_info_t *info = NULL; char *send_event = NULL; @@ -880,20 +871,20 @@ BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr return BLUETOOTH_ERROR_INVALID_PARAM; } - if (info->intr_fd != -1 && info->ctrl_fd == -1) - socket_fd = info->intr_fd; - else - socket_fd = info->ctrl_fd; - - send_event = g_malloc0(data_len + 2); - - send_event[0] = (char)btcode; - send_event[1] = (char)report_id; - memcpy(send_event + 2, data, data_len); + if (info->intr_fd >= 0) { + send_event = g_malloc0(data_len + 2); - written = write(socket_fd, send_event, data_len + 2); + send_event[0] = (char)btcode; + send_event[1] = (char)report_id; + memcpy(send_event + 2, data, data_len); - g_free(send_event); + written = write(info->intr_fd, send_event, data_len + 2); + g_free(send_event); + } else { + BT_ERR("intr_fd(%d) is invalid.", info->intr_fd); + __free_hid_info(info); + return BLUETOOTH_ERROR_INTERNAL; + } return written; } @@ -906,15 +897,22 @@ BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr, { int result; struct reports output_report = { 0 }; - int bytes = BLUETOOTH_ERROR_INTERNAL; + int bytes = -1; hid_connected_device_info_t *info = NULL; + + BT_CHECK_PARAMETER(remote_addr, return); + info = __find_hid_info_with_address(remote_addr); if (info == NULL) { BT_ERR("Connection Information not found"); return BLUETOOTH_ERROR_INVALID_PARAM; } - BT_CHECK_PARAMETER(remote_addr, return); + if (info->ctrl_fd < 0) { + BT_ERR("ctrl_fd(%d) is invalid.", info->ctrl_fd); + __free_hid_info(info); + return BLUETOOTH_ERROR_INTERNAL; + } switch (privilege_token_reply) { case 0: @@ -948,7 +946,7 @@ BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr, output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT; memcpy(output_report.rep_data, data, data_len); - bytes = write(info->intr_fd, &output_report, + bytes = write(info->ctrl_fd, &output_report, sizeof(output_report)); BT_DBG("Bytes Written %d", bytes); break; @@ -960,50 +958,40 @@ BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr, break; case HTYPE_TRANS_GET_PROTOCOL: { - BT_DBG("Replying to Get_PROTOCOL"); output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_OUTPUT; output_report.rep_data[0] = data[0]; - bytes = write(info->intr_fd, &output_report, 2); - BT_DBG("Bytes Written %d", bytes); + bytes = write(info->ctrl_fd, &output_report, 2); break; } case HTYPE_TRANS_SET_PROTOCOL: { - BT_DBG("Reply to Set_Protocol"); output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT; memcpy(output_report.rep_data, data, data_len); bytes = write(info->ctrl_fd, &output_report, sizeof(output_report)); - BT_DBG("Bytes Written %d", bytes); break; } case HTYPE_TRANS_HANDSHAKE: { - BT_DBG("Replying Handshake"); output_report.type = BT_HID_TRANS_HANDSHAKE | data[0]; memset(output_report.rep_data, 0, sizeof(output_report.rep_data)); - bytes = write(info->intr_fd, &output_report.type, + bytes = write(info->ctrl_fd, &output_report.type, sizeof(output_report.type)); - BT_DBG("Bytes Written %d", bytes); break; } case HTYPE_TRANS_GET_IDLE: { - BT_DBG("Replying to Get_IDLE"); output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_OUTPUT; output_report.rep_data[0] = data[0]; - bytes = write(info->intr_fd, &output_report, 2); - BT_DBG("Bytes Written %d", bytes); + bytes = write(info->ctrl_fd, &output_report, 2); break; } case HTYPE_TRANS_SET_IDLE: { - BT_DBG("Reply to Set_IDLE"); output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT; memcpy(output_report.rep_data, data, data_len); bytes = write(info->ctrl_fd, &output_report, sizeof(output_report)); - BT_DBG("Bytes Written %d", bytes); break; } @@ -1011,5 +999,8 @@ BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr, break; } } + + BT_DBG("Bytes Written %d", bytes); + return bytes; } diff --git a/bt-api/include/bt-common.h b/bt-api/include/bt-common.h index ca829f3..2537f01 100644 --- a/bt-api/include/bt-common.h +++ b/bt-api/include/bt-common.h @@ -395,7 +395,12 @@ void _bt_rfcomm_client_disconnect_all(void); gboolean _check_uuid_path(char *path, char *uuid); #endif -void _bluetooth_hid_free_hid_info(void); +int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr); + +int _bt_hid_new_connection(bluetooth_device_address_t *addr, + int ctrl_fd, int intr_fd); + +void _bt_hid_free_hid_info(void); int _bt_get_error_value_from_message(const char *error_message); diff --git a/bt-api/include/bt-event-handler.h b/bt-api/include/bt-event-handler.h index 541c284..6050f9b 100644 --- a/bt-api/include/bt-event-handler.h +++ b/bt-api/include/bt-event-handler.h @@ -63,10 +63,6 @@ void _bt_unregister_name_owner_changed(void); int _bt_register_manager_subscribe_signal(gboolean subscribe); -int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr); - -int new_hid_connection(const char *path, int fd, bluetooth_device_address_t *addr); - #ifdef __cplusplus } #endif diff --git a/include/bluetooth-api.h b/include/bluetooth-api.h index 2d23b9c..0e555dd 100644 --- a/include/bluetooth-api.h +++ b/include/bluetooth-api.h @@ -1380,13 +1380,6 @@ typedef struct { /**< device address */ } bluetooth_rfcomm_connection_request_t; -typedef struct { - int socket_fd; - /**< the socket fd */ - bluetooth_device_address_t device_addr; - /**< device address */ -} bluetooth_hid_request_t; - /** * HDP QOS types */ -- 2.7.4