blob: b0911620007a757d1bb657649b0a3aaec9aa4a0f [file] [log] [blame]
Yu Shan562c0f82022-08-04 17:10:38 -07001/*
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 Shan39b19852023-01-12 16:20:11 -080021#include "BindToDeviceSocketMutator.h"
22
23#include <android-base/logging.h>
Yu Shan562c0f82022-08-04 17:10:38 -070024#include <android/binder_manager.h>
25#include <android/binder_process.h>
Yu Shana725df42022-09-12 14:56:32 -070026#include <grpcpp/create_channel.h>
Yu Shan39b19852023-01-12 16:20:11 -080027#include <libnetdevice/libnetdevice.h>
Yu Shana725df42022-09-12 14:56:32 -070028#include <stdlib.h>
Yu Shan562c0f82022-08-04 17:10:38 -070029
Yu Shana725df42022-09-12 14:56:32 -070030constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default";
31
Yu Shan562c0f82022-08-04 17:10:38 -070032int main(int /* argc */, char* /* argv */[]) {
Yu Shan39b19852023-01-12 16:20:11 -080033 LOG(INFO) << "Registering RemoteAccessService as service...";
Yu Shan562c0f82022-08-04 17:10:38 -070034
Yu Shana725df42022-09-12 14:56:32 -070035#ifndef GRPC_SERVICE_ADDRESS
Yu Shan39b19852023-01-12 16:20:11 -080036 LOG(ERROR) << "GRPC_SERVICE_ADDRESS is not defined, exiting";
Yu Shana725df42022-09-12 14:56:32 -070037 exit(1);
38#endif
Yu Shan39b19852023-01-12 16:20:11 -080039 grpc::ChannelArguments grpcargs = {};
40
41#ifdef GRPC_SERVICE_IFNAME
42 grpcargs.SetSocketMutator(
Krzysztof KosiƄskie908adf2023-04-29 12:50:09 +000043 android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator(
Yu Shan39b19852023-01-12 16:20:11 -080044 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 Shana725df42022-09-12 14:56:32 -070053 auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
Yu Shan562c0f82022-08-04 17:10:38 -070054 auto service = ndk::SharedRefBase::make<
Yu Shana725df42022-09-12 14:56:32 -070055 android::hardware::automotive::remoteaccess::RemoteAccessService>(clientStub.get());
Yu Shan562c0f82022-08-04 17:10:38 -070056
Yu Shana725df42022-09-12 14:56:32 -070057 binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
Yu Shan562c0f82022-08-04 17:10:38 -070058 if (err != EX_NONE) {
Yu Shan39b19852023-01-12 16:20:11 -080059 LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
60 << ", exception: " << err;
Yu Shana725df42022-09-12 14:56:32 -070061 exit(1);
Yu Shan562c0f82022-08-04 17:10:38 -070062 }
63
64 if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
Yu Shan39b19852023-01-12 16:20:11 -080065 LOG(ERROR) << "failed to set thread pool max thread count";
Yu Shana725df42022-09-12 14:56:32 -070066 exit(1);
Yu Shan562c0f82022-08-04 17:10:38 -070067 }
68 ABinderProcess_startThreadPool();
69
Yu Shan39b19852023-01-12 16:20:11 -080070 LOG(INFO) << "RemoteAccess service Ready";
Yu Shan562c0f82022-08-04 17:10:38 -070071
72 ABinderProcess_joinThreadPool();
73
Yu Shan39b19852023-01-12 16:20:11 -080074 LOG(ERROR) << "Should not reach here";
Yu Shan562c0f82022-08-04 17:10:38 -070075
76 return 0;
77}