system/cfgdata: implement "format" option

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2022-03-21 22:51:00 +01:00 committed by Xiang Xiao
parent f1a74533f2
commit c88dd4bbaf

View File

@ -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,13 +639,41 @@ 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);
free(cfg.configdata);
}
/****************************************************************************
* 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]);