1 #pragma once 2 3 #include <fcntl.h> 4 #include <sys/ioctl.h> 5 #include <sys/stat.h> 6 7 #include <sdbusplus/async.hpp> 8 9 #include <cstdint> 10 #include <cstring> 11 #include <string> 12 13 extern "C" 14 { 15 #include <i2c/smbus.h> 16 #include <linux/i2c-dev.h> 17 #include <linux/i2c.h> 18 } 19 20 namespace phosphor::i2c 21 { 22 23 class I2C 24 { 25 public: I2C(uint16_t bus,uint16_t node)26 explicit I2C(uint16_t bus, uint16_t node) : 27 busStr("/dev/i2c-" + std::to_string(bus)), deviceNode(node) 28 { 29 open(); 30 } 31 32 I2C(I2C& i2c) = delete; 33 I2C& operator=(I2C other) = delete; 34 I2C(I2C&& other) = delete; 35 I2C& operator=(I2C&& other) = delete; 36 ~I2C()37 ~I2C() 38 { 39 this->close(); 40 } 41 42 sdbusplus::async::task<bool> sendReceive( 43 uint8_t* writeData, uint8_t writeSize, uint8_t* readData, 44 uint8_t readSize) const; 45 bool sendReceive(const std::vector<uint8_t>& writeData, 46 std::vector<uint8_t>& readData) const; 47 isOpen() const48 bool isOpen() const 49 { 50 return (fd != invalidFd); 51 } 52 53 int close() const; 54 55 private: 56 static constexpr int invalidFd = -1; 57 int fd = invalidFd; 58 std::string busStr; 59 uint16_t deviceNode; 60 int open(); 61 }; // end class I2C 62 63 } // namespace phosphor::i2c 64