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 <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2024-09-18 11:05:54 +02:00 committed by Anas Nashif
commit 5dc810e261
4 changed files with 39 additions and 13 deletions

View file

@ -152,9 +152,9 @@ struct pbuf {
}
/**
* @brief Initialize the packet buffer.
* @brief Initialize the Tx packet buffer.
*
* This function initializes the packet buffer based on provided configuration.
* This function initializes the Tx packet buffer based on provided configuration.
* If the configuration is incorrect, the function will return error.
*
* It is recommended to use PBUF_DEFINE macro for build time initialization.
@ -165,7 +165,23 @@ struct pbuf {
* @retval 0 on success.
* @retval -EINVAL when the input parameter is incorrect.
*/
int pbuf_init(struct pbuf *pb);
int pbuf_tx_init(struct pbuf *pb);
/**
* @brief Initialize the Rx packet buffer.
*
* This function initializes the Rx packet buffer.
* If the configuration is incorrect, the function will return error.
*
* It is recommended to use PBUF_DEFINE macro for build time initialization.
*
* @param pb Pointer to the packed buffer containing
* configuration and data. Configuration has to be
* fixed before the initialization.
* @retval 0 on success.
* @retval -EINVAL when the input parameter is incorrect.
*/
int pbuf_rx_init(struct pbuf *pb);
/**
* @brief Write specified amount of data to the packet buffer.

View file

@ -270,16 +270,14 @@ int icmsg_open(const struct icmsg_config_t *conf,
k_mutex_init(&dev_data->tx_lock);
#endif
int ret = pbuf_init(dev_data->tx_pb);
int ret = pbuf_tx_init(dev_data->tx_pb);
if (ret < 0) {
__ASSERT(false, "Incorrect configuration");
return ret;
}
/* Initialize local copies of rx_pb. */
dev_data->rx_pb->data.wr_idx = 0;
dev_data->rx_pb->data.rd_idx = 0;
(void)pbuf_rx_init(dev_data->rx_pb);
ret = pbuf_write(dev_data->tx_pb, magic, sizeof(magic));

View file

@ -54,7 +54,7 @@ static int validate_cfg(const struct pbuf_cfg *cfg)
return 0;
}
int pbuf_init(struct pbuf *pb)
int pbuf_tx_init(struct pbuf *pb)
{
if (validate_cfg(pb->cfg) != 0) {
return -EINVAL;
@ -77,6 +77,18 @@ int pbuf_init(struct pbuf *pb)
return 0;
}
int pbuf_rx_init(struct pbuf *pb)
{
if (validate_cfg(pb->cfg) != 0) {
return -EINVAL;
}
/* Initialize local copy of indexes. */
pb->data.wr_idx = 0;
pb->data.rd_idx = 0;
return 0;
}
int pbuf_write(struct pbuf *pb, const char *data, uint16_t len)
{
if (pb == NULL || len == 0 || data == NULL) {

View file

@ -58,7 +58,7 @@ ZTEST(test_pbuf, test_rw)
write_buf[i] = i+1;
}
zassert_equal(pbuf_init(&pb), 0);
zassert_equal(pbuf_tx_init(&pb), 0);
/* Write MSGA_SZ bytes packet. */
ret = pbuf_write(&pb, write_buf, MSGA_SZ);
@ -132,9 +132,9 @@ ZTEST(test_pbuf, test_retcodes)
};
/* Initialize buffers. */
zassert_equal(pbuf_init(&pb0), 0);
zassert_equal(pbuf_init(&pb1), 0);
zassert_equal(pbuf_init(&pb2), 0);
zassert_equal(pbuf_tx_init(&pb0), 0);
zassert_equal(pbuf_tx_init(&pb1), 0);
zassert_equal(pbuf_tx_init(&pb2), 0);
print_pbuf_info(&pb0);
print_pbuf_info(&pb1);
@ -274,7 +274,7 @@ ZTEST(test_pbuf, test_stress)
.cfg = &cfg,
};
zassert_equal(pbuf_init(&pb), 0);
zassert_equal(pbuf_tx_init(&pb), 0);
ctx.pbuf = &pb;
ctx.wr_cnt = 0;
ctx.rd_cnt = 0;