Skip to content

Commit

Permalink
I2CPort: Compile successfully without the SMBUS library.
Browse files Browse the repository at this point in the history
Compile succesfully in the absence of <i2c/smbus.h>

Signed-off-by: Kai Horstmann <horstmannkai@hotmail.com>
  • Loading branch information
hor63 committed Jul 16, 2024
1 parent 0a25cee commit db0f756
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/main/GliderVarioMainPriv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,10 @@ void GliderVarioMainPriv::registerPortDrivers() {
io::SerialPort::registerSerialPortType();
#endif

#if HAVE_I2C_H
#if HAVE_I2C_DEV_H
io::I2CPort::registerI2CPortType();
#else
# error Sorry folks. Use of the I2C Linux kernel device interface is hard-coded in the driver. Without most sensors do no work.
#endif

}
Expand Down
72 changes: 55 additions & 17 deletions src/util/io/I2CPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@
# include "config.h"
#endif

#if HAVE_I2C_DEV_H


#include <errno.h>
#include <unistd.h>

#include <sys/ioctl.h>

#if HAVE_I2C_DEV_H
# include <linux/i2c-dev.h>
#endif

#if HAVE_I2C_H
# include <linux/i2c.h>
#endif

#if HAVE_I2C_DEV_H
# include <linux/i2c-dev.h>
#endif

#if HAVE_SMBUS_H
// Treat the prototypes in i2c/smbus.h as C prototypes, not C++
extern "C" {
Expand Down Expand Up @@ -118,7 +121,7 @@ void I2CPort::openInternal() {
throw GliderVarioPortOpenException (__FILE__, __LINE__, str.c_str(),errN);
}

#if HAVE_I2C_H
#if HAVE_I2C_DEV_H
if (funcs & I2C_FUNC_I2C) {
useRawI2C = true;
}
Expand All @@ -136,11 +139,11 @@ void I2CPort::openInternal() {
if ((funcs & I2C_FUNC_SMBUS_BYTE_DATA) == I2C_FUNC_SMBUS_BYTE_DATA) {
useSimpleByteAddressSmBusFunctions = true;
}
#endif // if HAVE_SMBUS_H

if (funcs & I2C_FUNC_10BIT_ADDR) {
allow10BitAddress = true;
}
#endif // if HAVE_SMBUS_H

LOG4CXX_DEBUG(logger,__FUNCTION__ << "Device " << getDeviceName() << " functions = 0x" << std::hex << funcs << std::dec << ": "
<< " useRawI2C = " << useRawI2C
Expand Down Expand Up @@ -194,7 +197,9 @@ void I2CPort::writeByte(uint16_t devAddr, uint8_t data) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Wrote 0x" << std::hex << uint32_t(data) << std::dec
<< " with ioctl to " << getDeviceName());

} else if (useSimpleByteSmBusFunctions && !is10BitAddr) {
} else
#if HAVE_SMBUS_H
if (useSimpleByteSmBusFunctions && !is10BitAddr) {
rc = i2c_smbus_write_byte(devLock.deviceHandle, data);
if (rc < 0) {
auto errN = -rc;
Expand All @@ -209,7 +214,9 @@ void I2CPort::writeByte(uint16_t devAddr, uint8_t data) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Wrote 0x" << std::hex << uint32_t(data) << std::dec
<< " with i2c_smbus_write_byte to " << getDeviceName());

} else {
} else
#endif // #if HAVE_SMBUS_H
{
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand Down Expand Up @@ -271,6 +278,7 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
break;
case 1:

#if HAVE_SMBUS_H
if (useSimpleByteSmBusFunctions) {

rc = i2c_smbus_write_byte(devLock.deviceHandle, *data);
Expand All @@ -287,7 +295,10 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Wrote 0x" << std::hex << uint32_t(*data) << std::dec
<< " with i2c_smbus_write_byte to " << getDeviceName());

} else {
} else
#endif // #if HAVE_SMBUS_H

{
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand All @@ -298,6 +309,7 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
break;
case 2:

#if HAVE_SMBUS_H
if (useSimpleByteAddressSmBusFunctions) {

rc = i2c_smbus_write_byte_data(devLock.deviceHandle,data[0],data[1]);
Expand All @@ -315,7 +327,10 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
<< " to register 0x" << uint32_t(data[0]) << std::dec
<< " with i2c_smbus_write_byte_data to " << getDeviceName());

} else {
} else
#endif // #if HAVE_SMBUS_H

{
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand All @@ -326,6 +341,7 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
break;
default:

#if HAVE_SMBUS_H
if (useSmBusI2CBlockFunctions) {

rc = i2c_smbus_write_i2c_block_data(devLock.deviceHandle,data[0],dataLen-1,data+1);
Expand All @@ -341,7 +357,10 @@ void I2CPort::writeBlock(uint16_t devAddr, uint8_t *data, uint16_t dataLen) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Wrote " << dataLen
<< " bytes with i2c_smbus_write_i2c_block_data to " << getDeviceName());

} else {
} else
#endif // #if HAVE_SMBUS_H

{
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand Down Expand Up @@ -398,7 +417,10 @@ uint8_t I2CPort::readByte(uint16_t devAddr) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Read 0x" << std::hex << uint32_t(data) << std::dec
<< " with ioctl from " << getDeviceName());

} else if (useSimpleByteSmBusFunctions && !is10BitAddr) {
}

#if HAVE_SMBUS_H
else if (useSimpleByteSmBusFunctions && !is10BitAddr) {

rc = i2c_smbus_read_byte(devLock.deviceHandle);
if (rc < 0) {
Expand All @@ -416,7 +438,9 @@ uint8_t I2CPort::readByte(uint16_t devAddr) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Read 0x" << std::hex << uint32_t(data) << std::dec
<< " with i2c_smbus_read_byte from " << getDeviceName());

} else {
}
#endif // #if HAVE_SMBUS_H
else {
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand Down Expand Up @@ -474,7 +498,10 @@ uint8_t I2CPort::readByteAtRegAddrByte(uint16_t devAddr, uint8_t regAddr) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Read 0x" << std::hex << uint32_t(data)
<< " with ioctl from register 0x" << uint32_t(regAddr) << std::dec << " from " << getDeviceName());

} else if (useSimpleByteAddressSmBusFunctions && !is10BitAddr) {
}
#if HAVE_SMBUS_H

else if (useSimpleByteAddressSmBusFunctions && !is10BitAddr) {

rc = i2c_smbus_read_byte_data(devLock.deviceHandle,regAddr);
if (rc < 0) {
Expand All @@ -492,7 +519,10 @@ uint8_t I2CPort::readByteAtRegAddrByte(uint16_t devAddr, uint8_t regAddr) {
LOG4CXX_TRACE(logger, __FUNCTION__ << ": Read 0x" << std::hex << uint32_t(data)
<< " with i2c_smbus_read_byte_data from register 0x" << uint32_t(regAddr) << std::dec << " from " << getDeviceName());

} else {
}
#endif // #if HAVE_SMBUS_H

else {
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand Down Expand Up @@ -603,7 +633,10 @@ void I2CPort::readBlockAtRegAddrByte(
<< " bytes with ioctl from register 0x"
<< std::hex << uint32_t(regAddr) << std::dec << " from " << getDeviceName());

} else if (useSmBusI2CBlockFunctions && !is10BitAddr) {
}
#if HAVE_SMBUS_H

else if (useSmBusI2CBlockFunctions && !is10BitAddr) {

rc = i2c_smbus_read_i2c_block_data(devLock.deviceHandle,regAddr,dataLen,data);
if (rc < 0) {
Expand All @@ -620,7 +653,10 @@ void I2CPort::readBlockAtRegAddrByte(
<< " bytes with i2c_smbus_read_i2c_block_data from register 0x"
<< std::hex << uint32_t(regAddr) << std::dec << " from " << getDeviceName());

} else {
}
#endif // #if HAVE_SMBUS_H

else {
auto str = fmt::format(_("{0}, port \"{1}\": Neither SMBus nor direct I2C is supported."),
__PRETTY_FUNCTION__,getPortName());

Expand Down Expand Up @@ -711,3 +747,5 @@ bool I2CPort::check10BitAddr(uint16_t devAddr) {

} /* namespace io */
} /* namespace openEV */

#endif // #if HAVE_I2C_DEV_H
7 changes: 7 additions & 0 deletions src/util/io/I2CPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#ifndef UTIL_IO_I2CPORT_H_
#define UTIL_IO_I2CPORT_H_

#if HAVE_I2C_DEV_H

#define HAVE_I2C_PORT 1

#include <cstdint>

#include <mutex>
Expand Down Expand Up @@ -218,4 +222,7 @@ class OEV_UTILS_PUBLIC I2CPort: public PortBase {
} /* namespace io */
} /* namespace openEV */

#endif // #if HAVE_I2C_DEV_H


#endif /* UTIL_IO_I2CPORT_H_ */

0 comments on commit db0f756

Please sign in to comment.