blob: d230ef730112893e761da309d3cd33cf596664da [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;
Daniel Hsu37a30142025-06-12 17:57:24 +080045 bool sendReceive(const std::vector<uint8_t>& writeData,
46 std::vector<uint8_t>& readData) const;
Christopher Meis7e446a42024-10-22 09:36:41 +020047
48 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