blob: 501e4359cdf655f31f4498bbd438cc4800fddb59 [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]
Andrew Walbran9d4234c2023-08-18 11:16:04 +000035#[allow(unsafe_op_in_unsafe_fn)]
Prabir Pradhan44e6e832023-06-06 00:03:25 +000036mod ffi {
37 extern "C++" {
38 include!("InputFlingerBootstrap.h");
39 type IInputFlingerRustBootstrapCallbackAIBinder;
40 }
41
42 extern "Rust" {
43 unsafe fn create_inputflinger_rust(
44 callback: *mut IInputFlingerRustBootstrapCallbackAIBinder,
45 );
46 }
47}
48
49/// Create the IInputFlingerRust implementation.
50/// This is the singular entry point from C++ into Rust.
51/// The `callback` parameter must be a valid pointer to an AIBinder implementation of
52/// the `IInputFlingerRustBootstrapCallback` interface. The IInputFlingerRust implementation that
53/// is created will be passed back through the callback from within this function.
54/// NOTE: This function must not hold a strong reference to the callback beyond its scope.
55///
56/// # Safety
57///
58/// This function is safe iff `callback` is a valid pointer to an `AIBinder` interface of type
59/// `IInputFlingerRustBootstrapCallback`. The pointer must have had its reference count manually
60/// incremented using `AIBinder_incStrong`. See `binder::unstable_api::new_spibinder`.
61unsafe fn create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder) {
62 logger::init(
63 logger::Config::default().with_tag_on_device(LOG_TAG).with_min_level(log::Level::Trace),
64 );
65
66 let callback = callback as *mut AIBinder;
67 if callback.is_null() {
68 panic!("create_inputflinger_rust cannot be called with a null callback");
69 }
70
Andrew Walbran9d4234c2023-08-18 11:16:04 +000071 // SAFETY: Our caller guaranteed that `callback` is a valid pointer to an `AIBinder` and its
72 // reference count has been incremented..
73 let Some(callback) = (unsafe { new_spibinder(callback) }) else {
Prabir Pradhan44e6e832023-06-06 00:03:25 +000074 panic!("Failed to get SpAIBinder from raw callback pointer");
75 };
76
77 let callback: Result<Strong<dyn IInputFlingerRustBootstrapCallback>, StatusCode> =
78 callback.into_interface();
79 match callback {
80 Ok(callback) => {
81 debug!("Creating InputFlingerRust");
82 let service =
83 BnInputFlingerRust::new_binder(InputFlingerRust {}, BinderFeatures::default());
84 callback.onProvideInputFlingerRust(&service).unwrap();
85 }
86 Err(status) => {
87 panic!("Failed to convert AIBinder into the callback interface: {}", status);
88 }
89 }
90}
91
92struct InputFlingerRust {}
93
94impl Interface for InputFlingerRust {}
95
96impl IInputFlingerRust for InputFlingerRust {}
97
98impl Drop for InputFlingerRust {
99 fn drop(&mut self) {
100 debug!("Destroying InputFlingerRust");
101 }
102}