From 8e5aba86533abe49ee9844be94600cd513c18301 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Sat, 16 Jul 2016 02:16:09 -0300 Subject: [PATCH] AP_HAL_Linux: I2CDevice: move implementation to outside As we add methods, it's becoming too complex to be implemented together with the declaration. --- libraries/AP_HAL_Linux/I2CDevice.cpp | 64 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/libraries/AP_HAL_Linux/I2CDevice.cpp b/libraries/AP_HAL_Linux/I2CDevice.cpp index 72b20a3852..59e48b907d 100644 --- a/libraries/AP_HAL_Linux/I2CDevice.cpp +++ b/libraries/AP_HAL_Linux/I2CDevice.cpp @@ -75,44 +75,46 @@ static inline char *startswith(const char *s, const char *prefix) /* Private struct to maintain for each bus */ class I2CBus { public: - ~I2CBus() - { - if (fd >= 0) { - ::close(fd); - } - } - - int open(uint8_t n) - { - char path[sizeof("/dev/i2c-XXX")]; - int r; - - if (fd >= 0) { - return -EBUSY; - } - - r = snprintf(path, sizeof(path), "/dev/i2c-%u", n); - if (r < 0 || r >= (int)sizeof(path)) { - return -EINVAL; - } - - fd = ::open(path, O_RDWR | O_CLOEXEC); - if (fd < 0) { - return -errno; - } - - bus = n; - - return fd; - } + ~I2CBus(); + int open(uint8_t n); Semaphore sem; int fd = -1; uint8_t bus; - uint8_t ref; }; +I2CBus::~I2CBus() +{ + if (fd >= 0) { + ::close(fd); + } +} + +int I2CBus::open(uint8_t n) +{ + char path[sizeof("/dev/i2c-XXX")]; + int r; + + if (fd >= 0) { + return -EBUSY; + } + + r = snprintf(path, sizeof(path), "/dev/i2c-%u", n); + if (r < 0 || r >= (int)sizeof(path)) { + return -EINVAL; + } + + fd = ::open(path, O_RDWR | O_CLOEXEC); + if (fd < 0) { + return -errno; + } + + bus = n; + + return fd; +} + I2CDevice::~I2CDevice() { // Unregister itself from the I2CDeviceManager