1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include <stdio.h>
9 #include <string>
10 #include <iostream>
11 #include <sys/system_properties.h>
12 #include <log/log.h>
13
14 namespace {
15 template<typename T>
16 struct ConvStringTo;
17
18 template<>
19 struct ConvStringTo<float>
20 {
Func__anon4a2e62d20111::ConvStringTo21 static float Func(std::string s) { return std::stof(s); }
22 };
23
24 template<>
25 struct ConvStringTo<int>
26 {
Func__anon4a2e62d20111::ConvStringTo27 static int Func(std::string s) { return std::stoi(s); }
28 };
29
30 template<>
31 struct ConvStringTo<bool>
32 {
Func__anon4a2e62d20111::ConvStringTo33 static bool Func(std::string s) { return !!std::stoi(s); }
34 };
35
36 template<typename T>
GetCapabilitiesProperties(void * cookie,const char * name,const char * value,uint32_t serial)37 void GetCapabilitiesProperties([[maybe_unused]]void* cookie,
38 [[maybe_unused]]const char *name,
39 [[maybe_unused]]const char *value,
40 [[maybe_unused]]uint32_t serial)
41 {
42 T &prop = *reinterpret_cast<T*>(cookie);
43 prop = ConvStringTo<T>::Func(std::string(value));
44 }
45
46 template<typename T>
ParseSystemProperty(const char * name,T defaultValue)47 T ParseSystemProperty(const char* name, T defaultValue)
48 {
49 try
50 {
51 auto const prefixedName = std::string("ro.vendor.") + name;
52 const prop_info *pInfo = __system_property_find(prefixedName.c_str());
53 if (!pInfo)
54 {
55 ALOGW("ArmnnDriver::ParseSystemProperty(): Could not find property [%s].", name);
56 } else
57 {
58 T property;
59 __system_property_read_callback(pInfo, &GetCapabilitiesProperties<T>, &property);
60 std::stringstream messageBuilder;
61 messageBuilder << "ArmnnDriver::ParseSystemProperty(): Setting [" << name << "]=[" << property << "].";
62 ALOGD("%s", messageBuilder.str().c_str());
63 return property;
64 }
65 }
66 catch(const std::invalid_argument& e)
67 {
68 ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] has invalid data type.", name);
69 }
70 catch(const std::out_of_range& e)
71 {
72 ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] out of range for the data type.", name);
73 }
74 catch (...)
75 {
76 ALOGD("ArmnnDriver::ParseSystemProperty(): Unexpected exception reading system "
77 "property [%s].", name);
78 }
79
80 std::stringstream messageBuilder;
81 messageBuilder << "ArmnnDriver::ParseSystemProperty(): Falling back to default value [" << defaultValue << "]";
82 ALOGD("%s", messageBuilder.str().c_str());
83 return defaultValue;
84 }
85 } //namespace
86