• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * This API creates multiple threads, one for control, and a thread per audio
6  * stream. The control thread is used to receive messages and notifications
7  * from the audio server, and manage the per-stream threads. API calls below
8  * may send messages to the control thread, or directly to the server. It is
9  * required that the control thread is running in order to support audio
10  * streams and notifications from the server.
11  *
12  * The API has multiple initialization sequences, but some of those can block
13  * while waiting for a response from the server.
14  *
15  * The following is the non-blocking API initialization sequence:
16  *	cras_client_create()
17  *      cras_client_set_connection_status_cb()                       (optional)
18  *      cras_client_run_thread()
19  *      cras_client_connect_async()
20  *
21  * The connection callback is executed asynchronously from the control thread
22  * when the connection has been established. The connection callback should be
23  * used to turn on or off interactions with any API call that communicates with
24  * the audio server or starts/stops audio streams. The above is implemented by
25  * cras_helper_create_connect_async().
26  *
27  * The following alternative (deprecated) initialization sequence can ensure
28  * that the connection is established synchronously.
29  *
30  * Just connect to the server (no control thread):
31  *      cras_client_create()
32  *      cras_client_set_server_connection_cb()                       (optional)
33  *   one of:
34  *      cras_client_connect()                                  (blocks forever)
35  *   or
36  *      cras_client_connect_timeout()                      (blocks for timeout)
37  *
38  * For API calls below that require the control thread to be running:
39  *      cras_client_run_thread();
40  *      cras_client_connected_wait();                     (blocks up to 1 sec.)
41  *
42  * The above minus setting the connection callback is implemented within
43  * cras_helper_create_connect().
44  */
45 
46 #ifndef CRAS_CLIENT_H_
47 #define CRAS_CLIENT_H_
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #include <stdbool.h>
54 #include <stdint.h>
55 #include <sys/select.h>
56 
57 #include "cras_iodev_info.h"
58 #include "cras_types.h"
59 #include "cras_util.h"
60 
61 struct cras_client;
62 struct cras_hotword_handle;
63 struct cras_stream_params;
64 
65 /* Callback for audio received or transmitted.
66  * Args (All pointer will be valid - except user_arg, that's up to the user):
67  *    client: The client requesting service.
68  *    stream_id - Unique identifier for the stream needing data read/written.
69  *    samples - Read or write samples to/form here.
70  *    frames - Maximum number of frames to read or write.
71  *    sample_time - Playback time for the first sample read/written.
72  *    user_arg - Value passed to add_stream;
73  * Return:
74  *    Returns the number of frames read or written on success, or a negative
75  *    number if there is a stream-fatal error. Returns EOF when the end of the
76  *    stream is reached.
77  */
78 typedef int (*cras_playback_cb_t)(struct cras_client *client,
79 				  cras_stream_id_t stream_id, uint8_t *samples,
80 				  size_t frames,
81 				  const struct timespec *sample_time,
82 				  void *user_arg);
83 
84 /* Callback for audio received and/or transmitted.
85  * Args (All pointer will be valid - except user_arg, that's up to the user):
86  *    client: The client requesting service.
87  *    stream_id - Unique identifier for the stream needing data read/written.
88  *    captured_samples - Read samples form here.
89  *    playback_samples - Read or write samples to here.
90  *    frames - Maximum number of frames to read or write.
91  *    captured_time - Time the first sample was read.
92  *    playback_time - Playback time for the first sample written.
93  *    user_arg - Value passed to add_stream;
94  * Return:
95  *    Returns the number of frames read or written on success, or a negative
96  *    number if there is a stream-fatal error. Returns EOF when the end of the
97  *    stream is reached.
98  */
99 typedef int (*cras_unified_cb_t)(struct cras_client *client,
100 				 cras_stream_id_t stream_id,
101 				 uint8_t *captured_samples,
102 				 uint8_t *playback_samples, unsigned int frames,
103 				 const struct timespec *captured_time,
104 				 const struct timespec *playback_time,
105 				 void *user_arg);
106 
107 /* Callback for handling stream errors.
108  * Args:
109  *    client - The client created with cras_client_create().
110  *    stream_id - The ID for this stream.
111  *    error - The error code,
112  *    user_arg - The argument defined in cras_client_*_params_create().
113  */
114 typedef int (*cras_error_cb_t)(struct cras_client *client,
115 			       cras_stream_id_t stream_id, int error,
116 			       void *user_arg);
117 
118 /* Server connection status. */
119 typedef enum cras_connection_status {
120 	CRAS_CONN_STATUS_FAILED,
121 	/* Resource allocation problem. Free resources, and retry the
122 	 * connection with cras_client_connect_async(), or (blocking)
123 	 * cras_client_connect(). Do not call cras_client_connect(),
124 	 * cras_client_connect_timeout(), or cras_client_destroy()
125 	 * from the callback. */
126 	CRAS_CONN_STATUS_DISCONNECTED,
127 	/* The control thread is attempting to reconnect to the
128 	 * server in the background. Any attempt to access the
129 	 * server will fail or block (see
130 	 * cras_client_set_server_message_blocking(). */
131 	CRAS_CONN_STATUS_CONNECTED,
132 	/* Connection is established. All state change callbacks
133 	 * have been re-registered, but audio streams must be
134 	 * restarted, and node state data must be updated. */
135 } cras_connection_status_t;
136 
137 /* Callback for handling server connection status.
138  *
139  * See also cras_client_set_connection_status_cb(). Do not call
140  * cras_client_connect(), cras_client_connect_timeout(), or
141  * cras_client_destroy() from this callback.
142  *
143  * Args:
144  *    client - The client created with cras_client_create().
145  *    status - The status of the connection to the server.
146  *    user_arg - The argument defined in
147  *               cras_client_set_connection_status_cb().
148  */
149 typedef void (*cras_connection_status_cb_t)(struct cras_client *client,
150 					    cras_connection_status_t status,
151 					    void *user_arg);
152 
153 /* Callback for setting thread priority. */
154 typedef void (*cras_thread_priority_cb_t)(struct cras_client *client);
155 
156 /* Callback for handling get hotword models reply. */
157 typedef void (*get_hotword_models_cb_t)(struct cras_client *client,
158 					const char *hotword_models);
159 
160 /* Callback to wait for a hotword trigger. */
161 typedef void (*cras_hotword_trigger_cb_t)(struct cras_client *client,
162 					  struct cras_hotword_handle *handle,
163 					  void *user_data);
164 
165 /* Callback for handling hotword errors. */
166 typedef int (*cras_hotword_error_cb_t)(struct cras_client *client,
167 				       struct cras_hotword_handle *handle,
168 				       int error, void *user_data);
169 
170 /*
171  * Client handling.
172  */
173 
174 /* Creates a new client.
175  * Args:
176  *    client - Filled with a pointer to the new client.
177  * Returns:
178  *    0 on success (*client is filled with a valid cras_client pointer).
179  *    Negative error code on failure(*client will be NULL).
180  */
181 int cras_client_create(struct cras_client **client);
182 
183 /* Creates a new client with given connection type.
184  * Args:
185  *     client - Filled with a pointer to the new client.
186  *     conn_type - enum CRAS_CONNECTION_TYPE
187  *
188  * Returns:
189  *     0 on success (*client is filled with a valid cras_client pointer).
190  *     Negative error code on failure(*client will be NULL).
191  */
192 int cras_client_create_with_type(struct cras_client **client,
193 				 enum CRAS_CONNECTION_TYPE conn_type);
194 
195 /* Destroys a client.
196  * Args:
197  *    client - returned from "cras_client_create".
198  */
199 void cras_client_destroy(struct cras_client *client);
200 
201 /* Connects a client to the running server.
202  * Waits forever (until interrupted or connected).
203  * Args:
204  *    client - pointer returned from "cras_client_create".
205  * Returns:
206  *    0 on success, or a negative error code on failure (from errno.h).
207  */
208 int cras_client_connect(struct cras_client *client);
209 
210 /* Connects a client to the running server, retries until timeout.
211  * Args:
212  *    client - pointer returned from "cras_client_create".
213  *    timeout_ms - timeout in milliseconds or negative to wait forever.
214  * Returns:
215  *    0 on success, or a negative error code on failure (from errno.h).
216  */
217 int cras_client_connect_timeout(struct cras_client *client,
218 				unsigned int timeout_ms);
219 
220 /* Begins running the client control thread.
221  *
222  * Required for stream operations and other operations noted below.
223  *
224  * Args:
225  *    client - the client to start (from cras_client_create).
226  * Returns:
227  *    0 on success or if the thread is already running, -EINVAL if the client
228  *    pointer is NULL, or the negative result of pthread_create().
229  */
230 int cras_client_run_thread(struct cras_client *client);
231 
232 /* Stops running a client.
233  * This function is executed automatically by cras_client_destroy().
234  * Args:
235  *    client - the client to stop (from cras_client_create).
236  * Returns:
237  *    0 on success or if the thread was already stopped, -EINVAL if the client
238  *    isn't valid.
239  */
240 int cras_client_stop(struct cras_client *client);
241 
242 /* Wait up to 1 second for the client thread to complete the server connection.
243  *
244  * After cras_client_run_thread() is executed, this function can be used to
245  * ensure that the connection has been established with the server and ensure
246  * that any information about the server is up to date. If
247  * cras_client_run_thread() has not yet been executed, or cras_client_stop()
248  * was executed and thread isn't running, then this function returns -EINVAL.
249  *
250  * Args:
251  *    client - pointer returned from "cras_client_create".
252  * Returns:
253  *    0 on success, or a negative error code on failure (from errno.h).
254  */
255 int cras_client_connected_wait(struct cras_client *client);
256 
257 /* Ask the client control thread to connect to the audio server.
258  *
259  * After cras_client_run_thread() is executed, this function can be used
260  * to ask the control thread to connect to the audio server asynchronously.
261  * The callback set with cras_client_set_connection_status_cb() will be
262  * executed when the connection is established.
263  *
264  * Args:
265  *    client - The client from cras_client_create().
266  * Returns:
267  *    0 on success, or a negative error code on failure (from errno.h).
268  *    -EINVAL if the client pointer is invalid or the control thread is
269  *    not running.
270  */
271 int cras_client_connect_async(struct cras_client *client);
272 
273 /* Sets server connection status callback.
274  *
275  * See cras_connection_status_t for a description of the connection states
276  * and appropriate user action.
277  *
278  * Args:
279  *    client - The client from cras_client_create.
280  *    connection_cb - The callback function to register.
281  *    user_arg - Pointer that will be passed to the callback.
282  */
283 void cras_client_set_connection_status_cb(
284 	struct cras_client *client, cras_connection_status_cb_t connection_cb,
285 	void *user_arg);
286 
287 /* Sets callback for setting thread priority.
288  * Args:
289  *    client - The client from cras_client_create.
290  *    cb - The thread priority callback.
291  */
292 void cras_client_set_thread_priority_cb(struct cras_client *client,
293 					cras_thread_priority_cb_t cb);
294 
295 /* Returns the current list of output devices.
296  *
297  * Requires that the connection to the server has been established.
298  *
299  * Data is copied and thus can become out of date. This call must be
300  * re-executed to get updates.
301  *
302  * Args:
303  *    client - The client from cras_client_create.
304  *    devs - Array that will be filled with device info.
305  *    nodes - Array that will be filled with node info.
306  *    *num_devs - Maximum number of devices to put in the array.
307  *    *num_nodes - Maximum number of nodes to put in the array.
308  * Returns:
309  *    0 on success, -EINVAL if the client isn't valid or isn't running.
310  *    *num_devs is set to the actual number of devices info filled.
311  *    *num_nodes is set to the actual number of nodes info filled.
312  */
313 int cras_client_get_output_devices(const struct cras_client *client,
314 				   struct cras_iodev_info *devs,
315 				   struct cras_ionode_info *nodes,
316 				   size_t *num_devs, size_t *num_nodes);
317 
318 /* Returns the current list of input devices.
319  *
320  * Requires that the connection to the server has been established.
321  *
322  * Data is copied and thus can become out of date. This call must be
323  * re-executed to get updates.
324  *
325  * Args:
326  *    client - The client from cras_client_create.
327  *    devs - Array that will be filled with device info.
328  *    nodes - Array that will be filled with node info.
329  *    *num_devs - Maximum number of devices to put in the array.
330  *    *num_nodes - Maximum number of nodes to put in the array.
331  * Returns:
332  *    0 on success, -EINVAL if the client isn't valid or isn't running.
333  *    *num_devs is set to the actual number of devices info filled.
334  *    *num_nodes is set to the actual number of nodes info filled.
335  */
336 int cras_client_get_input_devices(const struct cras_client *client,
337 				  struct cras_iodev_info *devs,
338 				  struct cras_ionode_info *nodes,
339 				  size_t *num_devs, size_t *num_nodes);
340 
341 /* Returns the current list of clients attached to the server.
342  *
343  * Requires that the connection to the server has been established.
344  *
345  * Data is copied and thus can become out of date. This call must be
346  * re-executed to get updates.
347  *
348  * Args:
349  *    client - This client (from cras_client_create).
350  *    clients - Array that will be filled with a list of attached clients.
351  *    max_clients - Maximum number of clients to put in the array.
352  * Returns:
353  *    The number of attached clients.  This may be more that max_clients passed
354  *    in, this indicates that all of the clients wouldn't fit in the provided
355  *    array.
356  */
357 int cras_client_get_attached_clients(const struct cras_client *client,
358 				     struct cras_attached_client_info *clients,
359 				     size_t max_clients);
360 
361 /* Find a node info with the matching node id.
362  *
363  * Requires that the connection to the server has been established.
364  *
365  * Data is copied and thus can become out of date. This call must be
366  * re-executed to get updates.
367  *
368  * Args:
369  *    client - This client (from cras_client_create).
370  *    input - Non-zero for input nodes, zero for output nodes.
371  *    node_id - The node id to look for.
372  *    node_info - The information about the ionode will be returned here.
373  * Returns:
374  *    0 if successful, negative on error; -ENOENT if the node cannot be found.
375  */
376 int cras_client_get_node_by_id(const struct cras_client *client, int input,
377 			       const cras_node_id_t node_id,
378 			       struct cras_ionode_info *node_info);
379 
380 /* Checks if the output device with the given name is currently plugged in.
381  *
382  * For internal devices this checks that jack state, for USB devices this will
383  * always be true if they are present. The name parameter can be the complete
384  * name or any unique prefix of the name. If the name is not unique the first
385  * matching name will be checked.
386  *
387  * Requires that the connection to the server has been established.
388  *
389  * Data is copied and thus can become out of date. This call must be
390  * re-executed to get updates.
391  *
392  * Args:
393  *    client - The client from cras_client_create.
394  *    name - Name of the device to check.
395  * Returns:
396  *    1 if the device exists and is plugged, 0 otherwise.
397  */
398 int cras_client_output_dev_plugged(const struct cras_client *client,
399 				   const char *name);
400 
401 /* Set the value of an attribute of an ionode.
402  *
403  * Args:
404  *    client - The client from cras_client_create.
405  *    node_id - The id of the ionode.
406  *    attr - the attribute we want to change.
407  *    value - the value we want to set.
408  * Returns:
409  *    Returns 0 for success, negative on error (from errno.h).
410  */
411 int cras_client_set_node_attr(struct cras_client *client,
412 			      cras_node_id_t node_id, enum ionode_attr attr,
413 			      int value);
414 
415 /* Select the preferred node for playback/capture.
416  *
417  * Args:
418  *    client - The client from cras_client_create.
419  *    direction - The direction of the ionode.
420  *    node_id - The id of the ionode. If node_id is the special value 0, then
421  *        the preference is cleared and cras will choose automatically.
422  */
423 int cras_client_select_node(struct cras_client *client,
424 			    enum CRAS_STREAM_DIRECTION direction,
425 			    cras_node_id_t node_id);
426 
427 /* Adds an active node for playback/capture.
428  *
429  * Args:
430  *    client - The client from cras_client_create.
431  *    direction - The direction of the ionode.
432  *    node_id - The id of the ionode. If there's no node matching given
433  *        id, nothing will happen in CRAS.
434  */
435 int cras_client_add_active_node(struct cras_client *client,
436 				enum CRAS_STREAM_DIRECTION direction,
437 				cras_node_id_t node_id);
438 
439 /* Removes an active node for playback/capture.
440  *
441  * Args:
442  *    client - The client from cras_client_create.
443  *    direction - The direction of the ionode.
444  *    node_id - The id of the ionode. If there's no node matching given
445  *        id, nothing will happen in CRAS.
446  */
447 int cras_client_rm_active_node(struct cras_client *client,
448 			       enum CRAS_STREAM_DIRECTION direction,
449 			       cras_node_id_t node_id);
450 
451 /* Asks the server to reload dsp plugin configuration from the ini file.
452  *
453  * Args:
454  *    client - The client from cras_client_create.
455  * Returns:
456  *    0 on success, -EINVAL if the client isn't valid or isn't running.
457  */
458 int cras_client_reload_dsp(struct cras_client *client);
459 
460 /* Asks the server to dump current dsp information to syslog.
461  *
462  * Args:
463  *    client - The client from cras_client_create.
464  * Returns:
465  *    0 on success, -EINVAL if the client isn't valid or isn't running.
466  */
467 int cras_client_dump_dsp_info(struct cras_client *client);
468 
469 /* Asks the server to dump current audio thread information.
470  *
471  * Args:
472  *    client - The client from cras_client_create.
473  *    cb - A function to call when the data is received.
474  * Returns:
475  *    0 on success, -EINVAL if the client isn't valid or isn't running.
476  */
477 int cras_client_update_audio_debug_info(struct cras_client *client,
478 					void (*cb)(struct cras_client *));
479 
480 /* Asks the server to dump current main thread information.
481  * Args:
482  *    client - The client from cras_client_create.
483  *    cb - A function to call when the data is received.
484  * Returns:
485  *    0 on success, -EINVAL if the client isn't valid or isn't running.
486  */
487 int cras_client_update_main_thread_debug_info(struct cras_client *client,
488 					      void (*cb)(struct cras_client *));
489 
490 /* Asks the server to dump bluetooth debug information.
491  * Args:
492  *    client - The client from cras_client_create.
493  *    cb - Function to call when debug info is ready.
494  * Returns:
495  *    0 on success, -EINVAL if the client isn't valid or isn't running.
496  */
497 int cras_client_update_bt_debug_info(struct cras_client *client,
498 				     void (*cb)(struct cras_client *));
499 
500 /* Gets read-only access to audio thread log. Should be called once before
501    calling cras_client_read_atlog.
502  * Args:
503  *    client - The client from cras_client_create.
504  *    atlog_access_cb - Function to call after getting atlog access.
505  * Returns:
506  *    0 on success, -EINVAL if the client or atlog_access_cb isn't valid.
507  */
508 int cras_client_get_atlog_access(struct cras_client *client,
509 				 void (*atlog_access_cb)(struct cras_client *));
510 
511 /* Reads continuous audio thread log into 'buf', starting from 'read_idx'-th log
512  * till the latest. The number of missing logs within the range will be stored
513  * in 'missing'. Requires calling cras_client_get_atlog_access() beforehand
514  * to get access to audio thread log.
515  * Args:
516  *    client - The client from cras_client_create.
517  *    read_idx - The log number to start reading with.
518  *    missing - The pointer to store the number of missing logs.
519  *    buf - The buffer to which continuous logs will be copied.
520  * Returns:
521  *    The number of logs copied. < 0 if failed to read audio thread log.
522  */
523 int cras_client_read_atlog(struct cras_client *client, uint64_t *read_idx,
524 			   uint64_t *missing,
525 			   struct audio_thread_event_log *buf);
526 
527 /* Asks the server to dump current audio thread snapshots.
528  *
529  * Args:
530  *    client - The client from cras_client_create.
531  *    cb - A function to call when the data is received.
532  * Returns:
533  *    0 on success, -EINVAL if the client isn't valid or isn't running.
534  */
535 int cras_client_update_audio_thread_snapshots(struct cras_client *client,
536 					      void (*cb)(struct cras_client *));
537 
538 /* Gets the max supported channel count of the output device from node_id.
539  * Args:
540  *    client - The client from cras_client_create.
541  *    node_id - ID of the node.
542  *    max_channels - Out parameter will be filled with the max supported channel
543  *        count.
544  * Returns:
545  *    0 on success, or negative error code on failure.
546  */
547 int cras_client_get_max_supported_channels(const struct cras_client *client,
548 					   cras_node_id_t node_id,
549 					   uint32_t *max_channels);
550 
551 /*
552  * Stream handling.
553  */
554 
555 /* Setup stream configuration parameters.
556  * Args:
557  *    direction - playback(CRAS_STREAM_OUTPUT) or capture(CRAS_STREAM_INPUT).
558  *    buffer_frames - total number of audio frames to buffer (dictates latency).
559  *    cb_threshold - For playback, call back for more data when the buffer
560  *        reaches this level. For capture, this is ignored (Audio callback will
561  *        be called when buffer_frames have been captured).
562  *    unused - No longer used.
563  *    stream_type - media or talk (currently only support "default").
564  *    flags - Currently only used for CRAS_INPUT_STREAM_FLAG.
565  *    user_data - Pointer that will be passed to the callback.
566  *    aud_cb - Called when audio is needed(playback) or ready(capture). Allowed
567  *        return EOF to indicate that the stream should terminate.
568  *    err_cb - Called when there is an error with the stream.
569  *    format - The format of the audio stream.  Specifies bits per sample,
570  *        number of channels, and sample rate.
571  */
572 struct cras_stream_params *cras_client_stream_params_create(
573 	enum CRAS_STREAM_DIRECTION direction, size_t buffer_frames,
574 	size_t cb_threshold, size_t unused, enum CRAS_STREAM_TYPE stream_type,
575 	uint32_t flags, void *user_data, cras_playback_cb_t aud_cb,
576 	cras_error_cb_t err_cb, struct cras_audio_format *format);
577 
578 /* Functions to set the client type on given stream parameter.
579  * Args:
580  *    params - Stream configuration parameters.
581  *    client_type - A client type.
582  */
583 void cras_client_stream_params_set_client_type(
584 	struct cras_stream_params *params, enum CRAS_CLIENT_TYPE client_type);
585 
586 /* Functions to enable or disable specific effect on given stream parameter.
587  * Args:
588  *    params - Stream configuration parameters.
589  */
590 void cras_client_stream_params_enable_aec(struct cras_stream_params *params);
591 void cras_client_stream_params_disable_aec(struct cras_stream_params *params);
592 void cras_client_stream_params_enable_ns(struct cras_stream_params *params);
593 void cras_client_stream_params_disable_ns(struct cras_stream_params *params);
594 void cras_client_stream_params_enable_agc(struct cras_stream_params *params);
595 void cras_client_stream_params_disable_agc(struct cras_stream_params *params);
596 void cras_client_stream_params_enable_vad(struct cras_stream_params *params);
597 void cras_client_stream_params_disable_vad(struct cras_stream_params *params);
598 
599 /* Setup stream configuration parameters. DEPRECATED.
600  * TODO(crbug.com/972928): remove this
601  * Use cras_client_stream_params_create instead.
602  * Args:
603  *    direction - playback(CRAS_STREAM_OUTPUT) or capture(CRAS_STREAM_INPUT) or
604  *        loopback(CRAS_STREAM_POST_MIX_PRE_DSP).
605  *    block_size - The number of frames per callback(dictates latency).
606  *    stream_type - media or talk (currently only support "default").
607  *    flags - None currently used.
608  *    user_data - Pointer that will be passed to the callback.
609  *    unified_cb - Called to request audio data or to notify the client when
610  *                 captured audio is available. Though this is a unified_cb,
611  *                 only one direction will be used for a stream, depending
612  *                 on the 'direction' parameter.
613  *    err_cb - Called when there is an error with the stream.
614  *    format - The format of the audio stream.  Specifies bits per sample,
615  *        number of channels, and sample rate.
616  */
617 struct cras_stream_params *cras_client_unified_params_create(
618 	enum CRAS_STREAM_DIRECTION direction, unsigned int block_size,
619 	enum CRAS_STREAM_TYPE stream_type, uint32_t flags, void *user_data,
620 	cras_unified_cb_t unified_cb, cras_error_cb_t err_cb,
621 	struct cras_audio_format *format);
622 
623 /* Destroy stream params created with cras_client_stream_params_create. */
624 void cras_client_stream_params_destroy(struct cras_stream_params *params);
625 
626 /* Creates a new stream and return the stream id or < 0 on error.
627  *
628  * Requires execution of cras_client_run_thread(), and an active connection
629  * to the audio server.
630  *
631  * Args:
632  *    client - The client to add the stream to (from cras_client_create).
633  *    stream_id_out - On success will be filled with the new stream id.
634  *        Guaranteed to be set before any callbacks are made.
635  *    config - The cras_stream_params struct specifying the parameters for the
636  *        stream.
637  * Returns:
638  *    0 on success, negative error code on failure (from errno.h).
639  */
640 int cras_client_add_stream(struct cras_client *client,
641 			   cras_stream_id_t *stream_id_out,
642 			   struct cras_stream_params *config);
643 
644 /* Creates a pinned stream and return the stream id or < 0 on error.
645  *
646  * Requires execution of cras_client_run_thread(), and an active connection
647  * to the audio server.
648  *
649  * Args:
650  *    client - The client to add the stream to (from cras_client_create).
651  *    dev_idx - Index of the device to attach the newly created stream.
652  *    stream_id_out - On success will be filled with the new stream id.
653  *        Guaranteed to be set before any callbacks are made.
654  *    config - The cras_stream_params struct specifying the parameters for the
655  *        stream.
656  * Returns:
657  *    0 on success, negative error code on failure (from errno.h).
658  */
659 int cras_client_add_pinned_stream(struct cras_client *client, uint32_t dev_idx,
660 				  cras_stream_id_t *stream_id_out,
661 				  struct cras_stream_params *config);
662 
663 /* Removes a currently playing/capturing stream.
664  *
665  * Requires execution of cras_client_run_thread().
666  *
667  * Args:
668  *    client - Client to remove the stream (returned from cras_client_create).
669  *    stream_id - ID returned from cras_client_add_stream to identify the stream
670           to remove.
671  * Returns:
672  *    0 on success negative error code on failure (from errno.h).
673  */
674 int cras_client_rm_stream(struct cras_client *client,
675 			  cras_stream_id_t stream_id);
676 
677 /* Sets the volume scaling factor for the given stream.
678  *
679  * Requires execution of cras_client_run_thread().
680  *
681  * Args:
682  *    client - Client owning the stream.
683  *    stream_id - ID returned from cras_client_add_stream.
684  *    volume_scaler - 0.0-1.0 the new value to scale this stream by.
685  */
686 int cras_client_set_stream_volume(struct cras_client *client,
687 				  cras_stream_id_t stream_id,
688 				  float volume_scaler);
689 
690 /*
691  * System level functions.
692  */
693 
694 /* Sets the volume of the system.
695  *
696  * Volume here ranges from 0 to 100, and will be translated to dB based on the
697  * output-specific volume curve.
698  *
699  * Args:
700  *    client - The client from cras_client_create.
701  *    volume - 0-100 the new volume index.
702  * Returns:
703  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
704  *    -EINVAL if 'client' is invalid.
705  */
706 int cras_client_set_system_volume(struct cras_client *client, size_t volume);
707 
708 /* Sets the mute state of the system.
709  *
710  * Args:
711  *    client - The client from cras_client_create.
712  *    mute - 0 is un-mute, 1 is muted.
713  * Returns:
714  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
715  *    -EINVAL if 'client' is invalid.
716  */
717 int cras_client_set_system_mute(struct cras_client *client, int mute);
718 
719 /* Sets the user mute state of the system.
720  *
721  * This is used for mutes caused by user interaction. Like the mute key.
722  *
723  * Args:
724  *    client - The client from cras_client_create.
725  *    mute - 0 is un-mute, 1 is muted.
726  * Returns:
727  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
728  *    -EINVAL if 'client' is invalid.
729  */
730 int cras_client_set_user_mute(struct cras_client *client, int mute);
731 
732 /* Sets the mute locked state of the system.
733  *
734  * Changing mute state is impossible when this flag is set to locked.
735  *
736  * Args:
737  *    client - The client from cras_client_create.
738  *    locked - 0 is un-locked, 1 is locked.
739  * Returns:
740  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
741  *    -EINVAL if 'client' is invalid.
742  */
743 int cras_client_set_system_mute_locked(struct cras_client *client, int locked);
744 
745 /* Sets the capture mute state of the system.
746  *
747  * Recordings will be muted when this is set.
748  *
749  * Args:
750  *    client - The client from cras_client_create.
751  *    mute - 0 is un-mute, 1 is muted.
752  * Returns:
753  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
754  *    -EINVAL if 'client' is invalid.
755  */
756 int cras_client_set_system_capture_mute(struct cras_client *client, int mute);
757 
758 /* Sets the capture mute locked state of the system.
759  *
760  * Changing mute state is impossible when this flag is set to locked.
761  *
762  * Args:
763  *    client - The client from cras_client_create.
764  *    locked - 0 is un-locked, 1 is locked.
765  * Returns:
766  *    0 for success, -EPIPE if there is an I/O error talking to the server, or
767  *    -EINVAL if 'client' is invalid.
768  */
769 int cras_client_set_system_capture_mute_locked(struct cras_client *client,
770 					       int locked);
771 
772 /* Gets the current system volume.
773  *
774  * Requires that the connection to the server has been established.
775  *
776  * Args:
777  *    client - The client from cras_client_create.
778  * Returns:
779  *    The current system volume between 0 and 100.
780  */
781 size_t cras_client_get_system_volume(const struct cras_client *client);
782 
783 /* Gets the current system mute state.
784  *
785  * Requires that the connection to the server has been established.
786  *
787  * Args:
788  *    client - The client from cras_client_create.
789  * Returns:
790  *    0 if not muted, 1 if it is.
791  */
792 int cras_client_get_system_muted(const struct cras_client *client);
793 
794 /* Gets the current user mute state.
795  *
796  * Requires that the connection to the server has been established.
797  *
798  * Args:
799  *    client - The client from cras_client_create.
800  * Returns:
801  *    0 if not muted, 1 if it is.
802  */
803 int cras_client_get_user_muted(const struct cras_client *client);
804 
805 /* Gets the current system capture mute state.
806  *
807  * Requires that the connection to the server has been established.
808  *
809  * Args:
810  *    client - The client from cras_client_create.
811  * Returns:
812  *    0 if capture is not muted, 1 if it is.
813  */
814 int cras_client_get_system_capture_muted(const struct cras_client *client);
815 
816 /* Gets the current minimum system volume.
817  * Args:
818  *    client - The client from cras_client_create.
819  * Returns:
820  *    The minimum value for the current output device in dBFS * 100.  This is
821  *    the level of attenuation at volume == 1.
822  */
823 long cras_client_get_system_min_volume(const struct cras_client *client);
824 
825 /* Gets the current maximum system volume.
826  * Args:
827  *    client - The client from cras_client_create.
828  * Returns:
829  *    The maximum value for the current output device in dBFS * 100.  This is
830  *    the level of attenuation at volume == 100.
831  */
832 long cras_client_get_system_max_volume(const struct cras_client *client);
833 
834 /* Gets the default output buffer size.
835  * Args:
836  *    client - The client from cras_client_create.
837  * Returns:
838  *    Default output buffer size in frames. A negative error on failure.
839  */
840 int cras_client_get_default_output_buffer_size(struct cras_client *client);
841 
842 /* Gets audio debug info.
843  *
844  * Requires that the connection to the server has been established.
845  * Access to the resulting pointer is not thread-safe.
846  *
847  * Args:
848  *    client - The client from cras_client_create.
849  * Returns:
850  *    A pointer to the debug info.  This info is only updated when requested by
851  *    calling cras_client_update_audio_debug_info.
852  */
853 const struct audio_debug_info *
854 cras_client_get_audio_debug_info(const struct cras_client *client);
855 
856 /* Gets bluetooth debug info.
857  *
858  * Requires that the connection to the server has been established.
859  *
860  * Args:
861  *    client - The client from cras_client_create.
862  * Returns:
863  *    A pointer to the debug info. This info is updated and requested by
864  *    calling cras_client_update_bt_debug_info.
865  */
866 const struct cras_bt_debug_info *
867 cras_client_get_bt_debug_info(const struct cras_client *client);
868 
869 /* Gets main thread debug info.
870  * Args:
871  *    client - The client from cras_client_create.
872  * Returns:
873  *    A pointer to the debug info. This info is updated and requested by
874  *    calling cras_client_update_main_thread_debug_info.
875  */
876 const struct main_thread_debug_info *
877 cras_client_get_main_thread_debug_info(const struct cras_client *client);
878 
879 /* Gets audio thread snapshot buffer.
880  *
881  * Requires that the connection to the server has been established.
882  * Access to the resulting pointer is not thread-safe.
883  *
884  * Args:
885  *    client - The client from cras_client_create.
886  * Returns:
887  *    A pointer to the snapshot buffer.  This info is only updated when
888  *    requested by calling cras_client_update_audio_thread_snapshots.
889  */
890 const struct cras_audio_thread_snapshot_buffer *
891 cras_client_get_audio_thread_snapshot_buffer(const struct cras_client *client);
892 
893 /* Gets the number of streams currently attached to the server.
894  *
895  * This is the total number of capture and playback streams. If the ts argument
896  * is not null, then it will be filled with the last time audio was played or
897  * recorded. ts will be set to the current time if streams are currently
898  * active.
899  *
900  * Requires that the connection to the server has been established.
901  *
902  * Args:
903  *    client - The client from cras_client_create.
904  *    ts - Filled with the timestamp of the last stream.
905  * Returns:
906  *    The number of active streams.
907  */
908 unsigned cras_client_get_num_active_streams(const struct cras_client *client,
909 					    struct timespec *ts);
910 
911 /*
912  * Utility functions.
913  */
914 
915 /* Returns the number of bytes in an audio frame for a stream.
916  * Args:
917  *    format - The format of the audio stream.  Specifies bits per sample,
918  *        number of channels, and sample rate.
919  * Returns:
920  *   Positive number of bytes in a frame, or a negative error code if fmt is
921  *   NULL.
922  */
923 int cras_client_format_bytes_per_frame(struct cras_audio_format *fmt);
924 
925 /* For playback streams, calculates the latency of the next sample written.
926  * Only valid when called from the audio callback function for the stream
927  * (aud_cb).
928  * Args:
929  *    sample_time - The sample time stamp passed in to aud_cb.
930  *    delay - Out parameter will be filled with the latency.
931  * Returns:
932  *    0 on success, -EINVAL if delay is NULL.
933  */
934 int cras_client_calc_playback_latency(const struct timespec *sample_time,
935 				      struct timespec *delay);
936 
937 /* For capture returns the latency of the next frame to be read from the buffer
938  * (based on when it was captured).  Only valid when called from the audio
939  * callback function for the stream (aud_cb).
940  * Args:
941  *    sample_time - The sample time stamp passed in to aud_cb.
942  *    delay - Out parameter will be filled with the latency.
943  * Returns:
944  *    0 on success, -EINVAL if delay is NULL.
945  */
946 int cras_client_calc_capture_latency(const struct timespec *sample_time,
947 				     struct timespec *delay);
948 
949 /* Set the volume of the given output node. Only for output nodes.
950  *
951  * Args:
952  *    client - The client from cras_client_create.
953  *    node_id - ID of the node.
954  *    volume - New value for node volume.
955  */
956 int cras_client_set_node_volume(struct cras_client *client,
957 				cras_node_id_t node_id, uint8_t volume);
958 
959 /* Swap the left and right channel of the given node.
960  *
961  * Args:
962  *    client - The client from cras_client_create.
963  *    node_id - ID of the node.
964  *    enable - 1 to enable swap mode, 0 to disable.
965  */
966 int cras_client_swap_node_left_right(struct cras_client *client,
967 				     cras_node_id_t node_id, int enable);
968 
969 /* Set the capture gain of the given input node.  Only for input nodes.
970  *
971  * Args:
972  *    client - The client from cras_client_create.
973  *    node_id - ID of the node.
974  *    gain - New capture gain for the node, in range (0, 100) which will
975  *        linearly maps to (-4000, 4000) 100*dBFS.
976  */
977 int cras_client_set_node_capture_gain(struct cras_client *client,
978 				      cras_node_id_t node_id, long gain);
979 
980 /* Add a test iodev to the iodev list.
981  *
982  * Args:
983  *    client - The client from cras_client_create.
984  *    type - The type of test iodev, see cras_types.h
985  */
986 int cras_client_add_test_iodev(struct cras_client *client,
987 			       enum TEST_IODEV_TYPE type);
988 
989 /* Send a test command to a test iodev.
990  *
991  * Args:
992  *    client - The client from cras_client_create.
993  *    iodev_idx - The index of the test iodev.
994  *    command - The command to send.
995  *    data_len - Length of command data.
996  *    data - Command data.
997  */
998 int cras_client_test_iodev_command(struct cras_client *client,
999 				   unsigned int iodev_idx,
1000 				   enum CRAS_TEST_IODEV_CMD command,
1001 				   unsigned int data_len, const uint8_t *data);
1002 
1003 /* Finds the first node of the given type.
1004  *
1005  * This is used for finding a special hotword node.
1006  *
1007  * Requires that the connection to the server has been established.
1008  *
1009  * Args:
1010  *    client - The client from cras_client_create.
1011  *    type - The type of device to find.
1012  *    direction - Search input or output devices.
1013  *    node_id - The found node on success.
1014  * Returns:
1015  *    0 on success, a negative error on failure.
1016  */
1017 int cras_client_get_first_node_type_idx(const struct cras_client *client,
1018 					enum CRAS_NODE_TYPE type,
1019 					enum CRAS_STREAM_DIRECTION direction,
1020 					cras_node_id_t *node_id);
1021 
1022 /* Finds the first device that contains a node of the given type.
1023  *
1024  * This is used for finding a special hotword device.
1025  *
1026  * Requires that the connection to the server has been established.
1027  *
1028  * Args:
1029  *    client - The client from cras_client_create.
1030  *    type - The type of device to find.
1031  *    direction - Search input or output devices.
1032  * Returns the device index of a negative error on failure.
1033  */
1034 int cras_client_get_first_dev_type_idx(const struct cras_client *client,
1035 				       enum CRAS_NODE_TYPE type,
1036 				       enum CRAS_STREAM_DIRECTION direction);
1037 
1038 /* Sets the suspend state of audio playback and capture.
1039  *
1040  * Set this before putting the system into suspend.
1041  *
1042  * Args:
1043  *    client - The client from cras_client_create.
1044  *    suspend - Suspend the system if non-zero, otherwise resume.
1045  */
1046 int cras_client_set_suspend(struct cras_client *client, int suspend);
1047 
1048 /* Configures the global converter for output remixing.
1049  *
1050  * Args:
1051  *    client - The client from cras_client_create.
1052  *    num_channels - Number of output channels.
1053  *    coefficient - Float array representing |num_channels| * |num_channels|
1054  *        matrix. Channels of mixed PCM output will be remixed by
1055  *        multiplying this matrix.
1056  */
1057 int cras_client_config_global_remix(struct cras_client *client,
1058 				    unsigned num_channels, float *coefficient);
1059 
1060 /* Gets the set of supported hotword language models on a node. The supported
1061  * models may differ on different nodes.
1062  *
1063  * Args:
1064  *    client - The client from cras_client_create.
1065  *    node_id - ID of a hotword input node (CRAS_NODE_TYPE_HOTWORD).
1066  *    cb - The function to be called when hotword models are ready.
1067  * Returns:
1068  *    0 on success.
1069  */
1070 int cras_client_get_hotword_models(struct cras_client *client,
1071 				   cras_node_id_t node_id,
1072 				   get_hotword_models_cb_t cb);
1073 
1074 /* Sets the hotword language model on a node. If there are existing streams on
1075  * the hotword input node when this function is called, they need to be closed
1076  * then re-opend for the model change to take effect.
1077  * Args:
1078  *    client - The client from cras_client_create.
1079  *    node_id - ID of a hotword input node (CRAS_NODE_TYPE_HOTWORD).
1080  *    model_name - Name of the model to use, e.g. "en_us".
1081  * Returns:
1082  *    0 on success.
1083  *    -EINVAL if client or node_id is invalid.
1084  *    -ENOENT if the specified model is not found.
1085  */
1086 int cras_client_set_hotword_model(struct cras_client *client,
1087 				  cras_node_id_t node_id,
1088 				  const char *model_name);
1089 
1090 /*
1091  * Creates a hotword stream and waits for the hotword to trigger.
1092  *
1093  * Args:
1094  *    client - The client to add the stream to (from cras_client_create).
1095  *    user_data - Pointer that will be passed to the callback.
1096  *    trigger_cb - Called when a hotword is triggered.
1097  *    err_cb - Called when there is an error with the stream.
1098  *    handle_out - On success will be filled with a cras_hotword_handle.
1099  * Returns:
1100  *    0 on success, negative error code on failure (from errno.h).
1101  */
1102 int cras_client_enable_hotword_callback(
1103 	struct cras_client *client, void *user_data,
1104 	cras_hotword_trigger_cb_t trigger_cb, cras_hotword_error_cb_t err_cb,
1105 	struct cras_hotword_handle **handle_out);
1106 
1107 /*
1108  * Closes a hotword stream that was created by cras_client_wait_for_hotword.
1109  *
1110  * Args:
1111  *    client - Client to remove the stream (returned from cras_client_create).
1112  *    handle - cras_hotword_handle returned from cras_client_wait_for_hotword.
1113  * Returns:
1114  *    0 on success negative error code on failure (from errno.h).
1115  */
1116 int cras_client_disable_hotword_callback(struct cras_client *client,
1117 					 struct cras_hotword_handle *handle);
1118 
1119 /* Starts or stops the aec dump task on server side.
1120  * Args:
1121  *    client - The client from cras_client_create.
1122  *    stream_id - The id of the input stream running with aec effect.
1123  *    start - True to start APM debugging, otherwise to stop it.
1124  *    fd - File descriptor of the file to store aec dump result.
1125  */
1126 int cras_client_set_aec_dump(struct cras_client *client,
1127 			     cras_stream_id_t stream_id, int start, int fd);
1128 /*
1129  * Reloads the aec.ini config file on server side.
1130  */
1131 int cras_client_reload_aec_config(struct cras_client *client);
1132 
1133 /*
1134  * Returns if AEC is supported.
1135  */
1136 int cras_client_get_aec_supported(struct cras_client *client);
1137 
1138 /*
1139  * Returns the AEC group ID if available.
1140  */
1141 int cras_client_get_aec_group_id(struct cras_client *client);
1142 
1143 /*
1144  * Sets the flag to enable bluetooth wideband speech in server.
1145  */
1146 int cras_client_set_bt_wbs_enabled(struct cras_client *client, bool enabled);
1147 
1148 /* Set the context pointer for system state change callbacks.
1149  * Args:
1150  *    client - The client from cras_client_create.
1151  *    context - The context pointer passed to all callbacks.
1152  */
1153 void cras_client_set_state_change_callback_context(struct cras_client *client,
1154 						   void *context);
1155 
1156 /* Output volume change callback.
1157  *
1158  * Args:
1159  *    context - Context pointer set with
1160  *              cras_client_set_state_change_callback_context().
1161  *    volume - The system output volume, ranging from 0 to 100.
1162  */
1163 typedef void (*cras_client_output_volume_changed_callback)(void *context,
1164 							   int32_t volume);
1165 
1166 /* Output mute change callback.
1167  *
1168  * Args:
1169  *    context - Context pointer set with
1170  *              cras_client_set_state_change_callback_context().
1171  *    muted - Non-zero when the audio is muted, zero otherwise.
1172  *    user_muted - Non-zero when the audio has been muted by the
1173  *                 user, zero otherwise.
1174  *    mute_locked - Non-zero when the mute funcion is locked,
1175  *                  zero otherwise.
1176  */
1177 typedef void (*cras_client_output_mute_changed_callback)(void *context,
1178 							 int muted,
1179 							 int user_muted,
1180 							 int mute_locked);
1181 
1182 /* Capture gain change callback.
1183  *
1184  * Args:
1185  *    context - Context pointer set with
1186  *              cras_client_set_state_change_callback_context().
1187  *    gain - The system capture gain, in centi-decibels.
1188  */
1189 typedef void (*cras_client_capture_gain_changed_callback)(void *context,
1190 							  int32_t gain);
1191 
1192 /* Capture mute change callback.
1193  *
1194  * Args:
1195  *    context - Context pointer set with
1196  *              cras_client_set_state_change_callback_context().
1197  *    muted - Non-zero when the audio is muted, zero otherwise.
1198  *    mute_locked - Non-zero when the mute funcion is locked,
1199  *                  zero otherwise.
1200  */
1201 typedef void (*cras_client_capture_mute_changed_callback)(void *context,
1202 							  int muted,
1203 							  int mute_locked);
1204 
1205 /* Nodes change callback.
1206  *
1207  * Args:
1208  *    context - Context pointer set with
1209  *              cras_client_set_state_change_callback_context().
1210  */
1211 typedef void (*cras_client_nodes_changed_callback)(void *context);
1212 
1213 /* Active node change callback.
1214  *
1215  * Args:
1216  *    context - Context pointer set with
1217  *              cras_client_set_state_change_callback_context().
1218  *    direction - Indicates the direction of the selected node.
1219  *    node_id - The ID of the selected node. Special device ID values
1220  *              defined by CRAS_SPECIAL_DEVICE will be used when no other
1221  *              device or node is selected or between selections.
1222  */
1223 typedef void (*cras_client_active_node_changed_callback)(
1224 	void *context, enum CRAS_STREAM_DIRECTION direction,
1225 	cras_node_id_t node_id);
1226 
1227 /* Output node volume change callback.
1228  *
1229  * Args:
1230  *    context - Context pointer set with
1231  *              cras_client_set_state_change_callback_context().
1232  *    node_id - The ID of the output node.
1233  *    volume - The volume for this node with range 0 to 100.
1234  */
1235 typedef void (*cras_client_output_node_volume_changed_callback)(
1236 	void *context, cras_node_id_t node_id, int32_t volume);
1237 
1238 /* Node left right swapped change callback.
1239  *
1240  * Args:
1241  *    context - Context pointer set with
1242  *              cras_client_set_state_change_callback_context().
1243  *    node_id - The ID of the node.
1244  *    swapped - Non-zero if the node is left-right swapped, zero otherwise.
1245  */
1246 typedef void (*cras_client_node_left_right_swapped_changed_callback)(
1247 	void *context, cras_node_id_t node_id, int swapped);
1248 
1249 /* Input node gain change callback.
1250  * Args:
1251  *    context - Context pointer set with
1252  *              cras_client_set_state_change_callback_context().
1253  *    node_id - The ID of the input node.
1254  *    gain - The gain for this node in centi-decibels.
1255  */
1256 typedef void (*cras_client_input_node_gain_changed_callback)(
1257 	void *context, cras_node_id_t node_id, int32_t gain);
1258 
1259 /* Number of active streams change callback.
1260  *
1261  * Args:
1262  *    context - Context pointer set with
1263  *              cras_client_set_state_change_callback_context().
1264  *    direction - Indicates the direction of the stream's node.
1265  *    num_active_streams - The number of active streams.
1266  */
1267 typedef void (*cras_client_num_active_streams_changed_callback)(
1268 	void *context, enum CRAS_STREAM_DIRECTION direction,
1269 	uint32_t num_active_streams);
1270 
1271 /* Set system state information callbacks.
1272  * NOTE: These callbacks are executed from the client control thread.
1273  * Each state change callback is given the context pointer set with
1274  * cras_client_set_state_change_callback_context(). The context pointer is
1275  * NULL by default.
1276  * Args:
1277  *    client - The client from cras_client_create.
1278  *    cb - The callback, or NULL to disable the call-back.
1279  * Returns:
1280  *    0 for success or negative errno error code on error.
1281  */
1282 int cras_client_set_output_volume_changed_callback(
1283 	struct cras_client *client,
1284 	cras_client_output_volume_changed_callback cb);
1285 int cras_client_set_output_mute_changed_callback(
1286 	struct cras_client *client,
1287 	cras_client_output_mute_changed_callback cb);
1288 int cras_client_set_capture_gain_changed_callback(
1289 	struct cras_client *client,
1290 	cras_client_capture_gain_changed_callback cb);
1291 int cras_client_set_capture_mute_changed_callback(
1292 	struct cras_client *client,
1293 	cras_client_capture_mute_changed_callback cb);
1294 int cras_client_set_nodes_changed_callback(
1295 	struct cras_client *client, cras_client_nodes_changed_callback cb);
1296 int cras_client_set_active_node_changed_callback(
1297 	struct cras_client *client,
1298 	cras_client_active_node_changed_callback cb);
1299 int cras_client_set_output_node_volume_changed_callback(
1300 	struct cras_client *client,
1301 	cras_client_output_node_volume_changed_callback cb);
1302 int cras_client_set_node_left_right_swapped_changed_callback(
1303 	struct cras_client *client,
1304 	cras_client_node_left_right_swapped_changed_callback cb);
1305 int cras_client_set_input_node_gain_changed_callback(
1306 	struct cras_client *client,
1307 	cras_client_input_node_gain_changed_callback cb);
1308 int cras_client_set_num_active_streams_changed_callback(
1309 	struct cras_client *client,
1310 	cras_client_num_active_streams_changed_callback cb);
1311 
1312 /*
1313  * The functions below prefixed with libcras wrap the original CRAS library
1314  * They provide an interface that maps the pointers to the functions above.
1315  * Please add a new function instead of modifying the existing function.
1316  * Here are some rules about how to add a new function:
1317  * 1. Increase the CRAS_API_VERSION by 1.
1318  * 2. Write a new function in cras_client.c.
1319  * 3. Append the corresponding pointer to the structure. Remeber DO NOT change
1320  *    the order of functions in the structs.
1321  * 4. Assign the pointer to the new function in cras_client.c.
1322  * 5. Create the inline function in cras_client.h, which is used by clients.
1323  *    Remember to add DISABLE_CFI_ICALL on the inline function.
1324  * 6. Add CHECK_VERSION in the inline function. If the api_version is smaller
1325  *    than the supported version, this inline function will return -ENOSYS.
1326  */
1327 
1328 #define CRAS_API_VERSION 1
1329 #define CHECK_VERSION(object, version)                                         \
1330 	if (object->api_version < version) {                                   \
1331 		return -ENOSYS;                                                \
1332 	}
1333 
1334 /*
1335  * The inline functions use the indirect function call. Therefore, they are
1336  * incompatible with CFI-icall.
1337  */
1338 #define DISABLE_CFI_ICALL __attribute__((no_sanitize("cfi-icall")))
1339 
1340 struct libcras_node_info {
1341 	int api_version;
1342 	struct cras_node_info *node_;
1343 	int (*get_id)(struct cras_node_info *node, uint64_t *id);
1344 	int (*get_dev_idx)(struct cras_node_info *node, uint32_t *dev_idx);
1345 	int (*get_node_idx)(struct cras_node_info *node, uint32_t *node_idx);
1346 	int (*get_max_supported_channels)(struct cras_node_info *node,
1347 					  uint32_t *max_supported_channels);
1348 	int (*is_plugged)(struct cras_node_info *node, bool *plugged);
1349 	int (*is_active)(struct cras_node_info *node, bool *active);
1350 	int (*get_type)(struct cras_node_info *node, char **name);
1351 	int (*get_node_name)(struct cras_node_info *node, char **name);
1352 	int (*get_dev_name)(struct cras_node_info *node, char **name);
1353 };
1354 
1355 struct libcras_client {
1356 	int api_version;
1357 	struct cras_client *client_;
1358 	int (*connect)(struct cras_client *client);
1359 	int (*connect_timeout)(struct cras_client *client,
1360 			       unsigned int timeout_ms);
1361 	int (*connected_wait)(struct cras_client *client);
1362 	int (*run_thread)(struct cras_client *client);
1363 	int (*stop)(struct cras_client *client);
1364 	int (*add_pinned_stream)(struct cras_client *client, uint32_t dev_idx,
1365 				 cras_stream_id_t *stream_id_out,
1366 				 struct cras_stream_params *config);
1367 	int (*rm_stream)(struct cras_client *client,
1368 			 cras_stream_id_t stream_id);
1369 	int (*set_stream_volume)(struct cras_client *client,
1370 				 cras_stream_id_t stream_id,
1371 				 float volume_scaler);
1372 	int (*get_nodes)(struct cras_client *client,
1373 			 enum CRAS_STREAM_DIRECTION direction,
1374 			 struct libcras_node_info ***nodes, size_t *num);
1375 	int (*get_default_output_buffer_size)(struct cras_client *client,
1376 					      int *size);
1377 	int (*get_aec_group_id)(struct cras_client *client, int *id);
1378 	int (*get_aec_supported)(struct cras_client *client, int *supported);
1379 	int (*get_system_muted)(struct cras_client *client, int *muted);
1380 	int (*set_system_mute)(struct cras_client *client, int mute);
1381 	int (*get_loopback_dev_idx)(struct cras_client *client, int *idx);
1382 };
1383 
1384 struct cras_stream_cb_data;
1385 struct libcras_stream_cb_data {
1386 	int api_version;
1387 	struct cras_stream_cb_data *data_;
1388 	int (*get_stream_id)(struct cras_stream_cb_data *data,
1389 			     cras_stream_id_t *id);
1390 	int (*get_buf)(struct cras_stream_cb_data *data, uint8_t **buf);
1391 	int (*get_frames)(struct cras_stream_cb_data *data,
1392 			  unsigned int *frames);
1393 	int (*get_latency)(struct cras_stream_cb_data *data,
1394 			   struct timespec *latency);
1395 	int (*get_user_arg)(struct cras_stream_cb_data *data, void **user_arg);
1396 };
1397 typedef int (*libcras_stream_cb_t)(struct libcras_stream_cb_data *data);
1398 
1399 struct libcras_stream_params {
1400 	int api_version;
1401 	struct cras_stream_params *params_;
1402 	int (*set)(struct cras_stream_params *params,
1403 		   enum CRAS_STREAM_DIRECTION direction, size_t buffer_frames,
1404 		   size_t cb_threshold, enum CRAS_STREAM_TYPE stream_type,
1405 		   enum CRAS_CLIENT_TYPE client_type, uint32_t flags,
1406 		   void *user_data, libcras_stream_cb_t stream_cb,
1407 		   cras_error_cb_t err_cb, size_t rate, snd_pcm_format_t format,
1408 		   size_t num_channels);
1409 	int (*set_channel_layout)(struct cras_stream_params *params, int length,
1410 				  const int8_t *layout);
1411 	void (*enable_aec)(struct cras_stream_params *params);
1412 };
1413 
1414 /*
1415  * Creates a new client.
1416  * Returns:
1417  *    If success, return a valid libcras_client pointer. Otherwise, return
1418  *    NULL.
1419  */
1420 struct libcras_client *libcras_client_create();
1421 
1422 /*
1423  * Destroys a client.
1424  * Args:
1425  *    client - pointer returned from "libcras_client_create".
1426  */
1427 void libcras_client_destroy(struct libcras_client *client);
1428 
1429 /*
1430  * Connects a client to the running server.
1431  * Waits forever (until interrupted or connected).
1432  * Args:
1433  *    client - pointer returned from "libcras_client_create".
1434  * Returns:
1435  *    0 on success, or a negative error code on failure (from errno.h).
1436  */
1437 DISABLE_CFI_ICALL
libcras_client_connect(struct libcras_client * client)1438 inline int libcras_client_connect(struct libcras_client *client)
1439 {
1440 	return client->connect(client->client_);
1441 }
1442 
1443 /*
1444  * Connects a client to the running server, retries until timeout.
1445  * Args:
1446  *    client - pointer returned from "libcras_client_create".
1447  *    timeout_ms - timeout in milliseconds or negative to wait forever.
1448  * Returns:
1449  *    0 on success, or a negative error code on failure (from errno.h).
1450  */
1451 DISABLE_CFI_ICALL
libcras_client_connect_timeout(struct libcras_client * client,unsigned int timeout_ms)1452 inline int libcras_client_connect_timeout(struct libcras_client *client,
1453 					  unsigned int timeout_ms)
1454 {
1455 	return client->connect_timeout(client->client_, timeout_ms);
1456 }
1457 
1458 /*
1459  * Wait up to 1 second for the client thread to complete the server connection.
1460  *
1461  * After libcras_client_run_thread() is executed, this function can be
1462  * used to ensure that the connection has been established with the server and
1463  * ensure that any information about the server is up to date. If
1464  * libcras_client_run_thread() has not yet been executed, or
1465  * libcras_client_stop() was executed and thread isn't running, then this
1466  * function returns -EINVAL.
1467  *
1468  * Args:
1469  *    client - pointer returned from "libcras_client_create".
1470  * Returns:
1471  *    0 on success, or a negative error code on failure (from errno.h).
1472  */
1473 DISABLE_CFI_ICALL
libcras_client_connected_wait(struct libcras_client * client)1474 inline int libcras_client_connected_wait(struct libcras_client *client)
1475 {
1476 	return client->connected_wait(client->client_);
1477 }
1478 
1479 /*
1480  * Begins running the client control thread.
1481  *
1482  * Required for stream operations and other operations noted below.
1483  *
1484  * Args:
1485  *    client - pointer returned from "libcras_client_create".
1486  * Returns:
1487  *    0 on success, or a negative error code on failure (from errno.h).
1488  */
1489 DISABLE_CFI_ICALL
libcras_client_run_thread(struct libcras_client * client)1490 inline int libcras_client_run_thread(struct libcras_client *client)
1491 {
1492 	return client->run_thread(client->client_);
1493 }
1494 
1495 /*
1496  * Stops running a client.
1497  * This function is executed automatically by cras_client_destroy().
1498  * Args:
1499  *    client - pointer returned from "libcras_client_create".
1500  * Returns:
1501  *    0 on success or if the thread was already stopped, -EINVAL if the client
1502  *    isn't valid.
1503  */
1504 DISABLE_CFI_ICALL
libcras_client_stop(struct libcras_client * client)1505 inline int libcras_client_stop(struct libcras_client *client)
1506 {
1507 	return client->stop(client->client_);
1508 }
1509 
1510 /*
1511  * Creates a pinned stream and return the stream id or < 0 on error.
1512  *
1513  * Requires execution of libcras_client_run_thread(), and an active
1514  * connection to the audio server.
1515  *
1516  * Args:
1517  *    client - pointer returned from "libcras_client_create".
1518  *    dev_idx - Index of the device to attach the newly created stream.
1519  *    stream_id_out - On success will be filled with the new stream id.
1520  *        Guaranteed to be set before any callbacks are made.
1521  *    params - The pointer specifying the parameters for the stream.
1522  *        (returned from libcras_stream_params_create)
1523  * Returns:
1524  *    0 on success, negative error code on failure (from errno.h).
1525  */
1526 DISABLE_CFI_ICALL
libcras_client_add_pinned_stream(struct libcras_client * client,uint32_t dev_idx,cras_stream_id_t * stream_id_out,struct libcras_stream_params * params)1527 inline int libcras_client_add_pinned_stream(
1528 	struct libcras_client *client, uint32_t dev_idx,
1529 	cras_stream_id_t *stream_id_out, struct libcras_stream_params *params)
1530 {
1531 	return client->add_pinned_stream(client->client_, dev_idx,
1532 					 stream_id_out, params->params_);
1533 }
1534 
1535 /*
1536  * Removes a currently playing/capturing stream.
1537  *
1538  * Requires execution of libcras_client_run_thread().
1539  *
1540  * Args:
1541  *    client - pointer returned from "libcras_client_create".
1542  *    stream_id - ID returned from libcras_client_add_stream to identify
1543  *        the stream to remove.
1544  * Returns:
1545  *    0 on success negative error code on failure (from errno.h).
1546  */
1547 DISABLE_CFI_ICALL
libcras_client_rm_stream(struct libcras_client * client,cras_stream_id_t stream_id)1548 inline int libcras_client_rm_stream(struct libcras_client *client,
1549 				    cras_stream_id_t stream_id)
1550 {
1551 	return client->rm_stream(client->client_, stream_id);
1552 }
1553 
1554 /*
1555  * Sets the volume scaling factor for the given stream.
1556  *
1557  * Requires execution of cras_client_run_thread().
1558  *
1559  * Args:
1560  *    client - pointer returned from "libcras_client_create".
1561  *    stream_id - ID returned from libcras_client_add_stream.
1562  *    volume_scaler - 0.0-1.0 the new value to scale this stream by.
1563  * Returns:
1564  *    0 on success negative error code on failure (from errno.h).
1565  */
1566 DISABLE_CFI_ICALL
libcras_client_set_stream_volume(struct libcras_client * client,cras_stream_id_t stream_id,float volume_scaler)1567 inline int libcras_client_set_stream_volume(struct libcras_client *client,
1568 					    cras_stream_id_t stream_id,
1569 					    float volume_scaler)
1570 {
1571 	return client->set_stream_volume(client->client_, stream_id,
1572 					 volume_scaler);
1573 }
1574 
1575 /*
1576  * Gets the current list of audio nodes.
1577  *
1578  * Args:
1579  *    client - Pointer returned from "libcras_client_create".
1580  *    direction - Input or output.
1581  *    nodes - Array that will be filled with libcras_node_info pointers.
1582  *    num - Pointer to store the size of the array.
1583  * Returns:
1584  *    0 on success negative error code on failure (from errno.h).
1585  *    Remember to call libcras_node_info_array_destroy to free the array.
1586  */
1587 DISABLE_CFI_ICALL
libcras_client_get_nodes(struct libcras_client * client,enum CRAS_STREAM_DIRECTION direction,struct libcras_node_info *** nodes,size_t * num)1588 inline int libcras_client_get_nodes(struct libcras_client *client,
1589 				    enum CRAS_STREAM_DIRECTION direction,
1590 				    struct libcras_node_info ***nodes,
1591 				    size_t *num)
1592 {
1593 	return client->get_nodes(client->client_, direction, nodes, num);
1594 }
1595 
1596 /*
1597  * Gets the default output buffer size.
1598  * Args:
1599  *    client - Pointer returned from "libcras_client_create".
1600  *    size - The pointer to save the result.
1601  * Returns:
1602  *    0 on success negative error code on failure (from errno.h).
1603  */
1604 DISABLE_CFI_ICALL
1605 inline int
libcras_client_get_default_output_buffer_size(struct libcras_client * client,int * size)1606 libcras_client_get_default_output_buffer_size(struct libcras_client *client,
1607 					      int *size)
1608 {
1609 	return client->get_default_output_buffer_size(client->client_, size);
1610 }
1611 
1612 /*
1613  * Gets the AEC group ID.
1614  * Args:
1615  *    client - Pointer returned from "libcras_client_create".
1616  *    id - The pointer to save the result.
1617  * Returns:
1618  *    0 on success negative error code on failure (from errno.h).
1619  */
1620 DISABLE_CFI_ICALL
libcras_client_get_aec_group_id(struct libcras_client * client,int * id)1621 inline int libcras_client_get_aec_group_id(struct libcras_client *client,
1622 					   int *id)
1623 {
1624 	return client->get_aec_group_id(client->client_, id);
1625 }
1626 
1627 /*
1628  * Gets whether AEC is supported.
1629  * Args:
1630  *    client - Pointer returned from "libcras_client_create".
1631  *    supported - The pointer to save the result.
1632  * Returns:
1633  *    0 on success negative error code on failure (from errno.h).
1634  */
1635 DISABLE_CFI_ICALL
libcras_client_get_aec_supported(struct libcras_client * client,int * supported)1636 inline int libcras_client_get_aec_supported(struct libcras_client *client,
1637 					    int *supported)
1638 {
1639 	return client->get_aec_supported(client->client_, supported);
1640 }
1641 
1642 /*
1643  * Gets whether the system is muted.
1644  * Args:
1645  *    client - Pointer returned from "libcras_client_create".
1646  *    muted - The pointer to save the result.
1647  * Returns:
1648  *    0 on success negative error code on failure (from errno.h).
1649  */
1650 DISABLE_CFI_ICALL
libcras_client_get_system_muted(struct libcras_client * client,int * muted)1651 inline int libcras_client_get_system_muted(struct libcras_client *client,
1652 					   int *muted)
1653 {
1654 	return client->get_aec_group_id(client->client_, muted);
1655 }
1656 
1657 /*
1658  * Mutes or unmutes the system.
1659  * Args:
1660  *    client - Pointer returned from "libcras_client_create".
1661  *    mute - 1 is to mute and 0 is to unmute.
1662  * Returns:
1663  *    0 on success negative error code on failure (from errno.h).
1664  */
1665 DISABLE_CFI_ICALL
libcras_client_set_system_mute(struct libcras_client * client,int mute)1666 inline int libcras_client_set_system_mute(struct libcras_client *client,
1667 					  int mute)
1668 {
1669 	return client->set_system_mute(client->client_, mute);
1670 }
1671 
1672 /*
1673  * Gets the index of the loopback device.
1674  * Args:
1675  *    client - Pointer returned from "libcras_client_create".
1676  *    idx - The pointer to save the result.
1677  * Returns:
1678  *    0 on success negative error code on failure (from errno.h).
1679  */
1680 DISABLE_CFI_ICALL
libcras_client_get_loopback_dev_idx(struct libcras_client * client,int * idx)1681 inline int libcras_client_get_loopback_dev_idx(struct libcras_client *client,
1682 					       int *idx)
1683 {
1684 	return client->get_loopback_dev_idx(client->client_, idx);
1685 }
1686 
1687 /*
1688  * Creates a new struct to save stream params.
1689  * Returns:
1690  *    If success, return a valid libcras_stream_params pointer. Otherwise,
1691  *    return NULL.
1692  */
1693 struct libcras_stream_params *libcras_stream_params_create();
1694 
1695 /*
1696  * Destroys a stream params instance.
1697  * Args:
1698  *    params - The pointer returned from libcras_stream_params_create.
1699  */
1700 void libcras_stream_params_destroy(struct libcras_stream_params *params);
1701 
1702 /*
1703  * Setup stream configuration parameters.
1704  * Args:
1705  *    params - The pointer returned from libcras_stream_params_create.
1706  *    direction - Playback(CRAS_STREAM_OUTPUT) or capture(CRAS_STREAM_INPUT).
1707  *    buffer_frames - total number of audio frames to buffer (dictates latency).
1708  *    cb_threshold - For playback, call back for more data when the buffer
1709  *        reaches this level. For capture, this is ignored (Audio callback will
1710  *        be called when buffer_frames have been captured).
1711  *    stream_type - Media or talk (currently only support "default").
1712  *    client_type - The client type, like Chrome or CrOSVM.
1713  *    flags - Currently only used for CRAS_INPUT_STREAM_FLAG.
1714  *    user_data - Pointer that will be passed to the callback.
1715  *    stream_cb - The audio callback. Called when audio is needed(playback) or
1716  *        ready(capture).
1717  *    err_cb - Called when there is an error with the stream.
1718  *    rate - The sample rate of the audio stream.
1719  *    format - The format of the audio stream.
1720  *    num_channels - The number of channels of the audio stream.
1721  * Returns:
1722  *    0 on success negative error code on failure (from errno.h).
1723  */
1724 DISABLE_CFI_ICALL
libcras_stream_params_set(struct libcras_stream_params * params,enum CRAS_STREAM_DIRECTION direction,size_t buffer_frames,size_t cb_threshold,enum CRAS_STREAM_TYPE stream_type,enum CRAS_CLIENT_TYPE client_type,uint32_t flags,void * user_data,libcras_stream_cb_t stream_cb,cras_error_cb_t err_cb,size_t rate,snd_pcm_format_t format,size_t num_channels)1725 inline int libcras_stream_params_set(
1726 	struct libcras_stream_params *params,
1727 	enum CRAS_STREAM_DIRECTION direction, size_t buffer_frames,
1728 	size_t cb_threshold, enum CRAS_STREAM_TYPE stream_type,
1729 	enum CRAS_CLIENT_TYPE client_type, uint32_t flags, void *user_data,
1730 	libcras_stream_cb_t stream_cb, cras_error_cb_t err_cb, size_t rate,
1731 	snd_pcm_format_t format, size_t num_channels)
1732 {
1733 	return params->set(params->params_, direction, buffer_frames,
1734 			   cb_threshold, stream_type, client_type, flags,
1735 			   user_data, stream_cb, err_cb, rate, format,
1736 			   num_channels);
1737 }
1738 
1739 /*
1740  * Sets channel layout on given stream parameter.
1741  * Args:
1742  *    params - The pointer returned from libcras_stream_params_create.
1743  *    length - The length of the array.
1744  *    layout - An integer array representing the position of each channel in
1745  *    enum CRAS_CHANNEL.
1746  * Returns:
1747  *    0 on success negative error code on failure (from errno.h).
1748  */
1749 DISABLE_CFI_ICALL
1750 inline int
libcras_stream_params_set_channel_layout(struct libcras_stream_params * params,int length,const int8_t * layout)1751 libcras_stream_params_set_channel_layout(struct libcras_stream_params *params,
1752 					 int length, const int8_t *layout)
1753 {
1754 	return params->set_channel_layout(params->params_, length, layout);
1755 }
1756 
1757 /*
1758  * Enables AEC on given stream parameter.
1759  * Args:
1760  *    params - The pointer returned from libcras_stream_params_create.
1761  * Returns:
1762  *    0 on success negative error code on failure (from errno.h).
1763  */
1764 DISABLE_CFI_ICALL
1765 inline int
libcras_stream_params_enable_aec(struct libcras_stream_params * params)1766 libcras_stream_params_enable_aec(struct libcras_stream_params *params)
1767 {
1768 	params->enable_aec(params->params_);
1769 	return 0;
1770 }
1771 
1772 /*
1773  * Gets stream id from the callback data.
1774  * Args:
1775  *    data - The pointer passed to the callback function.
1776  *    id - The pointer to save the stream id.
1777  * Returns:
1778  *    0 on success negative error code on failure (from errno.h).
1779  */
1780 DISABLE_CFI_ICALL
1781 inline int
libcras_stream_cb_data_get_stream_id(struct libcras_stream_cb_data * data,cras_stream_id_t * id)1782 libcras_stream_cb_data_get_stream_id(struct libcras_stream_cb_data *data,
1783 				     cras_stream_id_t *id)
1784 {
1785 	return data->get_stream_id(data->data_, id);
1786 }
1787 
1788 /*
1789  * Gets stream buf from the callback data.
1790  * Args:
1791  *    data - The pointer passed to the callback function.
1792  *    buf - The pointer to save the stream buffer.
1793  * Returns:
1794  *    0 on success negative error code on failure (from errno.h).
1795  */
1796 DISABLE_CFI_ICALL
libcras_stream_cb_data_get_buf(struct libcras_stream_cb_data * data,uint8_t ** buf)1797 inline int libcras_stream_cb_data_get_buf(struct libcras_stream_cb_data *data,
1798 					  uint8_t **buf)
1799 {
1800 	return data->get_buf(data->data_, buf);
1801 }
1802 
1803 /*
1804  * Gets how many frames to read or play from the callback data.
1805  * Args:
1806  *    data - The pointer passed to the callback function.
1807  *    frames - The pointer to save the number of frames.
1808  * Returns:
1809  *    0 on success negative error code on failure (from errno.h).
1810  */
1811 DISABLE_CFI_ICALL
1812 inline int
libcras_stream_cb_data_get_frames(struct libcras_stream_cb_data * data,unsigned int * frames)1813 libcras_stream_cb_data_get_frames(struct libcras_stream_cb_data *data,
1814 				  unsigned int *frames)
1815 {
1816 	return data->get_frames(data->data_, frames);
1817 }
1818 
1819 /*
1820  * Gets the latency from the callback data.
1821  * Args:
1822  *    data - The pointer passed to the callback function.
1823  *    frames - The timespec pointer to save the latency.
1824  * Returns:
1825  *    0 on success negative error code on failure (from errno.h).
1826  */
1827 DISABLE_CFI_ICALL
1828 inline int
libcras_stream_cb_data_get_latency(struct libcras_stream_cb_data * data,struct timespec * latency)1829 libcras_stream_cb_data_get_latency(struct libcras_stream_cb_data *data,
1830 				   struct timespec *latency)
1831 {
1832 	return data->get_latency(data->data_, latency);
1833 }
1834 
1835 /*
1836  * Gets the user data from the callback data.
1837  * Args:
1838  *    data - The pointer passed to the callback function.
1839  *    frames - The pointer to save the user data.
1840  * Returns:
1841  *    0 on success negative error code on failure (from errno.h).
1842  */
1843 DISABLE_CFI_ICALL
1844 inline int
libcras_stream_cb_data_get_usr_arg(struct libcras_stream_cb_data * data,void ** user_arg)1845 libcras_stream_cb_data_get_usr_arg(struct libcras_stream_cb_data *data,
1846 				   void **user_arg)
1847 {
1848 	return data->get_user_arg(data->data_, user_arg);
1849 }
1850 
1851 /*
1852  * Destroys a node info instance.
1853  * Args:
1854  *    node - The libcras_node_info pointer to destroy.
1855  */
1856 void libcras_node_info_destroy(struct libcras_node_info *node);
1857 
1858 /*
1859  * Destroys a node info array.
1860  * Args:
1861  *    nodes - The libcras_node_info pointer array to destroy.
1862  *    num - The size of the array.
1863  */
1864 void libcras_node_info_array_destroy(struct libcras_node_info **nodes,
1865 				     size_t num);
1866 
1867 /*
1868  * Gets ID from the node info pointer.
1869  * Args:
1870  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1871  *    id - The pointer to save ID.
1872  * Returns:
1873  *    0 on success negative error code on failure (from errno.h).
1874  */
1875 DISABLE_CFI_ICALL
libcras_node_info_get_id(struct libcras_node_info * node,uint64_t * id)1876 inline int libcras_node_info_get_id(struct libcras_node_info *node,
1877 				    uint64_t *id)
1878 {
1879 	return node->get_id(node->node_, id);
1880 }
1881 
1882 /*
1883  * Gets device index from the node info pointer.
1884  * Args:
1885  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1886  *    dev_idx - The pointer to the device index.
1887  * Returns:
1888  *    0 on success negative error code on failure (from errno.h).
1889  */
1890 DISABLE_CFI_ICALL
libcras_node_info_get_dev_idx(struct libcras_node_info * node,uint32_t * dev_idx)1891 inline int libcras_node_info_get_dev_idx(struct libcras_node_info *node,
1892 					 uint32_t *dev_idx)
1893 {
1894 	return node->get_dev_idx(node->node_, dev_idx);
1895 }
1896 
1897 /*
1898  * Gets node index from the node info pointer.
1899  * Args:
1900  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1901  *    node_idx - The pointer to save the node index.
1902  * Returns:
1903  *    0 on success negative error code on failure (from errno.h).
1904  */
1905 DISABLE_CFI_ICALL
libcras_node_info_get_node_idx(struct libcras_node_info * node,uint32_t * node_idx)1906 inline int libcras_node_info_get_node_idx(struct libcras_node_info *node,
1907 					  uint32_t *node_idx)
1908 {
1909 	return node->get_node_idx(node->node_, node_idx);
1910 }
1911 
1912 /*
1913  * Gets the max supported channels from the node info pointer.
1914  * Args:
1915  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1916  *    max_supported_channels - The pointer to save the result.
1917  * Returns:
1918  *    0 on success negative error code on failure (from errno.h).
1919  */
1920 DISABLE_CFI_ICALL
1921 inline int
libcras_node_info_get_max_supported_channels(struct libcras_node_info * node,uint32_t * max_supported_channels)1922 libcras_node_info_get_max_supported_channels(struct libcras_node_info *node,
1923 					     uint32_t *max_supported_channels)
1924 {
1925 	return node->get_max_supported_channels(node->node_,
1926 						max_supported_channels);
1927 }
1928 
1929 /*
1930  * Gets whether the node is plugged from the node info pointer.
1931  * Args:
1932  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1933  *    plugged - The pointer to save the result.
1934  * Returns:
1935  *    0 on success negative error code on failure (from errno.h).
1936  */
1937 DISABLE_CFI_ICALL
libcras_node_info_is_plugged(struct libcras_node_info * node,bool * plugged)1938 inline int libcras_node_info_is_plugged(struct libcras_node_info *node,
1939 					bool *plugged)
1940 {
1941 	return node->is_plugged(node->node_, plugged);
1942 }
1943 
1944 /*
1945  * Gets whether the node is active from the node info pointer.
1946  * Args:
1947  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1948  *    active - The pointer to save the result.
1949  * Returns:
1950  *    0 on success negative error code on failure (from errno.h).
1951  */
1952 DISABLE_CFI_ICALL
libcras_node_info_is_active(struct libcras_node_info * node,bool * active)1953 inline int libcras_node_info_is_active(struct libcras_node_info *node,
1954 				       bool *active)
1955 {
1956 	return node->is_active(node->node_, active);
1957 }
1958 
1959 /*
1960  * Gets device type from the node info pointer.
1961  * Args:
1962  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1963  *    type - The pointer to save the device type.
1964  * Returns:
1965  *    0 on success negative error code on failure (from errno.h).
1966  */
1967 DISABLE_CFI_ICALL
libcras_node_info_get_type(struct libcras_node_info * node,char ** type)1968 inline int libcras_node_info_get_type(struct libcras_node_info *node,
1969 				      char **type)
1970 {
1971 	return node->get_type(node->node_, type);
1972 }
1973 
1974 /*
1975  * Gets device name from the node info pointer.
1976  * Args:
1977  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1978  *    name - The pointer to save the device name.
1979  * Returns:
1980  *    0 on success negative error code on failure (from errno.h).
1981  */
1982 DISABLE_CFI_ICALL
libcras_node_info_get_node_name(struct libcras_node_info * node,char ** name)1983 inline int libcras_node_info_get_node_name(struct libcras_node_info *node,
1984 					   char **name)
1985 {
1986 	return node->get_node_name(node->node_, name);
1987 }
1988 
1989 /*
1990  * Gets node name from the node info pointer.
1991  * Args:
1992  *    node - The node info pointer. (Returned from libcras_client_get_nodes)
1993  *    name - The pointer to save the node name.
1994  * Returns:
1995  *    0 on success negative error code on failure (from errno.h).
1996  */
1997 DISABLE_CFI_ICALL
libcras_node_info_get_dev_name(struct libcras_node_info * node,char ** name)1998 inline int libcras_node_info_get_dev_name(struct libcras_node_info *node,
1999 					  char **name)
2000 {
2001 	return node->get_dev_name(node->node_, name);
2002 }
2003 
2004 #ifdef __cplusplus
2005 }
2006 #endif
2007 
2008 #endif /* CRAS_CLIENT_H_ */
2009