blob: d384279f5e6907f5741acb3fe01c22c3de8764fe [file] [log] [blame]
Matt Spinler015e3ad2017-08-01 11:20:47 -05001#pragma once
2
3#include <experimental/filesystem>
4#include <string>
5#include <vector>
6
7namespace witherspoon
8{
9namespace pmbus
10{
11
12/**
Matt Spinler57868bc2017-08-03 10:07:41 -050013 * If the access should be done in the base
14 * device directory or the hwmon directory.
15 */
16enum class Type
17{
18 Base,
19 Hwmon
20};
21
22/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050023 * @class PMBus
24 *
25 * This class is an interface to communicating with PMBus devices
26 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050027 *
28 * Based on the Type parameter, the accesses can either be done
29 * in the base device directory (the one passed into the constructor),
30 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -050031 */
32class PMBus
33{
34 public:
35
36 PMBus() = delete;
37 ~PMBus() = default;
38 PMBus(const PMBus&) = default;
39 PMBus& operator=(const PMBus&) = default;
40 PMBus(PMBus&&) = default;
41 PMBus& operator=(PMBus&&) = default;
42
43 /**
44 * Constructor
45 *
46 * @param[in] path - path to the sysfs directory
47 */
48 PMBus(const std::string& path) :
49 basePath(path)
50 {
Matt Spinler57868bc2017-08-03 10:07:41 -050051 findHwmonRelativePath();
Matt Spinler015e3ad2017-08-01 11:20:47 -050052 }
53
54 /**
55 * Reads a file in sysfs that represents a single bit,
56 * therefore doing a PMBus read.
57 *
58 * @param[in] name - path concatenated to
59 * basePath to read
Matt Spinler57868bc2017-08-03 10:07:41 -050060 * @param[in] type - either Base or Hwmon
Matt Spinler015e3ad2017-08-01 11:20:47 -050061 *
62 * @return bool - false if result was 0, else true
63 */
Matt Spinler57868bc2017-08-03 10:07:41 -050064 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050065
66 /**
67 * Reads a file in sysfs that represents a single bit,
68 * where the page number passed in is substituted
69 * into the name in place of the 'P' character in it.
70 *
71 * @param[in] name - path concatenated to
72 * basePath to read
73 * @param[in] page - page number
Matt Spinler57868bc2017-08-03 10:07:41 -050074 * @param[in] type - either Base or Hwmon
Matt Spinler015e3ad2017-08-01 11:20:47 -050075 *
76 * @return bool - false if result was 0, else true
77 */
78 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -050079 size_t page,
80 Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050081
82 /**
83 * Writes an integer value to the file, therefore doing
84 * a PMBus write.
85 *
86 * @param[in] name - path concatenated to
87 * basePath to write
88 * @param[in] value - the value to write
Matt Spinler57868bc2017-08-03 10:07:41 -050089 * @param[in] type - either Base or Hwmon
Matt Spinler015e3ad2017-08-01 11:20:47 -050090 */
Matt Spinler57868bc2017-08-03 10:07:41 -050091 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050092
93 /**
94 * Returns the sysfs base path of this device
95 */
96 inline const auto& path() const
97 {
98 return basePath;
99 }
100
101 /**
102 * Replaces the 'P' in the string passed in with
103 * the page number passed in.
104 *
105 * For example:
106 * insertPageNum("inP_enable", 42)
107 * returns "in42_enable"
108 *
109 * @param[in] templateName - the name string, with a 'P' in it
110 * @param[in] page - the page number to insert where the P was
111 *
112 * @return string - the new string with the page number in it
113 */
114 static std::string insertPageNum(const std::string& templateName,
115 size_t page);
116
Matt Spinler57868bc2017-08-03 10:07:41 -0500117 /**
118 * Finds the path relative to basePath to the hwmon directory
119 * for the device and stores it in hwmonRelPath.
120 */
121 void findHwmonRelativePath();
122
Matt Spinler015e3ad2017-08-01 11:20:47 -0500123 private:
124
125 /**
126 * The sysfs device path
127 */
128 std::experimental::filesystem::path basePath;
129
Matt Spinler57868bc2017-08-03 10:07:41 -0500130 /**
131 * The relative (to basePath) path to the hwmon directory
132 */
133 std::experimental::filesystem::path hwmonRelPath;
134
Matt Spinler015e3ad2017-08-01 11:20:47 -0500135};
136
137}
138}