blob: 5a01ebf3bc5d3f4c2b63501ff79f3b29df0ec8b9 [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) :
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060027 busId(busId),
George Liub9cf0d22023-02-28 10:32:42 +080028 devAddr(devAddr), maxRetries(maxRetries),
29 busStr("/dev/i2c-" + std::to_string(busId))
Lei YUab1327c2019-11-04 16:53:39 +080030 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060031 if (initialState == InitialState::OPEN)
32 {
33 open();
34 }
Lei YUab1327c2019-11-04 16:53:39 +080035 }
36
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060037 /** @brief Invalid file descriptor */
38 static constexpr int INVALID_FD = -1;
Lei YU9af82a52019-11-06 14:51:22 +080039
Shawn McCarney38ed88d2019-12-11 12:26:09 -060040 /** @brief Empty adapter functionality value with no bit flags set */
41 static constexpr unsigned long NO_FUNCS = 0;
42
Lei YU9af82a52019-11-06 14:51:22 +080043 /** @brief The I2C bus ID */
44 uint8_t busId;
45
46 /** @brief The i2c device address in the bus */
47 uint8_t devAddr;
48
Shawn McCarney770de582021-11-05 02:28:35 -050049 /** @brief Maximum number of times to retry an I2C operation */
50 int maxRetries = 0;
51
Lei YU9af82a52019-11-06 14:51:22 +080052 /** @brief The file descriptor of the opened i2c device */
Shawn McCarney38ed88d2019-12-11 12:26:09 -060053 int fd = INVALID_FD;
Lei YU9af82a52019-11-06 14:51:22 +080054
55 /** @brief The i2c bus path in /dev */
56 std::string busStr;
57
Shawn McCarney38ed88d2019-12-11 12:26:09 -060058 /** @brief Cached I2C adapter functionality value */
59 unsigned long cachedFuncs = NO_FUNCS;
60
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060061 /** @brief Check that device interface is open
62 *
63 * @throw I2CException if device is not open
64 */
65 void checkIsOpen() const
66 {
67 if (!isOpen())
68 {
69 throw I2CException("Device not open", busStr, devAddr);
70 }
71 }
72
73 /** @brief Close device without throwing an exception if an error occurs */
74 void closeWithoutException() noexcept
75 {
76 try
77 {
78 close();
79 }
80 catch (...)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000081 {}
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060082 }
83
Shawn McCarney38ed88d2019-12-11 12:26:09 -060084 /** @brief Get I2C adapter functionality
85 *
86 * Caches the adapter functionality value since it shouldn't change after
87 * opening the device.
88 *
89 * @throw I2CException on error
90 * @return Adapter functionality value
91 */
92 unsigned long getFuncs();
93
Lei YU92e89eb2019-11-06 18:08:25 +080094 /** @brief Check i2c adapter read functionality
95 *
96 * Check if the i2c adapter has the functionality specified by the SMBus
97 * transaction type
98 *
99 * @param[in] type - The SMBus transaction type defined in linux/i2c.h
100 *
101 * @throw I2CException if the function is not supported
102 */
103 void checkReadFuncs(int type);
104
Lei YU34fb8bd2019-11-07 14:24:20 +0800105 /** @brief Check i2c adapter write functionality
106 *
107 * Check if the i2c adapter has the functionality specified by the SMBus
108 * transaction type
109 *
110 * @param[in] type - The SMBus transaction type defined in linux/i2c.h
111 *
112 * @throw I2CException if the function is not supported
113 */
114 void checkWriteFuncs(int type);
115
Lei YUab1327c2019-11-04 16:53:39 +0800116 public:
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600117 /** @copydoc I2CInterface::~I2CInterface() */
Lei YU9af82a52019-11-06 14:51:22 +0800118 ~I2CDevice()
119 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600120 if (isOpen())
121 {
122 // Note: destructors must not throw exceptions
123 closeWithoutException();
124 }
Lei YU9af82a52019-11-06 14:51:22 +0800125 }
Lei YUab1327c2019-11-04 16:53:39 +0800126
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600127 /** @copydoc I2CInterface::open() */
128 void open();
129
130 /** @copydoc I2CInterface::isOpen() */
131 bool isOpen() const
132 {
133 return (fd != INVALID_FD);
134 }
135
136 /** @copydoc I2CInterface::close() */
137 void close();
138
Lei YUab1327c2019-11-04 16:53:39 +0800139 /** @copydoc I2CInterface::read(uint8_t&) */
140 void read(uint8_t& data) override;
141
142 /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */
143 void read(uint8_t addr, uint8_t& data) override;
144
145 /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */
146 void read(uint8_t addr, uint16_t& data) override;
147
Lei YU1d103422019-11-29 14:00:02 +0800148 /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */
149 void read(uint8_t addr, uint8_t& size, uint8_t* data,
150 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800151
152 /** @copydoc I2CInterface::write(uint8_t) */
153 void write(uint8_t data) override;
154
155 /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
156 void write(uint8_t addr, uint8_t data) override;
157
158 /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
159 void write(uint8_t addr, uint16_t data) override;
160
Lei YU1d103422019-11-29 14:00:02 +0800161 /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */
162 void write(uint8_t addr, uint8_t size, const uint8_t* data,
163 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800164
165 /** @brief Create an I2CInterface instance
166 *
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600167 * Automatically opens the I2CInterface if initialState is OPEN.
168 *
Lei YUab1327c2019-11-04 16:53:39 +0800169 * @param[in] busId - The i2c bus ID
170 * @param[in] devAddr - The device address of the i2c
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600171 * @param[in] initialState - Initial state of the I2CInterface object
Shawn McCarney770de582021-11-05 02:28:35 -0500172 * @param[in] maxRetries - Maximum number of times to retry an I2C operation
Lei YUab1327c2019-11-04 16:53:39 +0800173 *
174 * @return The unique_ptr holding the I2CInterface
175 */
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600176 static std::unique_ptr<I2CInterface>
177 create(uint8_t busId, uint8_t devAddr,
Shawn McCarney770de582021-11-05 02:28:35 -0500178 InitialState initialState = InitialState::OPEN,
179 int maxRetries = 0);
Lei YUab1327c2019-11-04 16:53:39 +0800180};
181
182} // namespace i2c