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