Skip to content

Commit de19b81

Browse files
committed
fix master FSM error handling issue
1 parent 2f2b58f commit de19b81

File tree

6 files changed

+40
-33
lines changed

6 files changed

+40
-33
lines changed

examples/serial/mb_serial_master/main/serial_master.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ static void *master_get_param_data(const mb_parameter_descriptor_t *param_descri
272272
if (descr->param_opts.opt3) { \
273273
for EACH_ITEM(inst, descr->param_size / sizeof(*item_ptr)) { \
274274
if (*item_ptr != (typeof(*(inst)))descr->param_opts.opt3) { \
275+
mbc_master_lock(handle); \
275276
*item_ptr = (typeof(*(inst)))descr->param_opts.opt3; \
277+
mbc_master_unlock(handle); \
276278
ESP_LOGD(TAG, "Characteristic #%d (%s), initialize to 0x%" PRIx16 ".", \
277279
(int)descr->cid, \
278280
(char *)descr->param_key, \
@@ -567,24 +569,11 @@ static esp_err_t master_init(void)
567569
return err;
568570
}
569571

570-
#include "esp_heap_trace.h"
571-
572-
#define NUM_RECORDS 100
573-
static heap_trace_record_t trace_record[NUM_RECORDS];
574-
575572
void app_main(void)
576573
{
577574
// Initialization of device peripheral and objects
578575
ESP_ERROR_CHECK(master_init());
579576
vTaskDelay(10);
580577

581-
ESP_ERROR_CHECK( heap_trace_init_standalone(trace_record, NUM_RECORDS) );
582-
583-
ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
584-
585578
master_operation_func(NULL);
586-
587-
ESP_ERROR_CHECK( heap_trace_stop() );
588-
heap_trace_dump();
589-
590579
}

examples/tcp/mb_tcp_master/main/tcp_master.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ static void *master_get_param_data(const mb_parameter_descriptor_t *param_descri
411411
if (descr->param_opts.opt3) { \
412412
for EACH_ITEM(inst, descr->param_size / sizeof(*item_ptr)) { \
413413
if (*item_ptr != (typeof(*(inst)))descr->param_opts.opt3) { \
414+
mbc_master_lock(handle); \
414415
*item_ptr = (typeof(*(inst)))descr->param_opts.opt3; \
416+
mbc_master_lock(handle); \
415417
ESP_LOGD(TAG, "Characteristic #%d (%s), initialize to 0x%" PRIx16 ".", \
416418
(int)descr->cid, \
417419
(char *)descr->param_key, \

modbus/mb_objects/common/mb_types.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ typedef mb_exception_t (*mb_fn_handler_fp)(void *, uint8_t *frame_ptr, uint16_t
105105
* \brief Error event type
106106
*/
107107
typedef enum mb_err_event_enum {
108-
EV_ERROR_INIT, /*!< No error, initial state. */
109-
EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
110-
EV_ERROR_RECEIVE_DATA, /*!< Receive frame data error. */
111-
EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
112-
EV_ERROR_OK /*!< No error, processing completed. */
108+
EV_ERROR_INIT, /*!< No error, initial state. */
109+
EV_ERROR_RESPOND_TIMEOUT = 0x01, /*!< Slave respond timeout. */
110+
EV_ERROR_RECEIVE_DATA = 0x02, /*!< Receive frame data error. */
111+
EV_ERROR_EXECUTE_FUNCTION = 0x04, /*!< Execute function error. */
112+
EV_ERROR_OK = 0x08 /*!< No error, processing completed. */
113113
} mb_err_event_t;
114114

115115
typedef struct mb_event_s {

modbus/mb_objects/mb_master.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mb_err_enum_t mbm_get_handler_count(mb_base_t *inst, uint16_t *count)
9898
return MB_ENOERR;
9999
}
100100

101-
static mb_err_enum_t mbm_check_invoke_handler(mb_base_t *inst, uint8_t func_code, uint8_t *buf, uint16_t *len)
101+
static mb_exception_t mbm_check_invoke_handler(mb_base_t *inst, uint8_t func_code, uint8_t *buf, uint16_t *len)
102102
{
103103
mbm_object_t *mbm_obj = MB_GET_OBJ_CTX(inst, mbm_object_t, base);
104104
mb_exception_t exception = MB_EX_ILLEGAL_FUNCTION;
@@ -594,10 +594,8 @@ mb_err_enum_t mbm_poll(mb_base_t *inst)
594594
}
595595
} else {
596596
// Ignore the `EV_FRAME_RECEIVED` event because the respond timeout occurred
597-
// and this is likely respond to previous transaction
597+
// and this is likely respond to previous transaction.
598598
ESP_LOGE(TAG, MB_OBJ_FMT", drop data received outside of transaction.", MB_OBJ_PARENT(inst));
599-
mb_port_event_set_err_type(MB_OBJ(inst->port_obj), EV_ERROR_RESPOND_TIMEOUT);
600-
(void)mb_port_event_post(MB_OBJ(inst->port_obj), EVENT(EV_ERROR_PROCESS));
601599
}
602600
break;
603601

modbus/mb_ports/common/port_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ bool mb_port_event_post(mb_port_base_t *inst, mb_event_t event);
165165
bool mb_port_event_get(mb_port_base_t *inst, mb_event_t *event);
166166
bool mb_port_event_res_take(mb_port_base_t *inst, uint32_t timeout);
167167
void mb_port_event_res_release(mb_port_base_t *inst);
168-
void mb_port_event_set_resp_flag(mb_port_base_t *inst, mb_event_enum_t event_mask);
168+
void mb_port_event_set_resp_flag(mb_port_base_t *inst, mb_err_event_t event_mask);
169169
void mb_port_event_set_err_type(mb_port_base_t *inst, mb_err_event_t event);
170170
mb_err_event_t mb_port_event_get_err_type(mb_port_base_t *inst);
171171
void mb_port_event_delete(mb_port_base_t *inst);

modbus/mb_ports/common/port_event.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,37 +161,55 @@ void mb_port_event_res_release(mb_port_base_t *inst)
161161
}
162162
}
163163

164-
void mb_port_event_set_resp_flag(mb_port_base_t *inst, mb_event_enum_t event_mask)
164+
void mb_port_event_set_resp_flag(mb_port_base_t *inst, mb_err_event_t event_mask)
165165
{
166166
MB_RETURN_ON_FALSE((inst), ;, TAG, "incorrect object handle.");
167-
(void)xEventGroupSetBits(inst->event_obj->event_group_hdl, event_mask);
167+
(void)xEventGroupSetBits(inst->event_obj->event_group_hdl, (EventBits_t)event_mask);
168168
}
169169

170170
mb_err_enum_t mb_port_event_wait_req_finish(mb_port_base_t *inst)
171171
{
172172
MB_RETURN_ON_FALSE((inst), MB_EINVAL, TAG,
173173
"incorrect object handle.");
174-
mb_err_enum_t err_status = MB_ENOERR;
175-
mb_event_enum_t rcv_event;
176-
EventBits_t bits = xEventGroupWaitBits(inst->event_obj->event_group_hdl,// The event group being tested.
177-
MB_EVENT_REQ_MASK, // The bits within the event group to wait for.
178-
pdTRUE, // Masked bits should be cleared before returning.
179-
pdFALSE, // Don't wait for both bits, either bit will do.
180-
MB_EVENT_QUEUE_TIMEOUT_MAX);// Wait forever for either bit to be set.
181-
rcv_event = (mb_event_enum_t)(bits);
174+
mb_err_enum_t err_status = MB_ETIMEDOUT;
175+
mb_err_event_t rcv_event;
176+
EventBits_t bits = EV_ERROR_INIT;
177+
bits = xEventGroupWaitBits(inst->event_obj->event_group_hdl, // The event group being tested.
178+
MB_EVENT_REQ_MASK, // The bits within the event group to wait for.
179+
pdTRUE, // Masked bits should be cleared before returning.
180+
pdFALSE, // Don't wait for both bits, either bit will do.
181+
MB_EVENT_QUEUE_TIMEOUT_MAX); // Wait forever for either bit to be set.
182+
rcv_event = (mb_err_event_t)(bits);
182183
if (rcv_event) {
183184
ESP_LOGD(TAG, "%s, %s: returned event = 0x%x", inst->descr.parent_name, __func__, (int)rcv_event);
184185
if (!(rcv_event & MB_EVENT_REQ_MASK)) {
185186
// if we wait for certain event bits but get from poll subset
186187
ESP_LOGE(TAG, "%s, %s: incorrect event set = 0x%x", inst->descr.parent_name, __func__, (int)rcv_event);
187188
}
188189
if (MB_PORT_CHECK_EVENT(rcv_event, EV_ERROR_OK)) {
190+
// Just to check if abnormal state is detected (multiple errors are active). Should not happen in normal FSM handling.
191+
if (MB_PORT_CHECK_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_RESPOND_TIMEOUT | EV_ERROR_EXECUTE_FUNCTION))) {
192+
ESP_LOGD(TAG, "%s, %s: multiple errors detected? = 0x%x, clear.", inst->descr.parent_name, __func__, (int)rcv_event);
193+
MB_PORT_CLEAR_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_RESPOND_TIMEOUT | EV_ERROR_EXECUTE_FUNCTION));
194+
}
189195
err_status = MB_ENOERR;
190196
} else if (MB_PORT_CHECK_EVENT(rcv_event, EV_ERROR_RESPOND_TIMEOUT)) {
197+
if (MB_PORT_CHECK_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_OK | EV_ERROR_EXECUTE_FUNCTION))) {
198+
ESP_LOGD(TAG, "%s, %s: multiple errors detected? = 0x%x, clear.", inst->descr.parent_name, __func__, (int)rcv_event);
199+
MB_PORT_CLEAR_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_OK | EV_ERROR_EXECUTE_FUNCTION));
200+
}
191201
err_status = MB_ETIMEDOUT;
192202
} else if (MB_PORT_CHECK_EVENT(rcv_event, EV_ERROR_RECEIVE_DATA)) {
203+
if (MB_PORT_CHECK_EVENT(rcv_event, (EV_ERROR_RESPOND_TIMEOUT | EV_ERROR_OK | EV_ERROR_EXECUTE_FUNCTION))) {
204+
ESP_LOGD(TAG, "%s, %s: multiple errors detected? = 0x%x, clear.", inst->descr.parent_name, __func__, (int)rcv_event);
205+
MB_PORT_CLEAR_EVENT(rcv_event, (EV_ERROR_RESPOND_TIMEOUT | EV_ERROR_OK | EV_ERROR_EXECUTE_FUNCTION));
206+
}
193207
err_status = MB_ERECVDATA;
194208
} else if (MB_PORT_CHECK_EVENT(rcv_event, EV_ERROR_EXECUTE_FUNCTION)) {
209+
if (MB_PORT_CHECK_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_OK | EV_ERROR_RESPOND_TIMEOUT))) {
210+
ESP_LOGD(TAG, "%s, %s: multiple errors detected? = 0x%x, clear.", inst->descr.parent_name, __func__, (int)rcv_event);
211+
MB_PORT_CLEAR_EVENT(rcv_event, (EV_ERROR_RECEIVE_DATA | EV_ERROR_OK | EV_ERROR_RESPOND_TIMEOUT));
212+
}
195213
err_status = MB_EILLFUNC;
196214
}
197215
} else {

0 commit comments

Comments
 (0)