diff --git a/src/modbus-private.h b/src/modbus-private.h index 2c601c495..3e854f117 100644 --- a/src/modbus-private.h +++ b/src/modbus-private.h @@ -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); diff --git a/src/modbus.c b/src/modbus.c index 4a30cc73b..163170223 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -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) { @@ -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) { @@ -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); } diff --git a/src/modbus.h b/src/modbus.h index 5983e68fd..fd63146d9 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -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; @@ -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 @@ -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); diff --git a/tests/bandwidth-server-many-up.c b/tests/bandwidth-server-many-up.c index 0f00f3a2f..eb86db831 100644 --- a/tests/bandwidth-server-many-up.c +++ b/tests/bandwidth-server-many-up.c @@ -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)); diff --git a/tests/bandwidth-server-one.c b/tests/bandwidth-server-one.c index 8971d0763..4519471f8 100644 --- a/tests/bandwidth-server-one.c +++ b/tests/bandwidth-server-one.c @@ -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)); diff --git a/tests/random-test-server.c b/tests/random-test-server.c index 03bd87056..e5b97a9ca 100644 --- a/tests/random-test-server.c +++ b/tests/random-test-server.c @@ -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)); diff --git a/tests/unit-test-server.c b/tests/unit-test-server.c index 7002b10c6..834105d20 100644 --- a/tests/unit-test-server.c +++ b/tests/unit-test-server.c @@ -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));