17e446a40SChristopher Meis #pragma once 27e446a40SChristopher Meis 37e446a40SChristopher Meis #include <fcntl.h> 47e446a40SChristopher Meis #include <sys/ioctl.h> 57e446a40SChristopher Meis #include <sys/stat.h> 67e446a40SChristopher Meis 77e446a40SChristopher Meis #include <sdbusplus/async.hpp> 87e446a40SChristopher Meis 97e446a40SChristopher Meis #include <cstdint> 107e446a40SChristopher Meis #include <cstring> 117e446a40SChristopher Meis #include <string> 127e446a40SChristopher Meis 137e446a40SChristopher Meis extern "C" 147e446a40SChristopher Meis { 157e446a40SChristopher Meis #include <i2c/smbus.h> 167e446a40SChristopher Meis #include <linux/i2c-dev.h> 177e446a40SChristopher Meis #include <linux/i2c.h> 187e446a40SChristopher Meis } 197e446a40SChristopher Meis 207e446a40SChristopher Meis namespace phosphor::i2c 217e446a40SChristopher Meis { 227e446a40SChristopher Meis 237e446a40SChristopher Meis class I2C 247e446a40SChristopher Meis { 257e446a40SChristopher Meis public: I2C(uint16_t bus,uint16_t node)267e446a40SChristopher Meis explicit I2C(uint16_t bus, uint16_t node) : 277e446a40SChristopher Meis busStr("/dev/i2c-" + std::to_string(bus)), deviceNode(node) 287e446a40SChristopher Meis { 297e446a40SChristopher Meis open(); 307e446a40SChristopher Meis } 317e446a40SChristopher Meis 327e446a40SChristopher Meis I2C(I2C& i2c) = delete; 337e446a40SChristopher Meis I2C& operator=(I2C other) = delete; 347e446a40SChristopher Meis I2C(I2C&& other) = delete; 357e446a40SChristopher Meis I2C& operator=(I2C&& other) = delete; 367e446a40SChristopher Meis ~I2C()377e446a40SChristopher Meis ~I2C() 387e446a40SChristopher Meis { 397e446a40SChristopher Meis this->close(); 407e446a40SChristopher Meis } 417e446a40SChristopher Meis 427e446a40SChristopher Meis sdbusplus::async::task<bool> sendReceive( 437e446a40SChristopher Meis uint8_t* writeData, uint8_t writeSize, uint8_t* readData, 447e446a40SChristopher Meis uint8_t readSize) const; 45*37a30143SDaniel Hsu bool sendReceive(const std::vector<uint8_t>& writeData, isOpen() const46*37a30143SDaniel Hsu std::vector<uint8_t>& readData) const; 477e446a40SChristopher Meis 487e446a40SChristopher Meis bool isOpen() const 497e446a40SChristopher Meis { 507e446a40SChristopher Meis return (fd != invalidFd); 517e446a40SChristopher Meis } 527e446a40SChristopher Meis 537e446a40SChristopher Meis int close() const; 547e446a40SChristopher Meis 557e446a40SChristopher Meis private: 567e446a40SChristopher Meis static constexpr int invalidFd = -1; 577e446a40SChristopher Meis int fd = invalidFd; 587e446a40SChristopher Meis std::string busStr; 597e446a40SChristopher Meis uint16_t deviceNode; 607e446a40SChristopher Meis int open(); 617e446a40SChristopher Meis }; // end class I2C 627e446a40SChristopher Meis 637e446a40SChristopher Meis } // namespace phosphor::i2c 64