xiaoxiang781216 commented on code in PR #3893:
URL: https://github.com/apache/nuttx-apps/pull/3893#discussion_r4350436027
##########
netutils/wifibridge/wifibridge.c:
##########
@@ -346,3 +408,181 @@ 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 store)
+{
+ ssize_t nbytes;
+ int ret = OK;
+ int fd;
+
+ fd = open(CONFIG_NETUTILS_WIFIBRIDGE_STORAGE,
+ store ? O_WRONLY | O_CREAT | O_TRUNC : O_RDONLY, 0600);
+ if (fd < 0)
+ {
+ return -errno;
+ }
+
+ if (store)
+ {
+ nbytes = write(fd, record, sizeof(*record));
+ if (nbytes == sizeof(*record))
+ {
+ ret = fsync(fd);
+ if (ret < 0)
+ {
+ ret = -errno;
+ }
+ }
+ else
+ {
+ ret = nbytes < 0 ? -errno : -EIO;
+ }
+ }
+ else
+ {
+ nbytes = read(fd, record, sizeof(*record));
+ if (nbytes != sizeof(*record))
+ {
+ ret = nbytes < 0 ? -errno : -ENOENT;
+ }
+ }
+
+ close(fd);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: wifibridge_save
+ ****************************************************************************/
+
+int wifibridge_save(FAR const struct wifibridge_config_s *config)
+{
+ struct wifibridge_record_s record;
+ int ret;
+
+ if (config == NULL)
+ {
+ return -EINVAL;
+ }
+
+ ret = wifibridge_validate(config);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ memset(&record, 0, sizeof(record));
+ record.magic = WIFIBRIDGE_RECORD_MAGIC;
+ record.version = WIFIBRIDGE_SAVED_VERSION;
+ record.channel = config->channel;
+ record.security = config->security;
+ record.dhcpd = config->dhcpd;
+ record.ipaddr = config->ipaddr;
+ strlcpy(record.ssid, config->ssid, sizeof(record.ssid));
+ strlcpy(record.passphrase, config->passphrase,
+ sizeof(record.passphrase));
+ record.crc = wifibridge_record_crc(&record);
+
+ return wifibridge_record_io(&record, true);
+}
+
+/****************************************************************************
+ * Name: wifibridge_load
+ ****************************************************************************/
+
+int wifibridge_load(FAR struct wifibridge_config_s *config)
+{
+ struct wifibridge_config_s tmp;
+ struct wifibridge_record_s record;
+ int ret;
+
+ if (config == NULL)
+ {
+ return -EINVAL;
+ }
+
+ ret = wifibridge_record_io(&record, false);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* A blank or cleared storage has no record */
+
+ if (record.magic != WIFIBRIDGE_RECORD_MAGIC)
+ {
+ return -ENOENT;
+ }
+
+ /* Do not trust the rest: it can be damaged or come from another version */
+
+ if (record.crc != wifibridge_record_crc(&record) ||
+ record.version != WIFIBRIDGE_SAVED_VERSION)
+ {
+ return -EINVAL;
+ }
+
+ tmp = *config;
Review Comment:
why need tmp
##########
netutils/wifibridge/wifibridge.c:
##########
@@ -346,3 +408,181 @@ 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 store)
+{
+ ssize_t nbytes;
+ int ret = OK;
+ int fd;
+
+ fd = open(CONFIG_NETUTILS_WIFIBRIDGE_STORAGE,
+ store ? O_WRONLY | O_CREAT | O_TRUNC : O_RDONLY, 0600);
+ if (fd < 0)
+ {
+ return -errno;
+ }
+
+ if (store)
+ {
+ nbytes = write(fd, record, sizeof(*record));
+ if (nbytes == sizeof(*record))
+ {
+ ret = fsync(fd);
+ if (ret < 0)
+ {
+ ret = -errno;
+ }
+ }
+ else
+ {
+ ret = nbytes < 0 ? -errno : -EIO;
+ }
+ }
+ else
+ {
+ nbytes = read(fd, record, sizeof(*record));
+ if (nbytes != sizeof(*record))
+ {
+ ret = nbytes < 0 ? -errno : -ENOENT;
+ }
+ }
+
+ close(fd);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: wifibridge_save
+ ****************************************************************************/
+
+int wifibridge_save(FAR const struct wifibridge_config_s *config)
+{
+ struct wifibridge_record_s record;
+ int ret;
+
+ if (config == NULL)
+ {
+ return -EINVAL;
+ }
+
+ ret = wifibridge_validate(config);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ memset(&record, 0, sizeof(record));
+ record.magic = WIFIBRIDGE_RECORD_MAGIC;
Review Comment:
align =
--
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]