1 /*
2  * Copyright (C) 2024 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 #include <android/binder_libbinder.h>
18 #include <binder/RpcServer.h>
19 #include <binder/RpcServerTrusty.h>
20 #include <binder/RpcSession.h>
21 #include <binder/RpcTransportTipcTrusty.h>
22 
23 using android::RpcServer;
24 using android::RpcServerTrusty;
25 using android::RpcSession;
26 using android::RpcTransportCtxFactoryTipcTrusty;
27 using android::sp;
28 using android::wp;
29 
30 struct ARpcServerTrusty {
31     sp<RpcServer> mRpcServer;
32 
33     ARpcServerTrusty() = delete;
ARpcServerTrustyARpcServerTrusty34     ARpcServerTrusty(sp<RpcServer> rpcServer) : mRpcServer(std::move(rpcServer)) {}
35 };
36 
ARpcServerTrusty_newPerSession(AIBinder * (* cb)(const void *,size_t,char *),char * cbArg,void (* cbArgDeleter)(char *))37 ARpcServerTrusty* ARpcServerTrusty_newPerSession(AIBinder* (*cb)(const void*, size_t, char*),
38                                                  char* cbArg, void (*cbArgDeleter)(char*)) {
39     std::shared_ptr<char> cbArgSp(cbArg, cbArgDeleter);
40 
41     auto rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make();
42     if (rpcTransportCtxFactory == nullptr) {
43         return nullptr;
44     }
45 
46     auto ctx = rpcTransportCtxFactory->newServerCtx();
47     if (ctx == nullptr) {
48         return nullptr;
49     }
50 
51     auto rpcServer = RpcServerTrusty::makeRpcServer(std::move(ctx));
52     if (rpcServer == nullptr) {
53         return nullptr;
54     }
55 
56     rpcServer->setPerSessionRootObject(
57             [cb, cbArgSp](wp<RpcSession> /*session*/, const void* addrPtr, size_t len) {
58                 auto* aib = (*cb)(addrPtr, len, cbArgSp.get());
59                 auto b = AIBinder_toPlatformBinder(aib);
60 
61                 // We have a new sp<IBinder> backed by the same binder, so we can
62                 // finally release the AIBinder* from the callback
63                 AIBinder_decStrong(aib);
64 
65                 return b;
66             });
67 
68     return new (std::nothrow) ARpcServerTrusty(std::move(rpcServer));
69 }
70 
ARpcServerTrusty_delete(ARpcServerTrusty * rstr)71 void ARpcServerTrusty_delete(ARpcServerTrusty* rstr) {
72     delete rstr;
73 }
74 
ARpcServerTrusty_handleConnect(ARpcServerTrusty * rstr,handle_t chan,const uuid * peer,void ** ctx_p)75 int ARpcServerTrusty_handleConnect(ARpcServerTrusty* rstr, handle_t chan, const uuid* peer,
76                                    void** ctx_p) {
77     return RpcServerTrusty::handleConnectInternal(rstr->mRpcServer.get(), chan, peer, ctx_p);
78 }
79 
ARpcServerTrusty_handleMessage(void * ctx)80 int ARpcServerTrusty_handleMessage(void* ctx) {
81     return RpcServerTrusty::handleMessageInternal(ctx);
82 }
83 
ARpcServerTrusty_handleDisconnect(void * ctx)84 void ARpcServerTrusty_handleDisconnect(void* ctx) {
85     RpcServerTrusty::handleDisconnectInternal(ctx);
86 }
87 
ARpcServerTrusty_handleChannelCleanup(void * ctx)88 void ARpcServerTrusty_handleChannelCleanup(void* ctx) {
89     RpcServerTrusty::handleChannelCleanup(ctx);
90 }
91