From nobody Sat Jul 27 11:56:18 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WWNPb0XQMz5S6Kk; Sat, 27 Jul 2024 11:56:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WWNPZ5gKJz42c1; Sat, 27 Jul 2024 11:56:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722081378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AdtcOSGTr4S2cXKyf115XeL6NtO78T1oFyMQOZqskAk=; b=sWGiMx2/ah7ZRZgP/2PhFaFP4QaiE47AsXZ2oTATKl1bUhq3S6G9Dv1rcu0jRsbbb/N+nr UbIgKpOuLMcQP2Z+wABZeUJFH3y39dOpIPCx7Ffxd2RLela79DwEuHF/d0wbl+Dvl3fwij gz4Gym/lxJW7D8ZP5VPtTvOnwf1qQFXzbVUXT81kAZesbrF0V4NNZG0gicFMEShYJmvVbU qxhyoOspwv0sB1RlK1Jr9K/TYg2oXCm89xhH0zuO6yWmnn9binr21EPKMgivBQFmJxNxhR bsih0mr5aLKHVRH9fWbp6lL0n6CwjCS2ZQGJ6woQKQW0+R3TGunRX5+tBjZhzg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722081378; a=rsa-sha256; cv=none; b=BHF4w82ekLomqCAgFa6CupWFcYJljuA3DvqsciZ41gW2sZGOk8a40BVxr1+mxNQbnxD4SI pkGVItzBcGSmKhXwn6SwGzsL4I9o6T+6AJHmwtZd0X8ngp4O9/0m2kCmTqS/I56gZWoiXv 1BDMGm+mxd8zM7kh7dygwW4l0EcY6MlcYszo3V1C7yTY9mPV2OYlfawg/veWXaqDM7CBQw FASQWleTUKwl+0+w+dK2+GCh+3J30KP7ySSWNZYw2ztoN6MRSxy2ZLyk9nYLmhv0ZxoTiX JHXohQveMi0iDA8R+S6bZQiseM5QOuhkT0ZxTLNv6vrfh9Eoob8OSXXjqk5H2A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722081378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AdtcOSGTr4S2cXKyf115XeL6NtO78T1oFyMQOZqskAk=; b=t3+zXhA7YJ89tIES3N7gUNf1xFDah6rB66rxOImckXEIqH7KmN355j3p8fG2gxJhnd0pD9 fzDGLRsfifC6N8MkoHfX0UbEMmaeuEs/PyBpzshVCCdzwWzbtDc4bzFJRrcTvEZgxbnWmG SJiva6Fo6+hJFczJdhsVS/F9C5RFZZgdB2vVhHBEpHYJL5okG5lRpU3Hd2ujUn6lSl3iDa ITav1L+i6bXGgQOAXScIVx8372Ew6IpnXkDumUFP7IOnOirL23FwHLqN9zsDIJTZObRj6Y VhDd59AmR8ptKgmtALj5NSm4yvrX2vcZnpXKAgNlbOKQHp98136mmIcBoYN/xw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WWNPZ5Gp2z15vs; Sat, 27 Jul 2024 11:56:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46RBuIad039655; Sat, 27 Jul 2024 11:56:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46RBuIEv039652; Sat, 27 Jul 2024 11:56:18 GMT (envelope-from git) Date: Sat, 27 Jul 2024 11:56:18 GMT Message-Id: <202407271156.46RBuIEv039652@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: f08ecd24a19b - main - sound examples: Add sndstat nvlist example List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f08ecd24a19ba772701fc35caaad92b9fe91fa9c Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=f08ecd24a19ba772701fc35caaad92b9fe91fa9c commit f08ecd24a19ba772701fc35caaad92b9fe91fa9c Author: Christos Margiolis AuthorDate: 2024-07-27 11:54:40 +0000 Commit: Christos Margiolis CommitDate: 2024-07-27 11:54:40 +0000 sound examples: Add sndstat nvlist example Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch Differential Revision: https://reviews.freebsd.org/D45969 --- share/examples/Makefile | 3 +- share/examples/sound/sndstat_nv.c | 206 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 1 deletion(-) diff --git a/share/examples/Makefile b/share/examples/Makefile index 8839075452af..211ec2272c05 100644 --- a/share/examples/Makefile +++ b/share/examples/Makefile @@ -314,7 +314,8 @@ SE_SCSI_TARGET= \ scsi_cmds.c SE_DIRS+= sound -SE_SOUND= +SE_SOUND= \ + sndstat_nv.c SE_DIRS+= sound/oss SE_SOUND_OSS= \ diff --git a/share/examples/sound/sndstat_nv.c b/share/examples/sound/sndstat_nv.c new file mode 100644 index 000000000000..1056c1f4a08f --- /dev/null +++ b/share/examples/sound/sndstat_nv.c @@ -0,0 +1,206 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 The FreeBSD Foundation + * + * This software was developed by Christos Margiolis + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#include +#include +#include +#include +#include + +/* + * Example program showcasing how to use sndstat(4)'s nvlist interface, and how + * to fetch all currently supported fields, with the appropriate error checks. + * + * For more detailed information on what each nvlist field represents, please + * read sndstat(4)'s man page. + */ + +int +main(int argc, char *argv[]) +{ + nvlist_t *nvl; + const nvlist_t * const *di; + const nvlist_t * const *cdi; + struct sndstioc_nv_arg arg; + size_t nitems, nchans, i, j; + int fd, pchan, rchan; + + if ((fd = open("/dev/sndstat", O_RDONLY)) < 0) + err(1, "open(/dev/sndstat)"); + + if (ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL) < 0) + err(1, "ioctl(SNDSTIOC_REFRESH_DEVS)"); + + arg.nbytes = 0; + arg.buf = NULL; + if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0) + err(1, "ioctl(SNDSTIOC_GET_DEVS#1)"); + + if ((arg.buf = malloc(arg.nbytes)) == NULL) + err(1, "malloc"); + + if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0) + err(1, "ioctl(SNDSTIOC_GET_DEVS#2)"); + + if ((nvl = nvlist_unpack(arg.buf, arg.nbytes, 0)) == NULL) + err(1, "nvlist_unpack"); + + if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS)) + errx(1, "no soundcards attached"); + + di = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems); + for (i = 0; i < nitems; i++) { +#define NV(type, item) \ + nvlist_get_ ## type (di[i], SNDST_DSPS_ ## item) + printf("nameunit=%s\n", NV(string, NAMEUNIT)); + printf("\tfrom_user=%d\n", NV(bool, FROM_USER)); + printf("\tdevnode=%s\n", NV(string, DEVNODE)); + printf("\tdesc=%s\n", NV(string, DESC)); + printf("\tprovider=%s\n", NV(string, PROVIDER)); + printf("\tpchan=%d\n", (int)NV(number, PCHAN)); + printf("\trchan=%d\n", (int)NV(number, RCHAN)); + pchan = NV(number, PCHAN); + rchan = NV(number, RCHAN); +#undef NV + + if (pchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_PLAY)) + errx(1, "playback channel list empty"); + if (rchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_REC)) + errx(1, "recording channel list empty"); + +#define NV(type, mode, item) \ + nvlist_get_ ## type (nvlist_get_nvlist(di[i], \ + SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item) + if (pchan) { + printf("\tplay_min_rate=%d\n", + (int)NV(number, PLAY, MIN_RATE)); + printf("\tplay_max_rate=%d\n", + (int)NV(number, PLAY, MAX_RATE)); + printf("\tplay_formats=%#08x\n", + (int)NV(number, PLAY, FORMATS)); + printf("\tplay_min_chn=%d\n", + (int)NV(number, PLAY, MIN_CHN)); + printf("\tplay_max_chn=%d\n", + (int)NV(number, PLAY, MAX_CHN)); + } + if (rchan) { + printf("\trec_min_rate=%d\n", + (int)NV(number, REC, MIN_RATE)); + printf("\trec_max_rate=%d\n", + (int)NV(number, REC, MAX_RATE)); + printf("\trec_formats=%#08x\n", + (int)NV(number, REC, FORMATS)); + printf("\trec_min_chn=%d\n", + (int)NV(number, REC, MIN_CHN)); + printf("\trec_max_chn=%d\n", + (int)NV(number, REC, MAX_CHN)); + } +#undef NV + + if (!nvlist_exists(di[i], SNDST_DSPS_PROVIDER_INFO)) + continue; + +#define NV(type, item) \ + nvlist_get_ ## type (nvlist_get_nvlist(di[i], \ + SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_ ## item) + printf("\tunit=%d\n", (int)NV(number, UNIT)); + printf("\tstatus=%s\n", NV(string, STATUS)); + printf("\tbitperfect=%d\n", NV(bool, BITPERFECT)); + printf("\tpvchan=%d\n", (int)NV(number, PVCHAN)); + printf("\tpvchanrate=%d\n", (int)NV(number, PVCHANRATE)); + printf("\tpvchanformat=%#08x\n", (int)NV(number, PVCHANFORMAT)); + printf("\trvchan=%d\n", (int)NV(number, RVCHAN)); + printf("\trvchanrate=%d\n", (int)NV(number, RVCHANRATE)); + printf("\trvchanformat=%#08x\n", (int)NV(number, RVCHANFORMAT)); +#undef NV + + if (!nvlist_exists(nvlist_get_nvlist(di[i], + SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_CHAN_INFO)) + errx(1, "channel info list empty"); + + cdi = nvlist_get_nvlist_array( + nvlist_get_nvlist(di[i], SNDST_DSPS_PROVIDER_INFO), + SNDST_DSPS_SOUND4_CHAN_INFO, &nchans); + for (j = 0; j < nchans; j++) { +#define NV(type, item) \ + nvlist_get_ ## type (cdi[j], SNDST_DSPS_SOUND4_CHAN_ ## item) + printf("\tchan=%s\n", NV(string, NAME)); + printf("\t\tparentchan=%s\n", NV(string, PARENTCHAN)); + printf("\t\tunit=%d\n", (int)NV(number, UNIT)); + printf("\t\tcaps=%#08x\n", (int)NV(number, CAPS)); + printf("\t\tlatency=%d\n", (int)NV(number, LATENCY)); + printf("\t\trate=%d\n", (int)NV(number, RATE)); + printf("\t\tformat=%#08x\n", (int)NV(number, FORMAT)); + printf("\t\tpid=%d\n", (int)NV(number, PID)); + printf("\t\tcomm=%s\n", NV(string, COMM)); + printf("\t\tintr=%d\n", (int)NV(number, INTR)); + printf("\t\txruns=%d\n", (int)NV(number, XRUNS)); + printf("\t\tfeedcnt=%d\n", (int)NV(number, FEEDCNT)); + printf("\t\tleftvol=%d\n", (int)NV(number, LEFTVOL)); + printf("\t\trightvol=%d\n", (int)NV(number, RIGHTVOL)); + printf("\t\thwbuf_format=%#08x\n", + (int)NV(number, HWBUF_FORMAT)); + printf("\t\thwbuf_size=%d\n", + (int)NV(number, HWBUF_SIZE)); + printf("\t\thwbuf_blksz=%d\n", + (int)NV(number, HWBUF_BLKSZ)); + printf("\t\thwbuf_blkcnt=%d\n", + (int)NV(number, HWBUF_BLKCNT)); + printf("\t\thwbuf_free=%d\n", + (int)NV(number, HWBUF_FREE)); + printf("\t\thwbuf_ready=%d\n", + (int)NV(number, HWBUF_READY)); + printf("\t\tswbuf_format=%#08x\n", + (int)NV(number, SWBUF_FORMAT)); + printf("\t\tswbuf_size=%d\n", + (int)NV(number, SWBUF_SIZE)); + printf("\t\tswbuf_blksz=%d\n", + (int)NV(number, SWBUF_BLKSZ)); + printf("\t\tswbuf_blkcnt=%d\n", + (int)NV(number, SWBUF_BLKCNT)); + printf("\t\tswbuf_free=%d\n", + (int)NV(number, SWBUF_FREE)); + printf("\t\tswbuf_ready=%d\n", + (int)NV(number, SWBUF_READY)); + printf("\t\tswbuf_feederchain=%s\n", + NV(string, FEEDERCHAIN)); +#undef NV + } + } + + free(arg.buf); + nvlist_destroy(nvl); + close(fd); + + return (0); +}