• 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 #include "vsockinfo.h"
18 
19 #ifdef __BIONIC__
20 #include <sstream>
21 
22 #include <android-base/logging.h>
23 #include <cutils/properties.h>
24 
25 namespace android::hardware::automotive::utils {
26 
27 namespace {
getNumberFromProperty(const char * key)28 std::optional<unsigned> getNumberFromProperty(const char* key) {
29     auto value = property_get_int64(key, -1);
30     if ((value <= 0) || (value > UINT_MAX)) {
31         LOG(WARNING) << key << " is missing or out of bounds";
32         return std::nullopt;
33     }
34 
35     return static_cast<unsigned int>(value);
36 }
37 
getNumberFromProperties(const PropertyList & arr)38 std::optional<unsigned> getNumberFromProperties(const PropertyList& arr) {
39     for (const auto& key : arr) {
40         auto val = getNumberFromProperty(key.c_str());
41         if (val) return val;
42     }
43 
44     return std::nullopt;
45 }
46 }  // namespace
47 
fromRoPropertyStore(const PropertyList & cid_props,const PropertyList & port_props)48 std::optional<VsockConnectionInfo> VsockConnectionInfo::fromRoPropertyStore(
49         const PropertyList& cid_props, const PropertyList& port_props) {
50     const auto cid = getNumberFromProperties(cid_props);
51     const auto port = getNumberFromProperties(port_props);
52 
53     if (cid && port) {
54         return {{*cid, *port}};
55     } else {
56         return {};
57     }
58 }
59 
str() const60 std::string VsockConnectionInfo::str() const {
61     std::stringstream ss;
62 
63     ss << "vsock:" << cid << ":" << port;
64     return ss.str();
65 }
66 
67 }  // namespace android::hardware::automotive::utils
68 #endif  // __BIONIC__
69