nuttx/net/can/can_getsockopt.c

263 lines
7.7 KiB
C
Raw Normal View History

2020-06-15 10:23:25 +02:00
/****************************************************************************
* net/can/can_getsockopt.c
2020-06-15 10:23:25 +02:00
*
* 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>
#include <sys/time.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <netpacket/can.h>
#include <nuttx/net/net.h>
#include <nuttx/net/can.h>
#include "socket/socket.h"
#include "utils/utils.h"
#include "can/can.h"
#ifdef CONFIG_NET_CANPROTO_OPTIONS
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: can_getsockopt
*
* Description:
* can_getsockopt() retrieves the value for the option specified by the
* 'option' argument for the socket specified by the 'psock' argument. If
* the size of the option value is greater than 'value_len', the value
* stored in the object pointed to by the 'value' argument will be silently
* truncated. Otherwise, the length pointed to by the 'value_len' argument
* will be modified to indicate the actual length of the 'value'.
*
* See <sys/socket.h> a complete list of values for the socket-level
* 'option' argument. Protocol-specific options are are protocol specific
* header files (such as netpacket/can.h for the case of the CAN protocol).
*
* Input Parameters:
* psock Socket structure of the socket to query
* level Protocol level to set the option
* option identifies the option to get
* value Points to the argument value
* value_len The length of the argument value
*
* Returned Value:
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error. See psock_getsockopt() for
* the complete list of appropriate return error codes.
*
****************************************************************************/
int can_getsockopt(FAR struct socket *psock, int level, int option,
2020-06-15 10:23:25 +02:00
FAR void *value, FAR socklen_t *value_len)
{
FAR struct can_conn_s *conn;
int ret = OK;
2020-06-15 10:23:25 +02:00
DEBUGASSERT(value != NULL && value_len != NULL);
conn = psock->s_conn;
2020-06-15 10:23:25 +02:00
#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;
}
2020-06-15 10:23:25 +02:00
if (psock->s_type != SOCK_RAW)
{
nerr("ERROR: Not a RAW CAN socket\n");
return -ENOTCONN;
}
switch (option)
{
case CAN_RAW_FILTER:
if (*value_len % sizeof(struct can_filter) != 0)
{
ret = -EINVAL;
}
else if (*value_len > CONFIG_NET_CAN_RAW_FILTER_MAX *
sizeof(struct can_filter))
2020-06-15 10:23:25 +02:00
{
ret = -EINVAL;
}
else
{
int count = conn->filter_count;
int i;
2020-06-15 10:23:25 +02:00
if (*value_len < count * sizeof(struct can_filter))
2020-06-15 10:23:25 +02:00
{
count = *value_len / sizeof(struct can_filter);
}
else
{
*value_len = count * sizeof(struct can_filter);
}
2020-06-15 10:23:25 +02:00
for (i = 0; i < count; i++)
2020-06-15 10:23:25 +02:00
{
((FAR struct can_filter *)value)[i] = conn->filters[i];
2020-06-15 10:23:25 +02:00
}
}
break;
case CAN_RAW_ERR_FILTER:
#ifdef CONFIG_NET_CAN_ERRORS
if (*value_len < sizeof(can_err_mask_t))
{
return -EINVAL;
}
else
{
FAR can_err_mask_t *mask = (FAR can_err_mask_t *)value;
*mask = conn->err_mask;
*value_len = sizeof(can_err_mask_t);
}
#endif
2020-06-15 10:23:25 +02:00
break;
case CAN_RAW_LOOPBACK:
if (*value_len < sizeof(conn->loopback))
{
/* REVISIT: POSIX says that we should truncate the value if it
* is larger than value_len. That just doesn't make sense
* to me in this case.
*/
ret = -EINVAL;
2020-06-15 10:23:25 +02:00
}
else
{
FAR int32_t *loopback = (FAR int32_t *)value;
*loopback = conn->loopback;
*value_len = sizeof(conn->loopback);
2020-06-15 10:23:25 +02:00
}
break;
case CAN_RAW_RECV_OWN_MSGS:
if (*value_len < sizeof(conn->recv_own_msgs))
{
/* REVISIT: POSIX says that we should truncate the value if it
* is larger than value_len. That just doesn't make sense
* to me in this case.
*/
ret = -EINVAL;
2020-06-15 10:23:25 +02:00
}
else
{
FAR int32_t *recv_own_msgs = (FAR int32_t *)value;
*recv_own_msgs = conn->recv_own_msgs;
*value_len = sizeof(conn->recv_own_msgs);
2020-06-15 10:23:25 +02:00
}
break;
#ifdef CONFIG_NET_CAN_CANFD
2020-06-15 10:23:25 +02:00
case CAN_RAW_FD_FRAMES:
if (*value_len < sizeof(conn->fd_frames))
{
/* REVISIT: POSIX says that we should truncate the value if it
* is larger than value_len. That just doesn't make sense
* to me in this case.
*/
ret = -EINVAL;
2020-06-15 10:23:25 +02:00
}
else
{
FAR int32_t *fd_frames = (FAR int32_t *)value;
*fd_frames = conn->fd_frames;
*value_len = sizeof(conn->fd_frames);
2020-06-15 10:23:25 +02:00
}
break;
#endif
2020-06-15 10:23:25 +02:00
case CAN_RAW_JOIN_FILTERS:
break;
#ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
case CAN_RAW_TX_DEADLINE:
if (*value_len < sizeof(conn->tx_deadline))
{
/* REVISIT: POSIX says that we should truncate the value if it
* is larger than value_len. That just doesn't make sense
* to me in this case.
*/
ret = -EINVAL;
2020-06-15 10:23:25 +02:00
}
else
{
FAR int32_t *tx_deadline = (FAR int32_t *)value;
*tx_deadline = conn->tx_deadline;
*value_len = sizeof(conn->tx_deadline);
2020-06-15 10:23:25 +02:00
}
break;
#endif
#if CONFIG_NET_RECV_BUFSIZE > 0
case SO_RCVBUF:
/* Verify that option is the size of an 'int'. Should also check
* that 'value' is properly aligned for an 'int'
*/
if (*value_len != sizeof(int))
{
return -EINVAL;
}
*(FAR int *)value = conn->recv_buffnum * CONFIG_IOB_BUFSIZE;
break;
#endif
2020-06-15 10:23:25 +02:00
default:
nerr("ERROR: Unrecognized RAW CAN socket option: %d\n", option);
ret = -ENOPROTOOPT;
break;
}
return ret;
}
#endif /* CONFIG_NET_CANPROTO_OPTIONS */