From 3228ddb413d42d2eb33d5ba4004e762f63d9abcc Mon Sep 17 00:00:00 2001 From: Rick M Date: Tue, 12 Jul 2022 21:06:49 -0700 Subject: [PATCH 1/3] Added a client context parameter to the modbus_t context, and getter/setter methods. --- src/modbus-private.h | 1 + src/modbus.c | 18 ++++++++++++++++++ src/modbus.h | 3 +++ 3 files changed, 22 insertions(+) diff --git a/src/modbus-private.h b/src/modbus-private.h index 198baeffd..23dec56b2 100644 --- a/src/modbus-private.h +++ b/src/modbus-private.h @@ -101,6 +101,7 @@ struct _modbus { struct timeval indication_timeout; const modbus_backend_t *backend; void *backend_data; + const void *client_context; }; void _modbus_init_common(modbus_t *ctx); diff --git a/src/modbus.c b/src/modbus.c index 4c7a33a76..cd1c2d08a 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1759,6 +1759,24 @@ int modbus_set_debug(modbus_t *ctx, int flag) return 0; } +int modbus_set_client_context(modbus_t *ctx, const void *cctx) +{ + if (ctx == NULL) { + errno = EINVAL; + return -1; + } + + ctx->client_context = cctx; + + return 0; +} + +const void *modbus_get_client_context(modbus_t *ctx) +{ + return ctx->client_context; +} + + /* Allocates 4 arrays to store bits, input bits, registers and inputs registers. The pointers are stored in modbus_mapping structure. diff --git a/src/modbus.h b/src/modbus.h index 24808ead5..2ee82b335 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -238,6 +238,9 @@ MODBUS_API int modbus_reply(modbus_t *ctx, const uint8_t *req, MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code); +MODBUS_API int modbus_set_client_context(modbus_t *ctx, const void *cctx); +MODBUS_API const void* modbus_get_client_context(modbus_t *ctx); + /** * UTILS FUNCTIONS **/ From b4e42f44daadc01c1eb3a29ef3f135205d48b461 Mon Sep 17 00:00:00 2001 From: Rick M Date: Wed, 24 Aug 2022 12:55:11 -0700 Subject: [PATCH 2/3] Added documentation for get/set client context functions --- doc/libmodbus.txt | 3 ++ doc/modbus_get_client_context.txt | 40 +++++++++++++++++++ doc/modbus_set_client_context.txt | 66 +++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 doc/modbus_get_client_context.txt create mode 100644 doc/modbus_set_client_context.txt diff --git a/doc/libmodbus.txt b/doc/libmodbus.txt index 716c684aa..8b293d3c4 100644 --- a/doc/libmodbus.txt +++ b/doc/libmodbus.txt @@ -172,6 +172,9 @@ Set or get float numbers:: - linkmb:modbus_get_float[3] (deprecated) - linkmb:modbus_set_float[3] (deprecated) +Set or get the client context:: + - linkmb:modbus_set_client_context[3] + - linkmb:modbus_get_client_context[3] Connection diff --git a/doc/modbus_get_client_context.txt b/doc/modbus_get_client_context.txt new file mode 100644 index 000000000..d0fb0ed15 --- /dev/null +++ b/doc/modbus_get_client_context.txt @@ -0,0 +1,40 @@ +modbus_get_client_context(3) +============================ + + +NAME +---- +modbus_get_client_context - returns the client context value from the libmodbus +context + + +SYNOPSIS +-------- +*const void *modbus_get_client_context(modbus_t *'ctx');* + + +DESCRIPTION +----------- +The *modbus_get_client_context()* function shall return the client context value +from the libmodbus context. + + +RETURN VALUE +------------ +The function shall return the client context or NULL. + + +ERRORS +------ +This function does not return an error. + + +SEE ALSO +-------- +linkmb:modbus_set_client_context[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/doc/modbus_set_client_context.txt b/doc/modbus_set_client_context.txt new file mode 100644 index 000000000..4712d68bf --- /dev/null +++ b/doc/modbus_set_client_context.txt @@ -0,0 +1,66 @@ +modbus_set_client_context(3) +============================ + + +NAME +---- +modbus_set_client_context - set context information (usually a pointer) for use +by the client + + +SYNOPSIS +-------- +*int modbus_set_client_context(modbus_t *'ctx', void *'cctx')* + + +DESCRIPTION +----------- +The *modbus_set_client_context()* function can be used to set arbitrary client +context information. The actual value is not used by libmodbus, but instead +is available to client callback functions via *modbus_set_client_context()*. + + +RETURN VALUE +------------ +The function shall return 0 if successful. Otherwise it shall return -1 and set +errno to one of the values defined below. + + +ERRORS +------ +*EINVAL*:: +The libmodbus context is NULL. + + +EXAMPLE +------- +.Set and get the client context +[source,cpp] +------------------- +modbus_t *ctx; +uint16_t tab_reg[10]; +struct ClientContext { + int clientData1; + int clientData2; +}; +ClientContext *cctx = new ClientContext(); + +ctx = modbus_new_rtu("/dev/ttyS0", 115200, 'N', 8, 1); +modbus_set_slave(ctx, 1); +modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485); +modbus_set_client_context(ctx, cctx); + +ClientContext *myCCTX = reinterpret_cast(modbus_get_client_context(ctx)); + +modbus_free(ctx); +------------------- + +SEE ALSO +-------- +linkmb:modbus_get_client_context[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + From 3bf12292f7da9c2d872d69049d5d32b91e775ed9 Mon Sep 17 00:00:00 2001 From: Rick M Date: Wed, 24 Aug 2022 13:25:04 -0700 Subject: [PATCH 3/3] Minor formatting --- docs/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.md b/docs/index.md index a84167dff..a63859edb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -206,6 +206,7 @@ Information about header: - [modbus_get_header_length](modbus_get_header_length) Set or get the client data:: + - [modbus_set_client_context](modbus_set_client_context) - [modbus_get_client_context](modbus_get_client_context)