1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2024 The FreeBSD Foundation
5 *
6 * This software was developed by Christos Margiolis <[email protected]>
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/sndstat.h>
32 #include <sys/nv.h>
33
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 /*
41 * Example program showcasing how to use sndstat(4)'s nvlist interface, and how
42 * to fetch all currently supported fields, with the appropriate error checks.
43 *
44 * For more detailed information on what each nvlist field represents, please
45 * read sndstat(4)'s man page.
46 */
47
48 int
main(int argc,char * argv[])49 main(int argc, char *argv[])
50 {
51 nvlist_t *nvl;
52 const nvlist_t * const *di;
53 const nvlist_t * const *cdi;
54 struct sndstioc_nv_arg arg;
55 size_t nitems, nchans, i, j;
56 int fd, pchan, rchan;
57
58 if ((fd = open("/dev/sndstat", O_RDONLY)) < 0)
59 err(1, "open(/dev/sndstat)");
60
61 if (ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL) < 0)
62 err(1, "ioctl(SNDSTIOC_REFRESH_DEVS)");
63
64 arg.nbytes = 0;
65 arg.buf = NULL;
66 if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0)
67 err(1, "ioctl(SNDSTIOC_GET_DEVS#1)");
68
69 if ((arg.buf = malloc(arg.nbytes)) == NULL)
70 err(1, "malloc");
71
72 if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0)
73 err(1, "ioctl(SNDSTIOC_GET_DEVS#2)");
74
75 if ((nvl = nvlist_unpack(arg.buf, arg.nbytes, 0)) == NULL)
76 err(1, "nvlist_unpack");
77
78 if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS))
79 errx(1, "no soundcards attached");
80
81 di = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems);
82 for (i = 0; i < nitems; i++) {
83 #define NV(type, item) \
84 nvlist_get_ ## type (di[i], SNDST_DSPS_ ## item)
85 printf("nameunit=%s\n", NV(string, NAMEUNIT));
86 printf("\tfrom_user=%d\n", NV(bool, FROM_USER));
87 printf("\tdevnode=%s\n", NV(string, DEVNODE));
88 printf("\tdesc=%s\n", NV(string, DESC));
89 printf("\tprovider=%s\n", NV(string, PROVIDER));
90 printf("\tpchan=%d\n", (int)NV(number, PCHAN));
91 printf("\trchan=%d\n", (int)NV(number, RCHAN));
92 pchan = NV(number, PCHAN);
93 rchan = NV(number, RCHAN);
94 #undef NV
95
96 if (pchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_PLAY))
97 errx(1, "playback channel list empty");
98 if (rchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_REC))
99 errx(1, "recording channel list empty");
100
101 #define NV(type, mode, item) \
102 nvlist_get_ ## type (nvlist_get_nvlist(di[i], \
103 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item)
104 if (pchan) {
105 printf("\tplay_min_rate=%d\n",
106 (int)NV(number, PLAY, MIN_RATE));
107 printf("\tplay_max_rate=%d\n",
108 (int)NV(number, PLAY, MAX_RATE));
109 printf("\tplay_formats=%#08x\n",
110 (int)NV(number, PLAY, FORMATS));
111 printf("\tplay_min_chn=%d\n",
112 (int)NV(number, PLAY, MIN_CHN));
113 printf("\tplay_max_chn=%d\n",
114 (int)NV(number, PLAY, MAX_CHN));
115 }
116 if (rchan) {
117 printf("\trec_min_rate=%d\n",
118 (int)NV(number, REC, MIN_RATE));
119 printf("\trec_max_rate=%d\n",
120 (int)NV(number, REC, MAX_RATE));
121 printf("\trec_formats=%#08x\n",
122 (int)NV(number, REC, FORMATS));
123 printf("\trec_min_chn=%d\n",
124 (int)NV(number, REC, MIN_CHN));
125 printf("\trec_max_chn=%d\n",
126 (int)NV(number, REC, MAX_CHN));
127 }
128 #undef NV
129
130 if (!nvlist_exists(di[i], SNDST_DSPS_PROVIDER_INFO))
131 continue;
132
133 #define NV(type, item) \
134 nvlist_get_ ## type (nvlist_get_nvlist(di[i], \
135 SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_ ## item)
136 printf("\tunit=%d\n", (int)NV(number, UNIT));
137 printf("\tstatus=%s\n", NV(string, STATUS));
138 printf("\tbitperfect=%d\n", NV(bool, BITPERFECT));
139 printf("\tpvchan=%d\n", (int)NV(number, PVCHAN));
140 printf("\tpvchanrate=%d\n", (int)NV(number, PVCHANRATE));
141 printf("\tpvchanformat=%#08x\n", (int)NV(number, PVCHANFORMAT));
142 printf("\trvchan=%d\n", (int)NV(number, RVCHAN));
143 printf("\trvchanrate=%d\n", (int)NV(number, RVCHANRATE));
144 printf("\trvchanformat=%#08x\n", (int)NV(number, RVCHANFORMAT));
145 #undef NV
146
147 if (!nvlist_exists(nvlist_get_nvlist(di[i],
148 SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_CHAN_INFO))
149 errx(1, "channel info list empty");
150
151 cdi = nvlist_get_nvlist_array(
152 nvlist_get_nvlist(di[i], SNDST_DSPS_PROVIDER_INFO),
153 SNDST_DSPS_SOUND4_CHAN_INFO, &nchans);
154 for (j = 0; j < nchans; j++) {
155 #define NV(type, item) \
156 nvlist_get_ ## type (cdi[j], SNDST_DSPS_SOUND4_CHAN_ ## item)
157 printf("\tchan=%s\n", NV(string, NAME));
158 printf("\t\tparentchan=%s\n", NV(string, PARENTCHAN));
159 printf("\t\tunit=%d\n", (int)NV(number, UNIT));
160 printf("\t\tcaps=%#08x\n", (int)NV(number, CAPS));
161 printf("\t\tlatency=%d\n", (int)NV(number, LATENCY));
162 printf("\t\trate=%d\n", (int)NV(number, RATE));
163 printf("\t\tformat=%#08x\n", (int)NV(number, FORMAT));
164 printf("\t\tpid=%d\n", (int)NV(number, PID));
165 printf("\t\tcomm=%s\n", NV(string, COMM));
166 printf("\t\tintr=%d\n", (int)NV(number, INTR));
167 printf("\t\txruns=%d\n", (int)NV(number, XRUNS));
168 printf("\t\tfeedcnt=%d\n", (int)NV(number, FEEDCNT));
169 printf("\t\tleftvol=%d\n", (int)NV(number, LEFTVOL));
170 printf("\t\trightvol=%d\n", (int)NV(number, RIGHTVOL));
171 printf("\t\thwbuf_format=%#08x\n",
172 (int)NV(number, HWBUF_FORMAT));
173 printf("\t\thwbuf_size=%d\n",
174 (int)NV(number, HWBUF_SIZE));
175 printf("\t\thwbuf_blksz=%d\n",
176 (int)NV(number, HWBUF_BLKSZ));
177 printf("\t\thwbuf_blkcnt=%d\n",
178 (int)NV(number, HWBUF_BLKCNT));
179 printf("\t\thwbuf_free=%d\n",
180 (int)NV(number, HWBUF_FREE));
181 printf("\t\thwbuf_ready=%d\n",
182 (int)NV(number, HWBUF_READY));
183 printf("\t\tswbuf_format=%#08x\n",
184 (int)NV(number, SWBUF_FORMAT));
185 printf("\t\tswbuf_size=%d\n",
186 (int)NV(number, SWBUF_SIZE));
187 printf("\t\tswbuf_blksz=%d\n",
188 (int)NV(number, SWBUF_BLKSZ));
189 printf("\t\tswbuf_blkcnt=%d\n",
190 (int)NV(number, SWBUF_BLKCNT));
191 printf("\t\tswbuf_free=%d\n",
192 (int)NV(number, SWBUF_FREE));
193 printf("\t\tswbuf_ready=%d\n",
194 (int)NV(number, SWBUF_READY));
195 printf("\t\tswbuf_feederchain=%s\n",
196 NV(string, FEEDERCHAIN));
197 #undef NV
198 }
199 }
200
201 free(arg.buf);
202 nvlist_destroy(nvl);
203 close(fd);
204
205 return (0);
206 }
207