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