1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/time.h>
5#include "../include/asoundlib.h"
6
7void show_status(void *handle)
8{
9	int err;
10	snd_timer_status_t *status;
11
12	snd_timer_status_alloca(&status);
13	if ((err = snd_timer_status(handle, status)) < 0) {
14		fprintf(stderr, "timer status %i (%s)\n", err, snd_strerror(err));
15		return;
16	}
17	printf("STATUS:\n");
18	printf("  resolution = %li\n", snd_timer_status_get_resolution(status));
19	printf("  lost = %li\n", snd_timer_status_get_lost(status));
20	printf("  overrun = %li\n", snd_timer_status_get_overrun(status));
21	printf("  queue = %li\n", snd_timer_status_get_queue(status));
22}
23
24void read_loop(void *handle, int master_ticks, int timeout)
25{
26	int count, err;
27	struct pollfd *fds;
28	snd_timer_read_t tr;
29
30	count = snd_timer_poll_descriptors_count(handle);
31	fds = calloc(count, sizeof(struct pollfd));
32	if (fds == NULL) {
33		fprintf(stderr, "malloc error\n");
34		exit(EXIT_FAILURE);
35	}
36	while (master_ticks-- > 0) {
37		if ((err = snd_timer_poll_descriptors(handle, fds, count)) < 0) {
38			fprintf(stderr, "snd_timer_poll_descriptors error: %s\n", snd_strerror(err));
39			exit(EXIT_FAILURE);
40		}
41		if ((err = poll(fds, count, timeout)) < 0) {
42			fprintf(stderr, "poll error %i (%s)\n", err, strerror(err));
43			exit(EXIT_FAILURE);
44		}
45		if (err == 0) {
46			fprintf(stderr, "timer time out!!\n");
47			exit(EXIT_FAILURE);
48		}
49		while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
50			printf("TIMER: resolution = %uns, ticks = %u\n",
51				tr.resolution, tr.ticks);
52		}
53	}
54	free(fds);
55}
56
57static void async_callback(snd_async_handler_t *ahandler)
58{
59	snd_timer_t *handle = snd_async_handler_get_timer(ahandler);
60	int *acount = snd_async_handler_get_callback_private(ahandler);
61	snd_timer_read_t tr;
62
63	while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
64		printf("TIMER: resolution = %uns, ticks = %u\n",
65			tr.resolution, tr.ticks);
66	}
67	(*acount)++;
68}
69
70int main(int argc, char *argv[])
71{
72	int idx, err;
73	int class = SND_TIMER_CLASS_GLOBAL;
74	int sclass = SND_TIMER_CLASS_NONE;
75	int card = 0;
76	int device = SND_TIMER_GLOBAL_SYSTEM;
77	int subdevice = 0;
78	int list = 0;
79	int async = 0;
80	int acount = 0;
81	snd_timer_t *handle;
82	snd_timer_id_t *id;
83	snd_timer_info_t *info;
84	snd_timer_params_t *params;
85	char timername[64];
86	snd_async_handler_t *ahandler;
87
88	snd_timer_id_alloca(&id);
89	snd_timer_info_alloca(&info);
90	snd_timer_params_alloca(&params);
91
92	idx = 1;
93	while (idx < argc) {
94		if (!strncmp(argv[idx], "class=", 5)) {
95			class = atoi(argv[idx]+6);
96		} else if (!strncmp(argv[idx], "sclass=", 6)) {
97			sclass = atoi(argv[idx]+7);
98		} else if (!strncmp(argv[idx], "card=", 5)) {
99			card = atoi(argv[idx]+5);
100		} else if (!strncmp(argv[idx], "device=", 7)) {
101			device = atoi(argv[idx]+7);
102		} else if (!strncmp(argv[idx], "subdevice=", 10)) {
103			subdevice = atoi(argv[idx]+10);
104		} else if (!strcmp(argv[idx], "list")) {
105			list = 1;
106		} else if (!strcmp(argv[idx], "async")) {
107			async = 1;
108		}
109		idx++;
110	}
111	if (class == SND_TIMER_CLASS_SLAVE && sclass == SND_TIMER_SCLASS_NONE) {
112		fprintf(stderr, "slave class is not set\n");
113		exit(EXIT_FAILURE);
114	}
115	if (list) {
116		snd_timer_query_t *qhandle;
117		if ((err = snd_timer_query_open(&qhandle, "hw", 0)) < 0) {
118			fprintf(stderr, "snd_timer_query_open error: %s\n", snd_strerror(err));
119			exit(EXIT_FAILURE);
120		}
121		snd_timer_id_set_class(id, SND_TIMER_CLASS_NONE);
122		while (1) {
123			if ((err = snd_timer_query_next_device(qhandle, id)) < 0) {
124				fprintf(stderr, "timer next device error: %s\n", snd_strerror(err));
125				break;
126			}
127			if (snd_timer_id_get_class(id) < 0)
128				break;
129			printf("Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
130					snd_timer_id_get_class(id),
131					snd_timer_id_get_sclass(id),
132					snd_timer_id_get_card(id),
133					snd_timer_id_get_device(id),
134					snd_timer_id_get_subdevice(id));
135		}
136		snd_timer_query_close(qhandle);
137		exit(EXIT_SUCCESS);
138	}
139	sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", class, sclass, card, device, subdevice);
140	if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
141		fprintf(stderr, "timer open %i (%s)\n", err, snd_strerror(err));
142		exit(EXIT_FAILURE);
143	}
144	printf("Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n", class, sclass, card, device, subdevice);
145	if ((err = snd_timer_info(handle, info)) < 0) {
146		fprintf(stderr, "timer info %i (%s)\n", err, snd_strerror(err));
147		exit(0);
148	}
149	printf("Timer info:\n");
150	printf("  slave = %s\n", snd_timer_info_is_slave(info) ? "yes" : "no");
151	printf("  card = %i\n", snd_timer_info_get_card(info));
152	printf("  id = '%s'\n", snd_timer_info_get_id(info));
153	printf("  name = '%s'\n", snd_timer_info_get_name(info));
154	printf("  average resolution = %li\n", snd_timer_info_get_resolution(info));
155	snd_timer_params_set_auto_start(params, 1);
156	if (!snd_timer_info_is_slave(info)) {
157		snd_timer_params_set_ticks(params, (1000000000 / snd_timer_info_get_resolution(info)) / 50); /* 50Hz */
158		if (snd_timer_params_get_ticks(params) < 1)
159			snd_timer_params_set_ticks(params, 1);
160		printf("Using %li tick(s)\n", snd_timer_params_get_ticks(params));
161	} else {
162		snd_timer_params_set_ticks(params, 1);
163	}
164	if ((err = snd_timer_params(handle, params)) < 0) {
165		fprintf(stderr, "timer params %i (%s)\n", err, snd_strerror(err));
166		exit(0);
167	}
168	show_status(handle);
169	if (async) {
170		err = snd_async_add_timer_handler(&ahandler, handle, async_callback, &acount);
171		if (err < 0) {
172			fprintf(stderr, "unable to add async handler %i (%s)\n", err, snd_strerror(err));
173			exit(EXIT_FAILURE);
174		}
175	}
176	if ((err = snd_timer_start(handle)) < 0) {
177		fprintf(stderr, "timer start %i (%s)\n", err, snd_strerror(err));
178		exit(EXIT_FAILURE);
179	}
180	if (async) {
181		/* because all other work is done in the signal handler,
182		   suspend the process */
183		while (acount < 25)
184			sleep(1);
185		snd_timer_stop(handle);
186	} else {
187		read_loop(handle, 25, snd_timer_info_is_slave(info) ? 10000 : 25);
188	}
189	show_status(handle);
190	snd_timer_close(handle);
191	printf("Done\n");
192	return EXIT_SUCCESS;
193}
194