1 /*
2  * Copyright (C) 2023 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 #include "DefaultVehicleHal.h"
18 #include "GRPCVehicleHardware.h"
19 #include "vsockinfo.h"
20 
21 #include <android-base/logging.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 #include <cutils/properties.h>
25 #include <sys/socket.h>
26 #include <linux/vm_sockets.h>
27 
28 #include <chrono>
29 #include <memory>
30 #include <utility>
31 
32 using ::android::hardware::automotive::utils::VsockConnectionInfo;
33 using ::android::hardware::automotive::vehicle::DefaultVehicleHal;
34 using ::android::hardware::automotive::vehicle::virtualization::GRPCVehicleHardware;
35 
main(int,char * [])36 int main(int /* argc */, char* /* argv */[]) {
37     LOG(INFO) << "Starting thread pool...";
38     if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
39         LOG(ERROR) << "Failed to set thread pool max thread count.";
40         return 1;
41     }
42     ABinderProcess_startThreadPool();
43 
44     auto vsock = VsockConnectionInfo::fromRoPropertyStore(
45             {
46                     "ro.boot.vendor.vehiclehal.server.cid",
47                     "ro.vendor.vehiclehal.server.cid",
48             },
49             {
50                     "ro.boot.vendor.vehiclehal.server.port",
51                     "ro.vendor.vehiclehal.server.port",
52             });
53     CHECK(vsock.has_value()) << "Cannot read VHAL server address.";
54 
55     if (property_get_bool("ro.vendor.vehiclehal.server.use_local_fake_server",
56                           /* default_value = */ false)) {
57         // When using the local fake server in the same VM, the CID read from the system properties
58         // will be ignored. We use the loopback CID instead.
59         LOG(INFO)
60                 << "Using the local GRPC vehicle server that running on the same VM as the client.";
61         vsock->cid = VMADDR_CID_LOCAL;
62     }
63 
64     LOG(INFO) << "Connecting to vsock server at " << vsock->str();
65 
66     constexpr auto maxConnectWaitTime = std::chrono::seconds(5);
67     auto hardware = std::make_unique<GRPCVehicleHardware>(vsock->str());
68     if (hardware->waitForConnected(maxConnectWaitTime)) {
69         LOG(INFO) << "Connected to vsock server at " << vsock->str();
70     } else {
71         LOG(INFO) << "Failed to connect to vsock server at " << vsock->str()
72                   << ", check if it is working, or maybe the server is coming up late.";
73         return 1;
74     }
75 
76     auto vhal = ::ndk::SharedRefBase::make<DefaultVehicleHal>(std::move(hardware));
77     LOG(INFO) << "Registering as service...";
78     binder_exception_t err = AServiceManager_addService(
79             vhal->asBinder().get(), "android.hardware.automotive.vehicle.IVehicle/default");
80     if (err != EX_NONE) {
81         LOG(ERROR) << "Failed to register android.hardware.automotive.vehicle service, exception: "
82                    << err << ".";
83         return 1;
84     }
85 
86     LOG(INFO) << "Vehicle Service Ready.";
87 
88     ABinderProcess_joinThreadPool();
89 
90     LOG(INFO) << "Vehicle Service Exiting.";
91 
92     return 0;
93 }
94