GaIA BIOS Library
Loading...
Searching...
No Matches
modbus_tcp_master_utils.h File Reference

Modbus/TCP master common utility functions. More...

Functions

mb_tcp_transaction_tmb_tcp_get_transaction_by_id (mb_tcp_master_ch_t *ch, uni16_t id)
 Returns the transaction with the given id.
mb_tcp_master_ch_tmb_tcp_get_ch_by_socket (int sock)
 Returns the master channel corresponding to the given socket.
int mb_tcp_register_master_ch (mb_tcp_master_ch_t *ch)
 Registers a master channel.
int mb_tcp_unregister_master_ch (mb_tcp_master_ch_t *ch)
 Unregisters a master channel.
int mb_tcp_store_response (mb_tcp_master_ch_t *ch, const uint8_t *buf, uint32_t len)
 Stores a response in the pending transaction corresponding to the transaction id present in the specified buffer.
void mb_tcp_finalize_transaction (mb_tcp_transaction_t *trans)
 Finalizes the specified transaction copying the received data in the output buffer specified by the user when the transaction was started.
void mb_tcp_fire_transaction_event (mb_tcp_master_ch_t *ch, mb_tcp_transaction_t *trans, int event)
 Fires the specified transaction event.

Detailed Description

Modbus/TCP master common utility functions.

These functions should be used in the implementation of the Modbus/TCP master protocol for a specific platform.

Author
Tecnologix, 2021

Function Documentation

◆ mb_tcp_finalize_transaction()

void mb_tcp_finalize_transaction ( mb_tcp_transaction_t * trans)

Finalizes the specified transaction copying the received data in the output buffer specified by the user when the transaction was started.

Data is copied restoring the correct byte order for register read operations.

Parameters
transtransaction to be finalized.

◆ mb_tcp_fire_transaction_event()

void mb_tcp_fire_transaction_event ( mb_tcp_master_ch_t * ch,
mb_tcp_transaction_t * trans,
int event )

Fires the specified transaction event.

The callback provided by the user when the transaction was started is called by this function.

Parameters
chmaster channel.
transtransaction for which the event has been generated.
eventtransaction event. It can be MB_TRANS_EVENT_COMPLETED if the transaction was successfully completed or MB_TRANS_EVENT_TIMEOUT if a timeout occurred.

◆ mb_tcp_get_ch_by_socket()

mb_tcp_master_ch_t * mb_tcp_get_ch_by_socket ( int sock)

Returns the master channel corresponding to the given socket.

This function is used to get a specific channel when the only available information is the socket (e.g. in TCP/IP stack callbacks).

Parameters
socksocket.
Returns
the registered master channel associated with the specified socket or NULL if it does not exist.

◆ mb_tcp_get_transaction_by_id()

mb_tcp_transaction_t * mb_tcp_get_transaction_by_id ( mb_tcp_master_ch_t * ch,
uni16_t id )

Returns the transaction with the given id.

Parameters
chmaster channel.
idtransaction id.
Returns
the transaction with the specified id or NULL if it does not exist.

◆ mb_tcp_register_master_ch()

int mb_tcp_register_master_ch ( mb_tcp_master_ch_t * ch)

Registers a master channel.

This function should be called when the channel initialization is complete and it must be invoked if mb_tcp_get_ch_by_socket is needed.

Parameters
chmaster channel.
Returns
MB_TCP_NO_ERROR if the channel was successfully registered MB_TCP_ALLOC_ERROR otherwise.

◆ mb_tcp_store_response()

int mb_tcp_store_response ( mb_tcp_master_ch_t * ch,
const uint8_t * buf,
uint32_t len )

Stores a response in the pending transaction corresponding to the transaction id present in the specified buffer.

Parameters
chmaster channel.
bufdata received. It is expected to be a Modbus/TCP ADU.
lenlength of the data.
Returns
MB_TCP_NO_ERROR if the response was correctly stored, MB_TCP_NOT_FOUND if no transaction corresponding to the transaction id in buf is found and MB_TCP_INVALID if the ADU data was not valid (e.g. invalid length).

◆ mb_tcp_unregister_master_ch()

int mb_tcp_unregister_master_ch ( mb_tcp_master_ch_t * ch)

Unregisters a master channel.

This function should be called when the channel has been closed.

Parameters
chmaster channel.
Returns
MB_TCP_NO_ERROR if the channel was successfully unregistered MB_TCP_NOT_FOUND if the specified channel was not registered.