Commit 06680d4d authored by Johan Ström's avatar Johan Ström

i2c fix for Fedora 28

Also close file handle..
parent 8120b89b
Pipeline #372 passed with stage
in 6 minutes and 56 seconds
......@@ -4,6 +4,7 @@
#cmakedefine HAVE_I2C_SMBUS_VIA_I2C_TOOLS @HAVE_I2C_SMBUS_VIA_I2C_TOOLS@
#cmakedefine HAVE_I2C_SMBUS_VIA_LINUX_I2C_DEV @HAVE_I2C_SMBUS_VIA_LINUX_I2C_DEV@
#cmakedefine HAVE_I2C_SMBUS_VIA_LIBI2C @HAVE_I2C_SMBUS_VIA_LIBI2C@
#cmakedefine HAVE_XLOCALE_H @HAVE_XLOCALE_H@
......
......@@ -7,13 +7,23 @@ include (CheckSymbolExists)
check_symbol_exists(i2c_smbus_write_i2c_block_data i2c-tools/i2c-dev.h HAVE_I2C_SMBUS_VIA_I2C_TOOLS)
check_symbol_exists(i2c_smbus_write_i2c_block_data linux/i2c-dev.h HAVE_I2C_SMBUS_VIA_LINUX_I2C_DEV)
# Fedora 28 uses libi2c
# https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/BRUCA5P4WXL4MZLQ4Q6TP7IBDFR7WFWA/?sort=date
set(CMAKE_REQUIRED_LIBRARIES i2c)
check_symbol_exists(i2c_smbus_write_i2c_block_data i2c/smbus.h HAVE_I2C_SMBUS_VIA_LIBI2C)
set(CMAKE_REQUIRED_LIBRARIES)
if(HAVE_I2C_SMBUS_VIA_I2C_TOOLS)
message(STATUS "i2c_smbus_write_i2c_block_data available through i2c-tools/i2c-dev.h")
set(HAVE_I2C_SMBUS 1)
elseif(HAVE_I2C_SMBUS_VIA_LINUX_I2C_DEV)
message(STATUS "i2c_smbus_write_i2c_block_data available through linux/i2c-dev.h")
set(HAVE_I2C_SMBUS 1)
elseif(HAVE_I2C_SMBUS_VIA_LIBI2C)
message(STATUS "i2c_smbus_write_i2c_block_data available through i2c/smbus.h")
set(HAVE_I2C_SMBUS 1)
set(I2C_LIBRARIES i2c)
else()
message(STATUS "i2c_smbus_write_i2c_block_data not available, consider installing package providing i2c-tools")
message(STATUS "i2c_smbus_write_i2c_block_data not available, consider installing package providing i2c-tools/libi2c-devel")
set(HAVE_I2C_SMBUS 0)
endif()
......@@ -3,7 +3,7 @@ cmake_minimum_required (VERSION 3.0)
file (GLOB_RECURSE DEVICE_SOURCE_FILES *.cpp)
set (DEVICE_EXTRA_LIBRARIES
agoclient
agoclient ${I2C_LIBRARIES}
)
set (DEVICE "agoblinkm")
......
......@@ -17,14 +17,14 @@ class AgoBlinkm: public AgoApp {
private:
std::string devicefile;
bool i2ccommand(const char *device, int i2caddr, int command, size_t size, __u8 *buf);
bool i2ccommand(const char *device, int i2caddr, __u8 command, size_t size, const __u8 *buf);
Json::Value commandHandler(const Json::Value& content);
void setupApp();
public:
AGOAPP_CONSTRUCTOR(AgoBlinkm);
};
bool AgoBlinkm::i2ccommand(const char *device, int i2caddr, int command, size_t size, __u8 *buf) {
bool AgoBlinkm::i2ccommand(const char *device, int i2caddr, __u8 command, size_t size, const __u8 *buf) {
int file = open(device, O_RDWR);
if (file < 0) {
AGO_ERROR() << "cannot open " << file << " - error: " << file;
......@@ -39,9 +39,11 @@ bool AgoBlinkm::i2ccommand(const char *device, int i2caddr, int command, size_t
}
else
AGO_DEBUG() << "open i2c slave succeeded: 0x" << std::hex << i2caddr;
int result = i2c_smbus_write_i2c_block_data(file, command, size,buf);
int result = i2c_smbus_write_i2c_block_data(file, command, size, buf);
AGO_DEBUG() << "result: " << result;
close(file);
return true;
}
......
......@@ -3,7 +3,7 @@ cmake_minimum_required (VERSION 3.0)
file (GLOB_RECURSE DEVICE_SOURCE_FILES *.cpp)
set (DEVICE_EXTRA_LIBRARIES
agoclient
agoclient ${I2C_LIBRARIES}
)
set (DEVICE "agoi2c")
......
......@@ -29,8 +29,8 @@ private:
Json::Value commandHandler(const Json::Value& content);
void readBMP085();
bool i2ccommand(const char *device, int i2caddr, int command, size_t size, __u8 *buf);
bool i2cread(const char *device, int i2caddr, int command, size_t size, __u8 &buf);
bool i2ccommand(const char *device, int i2caddr, __u8 command, size_t size, const __u8 *buf);
bool i2cread(const char *device, int i2caddr, __u8 command, size_t size, __u8 *buf);
bool set_pcf8574_output(const char *device, int i2caddr, int output, bool state);
bool get_pcf8574_state(const char *device, int i2caddr, uint8_t &state);
public:
......@@ -57,7 +57,7 @@ void AgoI2c::readBMP085(){
}
}
bool AgoI2c::i2ccommand(const char *device, int i2caddr, int command, size_t size, __u8 *buf) {
bool AgoI2c::i2ccommand(const char *device, int i2caddr, __u8 command, size_t size, const __u8 *buf) {
int file = open(device, O_RDWR);
if (file < 0) {
AGO_FATAL() << "Cannot open device: " << device << " - error: " << file;
......@@ -72,13 +72,14 @@ bool AgoI2c::i2ccommand(const char *device, int i2caddr, int command, size_t siz
}
else
AGO_DEBUG() << "Open i2c slave succeeded: 0x" << std::hex << i2caddr;
int result = i2c_smbus_write_i2c_block_data(file, command, size,buf);
int result = i2c_smbus_write_i2c_block_data(file, command, size, buf);
AGO_DEBUG() << "result: " << result;
return true;
}
bool AgoI2c::i2cread(const char *device, int i2caddr, int command, size_t size, __u8 &buf) {
bool AgoI2c::i2cread(const char *device, int i2caddr, __u8 command, size_t size, __u8 *buf) {
int file = open(device, O_RDWR);
if (file < 0) {
AGO_FATAL() << "Cannot open device: " << device << " - error: " << file;
......@@ -94,9 +95,10 @@ bool AgoI2c::i2cread(const char *device, int i2caddr, int command, size_t size,
else
AGO_DEBUG() << "Open i2c slave succeeded: 0x" << std::hex << i2caddr;
int result = i2c_smbus_read_i2c_block_data(file, command, size,&buf);
int result = i2c_smbus_read_i2c_block_data(file, command, size, buf);
AGO_DEBUG() << "result: " << result;
close(file);
return true;
}
......
......@@ -10,9 +10,15 @@
#if HAVE_I2C_SMBUS_VIA_I2C_TOOLS
#include <i2c-tools/i2c-dev.h>
#elif HAVE_I2C_SMBUS_VIA_LINUX_I2C_DEV
// Fedora 27 etc
#include <linux/i2c-dev.h>
#elif HAVE_I2C_SMBUS_VIA_LIBI2C
extern "C" {
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
};
#else
#error "i2c-dev.h not found"
#error "No i2c smbus support"
#endif
#endif //AGOCONTROL_I2C_INCLUDE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment