spi: replace gdbus with direct implementation 55/259755/6
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 11 Jun 2021 12:50:22 +0000 (14:50 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Fri, 25 Jun 2021 06:01:51 +0000 (08:01 +0200)
Move open/close code from peripheral-bus.
Add flocks() where appropriate.

Change-Id: Id68566050afd74f1b6d0e42a87e239150ce4364f

CMakeLists.txt
include/gdbus/peripheral_gdbus_spi.h [deleted file]
src/gdbus/peripheral_gdbus_spi.c [deleted file]
src/peripheral_spi.c

index e3c144c..e5b255c 100644 (file)
@@ -69,7 +69,6 @@ SET(SOURCES src/peripheral_gpio.c
                        src/gdbus/peripheral_gdbus_pwm.c
                        src/gdbus/peripheral_gdbus_adc.c
                        src/gdbus/peripheral_gdbus_uart.c
-                       src/gdbus/peripheral_gdbus_spi.c
                        src/gdbus/peripheral_io_gdbus.c)
 
 ADD_LIBRARY(${fw_name} SHARED ${SOURCES})
diff --git a/include/gdbus/peripheral_gdbus_spi.h b/include/gdbus/peripheral_gdbus_spi.h
deleted file mode 100644 (file)
index 94b2242..0000000
+++ /dev/null
@@ -1,25 +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_GDBUS_SPI_H_
-#define __PERIPHERAL_GDBUS_SPI_H_
-
-#include "peripheral_gdbus_common.h"
-
-int peripheral_gdbus_spi_open(peripheral_spi_h spi, int bus, int cs);
-int peripheral_gdbus_spi_close(peripheral_spi_h spi);
-
-#endif /* __PERIPHERAL_GDBUS_SPI_H_ */
diff --git a/src/gdbus/peripheral_gdbus_spi.c b/src/gdbus/peripheral_gdbus_spi.c
deleted file mode 100644 (file)
index 9fcf339..0000000
+++ /dev/null
@@ -1,125 +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.
- */
-
-#include "peripheral_gdbus_spi.h"
-
-#define SPI_FD_INDEX 0
-
-static PeripheralIoGdbusSpi *spi_proxy = NULL;
-
-static int __spi_proxy_init(void)
-{
-       GError *error = NULL;
-
-       if (spi_proxy != NULL) {
-               _E("Spi proxy is already created");
-               g_object_ref(spi_proxy);
-               return PERIPHERAL_ERROR_NONE;
-       }
-
-       spi_proxy = peripheral_io_gdbus_spi_proxy_new_for_bus_sync(
-               G_BUS_TYPE_SYSTEM,
-               G_DBUS_PROXY_FLAGS_NONE,
-               PERIPHERAL_GDBUS_NAME,
-               PERIPHERAL_GDBUS_SPI_PATH,
-               NULL,
-               &error);
-
-       if (spi_proxy == NULL) {
-               if (error) {
-                       _E("Failed to create spi proxy : %s", error->message);
-                       g_error_free(error);
-               }
-               return PERIPHERAL_ERROR_IO_ERROR;
-       }
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-static int __spi_proxy_deinit(void)
-{
-       RETVM_IF(spi_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Spi proxy is NULL");
-
-       g_object_unref(spi_proxy);
-       if (!G_IS_OBJECT(spi_proxy))
-               spi_proxy = NULL;
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_gdbus_spi_open(peripheral_spi_h spi, int bus, int cs)
-{
-       int ret;
-       GError *error = NULL;
-       GUnixFDList *fd_list = NULL;
-
-       ret = __spi_proxy_init();
-       if (ret != PERIPHERAL_ERROR_NONE)
-               return ret;
-
-       if (peripheral_io_gdbus_spi_call_open_sync(
-                       spi_proxy,
-                       bus,
-                       cs,
-                       NULL,
-                       &spi->handle,
-                       &ret,
-                       &fd_list,
-                       NULL,
-                       &error) == FALSE) {
-               _E("Failed to request daemon to spi 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;
-
-       spi->fd = g_unix_fd_list_get(fd_list, SPI_FD_INDEX, &error);
-       if (spi->fd < 0) {
-               _E("Failed to get fd for spi : %s", error->message);
-               g_error_free(error);
-               ret = PERIPHERAL_ERROR_IO_ERROR;
-       }
-
-       g_object_unref(fd_list);
-
-       return ret;
-}
-
-int peripheral_gdbus_spi_close(peripheral_spi_h spi)
-{
-       RETVM_IF(spi_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Spi proxy is NULL");
-
-       int ret;
-       GError *error = NULL;
-
-       if (peripheral_io_gdbus_spi_call_close_sync(
-                       spi_proxy,
-                       spi->handle,
-                       &ret,
-                       NULL,
-                       &error) == FALSE) {
-               _E("Failed to request daemon to gpio spi : %s", error->message);
-               g_error_free(error);
-               return PERIPHERAL_ERROR_IO_ERROR;
-       }
-
-       __spi_proxy_deinit();
-
-       return ret;
-}
index 8b168e5..24dc78e 100644 (file)
  * 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_spi.h"
 #include "peripheral_interface_spi.h"
 #include "peripheral_log.h"
 
@@ -46,15 +49,26 @@ static bool __is_feature_supported(void)
        return (spi_feature == SPI_FEATURE_TRUE ? true : false);
 }
 
+static inline void cleanup_handlep(peripheral_spi_h *handle)
+{
+       if (*handle != NULL) {
+               if ((*handle)->fd != -1)
+                       close((*handle)->fd);
+               free(*handle);
+       }
+}
+
 int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
 {
-       peripheral_spi_h handle;
-       int ret = PERIPHERAL_ERROR_NONE;
+#define DEV_PATH_BASE "/dev/spidev"
+       char path[sizeof(DEV_PATH_BASE "1234567890.1234567890")] = {0, }; /* space for /dev/spidev%d.%d */
 
        RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
        RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi handle");
        RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
 
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_spi_h handle = NULL;
+
        /* Initialize */
        handle = (peripheral_spi_h)calloc(1, sizeof(struct _peripheral_spi_s));
        if (handle == NULL) {
@@ -62,35 +76,34 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
-       ret = peripheral_gdbus_spi_open(handle, bus, cs);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("SPI open error (%d, %d)", bus, cs);
-               free(handle);
-               handle = NULL;
+       snprintf(path, sizeof path, DEV_PATH_BASE "%d.%d", bus, cs);
+       handle->fd = open(path, O_RDWR | O_CLOEXEC);
+       CHECK_ERROR(handle->fd < 0);
+
+       if (flock(handle->fd, LOCK_EX | LOCK_NB)) {
+               if (errno == EWOULDBLOCK) {
+                       _E("bus : %d, cs : 0x%x is not available", bus, cs);
+                       return PERIPHERAL_ERROR_RESOURCE_BUSY;
+               } else {
+                       _E("bus : %d, cs : 0x%x flock() error: %d", bus, cs, errno);
+                       return PERIPHERAL_ERROR_IO_ERROR;
+               }
        }
 
        *spi = handle;
+       handle = NULL;
 
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
 
 int peripheral_spi_close(peripheral_spi_h spi)
 {
-       int ret = PERIPHERAL_ERROR_NONE;
-
        RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
        RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
 
-       ret = peripheral_gdbus_spi_close(spi);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to close SPI device, continuing anyway, ret : %d", ret);
-
-       peripheral_interface_spi_close(spi);
-
-       free(spi);
-       spi = NULL;
+       cleanup_handlep(&spi);
 
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
 
 int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)