feature add custom fc handlers for master and slave v2

This commit is contained in:
Alex Lisitsyn committed 2025-04-03 18:52:24 +08:00
1 parent 439c767aed
commit 8215e3d0d2
51 files changed
+1345 -300

No files matched your search

@@ -0,0 +1,110 @@
/*
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mb_common.h"
#include "mb_proto.h"
/* ----------------------- Defines ------------------------------------------*/
#define MB_IS_VALID_FUNC_CODE(fc) ((fc) >= MB_FUNC_CODE_MIN && (fc) <= MB_FUNC_CODE_MAX)
static const char TAG[] __attribute__((unused)) = "MB_FUNC_HANDLING";
mb_err_enum_t mb_set_handler(handler_descriptor_t *pdescriptor, uint8_t func_code, mb_fn_handler_fp phandler)
{
MB_RETURN_ON_FALSE((pdescriptor && phandler && pdescriptor->instance), MB_EINVAL, TAG, "invalid arguments.");
MB_RETURN_ON_FALSE(MB_IS_VALID_FUNC_CODE(func_code), MB_EINVAL, TAG,
"invalid function code (0x%x)", (int)func_code);
mb_command_entry_t *pitem = NULL;
LIST_FOREACH(pitem, &pdescriptor->head, entries) {
if (pitem && pitem->func_code == func_code) {
// The handler for the function already exists, rewrite it.
pitem->handler = phandler;
ESP_LOGD(TAG, "Inst: %p, set handler: 0x%x, %p", pdescriptor->instance, pitem->func_code, pitem->handler);
return MB_ENOERR;
}
}
// Insert new handler entry into list
if (pdescriptor->count >= MB_FUNC_HANDLERS_MAX) {
return MB_ENORES;
} else {
pdescriptor->count += 1;
}
pitem = (mb_command_entry_t *) heap_caps_malloc(sizeof(mb_command_entry_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
MB_RETURN_ON_FALSE(pitem, MB_ENORES, TAG, "mb can not allocate memory for command handler 0x%x.", func_code);
pitem->func_code = func_code;
pitem->handler = phandler;
LIST_INSERT_HEAD(&pdescriptor->head, pitem, entries);
ESP_LOGD(TAG, "Inst: %p, add handler: 0x%x, %p", pdescriptor->instance, pitem->func_code, pitem->handler);
return MB_ENOERR;
}
mb_err_enum_t mb_get_handler(handler_descriptor_t *pdescriptor, uint8_t func_code, mb_fn_handler_fp *phandler)
{
MB_RETURN_ON_FALSE((pdescriptor && phandler && pdescriptor->instance), MB_EINVAL, TAG, "invalid arguments.");
MB_RETURN_ON_FALSE(MB_IS_VALID_FUNC_CODE(func_code), MB_EINVAL, TAG,
"invalid function code (0x%x)", (int)func_code);
mb_command_entry_t *pitem = NULL;
LIST_FOREACH(pitem, &pdescriptor->head, entries) {
if (pitem && pitem->func_code == func_code) {
*phandler = pitem->handler;
ESP_LOGD(TAG, "Inst: %p, get handler: 0x%x, %p", pdescriptor->instance, pitem->func_code, pitem->handler);
return MB_ENOERR;
}
}
return MB_ENORES;
}
// Helper function to get handler
mb_err_enum_t mb_delete_handler(handler_descriptor_t *pdescriptor, uint8_t func_code)
{
MB_RETURN_ON_FALSE((pdescriptor && pdescriptor->instance), MB_EINVAL, TAG, "invalid arguments.");
MB_RETURN_ON_FALSE(MB_IS_VALID_FUNC_CODE(func_code), MB_EINVAL, TAG,
"invalid function code (0x%x)", (int)func_code);
if (LIST_EMPTY(&pdescriptor->head)) {
return MB_EINVAL;
}
mb_command_entry_t *pitem = NULL;
mb_command_entry_t *ptemp = NULL;
LIST_FOREACH_SAFE(pitem, &pdescriptor->head, entries, ptemp) {
if (pitem && pitem->func_code == func_code) {
ESP_LOGD(TAG, "Inst: %p, remove handler: 0x%x, %p", pdescriptor->instance, pitem->func_code, pitem->handler);
LIST_REMOVE(pitem, entries);
free(pitem);
if (pdescriptor->count) {
pdescriptor->count--;
}
return MB_ENOERR;
}
}
return MB_ENORES;
}
// Helper function to close all registered handlers in the list
mb_err_enum_t mb_delete_command_handlers(handler_descriptor_t *pdescriptor)
{
MB_RETURN_ON_FALSE((pdescriptor), MB_EINVAL, TAG, "invalid arguments.");
if (LIST_EMPTY(&pdescriptor->head)) {
return MB_EINVAL;
}
mb_command_entry_t *pitem = NULL;
while ((pitem = LIST_FIRST(&pdescriptor->head))) {
ESP_LOGD(TAG, "Inst: %p, close handler: 0x%x, %p", pdescriptor->instance, pitem->func_code, pitem->handler);
LIST_REMOVE(pitem, entries);
free(pitem);
if (pdescriptor->count) {
pdescriptor->count--;
}
}
return MB_ENOERR;
}
+33
View File
@@ -44,6 +44,39 @@
/* ----------------------- Start implementation -----------------------------*/
mb_exception_t mb_error_to_exception(mb_err_enum_t error_code);
/**
* This function will request read coil.
*
* @param uid slave address
* @param fc custom function code
* @param buf additional data to send
* @param buf_size size of data to send
* @param timeout timeout (-1 will waiting forever)
*
* @return error code (mb_err_enum_t)
*/
mb_err_enum_t mbm_rq_custom(mb_base_t *inst, uint8_t uid, uint8_t fc, uint8_t *buf, uint16_t buf_size, uint32_t tout)
{
uint8_t *mb_frame_ptr;
if (!buf || (uid > MB_ADDRESS_MAX) || (buf_size >= (MB_BUFFER_SIZE - 2))) {
return MB_EINVAL;
}
if (!mb_port_event_res_take(inst->port_obj, tout)) {
return MB_EBUSY;
}
inst->get_send_buf(inst, &mb_frame_ptr);
inst->set_dest_addr(inst, uid);
mb_frame_ptr[MB_PDU_FUNC_OFF] = fc;
memcpy(&mb_frame_ptr[MB_PDU_DATA_OFF], buf, buf_size);
inst->set_send_len(inst, MB_PDU_SIZE_MIN + buf_size);
(void)mb_port_event_post(inst->port_obj, EVENT(EV_FRAME_TRANSMIT | EV_TRANS_START));
return mb_port_event_wait_req_finish(inst->port_obj);
}
mb_err_enum_t mbm_rq_report_slave_id(mb_base_t *inst, uint8_t slave_addr, uint32_t timeout)
{
uint8_t *mb_frame_ptr = NULL;
+4 -5
View File
@@ -28,10 +28,11 @@
*
* File: $Id: mbutils.c, v 1.6 2007/02/18 23:49:07 wolti Exp $
*/
#include <mb_common.h>
#include <mb_proto.h>
#include "mb_common.h"
#include "mb_proto.h"
/* ----------------------- Defines ------------------------------------------*/
#define BITS_uint8_t 8U
#define BITS_uint8_t (8U)
static const char TAG[] __attribute__((unused)) = "MB_UTILS";
/* ----------------------- Start implementation -----------------------------*/
void mb_util_set_bits(uint8_t *byte_buf, uint16_t bit_offset, uint8_t but_num, uint8_t value)
@@ -125,5 +126,3 @@ mb_exception_t mb_error_to_exception(mb_err_enum_t error_code)
return status;
}