Rtop 58 plugin modbus slave#6
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Note 🎁 Summarized by CodeRabbit FreeYour organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above. Comment |
| self.safe_buffer_access = SafeBufferAccess(runtime_args) | ||
| if not self.safe_buffer_access.is_valid: | ||
| print(f"[MODBUS] Warning: Failed to create safe buffer access: {self.safe_buffer_access.error_msg}") | ||
| print(f"[MODBUS] Warning: Failed to create safe buffer access for coils: {self.safe_buffer_access.error_msg}") |
There was a problem hiding this comment.
Maybe switch to logging
| addr = ctypes.addressof(_runtime_args.bool_output[0][0]) | ||
| value, msg = _safe_buffer_access.safe_read_bool_output(0,0) | ||
| value, msg = _safe_buffer_access.read_bool_output(0,0, thread_safe=True) | ||
| print(f"Value at address 0x{addr:x}: {value} ({msg})") |
There was a problem hiding this comment.
Maybe use logging instead of print
* Add plugin driver system with config parsing Introduces a plugin driver framework supporting both native and Python plugins, including configuration parsing, driver management, and integration into the main PLC application. Updates CMake to link Python libraries and adds example configuration files for plugins. (WIP) * sync plugin driver * Adjusting python.h include Accordingly with the documentation, python header should be the first called and for security, we have to define PY_SSIZE_T_CLEAN * fix init driver's args encapsulation * adjusting brackets position and function identation Everything accordingly BARR Standard * fix cmakelist * adjust python_plugin_bridge.h identation everything accordingly BARR standard * python start funct running within a thread deleting python cycle function since it will be running async * fixing pointer dereferencing for some reason, when init is called it can successfully link buffers and function address between runtime and plugin, but when the same previous parsed "struct" is called within start function, the buffer pointer was no longer pointing to the right address. * Fix buffer access in Python plugin driver Corrects how bool_output buffer values are read and written by accessing the actual value via .contents.value instead of the pointer. Updates example plugin to use SafeBufferAccess for safer buffer operations and improves output logging. * deleting stop call Stop is already being called within destroy function * Remove unused _runtime_args_capsule variable Eliminated the _runtime_args_capsule global variable and related code from example_python_plugin.py, simplifying state management and usage of runtime arguments. * Refactor Python plugin threading and lifecycle management Moved plugin thread creation to Python side and removed native thread management for Python plugins. Updated function names in python_binds_t for clarity. Improved plugin start, stop, and cleanup logic to use Python-side functions and ensured proper GIL handling. Cleaned up resource management and removed unused thread fields from plugin_instance_t. * Refactor plugin driver cleanup and GIL management Improves Python GIL state management in plugin_driver by using a static variable and ensuring proper acquisition/release during plugin lifecycle. Moves plugin driver cleanup earlier in plc_main to avoid double destruction and adds more informative logging during plugin stop. * Refactor plugin loop to run in a separate thread The plugin's main loop now runs in a background thread using Python's threading module. Added a stop event to allow graceful termination of the loop in stop_loop, improving plugin lifecycle management and preventing blocking the main thread. * Refactor Modbus plugin for safer buffer access Replaces manual ctypes structure and buffer access with type-safe wrappers from python_plugin_types. Updates OpenPLCModbusDataBlock to use SafeBufferAccess for reading and writing coil values, improving safety and error handling. Refactors plugin initialization and server startup for better diagnostics and reliability. * Update Python plugin documentation and type safety Revised README to clarify plugin type values, enhance Python plugin type safety, and document usage of the new python_plugin_types.py module. Added examples for thread-safe buffer access, advanced Modbus implementation, and improved plugin initialization with structure validation and error handling. * moving examples and plugins to respective folder * deleting unused plugins from config * Expose plugin mutex helpers and use in PLC cycle Made plugin_mutex_take and plugin_mutex_give functions public in plugin_driver.h and used them to protect buffer access in the PLC cycle thread. Also refactored plugin_driver variable to be global in plc_main.c for thread safety. * Changing pluggins paths to meet exec.sh start path * Rtop 58 plugin modbus slave (#6) * thread safe and dump access in the same function * adding batch functions that allows multiple reads/writes * Providing wrappers for IS, IR and HR * avoiding magical numbers * avoiding generic exception handling * fixing plugin's dedicated data retrieval * RTOP 74 adding plugin individual venv usage * Update scripts/manage_plugin_venvs.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Disabling initial plugin prints and avoiding code insertion * install now has support for apt yum and dfs and plc program is being built * adding proper build error and success logs * [RTOP 74][WIP] implementing initialization scripts * changing runtime venv from .venv to venvs/runtime/ * Add requirements.txt to the Modbus driver * Add checks on install and start_openplc scripts * Quick fix on start_openplc.sh * [RTOP 59] plugin driver layer unit tests (#14) * adding ceedling configuration and tests folder * adding config parsing tests * adding support folder for tests * deleting unnecessary python include * adding plugin driver tests * updating tests to not use cmock * deleting unused files * deleting unused file * removing PY_SSIZE_T_CLEAN define * adding stubs to avoiding duplicated test codes * releasing memory within test free override * avoiding newline charactere within config structure * adjusting clang format standard * suppressing free tests warnings * renaming stubs to avoid being recognized as a test * updating plugins readme * Update tests/test_plugin_driver.c Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update core/src/drivers/plugins/python/modbus_slave/simple_modbus.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * importing sys and os at example_python_plugin.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Thiago Alves <thiagoralves@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Autonomy Server <autonomyserver@Autonomys-Mac-mini.local>
* Add plugin driver system with config parsing Introduces a plugin driver framework supporting both native and Python plugins, including configuration parsing, driver management, and integration into the main PLC application. Updates CMake to link Python libraries and adds example configuration files for plugins. (WIP) * Refactor plugin driver for Python Modbus support Removed legacy driver_api files and introduced new plugin driver structures to support Python-based plugins. Added a simple_modbus.py driver and configuration for Modbus slave integration. Updated plugin_driver.h and python_plugin_bridge.h to support Python plugin bindings and runtime argument passing. Modified plc_main.c to call plugin_driver_init instead of plugin_driver_start. Updated plugins.conf to use the new Python Modbus plugin. * sync plugin driver * Adjusting python.h include Accordingly with the documentation, python header should be the first called and for security, we have to define PY_SSIZE_T_CLEAN * fix init driver's args encapsulation * adjusting brackets position and function identation Everything accordingly BARR Standard * fix cmakelist * adjust python_plugin_bridge.h identation everything accordingly BARR standard * python start funct running within a thread deleting python cycle function since it will be running async * fixing pointer dereferencing for some reason, when init is called it can successfully link buffers and function address between runtime and plugin, but when the same previous parsed "struct" is called within start function, the buffer pointer was no longer pointing to the right address. * Fix buffer access in Python plugin driver Corrects how bool_output buffer values are read and written by accessing the actual value via .contents.value instead of the pointer. Updates example plugin to use SafeBufferAccess for safer buffer operations and improves output logging. * deleting stop call Stop is already being called within destroy function * Remove unused _runtime_args_capsule variable Eliminated the _runtime_args_capsule global variable and related code from example_python_plugin.py, simplifying state management and usage of runtime arguments. * Refactor Python plugin threading and lifecycle management Moved plugin thread creation to Python side and removed native thread management for Python plugins. Updated function names in python_binds_t for clarity. Improved plugin start, stop, and cleanup logic to use Python-side functions and ensured proper GIL handling. Cleaned up resource management and removed unused thread fields from plugin_instance_t. * Refactor plugin driver cleanup and GIL management Improves Python GIL state management in plugin_driver by using a static variable and ensuring proper acquisition/release during plugin lifecycle. Moves plugin driver cleanup earlier in plc_main to avoid double destruction and adds more informative logging during plugin stop. * Refactor plugin loop to run in a separate thread The plugin's main loop now runs in a background thread using Python's threading module. Added a stop event to allow graceful termination of the loop in stop_loop, improving plugin lifecycle management and preventing blocking the main thread. * Refactor Modbus plugin for safer buffer access Replaces manual ctypes structure and buffer access with type-safe wrappers from python_plugin_types. Updates OpenPLCModbusDataBlock to use SafeBufferAccess for reading and writing coil values, improving safety and error handling. Refactors plugin initialization and server startup for better diagnostics and reliability. * Update Python plugin documentation and type safety Revised README to clarify plugin type values, enhance Python plugin type safety, and document usage of the new python_plugin_types.py module. Added examples for thread-safe buffer access, advanced Modbus implementation, and improved plugin initialization with structure validation and error handling. * moving examples and plugins to respective folder * deleting unused plugins from config * Expose plugin mutex helpers and use in PLC cycle Made plugin_mutex_take and plugin_mutex_give functions public in plugin_driver.h and used them to protect buffer access in the PLC cycle thread. Also refactored plugin_driver variable to be global in plc_main.c for thread safety. * Changing pluggins paths to meet exec.sh start path * Rtop 58 plugin modbus slave (#6) * thread safe and dump access in the same function * adding batch functions that allows multiple reads/writes * Providing wrappers for IS, IR and HR * avoiding magical numbers * avoiding generic exception handling * fixing plugin's dedicated data retrieval * RTOP 74 adding plugin individual venv usage * Update scripts/manage_plugin_venvs.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Disabling initial plugin prints and avoiding code insertion * install now has support for apt yum and dfs and plc program is being built * adding proper build error and success logs * [RTOP 74][WIP] implementing initialization scripts * changing runtime venv from .venv to venvs/runtime/ * Add requirements.txt to the Modbus driver * Add checks on install and start_openplc scripts * Quick fix on start_openplc.sh * Fix zip file check to allow generated bash script * [RTOP-72] Parse and log unix socket log messages * [RTOP-72] Additional Exceptions * [RTOP-72] Logging format refactor * [RTOP-76] Logging module * [RTOP-76] Logger parser * [RTOP-76] Logger in app * [RTOP-76] Runtime logging buffer test * [RTOP-76] Fix logging instantiation, parser and buffer * [RTOP-76] Runtime Logs parser, buffering and json response --------- Co-authored-by: Marcone Tenório da Silva Filho <marconetsf@gmail.com> Co-authored-by: Thiago Alves <thiagoralves@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Autonomy Server <autonomyserver@Autonomys-Mac-mini.local>
* Add plugin driver system with config parsing Introduces a plugin driver framework supporting both native and Python plugins, including configuration parsing, driver management, and integration into the main PLC application. Updates CMake to link Python libraries and adds example configuration files for plugins. (WIP) * Refactor plugin driver for Python Modbus support Removed legacy driver_api files and introduced new plugin driver structures to support Python-based plugins. Added a simple_modbus.py driver and configuration for Modbus slave integration. Updated plugin_driver.h and python_plugin_bridge.h to support Python plugin bindings and runtime argument passing. Modified plc_main.c to call plugin_driver_init instead of plugin_driver_start. Updated plugins.conf to use the new Python Modbus plugin. * sync plugin driver * Adjusting python.h include Accordingly with the documentation, python header should be the first called and for security, we have to define PY_SSIZE_T_CLEAN * fix init driver's args encapsulation * adjusting brackets position and function identation Everything accordingly BARR Standard * fix cmakelist * adjust python_plugin_bridge.h identation everything accordingly BARR standard * python start funct running within a thread deleting python cycle function since it will be running async * fixing pointer dereferencing for some reason, when init is called it can successfully link buffers and function address between runtime and plugin, but when the same previous parsed "struct" is called within start function, the buffer pointer was no longer pointing to the right address. * Fix buffer access in Python plugin driver Corrects how bool_output buffer values are read and written by accessing the actual value via .contents.value instead of the pointer. Updates example plugin to use SafeBufferAccess for safer buffer operations and improves output logging. * deleting stop call Stop is already being called within destroy function * Remove unused _runtime_args_capsule variable Eliminated the _runtime_args_capsule global variable and related code from example_python_plugin.py, simplifying state management and usage of runtime arguments. * Refactor Python plugin threading and lifecycle management Moved plugin thread creation to Python side and removed native thread management for Python plugins. Updated function names in python_binds_t for clarity. Improved plugin start, stop, and cleanup logic to use Python-side functions and ensured proper GIL handling. Cleaned up resource management and removed unused thread fields from plugin_instance_t. * Refactor plugin driver cleanup and GIL management Improves Python GIL state management in plugin_driver by using a static variable and ensuring proper acquisition/release during plugin lifecycle. Moves plugin driver cleanup earlier in plc_main to avoid double destruction and adds more informative logging during plugin stop. * Refactor plugin loop to run in a separate thread The plugin's main loop now runs in a background thread using Python's threading module. Added a stop event to allow graceful termination of the loop in stop_loop, improving plugin lifecycle management and preventing blocking the main thread. * Refactor Modbus plugin for safer buffer access Replaces manual ctypes structure and buffer access with type-safe wrappers from python_plugin_types. Updates OpenPLCModbusDataBlock to use SafeBufferAccess for reading and writing coil values, improving safety and error handling. Refactors plugin initialization and server startup for better diagnostics and reliability. * Update Python plugin documentation and type safety Revised README to clarify plugin type values, enhance Python plugin type safety, and document usage of the new python_plugin_types.py module. Added examples for thread-safe buffer access, advanced Modbus implementation, and improved plugin initialization with structure validation and error handling. * moving examples and plugins to respective folder * deleting unused plugins from config * Expose plugin mutex helpers and use in PLC cycle Made plugin_mutex_take and plugin_mutex_give functions public in plugin_driver.h and used them to protect buffer access in the PLC cycle thread. Also refactored plugin_driver variable to be global in plc_main.c for thread safety. * Changing pluggins paths to meet exec.sh start path * Rtop 58 plugin modbus slave (#6) * thread safe and dump access in the same function * adding batch functions that allows multiple reads/writes * Providing wrappers for IS, IR and HR * avoiding magical numbers * avoiding generic exception handling * fixing plugin's dedicated data retrieval * RTOP 74 adding plugin individual venv usage * Update scripts/manage_plugin_venvs.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Disabling initial plugin prints and avoiding code insertion * install now has support for apt yum and dfs and plc program is being built * adding proper build error and success logs * [RTOP 74][WIP] implementing initialization scripts * changing runtime venv from .venv to venvs/runtime/ * Add requirements.txt to the Modbus driver * Add checks on install and start_openplc scripts * Quick fix on start_openplc.sh * Fix zip file check to allow generated bash script * [RTOP-72] Parse and log unix socket log messages * [RTOP-72] Additional Exceptions * [RTOP-72] Logging format refactor * [RTOP-76] Logging module * [RTOP-76] Logger parser * [RTOP-76] Logger in app * [RTOP-76] Runtime logging buffer test * [RTOP-76] Fix logging instantiation, parser and buffer * [RTOP-76] Runtime Logs parser, buffering and json response * [RTOP-77] Python logs parsing to JSON * [RTOP-78] Fix python restapi logging format * [RTOP-78] Logs id field * [RTOP-78] Adding python logging messages * [RTOP-78] Logs simple filter * [RTOP-78] Fix logs filter * Update webserver/plcapp_management.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * [RTOP-78] db exceptions COPILOT fix sugestions * [RTOP-78] Fix kwt Exception COPILOT sugestion * [RTOP-78] Remove allowed bash file in zip * [RTOP-78] Remove json library that is not used in this file * [RTOP-78] Fix and removing Exceptions imports * Fix failed build due to logger * [RTOP-78] Fix restapi database and env directory location for docker volumes * [RTOP-78] config.py import logging module * Update webserver/runtimemanager.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update webserver/logger/bufferhandler.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * [RTOP-78] Removing commented code --------- Co-authored-by: Marcone Tenório da Silva Filho <marconetsf@gmail.com> Co-authored-by: Thiago Alves <thiagoralves@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Autonomy Server <autonomyserver@Autonomys-Mac-mini.local>
- Fix #2: Guard buffer mutex release after crash with a volatile flag (holding_buffer_mutex) to avoid undefined behavior when unlocking a mutex not owned by the calling thread - Fix #3: Reset plc_crash_signal to 0 at the start of plc_cycle_thread so stale values don't persist after successful recovery - Fix #4: Add plc_force_error_state() function for the watchdog to transition to ERROR state through the mutex instead of writing plc_state directly. Remove extern PLCState from watchdog.c - Fix #5: Skip watchdog heartbeat check when already in ERROR state to prevent repeated error log spam every 2 seconds. Move heartbeat reset to the non-RUNNING branch so it happens once on ERROR entry - Fix #6: Add trailing newline to plc_state_manager.h - Fix #8: Add threading.Lock to protect _crash_times and _safe_mode in RuntimeManager against concurrent access from the monitor and compilation threads - Fix #9: Rename _should_enter_safe_mode to _record_crash_and_check_safe_mode to make the side effect explicit Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This pull request contains the following changes:
Define the folder shared as a module that can be imported within any example or plugin folder.
This folder contains python_plugin_types.py that offer functions to handle C Runtime arguments and access its buffers.
Now it is possible to import python_plugin_types in new plugins files using the following syntax
Major changes in SafeBufferAccess class:
New methods were added and some of them were changed.
Added methods
Note: Now read and write functions allow you to decide if you want to acquire mutex internally or not setting the parameter
thread_safeas true of false. By default it is true.Modbus Slave Plugin added
This plugin allows runtime data to be safely accessed through modbus tcp masters.
This plugin uses python_plugin_types.py classes and methods to get and set runtime's buffers values.