From: Mateusz Moscicki Date: Wed, 9 Jun 2021 09:23:25 +0000 (+0200) Subject: test direct X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fsandbox%2Fmmoscicki2%2Fdirect;p=platform%2Fcore%2Fapi%2Fperipheral-io.git test direct --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 43c0b29..8767b29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ 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) @@ -60,6 +61,7 @@ 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 new file mode 100644 index 0000000..b9459b8 --- /dev/null +++ b/include/direct/peripheral_direct_common.h @@ -0,0 +1,45 @@ +/* + * 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 new file mode 100644 index 0000000..7887cea --- /dev/null +++ b/include/direct/peripheral_direct_i2c.h @@ -0,0 +1 @@ +int peripheral_direct_i2c_open(int bus, int address, int *fd_out); diff --git a/src/direct/peripheral_i2c_direct.c b/src/direct/peripheral_i2c_direct.c new file mode 100644 index 0000000..f45a49d --- /dev/null +++ b/src/direct/peripheral_i2c_direct.c @@ -0,0 +1,33 @@ +#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 5f493f2..324f8bd 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include #include @@ -21,6 +20,7 @@ #include "peripheral_handle.h" #include "peripheral_gdbus_i2c.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" @@ -71,7 +71,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - ret = peripheral_gdbus_i2c_open(handle, bus, address); + 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); @@ -117,9 +117,9 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - ret = peripheral_gdbus_i2c_close(i2c); - if (ret != PERIPHERAL_ERROR_NONE) - _E("Failed to close i2c communcation, ret : %d", ret); + // ret = peripheral_gdbus_i2c_close(i2c); + // if (ret != PERIPHERAL_ERROR_NONE) + // _E("Failed to close i2c communcation, ret : %d", ret); peripheral_interface_i2c_close(i2c);