Skip to content

Commit

Permalink
Add modbus_file_t and update mapping functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mek-x committed Sep 10, 2018
1 parent add0a3b commit 6c08f48
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/modbus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct _modbus {
void _modbus_init_common(modbus_t *ctx);
void _error_print(modbus_t *ctx, const char *context);
int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type);
void _modbus_free_files(modbus_file_t *files, int nb_files);

#ifndef HAVE_STRLCPY
size_t strlcpy(char *dest, const char *src, size_t dest_size);
Expand Down
83 changes: 80 additions & 3 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,9 +1893,12 @@ modbus_mapping_t* modbus_mapping_new_start_address(
unsigned int start_bits, unsigned int nb_bits,
unsigned int start_input_bits, unsigned int nb_input_bits,
unsigned int start_registers, unsigned int nb_registers,
unsigned int start_input_registers, unsigned int nb_input_registers)
unsigned int start_input_registers, unsigned int nb_input_registers,
unsigned int start_files, unsigned int nb_files,
unsigned int nb_records, unsigned int record_size)
{
modbus_mapping_t *mb_mapping;
unsigned int i, j;

mb_mapping = (modbus_mapping_t *)malloc(sizeof(modbus_mapping_t));
if (mb_mapping == NULL) {
Expand Down Expand Up @@ -1970,16 +1973,88 @@ modbus_mapping_t* modbus_mapping_new_start_address(
nb_input_registers * sizeof(uint16_t));
}

/* 5X */
mb_mapping->nb_files = nb_files;
mb_mapping->start_files = start_files;
if (nb_files == 0) {
mb_mapping->files = NULL;
} else {
mb_mapping->files = (modbus_file_t *) malloc(nb_files * sizeof(modbus_file_t));
if (mb_mapping->files == NULL) {
free(mb_mapping->tab_input_registers);
free(mb_mapping->tab_registers);
free(mb_mapping->tab_input_bits);
free(mb_mapping->tab_bits);
free(mb_mapping);
return NULL;
}
memset(mb_mapping->files, 0,
nb_files * sizeof(modbus_file_t));

for (i = 0; i < nb_files; i++) {
mb_mapping->files[i].nb_records = nb_records;
mb_mapping->files[i].record_size = record_size;
mb_mapping->files[i].records = (uint16_t **) malloc(nb_records * sizeof(uint16_t *));
if (mb_mapping->files[i].records == NULL) {
while (i) {
free(mb_mapping->files[i--].records);
}
free(mb_mapping->files);
free(mb_mapping->tab_input_registers);
free(mb_mapping->tab_registers);
free(mb_mapping->tab_input_bits);
free(mb_mapping->tab_bits);
free(mb_mapping);
return NULL;
}
for (j = 0; j < nb_records; j++) {
mb_mapping->files[i].records[j] = (uint16_t *) malloc(record_size * sizeof(uint16_t));
if (mb_mapping->files[i].records[j] == NULL) {
while (j) {
free(mb_mapping->files[i].records[j--]);
}
while (i) {
free(mb_mapping->files[i--].records);
}
free(mb_mapping->files);
free(mb_mapping->tab_input_registers);
free(mb_mapping->tab_registers);
free(mb_mapping->tab_input_bits);
free(mb_mapping->tab_bits);
free(mb_mapping);
return NULL;
}
}
}
}

return mb_mapping;
}

modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
int nb_registers, int nb_input_registers)
int nb_registers, int nb_input_registers,
int nb_files, int nb_records, int record_size)
{
return modbus_mapping_new_start_address(
0, nb_bits, 0, nb_input_bits, 0, nb_registers, 0, nb_input_registers);
0, nb_bits, 0, nb_input_bits, 0, nb_registers, 0, nb_input_registers,
0, nb_files, nb_records, record_size);
}

void _modbus_free_files(modbus_file_t *files, int nb_files)
{
int i, j;

if (files == NULL) {
return;
}

for (i = 0; i < nb_files; i++) {
for (j = 0; j < files[i].nb_records; j++) {
free(files[i].records[j]);
}
free(files[i].records);
}
}
/* Frees the 4 arrays */
void modbus_mapping_free(modbus_mapping_t *mb_mapping)
{
Expand All @@ -1991,6 +2066,8 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping)
free(mb_mapping->tab_registers);
free(mb_mapping->tab_input_bits);
free(mb_mapping->tab_bits);
_modbus_free_files(mb_mapping->files, mb_mapping->nb_files);
free(mb_mapping->files);
free(mb_mapping);
}

Expand Down
16 changes: 14 additions & 2 deletions src/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ extern const unsigned int libmodbus_version_micro;

typedef struct _modbus modbus_t;

typedef struct _modbus_file_t {
int nb_records;
int record_size;
uint16_t **records;
} modbus_file_t;

typedef struct _modbus_mapping_t {
int nb_bits;
int start_bits;
Expand All @@ -170,10 +176,13 @@ typedef struct _modbus_mapping_t {
int start_input_registers;
int nb_registers;
int start_registers;
int nb_files;
int start_files;
uint8_t *tab_bits;
uint8_t *tab_input_bits;
uint16_t *tab_input_registers;
uint16_t *tab_registers;
modbus_file_t *files;
} modbus_mapping_t;

typedef enum
Expand Down Expand Up @@ -230,10 +239,13 @@ MODBUS_API modbus_mapping_t* modbus_mapping_new_start_address(
unsigned int start_bits, unsigned int nb_bits,
unsigned int start_input_bits, unsigned int nb_input_bits,
unsigned int start_registers, unsigned int nb_registers,
unsigned int start_input_registers, unsigned int nb_input_registers);
unsigned int start_input_registers, unsigned int nb_input_registers,
unsigned int start_files, unsigned int nb_files,
unsigned int nb_records, unsigned int record_size);

MODBUS_API modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
int nb_registers, int nb_input_registers);
int nb_registers, int nb_input_registers,
int nb_files, int nb_records, int record_size);
MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);

MODBUS_API int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
Expand Down
3 changes: 2 additions & 1 deletion tests/bandwidth-server-many-up.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ int main(void)
ctx = modbus_new_tcp("127.0.0.1", 1502);

mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
MODBUS_MAX_READ_REGISTERS, 0);
MODBUS_MAX_READ_REGISTERS, 0,
0, 0, 0);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
Expand Down
3 changes: 2 additions & 1 deletion tests/bandwidth-server-one.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ int main(int argc, char *argv[])
}

mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
MODBUS_MAX_READ_REGISTERS, 0);
MODBUS_MAX_READ_REGISTERS, 0,
0, 0, 0);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
Expand Down
2 changes: 1 addition & 1 deletion tests/random-test-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main(void)
ctx = modbus_new_tcp("127.0.0.1", 1502);
/* modbus_set_debug(ctx, TRUE); */

mb_mapping = modbus_mapping_new(500, 500, 500, 500);
mb_mapping = modbus_mapping_new(500, 500, 500, 500, 0, 0, 0);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
Expand Down
3 changes: 2 additions & 1 deletion tests/unit-test-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ int main(int argc, char*argv[])
UT_BITS_ADDRESS, UT_BITS_NB,
UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX,
UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB);
UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB,
0, 0, 0, 0);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
Expand Down

0 comments on commit 6c08f48

Please sign in to comment.