You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/10/09 17:03:50 UTC

[nuttx-apps] branch master updated (a3bbea86f -> 91a682f11)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


    from a3bbea86f remove the check that progname matches mainsrc
     new 529c0f1fc system: use fileno to get stream file descriptor
     new 91a682f11 testing: add test application for fopencookie utility

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 system/cle/cle.c                                   |   8 +-
 system/readline/readline.c                         |   8 +-
 {examples/netpkt => testing/fopencookie}/Kconfig   |   9 +-
 .../fmsynth => testing/fopencookie}/Make.defs      |   6 +-
 testing/{smart_test => fopencookie}/Makefile       |   8 +-
 testing/fopencookie/README.md                      |  13 +
 testing/fopencookie/fopencookie.c                  | 309 +++++++++++++++++++++
 7 files changed, 347 insertions(+), 14 deletions(-)
 copy {examples/netpkt => testing/fopencookie}/Kconfig (52%)
 copy {audioutils/fmsynth => testing/fopencookie}/Make.defs (87%)
 copy testing/{smart_test => fopencookie}/Makefile (89%)
 create mode 100644 testing/fopencookie/README.md
 create mode 100644 testing/fopencookie/fopencookie.c


[nuttx-apps] 01/02: system: use fileno to get stream file descriptor

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 529c0f1fc947de2731efcbbad2e912598192eb80
Author: Michal Lenc <mi...@seznam.cz>
AuthorDate: Fri Sep 22 13:37:51 2023 +0200

    system: use fileno to get stream file descriptor
    
    There is a POSIX defined interface to get file descriptor from file_struct
    stream. Applications should use it and not access the structure directly.
    
    Signed-off-by: Michal Lenc <mi...@seznam.cz>
---
 system/cle/cle.c           | 8 +++++++-
 system/readline/readline.c | 8 +++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/system/cle/cle.c b/system/cle/cle.c
index 31e01396c..9a0c0119c 100644
--- a/system/cle/cle.c
+++ b/system/cle/cle.c
@@ -1123,6 +1123,12 @@ int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
 int cle(FAR char *line, FAR const char *prompt, uint16_t linelen,
         FAR FILE *instream, FAR FILE *outstream)
 {
-  return cle_fd(line, prompt, linelen, instream->fs_fd, outstream->fs_fd);
+  int instream_fd;
+  int outstream_fd;
+
+  instream_fd  = fileno(instream);
+  outstream_fd = fileno(outstream);
+
+  return cle_fd(line, prompt, linelen, instream_fd, outstream_fd);
 }
 #endif
diff --git a/system/readline/readline.c b/system/readline/readline.c
index 8f263c2a0..cad9999e1 100644
--- a/system/readline/readline.c
+++ b/system/readline/readline.c
@@ -44,12 +44,18 @@
 #ifdef CONFIG_FILE_STREAM
 ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream)
 {
+  int instream_fd;
+  int outstream_fd;
+
   /* Sanity checks */
 
   DEBUGASSERT(instream && outstream);
 
   /* Let readline_fd do the work */
 
-  return readline_fd(buf, buflen, instream->fs_fd, outstream->fs_fd);
+  instream_fd  = fileno(instream);
+  outstream_fd = fileno(outstream);
+
+  return readline_fd(buf, buflen, instream_fd, outstream_fd);
 }
 #endif


[nuttx-apps] 02/02: testing: add test application for fopencookie utility

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 91a682f11bba1a537bd095cef45f17db57c9e7b9
Author: Michal Lenc <mi...@seznam.cz>
AuthorDate: Mon Oct 9 14:46:24 2023 +0200

    testing: add test application for fopencookie utility
    
    Adds test application for fopencookie utility. The test tool performs
    write/read/seek operations on custom stream defined at user space.
    
    Signed-off-by: Michal Lenc <mi...@seznam.cz>
---
 testing/fopencookie/Kconfig       |  13 ++
 testing/fopencookie/Make.defs     |  23 +++
 testing/fopencookie/Makefile      |  32 ++++
 testing/fopencookie/README.md     |  13 ++
 testing/fopencookie/fopencookie.c | 309 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 390 insertions(+)

diff --git a/testing/fopencookie/Kconfig b/testing/fopencookie/Kconfig
new file mode 100644
index 000000000..cc14780d3
--- /dev/null
+++ b/testing/fopencookie/Kconfig
@@ -0,0 +1,13 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config TESTING_FOPENCOOKIE_TEST
+	tristate "Fopencookie test tool"
+	default n
+	---help---
+		Performs a basic operations with fopencookie call.
+
+if TESTING_FOPENCOOKIE_TEST
+endif
diff --git a/testing/fopencookie/Make.defs b/testing/fopencookie/Make.defs
new file mode 100644
index 000000000..e9ff32601
--- /dev/null
+++ b/testing/fopencookie/Make.defs
@@ -0,0 +1,23 @@
+############################################################################
+# apps/testing/fopencookie/Make.defs
+#
+# 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.
+#
+############################################################################
+
+ifneq ($(CONFIG_TESTING_FOPENCOOKIE_TEST),)
+CONFIGURED_APPS += $(APPDIR)/testing/fopencookie
+endif
diff --git a/testing/fopencookie/Makefile b/testing/fopencookie/Makefile
new file mode 100644
index 000000000..e1c364c65
--- /dev/null
+++ b/testing/fopencookie/Makefile
@@ -0,0 +1,32 @@
+############################################################################
+# apps/testing/fopencookie/Makefile
+#
+# 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.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# SMART filesystem test tool
+
+PROGNAME = fopencookie_test
+PRIORITY = SCHED_PRIORITY_DEFAULT
+STACKSIZE = 4096
+MODULE = $(CONFIG_TESTING_FOPENCOOKIE_TEST)
+
+MAINSRC = fopencookie.c
+
+include $(APPDIR)/Application.mk
diff --git a/testing/fopencookie/README.md b/testing/fopencookie/README.md
new file mode 100644
index 000000000..d24d04733
--- /dev/null
+++ b/testing/fopencookie/README.md
@@ -0,0 +1,13 @@
+# Testing / `fopencookie` Fopencookie
+
+Performs a basic operations with fopencookie call.
+
+```conf
+CONFIG_TESTING_SMART_TEST=y
+```
+
+```
+Usage:
+
+    fopencookie
+```
diff --git a/testing/fopencookie/fopencookie.c b/testing/fopencookie/fopencookie.c
new file mode 100644
index 000000000..c69277899
--- /dev/null
+++ b/testing/fopencookie/fopencookie.c
@@ -0,0 +1,309 @@
+/****************************************************************************
+ * apps/testing/fopencookie/fopencookie.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define BUF_SIZE 32
+
+/****************************************************************************
+ * Private data
+ ****************************************************************************/
+
+struct my_cookie
+{
+  char   *buf;
+  size_t  endpos;
+  size_t  bufsize;
+  off_t   offset;
+};
+
+static const char correctbuf[] = "abcdefghijklmnopqrstuvwxyz";
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+ssize_t fopencookie_write_hook(void *c, const char *buf, size_t size)
+{
+  char *new_buff;
+  off_t realloc_size;
+  struct my_cookie *cookie = (struct my_cookie *)c;
+
+  DEBUGASSERT(c != NULL);
+
+  if (size + cookie->offset > cookie->bufsize)
+    {
+      realloc_size = size + cookie->offset - cookie->bufsize + 1;
+      new_buff = realloc(cookie->buf, realloc_size);
+      if (new_buff == NULL)
+        {
+          fprintf(stderr, "ERROR: Cannot reallocate memory!\n");
+          return -1;
+        }
+
+      cookie->buf = new_buff;
+      cookie->bufsize = realloc_size;
+    }
+
+  memcpy(cookie->buf + cookie->offset, buf, size);
+
+  cookie->offset += size;
+  if (cookie->offset > cookie->endpos)
+    {
+      cookie->endpos = cookie->offset;
+    }
+
+  return size;
+}
+
+ssize_t fopencookie_read_hook(void *c, char *buf, size_t size)
+{
+  struct my_cookie *cookie = (struct my_cookie *)c;
+
+  if (cookie->offset + size > cookie->endpos)
+    {
+      size = cookie->endpos - cookie->offset;
+    }
+
+  if (size < 0)
+    {
+      size = 0;
+    }
+
+  memcpy(buf, cookie->buf + cookie->offset, size);
+
+  cookie->offset += size;
+  return size;
+}
+
+off_t fopencookie_seek_hook(void *c, off_t *offset, int whence)
+{
+  off_t new_offset;
+  struct my_cookie *cookie = (struct my_cookie *)c;
+
+  if (whence == SEEK_SET)
+    {
+      new_offset = *offset;
+    }
+
+  else if (whence == SEEK_END)
+    {
+      new_offset = cookie->endpos + *offset;
+    }
+
+  else if (whence == SEEK_CUR)
+    {
+      new_offset = cookie->offset + *offset;
+    }
+
+  else
+    {
+      return -1;
+    }
+
+  if (new_offset < 0)
+    {
+      return -1;
+    }
+
+  cookie->offset = new_offset;
+  *offset = new_offset;
+  return new_offset;
+}
+
+int fopencookie_close_hook(void *c)
+{
+  struct my_cookie *cookie = (struct my_cookie *)c;
+
+  cookie->bufsize = 0;
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: fopencookie_write_test
+ ****************************************************************************/
+
+static int fopencookie_write_test(FILE *stream)
+{
+  int n;
+
+  n = fputs(correctbuf, stream);
+  if (n < 0)
+    {
+      printf("fopencookie_write_test: fputs failed: %d\n", -errno);
+      return -1;
+    }
+
+  printf("fopencookie_write_test: written buffer: %.*s\n", n, correctbuf);
+  printf("fopencookie_write_test: succesfull\n");
+  return 0;
+}
+
+/****************************************************************************
+ * Name: fopencookie_write_test
+ ****************************************************************************/
+
+static int fopencookie_read_test(FILE *stream)
+{
+  int n;
+  int endpos;
+  char buf[256];
+
+  fseek(stream, 0, SEEK_END);
+  endpos = ftell(stream);
+  fseek(stream, 0, SEEK_SET);
+
+  n = fread(buf, 1, endpos, stream);
+
+  for (int i = 0; i < n; i++)
+    {
+      if (correctbuf[i] != buf[i])
+        {
+          printf("fopencookie_write_test: error %d got %c, expected %c\n", i,
+                 buf[i], correctbuf[i]);
+          return -1;
+        }
+    }
+
+  printf("fopencookie_read_test: read buffer: %.*s\n", n, correctbuf);
+  printf("fopencookie_read_test: succesfull\n");
+  return 0;
+}
+
+/****************************************************************************
+ * Name: fopencookie_seek_test
+ ****************************************************************************/
+
+static int fopencookie_seek_test(FILE *stream)
+{
+  int endpos;
+  int i;
+  int n;
+  char buf[1];
+
+  fseek(stream, 0, SEEK_END);
+  endpos = ftell(stream);
+  fseek(stream, 0, SEEK_SET);
+
+  for (i = 0; i < endpos; i += 2)
+    {
+      fseek(stream, i, SEEK_SET);
+      n = fread(buf, 1, 1, stream);
+      if (n != 1)
+        {
+          printf("fopencookie_seek_test: read at %i failed\n", i);
+        }
+
+      if (correctbuf[i] != buf[0])
+        {
+          printf("fopencookie_seek_test: error %d got %c, expected %c\n", i,
+                 buf[0], correctbuf[i]);
+          return -1;
+        }
+
+      printf("fopencookie_seek_test: pos %d, char %c\n", i, buf[i]);
+    }
+
+  printf("fopencookie_seek_test: succesfull\n");
+
+  fseek(stream, 0, SEEK_SET);
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  int ret;
+  FILE *stream;
+  struct my_cookie mycookie;
+  cookie_io_functions_t cookie_funcs =
+    {
+      .read  = fopencookie_read_hook,
+      .write = fopencookie_write_hook,
+      .seek  = fopencookie_seek_hook,
+      .close = fopencookie_close_hook
+    };
+
+  /* Set up the cookie before calling fopencookie(). */
+
+  mycookie.buf = malloc(BUF_SIZE);
+  if (mycookie.buf == NULL)
+    {
+      printf("Could not allocate memory for cookie.\n");
+      return -1;
+    }
+
+  mycookie.bufsize = BUF_SIZE;
+  mycookie.offset = 0;
+  mycookie.endpos = 0;
+
+  stream = fopencookie(&mycookie, "w+", cookie_funcs);
+  if (stream == NULL)
+    {
+      free(mycookie.buf);
+      return -1;
+    }
+
+  ret = fopencookie_write_test(stream);
+  if (ret < 0)
+    {
+      goto fopencookie_ret;
+    }
+
+  ret = fopencookie_read_test(stream);
+  if (ret < 0)
+    {
+      goto fopencookie_ret;
+    }
+
+  ret = fopencookie_seek_test(stream);
+  if (ret < 0)
+    {
+      goto fopencookie_ret;
+    }
+
+  printf("fopencokie tests were succesfull.\n");
+
+fopencookie_ret:
+  fclose(stream);
+  free(mycookie.buf);
+
+  return ret;
+}