Skip to contentSkip to conversation

fakehid: Disconnect from PS3 remote after 10 mins

PATCH In review Bastien Nocera 3 participants 6 messages Jun 16, 2012 – Jun 29, 2012 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 fakehid: Disconnect from PS3 remote after 10 mins

After 10 minutes, disconnect the PS3 BD Remote to avoid draining its battery. This is consistent with its behaviour on the PS3.

Original patch by Ruslan N. Marchenko <[email protected]>

---
 input/device.h  |    1 +
 input/fakehid.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
input/device.h +1 -0
index ff52967..d8baa2c 100644
@@ -33,6 +33,7 @@ struct fake_input {
 	int		uinput;		/* uinput socket */
 	int		rfcomm;		/* RFCOMM socket */
 	uint8_t		ch;		/* RFCOMM channel number */
+	guint           timeout_id;	/* Disconnect timeout ID */
 	gboolean	(*connect) (struct input_conn *iconn, GError **err);
 	int		(*disconnect) (struct input_conn *iconn);
 	void		*priv;
input/fakehid.c +36 -0
index 3181538..a125356 100644
@@ -44,6 +44,9 @@
 #include "fakehid.h"
 #include "uinput.h"
 
+/* Timeout to get the PS3 remote disconnected, in seconds */
+#define PS3_REMOTE_TIMEOUT 10 * 60
+
 enum ps3remote_special_keys {
 	PS3R_BIT_PS = 0,
 	PS3R_BIT_ENTER = 3,
@@ -141,6 +144,20 @@ static unsigned int ps3remote_keymap[] = {
 	[0xff] = KEY_MAX,
 };
 
+static gboolean ps3_remote_timeout_cb(gpointer user_data);
+
+static void ps3remote_set_timeout(struct fake_input *fake, gboolean enable)
+{
+	if (enable) {
+		fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);
+	} else {
+		if (fake->timeout_id > 0) {
+			g_source_remove(fake->timeout_id);
+			fake->timeout_id = 0;
+		}
+	}
+}
+
 static int ps3remote_decode(char *buff, int size, unsigned int *value)
 {
 	static unsigned int lastkey = 0;
@@ -203,6 +220,16 @@ error:
 	return -1;
 }
 
+static gboolean
+ps3_remote_timeout_cb(gpointer user_data)
+{
+	struct fake_input *fake = (struct fake_input *) user_data;
+	input_device_request_disconnect(fake);
+	DBG("Disconnected PS3 BD Remote after timeout");
+	fake->timeout_id = 0;
+	return FALSE;
+}
+
 static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
 				gpointer data)
 {
@@ -221,6 +248,9 @@ static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
 		goto failed;
 	}
 
+	/* Remove the old timeout */
+	ps3remote_set_timeout(fake, FALSE);
+
 	fd = g_io_channel_unix_get_fd(chan);
 
 	memset(buff, 0, sizeof(buff));
@@ -256,6 +286,8 @@ static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
 		goto failed;
 	}
 
+	ps3remote_set_timeout(fake, TRUE);
+
 	return TRUE;
 
 failed:
@@ -318,6 +350,8 @@ static int ps3remote_setup_uinput(struct fake_input *fake,
 		goto err;
 	}
 
+	ps3remote_set_timeout(fake, TRUE);
+
 	return 0;
 
 err:
@@ -378,6 +412,8 @@ struct fake_input *fake_hid_connadd(struct fake_input *fake,
 	for (l = fake_hid->devices; l != NULL; l = l->next) {
 		old = l->data;
 		if (old->idev == fake->idev) {
+			if (fake->timeout_id > 0)
+				g_source_remove(fake->timeout_id);
 			g_free(fake);
 			fake = old;
 			fake_hid->connect(fake, NULL);
Signature · 1 line
1.7.10
#1 lore raw

Hi Bastien,

Quoted 42 lines · Bastien Nocera

After 10 minutes, disconnect the PS3 BD Remote to avoid draining its battery. This is consistent with its behaviour on the PS3.

Original patch by Ruslan N. Marchenko <[email protected]>
---
 input/device.h  |    1 +
 input/fakehid.c |   36 ++++++++++++++++++++++++++++++++++++

 2 files changed, 37 insertions(+)
input/device.h +0 -0
index ff52967..d8baa2c 100644
@@ -33,6 +33,7 @@ struct fake_input {

       int             uinput;         /* uinput socket */
       int             rfcomm;         /* RFCOMM socket */ uint8_t         ch;             /* RFCOMM channel number */
+       guint           timeout_id;     /* Disconnect timeout ID */ gboolean        (*connect) (struct input_conn *iconn, GError **err); int             (*disconnect) (struct input_conn *iconn); void            *priv;

input/fakehid.c +6 -0
index 3181538..a125356 100644
@@ -44,6 +44,9 @@
  #include "fakehid.h"
  #include "uinput.h"
 
+/* Timeout to get the PS3 remote disconnected, in seconds */
+#define PS3_REMOTE_TIMEOUT 10 * 60
+
  enum ps3remote_special_keys {
        PS3R_BIT_PS = 0,
        PS3R_BIT_ENTER = 3,
@@ -141,6 +144,20 @@ static unsigned int ps3remote_keymap[] = {
        [0xff] = KEY_MAX,
  };
 
+static gboolean ps3_remote_timeout_cb(gpointer user_data);
+
+static void ps3remote_set_timeout(struct fake_input *fake, gboolean enable)

input/fakehid.c static unsigned int ps3remote_keymap[] = { · line 159

Quoted 10 lines
input/fakehid.c +10 -0
+{
+       if (enable) {
+               fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);
+       } else {
+               if (fake->timeout_id > 0) {
+                       g_source_remove(fake->timeout_id);
+                       fake->timeout_id = 0;
+               }
+       }
+}

It doesn't really need to remove/add the timeout separately, upon event just reset the idle timeout.

Signature · 1 line
Luiz Augusto von Dentz
#2 lore raw
Quoted 42 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 39 lines · Bastien Nocera

After 10 minutes, disconnect the PS3 BD Remote to avoid draining its battery. This is consistent with its behaviour on the PS3.

Original patch by Ruslan N. Marchenko <[email protected]>

---
 input/device.h  |    1 +
 input/fakehid.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
input/device.h +1 -0
index ff52967..d8baa2c 100644
@@ -33,6 +33,7 @@ struct fake_input {
       int             uinput;         /* uinput socket */
       int             rfcomm;         /* RFCOMM socket */
       uint8_t         ch;             /* RFCOMM channel number */
+       guint           timeout_id;     /* Disconnect timeout ID */
       gboolean        (*connect) (struct input_conn *iconn, GError **err);
       int             (*disconnect) (struct input_conn *iconn);
       void            *priv;
input/fakehid.c +3 -0
index 3181538..a125356 100644
@@ -44,6 +44,9 @@
 #include "fakehid.h"
 #include "uinput.h"
 
+/* Timeout to get the PS3 remote disconnected, in seconds */
+#define PS3_REMOTE_TIMEOUT 10 * 60
+
 enum ps3remote_special_keys {
       PS3R_BIT_PS = 0,
       PS3R_BIT_ENTER = 3,
@@ -141,6 +144,20 @@ static unsigned int ps3remote_keymap[] = {
       [0xff] = KEY_MAX,
 };
 
Quoted 16 lines

input/fakehid.c static unsigned int ps3remote_keymap[] = { · line 159

Quoted 13 lines
input/fakehid.c +13 -0
+static gboolean ps3_remote_timeout_cb(gpointer user_data);
+
+static void ps3remote_set_timeout(struct fake_input *fake, gboolean enable)
+{
+       if (enable) {
+               fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);
+       } else {
+               if (fake->timeout_id > 0) {
+                       g_source_remove(fake->timeout_id);
+                       fake->timeout_id = 0;
+               }
+       }
+}

It doesn't really need to remove/add the timeout separately, upon event just reset the idle timeout.

I don't understand what you're saying here.

Are you saying I should call g_source_remove, and then g_timeout_add()? There's no functionality to change the interval for g_timeout_add* functions.

The call is separate because it is used multiple times in the code (and handling getting/setting in one place is cleaner).

By the way, the reason why it's handled this way is because the rest of the input code relies on the kernel switching off the connection using hidp_connadd_req->idle_to. fakehid doesn't use hidp.

Cheers

#3 lore raw

Hi Bastien,

Quoted 9 lines · Bastien Nocera
Quoted 2 lines

It doesn't really need to remove/add the timeout separately, upon event just reset the idle timeout.

I don't understand what you're saying here.

Are you saying I should call g_source_remove, and then g_timeout_add()? There's no functionality to change the interval for g_timeout_add* functions.

Quoted 6 lines

The call is separate because it is used multiple times in the code (and handling getting/setting in one place is cleaner).

By the way, the reason why it's handled this way is because the rest of the input code relies on the kernel switching off the connection using hidp_connadd_req->idle_to. fakehid doesn't use hidp.

Luiz told me that you had some IRC discussion about this and came to some sort of consensus. Should I be expecting a new patch set?

Johan

#4 lore raw
Quoted 15 lines · Johan Hedberg

Hi Bastien,

Quoted 12 lines · Bastien Nocera
Quoted 2 lines

It doesn't really need to remove/add the timeout separately, upon event just reset the idle timeout.

I don't understand what you're saying here.

Are you saying I should call g_source_remove, and then g_timeout_add()? There's no functionality to change the interval for g_timeout_add* functions.

The call is separate because it is used multiple times in the code (and handling getting/setting in one place is cleaner).

Quoted 6 lines
Quoted 3 lines

By the way, the reason why it's handled this way is because the rest of the input code relies on the kernel switching off the connection using hidp_connadd_req->idle_to. fakehid doesn't use hidp.

Luiz told me that you had some IRC discussion about this and came to some sort of consensus. Should I be expecting a new patch set?

Have we? I didn't understand what changes he wanted me to do. So don't expect any updated patches until he answers on the list.

#5 lore raw
In reply to Bastien Nocera

Hi Bastien,

Quoted 42 lines · Bastien Nocera

After 10 minutes, disconnect the PS3 BD Remote to avoid draining its battery. This is consistent with its behaviour on the PS3.

Original patch by Ruslan N. Marchenko <[email protected]>
---
 input/device.h  |    1 +
 input/fakehid.c |   36 ++++++++++++++++++++++++++++++++++++

 2 files changed, 37 insertions(+)
input/device.h +0 -0
index ff52967..d8baa2c 100644
@@ -33,6 +33,7 @@ struct fake_input {

       int             uinput;         /* uinput socket */
       int             rfcomm;         /* RFCOMM socket */ uint8_t         ch;             /* RFCOMM channel number */
+       guint           timeout_id;     /* Disconnect timeout ID */ gboolean        (*connect) (struct input_conn *iconn, GError **err); int             (*disconnect) (struct input_conn *iconn); void            *priv;

input/fakehid.c +6 -0
index 3181538..a125356 100644
@@ -44,6 +44,9 @@
  #include "fakehid.h"
  #include "uinput.h"
 
+/* Timeout to get the PS3 remote disconnected, in seconds */
+#define PS3_REMOTE_TIMEOUT 10 * 60
+
  enum ps3remote_special_keys {
        PS3R_BIT_PS = 0,
        PS3R_BIT_ENTER = 3,
@@ -141,6 +144,20 @@ static unsigned int ps3remote_keymap[] = {
        [0xff] = KEY_MAX,
  };
 
+static gboolean ps3_remote_timeout_cb(gpointer user_data);
+
+static void ps3remote_set_timeout(struct fake_input *fake, gboolean enable)

input/fakehid.c static unsigned int ps3remote_keymap[] = { · line 159

Quoted 10 lines
input/fakehid.c +10 -0
+{
+       if (enable) {
+               fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);
+       } else {
+               if (fake->timeout_id > 0) {
+                       g_source_remove(fake->timeout_id);
+                       fake->timeout_id = 0;
+               }
+       }
+}

What I suggested was to remove the enable parameter e.g:

if (fake->timeout_id > 0)

        g_source_remove(fake->timeout_id);

fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);

Because you don't really need to have it disable at any point other than when is not connected, basically you just reset the timer on event, no need to have a parameter to disable the timer while processing the events and reenable when done since it wont fire in the middle of the processing.

Signature · 1 line
Luiz Augusto von Dentz