diff --git a/include/netpacket/rpmsg.h b/include/netpacket/rpmsg.h index 1ae5a5035416f..09848479d2087 100644 --- a/include/netpacket/rpmsg.h +++ b/include/netpacket/rpmsg.h @@ -34,7 +34,7 @@ ****************************************************************************/ #define RPMSG_SOCKET_CPU_SIZE 16 -#define RPMSG_SOCKET_NAME_SIZE 16 +#define RPMSG_SOCKET_NAME_SIZE 108 /**************************************************************************** * Public Type Definitions diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c index d3302c47a1f00..8e040d33cac9a 100644 --- a/net/rpmsg/rpmsg_sockif.c +++ b/net/rpmsg/rpmsg_sockif.c @@ -27,15 +27,16 @@ #include #include +#include #include #include #include #include #include -#include #include #include +#include #include #include #include @@ -53,12 +54,9 @@ #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 - -static_assert(RPMSG_SOCKET_NAME_SIZE + RPMSG_SOCKET_NAME_PREFIX_LEN - <= RPMSG_NAME_SIZE, "socket name size config error"); +#define RPMSG_SOCKET_NAME_PREFIX "s:" +#define RPMSG_SOCKET_NAME_PREFIX_LEN 2 +#define RPMSG_SOCKET_NAME_LEN 10 /**************************************************************************** * Private Types @@ -81,7 +79,7 @@ begin_packed_struct struct rpmsg_socket_data_s /* Act data len, don't include len itself when SOCK_DGRAM */ uint32_t len; - char data[0]; + uint8_t data[0]; } end_packed_struct; begin_packed_struct struct rpmsg_socket_shutdown_s @@ -100,7 +98,7 @@ struct rpmsg_socket_conn_s struct rpmsg_endpoint ept; struct sockaddr_rpmsg rpaddr; - char nameid[RPMSG_SOCKET_NAME_ID_LEN]; + char nameid[RPMSG_SOCKET_NAME_LEN]; uint16_t crefs; uint32_t how; @@ -120,8 +118,6 @@ struct rpmsg_socket_conn_s FAR struct rpmsg_socket_conn_s *next; /* server listen-scoket listening: backlog > 0; - * server listen-scoket closed: backlog = -1; - * accept scoket: backlog = -2; * others: backlog = 0; */ @@ -224,7 +220,7 @@ const struct sock_intf_s g_rpmsg_sockif = * Private Data ****************************************************************************/ -static uint64_t g_rpmsg_id; +static uint32_t g_rpmsg_id; /**************************************************************************** * Private Functions @@ -241,9 +237,9 @@ static inline void rpmsg_socket_post(FAR sem_t *sem) } } -static inline void rpmsg_socket_poll_notify( - FAR struct rpmsg_socket_conn_s *conn, - pollevent_t eventset) +static inline void +rpmsg_socket_poll_notify(FAR struct rpmsg_socket_conn_s *conn, + pollevent_t eventset) { nxmutex_lock(&conn->polllock); poll_notify(conn->fds, CONFIG_NET_RPMSG_NPOLLWAITERS, eventset); @@ -316,12 +312,11 @@ static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn) } nxmutex_unlock(&conn->recvlock); - return ret ? rpmsg_send(&conn->ept, &msg, sizeof(msg)) : 0; } -static inline uint32_t rpmsg_socket_get_space( - FAR struct rpmsg_socket_conn_s *conn) +static inline uint32_t +rpmsg_socket_get_space(FAR struct rpmsg_socket_conn_s *conn) { return conn->sendsize - (conn->sendpos - conn->ackpos); } @@ -335,29 +330,26 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, if (head->cmd == RPMSG_SOCKET_CMD_SYNC) { - nxmutex_lock(&conn->recvlock); + nxmutex_lock(&conn->sendlock); conn->sendsize = head->size; conn->cred.pid = head->pid; conn->cred.uid = head->uid; conn->cred.gid = head->gid; conn->sconn.s_flags |= _SF_CONNECTED; - _SO_CONN_SETERRNO(conn, OK); rpmsg_socket_post(&conn->sendsem); rpmsg_socket_poll_notify(conn, POLLOUT); - nxmutex_unlock(&conn->recvlock); + nxmutex_unlock(&conn->sendlock); } 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; + FAR uint8_t *buf = msg->data; nxmutex_lock(&conn->sendlock); - conn->ackpos = msg->pos; - if (rpmsg_socket_get_space(conn) > 0) { rpmsg_socket_post(&conn->sendsem); @@ -365,19 +357,15 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, } nxmutex_unlock(&conn->sendlock); - if (len > sizeof(*msg)) { len -= sizeof(*msg); - DEBUGASSERT(len == msg->len || len == msg->len + sizeof(uint32_t)); nxmutex_lock(&conn->recvlock); - if (conn->recvdata) { conn->recvlen = MIN(conn->recvlen, msg->len); - if (len == msg->len) { /* SOCK_STREAM */ @@ -423,18 +411,20 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, if (msg->how & SHUT_WR) { + nxmutex_lock(&conn->recvlock); conn->how |= SHUT_RD; - rpmsg_socket_post(&conn->recvsem); rpmsg_socket_poll_notify(conn, POLLIN | POLLHUP); + nxmutex_unlock(&conn->recvlock); } if (msg->how & SHUT_RD) { + nxmutex_lock(&conn->sendlock); conn->how |= SHUT_WR; - rpmsg_socket_post(&conn->sendsem); rpmsg_socket_poll_notify(conn, POLLOUT | POLLHUP); + nxmutex_unlock(&conn->sendlock); } } @@ -454,14 +444,6 @@ static inline void rpmsg_socket_destroy_ept( if (conn->ept.rdev) { - if (conn->backlog > 0) - { - /* Listen socket */ - - conn->backlog = -1; - } - - rpmsg_destroy_ept(&conn->ept); rpmsg_socket_post(&conn->sendsem); rpmsg_socket_post(&conn->recvsem); rpmsg_socket_poll_notify(conn, POLLIN | POLLOUT); @@ -469,6 +451,7 @@ static inline void rpmsg_socket_destroy_ept( nxmutex_unlock(&conn->sendlock); nxmutex_unlock(&conn->recvlock); + rpmsg_destroy_ept(&conn->ept); } static void rpmsg_socket_ns_bound(struct rpmsg_endpoint *ept) @@ -489,11 +472,6 @@ static void rpmsg_socket_ns_unbind(FAR struct rpmsg_endpoint *ept) { FAR struct rpmsg_socket_conn_s *conn = ept->priv; - if (!conn) - { - return; - } - nxmutex_lock(&conn->recvlock); conn->unbind = true; @@ -504,11 +482,42 @@ static void rpmsg_socket_ns_unbind(FAR struct rpmsg_endpoint *ept) nxmutex_unlock(&conn->recvlock); } +static void rpmsg_socket_ept_release(FAR struct rpmsg_endpoint *ept) +{ + FAR struct rpmsg_socket_conn_s *conn = ept->priv; + + /* Release conn only when close is called */ + + if (!conn->crefs) + { + rpmsg_socket_free(conn); + } +} + +static void rpmsg_socket_format_name(FAR struct rpmsg_socket_conn_s *conn, + FAR char *namebuf) +{ + if (strlen(conn->rpaddr.rp_name) > RPMSG_SOCKET_NAME_LEN) + { + uint32_t crc = crc32((const uint8_t *)conn->rpaddr.rp_name, + strlen(conn->rpaddr.rp_name)); + snprintf(namebuf, RPMSG_NAME_SIZE, "%s%.2s%08" PRIx32 "%s", + RPMSG_SOCKET_NAME_PREFIX, conn->rpaddr.rp_name, + crc, conn->nameid); + } + else + { + snprintf(namebuf, RPMSG_NAME_SIZE, "%s%.*s%s", + RPMSG_SOCKET_NAME_PREFIX, RPMSG_SOCKET_NAME_LEN, + conn->rpaddr.rp_name, conn->nameid); + } +} + static void rpmsg_socket_device_created(FAR struct rpmsg_device *rdev, FAR void *priv) { FAR struct rpmsg_socket_conn_s *conn = priv; - char buf[RPMSG_NAME_SIZE]; + char namebuf[RPMSG_NAME_SIZE]; if (conn->ept.rdev) { @@ -519,42 +528,30 @@ static void rpmsg_socket_device_created(FAR struct rpmsg_device *rdev, { conn->ept.priv = conn; conn->ept.ns_bound_cb = rpmsg_socket_ns_bound; - snprintf(buf, sizeof(buf), "%s%s%s", RPMSG_SOCKET_NAME_PREFIX, - conn->rpaddr.rp_name, conn->nameid); + conn->ept.release_cb = rpmsg_socket_ept_release; + rpmsg_socket_format_name(conn, namebuf); - rpmsg_create_ept(&conn->ept, rdev, buf, + rpmsg_create_ept(&conn->ept, rdev, namebuf, RPMSG_ADDR_ANY, RPMSG_ADDR_ANY, rpmsg_socket_ept_cb, rpmsg_socket_ns_unbind); } } -static void rpmsg_socket_device_destroy(FAR struct rpmsg_device *rdev, - FAR void *priv) -{ - FAR struct rpmsg_socket_conn_s *conn = priv; - - if (strcmp(conn->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev)) == 0) - { - rpmsg_socket_destroy_ept(conn); - } -} - static bool rpmsg_socket_ns_match(FAR struct rpmsg_device *rdev, FAR void *priv, FAR const char *name, uint32_t dest) { FAR struct rpmsg_socket_conn_s *server = priv; - char buf[RPMSG_NAME_SIZE]; + char namebuf[RPMSG_NAME_SIZE]; - snprintf(buf, sizeof(buf), "%s%s", RPMSG_SOCKET_NAME_PREFIX, - server->rpaddr.rp_name); - if (strncmp(name, buf, strlen(buf))) + rpmsg_socket_format_name(server, namebuf); + if (strncmp(name, namebuf, strlen(namebuf))) { return false; } - if (strlen(server->rpaddr.rp_cpu) && - strcmp(server->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev))) + if (server->rpaddr.rp_cpu[0] && + strcmp(server->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev))) { /* Bind specific CPU, then only listen that CPU */ @@ -588,6 +585,7 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, } new->ept.priv = new; + new->ept.release_cb = rpmsg_socket_ept_release; ret = rpmsg_create_ept(&new->ept, rdev, name, RPMSG_ADDR_ANY, dest, rpmsg_socket_ept_cb, rpmsg_socket_ns_unbind); @@ -606,7 +604,6 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, rpmsg_socket_ns_bound(&new->ept); nxmutex_lock(&server->recvlock); - for (tmp = server; tmp->next; tmp = tmp->next) { if (++cnt >= server->backlog) @@ -622,10 +619,9 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, tmp->next = new; - nxmutex_unlock(&server->recvlock); - rpmsg_socket_post(&server->recvsem); rpmsg_socket_poll_notify(server, POLLIN); + nxmutex_unlock(&server->recvlock); } static int rpmsg_socket_getaddr(FAR struct rpmsg_socket_conn_s *conn, @@ -659,7 +655,7 @@ static int rpmsg_socket_setaddr(FAR struct rpmsg_socket_conn_s *conn, if (suffix) { - snprintf(conn->nameid, sizeof(conn->nameid), ":%" PRIx64, + snprintf(conn->nameid, sizeof(conn->nameid), ":%" PRIx32, g_rpmsg_id++); } else @@ -761,7 +757,7 @@ static int rpmsg_socket_connect_internal(FAR struct socket *psock) ret = rpmsg_register_callback(conn, rpmsg_socket_device_created, - rpmsg_socket_device_destroy, + NULL, NULL, NULL); if (ret < 0) @@ -769,30 +765,31 @@ static int rpmsg_socket_connect_internal(FAR struct socket *psock) return ret; } - nxmutex_lock(&conn->recvlock); + nxmutex_lock(&conn->sendlock); if (conn->sendsize == 0) { - nxmutex_unlock(&conn->recvlock); + nxmutex_unlock(&conn->sendlock); if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { return -EINPROGRESS; } ret = net_sem_timedwait(&conn->sendsem, - _SO_TIMEOUT(conn->sconn.s_rcvtimeo)); + _SO_TIMEOUT(conn->sconn.s_sndtimeo)); if (ret < 0) { rpmsg_unregister_callback(conn, rpmsg_socket_device_created, - rpmsg_socket_device_destroy, + NULL, NULL, NULL); + rpmsg_socket_destroy_ept(conn); } } else { - nxmutex_unlock(&conn->recvlock); + nxmutex_unlock(&conn->sendlock); } return ret; @@ -829,11 +826,6 @@ static int rpmsg_socket_accept(FAR struct socket *psock, FAR struct rpmsg_socket_conn_s *server = psock->s_conn; int ret = 0; - if (server->backlog == -1) - { - return -ECONNRESET; - } - if (!_SS_ISLISTENING(server->sconn.s_flags)) { return -EINVAL; @@ -856,13 +848,6 @@ static int rpmsg_socket_accept(FAR struct socket *psock, if (conn) { - conn->backlog = -2; - rpmsg_register_callback(conn, - NULL, - rpmsg_socket_device_destroy, - NULL, - NULL); - if (conn->sendsize == 0) { net_sem_wait(&conn->sendsem); @@ -886,11 +871,6 @@ static int rpmsg_socket_accept(FAR struct socket *psock, else { ret = net_sem_wait(&server->recvsem); - if (server->backlog == -1) - { - ret = -ECONNRESET; - } - if (ret < 0) { break; @@ -907,13 +887,11 @@ static int rpmsg_socket_poll(FAR struct socket *psock, { FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; pollevent_t eventset = 0; - int ret = 0; int i; if (setup) { nxmutex_lock(&conn->polllock); - for (i = 0; i < CONFIG_NET_RPMSG_NPOLLWAITERS; i++) { /* Find an available slot */ @@ -929,28 +907,23 @@ static int rpmsg_socket_poll(FAR struct socket *psock, } nxmutex_unlock(&conn->polllock); - if (i >= CONFIG_NET_RPMSG_NPOLLWAITERS) { fds->priv = NULL; - ret = -EBUSY; - goto errout; + return -EBUSY; } /* Immediately notify on any of the requested events */ if (_SS_ISLISTENING(conn->sconn.s_flags)) { - if (conn->backlog == -1) - { - ret = -ECONNRESET; - goto errout; - } - + nxmutex_lock(&conn->recvlock); if (conn->next) { eventset |= POLLIN; } + + nxmutex_unlock(&conn->recvlock); } else if (_SS_ISCONNECTED(conn->sconn.s_flags)) { @@ -960,7 +933,6 @@ static int rpmsg_socket_poll(FAR struct socket *psock, } nxmutex_lock(&conn->sendlock); - if (rpmsg_socket_get_space(conn) > 0) { eventset |= POLLOUT; @@ -969,7 +941,6 @@ static int rpmsg_socket_poll(FAR struct socket *psock, nxmutex_unlock(&conn->sendlock); nxmutex_lock(&conn->recvlock); - if (!circbuf_is_empty(&conn->recvbuf)) { eventset |= POLLIN; @@ -983,10 +954,6 @@ static int rpmsg_socket_poll(FAR struct socket *psock, { eventset |= POLLHUP; } - else - { - ret = OK; - } } poll_notify(&fds, 1, eventset); @@ -994,7 +961,6 @@ static int rpmsg_socket_poll(FAR struct socket *psock, else { nxmutex_lock(&conn->polllock); - if (fds->priv != NULL) { for (i = 0; i < CONFIG_NET_RPMSG_NPOLLWAITERS; i++) @@ -1011,8 +977,7 @@ static int rpmsg_socket_poll(FAR struct socket *psock, nxmutex_unlock(&conn->polllock); } -errout: - return ret; + return OK; } static uint32_t rpmsg_socket_get_iovlen(FAR const struct iovec *buf, @@ -1050,23 +1015,21 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, if (block == 0) { - if (!nonblock) + if (nonblock) { - ret = net_sem_timedwait(&conn->sendsem, - _SO_TIMEOUT(conn->sconn.s_sndtimeo)); - if (!conn->ept.rdev || conn->unbind) - { - ret = -ECONNRESET; - } + ret = -EAGAIN; + break; + } - if (ret < 0) - { - break; - } + ret = net_sem_timedwait(&conn->sendsem, + _SO_TIMEOUT(conn->sconn.s_sndtimeo)); + if (!conn->ept.rdev || conn->unbind) + { + ret = -ECONNRESET; } - else + + if (ret < 0) { - ret = -EAGAIN; break; } @@ -1081,7 +1044,6 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, } nxmutex_lock(&conn->sendlock); - block = MIN(len - written, rpmsg_socket_get_space(conn)); block = MIN(block, ipcsize - sizeof(*msg)); @@ -1092,7 +1054,7 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, { uint32_t chunk = MIN(block - block_written, buf->iov_len - offset); memcpy(msg->data + block_written, - (FAR const char *)buf->iov_base + offset, chunk); + (FAR const uint8_t *)buf->iov_base + offset, chunk); offset += chunk; if (offset == buf->iov_len) { @@ -1128,11 +1090,11 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; FAR struct rpmsg_socket_data_s *msg; uint32_t len = rpmsg_socket_get_iovlen(buf, iovcnt); - uint32_t total = len + sizeof(*msg) + sizeof(uint32_t); + uint32_t total = len + sizeof(uint32_t); uint32_t written = 0; uint32_t ipcsize; uint32_t space; - char *msgpos; + FAR uint8_t *msgpos; int ret; if (total > conn->sendsize) @@ -1146,26 +1108,26 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, space = rpmsg_socket_get_space(conn); nxmutex_unlock(&conn->sendlock); - if (space >= total - sizeof(*msg)) + if (space >= total) + { break; + } - if (!nonblock) + if (nonblock) { - ret = net_sem_timedwait(&conn->sendsem, - _SO_TIMEOUT(conn->sconn.s_sndtimeo)); - if (!conn->ept.rdev || conn->unbind) - { - ret = -ECONNRESET; - } + return -EAGAIN; + } - if (ret < 0) - { - return ret; - } + ret = net_sem_timedwait(&conn->sendsem, + _SO_TIMEOUT(conn->sconn.s_sndtimeo)); + if (!conn->ept.rdev || conn->unbind) + { + ret = -ECONNRESET; } - else + + if (ret < 0) { - return -EAGAIN; + return ret; } } @@ -1176,11 +1138,9 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, } nxmutex_lock(&conn->sendlock); - - space = rpmsg_socket_get_space(conn); - total = MIN(total, space + sizeof(*msg)); - total = MIN(total, ipcsize); - len = total - sizeof(*msg) - sizeof(uint32_t); + total = MIN(total, rpmsg_socket_get_space(conn)); + total = MIN(total, ipcsize - sizeof(*msg)); + len = total - sizeof(uint32_t); /* SOCK_DGRAM need write len to buffer */ @@ -1206,9 +1166,10 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, } conn->lastpos = conn->recvpos; - conn->sendpos += len + sizeof(uint32_t); + conn->sendpos += total; - ret = rpmsg_sendto_nocopy(&conn->ept, msg, total, conn->ept.dest_addr); + ret = rpmsg_sendto_nocopy(&conn->ept, msg, total + sizeof(*msg), + conn->ept.dest_addr); nxmutex_unlock(&conn->sendlock); if (ret < 0) { @@ -1256,7 +1217,7 @@ static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock, } nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) || - (flags & MSG_DONTWAIT) != 0; + (flags & MSG_DONTWAIT) != 0; if (psock->s_type == SOCK_STREAM) { @@ -1271,15 +1232,16 @@ static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock, static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, int flags) { - FAR void *buf = msg->msg_iov->iov_base; - size_t len = msg->msg_iov->iov_len; + FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; FAR struct sockaddr *from = msg->msg_name; FAR socklen_t *fromlen = &msg->msg_namelen; - FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; + FAR void *buf = msg->msg_iov->iov_base; + size_t len = msg->msg_iov->iov_len; ssize_t ret; - if (psock->s_type != SOCK_STREAM && _SS_ISBOUND(conn->sconn.s_flags) - && !_SS_ISCONNECTED(conn->sconn.s_flags)) + if (psock->s_type != SOCK_STREAM && + _SS_ISBOUND(conn->sconn.s_flags) && + !_SS_ISCONNECTED(conn->sconn.s_flags)) { ret = rpmsg_socket_connect_internal(psock); if (ret < 0) @@ -1299,7 +1261,6 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, } nxmutex_lock(&conn->recvlock); - if (psock->s_type != SOCK_STREAM) { uint32_t datalen; @@ -1319,7 +1280,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, else { ret = circbuf_read(&conn->recvbuf, buf, len); - conn->recvpos += ret > 0 ? ret : 0; + conn->recvpos += ret > 0 ? ret : 0; } if (ret > 0) @@ -1348,14 +1309,13 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, nxmutex_unlock(&conn->recvlock); ret = net_sem_timedwait(&conn->recvsem, - _SO_TIMEOUT(conn->sconn.s_rcvtimeo)); + _SO_TIMEOUT(conn->sconn.s_rcvtimeo)); if (!conn->ept.rdev || conn->unbind) { ret = 0; } nxmutex_lock(&conn->recvlock); - if (!conn->recvdata) { ret = conn->recvlen; @@ -1367,7 +1327,6 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, out: nxmutex_unlock(&conn->recvlock); - if (ret > 0) { rpmsg_socket_wakeup(conn); @@ -1381,21 +1340,12 @@ static int rpmsg_socket_close(FAR struct socket *psock) { FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; - if (conn->crefs > 1) + if (--conn->crefs) { - conn->crefs--; return 0; } - if (conn->backlog == -2) - { - rpmsg_unregister_callback(conn, - NULL, - rpmsg_socket_device_destroy, - NULL, - NULL); - } - else if (conn->backlog) + if (conn->backlog > 0) { rpmsg_unregister_callback(conn, NULL, @@ -1407,13 +1357,20 @@ static int rpmsg_socket_close(FAR struct socket *psock) { rpmsg_unregister_callback(conn, rpmsg_socket_device_created, - rpmsg_socket_device_destroy, + NULL, NULL, NULL); } - rpmsg_socket_destroy_ept(conn); - rpmsg_socket_free(conn); + if (conn->ept.rdev) + { + rpmsg_socket_destroy_ept(conn); + } + else + { + rpmsg_socket_free(conn); + } + return 0; }