android/hid: Add handling of incoming connections
Luiz Augusto von Dentz
maintainerPATCH 1/3 android/hid: Add handling of incoming connections
From: Luiz Augusto von Dentz <[email protected]>
--- android/hid.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+)
android/hid.c +90 -0
@@ -45,6 +45,8 @@#define MAX_READ_BUFFER 4096static GIOChannel *notification_io =NULL ; +static GIOChannel *ctrl_io =NULL ; +static GIOChannel *intr_io =NULL ;static GSList *devices =NULL ;struct hid_device {
@@ -313,12 +315,94 @@ void bt_hid_handle_cmd(GIOChannel *io, uint8_t opcode, void *buf, uint16_t len)ipc_send_rsp (io,HAL_SERVICE_ID_HIDHOST , status); } +static void connect_cb (GIOChannel *chan,GError *err,gpointer user_data) +{ +struct hid_device *hdev; +bdaddr_t dst; +char address[18 ]; +uint16_t psm; +GError *gerr =NULL ; +GSList *l; + +if (err) { +error ("%s" , err->message); +return ; + } + +bt_io_get (chan, &err, +BT_IO_OPT_DEST_BDADDR , &dst, +BT_IO_OPT_PSM , &psm, +BT_IO_OPT_INVALID ); +if (err) { +error ("%s" , gerr->message); +g_io_channel_shutdown (chan, TRUE,NULL ); +return ; + } + +ba2str (&dst, address); +DBG ("Incoming connection from %s on PSM %d" , address, psm); + +switch (psm) { +case L2CAP_PSM_HIDP_CTRL : + l =g_slist_find_custom (devices, &dst, device_cmp); +if (l) +return ; + + hdev = g_new0(struct hid_device, 1); +bacpy (&hdev->dst, &dst); + hdev->ctrl_io =g_io_channel_ref (chan); + + devices =g_slist_append (devices, hdev); + + hdev->ctrl_watch =g_io_add_watch (hdev->ctrl_io, +G_IO_HUP |G_IO_ERR |G_IO_NVAL , + ctrl_watch_cb, hdev); + +break ; + +case L2CAP_PSM_HIDP_INTR : + l =g_slist_find_custom (devices, &dst, device_cmp); +if (!l) +return ; + + hdev = l->data; + hdev->intr_io =g_io_channel_ref (chan); + hdev->intr_watch =g_io_add_watch (hdev->intr_io, +G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_NVAL , + intr_watch_cb, hdev); +break ; + } +} +bool bt_hid_register (GIOChannel *io,const bdaddr_t *addr) { +GError *err =NULL ; +DBG ("" ); notification_io =g_io_channel_ref (io); + ctrl_io =bt_io_listen (connect_cb,NULL ,NULL ,NULL , &err, +BT_IO_OPT_PSM ,L2CAP_PSM_HIDP_CTRL , +BT_IO_OPT_SEC_LEVEL ,BT_IO_SEC_LOW , +BT_IO_OPT_INVALID ); +if (!ctrl_io) { +error ("Failed to listen on control channel" ); +g_error_free (err); +return false; + } + + intr_io =bt_io_listen (connect_cb,NULL ,NULL ,NULL , &err, +BT_IO_OPT_PSM ,L2CAP_PSM_HIDP_INTR , +BT_IO_OPT_SEC_LEVEL ,BT_IO_SEC_LOW , +BT_IO_OPT_INVALID ); +if (!intr_io) { +error ("Failed to listen on interrupt channel" ); +g_io_channel_unref (ctrl_io); +g_error_free (err); +return false; + } +return true; }
@@ -328,4 +412,10 @@ void bt_hid_unregister(void)g_io_channel_unref (notification_io); notification_io =NULL ; + +g_io_channel_unref (ctrl_io); + ctrl_io =NULL ; + +g_io_channel_unref (intr_io); + intr_io =NULL ; }
Signature · 1 line
1.8.3.1
Luiz Augusto von Dentz
maintainerPATCH 2/3 android/hal-hidhost: Add implementation of .connection_state_cb
From: Luiz Augusto von Dentz <[email protected]>
--- android/hal-hidhost.c | 25 +++++++++++++++++++++++++ android/hal-ipc-api.txt | 1 + android/hal-ipc.c | 3 +++ android/hal-msg.h | 14 ++++++++++++++ android/hal.h | 1 + 5 files changed, 44 insertions(+)
android/hal-hidhost.c +25 -0
@@ -31,6 +31,31 @@ static bool interface_ready(void)return bt_hh_cbacks !=NULL ; } +static void handle_conn_state (void *buf) +{ +struct hal_ev_hid_conn_state *ev = buf; + +if (bt_hh_cbacks->connection_state_cb) + bt_hh_cbacks->connection_state_cb ((bt_bdaddr_t *) (ev->bdaddr), + ev->state); +} + +/* will be called from notification thread context */ +void bt_notify_hh (uint16_t opcode,void *buf,uint16_t len) +{ +if (!interface_ready ()) +return ; + +switch (opcode) { +case HAL_EV_HID_CONN_STATE : +handle_conn_state (buf); +break ; +default : +DBG ("Unhandled callback opcode=0x%x" , opcode); +break ; + } +} +static bt_status_t hh_connect (bt_bdaddr_t *bd_addr) {struct hal_cmd_hid_connect cmd;
android/hal-ipc-api.txt +1 -0
@@ -574,6 +574,7 @@ Notifications: Opcode 0x81 - Connection State notification Notification parameters: Remote address (6 octets) + Connection State (1 octets) Valid connection states: 0x00 = Connected 0x01 = Connecting
android/hal-ipc.c +3 -0
@@ -49,6 +49,9 @@ static void notification_dispatch(struct hal_hdr *msg, int fd)case HAL_SERVICE_ID_BLUETOOTH :bt_notify_adapter (msg->opcode, msg->payload, msg->len);break ; +case HAL_SERVICE_ID_HIDHOST : +bt_notify_hh (msg->opcode, msg->payload, msg->len); +break ;case HAL_SERVICE_ID_A2DP :bt_notify_av (msg->opcode, msg->payload, msg->len);break ;
android/hal-msg.h +14 -0
@@ -423,6 +423,20 @@ struct hal_ev_le_test_mode {uint16_t num_packets; } __attribute__((packed)); +#define HAL_HID_STATE_CONNECTED 0x00 +#define HAL_HID_STATE_CONNECTING 0x01 +#define HAL_HID_STATE_DISCONNECTED 0x02 +#define HAL_HID_STATE_DISCONNECTING 0x03 +#define HAL_HID_STATE_NO_HID 0x07 +#define HAL_HID_STATE_FAILED 0x08 +#define HAL_HID_STATE_UNKNOWN 0x09 + +#define HAL_EV_HID_CONN_STATE 0x81 +struct hal_ev_hid_conn_state { +uint8_t bdaddr[6 ]; +uint8_t state; +} __attribute__((packed)); +#define HAL_EV_AV_CONNECTION_STATE 0x81struct hal_ev_av_connection_state {uint8_t state;
android/hal.h +1 -0
@@ -29,4 +29,5 @@ btav_interface_t *bt_get_av_interface(void);void bt_notify_adapter (uint16_t opcode,void *buf,uint16_t len);void bt_thread_associate (void );void bt_thread_disassociate (void ); +void bt_notify_hh (uint16_t opcode,void *buf,uint16_t len);void bt_notify_av (uint16_t opcode,void *buf,uint16_t len);
Signature · 1 line
1.8.3.1
Luiz Augusto von Dentz
maintainerPATCH 3/3 android/hid: Add handling of HAL_EV_HID_CONN_STATE
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This patches generate proper events when the connection state changes:
Quoted 1 line
hidhost connect
BlueZ D: android/hal-hidhost.c:hh_connect()connection_state_cb: bd_addr= connection_state=BTHH_CONN_STATE_CONNECTING if_hh->connect: BT_STATUS_SUCCESSconnection_state_cb: bd_addr= connection_state=BTHH_CONN_STATE_CONNECTED
Quoted 1 line
hidhost disconnect
BlueZ D: android/hal-hidhost.c:hh_disconnect()connection_state_cb: bd_addr= connection_state=BTHH_CONN_STATE_DISCONNECTING if_hh->disconnect: BT_STATUS_SUCCESSconnection_state_cb: bd_addr= connection_state=BTHH_CONN_STATE_DISCONNECTED
--- android/hid.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-)
android/hid.c +39 -7
@@ -51,6 +51,7 @@ static GSList *devices = NULL;struct hid_device {bdaddr_t dst; +uint8_t state;GIOChannel *ctrl_io;GIOChannel *intr_io;guint ctrl_watch;
@@ -104,18 +105,34 @@ static gboolean intr_io_watch_cb(GIOChannel *chan, gpointer data)return TRUE; } +static void bt_hid_set_state (struct hid_device *hdev,uint8_t state) +{ +struct hal_ev_hid_conn_state ev; +char address[18 ]; + +if (hdev->state == state) +return ; + + hdev->state = state; + +ba2str (&hdev->dst, address); +DBG ("device %s state %u" , address, state); + +bdaddr2android (&hdev->dst, ev.bdaddr); + ev.state = state; + +ipc_send (notification_io,HAL_SERVICE_ID_HIDHOST , +HAL_EV_HID_CONN_STATE ,sizeof (ev), &ev,-1 ); +} +static gboolean intr_watch_cb (GIOChannel *chan,GIOCondition cond,gpointer data) {struct hid_device *hdev = data; -char address[18 ];if (cond &G_IO_IN )return intr_io_watch_cb (chan, data); -ba2str (&hdev->dst, address); -DBG ("Device %s disconnected" , address); -/* Checking for ctrl_watch avoids a double g_io_channel_shutdown since * it's likely that ctrl_watch_cb has been queued for dispatching in * this mainloop iteration */
@@ -143,7 +160,7 @@ static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond,char address[18 ];ba2str (&hdev->dst, address); -DBG ("Device %s disconnected" , address); +bt_hid_set_state (hdev,HAL_HID_STATE_DISCONNECTED );/* Checking for intr_watch avoids a double g_io_channel_shutdown since * it's likely that intr_watch_cb has been queued for dispatching in
@@ -175,6 +192,8 @@ static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_NVAL , intr_watch_cb, hdev); +bt_hid_set_state (hdev,HAL_HID_STATE_CONNECTED ); +return ; failed:
@@ -203,6 +222,7 @@ static void control_connect_cb(GIOChannel *chan, GError *conn_err,DBG ("" );if (conn_err) { +bt_hid_set_state (hdev,HAL_HID_STATE_DISCONNECTED );error ("%s" , conn_err->message); goto failed; }
@@ -270,6 +290,7 @@ static uint8_t bt_hid_connect(struct hal_cmd_hid_connect *cmd, uint16_t len) } devices =g_slist_append (devices, hdev); +bt_hid_set_state (hdev,HAL_HID_STATE_CONNECTING );return HAL_STATUS_SUCCESS ; }
@@ -277,6 +298,7 @@ static uint8_t bt_hid_connect(struct hal_cmd_hid_connect *cmd, uint16_t len)static uint8_t bt_hid_disconnect (struct hal_cmd_hid_disconnect *cmd,uint16_t len) { +struct hid_device *hdev;GSList *l;bdaddr_t dst;
@@ -291,7 +313,16 @@ static uint8_t bt_hid_disconnect(struct hal_cmd_hid_disconnect *cmd,if (!l)return HAL_STATUS_FAILED ; -hid_device_free (l->data); + hdev = l->data; + +/* Wait either channels to HUP */ +if (hdev->intr_io) +g_io_channel_shutdown (hdev->intr_io, TRUE,NULL ); + +if (hdev->ctrl_io) +g_io_channel_shutdown (hdev->ctrl_io, TRUE,NULL ); + +bt_hid_set_state (hdev,HAL_HID_STATE_DISCONNECTING );return HAL_STATUS_SUCCESS ; }
@@ -357,7 +388,7 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) hdev->ctrl_watch =g_io_add_watch (hdev->ctrl_io,G_IO_HUP |G_IO_ERR |G_IO_NVAL , ctrl_watch_cb, hdev); - +bt_hid_set_state (hdev,HAL_HID_STATE_CONNECTING );break ;case L2CAP_PSM_HIDP_INTR :
@@ -370,6 +401,7 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) hdev->intr_watch =g_io_add_watch (hdev->intr_io,G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_NVAL , intr_watch_cb, hdev); +bt_hid_set_state (hdev,HAL_HID_STATE_CONNECTED );break ; } }
Signature · 1 line
1.8.3.1
Hi Luiz,
Quoted 3 lines · Luiz Augusto von Dentz
--- android/hid.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+)
All three patches have been applied (with one minor coding style fix to 2/3). Thanks.
Johan