2020-11-05 13:30:49 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/rpmsg/rpmsg_sockif.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2020-11-05 13:30:49 +01:00
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2023-04-20 14:39:13 +02:00
|
|
|
#include <sys/socket.h>
|
2021-05-14 04:45:57 +02:00
|
|
|
#include <debug.h>
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/mm/circbuf.h>
|
|
|
|
#include <nuttx/rptun/openamp.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2020-11-05 13:30:49 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2021-12-09 10:24:44 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netpacket/rpmsg.h>
|
|
|
|
|
|
|
|
#include "socket/socket.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-10-18 17:06:10 +02:00
|
|
|
#define RPMSG_SOCKET_CMD_SYNC 1
|
|
|
|
#define RPMSG_SOCKET_CMD_DATA 2
|
2023-01-13 16:25:52 +01:00
|
|
|
#define RPMSG_SOCKET_NAME_PREFIX "sk:"
|
|
|
|
#define RPMSG_SOCKET_NAME_PREFIX_LEN 3
|
|
|
|
#define RPMSG_SOCKET_NAME_ID_LEN 13
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2021-10-18 17:06:10 +02:00
|
|
|
static_assert(RPMSG_SOCKET_NAME_SIZE + RPMSG_SOCKET_NAME_PREFIX_LEN
|
|
|
|
<= RPMSG_NAME_SIZE, "socket name size config error");
|
2021-10-15 16:39:17 +02:00
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
begin_packed_struct struct rpmsg_socket_sync_s
|
|
|
|
{
|
|
|
|
uint32_t cmd;
|
|
|
|
uint32_t size;
|
2023-04-20 14:39:13 +02:00
|
|
|
uint32_t pid;
|
|
|
|
uint32_t uid;
|
|
|
|
uint32_t gid;
|
2020-11-05 13:30:49 +01:00
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
begin_packed_struct struct rpmsg_socket_data_s
|
|
|
|
{
|
|
|
|
uint32_t cmd;
|
|
|
|
uint32_t pos;
|
|
|
|
|
|
|
|
/* Act data len, don't include len itself when SOCK_DGRAM */
|
|
|
|
|
|
|
|
uint32_t len;
|
|
|
|
char data[0];
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
struct rpmsg_socket_conn_s
|
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
/* Common prologue of all connection structures. */
|
|
|
|
|
|
|
|
struct socket_conn_s sconn;
|
|
|
|
|
2023-01-10 10:19:01 +01:00
|
|
|
bool unbind;
|
2020-11-05 13:30:49 +01:00
|
|
|
struct rpmsg_endpoint ept;
|
|
|
|
|
|
|
|
struct sockaddr_rpmsg rpaddr;
|
2023-01-13 16:25:52 +01:00
|
|
|
char nameid[RPMSG_SOCKET_NAME_ID_LEN];
|
2020-11-05 13:30:49 +01:00
|
|
|
uint16_t crefs;
|
|
|
|
|
|
|
|
FAR struct pollfd *fds[CONFIG_NET_RPMSG_NPOLLWAITERS];
|
2022-11-04 15:51:43 +01:00
|
|
|
mutex_t polllock;
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
sem_t sendsem;
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t sendlock;
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
sem_t recvsem;
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t recvlock;
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
FAR void *recvdata;
|
|
|
|
uint32_t recvlen;
|
|
|
|
FAR struct circbuf_s recvbuf;
|
|
|
|
|
|
|
|
FAR struct rpmsg_socket_conn_s *next;
|
|
|
|
|
|
|
|
/* server listen-scoket listening: backlog > 0;
|
|
|
|
* server listen-scoket closed: backlog = -1;
|
2023-01-06 15:27:03 +01:00
|
|
|
* accept scoket: backlog = -2;
|
2020-11-05 13:30:49 +01:00
|
|
|
* others: backlog = 0;
|
|
|
|
*/
|
|
|
|
|
|
|
|
int backlog;
|
|
|
|
|
2023-04-20 14:39:13 +02:00
|
|
|
/* The remote connection's credentials */
|
|
|
|
|
|
|
|
struct ucred cred;
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
/* Flow control, descript send side */
|
|
|
|
|
|
|
|
uint32_t sendsize;
|
|
|
|
uint32_t sendpos;
|
|
|
|
uint32_t ackpos;
|
|
|
|
|
|
|
|
/* Flow control, descript recv side */
|
|
|
|
|
|
|
|
uint32_t recvpos;
|
|
|
|
uint32_t lastpos;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int rpmsg_socket_setup(FAR struct socket *psock);
|
2020-11-05 13:30:49 +01:00
|
|
|
static sockcaps_t rpmsg_socket_sockcaps(FAR struct socket *psock);
|
|
|
|
static void rpmsg_socket_addref(FAR struct socket *psock);
|
|
|
|
static int rpmsg_socket_bind(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2020-11-05 13:30:49 +01:00
|
|
|
static int rpmsg_socket_getsockname(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2024-01-29 13:53:34 +01:00
|
|
|
static int rpmsg_socket_getpeername(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2020-11-05 13:30:49 +01:00
|
|
|
static int rpmsg_socket_listen(FAR struct socket *psock, int backlog);
|
|
|
|
static int rpmsg_socket_connect(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2020-11-05 13:30:49 +01:00
|
|
|
static int rpmsg_socket_accept(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen,
|
|
|
|
FAR struct socket *newsock, int flags);
|
2020-11-05 13:30:49 +01:00
|
|
|
static int rpmsg_socket_poll(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct pollfd *fds, bool setup);
|
2020-11-05 13:30:49 +01:00
|
|
|
static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct msghdr *msg, int flags);
|
2020-11-05 13:30:49 +01:00
|
|
|
static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct msghdr *msg, int flags);
|
2020-11-05 13:30:49 +01:00
|
|
|
static int rpmsg_socket_close(FAR struct socket *psock);
|
2022-08-30 20:18:33 +02:00
|
|
|
static int rpmsg_socket_ioctl(FAR struct socket *psock,
|
|
|
|
int cmd, unsigned long arg);
|
2023-04-20 14:39:13 +02:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
static int rpmsg_socket_getsockopt(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
int level, int option,
|
|
|
|
FAR void *value,
|
|
|
|
FAR socklen_t *value_len);
|
2023-04-20 14:39:13 +02:00
|
|
|
#endif
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_rpmsg_sockif =
|
|
|
|
{
|
|
|
|
rpmsg_socket_setup, /* si_setup */
|
|
|
|
rpmsg_socket_sockcaps, /* si_sockcaps */
|
|
|
|
rpmsg_socket_addref, /* si_addref */
|
|
|
|
rpmsg_socket_bind, /* si_bind */
|
|
|
|
rpmsg_socket_getsockname, /* si_getsockname */
|
2024-01-29 13:53:34 +01:00
|
|
|
rpmsg_socket_getpeername, /* si_getpeername */
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_listen, /* si_listen */
|
|
|
|
rpmsg_socket_connect, /* si_connect */
|
|
|
|
rpmsg_socket_accept, /* si_accept */
|
|
|
|
rpmsg_socket_poll, /* si_poll */
|
|
|
|
rpmsg_socket_sendmsg, /* si_sendmsg */
|
|
|
|
rpmsg_socket_recvmsg, /* si_recvmsg */
|
2021-12-09 10:24:44 +01:00
|
|
|
rpmsg_socket_close, /* si_close */
|
|
|
|
rpmsg_socket_ioctl, /* si_ioctl */
|
2023-04-20 14:39:13 +02:00
|
|
|
NULL, /* si_socketpair */
|
|
|
|
NULL /* si_shutdown */
|
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
, rpmsg_socket_getsockopt /* si_getsockopt */
|
|
|
|
, NULL /* si_setsockopt */
|
|
|
|
#endif
|
2020-11-05 13:30:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-13 16:25:52 +01:00
|
|
|
static uint64_t g_rpmsg_id;
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void rpmsg_socket_post(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
int semcount;
|
|
|
|
|
|
|
|
nxsem_get_value(sem, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 15:51:43 +01:00
|
|
|
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);
|
|
|
|
nxmutex_unlock(&conn->polllock);
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
static FAR struct rpmsg_socket_conn_s *rpmsg_socket_alloc(void)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn;
|
2023-08-22 09:41:07 +02:00
|
|
|
int ret;
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn = kmm_zalloc(sizeof(struct rpmsg_socket_conn_s));
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:41:07 +02:00
|
|
|
ret = circbuf_init(&conn->recvbuf, NULL, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2022-11-04 15:51:43 +01:00
|
|
|
nxmutex_init(&conn->polllock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&conn->sendlock);
|
|
|
|
nxmutex_init(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
nxsem_init(&conn->sendsem, 0, 0);
|
|
|
|
nxsem_init(&conn->recvsem, 0, 0);
|
|
|
|
|
|
|
|
conn->crefs = 1;
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpmsg_socket_free(FAR struct rpmsg_socket_conn_s *conn)
|
|
|
|
{
|
|
|
|
circbuf_uninit(&conn->recvbuf);
|
|
|
|
|
2022-11-04 15:51:43 +01:00
|
|
|
nxmutex_destroy(&conn->polllock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&conn->recvlock);
|
|
|
|
nxmutex_destroy(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
nxsem_destroy(&conn->sendsem);
|
|
|
|
nxsem_destroy(&conn->recvsem);
|
|
|
|
|
|
|
|
kmm_free(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn)
|
|
|
|
{
|
|
|
|
struct rpmsg_socket_data_s msg;
|
|
|
|
uint32_t space;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2021-07-14 11:58:01 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
space = conn->recvpos - conn->lastpos;
|
|
|
|
|
|
|
|
if (space > circbuf_size(&conn->recvbuf) / 2)
|
|
|
|
{
|
2021-09-13 06:18:28 +02:00
|
|
|
conn->lastpos = conn->recvpos;
|
2020-11-05 13:30:49 +01:00
|
|
|
msg.cmd = RPMSG_SOCKET_CMD_DATA;
|
|
|
|
msg.pos = conn->recvpos;
|
|
|
|
msg.len = 0;
|
2021-09-13 06:18:28 +02:00
|
|
|
ret = 1;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
|
|
|
|
return ret ? rpmsg_send(&conn->ept, &msg, sizeof(msg)) : 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t rpmsg_socket_get_space(
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn)
|
|
|
|
{
|
|
|
|
return conn->sendsize - (conn->sendpos - conn->ackpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len, uint32_t src,
|
|
|
|
FAR void *priv)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = ept->priv;
|
|
|
|
FAR struct rpmsg_socket_sync_s *head = data;
|
|
|
|
|
|
|
|
if (head->cmd == RPMSG_SOCKET_CMD_SYNC)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
conn->sendsize = head->size;
|
2023-04-20 14:39:13 +02:00
|
|
|
conn->cred.pid = head->pid;
|
|
|
|
conn->cred.uid = head->uid;
|
|
|
|
conn->cred.gid = head->gid;
|
2021-12-10 13:40:43 +01:00
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
conn->sconn.s_flags |= _SF_CONNECTED;
|
|
|
|
|
|
|
|
_SO_CONN_SETERRNO(conn, OK);
|
2021-12-10 13:40:43 +01:00
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_post(&conn->sendsem);
|
2022-11-04 15:51:43 +01:00
|
|
|
rpmsg_socket_poll_notify(conn, POLLOUT);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_data_s *msg = data;
|
|
|
|
FAR uint8_t *buf = (FAR uint8_t *)msg->data;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn->ackpos = msg->pos;
|
|
|
|
|
|
|
|
if (rpmsg_socket_get_space(conn) > 0)
|
|
|
|
{
|
|
|
|
rpmsg_socket_post(&conn->sendsem);
|
2022-11-04 15:51:43 +01:00
|
|
|
rpmsg_socket_poll_notify(conn, POLLOUT);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (len > sizeof(*msg))
|
|
|
|
{
|
|
|
|
len -= sizeof(*msg);
|
|
|
|
|
|
|
|
DEBUGASSERT(len == msg->len || len == msg->len + sizeof(uint32_t));
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (conn->recvdata)
|
|
|
|
{
|
|
|
|
conn->recvlen = MIN(conn->recvlen, msg->len);
|
|
|
|
|
|
|
|
if (len == msg->len)
|
|
|
|
{
|
2021-08-16 15:41:06 +02:00
|
|
|
/* SOCK_STREAM */
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn->recvpos += conn->recvlen;
|
|
|
|
memcpy(conn->recvdata, buf, conn->recvlen);
|
|
|
|
buf += conn->recvlen;
|
|
|
|
len -= conn->recvlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-16 15:41:06 +02:00
|
|
|
/* SOCK_DGRAM */
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn->recvpos += len;
|
|
|
|
memcpy(conn->recvdata, buf + sizeof(uint32_t),
|
|
|
|
conn->recvlen);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->recvdata = NULL;
|
2023-05-15 08:26:51 +02:00
|
|
|
rpmsg_socket_post(&conn->recvsem);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
written = circbuf_write(&conn->recvbuf, buf, len);
|
|
|
|
if (written != len)
|
|
|
|
{
|
2022-03-25 11:55:45 +01:00
|
|
|
nerr("circbuf_write overflow, %zu, %zu\n", written, len);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 15:51:43 +01:00
|
|
|
rpmsg_socket_poll_notify(conn, POLLIN);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void rpmsg_socket_destroy_ept(
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn)
|
|
|
|
{
|
2021-03-17 07:34:22 +01:00
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2023-01-06 15:24:42 +01:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2021-03-17 07:34:22 +01:00
|
|
|
|
|
|
|
if (conn->ept.rdev)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2023-09-13 05:50:19 +02:00
|
|
|
if (conn->backlog > 0)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
/* Listen socket */
|
|
|
|
|
|
|
|
conn->backlog = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpmsg_destroy_ept(&conn->ept);
|
|
|
|
rpmsg_socket_post(&conn->sendsem);
|
|
|
|
rpmsg_socket_post(&conn->recvsem);
|
2022-11-04 15:51:43 +01:00
|
|
|
rpmsg_socket_poll_notify(conn, POLLIN | POLLOUT);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
2021-03-17 07:34:22 +01:00
|
|
|
|
2023-01-06 15:24:42 +01:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:39:42 +02:00
|
|
|
static void rpmsg_socket_ns_bound(struct rpmsg_endpoint *ept)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = ept->priv;
|
|
|
|
struct rpmsg_socket_sync_s msg;
|
|
|
|
|
|
|
|
msg.cmd = RPMSG_SOCKET_CMD_SYNC;
|
|
|
|
msg.size = circbuf_size(&conn->recvbuf);
|
2023-04-20 14:39:13 +02:00
|
|
|
msg.pid = nxsched_getpid();
|
|
|
|
msg.uid = getuid();
|
|
|
|
msg.gid = getgid();
|
2021-10-19 13:39:42 +02:00
|
|
|
|
|
|
|
rpmsg_send(&conn->ept, &msg, sizeof(msg));
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
static void rpmsg_socket_ns_unbind(FAR struct rpmsg_endpoint *ept)
|
|
|
|
{
|
2023-01-10 10:19:01 +01:00
|
|
|
FAR struct rpmsg_socket_conn_s *conn = ept->priv;
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_lock(&conn->recvlock);
|
|
|
|
|
|
|
|
conn->unbind = true;
|
|
|
|
rpmsg_socket_post(&conn->sendsem);
|
|
|
|
rpmsg_socket_post(&conn->recvsem);
|
|
|
|
rpmsg_socket_poll_notify(conn, POLLIN | POLLOUT);
|
|
|
|
|
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rpmsg_socket_device_created(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = priv;
|
2021-10-15 16:39:17 +02:00
|
|
|
char buf[RPMSG_NAME_SIZE];
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (conn->ept.rdev)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(conn->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev)) == 0)
|
|
|
|
{
|
|
|
|
conn->ept.priv = conn;
|
2021-10-19 13:39:42 +02:00
|
|
|
conn->ept.ns_bound_cb = rpmsg_socket_ns_bound;
|
2023-01-13 16:25:52 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s%s", RPMSG_SOCKET_NAME_PREFIX,
|
|
|
|
conn->rpaddr.rp_name, conn->nameid);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2021-09-09 04:52:44 +02:00
|
|
|
rpmsg_create_ept(&conn->ept, rdev, buf,
|
2020-11-05 13:30:49 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:38:11 +02:00
|
|
|
static bool rpmsg_socket_ns_match(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv, FAR const char *name,
|
|
|
|
uint32_t dest)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *server = priv;
|
2021-10-15 16:39:17 +02:00
|
|
|
char buf[RPMSG_NAME_SIZE];
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2021-10-18 17:06:10 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s", RPMSG_SOCKET_NAME_PREFIX,
|
2021-09-09 04:52:44 +02:00
|
|
|
server->rpaddr.rp_name);
|
|
|
|
if (strncmp(name, buf, strlen(buf)))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-06-06 10:38:11 +02:00
|
|
|
return false;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(server->rpaddr.rp_cpu) &&
|
|
|
|
strcmp(server->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev)))
|
|
|
|
{
|
|
|
|
/* Bind specific CPU, then only listen that CPU */
|
|
|
|
|
2022-06-06 10:38:11 +02:00
|
|
|
return false;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:38:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv, FAR const char *name,
|
|
|
|
uint32_t dest)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *server = priv;
|
|
|
|
FAR struct rpmsg_socket_conn_s *tmp;
|
|
|
|
FAR struct rpmsg_socket_conn_s *new;
|
|
|
|
int cnt = 0;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
new = rpmsg_socket_alloc();
|
|
|
|
if (!new)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = circbuf_resize(&new->recvbuf, CONFIG_NET_RPMSG_RXBUF_SIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
rpmsg_socket_free(new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new->ept.priv = new;
|
|
|
|
ret = rpmsg_create_ept(&new->ept, rdev, name,
|
|
|
|
RPMSG_ADDR_ANY, dest,
|
|
|
|
rpmsg_socket_ept_cb, rpmsg_socket_ns_unbind);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
rpmsg_socket_free(new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:43:47 +02:00
|
|
|
new->rpaddr.rp_family = AF_RPMSG;
|
2022-04-01 09:03:47 +02:00
|
|
|
strlcpy(new->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev),
|
|
|
|
sizeof(new->rpaddr.rp_cpu));
|
|
|
|
strlcpy(new->rpaddr.rp_name, name + RPMSG_SOCKET_NAME_PREFIX_LEN,
|
|
|
|
sizeof(new->rpaddr.rp_name));
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-07-03 09:10:22 +02:00
|
|
|
rpmsg_socket_ns_bound(&new->ept);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&server->recvlock);
|
2021-07-15 17:42:16 +02:00
|
|
|
|
|
|
|
for (tmp = server; tmp->next; tmp = tmp->next)
|
|
|
|
{
|
|
|
|
if (++cnt >= server->backlog)
|
|
|
|
{
|
|
|
|
/* Reject the connection */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&server->recvlock);
|
2021-07-15 17:42:16 +02:00
|
|
|
rpmsg_destroy_ept(&new->ept);
|
|
|
|
rpmsg_socket_free(new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
tmp->next = new;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&server->recvlock);
|
2021-07-15 17:42:16 +02:00
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_post(&server->recvsem);
|
2022-11-04 15:51:43 +01:00
|
|
|
rpmsg_socket_poll_notify(server, POLLIN);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_getaddr(FAR struct rpmsg_socket_conn_s *conn,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
if (!addr || *addrlen < sizeof(struct sockaddr_rpmsg))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &conn->rpaddr, sizeof(struct sockaddr_rpmsg));
|
|
|
|
*addrlen = sizeof(struct sockaddr_rpmsg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_setaddr(FAR struct rpmsg_socket_conn_s *conn,
|
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen, bool suffix)
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_rpmsg *rpaddr = (FAR struct sockaddr_rpmsg *)addr;
|
|
|
|
|
|
|
|
if (rpaddr->rp_family != AF_RPMSG ||
|
2022-06-28 10:02:44 +02:00
|
|
|
addrlen < sizeof(struct sockaddr_rpmsg))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&conn->rpaddr, rpaddr, sizeof(struct sockaddr_rpmsg));
|
|
|
|
|
|
|
|
if (suffix)
|
|
|
|
{
|
2023-08-23 14:59:23 +02:00
|
|
|
snprintf(conn->nameid, sizeof(conn->nameid), ":%" PRIx64,
|
|
|
|
g_rpmsg_id++);
|
2023-01-13 16:25:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->nameid[0] = 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int rpmsg_socket_setup(FAR struct socket *psock)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn;
|
|
|
|
|
|
|
|
conn = rpmsg_socket_alloc();
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
psock->s_conn = conn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sockcaps_t rpmsg_socket_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
return SOCKCAP_NONBLOCKING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpmsg_socket_addref(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
conn->crefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
return rpmsg_socket_setaddr(psock->s_conn, addr, addrlen, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_getsockname(FAR struct socket *psock,
|
2021-10-18 17:06:10 +02:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2021-10-18 17:06:10 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = rpmsg_socket_getaddr(psock->s_conn, addr, addrlen);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2022-04-07 09:01:10 +02:00
|
|
|
strlcpy(((struct sockaddr_rpmsg *)addr)->rp_cpu,
|
2024-02-04 08:04:16 +01:00
|
|
|
CONFIG_RPMSG_LOCAL_CPUNAME, RPMSG_SOCKET_CPU_SIZE);
|
2021-10-18 17:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2024-01-29 13:53:34 +01:00
|
|
|
static int rpmsg_socket_getpeername(FAR struct socket *psock,
|
2020-11-05 13:30:49 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
2021-10-18 17:06:10 +02:00
|
|
|
return rpmsg_socket_getaddr(psock->s_conn, addr, addrlen);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_listen(FAR struct socket *psock, int backlog)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *server = psock->s_conn;
|
|
|
|
|
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!_SS_ISBOUND(server->sconn.s_flags) || backlog <= 0)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->backlog = backlog;
|
|
|
|
return rpmsg_register_callback(server,
|
2022-06-06 10:38:11 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
rpmsg_socket_ns_match,
|
|
|
|
rpmsg_socket_ns_bind);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_connect_internal(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = circbuf_resize(&conn->recvbuf, CONFIG_NET_RPMSG_RXBUF_SIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rpmsg_register_callback(conn,
|
|
|
|
rpmsg_socket_device_created,
|
|
|
|
rpmsg_socket_device_destroy,
|
2022-06-06 10:38:11 +02:00
|
|
|
NULL,
|
2021-10-19 13:39:42 +02:00
|
|
|
NULL);
|
2020-11-05 13:30:49 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-28 08:09:09 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2021-08-16 11:19:18 +02:00
|
|
|
if (conn->sendsize == 0)
|
|
|
|
{
|
2023-08-28 08:09:09 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISNONBLOCK(conn->sconn.s_flags))
|
2021-08-16 13:24:27 +02:00
|
|
|
{
|
|
|
|
return -EINPROGRESS;
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&conn->sendsem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_rcvtimeo));
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2021-08-16 13:24:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
rpmsg_unregister_callback(conn,
|
|
|
|
rpmsg_socket_device_created,
|
|
|
|
rpmsg_socket_device_destroy,
|
2022-06-06 10:38:11 +02:00
|
|
|
NULL,
|
2021-10-19 13:39:42 +02:00
|
|
|
NULL);
|
2021-08-16 13:24:27 +02:00
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
2023-08-28 08:09:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&conn->recvlock);
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
int ret;
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISCONNECTED(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rpmsg_socket_setaddr(conn, addr, addrlen,
|
|
|
|
psock->s_type == SOCK_STREAM);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpmsg_socket_connect_internal(psock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_accept(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen,
|
2023-03-06 18:38:39 +01:00
|
|
|
FAR struct socket *newsock,
|
|
|
|
int flags)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *server = psock->s_conn;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (server->backlog == -1)
|
|
|
|
{
|
|
|
|
return -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!_SS_ISLISTENING(server->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2021-07-15 17:42:16 +02:00
|
|
|
FAR struct rpmsg_socket_conn_s *conn = NULL;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&server->recvlock);
|
2021-07-15 17:42:16 +02:00
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
if (server->next)
|
|
|
|
{
|
2021-07-15 17:42:16 +02:00
|
|
|
conn = server->next;
|
2020-11-05 13:30:49 +01:00
|
|
|
server->next = conn->next;
|
|
|
|
conn->next = NULL;
|
2021-07-15 17:42:16 +02:00
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&server->recvlock);
|
2021-07-15 17:42:16 +02:00
|
|
|
|
|
|
|
if (conn)
|
|
|
|
{
|
2023-01-06 15:27:03 +01:00
|
|
|
conn->backlog = -2;
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_register_callback(conn,
|
2023-01-06 15:27:03 +01:00
|
|
|
NULL,
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_device_destroy,
|
2022-06-06 10:38:11 +02:00
|
|
|
NULL,
|
2021-10-19 13:39:42 +02:00
|
|
|
NULL);
|
2021-08-16 11:19:18 +02:00
|
|
|
|
|
|
|
if (conn->sendsize == 0)
|
|
|
|
{
|
2023-01-13 07:51:38 +01:00
|
|
|
net_sem_wait(&conn->sendsem);
|
2021-08-16 11:19:18 +02:00
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
newsock->s_domain = psock->s_domain;
|
|
|
|
newsock->s_sockif = psock->s_sockif;
|
|
|
|
newsock->s_type = SOCK_STREAM;
|
|
|
|
newsock->s_conn = conn;
|
|
|
|
|
|
|
|
rpmsg_socket_getaddr(conn, addr, addrlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-11 12:45:59 +01:00
|
|
|
if (_SS_ISNONBLOCK(server->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_wait(&server->recvsem);
|
2020-11-05 13:30:49 +01:00
|
|
|
if (server->backlog == -1)
|
|
|
|
{
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_poll(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
pollevent_t eventset = 0;
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
2022-11-04 15:51:43 +01:00
|
|
|
nxmutex_lock(&conn->polllock);
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_RPMSG_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!conn->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
conn->fds[i] = fds;
|
|
|
|
fds->priv = &conn->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 15:51:43 +01:00
|
|
|
nxmutex_unlock(&conn->polllock);
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
if (i >= CONFIG_NET_RPMSG_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediately notify on any of the requested events */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISLISTENING(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
if (conn->backlog == -1)
|
|
|
|
{
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->next)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 06:18:01 +01:00
|
|
|
else if (_SS_ISCONNECTED(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-07-06 16:19:35 +02:00
|
|
|
eventset |= POLLHUP;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:24:15 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-06-21 16:24:15 +02:00
|
|
|
if (rpmsg_socket_get_space(conn) > 0)
|
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-06-21 16:24:15 +02:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-06-21 16:24:15 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-06-21 16:24:15 +02:00
|
|
|
if (!circbuf_is_empty(&conn->recvbuf))
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
2023-06-21 16:24:15 +02:00
|
|
|
|
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
2023-08-16 11:11:52 +02:00
|
|
|
else /* !_SS_ISCONNECTED(conn->sconn.s_flags) */
|
2021-08-16 13:24:27 +02:00
|
|
|
{
|
2023-08-16 11:11:52 +02:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
|
|
|
{
|
|
|
|
eventset |= POLLHUP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
2022-11-04 15:51:43 +01:00
|
|
|
else
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-11-04 15:51:43 +01:00
|
|
|
nxmutex_lock(&conn->polllock);
|
|
|
|
|
|
|
|
if (fds->priv != NULL)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-11-04 15:51:43 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_RPMSG_NPOLLWAITERS; i++)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-11-04 15:51:43 +01:00
|
|
|
if (fds == conn->fds[i])
|
|
|
|
{
|
|
|
|
conn->fds[i] = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 15:51:43 +01:00
|
|
|
|
|
|
|
nxmutex_unlock(&conn->polllock);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 05:44:40 +01:00
|
|
|
static uint32_t rpmsg_socket_get_iovlen(FAR const struct iovec *buf,
|
|
|
|
size_t iovcnt)
|
|
|
|
{
|
|
|
|
uint32_t len = 0;
|
|
|
|
while (iovcnt--)
|
|
|
|
{
|
|
|
|
len += (buf++)->iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
|
2021-03-04 05:44:40 +01:00
|
|
|
FAR const struct iovec *buf,
|
2021-12-09 08:01:24 +01:00
|
|
|
size_t iovcnt, bool nonblock)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
2021-03-04 05:44:40 +01:00
|
|
|
uint32_t len = rpmsg_socket_get_iovlen(buf, iovcnt);
|
|
|
|
uint32_t written = 0;
|
|
|
|
uint32_t offset = 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
while (written < len)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_data_s *msg;
|
2021-03-04 05:44:40 +01:00
|
|
|
uint32_t block_written = 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
uint32_t ipcsize;
|
2021-09-13 06:18:28 +02:00
|
|
|
uint32_t block;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
block = MIN(len - written, rpmsg_socket_get_space(conn));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (block == 0)
|
|
|
|
{
|
2021-12-09 08:01:24 +01:00
|
|
|
if (!nonblock)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&conn->sendsem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_sndtimeo));
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2021-09-13 06:18:28 +02:00
|
|
|
|
|
|
|
continue;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true);
|
|
|
|
if (!msg)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
|
|
|
|
block = MIN(len - written, rpmsg_socket_get_space(conn));
|
2020-11-05 13:30:49 +01:00
|
|
|
block = MIN(block, ipcsize - sizeof(*msg));
|
|
|
|
|
|
|
|
msg->cmd = RPMSG_SOCKET_CMD_DATA;
|
|
|
|
msg->pos = conn->recvpos;
|
|
|
|
msg->len = block;
|
2021-03-04 05:44:40 +01:00
|
|
|
while (block_written < block)
|
|
|
|
{
|
|
|
|
uint32_t chunk = MIN(block - block_written, buf->iov_len - offset);
|
|
|
|
memcpy(msg->data + block_written,
|
|
|
|
(FAR const char *)buf->iov_base + offset, chunk);
|
|
|
|
offset += chunk;
|
|
|
|
if (offset == buf->iov_len)
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_written += chunk;
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn->lastpos = conn->recvpos;
|
|
|
|
conn->sendpos += msg->len;
|
|
|
|
|
2022-07-20 16:46:46 +02:00
|
|
|
ret = rpmsg_sendto_nocopy(&conn->ept, msg, block + sizeof(*msg),
|
|
|
|
conn->ept.dest_addr);
|
2023-01-06 15:24:42 +01:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-04 17:24:43 +01:00
|
|
|
rpmsg_release_tx_buffer(&conn->ept, msg);
|
2020-11-05 13:30:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += block;
|
|
|
|
}
|
|
|
|
|
|
|
|
return written ? written : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rpmsg_socket_send_single(FAR struct socket *psock,
|
2021-03-04 05:44:40 +01:00
|
|
|
FAR const struct iovec *buf,
|
2021-12-09 08:01:24 +01:00
|
|
|
size_t iovcnt, bool nonblock)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct rpmsg_socket_data_s *msg;
|
2021-03-04 05:44:40 +01:00
|
|
|
uint32_t len = rpmsg_socket_get_iovlen(buf, iovcnt);
|
2020-11-05 13:30:49 +01:00
|
|
|
uint32_t total = len + sizeof(*msg) + sizeof(uint32_t);
|
2021-03-04 05:44:40 +01:00
|
|
|
uint32_t written = 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
uint32_t ipcsize;
|
2021-09-13 06:18:28 +02:00
|
|
|
uint32_t space;
|
2021-03-04 05:44:40 +01:00
|
|
|
char *msgpos;
|
2020-11-05 13:30:49 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (total > conn->sendsize)
|
|
|
|
{
|
|
|
|
return -EFBIG;
|
|
|
|
}
|
|
|
|
|
2021-09-13 06:18:28 +02:00
|
|
|
while (1)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
space = rpmsg_socket_get_space(conn);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
|
|
|
|
if (space >= total - sizeof(*msg))
|
|
|
|
break;
|
|
|
|
|
2021-12-09 08:01:24 +01:00
|
|
|
if (!nonblock)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&conn->sendsem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_sndtimeo));
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-09-13 06:18:28 +02:00
|
|
|
return ret;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-13 06:18:28 +02:00
|
|
|
return -EAGAIN;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true);
|
|
|
|
if (!msg)
|
|
|
|
{
|
2021-09-13 06:18:28 +02:00
|
|
|
return -EINVAL;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->sendlock);
|
2021-09-13 06:18:28 +02:00
|
|
|
|
|
|
|
space = rpmsg_socket_get_space(conn);
|
|
|
|
total = MIN(total, space + sizeof(*msg));
|
|
|
|
total = MIN(total, ipcsize);
|
|
|
|
len = total - sizeof(*msg) - sizeof(uint32_t);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
/* SOCK_DGRAM need write len to buffer */
|
|
|
|
|
|
|
|
msg->cmd = RPMSG_SOCKET_CMD_DATA;
|
|
|
|
msg->pos = conn->recvpos;
|
|
|
|
msg->len = len;
|
|
|
|
memcpy(msg->data, &len, sizeof(uint32_t));
|
2021-03-04 05:44:40 +01:00
|
|
|
msgpos = msg->data + sizeof(uint32_t);
|
|
|
|
while (written < len)
|
|
|
|
{
|
|
|
|
if (len - written < buf->iov_len)
|
|
|
|
{
|
|
|
|
memcpy(msgpos, buf->iov_base, len - written);
|
|
|
|
written = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(msgpos, buf->iov_base, buf->iov_len);
|
|
|
|
written += buf->iov_len;
|
|
|
|
msgpos += buf->iov_len;
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
conn->lastpos = conn->recvpos;
|
|
|
|
conn->sendpos += len + sizeof(uint32_t);
|
|
|
|
|
2022-07-20 16:46:46 +02:00
|
|
|
ret = rpmsg_sendto_nocopy(&conn->ept, msg, total, conn->ept.dest_addr);
|
2023-01-06 15:24:42 +01:00
|
|
|
nxmutex_unlock(&conn->sendlock);
|
2023-01-04 17:24:43 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
rpmsg_release_tx_buffer(&conn->ept, msg);
|
|
|
|
}
|
2021-09-13 06:18:28 +02:00
|
|
|
|
2020-11-05 13:30:49 +01:00
|
|
|
return ret > 0 ? len : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock,
|
|
|
|
FAR struct msghdr *msg, int flags)
|
|
|
|
{
|
2021-12-09 08:01:24 +01:00
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
2021-03-04 05:44:40 +01:00
|
|
|
FAR const struct iovec *buf = msg->msg_iov;
|
|
|
|
size_t len = msg->msg_iovlen;
|
|
|
|
FAR const struct sockaddr *to = msg->msg_name;
|
2020-11-05 13:30:49 +01:00
|
|
|
socklen_t tolen = msg->msg_namelen;
|
2021-12-09 08:01:24 +01:00
|
|
|
bool nonblock;
|
2020-11-05 13:30:49 +01:00
|
|
|
ssize_t ret;
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!_SS_ISCONNECTED(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
if (to == NULL)
|
|
|
|
{
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rpmsg_socket_connect(psock, to, tolen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2021-12-09 08:01:24 +01:00
|
|
|
{
|
|
|
|
/* return ECONNRESET if lower IPC closed */
|
|
|
|
|
|
|
|
return -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) ||
|
|
|
|
(flags & MSG_DONTWAIT) != 0;
|
2021-12-09 08:01:24 +01:00
|
|
|
|
|
|
|
if (psock->s_type == SOCK_STREAM)
|
|
|
|
{
|
|
|
|
return rpmsg_socket_send_continuous(psock, buf, len, nonblock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return rpmsg_socket_send_single(psock, buf, len, nonblock);
|
|
|
|
}
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 sockaddr *from = msg->msg_name;
|
|
|
|
FAR socklen_t *fromlen = &msg->msg_namelen;
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
ssize_t ret;
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (psock->s_type != SOCK_STREAM && _SS_ISBOUND(conn->sconn.s_flags)
|
|
|
|
&& !_SS_ISCONNECTED(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
ret = rpmsg_socket_connect_internal(psock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!_SS_ISCONNECTED(conn->sconn.s_flags))
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2021-10-09 07:37:57 +02:00
|
|
|
if (psock->s_type != SOCK_STREAM)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
uint32_t datalen;
|
|
|
|
|
|
|
|
ret = circbuf_read(&conn->recvbuf, &datalen, sizeof(uint32_t));
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ret = circbuf_read(&conn->recvbuf, buf, MIN(datalen, len));
|
|
|
|
if (ret > 0 && datalen > ret)
|
|
|
|
{
|
|
|
|
circbuf_skip(&conn->recvbuf, datalen - ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->recvpos += datalen + sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = circbuf_read(&conn->recvbuf, buf, len);
|
|
|
|
conn->recvpos += ret > 0 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
/* return EOF if lower IPC closed */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->recvdata = buf;
|
|
|
|
conn->recvlen = len;
|
|
|
|
|
2023-05-15 08:26:51 +02:00
|
|
|
nxsem_reset(&conn->recvsem, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&conn->recvsem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_rcvtimeo));
|
2023-01-10 10:19:01 +01:00
|
|
|
if (!conn->ept.rdev || conn->unbind)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
2023-10-24 10:33:17 +02:00
|
|
|
ret = 0;
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (!conn->recvdata)
|
|
|
|
{
|
|
|
|
ret = conn->recvlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->recvdata = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->recvlock);
|
2020-11-05 13:30:49 +01:00
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2021-09-13 06:18:28 +02:00
|
|
|
rpmsg_socket_wakeup(conn);
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_getaddr(conn, from, fromlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmsg_socket_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
if (conn->crefs > 1)
|
|
|
|
{
|
|
|
|
conn->crefs--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:27:03 +01:00
|
|
|
if (conn->backlog == -2)
|
|
|
|
{
|
|
|
|
rpmsg_unregister_callback(conn,
|
|
|
|
NULL,
|
|
|
|
rpmsg_socket_device_destroy,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else if (conn->backlog)
|
2020-11-05 13:30:49 +01:00
|
|
|
{
|
|
|
|
rpmsg_unregister_callback(conn,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2022-06-06 10:38:11 +02:00
|
|
|
rpmsg_socket_ns_match,
|
2020-11-05 13:30:49 +01:00
|
|
|
rpmsg_socket_ns_bind);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rpmsg_unregister_callback(conn,
|
|
|
|
rpmsg_socket_device_created,
|
|
|
|
rpmsg_socket_device_destroy,
|
2022-06-06 10:38:11 +02:00
|
|
|
NULL,
|
2021-10-19 13:39:42 +02:00
|
|
|
NULL);
|
2020-11-05 13:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rpmsg_socket_destroy_ept(conn);
|
|
|
|
rpmsg_socket_free(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-09 10:24:44 +01:00
|
|
|
|
2023-08-02 10:58:46 +02:00
|
|
|
static void rpmsg_socket_path(FAR struct rpmsg_socket_conn_s *conn,
|
|
|
|
FAR char *buf, size_t len)
|
|
|
|
{
|
|
|
|
if (conn->backlog) /* Server */
|
|
|
|
{
|
|
|
|
snprintf(buf, len,
|
|
|
|
"rpmsg:[%s:[%s%s]<->%s]",
|
2024-02-04 08:04:16 +01:00
|
|
|
CONFIG_RPMSG_LOCAL_CPUNAME, conn->rpaddr.rp_name,
|
2023-08-02 10:58:46 +02:00
|
|
|
conn->nameid, conn->rpaddr.rp_cpu);
|
|
|
|
}
|
|
|
|
else /* Client */
|
|
|
|
{
|
|
|
|
snprintf(buf, len,
|
|
|
|
"rpmsg:[%s<->%s:[%s%s]]",
|
2024-02-04 08:04:16 +01:00
|
|
|
CONFIG_RPMSG_LOCAL_CPUNAME, conn->rpaddr.rp_cpu,
|
2023-08-02 10:58:46 +02:00
|
|
|
conn->rpaddr.rp_name, conn->nameid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:18:33 +02:00
|
|
|
static int rpmsg_socket_ioctl(FAR struct socket *psock,
|
|
|
|
int cmd, unsigned long arg)
|
2021-12-09 10:24:44 +01:00
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONREAD:
|
|
|
|
*(FAR int *)((uintptr_t)arg) = circbuf_used(&conn->recvbuf);
|
|
|
|
break;
|
|
|
|
|
2022-08-30 20:18:33 +02:00
|
|
|
case FIONSPACE:
|
2021-12-09 10:24:44 +01:00
|
|
|
*(FAR int *)((uintptr_t)arg) = rpmsg_socket_get_space(conn);
|
|
|
|
break;
|
2022-08-30 20:18:33 +02:00
|
|
|
|
2023-08-02 10:58:46 +02:00
|
|
|
case FIOC_FILEPATH:
|
|
|
|
rpmsg_socket_path(conn, (FAR char *)(uintptr_t)arg, PATH_MAX);
|
|
|
|
break;
|
|
|
|
|
2021-12-09 10:24:44 +01:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2023-04-20 14:39:13 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
static int rpmsg_socket_getsockopt(FAR struct socket *psock, int level,
|
|
|
|
int option, FAR void *value,
|
|
|
|
FAR socklen_t *value_len)
|
|
|
|
{
|
|
|
|
if (level == SOL_SOCKET && option == SO_PEERCRED)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_socket_conn_s *conn = psock->s_conn;
|
|
|
|
if (*value_len != sizeof(struct ucred))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(value, &conn->cred, sizeof(struct ucred));
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
#endif
|