Support disabling individual server callbacks

This helps reduce code size for systems that do not implement the full
set of function codes supported by the library.
This commit is contained in:
jonath.re@gmail.com 2022-07-22 17:14:32 +02:00
parent 10b9a0c13b
commit 17aa0ca226
3 changed files with 84 additions and 19 deletions

View File

@ -147,5 +147,15 @@ Please refer to `examples/arduino/README.md` for more info about building and ru
- To reduce code size, you can define the following `#define`s: - To reduce code size, you can define the following `#define`s:
- `NMBS_CLIENT_DISABLED` to disable all client code - `NMBS_CLIENT_DISABLED` to disable all client code
- `NMBS_SERVER_DISABLED` to disable all server code - `NMBS_SERVER_DISABLED` to disable all server code
- To disable individual server callbacks, define the following:
- `NMBS_SERVER_READ_COILS_DISABLED`
- `NMBS_SERVER_READ_COILS_DISABLED`
- `NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED`
- `NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED`
- `NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED`
- `NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED`
- `NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED`
- `NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED`
- `NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED`
- `NMBS_STRERROR_DISABLED` to disable the code that converts `nmbs_error`s to strings - `NMBS_STRERROR_DISABLED` to disable the code that converts `nmbs_error`s to strings
- Debug prints about received and sent messages can be enabled by defining `NMBS_DEBUG` - Debug prints about received and sent messages can be enabled by defining `NMBS_DEBUG`

View File

@ -442,7 +442,7 @@ static nmbs_error send_exception_msg(nmbs_t* nmbs, uint8_t exception) {
#endif #endif
#ifndef NMBS_SERVER_DISABLED #if !defined(NMBS_SERVER_READ_COILS_DISABLED) || !defined(NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED)
static nmbs_error handle_read_discrete(nmbs_t* nmbs, nmbs_error (*callback)(uint16_t, uint16_t, nmbs_bitfield)) { static nmbs_error handle_read_discrete(nmbs_t* nmbs, nmbs_error (*callback)(uint16_t, uint16_t, nmbs_bitfield)) {
nmbs_error err = recv(nmbs, 4); nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -503,7 +503,7 @@ static nmbs_error handle_read_discrete(nmbs_t* nmbs, nmbs_error (*callback)(uint
#endif #endif
#ifndef NMBS_SERVER_DISABLED #if !defined(NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED) || !defined(NMBS_SERVER_READ_INPUTS_REGISTERS_DISABLED)
static nmbs_error handle_read_registers(nmbs_t* nmbs, nmbs_error (*callback)(uint16_t, uint16_t, uint16_t*)) { static nmbs_error handle_read_registers(nmbs_t* nmbs, nmbs_error (*callback)(uint16_t, uint16_t, uint16_t*)) {
nmbs_error err = recv(nmbs, 4); nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -564,35 +564,35 @@ static nmbs_error handle_read_registers(nmbs_t* nmbs, nmbs_error (*callback)(uin
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_READ_COILS_DISABLED
static nmbs_error handle_read_coils(nmbs_t* nmbs) { static nmbs_error handle_read_coils(nmbs_t* nmbs) {
return handle_read_discrete(nmbs, nmbs->callbacks.read_coils); return handle_read_discrete(nmbs, nmbs->callbacks.read_coils);
} }
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
static nmbs_error handle_read_discrete_inputs(nmbs_t* nmbs) { static nmbs_error handle_read_discrete_inputs(nmbs_t* nmbs) {
return handle_read_discrete(nmbs, nmbs->callbacks.read_discrete_inputs); return handle_read_discrete(nmbs, nmbs->callbacks.read_discrete_inputs);
} }
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED
static nmbs_error handle_read_holding_registers(nmbs_t* nmbs) { static nmbs_error handle_read_holding_registers(nmbs_t* nmbs) {
return handle_read_registers(nmbs, nmbs->callbacks.read_holding_registers); return handle_read_registers(nmbs, nmbs->callbacks.read_holding_registers);
} }
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED
static nmbs_error handle_read_input_registers(nmbs_t* nmbs) { static nmbs_error handle_read_input_registers(nmbs_t* nmbs) {
return handle_read_registers(nmbs, nmbs->callbacks.read_input_registers); return handle_read_registers(nmbs, nmbs->callbacks.read_input_registers);
} }
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED
static nmbs_error handle_write_single_coil(nmbs_t* nmbs) { static nmbs_error handle_write_single_coil(nmbs_t* nmbs) {
nmbs_error err = recv(nmbs, 4); nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -642,7 +642,7 @@ static nmbs_error handle_write_single_coil(nmbs_t* nmbs) {
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED
static nmbs_error handle_write_single_register(nmbs_t* nmbs) { static nmbs_error handle_write_single_register(nmbs_t* nmbs) {
nmbs_error err = recv(nmbs, 4); nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -689,7 +689,7 @@ static nmbs_error handle_write_single_register(nmbs_t* nmbs) {
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
static nmbs_error handle_write_multiple_coils(nmbs_t* nmbs) { static nmbs_error handle_write_multiple_coils(nmbs_t* nmbs) {
nmbs_error err = recv(nmbs, 5); nmbs_error err = recv(nmbs, 5);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -759,7 +759,7 @@ static nmbs_error handle_write_multiple_coils(nmbs_t* nmbs) {
#endif #endif
#ifndef NMBS_SERVER_DISABLED #ifndef NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED
static nmbs_error handle_write_multiple_registers(nmbs_t* nmbs) { static nmbs_error handle_write_multiple_registers(nmbs_t* nmbs) {
nmbs_error err = recv(nmbs, 5); nmbs_error err = recv(nmbs, 5);
if (err != NMBS_ERROR_NONE) if (err != NMBS_ERROR_NONE)
@ -835,37 +835,52 @@ static nmbs_error handle_req_fc(nmbs_t* nmbs) {
nmbs_error err; nmbs_error err;
switch (nmbs->msg.fc) { switch (nmbs->msg.fc) {
#ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
case 1: case 1:
err = handle_read_coils(nmbs); err = handle_read_coils(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
case 2: case 2:
err = handle_read_discrete_inputs(nmbs); err = handle_read_discrete_inputs(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED
case 3: case 3:
err = handle_read_holding_registers(nmbs); err = handle_read_holding_registers(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED
case 4: case 4:
err = handle_read_input_registers(nmbs); err = handle_read_input_registers(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED
case 5: case 5:
err = handle_write_single_coil(nmbs); err = handle_write_single_coil(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED
case 6: case 6:
err = handle_write_single_register(nmbs); err = handle_write_single_register(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
case 15: case 15:
err = handle_write_multiple_coils(nmbs); err = handle_write_multiple_coils(nmbs);
break; break;
#endif
#ifndef NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED
case 16: case 16:
err = handle_write_multiple_registers(nmbs); err = handle_write_multiple_registers(nmbs);
break; break;
#endif
default: default:
err = NMBS_EXCEPTION_ILLEGAL_FUNCTION; err = NMBS_EXCEPTION_ILLEGAL_FUNCTION;

View File

@ -48,6 +48,22 @@
extern "C" { extern "C" {
#endif #endif
/**
* If NMBS_SERVER_DISABLED is set, disable all the server callbacks too.
*/
#ifdef NMBS_SERVER_DISABLED
#define NMBS_SERVER_READ_COILS_DISABLED 1
#define NMBS_SERVER_READ_COILS_DISABLED 1
#define NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED 1
#define NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED 1
#define NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED 1
#define NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED 1
#define NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED 1
#define NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED 1
#define NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED 1
#endif
/** /**
* nanoMODBUS errors. * nanoMODBUS errors.
* Values <= 0 are library errors, > 0 are modbus exceptions. * Values <= 0 are library errors, > 0 are modbus exceptions.
@ -141,14 +157,38 @@ typedef struct nmbs_platform_conf {
* Modbus server request callbacks. Passed to nmbs_server_create(). * Modbus server request callbacks. Passed to nmbs_server_create().
*/ */
typedef struct nmbs_callbacks { typedef struct nmbs_callbacks {
#ifndef NMBS_SERVER_READ_COILS_DISABLED
nmbs_error (*read_coils)(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out); nmbs_error (*read_coils)(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out);
#endif
#ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
nmbs_error (*read_discrete_inputs)(uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out); nmbs_error (*read_discrete_inputs)(uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out);
#endif
#ifndef NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED
nmbs_error (*read_holding_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out); nmbs_error (*read_holding_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out);
#endif
#ifndef NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED
nmbs_error (*read_input_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out); nmbs_error (*read_input_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out);
#endif
#ifndef NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED
nmbs_error (*write_single_coil)(uint16_t address, bool value); nmbs_error (*write_single_coil)(uint16_t address, bool value);
#endif
#ifndef NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED
nmbs_error (*write_single_register)(uint16_t address, uint16_t value); nmbs_error (*write_single_register)(uint16_t address, uint16_t value);
#endif
#ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
nmbs_error (*write_multiple_coils)(uint16_t address, uint16_t quantity, const nmbs_bitfield coils); nmbs_error (*write_multiple_coils)(uint16_t address, uint16_t quantity, const nmbs_bitfield coils);
#endif
#ifndef NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED
nmbs_error (*write_multiple_registers)(uint16_t address, uint16_t quantity, const uint16_t* registers); nmbs_error (*write_multiple_registers)(uint16_t address, uint16_t quantity, const uint16_t* registers);
#endif
} nmbs_callbacks; } nmbs_callbacks;
@ -246,7 +286,7 @@ void nmbs_set_destination_rtu_address(nmbs_t* nmbs, uint8_t address);
nmbs_error nmbs_server_poll(nmbs_t* nmbs); nmbs_error nmbs_server_poll(nmbs_t* nmbs);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_READ_COILS_DISABLED
/** Send a FC 01 (0x01) Read Coils request /** Send a FC 01 (0x01) Read Coils request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address
@ -258,7 +298,7 @@ nmbs_error nmbs_server_poll(nmbs_t* nmbs);
nmbs_error nmbs_read_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield coils_out); nmbs_error nmbs_read_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield coils_out);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
/** Send a FC 02 (0x02) Read Discrete Inputs request /** Send a FC 02 (0x02) Read Discrete Inputs request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address
@ -270,7 +310,7 @@ nmbs_error nmbs_read_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nm
nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out); nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED
/** Send a FC 03 (0x03) Read Holding Registers request /** Send a FC 03 (0x03) Read Holding Registers request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address
@ -282,7 +322,7 @@ nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t qu
nmbs_error nmbs_read_holding_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out); nmbs_error nmbs_read_holding_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED
/** Send a FC 04 (0x04) Read Input Registers request /** Send a FC 04 (0x04) Read Input Registers request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address
@ -294,7 +334,7 @@ nmbs_error nmbs_read_holding_registers(nmbs_t* nmbs, uint16_t address, uint16_t
nmbs_error nmbs_read_input_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out); nmbs_error nmbs_read_input_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED
/** Send a FC 05 (0x05) Write Single Coil request /** Send a FC 05 (0x05) Write Single Coil request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address coil address * @param address coil address
@ -305,7 +345,7 @@ nmbs_error nmbs_read_input_registers(nmbs_t* nmbs, uint16_t address, uint16_t qu
nmbs_error nmbs_write_single_coil(nmbs_t* nmbs, uint16_t address, bool value); nmbs_error nmbs_write_single_coil(nmbs_t* nmbs, uint16_t address, bool value);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED
/** Send a FC 06 (0x06) Write Single Register request /** Send a FC 06 (0x06) Write Single Register request
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address register address * @param address register address
@ -316,7 +356,7 @@ nmbs_error nmbs_write_single_coil(nmbs_t* nmbs, uint16_t address, bool value);
nmbs_error nmbs_write_single_register(nmbs_t* nmbs, uint16_t address, uint16_t value); nmbs_error nmbs_write_single_register(nmbs_t* nmbs, uint16_t address, uint16_t value);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
/** Send a FC 15 (0x0F) Write Multiple Coils /** Send a FC 15 (0x0F) Write Multiple Coils
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address
@ -328,7 +368,7 @@ nmbs_error nmbs_write_single_register(nmbs_t* nmbs, uint16_t address, uint16_t v
nmbs_error nmbs_write_multiple_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, const nmbs_bitfield coils); nmbs_error nmbs_write_multiple_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, const nmbs_bitfield coils);
#endif #endif
#ifndef NMBS_CLIENT_DISABLED #ifndef NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED
/** Send a FC 16 (0x10) Write Multiple Registers /** Send a FC 16 (0x10) Write Multiple Registers
* @param nmbs pointer to the nmbs_t instance * @param nmbs pointer to the nmbs_t instance
* @param address starting address * @param address starting address