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