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 
23 #include "os/log.h"
24 
25 namespace bluetooth {
26 namespace os {
27 
GetSystemProperty(const std::string & property)28 std::optional<std::string> GetSystemProperty(const std::string& property) {
29   std::array<char, PROPERTY_VALUE_MAX> value_array{0};
30   auto value_len = property_get(property.c_str(), value_array.data(), nullptr);
31   if (value_len <= 0) {
32     return std::nullopt;
33   }
34   return std::string(value_array.data(), value_len);
35 }
36 
SetSystemProperty(const std::string & property,const std::string & value)37 bool SetSystemProperty(const std::string& property, const std::string& value) {
38   if (value.size() >= PROPERTY_VALUE_MAX) {
39     LOG_ERROR("Property value's maximum size is %d, but %zu chars were given", PROPERTY_VALUE_MAX - 1, value.size());
40     return false;
41   }
42   auto ret = property_set(property.c_str(), value.c_str());
43   if (ret != 0) {
44     LOG_ERROR("Set property %s failed with error code %d", property.c_str(), ret);
45     return false;
46   }
47   return true;
48 }
49 
50 }  // namespace os
51 }  // namespace bluetooth