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

Added a client context parameter to the modbus_t context, and getter/setter methods. #646

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ 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)

## Data handling

Macros for data manipulation:
Expand Down
38 changes: 38 additions & 0 deletions docs/modbus_get_client_context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# modbus_get_client_context


## Name

`modbus_get_client_context` - returns the client context value from the libmodbus
context


## Synopsis

```c
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.

Do not be confused by the overloaded use of “client” and “context” in these
functions. Here, “client” refers to the code that makes use of `libmodbus`,
“client context” is the data set and get by the client code, and “libmodbus context”
is the `modbus_t*` context pointer.

## Return Value

The function shall return the client context or NULL.


## Errors

This function does not return an error.


## See also

- [`modbus_set_client_context()`](modbus_set_client_context)
61 changes: 61 additions & 0 deletions docs/modbus_set_client_context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# modbus_set_client_context


## Name

`modbus_set_client_context` - set context information (usually a pointer) for use
by the client


## Synopsis

```c
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_get_client_context()`.

Do not be confused by the overloaded use of “client” and “context” in these
functions. Here, “client” refers to the code that makes use of libmodbus and
“client context” is the data used by the client code.

## 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

```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<ClientContext*>(modbus_get_client_context(ctx));

modbus_free(ctx);
```

## See also

[`modbus_get_client_context()`](modbus_get_client_context)
1 change: 1 addition & 0 deletions src/modbus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,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.

Expand Down
3 changes: 3 additions & 0 deletions src/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/
Expand Down