blob: e86d574da7956785eb2dcd3fe5a1a09662d4a82e [file] [log] [blame]
Christopher Meis7e446a42024-10-22 09:36:41 +02001#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
13extern "C"
14{
15#include <i2c/smbus.h>
16#include <linux/i2c-dev.h>
17#include <linux/i2c.h>
18}
19
20namespace phosphor::i2c
21{
22
23class I2C
24{
25 public:
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
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
46 bool isOpen() const
47 {
48 return (fd != invalidFd);
49 }
50
51 int close() const;
52
53 private:
54 static constexpr int invalidFd = -1;
55 int fd = invalidFd;
56 std::string busStr;
57 uint16_t deviceNode;
58 int open();
59}; // end class I2C
60
61} // namespace phosphor::i2c