From: Takashi Sakamoto Date: Tue, 9 Jun 2020 02:20:50 +0000 (+0900) Subject: timer: add global method to retrieve current source of timestamp X-Git-Tag: v0.1.0~104 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=f9b96d1ffceebb24d7b52e5ee72bc055932e0eb7;p=alsa-gobject.git timer: add global method to retrieve current source of timestamp --- diff --git a/src/timer/alsatimer.map b/src/timer/alsatimer.map index 04a9832..661b75e 100644 --- a/src/timer/alsatimer.map +++ b/src/timer/alsatimer.map @@ -13,6 +13,7 @@ ALSA_GOBJECT_0_0_0 { "alsatimer_get_device_id_list"; "alsatimer_get_device_info"; "alsatimer_get_device_status"; + "alsatimer_get_tstamp_source"; "alsatimer_device_id_get_type"; "alsatimer_device_id_new"; diff --git a/src/timer/query.c b/src/timer/query.c index baa6bec..4a64187 100644 --- a/src/timer/query.c +++ b/src/timer/query.c @@ -2,6 +2,7 @@ #include "query.h" #include "privates.h" +#include #include #include #include @@ -12,10 +13,12 @@ #include #include +#include #include #define TIMER_SYSNAME_TEMPLATE "timer" +#define SYSFS_SND_TIMER_NODE "/sys/module/snd_timer/" /** * SECTION: query @@ -278,3 +281,94 @@ void alsatimer_set_device_params(ALSATimerDeviceId *device_id, close(fd); } + +static void timer_get_node_param_value(const char *param_name, char *buf, + gsize size, int *val, GError **error) +{ + char literal[64]; + int fd; + ssize_t len; + long v; + char *term; + + snprintf(literal, sizeof(literal), SYSFS_SND_TIMER_NODE "parameters/%s", + param_name); + + fd = open(literal, O_RDONLY); + if (fd < 0) { + generate_error(error, errno); + return; + } + + len = read(fd, buf, size); + if (len < 0) { + generate_error(error, errno); + goto end; + } + + v = strtol(buf, &term, 10); + if (errno > 0) + generate_error(error, errno); + else if (*term != '\n') + generate_error(error, EIO); + else + *val = (int)v; +end: + close(fd); +} + +/** + * alsatimer_get_tstamp_source: + * @clock_id: (out): The clock source for timestamp. The value of CLOCK_XXX in + * UAPI of Linux kernel. + * @error: A #GError. + * + * Get the clock source for timestamp when #ALSATimerUserInstance is configured + * to receive event with timestamp. The source is selected according to + * parameter of 'snd-timer' kernel module, and the call of function is just to + * refer to it. + * + * 0 means CLOCK_REALTIME is used. 1 means CLOCK_MONOTONIC is used. + */ +void alsatimer_get_tstamp_source(int *clock_id, GError **error) +{ + int val; + gsize size; + char *buf; + + // Count required digits. + val = INT_MAX; + size = 0; + while (val > 0) { + val /= 10; + ++size; + } + + // For codes of sign and new line. + size += 2; + + buf = g_try_malloc0(size); + if (buf == NULL) { + generate_error(error, ENOMEM); + return; + } + + timer_get_node_param_value("timer_tstamp_monotonic", buf, size, &val, + error); + if (*error != NULL) + goto end; + + switch (val) { + case 0: + *clock_id = CLOCK_REALTIME; + break; + case 1: + *clock_id = CLOCK_MONOTONIC; + break; + default: + generate_error(error, EPROTO); + break; + } +end: + g_free(buf); +} diff --git a/src/timer/query.h b/src/timer/query.h index 4707be6..6c33645 100644 --- a/src/timer/query.h +++ b/src/timer/query.h @@ -32,6 +32,8 @@ void alsatimer_set_device_params(ALSATimerDeviceId *device_id, const ALSATimerDeviceParams *device_params, GError **error); +void alsatimer_get_tstamp_source(int *clock_id, GError **error); + G_END_DECLS #endif