net/get/setsockopt: add si_get/setsockopt interface to simply get/setsockopt

move private option to protocol sockif

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2022-11-22 21:15:06 +08:00
committed by Xiang Xiao
parent c5704eb1e7
commit e6f8ccda4a
14 changed files with 620 additions and 486 deletions
+6 -2
View File
@@ -111,6 +111,9 @@ struct can_conn_s
int32_t tx_deadline;
# endif
#endif
#ifdef CONFIG_NET_TIMESTAMP
int32_t timestamp; /* Socket timestamp enabled/disabled */
#endif
};
/****************************************************************************
@@ -353,6 +356,7 @@ void can_readahead_signal(FAR struct can_conn_s *conn);
*
* Input Parameters:
* psock Socket structure of socket to operate on
* level Protocol level to set the option
* option identifies the option to set
* value Points to the argument value
* value_len The length of the argument value
@@ -365,7 +369,7 @@ void can_readahead_signal(FAR struct can_conn_s *conn);
****************************************************************************/
#ifdef CONFIG_NET_CANPROTO_OPTIONS
int can_setsockopt(FAR struct socket *psock, int option,
int can_setsockopt(FAR struct socket *psock, int level, int option,
FAR const void *value, socklen_t value_len);
#endif
@@ -399,7 +403,7 @@ int can_setsockopt(FAR struct socket *psock, int option,
****************************************************************************/
#ifdef CONFIG_NET_CANPROTO_OPTIONS
int can_getsockopt(FAR struct socket *psock, int option,
int can_getsockopt(FAR struct socket *psock, int level, int option,
FAR void *value, FAR socklen_t *value_len);
#endif
+1 -1
View File
@@ -123,7 +123,7 @@ uint16_t can_callback(FAR struct net_driver_s *dev,
#ifdef CONFIG_NET_TIMESTAMP
/* TIMESTAMP sockopt is activated, create timestamp and copy to iob */
if (conn->sconn.s_timestamp)
if (conn->timestamp)
{
struct timespec *ts = (struct timespec *)
&dev->d_appdata[dev->d_len];
+19 -1
View File
@@ -74,7 +74,7 @@
*
****************************************************************************/
int can_getsockopt(FAR struct socket *psock, int option,
int can_getsockopt(FAR struct socket *psock, int level, int option,
FAR void *value, FAR socklen_t *value_len)
{
FAR struct can_conn_s *conn;
@@ -84,6 +84,24 @@ int can_getsockopt(FAR struct socket *psock, int option,
psock->s_conn != NULL);
conn = (FAR struct can_conn_s *)psock->s_conn;
#ifdef CONFIG_NET_TIMESTAMP
if (level == SOL_SOCKET && option == SO_TIMESTAMP)
{
if (*value_len != sizeof(int32_t))
{
return -EINVAL;
}
*(FAR int32_t *)value = conn->timestamp;
return OK;
}
#endif
if (level != SOL_CAN_RAW)
{
return -ENOPROTOOPT;
}
if (psock->s_type != SOCK_RAW)
{
nerr("ERROR: Not a RAW CAN socket\n");
+5 -5
View File
@@ -434,9 +434,9 @@ static uint16_t can_recvfrom_eventhandler(FAR struct net_driver_s *dev,
#endif
{
#ifdef CONFIG_NET_TIMESTAMP
if ((conn->sconn.s_timestamp && (dev->d_len >
if ((conn->timestamp && (dev->d_len >
sizeof(struct can_frame) + sizeof(struct timeval)))
|| (!conn->sconn.s_timestamp && (dev->d_len >
|| (!conn->timestamp && (dev->d_len >
sizeof(struct can_frame))))
#else
if (dev->d_len > sizeof(struct can_frame))
@@ -454,7 +454,7 @@ static uint16_t can_recvfrom_eventhandler(FAR struct net_driver_s *dev,
can_newdata(dev, pstate);
#ifdef CONFIG_NET_TIMESTAMP
if (conn->sconn.s_timestamp)
if (conn->timestamp)
{
if (pstate->pr_msglen == sizeof(struct timeval))
{
@@ -587,7 +587,7 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
state.pr_buffer = msg->msg_iov->iov_base;
#ifdef CONFIG_NET_TIMESTAMP
if (conn->sconn.s_timestamp && msg->msg_controllen >=
if (conn->timestamp && msg->msg_controllen >=
(sizeof(struct cmsghdr) + sizeof(struct timeval)))
{
struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
@@ -619,7 +619,7 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
if (ret > 0)
{
#ifdef CONFIG_NET_TIMESTAMP
if (conn->sconn.s_timestamp)
if (conn->timestamp)
{
if (state.pr_msglen == sizeof(struct timeval))
{
+33 -1
View File
@@ -58,6 +58,7 @@
*
* Input Parameters:
* psock Socket structure of socket to operate on
* level Protocol level to set the option
* option identifies the option to set
* value Points to the argument value
* value_len The length of the argument value
@@ -69,7 +70,7 @@
*
****************************************************************************/
int can_setsockopt(FAR struct socket *psock, int option,
int can_setsockopt(FAR struct socket *psock, int level, int option,
FAR const void *value, socklen_t value_len)
{
FAR struct can_conn_s *conn;
@@ -81,6 +82,37 @@ int can_setsockopt(FAR struct socket *psock, int option,
conn = (FAR struct can_conn_s *)psock->s_conn;
#ifdef CONFIG_NET_TIMESTAMP
/* Generates a timestamp for each incoming packet */
if (level == SOL_SOCKET && option == SO_TIMESTAMP)
{
/* Verify that option is at least the size of an integer. */
if (value_len < sizeof(int32_t))
{
return -EINVAL;
}
/* Lock the network so that we have exclusive access to the socket
* options.
*/
net_lock();
conn->timestamp = *((FAR int32_t *)value);
net_unlock();
return OK;
}
#endif
if (level != SOL_CAN_RAW)
{
return -ENOPROTOOPT;
}
if (psock->s_type != SOCK_RAW)
{
nerr("ERROR: Not a RAW CAN socket\n");
+7 -1
View File
@@ -84,7 +84,13 @@ const struct sock_intf_s g_can_sockif =
can_poll_local, /* si_poll */
can_sendmsg, /* si_sendmsg */
can_recvmsg, /* si_recvmsg */
can_close /* si_close */
can_close, /* si_close */
NULL, /* si_ioctl */
NULL /* si_socketpair */
#if defined(CONFIG_NET_SOCKOPTS) && defined(CONFIG_NET_CANPROTO_OPTIONS)
, can_getsockopt /* si_getsockopt */
, can_setsockopt /* si_setsockopt */
#endif
};
/****************************************************************************