• 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 
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_unified_rclient.h"
28 #include "cras_util.h"
29 #include "stream_list.h"
30 #include "utlist.h"
31 
32 /* Removes all streams that the client owns and destroys it. */
cras_rclient_destroy(struct cras_rclient * client)33 void cras_rclient_destroy(struct cras_rclient *client)
34 {
35 	client->ops->destroy(client);
36 }
37 
38 /* Entry point for handling a message from the client.  Called from the main
39  * server context. */
cras_rclient_buffer_from_client(struct cras_rclient * client,const uint8_t * buf,size_t buf_len,int * fds,int num_fds)40 int cras_rclient_buffer_from_client(struct cras_rclient *client,
41 				    const uint8_t *buf, size_t buf_len,
42 				    int *fds, int num_fds)
43 {
44 	struct cras_server_message *msg = (struct cras_server_message *)buf;
45 
46 	if (buf_len < sizeof(*msg))
47 		return -EINVAL;
48 	if (msg->length != buf_len)
49 		return -EINVAL;
50 	return client->ops->handle_message_from_client(client, msg, fds,
51 						       num_fds);
52 }
53 
54 /* 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)55 int cras_rclient_send_message(const struct cras_rclient *client,
56 			      const struct cras_client_message *msg, int *fds,
57 			      unsigned int num_fds)
58 {
59 	return client->ops->send_message_to_client(client, msg, fds, num_fds);
60 }
61 
cras_rclient_set_client_type(struct cras_rclient * client,enum CRAS_CLIENT_TYPE client_type)62 static void cras_rclient_set_client_type(struct cras_rclient *client,
63 					 enum CRAS_CLIENT_TYPE client_type)
64 {
65 	client->client_type = client_type;
66 }
67 
cras_rclient_create(int fd,size_t id,enum CRAS_CONNECTION_TYPE conn_type)68 struct cras_rclient *cras_rclient_create(int fd, size_t id,
69 					 enum CRAS_CONNECTION_TYPE conn_type)
70 {
71 	struct cras_rclient *client;
72 	if (!cras_validate_connection_type(conn_type))
73 		goto error;
74 
75 	switch (conn_type) {
76 	case CRAS_CONTROL:
77 		return cras_control_rclient_create(fd, id);
78 	case CRAS_PLAYBACK:
79 		return cras_playback_rclient_create(fd, id);
80 	case CRAS_CAPTURE:
81 		return cras_capture_rclient_create(fd, id);
82 	case CRAS_VMS_LEGACY:
83 		return cras_playback_rclient_create(fd, id);
84 	case CRAS_VMS_UNIFIED:
85 		return cras_unified_rclient_create(fd, id);
86 	case CRAS_PLUGIN_PLAYBACK:
87 		client = cras_playback_rclient_create(fd, id);
88 		cras_rclient_set_client_type(client, CRAS_CLIENT_TYPE_PLUGIN);
89 		return client;
90 	case CRAS_PLUGIN_UNIFIED:
91 		client = cras_unified_rclient_create(fd, id);
92 		cras_rclient_set_client_type(client, CRAS_CLIENT_TYPE_PLUGIN);
93 		return client;
94 	default:
95 		goto error;
96 	}
97 
98 error:
99 	syslog(LOG_ERR, "unsupported connection type");
100 	return NULL;
101 }
102