xref: /openbmc/phosphor-hwmon/env.hpp (revision bd9bc00b8d7ce2e6631e81a9696addbc399be388)
1f3df6b4fSBrad Bishop #pragma once
2f3df6b4fSBrad Bishop 
39331ab78SPatrick Venture #include "sensorset.hpp"
4f3df6b4fSBrad Bishop 
5*bd9bc00bSKun Yi #include <fstream>
6043d3230SPatrick Venture #include <string>
7043d3230SPatrick Venture 
8043d3230SPatrick Venture namespace env
9043d3230SPatrick Venture {
107a5285deSPatrick Venture 
11*bd9bc00bSKun Yi /** @class Env
12*bd9bc00bSKun Yi  *  @brief Overridable std::getenv interface
13*bd9bc00bSKun Yi  */
14*bd9bc00bSKun Yi struct Env
15*bd9bc00bSKun Yi {
16*bd9bc00bSKun Yi     virtual ~Env() = default;
17*bd9bc00bSKun Yi 
18*bd9bc00bSKun Yi     virtual const char* get(const char* key) const = 0;
19*bd9bc00bSKun Yi };
20*bd9bc00bSKun Yi 
21*bd9bc00bSKun Yi /** @class EnvImpl
22*bd9bc00bSKun Yi  *  @brief Concrete implementation that calls std::getenv
23*bd9bc00bSKun Yi  */
24*bd9bc00bSKun Yi struct EnvImpl : public Env
25*bd9bc00bSKun Yi {
26*bd9bc00bSKun Yi     const char* get(const char* key) const override;
27*bd9bc00bSKun Yi };
28*bd9bc00bSKun Yi 
29*bd9bc00bSKun Yi /** @brief Default instantiation of Env */
30*bd9bc00bSKun Yi extern EnvImpl env_impl;
31*bd9bc00bSKun Yi 
32049e0dd2SMatt Spinler /** @brief Reads an environment variable
33049e0dd2SMatt Spinler  *
34a24c8808SPatrick Venture  *  Reads the environment for that key
35a24c8808SPatrick Venture  *
36a24c8808SPatrick Venture  *  @param[in] key - the key
37*bd9bc00bSKun Yi  *  @param[in] env - env interface that defaults to calling std::getenv
38a24c8808SPatrick Venture  *
39a24c8808SPatrick Venture  *  @return string - the env var value
40a24c8808SPatrick Venture  */
getEnv(const char * key,const Env * env=& env_impl)41*bd9bc00bSKun Yi inline std::string getEnv(const char* key, const Env* env = &env_impl)
42*bd9bc00bSKun Yi {
43*bd9bc00bSKun Yi     // Be aware value could be nullptr
44*bd9bc00bSKun Yi     auto value = env->get(key);
45*bd9bc00bSKun Yi     return (value) ? std::string(value) : std::string();
46*bd9bc00bSKun Yi }
47a24c8808SPatrick Venture 
48a24c8808SPatrick Venture /** @brief Reads an environment variable
49a24c8808SPatrick Venture  *
50049e0dd2SMatt Spinler  *  Reads <prefix>_<sensor.first><sensor.second>
51049e0dd2SMatt Spinler  *
52049e0dd2SMatt Spinler  *  @param[in] prefix - the variable prefix
53049e0dd2SMatt Spinler  *  @param[in] sensor - Sensor details
54*bd9bc00bSKun Yi  *  @param[in] env - env interface that defaults to calling std::getenv
55049e0dd2SMatt Spinler  *
56049e0dd2SMatt Spinler  *  @return string - the env var value
57049e0dd2SMatt Spinler  */
getEnv(const char * prefix,const SensorSet::key_type & sensor,const Env * env=& env_impl)58*bd9bc00bSKun Yi inline std::string getEnv(const char* prefix, const SensorSet::key_type& sensor,
59*bd9bc00bSKun Yi                           const Env* env = &env_impl)
60*bd9bc00bSKun Yi {
61*bd9bc00bSKun Yi     std::string key;
62*bd9bc00bSKun Yi 
63*bd9bc00bSKun Yi     key.assign(prefix);
64*bd9bc00bSKun Yi     key.append(1, '_');
65*bd9bc00bSKun Yi     key.append(sensor.first);
66*bd9bc00bSKun Yi     key.append(sensor.second);
67*bd9bc00bSKun Yi 
68*bd9bc00bSKun Yi     return getEnv(key.c_str(), env);
69*bd9bc00bSKun Yi }
701f8a9586STom Joseph 
71049e0dd2SMatt Spinler /** @brief Reads an environment variable, and takes type and id separately
72049e0dd2SMatt Spinler  *
73049e0dd2SMatt Spinler  *  @param[in] prefix - the variable prefix
74049e0dd2SMatt Spinler  *  @param[in] type - sensor type, like 'temp'
75049e0dd2SMatt Spinler  *  @param[in] id - sensor ID, like '5'
76*bd9bc00bSKun Yi  *  @param[in] env - env interface that defaults to calling std::getenv
77049e0dd2SMatt Spinler  *
78049e0dd2SMatt Spinler  *  @return string - the env var value
79049e0dd2SMatt Spinler  */
getEnv(const char * prefix,const std::string & type,const std::string & id,const Env * env=& env_impl)80*bd9bc00bSKun Yi inline std::string getEnv(const char* prefix, const std::string& type,
81*bd9bc00bSKun Yi                           const std::string& id, const Env* env = &env_impl)
82*bd9bc00bSKun Yi {
83*bd9bc00bSKun Yi     SensorSet::key_type sensor{type, id};
84*bd9bc00bSKun Yi     return getEnv(prefix, sensor, env);
85*bd9bc00bSKun Yi }
86049e0dd2SMatt Spinler 
8782d507d0SMatt Spinler /** @brief Gets the ID for the sensor with a level of indirection
8882d507d0SMatt Spinler  *
897c424807SMatt Spinler  *  Read the ID from the <path>/<item><X>_<suffix> file.
9082d507d0SMatt Spinler  *  <item> & <X> are populated from the sensor key.
9182d507d0SMatt Spinler  *
9282d507d0SMatt Spinler  *  @param[in] path - Directory path of the label file
937c424807SMatt Spinler  *  @param[in] fileSuffix - The file suffix
9482d507d0SMatt Spinler  *  @param[in] sensor - Sensor details
9582d507d0SMatt Spinler  */
getIndirectID(std::string path,const std::string & fileSuffix,const SensorSet::key_type & sensor)96*bd9bc00bSKun Yi inline std::string getIndirectID(std::string path,
97*bd9bc00bSKun Yi                                  const std::string& fileSuffix,
98*bd9bc00bSKun Yi                                  const SensorSet::key_type& sensor)
99*bd9bc00bSKun Yi {
100*bd9bc00bSKun Yi     std::string content;
101*bd9bc00bSKun Yi 
102*bd9bc00bSKun Yi     path.append(sensor.first);
103*bd9bc00bSKun Yi     path.append(sensor.second);
104*bd9bc00bSKun Yi     path.append(1, '_');
105*bd9bc00bSKun Yi     path.append(fileSuffix);
106*bd9bc00bSKun Yi 
107*bd9bc00bSKun Yi     std::ifstream handle(path.c_str());
108*bd9bc00bSKun Yi     if (!handle.fail())
109*bd9bc00bSKun Yi     {
110*bd9bc00bSKun Yi         content.assign((std::istreambuf_iterator<char>(handle)),
111*bd9bc00bSKun Yi                        (std::istreambuf_iterator<char>()));
112*bd9bc00bSKun Yi 
113*bd9bc00bSKun Yi         if (!content.empty())
114*bd9bc00bSKun Yi         {
115*bd9bc00bSKun Yi             // remove the newline
116*bd9bc00bSKun Yi             content.pop_back();
117*bd9bc00bSKun Yi         }
118*bd9bc00bSKun Yi     }
119*bd9bc00bSKun Yi 
120*bd9bc00bSKun Yi     return content;
121*bd9bc00bSKun Yi }
1227a5285deSPatrick Venture 
1237a5285deSPatrick Venture } // namespace env
124