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 <mutex>
20 #include <string>
21 #include <unordered_map>
22 
23 namespace bluetooth {
24 namespace os {
25 
26 namespace {
27 std::mutex properties_mutex;
28 std::unordered_map<std::string, std::string> properties;
29 }  // namespace
30 
GetSystemProperty(const std::string & property)31 std::optional<std::string> GetSystemProperty(const std::string& property) {
32   std::lock_guard<std::mutex> lock(properties_mutex);
33   auto iter = properties.find(property);
34   if (iter == properties.end()) {
35     return std::nullopt;
36   }
37   return iter->second;
38 }
39 
SetSystemProperty(const std::string & property,const std::string & value)40 bool SetSystemProperty(const std::string& property, const std::string& value) {
41   std::lock_guard<std::mutex> lock(properties_mutex);
42   properties.insert_or_assign(property, value);
43   return true;
44 }
45 
ClearSystemPropertiesForHost()46 void ClearSystemPropertiesForHost() {
47   std::lock_guard<std::mutex> lock(properties_mutex);
48   properties.clear();
49 }
50 
51 }  // namespace os
52 }  // namespace bluetooth