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 (path type) 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 (path type) 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 * Read byte(s) from file in sysfs. 87 * 88 * @param[in] name - path concatenated to basePath to read 89 * @param[in] type - one of Base, Hwmon, or Debug (path type) 90 * 91 * @return uint64_t - Up to 8 bytes of data read from file. 92 */ 93 uint64_t read(const std::string& name, Type type); 94 95 /** 96 * Writes an integer value to the file, therefore doing 97 * a PMBus write. 98 * 99 * @param[in] name - path concatenated to 100 * basePath to write 101 * @param[in] value - the value to write 102 * @param[in] type - one of Base, Hwmon, or Debug (path type) 103 */ 104 void write(const std::string& name, int value, Type type); 105 106 /** 107 * Returns the sysfs base path of this device 108 */ 109 inline const auto& path() const 110 { 111 return basePath; 112 } 113 114 /** 115 * Replaces the 'P' in the string passed in with 116 * the page number passed in. 117 * 118 * For example: 119 * insertPageNum("inP_enable", 42) 120 * returns "in42_enable" 121 * 122 * @param[in] templateName - the name string, with a 'P' in it 123 * @param[in] page - the page number to insert where the P was 124 * 125 * @return string - the new string with the page number in it 126 */ 127 static std::string insertPageNum(const std::string& templateName, 128 size_t page); 129 130 /** 131 * Finds the path relative to basePath to the hwmon directory 132 * for the device and stores it in hwmonRelPath. 133 */ 134 void findHwmonDir(); 135 136 /** 137 * Returns the path to use for the passed in type. 138 * 139 * @param[in] type - one of Base, Hwmon, or Debug (path type) 140 * 141 * @return fs::path - the full path to Base, Hwmon, or Debug path 142 */ 143 fs::path getPath(Type type); 144 145 private: 146 147 /** 148 * The sysfs device path 149 */ 150 fs::path basePath; 151 152 /** 153 * The directory name under the basePath hwmon directory 154 */ 155 fs::path hwmonDir; 156 157 /** 158 * The pmbus debug path with status files 159 */ 160 const fs::path debugPath = "/sys/kernel/debug/pmbus/"; 161 162 }; 163 164 } 165 } 166