1*20733378SAndroid Build Coastguard Worker //! Library for interacting with aconfigd. 2*20733378SAndroid Build Coastguard Worker use crate::ffi::{CppAconfigd, CppResultStatus, CppStringResult, CppVoidResult}; 3*20733378SAndroid Build Coastguard Worker use cxx::{let_cxx_string, CxxString, UniquePtr}; 4*20733378SAndroid Build Coastguard Worker use std::error::Error; 5*20733378SAndroid Build Coastguard Worker use std::fmt; 6*20733378SAndroid Build Coastguard Worker 7*20733378SAndroid Build Coastguard Worker /// Wrapper for interacting with aconfigd. 8*20733378SAndroid Build Coastguard Worker pub struct Aconfigd { 9*20733378SAndroid Build Coastguard Worker cpp_aconfigd: UniquePtr<CppAconfigd>, 10*20733378SAndroid Build Coastguard Worker } 11*20733378SAndroid Build Coastguard Worker 12*20733378SAndroid Build Coastguard Worker impl Aconfigd { 13*20733378SAndroid Build Coastguard Worker /// Create a new Aconfigd. new(root_dir: &str, persist_storage_records: &str) -> Self14*20733378SAndroid Build Coastguard Worker pub fn new(root_dir: &str, persist_storage_records: &str) -> Self { 15*20733378SAndroid Build Coastguard Worker let_cxx_string!(root_dir_ = root_dir); 16*20733378SAndroid Build Coastguard Worker let_cxx_string!(persist_storage_records_ = persist_storage_records); 17*20733378SAndroid Build Coastguard Worker Self { cpp_aconfigd: ffi::new_cpp_aconfigd(&root_dir_, &persist_storage_records_) } 18*20733378SAndroid Build Coastguard Worker } 19*20733378SAndroid Build Coastguard Worker 20*20733378SAndroid Build Coastguard Worker /// Create persistent storage files for platform partition. initialize_platform_storage(&self) -> Result<(), CppAconfigdError>21*20733378SAndroid Build Coastguard Worker pub fn initialize_platform_storage(&self) -> Result<(), CppAconfigdError> { 22*20733378SAndroid Build Coastguard Worker self.cpp_aconfigd.initialize_platform_storage().into() 23*20733378SAndroid Build Coastguard Worker } 24*20733378SAndroid Build Coastguard Worker 25*20733378SAndroid Build Coastguard Worker /// Create persistent storage files for mainline modules. initialize_mainline_storage(&self) -> Result<(), CppAconfigdError>26*20733378SAndroid Build Coastguard Worker pub fn initialize_mainline_storage(&self) -> Result<(), CppAconfigdError> { 27*20733378SAndroid Build Coastguard Worker self.cpp_aconfigd.initialize_mainline_storage().into() 28*20733378SAndroid Build Coastguard Worker } 29*20733378SAndroid Build Coastguard Worker 30*20733378SAndroid Build Coastguard Worker /// Read storage records into memory. initialize_in_memory_storage_records(&self) -> Result<(), CppAconfigdError>31*20733378SAndroid Build Coastguard Worker pub fn initialize_in_memory_storage_records(&self) -> Result<(), CppAconfigdError> { 32*20733378SAndroid Build Coastguard Worker self.cpp_aconfigd.initialize_in_memory_storage_records().into() 33*20733378SAndroid Build Coastguard Worker } 34*20733378SAndroid Build Coastguard Worker 35*20733378SAndroid Build Coastguard Worker /// Process a `StorageRequestMessages`, and return the bytes of a `StorageReturnMessages`. 36*20733378SAndroid Build Coastguard Worker /// 37*20733378SAndroid Build Coastguard Worker /// `messages_bytes` should contain the serialized bytes of a `StorageRequestMessages`. handle_socket_request( &self, messages_bytes: &[u8], ) -> Result<Vec<u8>, CppAconfigdError>38*20733378SAndroid Build Coastguard Worker pub fn handle_socket_request( 39*20733378SAndroid Build Coastguard Worker &self, 40*20733378SAndroid Build Coastguard Worker messages_bytes: &[u8], 41*20733378SAndroid Build Coastguard Worker ) -> Result<Vec<u8>, CppAconfigdError> { 42*20733378SAndroid Build Coastguard Worker let_cxx_string!(messages_string_ = messages_bytes); 43*20733378SAndroid Build Coastguard Worker let res: Result<UniquePtr<CxxString>, CppAconfigdError> = 44*20733378SAndroid Build Coastguard Worker self.cpp_aconfigd.handle_socket_request(&messages_string_).into(); 45*20733378SAndroid Build Coastguard Worker res.map(|s| s.as_bytes().to_vec()) 46*20733378SAndroid Build Coastguard Worker } 47*20733378SAndroid Build Coastguard Worker } 48*20733378SAndroid Build Coastguard Worker 49*20733378SAndroid Build Coastguard Worker /// Represents an error in the C++ aconfigd. 50*20733378SAndroid Build Coastguard Worker /// 51*20733378SAndroid Build Coastguard Worker /// The C++ aconfigd uses the C++ Result type. Result errors are mapped 52*20733378SAndroid Build Coastguard Worker /// to this type. 53*20733378SAndroid Build Coastguard Worker #[derive(Debug)] 54*20733378SAndroid Build Coastguard Worker pub struct CppAconfigdError { 55*20733378SAndroid Build Coastguard Worker msg: String, 56*20733378SAndroid Build Coastguard Worker } 57*20733378SAndroid Build Coastguard Worker 58*20733378SAndroid Build Coastguard Worker impl CppAconfigdError { new(msg: &str) -> Self59*20733378SAndroid Build Coastguard Worker pub fn new(msg: &str) -> Self { 60*20733378SAndroid Build Coastguard Worker Self { msg: msg.to_string() } 61*20733378SAndroid Build Coastguard Worker } 62*20733378SAndroid Build Coastguard Worker } 63*20733378SAndroid Build Coastguard Worker 64*20733378SAndroid Build Coastguard Worker impl fmt::Display for CppAconfigdError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result65*20733378SAndroid Build Coastguard Worker fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 66*20733378SAndroid Build Coastguard Worker write!(f, "CppAconfigd error: {}", self.msg) 67*20733378SAndroid Build Coastguard Worker } 68*20733378SAndroid Build Coastguard Worker } 69*20733378SAndroid Build Coastguard Worker 70*20733378SAndroid Build Coastguard Worker impl Error for CppAconfigdError {} 71*20733378SAndroid Build Coastguard Worker 72*20733378SAndroid Build Coastguard Worker #[cxx::bridge(namespace = "aconfigdwrapper")] 73*20733378SAndroid Build Coastguard Worker mod ffi { 74*20733378SAndroid Build Coastguard Worker enum CppResultStatus { 75*20733378SAndroid Build Coastguard Worker Ok, 76*20733378SAndroid Build Coastguard Worker Err, 77*20733378SAndroid Build Coastguard Worker } 78*20733378SAndroid Build Coastguard Worker 79*20733378SAndroid Build Coastguard Worker struct CppVoidResult { 80*20733378SAndroid Build Coastguard Worker error_message: String, 81*20733378SAndroid Build Coastguard Worker status: CppResultStatus, 82*20733378SAndroid Build Coastguard Worker } 83*20733378SAndroid Build Coastguard Worker 84*20733378SAndroid Build Coastguard Worker struct CppStringResult { 85*20733378SAndroid Build Coastguard Worker data: UniquePtr<CxxString>, 86*20733378SAndroid Build Coastguard Worker error_message: String, 87*20733378SAndroid Build Coastguard Worker status: CppResultStatus, 88*20733378SAndroid Build Coastguard Worker } 89*20733378SAndroid Build Coastguard Worker 90*20733378SAndroid Build Coastguard Worker unsafe extern "C++" { 91*20733378SAndroid Build Coastguard Worker include!("libcxx_aconfigd.hpp"); 92*20733378SAndroid Build Coastguard Worker 93*20733378SAndroid Build Coastguard Worker type CppAconfigd; 94*20733378SAndroid Build Coastguard Worker new_cpp_aconfigd(str1: &CxxString, str2: &CxxString) -> UniquePtr<CppAconfigd>95*20733378SAndroid Build Coastguard Worker fn new_cpp_aconfigd(str1: &CxxString, str2: &CxxString) -> UniquePtr<CppAconfigd>; initialize_platform_storage(&self) -> CppVoidResult96*20733378SAndroid Build Coastguard Worker fn initialize_platform_storage(&self) -> CppVoidResult; initialize_mainline_storage(&self) -> CppVoidResult97*20733378SAndroid Build Coastguard Worker fn initialize_mainline_storage(&self) -> CppVoidResult; 98*20733378SAndroid Build Coastguard Worker initialize_in_memory_storage_records(&self) -> CppVoidResult99*20733378SAndroid Build Coastguard Worker fn initialize_in_memory_storage_records(&self) -> CppVoidResult; handle_socket_request(&self, message_string: &CxxString) -> CppStringResult100*20733378SAndroid Build Coastguard Worker fn handle_socket_request(&self, message_string: &CxxString) -> CppStringResult; 101*20733378SAndroid Build Coastguard Worker } 102*20733378SAndroid Build Coastguard Worker } 103*20733378SAndroid Build Coastguard Worker 104*20733378SAndroid Build Coastguard Worker impl Into<Result<(), CppAconfigdError>> for CppVoidResult { into(self) -> Result<(), CppAconfigdError>105*20733378SAndroid Build Coastguard Worker fn into(self) -> Result<(), CppAconfigdError> { 106*20733378SAndroid Build Coastguard Worker match self.status { 107*20733378SAndroid Build Coastguard Worker CppResultStatus::Ok => Ok(()), 108*20733378SAndroid Build Coastguard Worker CppResultStatus::Err => Err(CppAconfigdError::new(&self.error_message)), 109*20733378SAndroid Build Coastguard Worker _ => Err(CppAconfigdError::new("unknown status")), 110*20733378SAndroid Build Coastguard Worker } 111*20733378SAndroid Build Coastguard Worker } 112*20733378SAndroid Build Coastguard Worker } 113*20733378SAndroid Build Coastguard Worker 114*20733378SAndroid Build Coastguard Worker impl Into<Result<UniquePtr<CxxString>, CppAconfigdError>> for CppStringResult { into(self) -> Result<UniquePtr<CxxString>, CppAconfigdError>115*20733378SAndroid Build Coastguard Worker fn into(self) -> Result<UniquePtr<CxxString>, CppAconfigdError> { 116*20733378SAndroid Build Coastguard Worker match self.status { 117*20733378SAndroid Build Coastguard Worker CppResultStatus::Ok => Ok(self.data), 118*20733378SAndroid Build Coastguard Worker CppResultStatus::Err => Err(CppAconfigdError::new(&self.error_message)), 119*20733378SAndroid Build Coastguard Worker _ => Err(CppAconfigdError::new("unknown status")), 120*20733378SAndroid Build Coastguard Worker } 121*20733378SAndroid Build Coastguard Worker } 122*20733378SAndroid Build Coastguard Worker } 123