Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic IEEE-1588 PTP server/client #2101

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/adjtime/adjtime_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int main(int argc, FAR char *argv[])
parse_args(&delta, argc, argv);

printf("Delta time is %ld seconds and %ld micro seconds.\n",
delta.tv_sec, delta.tv_usec);
(long)delta.tv_sec, delta.tv_usec);
xiaoxiang781216 marked this conversation as resolved.
Show resolved Hide resolved

/* Call adjtime function. */

Expand All @@ -160,7 +160,7 @@ int main(int argc, FAR char *argv[])
else
{
printf("Returned olddelta is %ld seconds and %ld micro seconds.\n",
olddelta.tv_sec, olddelta.tv_usec);
(long)olddelta.tv_sec, olddelta.tv_usec);
}

return ret;
Expand Down
71 changes: 71 additions & 0 deletions include/netutils/ptpd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/****************************************************************************
* apps/include/netutils/ptpd.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 __APPS_INCLUDE_NETUTILS_PTPD_H
#define __APPS_INCLUDE_NETUTILS_PTPD_H

/****************************************************************************
* Included Files
****************************************************************************/

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Public Types
****************************************************************************/

/****************************************************************************
* Public Data
****************************************************************************/

#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

/****************************************************************************
* Name: ptpd_start
*
* Description:
* Start the PTP daemon and bind it to specified interface.
*
* Returned Value:
* On success, the non-negative task ID of the PTP daemon is returned;
* On failure, a negated errno value is returned.
*
****************************************************************************/

int ptpd_start(const char *interface);

#undef EXTERN
#ifdef __cplusplus
}
#endif

#endif /* __APPS_INCLUDE_NETUTILS_PTPD_H */
167 changes: 167 additions & 0 deletions netutils/ptpd/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config NETUTILS_PTPD
bool "PTPD client/server"
default n
depends on NET_IPv4
depends on NET_IGMP
depends on NET_UDP
---help---
Build a minimal implementation of IEEE-1588 precision time protocol.
Uses system gettimeofday() and adjtime() calls to synchronize clock
with a master clock through network, or to provide a master clock to
other systems.

if NETUTILS_PTPD

config NETUTILS_PTPD_DEBUG
bool "Enable PTP debug messages"
default n
depends on DEBUG_INFO
---help---
Enable PTP debug messages even if CONFIG_DEBUG_NET_INFO is not enabled.

config NETUTILS_PTPD_CLIENT
bool "Enable client support"
default y
---help---
Act as a PTP client, synchronizing the NuttX clock to a remote master
clock.

config NETUTILS_PTPD_SERVER
bool "Enable server support"
default n
---help---
Act as a PTP server, providing NuttX clock time to other systems.

Both server and client can be simultaneously enabled. NuttX will then
synchronize to a higher priority master clock, or act as a master
clock itself if it has the highest priority.
Refer to Best Master Clock algorithm in IEEE-1588 for details.

config NETUTILS_PTPD_STACKSIZE
int "PTP daemon stack stack size"
default DEFAULT_TASK_STACKSIZE

config NETUTILS_PTPD_SERVERPRIO
int "PTP daemon priority"
default 100

config NETUTILS_PTPD_DOMAIN
int "PTP domain selection"
default 0
range 0 127
---help---
Set PTP domain to participate in. Default domain is 0, other domains
can be used to isolate reference clocks from each other.

if NETUTILS_PTPD_SERVER

config NETUTILS_PTPD_PRIORITY1
int "PTP server priority1"
default 128
range 0 255
---help---
Set clock priority to announce when acting as a PTP server.
Lower value is higher priority.

A higher priority1 clock will be selected without regard to announced
clock quality fields.
Refer to Best Master Clock algorithm in IEEE-1588 for details.

config NETUTILS_PTPD_PRIORITY2
int "PTP server priority2"
default 128
range 0 255
---help---
Set clock subpriority to announce when acting as a PTP server.
This will distinguish between two clocks that are equivalent in
priority1, class and accuracy values.
Lower value is higher priority.

config NETUTILS_PTPD_CLASS
int "PTP server class"
default 248
range 0 255
---help---
Set master clock class to announce when acting as a PTP server.
Lower value means higher quality clock source.
248 is the default for unknown class.

config NETUTILS_PTPD_ACCURACY
int "PTP server accuracy"
default 254
range 0 255
---help---
Set master clock accuracy to announce when acting as a PTP server.
Logarithmic scale is defined in IEEE-1588:
32: +- 25 ns
33: +- 100 ns
34: +- 250 ns
35: +- 1 us
36: +- 2.5 us
37: +- 10 us
38: +- 25 us
39: +- 100 us
40: +- 250 us
41: +- 1 ms
42: +- 2.5 ms
43: +- 10 ms
44: +- 25 ms
45: +- 100 ms
46: +- 250 ms
47: +- 1 s
48: +- 10 s
49: +- more than 10 s
254: Unknown

config NETUTILS_PTPD_CLOCKSOURCE
int "PTP server clock source type"
default 160
range 0 255
---help---
Set clock source type to announce when acting as a PTP server.
Common values:
32: GPS
64: PTP
80: NTP
144: Other
160: Internal oscillator

config NETUTILS_PTPD_SYNC_INTERVAL_MSEC
int "PTP server sync transmit interval (ms)"
default 1000
---help---
How often to transmit sync packets in server mode.

config NETUTILS_PTPD_ANNOUNCE_INTERVAL_MSEC
int "PTP server announce transmit interval (ms)"
default 10000
---help---
How often to transmit announce packets in server mode.

endif # NETUTILS_PTPD_SERVER

if NETUTILS_PTPD_CLIENT

config NETUTILS_PTPD_TIMEOUT_MS
int "PTP client timeout for changing clock source (ms)"
default 60000
---help---
If no packets are being received from currently chosen clock source,
fall back to next best clock source after this many seconds.

config NETUTILS_PTPD_SETTIME_THRESHOLD_MS
int "PTP client threshold for changing system time (ms)"
default 1000
---help---
If difference between local and remote clock exceeds this threshold,
time is reset with settimeofday() instead of changing the rate with
adjtime().

endif # NETUTILS_PTPD_CLIENT

endif # NETUTILS_PTPD
23 changes: 23 additions & 0 deletions netutils/ptpd/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/netutils/ptpd/Make.defs
#
# 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.
#
############################################################################

ifneq ($(CONFIG_NETUTILS_PTPD),)
CONFIGURED_APPS += $(APPDIR)/netutils/ptpd
endif
27 changes: 27 additions & 0 deletions netutils/ptpd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
############################################################################
# apps/netutils/ptpd/Makefile
#
# 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.
#
############################################################################

include $(APPDIR)/Make.defs

# PTP server/client implementation

CSRCS = ptpd.c

include $(APPDIR)/Application.mk
Loading