xref: /aosp_15_r20/hardware/interfaces/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 
21 #include "BindToDeviceSocketMutator.h"
22 
23 #include <android-base/logging.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <grpcpp/create_channel.h>
27 #include <libnetdevice/libnetdevice.h>
28 #include <stdlib.h>
29 
30 namespace {
31 
32 constexpr char GRPC_SERVICE_CONFIG_FILE[] = "/vendor/etc/automotive/powercontroller/serverconfig";
33 constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default";
34 
maybeGetGrpcServiceInfo(std::string * address,std::string * ifname)35 void maybeGetGrpcServiceInfo(std::string* address, std::string* ifname) {
36     std::ifstream ifs(GRPC_SERVICE_CONFIG_FILE);
37     if (!ifs) {
38         LOG(INFO) << "Cannot open grpc service config file at: " << GRPC_SERVICE_CONFIG_FILE
39                   << ", assume no service is available";
40         return;
41     }
42     int count = 0;
43     while (ifs.good()) {
44         std::string line;
45         ifs >> line;
46         // First line is address, second line, if present is ifname.
47         if (count == 0) {
48             *address = line;
49         } else {
50             *ifname = line;
51             break;
52         }
53         count++;
54     }
55     ifs.close();
56 }
57 
58 }  // namespace
59 
main(int,char * [])60 int main(int /* argc */, char* /* argv */[]) {
61     std::string grpcServiceAddress = "";
62     std::string grpcServiceIfname = "";
63     maybeGetGrpcServiceInfo(&grpcServiceAddress, &grpcServiceIfname);
64 
65     std::unique_ptr<android::hardware::automotive::remoteaccess::WakeupClient::Stub> grpcStub;
66 
67     if (grpcServiceAddress != "") {
68         LOG(INFO) << "Registering RemoteAccessService as service, server: " << grpcServiceAddress
69                   << "...";
70         grpc::ChannelArguments grpcargs = {};
71 
72         if (grpcServiceIfname != "") {
73             grpcargs.SetSocketMutator(
74                     android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator(
75                             grpcServiceIfname));
76             LOG(DEBUG) << "grpcServiceIfname specified as: " << grpcServiceIfname;
77             LOG(INFO) << "Waiting for interface: " << grpcServiceIfname;
78             android::netdevice::waitFor({grpcServiceIfname},
79                                         android::netdevice::WaitCondition::PRESENT_AND_UP);
80             LOG(INFO) << "Waiting for interface: " << grpcServiceIfname << " done";
81         }
82         auto channel = grpc::CreateChannel(grpcServiceAddress, grpc::InsecureChannelCredentials());
83         grpcStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
84     } else {
85         LOG(INFO) << "grpcServiceAddress is not defined, work in fake mode";
86     }
87 
88     auto service = ndk::SharedRefBase::make<
89             android::hardware::automotive::remoteaccess::RemoteAccessService>(grpcStub.get());
90 
91     binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
92     if (err != EX_NONE) {
93         LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
94                    << ", exception: " << err;
95         exit(1);
96     }
97 
98     if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
99         LOG(ERROR) << "failed to set thread pool max thread count";
100         exit(1);
101     }
102     ABinderProcess_startThreadPool();
103 
104     LOG(INFO) << "RemoteAccess service Ready";
105 
106     ABinderProcess_joinThreadPool();
107 
108     LOG(ERROR) << "Should not reach here";
109 
110     return 0;
111 }
112