aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>2023-06-25 22:04:35 +0900
committer坂本 貴史 <o-takashi@sakamocchi.jp>2023-06-26 07:34:07 +0900
commit800c134496751eb675990238c3fc2ce2b40f3436 (patch)
tree89bb9f320e06789e0be89b0d175e4ceb1bfc6486
parentd1c484c9b3a12d336711fa0e46716ca038f89783 (diff)
downloadlibhinawa-800c134496751eb675990238c3fc2ce2b40f3436.tar.gz
cycle_time: add boxed object to express cycle time
It is helpful for application to read current value of CYCLE_TIME register in 1394 OHCI controller. Linux FireWire subsystem allows user space application to read it as well as system time according to clock id. The read data is aggregated. This commit adds boxed object to express the aggregated data. Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
-rw-r--r--src/cycle_time.c125
-rw-r--r--src/cycle_time.h27
-rw-r--r--src/hinawa.h1
-rw-r--r--src/hinawa.map10
-rw-r--r--src/meson.build2
-rw-r--r--tests/cycle-time22
-rw-r--r--tests/meson.build1
7 files changed, 188 insertions, 0 deletions
diff --git a/src/cycle_time.c b/src/cycle_time.c
new file mode 100644
index 0000000..e0e0f08
--- /dev/null
+++ b/src/cycle_time.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+#include "internal.h"
+
+/**
+ * HinawaCycleTime:
+ * A boxed object to express data of cycle time.
+ *
+ * A [struct@CycleTime] expresses the value of cycle time of 1394 OHCI as well as Linux system
+ * time referring to clock_id.
+ */
+HinawaCycleTime *hinawa_cycle_time_copy(const HinawaCycleTime *self)
+{
+#ifdef g_memdup2
+ return g_memdup2(self, sizeof(*self));
+#else
+ // GLib v2.68 deprecated g_memdup() with concern about overflow by narrow conversion from
+ // size_t to unsigned int in ABI for LP64 data model.
+ gpointer ptr = g_malloc(sizeof(*self));
+ memcpy(ptr, self, sizeof(*self));
+ return ptr;
+#endif
+}
+
+G_DEFINE_BOXED_TYPE(HinawaCycleTime, hinawa_cycle_time, hinawa_cycle_time_copy, g_free)
+
+/**
+ * hinawa_cycle_time_new:
+ *
+ * Allocate and return an instance of [struct@CycleTime].
+ *
+ * Returns: (transfer full): An instance of [struct@CycleTime].
+ *
+ * Since: 2.6.
+ */
+HinawaCycleTime *hinawa_cycle_time_new()
+{
+ return g_malloc0(sizeof(HinawaCycleTime));
+}
+
+/**
+ * hinawa_cycle_time_get_system_time:
+ * @self: A [struct@CycleTime].
+ * @tv_sec: (out caller-allocates): The second part of timestamp.
+ * @tv_nsec: (out caller-allocates): The nanosecond part of timestamp.
+ *
+ * Get system time with enough size of strorage. The timestamp refers to clock_id available by
+ * [method@CycleTime.get_clock_id].
+ *
+ * Since: 2.6.
+ */
+void hinawa_cycle_time_get_system_time(HinawaCycleTime *self, gint64 *tv_sec, gint32 *tv_nsec)
+{
+ *tv_sec = self->tv_sec;
+ *tv_nsec = self->tv_nsec;
+}
+
+/**
+ * hinawa_cycle_time_get_clock_id:
+ * @self: A [struct@CycleTime].
+ * @clock_id: (out caller-allocates): The numeric ID of clock source for the reference timestamp.
+ * One of CLOCK_REALTIME(0), CLOCK_MONOTONIC(1), and CLOCK_MONOTONIC_RAW(4) is available
+ * UAPI of Linux kernel.
+ *
+ * Get the ID of clock for timestamp.
+ *
+ * Since: 2.6.
+ */
+void hinawa_cycle_time_get_clock_id(HinawaCycleTime *self, gint *clock_id)
+{
+ *clock_id = self->clk_id;
+}
+
+#define IEEE1394_CYCLE_TIME_SEC_MASK 0xfe000000
+#define IEEE1394_CYCLE_TIME_SEC_SHIFT 25
+#define IEEE1394_CYCLE_TIME_CYCLE_MASK 0x01fff000
+#define IEEE1394_CYCLE_TIME_CYCLE_SHIFT 12
+#define IEEE1394_CYCLE_TIME_OFFSET_MASK 0x00000fff
+
+static guint ieee1394_cycle_time_to_sec(guint32 cycle_time)
+{
+ return (cycle_time & IEEE1394_CYCLE_TIME_SEC_MASK) >> IEEE1394_CYCLE_TIME_SEC_SHIFT;
+}
+
+static guint ieee1394_cycle_time_to_cycle(guint32 cycle_time)
+{
+ return (cycle_time & IEEE1394_CYCLE_TIME_CYCLE_MASK) >> IEEE1394_CYCLE_TIME_CYCLE_SHIFT;
+}
+
+static guint ieee1394_cycle_time_to_offset(guint32 cycle_time)
+{
+ return cycle_time & IEEE1394_CYCLE_TIME_OFFSET_MASK;
+}
+
+/**
+ * hinawa_cycle_time_get_fields:
+ * @self: A [struct@CycleTime].
+ * @cycle_time: (array fixed-size=3) (out caller-allocates): The value of cycle time register of
+ * 1394 OHCI, including three elements; second, cycle, and offset.
+ *
+ * Get the value of cycle time in 1394 OHCI controller. The first element of array expresses the
+ * value of sec field, up to 127. The second element of array expresses the value of cycle field,
+ * up to 7999. The third element of array expresses the value of offset field, up to 3071.
+ *
+ * Since: 2.6.
+ */
+void hinawa_cycle_time_get_fields(HinawaCycleTime *self, guint16 cycle_time[3])
+{
+ cycle_time[0] = ieee1394_cycle_time_to_sec(self->cycle_timer);
+ cycle_time[1] = ieee1394_cycle_time_to_cycle(self->cycle_timer);
+ cycle_time[2] = ieee1394_cycle_time_to_offset(self->cycle_timer);
+}
+
+/**
+ * hinawa_cycle_time_get_raw:
+ * @self: A [struct@CycleTime].
+ * @raw: (out caller-allocates): The raw value for CYCLE_TIME register.
+ *
+ * Get the value of cycle time in 1394 OHCI controller.
+ *
+ * Since: 2.6.
+ */
+void hinawa_cycle_time_get_raw(HinawaCycleTime *self, guint32 *raw)
+{
+ *raw = self->cycle_timer;
+}
diff --git a/src/cycle_time.h b/src/cycle_time.h
new file mode 100644
index 0000000..89862ba
--- /dev/null
+++ b/src/cycle_time.h
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+#ifndef __HINAWA_CYCLE_TIME_H__
+#define __HINAWA_CYCLE_TIME_H__
+
+#include <hinawa.h>
+
+G_BEGIN_DECLS
+
+#define HINAWA_TYPE_CYCLE_TIME (hinawa_cycle_time_get_type())
+
+typedef struct fw_cdev_get_cycle_timer2 HinawaCycleTime;
+
+GType hinawa_cycle_time_get_type() G_GNUC_CONST;
+
+HinawaCycleTime *hinawa_cycle_time_new();
+
+void hinawa_cycle_time_get_system_time(HinawaCycleTime *self, gint64 *tv_sec, gint32 *tv_nsec);
+
+void hinawa_cycle_time_get_clock_id(HinawaCycleTime *self, gint *clock_id);
+
+void hinawa_cycle_time_get_fields(HinawaCycleTime *self, guint16 cycle_time[3]);
+
+void hinawa_cycle_time_get_raw(HinawaCycleTime *self, guint32 *raw);
+
+G_END_DECLS
+
+#endif
diff --git a/src/hinawa.h b/src/hinawa.h
index b690f62..c77e151 100644
--- a/src/hinawa.h
+++ b/src/hinawa.h
@@ -14,6 +14,7 @@
#include <hinawa_enum_types.h>
#include <hinawa_enums.h>
+#include <cycle_time.h>
#include <snd_motu_register_dsp_parameter.h>
#include <fw_node.h>
diff --git a/src/hinawa.map b/src/hinawa.map
index 34fa3c9..be6ddcd 100644
--- a/src/hinawa.map
+++ b/src/hinawa.map
@@ -158,3 +158,13 @@ HINAWA_2_5_0 {
global:
"hinawa_fw_req_error_get_type";
} HINAWA_2_3_0;
+
+HINAWA_2_6_0 {
+ global:
+ "hinawa_cycle_time_get_type";
+ "hinawa_cycle_time_new";
+ "hinawa_cycle_time_get_system_time";
+ "hinawa_cycle_time_get_clock_id";
+ "hinawa_cycle_time_get_fields";
+ "hinawa_cycle_time_get_raw";
+} HINAWA_2_5_0;
diff --git a/src/meson.build b/src/meson.build
index 5e0b763..726052b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -11,6 +11,7 @@ sources = [
'fw_resp.c',
'fw_req.c',
'fw_fcp.c',
+ 'cycle_time.c',
'snd_unit.c',
'snd_dice.c',
'snd_efw.c',
@@ -26,6 +27,7 @@ headers = [
'fw_resp.h',
'fw_req.h',
'fw_fcp.h',
+ 'cycle_time.h',
'snd_unit.h',
'snd_dice.h',
'snd_efw.h',
diff --git a/tests/cycle-time b/tests/cycle-time
new file mode 100644
index 0000000..f01d4f6
--- /dev/null
+++ b/tests/cycle-time
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+from sys import exit
+from errno import ENXIO
+
+from helper import test_struct
+
+import gi
+gi.require_version('Hinawa', '3.0')
+from gi.repository import Hinawa
+
+target_type = Hinawa.CycleTime
+methods = (
+ 'new',
+ 'get_system_time',
+ 'get_clock_id',
+ 'get_fields',
+ 'get_raw',
+)
+
+if not test_struct(target_type, methods):
+ exit(ENXIO)
diff --git a/tests/meson.build b/tests/meson.build
index 8652c84..0040efd 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -3,6 +3,7 @@ tests = [
'fw-req',
'fw-resp',
'fw-fcp',
+ 'cycle-time',
'snd-unit',
'snd-dice',
'snd-efw',