This patch adds dedicated logic for opening and closing posix clocks.
We will need this for adding logic to ptp4l for using clocks that
may not be ptp clocks.

Signed-off-by: Dimitrios Katsaros <[email protected]>
---
 makefile     |  2 +-
 missing.h    | 10 +++++++++-
 posixclock.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 posixclock.h | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+), 2 deletions(-)
 create mode 100644 posixclock.c
 create mode 100644 posixclock.h

diff --git a/makefile b/makefile
index d09a4a9..dc3bf7b 100644
--- a/makefile
+++ b/makefile
@@ -28,7 +28,7 @@ e2e_tc.o fault.o filter.o fsm.o hash.o linreg.o mave.o 
mmedian.o msg.o ntpshm.o
 nullf.o phc.o pi.o port.o port_signaling.o pqueue.o print.o ptp4l.o p2p_tc.o \
 raw.o rtnl.o servo.o sk.o stats.o tc.o telecom.o tlv.o transport.o tsproc.o \
 udp.o udp6.o uds.o unicast_client.o unicast_fsm.o unicast_service.o util.o \
-version.o
+version.o posixclock.o
 
 OBJECTS        = $(OBJ) hwstamp_ctl.o nsm.o phc2sys.o phc_ctl.o pmc.o 
pmc_common.o \
  sysoff.o timemaster.o
diff --git a/missing.h b/missing.h
index 2f7adb9..8eb89db 100644
--- a/missing.h
+++ b/missing.h
@@ -44,10 +44,18 @@
 #define CLOCK_INVALID -1
 #endif
 
+#ifndef CPUCLOCK_PERTHREAD_MASK
+#define CPUCLOCK_PERTHREAD_MASK 4
+#endif
+
+#ifndef CPUCLOCK_CLOCK_MASK
+#define CPUCLOCK_CLOCK_MASK    3
+#endif
+
 #define CLOCKFD 3
 #define FD_TO_CLOCKID(fd)      ((clockid_t) ((((unsigned int) ~fd) << 3) | 
CLOCKFD))
 #define CLOCKID_TO_FD(clk)     ((unsigned int) ~((clk) >> 3))
-
+#define CLOCKFD_MASK           (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
 #ifndef HAVE_ONESTEP_SYNC
 enum _missing_hwtstamp_tx_types {
        HWTSTAMP_TX_ONESTEP_SYNC = 2,
diff --git a/posixclock.c b/posixclock.c
new file mode 100644
index 0000000..9861aec
--- /dev/null
+++ b/posixclock.c
@@ -0,0 +1,51 @@
+/**
+ * @file posixclock.c
+ * @note Copyright (C) 2018 Dimitrios Katsaros <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <fcntl.h>
+#include "missing.h"
+
+#include "posixclock.h"
+
+clockid_t posix_clock_open(const char *device_path)
+{
+       clockid_t clkid;
+       struct timespec ts;
+       int fd;
+
+       if (!device_path)
+               return CLOCK_INVALID;
+       fd = open(device_path, O_RDWR);
+       if (fd < 0)
+               return CLOCK_INVALID;
+
+       clkid = FD_TO_CLOCKID(fd);
+       /* did we actually open a clock? */
+       if (clock_gettime(clkid, &ts)) {
+               close(fd);
+               return CLOCK_INVALID;
+       }
+       return clkid;
+}
+
+void posix_clock_close(clockid_t clkid)
+{
+       if ((clkid < 0) && (clkid & CLOCKFD_MASK) != CLOCKFD)
+               return;
+
+       close(CLOCKID_TO_FD(clkid));
+}
diff --git a/posixclock.h b/posixclock.h
new file mode 100644
index 0000000..11db38f
--- /dev/null
+++ b/posixclock.h
@@ -0,0 +1,37 @@
+/**
+ * @file posixclock.h
+ * @brief Wraps clock character device functionality.
+ * @note Copyright (C) 2018 Dimitrios Katsaros <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef HAVE_POSIX_CLOCK_H
+#define HAVE_POSIX_CLOCK_H
+
+#include <time.h>
+
+/**
+ * Tries to open a posix clock device using the passed device path
+ * @param devpath The clock device path.
+ */
+clockid_t posix_clock_open(const char *device_path);
+
+/**
+ * Closes a posix clock. It checks that the clock id passed is a valid clock fd
+ * @param clkid The clock identifier.
+ */
+void posix_clock_close(clockid_t clkid);
+
+#endif
-- 
2.17.1



_______________________________________________
Linuxptp-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to