Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make internal buffer size configurable #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nanomodbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,14 +803,14 @@ static nmbs_error handle_read_registers(nmbs_t* nmbs,
return err;

if (!nmbs->msg.ignored) {
if (quantity < 1 || quantity > 125)
if (quantity < 1 || quantity > NMBS_INTERNAL_MAX_REGISTER_QUANTITY)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_VALUE);

if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS);

if (callback) {
uint16_t regs[125] = {0};
uint16_t regs[NMBS_INTERNAL_MAX_REGISTER_QUANTITY] = {0};
err = callback(address, quantity, regs, nmbs->msg.unit_id, nmbs->platform.arg);
if (err != NMBS_ERROR_NONE) {
if (nmbs_error_is_exception(err))
Expand Down Expand Up @@ -1508,7 +1508,7 @@ nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t qu
}

static nmbs_error read_registers(nmbs_t* nmbs, uint8_t fc, uint16_t address, uint16_t quantity, uint16_t* registers) {
if (quantity < 1 || quantity > 125)
if (quantity < 1 || quantity > NMBS_INTERNAL_MAX_REGISTER_QUANTITY)
return NMBS_ERROR_INVALID_ARGUMENT;

if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
Expand Down
7 changes: 6 additions & 1 deletion nanomodbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,18 @@ typedef struct nmbs_callbacks {
char _nonempty; // Struct may become empty, which is undefined behavior
} nmbs_callbacks;

#ifndef NMBS_INTERNAL_BUFFER_SIZE
#define NMBS_INTERNAL_BUFFER_SIZE 260
#endif

#define NMBS_INTERNAL_MAX_REGISTER_QUANTITY ((NMBS_INTERNAL_BUFFER_SIZE - 7) / 2) // 7 Extra bytes for MODBUS (tcp) needed

/**
* nanoMODBUS client/server instance type. All struct members are to be considered private, it is not advisable to read/write them directly.
*/
typedef struct nmbs_t {
struct {
uint8_t buf[260];
uint8_t buf[NMBS_INTERNAL_BUFFER_SIZE];
uint16_t buf_idx;

uint8_t unit_id;
Expand Down