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

Commit d35d31df authored by Victor Hsieh's avatar Victor Hsieh
Browse files

Add RPC binder as unstable API with Rust bindgen

The build targets have limited visibility to the current (and hopefully
no more) clients.

Bug: 190547489
Bug: 189947807
Test: use in the client

Change-Id: I8701066e0b44c21d2b2a77bbf872efd4e28c3662
parent 1f6f6b67
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -257,6 +257,32 @@ aidl_interface {
    },
}

// TODO(b/184872979): remove once the Rust API is created.
cc_library {
    name: "libbinder_rpc_unstable",
    srcs: ["libbinder_rpc_unstable.cpp"],
    defaults: ["libbinder_ndk_host_user"],
    shared_libs: [
        "libbase",
        "libbinder",
        "libbinder_ndk",
        "libutils",
    ],

    // enumerate stable entry points, for apex use
    stubs: {
        symbol_file: "libbinder_rpc_unstable.map.txt",
    },

    // This library is intentionally limited to these targets, and it will be removed later.
    // Do not expand the visibility.
    visibility: [
        "//packages/modules/Virtualization/authfs:__subpackages__",
        "//packages/modules/Virtualization/compos",
        "//packages/modules/Virtualization/microdroid",
    ],
}

// libbinder historically contained additional interfaces that provided specific
// functionality in the platform but have nothing to do with binder itself. These
// are moved out of libbinder in order to avoid the overhead of their vtables.
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/logging.h>
#include <android/binder_libbinder.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>

using android::RpcServer;
using android::RpcSession;

extern "C" {

bool RunRpcServer(AIBinder* service, unsigned int port) {
    auto server = RpcServer::make();
    server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
    if (!server->setupVsockServer(port)) {
        LOG(ERROR) << "Failed to set up vsock server with port " << port;
        return false;
    }
    server->setRootObject(AIBinder_toPlatformBinder(service));
    server->join();

    // Shutdown any open sessions since server failed.
    (void)server->shutdown();
    return true;
}

AIBinder* RpcClient(unsigned int cid, unsigned int port) {
    auto session = RpcSession::make();
    if (!session->setupVsockClient(cid, port)) {
        LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port;
        return nullptr;
    }
    return AIBinder_fromPlatformBinder(session->getRootObject());
}
}
+7 −0
Original line number Diff line number Diff line
LIBBINDER_RPC_UNSTABLE_SHIM { # platform-only
  global:
    RunRpcServer;
    RpcClient;
  local:
    *;
};
+15 −0
Original line number Diff line number Diff line
@@ -106,6 +106,21 @@ rust_bindgen {
    ],
}

// TODO(b/184872979): remove once the Rust API is created.
rust_bindgen {
    name: "libbinder_rpc_unstable_bindgen",
    wrapper_src: "src/binder_rpc_unstable.hpp",
    crate_name: "binder_rpc_unstable_bindgen",
    source_stem: "bindings",
    shared_libs: [
        "libutils",
    ],
    apex_available: [
        "com.android.compos",
        "com.android.virt",
    ],
}

rust_test {
    name: "libbinder_rs-internal_test",
    crate_name: "binder",
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

extern "C" {

struct AIBinder;

bool RunRpcServer(AIBinder* service, unsigned int port);
AIBinder* RpcClient(unsigned int cid, unsigned int port);

}
Loading