Seperate files for util functions 33/130133/2
authorHyeongsik Min <hyeongsik.min@samsung.com>
Fri, 19 May 2017 07:42:57 +0000 (16:42 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Fri, 19 May 2017 11:17:47 +0000 (20:17 +0900)
Add peripheral_bus_util.c/.h and put util functions that can be used by
peripheral_bus and peripheral_bus_<interface>.

Change-Id: I6a2e643f322a3af01b0d52001d36802310d4fd4f
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
CMakeLists.txt
src/daemon/peripheral_bus.c
src/daemon/peripheral_bus_i2c.c
src/daemon/peripheral_bus_uart.c
src/daemon/peripheral_bus_uart.h
src/daemon/peripheral_bus_util.c [new file with mode: 0644]
src/daemon/peripheral_bus_util.h [new file with mode: 0644]

index a2d7b27..2d4a846 100644 (file)
@@ -21,6 +21,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/interface/include)
 SET(PERIPHERAL-BUS "peripheral-bus")
 SET(SRCS
        src/daemon/peripheral_bus.c
+       src/daemon/peripheral_bus_util.c
        src/daemon/peripheral_bus_pwm.c
        src/daemon/peripheral_bus_i2c.c
        src/daemon/peripheral_bus_gpio.c
index 3faf358..1728b79 100644 (file)
 #include "peripheral_bus_pwm.h"
 #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;
-}
+#include "peripheral_bus_util.h"
 
 gboolean handle_gpio_open(
                PeripheralIoGdbusGpio *gpio,
@@ -281,17 +245,20 @@ 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;
+       uint8_t err_buf[2] = {0, };
        const gchar *id;
 
        /* Handle validation */
        if (!i2c_handle || !i2c_handle->client_info.id) {
                _E("i2c handle is not valid");
+               data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
                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);
+               data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
                ret = PERIPHERAL_ERROR_INVALID_OPERATION;
                goto out;
        }
index 4def973..eb17dd6 100644 (file)
@@ -23,6 +23,7 @@
 #include "i2c.h"
 #include "peripheral_bus.h"
 #include "peripheral_common.h"
+#include "peripheral_bus_util.h"
 
 #define INITIAL_BUFFER_SIZE 128
 #define MAX_BUFFER_SIZE 8192
@@ -151,8 +152,8 @@ 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)
 {
-       GVariantBuilder *builder;
-       int ret, i;
+       uint8_t err_buf[2] = {0, };
+       int ret;
 
        /* Limit maximum length */
        if (length > MAX_BUFFER_SIZE) length = MAX_BUFFER_SIZE;
@@ -172,23 +173,12 @@ int peripheral_bus_i2c_read(pb_i2c_data_h i2c, int length, GVariant **data_array
        }
 
        ret = i2c_read(i2c->fd, i2c->buffer, length);
-
-       builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
-
-       for (i = 0; i < length; i++)
-               g_variant_builder_add(builder, "(y)", i2c->buffer[i]);
-       *data_array = g_variant_new("a(y)", builder);
-       g_variant_builder_unref(builder);
+       *data_array = peripheral_bus_build_variant_ay(i2c->buffer, length);
 
        return ret;
 
 out:
-       builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
-
-       for (i = 0; i < i2c->buffer_size; i++)
-               g_variant_builder_add(builder, "(y)", 0x0);
-       *data_array = g_variant_new("a(y)", builder);
-       g_variant_builder_unref(builder);
+       *data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
 
        return ret;
 }
index db057b1..cd81e6b 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_bus.h"
 #include "peripheral_common.h"
+#include "peripheral_bus_util.h"
 
 #define INITIAL_BUFFER_SIZE 128
 #define MAX_BUFFER_SIZE 8192
 
-GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length)
-{
-       GVariantBuilder *builder;
-       GVariant *variant;
-       int i;
-
-       if (data == NULL)
-               return NULL;
-
-       builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
-
-       for (i = 0; i < length; i++)
-               g_variant_builder_add(builder, "(y)", data[i]);
-
-       variant = g_variant_new("a(y)", builder);
-       g_variant_builder_unref(builder);
-
-       return variant;
-}
-
 static pb_uart_data_h peripheral_bus_uart_data_get(int port, GList **list)
 {
        GList *uart_list = *list;
index bdc7e16..fa710ae 100644 (file)
@@ -17,7 +17,6 @@
 #ifndef __PERIPHERAL_BUS_UART_H__
 #define __PERIPHERAL_BUS_UART_H__
 
-GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length);
 int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data);
 int peripheral_bus_uart_close(pb_uart_data_h uart, gpointer user_data);
 int peripheral_bus_uart_flush(pb_uart_data_h uart);
diff --git a/src/daemon/peripheral_bus_util.c b/src/daemon/peripheral_bus_util.c
new file mode 100644 (file)
index 0000000..921a64c
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 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 <stdint.h>
+#include <string.h>
+#include <gio/gio.h>
+
+#include "peripheral_bus.h"
+#include "peripheral_common.h"
+
+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;
+}
+
+GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length)
+{
+       GVariantBuilder *builder;
+       GVariant *variant;
+       int i;
+
+       if (data == NULL)
+               return NULL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
+
+       for (i = 0; i < length; i++)
+               g_variant_builder_add(builder, "(y)", data[i]);
+
+       variant = g_variant_new("a(y)", builder);
+       g_variant_builder_unref(builder);
+
+       return variant;
+}
diff --git a/src/daemon/peripheral_bus_util.h b/src/daemon/peripheral_bus_util.h
new file mode 100644 (file)
index 0000000..0aa49be
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 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_UTIL_H__
+#define __PERIPHERAL_UTIL_H__
+
+int peripheral_bus_get_client_info(GDBusMethodInvocation *invocation, peripheral_bus_s *pb_data, pb_client_info_s *client_info);
+GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length);
+
+#endif /* __PERIPHERAL_UTIL_H__ */