blob: 5bd049fd84fbf952ea092d493d77e94afa8d4edc [file] [log] [blame]
Lei YUab1327c2019-11-04 16:53:39 +08001#pragma once
2
3#include "i2c_interface.hpp"
4
5namespace i2c
6{
7
8class I2CDevice : public I2CInterface
9{
10 private:
11 I2CDevice() = delete;
12
Lei YU9af82a52019-11-06 14:51:22 +080013 /** @brief Constructor
14 *
15 * Construct I2CDevice object from the bus id and device address
16 *
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060017 * Automatically opens the I2CDevice if initialState is OPEN.
18 *
Lei YU9af82a52019-11-06 14:51:22 +080019 * @param[in] busId - The i2c bus ID
20 * @param[in] devAddr - The device address of the I2C device
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060021 * @param[in] initialState - Initial state of the I2CDevice object
Shawn McCarney770de582021-11-05 02:28:35 -050022 * @param[in] maxRetries - Maximum number of times to retry an I2C operation
Lei YU9af82a52019-11-06 14:51:22 +080023 */
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060024 explicit I2CDevice(uint8_t busId, uint8_t devAddr,
Shawn McCarney770de582021-11-05 02:28:35 -050025 InitialState initialState = InitialState::OPEN,
26 int maxRetries = 0) :
Patrick Williamsf5402192024-08-16 15:20:53 -040027 busId(busId), devAddr(devAddr), maxRetries(maxRetries),
George Liub9cf0d22023-02-28 10:32:42 +080028 busStr("/dev/i2c-" + std::to_string(busId))
Lei YUab1327c2019-11-04 16:53:39 +080029 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060030 if (initialState == InitialState::OPEN)
31 {
32 open();
33 }
Lei YUab1327c2019-11-04 16:53:39 +080034 }
35
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060036 /** @brief Invalid file descriptor */
37 static constexpr int INVALID_FD = -1;
Lei YU9af82a52019-11-06 14:51:22 +080038
Shawn McCarney38ed88d2019-12-11 12:26:09 -060039 /** @brief Empty adapter functionality value with no bit flags set */
40 static constexpr unsigned long NO_FUNCS = 0;
41
Lei YU9af82a52019-11-06 14:51:22 +080042 /** @brief The I2C bus ID */
43 uint8_t busId;
44
45 /** @brief The i2c device address in the bus */
46 uint8_t devAddr;
47
Shawn McCarney770de582021-11-05 02:28:35 -050048 /** @brief Maximum number of times to retry an I2C operation */
49 int maxRetries = 0;
50
Lei YU9af82a52019-11-06 14:51:22 +080051 /** @brief The file descriptor of the opened i2c device */
Shawn McCarney38ed88d2019-12-11 12:26:09 -060052 int fd = INVALID_FD;
Lei YU9af82a52019-11-06 14:51:22 +080053
54 /** @brief The i2c bus path in /dev */
55 std::string busStr;
56
Shawn McCarney38ed88d2019-12-11 12:26:09 -060057 /** @brief Cached I2C adapter functionality value */
58 unsigned long cachedFuncs = NO_FUNCS;
59
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060060 /** @brief Check that device interface is open
61 *
62 * @throw I2CException if device is not open
63 */
64 void checkIsOpen() const
65 {
66 if (!isOpen())
67 {
68 throw I2CException("Device not open", busStr, devAddr);
69 }
70 }
71
72 /** @brief Close device without throwing an exception if an error occurs */
73 void closeWithoutException() noexcept
74 {
75 try
76 {
77 close();
78 }
79 catch (...)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000080 {}
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060081 }
82
Shawn McCarney38ed88d2019-12-11 12:26:09 -060083 /** @brief Get I2C adapter functionality
84 *
85 * Caches the adapter functionality value since it shouldn't change after
86 * opening the device.
87 *
88 * @throw I2CException on error
89 * @return Adapter functionality value
90 */
91 unsigned long getFuncs();
92
Lei YU92e89eb2019-11-06 18:08:25 +080093 /** @brief Check i2c adapter read functionality
94 *
95 * Check if the i2c adapter has the functionality specified by the SMBus
96 * transaction type
97 *
98 * @param[in] type - The SMBus transaction type defined in linux/i2c.h
99 *
100 * @throw I2CException if the function is not supported
101 */
102 void checkReadFuncs(int type);
103
Lei YU34fb8bd2019-11-07 14:24:20 +0800104 /** @brief Check i2c adapter write functionality
105 *
106 * Check if the i2c adapter has the functionality specified by the SMBus
107 * transaction type
108 *
109 * @param[in] type - The SMBus transaction type defined in linux/i2c.h
110 *
111 * @throw I2CException if the function is not supported
112 */
113 void checkWriteFuncs(int type);
114
Lei YUab1327c2019-11-04 16:53:39 +0800115 public:
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600116 /** @copydoc I2CInterface::~I2CInterface() */
Lei YU9af82a52019-11-06 14:51:22 +0800117 ~I2CDevice()
118 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600119 if (isOpen())
120 {
121 // Note: destructors must not throw exceptions
122 closeWithoutException();
123 }
Lei YU9af82a52019-11-06 14:51:22 +0800124 }
Lei YUab1327c2019-11-04 16:53:39 +0800125
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600126 /** @copydoc I2CInterface::open() */
127 void open();
128
129 /** @copydoc I2CInterface::isOpen() */
130 bool isOpen() const
131 {
132 return (fd != INVALID_FD);
133 }
134
135 /** @copydoc I2CInterface::close() */
136 void close();
137
Lei YUab1327c2019-11-04 16:53:39 +0800138 /** @copydoc I2CInterface::read(uint8_t&) */
139 void read(uint8_t& data) override;
140
141 /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */
142 void read(uint8_t addr, uint8_t& data) override;
143
144 /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */
145 void read(uint8_t addr, uint16_t& data) override;
146
Lei YU1d103422019-11-29 14:00:02 +0800147 /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */
148 void read(uint8_t addr, uint8_t& size, uint8_t* data,
149 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800150
151 /** @copydoc I2CInterface::write(uint8_t) */
152 void write(uint8_t data) override;
153
154 /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
155 void write(uint8_t addr, uint8_t data) override;
156
157 /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
158 void write(uint8_t addr, uint16_t data) override;
159
Lei YU1d103422019-11-29 14:00:02 +0800160 /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */
161 void write(uint8_t addr, uint8_t size, const uint8_t* data,
162 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800163
164 /** @brief Create an I2CInterface instance
165 *
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600166 * Automatically opens the I2CInterface if initialState is OPEN.
167 *
Lei YUab1327c2019-11-04 16:53:39 +0800168 * @param[in] busId - The i2c bus ID
169 * @param[in] devAddr - The device address of the i2c
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600170 * @param[in] initialState - Initial state of the I2CInterface object
Shawn McCarney770de582021-11-05 02:28:35 -0500171 * @param[in] maxRetries - Maximum number of times to retry an I2C operation
Lei YUab1327c2019-11-04 16:53:39 +0800172 *
173 * @return The unique_ptr holding the I2CInterface
174 */
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600175 static std::unique_ptr<I2CInterface>
176 create(uint8_t busId, uint8_t devAddr,
Shawn McCarney770de582021-11-05 02:28:35 -0500177 InitialState initialState = InitialState::OPEN,
178 int maxRetries = 0);
Lei YUab1327c2019-11-04 16:53:39 +0800179};
180
181} // namespace i2c