Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 18 | #include <android-base/unique_fd.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 19 | #include <android/binder_libbinder.h> |
| 20 | #include <binder/RpcServer.h> |
| 21 | #include <binder/RpcSession.h> |
| 22 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 23 | using android::OK; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 24 | using android::RpcServer; |
| 25 | using android::RpcSession; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 26 | using android::status_t; |
| 27 | using android::statusToString; |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 28 | using android::base::unique_fd; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 29 | |
| 30 | extern "C" { |
| 31 | |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame^] | 32 | bool RunRpcServerCallback(AIBinder* service, unsigned int port, void (*readyCallback)(void* param), |
| 33 | void* param) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 34 | auto server = RpcServer::make(); |
| 35 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 36 | if (status_t status = server->setupVsockServer(port); status != OK) { |
| 37 | LOG(ERROR) << "Failed to set up vsock server with port " << port |
| 38 | << " error: " << statusToString(status).c_str(); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame^] | 42 | |
| 43 | if (readyCallback) readyCallback(param); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 44 | server->join(); |
| 45 | |
| 46 | // Shutdown any open sessions since server failed. |
| 47 | (void)server->shutdown(); |
| 48 | return true; |
| 49 | } |
| 50 | |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame^] | 51 | bool RunRpcServer(AIBinder* service, unsigned int port) { |
| 52 | return RunRpcServerCallback(service, port, nullptr, nullptr); |
| 53 | } |
| 54 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 55 | AIBinder* RpcClient(unsigned int cid, unsigned int port) { |
| 56 | auto session = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 57 | if (status_t status = session->setupVsockClient(cid, port); status != OK) { |
| 58 | LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port |
| 59 | << " error: " << statusToString(status).c_str(); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 60 | return nullptr; |
| 61 | } |
| 62 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 63 | } |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 64 | |
| 65 | AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) { |
| 66 | auto session = RpcSession::make(); |
| 67 | auto request = [=] { return unique_fd{requestFd(param)}; }; |
| 68 | if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) { |
| 69 | LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str(); |
| 70 | return nullptr; |
| 71 | } |
| 72 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 73 | } |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 74 | } |