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 | 0fbc17f | 2023-12-26 15:57:47 -0800 | [diff] [blame] | 33 | android::hardware::automotive::remoteaccess::WakeupClient::StubInterface* grpcStub = nullptr; |
| 34 | |
| 35 | #ifdef GRPC_SERVICE_ADDRESS |
Yu Shan | 3a129d7 | 2023-05-31 16:33:07 -0700 | [diff] [blame] | 36 | LOG(INFO) << "Registering RemoteAccessService as service, server: " << GRPC_SERVICE_ADDRESS |
| 37 | << "..."; |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 38 | grpc::ChannelArguments grpcargs = {}; |
| 39 | |
| 40 | #ifdef GRPC_SERVICE_IFNAME |
| 41 | grpcargs.SetSocketMutator( |
Krzysztof KosiĆski | e908adf | 2023-04-29 12:50:09 +0000 | [diff] [blame] | 42 | android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator( |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 43 | GRPC_SERVICE_IFNAME)); |
| 44 | LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME; |
| 45 | LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME; |
| 46 | android::netdevice::waitFor({GRPC_SERVICE_IFNAME}, |
| 47 | android::netdevice::WaitCondition::PRESENT_AND_UP); |
| 48 | LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME << " done"; |
Yu Shan | 0fbc17f | 2023-12-26 15:57:47 -0800 | [diff] [blame] | 49 | #endif // #ifdef GRPC_SERVICE_IFNAME |
Yu Shan | 3a129d7 | 2023-05-31 16:33:07 -0700 | [diff] [blame] | 50 | auto channel = grpc::CreateChannel(GRPC_SERVICE_ADDRESS, grpc::InsecureChannelCredentials()); |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 51 | auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel); |
Yu Shan | 0fbc17f | 2023-12-26 15:57:47 -0800 | [diff] [blame] | 52 | |
| 53 | grpcStub = clientStub.get(); |
| 54 | |
| 55 | #else |
| 56 | LOG(INFO) << "GRPC_SERVICE_ADDRESS is not defined, work in fake mode"; |
| 57 | #endif // #ifdef GRPC_SERVICE_ADDRESS |
| 58 | |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 59 | auto service = ndk::SharedRefBase::make< |
Yu Shan | 0fbc17f | 2023-12-26 15:57:47 -0800 | [diff] [blame] | 60 | android::hardware::automotive::remoteaccess::RemoteAccessService>(grpcStub); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 61 | |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 62 | binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 63 | if (err != EX_NONE) { |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 64 | LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service" |
| 65 | << ", exception: " << err; |
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 | |
| 69 | if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) { |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 70 | LOG(ERROR) << "failed to set thread pool max thread count"; |
Yu Shan | a725df4 | 2022-09-12 14:56:32 -0700 | [diff] [blame] | 71 | exit(1); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 72 | } |
| 73 | ABinderProcess_startThreadPool(); |
| 74 | |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 75 | LOG(INFO) << "RemoteAccess service Ready"; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 76 | |
| 77 | ABinderProcess_joinThreadPool(); |
| 78 | |
Yu Shan | 39b1985 | 2023-01-12 16:20:11 -0800 | [diff] [blame] | 79 | LOG(ERROR) << "Should not reach here"; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |