xref: /freebsd/usr.sbin/virtual_oss/virtual_oss_cmd/command.c (revision 9cab9fde5edad9b409dd2317a2aec7815e6d6bed)
1*9cab9fdeSChristos Margiolis /*-
2*9cab9fdeSChristos Margiolis  * Copyright (c) 2021-2022 Hans Petter Selasky
3*9cab9fdeSChristos Margiolis  *
4*9cab9fdeSChristos Margiolis  * Redistribution and use in source and binary forms, with or without
5*9cab9fdeSChristos Margiolis  * modification, are permitted provided that the following conditions
6*9cab9fdeSChristos Margiolis  * are met:
7*9cab9fdeSChristos Margiolis  * 1. Redistributions of source code must retain the above copyright
8*9cab9fdeSChristos Margiolis  *    notice, this list of conditions and the following disclaimer.
9*9cab9fdeSChristos Margiolis  * 2. Redistributions in binary form must reproduce the above copyright
10*9cab9fdeSChristos Margiolis  *    notice, this list of conditions and the following disclaimer in the
11*9cab9fdeSChristos Margiolis  *    documentation and/or other materials provided with the distribution.
12*9cab9fdeSChristos Margiolis  *
13*9cab9fdeSChristos Margiolis  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*9cab9fdeSChristos Margiolis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*9cab9fdeSChristos Margiolis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*9cab9fdeSChristos Margiolis  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*9cab9fdeSChristos Margiolis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*9cab9fdeSChristos Margiolis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*9cab9fdeSChristos Margiolis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*9cab9fdeSChristos Margiolis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*9cab9fdeSChristos Margiolis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*9cab9fdeSChristos Margiolis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*9cab9fdeSChristos Margiolis  * SUCH DAMAGE.
24*9cab9fdeSChristos Margiolis  */
25*9cab9fdeSChristos Margiolis 
26*9cab9fdeSChristos Margiolis #include <stdio.h>
27*9cab9fdeSChristos Margiolis #include <stdint.h>
28*9cab9fdeSChristos Margiolis #include <stdlib.h>
29*9cab9fdeSChristos Margiolis #include <string.h>
30*9cab9fdeSChristos Margiolis #include <unistd.h>
31*9cab9fdeSChristos Margiolis #include <err.h>
32*9cab9fdeSChristos Margiolis #include <sysexits.h>
33*9cab9fdeSChristos Margiolis #include <stdarg.h>
34*9cab9fdeSChristos Margiolis #include <fcntl.h>
35*9cab9fdeSChristos Margiolis 
36*9cab9fdeSChristos Margiolis #include "virtual_oss.h"
37*9cab9fdeSChristos Margiolis 
38*9cab9fdeSChristos Margiolis static void
message(const char * fmt,...)39*9cab9fdeSChristos Margiolis message(const char *fmt, ...)
40*9cab9fdeSChristos Margiolis {
41*9cab9fdeSChristos Margiolis 	va_list list;
42*9cab9fdeSChristos Margiolis 
43*9cab9fdeSChristos Margiolis 	va_start(list, fmt);
44*9cab9fdeSChristos Margiolis 	vfprintf(stderr, fmt, list);
45*9cab9fdeSChristos Margiolis 	va_end(list);
46*9cab9fdeSChristos Margiolis }
47*9cab9fdeSChristos Margiolis 
48*9cab9fdeSChristos Margiolis static void
usage(void)49*9cab9fdeSChristos Margiolis usage(void)
50*9cab9fdeSChristos Margiolis {
51*9cab9fdeSChristos Margiolis 	message("Usage: virtual_oss_cmd /dev/vdsp.ctl [command line arguments to pass to virtual_oss]\n");
52*9cab9fdeSChristos Margiolis 	exit(EX_USAGE);
53*9cab9fdeSChristos Margiolis }
54*9cab9fdeSChristos Margiolis 
55*9cab9fdeSChristos Margiolis int
main(int argc,char ** argv)56*9cab9fdeSChristos Margiolis main(int argc, char **argv)
57*9cab9fdeSChristos Margiolis {
58*9cab9fdeSChristos Margiolis 	char options[VIRTUAL_OSS_OPTIONS_MAX] = {};
59*9cab9fdeSChristos Margiolis 	size_t offset = 0;
60*9cab9fdeSChristos Margiolis 	size_t len = VIRTUAL_OSS_OPTIONS_MAX - 1;
61*9cab9fdeSChristos Margiolis 	int fd;
62*9cab9fdeSChristos Margiolis 
63*9cab9fdeSChristos Margiolis 	/* check if no options */
64*9cab9fdeSChristos Margiolis 	if (argc < 2)
65*9cab9fdeSChristos Margiolis 		usage();
66*9cab9fdeSChristos Margiolis 
67*9cab9fdeSChristos Margiolis 	fd = open(argv[1], O_RDWR);
68*9cab9fdeSChristos Margiolis 	if (fd < 0)
69*9cab9fdeSChristos Margiolis 		errx(EX_SOFTWARE, "Could not open '%s'", argv[1]);
70*9cab9fdeSChristos Margiolis 
71*9cab9fdeSChristos Margiolis 	for (int x = 2; x != argc; x++) {
72*9cab9fdeSChristos Margiolis 		size_t tmp = strlen(argv[x]) + 1;
73*9cab9fdeSChristos Margiolis 		if (tmp > len)
74*9cab9fdeSChristos Margiolis 			errx(EX_SOFTWARE, "Too many options passed");
75*9cab9fdeSChristos Margiolis 		memcpy(options + offset, argv[x], tmp);
76*9cab9fdeSChristos Margiolis 		options[offset + tmp - 1] = ' ';
77*9cab9fdeSChristos Margiolis 		offset += tmp;
78*9cab9fdeSChristos Margiolis 		len -= tmp;
79*9cab9fdeSChristos Margiolis 	}
80*9cab9fdeSChristos Margiolis 
81*9cab9fdeSChristos Margiolis 	if (options[0] == 0) {
82*9cab9fdeSChristos Margiolis 		struct virtual_oss_system_info info;
83*9cab9fdeSChristos Margiolis 		if (ioctl(fd, VIRTUAL_OSS_GET_SYSTEM_INFO, &info) < 0)
84*9cab9fdeSChristos Margiolis 			errx(EX_SOFTWARE, "Cannot get system information");
85*9cab9fdeSChristos Margiolis 
86*9cab9fdeSChristos Margiolis 		info.rx_device_name[sizeof(info.rx_device_name) - 1] = 0;
87*9cab9fdeSChristos Margiolis 		info.tx_device_name[sizeof(info.tx_device_name) - 1] = 0;
88*9cab9fdeSChristos Margiolis 
89*9cab9fdeSChristos Margiolis 		printf("Sample rate: %u Hz\n"
90*9cab9fdeSChristos Margiolis 		       "Sample width: %u bits\n"
91*9cab9fdeSChristos Margiolis 		       "Sample channels: %u\n"
92*9cab9fdeSChristos Margiolis 		       "Output jitter: %u / %u\n"
93*9cab9fdeSChristos Margiolis 		       "Input device name: %s\n"
94*9cab9fdeSChristos Margiolis 		       "Output device name: %s\n",
95*9cab9fdeSChristos Margiolis 		       info.sample_rate,
96*9cab9fdeSChristos Margiolis 		       info.sample_bits,
97*9cab9fdeSChristos Margiolis 		       info.sample_channels,
98*9cab9fdeSChristos Margiolis 		       info.tx_jitter_down,
99*9cab9fdeSChristos Margiolis 		       info.tx_jitter_up,
100*9cab9fdeSChristos Margiolis 		       info.rx_device_name,
101*9cab9fdeSChristos Margiolis 		       info.tx_device_name);
102*9cab9fdeSChristos Margiolis 	} else {
103*9cab9fdeSChristos Margiolis 		/* execute options */
104*9cab9fdeSChristos Margiolis 		if (ioctl(fd, VIRTUAL_OSS_ADD_OPTIONS, options) < 0)
105*9cab9fdeSChristos Margiolis 			errx(EX_SOFTWARE, "One or more invalid options");
106*9cab9fdeSChristos Margiolis 		/* show error, if any */
107*9cab9fdeSChristos Margiolis 		if (options[0] != '\0')
108*9cab9fdeSChristos Margiolis 			errx(EX_SOFTWARE, "%s", options);
109*9cab9fdeSChristos Margiolis 	}
110*9cab9fdeSChristos Margiolis 
111*9cab9fdeSChristos Margiolis 	close(fd);
112*9cab9fdeSChristos Margiolis 	return (0);
113*9cab9fdeSChristos Margiolis }
114