liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
slave.h
Go to the documentation of this file.
1 /*
2  liblightmodbus - a lightweight, multiplatform Modbus library
3  Copyright (C) 2017 Jacek Wieczorek <mrjjot@gmail.com>
4 
5  This file is part of liblightmodbus.
6 
7  Liblightmodbus is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Liblightmodbus is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
28 #ifndef LIGHTMODBUS_SLAVE_H
29 #define LIGHTMODBUS_SLAVE_H
30 
31 // For C++
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <inttypes.h>
37 #include <stddef.h>
38 #include "parser.h"
39 #include "libconf.h"
40 #include "lightmodbus.h"
41 
42 #ifdef LIGHTMODBUS_SLAVE_BASE
43 
44 #ifdef LIGHTMODBUS_SLAVE_USER_FUNCTIONS
45  struct modbusSlave;
51  typedef struct modbusSlaveUserFunction
52  {
53  uint8_t function;
54 
60  ModbusError ( *handler )( struct modbusSlave *status, ModbusParser *parser );
62 #endif
63 
64 #if defined( LIGHTMODBUS_REGISTER_CALLBACK ) || defined( LIGHTMODBUS_COIL_CALLBACK )
65  #ifndef LIGHTMODBUS_EXPERIMENTAL
66  #error Register callback functions are an experimental feature that may cause problems. Please define LIGHTMODBUS_EXPERIMENTAL to dismiss this error message.
67  #endif
68 
73  typedef enum modbusRegisterQuery
74  {
80 
90  typedef uint16_t ( *ModbusRegisterCallbackFunction )( ModbusRegisterQuery query, ModbusDataType datatype, uint16_t index, uint16_t value, void *ctx );
91 #endif
92 
96 typedef struct modbusSlave
97 {
98  uint8_t address;
99 
100  //Universal register/coil callback function
101  #if defined( LIGHTMODBUS_COIL_CALLBACK ) || defined( LIGHTMODBUS_REGISTER_CALLBACK )
102 
108 
115  #endif
116 
117  //Slave registers arrays
118  #ifndef LIGHTMODBUS_REGISTER_CALLBACK
119  uint16_t *registers;
120  uint16_t *inputRegisters;
121  uint8_t *registerMask;
123  #endif
124  uint16_t registerCount;
126 
127  //Slave coils array
128  #ifndef LIGHTMODBUS_COIL_CALLBACK
129  uint8_t *coils;
130  uint8_t *discreteInputs;
131  uint8_t *coilMask;
132  uint16_t coilMaskLength;
133  #endif
134  uint16_t coilCount;
136 
142 
150 
151  //Array of user defined functions - these can override default Modbus functions
152  #ifdef LIGHTMODBUS_SLAVE_USER_FUNCTIONS
153 
160  uint16_t userFunctionCount;
161  #endif
162 
172  struct
173  {
174  #ifdef LIGHTMODBUS_STATIC_MEM_SLAVE_RESPONSE
175  uint8_t frame[LIGHTMODBUS_STATIC_MEM_SLAVE_RESPONSE];
177  #else
178  uint8_t *frame;
180  #endif
181 
183  uint8_t length;
184  } response;
185 
195  struct
196  {
197  #ifdef LIGHTMODBUS_STATIC_MEM_SLAVE_REQUEST
198  uint8_t frame[LIGHTMODBUS_STATIC_MEM_SLAVE_REQUEST];
200  #else
201  const uint8_t *frame;
203  #endif
204 
206  uint8_t length;
207  } request;
208 
209 } ModbusSlave;
210 #endif
211 
212 //Function prototypes
213 #ifdef LIGHTMODBUS_SLAVE_BASE
214 
231 extern ModbusError modbusBuildException( ModbusSlave *status, uint8_t function, ModbusExceptionCode code );
232 
258 extern ModbusError modbusParseRequest( ModbusSlave *status );
259 
265 extern ModbusError modbusSlaveInit( ModbusSlave *status );
266 
272 extern ModbusError modbusSlaveEnd( ModbusSlave *status );
273 
292 static inline ModbusError modbusBuildExceptionErr( ModbusSlave *status, uint8_t function, ModbusExceptionCode code, ModbusFrameError parseError ) //Build an exception and write error to status->parseError
293 {
294  if ( status == NULL ) return MODBUS_ERROR_NULLPTR;
295  status->parseError = parseError;
296  ModbusError err = modbusBuildException( status, function, code );
297  if ( err == MODBUS_ERROR_OK ) return MODBUS_ERROR_PARSE;
298  else return err;
299 }
300 #endif
301 
302 // For C++ (closes `extern "C"` )
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif
static ModbusError modbusBuildExceptionErr(ModbusSlave *status, uint8_t function, ModbusExceptionCode code, ModbusFrameError parseError)
Handles Modbus parsing errors.
Definition: slave.h:292
uint16_t * inputRegisters
Pointer to input registers data.
Definition: slave.h:120
uint16_t registerCount
Slave&#39;s register count.
Definition: slave.h:124
ModbusError modbusSlaveInit(ModbusSlave *status)
Performs initialization of the ModbusSlave structure.
Definition: slave.c:222
ModbusSlaveUserFunction * userFunctions
A pointer to user defined Modbus functions array.
Definition: slave.h:159
uint8_t * coilMask
Masks for coil write protection (each bit corresponds to one coil)
Definition: slave.h:131
Asks callback function if register can be read.
Definition: slave.h:77
ModbusFrameError parseError
More specific error code of problem encountered during frame parsing.
Definition: slave.h:149
Asks callback function if register can be written.
Definition: slave.h:78
A NULL pointer provided as some crucial parameter.
Definition: lightmodbus.h:73
uint16_t userFunctionCount
Number of user-defined Modbus functions /see userFunctions.
Definition: slave.h:160
Represents Modbus slave device&#39;s status and configuration.
Definition: slave.h:96
ModbusExceptionCode lastException
Exception code of the last exception generated by modbusBuildException.
Definition: slave.h:141
uint16_t * registers
Pointer to registers data.
Definition: slave.h:119
A big union of structures used for parsing standard Modbus requests and building responses.
Definition: parser.h:43
Requests callback function to write the register.
Definition: slave.h:76
Requests callback function to return register value.
Definition: slave.h:75
Contains the modbusParser union used during frame creation and parsing.
uint16_t(* ModbusRegisterCallbackFunction)(ModbusRegisterQuery query, ModbusDataType datatype, uint16_t index, uint16_t value, void *ctx)
Type representing a pointer to the user-defined register callback function.
Definition: slave.h:90
ModbusError modbusSlaveEnd(ModbusSlave *status)
Frees memory used by slave structure, previously initialized with modbusSlaveInit.
Definition: slave.c:287
enum modbusExceptionCode ModbusExceptionCode
Represents a Modbus exception code, defined by the standart.
struct modbusSlaveUserFunction ModbusSlaveUserFunction
Associates user defined parser function with the function ID.
enum modbusFrameError ModbusFrameError
Provides more information on frame building/parsing error.
void * registerCallbackContext
The user data pointer passed to the callback function each time it&#39;s used.
Definition: slave.h:114
Definition: lightmodbus.h:81
uint8_t length
Frame length in bytes.
Definition: slave.h:183
ModbusError modbusBuildException(ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
Builds an exception frame and stores it in the ModbusSlave structure.
Definition: slave.c:30
Associates user defined parser function with the function ID.
Definition: slave.h:51
ModbusRegisterCallbackFunction registerCallback
The pointer to the user-defined register callback function.
Definition: slave.h:107
uint8_t address
The slave&#39;s address.
Definition: slave.h:98
ModbusError(* handler)(struct modbusSlave *status, ModbusParser *parser)
Pointer to the user defined function.
Definition: slave.h:60
No error.
Definition: lightmodbus.h:51
uint8_t * registerMask
Mask for register write protection (each bit corresponds to one register)
Definition: slave.h:121
uint16_t discreteInputCount
Slave&#39;s discrete input count.
Definition: slave.h:135
struct modbusSlave ModbusSlave
Represents Modbus slave device&#39;s status and configuration.
uint16_t coilMaskLength
Write protection mask (coilMask) length in bytes (each byte covers 8 coils)
Definition: slave.h:132
uint8_t * discreteInputs
Pointer to discrete inputs data.
Definition: slave.h:130
modbusRegisterQuery
Represents register callback function.
Definition: slave.h:73
enum modbusRegisterQuery ModbusRegisterQuery
Represents register callback function.
uint16_t coilCount
Slave&#39;s coil count.
Definition: slave.h:134
Core Modbus functions.
uint16_t registerMaskLength
Write protection mask (registerMask) length in bytes (each byte covers 8 registers) ...
Definition: slave.h:122
uint16_t inputRegisterCount
Slave&#39;s input register count.
Definition: slave.h:125
uint8_t * coils
Pointer to coils data.
Definition: slave.h:129
ModbusError modbusParseRequest(ModbusSlave *status)
Interprets incoming Modbus request frame located in the slave structure.
Definition: slave.c:75
enum modbusDataType ModbusDataType
Stores information about Modbus data types.
enum modbusError ModbusError
Represents a library runtime error code.