source: mainline/uspace/lib/drv/generic/remote_audio_mixer.c@ a35b458

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was 33b8d024, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove const qualifier from the argument of free() and realloc(),
as well as in numerous other variables that hold ownership of memory.

By convention, a pointer that holds ownership is _never_ qualified by const.
This is reflected in the standard type signature of free() and realloc().
Allowing const pointers to hold ownership may seem superficially convenient,
but is actually quite confusing to experienced C programmers.

  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libdrv
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
37#include <assert.h>
38#include <str.h>
39#include <macros.h>
40
41#include "audio_mixer_iface.h"
42#include "ddf/driver.h"
43
44typedef enum {
45 /** Asks for basic mixer info: Mixer name and number of controllable
46 * items.
47 * Answer:
48 * - ENOTSUP - call not supported
49 * - EOK - call successful, info is valid
50 * Answer arguments:
51 * - Mixer name
52 * - Number of controllable items
53 */
54 IPC_M_AUDIO_MIXER_GET_INFO,
55
56 /** Asks for item mixer info: Item name and number of controllable
57 * channels.
58 * Answer:
59 * - ENOTSUP - call not supported
60 * - ENOENT - no such item
61 * - EOK - call successful, info is valid
62 * Answer arguments:
63 * - Item name
64 * - Number of controllable channels
65 */
66 IPC_M_AUDIO_MIXER_GET_ITEM_INFO,
67
68 /** Set new control item setting
69 * Answer:
70 * - ENOTSUP - call not supported
71 * - ENOENT - no such control item
72 * - EOK - call successful, info is valid
73 */
74 IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL,
75
76 /** Get control item setting
77 * Answer:
78 * - ENOTSUP - call not supported
79 * - ENOENT - no such control item
80 * - EOK - call successful, info is valid
81 */
82 IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL,
83} audio_mixer_iface_funcs_t;
84
85/*
86 * CLIENT SIDE
87 */
88
89/**
90 * Query audio mixer for basic info (name and items count).
91 * @param[in] exch IPC exchange connected to the device
92 * @param[out] name Audio mixer string identifier
93 * @param[out] items Number of items controlled by the mixer.
94 * @return Error code.
95 */
96errno_t audio_mixer_get_info(async_exch_t *exch, char **name, unsigned *items)
97{
98 if (!exch)
99 return EINVAL;
100 sysarg_t name_size, itemc;
101 const errno_t ret = async_req_1_2(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
102 IPC_M_AUDIO_MIXER_GET_INFO, &name_size, &itemc);
103 if (ret == EOK && name) {
104 char *name_place = calloc(1, name_size);
105 if (!name_place) {
106 /* Make the other side fail
107 * as it waits for read request */
108 async_data_read_start(exch, (void*)-1, 0);
109 return ENOMEM;
110 }
111 const errno_t ret =
112 async_data_read_start(exch, name_place, name_size);
113 if (ret != EOK) {
114 free(name_place);
115 return ret;
116 }
117 *name = name_place;
118 }
119 if (ret == EOK && items)
120 *items = itemc;
121 return ret;
122}
123
124/**
125 * Query audio mixer for item specific info (name and channels count).
126 * @param[in] exch IPC exchange connected to the device
127 * @param[in] item The control item
128 * @param[out] name Control item string identifier.
129 * @param[out] channles Number of channels associated with this control item.
130 * @return Error code.
131 */
132errno_t audio_mixer_get_item_info(async_exch_t *exch, unsigned item,
133 char **name, unsigned *levels)
134{
135 if (!exch)
136 return EINVAL;
137 sysarg_t name_size, lvls;
138 const errno_t ret = async_req_2_2(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
139 IPC_M_AUDIO_MIXER_GET_ITEM_INFO, item, &name_size, &lvls);
140 if (ret == EOK && name) {
141 char *name_place = calloc(1, name_size);
142 if (!name_place) {
143 /* Make the other side fail
144 * as it waits for read request */
145 async_data_read_start(exch, (void*)-1, 0);
146 return ENOMEM;
147 }
148 const errno_t ret =
149 async_data_read_start(exch, name_place, name_size);
150 if (ret != EOK) {
151 free(name_place);
152 return ret;
153 }
154 *name = name_place;
155 }
156 if (ret == EOK && levels)
157 *levels = lvls;
158 return ret;
159}
160
161/**
162 * Set control item to a new level.
163 * @param[in] exch IPC exchange connected to the device.
164 * @param[in] item The control item controlling the channel.
165 * @param[in] level The new value.
166 * @return Error code.
167 */
168errno_t audio_mixer_set_item_level(async_exch_t *exch, unsigned item,
169 unsigned level)
170{
171 if (!exch)
172 return EINVAL;
173 return async_req_3_0(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
174 IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL, item, level);
175}
176
177/**
178 * Get current level of a control item.
179 * @param[in] exch IPC exchange connected to the device.
180 * @param[in] item The control item controlling the channel.
181 * @param[in] channel The channel index.
182 * @param[out] level Currently set value.
183 * @return Error code.
184 */
185errno_t audio_mixer_get_item_level(async_exch_t *exch, unsigned item,
186 unsigned *level)
187{
188 if (!exch)
189 return EINVAL;
190 sysarg_t current;
191 const errno_t ret = async_req_2_1(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
192 IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL, item, &current);
193 if (ret == EOK && level)
194 *level = current;
195 return ret;
196}
197
198/*
199 * SERVER SIDE
200 */
201static void remote_audio_mixer_get_info(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
202static void remote_audio_mixer_get_item_info(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
203static void remote_audio_mixer_get_item_level(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
204static void remote_audio_mixer_set_item_level(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
205
206/** Remote audio mixer interface operations. */
207static const remote_iface_func_ptr_t remote_audio_mixer_iface_ops[] = {
208 [IPC_M_AUDIO_MIXER_GET_INFO] = remote_audio_mixer_get_info,
209 [IPC_M_AUDIO_MIXER_GET_ITEM_INFO] = remote_audio_mixer_get_item_info,
210 [IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL] = remote_audio_mixer_get_item_level,
211 [IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL] = remote_audio_mixer_set_item_level,
212};
213
214/** Remote audio mixer interface structure. */
215const remote_iface_t remote_audio_mixer_iface = {
216 .method_count = ARRAY_SIZE(remote_audio_mixer_iface_ops),
217 .methods = remote_audio_mixer_iface_ops
218};
219
220void remote_audio_mixer_get_info(
221 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
222{
223 audio_mixer_iface_t *mixer_iface = iface;
224
225 if (!mixer_iface->get_info) {
226 async_answer_0(callid, ENOTSUP);
227 return;
228 }
229 const char *name = NULL;
230 unsigned items = 0;
231 const errno_t ret = mixer_iface->get_info(fun, &name, &items);
232 const size_t name_size = name ? str_size(name) + 1 : 0;
233 async_answer_2(callid, ret, name_size, items);
234 /* Send the name. */
235 if (ret == EOK && name_size > 0) {
236 size_t size;
237 ipc_callid_t name_id;
238 if (!async_data_read_receive(&name_id, &size)) {
239 async_answer_0(name_id, EPARTY);
240 return;
241 }
242 if (size != name_size) {
243 async_answer_0(name_id, ELIMIT);
244 return;
245 }
246 async_data_read_finalize(name_id, name, name_size);
247 }
248}
249
250void remote_audio_mixer_get_item_info(
251 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
252{
253 audio_mixer_iface_t *mixer_iface = iface;
254
255 if (!mixer_iface->get_item_info) {
256 async_answer_0(callid, ENOTSUP);
257 return;
258 }
259
260 const unsigned item = DEV_IPC_GET_ARG1(*call);
261 const char *name = NULL;
262 unsigned values = 0;
263 const errno_t ret = mixer_iface->get_item_info(fun, item, &name, &values);
264 const size_t name_size = name ? str_size(name) + 1 : 0;
265 async_answer_2(callid, ret, name_size, values);
266 /* Send the name. */
267 if (ret == EOK && name_size > 0) {
268 size_t size;
269 ipc_callid_t name_id;
270 if (!async_data_read_receive(&name_id, &size)) {
271 async_answer_0(name_id, EPARTY);
272 return;
273 }
274 if (size != name_size) {
275 async_answer_0(name_id, ELIMIT);
276 return;
277 }
278 async_data_read_finalize(name_id, name, name_size);
279 }
280}
281
282void remote_audio_mixer_set_item_level(
283 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
284{
285 audio_mixer_iface_t *mixer_iface = iface;
286
287 if (!mixer_iface->set_item_level) {
288 async_answer_0(callid, ENOTSUP);
289 return;
290 }
291 const unsigned item = DEV_IPC_GET_ARG1(*call);
292 const unsigned value = DEV_IPC_GET_ARG2(*call);
293 const errno_t ret = mixer_iface->set_item_level(fun, item, value);
294 async_answer_0(callid, ret);
295}
296
297void remote_audio_mixer_get_item_level(
298 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
299{
300 audio_mixer_iface_t *mixer_iface = iface;
301
302 if (!mixer_iface->get_item_level) {
303 async_answer_0(callid, ENOTSUP);
304 return;
305 }
306 const unsigned item = DEV_IPC_GET_ARG1(*call);
307 unsigned current = 0;
308 const errno_t ret =
309 mixer_iface->get_item_level(fun, item, &current);
310 async_answer_1(callid, ret, current);
311}
312
313/**
314 * @}
315 */
Note: See TracBrowser for help on using the repository browser.