xiaoxiang781216 commented on code in PR #2101: URL: https://github.com/apache/nuttx-apps/pull/2101#discussion_r1347559512
########## netutils/ptpd/ptpd.c: ########## @@ -0,0 +1,931 @@ +/**************************************************************************** + * apps/netutils/ptpd/ptpd.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> + +#include <stdbool.h> +#include <stdint.h> + +#include <sys/socket.h> +#include <sys/time.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sched.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <unistd.h> +#include <fcntl.h> + +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netutils/ipmsfilter.h> + +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/poll.h> +#include <nuttx/net/netconfig.h> +#include <netutils/ptpd.h> + +#include "ptpv2.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct ptp_state_s +{ + bool stop; + + /* Address of network interface we are operating on */ + + struct sockaddr_in interface_addr; + + /* Socket bound to interface for transmission */ + + int tx_socket; + + /* Sockets for PTP event and information ports */ + + int event_socket; + int info_socket; + + /* Our own identity as a clock source */ + + struct ptp_announce_s own_identity; + + /* Sequence number counters per message type */ + + uint16_t announce_seq; + uint16_t sync_seq; + uint16_t delay_req_seq; + + /* Identity of currently selected clock source, + * from the latest announcement message. + * + * The timestamp is used for timeout when a source + * disappears, it is from the local monotonic clock. + */ + + struct ptp_announce_s selected_source; + struct timespec last_received_sync; + + /* Last transmitted sync & announcement packets */ + + struct timespec last_transmitted_sync; + struct timespec last_transmitted_announce; + + /* Latest received packet and its timestamp */ + + struct timespec rxtime; + union + { + struct ptp_header_s header; + struct ptp_announce_s announce; + struct ptp_sync_s sync; + struct ptp_follow_up_s follow_up; + uint8_t raw[128]; + } rxbuf; + + union + { + uint8_t raw[64]; + } rxcmsg; + + /* Buffered sync packet for two-step clock setting */ + + struct ptp_sync_s twostep_packet; + struct timespec twostep_rxtime; +}; + +#ifdef CONFIG_NETUTILS_PTPD_SERVER +# define PTPD_POLL_INTERVAL CONFIG_NETUTILS_PTPD_SYNC_INTERVAL_MSEC +#else +# define PTPD_POLL_INTERVAL CONFIG_NETUTILS_PTPD_TIMEOUT_MS +#endif + +/* PTP debug messages are enabled by either CONFIG_DEBUG_NET_INFO + * or separately by CONFIG_NETUTILS_PTPD_DEBUG. This simplifies + * debugging without having excessive amount of logging from net. + */ + +#ifdef CONFIG_NETUTILS_PTPD_DEBUG +# ifndef CONFIG_DEBUG_NET_INFO +# define ptpinfo _info Review Comment: need four spaces ########## netutils/ptpd/ptpd.c: ########## @@ -0,0 +1,931 @@ +/**************************************************************************** + * apps/netutils/ptpd/ptpd.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> + +#include <stdbool.h> +#include <stdint.h> + +#include <sys/socket.h> +#include <sys/time.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sched.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <unistd.h> +#include <fcntl.h> + +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netutils/ipmsfilter.h> + +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/poll.h> +#include <nuttx/net/netconfig.h> +#include <netutils/ptpd.h> + +#include "ptpv2.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct ptp_state_s +{ + bool stop; + + /* Address of network interface we are operating on */ + + struct sockaddr_in interface_addr; + + /* Socket bound to interface for transmission */ + + int tx_socket; + + /* Sockets for PTP event and information ports */ + + int event_socket; + int info_socket; + + /* Our own identity as a clock source */ + + struct ptp_announce_s own_identity; + + /* Sequence number counters per message type */ + + uint16_t announce_seq; + uint16_t sync_seq; + uint16_t delay_req_seq; + + /* Identity of currently selected clock source, + * from the latest announcement message. + * + * The timestamp is used for timeout when a source + * disappears, it is from the local monotonic clock. + */ + + struct ptp_announce_s selected_source; + struct timespec last_received_sync; + + /* Last transmitted sync & announcement packets */ + + struct timespec last_transmitted_sync; + struct timespec last_transmitted_announce; + + /* Latest received packet and its timestamp */ + + struct timespec rxtime; + union + { + struct ptp_header_s header; + struct ptp_announce_s announce; + struct ptp_sync_s sync; + struct ptp_follow_up_s follow_up; + uint8_t raw[128]; + } rxbuf; + + union + { + uint8_t raw[64]; + } rxcmsg; + + /* Buffered sync packet for two-step clock setting */ + + struct ptp_sync_s twostep_packet; + struct timespec twostep_rxtime; +}; + +#ifdef CONFIG_NETUTILS_PTPD_SERVER +# define PTPD_POLL_INTERVAL CONFIG_NETUTILS_PTPD_SYNC_INTERVAL_MSEC Review Comment: need two spaces ########## netutils/ptpd/ptpd.c: ########## @@ -0,0 +1,931 @@ +/**************************************************************************** + * apps/netutils/ptpd/ptpd.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> + +#include <stdbool.h> +#include <stdint.h> + +#include <sys/socket.h> +#include <sys/time.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sched.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <unistd.h> +#include <fcntl.h> + +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netutils/ipmsfilter.h> + +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/poll.h> +#include <nuttx/net/netconfig.h> +#include <netutils/ptpd.h> + +#include "ptpv2.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct ptp_state_s +{ + bool stop; + + /* Address of network interface we are operating on */ + + struct sockaddr_in interface_addr; + + /* Socket bound to interface for transmission */ + + int tx_socket; + + /* Sockets for PTP event and information ports */ + + int event_socket; + int info_socket; + + /* Our own identity as a clock source */ + + struct ptp_announce_s own_identity; + + /* Sequence number counters per message type */ + + uint16_t announce_seq; + uint16_t sync_seq; + uint16_t delay_req_seq; + + /* Identity of currently selected clock source, + * from the latest announcement message. + * + * The timestamp is used for timeout when a source + * disappears, it is from the local monotonic clock. + */ + + struct ptp_announce_s selected_source; + struct timespec last_received_sync; + + /* Last transmitted sync & announcement packets */ + + struct timespec last_transmitted_sync; + struct timespec last_transmitted_announce; + + /* Latest received packet and its timestamp */ + + struct timespec rxtime; + union + { + struct ptp_header_s header; + struct ptp_announce_s announce; + struct ptp_sync_s sync; + struct ptp_follow_up_s follow_up; + uint8_t raw[128]; + } rxbuf; + + union + { + uint8_t raw[64]; + } rxcmsg; + + /* Buffered sync packet for two-step clock setting */ + + struct ptp_sync_s twostep_packet; + struct timespec twostep_rxtime; +}; + +#ifdef CONFIG_NETUTILS_PTPD_SERVER +# define PTPD_POLL_INTERVAL CONFIG_NETUTILS_PTPD_SYNC_INTERVAL_MSEC +#else +# define PTPD_POLL_INTERVAL CONFIG_NETUTILS_PTPD_TIMEOUT_MS +#endif + +/* PTP debug messages are enabled by either CONFIG_DEBUG_NET_INFO + * or separately by CONFIG_NETUTILS_PTPD_DEBUG. This simplifies + * debugging without having excessive amount of logging from net. + */ + +#ifdef CONFIG_NETUTILS_PTPD_DEBUG +# ifndef CONFIG_DEBUG_NET_INFO Review Comment: need two spaces -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org