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


##########
system/fastboot/fastboot.c:
##########
@@ -1074,18 +1151,240 @@ static int fastboot_usbdev_initialize(void)
     }
 #endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
 
+  ctx->tran_fd[0]  =
+      fastboot_open_usb(FASTBOOT_EP_BULKOUT_IDX, O_RDONLY | O_CLOEXEC);
+  if (ctx->tran_fd[0] < 0)
+    {
+      return ctx->tran_fd[0];
+    }
+
+  ctx->tran_fd[1] =
+      fastboot_open_usb(FASTBOOT_EP_BULKIN_IDX, O_WRONLY | O_CLOEXEC);
+  if (ctx->tran_fd[1] < 0)
+    {
+      close(ctx->tran_fd[0]);
+      ctx->tran_fd[0] = -1;
+      return ctx->tran_fd[1];
+    }
+
   return 0;
 }
 
+static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  int i;
+
+  for (i = 0; i < nitems(ctx->tran_fd); i++)
+    {
+      close(ctx->tran_fd[i]);
+      ctx->tran_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->tran_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR const void *buf, size_t len)
+{
+  return fastboot_write(ctx->tran_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->tran_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->tran_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->tran_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      goto error;
+    }
+
+  if (listen(ctx->tran_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      goto error;
+    }
+
+  return 0;
+error:
+  close(ctx->tran_fd[0]);
+  ctx->tran_fd[0] = -1;
+  return -errno;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->tran_fd[1]);
+  ctx->tran_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->tran_fd[0]);
+  ctx->tran_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)
+    {
+      nread = fastboot_read(fd, buf, len);
+      if (nread <= 0)
+        {
+          if (total == 0)
+            {
+              return nread;
+            }
+
+          break;
+        }
+
+      total += nread;
+    }
+
+  return total;
+}
+
+static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
+                                 FAR void *buf, size_t len)
+{
+  char handshake[FASTBOOT_TCP_HANDSHAKE_LEN];
+  uint64_t data_size;
+  ssize_t nread;
+
+  if (ctx->tran_fd[1] == -1)
+    {
+      while (1)
+        {
+          /* Accept a connection, not care the address of the peer socket */
+
+          ctx->tran_fd[1] = accept(ctx->tran_fd[0], NULL, 0);
+          if (ctx->tran_fd[1] < 0)
+            {
+              continue;
+            }
+
+          /* Handshake */
+
+          if (fastboot_read_all(ctx->tran_fd[1], handshake,
+                                sizeof(handshake)) != sizeof(handshake) ||
+              strncmp(handshake, FASTBOOT_TCP_HANDSHAKE,
+                      sizeof(handshake)) != 0 ||
+              fastboot_write(ctx->tran_fd[1], handshake,
+                             sizeof(handshake)) < 0)
+            {
+              fb_err("%s err handshake %d", __func__, errno);

Review Comment:
   whether need to print handshake



##########
system/fastboot/fastboot.c:
##########
@@ -1074,18 +1151,240 @@ static int fastboot_usbdev_initialize(void)
     }
 #endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
 
+  ctx->tran_fd[0]  =
+      fastboot_open_usb(FASTBOOT_EP_BULKOUT_IDX, O_RDONLY | O_CLOEXEC);
+  if (ctx->tran_fd[0] < 0)
+    {
+      return ctx->tran_fd[0];
+    }
+
+  ctx->tran_fd[1] =
+      fastboot_open_usb(FASTBOOT_EP_BULKIN_IDX, O_WRONLY | O_CLOEXEC);
+  if (ctx->tran_fd[1] < 0)
+    {
+      close(ctx->tran_fd[0]);
+      ctx->tran_fd[0] = -1;
+      return ctx->tran_fd[1];
+    }
+
   return 0;
 }
 
+static void fastboot_usbdev_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  int i;
+
+  for (i = 0; i < nitems(ctx->tran_fd); i++)
+    {
+      close(ctx->tran_fd[i]);
+      ctx->tran_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->tran_fd[0], buf, len);
+}
+
+static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
+                                 FAR const void *buf, size_t len)
+{
+  return fastboot_write(ctx->tran_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->tran_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
+  if (ctx->tran_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->tran_fd[0], (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    {
+      fb_err("bind() failed %d", errno);
+      goto error;
+    }
+
+  if (listen(ctx->tran_fd[0], 1) < 0)
+    {
+      fb_err("listen() failed %d", errno);
+      goto error;
+    }
+
+  return 0;
+error:
+  close(ctx->tran_fd[0]);
+  ctx->tran_fd[0] = -1;
+  return -errno;
+}
+
+static void fastboot_tcp_disconn(FAR struct fastboot_ctx_s *ctx)
+{
+  close(ctx->tran_fd[1]);
+  ctx->tran_fd[1] = -1;
+}
+
+static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx)
+{
+  fastboot_tcp_disconn(ctx);
+  close(ctx->tran_fd[0]);
+  ctx->tran_fd[0] = -1;
+}
+
+static ssize_t fastboot_read_all(int fd, FAR void *buf, size_t len)

Review Comment:
   ```suggestion
   static ssize_t fastboot_tcp_read_all(int fd, FAR void *buf, size_t len)
   ```



-- 
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