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/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new c88dd4b  system/cfgdata: implement "format" option
c88dd4b is described below

commit c88dd4bbaf2deed7fca738efaaba05516199b420
Author: Petro Karashchenko <petro.karashche...@gmail.com>
AuthorDate: Mon Mar 21 22:51:00 2022 +0100

    system/cfgdata: implement "format" option
    
    Signed-off-by: Petro Karashchenko <petro.karashche...@gmail.com>
---
 system/cfgdata/cfgdata_main.c | 77 +++++++++++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 18 deletions(-)

diff --git a/system/cfgdata/cfgdata_main.c b/system/cfgdata/cfgdata_main.c
index 462b702..50514a6 100644
--- a/system/cfgdata/cfgdata_main.c
+++ b/system/cfgdata/cfgdata_main.c
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <inttypes.h>
 
 /****************************************************************************
  * Private data
@@ -87,7 +88,9 @@ static void cfgdatacmd_help(void)
 static int cfgdatacmd_idtok(int startpos, char *token)
 {
   while (token[startpos] != ',' && token[startpos] != '\0')
-    startpos++;
+    {
+      startpos++;
+    }
 
   if (token[startpos] != ',')
     {
@@ -112,7 +115,7 @@ static int cfgdatacmd_idtok(int startpos, char *token)
  ****************************************************************************/
 
 static void cfgdatacmd_parse_byte_array(struct config_data_s *cfg,
-        int argc, char *argv[])
+                                        int argc, char *argv[])
 {
   int   x;
   int   c;
@@ -162,7 +165,7 @@ static void cfgdatacmd_parse_byte_array(struct 
config_data_s *cfg,
     {
       /* Perform dynamic memory allocation */
 
-      cfg->configdata = (FAR uint8_t *) malloc(count);
+      cfg->configdata = (FAR uint8_t *)malloc(count);
       cfg->len = count;
     }
 
@@ -183,13 +186,13 @@ static void cfgdatacmd_parse_byte_array(struct 
config_data_s *cfg,
           /* Hex byte */
 
           sscanf(&argv[x][c + 2], "%x", &val);
-          cfg->configdata[count] = (uint8_t) val;
+          cfg->configdata[count] = (uint8_t)val;
         }
       else
         {
           /* Decimal value */
 
-          cfg->configdata[count] = (uint8_t) atoi(&argv[x][c]);
+          cfg->configdata[count] = (uint8_t)atoi(&argv[x][c]);
         }
 
       /* Increment the count */
@@ -295,7 +298,7 @@ static void cfgdatacmd_set(int argc, char *argv[])
 
           if (isnumber)
             {
-              sscanf(&argv[3][2], "%x", (int32_t *) &cfg.configdata);
+              sscanf(&argv[3][2], "%" SCNx32, (int32_t *)&cfg.configdata);
               cfg.len = 4;
             }
         }
@@ -315,7 +318,7 @@ static void cfgdatacmd_set(int argc, char *argv[])
           if (isnumber)
             {
               int32_t temp = atoi(argv[3]);
-              *((int32_t *) cfg.configdata) = temp;
+              *((int32_t *)cfg.configdata) = temp;
               cfg.len = 4;
             }
         }
@@ -324,14 +327,14 @@ static void cfgdatacmd_set(int argc, char *argv[])
         {
           /* Point to the string and calculate the length */
 
-          cfg.configdata = (FAR uint8_t *) argv[3];
+          cfg.configdata = (FAR uint8_t *)argv[3];
           cfg.len = strlen(argv[3]) + 1;
         }
     }
 
   /* Now open the /dev/config file and set the config item */
 
-  if ((fd = open(g_config_dev, O_RDONLY)) < 2)
+  if ((fd = open(g_config_dev, O_WRONLY)) < 2)
     {
       /* Display error */
 
@@ -339,7 +342,7 @@ static void cfgdatacmd_set(int argc, char *argv[])
       return;
     }
 
-  ret = ioctl(fd, CFGDIOC_SETCONFIG, (unsigned long) &cfg);
+  ret = ioctl(fd, CFGDIOC_SETCONFIG, (unsigned long)(uintptr_t)&cfg);
 
   /* Close the file and report error if any */
 
@@ -351,7 +354,7 @@ static void cfgdatacmd_set(int argc, char *argv[])
 
   /* Free the cfg.configdata if needed */
 
-  if (cfg.configdata != (FAR uint8_t *) argv[3] &&
+  if (cfg.configdata != (FAR uint8_t *)argv[3] &&
       cfg.configdata != data)
     {
       free(cfg.configdata);
@@ -398,7 +401,7 @@ static void cfgdatacmd_unset(int argc, char *argv[])
 
   /* Try to open the /dev/config file */
 
-  if ((fd = open(g_config_dev, O_RDONLY)) < 2)
+  if ((fd = open(g_config_dev, O_WRONLY)) < 2)
     {
       /* Display error */
 
@@ -408,7 +411,7 @@ static void cfgdatacmd_unset(int argc, char *argv[])
 
   /* Delete the config item */
 
-  ret = ioctl(fd, CFGDIOC_DELCONFIG, (unsigned long) &cfg);
+  ret = ioctl(fd, CFGDIOC_DELCONFIG, (unsigned long)(uintptr_t)&cfg);
   close(fd);
 
   if (ret != OK)
@@ -468,7 +471,7 @@ static void cfgdatacmd_print(int argc, char *argv[])
       return;
     }
 
-  cfg.configdata = (FAR uint8_t *) malloc(256);
+  cfg.configdata = (FAR uint8_t *)malloc(256);
   cfg.len = 256;
   if (cfg.configdata == NULL)
     {
@@ -478,7 +481,7 @@ static void cfgdatacmd_print(int argc, char *argv[])
 
   /* Get the config item */
 
-  ret = ioctl(fd, CFGDIOC_GETCONFIG, (unsigned long) &cfg);
+  ret = ioctl(fd, CFGDIOC_GETCONFIG, (unsigned long)(uintptr_t)&cfg);
   close(fd);
 
   if (ret != OK)
@@ -567,7 +570,7 @@ static void cfgdatacmd_show_all_config_items(void)
 
   /* Get the first config item */
 
-  cfg.configdata = (FAR uint8_t *) malloc(256);
+  cfg.configdata = (FAR uint8_t *)malloc(256);
   cfg.len = 256;
   if (cfg.configdata == NULL)
     {
@@ -575,7 +578,7 @@ static void cfgdatacmd_show_all_config_items(void)
       return;
     }
 
-  ret = ioctl(fd, CFGDIOC_FIRSTCONFIG, (unsigned long) &cfg);
+  ret = ioctl(fd, CFGDIOC_FIRSTCONFIG, (unsigned long)(uintptr_t)&cfg);
 
   while (ret != -1)
     {
@@ -636,7 +639,7 @@ static void cfgdatacmd_show_all_config_items(void)
       /* Get the next config item */
 
       cfg.len = 256;
-      ret = ioctl(fd, CFGDIOC_NEXTCONFIG, (unsigned long) &cfg);
+      ret = ioctl(fd, CFGDIOC_NEXTCONFIG, (unsigned long)(uintptr_t)&cfg);
     }
 
   close(fd);
@@ -644,6 +647,34 @@ static void cfgdatacmd_show_all_config_items(void)
 }
 
 /****************************************************************************
+ * Erase all config items
+ ****************************************************************************/
+
+static void cfgdatacmd_format(void)
+{
+  int fd;
+  int ret;
+
+  /* Try to open the /dev/config file */
+
+  if ((fd = open(g_config_dev, O_WRONLY)) < 2)
+    {
+      /* Display error */
+
+      printf("error: unable to open %s\n", g_config_dev);
+      return;
+    }
+
+  ret = ioctl(fd, MTDIOC_BULKERASE, 0);
+  close(fd);
+
+  if (ret != OK)
+    {
+      printf("Error %d config format\n", errno);
+    }
+}
+
+/****************************************************************************
  * Public Functions
  ****************************************************************************/
 
@@ -721,6 +752,16 @@ int main(int argc, FAR char *argv[])
       return 0;
     }
 
+  /* Test for "format" cmd */
+
+  if (strcmp(argv[1], "format") == 0)
+    {
+      /* Call the routine to erase all config items */
+
+      cfgdatacmd_format();
+      return 0;
+    }
+
   /* Unknown cmd */
 
   printf("Unknown config command: %s\n", argv[1]);

Reply via email to