blob: 28c5cd5cf2246cee940cf2bfef6728a3f9d5dec7 [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 Shan0fbc17f2023-12-26 15:57:47 -080033 android::hardware::automotive::remoteaccess::WakeupClient::StubInterface* grpcStub = nullptr;
34
35#ifdef GRPC_SERVICE_ADDRESS
Yu Shan3a129d72023-05-31 16:33:07 -070036 LOG(INFO) << "Registering RemoteAccessService as service, server: " << GRPC_SERVICE_ADDRESS
37 << "...";
Yu Shan39b19852023-01-12 16:20:11 -080038 grpc::ChannelArguments grpcargs = {};
39
40#ifdef GRPC_SERVICE_IFNAME
41 grpcargs.SetSocketMutator(
Krzysztof KosiƄskie908adf2023-04-29 12:50:09 +000042 android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator(
Yu Shan39b19852023-01-12 16:20:11 -080043 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 Shan0fbc17f2023-12-26 15:57:47 -080049#endif // #ifdef GRPC_SERVICE_IFNAME
Yu Shan3a129d72023-05-31 16:33:07 -070050 auto channel = grpc::CreateChannel(GRPC_SERVICE_ADDRESS, grpc::InsecureChannelCredentials());
Yu Shana725df42022-09-12 14:56:32 -070051 auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
Yu Shan0fbc17f2023-12-26 15:57:47 -080052
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 Shan562c0f82022-08-04 17:10:38 -070059 auto service = ndk::SharedRefBase::make<
Yu Shan0fbc17f2023-12-26 15:57:47 -080060 android::hardware::automotive::remoteaccess::RemoteAccessService>(grpcStub);
Yu Shan562c0f82022-08-04 17:10:38 -070061
Yu Shana725df42022-09-12 14:56:32 -070062 binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
Yu Shan562c0f82022-08-04 17:10:38 -070063 if (err != EX_NONE) {
Yu Shan39b19852023-01-12 16:20:11 -080064 LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
65 << ", exception: " << err;
Yu Shana725df42022-09-12 14:56:32 -070066 exit(1);
Yu Shan562c0f82022-08-04 17:10:38 -070067 }
68
69 if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
Yu Shan39b19852023-01-12 16:20:11 -080070 LOG(ERROR) << "failed to set thread pool max thread count";
Yu Shana725df42022-09-12 14:56:32 -070071 exit(1);
Yu Shan562c0f82022-08-04 17:10:38 -070072 }
73 ABinderProcess_startThreadPool();
74
Yu Shan39b19852023-01-12 16:20:11 -080075 LOG(INFO) << "RemoteAccess service Ready";
Yu Shan562c0f82022-08-04 17:10:38 -070076
77 ABinderProcess_joinThreadPool();
78
Yu Shan39b19852023-01-12 16:20:11 -080079 LOG(ERROR) << "Should not reach here";
Yu Shan562c0f82022-08-04 17:10:38 -070080
81 return 0;
82}