Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #define LOG_TAG "RemoteAccessImpl" |
| 18 | |
| 19 | #include "RemoteAccessService.h" |
| 20 | |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 21 | #include "BindToDeviceSocketMutator.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 24 | #include <android/binder_manager.h> |
| 25 | #include <android/binder_process.h> |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 26 | #include <grpcpp/create_channel.h> |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 27 | #include <libnetdevice/libnetdevice.h> |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 29 | |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 30 | constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default"; |
| 31 | |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 32 | int main(int /* argc */, char* /* argv */[]) { |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 33 | LOG(INFO) << "Registering RemoteAccessService as service..."; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 34 | |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 35 | #ifndef GRPC_SERVICE_ADDRESS |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 36 | LOG(ERROR) << "GRPC_SERVICE_ADDRESS is not defined, exiting"; |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 37 | exit(1); |
| 38 | #endif |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 39 | grpc::ChannelArguments grpcargs = {}; |
| 40 | |
| 41 | #ifdef GRPC_SERVICE_IFNAME |
| 42 | grpcargs.SetSocketMutator( |
| 43 | new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator( |
| 44 | GRPC_SERVICE_IFNAME)); |
| 45 | LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME; |
| 46 | LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME; |
| 47 | android::netdevice::waitFor({GRPC_SERVICE_IFNAME}, |
| 48 | android::netdevice::WaitCondition::PRESENT_AND_UP); |
| 49 | LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME << " done"; |
| 50 | #endif |
| 51 | auto channel = grpc::CreateCustomChannel(GRPC_SERVICE_ADDRESS, |
| 52 | grpc::InsecureChannelCredentials(), grpcargs); |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 53 | auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 54 | auto service = ndk::SharedRefBase::make< |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 55 | android::hardware::automotive::remoteaccess::RemoteAccessService>(clientStub.get()); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 56 | |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 57 | binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 58 | if (err != EX_NONE) { |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 59 | LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service" |
| 60 | << ", exception: " << err; |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 61 | exit(1); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) { |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 65 | LOG(ERROR) << "failed to set thread pool max thread count"; |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 66 | exit(1); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 67 | } |
| 68 | ABinderProcess_startThreadPool(); |
| 69 | |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 70 | LOG(INFO) << "RemoteAccess service Ready"; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 71 | |
| 72 | ABinderProcess_joinThreadPool(); |
| 73 | |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 74 | LOG(ERROR) << "Should not reach here"; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |