#include "bt-event-handler.h"
#define HID_UUID "00001124-0000-1000-8000-00805f9b34fb"
+#define HID_DEVICE_UUID "00001124-0000-1000-8000-00805f9b43bf"
#define REPORTID_MOUSE 1
#define BT_HID_BUFFER_LEN 100
-/* The types of HIDP transaction */
-#define BT_HIDP_TRANSACTION_SET_PROTOCOL 0x70
-#define BT_HIDP_TRANSACTION_GET_IDLE 0x80
-#define BT_HIDP_TRANSACTION_SET_IDLE 0x90
-#define BT_HIDP_TRANSACTION_DATA 0xa0
-#define BT_HIDP_TRANSACTION_DATC 0xb0
-#define BT_HIDP_TRANSACTION_HANDSHAKE 0x00
-#define BT_HIDP_TRANSACTION_HID_CONTROL 0x10
-#define BT_HIDP_TRANSACTION_GET_REPORT 0x40
-#define BT_HIDP_TRANSACTION_SET_REPORT 0x50
-#define BT_HIDP_TRANSACTION_GET_PROTOCOL 0x60
-
-#define BT_HIDP_DATA_OUT_RTYPE 0x02
-#define BT_HIDP_DATA_IN_RTYPE 0x01
-
-#define BT_HIDP_HSHK_ERROR_UNSUPPORTED_REQUEST 0x03
-#define BT_HIDP_HSHK_ERROR_INVALID_PARAM 0x04
-#define BT_HIDP_HSHK_ERROR_UNKNOWN 0x0E
-#define BT_HIDP_HSHK_ERROR_FATAL 0x0F
-#define BT_HIDP_HSHK_OK_SUCCESSFUL 0x00
-#define BT_HIDP_HSHK_NOT_AVAILABLE 0x01
-#define BT_HIDP_HSHK_ERROR_INVALID_REPORT_ID 0x02
-
-/* The masks of BT_HIDP header */
-#define BT_HIDP_HEADER_PARAMETER_MASK 0x0f
-#define BT_HIDP_HEADER_TRANSACTION_MASK 0xf0
+/* HIDP header masks */
+#define BT_HID_HEADER_TRANS_MASK 0xf0
+#define BT_HID_HEADER_PARAM_MASK 0x0f
+
+/* HIDP transaction types */
+#define BT_HID_TRANS_HANDSHAKE 0x00
+#define BT_HID_TRANS_HID_CONTROL 0x10
+#define BT_HID_TRANS_GET_REPORT 0x40
+#define BT_HID_TRANS_SET_REPORT 0x50
+#define BT_HID_TRANS_GET_PROTOCOL 0x60
+#define BT_HID_TRANS_SET_PROTOCOL 0x70
+#define BT_HID_TRANS_GET_IDLE 0x80
+#define BT_HID_TRANS_SET_IDLE 0x90
+#define BT_HID_TRANS_DATA 0xa0
+#define BT_HID_TRANS_DATC 0xb0
+
+#define BT_HID_DATA_RTYPE_INPUT 0x01
+#define BT_HID_DATA_RTYPE_OUTPUT 0x02
+
+#define BT_HID_HSHK_SUCCESSFUL 0x00
+#define BT_HID_HSHK_NOT_READY 0x01
+#define BT_HID_HSHK_ERR_INVALID_REPORT_ID 0x02
+#define BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST 0x03
+#define BT_HID_HSHK_ERR_INVALID_PARAMETER 0x04
+#define BT_HID_HSHK_ERR_UNKNOWN 0x0E
+#define BT_HID_HSHK_ERR_FATAL 0x0F
typedef struct {
guint object_id;
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 hid_connected_device_info_t *__find_hid_info_with_address(const char *remote_addr)
{
GSList *l;
}
static void __hid_connected_cb(hid_connected_device_info_t *info,
- bt_event_info_t *event_info)
+ int result)
{
bluetooth_hid_request_t conn_info;
+ 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);
+ _bt_convert_addr_string_to_type (conn_info.device_addr.addr , info->address);
+
+ if (result == BLUETOOTH_ERROR_NONE)
+ BT_INFO_C("Connected [HID Device]");
- BT_INFO_C("Connected [HID Device]");
_bt_common_event_cb(BLUETOOTH_HID_DEVICE_CONNECTED,
- BLUETOOTH_ERROR_NONE, &conn_info,
+ result, &conn_info,
event_info->cb, event_info->user_data);
}
+static void __hid_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
+ gpointer user_data)
+
+{
+ int result;
+ GError *error = NULL;
+ GVariant *ret = NULL;
+ hid_connected_device_info_t info;
+ const char *path;
+
+ BT_DBG("+");
+
+ ret = g_dbus_proxy_call_finish(proxy, res, &error);
+ if (ret == NULL) {
+ g_dbus_error_strip_remote_error(error);
+ BT_ERR("Error : %s \n", error->message);
+
+ if (g_strcmp0(error->message, "In Progress") == 0)
+ result = BLUETOOTH_ERROR_DEVICE_BUSY;
+ else
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ info.ctrl_fd = -1;
+ info.intr_fd = -1;
+
+ info.address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+ path = g_dbus_proxy_get_object_path(proxy);
+ _bt_convert_device_path_to_address(path, info.address);
+
+ __hid_connected_cb(&info, result);
+
+ g_free(info.address);
+
+ g_error_free(error);
+ } else {
+ g_variant_unref(ret);
+ }
+
+ if (proxy)
+ g_object_unref(proxy);
+
+ BT_DBG("-");
+}
+
static gboolean __hid_disconnect(hid_connected_device_info_t *info)
{
bluetooth_hid_request_t disconn_info;
info->intr_data_id = 0;
}
- if (info->intr_fd >= 0) {
- close(info->ctrl_fd);
- close(info->intr_fd);
- info->intr_fd = -1;
- info->ctrl_fd = -1;
- }
-
if (info->ctrl_data_io) {
g_io_channel_shutdown(info->ctrl_data_io, TRUE, NULL);
g_io_channel_unref(info->ctrl_data_io);
g_io_channel_unref(info->intr_data_io);
info->intr_data_io = NULL;
}
+ if (info->intr_fd >= 0) {
+ close(info->ctrl_fd);
+ close(info->intr_fd);
+ info->intr_fd = -1;
+ info->ctrl_fd = -1;
+ }
info->disconnect_idle_id = 0;
event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
if (event_info == NULL)
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);
+ _bt_convert_addr_string_to_type(disconn_info.device_addr.addr , info->address);
_bt_common_event_cb(BLUETOOTH_HID_DEVICE_DISCONNECTED,
BLUETOOTH_ERROR_NONE, &disconn_info,
event_info->cb, event_info->user_data);
BT_INFO("Parsing Data");
bluetooth_hid_received_data_t data = {0};
header = buffer[0];
- type = header & BT_HIDP_HEADER_TRANSACTION_MASK;
- param = header & BT_HIDP_HEADER_PARAMETER_MASK;
+ type = header & BT_HID_HEADER_TRANS_MASK;
+ param = header & BT_HID_HEADER_PARAM_MASK;
BT_INFO("type %d , param %d", type, param);
BT_INFO("Data Reveived from %s" , info->address);
+
+ event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
+ if (event_info == NULL)
+ return FALSE;
+
data.address = g_strdup(info->address);
switch (type) {
- case BT_HIDP_TRANSACTION_HANDSHAKE:
+ case BT_HID_TRANS_HANDSHAKE:
BT_INFO("TRANS HANDSHAKE");
data.type = HTYPE_TRANS_HANDSHAKE;
data.buffer_size = len;
if (data.buffer)
memcpy(data.buffer, buffer, len);
break;
- case BT_HIDP_TRANSACTION_HID_CONTROL:
+ case BT_HID_TRANS_HID_CONTROL:
BT_INFO("HID CONTROL");
data.type = HTYPE_TRANS_HID_CONTROL;
data.buffer_size = len;
if (data.buffer)
memcpy(data.buffer, buffer, len);
break;
- case BT_HIDP_TRANSACTION_DATA:
+ case BT_HID_TRANS_DATA:
BT_INFO("TRANS DATA");
data.type = HTYPE_TRANS_DATA;
- if (param & BT_HIDP_DATA_IN_RTYPE) {
+ if (param & BT_HID_DATA_RTYPE_INPUT) {
BT_INFO("Input Report");
data.param = PTYPE_DATA_RTYPE_INPUT;
data.buffer_size = len;
memcpy(data.buffer, buffer, len);
}
break;
- case BT_HIDP_TRANSACTION_GET_REPORT: {
+ case BT_HID_TRANS_GET_REPORT: {
BT_INFO("Get Report");
data.type = HTYPE_TRANS_GET_REPORT;
- if (param & BT_HIDP_DATA_IN_RTYPE) {
+ if (param & BT_HID_DATA_RTYPE_INPUT) {
BT_INFO("Input Report");
data.param = PTYPE_DATA_RTYPE_INPUT;
} else {
memcpy(data.buffer, buffer, len);
break;
}
- case BT_HIDP_TRANSACTION_SET_REPORT: {
+ case BT_HID_TRANS_SET_REPORT: {
BT_INFO("Set Report");
data.type = HTYPE_TRANS_SET_REPORT;
- if (param & BT_HIDP_DATA_IN_RTYPE) {
+ if (param & BT_HID_DATA_RTYPE_INPUT) {
BT_INFO("Input Report");
data.param = PTYPE_DATA_RTYPE_INPUT;
} else {
memcpy(data.buffer, buffer, len);
break;
}
- case BT_HIDP_TRANSACTION_GET_PROTOCOL:{
+ case BT_HID_TRANS_GET_PROTOCOL:{
BT_INFO("Get_PROTOCOL");
data.type = HTYPE_TRANS_GET_PROTOCOL;
data.param = PTYPE_DATA_RTYPE_INPUT;
memcpy(data.buffer, buffer, len);
break;
}
- case BT_HIDP_TRANSACTION_SET_PROTOCOL:{
+ case BT_HID_TRANS_SET_PROTOCOL:{
BT_INFO("Set_PROTOCOL");
data.type = HTYPE_TRANS_SET_PROTOCOL;
data.param = PTYPE_DATA_RTYPE_INPUT;
default: {
BT_INFO("unsupported HIDP control message");
BT_ERR("Send Handshake Message");
- guint8 type = BT_HIDP_TRANSACTION_HANDSHAKE |
- BT_HIDP_HSHK_ERROR_UNSUPPORTED_REQUEST;
+ guint8 type = BT_HID_TRANS_HANDSHAKE |
+ BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST;
data.type = HTYPE_TRANS_UNKNOWN;
int fd = g_io_channel_unix_get_fd(chan);
int bytes = write(fd, &type, sizeof(type));
break;
}
}
- event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
- if (event_info == NULL) {
- g_free(data.buffer);
- g_free((char *)data.address);
- return FALSE;
- }
_bt_common_event_cb(BLUETOOTH_HID_DEVICE_DATA_RECEIVED,
BLUETOOTH_ERROR_NONE, &data,
event_info->cb, event_info->user_data);
-
- g_free(data.buffer);
- g_free((char *)data.address);
+ if (data.buffer)
+ g_free(data.buffer);
+ if (data.address)
+ g_free((char *)data.address);
} else {
BT_INFO("Error while reading data");
}
{
hid_info_t *info = NULL;
hid_connected_device_info_t *dev_info = NULL;
- bt_event_info_t *event_info = NULL;
char address[18];
info = hid_info;
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);
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) {
- event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
- if (event_info)
- __hid_connected_cb(dev_info, event_info);
+ __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE);
}
return 0;
if (hid_info == NULL)
return BLUETOOTH_ERROR_INTERNAL;
- hid_info->uuid = g_strdup(HID_UUID);
+ hid_info->uuid = g_strdup(HID_DEVICE_UUID);
profile_info.authentication = TRUE;
profile_info.authorization = TRUE;
}
memcpy(device_address, remote_addr, BT_ADDRESS_STRING_SIZE);
- ret = _bt_connect_profile(device_address, HID_UUID, NULL, NULL);
+ ret = _bt_connect_profile(device_address, HID_DEVICE_UUID,
+ __hid_connect_response_cb, NULL);
return ret;
}
BT_ERR("Don't have a privilege to use this API");
return BLUETOOTH_ERROR_PERMISSION_DEINED;
}
+
hid_connected_device_info_t *info = NULL;
info = __find_hid_info_with_address(remote_addr);
if (info == NULL)
return BLUETOOTH_ERROR_INVALID_PARAM;
- _bt_disconnect_profile((char *)remote_addr, HID_UUID, NULL, NULL);
+ _bt_disconnect_profile((char *)remote_addr, HID_DEVICE_UUID, NULL, NULL);
info->disconnect_idle_id = g_idle_add((GSourceFunc)__hid_disconnect, info);
{
int result;
int written = 0;
- int socket_fd;
hid_connected_device_info_t *info = NULL;
switch (privilege_token_send_mouse) {
case 0:
- result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HID_DEVICE_SEND_MOUSE_EVENT);
+ result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_MOUSE_EVENT);
if (result == BLUETOOTH_ERROR_NONE) {
privilege_token_send_mouse = 1; /* Have a permission */
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;
{
int result;
int written = 0;
- int socket_fd;
hid_connected_device_info_t *info = NULL;
switch (privilege_token_send_key) {
case 0:
- result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HID_DEVICE_SEND_KEY_EVENT);
+ result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_KEY_EVENT);
if (result == BLUETOOTH_ERROR_NONE) {
privilege_token_send_key = 1; /* Have a permission */
return BLUETOOTH_ERROR_INVALID_PARAM;
}
+ int socket_fd;
+
if (info->intr_fd != -1 && info->ctrl_fd == -1)
socket_fd = info->intr_fd;
else
return written;
}
+BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr,
+ unsigned char btcode, unsigned char report_id,
+ const char *data, unsigned int data_len)
+{
+ int result;
+ int written = 0;
+ int socket_fd;
+ hid_connected_device_info_t *info = NULL;
+ char *send_event = NULL;
+
+ BT_CHECK_PARAMETER(remote_addr, return);
+ BT_CHECK_PARAMETER(data, return);
+
+ switch (privilege_token_send_key) {
+ case 0:
+ result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_CUSTOM_EVENT);
+
+ if (result == BLUETOOTH_ERROR_NONE) {
+ privilege_token_send_key = 1; /* Have a permission */
+ } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ privilege_token_send_key = -1; /* Don't have a permission */
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ } else {
+ /* Just break - It is not related with permission error */
+ }
+ break;
+ case 1:
+ /* Already have a privilege */
+ break;
+ case -1:
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ default:
+ /* Invalid privilge token value */
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ info = __find_hid_info_with_address(remote_addr);
+ if (info == NULL) {
+ BT_ERR("Connection Information not found");
+ 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 = (char*)g_malloc0(data_len + 2);
+ if (send_event == NULL)
+ return BLUETOOTH_ERROR_OUT_OF_MEMORY;
+
+ send_event[0] = (char)btcode;
+ send_event[1] = (char)report_id;
+ memcpy(send_event + 2, data, data_len);
+
+ written = write(socket_fd, send_event, data_len + 2);
+
+ g_free(send_event);
+
+ return written;
+}
+
BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr,
bt_hid_header_type_t htype,
bt_hid_param_type_t ptype,
switch (privilege_token_reply) {
case 0:
- result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HID_DEVICE_SEND_REPLY_TO_REPORT);
+ result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_REPLY_TO_REPORT);
if (result == BLUETOOTH_ERROR_NONE) {
privilege_token_reply = 1; /* Have a permission */
}
BT_INFO("htype %d ptype %d", htype, ptype);
- switch (htype) {
+ switch(htype) {
case HTYPE_TRANS_GET_REPORT: {
- switch (ptype) {
+ switch(ptype) {
case PTYPE_DATA_RTYPE_INPUT: {
- output_report.type = BT_HIDP_TRANSACTION_DATA |
- BT_HIDP_DATA_IN_RTYPE;
+ 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,
sizeof(output_report));
break;
case HTYPE_TRANS_GET_PROTOCOL: {
BT_DBG("Replying to Get_PROTOCOL");
- output_report.type = BT_HIDP_TRANSACTION_DATA | BT_HIDP_DATA_OUT_RTYPE;
+ 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);
}
case HTYPE_TRANS_SET_PROTOCOL: {
BT_DBG("Reply to Set_Protocol");
- output_report.type = BT_HIDP_TRANSACTION_DATA | BT_HIDP_DATA_IN_RTYPE;
+ 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));
}
case HTYPE_TRANS_HANDSHAKE: {
BT_DBG("Replying Handshake");
- output_report.type = BT_HIDP_TRANSACTION_HANDSHAKE | data[0];
+ 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,
sizeof(output_report.type));