Joel Galenson | c19f006 | 2021-02-22 09:52:18 -0800 | [diff] [blame^] | 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::{freeNames, getHalNames, getHalNamesAndVersions}; |
| 18 | use std::ffi::CStr; |
| 19 | use std::os::raw::c_char; |
| 20 | use 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. |
| 24 | pub struct HalNames { |
| 25 | data: *mut *mut c_char, |
| 26 | len: usize, |
| 27 | } |
| 28 | |
| 29 | impl 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 | |
| 36 | impl<'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. |
| 48 | pub 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. |
| 58 | pub 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 | |
| 66 | #[cfg(test)] |
| 67 | mod tests { |
| 68 | |
| 69 | use super::*; |
| 70 | |
| 71 | #[test] |
| 72 | fn test() -> Result<(), Utf8Error> { |
| 73 | let result = get_hal_names(); |
| 74 | let names = result.as_vec()?; |
| 75 | assert_ne!(names.len(), 0); |
| 76 | |
| 77 | let result = get_hal_names_and_versions(); |
| 78 | let names_and_versions = result.as_vec()?; |
| 79 | assert_ne!(names_and_versions.len(), 0); |
| 80 | |
| 81 | assert!(names_and_versions.len() >= names.len()); |
| 82 | |
| 83 | Ok(()) |
| 84 | } |
| 85 | } |