summaryrefslogtreecommitdiff
path: root/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/ARpcServerTrusty.cpp
blob: 451383a90a38cdb899364fba51cfab02c1bdcfc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * 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);
}