aboutsummaryrefslogtreecommitdiff
path: root/SystemPropertiesUtils.hpp
blob: 78e5ac7b1a157b8b339e4552130b466adcb1be75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include <stdio.h>
#include <string>
#include <iostream>
#include <sys/system_properties.h>
#include <log/log.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