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


##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)

Review Comment:
   `static size_t kbd_dump(FAR char *buffer, size_t nbytes)`



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  FAR struct keyboard_event_s *key = (FAR struct keyboard_event_s *)buffer;
+  long nkeys = 0;
+
+  if ((nbytes % sizeof(struct keyboard_event_s)) != 0)
+    {
+      fprintf(stderr, "kbd: short read of %zd bytes, ignoring\n", nbytes);
+      return 0;
+    }
+
+  while (nbytes >= (ssize_t)sizeof(struct keyboard_event_s))
+    {
+      kbd_show(key->code, key->type);
+
+      nbytes -= sizeof(struct keyboard_event_s);
+      key++;
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  FAR const char *devpath = CONFIG_SYSTEM_KBD_DEVPATH;
+  char buffer[KBD_READ_SIZE];
+  long nkeys = 0;
+  long limit = 0;

Review Comment:
   change ALL long to size_t



##########
system/kbd/kbd_main.c:
##########
@@ -178,6 +186,157 @@ static long kbd_dump(FAR char *buffer, ssize_t nbytes)
 }
 #endif
 
+/****************************************************************************
+ * Name: kbd_inject
+ *
+ * Description:
+ *   Turn each byte read from stdin into a key press and release on the
+ *   given device.  The device has to be a uinput keyboard:  a real one has
+ *   nothing to inject into.
+ *
+ *   Note that this always writes struct keyboard_event_s.  Injection goes
+ *   through the lower half write method, which is not affected by
+ *   INPUT_KEYBOARD_BYTESTREAM.
+ *
+ * Returned Value:
+ *   The number of keys injected.
+ *
+ ****************************************************************************/
+
+static bool kbd_emit(int fd, uint32_t code, uint32_t type)
+{
+  struct keyboard_event_s key;
+
+  key.code = code;
+  key.type = type;
+
+  if (write(fd, &key, sizeof(key)) != (ssize_t)sizeof(key))
+    {
+      fprintf(stderr, "kbd: inject failed: %d\n", errno);
+      return false;
+    }
+
+  return true;
+}
+
+static long kbd_inject(int fd)
+{
+  char buf[KBD_READ_SIZE];
+  uint32_t code;
+  ssize_t nread;
+  ssize_t i;
+  long nkeys = 0;
+
+  for (; ; )
+    {
+      nread = read(STDIN_FILENO, buf, sizeof(buf));
+      if (nread <= 0)
+        {
+          break;
+        }
+
+      for (i = 0; i < nread; i++)
+        {
+          /* A terminal sends a carriage return where a keyboard would
+           * send a line feed.
+           */
+
+          code = (buf[i] == '\r') ? '\n' : (uint8_t)buf[i];
+
+          if (!kbd_emit(fd, code, KEYBOARD_PRESS))
+            {
+              return nkeys;
+            }
+
+          kbd_emit(fd, code, KEYBOARD_RELEASE);
+          nkeys++;
+        }
+    }
+
+  return nkeys;
+}
+
+/****************************************************************************
+ * Name: kbd_forward
+ *
+ * Description:
+ *   Copy every key from one keyboard onto another.  Pointing an
+ *   application at the destination lets a real keyboard and an injected one
+ *   drive it at the same time, which neither can do on its own since an
+ *   application opens a single device.
+ *
+ * Returned Value:
+ *   The number of keys forwarded.
+ *
+ ****************************************************************************/
+
+static long kbd_forward(int fd, int srcfd)

Review Comment:
   long->size_t



##########
system/kbd/kbd_main.c:
##########
@@ -178,6 +186,157 @@ static long kbd_dump(FAR char *buffer, ssize_t nbytes)
 }
 #endif
 
+/****************************************************************************
+ * Name: kbd_inject
+ *
+ * Description:
+ *   Turn each byte read from stdin into a key press and release on the
+ *   given device.  The device has to be a uinput keyboard:  a real one has
+ *   nothing to inject into.
+ *
+ *   Note that this always writes struct keyboard_event_s.  Injection goes
+ *   through the lower half write method, which is not affected by
+ *   INPUT_KEYBOARD_BYTESTREAM.
+ *
+ * Returned Value:
+ *   The number of keys injected.
+ *
+ ****************************************************************************/
+
+static bool kbd_emit(int fd, uint32_t code, uint32_t type)
+{
+  struct keyboard_event_s key;
+
+  key.code = code;
+  key.type = type;
+
+  if (write(fd, &key, sizeof(key)) != (ssize_t)sizeof(key))
+    {
+      fprintf(stderr, "kbd: inject failed: %d\n", errno);
+      return false;
+    }
+
+  return true;
+}
+
+static long kbd_inject(int fd)

Review Comment:
   long->size_t



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  FAR struct keyboard_event_s *key = (FAR struct keyboard_event_s *)buffer;
+  long nkeys = 0;
+
+  if ((nbytes % sizeof(struct keyboard_event_s)) != 0)
+    {
+      fprintf(stderr, "kbd: short read of %zd bytes, ignoring\n", nbytes);
+      return 0;
+    }
+
+  while (nbytes >= (ssize_t)sizeof(struct keyboard_event_s))
+    {
+      kbd_show(key->code, key->type);
+
+      nbytes -= sizeof(struct keyboard_event_s);
+      key++;
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  FAR const char *devpath = CONFIG_SYSTEM_KBD_DEVPATH;
+  char buffer[KBD_READ_SIZE];
+  long nkeys = 0;
+  long limit = 0;
+  ssize_t nbytes;
+  int fd;
+
+  if (argc > 1)
+    {
+      devpath = argv[1];
+    }
+
+  if (argc > 2)
+    {
+      limit = strtol(argv[2], NULL, 10);
+    }
+
+  /* Wait for the device.  A USB keyboard appears when it is plugged in, so
+   * this is the normal way to start rather than an error path.
+   */
+
+  for (; ; )
+    {
+      fd = open(devpath, O_RDONLY);

Review Comment:
   add O_CLOEXEC



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  FAR struct keyboard_event_s *key = (FAR struct keyboard_event_s *)buffer;
+  long nkeys = 0;
+
+  if ((nbytes % sizeof(struct keyboard_event_s)) != 0)
+    {
+      fprintf(stderr, "kbd: short read of %zd bytes, ignoring\n", nbytes);
+      return 0;
+    }
+
+  while (nbytes >= (ssize_t)sizeof(struct keyboard_event_s))

Review Comment:
   remove the cast



##########
system/kbd/kbd_main.c:
##########
@@ -178,6 +186,157 @@ static long kbd_dump(FAR char *buffer, ssize_t nbytes)
 }
 #endif
 
+/****************************************************************************
+ * Name: kbd_inject
+ *
+ * Description:
+ *   Turn each byte read from stdin into a key press and release on the
+ *   given device.  The device has to be a uinput keyboard:  a real one has
+ *   nothing to inject into.
+ *
+ *   Note that this always writes struct keyboard_event_s.  Injection goes
+ *   through the lower half write method, which is not affected by
+ *   INPUT_KEYBOARD_BYTESTREAM.
+ *
+ * Returned Value:
+ *   The number of keys injected.
+ *
+ ****************************************************************************/
+
+static bool kbd_emit(int fd, uint32_t code, uint32_t type)
+{
+  struct keyboard_event_s key;
+
+  key.code = code;
+  key.type = type;
+
+  if (write(fd, &key, sizeof(key)) != (ssize_t)sizeof(key))
+    {
+      fprintf(stderr, "kbd: inject failed: %d\n", errno);
+      return false;
+    }
+
+  return true;
+}
+
+static long kbd_inject(int fd)
+{
+  char buf[KBD_READ_SIZE];

Review Comment:
   unsigned char and remove the cast at line 244



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  FAR struct keyboard_event_s *key = (FAR struct keyboard_event_s *)buffer;
+  long nkeys = 0;

Review Comment:
   size_t



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;

Review Comment:
   ```
     size_t nkeys = 0;
     uint8_t ch;
   ```



##########
system/kbd/kbd_main.c:
##########
@@ -178,6 +186,157 @@ static long kbd_dump(FAR char *buffer, ssize_t nbytes)
 }
 #endif
 
+/****************************************************************************
+ * Name: kbd_inject
+ *
+ * Description:
+ *   Turn each byte read from stdin into a key press and release on the
+ *   given device.  The device has to be a uinput keyboard:  a real one has
+ *   nothing to inject into.
+ *
+ *   Note that this always writes struct keyboard_event_s.  Injection goes
+ *   through the lower half write method, which is not affected by
+ *   INPUT_KEYBOARD_BYTESTREAM.
+ *
+ * Returned Value:
+ *   The number of keys injected.
+ *
+ ****************************************************************************/
+
+static bool kbd_emit(int fd, uint32_t code, uint32_t type)
+{
+  struct keyboard_event_s key;
+
+  key.code = code;
+  key.type = type;
+
+  if (write(fd, &key, sizeof(key)) != (ssize_t)sizeof(key))
+    {
+      fprintf(stderr, "kbd: inject failed: %d\n", errno);
+      return false;
+    }
+
+  return true;
+}
+
+static long kbd_inject(int fd)
+{
+  char buf[KBD_READ_SIZE];
+  uint32_t code;
+  ssize_t nread;
+  ssize_t i;
+  long nkeys = 0;

Review Comment:
   `size_t nkeys = 0;`



##########
system/kbd/kbd_main.c:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/system/kbd/kbd_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Dump what a keyboard reports.
+ *
+ * Every keyboard registered with keyboard_register() is read the same way,
+ * whatever the hardware behind it is:  a USB HID keyboard, a matrix, the
+ * simulator, virtio.  So this works with all of them, and it is the place
+ * to look when bringing up a new one.
+ *
+ * The device delivers struct keyboard_event_s events unless the kernel was
+ * built with INPUT_KEYBOARD_BYTESTREAM, in which case it delivers the byte
+ * stream that the keyboard codec defines.  This follows that setting rather
+ * than having a switch of its own.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/input/keyboard.h>
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+#  include <nuttx/streams.h>
+#  include <nuttx/input/kbd_codec.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define KBD_READ_SIZE 64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kbd_typename
+ ****************************************************************************/
+
+static FAR const char *kbd_typename(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "specpress";
+
+      case KEYBOARD_SPECREL:
+        return "specrel";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Name: kbd_show
+ *
+ * Description:
+ *   Print one key.  A normal key carries a character, a special key carries
+ *   a value from enum kbd_keycode_e, and the type is what says which.
+ *
+ ****************************************************************************/
+
+static void kbd_show(uint32_t code, uint32_t type)
+{
+  if (type == KEYBOARD_PRESS || type == KEYBOARD_RELEASE)
+    {
+      printf("%-9s code %3" PRIu32 " '%c'\n", kbd_typename(type), code,
+             isprint(code) ? (int)code : '.');
+    }
+  else
+    {
+      printf("%-9s keycode %" PRIu32 "\n", kbd_typename(type), code);
+    }
+
+  fflush(stdout);
+}
+
+/****************************************************************************
+ * Name: kbd_dump
+ *
+ * Description:
+ *   Print everything in one read.
+ *
+ * Returned Value:
+ *   The number of keys printed.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_INPUT_KEYBOARD_BYTESTREAM
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  struct lib_meminstream_s stream;
+  struct kbd_getstate_s state;
+  uint8_t ch;
+  long nkeys = 0;
+  int ret;
+
+  lib_meminstream(&stream, buffer, nbytes);
+  memset(&state, 0, sizeof(state));
+
+  for (; ; )
+    {
+      ret = kbd_decode(&stream.common, &state, &ch);
+      if (ret == KBD_ERROR)
+        {
+          break;
+        }
+
+      kbd_show(ch, ret);
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#else
+static long kbd_dump(FAR char *buffer, ssize_t nbytes)
+{
+  FAR struct keyboard_event_s *key = (FAR struct keyboard_event_s *)buffer;
+  long nkeys = 0;
+
+  if ((nbytes % sizeof(struct keyboard_event_s)) != 0)
+    {
+      fprintf(stderr, "kbd: short read of %zd bytes, ignoring\n", nbytes);
+      return 0;
+    }
+
+  while (nbytes >= (ssize_t)sizeof(struct keyboard_event_s))
+    {
+      kbd_show(key->code, key->type);
+
+      nbytes -= sizeof(struct keyboard_event_s);
+      key++;
+      nkeys++;
+    }
+
+  return nkeys;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  FAR const char *devpath = CONFIG_SYSTEM_KBD_DEVPATH;
+  char buffer[KBD_READ_SIZE];
+  long nkeys = 0;
+  long limit = 0;
+  ssize_t nbytes;
+  int fd;
+
+  if (argc > 1)
+    {
+      devpath = argv[1];
+    }
+
+  if (argc > 2)
+    {
+      limit = strtol(argv[2], NULL, 10);

Review Comment:
   strtoul



##########
system/kbd/kbd_main.c:
##########
@@ -178,6 +186,157 @@ static long kbd_dump(FAR char *buffer, ssize_t nbytes)
 }
 #endif
 
+/****************************************************************************
+ * Name: kbd_inject
+ *
+ * Description:
+ *   Turn each byte read from stdin into a key press and release on the
+ *   given device.  The device has to be a uinput keyboard:  a real one has
+ *   nothing to inject into.
+ *
+ *   Note that this always writes struct keyboard_event_s.  Injection goes
+ *   through the lower half write method, which is not affected by
+ *   INPUT_KEYBOARD_BYTESTREAM.
+ *
+ * Returned Value:
+ *   The number of keys injected.
+ *
+ ****************************************************************************/
+
+static bool kbd_emit(int fd, uint32_t code, uint32_t type)
+{
+  struct keyboard_event_s key;
+
+  key.code = code;
+  key.type = type;
+
+  if (write(fd, &key, sizeof(key)) != (ssize_t)sizeof(key))
+    {
+      fprintf(stderr, "kbd: inject failed: %d\n", errno);
+      return false;
+    }
+
+  return true;
+}
+
+static long kbd_inject(int fd)
+{
+  char buf[KBD_READ_SIZE];
+  uint32_t code;
+  ssize_t nread;
+  ssize_t i;
+  long nkeys = 0;
+
+  for (; ; )
+    {
+      nread = read(STDIN_FILENO, buf, sizeof(buf));
+      if (nread <= 0)
+        {
+          break;
+        }
+
+      for (i = 0; i < nread; i++)
+        {
+          /* A terminal sends a carriage return where a keyboard would
+           * send a line feed.
+           */
+
+          code = (buf[i] == '\r') ? '\n' : (uint8_t)buf[i];
+
+          if (!kbd_emit(fd, code, KEYBOARD_PRESS))
+            {
+              return nkeys;
+            }
+
+          kbd_emit(fd, code, KEYBOARD_RELEASE);
+          nkeys++;
+        }
+    }
+
+  return nkeys;
+}
+
+/****************************************************************************
+ * Name: kbd_forward
+ *
+ * Description:
+ *   Copy every key from one keyboard onto another.  Pointing an
+ *   application at the destination lets a real keyboard and an injected one
+ *   drive it at the same time, which neither can do on its own since an
+ *   application opens a single device.
+ *
+ * Returned Value:
+ *   The number of keys forwarded.
+ *
+ ****************************************************************************/
+
+static long kbd_forward(int fd, int srcfd)
+{
+  char buf[KBD_READ_SIZE];
+  ssize_t nread;
+  long nkeys = 0;

Review Comment:
   long->size_t



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to