fjpanag commented on code in PR #2228: URL: https://github.com/apache/nuttx-apps/pull/2228#discussion_r1426654083
########## system/settings/settings.c: ########## @@ -0,0 +1,1717 @@ +/**************************************************************************** + * 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 + ****************************************************************************/ + +#if defined(CONFIG_SYSTEM_SETTINGS_CACHED_SAVES) +# define TIMER_SIGNAL SIGRTMIN +#endif + +#ifndef CONFIG_SYSTEM_SETTINGS_SIGNO +# define CONFIG_SYSTEM_SETTINGS_SIGNO 32 +#endif + +#ifndef CONFIG_SYSTEM_SETTINGS_CACHE_TIME_MS +# define CONFIG_SYSTEM_SETTINGS_CACHE_TIME_MS 100 +#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_byte(FAR setting_t *setting, FAR int *i); +static int set_byte(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); +void dump_cache(union sigval ptr); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct +{ + pthread_mutex_t mtx; + uint32_t hash; + bool wrpend; + bool initialized; + storage_t store[CONFIG_SYSTEM_SETTINGS_MAX_STORAGES]; + struct notify_s notify[CONFIG_SYSTEM_SETTINGS_MAX_SIGNALS]; +#if defined(CONFIG_SYSTEM_SETTINGS_CACHED_SAVES) + struct sigevent sev; + struct itimerspec trigger; + timer_t timerid; +#endif +} g_settings; + +setting_t map[CONFIG_SYSTEM_SETTINGS_MAP_SIZE]; + +/**************************************************************************** + * 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 *)map, sizeof(map)); +} + +/**************************************************************************** + * Name: get_setting + * + * Description: + * Gets a setting for a string value + * + * Input Parameters: + * key - key of the required setting + * setting - pointer to pointer for the setting + * + * Returned Value: + * The value of the setting for the given key + * + ****************************************************************************/ + +static int get_setting(FAR char *key, FAR setting_t **setting) +{ + DEBUGASSERT(*setting == NULL); Review Comment: Maybe it has to be: ``` DEBUGASSERT(setting && *setting); ``` Can you please spend some thought on this? -- 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