daniel-p-carvalho commented on code in PR #3893:
URL: https://github.com/apache/nuttx-apps/pull/3893#discussion_r4339660868


##########
include/netutils/wifibridge.h:
##########
@@ -125,6 +135,58 @@ int wifibridge_stop(FAR const struct wifibridge_config_s 
*config);
 int wifibridge_status(FAR const struct wifibridge_config_s *config,
                       FAR struct wifibridge_status_s *status);
 
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_PERSIST
+
+/****************************************************************************
+ * Name: wifibridge_save
+ *
+ * Description:
+ *   Keep the SSID, passphrase, channel, security, address and DHCP server
+ *   option of the configuration in the storage
+ *   (CONFIG_NETUTILS_WIFIBRIDGE_STORAGE), to survive a reset.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int wifibridge_save(FAR const struct wifibridge_config_s *config);
+
+/****************************************************************************
+ * Name: wifibridge_load
+ *
+ * Description:
+ *   Read what wifibridge_save() kept and set the matching fields of the
+ *   configuration.  The SSID and the passphrase are copied to the buffers

Review Comment:
   Done, ssid and passphrase are now arrays with the sizes in the prototype.



##########
netutils/wifibridge/wifibridge.c:
##########
@@ -346,3 +409,185 @@ int wifibridge_status(FAR const struct 
wifibridge_config_s *config,
   close(sock);
   return OK;
 }
+
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_PERSIST
+
+/****************************************************************************
+ * Name: wifibridge_record_crc
+ *
+ * Description:
+ *   CRC-32 of the record after the CRC field.
+ *
+ ****************************************************************************/
+
+static uint32_t wifibridge_record_crc(
+                              FAR const struct wifibridge_record_s *record)
+{
+  return crc32((FAR const uint8_t *)&record->version,
+               sizeof(*record) - offsetof(struct wifibridge_record_s,
+                                          version));
+}
+
+/****************************************************************************
+ * Name: wifibridge_record_io
+ *
+ * Description:
+ *   Read or write the record at the start of the storage.
+ *
+ ****************************************************************************/
+
+static int wifibridge_record_io(FAR struct wifibridge_record_s *record,
+                                bool write)
+{
+  ssize_t nbytes;
+  int ret = OK;
+  int fd;
+
+  fd = open(CONFIG_NETUTILS_WIFIBRIDGE_STORAGE,
+            write ? O_RDWR | O_CREAT : O_RDONLY, 0600);

Review Comment:
   Done, the write opens with O_WRONLY | O_CREAT | O_TRUNC. Tested on the 
simulator: a 400-byte file is left with the 116 bytes of the record.



##########
netutils/wifibridge/wifibridge.c:
##########
@@ -346,3 +409,185 @@ int wifibridge_status(FAR const struct 
wifibridge_config_s *config,
   close(sock);
   return OK;
 }
+
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_PERSIST
+
+/****************************************************************************
+ * Name: wifibridge_record_crc
+ *
+ * Description:
+ *   CRC-32 of the record after the CRC field.
+ *
+ ****************************************************************************/
+
+static uint32_t wifibridge_record_crc(
+                              FAR const struct wifibridge_record_s *record)
+{
+  return crc32((FAR const uint8_t *)&record->version,
+               sizeof(*record) - offsetof(struct wifibridge_record_s,
+                                          version));
+}
+
+/****************************************************************************
+ * Name: wifibridge_record_io
+ *
+ * Description:
+ *   Read or write the record at the start of the storage.
+ *
+ ****************************************************************************/
+
+static int wifibridge_record_io(FAR struct wifibridge_record_s *record,
+                                bool write)
+{
+  ssize_t nbytes;
+  int ret = OK;
+  int fd;
+
+  fd = open(CONFIG_NETUTILS_WIFIBRIDGE_STORAGE,
+            write ? O_RDWR | O_CREAT : O_RDONLY, 0600);
+  if (fd < 0)
+    {
+      return -errno;
+    }
+
+  if (write)
+    {
+      nbytes = pwrite(fd, record, sizeof(*record), 0);

Review Comment:
   Done, it is a plain write() now (the offset was always 0). I renamed the 
bool argument of wifibridge_record_io() to store, since it hid write().



##########
netutils/wifibridge/wifibridge.c:
##########
@@ -346,3 +409,185 @@ int wifibridge_status(FAR const struct 
wifibridge_config_s *config,
   close(sock);
   return OK;
 }
+
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_PERSIST
+
+/****************************************************************************
+ * Name: wifibridge_record_crc
+ *
+ * Description:
+ *   CRC-32 of the record after the CRC field.
+ *
+ ****************************************************************************/
+
+static uint32_t wifibridge_record_crc(
+                              FAR const struct wifibridge_record_s *record)
+{
+  return crc32((FAR const uint8_t *)&record->version,
+               sizeof(*record) - offsetof(struct wifibridge_record_s,
+                                          version));
+}
+
+/****************************************************************************
+ * Name: wifibridge_record_io
+ *
+ * Description:
+ *   Read or write the record at the start of the storage.
+ *
+ ****************************************************************************/
+
+static int wifibridge_record_io(FAR struct wifibridge_record_s *record,
+                                bool write)
+{
+  ssize_t nbytes;
+  int ret = OK;
+  int fd;
+
+  fd = open(CONFIG_NETUTILS_WIFIBRIDGE_STORAGE,
+            write ? O_RDWR | O_CREAT : O_RDONLY, 0600);
+  if (fd < 0)
+    {
+      return -errno;
+    }
+
+  if (write)
+    {
+      nbytes = pwrite(fd, record, sizeof(*record), 0);
+      if (nbytes == sizeof(*record))
+        {
+          ret = fsync(fd);
+          if (ret < 0)
+            {
+              ret = -errno;
+            }
+        }
+      else
+        {
+          ret = nbytes < 0 ? -errno : -EIO;
+        }
+    }
+  else
+    {
+      nbytes = pread(fd, record, sizeof(*record), 0);

Review Comment:
   Done, a plain read() now.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to