Skip to content

Commit d0e7059

Browse files
committed
feat(isotp): add non-blocking send function with customizable TWAI ID
1 parent 7184c51 commit d0e7059

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

esp_isotp/inc/esp_isotp.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ esp_err_t esp_isotp_new_transport(twai_node_handle_t twai_node, const esp_isotp_
7878
*/
7979
esp_err_t esp_isotp_send(esp_isotp_handle_t handle, const uint8_t *data, uint32_t size);
8080

81+
/**
82+
* @brief Send data over an ISO-TP link with specified TWAI ID (non-blocking)
83+
*
84+
* Similar to esp_isotp_send(), but allows specifying a different TWAI ID for transmission.
85+
* This function is primarily used for functional addressing where multiple nodes
86+
* may respond to the same request.
87+
*
88+
* @param handle ISO-TP handle
89+
* @param id TWAI identifier to use for transmission (overrides configured tx_id)
90+
* @param data Data to send
91+
* @param size Data length in bytes
92+
* @return
93+
* - ESP_OK: Send initiated successfully
94+
* - ESP_ERR_NOT_FINISHED: Previous send still in progress
95+
* - ESP_ERR_NO_MEM: Data too large for buffer or no space available
96+
* - ESP_ERR_INVALID_SIZE: Invalid data size
97+
* - ESP_ERR_TIMEOUT: Send operation timed out
98+
* - ESP_ERR_INVALID_ARG: Invalid parameters
99+
* - ESP_FAIL: Other send errors
100+
*/
101+
esp_err_t esp_isotp_send_with_id(esp_isotp_handle_t handle, uint32_t id, const uint8_t *data, uint32_t size);
102+
81103
/**
82104
* @brief Extract a complete received message (non-blocking)
83105
*

esp_isotp/src/esp_isotp.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,34 @@ esp_err_t esp_isotp_send(esp_isotp_handle_t handle, const uint8_t *data, uint32_
213213
}
214214
}
215215

216+
esp_err_t esp_isotp_send_with_id(esp_isotp_handle_t handle, uint32_t id, const uint8_t *data, uint32_t size)
217+
{
218+
ESP_RETURN_ON_FALSE(handle && data && size, ESP_ERR_INVALID_ARG, TAG, "Invalid parameters");
219+
220+
// Validate ID range based on configured format
221+
uint32_t mask = handle->use_extended_id ? TWAI_EXT_ID_MASK : TWAI_STD_ID_MASK;
222+
ESP_RETURN_ON_FALSE((id & ~mask) == 0, ESP_ERR_INVALID_ARG, TAG, "ID exceeds mask for selected format");
223+
224+
int ret = isotp_send_with_id(&handle->link, id, data, size);
225+
switch (ret) {
226+
case ISOTP_RET_OK:
227+
return ESP_OK;
228+
case ISOTP_RET_INPROGRESS:
229+
return ESP_ERR_NOT_FINISHED;
230+
case ISOTP_RET_OVERFLOW:
231+
case ISOTP_RET_NOSPACE:
232+
return ESP_ERR_NO_MEM;
233+
case ISOTP_RET_LENGTH:
234+
return ESP_ERR_INVALID_SIZE;
235+
case ISOTP_RET_TIMEOUT:
236+
return ESP_ERR_TIMEOUT;
237+
case ISOTP_RET_ERROR:
238+
default:
239+
ESP_LOGE(TAG, "ISO-TP send with ID failed with error code: %d", ret);
240+
return ESP_FAIL;
241+
}
242+
}
243+
216244
esp_err_t esp_isotp_receive(esp_isotp_handle_t handle, uint8_t *data, uint32_t size, uint32_t *received_size)
217245
{
218246
ESP_RETURN_ON_FALSE(handle && data && size && received_size, ESP_ERR_INVALID_ARG, TAG, "Invalid parameters");

0 commit comments

Comments
 (0)