1 #pragma once 2 3 #include <experimental/filesystem> 4 #include <string> 5 #include <vector> 6 7 namespace witherspoon 8 { 9 namespace pmbus 10 { 11 12 namespace fs = std::experimental::filesystem; 13 14 /** 15 * If the access should be done in the base 16 * device directory, the hwmon directory, or 17 * the debug director. 18 */ 19 enum class Type 20 { 21 Base, 22 Hwmon, 23 Debug 24 }; 25 26 /** 27 * @class PMBus 28 * 29 * This class is an interface to communicating with PMBus devices 30 * by reading and writing sysfs files. 31 * 32 * Based on the Type parameter, the accesses can either be done 33 * in the base device directory (the one passed into the constructor), 34 * or in the hwmon directory for the device. 35 */ 36 class PMBus 37 { 38 public: 39 40 PMBus() = delete; 41 ~PMBus() = default; 42 PMBus(const PMBus&) = default; 43 PMBus& operator=(const PMBus&) = default; 44 PMBus(PMBus&&) = default; 45 PMBus& operator=(PMBus&&) = default; 46 47 /** 48 * Constructor 49 * 50 * @param[in] path - path to the sysfs directory 51 */ 52 PMBus(const std::string& path) : 53 basePath(path) 54 { 55 findHwmonDir(); 56 } 57 58 /** 59 * Reads a file in sysfs that represents a single bit, 60 * therefore doing a PMBus read. 61 * 62 * @param[in] name - path concatenated to 63 * basePath to read 64 * @param[in] type - one of Base, Hwmon, or Debug 65 * 66 * @return bool - false if result was 0, else true 67 */ 68 bool readBit(const std::string& name, Type type); 69 70 /** 71 * Reads a file in sysfs that represents a single bit, 72 * where the page number passed in is substituted 73 * into the name in place of the 'P' character in it. 74 * 75 * @param[in] name - path concatenated to 76 * basePath to read 77 * @param[in] page - page number 78 * @param[in] type - one of Base, Hwmon, or Debug 79 * 80 * @return bool - false if result was 0, else true 81 */ 82 bool readBitInPage(const std::string& name, 83 size_t page, 84 Type type); 85 86 /** 87 * Writes an integer value to the file, therefore doing 88 * a PMBus write. 89 * 90 * @param[in] name - path concatenated to 91 * basePath to write 92 * @param[in] value - the value to write 93 * @param[in] type - one of Base, Hwmon, or Debug 94 */ 95 void write(const std::string& name, int value, Type type); 96 97 /** 98 * Returns the sysfs base path of this device 99 */ 100 inline const auto& path() const 101 { 102 return basePath; 103 } 104 105 /** 106 * Replaces the 'P' in the string passed in with 107 * the page number passed in. 108 * 109 * For example: 110 * insertPageNum("inP_enable", 42) 111 * returns "in42_enable" 112 * 113 * @param[in] templateName - the name string, with a 'P' in it 114 * @param[in] page - the page number to insert where the P was 115 * 116 * @return string - the new string with the page number in it 117 */ 118 static std::string insertPageNum(const std::string& templateName, 119 size_t page); 120 121 /** 122 * Finds the path relative to basePath to the hwmon directory 123 * for the device and stores it in hwmonRelPath. 124 */ 125 void findHwmonDir(); 126 127 /** 128 * Returns the path to use for the passed in type. 129 * 130 * @param[in] type - one of Base, Hwmon, or Debug 131 * 132 * @return fs::path - the full path to Base, Hwmon, or Debug path 133 */ 134 fs::path getPath(Type type); 135 136 private: 137 138 /** 139 * The sysfs device path 140 */ 141 fs::path basePath; 142 143 /** 144 * The directory name under the basePath hwmon directory 145 */ 146 fs::path hwmonDir; 147 148 /** 149 * The pmbus debug path with status files 150 */ 151 const fs::path debugPath = "/sys/kernel/debug/pmbus/"; 152 153 }; 154 155 } 156 } 157