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 #include "Utils.h"
18
19 #ifdef __BIONIC__
20 #include <cutils/properties.h>
21 #endif // __BIONIC__
22
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <array>
28 #include <climits>
29 #include <iostream>
30 #include <sstream>
31
32 using std::cerr;
33 using std::endl;
34
35 namespace android {
36 namespace hardware {
37 namespace automotive {
38 namespace vehicle {
39 namespace V2_0 {
40 namespace impl {
41
42 #ifdef __BIONIC__
getServerUri() const43 std::string VirtualizedVhalServerInfo::getServerUri() const {
44 return vsock.str();
45 }
46 #else
47 std::string VirtualizedVhalServerInfo::getServerUri() const {
48 std::stringstream ss;
49
50 ss << "vsock:" << vsock.cid << ":" << vsock.port;
51 return ss.str();
52 }
53 #endif
54
WaitForReadWithTimeout(int fd,struct timeval && timeout)55 bool WaitForReadWithTimeout(int fd, struct timeval&& timeout) {
56 fd_set read_fd_set;
57 FD_ZERO(&read_fd_set);
58 FD_SET(fd, &read_fd_set);
59 auto ready = select(FD_SETSIZE, &read_fd_set, nullptr, nullptr, &timeout);
60
61 if (ready < 0) {
62 cerr << __func__ << ": fd: " << fd << ", errno: " << errno << ", " << strerror(errno)
63 << endl;
64 return false;
65 }
66 return ready > 0;
67 }
68
parseUnsignedIntFromString(const char * optarg,const char * name)69 static std::optional<unsigned> parseUnsignedIntFromString(const char* optarg, const char* name) {
70 auto v = strtoul(optarg, nullptr, 0);
71 if (((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT_MAX)) {
72 cerr << name << " value is out of range: " << optarg << endl;
73 } else if (v != 0) {
74 return v;
75 } else {
76 cerr << name << " value is invalid or missing: " << optarg << endl;
77 }
78
79 return std::nullopt;
80 }
81
fromCommandLine(int argc,char * argv[],std::string * error)82 std::optional<VirtualizedVhalServerInfo> VirtualizedVhalServerInfo::fromCommandLine(
83 int argc, char* argv[], std::string* error) {
84 // TODO(egranata): move command-line parsing into vsockinfo
85 std::optional<unsigned int> cid;
86 std::optional<unsigned int> port;
87 std::optional<std::string> powerStateMarkerFilePath;
88 std::optional<std::string> powerStateSocketPath;
89
90 // unique values to identify the options
91 constexpr int OPT_VHAL_SERVER_CID = 1001;
92 constexpr int OPT_VHAL_SERVER_PORT_NUMBER = 1002;
93 constexpr int OPT_VHAL_SERVER_POWER_STATE_FILE = 1003;
94 constexpr int OPT_VHAL_SERVER_POWER_STATE_SOCKET = 1004;
95
96 struct option longOptions[] = {
97 {"server_cid", 1, 0, OPT_VHAL_SERVER_CID},
98 {"server_port", 1, 0, OPT_VHAL_SERVER_PORT_NUMBER},
99 {"power_state_file", 1, 0, OPT_VHAL_SERVER_POWER_STATE_FILE},
100 {"power_state_socket", 1, 0, OPT_VHAL_SERVER_POWER_STATE_SOCKET},
101 {},
102 };
103
104 int optValue;
105 while ((optValue = getopt_long_only(argc, argv, ":", longOptions, 0)) != -1) {
106 switch (optValue) {
107 case OPT_VHAL_SERVER_CID:
108 cid = parseUnsignedIntFromString(optarg, "cid");
109 break;
110 case OPT_VHAL_SERVER_PORT_NUMBER:
111 port = parseUnsignedIntFromString(optarg, "port");
112 break;
113 case OPT_VHAL_SERVER_POWER_STATE_FILE:
114 powerStateMarkerFilePath = std::string(optarg);
115 break;
116 case OPT_VHAL_SERVER_POWER_STATE_SOCKET:
117 powerStateSocketPath = std::string(optarg);
118 break;
119 default:
120 // ignore other options
121 break;
122 }
123 }
124
125 if (!cid.has_value() && error) {
126 *error += "Missing server CID. ";
127 }
128 if (!port.has_value() && error) {
129 *error += "Missing server port number. ";
130 }
131 if (!powerStateMarkerFilePath.has_value() && error) {
132 *error += "Missing power state marker file path. ";
133 }
134 if (!powerStateSocketPath.has_value() && error) {
135 *error += "Missing power state socket path. ";
136 }
137
138 if (cid && port && powerStateMarkerFilePath && powerStateSocketPath) {
139 return VirtualizedVhalServerInfo{
140 {*cid, *port}, *powerStateMarkerFilePath, *powerStateSocketPath};
141 }
142 return std::nullopt;
143 }
144
145 #ifdef __BIONIC__
fromRoPropertyStore()146 std::optional<VirtualizedVhalServerInfo> VirtualizedVhalServerInfo::fromRoPropertyStore() {
147 auto vsock = android::hardware::automotive::utils::VsockConnectionInfo::fromRoPropertyStore(
148 {
149 "ro.boot.vendor.vehiclehal.server.cid",
150 "ro.vendor.vehiclehal.server.cid",
151 },
152 {
153 "ro.boot.vendor.vehiclehal.server.port",
154 "ro.vendor.vehiclehal.server.port",
155 });
156
157 if (vsock) {
158 return VirtualizedVhalServerInfo{*vsock, "", ""};
159 }
160 return std::nullopt;
161 }
162 #endif // __BIONIC__
163
164 } // namespace impl
165 } // namespace V2_0
166 } // namespace vehicle
167 } // namespace automotive
168 } // namespace hardware
169 } // namespace android
170