Put client_info into i2c_handle 82/130082/3
authorHyeongsik Min <hyeongsik.min@samsung.com>
Fri, 19 May 2017 04:37:14 +0000 (13:37 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Fri, 19 May 2017 05:53:18 +0000 (14:53 +0900)
This patch put struct client_info in struct pb_data_h instead of pointer of it.

Change-Id: I2e971129337202895e730324a7baeab47fa73fda
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
src/daemon/peripheral_bus.c
src/daemon/peripheral_bus.h
src/daemon/peripheral_bus_i2c.c
src/daemon/peripheral_bus_i2c.h

index 06c206b..3faf358 100644 (file)
 #include "peripheral_bus_uart.h"
 #include "peripheral_common.h"
 
+static int peripheral_bus_get_client_info(GDBusMethodInvocation *invocation, peripheral_bus_s *pb_data, pb_client_info_s *client_info)
+{
+       guint pid = 0;
+       GError *error = NULL;
+       GVariant *_ret;
+       const gchar *id;
+
+       id = g_dbus_method_invocation_get_sender(invocation);
+       _ret = g_dbus_connection_call_sync(pb_data->connection,
+               "org.freedesktop.DBus",
+               "/org/freedesktop/DBus",
+               "org.freedesktop.DBus",
+               "GetConnectionUnixProcessID",
+               g_variant_new("(s)", id),
+               NULL,
+               G_DBUS_CALL_FLAGS_NONE,
+               -1,
+               NULL,
+               &error);
+
+       if (_ret == NULL) {
+               _E("Failed to get client pid, %s", error->message);
+               g_error_free(error);
+
+               return -1;
+       }
+
+       g_variant_get(_ret, "(u)", &pid);
+       g_variant_unref(_ret);
+
+       client_info->pid = (pid_t)pid;
+       client_info->pgid = getpgid(pid);
+       client_info->id = strdup(id);
+
+       return 0;
+}
+
 gboolean handle_gpio_open(
                PeripheralIoGdbusGpio *gpio,
                GDBusMethodInvocation *invocation,
@@ -185,10 +222,19 @@ gboolean handle_i2c_open(
                gint address,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
        pb_i2c_data_h i2c_handle;
 
-       ret = peripheral_bus_i2c_open(invocation, bus, address, &i2c_handle, user_data);
+       ret = peripheral_bus_i2c_open(bus, address, &i2c_handle, user_data);
+
+       if (ret == PERIPHERAL_ERROR_NONE) {
+               if (peripheral_bus_get_client_info(invocation, pb_data, &i2c_handle->client_info) == 0)
+                       _D("bus : %d, address : %d, id = %s", bus, address, i2c_handle->client_info.id);
+               else
+                       ret = PERIPHERAL_ERROR_UNKNOWN;
+       }
+
        peripheral_io_gdbus_i2c_complete_open(i2c, invocation, GPOINTER_TO_UINT(i2c_handle), ret);
 
        return true;
@@ -202,8 +248,24 @@ gboolean handle_i2c_close(
 {
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
        pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
+       const gchar *id;
+
+       /* Handle validation */
+       if (!i2c_handle || !i2c_handle->client_info.id) {
+               _E("i2c handle is not valid");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+               goto out;
+       }
+       id = g_dbus_method_invocation_get_sender(invocation);
+       if (strcmp(i2c_handle->client_info.id, id)) {
+               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
+               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
+               goto out;
+       }
 
-       ret = peripheral_bus_i2c_close(invocation, i2c_handle, user_data);
+       ret = peripheral_bus_i2c_close(i2c_handle, user_data);
+
+out:
        peripheral_io_gdbus_i2c_complete_close(i2c, invocation, ret);
 
        return true;
@@ -219,8 +281,24 @@ gboolean handle_i2c_read(
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
        pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
        GVariant *data_array = NULL;
+       const gchar *id;
+
+       /* Handle validation */
+       if (!i2c_handle || !i2c_handle->client_info.id) {
+               _E("i2c handle is not valid");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+               goto out;
+       }
+       id = g_dbus_method_invocation_get_sender(invocation);
+       if (strcmp(i2c_handle->client_info.id, id)) {
+               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
+               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
+               goto out;
+       }
+
+       ret = peripheral_bus_i2c_read(i2c_handle, length, &data_array);
 
-       ret = peripheral_bus_i2c_read(invocation, i2c_handle, length, &data_array, user_data);
+out:
        peripheral_io_gdbus_i2c_complete_read(i2c, invocation, data_array, ret);
 
        return true;
@@ -236,8 +314,24 @@ gboolean handle_i2c_write(
 {
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
        pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
+       const gchar *id;
 
-       ret = peripheral_bus_i2c_write(invocation, i2c_handle, length, data_array, user_data);
+       /* Handle validation */
+       if (!i2c_handle || !i2c_handle->client_info.id) {
+               _E("i2c handle is not valid");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+               goto out;
+       }
+       id = g_dbus_method_invocation_get_sender(invocation);
+       if (strcmp(i2c_handle->client_info.id, id)) {
+               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
+               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
+               goto out;
+       }
+
+       ret = peripheral_bus_i2c_write(i2c_handle, length, data_array);
+
+out:
        peripheral_io_gdbus_i2c_complete_write(i2c, invocation, ret);
 
        return true;
@@ -255,36 +349,12 @@ gboolean handle_pwm_open(
        pb_pwm_data_h pwm_handle;
 
        ret = peripheral_bus_pwm_open(device, channel, &pwm_handle, user_data);
-       if (ret == PERIPHERAL_ERROR_NONE) {
-               guint pid = 0;
-               GError *error = NULL;
-               GVariant *_ret;
-               const gchar *id;
 
-               id = g_dbus_method_invocation_get_sender(invocation);
-               _ret = g_dbus_connection_call_sync(pb_data->connection,
-                       "org.freedesktop.DBus",
-                       "/org/freedesktop/DBus",
-                       "org.freedesktop.DBus",
-                       "GetConnectionUnixProcessID",
-                       g_variant_new("(s)", id),
-                       NULL,
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &error);
-
-               if (_ret != NULL) {
-                       g_variant_get(_ret, "(u)", &pid);
-                       g_variant_unref(_ret);
-               } else
-                       g_error_free(error);
-
-               pwm_handle->client_info.pid = (pid_t)pid;
-               pwm_handle->client_info.pgid = getpgid(pid);
-               pwm_handle->client_info.id = strdup(id);
-
-               _D("device : %d, channel : %d, id = %s", device, channel, pwm_handle->client_info.id);
+       if (ret == PERIPHERAL_ERROR_NONE) {
+               if (peripheral_bus_get_client_info(invocation, pb_data, &pwm_handle->client_info) == 0)
+                       _D("device : %d, channel : %d, id = %s", device, channel, pwm_handle->client_info.id);
+               else
+                       ret = PERIPHERAL_ERROR_UNKNOWN;
        }
 
        peripheral_io_gdbus_pwm_complete_open(pwm, invocation, GPOINTER_TO_UINT(pwm_handle), ret);
index 8b74484..1918034 100644 (file)
@@ -66,7 +66,7 @@ typedef struct {
        uint8_t *buffer;
        int buffer_size;
        /* client info */
-       pb_client_info_s *client_info;
+       pb_client_info_s client_info;
 } peripheral_bus_i2c_data_s;
 
 typedef struct {
index c9dce59..4def973 100644 (file)
 #define INITIAL_BUFFER_SIZE 128
 #define MAX_BUFFER_SIZE 8192
 
-static pb_client_info_s* peripheral_bus_i2c_get_client_info(GDBusMethodInvocation *invocation, peripheral_bus_s *pb_data)
-{
-       pb_client_info_s *client_info;
-       guint pid = 0;
-       GError *error = NULL;
-       GVariant *_ret;
-       const gchar *id;
-
-       client_info = (pb_client_info_s*)calloc(1, sizeof(pb_client_info_s));
-
-       id = g_dbus_method_invocation_get_sender(invocation);
-
-       _ret = g_dbus_connection_call_sync(pb_data->connection,
-               "org.freedesktop.DBus",
-               "/org/freedesktop/DBus",
-               "org.freedesktop.DBus",
-               "GetConnectionUnixProcessID",
-               g_variant_new("(s)", id),
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               &error);
-
-       if (_ret != NULL) {
-               g_variant_get(_ret, "(u)", &pid);
-               g_variant_unref(_ret);
-       }
-       g_error_free(error);
-
-       client_info->pid = (pid_t)pid;
-       client_info->pgid = getpgid(pid);
-       client_info->id = strdup(id);
-
-       return client_info;
-}
-
 static pb_i2c_data_h peripheral_bus_i2c_data_get(int bus, int address, GList **list)
 {
        GList *i2c_list = *list;
@@ -115,10 +78,6 @@ static int peripheral_bus_i2c_data_free(pb_i2c_data_h i2c_handle, GList **list)
 
                if (i2c == i2c_handle) {
                        *list = g_list_remove_link(i2c_list, link);
-                       if (i2c->client_info) {
-                               free(i2c->client_info->id);
-                               free(i2c->client_info);
-                       }
                        if (i2c->buffer)
                                free(i2c->buffer);
                        free(i2c);
@@ -131,7 +90,7 @@ static int peripheral_bus_i2c_data_free(pb_i2c_data_h i2c_handle, GList **list)
        return -1;
 }
 
-int peripheral_bus_i2c_open(GDBusMethodInvocation *invocation, int bus, int address, pb_i2c_data_h *i2c, gpointer user_data)
+int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        pb_i2c_data_h i2c_handle;
@@ -156,9 +115,8 @@ int peripheral_bus_i2c_open(GDBusMethodInvocation *invocation, int bus, int addr
        i2c_handle->fd = fd;
        i2c_handle->bus = bus;
        i2c_handle->address = address;
-       i2c_handle->client_info = peripheral_bus_i2c_get_client_info(invocation, pb_data);
-
        i2c_handle->buffer = malloc(INITIAL_BUFFER_SIZE);
+
        if (!(i2c_handle->buffer)) {
                i2c_close(fd);
                _E("Failed to allocate data buffer");
@@ -169,22 +127,32 @@ int peripheral_bus_i2c_open(GDBusMethodInvocation *invocation, int bus, int addr
        *i2c = i2c_handle;
 
        _D("bus : %d, address : %d, pgid = %d, id = %s", bus, address,
-               i2c_handle->client_info->pgid,
-               i2c_handle->client_info->id);
+               i2c_handle->client_info.pgid,
+               i2c_handle->client_info.id);
 
        return ret;
 }
 
-int peripheral_bus_i2c_read(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, int length, GVariant **data_array, gpointer user_data)
+int peripheral_bus_i2c_close(pb_i2c_data_h i2c, gpointer user_data)
 {
-       const gchar *id;
-       GVariantBuilder *builder;
-       int ret, i;
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
+       int ret;
 
-       /* Handle validation */
-       RETVM_IF(!i2c || !(i2c->client_info), PERIPHERAL_ERROR_UNKNOWN, "i2c handle is not valid");
+       _D("bus : %d, address : %d, pgid = %d", i2c->bus, i2c->address, i2c->client_info.pgid);
 
-       id = g_dbus_method_invocation_get_sender(invocation);
+       if ((ret = i2c_close(i2c->fd)) < 0)
+               return ret;
+
+       if (peripheral_bus_i2c_data_free(i2c, &pb_data->i2c_list) < 0)
+               _E("Failed to free i2c data");
+
+       return ret;
+}
+
+int peripheral_bus_i2c_read(pb_i2c_data_h i2c, int length, GVariant **data_array)
+{
+       GVariantBuilder *builder;
+       int ret, i;
 
        /* Limit maximum length */
        if (length > MAX_BUFFER_SIZE) length = MAX_BUFFER_SIZE;
@@ -203,12 +171,6 @@ int peripheral_bus_i2c_read(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c
                i2c->buffer_size = length;
        }
 
-       if (strcmp(i2c->client_info->id, id)) {
-               _E("Invalid access, handie id : %s, current id : %s", i2c->client_info->id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
        ret = i2c_read(i2c->fd, i2c->buffer, length);
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
@@ -231,23 +193,12 @@ out:
        return ret;
 }
 
-int peripheral_bus_i2c_write(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, int length, GVariant *data_array, gpointer user_data)
+int peripheral_bus_i2c_write(pb_i2c_data_h i2c, int length, GVariant *data_array)
 {
-       const gchar *id;
        GVariantIter *iter;
        guchar str;
        int i = 0;
 
-       /* Handle validation */
-       RETVM_IF(!i2c || !(i2c->client_info), PERIPHERAL_ERROR_UNKNOWN, "i2c handle is not valid");
-
-       id = g_dbus_method_invocation_get_sender(invocation);
-
-       RETVM_IF(strcmp(i2c->client_info->id, id),
-               PERIPHERAL_ERROR_INVALID_OPERATION,
-               "Invalid access, handie id : %s, current id : %s",
-               i2c->client_info->id, id);
-
        /* Limit maximum length */
        if (length > MAX_BUFFER_SIZE) length = MAX_BUFFER_SIZE;
 
@@ -262,37 +213,9 @@ int peripheral_bus_i2c_write(GDBusMethodInvocation *invocation, pb_i2c_data_h i2
        }
 
        g_variant_get(data_array, "a(y)", &iter);
-       while (g_variant_iter_loop(iter, "(y)", &str) && i < length) {
+       while (g_variant_iter_loop(iter, "(y)", &str) && i < length)
                i2c->buffer[i++] = str;
-       }
        g_variant_iter_free(iter);
 
        return i2c_write(i2c->fd, i2c->buffer, length);
 }
-
-int peripheral_bus_i2c_close(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, gpointer user_data)
-{
-       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       const gchar *id;
-       int ret;
-
-       /* Handle validation */
-       RETVM_IF(!i2c || !(i2c->client_info), PERIPHERAL_ERROR_UNKNOWN, "i2c handle is not valid");
-
-       _D("bus : %d, address : %d, pgid = %d", i2c->bus, i2c->address, i2c->client_info->pgid);
-
-       id = g_dbus_method_invocation_get_sender(invocation);
-
-       RETVM_IF(strcmp(i2c->client_info->id, id),
-               PERIPHERAL_ERROR_INVALID_OPERATION,
-               "Invalid access, handle id : %s, current id : %s",
-               i2c->client_info->id, id);
-
-       if ((ret = i2c_close(i2c->fd)) < 0)
-               return ret;
-
-       if (peripheral_bus_i2c_data_free(i2c, &pb_data->i2c_list) < 0)
-               _E("Failed to free i2c data");
-
-       return ret;
-}
index c52bd39..350a53a 100644 (file)
@@ -17,9 +17,9 @@
 #ifndef __PERIPHERAL_BUS_I2C_H__
 #define __PERIPHERAL_BUS_I2C_H__
 
-int peripheral_bus_i2c_open(GDBusMethodInvocation *invocation, int bus, int address, pb_i2c_data_h *i2c, gpointer user_data);
-int peripheral_bus_i2c_read(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, int length, GVariant **data_array, gpointer user_data);
-int peripheral_bus_i2c_write(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, int length, GVariant *data_array, gpointer user_data);
-int peripheral_bus_i2c_close(GDBusMethodInvocation *invocation, pb_i2c_data_h i2c, gpointer user_data);
+int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer user_data);
+int peripheral_bus_i2c_close(pb_i2c_data_h i2c, gpointer user_data);
+int peripheral_bus_i2c_read(pb_i2c_data_h i2c, int length, GVariant **data_array);
+int peripheral_bus_i2c_write(pb_i2c_data_h i2c, int length, GVariant *data_array);
 
 #endif /* __PERIPHERAL_BUS_I2C_H__ */