xiaoxiang781216 commented on code in PR #3098:
URL: https://github.com/apache/nuttx-apps/pull/3098#discussion_r2152833432


##########
system/fastboot/fastboot.c:
##########
@@ -1094,29 +1182,206 @@ static int fastboot_usbdev_initialize(FAR struct 
fastboot_ctx_s *ctx)
 
 static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
 {
-  if (ctx->usbdev_out > 0)
+  int i;
+
+  for (i = 0; i < nitems(ctx->t_fd); i++)
+    {
+      if (ctx->t_fd[i] > 0)
+        {
+          close(ctx->t_fd[i]);
+          ctx->t_fd[i] = -1;
+        }
+    }
+}
+
+static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
+                                    FAR void *buf, size_t len)
+{
+  return fastboot_read(ctx->t_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
+#ifdef CONFIG_NET_TCP
+static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
+{
+  struct sockaddr_in addr;
+
+  ctx->t_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->t_fd[0] < 0)
+    {
+      fb_err("create socket failed %d", errno);
+      return -errno;
+    }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+  addr.sin_port = htons(FASTBOOT_TCP_PORT);
+  if (bind(ctx->t_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      return -errno;
+    }
+
+  if (listen(ctx->t_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      return -errno;
+    }
+
+  return 0;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->t_fd[1]);
+  ctx->t_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->t_fd[0]);
+  ctx->t_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)
+{
+  size_t total = 0;
+  ssize_t nread;
+
+  while (total < len)
     {
-      close(ctx->usbdev_out);
-      ctx->usbdev_out = -1;
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
     }
 
-  if (ctx->usbdev_in > 0)
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char buff[FASTBOOT_TCP_HANDSHAKE_LEN];
+  uint64_t data_size;
+  ssize_t nread;
+
+  if (ctx->t_fd[1] == -1)
     {
-      close(ctx->usbdev_in);
-      ctx->usbdev_in = -1;
+      while (1)
+        {
+          /* accept a connection, not care the address of the peer socket */
+
+          ctx->t_fd[1] = accept(ctx->t_fd[0], NULL, 0);
+          if (ctx->t_fd[1] < 0)
+            {
+              continue;
+            }
+
+          /* handshake */
+
+          if ((fastboot_read_all(ctx->t_fd[1], buff,
+               FASTBOOT_TCP_HANDSHAKE_LEN) != FASTBOOT_TCP_HANDSHAKE_LEN) ||

Review Comment:
   align all field to (



##########
system/fastboot/fastboot.c:
##########
@@ -122,19 +139,42 @@ struct fastboot_file_s
   off_t offset;
 };
 
+struct fastboot_transport_s
+{
+  int (*init)(FAR struct fastboot_ctx_s *);
+  void (*deinit)(FAR struct fastboot_ctx_s *);
+  ssize_t (*read)(FAR struct fastboot_ctx_s *, FAR void *, size_t);
+  int (*write)(FAR struct fastboot_ctx_s *, FAR void *, size_t);

Review Comment:
   add const to buffer



##########
system/fastboot/fastboot.c:
##########
@@ -1094,29 +1182,206 @@ static int fastboot_usbdev_initialize(FAR struct 
fastboot_ctx_s *ctx)
 
 static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
 {
-  if (ctx->usbdev_out > 0)
+  int i;
+
+  for (i = 0; i < nitems(ctx->t_fd); i++)
+    {
+      if (ctx->t_fd[i] > 0)
+        {
+          close(ctx->t_fd[i]);
+          ctx->t_fd[i] = -1;
+        }
+    }
+}
+
+static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
+                                    FAR void *buf, size_t len)
+{
+  return fastboot_read(ctx->t_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
+#ifdef CONFIG_NET_TCP
+static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
+{
+  struct sockaddr_in addr;
+
+  ctx->t_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->t_fd[0] < 0)
+    {
+      fb_err("create socket failed %d", errno);
+      return -errno;
+    }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+  addr.sin_port = htons(FASTBOOT_TCP_PORT);
+  if (bind(ctx->t_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      return -errno;
+    }
+
+  if (listen(ctx->t_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      return -errno;
+    }
+
+  return 0;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->t_fd[1]);
+  ctx->t_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->t_fd[0]);
+  ctx->t_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)
+{
+  size_t total = 0;
+  ssize_t nread;
+
+  while (total < len)
     {
-      close(ctx->usbdev_out);
-      ctx->usbdev_out = -1;
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
     }
 
-  if (ctx->usbdev_in > 0)
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char buff[FASTBOOT_TCP_HANDSHAKE_LEN];
+  uint64_t data_size;
+  ssize_t nread;
+
+  if (ctx->t_fd[1] == -1)
     {
-      close(ctx->usbdev_in);
-      ctx->usbdev_in = -1;
+      while (1)
+        {
+          /* accept a connection, not care the address of the peer socket */

Review Comment:
   first char need upper case in all comment



##########
system/fastboot/fastboot.c:
##########
@@ -122,19 +139,42 @@ struct fastboot_file_s
   off_t offset;
 };
 
+struct fastboot_transport_s
+{
+  int (*init)(FAR struct fastboot_ctx_s *);
+  void (*deinit)(FAR struct fastboot_ctx_s *);
+  ssize_t (*read)(FAR struct fastboot_ctx_s *, FAR void *, size_t);
+  int (*write)(FAR struct fastboot_ctx_s *, FAR void *, size_t);
+};
+
 struct fastboot_ctx_s
 {
-  int usbdev_in;
-  int usbdev_out;
+  /* Transport file descriptors
+   *
+   * | idx |    USB   |      TCP      | poll |
+   * |-----|----------|---------------|------|
+   * |   0 |usbdev in |TCP socket     |  Y   |
+   * |   1 |usbdev out|accepted socket|  N   |
+   */
+
+  int t_fd[2];

Review Comment:
   tran_fd



##########
system/fastboot/fastboot.c:
##########
@@ -1163,7 +1431,10 @@ int main(int argc, FAR char **argv)
   free(context.download_buffer);
 
 error:
-  fastboot_usbdev_deinit(&context);
+  if (context.transport->deinit)

Review Comment:
   remove the check



##########
system/fastboot/fastboot.c:
##########
@@ -217,6 +279,26 @@ static const struct memory_region_s g_memory_region[] =
 };
 #endif
 
+#ifdef CONFIG_USBFASTBOOT
+struct fastboot_transport_s g_transport_usb =

Review Comment:
   add static and const



##########
system/fastboot/fastboot.c:
##########
@@ -1094,29 +1182,206 @@ static int fastboot_usbdev_initialize(FAR struct 
fastboot_ctx_s *ctx)
 
 static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
 {
-  if (ctx->usbdev_out > 0)
+  int i;
+
+  for (i = 0; i < nitems(ctx->t_fd); i++)
+    {
+      if (ctx->t_fd[i] > 0)
+        {
+          close(ctx->t_fd[i]);
+          ctx->t_fd[i] = -1;
+        }
+    }
+}
+
+static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
+                                    FAR void *buf, size_t len)
+{
+  return fastboot_read(ctx->t_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
+#ifdef CONFIG_NET_TCP
+static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
+{
+  struct sockaddr_in addr;
+
+  ctx->t_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->t_fd[0] < 0)
+    {
+      fb_err("create socket failed %d", errno);
+      return -errno;
+    }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+  addr.sin_port = htons(FASTBOOT_TCP_PORT);
+  if (bind(ctx->t_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      return -errno;
+    }
+
+  if (listen(ctx->t_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      return -errno;
+    }
+
+  return 0;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->t_fd[1]);
+  ctx->t_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->t_fd[0]);
+  ctx->t_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)
+{
+  size_t total = 0;
+  ssize_t nread;
+
+  while (total < len)
     {
-      close(ctx->usbdev_out);
-      ctx->usbdev_out = -1;
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
     }
 
-  if (ctx->usbdev_in > 0)
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char buff[FASTBOOT_TCP_HANDSHAKE_LEN];
+  uint64_t data_size;
+  ssize_t nread;
+
+  if (ctx->t_fd[1] == -1)
     {
-      close(ctx->usbdev_in);
-      ctx->usbdev_in = -1;
+      while (1)
+        {
+          /* accept a connection, not care the address of the peer socket */
+
+          ctx->t_fd[1] = accept(ctx->t_fd[0], NULL, 0);
+          if (ctx->t_fd[1] < 0)
+            {
+              continue;
+            }
+
+          /* handshake */
+
+          if ((fastboot_read_all(ctx->t_fd[1], buff,
+               FASTBOOT_TCP_HANDSHAKE_LEN) != FASTBOOT_TCP_HANDSHAKE_LEN) ||
+              (strncmp(buff, FASTBOOT_TCP_HANDSHAKE,
+               FASTBOOT_TCP_HANDSHAKE_LEN) != 0) ||
+              (fastboot_write(ctx->t_fd[1], buff,
+               FASTBOOT_TCP_HANDSHAKE_LEN) < 0))
+            {
+              fb_err("%s err handshake %d", __func__, errno);
+              fastboot_tcp_disconn(ctx);
+              continue;
+            }
+
+          break;
+        }
     }
+
+  if (ctx->left == 0)
+    {
+      nread = fastboot_read_all(ctx->t_fd[1], &data_size, sizeof(data_size));
+      if (nread != sizeof(data_size))
+        {
+          /* as normal, end of file if client has closed the connection */
+
+          if (nread != 0)
+            {
+              fb_err("%s err read data_size %zd %d", __func__, nread, errno);
+            }
+
+          fastboot_tcp_disconn(ctx);
+          return nread;
+        }
+
+      ctx->left = be64toh(data_size);
+    }
+
+  if (len > ctx->left)
+    {
+      len = ctx->left;
+    }
+
+  nread = fastboot_read(ctx->t_fd[1], buf, len);
+  if (nread <= 0)
+    {
+      fastboot_tcp_disconn(ctx);
+      ctx->left = 0;
+    }
+  else
+    {
+      ctx->left -= nread;
+    }
+
+  return nread;
 }
 
+static int fastboot_tcp_write(FAR struct fastboot_ctx_s *ctx,
+                              FAR void *buf, size_t len)
+{
+  uint64_t data_size = htobe64(len);
+  int ret;
+
+  ret = fastboot_write(ctx->t_fd[1], &data_size, sizeof(data_size));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
 static void fastboot_context_initialize(FAR struct fastboot_ctx_s *ctx)
 {
+  ctx->t_fd[1]         = -1;

Review Comment:
   why need? init t_fd[0] too



##########
system/fastboot/fastboot.c:
##########
@@ -1094,29 +1182,206 @@ static int fastboot_usbdev_initialize(FAR struct 
fastboot_ctx_s *ctx)
 
 static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
 {
-  if (ctx->usbdev_out > 0)
+  int i;
+
+  for (i = 0; i < nitems(ctx->t_fd); i++)
+    {
+      if (ctx->t_fd[i] > 0)
+        {
+          close(ctx->t_fd[i]);
+          ctx->t_fd[i] = -1;
+        }
+    }
+}
+
+static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
+                                    FAR void *buf, size_t len)
+{
+  return fastboot_read(ctx->t_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
+#ifdef CONFIG_NET_TCP
+static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
+{
+  struct sockaddr_in addr;
+
+  ctx->t_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->t_fd[0] < 0)
+    {
+      fb_err("create socket failed %d", errno);
+      return -errno;
+    }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+  addr.sin_port = htons(FASTBOOT_TCP_PORT);
+  if (bind(ctx->t_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      return -errno;
+    }
+
+  if (listen(ctx->t_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      return -errno;
+    }
+
+  return 0;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->t_fd[1]);
+  ctx->t_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->t_fd[0]);
+  ctx->t_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)
+{
+  size_t total = 0;
+  ssize_t nread;
+
+  while (total < len)
     {
-      close(ctx->usbdev_out);
-      ctx->usbdev_out = -1;
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
     }
 
-  if (ctx->usbdev_in > 0)
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char buff[FASTBOOT_TCP_HANDSHAKE_LEN];

Review Comment:
   buff->handshake



##########
system/fastboot/fastboot.c:
##########
@@ -1140,10 +1405,13 @@ int main(int argc, FAR char **argv)
           return 0;
         }
 
-      context.wait_ms = atoi(argv[1]);
+      if (sscanf(argv[1], "%" SCNu64 , &context.left) != 1)
+        {
+          return -EINVAL;
+        }
     }
 
-  ret = fastboot_usbdev_initialize(&context);
+  ret = context.transport->init ? context.transport->init(&context) : OK;

Review Comment:
   remove  the check



##########
system/fastboot/fastboot.c:
##########
@@ -217,6 +279,26 @@ static const struct memory_region_s g_memory_region[] =
 };
 #endif
 
+#ifdef CONFIG_USBFASTBOOT
+struct fastboot_transport_s g_transport_usb =
+{
+  .init    = fastboot_usbdev_initialize,
+  .deinit  = fastboot_usbdev_deinit,
+  .read    = fastboot_usbdev_read,
+  .write   = fastboot_usbdev_write,
+};
+#endif
+
+#ifdef CONFIG_NET_TCP
+struct fastboot_transport_s g_transport_tcp =

Review Comment:
   ditto



##########
system/fastboot/fastboot.c:
##########
@@ -1094,29 +1182,206 @@ static int fastboot_usbdev_initialize(FAR struct 
fastboot_ctx_s *ctx)
 
 static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
 {
-  if (ctx->usbdev_out > 0)
+  int i;
+
+  for (i = 0; i < nitems(ctx->t_fd); i++)
+    {
+      if (ctx->t_fd[i] > 0)
+        {
+          close(ctx->t_fd[i]);
+          ctx->t_fd[i] = -1;
+        }
+    }
+}
+
+static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
+                                    FAR void *buf, size_t len)
+{
+  return fastboot_read(ctx->t_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  return fastboot_write(ctx->t_fd[1], buf, len);
+}
+#endif
+
+#ifdef CONFIG_NET_TCP
+static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
+{
+  struct sockaddr_in addr;
+
+  ctx->t_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->t_fd[0] < 0)
+    {
+      fb_err("create socket failed %d", errno);
+      return -errno;
+    }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+  addr.sin_port = htons(FASTBOOT_TCP_PORT);
+  if (bind(ctx->t_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      return -errno;
+    }
+
+  if (listen(ctx->t_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      return -errno;
+    }
+
+  return 0;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->t_fd[1]);
+  ctx->t_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->t_fd[0]);
+  ctx->t_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)
+{
+  size_t total = 0;
+  ssize_t nread;
+
+  while (total < len)
     {
-      close(ctx->usbdev_out);
-      ctx->usbdev_out = -1;
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
     }
 
-  if (ctx->usbdev_in > 0)
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char buff[FASTBOOT_TCP_HANDSHAKE_LEN];
+  uint64_t data_size;
+  ssize_t nread;
+
+  if (ctx->t_fd[1] == -1)
     {
-      close(ctx->usbdev_in);
-      ctx->usbdev_in = -1;
+      while (1)
+        {
+          /* accept a connection, not care the address of the peer socket */
+
+          ctx->t_fd[1] = accept(ctx->t_fd[0], NULL, 0);
+          if (ctx->t_fd[1] < 0)
+            {
+              continue;
+            }
+
+          /* handshake */
+
+          if ((fastboot_read_all(ctx->t_fd[1], buff,
+               FASTBOOT_TCP_HANDSHAKE_LEN) != FASTBOOT_TCP_HANDSHAKE_LEN) ||
+              (strncmp(buff, FASTBOOT_TCP_HANDSHAKE,

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to