Agile Modbus  1.1.4
Lightweight modbus protocol stack.
agile_modbus_tcp.c
Go to the documentation of this file.
1 
14 #include "agile_modbus.h"
15 
16 #if AGILE_MODBUS_USING_TCP
17 
18 #include "agile_modbus_tcp.h"
19 
34 static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
35 {
36  ctx->slave = slave;
37  return 0;
38 }
39 
50  int addr, int nb,
51  uint8_t *req)
52 {
53  agile_modbus_tcp_t *ctx_tcp = ctx->backend_data;
54 
55  /* Increase transaction ID */
56  if (ctx_tcp->t_id < UINT16_MAX)
57  ctx_tcp->t_id++;
58  else
59  ctx_tcp->t_id = 0;
60  req[0] = ctx_tcp->t_id >> 8;
61  req[1] = ctx_tcp->t_id & 0x00ff;
62 
63  /* Protocol Modbus */
64  req[2] = 0;
65  req[3] = 0;
66 
67  /* Length will be defined later by set_req_length_tcp at offsets 4
68  and 5 */
69 
70  req[6] = ctx->slave;
71  req[7] = function;
72  req[8] = addr >> 8;
73  req[9] = addr & 0x00ff;
74  req[10] = nb >> 8;
75  req[11] = nb & 0x00ff;
76 
78 }
79 
87 {
88  /* Extract from MODBUS Messaging on TCP/IP Implementation
89  Guide V1.0b (page 23/46):
90  The transaction identifier is used to associate the future
91  response with the request. */
92  rsp[0] = sft->t_id >> 8;
93  rsp[1] = sft->t_id & 0x00ff;
94 
95  /* Protocol Modbus */
96  rsp[2] = 0;
97  rsp[3] = 0;
98 
99  /* Length will be set later by send_msg (4 and 5) */
100 
101  /* The slave ID is copied from the indication */
102  rsp[6] = sft->slave;
103  rsp[7] = sft->function;
104 
106 }
107 
114 static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
115 {
116  (void)req_length;
117  return (req[0] << 8) + req[1];
118 }
119 
126 static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
127 {
128  /* Substract the header length to the message length */
129  int mbap_length = req_length - 6;
130 
131  req[4] = mbap_length >> 8;
132  req[5] = mbap_length & 0x00FF;
133 
134  return req_length;
135 }
136 
144 static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
145 {
146  (void)ctx;
147  (void)msg;
148  return msg_length;
149 }
150 
159 static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
160  const uint8_t *rsp, int rsp_length)
161 {
162  (void)ctx;
163  (void)rsp_length;
164  /* Check transaction ID */
165  if (req[0] != rsp[0] || req[1] != rsp[1])
166  return -1;
167 
168  /* Check protocol ID */
169  if (rsp[2] != 0x0 && rsp[3] != 0x0)
170  return -1;
171 
172  return 0;
173 }
174 
187  {
199 
217 int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
218 {
219  agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
221  ctx->_ctx.backend_data = ctx;
222 
223  ctx->t_id = 0;
224 
225  return 0;
226 }
227 
236 #endif /* AGILE_MODBUS_USING_TCP */
Agile Modbus software package common header file.
Agile Modbus package TCP header file.
void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
initialize modbus handle
Definition: agile_modbus.c:257
@ AGILE_MODBUS_BACKEND_TYPE_TCP
TCP.
Definition: agile_modbus.h:166
#define AGILE_MODBUS_TCP_CHECKSUM_LENGTH
#define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH
#define AGILE_MODBUS_TCP_MAX_ADU_LENGTH
#define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH
#define AGILE_MODBUS_TCP_HEADER_LENGTH
int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
TCP initialization.
static const agile_modbus_backend_t agile_modbus_tcp_backend
TCP backend interface.
static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
TCP pre-check confirmation interface (compare transaction identifier and protocol identifier)
static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
TCP set address interface.
static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
TCP check receiving data integrity interface.
static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
TCP pre-send data interface (calculate the value of the length field and store it)
static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
TCP builds the basic request message interface (header message)
static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
TCP builds a basic response message interface (header message)
static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
TCP ready response interface.
Agile Modbus backend interface structure.
Definition: agile_modbus.h:198
contains the modbus header parameter structure
Definition: agile_modbus.h:187
int slave
slave address
Definition: agile_modbus.h:188
int t_id
Transaction identifier.
Definition: agile_modbus.h:190
int function
function code
Definition: agile_modbus.h:189
TCP structure.
agile_modbus_t _ctx
modbus handle
uint16_t t_id
Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b (page 23/46): The transaction iden...
Agile Modbus structure.
Definition: agile_modbus.h:217
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
int slave
slave address
Definition: agile_modbus.h:218
void * backend_data
Backend data, pointing to RTU or TCP structure.
Definition: agile_modbus.h:228