This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 6e3371f5bb14d9b176d966b0a123b0a42d85bada
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Thu Apr 25 15:38:17 2024 +0200

    tinyusb/msc_fat_view: Add entry for sys/config
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/usb/tinyusb/msc_fat_view/pkg.yml            |   7 ++
 hw/usb/tinyusb/msc_fat_view/src/entry_config.c | 112 +++++++++++++++++++++++++
 hw/usb/tinyusb/msc_fat_view/syscfg.yml         |   5 ++
 3 files changed, 124 insertions(+)

diff --git a/hw/usb/tinyusb/msc_fat_view/pkg.yml 
b/hw/usb/tinyusb/msc_fat_view/pkg.yml
index 51355fdf1..e57fb4ba5 100644
--- a/hw/usb/tinyusb/msc_fat_view/pkg.yml
+++ b/hw/usb/tinyusb/msc_fat_view/pkg.yml
@@ -30,8 +30,12 @@ pkg.deps:
     - "@apache-mynewt-core/kernel/os"
     - "@apache-mynewt-core/hw/usb/tinyusb"
     - "@apache-mynewt-core/mgmt/imgmgr"
+    - "@apache-mynewt-core/util/stream"
     - "@mcuboot/boot/bootutil"
 
+pkg.deps.MSC_FAT_VIEW_CONFIG:
+    - "@apache-mynewt-core/sys/config"
+
 pkg.deps.MSC_FAT_VIEW_COREDUMP_FILES:
     - "@apache-mynewt-core/sys/coredump"
 
@@ -58,6 +62,9 @@ pkg.source_files.MSC_FAT_VIEW_SLOT0_HEX:
 pkg.source_files.MSC_FAT_VIEW_MYNEWT_SHORTCUT:
     - src/entry_mynewt_htm.c
 
+pkg.source_files.MSC_FAT_VIEW_CONFIG:
+    - src/entry_config.c
+
 pkg.link_tables:
     - msc_fat_view_root_entry
 
diff --git a/hw/usb/tinyusb/msc_fat_view/src/entry_config.c 
b/hw/usb/tinyusb/msc_fat_view/src/entry_config.c
new file mode 100644
index 000000000..be7f25a20
--- /dev/null
+++ b/hw/usb/tinyusb/msc_fat_view/src/entry_config.c
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+
+#include <stdint.h>
+#include <stream/stream.h>
+#include <msc_fat_view/msc_fat_view.h>
+#include <modlog/modlog.h>
+#include <hal/hal_flash.h>
+#include <config/config.h>
+
+struct config_export_stream {
+    struct out_stream out_stream;
+    uint8_t *buffer;
+    uint32_t buffer_start_offset;
+    uint16_t buffer_end_offset;
+    uint32_t write_offset;
+};
+
+static int
+config_export_write(struct out_stream *ostream, const uint8_t *buf, uint32_t 
count)
+{
+    struct config_export_stream *str = (struct config_export_stream *)ostream;
+    uint32_t upper_limit = str->write_offset + count;
+    uint32_t lower_limit = str->write_offset;
+    int cnt = count;
+
+    if (lower_limit < str->buffer_end_offset && upper_limit > 
str->buffer_start_offset) {
+        if (lower_limit < str->buffer_start_offset) {
+            cnt -= str->buffer_start_offset - lower_limit;
+            lower_limit = str->buffer_start_offset;
+        }
+        if (upper_limit > str->buffer_end_offset) {
+            cnt -= upper_limit - str->buffer_end_offset;
+        }
+        memcpy(str->buffer + lower_limit - str->buffer_start_offset,
+               buf + (lower_limit - str->write_offset), cnt);
+    }
+    str->write_offset += count;
+
+    return count;
+}
+
+static int
+config_export_flush(struct out_stream *ostream)
+{
+    return 0;
+}
+
+OSTREAM_DEF(config_export);
+
+static struct config_export_stream export_stream = {
+    .out_stream.vft = &config_export_vft,
+};
+
+static void
+config_text_export(char *name, char *val)
+{
+    int name_len = strlen(name);
+    int val_len = 0;
+    if (val) {
+        val_len = strlen(val);
+    }
+    ostream_write(&export_stream.out_stream, (const uint8_t *)name, name_len, 
false);
+    ostream_write(&export_stream.out_stream, (const uint8_t *)" = ", 3, false);
+    if (val) {
+        ostream_write(&export_stream.out_stream, (const uint8_t *)val, 
val_len, false);
+    }
+    ostream_write(&export_stream.out_stream, (const uint8_t *)"\n", 1, false);
+}
+
+static uint32_t
+config_txt_size(const file_entry_t *file_entry)
+{
+    export_stream.buffer = NULL;
+    export_stream.buffer_start_offset = 0;
+    export_stream.buffer_end_offset = 0;
+    export_stream.write_offset = 0;
+
+    conf_export(config_text_export, CONF_EXPORT_SHOW);
+
+    return export_stream.write_offset;
+}
+
+static void
+config_txt_read(const struct file_entry *entry, uint32_t file_sector, uint8_t 
buffer[512])
+{
+    export_stream.buffer = buffer;
+    export_stream.buffer_start_offset = 512 * file_sector;
+    export_stream.buffer_end_offset = export_stream.buffer_start_offset + 512;
+    export_stream.write_offset = 0;
+
+    MSC_FAT_VIEW_LOG_DEBUG("Config.txt read %d\n", file_sector);
+    conf_export(config_text_export, CONF_EXPORT_SHOW);
+}
+
+ROOT_DIR_ENTRY(config_txt, "CONFIG.TXT", FAT_FILE_ENTRY_ATTRIBUTE_READ_ONLY, 
config_txt_size, config_txt_read, NULL, NULL);
diff --git a/hw/usb/tinyusb/msc_fat_view/syscfg.yml 
b/hw/usb/tinyusb/msc_fat_view/syscfg.yml
index b1d0700ba..a3c773d7a 100644
--- a/hw/usb/tinyusb/msc_fat_view/syscfg.yml
+++ b/hw/usb/tinyusb/msc_fat_view/syscfg.yml
@@ -103,6 +103,11 @@ syscfg.defs:
             Normally this if folder (not file) generated by Windows.
             If this file is present Windows will not create folder saving time 
for writes.
         value: 0
+    MSC_FAT_VIEW_CONFIG:
+        description: >
+            If set to 1, root directory will have CONFIG.TXT with content
+            of sys/config.
+        value: 0
     MSC_FAT_VIEW_AUTO_INSERT:
         description: >
             If set to 1, media is reported as present during system init.

Reply via email to