mb master, slave add custom fc handlers

This commit is contained in:
aleks committed 2025-03-10 16:09:03 +01:00
1 parent a54f11355c
commit fc8fc06fc5
23 files changed
+438 -84

No files matched your search

@@ -732,3 +732,33 @@ esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param
}
return err;
}
/**
* Register or override command handler for the command in master command handler table
*/
esp_err_t mbc_master_set_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp phandler)
{
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
"Master interface is not correctly initialized.");
mbm_controller_iface_t *mbm_controller = MB_MASTER_GET_IFACE(ctx);
mb_base_t *pmb_obj = (mb_base_t *)mbm_controller->mb_base;
MB_RETURN_ON_FALSE((pmb_obj && pmb_obj->descr.is_master), ESP_ERR_INVALID_STATE, TAG,
"Master interface is not correctly initialized.");
mb_err_enum_t ret = mbm_set_handler(func_code, phandler);
return MB_ERR_TO_ESP_ERR(ret);
}
/**
* Get command master command handler from the slave command handler table
*/
esp_err_t mbc_master_get_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp *phandler)
{
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
"Master interface is not correctly initialized.");
mbm_controller_iface_t *mbm_controller = MB_MASTER_GET_IFACE(ctx);
mb_base_t *pmb_obj = (mb_base_t *)mbm_controller->mb_base;
MB_RETURN_ON_FALSE((pmb_obj && pmb_obj->descr.is_master), ESP_ERR_INVALID_STATE, TAG,
"Master interface is not correctly initialized.");
mb_err_enum_t ret = mbm_get_handler(func_code, phandler);
return MB_ERR_TO_ESP_ERR(ret);
}
@@ -547,3 +547,33 @@ mb_err_enum_t mbc_reg_discrete_slave_cb(mb_base_t *inst, uint8_t *reg_buffer, ui
}
return status;
}
/**
* Register or override command handler for the command in slave command handler table
*/
esp_err_t mbc_slave_set_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp phandler)
{
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
"Slave interface is not correctly initialized.");
mbs_controller_iface_t *mbs_controller = MB_SLAVE_GET_IFACE(ctx);
mb_base_t *pmb_obj = (mb_base_t *)mbs_controller->mb_base;
MB_RETURN_ON_FALSE((pmb_obj && !pmb_obj->descr.is_master), ESP_ERR_INVALID_STATE, TAG,
"Slave interface is not correctly initialized.");
mb_err_enum_t ret = mbs_set_handler(func_code, phandler);
return MB_ERR_TO_ESP_ERR(ret);
}
/**
* Get slave command handler from the slave command handler table
*/
esp_err_t mbc_slave_get_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp *phandler)
{
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
"Slave interface is not correctly initialized.");
mbs_controller_iface_t *mbs_controller = MB_SLAVE_GET_IFACE(ctx);
mb_base_t *pmb_obj = (mb_base_t *)mbs_controller->mb_base;
MB_RETURN_ON_FALSE((pmb_obj && !pmb_obj->descr.is_master), ESP_ERR_INVALID_STATE, TAG,
"Slave interface is not correctly initialized.");
mb_err_enum_t ret = mbs_get_handler(func_code, phandler);
return MB_ERR_TO_ESP_ERR(ret);
}
@@ -201,6 +201,27 @@ typedef union
typedef esp_err_t (*iface_create_fp)(mb_communication_info_t*, void **); /*!< Interface method create */
typedef esp_err_t (*iface_method_default_fp)(void *ctx); /*!< Interface method default prototype */
/**
* @brief The function registers the new function handler for specified command and
* allows to override the existing one for the communication object.
* If the phandler == 0, the function allows to clear the actual function handler for the `func_code` parameter.
*
* @param[in] ctx context pointer to the communication object
* @param[in] func_code the function code for the handler
* @param[in] phandler the pointer to function handler being used for command
*
* @return
* - esp_err_t ESP_OK - the function handler is correctly set
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
* - esp_err_t ESP_ERR_NOT_FOUND - the requested slave is not found (not connected or not configured)
* - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
*/
esp_err_t mbc_register_cb(void *ctx, uint8_t func_code, mb_fn_handler_fp phandler);
#ifdef __cplusplus
}
#endif
@@ -466,6 +466,38 @@ mb_err_enum_t mbc_reg_coils_master_cb(mb_base_t *inst, uint8_t *reg_buffer, uint
*/
esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param_type, size_t param_size);
/**
* @brief The function registers the new function handler for specified command
* and allows to override the existing one for the master object.
* If the phandler == NULL, the function allows to reset the actual function handler
* for the `func_code` parameter.
*
* @param[in] ctx context pointer to the master controller object
* @param[in] func_code the function code for the handler
* @param[in] phandler the pointer to function handler being used for command
*
* @return
* - esp_err_t ESP_OK - the function handler is correctly set
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
* - esp_err_t ESP_ERR_NOT_FOUND - the requested slave is not found (not connected or not configured)
*/
esp_err_t mbc_master_set_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp phandler);
/**
* @brief The function gets function handler for specified command from master command handler table.
*
* @param[in] ctx context pointer to the master controller object
* @param[in] func_code the function code for the handler
* @param[out] phandler the pointer to function handler being returned
*
* @return
* - esp_err_t ESP_OK - the function handler is returned in the
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
*/
esp_err_t mbc_master_get_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp *phandler);
#ifdef __cplusplus
}
#endif
@@ -292,6 +292,37 @@ mb_err_enum_t mbc_reg_discrete_slave_cb(mb_base_t *inst, uint8_t *reg_buffer, ui
*/
mb_err_enum_t mbc_reg_coils_slave_cb(mb_base_t *inst, uint8_t *reg_buffer, uint16_t address, uint16_t n_coils, mb_reg_mode_enum_t mode) __attribute__ ((weak));
/**
* @brief The function registers the new function handler for specified command
* and allows to override the existing one for the slave object.
* If the phandler == NULL, the function allows to reset the actual function handler
* for the `func_code` parameter.
*
* @param[in] ctx context pointer to the slave controller object
* @param[in] func_code the function code for the handler
* @param[in] phandler the pointer to function handler being used for command
*
* @return
* - esp_err_t ESP_OK - the function handler is correctly set
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
*/
esp_err_t mbc_slave_set_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp phandler);
/**
* @brief The function gets function handler for specified command from slave command handler table.
*
* @param[in] ctx context pointer to the slave controller object
* @param[in] func_code the function code for the handler
* @param[out] phandler the pointer to function handler being returned
*
* @return
* - esp_err_t ESP_OK - the function handler is returned in the
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
*/
esp_err_t mbc_slave_get_handler(void *ctx, uint8_t func_code, mb_fn_handler_fp *phandler);
#ifdef __cplusplus
}
#endif
+1
View File
@@ -18,6 +18,7 @@ extern "C" {
#include "mb_common.h" // for mb_base_t
#include "esp_modbus_slave.h" // for public type defines
#include "mb_slave.h"
/* ----------------------- Defines ------------------------------------------*/
#define MB_INST_MIN_SIZE (1) // The minimal size of Modbus registers area in bytes
@@ -256,9 +256,19 @@ static esp_err_t mbc_serial_master_send_request(void *ctx, mb_param_request_t *r
break;
#endif
default:
ESP_LOGE(TAG, "%s: Incorrect or unsupported function in request (%u) ",
__FUNCTION__, mb_command);
mb_error = MB_ENOREG;
mb_fn_handler_fp phandler = NULL;
// check registered function handler
mb_error = mbm_get_handler(mb_command, &phandler);
if (mb_error == MB_ENOERR) {
// send the request for custom command
mb_error = mbm_rq_custom(mbm_controller_iface->mb_base, mb_slave_addr, mb_command,
data_ptr, (uint16_t)(mb_size << 1),
pdMS_TO_TICKS(MB_MAX_RESP_DELAY_MS));
ESP_LOGD(TAG, "%s: Send custom request (%u), error = (0x%d) ", __FUNCTION__, mb_command, (int)mb_error);
} else {
ESP_LOGE(TAG, "%s: Incorrect or unsupported function in request (%u) ", __FUNCTION__, mb_command);
mb_error = MB_ENOREG;
}
break;
}
} else {
+13 -3
View File
@@ -255,9 +255,19 @@ static esp_err_t mbc_tcp_master_send_request(void *ctx, mb_param_request_t *requ
break;
#endif
default:
ESP_LOGE(TAG, "%s: Incorrect or unsupported function in request (%u) ",
__FUNCTION__, (unsigned)mb_command);
mb_error = MB_ENOREG;
mb_fn_handler_fp phandler = NULL;
// check registered function handler
mb_error = mbm_get_handler(mb_command, &phandler);
if (mb_error == MB_ENOERR) {
// send the request for custom command
mb_error = mbm_rq_custom(mbm_controller_iface->mb_base, mb_slave_addr, mb_command,
data_ptr, (uint16_t)(mb_size << 1),
pdMS_TO_TICKS(MB_MAX_RESP_DELAY_MS));
ESP_LOGD(TAG, "%s: Send custom request (%u), error = (0x%d) ", __FUNCTION__, mb_command, (int)mb_error);
} else {
ESP_LOGE(TAG, "%s: Incorrect or unsupported function in request (%u) ", __FUNCTION__, mb_command);
mb_error = MB_ENOREG;
}
break;
}
} else {