blob: 89282ac5232470165ea9ebe79f615796ea19a940 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#pragma once
7
8#include <stdio.h>
9#include <string>
10#include <iostream>
11#include <sys/system_properties.h>
telsoa01ce3e84a2018-08-31 09:31:35 +010012#include <log/log.h>
telsoa015307bc12018-03-09 13:51:08 +000013
14namespace {
15template<typename T>
16struct ConvStringTo;
17
18template<>
19struct ConvStringTo<float>
20{
21 static float Func(std::string s) { return std::stof(s); }
22};
23
24template<>
25struct ConvStringTo<int>
26{
27 static int Func(std::string s) { return std::stoi(s); }
28};
29
30template<>
31struct ConvStringTo<bool>
32{
33 static bool Func(std::string s) { return !!std::stoi(s); }
34};
35
36template<typename T>
37void 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
46template<typename T>
47T ParseSystemProperty(const char* name, T defaultValue)
48{
49 try
50 {
Bruno BELANYI6b8dc382023-04-05 14:47:40 +000051 auto const prefixedName = std::string("ro.vendor.") + name;
52 const prop_info *pInfo = __system_property_find(prefixedName.c_str());
telsoa015307bc12018-03-09 13:51:08 +000053 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}
telsoa01ce3e84a2018-08-31 09:31:35 +010085} //namespace