• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 "os/system_properties.h"
18 
19 #include <bluetooth/log.h>
20 #include <cutils/properties.h>
21 
22 #include <array>
23 #include <cctype>
24 
25 #include "common/strings.h"
26 #include "os/log.h"
27 
28 namespace bluetooth {
29 namespace os {
30 
GetSystemProperty(const std::string & property)31 std::optional<std::string> GetSystemProperty(const std::string& property) {
32   std::array<char, PROPERTY_VALUE_MAX> value_array{0};
33   auto value_len = property_get(property.c_str(), value_array.data(), nullptr);
34   if (value_len <= 0) {
35     return std::nullopt;
36   }
37   return std::string(value_array.data(), value_len);
38 }
39 
SetSystemProperty(const std::string & property,const std::string & value)40 bool SetSystemProperty(const std::string& property, const std::string& value) {
41   if (value.size() >= PROPERTY_VALUE_MAX) {
42     log::error(
43         "Property value's maximum size is {}, but {} chars were given",
44         PROPERTY_VALUE_MAX - 1,
45         value.size());
46     return false;
47   }
48   auto ret = property_set(property.c_str(), value.c_str());
49   if (ret != 0) {
50     log::error("Set property {} failed with error code {}", property, ret);
51     return false;
52   }
53   return true;
54 }
55 
IsRootCanalEnabled()56 bool IsRootCanalEnabled() {
57   auto value = GetSystemProperty("ro.vendor.build.fingerprint");
58   if (value.has_value()) {
59     log::info("ro.vendor.build.fingerprint='{}', length={}", value->c_str(), value->length());
60   } else {
61     log::info("ro.vendor.build.fingerprint is not found");
62   }
63   // aosp_cf_x86_64_phone is just one platform that currently runs root canal
64   // When other platforms appears, or there is a better signal, add them here
65   if (value->find("generic/aosp_cf_x86_64_phone") == std::string::npos) {
66     log::info("Not on generic/aosp_cf_x86_64_phone and hence not root canal");
67     return false;
68   }
69   return true;
70 }
71 
GetAndroidVendorReleaseVersion()72 int GetAndroidVendorReleaseVersion() {
73   auto value = GetSystemProperty("ro.vendor.build.version.release_or_codename");
74   if (!value) {
75     log::info("ro.vendor.build.version.release_or_codename does not exist");
76     return 0;
77   }
78   log::info(
79       "ro.vendor.build.version.release_or_codename='{}', length={}",
80       value->c_str(),
81       value->length());
82   auto int_value = common::Int64FromString(*value);
83   if (int_value) {
84     return static_cast<int>(*int_value);
85   }
86   log::info("value '{}' cannot be parsed to int", value->c_str());
87   if (value->empty()) {
88     log::info("value '{}' is empty", value->c_str());
89     return 0;
90   }
91   if (value->length() > 1) {
92     log::info("value '{}' length is {}, which is > 1", value->c_str(), value->length());
93   }
94   char release_code = toupper(value->at(0));
95   switch (release_code) {
96     case 'S':
97       return 11;
98     case 'R':
99       return 10;
100     case 'P':
101       return 9;
102     case 'O':
103       return 8;
104     default:
105       // Treble not enabled before Android O
106       return 0;
107   }
108 }
109 
ClearSystemPropertiesForHost()110 bool ClearSystemPropertiesForHost() {
111   return false;
112 }
113 
114 }  // namespace os
115 }  // namespace bluetooth
116