blob: 78e5ac7b1a157b8b339e4552130b466adcb1be75 [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 {
51 const prop_info *pInfo = __system_property_find(name);
52 if (!pInfo)
53 {
54 ALOGW("ArmnnDriver::ParseSystemProperty(): Could not find property [%s].", name);
55 } else
56 {
57 T property;
58 __system_property_read_callback(pInfo, &GetCapabilitiesProperties<T>, &property);
59 std::stringstream messageBuilder;
60 messageBuilder << "ArmnnDriver::ParseSystemProperty(): Setting [" << name << "]=[" << property << "].";
61 ALOGD("%s", messageBuilder.str().c_str());
62 return property;
63 }
64 }
65 catch(const std::invalid_argument& e)
66 {
67 ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] has invalid data type.", name);
68 }
69 catch(const std::out_of_range& e)
70 {
71 ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] out of range for the data type.", name);
72 }
73 catch (...)
74 {
75 ALOGD("ArmnnDriver::ParseSystemProperty(): Unexpected exception reading system "
76 "property [%s].", name);
77 }
78
79 std::stringstream messageBuilder;
80 messageBuilder << "ArmnnDriver::ParseSystemProperty(): Falling back to default value [" << defaultValue << "]";
81 ALOGD("%s", messageBuilder.str().c_str());
82 return defaultValue;
83}
telsoa01ce3e84a2018-08-31 09:31:35 +010084} //namespace