xiaoxiang781216 commented on code in PR #2178: URL: https://github.com/apache/nuttx-apps/pull/2178#discussion_r1383715101
########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; + int i; + int ret = OK; + uint16_t valid; + uint16_t count; + + fd = open(file, O_RDONLY); + if (fd < 0) + { + return -ENOENT; + } + + valid = 0; + read(fd, &valid, sizeof(uint16_t)); + + if (valid != VALID) + { + goto abort; /* Just exit - the settings aren't valid. What to do? */ + } + + count = 0; + read(fd, &count, sizeof(uint16_t)); + + uint32_t calc_crc = 0; + uint32_t exp_crc = 0; + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + calc_crc = crc32part((FAR uint8_t *)&setting, sizeof(setting_t), + calc_crc); + } + + read(fd, &exp_crc, sizeof(uint32_t)); + + if (calc_crc != exp_crc) + { + ret = -EIO; + goto abort; + } + + lseek(fd, (sizeof(uint16_t) *2), SEEK_SET); /* Get after valid & size */ Review Comment: ```suggestion lseek(fd, (sizeof(uint16_t) * 2), SEEK_SET); /* Get after valid & size */ ``` ########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; Review Comment: ```suggestion int fd; ``` ########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; + int i; + int ret = OK; + uint16_t valid; + uint16_t count; + + fd = open(file, O_RDONLY); + if (fd < 0) + { + return -ENOENT; + } + + valid = 0; + read(fd, &valid, sizeof(uint16_t)); + + if (valid != VALID) + { + goto abort; /* Just exit - the settings aren't valid. What to do? */ + } + + count = 0; + read(fd, &count, sizeof(uint16_t)); + + uint32_t calc_crc = 0; + uint32_t exp_crc = 0; + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + calc_crc = crc32part((FAR uint8_t *)&setting, sizeof(setting_t), + calc_crc); + } + + read(fd, &exp_crc, sizeof(uint32_t)); + + if (calc_crc != exp_crc) + { + ret = -EIO; + goto abort; + } + + lseek(fd, (sizeof(uint16_t) *2), SEEK_SET); /* Get after valid & size */ + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + + FAR setting_t *slot = getsetting(setting.key); Review Comment: move the declaration to the beginning of for loop ########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; + int i; + int ret = OK; + uint16_t valid; + uint16_t count; + + fd = open(file, O_RDONLY); + if (fd < 0) + { + return -ENOENT; + } + + valid = 0; + read(fd, &valid, sizeof(uint16_t)); + + if (valid != VALID) + { + goto abort; /* Just exit - the settings aren't valid. What to do? */ + } + + count = 0; + read(fd, &count, sizeof(uint16_t)); + + uint32_t calc_crc = 0; + uint32_t exp_crc = 0; + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + calc_crc = crc32part((FAR uint8_t *)&setting, sizeof(setting_t), + calc_crc); + } + + read(fd, &exp_crc, sizeof(uint32_t)); + + if (calc_crc != exp_crc) + { + ret = -EIO; + goto abort; + } + + lseek(fd, (sizeof(uint16_t) *2), SEEK_SET); /* Get after valid & size */ + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + + FAR setting_t *slot = getsetting(setting.key); + if (slot == NULL) + { + continue; + } + + memcpy(slot, &setting, sizeof(setting_t)); + } + +abort: + close(fd); + return ret; +} + +/**************************************************************************** + * Name: save_bin + * + * Description: + * Saves binary data to a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int save_bin(FAR char *file) +{ + FAR uint8_t *buffer = malloc(BUFFER_SIZE); + int count; + int fd; + int ret = OK; + + if (buffer == NULL) + { + return -ENOMEM; + } + + count = 0; + int i; + for (i = 0; i < CONFIG_SETTINGS_MAP_SIZE; i++) + { + if (g_settings.map[i].type == SETTING_EMPTY) + { + break; + } + + count++; + } + + fd = open(file, (O_RDWR | O_TRUNC), 0666); + if (fd < 0) + { + ret = -ENODEV; + goto abort; + } + + memset(buffer, 0xff, BUFFER_SIZE); + *((FAR uint16_t *)buffer) = VALID; + *(((FAR uint16_t *)buffer) + 1) = count; + + off_t offset = sizeof(uint16_t) *2; /* Valid & count */ Review Comment: ```suggestion off_t offset = sizeof(uint16_t) * 2; /* Valid & count */ ``` move the declaration to the beginning of function, same for data_size, rem_data, crc and rem_crc. ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); Review Comment: move to the beginning of function ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); + if (buffer == NULL) + { + ret = -ENOMEM; + goto abort; + } + + while (fgets(buffer, BUFFER_SIZE, f)) + { + /* Remove any line terminators */ + + for (int i = ((int)strlen(buffer) - 1); i > 0; i--) Review Comment: ```suggestion int i; for (i = ((int)strlen(buffer) - 1); i > 0; i--) ``` ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); + if (buffer == NULL) + { + ret = -ENOMEM; + goto abort; + } + + while (fgets(buffer, BUFFER_SIZE, f)) + { + /* Remove any line terminators */ + + for (int i = ((int)strlen(buffer) - 1); i > 0; i--) + { + if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r') + { + buffer[i] = '\0'; + } + else + { + break; + } + } + + /* Separate the key / value pair */ + + FAR char *eq = strchr(buffer, '='); + if (eq == NULL) + { + continue; + } + + FAR char *key = buffer; + FAR char *val = eq + 1; + *eq = '\0'; + + /* Check if the key is valid */ + + if (!isalpha(key[0])) + { + continue; + } + + /* Get the setting slot */ + + FAR setting_t *setting = getsetting(key); Review Comment: move to the beginning of function ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); + if (buffer == NULL) + { + ret = -ENOMEM; + goto abort; + } + + while (fgets(buffer, BUFFER_SIZE, f)) + { + /* Remove any line terminators */ + + for (int i = ((int)strlen(buffer) - 1); i > 0; i--) + { + if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r') + { + buffer[i] = '\0'; + } + else + { + break; + } + } + + /* Separate the key / value pair */ + + FAR char *eq = strchr(buffer, '='); + if (eq == NULL) + { + continue; + } + + FAR char *key = buffer; + FAR char *val = eq + 1; Review Comment: move to the beginning of function ########## system/settings/settings.c: ########## @@ -0,0 +1,1432 @@ +/**************************************************************************** + * apps/system/settings/settings.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 <sys/types.h> +#include <pthread.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <nuttx/crc32.h> +#include <errno.h> +#include <signal.h> +#include <time.h> +#include <assert.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <ctype.h> +#include <nuttx/config.h> +#include <sys/types.h> + +#include "system/storage.h" +#include "system/settings.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_SETTINGS_CACHED_SAVES +# define TIMER_SIGNAL SIGRTMIN +#endif + +#ifndef CONFIG_SETTINGS_SIGNO +# define CONFIG_SETTINGS_SIGNO 32 +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int sanity_check(FAR char *str); +static uint32_t hash_calc(void); +static int get_setting(FAR char *key, FAR setting_t *setting); +static size_t get_string(FAR setting_t *setting, FAR char *buffer, + size_t size); +static int set_string(FAR setting_t *setting, FAR char *str); +static int get_int(FAR setting_t *setting, FAR int *i); +static int set_int(FAR setting_t *setting, int i); +static int get_bool(FAR setting_t *setting, FAR int *i); +static int set_bool(FAR setting_t *setting, int i); +static int get_float(FAR setting_t *setting, FAR double *f); +static int set_float(FAR setting_t *setting, double f); +static int get_ip(FAR setting_t *setting, struct in_addr *ip); +static int set_ip(FAR setting_t *setting, struct in_addr *ip); +static int load(void); +static int save(void); +static void signotify(void); +static int dump_cache(union sigval value); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +state_t g_settings; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hash_calc + * + * Description: + * Gets a setting for a string value + * + * Input Parameters: + * none + * Returned Value: + * crc32 hash of all the settings + * + ****************************************************************************/ + +static uint32_t hash_calc(void) +{ + return crc32((FAR uint8_t *)g_settings.map, sizeof(g_settings.map)); +} + +/**************************************************************************** + * Name: get_setting + * + * Description: + * Gets a setting for a string value + * + * Input Parameters: + * key - key of the required setting + * setting - pointer to return the setting + * + * Returned Value: + * The value of the setting for the given key + * + ****************************************************************************/ + +static int get_setting(FAR char *key, FAR setting_t *setting) +{ + int i; + int ret = OK; + + for (i = 0; i < CONFIG_SETTINGS_MAP_SIZE; i++) + { + if (g_settings.map[i].type == SETTING_EMPTY) + { + ret = -ENOENT; + goto exit; + } + + if (strcmp(g_settings.map[i].key, key) == 0) + { + *setting = g_settings.map[i]; + goto exit; + } + } + + ret = -ENOENT; + +exit: + return ret; +} + +/**************************************************************************** + * Name: get_string + * + * Description: + * Gets a setting for a string value + * + * Input Parameters: + * setting - pointer to the setting type + * str - pointer to return the string value + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static size_t get_string(FAR setting_t *setting, FAR char *buffer, + size_t size) +{ + DEBUGASSERT(setting == NULL); + DEBUGASSERT((setting->type == SETTING_STRING) || + (setting->type == SETTING_IP_ADDR)); + + if (setting->type == SETTING_STRING) + { + FAR const char *s = setting->s; + size_t len = strlen(s); + + DEBUGASSERT(len < size); + if (len >= size) + { + return 0; + } + + strncpy(buffer, s, size); + buffer[size - 1] = '\0'; + + return len; + } + else if (setting->type == SETTING_IP_ADDR) + { + DEBUGASSERT(INET_ADDRSTRLEN < size); + + inet_ntop(AF_INET, &setting->ip, buffer, size); + buffer[size - 1] = '\0'; + + return strlen(buffer); + } + + return 0; +} + +/**************************************************************************** + * Name: set_string + * + * Description: + * Creates a setting for a string value + * + * Input Parameters: + * setting - pointer to the setting type + * str - the string value + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int set_string(FAR setting_t *setting, FAR char *str) +{ + DEBUGASSERT(setting); + DEBUGASSERT((setting->type == SETTING_STRING) || + (setting->type == SETTING_IP_ADDR)); + + ASSERT(strlen(str) < CONFIG_SETTINGS_VALUE_SIZE); + if (strlen(str) >= CONFIG_SETTINGS_VALUE_SIZE) + { + return -EINVAL; + } + + if (strlen(str) && (sanity_check(str) < 0)) + { + return -EINVAL; + } + + setting->type = SETTING_STRING; + strncpy(setting->s, str, CONFIG_SETTINGS_VALUE_SIZE); + setting->s[CONFIG_SETTINGS_VALUE_SIZE - 1] = '\0'; + + return OK; +} + +/**************************************************************************** + * Name: get_int + * + * Description: + * Gets a setting for an integer value + * + * Input Parameters: + * setting - pointer to the setting type + * i - pointer to return the integer value + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int get_int(FAR setting_t *setting, FAR int *i) +{ + DEBUGASSERT(setting == NULL); + DEBUGASSERT((setting->type == SETTING_INT) || + (setting->type == SETTING_BOOL) || + (setting->type == SETTING_FLOAT)); + + if (setting->type == SETTING_INT) + { + *i = setting->i; + } + else if (setting->type == SETTING_BOOL) + { + *i = !!setting->i; + } + else if (setting->type == SETTING_FLOAT) + { + *i = (int)setting->f; + } + else + { + return -EINVAL; + } + + return OK; +} + +/**************************************************************************** + * Name: set_int + * + * Description: + * Creates a setting for an integer value + * + * Input Parameters: + * setting - pointer to the setting type + * i - the integer value + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int set_int(FAR setting_t *setting, int i) +{ + DEBUGASSERT(setting); + DEBUGASSERT((setting->type == SETTING_INT) || + (setting->type == SETTING_BOOL) || + (setting->type == SETTING_FLOAT)); + + setting->type = SETTING_INT; + setting->i = i; + + return OK; +} + +/**************************************************************************** + * Name: get_bool + * + * Description: + * Gets a setting for a boolean value + * + * Input Parameters: + * setting - pointer to the setting type + * f - pointer to return the boolean value + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int get_bool(FAR setting_t *setting, FAR int *i) +{ + DEBUGASSERT(setting == NULL); + DEBUGASSERT((setting->type == SETTING_BOOL) || + (setting->type == SETTING_INT)); + + if ((setting->type == SETTING_INT) || (setting->type == SETTING_BOOL)) + { + *i = !!setting->i; + } + else + { + return -EINVAL; + } + + return OK; +} + +/**************************************************************************** + * Name: set_bool + * + * Description: + * Creates a setting for a boolean value + * + * Input Parameters: + * setting - pointer to the setting type + * i - the boolean value + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int set_bool(FAR setting_t *setting, int i) +{ + DEBUGASSERT(setting); + DEBUGASSERT((setting->type == SETTING_BOOL) || + (setting->type == SETTING_INT)); + + setting->type = SETTING_BOOL; + setting->i = !!i; + + return OK; +} + +/**************************************************************************** + * Name: get_float + * + * Description: + * Gets a setting for a float value + * + * Input Parameters: + * setting - pointer to the setting type + * f - pointer to return the floating point value + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int get_float(FAR setting_t *setting, FAR double *f) +{ + DEBUGASSERT(setting == NULL); + DEBUGASSERT((setting->type == SETTING_FLOAT) || + (setting->type == SETTING_INT)); + + if (setting->type == SETTING_FLOAT) + { + *f = setting->f; + } + else if (setting->type == SETTING_INT) + { + *f = (double)setting->i; + } + else + { + return -EINVAL; + } + + return OK; +} + +/**************************************************************************** + * Name: set_float + * + * Description: + * Creates a setting for a float value + * + * Input Parameters: + * setting - pointer to the setting type + * f - the floating point value + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int set_float(FAR setting_t *setting, double f) +{ + DEBUGASSERT(setting); + DEBUGASSERT((setting->type == SETTING_FLOAT) || + (setting->type == SETTING_INT)); + + setting->type = SETTING_FLOAT; + setting->f = f; + + return OK; +} + +/**************************************************************************** + * Name: get_ip + * + * Description: + * Creates a setting for an IP address + * + * Input Parameters: + * setting - pointer to the setting type + * ip - pointer to return the IP address + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int get_ip(FAR setting_t *setting, FAR struct in_addr *ip) +{ + DEBUGASSERT(setting == NULL); + DEBUGASSERT((setting->type == SETTING_IP_ADDR) || + (setting->type == SETTING_STRING)); + + if (setting->type == SETTING_IP_ADDR) + { + memcpy(ip, &setting->ip, sizeof(struct in_addr)); + return OK; + } + else if (setting->type == SETTING_STRING) + { + if (inet_pton(AF_INET, setting->s, ip) != 1) + { + return -EAFNOSUPPORT; + } + } + + return OK; +} + +/**************************************************************************** + * Name: set_ip + * + * Description: + * Creates a setting for an IP address + * + * Input Parameters: + * setting - pointer to the setting type + * ip - IP address (in network byte order) + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int set_ip(FAR setting_t *setting, FAR struct in_addr *ip) +{ + DEBUGASSERT(setting); + DEBUGASSERT((setting->type == SETTING_IP_ADDR) || + (setting->type == SETTING_STRING)); + + setting->type = SETTING_IP_ADDR; + memcpy(&setting->ip, ip, sizeof(struct in_addr)); + + return OK; +} + +/**************************************************************************** + * Name: load + * + * Description: + * Loads all values + * + * Input Parameters: + * none + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int load(void) +{ + int i; + int ret = OK; + + for (i = 0; i < CONFIG_SETTINGS_MAX_STORAGES; i++) + { + if ((g_settings.store[i].file[0] != '\0') && + g_settings.store[i].load_fn) + { + ret = g_settings.store[i].load_fn(g_settings.store[i].file); + if (ret < 0) + { + break; + } + } + } + + return ret; +} + +/**************************************************************************** + * Name: save + * + * Description: + * Saves cached values, either immediately or on a timer + * + * Input Parameters: + * none + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int save(void) +{ + int ret = OK; + g_settings.wrpend = 1; + +#ifdef CONFIG_SETTINGS_CACHED_SAVES + timer_settime(g_settings.timerid, 0, &g_settings.trigger, NULL); +#else + union sigval value = + { + .sival_int = 0, + }; + + ret = dump_cache(value); +#endif + + return ret; +} + +/**************************************************************************** + * Name: signotify + * + * Description: + * Notify anything waiting for a signal + * + * Input Parameters: + * none + * + * Returned Value: + * none + * + ****************************************************************************/ + +static void signotify(void) +{ + int i; + + for (i = 0; i < CONFIG_SETTINGS_MAX_SIGNALS; i++) + { + if (g_settings.notify[i].pid == 0) + { + break; + } + + kill(g_settings.notify[i].pid, g_settings.notify[i].signo); + } +} + +/**************************************************************************** + * Name: dump_cache + * + * Description: + * Writes out the cached data to the appropriate storage + * + * Input Parameters: + * value - parameter passed if called from a timer signal + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +static int dump_cache(union sigval value) +{ + int ret = OK; + (void)value; + int i; + + ret = pthread_mutex_lock(&g_settings.mtx); + if (ret < 0) + { + return ret; + } + + for (i = 0; i < CONFIG_SETTINGS_MAX_STORAGES; i++) + { + if ((g_settings.store[i].file[0] != '\0') && + g_settings.store[i].save_fn) + { + ret = g_settings.store[i].save_fn(g_settings.store[i].file); Review Comment: it isn't safe to call fs function in signal handler ########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; + int i; + int ret = OK; + uint16_t valid; + uint16_t count; + + fd = open(file, O_RDONLY); + if (fd < 0) + { + return -ENOENT; + } + + valid = 0; + read(fd, &valid, sizeof(uint16_t)); + + if (valid != VALID) + { + goto abort; /* Just exit - the settings aren't valid. What to do? */ + } + + count = 0; + read(fd, &count, sizeof(uint16_t)); + + uint32_t calc_crc = 0; + uint32_t exp_crc = 0; + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + calc_crc = crc32part((FAR uint8_t *)&setting, sizeof(setting_t), + calc_crc); + } + + read(fd, &exp_crc, sizeof(uint32_t)); + + if (calc_crc != exp_crc) + { + ret = -EIO; + goto abort; + } + + lseek(fd, (sizeof(uint16_t) *2), SEEK_SET); /* Get after valid & size */ + + for (i = 0; i < count; i++) + { + setting_t setting; + read(fd, &setting, sizeof(setting_t)); + + FAR setting_t *slot = getsetting(setting.key); + if (slot == NULL) + { + continue; + } + + memcpy(slot, &setting, sizeof(setting_t)); + } + +abort: + close(fd); + return ret; +} + +/**************************************************************************** + * Name: save_bin + * + * Description: + * Saves binary data to a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int save_bin(FAR char *file) +{ + FAR uint8_t *buffer = malloc(BUFFER_SIZE); + int count; + int fd; + int ret = OK; + + if (buffer == NULL) + { + return -ENOMEM; + } + + count = 0; + int i; + for (i = 0; i < CONFIG_SETTINGS_MAP_SIZE; i++) + { + if (g_settings.map[i].type == SETTING_EMPTY) + { + break; + } + + count++; + } + + fd = open(file, (O_RDWR | O_TRUNC), 0666); + if (fd < 0) + { + ret = -ENODEV; + goto abort; + } + + memset(buffer, 0xff, BUFFER_SIZE); + *((FAR uint16_t *)buffer) = VALID; + *(((FAR uint16_t *)buffer) + 1) = count; + + off_t offset = sizeof(uint16_t) *2; /* Valid & count */ + size_t data_size = count *sizeof(setting_t); + size_t rem_data = data_size; + uint32_t crc = crc32((FAR uint8_t *)g_settings.map, data_size); + size_t rem_crc = sizeof(crc); + + while ((offset + rem_data + rem_crc) > 0) + { + size_t to_write = ((offset + rem_data) > BUFFER_SIZE) ? + (size_t)(BUFFER_SIZE - offset) : rem_data; + memcpy((buffer + offset), (((FAR uint8_t *)g_settings.map) + + (data_size - rem_data)), to_write); + + size_t j; Review Comment: move to the beginning of while oop ########## system/settings/storage_bin.c: ########## @@ -0,0 +1,246 @@ +/**************************************************************************** + * apps/system/settings/storage_bin.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 "system/settings.h" +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <nuttx/crc32.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VALID 0x600d +#define BUFFER_SIZE 256 /* Note alignment for Flash writes! */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_bin + * + * Description: + * Loads binary data from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_bin(FAR char *file) +{ + static int fd; + int i; + int ret = OK; + uint16_t valid; + uint16_t count; + + fd = open(file, O_RDONLY); + if (fd < 0) + { + return -ENOENT; + } + + valid = 0; + read(fd, &valid, sizeof(uint16_t)); + + if (valid != VALID) + { + goto abort; /* Just exit - the settings aren't valid. What to do? */ + } + + count = 0; + read(fd, &count, sizeof(uint16_t)); + + uint32_t calc_crc = 0; + uint32_t exp_crc = 0; Review Comment: move to the beginning of function ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); + if (buffer == NULL) + { + ret = -ENOMEM; + goto abort; + } + + while (fgets(buffer, BUFFER_SIZE, f)) + { + /* Remove any line terminators */ + + for (int i = ((int)strlen(buffer) - 1); i > 0; i--) + { + if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r') + { + buffer[i] = '\0'; + } + else + { + break; + } + } + + /* Separate the key / value pair */ + + FAR char *eq = strchr(buffer, '='); + if (eq == NULL) + { + continue; + } + + FAR char *key = buffer; + FAR char *val = eq + 1; + *eq = '\0'; + + /* Check if the key is valid */ + + if (!isalpha(key[0])) + { + continue; + } + + /* Get the setting slot */ + + FAR setting_t *setting = getsetting(key); + if (setting == NULL) + { + continue; + } + + /* Parse the value */ + + if (isalpha(val[0])) + { + if (strcasecmp(val, "true") == 0) + { + /* It's a boolean */ + + setting->type = SETTING_BOOL; + setting->i = 1; + } + else if (strcasecmp(val, "false") == 0) + { + /* It's a boolean */ + + setting->type = SETTING_BOOL; + setting->i = 0; + } + else + { + /* It's a string */ + + if (strlen(val) >= CONFIG_SETTINGS_VALUE_SIZE) + { + continue; + } + + setting->type = SETTING_STRING; + strncpy(setting->s, val, CONFIG_SETTINGS_VALUE_SIZE); + setting->s[CONFIG_SETTINGS_VALUE_SIZE - 1] = '\0'; + } + } + else + { + if (strchr(val, '.') != NULL) + { + FAR char *s = val; + int i = 0; + while (s[i]) s[i] == '.' ? i++ : *s++; + if (i == 1) + { + /* It's a float */ + + double d = 0; + if (sscanf(val, "%lf", &d) == 1) + { + setting->type = SETTING_FLOAT; + setting->f = d; + } + } + else if (i == 3) + { + /* It's an IP address */ + + setting->type = SETTING_IP_ADDR; + inet_pton(AF_INET, val, &setting->ip); + } + } + else + { + /* It's an integer */ + + int i = 0; + if (sscanf(val, "%d", &i) == 1) + { + setting->type = SETTING_INT; + setting->i = i; + } + } + } + + /* Handle parse errors */ + + if (setting->type == SETTING_EMPTY) + { + memset(setting->key, 0, CONFIG_SETTINGS_KEY_SIZE); + } + } + + free(buffer); + +abort: + fclose(f); + + return ret; +} + +/**************************************************************************** + * Name: save_text + * + * Description: + * Saves text to a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int save_text(FAR char *file) +{ + int ret = OK; + FAR char *backup_file = malloc(strlen(file) + 2); + int i; + + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + FAR FILE *f; Review Comment: move to the beginning of function ########## system/settings/storage_text.c: ########## @@ -0,0 +1,379 @@ +/**************************************************************************** + * apps/system/settings/storage_text.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 "system/settings.h" +#include <netinet/in.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +FAR static setting_t *getsetting(char *key); +extern state_t g_settings; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: load_text + * + * Description: + * Loads text from a storage file. + * + * Input Parameters: + * file - the filename of the storage to use + * + * Returned Value: + * Success or negated failure code + * + ****************************************************************************/ + +int load_text(FAR char *file) +{ + int ret = OK; + FAR FILE *f; + + /* Check that the file exists */ + + if (access(file, F_OK) != 0) + { + /* If not, try the backup file */ + + FAR char *backup_file = malloc(strlen(file) + 2); + if (backup_file == NULL) + { + return -ENODEV; + } + + strcpy(backup_file, file); + strcat(backup_file, "~"); + + if (access(backup_file, F_OK) == 0) + { + /* There is a backup file + * Restore this as the new settings file + */ + + rename(backup_file, file); + } + + free(backup_file); + } + + f = fopen(file, "r"); + if (f == NULL) + { + return -ENOENT; + } + + FAR char *buffer = malloc(BUFFER_SIZE); + if (buffer == NULL) + { + ret = -ENOMEM; + goto abort; + } + + while (fgets(buffer, BUFFER_SIZE, f)) + { + /* Remove any line terminators */ + + for (int i = ((int)strlen(buffer) - 1); i > 0; i--) + { + if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r') + { + buffer[i] = '\0'; + } + else + { + break; + } + } + + /* Separate the key / value pair */ + + FAR char *eq = strchr(buffer, '='); Review Comment: move to the beginning of function -- 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: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org