Skip to content

Commit 1f1552b

Browse files
committed
Move Modbus callbacks to its own file
1 parent 0549210 commit 1f1552b

8 files changed

Lines changed: 247 additions & 173 deletions

File tree

modbus/functions/mbfunccoils.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mbfunc.h"
3232

3333
#include "mb.h"
34+
#include "mbcallbacks.h"
3435
#include "mbframe.h"
3536
#include "mbinstance.h"
3637

@@ -106,7 +107,7 @@ eMBFuncReadCoils( struct xMBInstance * xInstance, uint8_t * pucFrame, uint16_t *
106107
*usLen += 1;
107108

108109
eRegStatus =
109-
eMBRegCoilsCB( xInstance, pucFrameCur, usRegAddress, usCoilCount,
110+
xInstance->xCallbacks->eMBRegCoilsCB( xInstance, pucFrameCur, usRegAddress, usCoilCount,
110111
MB_REG_READ );
111112

112113
/* If an error occured convert it into a Modbus exception. */
@@ -166,7 +167,7 @@ eMBFuncWriteCoil( struct xMBInstance * xInstance, uint8_t * pucFrame, uint16_t *
166167
ucBuf[0] = 0;
167168
}
168169
eRegStatus =
169-
eMBRegCoilsCB( xInstance, &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
170+
xInstance->xCallbacks->eMBRegCoilsCB( xInstance, &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
170171

171172
/* If an error occured convert it into a Modbus exception. */
172173
if( eRegStatus != MB_ENOERR )
@@ -228,7 +229,7 @@ eMBFuncWriteMultipleCoils( struct xMBInstance * xInstance, uint8_t * pucFrame, u
228229
( ucByteCountVerify == ucByteCount ) )
229230
{
230231
eRegStatus =
231-
eMBRegCoilsCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
232+
xInstance->xCallbacks->eMBRegCoilsCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
232233
usRegAddress, usCoilCnt, MB_REG_WRITE );
233234

234235
/* If an error occured convert it into a Modbus exception. */

modbus/functions/mbfuncdisc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mbfunc.h"
2121

2222
#include "mb.h"
23+
#include "mbcallbacks.h"
2324
#include "mbframe.h"
2425
#include "mbinstance.h"
2526

@@ -84,7 +85,7 @@ eMBFuncReadDiscreteInputs( struct xMBInstance * xInstance, uint8_t * pucFrame, u
8485
*usLen += 1;
8586

8687
eRegStatus =
87-
eMBRegDiscreteCB( xInstance, pucFrameCur, usRegAddress, usDiscreteCnt );
88+
xInstance->xCallbacks->eMBRegDiscreteCB( xInstance, pucFrameCur, usRegAddress, usDiscreteCnt );
8889

8990
/* If an error occured convert it into a Modbus exception. */
9091
if( eRegStatus != MB_ENOERR )

modbus/functions/mbfuncholding.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mbfunc.h"
3232

3333
#include "mb.h"
34+
#include "mbcallbacks.h"
3435
#include "mbframe.h"
3536
#include "mbinstance.h"
3637

@@ -80,8 +81,9 @@ eMBFuncWriteHoldingRegister( struct xMBInstance * xInstance, uint8_t * pucFrame,
8081
usRegAddress++;
8182

8283
/* Make callback to update the value. */
83-
eRegStatus = eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
84-
usRegAddress, 1, MB_REG_WRITE );
84+
eRegStatus =
85+
xInstance->xCallbacks->eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
86+
usRegAddress, 1, MB_REG_WRITE );
8587

8688
/* If an error occured convert it into a Modbus exception. */
8789
if( eRegStatus != MB_ENOERR )
@@ -126,8 +128,8 @@ eMBFuncWriteMultipleHoldingRegister( struct xMBInstance * xInstance, uint8_t * p
126128
{
127129
/* Make callback to update the register values. */
128130
eRegStatus =
129-
eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
130-
usRegAddress, usRegCount, MB_REG_WRITE );
131+
xInstance->xCallbacks->eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
132+
usRegAddress, usRegCount, MB_REG_WRITE );
131133

132134
/* If an error occured convert it into a Modbus exception. */
133135
if( eRegStatus != MB_ENOERR )
@@ -196,7 +198,8 @@ eMBFuncReadHoldingRegister( struct xMBInstance * xInstance, uint8_t * pucFrame,
196198
*usLen += 1;
197199

198200
/* Make callback to fill the buffer. */
199-
eRegStatus = eMBRegHoldingCB( xInstance, pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
201+
eRegStatus =
202+
xInstance->xCallbacks->eMBRegHoldingCB( xInstance, pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
200203
/* If an error occured convert it into a Modbus exception. */
201204
if( eRegStatus != MB_ENOERR )
202205
{
@@ -260,8 +263,9 @@ eMBFuncReadWriteMultipleHoldingRegister( struct xMBInstance * xInstance, uint8_t
260263
( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
261264
{
262265
/* Make callback to update the register values. */
263-
eRegStatus = eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
264-
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
266+
eRegStatus =
267+
xInstance->xCallbacks->eMBRegHoldingCB( xInstance, &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
268+
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
265269

266270
if( eRegStatus == MB_ENOERR )
267271
{
@@ -279,7 +283,8 @@ eMBFuncReadWriteMultipleHoldingRegister( struct xMBInstance * xInstance, uint8_t
279283

280284
/* Make the read callback. */
281285
eRegStatus =
282-
eMBRegHoldingCB( xInstance, pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
286+
xInstance->xCallbacks->eMBRegHoldingCB( xInstance, pucFrameCur, usRegReadAddress, usRegReadCount,
287+
MB_REG_READ );
283288
if( eRegStatus == MB_ENOERR )
284289
{
285290
*usLen += 2 * usRegReadCount;

modbus/functions/mbfuncinput.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mbfunc.h"
3232

3333
#include "mb.h"
34+
#include "mbcallbacks.h"
3435
#include "mbframe.h"
3536
#include "mbinstance.h"
3637

@@ -86,7 +87,7 @@ eMBFuncReadInputRegister( struct xMBInstance * xInstance, uint8_t * pucFrame, ui
8687
*usLen += 1;
8788

8889
eRegStatus =
89-
eMBRegInputCB( xInstance, pucFrameCur, usRegAddress, usRegCount );
90+
xInstance->xCallbacks->eMBRegInputCB( xInstance, pucFrameCur, usRegAddress, usRegCount );
9091

9192
/* If an error occured convert it into a Modbus exception. */
9293
if( eRegStatus != MB_ENOERR )

modbus/include/mb.h

Lines changed: 9 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern "C" {
7070
* \endcode
7171
*/
7272

73+
struct xMBCallbacks;
7374
struct xMBInstance;
7475

7576
/* ----------------------- Defines ------------------------------------------*/
@@ -89,6 +90,7 @@ struct xMBInstance;
8990
* processed until eMBEnable( ) has been called.
9091
*
9192
* \param xInstance The pointer to instance struct.
93+
* \param xMBCallbacks The pointer to struct containing Modbus callbacks.
9294
* \param eMode If ASCII or RTU mode should be used.
9395
* \param ucSlaveAddress The slave address. Only frames sent to this
9496
* address or to the broadcast address are processed.
@@ -106,9 +108,11 @@ struct xMBInstance;
106108
* slave addresses are in the range 1 - 247.
107109
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
108110
*/
109-
eMBErrorCode eMBInit( struct xMBInstance * xInstance, eMBMode eMode,
110-
uint8_t ucSlaveAddress, uint8_t ucPort,
111-
uint32_t ulBaudRate, eMBParity eParity );
111+
eMBErrorCode eMBInit( struct xMBInstance * xInstance,
112+
const struct xMBCallbacks * xCallbacks,
113+
eMBMode eMode, uint8_t ucSlaveAddress,
114+
uint8_t ucPort, uint32_t ulBaudRate,
115+
eMBParity eParity );
112116

113117
/*! \ingroup modbus
114118
* \brief Initialize the Modbus protocol stack for Modbus TCP.
@@ -117,6 +121,7 @@ eMBErrorCode eMBInit( struct xMBInstance * xInstance, eMBMode eMode,
117121
* frame processing is still disabled until eMBEnable( ) is called.
118122
*
119123
* \param xInstance The pointer to instance struct.
124+
* \param xMBCallbacks The pointer to struct containing Modbus callbacks.
120125
* \param usTCPPort The TCP port to listen on.
121126
*
122127
* \return If the protocol stack has been initialized correctly the function
@@ -127,6 +132,7 @@ eMBErrorCode eMBInit( struct xMBInstance * xInstance, eMBMode eMode,
127132
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
128133
*/
129134
eMBErrorCode eMBTCPInit( struct xMBInstance * xInstance,
135+
const struct xMBCallbacks * xCallbacks,
130136
uint16_t usTCPPort );
131137

132138
/*! \ingroup modbus
@@ -238,160 +244,6 @@ eMBErrorCode eMBRegisterCB( struct xMBInstance * xInstance,
238244
uint8_t ucFunctionCode,
239245
pxMBFunctionHandler pxHandler );
240246

241-
/* ----------------------- Callback -----------------------------------------*/
242-
243-
/*! \defgroup modbus_registers Modbus Registers
244-
* \code #include "mb.h" \endcode
245-
* The protocol stack does not internally allocate any memory for the
246-
* registers. This makes the protocol stack very small and also usable on
247-
* low end targets. In addition the values don't have to be in the memory
248-
* and could for example be stored in a flash.<br>
249-
* Whenever the protocol stack requires a value it calls one of the callback
250-
* function with the register address and the number of registers to read
251-
* as an argument. The application should then read the actual register values
252-
* (for example the ADC voltage) and should store the result in the supplied
253-
* buffer.<br>
254-
* If the protocol stack wants to update a register value because a write
255-
* register function was received a buffer with the new register values is
256-
* passed to the callback function. The function should then use these values
257-
* to update the application register values.
258-
*/
259-
260-
/*! \ingroup modbus_registers
261-
* \brief Callback function used if the value of a <em>Input Register</em>
262-
* is required by the protocol stack. The starting register address is given
263-
* by \c usAddress and the last register is given by <tt>usAddress +
264-
* usNRegs - 1</tt>.
265-
*
266-
* \param xInstance The pointer to instance struct.
267-
* \param pucRegBuffer A buffer where the callback function should write
268-
* the current value of the modbus registers to.
269-
* \param usAddress The starting address of the register. Input registers
270-
* are in the range 1 - 65535.
271-
* \param usNRegs Number of registers the callback function must supply.
272-
*
273-
* \return The function must return one of the following error codes:
274-
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
275-
* Modbus response is sent.
276-
* - eMBErrorCode::MB_ENOREG If the application can not supply values
277-
* for registers within this range. In this case a
278-
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
279-
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
280-
* currently not available and the application dependent response
281-
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
282-
* exception is sent as a response.
283-
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
284-
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
285-
*/
286-
eMBErrorCode eMBRegInputCB( struct xMBInstance * xInstance,
287-
uint8_t * pucRegBuffer, uint16_t usAddress,
288-
uint16_t usNRegs );
289-
290-
/*! \ingroup modbus_registers
291-
* \brief Callback function used if a <em>Holding Register</em> value is
292-
* read or written by the protocol stack. The starting register address
293-
* is given by \c usAddress and the last register is given by
294-
* <tt>usAddress + usNRegs - 1</tt>.
295-
*
296-
* \param xInstance The pointer to instance struct.
297-
* \param pucRegBuffer If the application registers values should be updated the
298-
* buffer points to the new registers values. If the protocol stack needs
299-
* to now the current values the callback function should write them into
300-
* this buffer.
301-
* \param usAddress The starting address of the register.
302-
* \param usNRegs Number of registers to read or write.
303-
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
304-
* values should be updated from the values in the buffer. For example
305-
* this would be the case when the Modbus master has issued an
306-
* <b>WRITE SINGLE REGISTER</b> command.
307-
* If the value eMBRegisterMode::MB_REG_READ the application should copy
308-
* the current values into the buffer \c pucRegBuffer.
309-
*
310-
* \return The function must return one of the following error codes:
311-
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
312-
* Modbus response is sent.
313-
* - eMBErrorCode::MB_ENOREG If the application can not supply values
314-
* for registers within this range. In this case a
315-
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
316-
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
317-
* currently not available and the application dependent response
318-
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
319-
* exception is sent as a response.
320-
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
321-
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
322-
*/
323-
eMBErrorCode eMBRegHoldingCB( struct xMBInstance * xInstance,
324-
uint8_t * pucRegBuffer, uint16_t usAddress,
325-
uint16_t usNRegs, eMBRegisterMode eMode );
326-
327-
/*! \ingroup modbus_registers
328-
* \brief Callback function used if a <em>Coil Register</em> value is
329-
* read or written by the protocol stack. If you are going to use
330-
* this function you might use the functions xMBUtilSetBits( ) and
331-
* xMBUtilGetBits( ) for working with bitfields.
332-
*
333-
* \param xInstance The pointer to instance struct.
334-
* \param pucRegBuffer The bits are packed in bytes where the first coil
335-
* starting at address \c usAddress is stored in the LSB of the
336-
* first byte in the buffer <code>pucRegBuffer</code>.
337-
* If the buffer should be written by the callback function unused
338-
* coil values (I.e. if not a multiple of eight coils is used) should be set
339-
* to zero.
340-
* \param usAddress The first coil number.
341-
* \param usNCoils Number of coil values requested.
342-
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
343-
* be updated from the values supplied in the buffer \c pucRegBuffer.
344-
* If eMBRegisterMode::MB_REG_READ the application should store the current
345-
* values in the buffer \c pucRegBuffer.
346-
*
347-
* \return The function must return one of the following error codes:
348-
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
349-
* Modbus response is sent.
350-
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
351-
* within the requested address range. In this case a
352-
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
353-
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
354-
* currently not available and the application dependent response
355-
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
356-
* exception is sent as a response.
357-
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
358-
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
359-
*/
360-
eMBErrorCode eMBRegCoilsCB( struct xMBInstance * xInstance,
361-
uint8_t * pucRegBuffer, uint16_t usAddress,
362-
uint16_t usNCoils, eMBRegisterMode eMode );
363-
364-
/*! \ingroup modbus_registers
365-
* \brief Callback function used if a <em>Input Discrete Register</em> value is
366-
* read by the protocol stack.
367-
*
368-
* If you are going to use his function you might use the functions
369-
* xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
370-
*
371-
* \param xInstance The pointer to instance struct.
372-
* \param pucRegBuffer The buffer should be updated with the current
373-
* coil values. The first discrete input starting at \c usAddress must be
374-
* stored at the LSB of the first byte in the buffer. If the requested number
375-
* is not a multiple of eight the remaining bits should be set to zero.
376-
* \param usAddress The starting address of the first discrete input.
377-
* \param usNDiscrete Number of discrete input values.
378-
* \return The function must return one of the following error codes:
379-
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
380-
* Modbus response is sent.
381-
* - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
382-
* In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
383-
* as a response.
384-
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
385-
* currently not available and the application dependent response
386-
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
387-
* exception is sent as a response.
388-
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
389-
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
390-
*/
391-
eMBErrorCode eMBRegDiscreteCB( struct xMBInstance * xInstance,
392-
uint8_t * pucRegBuffer, uint16_t usAddress,
393-
uint16_t usNDiscrete );
394-
395247
#ifdef __cplusplus
396248
}
397249
#endif

0 commit comments

Comments
 (0)