From 6fd30be0ebe150c61bb5a5a9cf0bae44b22b82b9 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Fri, 11 Jun 2021 11:32:56 +0200 Subject: [PATCH] i2c: flattening structure - remove 'direct' --- CMakeLists.txt | 2 -- include/direct/peripheral_direct_common.h | 45 ------------------------- include/direct/peripheral_direct_i2c.h | 1 - include/interface/peripheral_interface_common.h | 17 +++++++++- src/direct/peripheral_i2c_direct.c | 33 ------------------ src/peripheral_i2c.c | 14 ++++---- 6 files changed, 23 insertions(+), 89 deletions(-) delete mode 100644 include/direct/peripheral_direct_common.h delete mode 100644 include/direct/peripheral_direct_i2c.h delete mode 100644 src/direct/peripheral_i2c_direct.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 84dd024..8fea6c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,6 @@ SET(INC_DIR include) INCLUDE_DIRECTORIES(${INC_DIR}) INCLUDE_DIRECTORIES(${INC_DIR}/gdbus) INCLUDE_DIRECTORIES(${INC_DIR}/interface) -INCLUDE_DIRECTORIES(${INC_DIR}/direct) SET(SRC_DIR src) INCLUDE_DIRECTORIES(${SRC_DIR}/gdbus) @@ -61,7 +60,6 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_adc.c src/peripheral_uart.c src/peripheral_spi.c - src/direct/peripheral_i2c_direct.c src/interface/peripheral_interface_gpio.c src/interface/peripheral_interface_i2c.c src/interface/peripheral_interface_pwm.c diff --git a/include/direct/peripheral_direct_common.h b/include/direct/peripheral_direct_common.h deleted file mode 100644 index b9459b8..0000000 --- a/include/direct/peripheral_direct_common.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __PERIPHERAL_DIRECT_COMMON_H__ -#define __PERIPHERAL_DIRECT_COMMON_H__ - -#include -#include -#include -#include -#include - -#include "peripheral_log.h" - -#define MAX_ERR_LEN 255 -#define MAX_BUF_LEN 96 - -#define IF_ERROR_RETURN(expr, func...) \ - do { \ - if (expr) { \ - int temp = errno; \ - func; \ - if (temp == EAGAIN) \ - return PERIPHERAL_ERROR_TRY_AGAIN; \ - char errmsg[MAX_ERR_LEN]; \ - strerror_r(temp, errmsg, sizeof(errmsg)); \ - _E("Failed the %s(%d) function. errmsg: %s", __FUNCTION__, __LINE__, errmsg); \ - return PERIPHERAL_ERROR_IO_ERROR; \ - } \ - } while (0) - -#endif /*__PERIPHERAL_DIRECT_COMMON_H__*/ diff --git a/include/direct/peripheral_direct_i2c.h b/include/direct/peripheral_direct_i2c.h deleted file mode 100644 index 7887cea..0000000 --- a/include/direct/peripheral_direct_i2c.h +++ /dev/null @@ -1 +0,0 @@ -int peripheral_direct_i2c_open(int bus, int address, int *fd_out); diff --git a/include/interface/peripheral_interface_common.h b/include/interface/peripheral_interface_common.h index d6137ce..10f4552 100644 --- a/include/interface/peripheral_interface_common.h +++ b/include/interface/peripheral_interface_common.h @@ -26,6 +26,7 @@ #include "peripheral_log.h" #define MAX_ERR_LEN 255 +#define MAX_BUF_LEN 96 #define CHECK_ERROR(expr) \ do { \ @@ -41,9 +42,23 @@ } \ } while (0) +#define IF_ERROR_RETURN(expr, func...) \ + do { \ + if (expr) { \ + int temp = errno; \ + func; \ + if (temp == EAGAIN) \ + return PERIPHERAL_ERROR_TRY_AGAIN; \ + char errmsg[MAX_ERR_LEN]; \ + strerror_r(temp, errmsg, sizeof(errmsg)); \ + _E("Failed the %s(%d) function. errmsg: %s", __FUNCTION__, __LINE__, errmsg); \ + return PERIPHERAL_ERROR_IO_ERROR; \ + } \ + } while (0) + typedef struct predefined_type { char *type; int len; } predefined_type_s; -#endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/ \ No newline at end of file +#endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/ diff --git a/src/direct/peripheral_i2c_direct.c b/src/direct/peripheral_i2c_direct.c deleted file mode 100644 index f45a49d..0000000 --- a/src/direct/peripheral_i2c_direct.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -#include -#include - -#include "peripheral_io.h" -#include "peripheral_log.h" -#include "peripheral_interface_i2c.h" -#include "peripheral_direct_common.h" - - -int peripheral_direct_i2c_open(int bus, int address, int *fd_out) -{ - RETVM_IF(bus < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c bus"); - RETVM_IF(address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c address"); - RETVM_IF(fd_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd_out for i2c"); - - int ret; - int fd; - char path[MAX_BUF_LEN] = {0, }; - - snprintf(path, MAX_BUF_LEN, "/dev/i2c-%d", bus); - fd = open(path, O_RDWR); - IF_ERROR_RETURN(fd < 0); - - ret = ioctl(fd, I2C_SLAVE, address); - IF_ERROR_RETURN(ret != 0, close(fd)); - - *fd_out = fd; - - return PERIPHERAL_ERROR_NONE; -} diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 329dfb2..a815334 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -16,12 +16,12 @@ #include #include #include +#include #include #include "peripheral_io.h" #include "peripheral_handle.h" #include "peripheral_interface_i2c.h" -#include "peripheral_direct_i2c.h" #include "peripheral_log.h" #define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c" @@ -62,6 +62,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; int lock_type = LOCK_EX; + char path[MAX_BUF_LEN] = {0, }; RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle"); @@ -75,12 +76,11 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - ret = peripheral_direct_i2c_open(bus, address, &handle->fd); - if (ret != PERIPHERAL_ERROR_NONE) { - _E("Failed to open i2c communication, ret : %d", ret); - free(handle); - return ret; - } + snprintf(path, MAX_BUF_LEN, "/dev/i2c-%d", bus); + handle->fd = open(path, O_RDWR); + IF_ERROR_RETURN(handle->fd < 0, free(handle)); + + IF_ERROR_RETURN(ioctl(handle->fd, I2C_SLAVE, address), close(handle->fd); free(handle)); if (flags == PERIPHERAL_OPEN_FLAGS_SHARED) lock_type = LOCK_SH; -- 2.7.4