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 <linux/vm_sockets.h>
26 #include <sys/socket.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::
35 GRPCVehicleHardware;
36
37 const char* SERVICE_NAME =
38 "android.hardware.automotive.vehicle.IVehicle/default";
39 const char* BOOTCONFIG_PORT = "ro.boot.vhal_proxy_server_port";
40
main(int,char * [])41 int main(int /* argc */, char* /* argv */[]) {
42 LOG(INFO) << "Starting thread pool...";
43 if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
44 LOG(ERROR) << "Failed to set thread pool max thread count.";
45 return 1;
46 }
47 ABinderProcess_startThreadPool();
48
49 VsockConnectionInfo vsock = {
50 .cid = VMADDR_CID_HOST,
51 .port =
52 static_cast<unsigned int>(property_get_int32(BOOTCONFIG_PORT, -1)),
53 };
54 CHECK(vsock.port >= 0) << "Failed to read port number from: "
55 << BOOTCONFIG_PORT;
56 std::string vsockStr = vsock.str();
57
58 LOG(INFO) << "Connecting to vsock server at " << vsockStr;
59
60 constexpr auto maxConnectWaitTime = std::chrono::seconds(5);
61 auto hardware = std::make_unique<GRPCVehicleHardware>(vsockStr);
62 if (const auto connected = hardware->waitForConnected(maxConnectWaitTime)) {
63 LOG(INFO) << "Connected to vsock server at " << vsockStr;
64 } else {
65 LOG(INFO)
66 << "Failed to connect to vsock server at " << vsockStr
67 << ", check if it is working, or maybe the server is coming up late.";
68 return 1;
69 }
70
71 auto vhal =
72 ::ndk::SharedRefBase::make<DefaultVehicleHal>(std::move(hardware));
73 LOG(INFO) << "Registering as service...";
74 binder_exception_t err =
75 AServiceManager_addService(vhal->asBinder().get(), SERVICE_NAME);
76 CHECK(err == EX_NONE) << "Failed to register " << SERVICE_NAME
77 << " service, exception: " << err << ".";
78
79 LOG(INFO) << "Vehicle Service Ready.";
80
81 ABinderProcess_joinThreadPool();
82
83 LOG(INFO) << "Vehicle Service Exiting.";
84
85 return 0;
86 }
87