1 #include "fake_android_property_manager.h"
2 
3 #include <android-base/parseint.h>
4 #include <android-base/result.h>
5 
6 #include <cerrno>
7 #include <map>
8 #include <string>
9 
10 #include "modem_log_constants.h"
11 
12 namespace pixel_modem {
13 
GetBoolProperty(const std::string & key,bool default_value)14 bool FakeAndroidPropertyManager::GetBoolProperty(const std::string& key,
15                                                  bool default_value) {
16   auto value_result = GetProperty(key);
17   return value_result.ok() ? (*value_result) == kTruthString : default_value;
18 }
19 
GetProperty(const std::string & key,const std::string & default_value)20 std::string FakeAndroidPropertyManager::GetProperty(
21     const std::string& key, const std::string& default_value) {
22   auto value_result = GetProperty(key);
23   return value_result.ok() ? *value_result : default_value;
24 }
25 
GetIntProperty(const std::string & key,int default_value)26 int FakeAndroidPropertyManager::GetIntProperty(const std::string& key,
27                                                int default_value) {
28   int value = default_value;
29 
30   auto property_result = GetProperty(key);
31   if (property_result.ok()) {
32     android::base::ParseInt<int>((*property_result).data(), &value);
33   }
34   return value;
35 }
36 
37 /**
38  * This function needs to copy the behaviour of `modem_logging_control` to
39  * ensure that the right properties are being set in order.
40  *
41  * More specifically, this function will also set the
42  * `kModemLoggingStatusProperty` whenever `kModemLoggingEnabledProperty` is
43  * set to simulate modem logging stopping / starting.
44  */
SetProperty(const std::string & key,const std::string & value)45 bool FakeAndroidPropertyManager::SetProperty(const std::string& key,
46                                              const std::string& value) {
47   if (key == logging::kModemLoggingEnabledProperty) {
48     property_map_[logging::kModemLoggingStatusProperty.data()] = value;
49 
50     // need to track if modem logging has restarted or not
51     if (value == kFalseString) {
52       modem_logging_has_been_off_ = true;
53     }
54     if (modem_logging_has_been_off_ && (value == kTruthString)) {
55       modem_logging_has_restarted_ = true;
56     }
57   }
58   property_map_[key] = value;
59   return true;
60 }
61 
62 /**
63  * @brief Gets android system property if present.
64  *
65  * @param[in] key Name of property.
66  *
67  * @return Status of get operation and value if successful.
68  * @retval EINVAL Key not present in map.
69  */
GetProperty(const std::string & key)70 android::base::Result<std::string> FakeAndroidPropertyManager::GetProperty(
71     const std::string& key) {
72   const auto it = property_map_.find(key);
73   if (it == property_map_.end()) {
74     return android::base::Error()
75            << "Property: " << key << " not found." << EINVAL;
76   }
77   return it->second;
78 }
79 
80 }  // namespace pixel_modem
81