blob: c0561d7e3c3c36260ad4b8b85b0525a5c16f177a [file] [log] [blame]
Prabir Pradhan44e6e832023-06-06 00:03:25 +00001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! # The rust component of InputFlinger
18//!
19//! We use cxxbridge to create IInputFlingerRust - the Rust component of inputflinger - and
20//! pass it back to C++ as a local AIDL interface.
21
22use binder::{
23 unstable_api::{AIBinder, new_spibinder,},
24 BinderFeatures, Interface, StatusCode, Strong,
25};
26use com_android_server_inputflinger::aidl::com::android::server::inputflinger::IInputFlingerRust::{
27 BnInputFlingerRust, IInputFlingerRust,
28 IInputFlingerRustBootstrapCallback::IInputFlingerRustBootstrapCallback,
29};
30use log::debug;
31
32const LOG_TAG: &str = "inputflinger_bootstrap";
33
34#[cxx::bridge]
35mod ffi {
36 extern "C++" {
37 include!("InputFlingerBootstrap.h");
38 type IInputFlingerRustBootstrapCallbackAIBinder;
39 }
40
41 extern "Rust" {
42 unsafe fn create_inputflinger_rust(
43 callback: *mut IInputFlingerRustBootstrapCallbackAIBinder,
44 );
45 }
46}
47
48/// Create the IInputFlingerRust implementation.
49/// This is the singular entry point from C++ into Rust.
50/// The `callback` parameter must be a valid pointer to an AIBinder implementation of
51/// the `IInputFlingerRustBootstrapCallback` interface. The IInputFlingerRust implementation that
52/// is created will be passed back through the callback from within this function.
53/// NOTE: This function must not hold a strong reference to the callback beyond its scope.
54///
55/// # Safety
56///
57/// This function is safe iff `callback` is a valid pointer to an `AIBinder` interface of type
58/// `IInputFlingerRustBootstrapCallback`. The pointer must have had its reference count manually
59/// incremented using `AIBinder_incStrong`. See `binder::unstable_api::new_spibinder`.
60unsafe fn create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder) {
61 logger::init(
62 logger::Config::default().with_tag_on_device(LOG_TAG).with_min_level(log::Level::Trace),
63 );
64
65 let callback = callback as *mut AIBinder;
66 if callback.is_null() {
67 panic!("create_inputflinger_rust cannot be called with a null callback");
68 }
69
70 let Some(callback) = new_spibinder(callback) else {
71 panic!("Failed to get SpAIBinder from raw callback pointer");
72 };
73
74 let callback: Result<Strong<dyn IInputFlingerRustBootstrapCallback>, StatusCode> =
75 callback.into_interface();
76 match callback {
77 Ok(callback) => {
78 debug!("Creating InputFlingerRust");
79 let service =
80 BnInputFlingerRust::new_binder(InputFlingerRust {}, BinderFeatures::default());
81 callback.onProvideInputFlingerRust(&service).unwrap();
82 }
83 Err(status) => {
84 panic!("Failed to convert AIBinder into the callback interface: {}", status);
85 }
86 }
87}
88
89struct InputFlingerRust {}
90
91impl Interface for InputFlingerRust {}
92
93impl IInputFlingerRust for InputFlingerRust {}
94
95impl Drop for InputFlingerRust {
96 fn drop(&mut self) {
97 debug!("Destroying InputFlingerRust");
98 }
99}