fjpanag commented on code in PR #2228:
URL: https://github.com/apache/nuttx-apps/pull/2228#discussion_r1426637311


##########
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);
+
+  int i;
+  int ret = OK;
+
+  for (i = 0; i < CONFIG_SYSTEM_SETTINGS_MAP_SIZE; i++)
+    {
+      if (map[i].type == SETTING_EMPTY)
+        {
+          ret = -ENOENT;
+          goto exit;
+        }
+
+      if (strcmp(map[i].key, key) == 0)
+        {
+          *setting = &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);
+  DEBUGASSERT((setting->type == SETTING_STRING) ||
+              (setting->type == SETTING_IP_ADDR));
+
+  if (setting->type == SETTING_STRING)
+    {
+      FAR const char *s = setting->val.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->val.ip, buffer, size);
+      buffer[size - 1] = '\0';
+
+      return strlen(buffer);
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: set_string
+ *
+ * Description:
+ *    Creates a setting for a string valuesetting->type != set_
+ *
+ * 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);
+
+  if ((setting->type != SETTING_STRING) &&
+              (setting->type != SETTING_IP_ADDR))
+    {
+      return -EACCES;
+    }
+
+  ASSERT(strlen(str) < CONFIG_SYSTEM_SETTINGS_VALUE_SIZE);
+  if (strlen(str) >= CONFIG_SYSTEM_SETTINGS_VALUE_SIZE)
+    {
+      return -EINVAL;
+    }
+
+  if (strlen(str) && (sanity_check(str) < 0))
+    {
+      return -EINVAL;
+    }
+
+  setting->type = SETTING_STRING;
+  strncpy(setting->val.s, str, CONFIG_SYSTEM_SETTINGS_VALUE_SIZE);
+  setting->val.s[CONFIG_SYSTEM_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);
+  DEBUGASSERT((setting->type == SETTING_INT)  ||
+              (setting->type == SETTING_BYTE) ||
+              (setting->type == SETTING_BOOL) ||
+              (setting->type == SETTING_FLOAT));
+
+  if (setting->type == SETTING_INT)
+    {
+      *i = setting->val.i;
+    }
+  else if (setting->type == SETTING_BYTE)
+    {
+      *i = (int)setting->val.i;
+    }
+  else if (setting->type == SETTING_BOOL)
+    {
+      *i = !!setting->val.i;
+    }
+  else if (setting->type == SETTING_FLOAT)
+    {
+      *i = (int)setting->val.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);
+  if ((setting->type == SETTING_INT)  &&
+      (setting->type == SETTING_BYTE) &&
+      (setting->type == SETTING_BOOL) &&
+      (setting->type == SETTING_FLOAT))
+    {
+      return -EACCES;
+    }
+
+  setting->type = SETTING_INT;
+  setting->val.i = i;
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: get_byte
+ *
+ * Description:
+ *    Gets a setting for a byte value
+ *
+ * Input Parameters:
+ *    setting        - pointer to the setting type
+ *    i              - pointer to return the byte value
+ * Returned Value:
+ *   Success or negated failure code
+ *
+ ****************************************************************************/
+
+static int get_byte(FAR setting_t *setting, FAR int *i)
+{
+  DEBUGASSERT(setting);
+  DEBUGASSERT((setting->type == SETTING_INT)  ||
+              (setting->type == SETTING_BYTE) ||
+              (setting->type == SETTING_BOOL) ||
+              (setting->type == SETTING_FLOAT));
+
+  if (setting->type == SETTING_INT)
+    {
+      *i = (uint8_t)setting->val.i;
+    }
+  else if (setting->type == SETTING_BYTE)
+    {
+      *i = (uint8_t)setting->val.i;
+    }
+  else if (setting->type == SETTING_BOOL)
+    {
+      *i = !!setting->val.i;
+    }
+  else if (setting->type == SETTING_FLOAT)
+    {
+      *i = (uint8_t)setting->val.f;
+    }
+  else
+    {
+      return -EINVAL;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: set_byte
+ *
+ * Description:
+ *    Creates a setting for a byte value
+ *
+ * Input Parameters:
+ *    setting        - pointer to the setting type
+ *    i              - the byte value
+ *
+ * Returned Value:
+ *   Success or negated failure code
+ *
+ ****************************************************************************/
+
+static int set_byte(FAR setting_t *setting, int i)
+{
+  DEBUGASSERT(setting);
+  if ((setting->type == SETTING_INT)  &&
+      (setting->type == SETTING_BYTE) &&
+      (setting->type == SETTING_BOOL) &&
+      (setting->type == SETTING_FLOAT))
+    {
+      return -EACCES;
+    }
+
+  setting->type = SETTING_BYTE;
+  setting->val.i = (int)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);
+  DEBUGASSERT((setting->type == SETTING_BOOL) ||
+              (setting->type == SETTING_BYTE) ||
+              (setting->type == SETTING_INT));
+
+  if ((setting->type == SETTING_INT) || (setting->type == SETTING_BYTE) ||
+      (setting->type == SETTING_BOOL))
+    {
+      *i = !!setting->val.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);
+  if ((setting->type == SETTING_BOOL) &&
+      (setting->type == SETTING_BYTE) &&
+      (setting->type == SETTING_INT))
+    {
+      return -EACCES;
+    }
+
+  setting->type = SETTING_BOOL;
+  setting->val.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);
+  DEBUGASSERT((setting->type == SETTING_FLOAT) ||
+              (setting->type == SETTING_BYTE)  ||
+              (setting->type == SETTING_INT));
+
+  if (setting->type == SETTING_FLOAT)
+    {
+      *f = setting->val.f;
+    }
+  else if (setting->type == SETTING_INT)
+    {
+      *f = (double)setting->val.i;
+    }
+  else if (setting->type == SETTING_BYTE)
+    {
+      *f = (double)setting->val.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);
+  if ((setting->type == SETTING_FLOAT) &&
+      (setting->type == SETTING_BYTE) &&
+      (setting->type == SETTING_INT))
+    {
+      return -EACCES;
+    }
+
+  setting->type = SETTING_FLOAT;
+  setting->val.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)
+{
+  int ret = OK;
+  DEBUGASSERT(setting);
+  DEBUGASSERT((setting->type == SETTING_IP_ADDR) ||
+              (setting->type == SETTING_STRING));
+
+  if (setting->type == SETTING_IP_ADDR)
+    {
+      memcpy(ip, &setting->val.ip, sizeof(struct in_addr));
+    }
+  else if (setting->type == SETTING_STRING)
+    {
+      ret = inet_pton(AF_INET, setting->val.s, ip);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * 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);
+  if ((setting->type == SETTING_IP_ADDR) &&
+      (setting->type == SETTING_STRING))
+    {
+      return -EACCES;
+    }
+
+  setting->type = SETTING_IP_ADDR;
+  memcpy(&setting->val.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_SYSTEM_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 = true;
+
+#ifdef CONFIG_SYSTEM_SETTINGS_CACHED_SAVES
+  ret = timer_settime(g_settings.timerid, 0, &g_settings.trigger, NULL);
+#else
+  union sigval value =
+  {
+    .sival_ptr = &g_settings.wrpend,
+  };
+
+  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_SYSTEM_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:
+ *   None
+ *
+ ****************************************************************************/
+
+void dump_cache(union sigval ptr)
+{
+  int ret = OK;
+  FAR bool *wrpend = (bool *)ptr.sival_ptr;
+
+  int i;
+
+  ret = pthread_mutex_lock(&g_settings.mtx);
+  if (ret < 0)
+    {
+      DEBUGASSERT(0);
+    }
+
+  for (i = 0; i < CONFIG_SYSTEM_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);
+          if (ret < 0)
+            {
+              break;
+            }
+        }
+    }
+
+  *wrpend = false;
+
+  pthread_mutex_unlock(&g_settings.mtx);
+}
+
+/****************************************************************************
+ * Name: sanity_check
+ *
+ * Description:
+ *    Checks that the string does not contain "illegal" characters
+ *
+ * Input Parameters:
+ *    str              - the string to check
+ *
+ * Returned Value:
+ *   Success or negated failure code
+ *
+ ****************************************************************************/
+
+static int sanity_check(FAR char *str)
+{
+#ifdef CONFIG_DEBUG_ASSERTIONS
+  DEBUGASSERT(strchr(str, '=')  == NULL);
+  DEBUGASSERT(strchr(str, ';')  == NULL);
+  DEBUGASSERT(strchr(str, '\n') == NULL);
+  DEBUGASSERT(strchr(str, '\r') == NULL);
+#else
+  if ((strchr(str, '=')  != NULL) || (strchr(str, ';')  != NULL) ||
+      (strchr(str, '\n') != NULL) || (strchr(str, '\r') != NULL))
+    {
+      return -EINVAL;
+    }
+
+#endif
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: settings_init
+ *
+ * Description:
+ *   Initializes the settings storage.
+ *
+ * Input Parameters:  none
+ *
+ * Returned Value:    none
+ *
+ ****************************************************************************/
+
+int settings_init(void)

Review Comment:
   It's not just about the return type.
   I believe it will be good to retain the convention that init never fails.
   
   Error handling before the system even begins can become stupidly complicated.



-- 
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

Reply via email to