Skip to content

Commit

Permalink
rpmsg_sockif: add rpmsg_socket_ept_release for free conn
Browse files Browse the repository at this point in the history
Signed-off-by: yintao <[email protected]>
  • Loading branch information
yintao committed Oct 11, 2024
1 parent 69f3774 commit 538ecce
Showing 1 changed file with 30 additions and 43 deletions.
73 changes: 30 additions & 43 deletions net/rpmsg/rpmsg_sockif.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ struct rpmsg_socket_conn_s

/* server listen-scoket listening: backlog > 0;
* server listen-scoket closed: backlog = -1;
* accept scoket: backlog = -2;
* others: backlog = 0;
*/

Expand Down Expand Up @@ -454,21 +453,14 @@ 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);
}

nxmutex_unlock(&conn->sendlock);
nxmutex_unlock(&conn->recvlock);
rpmsg_destroy_ept(&conn->ept);
}

static void rpmsg_socket_ns_bound(struct rpmsg_endpoint *ept)
Expand Down Expand Up @@ -504,6 +496,18 @@ 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_device_created(FAR struct rpmsg_device *rdev,
FAR void *priv)
{
Expand All @@ -519,6 +523,7 @@ static void rpmsg_socket_device_created(FAR struct rpmsg_device *rdev,
{
conn->ept.priv = conn;
conn->ept.ns_bound_cb = rpmsg_socket_ns_bound;
conn->ept.release_cb = rpmsg_socket_ept_release;
snprintf(buf, sizeof(buf), "%s%s%s", RPMSG_SOCKET_NAME_PREFIX,
conn->rpaddr.rp_name, conn->nameid);

Expand All @@ -528,17 +533,6 @@ static void rpmsg_socket_device_created(FAR struct rpmsg_device *rdev,
}
}

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)
Expand Down Expand Up @@ -588,6 +582,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);
Expand Down Expand Up @@ -761,7 +756,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)
Expand All @@ -785,9 +780,10 @@ static int rpmsg_socket_connect_internal(FAR struct socket *psock)
{
rpmsg_unregister_callback(conn,
rpmsg_socket_device_created,
rpmsg_socket_device_destroy,
NULL,
NULL,
NULL);
rpmsg_socket_destroy_ept(conn);
}
}
else
Expand Down Expand Up @@ -856,13 +852,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);
Expand Down Expand Up @@ -1381,21 +1370,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,
Expand All @@ -1407,13 +1387,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;
}

Expand Down

0 comments on commit 538ecce

Please sign in to comment.