blob: 68ec669c3138eb766cd5d7b614304aac31ce6577 [file] [log] [blame]
Victor Hsiehd35d31d2021-06-03 11:24:31 -07001/*
2 * Copyright (C) 2021 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-base/logging.h>
18#include <android/binder_libbinder.h>
19#include <binder/RpcServer.h>
20#include <binder/RpcSession.h>
21
22using android::RpcServer;
23using android::RpcSession;
24
25extern "C" {
26
27bool RunRpcServer(AIBinder* service, unsigned int port) {
28 auto server = RpcServer::make();
29 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
30 if (!server->setupVsockServer(port)) {
31 LOG(ERROR) << "Failed to set up vsock server with port " << port;
32 return false;
33 }
34 server->setRootObject(AIBinder_toPlatformBinder(service));
35 server->join();
36
37 // Shutdown any open sessions since server failed.
38 (void)server->shutdown();
39 return true;
40}
41
42AIBinder* RpcClient(unsigned int cid, unsigned int port) {
43 auto session = RpcSession::make();
44 if (!session->setupVsockClient(cid, port)) {
45 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port;
46 return nullptr;
47 }
48 return AIBinder_fromPlatformBinder(session->getRootObject());
49}
50}