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 /* 7 * A remote client to the server. 8 */ 9 #ifndef CRAS_RCLIENT_H_ 10 #define CRAS_RCLIENT_H_ 11 12 #include "cras_types.h" 13 14 struct cras_client_message; 15 struct cras_message; 16 struct cras_server_message; 17 18 /* An attached client. 19 * id - The id of the client. 20 * fd - Connection for client communication. 21 * ops - cras_rclient_ops for the cras_rclient. 22 * supported_directions - Bit mask for supported stream directions. 23 * client_type - Client type of this rclient. If this is set to value other 24 * than CRAS_CLIENT_TYPE_UNKNOWN, rclient will overwrite incoming 25 * messages' client type. 26 */ 27 struct cras_rclient { 28 struct cras_observer_client *observer; 29 size_t id; 30 int fd; 31 const struct cras_rclient_ops *ops; 32 int supported_directions; 33 enum CRAS_CLIENT_TYPE client_type; 34 }; 35 36 /* Operations for cras_rclient. 37 * handle_message_from_client - Entry point for handling a message from the 38 * corresponded client. 39 * send_message_to_client - Method for sending message to the corresponded 40 * client. 41 * destroy - Method to destroy and free the cras_rclient. 42 */ 43 struct cras_rclient_ops { 44 int (*handle_message_from_client)(struct cras_rclient *, 45 const struct cras_server_message *, 46 int *fds, unsigned int num_fds); 47 int (*send_message_to_client)(const struct cras_rclient *, 48 const struct cras_client_message *, 49 int *fds, unsigned int num_fds); 50 void (*destroy)(struct cras_rclient *); 51 }; 52 53 /* Creates an rclient structure. 54 * Args: 55 * fd - The file descriptor used for communication with the client. 56 * id - Unique identifier for this client. 57 * conn_type - Client connection type. 58 * Returns: 59 * A pointer to the newly created rclient on success, NULL on failure. 60 */ 61 struct cras_rclient *cras_rclient_create(int fd, size_t id, 62 enum CRAS_CONNECTION_TYPE conn_type); 63 64 /* Destroys an rclient created with "cras_rclient_create". 65 * Args: 66 * client - The client to destroy. 67 */ 68 void cras_rclient_destroy(struct cras_rclient *client); 69 70 /* Handles a received buffer from the client. 71 * Args: 72 * client - The client that received this message. 73 * buf - The raw byte buffer the client sent. It should contain a valid 74 * cras_server_message. 75 * buf_len - The length of |buf|. 76 * fds - Array of valid file descriptors sent by the remote client. 77 * num_fds - Length of |fds|. 78 * Returns: 79 * 0 on success, otherwise a negative error code. 80 */ 81 int cras_rclient_buffer_from_client(struct cras_rclient *client, 82 const uint8_t *buf, size_t buf_len, 83 int *fds, int num_fds); 84 85 /* Sends a message to the client. 86 * Args: 87 * client - The client to send the message to. 88 * msg - The message to send. 89 * fds - Array of file descriptors or null 90 * num_fds - Number of entries in the fds array. 91 * Returns: 92 * number of bytes written on success, otherwise a negative error code. 93 */ 94 int cras_rclient_send_message(const struct cras_rclient *client, 95 const struct cras_client_message *msg, int *fds, 96 unsigned int num_fds); 97 98 #endif /* CRAS_RCLIENT_H_ */ 99