opt/1085-optimization-and-cleanup #37

Merged
ismail merged 36 commits from opt/1085-optimization-and-cleanup into dev 2025-09-05 10:05:42 +02:00
Owner

Summary

This pull request introduces multiple improvements, refactorings, and bug fixes across the relay channel core, NVS module, notification system, and testing infrastructure.
The primary goals are to optimize performance, improve reliability, clean up the codebase, and align the implementation with ESP-IDF best practices.
Additionally, it enhances test coverage, adds SPDX headers, introduces build profile usage, and improves maintainability by addressing long-standing issues.

TICKET-1085

Changes

  • Fixed conditional compilation issues in CMakeLists and source files.
  • Restored original naming convention for config parameters (with CONFIG_ prefix).
  • Fixed static variable naming to follow ESP-IDF style.
  • Added SPDX license headers to source files.
  • Introduced a dedicated notification system for relay channel state changes.
  • Enhanced the NVS module with a background task, async writes, better commit logic, and improved error handling.
  • Fixed critical NVS key generation bug for multi-channel storage.
  • Added default tilt sensitivity get function and tests.
  • Refactored and improved unit tests (NVS, reset functions, tilt, etc.).
  • Added new *all and *all_with functions to simplify API.
  • Improved relay command handling with prioritized STOP and tilt sensitivity boundary checks.
  • Implemented relay_chn_stop_prv for more efficient stop handling.
  • Fixed listener unregistration behavior and notification queue handling.
  • Fixed various bugs (timer underflow, esp_timer_start error handling, STOP command not interrupting).
  • Introduced build profiles instead of multiple sdkconfig.defaults.* files.
  • Stopped tracking auto-generated sdkconfig files via .gitignore.
  • General code cleanup: constants replacement, bracket refactors, style alignment, improved comments, and removed unused declarations.
  • Expanded test coverage with missing cases, tags, and fixes for multi-channel tests.
  • Added .ESP-IDF vscode configuration files for development.
  • #1085 - Optimization and cleanup
  • #1086 - Add boundary checks for tilt sensitivity
  • #1087 - Rename time_sec to limit_sec for clarity
  • #1088 - Enhance relay command handling
  • #1089 - Implement *all and *all_with functions and related tests
  • #1090 - Add missing test cases
  • #1091 - Fix possible integer underflow bug
  • #1092 - Fix esp_timer_start error handling
  • #1093 - STOP command does not interrupt
  • #1094 - Fix single channel tests in multi mode
  • #1095 - Stop between transitions leads to many state changes
  • #1096 - Notification system and NVS enhancements
  • #1097 - Notification system improvements with bakcground task
  • #1098 - NVS improvements with background task
  • #1099 - Add SPDX license headers
  • #1100 - Fix NVS key generation bug
  • #1101 - Add default tilt sensitivity get function
  • #1102 - Fix unregister listener behavior
  • #1103 - Fix static variable names

Breaking Changes

  • Config parameter names have been restored to their original CONFIG_ prefixed form.
  • Old listener management code has been removed and replaced with the new notification system. Now, listener callbacks are called from a background task.
  • NVS write operations are now processed in a dedicated background task.
  • API changes and new API functions for relay channel batch operation in multi-channel mode.

Migration Guide

  • Update any code that referenced renamed config parameters to use the CONFIG_ prefixed versions.
  • No changes are required to the listener-related code, as no changes have been made to the public API.
  • Update any code that sets or gets settings saved in NVS (if enabled). Note that setting functions save values to NVS asynchronously, while getting is still synchronous. See README > NVS Operation Details.
  • If relying on the old batch operation relay command APIs, update to the new *all/*all_with functions. These functions are available only for multi-channel mode. See README > Usage for further information.
## Summary This pull request introduces multiple improvements, refactorings, and bug fixes across the relay channel core, NVS module, notification system, and testing infrastructure. The primary goals are to optimize performance, improve reliability, clean up the codebase, and align the implementation with ESP-IDF best practices. Additionally, it enhances test coverage, adds SPDX headers, introduces build profile usage, and improves maintainability by addressing long-standing issues. TICKET-1085 ## Changes - Fixed conditional compilation issues in CMakeLists and source files. - Restored original naming convention for config parameters (with `CONFIG_` prefix). - Fixed static variable naming to follow ESP-IDF style. - Added SPDX license headers to source files. - Introduced a dedicated notification system for relay channel state changes. - Enhanced the NVS module with a background task, async writes, better commit logic, and improved error handling. - Fixed critical NVS key generation bug for multi-channel storage. - Added default tilt sensitivity get function and tests. - Refactored and improved unit tests (NVS, reset functions, tilt, etc.). - Added new `*all` and `*all_with` functions to simplify API. - Improved relay command handling with prioritized STOP and tilt sensitivity boundary checks. - Implemented `relay_chn_stop_prv` for more efficient stop handling. - Fixed listener unregistration behavior and notification queue handling. - Fixed various bugs (timer underflow, esp_timer_start error handling, STOP command not interrupting). - Introduced build profiles instead of multiple `sdkconfig.defaults.*` files. - Stopped tracking auto-generated sdkconfig files via `.gitignore`. - General code cleanup: constants replacement, bracket refactors, style alignment, improved comments, and removed unused declarations. - Expanded test coverage with missing cases, tags, and fixes for multi-channel tests. - Added `.ESP-IDF` vscode configuration files for development. ## Related Issues - #1085 - Optimization and cleanup - #1086 - Add boundary checks for tilt sensitivity - #1087 - Rename time_sec to limit_sec for clarity - #1088 - Enhance relay command handling - #1089 - Implement *all and *all_with functions and related tests - #1090 - Add missing test cases - #1091 - Fix possible integer underflow bug - #1092 - Fix esp_timer_start error handling - #1093 - STOP command does not interrupt - #1094 - Fix single channel tests in multi mode - #1095 - Stop between transitions leads to many state changes - #1096 - Notification system and NVS enhancements - #1097 - Notification system improvements with bakcground task - #1098 - NVS improvements with background task - #1099 - Add SPDX license headers - #1100 - Fix NVS key generation bug - #1101 - Add default tilt sensitivity get function - #1102 - Fix unregister listener behavior - #1103 - Fix static variable names ## Breaking Changes - Config parameter names have been restored to their original `CONFIG_` prefixed form. - Old listener management code has been removed and replaced with the new notification system. Now, listener callbacks are called from a background task. - NVS write operations are now processed in a dedicated background task. - API changes and new API functions for relay channel batch operation in multi-channel mode. ## Migration Guide - Update any code that referenced renamed config parameters to use the `CONFIG_` prefixed versions. - No changes are required to the listener-related code, as no changes have been made to the public API. - Update any code that sets or gets settings saved in NVS (if enabled). Note that setting functions save values to NVS asynchronously, while getting is still synchronous. See [README > NVS Operation Details](https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn/src/branch/opt/1085-optimization-and-cleanup/README.md#nvs-operation-details). - If relying on the old batch operation relay command APIs, update to the new `*all/*all_with` functions. These functions are available only for multi-channel mode. See [README > Usage](https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn/src/branch/opt/1085-optimization-and-cleanup/README.md#usage) for further information.
ismail added 36 commits 2025-09-05 09:52:59 +02:00
Refs #1085.
The config parameter names defined in the relay_chn_defs.h file
have been changed back to their original names (with the CONFIG_ prefix),
so that they are not confused with application-level defines.

Refs #1085
Unnecessary `#if CONFIG_FOO == 1` checks were removed and the
statements were simplified to` #if CONFIG_FOO`.
`#ifdef CONFIG_FOO` statements were also changed to `#if CONFIG_FOO`
to keep the style as uniform as possible.

Refs #1085
Specific `_*all` and `_*all_with` functions were implemented to make
the API more concise and clean, and to replace the use of
`RELAY_CHN_ID_ALL`, which caused confusion within the API.

Refs #1085
- Delete unused declaration of `g_is_component_initialized`.
- Replace the following constants with approprite config options:
  + `relay_chn_count` > `CONFIG_RELAY_CHN_COUNT`
  + `opposite_inerta_ms` > `CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS`
- Replace the definition of the `test_delay_margin_ms` constant with  `#define TEST_DELAY_MARGIN_MS 50` for preprocessor calculations.
- `TILT_STOP` command is prioritized in `relay_chn_tilt_issue_cmd()` because it should override any other tilt commands.
- The debug logs are cleaned up.

Fixes #1088
Refs #1085
Refactor tilt test functions to use batch operations for all channels and improve state checks.

Refs #1085
Refactor the brackets of the test case functions to align with ESP-IDF style.

Refs #1085
ESP-IDF provides a more efficient way of handling and combining
multi-configurations for a project than the way currently used:
Build profiles.

So I switched to this method instead of providing the sdkconfig.defaults.*
files manually. This reduced the number of sdkconfig.defaults.* files
as well as the configuration parameters defined in them.

Refs #1085
The tilt sensitivity values were passed to the NVS module without
the boundaries being checked, even though the maximum
percent value is 100. This commit fixes this issue.
Also test cases are added to cover the upper boundary checks
for the tilt sensitivity settings.

Fixes #1086
Fixes #1087
Fixed a possible integer underflow bug that may cause the timer
to be set for an unexpectedly long duration.

Fixes #1091
A helper function added for each module so that each module
handles errors by itself.

Fixes #1092
An if statement has been added to handle the STOP command properly
when the `previous_state` is one of the `*PENDING` states.

Fixes #1093
Refs #1090
Some missing test cases for the public API have been added.

Closes #1090
- Refactor reset_channels_to_idle_state to reset_channels_to_defaults
and enhance functionality with direction reset logic. This is because
some tilt test cases were failing due to modified run limit values in some
of the previous core test cases. See #1089-3.
- A relay channel listener has been added to diagnose channel
states during tests.

Refs #1089
Added test cases for the recently implemented `*all` and `*all_with`
functions. Closes #1089.
Implemented `relay_chn_stop_prv` to streamline stop command handling
and avoid unnecessary code execution. Fixes #1095.
Some test cases that were testing for only one channel in multi-channel mode are fixed to test all available channels. Fixes #1094
- Introduced a new notification module to handle state change listeners.
- Added functions to register and unregister listeners for relay channel state changes.
- Implemented a queue-based system to manage notifications and listener callbacks.
- Updated core relay channel logic to utilize the new notification system.
- Removed old listener management code from relay channel core.
- Refactored the former listener tests to notify tests and added tests for the notification system, including handling of multiple listeners and queue overflow scenarios.
- Updated CMakeLists.txt to include new source files and headers for the notification module.
- Revised README.md to include warnings about callback execution context and performance considerations.

Refs #1096, #1085 and closes #1097
- Fixed a critical NVS key generation bug that would cause overwriting the values for all channels.
- Optimized the code for single channel mode since no formatting required.
- Improved multi-channel test coverage to cover that each value for each channel stored correctly.

Refs #1096, #1098 and fixes #1100
- Implemented a dedicated background task to decouple long-running code from the main application task.
- Improved the NVS commit code logic, especially for batch writes to minimize flash wear.
- Updated NVS functions to support asynchronous writes and synchronous reads.
- Added default value parameters to `get` functions for better usability.
- Improved error handling and logging in NVS operations.
- Refactored related code in multiple source files to accommodate these changes.

Refs #1085, #1096 and closes #1098
- Refactored and improved NVS tests to accomodate latest changes in NVS module. See #1098
- Improved channel reset function that is called from `tearDown` to minimize public API calls that trigger a chain of internal calls that involve NVS and timer operations.
- Fixed test case bugs that make some test cases to fail.

Refs #1096, #1098
- Add check for empty listener list in find_listener_entry function.
- Reset notification queue when all listeners are removed.
- Change queue operation to send remove_listener message to the front of the queue.

Refs #1085, #1096 and fixes #1102
Added `relay_chn_tilt_get_default_sensitivity` function and test cases.

Refs #1085 and fixes #1101
Refs #1085 and closes #1099
Fixed static variable names according to the ESP-IDF C code formatting guide.

Refs #1085 and fixes #1103
Fixed include directory settings and conditional includes in CMakeLists and test_common.c. Refs #1085.
ismail merged commit a3f83eaaee into dev 2025-09-05 10:05:42 +02:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: KozmotronikTech/relay_chn#37