diff options
author | paulhsia <paulhsia@chromium.org> | 2020-04-27 17:45:16 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-04-29 00:12:32 +0000 |
commit | 8405d713c2c293646723a424c218af4a72e598f2 (patch) | |
tree | 16d892038cf206dc1efdfb46d7ec48dd53ae9747 | |
parent | 1c9c78845f713c3544a0500b1fccba6f9367de34 (diff) | |
download | adhd-8405d713c2c293646723a424c218af4a72e598f2.tar.gz |
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 <paulhsia@chromium.org>
Commit-Queue: Chih-Yang Hsia <paulhsia@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
-rw-r--r-- | cras/client/libcras/src/cras_server_socket.rs | 32 | ||||
-rw-r--r-- | cras/client/libcras/src/libcras.rs | 14 |
2 files changed, 41 insertions, 5 deletions
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<CrasServerSocket> { - 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<CrasServerSocket> { + 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<Self> { - // 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<Self> { + // 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)? |