Skip to content

Commit 75479f5

Browse files
parthitcecarlescufi
authored andcommitted
modbus: add user data for adu callback
add user data for adu callback, which helps in passing socket and relevant application parameters. Signed-off-by: Parthiban Nallathambi <[email protected]>
1 parent 8c1efa8 commit 75479f5

File tree

9 files changed

+29
-14
lines changed

9 files changed

+29
-14
lines changed

include/zephyr/modbus/modbus.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,12 @@ int modbus_iface_get_by_name(const char *iface_name);
377377
*
378378
* @param iface Modbus RTU interface index
379379
* @param adu Pointer to the RAW ADU struct to send
380+
* @param user_data Pointer to the user data
380381
*
381382
* @retval 0 If transfer was successful
382383
*/
383-
typedef int (*modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu);
384+
typedef int (*modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu,
385+
void *user_data);
384386

385387
/**
386388
* @brief Modbus interface mode
@@ -425,6 +427,11 @@ struct modbus_server_param {
425427
uint8_t unit_id;
426428
};
427429

430+
struct modbus_raw_cb {
431+
modbus_raw_cb_t raw_tx_cb;
432+
void *user_data;
433+
};
434+
428435
/**
429436
* @brief User parameter structure to configure Modbus interface
430437
* as client or server.
@@ -443,7 +450,7 @@ struct modbus_iface_param {
443450
/** Serial support parameter of the interface */
444451
struct modbus_serial_param serial;
445452
/** Pointer to raw ADU callback function */
446-
modbus_raw_cb_t raw_tx_cb;
453+
struct modbus_raw_cb rawcb;
447454
};
448455
};
449456

samples/subsys/modbus/tcp_server/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ static struct modbus_adu tmp_adu;
123123
K_SEM_DEFINE(received, 0, 1);
124124
static int server_iface;
125125

126-
static int server_raw_cb(const int iface, const struct modbus_adu *adu)
126+
static int server_raw_cb(const int iface, const struct modbus_adu *adu,
127+
void *user_data)
127128
{
128129
LOG_DBG("Server raw callback from interface %d", iface);
129130

@@ -147,7 +148,8 @@ const static struct modbus_iface_param server_param = {
147148
.user_cb = &mbs_cbs,
148149
.unit_id = 1,
149150
},
150-
.raw_tx_cb = server_raw_cb,
151+
.rawcb.raw_tx_cb = server_raw_cb,
152+
.rawcb.user_data = NULL
151153
};
152154

153155
static int init_modbus_server(void)

subsys/modbus/modbus_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static struct modbus_serial_config modbus_serial_cfg[] = {
5353

5454
#define DEFINE_MODBUS_RAW_ADU(x, _) { \
5555
.iface_name = "RAW_"#x, \
56-
.raw_tx_cb = NULL, \
56+
.rawcb.raw_tx_cb = NULL, \
5757
.mode = MODBUS_MODE_RAW, \
5858
}
5959

subsys/modbus/modbus_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct modbus_context {
108108
/* Serial line configuration */
109109
struct modbus_serial_config *cfg;
110110
/* RAW TX callback */
111-
modbus_raw_cb_t raw_tx_cb;
111+
struct modbus_raw_cb rawcb;
112112
};
113113
/* MODBUS mode */
114114
enum modbus_mode mode;

subsys/modbus/modbus_raw.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int modbus_raw_tx_adu(struct modbus_context *ctx)
4343
return -ENODEV;
4444
}
4545

46-
ctx->raw_tx_cb(iface, &ctx->tx_adu);
46+
ctx->rawcb.raw_tx_cb(iface, &ctx->tx_adu, ctx->rawcb.user_data);
4747

4848
return 0;
4949
}
@@ -172,7 +172,8 @@ int modbus_raw_init(struct modbus_context *ctx,
172172
return -ENOTSUP;
173173
}
174174

175-
ctx->raw_tx_cb = param.raw_tx_cb;
175+
ctx->rawcb.raw_tx_cb = param.rawcb.raw_tx_cb;
176+
ctx->rawcb.user_data = param.rawcb.user_data;
176177

177178
return 0;
178179
}

tests/subsys/modbus/src/test_modbus.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ void test_holding_reg(void);
4747
void test_diagnostic(void);
4848
void test_client_disable(void);
4949

50-
int client_raw_cb(const int iface, const struct modbus_adu *adu);
51-
int server_raw_cb(const int iface, const struct modbus_adu *adu);
50+
int client_raw_cb(const int iface, const struct modbus_adu *adu,
51+
void *user_data);
52+
int server_raw_cb(const int iface, const struct modbus_adu *adu,
53+
void *user_data);
5254

5355
#endif /* __TEST_MODBUS_H__ */

tests/subsys/modbus/src/test_modbus_client.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ void test_client_setup_raw(void)
289289

290290
client_iface = modbus_iface_get_by_name(iface_name);
291291
client_param.mode = MODBUS_MODE_RAW;
292-
client_param.raw_tx_cb = client_raw_cb;
292+
client_param.rawcb.raw_tx_cb = client_raw_cb;
293+
client_param.rawcb.user_data = NULL;
293294

294295
err = modbus_init_client(client_iface, client_param);
295296
zassert_equal(err, 0, "Failed to configure RAW client");

tests/subsys/modbus/src/test_modbus_raw.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ K_SEM_DEFINE(received, 0, 1);
1616
* Server wants to send the data back.
1717
* We just store them in between and pass them to the client.
1818
*/
19-
int server_raw_cb(const int iface, const struct modbus_adu *adu)
19+
int server_raw_cb(const int iface, const struct modbus_adu *adu,
20+
void *user_data)
2021
{
2122
LOG_DBG("Server raw callback from interface %d", iface);
2223

@@ -38,7 +39,8 @@ int server_raw_cb(const int iface, const struct modbus_adu *adu)
3839
* Client wants to send the data via whatever.
3940
* We just store it in between and submit to the server.
4041
*/
41-
int client_raw_cb(const int iface, const struct modbus_adu *adu)
42+
int client_raw_cb(const int iface, const struct modbus_adu *adu,
43+
void *user_data)
4244
{
4345
uint8_t server_iface = test_get_server_iface();
4446
uint8_t client_iface = test_get_client_iface();

tests/subsys/modbus/src/test_modbus_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void test_server_setup_raw(void)
271271

272272
server_iface = modbus_iface_get_by_name(iface_name);
273273
server_param.mode = MODBUS_MODE_RAW;
274-
server_param.raw_tx_cb = server_raw_cb;
274+
server_param.rawcb.raw_tx_cb = server_raw_cb;
275275

276276
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
277277
err = modbus_init_server(server_iface, server_param);

0 commit comments

Comments
 (0)