Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2d7c8a6c authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: add sysprop ffi for android

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I6afb19301b0d1db3bcc06283d4d8b8cc1a855c74
parent 03186560
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@ rust_library {
            rustlibs: [
                "libandroid_logger",
            ],
            static_libs: ["libbt_common_sys_prop_cxx"],
            shared_libs: [
                "libcutils",
            ],
        },
        host: {
            rustlibs: [
@@ -45,3 +49,23 @@ rust_test_host {
        "libpaste",
    ],
}

genrule {
    name: "libbt_common_sys_prop_bridge_code",
    tools: ["cxxbridge"],
    cmd: "$(location cxxbridge) $(in) >> $(out)",
    srcs: ["src/sys_prop.rs"],
    out: ["sys_prop_generated.cc"],
}

cc_library_static {
    name: "libbt_common_sys_prop_cxx",
    defaults: ["gd_ffi_defaults"],
    local_include_dirs: ["src/ffi"],
    srcs: ["src/ffi/sys_prop.cc"],
    generated_headers: ["cxx-bridge-header"],
    generated_sources: ["libbt_common_sys_prop_bridge_code"],
    shared_libs: [
        "libcutils",
    ],
}
+20 −0
Original line number Diff line number Diff line
#include "sys_prop.h"
#include <cutils/properties.h>

namespace bluetooth {
namespace common {
namespace sys_prop {

rust::String get(rust::Str property) {
  auto name = std::string(property.data(), property.length());
  std::array<char, PROPERTY_VALUE_MAX> value_array{0};
  auto value_len = property_get(name.c_str(), value_array.data(), nullptr);
  if (value_len <= 0) {
    value_len = 0;
  }
  return rust::String(value_array.data(), value_len);
}

}  // namespace sys_prop
}  // namespace common
}  // namespace bluetooth
+12 −0
Original line number Diff line number Diff line
#include <string>
#include "rust/cxx.h"

namespace bluetooth {
namespace common {
namespace sys_prop {

rust::String get(rust::Str property);

}
}  // namespace common
}  // namespace bluetooth
+3 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ mod asserts;
/// Provides runtime configured-at-startup flags
pub mod init_flags;

/// Provides runtime configured system properties. Stubbed for non-Android.
pub mod sys_prop;

/// Inits logging for Android
#[cfg(target_os = "android")]
pub fn init_logging() {
+29 −0
Original line number Diff line number Diff line
//! System properties on Android

#[cfg(target_os = "android")]
#[cxx::bridge(namespace = bluetooth::common::sys_prop)]
mod ffi {
    extern "C" {
        include!("src/ffi/sys_prop.h");
        fn get(name: &str) -> String;
    }
}

/// Gets the value of a system property on Android
#[cfg(target_os = "android")]
pub fn get(name: &str) -> Option<String> {
    let value = ffi::get(name);

    if !value.is_empty() {
        Some(value)
    } else {
        None
    }
}

/// Fake getter for non-Android, which will always return nothing.
/// Only added so it compiles & you can conditionally using cfg!
#[cfg(not(target_os = "android"))]
pub fn get(_name: &str) -> Option<String> {
    None
}