2020-06-15 10:23:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/can/can.h
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __NET_CAN_CAN_H
|
|
|
|
#define __NET_CAN_CAN_H
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <poll.h>
|
|
|
|
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/can.h>
|
2022-02-07 04:55:18 +01:00
|
|
|
#include <nuttx/net/net.h>
|
2020-06-15 10:23:25 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "socket/socket.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CAN_NOTIFIER
|
|
|
|
# include <nuttx/wqueue.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CAN
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Allocate a new packet socket data callback */
|
|
|
|
|
|
|
|
#define can_callback_alloc(dev,conn) \
|
2022-02-07 04:55:18 +01:00
|
|
|
devif_callback_alloc(dev, &conn->sconn.list, &conn->sconn.list_tail)
|
2020-06-15 10:23:25 +02:00
|
|
|
#define can_callback_free(dev,conn,cb) \
|
2022-02-07 04:55:18 +01:00
|
|
|
devif_conn_callback_free(dev, cb, &conn->sconn.list, &conn->sconn.list_tail)
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is a container that holds the poll-related information */
|
|
|
|
|
|
|
|
struct can_poll_s
|
|
|
|
{
|
|
|
|
FAR struct socket *psock; /* Needed to handle loss of connection */
|
|
|
|
FAR struct net_driver_s *dev; /* Needed to free the callback structure */
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd *fds; /* Needed to handle poll events */
|
2020-06-15 10:23:25 +02:00
|
|
|
FAR struct devif_callback_s *cb; /* Needed to teardown the poll */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This "connection" structure describes the underlying state of the socket */
|
|
|
|
|
|
|
|
struct can_conn_s
|
|
|
|
{
|
|
|
|
/* Common prologue of all connection structures. */
|
|
|
|
|
2022-02-07 04:55:18 +01:00
|
|
|
struct socket_conn_s sconn;
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
FAR struct net_driver_s *dev; /* Reference to CAN device */
|
|
|
|
|
|
|
|
/* Read-ahead buffering.
|
|
|
|
*
|
|
|
|
* readahead - A singly linked list of type struct iob_qentry_s
|
|
|
|
* where the CAN/IP read-ahead data is retained.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct iob_queue_s readahead; /* remove Read-ahead buffering */
|
|
|
|
|
2024-06-06 13:54:54 +02:00
|
|
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
|
|
|
int32_t recv_buffnum; /* Recv buffer number */
|
|
|
|
#endif
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
/* CAN-specific content follows */
|
|
|
|
|
|
|
|
int16_t crefs; /* Reference count */
|
|
|
|
|
|
|
|
/* The following is a list of poll structures of threads waiting for
|
|
|
|
* socket events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct can_poll_s pollinfo[4]; /* FIXME make dynamic */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CANPROTO_OPTIONS
|
|
|
|
int32_t loopback;
|
|
|
|
int32_t recv_own_msgs;
|
2023-05-20 00:32:34 +02:00
|
|
|
# ifdef CONFIG_NET_CAN_CANFD
|
2020-06-15 10:23:25 +02:00
|
|
|
int32_t fd_frames;
|
2023-05-20 00:32:34 +02:00
|
|
|
# endif
|
2020-06-15 10:23:25 +02:00
|
|
|
struct can_filter filters[CONFIG_NET_CAN_RAW_FILTER_MAX];
|
|
|
|
int32_t filter_count;
|
2023-08-17 09:27:28 +02:00
|
|
|
# ifdef CONFIG_NET_CAN_ERRORS
|
|
|
|
can_err_mask_t err_mask;
|
|
|
|
# endif
|
2023-05-20 00:32:34 +02:00
|
|
|
# ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
|
2020-06-15 10:23:25 +02:00
|
|
|
int32_t tx_deadline;
|
2023-05-20 00:32:34 +02:00
|
|
|
# endif
|
2020-06-15 10:23:25 +02:00
|
|
|
#endif
|
2022-11-22 14:15:06 +01:00
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
int32_t timestamp; /* Socket timestamp enabled/disabled */
|
|
|
|
#endif
|
2020-06-15 10:23:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
# define EXTERN extern "C"
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
# define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
|
|
|
EXTERN const struct sock_intf_s g_can_sockif;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct sockaddr_can; /* Forward reference */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_initialize()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the NetLink connection structures. Called once and only
|
|
|
|
* from the networking layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void can_initialize(void);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_alloc()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new, uninitialized NetLink connection structure. This is
|
|
|
|
* normally something done by the implementation of the socket() API
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_alloc(void);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_free()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a NetLink connection structure that is no longer in use. This
|
|
|
|
* should be done by the implementation of close().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void can_free(FAR struct can_conn_s *conn);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_nextconn()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of allocated NetLink connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from NetLink device logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_nextconn(FAR struct can_conn_s *conn);
|
|
|
|
|
2024-03-28 08:01:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_active()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of NetLink connections that match dev
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device to search for.
|
|
|
|
* conn - The current connection; may be NULL to start the search at the
|
|
|
|
* beginning
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from NetLink device logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_active(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct can_conn_s *conn);
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Inform the application holding the packet socket of a change in state.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if packet has been processed, otherwise ERROR.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from network logic at with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint16_t can_callback(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct can_conn_s *conn, uint16_t flags);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_datahandler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle data that is not accepted by the application. This may be called
|
|
|
|
* either (1) from the data receive logic if it cannot buffer the data, or
|
|
|
|
* (2) from the CAN event logic is there is no listener in place ready to
|
|
|
|
* receive the data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-11-23 12:39:40 +01:00
|
|
|
* dev - The device which as active when the event was detected.
|
2020-06-15 10:23:25 +02:00
|
|
|
* conn - A pointer to the CAN connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of bytes actually buffered is returned. This will be either
|
|
|
|
* zero or equal to buflen; partial packets are not buffered.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - The caller has checked that CAN_NEWDATA is set in flags and that is no
|
|
|
|
* other handler available to process the incoming data.
|
|
|
|
* - Called from network stack logic with the network stack locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
uint16_t can_datahandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct can_conn_s *conn);
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_recvmsg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* recvmsg() receives messages from a socket, and may be used to receive
|
|
|
|
* data on a socket whether or not it is connection-oriented.
|
|
|
|
*
|
|
|
|
* If from is not NULL, and the underlying protocol provides the source
|
2020-12-01 07:55:16 +01:00
|
|
|
* address, this source address is filled in. The argument 'msg_namelen'
|
2020-06-15 10:23:25 +02:00
|
|
|
* initialized to the size of the buffer associated with from, and modified
|
|
|
|
* on return to indicate the actual size of the address stored there.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg Buffer to receive the message
|
2020-06-15 10:23:25 +02:00
|
|
|
* flags Receive flags (ignored)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2020-12-01 07:55:16 +01:00
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Poll a CAN connection structure for availability of TX data
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device driver structure to use in the send operation
|
|
|
|
* conn - The CAN "connection" to poll for TX data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from network stack logic with the network stack locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void can_poll(FAR struct net_driver_s *dev, FAR struct can_conn_s *conn);
|
|
|
|
|
2022-07-22 10:23:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_can_cansend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_can_cansend() returns a value indicating if a write to the socket
|
|
|
|
* would block. It is still possible that the write may block if another
|
|
|
|
* write occurs first.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock An instance of the internal socket structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK
|
|
|
|
* At least one byte of data could be successfully written.
|
|
|
|
* -EWOULDBLOCK
|
|
|
|
* There is no room in the output buffer.
|
|
|
|
* -EBADF
|
|
|
|
* An invalid descriptor was specified.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int psock_can_cansend(FAR struct socket *psock);
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: can_sendmsg
|
2020-06-15 10:23:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-12-01 07:55:16 +01:00
|
|
|
* The can_sendmsg() call may be used only when the packet socket is
|
2020-06-15 10:23:25 +02:00
|
|
|
* in a connected state (so that the intended recipient is known).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock An instance of the internal socket structure.
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg CAN frame and optional CMSG
|
|
|
|
* flags Send flags (ignored)
|
2020-06-15 10:23:25 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
2020-12-01 07:55:16 +01:00
|
|
|
* a negated errno value is retruend. See sendmsg() for the complete list
|
2020-06-15 10:23:25 +02:00
|
|
|
* of return values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags);
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_readahead_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read-ahead data has been buffered. Signal all threads waiting for
|
|
|
|
* read-ahead data to become available.
|
|
|
|
*
|
|
|
|
* When read-ahead data becomes available, *all* of the workers waiting
|
|
|
|
* for read-ahead data will be executed. If there are multiple workers
|
|
|
|
* waiting for read-ahead data then only the first to execute will get the
|
|
|
|
* data. Others will need to call can_readahead_notifier_setup() once
|
|
|
|
* again.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The CAN connection where read-ahead data was just buffered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CAN_NOTIFIER
|
|
|
|
void can_readahead_signal(FAR struct can_conn_s *conn);
|
|
|
|
#endif
|
|
|
|
|
2020-06-15 19:57:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_setsockopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* can_setsockopt() sets the CAN-protocol option specified by the
|
|
|
|
* 'option' argument to the value pointed to by the 'value' argument for
|
|
|
|
* the socket specified by the 'psock' argument.
|
|
|
|
*
|
2024-04-05 18:49:46 +02:00
|
|
|
* See <nuttx/can.h> for the a complete list of values of CAN protocol
|
2020-06-15 19:57:13 +02:00
|
|
|
* options.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of socket to operate on
|
2022-11-22 14:15:06 +01:00
|
|
|
* level Protocol level to set the option
|
2020-06-15 19:57:13 +02:00
|
|
|
* option identifies the option to set
|
|
|
|
* 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_setcockopt() for
|
|
|
|
* the list of possible error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CANPROTO_OPTIONS
|
2022-11-22 14:15:06 +01:00
|
|
|
int can_setsockopt(FAR struct socket *psock, int level, int option,
|
2020-06-15 19:57:13 +02:00
|
|
|
FAR const void *value, socklen_t value_len);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2024-04-05 18:49:46 +02:00
|
|
|
* header files (such as nuttx/can.h for the case of the CAN protocol).
|
2020-06-15 19:57:13 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CANPROTO_OPTIONS
|
2022-11-22 14:15:06 +01:00
|
|
|
int can_getsockopt(FAR struct socket *psock, int level, int option,
|
2020-06-15 19:57:13 +02:00
|
|
|
FAR void *value, FAR socklen_t *value_len);
|
|
|
|
#endif
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
#undef EXTERN
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_CAN */
|
|
|
|
#endif /* __NET_CAN_CAN_H */
|