OSDN Git Service

adb: reserve a wakelock when clients are connected over socket
authorAndrew Boie <andrew.p.boie@intel.com>
Thu, 12 Jul 2012 22:39:25 +0000 (15:39 -0700)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Sun, 16 Jun 2013 06:52:00 +0000 (14:52 +0800)
USB OTG connections already have a wakelock reserved, but if a network
socket connection is made, the device can unexpectedly suspend
in the middle of a session. The lock is released when the client
disconnects.

Change-Id: Id5ac01699fab9350955c9814042259542fc6a694
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
adb/transport_local.c

index 96a24ba..25586ca 100644 (file)
@@ -50,7 +50,46 @@ static inline void fix_endians(apacket *p)
 ADB_MUTEX_DEFINE( local_transports_lock );
 
 static atransport*  local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
-#endif /* ADB_HOST */
+#else /* !ADB_HOST */
+
+#define WAKE_LOCK_NAME      "adb-socket-connection"
+#define WAKE_LOCK_ACQUIRE   "/sys/power/wake_lock"
+#define WAKE_LOCK_RELEASE   "/sys/power/wake_unlock"
+
+static int sysfs_write(const char *node, const char *message)
+{
+    int fd;
+    ssize_t to_write;
+    int ret = 0;
+
+    fd = adb_open(node, O_RDWR);
+    if (!fd) {
+        D("open '%s' failed: %s\n", node, strerror(errno));
+        return -1;
+    }
+
+    to_write = strlen(message);
+    if (adb_write(fd, message, to_write) != to_write) {
+        D("write '%s' failed: %s\n", node, strerror(errno));
+        ret = -1;
+    }
+    adb_close(fd);
+    return ret;
+}
+
+static void get_wakelock(void)
+{
+    if (sysfs_write(WAKE_LOCK_ACQUIRE, WAKE_LOCK_NAME))
+        D("couldn't reserve wakelock for socket connection\n");
+}
+
+static void release_wakelock(void)
+{
+    if (sysfs_write(WAKE_LOCK_RELEASE, WAKE_LOCK_NAME))
+        D("couldn't release wakelock for socket connection\n");
+}
+
+#endif /* !ADB_HOST */
 
 static int remote_read(apacket *p, atransport *t)
 {
@@ -176,6 +215,9 @@ static void *server_socket_thread(void * arg)
         fd = adb_socket_accept(serverfd, &addr, &alen);
         if(fd >= 0) {
             D("server: new connection on fd %d\n", fd);
+#if !ADB_HOST
+            get_wakelock();
+#endif
             close_on_exec(fd);
             disable_tcp_nagle(fd);
             register_socket_transport(fd, "host", port, 1);
@@ -353,6 +395,9 @@ static void remote_kick(atransport *t)
 
 static void remote_close(atransport *t)
 {
+#if !ADB_HOST
+    release_wakelock();
+#endif
     adb_close(t->fd);
 }