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
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <syslog.h>
9
10 #include "audio_thread.h"
11 #include "cras_apm_list.h"
12 #include "cras_bt_log.h"
13 #include "cras_capture_rclient.h"
14 #include "cras_config.h"
15 #include "cras_control_rclient.h"
16 #include "cras_dsp.h"
17 #include "cras_iodev.h"
18 #include "cras_iodev_list.h"
19 #include "cras_messages.h"
20 #include "cras_observer.h"
21 #include "cras_playback_rclient.h"
22 #include "cras_rclient.h"
23 #include "cras_rstream.h"
24 #include "cras_server_metrics.h"
25 #include "cras_system_state.h"
26 #include "cras_types.h"
27 #include "cras_util.h"
28 #include "stream_list.h"
29 #include "utlist.h"
30
31 /* Removes all streams that the client owns and destroys it. */
cras_rclient_destroy(struct cras_rclient * client)32 void cras_rclient_destroy(struct cras_rclient *client)
33 {
34 client->ops->destroy(client);
35 }
36
37 /* Entry point for handling a message from the client. Called from the main
38 * server context. */
cras_rclient_buffer_from_client(struct cras_rclient * client,const uint8_t * buf,size_t buf_len,int * fds,int num_fds)39 int cras_rclient_buffer_from_client(struct cras_rclient *client,
40 const uint8_t *buf, size_t buf_len,
41 int *fds, int num_fds)
42 {
43 struct cras_server_message *msg = (struct cras_server_message *)buf;
44
45 if (buf_len < sizeof(*msg))
46 return -EINVAL;
47 if (msg->length != buf_len)
48 return -EINVAL;
49 return client->ops->handle_message_from_client(client, msg, fds,
50 num_fds);
51 }
52
53 /* Sends a message to the client. */
cras_rclient_send_message(const struct cras_rclient * client,const struct cras_client_message * msg,int * fds,unsigned int num_fds)54 int cras_rclient_send_message(const struct cras_rclient *client,
55 const struct cras_client_message *msg, int *fds,
56 unsigned int num_fds)
57 {
58 return client->ops->send_message_to_client(client, msg, fds, num_fds);
59 }
60
cras_rclient_create(int fd,size_t id,enum CRAS_CONNECTION_TYPE conn_type)61 struct cras_rclient *cras_rclient_create(int fd, size_t id,
62 enum CRAS_CONNECTION_TYPE conn_type)
63 {
64 if (!cras_validate_connection_type(conn_type))
65 goto error;
66
67 switch (conn_type) {
68 case CRAS_CONTROL:
69 return cras_control_rclient_create(fd, id);
70 case CRAS_PLAYBACK:
71 return cras_playback_rclient_create(fd, id);
72 case CRAS_CAPTURE:
73 return cras_capture_rclient_create(fd, id);
74 default:
75 goto error;
76 }
77
78 error:
79 syslog(LOG_ERR, "unsupported connection type");
80 return NULL;
81 }
82