Skip to content

Conversation

eternal-echo
Copy link
Contributor

Overview

This PR enhances the esp_isotp component with thread safety, event-driven callbacks, and extended feature support, transforming it from a basic polling implementation to a production-ready ISO-TP communication library.

Key Features

Thread Safety & ISR Support

  • ISR-safe send functions: esp_isotp_send() and esp_isotp_send_with_id() can be called from interrupt context
  • Spinlock protection: Core state protected with portMUX locks to prevent race conditions
  • TX frame buffer pool: Pre-allocated memory pool eliminates stack corruption in async TWAI operations

Event-Driven Callbacks

  • Asynchronous notifications: Optional rx_callback and tx_callback in configuration
  • Non-blocking operation: Eliminates need for continuous polling in callback mode

Extended Features

  • 29-bit ID support: Added use_extended_id configuration option
  • Dynamic TX ID: New esp_isotp_send_with_id() function for runtime ID override
  • Enhanced error handling: More specific esp_err_t return codes

API Changes

New Configuration Options

esp_isotp_config_t config = {
    .use_extended_id = false,
    .tx_frame_pool_size = 8,
    .rx_callback = my_rx_callback,
    .tx_callback = my_tx_callback,
    .callback_arg = user_data
};

New Functions

  • esp_err_t esp_isotp_send_with_id(esp_isotp_handle_t handle, uint32_t id, const uint8_t *data, uint32_t size)

Breaking Changes

None. This enhancement maintains full backward compatibility.

Files Modified

  • src/esp_isotp.c: Core implementation with memory management and thread safety
  • inc/esp_isotp.h: API enhancements and documentation
  • README.md: Updated documentation with usage examples

Notes

  • esp_isotp_poll() must still be called periodically for multi-frame message handling
  • RX callbacks execute in ISR context - keep processing minimal
  • TX frame pool prevents memory corruption in async operations

Checklist

  • Component contains README.md
  • Component contains idf_component.yml file with url field defined
  • Component contains unit tests (pytest)

@eternal-echo eternal-echo marked this pull request as ready for review September 5, 2025 05:42
// Set user argument for TWAI operations
// Pre-allocate ISR-safe receive frame buffer to avoid dynamic allocation in interrupt context.
// This buffer is reused for each incoming TWAI frame received in the ISR.
memset(&isotp->isr_rx_frame_buffer, 0, sizeof(esp_isotp_frame_t));

Check warning

Code scanning / clang-tidy

Call to function 'memset' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memset_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] Warning

Call to function 'memset' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memset_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling]
@suda-morris suda-morris requested a review from Copilot September 5, 2025 06:58
Copilot

This comment was marked as outdated.

@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch 3 times, most recently from abf8778 to 3e62e8b Compare September 5, 2025 12:49
@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch from 3e62e8b to 77d10b1 Compare September 5, 2025 13:59
ESP_RETURN_ON_FALSE_ISR(tx_frame != NULL, ISOTP_RET_ERROR, TAG, "No available frames in pool");

// Initialize TWAI frame header and copy payload data into embedded buffer.
memset(&tx_frame->frame, 0, sizeof(twai_frame_t));

Check warning

Code scanning / clang-tidy

Call to function 'memset' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memset_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] Warning

Call to function 'memset' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memset_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling]
@github-actions github-actions bot changed the title feat(isotp): Add thread safety, callbacks, and extended ID support feat(isotp): Add thread safety, callbacks, and extended ID support (IEC-370) Sep 7, 2025
@eternal-echo eternal-echo requested a review from Copilot September 8, 2025 02:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR transforms the esp_isotp component from a basic polling implementation to a production-ready ISO-TP library with thread safety, event-driven callbacks, and extended feature support.

  • Adds thread safety with spinlock protection and ISR-safe send functions
  • Introduces event-driven callbacks (rx_callback, tx_callback) for asynchronous notifications
  • Implements 29-bit extended ID support with automatic format detection

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
esp_isotp/src/esp_isotp.c Core implementation with thread safety, memory management, callback system, and extended ID support
esp_isotp/inc/esp_isotp.h API enhancements with callback types, updated function signatures, and comprehensive documentation
esp_isotp/README.md Updated documentation with usage examples, extended ID configuration, and error handling guidance

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch from 77d10b1 to 2eccc39 Compare September 8, 2025 04:06
@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch 2 times, most recently from b37080a to fc54250 Compare September 10, 2025 04:28
ESP_RETURN_ON_FALSE_ISR(size <= 8, ISOTP_RET_ERROR, TAG, "Invalid TWAI frame size");

// Copy payload into the embedded buffer to ensure data lifetime during async transmission.
memcpy(tx_frame->data_payload, data, size);

Check warning

Code scanning / clang-tidy

Call to function 'memcpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memcpy_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] Warning

Call to function 'memcpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memcpy_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling]
@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch 2 times, most recently from 5a49f7b to 03db9f8 Compare September 10, 2025 05:56
@CLAassistant
Copy link

CLAassistant commented Sep 10, 2025

CLA assistant check
All committers have signed the CLA.

@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch from 9bd1a9f to a8920b1 Compare September 11, 2025 05:32
Copy link
Collaborator

@suda-morris suda-morris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The using of the link_lock to protect all isotpc API is a little bit overkill to me.

@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch from a8920b1 to 223dcd4 Compare September 14, 2025 07:04
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@eternal-echo eternal-echo force-pushed the features/isotp-fix-interface branch from 223dcd4 to 49e973f Compare September 15, 2025 04:52
tx_frame->frame.header.ide = is_extended_id(arbitration_id); // Extended (29-bit) vs Standard (11-bit) ID

// Size validation - TWAI frames are max 8 bytes by protocol
ESP_RETURN_ON_FALSE_ISR(size <= 8, ISOTP_RET_ERROR, TAG, "Invalid TWAI frame size");
Copy link
Collaborator

@suda-morris suda-morris Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check should be moved to the begging of the function. @eternal-echo

@suda-morris suda-morris merged commit f0f25cd into espressif:master Sep 15, 2025
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants