| 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> | 
|  | 18 | #include <android/binder_libbinder.h> | 
|  | 19 | #include <binder/RpcServer.h> | 
|  | 20 | #include <binder/RpcSession.h> | 
|  | 21 |  | 
| Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 22 | using android::OK; | 
| Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 23 | using android::RpcServer; | 
|  | 24 | using android::RpcSession; | 
| Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 25 | using android::status_t; | 
|  | 26 | using android::statusToString; | 
| Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | extern "C" { | 
|  | 29 |  | 
|  | 30 | bool RunRpcServer(AIBinder* service, unsigned int port) { | 
|  | 31 | auto server = RpcServer::make(); | 
|  | 32 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); | 
| Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 33 | 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 Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 36 | 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 |  | 
|  | 46 | AIBinder* RpcClient(unsigned int cid, unsigned int port) { | 
|  | 47 | auto session = RpcSession::make(); | 
| Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 48 | 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 Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 51 | return nullptr; | 
|  | 52 | } | 
|  | 53 | return AIBinder_fromPlatformBinder(session->getRootObject()); | 
|  | 54 | } | 
|  | 55 | } |