From 69bcfb98ba7e38ed4f78f8610e152a2a07d08391 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Mon, 14 Jun 2021 10:44:20 +0200 Subject: [PATCH] adc: replace gdbus with direct implementation Move open/close code from peripheral-bus. Add flocks() where appropriate. Change-Id: I696c87502d606b54a483f72990b57acf2904bb72 --- CMakeLists.txt | 1 - include/gdbus/peripheral_gdbus_adc.h | 25 ------- src/gdbus/peripheral_gdbus_adc.c | 125 ----------------------------------- src/peripheral_adc.c | 55 +++++++++------ 4 files changed, 33 insertions(+), 173 deletions(-) delete mode 100644 include/gdbus/peripheral_gdbus_adc.h delete mode 100644 src/gdbus/peripheral_gdbus_adc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e1b8631..0e3198b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,6 @@ SET(SOURCES src/peripheral_gpio.c src/interface/peripheral_interface_adc.c src/gdbus/peripheral_gdbus_gpio.c src/gdbus/peripheral_gdbus_pwm.c - src/gdbus/peripheral_gdbus_adc.c src/gdbus/peripheral_io_gdbus.c) ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) diff --git a/include/gdbus/peripheral_gdbus_adc.h b/include/gdbus/peripheral_gdbus_adc.h deleted file mode 100644 index ffb1af7..0000000 --- a/include/gdbus/peripheral_gdbus_adc.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2018 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_GDBUS_ADC_H__ -#define __PERIPHERAL_GDBUS_ADC_H__ - -#include "peripheral_gdbus_common.h" - -int peripheral_gdbus_adc_open(peripheral_adc_h adc, int device, int channel); -int peripheral_gdbus_adc_close(peripheral_adc_h adc); - -#endif /* __PERIPHERAL_GDBUS_ADC_H__ */ diff --git a/src/gdbus/peripheral_gdbus_adc.c b/src/gdbus/peripheral_gdbus_adc.c deleted file mode 100644 index 30cc9b4..0000000 --- a/src/gdbus/peripheral_gdbus_adc.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2018 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. - */ - -#include "peripheral_gdbus_adc.h" - -#define ADC_FD_INDEX 0 - -static PeripheralIoGdbusAdc *adc_proxy = NULL; - -static int __adc_proxy_init(void) -{ - GError *error = NULL; - - if (adc_proxy != NULL) { - _E("Adc proxy is already created"); - g_object_ref(adc_proxy); - return PERIPHERAL_ERROR_NONE; - } - - adc_proxy = peripheral_io_gdbus_adc_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - PERIPHERAL_GDBUS_NAME, - PERIPHERAL_GDBUS_ADC_PATH, - NULL, - &error); - - if (adc_proxy == NULL) { - if (error) { - _E("Failed to create adc proxy : %s", error->message); - g_error_free(error); - } - return PERIPHERAL_ERROR_IO_ERROR; - } - - return PERIPHERAL_ERROR_NONE; -} - -static int __adc_proxy_deinit(void) -{ - RETVM_IF(adc_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Adc proxy is NULL"); - - g_object_unref(adc_proxy); - if (!G_IS_OBJECT(adc_proxy)) - adc_proxy = NULL; - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_gdbus_adc_open(peripheral_adc_h adc, int device, int channel) -{ - int ret; - GError *error = NULL; - GUnixFDList *fd_list = NULL; - - ret = __adc_proxy_init(); - if (ret != PERIPHERAL_ERROR_NONE) - return ret; - - if (peripheral_io_gdbus_adc_call_open_sync( - adc_proxy, - device, - channel, - NULL, - &adc->handle, - &ret, - &fd_list, - NULL, - &error) == FALSE) { - _E("Failed to request daemon to adc open : %s", error->message); - g_error_free(error); - return PERIPHERAL_ERROR_IO_ERROR; - } - - // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon. - if (ret != PERIPHERAL_ERROR_NONE) - return ret; - - adc->fd = g_unix_fd_list_get(fd_list, ADC_FD_INDEX, &error); - if (adc->fd < 0) { - _E("Failed to get fd for adc : %s", error->message); - g_error_free(error); - ret = PERIPHERAL_ERROR_IO_ERROR; - } - - g_object_unref(fd_list); - - return ret; -} - -int peripheral_gdbus_adc_close(peripheral_adc_h adc) -{ - RETVM_IF(adc_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Adc proxy is NULL"); - - int ret; - GError *error = NULL; - - if (peripheral_io_gdbus_adc_call_close_sync( - adc_proxy, - adc->handle, - &ret, - NULL, - &error) == FALSE) { - _E("Failed to request daemon to adc close : %s", error->message); - g_error_free(error); - return PERIPHERAL_ERROR_IO_ERROR; - } - - __adc_proxy_deinit(); - - return ret; -} diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 84baf24..bf45995 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -14,12 +14,15 @@ * limitations under the License. */ +#include #include +#include +#include +#include #include #include "peripheral_io.h" #include "peripheral_handle.h" -#include "peripheral_gdbus_adc.h" #include "peripheral_interface_adc.h" #include "peripheral_log.h" @@ -45,34 +48,52 @@ static bool __is_feature_supported(void) return (adc_feature == ADC_FEATURE_TRUE ? true : false); } +static inline void cleanup_handlep(peripheral_adc_h *handle) +{ + if (*handle != NULL) { + if ((*handle)->fd != -1) + close((*handle)->fd); + free(*handle); + } +} + /** * @brief Initializes adc pin and creates adc handle. */ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) { - int ret = PERIPHERAL_ERROR_NONE; - peripheral_adc_h handle; - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc handle"); RETVM_IF(device < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc device number"); RETVM_IF(channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc channel number"); + __attribute__ ((cleanup(cleanup_handlep))) peripheral_adc_h handle = NULL; handle = (peripheral_adc_h)calloc(1, sizeof(struct _peripheral_adc_s)); if (handle == NULL) { _E("Failed to allocate peripheral_adc_h"); return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - ret = peripheral_gdbus_adc_open(handle, device, channel); - if (ret != PERIPHERAL_ERROR_NONE) { - _E("Failed to open the adc pin, ret : %d", ret); - free(handle); - handle = NULL; - return ret; +#define DEV_PATH_BASE(device, channel) ("/sys/bus/iio/devices/iio:device" device "/in_voltage" channel "_raw") + /* space for /sys/bus/iio/devices/iio:device%d/in_voltage%d_raw */ + char path[sizeof(DEV_PATH_BASE("1234567890", "1234567890"))] = {0, }; + + snprintf(path, sizeof path, DEV_PATH_BASE("%d", "%d"), device, channel); + handle->fd = open(path, O_RDONLY | O_CLOEXEC); + CHECK_ERROR(handle->fd < 0); + + if (flock(handle->fd, LOCK_EX | LOCK_NB)) { + if (errno == EWOULDBLOCK) { + _E("device : %d, channel : 0x%x is not available", device, channel); + return PERIPHERAL_ERROR_RESOURCE_BUSY; + } else { + _E("device : %d, channel : 0x%x flock() error: %d", device, channel, errno); + return PERIPHERAL_ERROR_IO_ERROR; + } } *adc = handle; + handle = NULL; return PERIPHERAL_ERROR_NONE; } @@ -82,22 +103,12 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) */ int peripheral_adc_close(peripheral_adc_h adc) { - int ret = PERIPHERAL_ERROR_NONE; - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc handle is NULL"); - /* call adc_close */ - ret = peripheral_gdbus_adc_close(adc); - if (ret != PERIPHERAL_ERROR_NONE) - _E("Failed to close the adc pin, ret : %d", ret); - - peripheral_interface_adc_close(adc); + cleanup_handlep(&adc); - free(adc); - adc = NULL; - - return ret; + return PERIPHERAL_ERROR_NONE; } /** -- 2.7.4