blob: 304415a7ce725ce9c20e47b1d6f7e9dd08271bf0 [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>
Inseob Kime12fccc2021-09-02 17:23:33 +090018#include <android-base/unique_fd.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070019#include <android/binder_libbinder.h>
20#include <binder/RpcServer.h>
21#include <binder/RpcSession.h>
22
Steven Moreland2372f9d2021-08-05 15:42:01 -070023using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070024using android::RpcServer;
25using android::RpcSession;
Steven Moreland2372f9d2021-08-05 15:42:01 -070026using android::status_t;
27using android::statusToString;
Inseob Kime12fccc2021-09-02 17:23:33 +090028using android::base::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070029
30extern "C" {
31
32bool RunRpcServer(AIBinder* service, unsigned int port) {
33 auto server = RpcServer::make();
34 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland2372f9d2021-08-05 15:42:01 -070035 if (status_t status = server->setupVsockServer(port); status != OK) {
36 LOG(ERROR) << "Failed to set up vsock server with port " << port
37 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -070038 return false;
39 }
40 server->setRootObject(AIBinder_toPlatformBinder(service));
41 server->join();
42
43 // Shutdown any open sessions since server failed.
44 (void)server->shutdown();
45 return true;
46}
47
48AIBinder* RpcClient(unsigned int cid, unsigned int port) {
49 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -070050 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
51 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port
52 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -070053 return nullptr;
54 }
55 return AIBinder_fromPlatformBinder(session->getRootObject());
56}
Inseob Kime12fccc2021-09-02 17:23:33 +090057
58AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) {
59 auto session = RpcSession::make();
60 auto request = [=] { return unique_fd{requestFd(param)}; };
61 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
62 LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str();
63 return nullptr;
64 }
65 return AIBinder_fromPlatformBinder(session->getRootObject());
66}
Victor Hsiehd35d31d2021-06-03 11:24:31 -070067}