• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #ifndef android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
18 #define android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
19 
20 #include <chrono>
21 #include <optional>
22 #include <string>
23 
24 #ifdef __BIONIC__
25 #include <android-base/logging.h>
26 
27 #include <vsockinfo.h>
28 #endif  // __BIONIC__
29 
30 namespace android {
31 namespace hardware {
32 namespace automotive {
33 namespace vehicle {
34 namespace V2_0 {
35 namespace impl {
36 
37 template <class duration_t>
TimeValFromChronoDuration(duration_t duration)38 constexpr struct timeval TimeValFromChronoDuration(duration_t duration) {
39     using std::micro;
40     using std::chrono::duration_cast;
41     using std::chrono::microseconds;
42     using std::chrono::seconds;
43     return {
44             .tv_sec = static_cast<time_t>(duration_cast<seconds>(duration).count()),
45             .tv_usec = static_cast<suseconds_t>(duration_cast<microseconds>(duration).count() %
46                                                 micro::den),
47     };
48 }
49 
50 // True means fd is ready, False means timeout
51 bool WaitForReadWithTimeout(int fd, struct timeval&& timeout);
52 
53 // True means fd is ready, False means timeout
54 template <class duration_t>
WaitForReadWithTimeout(int fd,duration_t timeout)55 bool WaitForReadWithTimeout(int fd, duration_t timeout) {
56     return WaitForReadWithTimeout(fd, TimeValFromChronoDuration(timeout));
57 }
58 
59 struct VirtualizedVhalServerInfo {
60 #ifdef __BIONIC__
61     android::hardware::automotive::utils::VsockConnectionInfo vsock;
62 #else
63     struct {
64         unsigned cid = 0;
65         unsigned port = 0;
66     } vsock;
67 #endif
68 
69     std::string powerStateMarkerFilePath;
70     std::string powerStateSocket;
71 
72     static std::optional<VirtualizedVhalServerInfo> fromCommandLine(int argc, char* argv[],
73                                                                     std::string* error);
74 
75 #ifdef __BIONIC__
76     static std::optional<VirtualizedVhalServerInfo> fromRoPropertyStore();
77 #endif
78 
79     std::string getServerUri() const;
80 };
81 
82 }  // namespace impl
83 }  // namespace V2_0
84 }  // namespace vehicle
85 }  // namespace automotive
86 }  // namespace hardware
87 }  // namespace android
88 
89 #endif  // android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
90