blob: c3d6d8ad27ea3793535715833144b16acfa5ee28 [file] [log] [blame]
Joel Galensonc19f0062021-02-22 09:52:18 -08001// 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
Janis Danisevskisef14e1a2021-02-23 23:16:55 -080017use keystore2_vintf_bindgen::{freeNames, getAidlInstances, getHalNames, getHalNamesAndVersions};
18use std::ffi::{CStr, CString};
Joel Galensonc19f0062021-02-22 09:52:18 -080019use std::os::raw::c_char;
20use std::str::Utf8Error;
21
22/// A struct that contains a list of HALs (optionally with version numbers).
23/// To use it, call as_vec to get a Vec view of the data it contains.
24pub struct HalNames {
25 data: *mut *mut c_char,
26 len: usize,
27}
28
29impl Drop for HalNames {
30 fn drop(&mut self) {
31 // Safety: The memory is allocated by our C shim so it must free it as well.
32 unsafe { freeNames(self.data, self.len) }
33 }
34}
35
36impl<'a> HalNames {
37 /// Get a Vec view of the list of HALs.
38 pub fn as_vec(&'a self) -> Result<Vec<&'a str>, Utf8Error> {
39 // Safety: self.data contains self.len C strings.
40 // The lifetimes ensure that the HalNames (and hence the strings) live
41 // at least as long as the returned vector.
42 unsafe { (0..self.len).map(|i| CStr::from_ptr(*self.data.add(i)).to_str()) }.collect()
43 }
44}
45
46/// Gets all HAL names.
47/// Note that this is not a zero-cost shim: it will make copies of the strings.
48pub fn get_hal_names() -> HalNames {
49 let mut len: usize = 0;
50 // Safety: We'll wrap this in HalNames to free the memory it allocates.
51 // It stores the size of the array it returns in len.
52 let raw_strs = unsafe { getHalNames(&mut len) };
53 HalNames { data: raw_strs, len }
54}
55
56/// Gets all HAL names and versions.
57/// Note that this is not a zero-cost shim: it will make copies of the strings.
58pub fn get_hal_names_and_versions() -> HalNames {
59 let mut len: usize = 0;
60 // Safety: We'll wrap this in HalNames to free the memory it allocates.
61 // It stores the size of the array it returns in len.
62 let raw_strs = unsafe { getHalNamesAndVersions(&mut len) };
63 HalNames { data: raw_strs, len }
64}
65
Janis Danisevskisef14e1a2021-02-23 23:16:55 -080066/// Gets the instances of the given package, version, and interface tuple.
67/// Note that this is not a zero-cost shim: it will make copies of the strings.
68pub fn get_aidl_instances(package: &str, version: usize, interface_name: &str) -> HalNames {
69 let mut len: usize = 0;
70 let packages = CString::new(package).expect("Failed to make CString from package.");
71 let interface_name =
72 CString::new(interface_name).expect("Failed to make CString from interface_name.");
73 // Safety: We'll wrap this in HalNames to free the memory it allocates.
74 // It stores the size of the array it returns in len.
75 let raw_strs =
76 unsafe { getAidlInstances(&mut len, packages.as_ptr(), version, interface_name.as_ptr()) };
77 HalNames { data: raw_strs, len }
78}
79
Joel Galensonc19f0062021-02-22 09:52:18 -080080#[cfg(test)]
81mod tests {
82
83 use super::*;
84
85 #[test]
86 fn test() -> Result<(), Utf8Error> {
87 let result = get_hal_names();
88 let names = result.as_vec()?;
89 assert_ne!(names.len(), 0);
90
91 let result = get_hal_names_and_versions();
92 let names_and_versions = result.as_vec()?;
93 assert_ne!(names_and_versions.len(), 0);
94
95 assert!(names_and_versions.len() >= names.len());
96
97 Ok(())
98 }
99}