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 /** 13 * @class PMBus 14 * 15 * This class is an interface to communicating with PMBus devices 16 * by reading and writing sysfs files. 17 */ 18 class PMBus 19 { 20 public: 21 22 PMBus() = delete; 23 ~PMBus() = default; 24 PMBus(const PMBus&) = default; 25 PMBus& operator=(const PMBus&) = default; 26 PMBus(PMBus&&) = default; 27 PMBus& operator=(PMBus&&) = default; 28 29 /** 30 * Constructor 31 * 32 * @param[in] path - path to the sysfs directory 33 */ 34 PMBus(const std::string& path) : 35 basePath(path) 36 { 37 } 38 39 /** 40 * Reads a file in sysfs that represents a single bit, 41 * therefore doing a PMBus read. 42 * 43 * @param[in] name - path concatenated to 44 * basePath to read 45 * 46 * @return bool - false if result was 0, else true 47 */ 48 bool readBit(const std::string& name); 49 50 /** 51 * Reads a file in sysfs that represents a single bit, 52 * where the page number passed in is substituted 53 * into the name in place of the 'P' character in it. 54 * 55 * @param[in] name - path concatenated to 56 * basePath to read 57 * @param[in] page - page number 58 * 59 * @return bool - false if result was 0, else true 60 */ 61 bool readBitInPage(const std::string& name, 62 size_t page); 63 64 /** 65 * Writes an integer value to the file, therefore doing 66 * a PMBus write. 67 * 68 * @param[in] name - path concatenated to 69 * basePath to write 70 * @param[in] value - the value to write 71 */ 72 void write(const std::string& name, int value); 73 74 /** 75 * Returns the sysfs base path of this device 76 */ 77 inline const auto& path() const 78 { 79 return basePath; 80 } 81 82 /** 83 * Replaces the 'P' in the string passed in with 84 * the page number passed in. 85 * 86 * For example: 87 * insertPageNum("inP_enable", 42) 88 * returns "in42_enable" 89 * 90 * @param[in] templateName - the name string, with a 'P' in it 91 * @param[in] page - the page number to insert where the P was 92 * 93 * @return string - the new string with the page number in it 94 */ 95 static std::string insertPageNum(const std::string& templateName, 96 size_t page); 97 98 private: 99 100 /** 101 * The sysfs device path 102 */ 103 std::experimental::filesystem::path basePath; 104 105 }; 106 107 } 108 } 109