aboutsummaryrefslogtreecommitdiff
path: root/SystemPropertiesUtils.hpp
diff options
context:
space:
mode:
authortelsoa01 <telmo.soares@arm.com>2018-03-09 13:51:08 +0000
committertelsoa01 <telmo.soares@arm.com>2018-03-09 14:05:45 +0000
commit5307bc10ac488261e84ac76b2dede6039ea3fe96 (patch)
tree09de3cc29026ca9722179f6beb25b9a66efcf88e /SystemPropertiesUtils.hpp
downloadandroid-nn-driver-5307bc10ac488261e84ac76b2dede6039ea3fe96.tar.gz
Release 18.02
Change-Id: I41a89c149534a7c354a58e2c66a32cba572fc0c1
Diffstat (limited to 'SystemPropertiesUtils.hpp')
-rw-r--r--SystemPropertiesUtils.hpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/SystemPropertiesUtils.hpp b/SystemPropertiesUtils.hpp
new file mode 100644
index 00000000..57aa98ca
--- /dev/null
+++ b/SystemPropertiesUtils.hpp
@@ -0,0 +1,83 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// See LICENSE file in the project root for full license information.
+//
+
+#pragma once
+
+#include <stdio.h>
+#include <string>
+#include <iostream>
+#include <sys/system_properties.h>
+
+namespace {
+template<typename T>
+struct ConvStringTo;
+
+template<>
+struct ConvStringTo<float>
+{
+ static float Func(std::string s) { return std::stof(s); }
+};
+
+template<>
+struct ConvStringTo<int>
+{
+ static int Func(std::string s) { return std::stoi(s); }
+};
+
+template<>
+struct ConvStringTo<bool>
+{
+ static bool Func(std::string s) { return !!std::stoi(s); }
+};
+
+template<typename T>
+void GetCapabilitiesProperties([[maybe_unused]]void* cookie,
+ [[maybe_unused]]const char *name,
+ [[maybe_unused]]const char *value,
+ [[maybe_unused]]uint32_t serial)
+{
+ T &prop = *reinterpret_cast<T*>(cookie);
+ prop = ConvStringTo<T>::Func(std::string(value));
+}
+
+template<typename T>
+T ParseSystemProperty(const char* name, T defaultValue)
+{
+ try
+ {
+ const prop_info *pInfo = __system_property_find(name);
+ if (!pInfo)
+ {
+ ALOGW("ArmnnDriver::ParseSystemProperty(): Could not find property [%s].", name);
+ } else
+ {
+ T property;
+ __system_property_read_callback(pInfo, &GetCapabilitiesProperties<T>, &property);
+ std::stringstream messageBuilder;
+ messageBuilder << "ArmnnDriver::ParseSystemProperty(): Setting [" << name << "]=[" << property << "].";
+ ALOGD("%s", messageBuilder.str().c_str());
+ return property;
+ }
+ }
+ catch(const std::invalid_argument& e)
+ {
+ ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] has invalid data type.", name);
+ }
+ catch(const std::out_of_range& e)
+ {
+ ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] out of range for the data type.", name);
+ }
+ catch (...)
+ {
+ ALOGD("ArmnnDriver::ParseSystemProperty(): Unexpected exception reading system "
+ "property [%s].", name);
+ }
+
+ std::stringstream messageBuilder;
+ messageBuilder << "ArmnnDriver::ParseSystemProperty(): Falling back to default value [" << defaultValue << "]";
+ ALOGD("%s", messageBuilder.str().c_str());
+ return defaultValue;
+}
+} //namespace \ No newline at end of file