Move open/close code from peripheral-bus.
Add flocks() where appropriate.
Change-Id: I696c87502d606b54a483f72990b57acf2904bb72
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})
+++ /dev/null
-/*
- * 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__ */
+++ /dev/null
-/*
- * 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;
-}
* limitations under the License.
*/
+#include <fcntl.h>
#include <stdlib.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <system_info.h>
#include "peripheral_io.h"
#include "peripheral_handle.h"
-#include "peripheral_gdbus_adc.h"
#include "peripheral_interface_adc.h"
#include "peripheral_log.h"
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;
}
*/
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;
}
/**