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