ptpd.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2024 The Apache Software Foundation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /****************************************************************************
  9. * apps/include/netutils/ptpd.h
  10. *
  11. * Licensed to the Apache Software Foundation (ASF) under one or more
  12. * contributor license agreements. See the NOTICE file distributed with
  13. * this work for additional information regarding copyright ownership. The
  14. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  15. * "License"); you may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  23. * License for the specific language governing permissions and limitations
  24. * under the License.
  25. *
  26. ****************************************************************************/
  27. #ifndef __APPS_INCLUDE_NETUTILS_PTPD_H
  28. #define __APPS_INCLUDE_NETUTILS_PTPD_H
  29. // ESP_PTP
  30. #include <time.h>
  31. #ifndef FAR
  32. #define FAR
  33. #endif
  34. /****************************************************************************
  35. * Included Files
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Pre-processor Definitions
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Public Types
  42. ****************************************************************************/
  43. /* PTPD status information structure */
  44. struct ptpd_status_s
  45. {
  46. /* Is there a valid remote clock source active? */
  47. bool clock_source_valid;
  48. /* Information about selected best clock source */
  49. struct
  50. {
  51. uint8_t id[8]; /* Clock identity */
  52. int utcoffset; /* Offset between clock time and UTC time (seconds) */
  53. int priority1; /* Main priority field */
  54. int clockclass; /* Clock class (IEEE-1588, lower is better) */
  55. int accuracy; /* Clock accuracy (IEEE-1588, lower is better) */
  56. int variance; /* Clock variance (IEEE-1588, lower is better) */
  57. int priority2; /* Secondary priority field */
  58. uint8_t gm_id[8]; /* Grandmaster clock identity */
  59. int stepsremoved; /* How many steps from grandmaster clock */
  60. int timesource; /* Type of time source (IEEE-1588) */
  61. } clock_source_info;
  62. /* When was clock last updated or adjusted (CLOCK_REALTIME).
  63. * Matches last_received_sync but in different clock.
  64. */
  65. struct timespec last_clock_update;
  66. /* Details of clock adjustment made at last_clock_update */
  67. int64_t last_delta_ns; /* Latest measured clock error */
  68. int64_t last_adjtime_ns; /* Previously applied adjtime() offset */
  69. /* Averaged clock drift estimate (parts per billion).
  70. * Positive means remote clock runs faster than local clock before
  71. * adjustment.
  72. */
  73. long drift_ppb;
  74. /* Averaged path delay */
  75. long path_delay_ns;
  76. /* Timestamps of latest received packets (CLOCK_MONOTONIC) */
  77. struct timespec last_received_multicast; /* Any multicast packet */
  78. struct timespec last_received_announce; /* Announce from any server */
  79. struct timespec last_received_sync; /* Sync from selected source */
  80. /* Timestamps of latest transmitted packets (CLOCK_MONOTONIC) */
  81. struct timespec last_transmitted_sync;
  82. struct timespec last_transmitted_announce;
  83. struct timespec last_transmitted_delayresp;
  84. struct timespec last_transmitted_delayreq;
  85. };
  86. /****************************************************************************
  87. * Public Data
  88. ****************************************************************************/
  89. #ifdef __cplusplus
  90. #define EXTERN extern "C"
  91. extern "C"
  92. {
  93. #else
  94. #define EXTERN extern
  95. #endif
  96. /****************************************************************************
  97. * Public Function Prototypes
  98. ****************************************************************************/
  99. /****************************************************************************
  100. * Name: ptpd_start
  101. *
  102. * Description:
  103. * Start the PTP daemon and bind it to specified interface.
  104. *
  105. * Input Parameters:
  106. * interface - Name of the network interface to bind to, e.g. "eth0"
  107. *
  108. * Returned Value:
  109. * On success, the non-negative task ID of the PTP daemon is returned;
  110. * On failure, a negated errno value is returned.
  111. *
  112. ****************************************************************************/
  113. int ptpd_start(FAR const char *interface);
  114. /****************************************************************************
  115. * Name: ptpd_status
  116. *
  117. * Description:
  118. * Query status from a running PTP daemon.
  119. *
  120. * Input Parameters:
  121. * pid - Process ID previously returned by ptpd_start()
  122. * status - Pointer to storage for status information.
  123. *
  124. * Returned Value:
  125. * On success, returns OK.
  126. * On failure, a negated errno value is returned.
  127. *
  128. * Assumptions/Limitations:
  129. * Multiple threads with priority less than CONFIG_NETUTILS_PTPD_SERVERPRIO
  130. * can request status simultaneously. If higher priority threads request
  131. * status simultaneously, some of the requests may timeout.
  132. *
  133. ****************************************************************************/
  134. int ptpd_status(int pid, FAR struct ptpd_status_s *status);
  135. /****************************************************************************
  136. * Name: ptpd_stop
  137. *
  138. * Description:
  139. * Stop PTP daemon
  140. *
  141. * Input Parameters:
  142. * pid - Process ID previously returned by ptpd_start()
  143. *
  144. * Returned Value:
  145. * On success, returns OK.
  146. * On failure, a negated errno value is returned.
  147. *
  148. ****************************************************************************/
  149. int ptpd_stop(int pid);
  150. #undef EXTERN
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif /* __APPS_INCLUDE_NETUTILS_PTPD_H */