1 // Copyright 2021, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Bindings for getting the list of HALs.
16 
17 use keystore2_vintf_bindgen::{
18     freeNames, getAidlInstances, getHalNames, getHalNamesAndVersions, getHidlInstances,
19 };
20 use std::ffi::{CStr, CString};
21 use std::os::raw::c_char;
22 use std::str::Utf8Error;
23 
24 /// A struct that contains a list of HALs (optionally with version numbers).
25 /// To use it, call as_vec to get a Vec view of the data it contains.
26 pub struct HalNames {
27     data: *mut *mut c_char,
28     len: usize,
29 }
30 
31 impl Drop for HalNames {
drop(&mut self)32     fn drop(&mut self) {
33         // Safety: The memory is allocated by our C shim so it must free it as well.
34         unsafe { freeNames(self.data, self.len) }
35     }
36 }
37 
38 impl<'a> HalNames {
39     /// Get a Vec view of the list of HALs.
as_vec(&'a self) -> Result<Vec<&'a str>, Utf8Error>40     pub fn as_vec(&'a self) -> Result<Vec<&'a str>, Utf8Error> {
41         // Safety: self.data contains self.len C strings.
42         // The lifetimes ensure that the HalNames (and hence the strings) live
43         // at least as long as the returned vector.
44         unsafe { (0..self.len).map(|i| CStr::from_ptr(*self.data.add(i)).to_str()) }.collect()
45     }
46 }
47 
48 /// Gets all HAL names.
49 /// Note that this is not a zero-cost shim: it will make copies of the strings.
get_hal_names() -> HalNames50 pub fn get_hal_names() -> HalNames {
51     let mut len: usize = 0;
52     // Safety: We'll wrap this in HalNames to free the memory it allocates.
53     // It stores the size of the array it returns in len.
54     let raw_strs = unsafe { getHalNames(&mut len) };
55     HalNames { data: raw_strs, len }
56 }
57 
58 /// Gets all HAL names and versions.
59 /// Note that this is not a zero-cost shim: it will make copies of the strings.
get_hal_names_and_versions() -> HalNames60 pub fn get_hal_names_and_versions() -> HalNames {
61     let mut len: usize = 0;
62     // Safety: We'll wrap this in HalNames to free the memory it allocates.
63     // It stores the size of the array it returns in len.
64     let raw_strs = unsafe { getHalNamesAndVersions(&mut len) };
65     HalNames { data: raw_strs, len }
66 }
67 
68 /// Gets the instances of the given package, version, and interface tuple.
69 /// Note that this is not a zero-cost shim: it will make copies of the strings.
get_hidl_instances( package: &str, major_version: usize, minor_version: usize, interface_name: &str, ) -> HalNames70 pub fn get_hidl_instances(
71     package: &str,
72     major_version: usize,
73     minor_version: usize,
74     interface_name: &str,
75 ) -> HalNames {
76     let mut len: usize = 0;
77     let packages = CString::new(package).expect("Failed to make CString from package.");
78     let interface_name =
79         CString::new(interface_name).expect("Failed to make CString from interface_name.");
80     // Safety: We'll wrap this in HalNames to free the memory it allocates.
81     // It stores the size of the array it returns in len.
82     let raw_strs = unsafe {
83         getHidlInstances(
84             &mut len,
85             packages.as_ptr(),
86             major_version,
87             minor_version,
88             interface_name.as_ptr(),
89         )
90     };
91     HalNames { data: raw_strs, len }
92 }
93 
94 /// Gets the instances of the given package, version, and interface tuple.
95 /// Note that this is not a zero-cost shim: it will make copies of the strings.
get_aidl_instances(package: &str, version: usize, interface_name: &str) -> HalNames96 pub fn get_aidl_instances(package: &str, version: usize, interface_name: &str) -> HalNames {
97     let mut len: usize = 0;
98     let packages = CString::new(package).expect("Failed to make CString from package.");
99     let interface_name =
100         CString::new(interface_name).expect("Failed to make CString from interface_name.");
101     // Safety: We'll wrap this in HalNames to free the memory it allocates.
102     // It stores the size of the array it returns in len.
103     let raw_strs =
104         unsafe { getAidlInstances(&mut len, packages.as_ptr(), version, interface_name.as_ptr()) };
105     HalNames { data: raw_strs, len }
106 }
107 
108 #[cfg(test)]
109 mod tests {
110 
111     use super::*;
112 
113     #[test]
test() -> Result<(), Utf8Error>114     fn test() -> Result<(), Utf8Error> {
115         let result = get_hal_names();
116         let names = result.as_vec()?;
117         assert_ne!(names.len(), 0);
118 
119         let result = get_hal_names_and_versions();
120         let names_and_versions = result.as_vec()?;
121         assert_ne!(names_and_versions.len(), 0);
122 
123         assert!(names_and_versions.len() >= names.len());
124 
125         Ok(())
126     }
127 }
128