This is an automated email from the ASF dual-hosted git repository. gnutt pushed a commit to branch SocketCAN in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit 8aef0cb9075b2e27fbeba8bfed3aed5a60e2b89e Author: Jari van Ewijk <jari.vanew...@nxp.com> AuthorDate: Fri Mar 13 17:27:21 2020 +0100 Code style fixes 3 --- net/can/can.h | 13 ++-- net/can/can_callback.c | 21 +++--- net/can/can_conn.c | 8 +- net/can/can_getsockopt.c | 18 +++-- net/can/can_recvfrom.c | 189 +++++++++++++++++++++++++---------------------- net/can/can_send.c | 36 ++++----- net/can/can_setsockopt.c | 51 ++++++------- net/can/can_sockif.c | 14 ++-- 8 files changed, 185 insertions(+), 165 deletions(-) diff --git a/net/can/can.h b/net/can/can.h index c74fca4..7c6b614 100644 --- a/net/can/can.h +++ b/net/can/can.h @@ -104,22 +104,20 @@ struct can_conn_s */ struct can_poll_s pollinfo[4]; /* FIXME make dynamic */ - + #ifdef CONFIG_NET_CANPROTO_OPTIONS int32_t loopback; int32_t recv_own_msgs; int32_t fd_frames; struct can_filter filters[CONFIG_NET_CAN_RAW_FILTER_MAX]; int32_t filter_count; - + /* TODO add filter support */ #endif - + #ifdef CONFIG_NET_TIMESTAMP FAR struct socket *psock; /* Needed to get SO_TIMESTAMP value */ #endif - - }; /**************************************************************************** @@ -168,8 +166,8 @@ 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(). + * Free a NetLink connection structure that is no longer in use. This + * should be done by the implementation of close(). * ****************************************************************************/ @@ -258,7 +256,6 @@ uint16_t can_datahandler(FAR struct can_conn_s *conn, FAR uint8_t *buffer, * ****************************************************************************/ - ssize_t can_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, int flags, FAR struct sockaddr *from, FAR socklen_t *fromlen); diff --git a/net/can/can_callback.c b/net/can/can_callback.c index f8bff86..aab0e3e 100644 --- a/net/can/can_callback.c +++ b/net/can/can_callback.c @@ -119,15 +119,18 @@ uint16_t can_callback(FAR struct net_driver_s *dev, if (conn) { #ifdef CONFIG_NET_TIMESTAMP - /* TIMESTAMP sockopt is activated, create timestamp and copy to iob */ - if(conn->psock->s_timestamp) - { - struct timespec *ts = (struct timespec*)&dev->d_appdata[dev->d_len]; - struct timeval *tv = (struct timeval*)&dev->d_appdata[dev->d_len]; - dev->d_len += sizeof(struct timeval); - clock_systimespec(ts); - tv->tv_usec = ts->tv_nsec / 1000; - } + /* TIMESTAMP sockopt is activated, create timestamp and copy to iob */ + + if (conn->psock->s_timestamp) + { + struct timespec *ts = (struct timespec *) + &dev->d_appdata[dev->d_len]; + struct timeval *tv = (struct timeval *) + &dev->d_appdata[dev->d_len]; + dev->d_len += sizeof(struct timeval); + clock_systimespec(ts); + tv->tv_usec = ts->tv_nsec / 1000; + } #endif /* Perform the callback */ diff --git a/net/can/can_conn.c b/net/can/can_conn.c index 74f2d9b..574796c 100644 --- a/net/can/can_conn.c +++ b/net/can/can_conn.c @@ -138,9 +138,8 @@ FAR struct can_conn_s *can_alloc(void) /* Make sure that the connection is marked as uninitialized */ memset(conn, 0, sizeof(*conn)); - + /* FIXME SocketCAN default behavior enables loopback */ - #ifdef CONFIG_NET_CANPROTO_OPTIONS /* By default the filter is configured to catch all, @@ -158,7 +157,6 @@ FAR struct can_conn_s *can_alloc(void) conn->filter_count = 1; #endif - /* Enqueue the connection into the active list */ dq_addlast(&conn->node, &g_active_can_connections); @@ -172,8 +170,8 @@ 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(). + * Free a NetLink connection structure that is no longer in use. This + * should be done by the implementation of close(). * ****************************************************************************/ diff --git a/net/can/can_getsockopt.c b/net/can/can_getsockopt.c index e4149b6..238695c 100644 --- a/net/can/can_getsockopt.c +++ b/net/can/can_getsockopt.c @@ -98,7 +98,8 @@ int can_getsockopt(FAR struct socket *psock, int option, { ret = -EINVAL; } - else if (*value_len > CONFIG_NET_CAN_RAW_FILTER_MAX * sizeof(struct can_filter)) + else if (*value_len > CONFIG_NET_CAN_RAW_FILTER_MAX * + sizeof(struct can_filter)) { ret = -EINVAL; } @@ -106,19 +107,20 @@ int can_getsockopt(FAR struct socket *psock, int option, { int count = conn->filter_count; - if (*value_len < count * sizeof(struct can_filter)) + if (*value_len < count * sizeof(struct can_filter)) { count = *value_len / sizeof(struct can_filter); } - else - { - *value_len = count * sizeof(struct can_filter); - } + else + { + *value_len = count * sizeof(struct can_filter); + } - for(int i = 0; i < count; i++) + for (int i = 0; i < count; i++) { - ((struct can_filter *)value)[i] = conn->filters[i]; + ((struct can_filter *)value)[i] = conn->filters[i]; } + ret = OK; } break; diff --git a/net/can/can_recvfrom.c b/net/can/can_recvfrom.c index 98a0b2a..2211ed0 100644 --- a/net/can/can_recvfrom.c +++ b/net/can/can_recvfrom.c @@ -50,14 +50,13 @@ #include <sys/time.h> #endif - /**************************************************************************** * Private Types ****************************************************************************/ struct can_recvfrom_s { - FAR struct socket *pr_sock; /* The parent socket structure */ + FAR struct socket *pr_sock; /* The parent socket structure */ FAR struct devif_callback_s *pr_cb; /* Reference to callback instance */ sem_t pr_sem; /* Semaphore signals recv completion */ size_t pr_buflen; /* Length of receive buffer */ @@ -233,14 +232,15 @@ static inline void can_newdata(FAR struct net_driver_s *dev, static inline int can_readahead(struct can_recvfrom_s *pstate) { - FAR struct can_conn_s *conn = (FAR struct can_conn_s *)pstate->pr_sock->s_conn; + FAR struct can_conn_s *conn = + (FAR struct can_conn_s *) pstate->pr_sock->s_conn; FAR struct iob_s *iob; int recvlen; /* Check there is any CAN data already buffered in a read-ahead * buffer. */ - + pstate->pr_recvlen = -1; if ((iob = iob_peek_queue(&conn->readahead)) != NULL && @@ -287,15 +287,16 @@ static inline int can_readahead(struct can_recvfrom_s *pstate) IOBUSER_NET_CAN_READAHEAD); } - /* do not pass frames with DLC > 8 to a legacy socket */ - if (!conn->fd_frames) - { - struct canfd_frame *cfd = (struct canfd_frame *)pstate->pr_buffer; - if (cfd->len > CAN_MAX_DLEN) - { - return 0; - } - } + /* do not pass frames with DLC > 8 to a legacy socket */ + + if (!conn->fd_frames) + { + struct canfd_frame *cfd = (struct canfd_frame *)pstate->pr_buffer; + if (cfd->len > CAN_MAX_DLEN) + { + return 0; + } + } return recvlen; } @@ -319,13 +320,14 @@ static inline int can_readahead(struct can_recvfrom_s *pstate) * The network is locked. * ****************************************************************************/ + #ifdef CONFIG_NET_TIMESTAMP -static inline int can_readahead_timestamp(struct can_conn_s *conn, FAR uint8_t *buffer) +static inline int can_readahead_timestamp(struct can_conn_s *conn, + FAR uint8_t *buffer) { FAR struct iob_s *iob; int recvlen; - if ((iob = iob_peek_queue(&conn->readahead)) != NULL) { DEBUGASSERT(iob->io_pktlen > 0); @@ -379,26 +381,27 @@ static inline int can_readahead_timestamp(struct can_conn_s *conn, FAR uint8_t * #ifdef CONFIG_NET_CANPROTO_OPTIONS static int can_recv_filter(struct can_conn_s *conn, canid_t id) { - for(int i = 0; i < conn->filter_count; i++) + for (int i = 0; i < conn->filter_count; i++) { - if (conn->filters[i].can_id & CAN_INV_FILTER) - { - if((id & conn->filters[i].can_mask) != - ((conn->filters[i].can_id & ~CAN_INV_FILTER) - & conn->filters[i].can_mask)) - { - return 1; - } - } - else - { - if((id & conn->filters[i].can_mask) == - (conn->filters[i].can_id & conn->filters[i].can_mask)) - { - return 1; - } - } + if (conn->filters[i].can_id & CAN_INV_FILTER) + { + if ((id & conn->filters[i].can_mask) != + ((conn->filters[i].can_id & ~CAN_INV_FILTER) & + conn->filters[i].can_mask)) + { + return 1; + } + } + else + { + if ((id & conn->filters[i].can_mask) == + (conn->filters[i].can_id & conn->filters[i].can_mask)) + { + return 1; + } + } } + return 0; } #endif @@ -416,46 +419,50 @@ static uint16_t can_recvfrom_eventhandler(FAR struct net_driver_s *dev, { if ((flags & CAN_NEWDATA) != 0) { - /* If a new packet is available, check receive filters - * when is valid then complete the read action. */ + /* If a new packet is available, check receive filters + * when is valid then complete the read action. + */ #ifdef CONFIG_NET_CANPROTO_OPTIONS - if(can_recv_filter(conn,(canid_t)*dev->d_appdata) == 0) - { - flags &= ~CAN_NEWDATA; - return flags; - } + if (can_recv_filter(conn, (canid_t) *dev->d_appdata) == 0) + { + flags &= ~CAN_NEWDATA; + return flags; + } #endif - /* do not pass frames with DLC > 8 to a legacy socket */ - if (!conn->fd_frames) - { - struct canfd_frame *cfd = (struct canfd_frame*)dev->d_appdata; - if (cfd->len > CAN_MAX_DLEN) - { - /* DO WE NEED TO CLEAR FLAGS?? */ - flags &= ~CAN_NEWDATA; - return flags; - } - } + /* do not pass frames with DLC > 8 to a legacy socket */ + + if (!conn->fd_frames) + { + struct canfd_frame *cfd = (struct canfd_frame *)dev->d_appdata; + if (cfd->len > CAN_MAX_DLEN) + { + /* DO WE NEED TO CLEAR FLAGS?? */ + + flags &= ~CAN_NEWDATA; + return flags; + } + } /* Copy the packet */ can_newdata(dev, pstate); #ifdef CONFIG_NET_TIMESTAMP - if(pstate->pr_sock->s_timestamp) - { - if(pstate->pr_msglen == sizeof(struct timeval)) - { - can_readahead_timestamp(conn, pstate->pr_msgbuf); - } - else - { - /* We still have to consume the data otherwise IOB gets full */ - uint8_t dummy_buf[sizeof(struct timeval)]; - can_readahead_timestamp(conn, &dummy_buf); - } - } + if (pstate->pr_sock->s_timestamp) + { + if (pstate->pr_msglen == sizeof(struct timeval)) + { + can_readahead_timestamp(conn, pstate->pr_msgbuf); + } + else + { + /* We still have to consume the data otherwise IOB gets full */ + + uint8_t dummy_buf[sizeof(struct timeval)]; + can_readahead_timestamp(conn, &dummy_buf); + } + } #endif /* We are finished. */ @@ -508,6 +515,7 @@ static ssize_t can_recvfrom_result(int result, if (pstate->pr_result < 0) { /* This might return EAGAIN on a timeout */ + return pstate->pr_result; } @@ -592,10 +600,10 @@ ssize_t can_recvfrom(FAR struct socket *psock, FAR void *buf, ret = can_readahead(&state); if (ret > 0) - { + { goto errout_with_state; } - + ret = state.pr_recvlen; /* Handle non-blocking CAN sockets */ @@ -610,7 +618,7 @@ ssize_t can_recvfrom(FAR struct socket *psock, FAR void *buf, { /* Nothing was received */ - ret = -EAGAIN; + ret = -EAGAIN; goto errout_with_state; } } @@ -675,6 +683,7 @@ errout_with_state: * flags Receive flags (ignored) * ****************************************************************************/ + #ifdef CONFIG_NET_RECVMSG_CMSG ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, size_t len, int flags) @@ -707,20 +716,22 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, nxsem_init(&state.pr_sem, 0, 0); /* Doesn't really fail */ nxsem_setprotocol(&state.pr_sem, SEM_PRIO_NONE); - state.pr_buflen = msg->msg_iov->iov_len; state.pr_buffer = msg->msg_iov->iov_base; + #ifdef CONFIG_NET_TIMESTAMP - if(psock->s_timestamp && msg->msg_controllen == (sizeof(struct cmsghdr) + sizeof(struct timeval))) + if (psock->s_timestamp && msg->msg_controllen == + (sizeof(struct cmsghdr) + sizeof(struct timeval))) { - struct cmsghdr* cmsg = CMSG_FIRSTHDR(msg); - state.pr_msglen = sizeof(struct timeval); - state.pr_msgbuf = CMSG_DATA(cmsg); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SO_TIMESTAMP; - cmsg->cmsg_len = state.pr_msglen; + struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); + state.pr_msglen = sizeof(struct timeval); + state.pr_msgbuf = CMSG_DATA(cmsg); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SO_TIMESTAMP; + cmsg->cmsg_len = state.pr_msglen; } #endif + state.pr_sock = psock; /* Handle any any CAN data already buffered in a read-ahead buffer. NOTE @@ -732,20 +743,22 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, if (ret > 0) { #ifdef CONFIG_NET_TIMESTAMP - if(psock->s_timestamp) - { - if(state.pr_msglen == sizeof(struct timeval)) - { - can_readahead_timestamp(conn, state.pr_msgbuf); - } - else - { - /* We still have to consume the data otherwise IOB gets full */ - uint8_t dummy_buf[sizeof(struct timeval)]; - can_readahead_timestamp(conn, &dummy_buf); - } - } + if (psock->s_timestamp) + { + if (state.pr_msglen == sizeof(struct timeval)) + { + can_readahead_timestamp(conn, state.pr_msgbuf); + } + else + { + /* We still have to consume the data otherwise IOB gets full */ + + uint8_t dummy_buf[sizeof(struct timeval)]; + can_readahead_timestamp(conn, &dummy_buf); + } + } #endif + goto errout_with_state; } diff --git a/net/can/can_send.c b/net/can/can_send.c index be621b4..644adc3 100644 --- a/net/can/can_send.c +++ b/net/can/can_send.c @@ -118,8 +118,10 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev, else { /* Copy the packet data into the device packet buffer and send it */ - //FIXME potentialy wrong function do we have a header?? - devif_can_send(dev, pstate->snd_buffer, pstate->snd_buflen); + + /* FIXME potentialy wrong function do we have a header?? */ + + devif_can_send(dev, pstate->snd_buffer, pstate->snd_buflen); pstate->snd_sent = pstate->snd_buflen; } @@ -164,10 +166,10 @@ ssize_t psock_can_send(FAR struct socket *psock, FAR const void *buf, size_t len) { FAR struct net_driver_s *dev; - FAR struct can_conn_s *conn; + FAR struct can_conn_s *conn; struct send_s state; int ret = OK; - + conn = (FAR struct can_conn_s *)psock->s_conn; /* Verify that the sockfd corresponds to valid, allocated socket */ @@ -184,20 +186,20 @@ ssize_t psock_can_send(FAR struct socket *psock, FAR const void *buf, { return -ENODEV; } - - if(conn->fd_frames) + + if (conn->fd_frames) { - if(len != CANFD_MTU && len != CAN_MTU) - { - return -EINVAL; - } + if (len != CANFD_MTU && len != CAN_MTU) + { + return -EINVAL; + } } - else + else { - if(len != CAN_MTU) - { - return -EINVAL; - } + if (len != CAN_MTU) + { + return -EINVAL; + } } /* Perform the send operation */ @@ -236,8 +238,8 @@ ssize_t psock_can_send(FAR struct socket *psock, FAR const void *buf, netdev_txnotify_dev(dev); /* Wait for the send to complete or an error to occur. - * net_lockedwait will also terminate if a signal is received. - */ + * net_lockedwait will also terminate if a signal is received. + */ ret = net_lockedwait(&state.snd_sem); diff --git a/net/can/can_setsockopt.c b/net/can/can_setsockopt.c index 659d84b..6858c1a 100644 --- a/net/can/can_setsockopt.c +++ b/net/can/can_setsockopt.c @@ -88,29 +88,30 @@ int can_setsockopt(FAR struct socket *psock, int option, switch (option) { case CAN_RAW_FILTER: - if (value_len == 0) - { - conn->filter_count = 0; - ret = OK; - } - else if (value_len % sizeof(struct can_filter) != 0) + if (value_len == 0) + { + conn->filter_count = 0; + ret = OK; + } + else if (value_len % sizeof(struct can_filter) != 0) { ret = -EINVAL; } - else if (value_len > CONFIG_NET_CAN_RAW_FILTER_MAX * sizeof(struct can_filter)) + else if (value_len > CONFIG_NET_CAN_RAW_FILTER_MAX * + sizeof(struct can_filter)) { ret = -EINVAL; } else { - count = value_len / sizeof(struct can_filter); + count = value_len / sizeof(struct can_filter); - for(int i = 0; i < count; i++) - { - conn->filters[i] = ((struct can_filter *)value)[i]; - } + for (int i = 0; i < count; i++) + { + conn->filters[i] = ((struct can_filter *)value)[i]; + } - conn->filter_count = count; + conn->filter_count = count; ret = OK; } @@ -120,28 +121,28 @@ int can_setsockopt(FAR struct socket *psock, int option, break; case CAN_RAW_LOOPBACK: - if (value_len != sizeof(conn->loopback)) - return -EINVAL; + if (value_len != sizeof(conn->loopback)) + return -EINVAL; - conn->loopback = *(FAR int32_t *)value; + conn->loopback = *(FAR int32_t *)value; - break; + break; case CAN_RAW_RECV_OWN_MSGS: - if (value_len != sizeof(conn->recv_own_msgs)) - return -EINVAL; + if (value_len != sizeof(conn->recv_own_msgs)) + return -EINVAL; - conn->recv_own_msgs = *(FAR int32_t *)value; + conn->recv_own_msgs = *(FAR int32_t *)value; - break; + break; case CAN_RAW_FD_FRAMES: - if (value_len != sizeof(conn->fd_frames)) - return -EINVAL; + if (value_len != sizeof(conn->fd_frames)) + return -EINVAL; - conn->fd_frames = *(FAR int32_t *)value; + conn->fd_frames = *(FAR int32_t *)value; - break; + break; case CAN_RAW_JOIN_FILTERS: break; diff --git a/net/can/can_sockif.c b/net/can/can_sockif.c index 844ac3b..737788e 100644 --- a/net/can/can_sockif.c +++ b/net/can/can_sockif.c @@ -154,7 +154,8 @@ static uint16_t can_poll_eventhandler(FAR struct net_driver_s *dev, #if 0 /* A poll is a sign that we are free to send data. */ - else if ((flags & CAN_POLL) != 0 && psock_udp_cansend(info->psock) >= 0) + else if ((flags & CAN_POLL) != 0 && + psock_udp_cansend(info->psock) >= 0) { eventset |= (POLLOUT & info->fds->events); } @@ -234,6 +235,7 @@ static int can_setup(FAR struct socket *psock, int protocol) #ifdef CONFIG_NET_TIMESTAMP /* Store psock in conn se we can read the SO_TIMESTAMP value */ + conn->psock = psock; #endif @@ -486,7 +488,8 @@ static int can_listen(FAR struct socket *psock, int backlog) * Perform a can connection * * Input Parameters: - * psock A reference to the socket structure of the socket to be connected + * psock A reference to the socket structure of the socket + * to be connected * addr The address of the remote server to connect to * addrlen Length of address buffer * @@ -615,8 +618,8 @@ static int can_poll_local(FAR struct socket *psock, FAR struct pollfd *fds, info->cb = cb; /* Initialize the callback structure. Save the reference to the info - * structure as callback private data so that it will be available during - * callback processing. + * structure as callback private data so that it will be available + * during callback processing. */ cb->flags = NETDEV_DOWN; @@ -747,7 +750,8 @@ static ssize_t can_send(FAR struct socket *psock, FAR const void *buf, * returned when the socket was not actually connected. * * Input Parameters: - * psock A reference to the socket structure of the socket to be connected + * psock A reference to the socket structure of the socket + * to be connected * buf Data to send * len Length of data to send * flags Send flags (ignored)