feature modbus tcp master slave v2

This commit is contained in:
Alex Lisitsyn
2024-09-02 03:12:36 +08:00
parent d4345f8cd7
commit 2e2be467f6
109 changed files with 4454 additions and 2319 deletions
+35 -18
View File
@@ -55,36 +55,53 @@ extern "C" {
(void)log_tag; \
if (!(a)) { \
ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
ret = (err_code); \
ret = (err_code); \
goto goto_tag; \
} \
} while (0)
#endif
#define MB_CAT_BUF_SIZE (100)
#define MB_STR_CAT(pref, message) (__extension__( \
{ \
char buf##__FUNCTION__##__LINE__[MB_CAT_BUF_SIZE]; \
strncpy(&(buf##__FUNCTION__##__LINE__)[0], pref, (MB_CAT_BUF_SIZE - 1)); \
strncat((buf##__FUNCTION__##__LINE__), message, (MB_CAT_BUF_SIZE - 1)); \
(&((buf##__FUNCTION__##__LINE__)[0])); \
} \
))
#define MB_OBJ_FMT "%p"
#define MB_OBJ_PTR(inst) (__extension__( \
{ \
assert(inst); \
(((obj_descr_t*)(inst))->parent); \
} \
#define MB_GET_OBJ_CTX(pinst, type, base) (__extension__( \
{ \
assert(pinst); \
((type *)__containerof(pinst, type, base)); \
} \
))
#define MB_BASE2PORT(inst) (__extension__( \
{ \
assert(inst); \
(((mb_base_t *)inst)->port_obj); \
} \
#define MB_OBJ(pinst) (__extension__( \
{ \
assert(pinst); \
((typeof(pinst))(pinst)); \
} \
))
#define CAT_BUF_SIZE (100)
#define MB_STR_CAT(pref, message) (__extension__( \
{ \
static char string_buf[CAT_BUF_SIZE]; \
strncpy(string_buf, pref, (CAT_BUF_SIZE - 1)); \
strncat(string_buf, message, (CAT_BUF_SIZE - 1)); \
} \
#define MB_OBJ_PARENT(pinst) (__extension__( \
{ \
assert(pinst); \
(((obj_descr_t*)(pinst))->parent); \
} \
))
#define MB_BASE2PORT(pinst) (__extension__( \
{ \
assert(pinst); \
assert(((mb_base_t *)pinst)->port_obj); \
(((mb_base_t *)pinst)->port_obj); \
} \
))
typedef struct mb_base_t mb_base_t;
+24 -19
View File
@@ -5,6 +5,7 @@
*/
#pragma once
#include "stdatomic.h"
#include "mb_config.h"
#include "mb_types.h"
@@ -12,6 +13,8 @@
extern "C" {
#endif
#define MB_ATTR_WEAK __attribute__ ((weak))
typedef enum _mb_comm_mode mb_mode_type_t;
#if (CONFIG_FMB_COMM_MODE_ASCII_EN || CONFIG_FMB_COMM_MODE_RTU_EN)
@@ -60,42 +63,44 @@ struct _port_tcp_opts {
mb_addr_type_t addr_type; /*!< Modbus address type */
void *ip_addr_table; /*!< Modbus address or table for connection */
void *ip_netif_ptr; /*!< Modbus network interface */
bool start_disconnected; /*!< do not wait connection to all nodes before polling */
char *dns_name; /*!< Modbus node DNS name */
bool start_disconnected; /*!< (Master only option) do not wait for connection to all nodes before polling */
};
typedef struct _port_tcp_opts mb_tcp_opts_t;
// The common object descriptor struture (common for mb, transport, port objects)
struct _obj_descr {
char *parent_name;
char *obj_name;
void *parent;
uint32_t inst_index;
bool is_master;
char *parent_name; /*!< Name of the parent (base) object */
char *obj_name; /*!< Name of the object */
void *parent; /*!< Pointer to the parent (base) object */
uint32_t inst_index; /*!< The consicutive index of the object instance */
bool is_master; /*!< The current object is master or slave (false) */
};
typedef struct _obj_descr obj_descr_t;
typedef enum _mb_sock_state {
MB_SOCK_STATE_UNDEF = 0x0000,
MB_SOCK_STATE_CLOSED,
MB_SOCK_STATE_READY,
MB_SOCK_STATE_OPENED,
MB_SOCK_STATE_RESOLVED,
MB_SOCK_STATE_CONNECTING,
MB_SOCK_STATE_CONNECTED
MB_SOCK_STATE_UNDEF = 0x0000, /*!< Default init state */
MB_SOCK_STATE_CLOSED, /*!< Node is closed */
MB_SOCK_STATE_READY, /*!< Node is ready for communication */
MB_SOCK_STATE_OPENED, /*!< Node is opened */
MB_SOCK_STATE_RESOLVED, /*!< Node address is resolved */
MB_SOCK_STATE_CONNECTING, /*!< Node connection is in progress */
MB_SOCK_STATE_CONNECTED, /*!< Node is connected */
MB_SOCK_STATE_ACCEPTED /*!< Slave node accepted the connection */
} mb_sock_state_t;
typedef struct _uid_info {
uint16_t index; /*!< index of the address info */
int fd; /*!< slave global FD for VFS (reserved) */
char *node_name_str; /*!< node name string (host name of slave to resolve) */
char *ip_addr_str; /*!< represents the IP address of the slave */
int fd; /*!< node global FD for VFS (reserved) */
char *node_name_str; /*!< node name string (host name of node to resolve) */
char *ip_addr_str; /*!< represents the IP address of the node */
mb_addr_type_t addr_type; /*!< type of IP address */
uint16_t uid; /*!< slave unit ID (UID) field for MBAP frame */
uint16_t port; /*!< slave port number */
uint16_t uid; /*!< node unit ID (UID) field for MBAP frame */
uint16_t port; /*!< node port number */
mb_comm_mode_t proto; /*!< protocol type */
mb_sock_state_t state; /*!< slave state */
_Atomic mb_sock_state_t state; /*!< node state */
void *inst; /*!< pointer to linked instance */
} mb_uid_info_t;
+1 -1
View File
@@ -33,7 +33,7 @@ typedef enum _mb_commands_enum
MB_FUNC_DIAG_GET_COM_EVENT_CNT = ( 11 ),
MB_FUNC_DIAG_GET_COM_EVENT_LOG = ( 12 ),
MB_FUNC_OTHER_REPORT_SLAVEID = ( 17 ),
MB_FUNC_ERROR = ( 128u ),
MB_FUNC_ERROR = ( 128U ),
} mb_commands_t;
/* ----------------------- Type definitions ---------------------------------*/
+1 -1
View File
@@ -133,7 +133,7 @@ typedef enum
MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */
MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */
MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast , then delay sometime.*/
} mb_tmr_mode_enum_t;
} mb_timer_mode_enum_t;
#ifdef __cplusplus
}