Path: blob/master/tools/testing/selftests/alsa/global-timer.c
26285 views
// SPDX-License-Identifier: GPL-2.01/*2* This tool is used by the utimer test, and it allows us to3* count the ticks of a global timer in a certain time frame4* (which is set by `timeout` parameter).5*6* Author: Ivan Orlov <[email protected]>7*/8#include <stdio.h>9#include <stdlib.h>10#include <alsa/asoundlib.h>11#include <time.h>1213static int ticked;14static void async_callback(snd_async_handler_t *ahandler)15{16ticked++;17}1819static char timer_name[64];20static void bind_to_timer(int device, int subdevice, int timeout)21{22snd_timer_t *handle;23snd_timer_params_t *params;24snd_async_handler_t *ahandler;2526time_t end;2728sprintf(timer_name, "hw:CLASS=%d,SCLASS=%d,DEV=%d,SUBDEV=%d",29SND_TIMER_CLASS_GLOBAL, SND_TIMER_SCLASS_NONE,30device, subdevice);3132snd_timer_params_alloca(¶ms);3334if (snd_timer_open(&handle, timer_name, SND_TIMER_OPEN_NONBLOCK) < 0) {35perror("Can't open the timer");36exit(EXIT_FAILURE);37}3839snd_timer_params_set_auto_start(params, 1);40snd_timer_params_set_ticks(params, 1);41if (snd_timer_params(handle, params) < 0) {42perror("Can't set timer params");43exit(EXIT_FAILURE);44}4546if (snd_async_add_timer_handler(&ahandler, handle, async_callback, NULL) < 0) {47perror("Can't create a handler");48exit(EXIT_FAILURE);49}50end = time(NULL) + timeout;51if (snd_timer_start(handle) < 0) {52perror("Failed to start the timer");53exit(EXIT_FAILURE);54}55printf("Timer has started\n");56while (time(NULL) <= end) {57/*58* Waiting for the timeout to elapse. Can't use sleep here, as it gets59* constantly interrupted by the signal from the timer (SIGIO)60*/61}62snd_timer_stop(handle);63snd_timer_close(handle);64}6566int main(int argc, char *argv[])67{68int device, subdevice, timeout;6970if (argc < 4) {71perror("Usage: %s <device> <subdevice> <timeout>");72return EXIT_FAILURE;73}7475setlinebuf(stdout);7677device = atoi(argv[1]);78subdevice = atoi(argv[2]);79timeout = atoi(argv[3]);8081bind_to_timer(device, subdevice, timeout);8283printf("Total ticks count: %d\n", ticked);8485return EXIT_SUCCESS;86}878889