interface: move gpio_wait_for_udev() into peripheral_interface_gpio_open() 32/161332/1
authorSegwon <segwon.han@samsung.com>
Thu, 23 Nov 2017 01:30:37 +0000 (10:30 +0900)
committerSegwon <segwon.han@samsung.com>
Thu, 23 Nov 2017 01:30:37 +0000 (10:30 +0900)
Change-Id: Iaf6ca8c0d9ed435b312cf750ab7bcb579159c5c8
Signed-off-by: Segwon <segwon.han@samsung.com>
src/gdbus/peripheral_gdbus_i2c.c
src/handle/peripheral_handle_gpio.c
src/interface/peripheral_interface_gpio.c

index 05b29ce67231499ca7dcc353f29f82fda1b00560..a6358886487cb15e27cf08504b54f25ec25dd776 100644 (file)
@@ -23,7 +23,6 @@
 #include "peripheral_handle_i2c.h"
 #include "peripheral_gdbus_i2c.h"
 
-
 static void __i2c_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
index 474572f327fe59f2ce7a7d12f71932843079501e..f1d70bbe1ae93dd0843efbb423501547e5ae0a8e 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <gio/gio.h>
-#include <poll.h>
-#include <sys/time.h>
-#include <libudev.h>
-
 #include <peripheral_io.h>
 
 #include "peripheral_interface_gpio.h"
@@ -54,71 +50,6 @@ static bool peripheral_bus_gpio_is_available(int pin, peripheral_bus_s *pb_data)
        return true;
 }
 
-static int gpio_wait_for_udev(int gpiopin)
-{
-       #define GPIO_NAME_LEN 8
-       struct udev *udev;
-       struct udev_monitor *monitor;
-       struct udev_device *dev;
-       struct pollfd pfd;
-       char gpio_name[GPIO_NAME_LEN];
-       int ret = -EIO;
-
-       udev = udev_new();
-       if (!udev) {
-               _E("Cannot create udev");
-               return ret;
-       }
-
-       monitor = udev_monitor_new_from_netlink(udev, "udev");
-       if (!monitor) {
-               _E("Cannot create udev monitor");
-               udev_unref(udev);
-               return ret;
-       }
-
-       ret = udev_monitor_filter_add_match_subsystem_devtype(monitor, "gpio", NULL);
-       if (ret < 0) {
-               _E("Failed to add monitor filter");
-               goto out;
-       }
-
-       ret = udev_monitor_enable_receiving(monitor);
-       if (ret < 0) {
-               _E("Failed to enable udev receiving");
-               goto out;
-       }
-
-       pfd.fd = udev_monitor_get_fd(monitor);
-       pfd.events = POLLIN;
-
-       snprintf(gpio_name, GPIO_NAME_LEN, "gpio%d", gpiopin);
-
-       for (int cnt = 0; cnt < 10; cnt++) {
-               if (poll(&pfd, 1, 100) < 0) {
-                       _E("Failed to watch udev monitor");
-                       goto out;
-               }
-
-               dev = udev_monitor_receive_device(monitor);
-               if (dev) {
-                       if (strcmp(udev_device_get_sysname(dev), gpio_name) == 0) {
-                               _D("udev for %s is initialized", gpio_name);
-                               ret = 0;
-                               goto out;
-                       }
-               }
-       }
-       _E("Time out");
-
-out:
-       udev_monitor_unref(monitor);
-       udev_unref(udev);
-
-       return ret;
-}
-
-
 int peripheral_bus_gpio_open(gint pin, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
@@ -139,12 +70,6 @@ int peripheral_bus_gpio_open(gint pin, pb_data_h *handle, gpointer user_data)
                goto err;
        }
 
-       if (gpio_wait_for_udev(pin) < 0) {
-               _E("device nodes are not writable");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto err;
-       }
-
        gpio_handle->type = PERIPHERAL_BUS_TYPE_GPIO;
        gpio_handle->list = &pb_data->gpio_list;
        gpio_handle->dev.gpio.pin = pin;
index 6f05c3c1809d617a705ca889aa75dcfdad5421c4..d7b8e84b64240442c836400350086eccdb3f748f 100644 (file)
  * limitations under the License.
  */
 
+#include <libudev.h>
+#include <poll.h>
+
 #include "peripheral_interface_gpio.h"
 #include "peripheral_interface_common.h"
 
+static int __gpio_wait_for_udev(int pin)
+{
+       #define GPIO_NAME_LEN 8
+       struct udev *udev;
+       struct udev_monitor *monitor;
+       struct udev_device *dev;
+       struct pollfd pfd;
+       char gpio_name[GPIO_NAME_LEN];
+       int ret = -EIO;
+
+       udev = udev_new();
+       if (!udev) {
+               _E("Cannot create udev");
+               return ret;
+       }
+
+       monitor = udev_monitor_new_from_netlink(udev, "udev");
+       if (!monitor) {
+               _E("Cannot create udev monitor");
+               udev_unref(udev);
+               return ret;
+       }
+
+       ret = udev_monitor_filter_add_match_subsystem_devtype(monitor, "gpio", NULL);
+       if (ret < 0) {
+               _E("Failed to add monitor filter");
+               goto out;
+       }
+
+       ret = udev_monitor_enable_receiving(monitor);
+       if (ret < 0) {
+               _E("Failed to enable udev receiving");
+               goto out;
+       }
+
+       pfd.fd = udev_monitor_get_fd(monitor);
+       pfd.events = POLLIN;
+
+       snprintf(gpio_name, GPIO_NAME_LEN, "gpio%d", pin);
+
+       for (int cnt = 0; cnt < 10; cnt++) {
+               if (poll(&pfd, 1, 100) < 0) {
+                       _E("Failed to watch udev monitor");
+                       goto out;
+               }
+
+               dev = udev_monitor_receive_device(monitor);
+               if (dev) {
+                       if (strcmp(udev_device_get_sysname(dev), gpio_name) == 0) {
+                               _D("udev for %s is initialized", gpio_name);
+                               ret = 0;
+                               goto out;
+                       }
+               }
+       }
+       _E("Time out");
+
+out:
+       udev_monitor_unref(monitor);
+       udev_unref(udev);
+
+       return ret;
+}
+
 int peripheral_interface_gpio_open(int pin)
 {
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
@@ -36,6 +103,12 @@ int peripheral_interface_gpio_open(int pin)
        ret = close(fd);
        IF_ERROR_RETURN(ret != 0);
 
+       ret = __gpio_wait_for_udev(pin);
+       if (ret < 0) {
+               _E("device nodes are not writable");
+               return PERIPHERAL_ERROR_IO_ERROR;
+       }
+
        return PERIPHERAL_ERROR_NONE;
 }