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