1 /* Copyright 2019 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 /* 7 * Common utility functions for rclients. 8 */ 9 #ifndef CRAS_RCLIENT_UTIL_H_ 10 #define CRAS_RCLIENT_UTIL_H_ 11 12 #define MSG_LEN_VALID(msg, type) ((msg)->length >= sizeof(type)) 13 14 struct cras_connect_message; 15 struct cras_rclient; 16 struct cras_rclient_message; 17 struct cras_rstream_config; 18 struct cras_server_message; 19 20 /* Sends a message to the client. */ 21 int rclient_send_message_to_client(const struct cras_rclient *client, 22 const struct cras_client_message *msg, 23 int *fds, unsigned int num_fds); 24 25 /* Removes all streams that the client owns and destroys it. */ 26 void rclient_destroy(struct cras_rclient *client); 27 28 /* Checks if the number of incoming fds matches the needs of the message from 29 * client. 30 * 31 * Args: 32 * msg - The cras_server_message from client. 33 * fds - The array for incoming fds from client. 34 * num_fds - The number of fds from client. 35 * 36 * Returns: 37 * 0 on success. Or negative value if the number of fds is invalid. 38 */ 39 int rclient_validate_message_fds(const struct cras_server_message *msg, 40 int *fds, unsigned int num_fds); 41 42 /* Checks if the incoming stream connect message contains 43 * - stream_id matches client->id. 44 * - direction supported by the client. 45 * 46 * Args: 47 * client - The cras_rclient which gets the message. 48 * msg - cras_connect_message from client. 49 * audio_fd - Audio fd from client. 50 * client_shm_fd - client shared memory fd from client. It can be -1. 51 * 52 * Returns: 53 * 0 on success, negative error on failure. 54 */ 55 int rclient_validate_stream_connect_params( 56 const struct cras_rclient *client, 57 const struct cras_connect_message *msg, int audio_fd, 58 int client_shm_fd); 59 60 /* Handles a message from the client to connect a new stream 61 * 62 * Args: 63 * client - The cras_rclient which gets the message. 64 * msg - The cras_connect_message from client. 65 * aud_fd - The audio fd comes from client. Its ownership will be taken. 66 * client_shm_fd - The client_shm_fd from client. Its ownership will be taken. 67 * 68 * Returns: 69 * 0 on success, negative error on failure. 70 */ 71 int rclient_handle_client_stream_connect(struct cras_rclient *client, 72 const struct cras_connect_message *msg, 73 int aud_fd, int client_shm_fd); 74 75 /* Handles messages from the client requesting that a stream be removed from the 76 * server. 77 * 78 * Args: 79 * client - The cras_rclient which gets the message. 80 * msg - The cras_disconnect_stream_message from client. 81 * 82 * Returns: 83 * 0 on success, negative error on failure. 84 */ 85 int rclient_handle_client_stream_disconnect( 86 struct cras_rclient *client, 87 const struct cras_disconnect_stream_message *msg); 88 89 /* Generic rclient create function for different types of rclients. 90 * Creates a client structure and sends a message back informing the client 91 * that the connection has succeeded. 92 * 93 * Args: 94 * fd - The file descriptor used for communication with the client. 95 * id - Unique identifier for this client. 96 * ops - cras_rclient_ops pointer for the client. 97 * supported_directions - supported directions for the this rclient. 98 * Returns: 99 * A pointer to the newly created rclient on success, NULL on failure. 100 */ 101 struct cras_rclient *rclient_generic_create(int fd, size_t id, 102 const struct cras_rclient_ops *ops, 103 int supported_directions); 104 105 /* Generic handle_message_from_client function for different types of rlicnets. 106 * Supports only stream connect and stream disconnect messages. 107 * 108 * If the message from clients has incorrect length (truncated message), return 109 * an error up to CRAS server. 110 * If the message from clients has invalid content, should return the errors to 111 * clients by send_message_to_client and return 0 here. 112 * 113 * Args: 114 * client - The cras_rclient which gets the message. 115 * msg - The cras_server_message from client. 116 * fds - The array for incoming fds from client. 117 * num_fds - The number of fds from client. 118 * Returns: 119 * 0 on success, negative error on failure. 120 */ 121 int rclient_handle_message_from_client(struct cras_rclient *client, 122 const struct cras_server_message *msg, 123 int *fds, unsigned int num_fds); 124 125 #endif /* CRAS_RCLIENT_UTIL_H_ */ 126