Skip to content

Commit 4ab7e37

Browse files
aescolargmarull
authored andcommitted
[nrf fromtree] ipc: pbuf: Provide function for Rx side initialization
Provide a new function for initializing the Rx side, so users do not need to initialize the pointers by hand if they did not use PBUF_DEFINE(). Let's also rename pbuf_init() to pbuf_tx_init() to clearly signify the previous function was only meant to be used by the Tx side. Signed-off-by: Alberto Escolar Piedras <[email protected]> (cherry picked from commit 5dc810e)
1 parent 64aa257 commit 4ab7e37

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

include/zephyr/ipc/pbuf.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ struct pbuf {
152152
}
153153

154154
/**
155-
* @brief Initialize the packet buffer.
155+
* @brief Initialize the Tx packet buffer.
156156
*
157-
* This function initializes the packet buffer based on provided configuration.
157+
* This function initializes the Tx packet buffer based on provided configuration.
158158
* If the configuration is incorrect, the function will return error.
159159
*
160160
* It is recommended to use PBUF_DEFINE macro for build time initialization.
@@ -165,7 +165,23 @@ struct pbuf {
165165
* @retval 0 on success.
166166
* @retval -EINVAL when the input parameter is incorrect.
167167
*/
168-
int pbuf_init(struct pbuf *pb);
168+
int pbuf_tx_init(struct pbuf *pb);
169+
170+
/**
171+
* @brief Initialize the Rx packet buffer.
172+
*
173+
* This function initializes the Rx packet buffer.
174+
* If the configuration is incorrect, the function will return error.
175+
*
176+
* It is recommended to use PBUF_DEFINE macro for build time initialization.
177+
*
178+
* @param pb Pointer to the packed buffer containing
179+
* configuration and data. Configuration has to be
180+
* fixed before the initialization.
181+
* @retval 0 on success.
182+
* @retval -EINVAL when the input parameter is incorrect.
183+
*/
184+
int pbuf_rx_init(struct pbuf *pb);
169185

170186
/**
171187
* @brief Write specified amount of data to the packet buffer.

subsys/ipc/ipc_service/lib/icmsg.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,14 @@ int icmsg_open(const struct icmsg_config_t *conf,
270270
k_mutex_init(&dev_data->tx_lock);
271271
#endif
272272

273-
int ret = pbuf_init(dev_data->tx_pb);
273+
int ret = pbuf_tx_init(dev_data->tx_pb);
274274

275275
if (ret < 0) {
276276
__ASSERT(false, "Incorrect configuration");
277277
return ret;
278278
}
279279

280-
/* Initialize local copies of rx_pb. */
281-
dev_data->rx_pb->data.wr_idx = 0;
282-
dev_data->rx_pb->data.rd_idx = 0;
280+
(void)pbuf_rx_init(dev_data->rx_pb);
283281

284282
ret = pbuf_write(dev_data->tx_pb, magic, sizeof(magic));
285283

subsys/ipc/ipc_service/lib/pbuf.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int validate_cfg(const struct pbuf_cfg *cfg)
5454
return 0;
5555
}
5656

57-
int pbuf_init(struct pbuf *pb)
57+
int pbuf_tx_init(struct pbuf *pb)
5858
{
5959
if (validate_cfg(pb->cfg) != 0) {
6060
return -EINVAL;
@@ -77,6 +77,18 @@ int pbuf_init(struct pbuf *pb)
7777
return 0;
7878
}
7979

80+
int pbuf_rx_init(struct pbuf *pb)
81+
{
82+
if (validate_cfg(pb->cfg) != 0) {
83+
return -EINVAL;
84+
}
85+
/* Initialize local copy of indexes. */
86+
pb->data.wr_idx = 0;
87+
pb->data.rd_idx = 0;
88+
89+
return 0;
90+
}
91+
8092
int pbuf_write(struct pbuf *pb, const char *data, uint16_t len)
8193
{
8294
if (pb == NULL || len == 0 || data == NULL) {

tests/subsys/ipc/pbuf/src/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ZTEST(test_pbuf, test_rw)
5858
write_buf[i] = i+1;
5959
}
6060

61-
zassert_equal(pbuf_init(&pb), 0);
61+
zassert_equal(pbuf_tx_init(&pb), 0);
6262

6363
/* Write MSGA_SZ bytes packet. */
6464
ret = pbuf_write(&pb, write_buf, MSGA_SZ);
@@ -132,9 +132,9 @@ ZTEST(test_pbuf, test_retcodes)
132132
};
133133

134134
/* Initialize buffers. */
135-
zassert_equal(pbuf_init(&pb0), 0);
136-
zassert_equal(pbuf_init(&pb1), 0);
137-
zassert_equal(pbuf_init(&pb2), 0);
135+
zassert_equal(pbuf_tx_init(&pb0), 0);
136+
zassert_equal(pbuf_tx_init(&pb1), 0);
137+
zassert_equal(pbuf_tx_init(&pb2), 0);
138138

139139
print_pbuf_info(&pb0);
140140
print_pbuf_info(&pb1);
@@ -274,7 +274,7 @@ ZTEST(test_pbuf, test_stress)
274274
.cfg = &cfg,
275275
};
276276

277-
zassert_equal(pbuf_init(&pb), 0);
277+
zassert_equal(pbuf_tx_init(&pb), 0);
278278
ctx.pbuf = &pb;
279279
ctx.wr_cnt = 0;
280280
ctx.rd_cnt = 0;

0 commit comments

Comments
 (0)