nuttx-apps/include/netutils/ptpd.h
Daniel P. Carvalho c78e4d69ea netutils/ptpd: Add egress latency compensation for TX timestamps.
The frame leaves the MAC later than the moment its hardware transmit
timestamp is latched, because of the clock domain crossing and the PHY.
This fixed delay is the egressLatency port parameter of IEEE 1588.

Add the configured latency to every hardware transmit timestamp
obtained through MSG_ERRQUEUE, the counterpart of the ingress
compensation.

- Add CONFIG_NETUTILS_PTPD_EGRESS_LATENCY_NS (default 0, which applies
  no compensation).
- Add the -O option to override it at run time.
- Add egress_latency_ns to struct ptpd_config_s.

Software timestamps are not affected.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
2026-09-24 11:07:31 +08:00

199 lines
6.3 KiB
C

/****************************************************************************
* apps/include/netutils/ptpd.h
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
****************************************************************************/
#include <sys/types.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
enum ptp_delay_mechanism_e
{
PTP_DELAY_NONE = 0,
PTP_DELAY_E2E,
PTP_DELAY_P2P
};
struct ptpd_config_s
{
FAR const char *interface;
FAR const char *clock;
bool client_only;
bool hardware_ts;
enum ptp_delay_mechanism_e delay_mechanism;
bool bmca;
sa_family_t af;
int32_t ingress_latency_ns; /* Hardware RX timestamp latency (ns) */
int32_t egress_latency_ns; /* Hardware TX timestamp latency (ns) */
};
/* PTPD status information structure */
struct ptpd_status_s
{
/* Is there a valid remote clock source active? */
bool clock_source_valid;
/* Information about selected best clock source */
struct
{
uint8_t id[8]; /* Clock identity */
int utcoffset; /* Offset between clock time and UTC time (seconds) */
int priority1; /* Main priority field */
int clockclass; /* Clock class (IEEE-1588, lower is better) */
int accuracy; /* Clock accuracy (IEEE-1588, lower is better) */
int variance; /* Clock variance (IEEE-1588, lower is better) */
int priority2; /* Secondary priority field */
uint8_t gm_id[8]; /* Grandmaster clock identity */
int stepsremoved; /* How many steps from grandmaster clock */
int timesource; /* Type of time source (IEEE-1588) */
} clock_source_info;
/* When was clock last updated or adjusted (CLOCK_REALTIME).
* Matches last_received_sync but in different clock.
*/
struct timespec last_clock_update;
/* Details of clock adjustment made at last_clock_update */
int64_t last_delta_ns; /* Latest measured clock error */
int64_t last_adjtime_ns; /* Previously applied adjtime() offset */
/* Averaged clock drift estimate (parts per billion).
* Positive means remote clock runs faster than local clock before
* adjustment.
*/
long drift_ppb;
/* Averaged path delay */
long path_delay_ns;
/* Timestamps of latest received packets (CLOCK_MONOTONIC) */
struct timespec last_received_multicast; /* Any multicast packet */
struct timespec last_received_announce; /* Announce from any server */
struct timespec last_received_sync; /* Sync from selected source */
/* Timestamps of latest transmitted packets (CLOCK_MONOTONIC) */
struct timespec last_transmitted_sync;
struct timespec last_transmitted_announce;
struct timespec last_transmitted_delayresp;
struct timespec last_transmitted_delayreq;
struct timespec last_transmitted_pdelayreq;
};
/****************************************************************************
* 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 config.
*
* Input Parameters:
* config - The configs of PTP daemon, includes interface, af and clock...
*
* 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(FAR const struct ptpd_config_s *config);
/****************************************************************************
* Name: ptpd_status
*
* Description:
* Query status from a running PTP daemon.
*
* Input Parameters:
* pid - Process ID previously returned by ptpd_start()
* status - Pointer to storage for status information.
*
* Returned Value:
* On success, returns OK.
* On failure, a negated errno value is returned.
*
* Assumptions/Limitations:
* Multiple threads with priority less than CONFIG_NETUTILS_PTPD_SERVERPRIO
* can request status simultaneously. If higher priority threads request
* status simultaneously, some of the requests may timeout.
*
****************************************************************************/
int ptpd_status(int pid, FAR struct ptpd_status_s *status);
/****************************************************************************
* Name: ptpd_stop
*
* Description:
* Stop PTP daemon
*
* Input Parameters:
* pid - Process ID previously returned by ptpd_start()
*
* Returned Value:
* On success, returns OK.
* On failure, a negated errno value is returned.
*
****************************************************************************/
int ptpd_stop(int pid);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __APPS_INCLUDE_NETUTILS_PTPD_H */