[PATCH rtems6 - v2 08/16] flashdev: add function to deregister

berndmoessner80 at gmail.com berndmoessner80 at gmail.com
Sun Jan 7 18:18:42 UTC 2024


From: Bernd Moessner <berndmoessner80 at gmail.com>

In addition to that update the test case

Updates #4981
---
 cpukit/dev/flash/flashdev.c                   | 12 ++++++++++
 cpukit/include/dev/flash/flashdev.h           | 14 +++++++++++
 testsuites/libtests/flashdev01/init.c         | 15 ++++++++++--
 .../libtests/flashdev01/test_flashdev.c       | 24 +++++++++++++++++++
 .../libtests/flashdev01/test_flashdev.h       |  2 ++
 5 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/cpukit/dev/flash/flashdev.c b/cpukit/dev/flash/flashdev.c
index b908e87023..37d42955eb 100644
--- a/cpukit/dev/flash/flashdev.c
+++ b/cpukit/dev/flash/flashdev.c
@@ -484,6 +484,18 @@ int rtems_flashdev_register(
   return rv;
 }
 
+int rtems_flashdev_deregister(
+  const char *flash_path
+)
+{
+  rtems_filesystem_eval_path_context_t ctx;
+  int eval_flags = RTEMS_FS_FOLLOW_LINK;
+  const rtems_filesystem_location_info_t *currentloc =
+    rtems_filesystem_eval_path_start( &ctx , flash_path, eval_flags );
+
+  return IMFS_rmnod(NULL, currentloc);
+}
+
 static int rtems_flashdev_do_init(
   rtems_flashdev *flash,
   void ( *destroy )( rtems_flashdev *flash )
diff --git a/cpukit/include/dev/flash/flashdev.h b/cpukit/include/dev/flash/flashdev.h
index 6244d38e3a..51e0486148 100644
--- a/cpukit/include/dev/flash/flashdev.h
+++ b/cpukit/include/dev/flash/flashdev.h
@@ -441,6 +441,20 @@ int rtems_flashdev_register(
   const char *flash_path
 );
 
+/**
+ * @brief Deregister the flash device.
+ *
+ * This function removes the node allocated for the flash device.
+ *
+ * @param[in] flash_path The path to the flash device file.
+ *
+ * @retval 0 Successful operation.
+ * @retval non-zero Failed operation.
+ */
+int rtems_flashdev_deregister(
+  const char *flash_path
+);
+
 /**
  * @brief Destroys the flash device.
  *
diff --git a/testsuites/libtests/flashdev01/init.c b/testsuites/libtests/flashdev01/init.c
index 30af3f33c1..48cb033c04 100644
--- a/testsuites/libtests/flashdev01/init.c
+++ b/testsuites/libtests/flashdev01/init.c
@@ -60,17 +60,18 @@ static void run_test(void) {
   int page_count;
   int type;
   size_t wb_size;
+  const char flash_path[] = "/dev/flashdev0";
 
   /* Initalize the flash device driver and flashdev */
   flash = test_flashdev_init();
   rtems_test_assert(flash != NULL);
 
   /* Register the flashdev as a device */
-  status = rtems_flashdev_register(flash, "dev/flashdev0");
+  status = rtems_flashdev_register(flash, flash_path);
   rtems_test_assert(!status);
 
   /* Open the flashdev */
-  file = fopen("dev/flashdev0", "r+");
+  file = fopen(flash_path, "r+");
   rtems_test_assert(file != NULL);
   fd = fileno(file);
 
@@ -159,6 +160,16 @@ static void run_test(void) {
   fseek(file, 0x400, SEEK_SET);
   fgets(buff, 11, file);
   rtems_test_assert(strncmp(buff, "HELLO WORLD", 11));
+
+  /* Close the file handle */
+  status = fclose(file);
+  rtems_test_assert(!status);
+
+  /* Deregister path */
+  status = rtems_flashdev_deregister(flash_path);
+  rtems_test_assert(!status);
+
+  test_flashdev_deinit(flash);
 }
 
 static void Init(rtems_task_argument arg)
diff --git a/testsuites/libtests/flashdev01/test_flashdev.c b/testsuites/libtests/flashdev01/test_flashdev.c
index d97f5d8145..9e257863b1 100644
--- a/testsuites/libtests/flashdev01/test_flashdev.c
+++ b/testsuites/libtests/flashdev01/test_flashdev.c
@@ -273,3 +273,27 @@ rtems_flashdev* test_flashdev_init(void)
 
   return flash;
 }
+
+/* Free Flashdev and underlying driver. */
+void test_flashdev_deinit(
+  rtems_flashdev* flash
+)
+{
+  if (NULL != flash)
+  {
+    if (NULL != flash->driver)
+    {
+      free(flash->region_table);
+    }
+    if (NULL != flash->driver)
+    {
+      test_flashdev* flash_driver = (test_flashdev*) flash->driver;
+      if (NULL != flash_driver->data)
+      {
+        free( flash_driver->data);
+      }
+      free(flash->driver);
+    }
+    rtems_flashdev_destroy_and_free(flash);
+  }
+}
diff --git a/testsuites/libtests/flashdev01/test_flashdev.h b/testsuites/libtests/flashdev01/test_flashdev.h
index 8b03959c42..894c5d3747 100644
--- a/testsuites/libtests/flashdev01/test_flashdev.h
+++ b/testsuites/libtests/flashdev01/test_flashdev.h
@@ -32,4 +32,6 @@
 
 rtems_flashdev* test_flashdev_init(void);
 
+void test_flashdev_deinit(rtems_flashdev* flash);
+
 #endif /* __TEST_FLASHDEV_H */
-- 
2.34.1



More information about the devel mailing list