Skip to content

Commit

Permalink
rpmsg_socket: add shutdown support
Browse files Browse the repository at this point in the history
Signed-off-by: ligd <[email protected]>
  • Loading branch information
GUIDINGLI authored and yintao committed Oct 8, 2024
1 parent 0b38595 commit d3167d1
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions net/rpmsg/rpmsg_sockif.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

#define RPMSG_SOCKET_CMD_SYNC 1
#define RPMSG_SOCKET_CMD_DATA 2
#define RPMSG_SOCKET_CMD_SHUTDOWN 3
#define RPMSG_SOCKET_NAME_PREFIX "sk:"
#define RPMSG_SOCKET_NAME_PREFIX_LEN 3
#define RPMSG_SOCKET_NAME_ID_LEN 13
Expand Down Expand Up @@ -83,6 +84,12 @@ begin_packed_struct struct rpmsg_socket_data_s
char data[0];
} end_packed_struct;

begin_packed_struct struct rpmsg_socket_shutdown_s
{
uint32_t cmd;
uint32_t how;
} end_packed_struct;

struct rpmsg_socket_conn_s
{
/* Common prologue of all connection structures. */
Expand All @@ -95,6 +102,7 @@ struct rpmsg_socket_conn_s
struct sockaddr_rpmsg rpaddr;
char nameid[RPMSG_SOCKET_NAME_ID_LEN];
uint16_t crefs;
uint32_t how;

FAR struct pollfd *fds[CONFIG_NET_RPMSG_NPOLLWAITERS];
mutex_t polllock;
Expand Down Expand Up @@ -168,6 +176,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock,
static int rpmsg_socket_close(FAR struct socket *psock);
static int rpmsg_socket_ioctl(FAR struct socket *psock,
int cmd, unsigned long arg);
static int rpmsg_socket_shutdown(FAR struct socket *psock, int how);
#ifdef CONFIG_NET_SOCKOPTS
static int rpmsg_socket_getsockopt(FAR struct socket *psock,
int level, int option,
Expand Down Expand Up @@ -196,7 +205,7 @@ const struct sock_intf_s g_rpmsg_sockif =
rpmsg_socket_close, /* si_close */
rpmsg_socket_ioctl, /* si_ioctl */
NULL, /* si_socketpair */
NULL /* si_shutdown */
rpmsg_socket_shutdown /* si_shutdown */
#ifdef CONFIG_NET_SOCKOPTS
, rpmsg_socket_getsockopt /* si_getsockopt */
, NULL /* si_setsockopt */
Expand Down Expand Up @@ -331,7 +340,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept,
rpmsg_socket_poll_notify(conn, POLLOUT);
nxmutex_unlock(&conn->recvlock);
}
else
else if (head->cmd == RPMSG_SOCKET_CMD_DATA)
{
FAR struct rpmsg_socket_data_s *msg = data;
FAR uint8_t *buf = (FAR uint8_t *)msg->data;
Expand Down Expand Up @@ -399,6 +408,26 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept,
nxmutex_unlock(&conn->recvlock);
}
}
else if (head->cmd == RPMSG_SOCKET_CMD_SHUTDOWN)
{
FAR struct rpmsg_socket_shutdown_s *msg = data;

if (msg->how & SHUT_WR)
{
conn->how |= SHUT_RD;

rpmsg_socket_post(&conn->recvsem);
rpmsg_socket_poll_notify(conn, POLLIN | POLLHUP);
}

if (msg->how & SHUT_RD)
{
conn->how |= SHUT_WR;

rpmsg_socket_post(&conn->sendsem);
rpmsg_socket_poll_notify(conn, POLLOUT | POLLHUP);
}
}

return 0;
}
Expand Down Expand Up @@ -1212,6 +1241,11 @@ static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock,
return -ECONNRESET;
}

if (conn->how & SHUT_WR)
{
return -EPIPE;
}

nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) ||
(flags & MSG_DONTWAIT) != 0;

Expand Down Expand Up @@ -1250,6 +1284,11 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock,
return -EISCONN;
}

if (conn->how & SHUT_RD)
{
return 0;
}

nxmutex_lock(&conn->recvlock);

if (psock->s_type != SOCK_STREAM)
Expand Down Expand Up @@ -1416,6 +1455,29 @@ static int rpmsg_socket_ioctl(FAR struct socket *psock,
return ret;
}

static int rpmsg_socket_shutdown(FAR struct socket *psock, int how)
{
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
struct rpmsg_socket_shutdown_s msg;
int ret;

if (!conn->ept.rdev || conn->unbind)
{
return -ENOTCONN;
}

conn->how |= how;

msg.cmd = RPMSG_SOCKET_CMD_SHUTDOWN;
msg.how = how;

ret = rpmsg_send(&conn->ept, &msg, sizeof(msg));
if (ret < 0)
return ret;

return OK;
}

#ifdef CONFIG_NET_SOCKOPTS
static int rpmsg_socket_getsockopt(FAR struct socket *psock, int level,
int option, FAR void *value,
Expand Down

0 comments on commit d3167d1

Please sign in to comment.