Skip to content

Commit

Permalink
rpmsg_ping.c: change msg cmd type for more compatible
Browse files Browse the repository at this point in the history
Use bit mask method to represent the command, because it's more
convenient to express multiple characteristics simultaneously.

Signed-off-by: Yongrong Wang <[email protected]>
  • Loading branch information
wyr-7 authored and GUIDINGLI committed Oct 11, 2024
1 parent 1cf97ac commit 4b77dd0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 46 deletions.
99 changes: 54 additions & 45 deletions drivers/rpmsg/rpmsg_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/lib/lib.h>
#include <nuttx/signal.h>

#include <inttypes.h>
Expand All @@ -39,11 +40,14 @@
****************************************************************************/

#define RPMSG_PING_EPT_NAME "rpmsg-ping"
#define RPMSG_PING_SEND 1
#define RPMSG_PING_SEND_CHECK 2
#define RPMSG_PING_SEND_NOACK 3
#define RPMSG_PING_SEND_ACK 4
#define RPMSG_PING_ACK 5

#define RPMSG_PING_ACK_MASK 0x00000001
#define RPMSG_PING_CHECK_MASK 0x00000002
#define RPMSG_PING_RANDOMLEN_MASK 0x00000004

#define RPMSG_PING_CMD_MASK 0x000000f0
# define RPMSG_PING_CMD_REQ 0x00000000
# define RPMSG_PING_CMD_RSP 0x00000020

/****************************************************************************
* Private Types
Expand All @@ -68,12 +72,22 @@ static int rpmsg_ping_ept_cb(FAR struct rpmsg_endpoint *ept,
FAR struct rpmsg_ping_msg_s *msg = data;
FAR sem_t *sem = (FAR sem_t *)(uintptr_t)msg->cookie;

if (msg->cmd == RPMSG_PING_SEND)
/* Handle the ping response */

if ((msg->cmd & RPMSG_PING_CMD_MASK) == RPMSG_PING_CMD_RSP)
{
nxsem_post(sem);
return 0;
}

/* Handle the ping request */

if ((msg->cmd & RPMSG_PING_ACK_MASK) == 0)
{
msg->cmd = RPMSG_PING_ACK;
rpmsg_send(ept, msg, len);
return 0;
}
else if (msg->cmd == RPMSG_PING_SEND_CHECK)

if ((msg->cmd & RPMSG_PING_CHECK_MASK) != 0)
{
size_t data_len;
size_t i;
Expand All @@ -90,25 +104,24 @@ static int rpmsg_ping_ept_cb(FAR struct rpmsg_endpoint *ept,

msg->data[i] = 0;
}

msg->cmd = RPMSG_PING_ACK;
rpmsg_send(ept, msg, len);
}
else if (msg->cmd == RPMSG_PING_SEND_ACK)

if ((msg->cmd & RPMSG_PING_CMD_MASK) == RPMSG_PING_CMD_REQ)
{
msg->cmd = RPMSG_PING_ACK;
rpmsg_send(ept, msg, sizeof(*msg));
msg->len = len;
}
else if (msg->cmd == RPMSG_PING_ACK)
else
{
nxsem_post(sem);
msg->len = sizeof(*msg);
}

msg->cmd = RPMSG_PING_CMD_RSP;
rpmsg_send(ept, msg, msg->len);
return 0;
}

static int rpmsg_ping_once(FAR struct rpmsg_endpoint *ept, int len,
int ack, uint32_t *buf_len, char i)
int cmd, uint32_t *buf_len, char i)
{
FAR struct rpmsg_ping_msg_s *msg;
int ret;
Expand All @@ -122,36 +135,34 @@ static int rpmsg_ping_once(FAR struct rpmsg_endpoint *ept, int len,
len = MAX(len, sizeof(struct rpmsg_ping_msg_s));
len = MIN(len, *buf_len);

memset(msg, 0, len);
msg->cmd = cmd;

if (ack)
if ((msg->cmd & RPMSG_PING_RANDOMLEN_MASK) != 0)
{
sem_t sem;

if (ack == 1)
{
msg->cmd = RPMSG_PING_SEND;
}
else if (ack == 2)
{
msg->cmd = RPMSG_PING_SEND_CHECK;
}
else
{
msg->cmd = RPMSG_PING_SEND_ACK;
}
msg->len = nrand(len);
}
else
{
msg->len = len;
}

msg->len = len;
msg->cookie = (uintptr_t)&sem;
if ((msg->cmd & RPMSG_PING_CHECK_MASK) != 0)
{
memset(msg->data, i, msg->len - sizeof(struct rpmsg_ping_msg_s) + 1);
}
else
{
memset(msg->data, 0, msg->len);
}

if (msg->cmd == RPMSG_PING_SEND_CHECK)
{
memset(msg->data, i, len - sizeof(struct rpmsg_ping_msg_s) + 1);
}
if ((msg->cmd & RPMSG_PING_ACK_MASK) != 0)
{
sem_t sem;

msg->cookie = (uintptr_t)&sem;
nxsem_init(&sem, 0, 0);

ret = rpmsg_send_nocopy(ept, msg, len);
ret = rpmsg_send_nocopy(ept, msg, msg->len);
if (ret >= 0)
{
nxsem_wait_uninterruptible(&sem);
Expand All @@ -161,9 +172,7 @@ static int rpmsg_ping_once(FAR struct rpmsg_endpoint *ept, int len,
}
else
{
msg->cmd = RPMSG_PING_SEND_NOACK;
msg->len = len;
ret = rpmsg_send_nocopy(ept, msg, len);
ret = rpmsg_send_nocopy(ept, msg, msg->len);
}

if (ret < 0)
Expand Down Expand Up @@ -226,7 +235,7 @@ int rpmsg_ping(FAR struct rpmsg_endpoint *ept,
{
clock_t tm = perf_gettime();

send_len = rpmsg_ping_once(ept, ping->len, ping->ack, &buf_len, i);
send_len = rpmsg_ping_once(ept, ping->len, ping->cmd, &buf_len, i);
if (send_len < 0)
{
return send_len;
Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/rpmsg/rpmsg_ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct rpmsg_ping_s
{
int times;
int len;
int ack;
int cmd;
int sleep; /* unit: ms */
};

Expand Down

0 comments on commit 4b77dd0

Please sign in to comment.