From 8405d713c2c293646723a424c218af4a72e598f2 Mon Sep 17 00:00:00 2001 From: paulhsia Date: Mon, 27 Apr 2020 17:45:16 +0800 Subject: libcras: Expose try_with_socket_type API Allow users to select different server sockets while creating the clients. * Add with_type in CrasServerSocket * Add with_type in CrasClient * Expose enum CrasSocketType for users BUG=b:155048379 TEST=Apply full patch set and test with VMs Change-Id: Ic2020d9f9e0cf1c1321772baa339934989db145f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/2167813 Tested-by: Chih-Yang Hsia Commit-Queue: Chih-Yang Hsia Reviewed-by: Dylan Reid --- cras/client/libcras/src/cras_server_socket.rs | 32 ++++++++++++++++++++++++--- cras/client/libcras/src/libcras.rs | 14 ++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) (limited to 'cras') diff --git a/cras/client/libcras/src/cras_server_socket.rs b/cras/client/libcras/src/cras_server_socket.rs index 6fa84fbd..34e8cbd4 100644 --- a/cras/client/libcras/src/cras_server_socket.rs +++ b/cras/client/libcras/src/cras_server_socket.rs @@ -9,7 +9,23 @@ use sys_util::{net::UnixSeqpacket, ScmSocket}; use data_model::DataInit; -const CRAS_SERVER_SOCKET_PATH: &str = "/run/cras/.cras_socket"; +/// Server socket type to connect. +pub enum CrasSocketType { + /// A server socket type supports only playback function. + Legacy, + /// A server socket type supports both playback and capture functions. + Unified, +} + +impl CrasSocketType { + fn sock_path(&self) -> &str { + match self { + Self::Legacy => "/run/cras/.cras_socket", + Self::Unified => "/run/cras/.cras_unified", + } + } +} + /// A socket connecting to the CRAS audio server. pub struct CrasServerSocket { socket: UnixSeqpacket, @@ -17,8 +33,18 @@ pub struct CrasServerSocket { impl CrasServerSocket { pub fn new() -> io::Result { - let socket = UnixSeqpacket::connect(CRAS_SERVER_SOCKET_PATH)?; - Ok(CrasServerSocket { socket }) + Self::with_type(CrasSocketType::Legacy) + } + + /// Creates a `CrasServerSocket` with given `CrasSocketType`. + /// + /// # Errors + /// + /// Returns the `io::Error` generated when connecting to the socket on failure. + pub fn with_type(socket_type: CrasSocketType) -> io::Result { + Ok(CrasServerSocket { + socket: UnixSeqpacket::connect(socket_type.sock_path())?, + }) } /// Sends a sized and packed server messge to the server socket. The message diff --git a/cras/client/libcras/src/libcras.rs b/cras/client/libcras/src/libcras.rs index cdad706f..69a06ace 100644 --- a/cras/client/libcras/src/libcras.rs +++ b/cras/client/libcras/src/libcras.rs @@ -142,6 +142,7 @@ mod audio_socket; use crate::audio_socket::AudioSocket; mod cras_server_socket; use crate::cras_server_socket::CrasServerSocket; +pub use crate::cras_server_socket::CrasSocketType; mod cras_shm; use crate::cras_shm::CrasServerState; pub mod cras_shm_stream; @@ -227,9 +228,18 @@ impl<'a> CrasClient<'a> { /// Returns error if error occurs while handling server message or message /// type is incorrect pub fn new() -> Result { - // Create a connection to the server. - let mut server_socket = CrasServerSocket::new()?; + Self::with_type(CrasSocketType::Legacy) + } + /// Tries to create a `CrasClient` with a given `CrasSocketType`. + /// + /// # Errors + /// + /// Returns error if error occurs while handling server message or message + /// type is incorrect. + pub fn with_type(socket_type: CrasSocketType) -> Result { + // Create a connection to the server. + let mut server_socket = CrasServerSocket::with_type(socket_type)?; // Gets client ID and server state fd from server if let ServerResult::Connected(client_id, server_state_fd) = CrasClient::wait_for_message(&mut server_socket)? -- cgit v1.2.3