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