From a997ca63a68392cb806e623f8909162c2e17eb0c Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 21:37:50 +0200 Subject: [PATCH 01/12] Esp32: compatibility with IDF 5.1 --- src/Espressif.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Espressif.cpp b/src/Espressif.cpp index e73a77ce..8cdbcfe7 100644 --- a/src/Espressif.cpp +++ b/src/Espressif.cpp @@ -5,6 +5,7 @@ #include "esp_heap_caps.h" #include "esp_task_wdt.h" +#include "esp_mac.h" #include "Espressif.hpp" #include "Logging.hpp" @@ -37,10 +38,10 @@ namespace fabomatic::esp32 auto retVal = esp_task_wdt_init(secs, true); ESP_LOGI(TAG, "taskEspWatchdog - initialized %u seconds", secs); #else - auto msecs = static_cast(msecs.count()); - esp_task_wdt_config_t conf{.timeout_ms = msecs, .idle_core_mask = 1, .trigger_panic = true}; + auto ui_msecs = static_cast(msecs.count()); + esp_task_wdt_config_t conf{.timeout_ms = ui_msecs, .idle_core_mask = 1, .trigger_panic = true}; auto retVal = esp_task_wdt_init(&conf); - ESP_LOGI(TAG, "taskEspWatchdog - initialized %u milliseconds", msecs); + ESP_LOGI(TAG, "taskEspWatchdog - initialized %lu milliseconds", ui_msecs); #endif if (retVal != ESP_OK) From d853eb90f4f489780ee859d4e9ad5fb4ff5e460b Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 21:50:47 +0200 Subject: [PATCH 02/12] Reduced warnings/errors --- src/LCDWrapper.tpp | 2 +- src/SavedConfig.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/LCDWrapper.tpp b/src/LCDWrapper.tpp index d34f6572..2f62e8fd 100644 --- a/src/LCDWrapper.tpp +++ b/src/LCDWrapper.tpp @@ -207,7 +207,7 @@ namespace fabomatic { if (text.length() >= conf::lcd::COLS) { - ESP_LOGE(TAG, "LCDWrapper::setRow: text too long : %s\r\n", text.data()); + ESP_LOGW(TAG, "LCDWrapper::setRow: text too long : %s\r\n", text.data()); } if (row < conf::lcd::ROWS) diff --git a/src/SavedConfig.cpp b/src/SavedConfig.cpp index 73ef2172..c6902641 100644 --- a/src/SavedConfig.cpp +++ b/src/SavedConfig.cpp @@ -125,7 +125,6 @@ namespace fabomatic if (result != DeserializationError::Ok) { ESP_LOGE(TAG, "fromJsonDocument() : deserializeJson failed with code %s", result.c_str()); - ESP_LOGE(TAG, "fromJsonDocument() : %s", json_text.c_str()); return std::nullopt; } From d2a479db8d6445d8a6a1b615ac3cf0069e3079d6 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 21:50:58 +0200 Subject: [PATCH 03/12] Added colored output --- platformio.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/platformio.ini b/platformio.ini index 5a6ed902..0fc93f0e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -42,6 +42,7 @@ build_flags = -std=gnu++2a -Os -I conf -Wno-deprecated-declarations + -D CONFIG_LOG_COLORS build_src_flags = -Wformat=2 -Wformat-truncation -Wall From b585450398d62ae3a5509330ca2b1290bf3e73b7 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:18:01 +0200 Subject: [PATCH 04/12] Updated workflows to run if needed --- .github/workflows/build.yml | 11 ++++------- .github/workflows/sizes.yml | 4 +++- .github/workflows/tests.yml | 9 ++++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1137d1f7..69d603b9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,13 +2,10 @@ name: Build firmware images on: workflow_dispatch: - push: - paths: - - 'src/**' - - 'conf/**' - - 'include/**' - - 'platformio.ini' - - '.github/workflows/build.yml' + workflow_run: + workflows: [Test suite based on Wokwi CLI] + types: + - completed concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/.github/workflows/sizes.yml b/.github/workflows/sizes.yml index 35f7f308..afde5905 100644 --- a/.github/workflows/sizes.yml +++ b/.github/workflows/sizes.yml @@ -2,7 +2,9 @@ name: Compare firmware sizes on: workflow_dispatch: - pull_request: + workflow_run: + workflows: [Build firmware images] + types: [completed] concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index aa293618..ec85978d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,7 +3,14 @@ name: Test suite based on Wokwi CLI on: workflow_dispatch: pull_request: - + push: + paths: + - 'src/**' + - 'conf/**' + - 'include/**' + - 'test/**' + - 'platformio.ini' + - '.github/workflows/tests.yml' concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true From 081c43d2be61382560076fa6f9f71d432a856b97 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:11:53 +0200 Subject: [PATCH 05/12] Update test_mqtt.cpp --- test/test_mqtt/test_mqtt.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_mqtt/test_mqtt.cpp b/test/test_mqtt/test_mqtt.cpp index 7068aca3..5a4a55b7 100644 --- a/test/test_mqtt/test_mqtt.cpp +++ b/test/test_mqtt/test_mqtt.cpp @@ -271,7 +271,7 @@ namespace fabomatic::tests /// @brief Keep the ESP32 HW watchdog alive. /// If code gets stuck this will cause an automatic reset. - void taskEspWatchdog() + void test_taskEspWatchdog() { static auto initialized = false; @@ -280,7 +280,7 @@ namespace fabomatic::tests if (!initialized) { initialized = esp32::setupWatchdog(conf::tasks::WATCHDOG_TIMEOUT); - TEST_ASSERT_TRUE_MESSAGE(initialized, "Watchdog initialization works") + TEST_ASSERT_TRUE_MESSAGE(initialized, "Watchdog initialization works"); } if (initialized) { From c2cd8065c8128dab92f2f244cceac983225f815e Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:35:17 +0200 Subject: [PATCH 06/12] Esp32: tests --- test/test_tasks/test_tasks.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_tasks/test_tasks.cpp b/test/test_tasks/test_tasks.cpp index f763088b..e01464f7 100644 --- a/test/test_tasks/test_tasks.cpp +++ b/test/test_tasks/test_tasks.cpp @@ -6,6 +6,8 @@ #define UNITY_INCLUDE_PRINT_FORMATTED #include #include "Tasks.hpp" +#include "Logging.hpp" +#include "Espressif.hpp" using namespace std::chrono_literals; @@ -126,6 +128,17 @@ namespace fabomatic::tests execute(); TEST_ASSERT_EQUAL_MESSAGE(NB_TASKS, task_counter, "Restarted tasks did not run immediately"); } + + void test_esp32() + { + auto result = fabomatic::esp32::esp_serial(); + TEST_ASSERT_GREATER_OR_EQUAL_UINT32_MESSAGE(0, result.length(), "ESP32 serial must be non empty"); + auto mem_free = fabomatic::esp32::getFreeHeap(); + TEST_ASSERT_GREATER_OR_EQUAL_UINT32_MESSAGE(0, mem_free, "ESP32 mem free must be > 0"); + fabomatic::esp32::showHeapStats(); + TEST_ASSERT_TRUE_MESSAGE(fabomatic::esp32::setupWatchdog(30s), "Watchdog setup"); + fabomatic::esp32::removeWatchdog(); + } } // namespace fabomatic::tests void setup() @@ -134,6 +147,7 @@ void setup() UNITY_BEGIN(); RUN_TEST(fabomatic::tests::test_execute_runs_all_tasks); RUN_TEST(fabomatic::tests::test_stop_start_tasks); + RUN_TEST(fabomatic::tests::test_esp32); UNITY_END(); // stop unit testing } From edc2840b338a068d76f6c3b39f910ba45ec6d89c Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:35:30 +0200 Subject: [PATCH 07/12] Update sizes.yml --- .github/workflows/sizes.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/sizes.yml b/.github/workflows/sizes.yml index afde5905..35f7f308 100644 --- a/.github/workflows/sizes.yml +++ b/.github/workflows/sizes.yml @@ -2,9 +2,7 @@ name: Compare firmware sizes on: workflow_dispatch: - workflow_run: - workflows: [Build firmware images] - types: [completed] + pull_request: concurrency: group: ${{ github.workflow }}-${{ github.ref }} From 211d10fb526c1426213497d517b7e89e88e51e35 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:39:38 +0200 Subject: [PATCH 08/12] Github actions: added sizes --- .github/workflows/sizes.yml | 2 +- .github/workflows/tags_sizes.yml | 102 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/tags_sizes.yml diff --git a/.github/workflows/sizes.yml b/.github/workflows/sizes.yml index 35f7f308..dd057a2d 100644 --- a/.github/workflows/sizes.yml +++ b/.github/workflows/sizes.yml @@ -1,4 +1,4 @@ -name: Compare firmware sizes +name: Compare firmware sizes (Pull request) on: workflow_dispatch: diff --git a/.github/workflows/tags_sizes.yml b/.github/workflows/tags_sizes.yml new file mode 100644 index 00000000..ad99233f --- /dev/null +++ b/.github/workflows/tags_sizes.yml @@ -0,0 +1,102 @@ +name: Compare firmware sizes (tag) + +on: + workflow_dispatch: + push: + tags: + - '*' # Trigger on all tags + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + size_report: + runs-on: ubuntu-latest + strategy: + matrix: + variant: + - hardware-rev0-it_IT + - esp32-devboard + steps: + - name: Check out the code + uses: actions/checkout@v4 + with: + fetch-depth: 50 + + - name: Install PlatformIO Core + uses: actions/setup-python@v5 + with: + python-version: '3.11' + - run: pip install --upgrade platformio + + - name: Use secrets.hpp.example as base for the build + run: cp conf/secrets.hpp.example conf/secrets.hpp + + - name: Build PlatformIO Project (current version) + run: pio run --environment ${{ matrix.variant }} + + - name: Copy latest MAP file + run: cp .pio/build/${{ matrix.variant }}/firmware.map ../firmware.map.latest + + - name: Find previous tag + id: prev_tag + run: | + tags=$(git tag --sort=-creatordate) + for tag in $tags; do + if [ "$tag" != "${GITHUB_REF#refs/tags/}" ]; then + echo "previous_tag=$tag" >> $GITHUB_ENV + break + fi + done + + - name: Check out previous tag + run: git checkout ${{ env.previous_tag }} + + - name: Use secrets.hpp.example as base for the build + run: cp conf/secrets.hpp.example conf/secrets.hpp + + - name: Build PlatformIO Project (previous version) + run: pio run --environment ${{ matrix.variant }} + + - name: Copy previous MAP file + run: cp .pio/build/${{ matrix.variant }}/firmware.map ../firmware.map.previous + + - name: Compare MAP files + run: python -m esp_idf_size --format=text --diff=../firmware.map.previous ../firmware.map.latest -o size_report.txt + + - name: Detailed report + run: python -m esp_idf_size --archives --format=text --diff=../firmware.map.previous ../firmware.map.latest -o size_report_details.txt + + - name: Upload size report + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.variant }}_size_report + path: | + size_report.txt + size_report_details.txt + retention-days: 90 + + - name: Comment size changes on release/tag page + if: startsWith(github.ref, 'refs/tags/') + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + const report = fs.readFileSync('size_report.txt', 'utf8'); + const previousTag = process.env.previous_tag; + if (report) { + const release = await github.rest.repos.getReleaseByTag({ + owner: context.repo.owner, + repo: context.repo.repo, + tag: context.ref.substring(10) + }); + const truncatedReport = report.length > 63*1024 ? report.substring(0, 63*1024) + '... (truncated)' : report; + await github.rest.repos.updateRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + release_id: release.data.id, + body: release.data.body + '\n\n## Firmware size changes for ${{ matrix.variant }}\n\nCommit CURRENT ' + context.sha + ' vs REFERENCE ' + previousTag + '\n```\n' + truncatedReport + '\n```' + }); + } From 1fe72b2fd14ea59a3b29d4ca8ea6ff89403faeba Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 22:43:21 +0200 Subject: [PATCH 09/12] Update tags_sizes.yml --- .github/workflows/tags_sizes.yml | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/tags_sizes.yml b/.github/workflows/tags_sizes.yml index ad99233f..6ac4a713 100644 --- a/.github/workflows/tags_sizes.yml +++ b/.github/workflows/tags_sizes.yml @@ -1,10 +1,10 @@ -name: Compare firmware sizes (tag) +name: Compare firmware sizes (Tag) on: workflow_dispatch: push: tags: - - '*' # Trigger on all tags + - '*' # Trigger on version tags concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -22,7 +22,7 @@ jobs: - name: Check out the code uses: actions/checkout@v4 with: - fetch-depth: 50 + fetch-depth: 0 # Fetch all tags - name: Install PlatformIO Core uses: actions/setup-python@v5 @@ -43,14 +43,22 @@ jobs: id: prev_tag run: | tags=$(git tag --sort=-creatordate) + current_tag=${GITHUB_REF#refs/tags/} + previous_tag="" + found=false for tag in $tags; do - if [ "$tag" != "${GITHUB_REF#refs/tags/}" ]; then - echo "previous_tag=$tag" >> $GITHUB_ENV + if [ "$found" = true ]; then + previous_tag=$tag break fi + if [ "$tag" = "$current_tag" ]; then + found=true + fi done + echo "previous_tag=$previous_tag" >> $GITHUB_ENV - - name: Check out previous tag + - name: Checkout previous tag + if: env.previous_tag != '' run: git checkout ${{ env.previous_tag }} - name: Use secrets.hpp.example as base for the build From 6b91a333702711d00408725ecfd23794fb907f27 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 23:07:27 +0200 Subject: [PATCH 10/12] Update tags_sizes.yml --- .github/workflows/tags_sizes.yml | 34 ++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tags_sizes.yml b/.github/workflows/tags_sizes.yml index 6ac4a713..0ab0e630 100644 --- a/.github/workflows/tags_sizes.yml +++ b/.github/workflows/tags_sizes.yml @@ -94,17 +94,35 @@ jobs: const fs = require('fs'); const report = fs.readFileSync('size_report.txt', 'utf8'); const previousTag = process.env.previous_tag; + const tagName = context.ref.substring(10); // Remove 'refs/tags/' prefix + const owner = context.repo.owner; + const repo = context.repo.repo; + const sha = context.sha; + const variant = context.payload.matrix.variant; + + let release; + try { + release = await github.rest.repos.getReleaseByTag({ owner, repo, tag: tagName }); + } catch (error) { + if (error.status === 404) { + release = await github.rest.repos.createRelease({ + owner, + repo, + tag_name: tagName, + name: tagName, + body: '' + }); + } else { + throw error; + } + } + if (report) { - const release = await github.rest.repos.getReleaseByTag({ - owner: context.repo.owner, - repo: context.repo.repo, - tag: context.ref.substring(10) - }); const truncatedReport = report.length > 63*1024 ? report.substring(0, 63*1024) + '... (truncated)' : report; await github.rest.repos.updateRelease({ - owner: context.repo.owner, - repo: context.repo.repo, + owner, + repo, release_id: release.data.id, - body: release.data.body + '\n\n## Firmware size changes for ${{ matrix.variant }}\n\nCommit CURRENT ' + context.sha + ' vs REFERENCE ' + previousTag + '\n```\n' + truncatedReport + '\n```' + body: release.data.body + '\n\n## Firmware size changes for ' + variant + '\n\nCommit CURRENT ' + sha + ' vs REFERENCE ' + previousTag + '\n```\n' + truncatedReport + '\n```' }); } From 14eb6c2ac75f0cc7d30b6094af7663941e5f01bd Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Mon, 27 May 2024 23:15:58 +0200 Subject: [PATCH 11/12] Github Actions --- .github/workflows/build.yml | 1 + .github/workflows/tags_sizes.yml | 128 ------------------------------- 2 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 .github/workflows/tags_sizes.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 69d603b9..4c4162f6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,6 +2,7 @@ name: Build firmware images on: workflow_dispatch: + pull_request: workflow_run: workflows: [Test suite based on Wokwi CLI] types: diff --git a/.github/workflows/tags_sizes.yml b/.github/workflows/tags_sizes.yml deleted file mode 100644 index 0ab0e630..00000000 --- a/.github/workflows/tags_sizes.yml +++ /dev/null @@ -1,128 +0,0 @@ -name: Compare firmware sizes (Tag) - -on: - workflow_dispatch: - push: - tags: - - '*' # Trigger on version tags - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - size_report: - runs-on: ubuntu-latest - strategy: - matrix: - variant: - - hardware-rev0-it_IT - - esp32-devboard - steps: - - name: Check out the code - uses: actions/checkout@v4 - with: - fetch-depth: 0 # Fetch all tags - - - name: Install PlatformIO Core - uses: actions/setup-python@v5 - with: - python-version: '3.11' - - run: pip install --upgrade platformio - - - name: Use secrets.hpp.example as base for the build - run: cp conf/secrets.hpp.example conf/secrets.hpp - - - name: Build PlatformIO Project (current version) - run: pio run --environment ${{ matrix.variant }} - - - name: Copy latest MAP file - run: cp .pio/build/${{ matrix.variant }}/firmware.map ../firmware.map.latest - - - name: Find previous tag - id: prev_tag - run: | - tags=$(git tag --sort=-creatordate) - current_tag=${GITHUB_REF#refs/tags/} - previous_tag="" - found=false - for tag in $tags; do - if [ "$found" = true ]; then - previous_tag=$tag - break - fi - if [ "$tag" = "$current_tag" ]; then - found=true - fi - done - echo "previous_tag=$previous_tag" >> $GITHUB_ENV - - - name: Checkout previous tag - if: env.previous_tag != '' - run: git checkout ${{ env.previous_tag }} - - - name: Use secrets.hpp.example as base for the build - run: cp conf/secrets.hpp.example conf/secrets.hpp - - - name: Build PlatformIO Project (previous version) - run: pio run --environment ${{ matrix.variant }} - - - name: Copy previous MAP file - run: cp .pio/build/${{ matrix.variant }}/firmware.map ../firmware.map.previous - - - name: Compare MAP files - run: python -m esp_idf_size --format=text --diff=../firmware.map.previous ../firmware.map.latest -o size_report.txt - - - name: Detailed report - run: python -m esp_idf_size --archives --format=text --diff=../firmware.map.previous ../firmware.map.latest -o size_report_details.txt - - - name: Upload size report - uses: actions/upload-artifact@v4 - with: - name: ${{ matrix.variant }}_size_report - path: | - size_report.txt - size_report_details.txt - retention-days: 90 - - - name: Comment size changes on release/tag page - if: startsWith(github.ref, 'refs/tags/') - uses: actions/github-script@v7 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const fs = require('fs'); - const report = fs.readFileSync('size_report.txt', 'utf8'); - const previousTag = process.env.previous_tag; - const tagName = context.ref.substring(10); // Remove 'refs/tags/' prefix - const owner = context.repo.owner; - const repo = context.repo.repo; - const sha = context.sha; - const variant = context.payload.matrix.variant; - - let release; - try { - release = await github.rest.repos.getReleaseByTag({ owner, repo, tag: tagName }); - } catch (error) { - if (error.status === 404) { - release = await github.rest.repos.createRelease({ - owner, - repo, - tag_name: tagName, - name: tagName, - body: '' - }); - } else { - throw error; - } - } - - if (report) { - const truncatedReport = report.length > 63*1024 ? report.substring(0, 63*1024) + '... (truncated)' : report; - await github.rest.repos.updateRelease({ - owner, - repo, - release_id: release.data.id, - body: release.data.body + '\n\n## Firmware size changes for ' + variant + '\n\nCommit CURRENT ' + sha + ' vs REFERENCE ' + previousTag + '\n```\n' + truncatedReport + '\n```' - }); - } From efb8e5d46e1eec97f128d73fedfeb24e1518201f Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Wed, 29 May 2024 20:06:27 +0200 Subject: [PATCH 12/12] BufferedMsg: changed push_back/push_front to accept BufferedMsg --- include/BufferedMsg.hpp | 4 ++-- src/BufferedMsg.cpp | 27 +++++++++++++--------- src/FabBackend.cpp | 15 ++++++++---- test/test_savedconfig/test_savedconfig.cpp | 9 +++++--- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/include/BufferedMsg.hpp b/include/BufferedMsg.hpp index cd6a46cd..8b5b1342 100644 --- a/include/BufferedMsg.hpp +++ b/include/BufferedMsg.hpp @@ -31,8 +31,8 @@ namespace fabomatic static constexpr auto MAGIC_NUMBER = 1; public: - auto push_back(const std::string &message, const std::string &topic, bool wait) -> void; - auto push_front(const std::string &message, const std::string &topic, bool wait) -> void; + auto push_back(const BufferedMsg &message) -> void; + auto push_front(const BufferedMsg &message) -> void; auto getMessage() -> const BufferedMsg; auto count() const -> size_t; auto toJson(JsonDocument &doc, const std::string &element_name) const -> void; diff --git a/src/BufferedMsg.cpp b/src/BufferedMsg.cpp index 4dbc2895..d7637c1a 100644 --- a/src/BufferedMsg.cpp +++ b/src/BufferedMsg.cpp @@ -6,12 +6,11 @@ namespace fabomatic { - auto Buffer::push_back(const std::string &message, const std::string &topic, bool wait) -> void + auto Buffer::push_back(const BufferedMsg &message) -> void { if constexpr (conf::debug::ENABLE_BUFFERING) { - BufferedMsg msg{message, topic, wait}; - msg_queue.push_back(msg); + msg_queue.push_back(message); if (msg_queue.size() > MAX_MESSAGES) { msg_queue.pop_front(); @@ -19,16 +18,18 @@ namespace fabomatic } has_changed = true; - ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued", message.c_str(), topic.c_str(), msg_queue.size()); + ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued", + message.mqtt_message.c_str(), + message.mqtt_topic.c_str(), + msg_queue.size()); } } - auto Buffer::push_front(const std::string &message, const std::string &topic, bool wait) -> void + auto Buffer::push_front(const BufferedMsg &message) -> void { if constexpr (conf::debug::ENABLE_BUFFERING) { - BufferedMsg msg{message, topic, wait}; - msg_queue.push_front(msg); + msg_queue.push_front(message); if (msg_queue.size() > MAX_MESSAGES) { msg_queue.pop_back(); @@ -36,7 +37,10 @@ namespace fabomatic } has_changed = true; - ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued", message.c_str(), topic.c_str(), msg_queue.size()); + ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued", + message.mqtt_message.c_str(), + message.mqtt_topic.c_str(), + msg_queue.size()); } } @@ -89,9 +93,10 @@ namespace fabomatic for (const auto &elem : json_obj["messages"].as()) { - buff.push_back(elem["msg"].as(), - elem["tp"].as(), - elem["wait"].as()); + const auto &msg = BufferedMsg{elem["msg"].as(), + elem["tp"].as(), + elem["wait"].as()}; + buff.push_back(msg); } ESP_LOGD(TAG, "Buffer::fromJsonElement() : data loaded successfully"); diff --git a/src/FabBackend.cpp b/src/FabBackend.cpp index f34f2d58..e0ea7893 100644 --- a/src/FabBackend.cpp +++ b/src/FabBackend.cpp @@ -94,7 +94,8 @@ namespace fabomatic // Do not send twice if response did not arrive if (query.buffered() && !published) { - buffer.push_back(query.payload(), topic, query.waitForReply()); + const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()}; + buffer.push_back(msg); } if (published) @@ -440,7 +441,8 @@ namespace fabomatic if (query.buffered()) { - buffer.push_back(query.payload(), topic, query.waitForReply()); + const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()}; + buffer.push_back(msg); } return std::make_unique(false); @@ -489,7 +491,8 @@ namespace fabomatic if (query.buffered()) { - buffer.push_back(query.payload(), topic, query.waitForReply()); + const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()}; + buffer.push_back(msg); } return false; } @@ -602,7 +605,8 @@ namespace fabomatic // If it has been published but not answered, do not try again if (result == PublishResult::PublishedWithoutAnswer) { - buffer.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer); + const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer}; + buffer.push_front(bmsg); } break; } @@ -613,7 +617,8 @@ namespace fabomatic { // Will try again ESP_LOGW(TAG, "Retransmitting buffered message failed!"); - buffer.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer); + const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer}; + buffer.push_front(bmsg); break; } } diff --git a/test/test_savedconfig/test_savedconfig.cpp b/test/test_savedconfig/test_savedconfig.cpp index 1754c567..4ad8e479 100644 --- a/test/test_savedconfig/test_savedconfig.cpp +++ b/test/test_savedconfig/test_savedconfig.cpp @@ -238,7 +238,8 @@ namespace fabomatic::tests for (const auto &msg : messages) { TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "Push_back: Buffer count is correct"); - buff.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer); + const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer}; + buff.push_front(bmsg); msg_count++; } @@ -265,7 +266,8 @@ namespace fabomatic::tests for (const auto &msg : messages) { TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "(1) Push_front: Buffer count is correct"); - buff.push_back(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer); + const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer}; + buff.push_back(bmsg); msg_count++; } @@ -293,7 +295,8 @@ namespace fabomatic::tests for (const auto &msg : messages) { TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "(2) Push_front: Buffer count is correct"); - buff.push_back(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer); + const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer}; + buff.push_back(bmsg); msg_count++; }