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* kServiceName =
38     "android.hardware.automotive.vehicle.IVehicle/default";
39 const char* kBootConfigPort = "ro.boot.vhal_proxy_server_port";
40 const char* kAutoEthNamespaceSetupProp =
41     "android.car.auto_eth_namespace_setup_complete";
42 const char* kVsockServiceName = "vendor.vehicle-cf-vsock";
43 const char* kEthServerAddr = "192.168.98.1";
44 
main(int argc,char * argv[])45 int main(int argc, char* argv[]) {
46   bool useVsock = false;
47 
48   if (argc > 1 && strcmp(argv[1], "vsock") == 0) {
49     if (property_get_bool(kAutoEthNamespaceSetupProp, false)) {
50       LOG(INFO) << "Skip starting VHAL in vsock mode since ethernet is enabled";
51       return 0;
52     }
53 
54     // If we are not exiting intentionally, we need to turn off oneshot so that
55     // VHAL will be restarted in case it exits. vendor.vehicle-cf-eth does not
56     // have oneshot in the rc file so nothing to do here.
57     property_set("ctl.oneshot_off", kVsockServiceName);
58     useVsock = true;
59   }
60 
61   LOG(INFO) << "Starting thread pool...";
62   if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
63     LOG(ERROR) << "Failed to set thread pool max thread count.";
64     return 1;
65   }
66   ABinderProcess_startThreadPool();
67 
68   unsigned int port =
69       static_cast<unsigned int>(property_get_int32(kBootConfigPort, -1));
70   CHECK(port >= 0) << "Failed to read port number from: " << kBootConfigPort;
71 
72   std::string serverAddr;
73   if (useVsock) {
74     VsockConnectionInfo vsock = {
75         .cid = VMADDR_CID_HOST,
76         .port = port,
77     };
78     serverAddr = vsock.str();
79     LOG(INFO) << "Connecting to vsock server at " << serverAddr;
80   } else {
81     serverAddr = fmt::format("{}:{}", kEthServerAddr, port);
82     LOG(INFO) << "Connecting to ethernet server at " << serverAddr;
83   }
84 
85   constexpr auto maxConnectWaitTime = std::chrono::seconds(5);
86   auto hardware = std::make_unique<GRPCVehicleHardware>(serverAddr);
87   if (const auto connected = hardware->waitForConnected(maxConnectWaitTime)) {
88     LOG(INFO) << "Connected to GRPC server at " << serverAddr;
89   } else {
90     LOG(INFO)
91         << "Failed to connect to GRPC server at " << serverAddr
92         << ", check if it is working, or maybe the server is coming up late.";
93     return 1;
94   }
95 
96   auto vhal =
97       ::ndk::SharedRefBase::make<DefaultVehicleHal>(std::move(hardware));
98   LOG(INFO) << "Registering as service...";
99   binder_exception_t err =
100       AServiceManager_addService(vhal->asBinder().get(), kServiceName);
101   CHECK(err == EX_NONE) << "Failed to register " << kServiceName
102                         << " service, exception: " << err << ".";
103 
104   LOG(INFO) << "Vehicle Service Ready.";
105 
106   ABinderProcess_joinThreadPool();
107 
108   LOG(INFO) << "Vehicle Service Exiting, must not happen!.";
109 
110   return 0;
111 }
112