blob: bcb13aebdd90311dda1cf21c72991c2fc5851a5e [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
Steven Moreland2372f9d2021-08-05 15:42:01 -070022using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070023using android::RpcServer;
24using android::RpcSession;
Steven Moreland2372f9d2021-08-05 15:42:01 -070025using android::status_t;
26using android::statusToString;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070027
28extern "C" {
29
30bool RunRpcServer(AIBinder* service, unsigned int port) {
31 auto server = RpcServer::make();
32 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland2372f9d2021-08-05 15:42:01 -070033 if (status_t status = server->setupVsockServer(port); status != OK) {
34 LOG(ERROR) << "Failed to set up vsock server with port " << port
35 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -070036 return false;
37 }
38 server->setRootObject(AIBinder_toPlatformBinder(service));
39 server->join();
40
41 // Shutdown any open sessions since server failed.
42 (void)server->shutdown();
43 return true;
44}
45
46AIBinder* RpcClient(unsigned int cid, unsigned int port) {
47 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -070048 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
49 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port
50 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -070051 return nullptr;
52 }
53 return AIBinder_fromPlatformBinder(session->getRootObject());
54}
55}