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

Commit a800ac7b authored by Andrei Homescu's avatar Andrei Homescu
Browse files

libbinder: Add ARpcServerTrusty API

Add an API for Rust code to create and call into
RpcServerTrusty objects.

Bug: 242243245
Test: trusty/vendor/google/aosp/scripts/build.py \
    --test "boot-test:com.android.trusty.rust.binder_rpc_test.test" \
    qemu-generic-arm64-test-debug
Change-Id: Iaaf255d9df59c62923dcc894d365e93beaec6c66
parent ab2daeca
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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

#include <lib/tipc/tipc_srv.h>

#if defined(__cplusplus)
extern "C" {
#endif

struct AIBinder;
struct ARpcServerTrusty;

struct ARpcServerTrusty* ARpcServerTrusty_newPerSession(struct AIBinder* (*)(const void*, size_t,
                                                                             char*),
                                                        char*, void (*)(char*));
void ARpcServerTrusty_delete(struct ARpcServerTrusty*);
int ARpcServerTrusty_handleConnect(struct ARpcServerTrusty*, handle_t, const struct uuid*, void**);
int ARpcServerTrusty_handleMessage(void*);
void ARpcServerTrusty_handleDisconnect(void*);
void ARpcServerTrusty_handleChannelCleanup(void*);

#if defined(__cplusplus)
}
#endif
+11 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include <binder/ARpcServerTrusty.h>
#include <binder/IBinder.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>
@@ -100,6 +101,16 @@ private:
        return rpcServer;
    }

    friend struct ::ARpcServerTrusty;
    friend ::ARpcServerTrusty* ::ARpcServerTrusty_newPerSession(::AIBinder* (*)(const void*, size_t,
                                                                                char*),
                                                                char*, void (*)(char*));
    friend void ::ARpcServerTrusty_delete(::ARpcServerTrusty*);
    friend int ::ARpcServerTrusty_handleConnect(::ARpcServerTrusty*, handle_t, const uuid*, void**);
    friend int ::ARpcServerTrusty_handleMessage(void*);
    friend void ::ARpcServerTrusty_handleDisconnect(void*);
    friend void ::ARpcServerTrusty_handleChannelCleanup(void*);

    // The Rpc-specific context maintained for every open TIPC channel.
    struct ChannelContext {
        sp<RpcSession> session;
+90 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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/binder_libbinder.h>
#include <binder/RpcServer.h>
#include <binder/RpcServerTrusty.h>
#include <binder/RpcSession.h>
#include <binder/RpcTransportTipcTrusty.h>

using android::RpcServer;
using android::RpcServerTrusty;
using android::RpcSession;
using android::RpcTransportCtxFactoryTipcTrusty;
using android::sp;
using android::wp;

struct ARpcServerTrusty {
    sp<RpcServer> mRpcServer;

    ARpcServerTrusty() = delete;
    ARpcServerTrusty(sp<RpcServer> rpcServer) : mRpcServer(std::move(rpcServer)) {}
};

ARpcServerTrusty* ARpcServerTrusty_newPerSession(AIBinder* (*cb)(const void*, size_t, char*),
                                                 char* cbArg, void (*cbArgDeleter)(char*)) {
    std::shared_ptr<char> cbArgSp(cbArg, cbArgDeleter);

    auto rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make();
    if (rpcTransportCtxFactory == nullptr) {
        return nullptr;
    }

    auto ctx = rpcTransportCtxFactory->newServerCtx();
    if (ctx == nullptr) {
        return nullptr;
    }

    auto rpcServer = RpcServerTrusty::makeRpcServer(std::move(ctx));
    if (rpcServer == nullptr) {
        return nullptr;
    }

    rpcServer->setPerSessionRootObject(
            [cb, cbArgSp](wp<RpcSession> /*session*/, const void* addrPtr, size_t len) {
                auto* aib = (*cb)(addrPtr, len, cbArgSp.get());
                auto b = AIBinder_toPlatformBinder(aib);

                // We have a new sp<IBinder> backed by the same binder, so we can
                // finally release the AIBinder* from the callback
                AIBinder_decStrong(aib);

                return b;
            });

    return new (std::nothrow) ARpcServerTrusty(std::move(rpcServer));
}

void ARpcServerTrusty_delete(ARpcServerTrusty* rstr) {
    delete rstr;
}

int ARpcServerTrusty_handleConnect(ARpcServerTrusty* rstr, handle_t chan, const uuid* peer,
                                   void** ctx_p) {
    return RpcServerTrusty::handleConnectInternal(rstr->mRpcServer.get(), chan, peer, ctx_p);
}

int ARpcServerTrusty_handleMessage(void* ctx) {
    return RpcServerTrusty::handleMessageInternal(ctx);
}

void ARpcServerTrusty_handleDisconnect(void* ctx) {
    RpcServerTrusty::handleDisconnectInternal(ctx);
}

void ARpcServerTrusty_handleChannelCleanup(void* ctx) {
    RpcServerTrusty::handleChannelCleanup(ctx);
}
+29 −0
Original line number Diff line number Diff line
# Copyright (C) 2024 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.
#

LOCAL_DIR := $(GET_LOCAL_DIR)
LIBBINDER_DIR := $(LOCAL_DIR)/../../../..

MODULE := $(LOCAL_DIR)

MODULE_SRCS := \
	$(LOCAL_DIR)/ARpcServerTrusty.cpp \

MODULE_LIBRARY_DEPS += \
	$(LIBBINDER_DIR)/trusty \
	$(LIBBINDER_DIR)/trusty/ndk \
	trusty/user/base/lib/libstdc++-trusty \

include make/library.mk
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

//! Generated Rust bindings to binder_rpc_server

#[allow(bad_style)]
mod sys {
    include!(env!("BINDGEN_INC_FILE"));
}

pub use sys::*;
Loading