1 // SPDX-License-Identifier: GPL-2.0
3 * This tool is used by the utimer test, and it allows us to
4 * count the ticks of a global timer in a certain time frame
5 * (which is set by `timeout` parameter).
7 * Author: Ivan Orlov <ivan.orlov0322@gmail.com>
11 #include <alsa/asoundlib.h>
15 static void async_callback(snd_async_handler_t
*ahandler
)
20 static char timer_name
[64];
21 static void bind_to_timer(int device
, int subdevice
, int timeout
)
24 snd_timer_params_t
*params
;
25 snd_async_handler_t
*ahandler
;
29 sprintf(timer_name
, "hw:CLASS=%d,SCLASS=%d,DEV=%d,SUBDEV=%d",
30 SND_TIMER_CLASS_GLOBAL
, SND_TIMER_SCLASS_NONE
,
33 snd_timer_params_alloca(¶ms
);
35 if (snd_timer_open(&handle
, timer_name
, SND_TIMER_OPEN_NONBLOCK
) < 0) {
36 perror("Can't open the timer");
40 snd_timer_params_set_auto_start(params
, 1);
41 snd_timer_params_set_ticks(params
, 1);
42 if (snd_timer_params(handle
, params
) < 0) {
43 perror("Can't set timer params");
47 if (snd_async_add_timer_handler(&ahandler
, handle
, async_callback
, NULL
) < 0) {
48 perror("Can't create a handler");
51 end
= time(NULL
) + timeout
;
52 if (snd_timer_start(handle
) < 0) {
53 perror("Failed to start the timer");
56 printf("Timer has started\n");
57 while (time(NULL
) <= end
) {
59 * Waiting for the timeout to elapse. Can't use sleep here, as it gets
60 * constantly interrupted by the signal from the timer (SIGIO)
63 snd_timer_stop(handle
);
64 snd_timer_close(handle
);
67 int main(int argc
, char *argv
[])
69 int device
, subdevice
, timeout
;
72 perror("Usage: %s <device> <subdevice> <timeout>");
78 device
= atoi(argv
[1]);
79 subdevice
= atoi(argv
[2]);
80 timeout
= atoi(argv
[3]);
82 bind_to_timer(device
, subdevice
, timeout
);
84 printf("Total ticks count: %d\n", ticked
);